2 * hostapd / ubus support
3 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "utils/eloop.h"
12 #include "utils/wpabuf.h"
13 #include "common/ieee802_11_defs.h"
15 #include "neighbor_db.h"
16 #include "wps_hostapd.h"
19 #include "ap_drv_ops.h"
25 static struct ubus_context
*ctx
;
26 static struct blob_buf b
;
29 static inline struct hapd_interfaces
*get_hapd_interfaces_from_object(struct ubus_object
*obj
)
31 return container_of(obj
, struct hapd_interfaces
, ubus
);
34 static inline struct hostapd_data
*get_hapd_from_object(struct ubus_object
*obj
)
36 return container_of(obj
, struct hostapd_data
, ubus
.obj
);
39 struct ubus_banned_client
{
44 static void ubus_receive(int sock
, void *eloop_ctx
, void *sock_ctx
)
46 struct ubus_context
*ctx
= eloop_ctx
;
47 ubus_handle_event(ctx
);
50 static void ubus_reconnect_timeout(void *eloop_data
, void *user_ctx
)
52 if (ubus_reconnect(ctx
, NULL
)) {
53 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
57 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
60 static void hostapd_ubus_connection_lost(struct ubus_context
*ctx
)
62 eloop_unregister_read_sock(ctx
->sock
.fd
);
63 eloop_register_timeout(1, 0, ubus_reconnect_timeout
, ctx
, NULL
);
66 static bool hostapd_ubus_init(void)
71 ctx
= ubus_connect(NULL
);
75 ctx
->connection_lost
= hostapd_ubus_connection_lost
;
76 eloop_register_read_sock(ctx
->sock
.fd
, ubus_receive
, ctx
, NULL
);
80 static void hostapd_ubus_ref_inc(void)
85 static void hostapd_ubus_ref_dec(void)
94 eloop_unregister_read_sock(ctx
->sock
.fd
);
99 void hostapd_ubus_add_iface(struct hostapd_iface
*iface
)
101 if (!hostapd_ubus_init())
105 void hostapd_ubus_free_iface(struct hostapd_iface
*iface
)
111 static void hostapd_send_ubus_event(char *bssname
, char *event
)
118 if (asprintf(&name
, "hostapd.%s.%s", bssname
, event
) < 0)
121 blob_buf_init(&b
, 0);
122 ubus_send_event(ctx
, name
, b
.head
);
127 hostapd_bss_del_ban(void *eloop_data
, void *user_ctx
)
129 struct ubus_banned_client
*ban
= eloop_data
;
130 struct hostapd_data
*hapd
= user_ctx
;
132 avl_delete(&hapd
->ubus
.banned
, &ban
->avl
);
137 hostapd_bss_ban_client(struct hostapd_data
*hapd
, u8
*addr
, int time
)
139 struct ubus_banned_client
*ban
;
144 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
149 ban
= os_zalloc(sizeof(*ban
));
150 memcpy(ban
->addr
, addr
, sizeof(ban
->addr
));
151 ban
->avl
.key
= ban
->addr
;
152 avl_insert(&hapd
->ubus
.banned
, &ban
->avl
);
154 eloop_cancel_timeout(hostapd_bss_del_ban
, ban
, hapd
);
156 hostapd_bss_del_ban(ban
, hapd
);
161 eloop_register_timeout(0, time
* 1000, hostapd_bss_del_ban
, ban
, hapd
);
165 hostapd_bss_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
166 struct ubus_request_data
*req
, const char *method
,
167 struct blob_attr
*msg
)
169 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
170 int ret
= hostapd_reload_config(hapd
->iface
, 1);
172 hostapd_send_ubus_event(hapd
->conf
->iface
, "reload");
177 hostapd_bss_get_clients(struct ubus_context
*ctx
, struct ubus_object
*obj
,
178 struct ubus_request_data
*req
, const char *method
,
179 struct blob_attr
*msg
)
181 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
182 struct sta_info
*sta
;
185 static const struct {
189 { "auth", WLAN_STA_AUTH
},
190 { "assoc", WLAN_STA_ASSOC
},
191 { "authorized", WLAN_STA_AUTHORIZED
},
192 { "preauth", WLAN_STA_PREAUTH
},
193 { "wds", WLAN_STA_WDS
},
194 { "wmm", WLAN_STA_WMM
},
195 { "ht", WLAN_STA_HT
},
196 { "vht", WLAN_STA_VHT
},
197 { "wps", WLAN_STA_WPS
},
198 { "mfp", WLAN_STA_MFP
},
201 blob_buf_init(&b
, 0);
202 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
203 list
= blobmsg_open_table(&b
, "clients");
204 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
208 sprintf(mac_buf
, MACSTR
, MAC2STR(sta
->addr
));
209 c
= blobmsg_open_table(&b
, mac_buf
);
210 for (i
= 0; i
< ARRAY_SIZE(sta_flags
); i
++)
211 blobmsg_add_u8(&b
, sta_flags
[i
].name
,
212 !!(sta
->flags
& sta_flags
[i
].flag
));
214 r
= blobmsg_open_array(&b
, "rrm");
215 for (i
= 0; i
< ARRAY_SIZE(sta
->rrm_enabled_capa
); i
++)
216 blobmsg_add_u32(&b
, "", sta
->rrm_enabled_capa
[i
]);
217 blobmsg_close_array(&b
, r
);
218 blobmsg_add_u32(&b
, "aid", sta
->aid
);
219 #ifdef CONFIG_TAXONOMY
220 r
= blobmsg_alloc_string_buffer(&b
, "signature", 1024);
221 if (retrieve_sta_taxonomy(hapd
, sta
, r
, 1024) > 0)
222 blobmsg_add_string_buffer(&b
);
224 blobmsg_close_table(&b
, c
);
226 blobmsg_close_array(&b
, list
);
227 ubus_send_reply(ctx
, req
, b
.head
);
233 hostapd_bss_get_features(struct ubus_context
*ctx
, struct ubus_object
*obj
,
234 struct ubus_request_data
*req
, const char *method
,
235 struct blob_attr
*msg
)
237 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
239 blob_buf_init(&b
, 0);
240 blobmsg_add_u8(&b
, "ht_supported", ht_supported(hapd
->iface
->hw_features
));
241 blobmsg_add_u8(&b
, "vht_supported", vht_supported(hapd
->iface
->hw_features
));
242 ubus_send_reply(ctx
, req
, b
.head
);
252 static const struct blobmsg_policy notify_policy
[__NOTIFY_MAX
] = {
253 [NOTIFY_RESPONSE
] = { "notify_response", BLOBMSG_TYPE_INT32
},
257 hostapd_notify_response(struct ubus_context
*ctx
, struct ubus_object
*obj
,
258 struct ubus_request_data
*req
, const char *method
,
259 struct blob_attr
*msg
)
261 struct blob_attr
*tb
[__NOTIFY_MAX
];
262 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
263 struct wpabuf
*elems
;
267 blobmsg_parse(notify_policy
, __NOTIFY_MAX
, tb
,
268 blob_data(msg
), blob_len(msg
));
270 if (!tb
[NOTIFY_RESPONSE
])
271 return UBUS_STATUS_INVALID_ARGUMENT
;
273 hapd
->ubus
.notify_response
= blobmsg_get_u32(tb
[NOTIFY_RESPONSE
]);
275 return UBUS_STATUS_OK
;
286 static const struct blobmsg_policy del_policy
[__DEL_CLIENT_MAX
] = {
287 [DEL_CLIENT_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
288 [DEL_CLIENT_REASON
] = { "reason", BLOBMSG_TYPE_INT32
},
289 [DEL_CLIENT_DEAUTH
] = { "deauth", BLOBMSG_TYPE_INT8
},
290 [DEL_CLIENT_BAN_TIME
] = { "ban_time", BLOBMSG_TYPE_INT32
},
294 hostapd_bss_del_client(struct ubus_context
*ctx
, struct ubus_object
*obj
,
295 struct ubus_request_data
*req
, const char *method
,
296 struct blob_attr
*msg
)
298 struct blob_attr
*tb
[__DEL_CLIENT_MAX
];
299 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
300 struct sta_info
*sta
;
305 blobmsg_parse(del_policy
, __DEL_CLIENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
307 if (!tb
[DEL_CLIENT_ADDR
])
308 return UBUS_STATUS_INVALID_ARGUMENT
;
310 if (hwaddr_aton(blobmsg_data(tb
[DEL_CLIENT_ADDR
]), addr
))
311 return UBUS_STATUS_INVALID_ARGUMENT
;
313 if (tb
[DEL_CLIENT_REASON
])
314 reason
= blobmsg_get_u32(tb
[DEL_CLIENT_REASON
]);
316 if (tb
[DEL_CLIENT_DEAUTH
])
317 deauth
= blobmsg_get_bool(tb
[DEL_CLIENT_DEAUTH
]);
319 sta
= ap_get_sta(hapd
, addr
);
322 hostapd_drv_sta_deauth(hapd
, addr
, reason
);
323 ap_sta_deauthenticate(hapd
, sta
, reason
);
325 hostapd_drv_sta_disassoc(hapd
, addr
, reason
);
326 ap_sta_disassociate(hapd
, sta
, reason
);
330 if (tb
[DEL_CLIENT_BAN_TIME
])
331 hostapd_bss_ban_client(hapd
, addr
, blobmsg_get_u32(tb
[DEL_CLIENT_BAN_TIME
]));
337 blobmsg_add_macaddr(struct blob_buf
*buf
, const char *name
, const u8
*addr
)
341 s
= blobmsg_alloc_string_buffer(buf
, name
, 20);
342 sprintf(s
, MACSTR
, MAC2STR(addr
));
343 blobmsg_add_string_buffer(buf
);
347 hostapd_bss_list_bans(struct ubus_context
*ctx
, struct ubus_object
*obj
,
348 struct ubus_request_data
*req
, const char *method
,
349 struct blob_attr
*msg
)
351 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
352 struct ubus_banned_client
*ban
;
355 blob_buf_init(&b
, 0);
356 c
= blobmsg_open_array(&b
, "clients");
357 avl_for_each_element(&hapd
->ubus
.banned
, ban
, avl
)
358 blobmsg_add_macaddr(&b
, NULL
, ban
->addr
);
359 blobmsg_close_array(&b
, c
);
360 ubus_send_reply(ctx
, req
, b
.head
);
366 hostapd_bss_wps_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
367 struct ubus_request_data
*req
, const char *method
,
368 struct blob_attr
*msg
)
371 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
373 rc
= hostapd_wps_button_pushed(hapd
, NULL
);
376 return UBUS_STATUS_NOT_SUPPORTED
;
382 hostapd_bss_wps_cancel(struct ubus_context
*ctx
, struct ubus_object
*obj
,
383 struct ubus_request_data
*req
, const char *method
,
384 struct blob_attr
*msg
)
387 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
389 rc
= hostapd_wps_cancel(hapd
);
392 return UBUS_STATUS_NOT_SUPPORTED
;
398 hostapd_bss_update_beacon(struct ubus_context
*ctx
, struct ubus_object
*obj
,
399 struct ubus_request_data
*req
, const char *method
,
400 struct blob_attr
*msg
)
403 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
405 rc
= ieee802_11_set_beacon(hapd
);
408 return UBUS_STATUS_NOT_SUPPORTED
;
419 static const struct blobmsg_policy config_add_policy
[__CONFIG_MAX
] = {
420 [CONFIG_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
421 [CONFIG_FILE
] = { "config", BLOBMSG_TYPE_STRING
},
425 hostapd_config_add(struct ubus_context
*ctx
, struct ubus_object
*obj
,
426 struct ubus_request_data
*req
, const char *method
,
427 struct blob_attr
*msg
)
429 struct blob_attr
*tb
[__CONFIG_MAX
];
430 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
433 blobmsg_parse(config_add_policy
, __CONFIG_MAX
, tb
, blob_data(msg
), blob_len(msg
));
435 if (!tb
[CONFIG_FILE
] || !tb
[CONFIG_IFACE
])
436 return UBUS_STATUS_INVALID_ARGUMENT
;
438 snprintf(buf
, sizeof(buf
), "bss_config=%s:%s",
439 blobmsg_get_string(tb
[CONFIG_IFACE
]),
440 blobmsg_get_string(tb
[CONFIG_FILE
]));
442 if (hostapd_add_iface(interfaces
, buf
))
443 return UBUS_STATUS_INVALID_ARGUMENT
;
445 return UBUS_STATUS_OK
;
453 static const struct blobmsg_policy config_remove_policy
[__CONFIG_REM_MAX
] = {
454 [CONFIG_REM_IFACE
] = { "iface", BLOBMSG_TYPE_STRING
},
458 hostapd_config_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
459 struct ubus_request_data
*req
, const char *method
,
460 struct blob_attr
*msg
)
462 struct blob_attr
*tb
[__CONFIG_REM_MAX
];
463 struct hapd_interfaces
*interfaces
= get_hapd_interfaces_from_object(obj
);
466 blobmsg_parse(config_remove_policy
, __CONFIG_REM_MAX
, tb
, blob_data(msg
), blob_len(msg
));
468 if (!tb
[CONFIG_REM_IFACE
])
469 return UBUS_STATUS_INVALID_ARGUMENT
;
471 if (hostapd_remove_iface(interfaces
, blobmsg_get_string(tb
[CONFIG_REM_IFACE
])))
472 return UBUS_STATUS_INVALID_ARGUMENT
;
474 return UBUS_STATUS_OK
;
483 CSA_SEC_CHANNEL_OFFSET
,
490 static const struct blobmsg_policy csa_policy
[__CSA_MAX
] = {
491 [CSA_FREQ
] = { "freq", BLOBMSG_TYPE_INT32
},
492 [CSA_BCN_COUNT
] = { "bcn_count", BLOBMSG_TYPE_INT32
},
493 [CSA_CENTER_FREQ1
] = { "center_freq1", BLOBMSG_TYPE_INT32
},
494 [CSA_CENTER_FREQ2
] = { "center_freq2", BLOBMSG_TYPE_INT32
},
495 [CSA_BANDWIDTH
] = { "bandwidth", BLOBMSG_TYPE_INT32
},
496 [CSA_SEC_CHANNEL_OFFSET
] = { "sec_channel_offset", BLOBMSG_TYPE_INT32
},
497 [CSA_HT
] = { "ht", BLOBMSG_TYPE_BOOL
},
498 [CSA_VHT
] = { "vht", BLOBMSG_TYPE_BOOL
},
499 [CSA_BLOCK_TX
] = { "block_tx", BLOBMSG_TYPE_BOOL
},
504 hostapd_switch_chan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
505 struct ubus_request_data
*req
, const char *method
,
506 struct blob_attr
*msg
)
508 struct blob_attr
*tb
[__CSA_MAX
];
509 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
510 struct csa_settings css
;
512 blobmsg_parse(csa_policy
, __CSA_MAX
, tb
, blob_data(msg
), blob_len(msg
));
515 return UBUS_STATUS_INVALID_ARGUMENT
;
517 memset(&css
, 0, sizeof(css
));
518 css
.freq_params
.freq
= blobmsg_get_u32(tb
[CSA_FREQ
]);
520 #define SET_CSA_SETTING(name, field, type) \
523 css.field = blobmsg_get_ ## type(tb[name]); \
526 SET_CSA_SETTING(CSA_BCN_COUNT
, cs_count
, u32
);
527 SET_CSA_SETTING(CSA_CENTER_FREQ1
, freq_params
.center_freq1
, u32
);
528 SET_CSA_SETTING(CSA_CENTER_FREQ2
, freq_params
.center_freq2
, u32
);
529 SET_CSA_SETTING(CSA_BANDWIDTH
, freq_params
.bandwidth
, u32
);
530 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET
, freq_params
.sec_channel_offset
, u32
);
531 SET_CSA_SETTING(CSA_HT
, freq_params
.ht_enabled
, bool);
532 SET_CSA_SETTING(CSA_VHT
, freq_params
.vht_enabled
, bool);
533 SET_CSA_SETTING(CSA_BLOCK_TX
, block_tx
, bool);
536 if (hostapd_switch_channel(hapd
, &css
) != 0)
537 return UBUS_STATUS_NOT_SUPPORTED
;
538 return UBUS_STATUS_OK
;
539 #undef SET_CSA_SETTING
545 __VENDOR_ELEMENTS_MAX
548 static const struct blobmsg_policy ve_policy
[__VENDOR_ELEMENTS_MAX
] = {
549 /* vendor elements are provided as hex-string */
550 [VENDOR_ELEMENTS
] = { "vendor_elements", BLOBMSG_TYPE_STRING
},
554 hostapd_vendor_elements(struct ubus_context
*ctx
, struct ubus_object
*obj
,
555 struct ubus_request_data
*req
, const char *method
,
556 struct blob_attr
*msg
)
558 struct blob_attr
*tb
[__VENDOR_ELEMENTS_MAX
];
559 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
560 struct hostapd_bss_config
*bss
= hapd
->conf
;
561 struct wpabuf
*elems
;
565 blobmsg_parse(ve_policy
, __VENDOR_ELEMENTS_MAX
, tb
,
566 blob_data(msg
), blob_len(msg
));
568 if (!tb
[VENDOR_ELEMENTS
])
569 return UBUS_STATUS_INVALID_ARGUMENT
;
571 pos
= blobmsg_data(tb
[VENDOR_ELEMENTS
]);
572 len
= os_strlen(pos
);
574 return UBUS_STATUS_INVALID_ARGUMENT
;
578 wpabuf_free(bss
->vendor_elements
);
579 bss
->vendor_elements
= NULL
;
583 elems
= wpabuf_alloc(len
);
587 if (hexstr2bin(pos
, wpabuf_put(elems
, len
), len
)) {
589 return UBUS_STATUS_INVALID_ARGUMENT
;
592 wpabuf_free(bss
->vendor_elements
);
593 bss
->vendor_elements
= elems
;
595 /* update beacons if vendor elements were set successfully */
596 if (ieee802_11_update_beacons(hapd
->iface
) != 0)
597 return UBUS_STATUS_NOT_SUPPORTED
;
598 return UBUS_STATUS_OK
;
602 hostapd_rrm_print_nr(struct hostapd_neighbor_entry
*nr
)
608 blobmsg_printf(&b
, "", MACSTR
, MAC2STR(nr
->bssid
));
610 str
= blobmsg_alloc_string_buffer(&b
, "", nr
->ssid
.ssid_len
+ 1);
611 memcpy(str
, nr
->ssid
.ssid
, nr
->ssid
.ssid_len
);
612 str
[nr
->ssid
.ssid_len
] = 0;
613 blobmsg_add_string_buffer(&b
);
615 len
= wpabuf_len(nr
->nr
);
616 str
= blobmsg_alloc_string_buffer(&b
, "", 2 * len
+ 1);
617 wpa_snprintf_hex(str
, 2 * len
+ 1, wpabuf_head_u8(nr
->nr
), len
);
618 blobmsg_add_string_buffer(&b
);
622 BSS_MGMT_EN_NEIGHBOR
,
625 BSS_MGMT_EN_BSS_TRANSITION
,
631 __hostapd_bss_mgmt_enable_f(struct hostapd_data
*hapd
, int flag
)
633 struct hostapd_bss_config
*bss
= hapd
->conf
;
637 case BSS_MGMT_EN_NEIGHBOR
:
638 if (bss
->radio_measurements
[0] &
639 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)
642 bss
->radio_measurements
[0] |=
643 WLAN_RRM_CAPS_NEIGHBOR_REPORT
;
644 hostapd_neighbor_set_own_report(hapd
);
646 case BSS_MGMT_EN_BEACON
:
647 flags
= WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE
|
648 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE
|
649 WLAN_RRM_CAPS_BEACON_REPORT_TABLE
;
651 if (bss
->radio_measurements
[0] & flags
== flags
)
654 bss
->radio_measurements
[0] |= (u8
) flags
;
657 case BSS_MGMT_EN_BSS_TRANSITION
:
658 if (bss
->bss_transition
)
661 bss
->bss_transition
= 1;
668 __hostapd_bss_mgmt_enable(struct hostapd_data
*hapd
, uint32_t flags
)
673 for (i
= 0; i
< __BSS_MGMT_EN_MAX
; i
++) {
674 if (!(flags
& (1 << i
)))
677 update
|= __hostapd_bss_mgmt_enable_f(hapd
, i
);
681 ieee802_11_update_beacons(hapd
->iface
);
685 static const struct blobmsg_policy bss_mgmt_enable_policy
[__BSS_MGMT_EN_MAX
] = {
686 [BSS_MGMT_EN_NEIGHBOR
] = { "neighbor_report", BLOBMSG_TYPE_BOOL
},
687 [BSS_MGMT_EN_BEACON
] = { "beacon_report", BLOBMSG_TYPE_BOOL
},
689 [BSS_MGMT_EN_BSS_TRANSITION
] = { "bss_transition", BLOBMSG_TYPE_BOOL
},
694 hostapd_bss_mgmt_enable(struct ubus_context
*ctx
, struct ubus_object
*obj
,
695 struct ubus_request_data
*req
, const char *method
,
696 struct blob_attr
*msg
)
699 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
700 struct blob_attr
*tb
[__BSS_MGMT_EN_MAX
];
701 struct blob_attr
*cur
;
704 bool neigh
= false, beacon
= false;
706 blobmsg_parse(bss_mgmt_enable_policy
, __BSS_MGMT_EN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
708 for (i
= 0; i
< ARRAY_SIZE(tb
); i
++) {
709 if (!tb
[i
] || !blobmsg_get_bool(tb
[i
]))
715 __hostapd_bss_mgmt_enable(hapd
, flags
);
720 hostapd_rrm_nr_enable(struct hostapd_data
*hapd
)
722 __hostapd_bss_mgmt_enable(hapd
, 1 << BSS_MGMT_EN_NEIGHBOR
);
726 hostapd_rrm_nr_get_own(struct ubus_context
*ctx
, struct ubus_object
*obj
,
727 struct ubus_request_data
*req
, const char *method
,
728 struct blob_attr
*msg
)
730 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
731 struct hostapd_neighbor_entry
*nr
;
734 hostapd_rrm_nr_enable(hapd
);
736 nr
= hostapd_neighbor_get(hapd
, hapd
->own_addr
, NULL
);
738 return UBUS_STATUS_NOT_FOUND
;
740 blob_buf_init(&b
, 0);
742 c
= blobmsg_open_array(&b
, "value");
743 hostapd_rrm_print_nr(nr
);
744 blobmsg_close_array(&b
, c
);
746 ubus_send_reply(ctx
, req
, b
.head
);
752 hostapd_rrm_nr_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
753 struct ubus_request_data
*req
, const char *method
,
754 struct blob_attr
*msg
)
756 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
757 struct hostapd_neighbor_entry
*nr
;
760 hostapd_rrm_nr_enable(hapd
);
761 blob_buf_init(&b
, 0);
763 c
= blobmsg_open_array(&b
, "list");
764 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
767 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
770 cur
= blobmsg_open_array(&b
, NULL
);
771 hostapd_rrm_print_nr(nr
);
772 blobmsg_close_array(&b
, cur
);
774 blobmsg_close_array(&b
, c
);
776 ubus_send_reply(ctx
, req
, b
.head
);
786 static const struct blobmsg_policy nr_set_policy
[__NR_SET_LIST_MAX
] = {
787 [NR_SET_LIST
] = { "list", BLOBMSG_TYPE_ARRAY
},
792 hostapd_rrm_nr_clear(struct hostapd_data
*hapd
)
794 struct hostapd_neighbor_entry
*nr
;
797 dl_list_for_each(nr
, &hapd
->nr_db
, struct hostapd_neighbor_entry
, list
) {
798 if (!memcmp(nr
->bssid
, hapd
->own_addr
, ETH_ALEN
))
801 hostapd_neighbor_remove(hapd
, nr
->bssid
, &nr
->ssid
);
807 hostapd_rrm_nr_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
808 struct ubus_request_data
*req
, const char *method
,
809 struct blob_attr
*msg
)
811 static const struct blobmsg_policy nr_e_policy
[] = {
812 { .type
= BLOBMSG_TYPE_STRING
},
813 { .type
= BLOBMSG_TYPE_STRING
},
814 { .type
= BLOBMSG_TYPE_STRING
},
816 struct hostapd_data
*hapd
= get_hapd_from_object(obj
);
817 struct blob_attr
*tb_l
[__NR_SET_LIST_MAX
];
818 struct blob_attr
*tb
[ARRAY_SIZE(nr_e_policy
)];
819 struct blob_attr
*cur
;
823 hostapd_rrm_nr_enable(hapd
);
825 blobmsg_parse(nr_set_policy
, __NR_SET_LIST_MAX
, tb_l
, blob_data(msg
), blob_len(msg
));
826 if (!tb_l
[NR_SET_LIST
])
827 return UBUS_STATUS_INVALID_ARGUMENT
;
829 hostapd_rrm_nr_clear(hapd
);
830 blobmsg_for_each_attr(cur
, tb_l
[NR_SET_LIST
], rem
) {
831 struct wpa_ssid_value ssid
;
836 blobmsg_parse_array(nr_e_policy
, ARRAY_SIZE(nr_e_policy
), tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
837 if (!tb
[0] || !tb
[1] || !tb
[2])
840 s
= blobmsg_get_string(tb
[0]);
841 if (hwaddr_aton(s
, bssid
))
844 s
= blobmsg_get_string(tb
[1]);
845 ssid
.ssid_len
= strlen(s
);
846 if (ssid
.ssid_len
> sizeof(ssid
.ssid
))
849 memcpy(&ssid
, s
, ssid
.ssid_len
);
850 data
= wpabuf_parse_bin(blobmsg_get_string(tb
[2]));
854 hostapd_neighbor_set(hapd
, bssid
, &ssid
, data
, NULL
, NULL
, 0);
859 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
876 static const struct blobmsg_policy beacon_req_policy
[__BEACON_REQ_MAX
] = {
877 [BEACON_REQ_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
878 [BEACON_REQ_OP_CLASS
] { "op_class", BLOBMSG_TYPE_INT32
},
879 [BEACON_REQ_CHANNEL
] { "channel", BLOBMSG_TYPE_INT32
},
880 [BEACON_REQ_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
881 [BEACON_REQ_MODE
] { "mode", BLOBMSG_TYPE_INT32
},
882 [BEACON_REQ_BSSID
] { "bssid", BLOBMSG_TYPE_STRING
},
883 [BEACON_REQ_SSID
] { "ssid", BLOBMSG_TYPE_STRING
},
887 hostapd_rrm_beacon_req(struct ubus_context
*ctx
, struct ubus_object
*obj
,
888 struct ubus_request_data
*ureq
, const char *method
,
889 struct blob_attr
*msg
)
891 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
892 struct blob_attr
*tb
[__BEACON_REQ_MAX
];
893 struct blob_attr
*cur
;
895 u8 bssid
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
900 blobmsg_parse(beacon_req_policy
, __BEACON_REQ_MAX
, tb
, blob_data(msg
), blob_len(msg
));
902 if (!tb
[BEACON_REQ_ADDR
] || !tb
[BEACON_REQ_MODE
] || !tb
[BEACON_REQ_DURATION
] ||
903 !tb
[BEACON_REQ_OP_CLASS
] || !tb
[BEACON_REQ_CHANNEL
])
904 return UBUS_STATUS_INVALID_ARGUMENT
;
906 if (tb
[BEACON_REQ_SSID
])
907 buf_len
+= blobmsg_data_len(tb
[BEACON_REQ_SSID
]) + 2 - 1;
909 mode
= blobmsg_get_u32(tb
[BEACON_REQ_MODE
]);
910 if (hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_ADDR
]), addr
))
911 return UBUS_STATUS_INVALID_ARGUMENT
;
913 if (tb
[BEACON_REQ_BSSID
] &&
914 hwaddr_aton(blobmsg_data(tb
[BEACON_REQ_BSSID
]), bssid
))
915 return UBUS_STATUS_INVALID_ARGUMENT
;
917 req
= wpabuf_alloc(buf_len
);
919 return UBUS_STATUS_UNKNOWN_ERROR
;
921 /* 1: regulatory class */
922 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_OP_CLASS
]));
924 /* 2: channel number */
925 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_CHANNEL
]));
927 /* 3-4: randomization interval */
928 wpabuf_put_le16(req
, 0);
931 wpabuf_put_le16(req
, blobmsg_get_u32(tb
[BEACON_REQ_DURATION
]));
934 wpabuf_put_u8(req
, blobmsg_get_u32(tb
[BEACON_REQ_MODE
]));
937 wpabuf_put_data(req
, bssid
, ETH_ALEN
);
939 if ((cur
= tb
[BEACON_REQ_SSID
]) != NULL
) {
940 wpabuf_put_u8(req
, WLAN_EID_SSID
);
941 wpabuf_put_u8(req
, blobmsg_data_len(cur
) - 1);
942 wpabuf_put_data(req
, blobmsg_data(cur
), blobmsg_data_len(cur
) - 1);
945 ret
= hostapd_send_beacon_req(hapd
, addr
, 0, req
);
956 WNM_DISASSOC_DURATION
,
957 WNM_DISASSOC_NEIGHBORS
,
958 WNM_DISASSOC_ABRIDGED
,
962 static const struct blobmsg_policy wnm_disassoc_policy
[__WNM_DISASSOC_MAX
] = {
963 [WNM_DISASSOC_ADDR
] = { "addr", BLOBMSG_TYPE_STRING
},
964 [WNM_DISASSOC_DURATION
] { "duration", BLOBMSG_TYPE_INT32
},
965 [WNM_DISASSOC_NEIGHBORS
] { "neighbors", BLOBMSG_TYPE_ARRAY
},
966 [WNM_DISASSOC_ABRIDGED
] { "abridged", BLOBMSG_TYPE_BOOL
},
970 hostapd_wnm_disassoc_imminent(struct ubus_context
*ctx
, struct ubus_object
*obj
,
971 struct ubus_request_data
*ureq
, const char *method
,
972 struct blob_attr
*msg
)
974 struct hostapd_data
*hapd
= container_of(obj
, struct hostapd_data
, ubus
.obj
);
975 struct blob_attr
*tb
[__WNM_DISASSOC_MAX
];
976 struct blob_attr
*cur
;
977 struct sta_info
*sta
;
982 u8 req_mode
= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
985 blobmsg_parse(wnm_disassoc_policy
, __WNM_DISASSOC_MAX
, tb
, blob_data(msg
), blob_len(msg
));
987 if (!tb
[WNM_DISASSOC_ADDR
])
988 return UBUS_STATUS_INVALID_ARGUMENT
;
990 if (hwaddr_aton(blobmsg_data(tb
[WNM_DISASSOC_ADDR
]), addr
))
991 return UBUS_STATUS_INVALID_ARGUMENT
;
993 if ((cur
= tb
[WNM_DISASSOC_DURATION
]) != NULL
)
994 duration
= blobmsg_get_u32(cur
);
996 sta
= ap_get_sta(hapd
, addr
);
998 return UBUS_STATUS_NOT_FOUND
;
1000 if (tb
[WNM_DISASSOC_NEIGHBORS
]) {
1003 if (blobmsg_check_array(tb
[WNM_DISASSOC_NEIGHBORS
],
1004 BLOBMSG_TYPE_STRING
) < 0)
1005 return UBUS_STATUS_INVALID_ARGUMENT
;
1007 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
1008 int len
= strlen(blobmsg_get_string(cur
));
1011 return UBUS_STATUS_INVALID_ARGUMENT
;
1013 nr_len
+= (len
/ 2) + 2;
1017 nr
= os_zalloc(nr_len
);
1019 return UBUS_STATUS_UNKNOWN_ERROR
;
1023 blobmsg_for_each_attr(cur
, tb
[WNM_DISASSOC_NEIGHBORS
], rem
) {
1024 int len
= strlen(blobmsg_get_string(cur
)) / 2;
1026 *nr_cur
++ = WLAN_EID_NEIGHBOR_REPORT
;
1027 *nr_cur
++ = (u8
) len
;
1028 if (hexstr2bin(blobmsg_data(cur
), nr_cur
, len
)) {
1030 return UBUS_STATUS_INVALID_ARGUMENT
;
1038 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
1040 if (tb
[WNM_DISASSOC_ABRIDGED
] && blobmsg_get_bool(tb
[WNM_DISASSOC_ABRIDGED
]))
1041 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
1043 if (wnm_send_bss_tm_req(hapd
, sta
, req_mode
, duration
, 0, NULL
,
1044 NULL
, nr
, nr_len
, NULL
, 0))
1045 return UBUS_STATUS_UNKNOWN_ERROR
;
1051 static const struct ubus_method bss_methods
[] = {
1052 UBUS_METHOD_NOARG("reload", hostapd_bss_reload
),
1053 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients
),
1054 UBUS_METHOD("del_client", hostapd_bss_del_client
, del_policy
),
1055 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans
),
1056 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start
),
1057 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel
),
1058 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon
),
1059 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features
),
1061 UBUS_METHOD("switch_chan", hostapd_switch_chan
, csa_policy
),
1063 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements
, ve_policy
),
1064 UBUS_METHOD("notify_response", hostapd_notify_response
, notify_policy
),
1065 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable
, bss_mgmt_enable_policy
),
1066 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own
),
1067 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list
),
1068 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set
, nr_set_policy
),
1069 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req
, beacon_req_policy
),
1070 #ifdef CONFIG_WNM_AP
1071 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent
, wnm_disassoc_policy
),
1075 static struct ubus_object_type bss_object_type
=
1076 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods
);
1078 static int avl_compare_macaddr(const void *k1
, const void *k2
, void *ptr
)
1080 return memcmp(k1
, k2
, ETH_ALEN
);
1083 void hostapd_ubus_add_bss(struct hostapd_data
*hapd
)
1085 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1090 if (hapd
->conf
->mesh
& MESH_ENABLED
)
1094 if (!hostapd_ubus_init())
1097 if (asprintf(&name
, "hostapd.%s", hapd
->conf
->iface
) < 0)
1100 avl_init(&hapd
->ubus
.banned
, avl_compare_macaddr
, false, NULL
);
1102 obj
->type
= &bss_object_type
;
1103 obj
->methods
= bss_object_type
.methods
;
1104 obj
->n_methods
= bss_object_type
.n_methods
;
1105 ret
= ubus_add_object(ctx
, obj
);
1106 hostapd_ubus_ref_inc();
1108 hostapd_send_ubus_event(hapd
->conf
->iface
, "add");
1111 void hostapd_ubus_free_bss(struct hostapd_data
*hapd
)
1113 struct ubus_object
*obj
= &hapd
->ubus
.obj
;
1114 char *name
= (char *) obj
->name
;
1119 hostapd_send_ubus_event(hapd
->conf
->iface
, "remove");
1122 ubus_remove_object(ctx
, obj
);
1123 hostapd_ubus_ref_dec();
1129 static const struct ubus_method daemon_methods
[] = {
1130 UBUS_METHOD("config_add", hostapd_config_add
, config_add_policy
),
1131 UBUS_METHOD("config_remove", hostapd_config_remove
, config_remove_policy
),
1134 static struct ubus_object_type daemon_object_type
=
1135 UBUS_OBJECT_TYPE("hostapd", daemon_methods
);
1137 void hostapd_ubus_add(struct hapd_interfaces
*interfaces
)
1139 struct ubus_object
*obj
= &interfaces
->ubus
;
1142 if (!hostapd_ubus_init())
1145 obj
->name
= strdup("hostapd");
1147 obj
->type
= &daemon_object_type
;
1148 obj
->methods
= daemon_object_type
.methods
;
1149 obj
->n_methods
= daemon_object_type
.n_methods
;
1150 ret
= ubus_add_object(ctx
, obj
);
1151 hostapd_ubus_ref_inc();
1154 void hostapd_ubus_free(struct hapd_interfaces
*interfaces
)
1156 struct ubus_object
*obj
= &interfaces
->ubus
;
1157 char *name
= (char *) obj
->name
;
1163 ubus_remove_object(ctx
, obj
);
1164 hostapd_ubus_ref_dec();
1170 struct ubus_event_req
{
1171 struct ubus_notify_request nreq
;
1176 ubus_event_cb(struct ubus_notify_request
*req
, int idx
, int ret
)
1178 struct ubus_event_req
*ureq
= container_of(req
, struct ubus_event_req
, nreq
);
1183 int hostapd_ubus_handle_event(struct hostapd_data
*hapd
, struct hostapd_ubus_request
*req
)
1185 struct ubus_banned_client
*ban
;
1186 const char *types
[HOSTAPD_UBUS_TYPE_MAX
] = {
1187 [HOSTAPD_UBUS_PROBE_REQ
] = "probe",
1188 [HOSTAPD_UBUS_AUTH_REQ
] = "auth",
1189 [HOSTAPD_UBUS_ASSOC_REQ
] = "assoc",
1191 const char *type
= "mgmt";
1192 struct ubus_event_req ureq
= {};
1195 if (req
->mgmt_frame
)
1196 addr
= req
->mgmt_frame
->sa
;
1200 ban
= avl_find_element(&hapd
->ubus
.banned
, addr
, ban
, avl
);
1202 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA
;
1204 if (!hapd
->ubus
.obj
.has_subscribers
)
1205 return WLAN_STATUS_SUCCESS
;
1207 if (req
->type
< ARRAY_SIZE(types
))
1208 type
= types
[req
->type
];
1210 blob_buf_init(&b
, 0);
1211 blobmsg_add_macaddr(&b
, "address", addr
);
1212 if (req
->mgmt_frame
)
1213 blobmsg_add_macaddr(&b
, "target", req
->mgmt_frame
->da
);
1214 if (req
->ssi_signal
)
1215 blobmsg_add_u32(&b
, "signal", req
->ssi_signal
);
1216 blobmsg_add_u32(&b
, "freq", hapd
->iface
->freq
);
1219 if(req
->elems
->ht_capabilities
)
1221 struct ieee80211_ht_capabilities
*ht_capabilities
;
1222 void *ht_cap
, *ht_cap_mcs_set
, *mcs_set
;
1225 ht_capabilities
= (struct ieee80211_ht_capabilities
*) req
->elems
->ht_capabilities
;
1226 ht_cap
= blobmsg_open_table(&b
, "ht_capabilities");
1227 blobmsg_add_u16(&b
, "ht_capabilities_info", ht_capabilities
->ht_capabilities_info
);
1228 ht_cap_mcs_set
= blobmsg_open_table(&b
, "supported_mcs_set");
1229 blobmsg_add_u16(&b
, "a_mpdu_params", ht_capabilities
->a_mpdu_params
);
1230 blobmsg_add_u16(&b
, "ht_extended_capabilities", ht_capabilities
->ht_extended_capabilities
);
1231 blobmsg_add_u32(&b
, "tx_bf_capability_info", ht_capabilities
->tx_bf_capability_info
);
1232 blobmsg_add_u16(&b
, "asel_capabilities", ht_capabilities
->asel_capabilities
);
1233 mcs_set
= blobmsg_open_array(&b
, "supported_mcs_set");
1234 for (int i
= 0; i
< 16; i
++) {
1235 blobmsg_add_u16(&b
, NULL
, (u16
) ht_capabilities
->supported_mcs_set
[i
]);
1237 blobmsg_close_array(&b
, mcs_set
);
1238 blobmsg_close_table(&b
, ht_cap_mcs_set
);
1239 blobmsg_close_table(&b
, ht_cap
);
1241 if(req
->elems
->vht_capabilities
)
1243 struct ieee80211_vht_capabilities
*vht_capabilities
;
1244 void *vht_cap
, *vht_cap_mcs_set
;
1246 vht_capabilities
= (struct ieee80211_vht_capabilities
*) req
->elems
->vht_capabilities
;
1247 vht_cap
= blobmsg_open_table(&b
, "vht_capabilities");
1248 blobmsg_add_u32(&b
, "vht_capabilities_info", vht_capabilities
->vht_capabilities_info
);
1249 vht_cap_mcs_set
= blobmsg_open_table(&b
, "vht_supported_mcs_set");
1250 blobmsg_add_u16(&b
, "rx_map", vht_capabilities
->vht_supported_mcs_set
.rx_map
);
1251 blobmsg_add_u16(&b
, "rx_highest", vht_capabilities
->vht_supported_mcs_set
.rx_highest
);
1252 blobmsg_add_u16(&b
, "tx_map", vht_capabilities
->vht_supported_mcs_set
.tx_map
);
1253 blobmsg_add_u16(&b
, "tx_highest", vht_capabilities
->vht_supported_mcs_set
.tx_highest
);
1254 blobmsg_close_table(&b
, vht_cap_mcs_set
);
1255 blobmsg_close_table(&b
, vht_cap
);
1259 if (!hapd
->ubus
.notify_response
) {
1260 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1261 return WLAN_STATUS_SUCCESS
;
1264 if (ubus_notify_async(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, &ureq
.nreq
))
1265 return WLAN_STATUS_SUCCESS
;
1267 ureq
.nreq
.status_cb
= ubus_event_cb
;
1268 ubus_complete_request(ctx
, &ureq
.nreq
.req
, 100);
1273 return WLAN_STATUS_SUCCESS
;
1276 void hostapd_ubus_notify(struct hostapd_data
*hapd
, const char *type
, const u8
*addr
)
1278 if (!hapd
->ubus
.obj
.has_subscribers
)
1284 blob_buf_init(&b
, 0);
1285 blobmsg_add_macaddr(&b
, "address", addr
);
1287 ubus_notify(ctx
, &hapd
->ubus
.obj
, type
, b
.head
, -1);
1290 void hostapd_ubus_notify_beacon_report(
1291 struct hostapd_data
*hapd
, const u8
*addr
, u8 token
, u8 rep_mode
,
1292 struct rrm_measurement_beacon_report
*rep
, size_t len
)
1294 if (!hapd
->ubus
.obj
.has_subscribers
)
1300 blob_buf_init(&b
, 0);
1301 blobmsg_add_macaddr(&b
, "address", addr
);
1302 blobmsg_add_u16(&b
, "op-class", rep
->op_class
);
1303 blobmsg_add_u16(&b
, "channel", rep
->channel
);
1304 blobmsg_add_u64(&b
, "start-time", rep
->start_time
);
1305 blobmsg_add_u16(&b
, "duration", rep
->duration
);
1306 blobmsg_add_u16(&b
, "report-info", rep
->report_info
);
1307 blobmsg_add_u16(&b
, "rcpi", rep
->rcpi
);
1308 blobmsg_add_u16(&b
, "rsni", rep
->rsni
);
1309 blobmsg_add_macaddr(&b
, "bssid", rep
->bssid
);
1310 blobmsg_add_u16(&b
, "antenna-id", rep
->antenna_id
);
1311 blobmsg_add_u16(&b
, "parent-tsf", rep
->parent_tsf
);
1313 ubus_notify(ctx
, &hapd
->ubus
.obj
, "beacon-report", b
.head
, -1);