hostapd: send ubus event on BSS update
[openwrt/openwrt.git] / package / network / services / hostapd / src / src / ap / ubus.c
1 /*
2 * hostapd / ubus support
3 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
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"
14 #include "hostapd.h"
15 #include "neighbor_db.h"
16 #include "wps_hostapd.h"
17 #include "sta_info.h"
18 #include "ubus.h"
19 #include "ap_drv_ops.h"
20 #include "beacon.h"
21 #include "rrm.h"
22 #include "wnm_ap.h"
23 #include "taxonomy.h"
24
25 static struct ubus_context *ctx;
26 static struct blob_buf b;
27 static int ctx_ref;
28
29 static inline struct hapd_interfaces *get_hapd_interfaces_from_object(struct ubus_object *obj)
30 {
31 return container_of(obj, struct hapd_interfaces, ubus);
32 }
33
34 static inline struct hostapd_data *get_hapd_from_object(struct ubus_object *obj)
35 {
36 return container_of(obj, struct hostapd_data, ubus.obj);
37 }
38
39 struct ubus_banned_client {
40 struct avl_node avl;
41 u8 addr[ETH_ALEN];
42 };
43
44 static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
45 {
46 struct ubus_context *ctx = eloop_ctx;
47 ubus_handle_event(ctx);
48 }
49
50 static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
51 {
52 if (ubus_reconnect(ctx, NULL)) {
53 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
54 return;
55 }
56
57 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
58 }
59
60 static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
61 {
62 eloop_unregister_read_sock(ctx->sock.fd);
63 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
64 }
65
66 static bool hostapd_ubus_init(void)
67 {
68 if (ctx)
69 return true;
70
71 ctx = ubus_connect(NULL);
72 if (!ctx)
73 return false;
74
75 ctx->connection_lost = hostapd_ubus_connection_lost;
76 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
77 return true;
78 }
79
80 static void hostapd_ubus_ref_inc(void)
81 {
82 ctx_ref++;
83 }
84
85 static void hostapd_ubus_ref_dec(void)
86 {
87 ctx_ref--;
88 if (!ctx)
89 return;
90
91 if (ctx_ref)
92 return;
93
94 eloop_unregister_read_sock(ctx->sock.fd);
95 ubus_free(ctx);
96 ctx = NULL;
97 }
98
99 void hostapd_ubus_add_iface(struct hostapd_iface *iface)
100 {
101 if (!hostapd_ubus_init())
102 return;
103 }
104
105 void hostapd_ubus_free_iface(struct hostapd_iface *iface)
106 {
107 if (!ctx)
108 return;
109 }
110
111 static void hostapd_send_ubus_event(char *bssname, char *event)
112 {
113 char *name;
114
115 if (!ctx)
116 return;
117
118 if (asprintf(&name, "hostapd.%s.%s", bssname, event) < 0)
119 return;
120
121 blob_buf_init(&b, 0);
122 ubus_send_event(ctx, name, b.head);
123 free(name);
124 }
125
126 static void
127 hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
128 {
129 struct ubus_banned_client *ban = eloop_data;
130 struct hostapd_data *hapd = user_ctx;
131
132 avl_delete(&hapd->ubus.banned, &ban->avl);
133 free(ban);
134 }
135
136 static void
137 hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
138 {
139 struct ubus_banned_client *ban;
140
141 if (time < 0)
142 time = 0;
143
144 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
145 if (!ban) {
146 if (!time)
147 return;
148
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);
153 } else {
154 eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
155 if (!time) {
156 hostapd_bss_del_ban(ban, hapd);
157 return;
158 }
159 }
160
161 eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
162 }
163
164 static int
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)
168 {
169 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
170 int ret = hostapd_reload_config(hapd->iface, 1);
171
172 hostapd_send_ubus_event(hapd->conf->iface, "reload");
173 return ret;
174 }
175
176 static int
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)
180 {
181 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
182 struct sta_info *sta;
183 void *list, *c;
184 char mac_buf[20];
185 static const struct {
186 const char *name;
187 uint32_t flag;
188 } sta_flags[] = {
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 },
199 };
200
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) {
205 void *r;
206 int i;
207
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));
213
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);
223 #endif
224 blobmsg_close_table(&b, c);
225 }
226 blobmsg_close_array(&b, list);
227 ubus_send_reply(ctx, req, b.head);
228
229 return 0;
230 }
231
232 static int
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)
236 {
237 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
238
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);
243
244 return 0;
245 }
246
247 enum {
248 NOTIFY_RESPONSE,
249 __NOTIFY_MAX
250 };
251
252 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
253 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
254 };
255
256 static int
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)
260 {
261 struct blob_attr *tb[__NOTIFY_MAX];
262 struct hostapd_data *hapd = get_hapd_from_object(obj);
263 struct wpabuf *elems;
264 const char *pos;
265 size_t len;
266
267 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
268 blob_data(msg), blob_len(msg));
269
270 if (!tb[NOTIFY_RESPONSE])
271 return UBUS_STATUS_INVALID_ARGUMENT;
272
273 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
274
275 return UBUS_STATUS_OK;
276 }
277
278 enum {
279 DEL_CLIENT_ADDR,
280 DEL_CLIENT_REASON,
281 DEL_CLIENT_DEAUTH,
282 DEL_CLIENT_BAN_TIME,
283 __DEL_CLIENT_MAX
284 };
285
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 },
291 };
292
293 static int
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)
297 {
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;
301 bool deauth = false;
302 int reason;
303 u8 addr[ETH_ALEN];
304
305 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
306
307 if (!tb[DEL_CLIENT_ADDR])
308 return UBUS_STATUS_INVALID_ARGUMENT;
309
310 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
311 return UBUS_STATUS_INVALID_ARGUMENT;
312
313 if (tb[DEL_CLIENT_REASON])
314 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
315
316 if (tb[DEL_CLIENT_DEAUTH])
317 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
318
319 sta = ap_get_sta(hapd, addr);
320 if (sta) {
321 if (deauth) {
322 hostapd_drv_sta_deauth(hapd, addr, reason);
323 ap_sta_deauthenticate(hapd, sta, reason);
324 } else {
325 hostapd_drv_sta_disassoc(hapd, addr, reason);
326 ap_sta_disassociate(hapd, sta, reason);
327 }
328 }
329
330 if (tb[DEL_CLIENT_BAN_TIME])
331 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
332
333 return 0;
334 }
335
336 static void
337 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
338 {
339 char *s;
340
341 s = blobmsg_alloc_string_buffer(buf, name, 20);
342 sprintf(s, MACSTR, MAC2STR(addr));
343 blobmsg_add_string_buffer(buf);
344 }
345
346 static int
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)
350 {
351 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
352 struct ubus_banned_client *ban;
353 void *c;
354
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);
361
362 return 0;
363 }
364
365 static int
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)
369 {
370 int rc;
371 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
372
373 rc = hostapd_wps_button_pushed(hapd, NULL);
374
375 if (rc != 0)
376 return UBUS_STATUS_NOT_SUPPORTED;
377
378 return 0;
379 }
380
381 static int
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)
385 {
386 int rc;
387 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
388
389 rc = hostapd_wps_cancel(hapd);
390
391 if (rc != 0)
392 return UBUS_STATUS_NOT_SUPPORTED;
393
394 return 0;
395 }
396
397 static int
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)
401 {
402 int rc;
403 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
404
405 rc = ieee802_11_set_beacon(hapd);
406
407 if (rc != 0)
408 return UBUS_STATUS_NOT_SUPPORTED;
409
410 return 0;
411 }
412
413 enum {
414 CONFIG_IFACE,
415 CONFIG_FILE,
416 __CONFIG_MAX
417 };
418
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 },
422 };
423
424 static int
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)
428 {
429 struct blob_attr *tb[__CONFIG_MAX];
430 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
431 char buf[128];
432
433 blobmsg_parse(config_add_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
434
435 if (!tb[CONFIG_FILE] || !tb[CONFIG_IFACE])
436 return UBUS_STATUS_INVALID_ARGUMENT;
437
438 snprintf(buf, sizeof(buf), "bss_config=%s:%s",
439 blobmsg_get_string(tb[CONFIG_IFACE]),
440 blobmsg_get_string(tb[CONFIG_FILE]));
441
442 if (hostapd_add_iface(interfaces, buf))
443 return UBUS_STATUS_INVALID_ARGUMENT;
444
445 return UBUS_STATUS_OK;
446 }
447
448 enum {
449 CONFIG_REM_IFACE,
450 __CONFIG_REM_MAX
451 };
452
453 static const struct blobmsg_policy config_remove_policy[__CONFIG_REM_MAX] = {
454 [CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
455 };
456
457 static int
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)
461 {
462 struct blob_attr *tb[__CONFIG_REM_MAX];
463 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
464 char buf[128];
465
466 blobmsg_parse(config_remove_policy, __CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
467
468 if (!tb[CONFIG_REM_IFACE])
469 return UBUS_STATUS_INVALID_ARGUMENT;
470
471 if (hostapd_remove_iface(interfaces, blobmsg_get_string(tb[CONFIG_REM_IFACE])))
472 return UBUS_STATUS_INVALID_ARGUMENT;
473
474 return UBUS_STATUS_OK;
475 }
476
477 enum {
478 CSA_FREQ,
479 CSA_BCN_COUNT,
480 CSA_CENTER_FREQ1,
481 CSA_CENTER_FREQ2,
482 CSA_BANDWIDTH,
483 CSA_SEC_CHANNEL_OFFSET,
484 CSA_HT,
485 CSA_VHT,
486 CSA_BLOCK_TX,
487 __CSA_MAX
488 };
489
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 },
500 };
501
502 #ifdef NEED_AP_MLME
503 static int
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)
507 {
508 struct blob_attr *tb[__CSA_MAX];
509 struct hostapd_data *hapd = get_hapd_from_object(obj);
510 struct csa_settings css;
511
512 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
513
514 if (!tb[CSA_FREQ])
515 return UBUS_STATUS_INVALID_ARGUMENT;
516
517 memset(&css, 0, sizeof(css));
518 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
519
520 #define SET_CSA_SETTING(name, field, type) \
521 do { \
522 if (tb[name]) \
523 css.field = blobmsg_get_ ## type(tb[name]); \
524 } while(0)
525
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);
534
535
536 if (hostapd_switch_channel(hapd, &css) != 0)
537 return UBUS_STATUS_NOT_SUPPORTED;
538 return UBUS_STATUS_OK;
539 #undef SET_CSA_SETTING
540 }
541 #endif
542
543 enum {
544 VENDOR_ELEMENTS,
545 __VENDOR_ELEMENTS_MAX
546 };
547
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 },
551 };
552
553 static int
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)
557 {
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;
562 const char *pos;
563 size_t len;
564
565 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
566 blob_data(msg), blob_len(msg));
567
568 if (!tb[VENDOR_ELEMENTS])
569 return UBUS_STATUS_INVALID_ARGUMENT;
570
571 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
572 len = os_strlen(pos);
573 if (len & 0x01)
574 return UBUS_STATUS_INVALID_ARGUMENT;
575
576 len /= 2;
577 if (len == 0) {
578 wpabuf_free(bss->vendor_elements);
579 bss->vendor_elements = NULL;
580 return 0;
581 }
582
583 elems = wpabuf_alloc(len);
584 if (elems == NULL)
585 return 1;
586
587 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
588 wpabuf_free(elems);
589 return UBUS_STATUS_INVALID_ARGUMENT;
590 }
591
592 wpabuf_free(bss->vendor_elements);
593 bss->vendor_elements = elems;
594
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;
599 }
600
601 static void
602 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
603 {
604 const u8 *data;
605 char *str;
606 int len;
607
608 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
609
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);
614
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);
619 }
620
621 enum {
622 BSS_MGMT_EN_NEIGHBOR,
623 BSS_MGMT_EN_BEACON,
624 #ifdef CONFIG_WNM_AP
625 BSS_MGMT_EN_BSS_TRANSITION,
626 #endif
627 __BSS_MGMT_EN_MAX
628 };
629
630 static bool
631 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
632 {
633 struct hostapd_bss_config *bss = hapd->conf;
634 uint32_t flags;
635
636 switch (flag) {
637 case BSS_MGMT_EN_NEIGHBOR:
638 if (bss->radio_measurements[0] &
639 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
640 return false;
641
642 bss->radio_measurements[0] |=
643 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
644 hostapd_neighbor_set_own_report(hapd);
645 return true;
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;
650
651 if (bss->radio_measurements[0] & flags == flags)
652 return false;
653
654 bss->radio_measurements[0] |= (u8) flags;
655 return true;
656 #ifdef CONFIG_WNM_AP
657 case BSS_MGMT_EN_BSS_TRANSITION:
658 if (bss->bss_transition)
659 return false;
660
661 bss->bss_transition = 1;
662 return true;
663 #endif
664 }
665 }
666
667 static void
668 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
669 {
670 bool update = false;
671 int i;
672
673 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
674 if (!(flags & (1 << i)))
675 continue;
676
677 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
678 }
679
680 if (update)
681 ieee802_11_update_beacons(hapd->iface);
682 }
683
684
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 },
688 #ifdef CONFIG_WNM_AP
689 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
690 #endif
691 };
692
693 static int
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)
697
698 {
699 struct hostapd_data *hapd = get_hapd_from_object(obj);
700 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
701 struct blob_attr *cur;
702 uint32_t flags = 0;
703 int i;
704 bool neigh = false, beacon = false;
705
706 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
707
708 for (i = 0; i < ARRAY_SIZE(tb); i++) {
709 if (!tb[i] || !blobmsg_get_bool(tb[i]))
710 continue;
711
712 flags |= (1 << i);
713 }
714
715 __hostapd_bss_mgmt_enable(hapd, flags);
716 }
717
718
719 static void
720 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
721 {
722 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
723 }
724
725 static int
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)
729 {
730 struct hostapd_data *hapd = get_hapd_from_object(obj);
731 struct hostapd_neighbor_entry *nr;
732 void *c;
733
734 hostapd_rrm_nr_enable(hapd);
735
736 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
737 if (!nr)
738 return UBUS_STATUS_NOT_FOUND;
739
740 blob_buf_init(&b, 0);
741
742 c = blobmsg_open_array(&b, "value");
743 hostapd_rrm_print_nr(nr);
744 blobmsg_close_array(&b, c);
745
746 ubus_send_reply(ctx, req, b.head);
747
748 return 0;
749 }
750
751 static int
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)
755 {
756 struct hostapd_data *hapd = get_hapd_from_object(obj);
757 struct hostapd_neighbor_entry *nr;
758 void *c;
759
760 hostapd_rrm_nr_enable(hapd);
761 blob_buf_init(&b, 0);
762
763 c = blobmsg_open_array(&b, "list");
764 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
765 void *cur;
766
767 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
768 continue;
769
770 cur = blobmsg_open_array(&b, NULL);
771 hostapd_rrm_print_nr(nr);
772 blobmsg_close_array(&b, cur);
773 }
774 blobmsg_close_array(&b, c);
775
776 ubus_send_reply(ctx, req, b.head);
777
778 return 0;
779 }
780
781 enum {
782 NR_SET_LIST,
783 __NR_SET_LIST_MAX
784 };
785
786 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
787 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
788 };
789
790
791 static void
792 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
793 {
794 struct hostapd_neighbor_entry *nr;
795
796 restart:
797 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
798 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
799 continue;
800
801 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
802 goto restart;
803 }
804 }
805
806 static int
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)
810 {
811 static const struct blobmsg_policy nr_e_policy[] = {
812 { .type = BLOBMSG_TYPE_STRING },
813 { .type = BLOBMSG_TYPE_STRING },
814 { .type = BLOBMSG_TYPE_STRING },
815 };
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;
820 int ret = 0;
821 int rem;
822
823 hostapd_rrm_nr_enable(hapd);
824
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;
828
829 hostapd_rrm_nr_clear(hapd);
830 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
831 struct wpa_ssid_value ssid;
832 struct wpabuf *data;
833 u8 bssid[ETH_ALEN];
834 char *s;
835
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])
838 goto invalid;
839
840 s = blobmsg_get_string(tb[0]);
841 if (hwaddr_aton(s, bssid))
842 goto invalid;
843
844 s = blobmsg_get_string(tb[1]);
845 ssid.ssid_len = strlen(s);
846 if (ssid.ssid_len > sizeof(ssid.ssid))
847 goto invalid;
848
849 memcpy(&ssid, s, ssid.ssid_len);
850 data = wpabuf_parse_bin(blobmsg_get_string(tb[2]));
851 if (!data)
852 goto invalid;
853
854 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
855 wpabuf_free(data);
856 continue;
857
858 invalid:
859 ret = UBUS_STATUS_INVALID_ARGUMENT;
860 }
861
862 return 0;
863 }
864
865 enum {
866 BEACON_REQ_ADDR,
867 BEACON_REQ_MODE,
868 BEACON_REQ_OP_CLASS,
869 BEACON_REQ_CHANNEL,
870 BEACON_REQ_DURATION,
871 BEACON_REQ_BSSID,
872 BEACON_REQ_SSID,
873 __BEACON_REQ_MAX,
874 };
875
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 },
884 };
885
886 static int
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)
890 {
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;
894 struct wpabuf *req;
895 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
896 u8 addr[ETH_ALEN];
897 int mode, rem, ret;
898 int buf_len = 13;
899
900 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
901
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;
905
906 if (tb[BEACON_REQ_SSID])
907 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
908
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;
912
913 if (tb[BEACON_REQ_BSSID] &&
914 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
915 return UBUS_STATUS_INVALID_ARGUMENT;
916
917 req = wpabuf_alloc(buf_len);
918 if (!req)
919 return UBUS_STATUS_UNKNOWN_ERROR;
920
921 /* 1: regulatory class */
922 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
923
924 /* 2: channel number */
925 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
926
927 /* 3-4: randomization interval */
928 wpabuf_put_le16(req, 0);
929
930 /* 5-6: duration */
931 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
932
933 /* 7: mode */
934 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
935
936 /* 8-13: BSSID */
937 wpabuf_put_data(req, bssid, ETH_ALEN);
938
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);
943 }
944
945 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
946 if (ret < 0)
947 return -ret;
948
949 return 0;
950 }
951
952
953 #ifdef CONFIG_WNM_AP
954 enum {
955 WNM_DISASSOC_ADDR,
956 WNM_DISASSOC_DURATION,
957 WNM_DISASSOC_NEIGHBORS,
958 WNM_DISASSOC_ABRIDGED,
959 __WNM_DISASSOC_MAX,
960 };
961
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 },
967 };
968
969 static int
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)
973 {
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;
978 int duration = 10;
979 int rem;
980 int nr_len = 0;
981 u8 *nr = NULL;
982 u8 req_mode = WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
983 u8 addr[ETH_ALEN];
984
985 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
986
987 if (!tb[WNM_DISASSOC_ADDR])
988 return UBUS_STATUS_INVALID_ARGUMENT;
989
990 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
991 return UBUS_STATUS_INVALID_ARGUMENT;
992
993 if ((cur = tb[WNM_DISASSOC_DURATION]) != NULL)
994 duration = blobmsg_get_u32(cur);
995
996 sta = ap_get_sta(hapd, addr);
997 if (!sta)
998 return UBUS_STATUS_NOT_FOUND;
999
1000 if (tb[WNM_DISASSOC_NEIGHBORS]) {
1001 u8 *nr_cur;
1002
1003 if (blobmsg_check_array(tb[WNM_DISASSOC_NEIGHBORS],
1004 BLOBMSG_TYPE_STRING) < 0)
1005 return UBUS_STATUS_INVALID_ARGUMENT;
1006
1007 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1008 int len = strlen(blobmsg_get_string(cur));
1009
1010 if (len % 2)
1011 return UBUS_STATUS_INVALID_ARGUMENT;
1012
1013 nr_len += (len / 2) + 2;
1014 }
1015
1016 if (nr_len) {
1017 nr = os_zalloc(nr_len);
1018 if (!nr)
1019 return UBUS_STATUS_UNKNOWN_ERROR;
1020 }
1021
1022 nr_cur = nr;
1023 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1024 int len = strlen(blobmsg_get_string(cur)) / 2;
1025
1026 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
1027 *nr_cur++ = (u8) len;
1028 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
1029 free(nr);
1030 return UBUS_STATUS_INVALID_ARGUMENT;
1031 }
1032
1033 nr_cur += len;
1034 }
1035 }
1036
1037 if (nr)
1038 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1039
1040 if (tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]))
1041 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1042
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;
1046
1047 return 0;
1048 }
1049 #endif
1050
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),
1060 #ifdef NEED_AP_MLME
1061 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
1062 #endif
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),
1072 #endif
1073 };
1074
1075 static struct ubus_object_type bss_object_type =
1076 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
1077
1078 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
1079 {
1080 return memcmp(k1, k2, ETH_ALEN);
1081 }
1082
1083 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
1084 {
1085 struct ubus_object *obj = &hapd->ubus.obj;
1086 char *name;
1087 int ret;
1088
1089 #ifdef CONFIG_MESH
1090 if (hapd->conf->mesh & MESH_ENABLED)
1091 return;
1092 #endif
1093
1094 if (!hostapd_ubus_init())
1095 return;
1096
1097 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
1098 return;
1099
1100 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
1101 obj->name = name;
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();
1107
1108 hostapd_send_ubus_event(hapd->conf->iface, "add");
1109 }
1110
1111 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
1112 {
1113 struct ubus_object *obj = &hapd->ubus.obj;
1114 char *name = (char *) obj->name;
1115
1116 if (!ctx)
1117 return;
1118
1119 hostapd_send_ubus_event(hapd->conf->iface, "remove");
1120
1121 if (obj->id) {
1122 ubus_remove_object(ctx, obj);
1123 hostapd_ubus_ref_dec();
1124 }
1125
1126 free(name);
1127 }
1128
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),
1132 };
1133
1134 static struct ubus_object_type daemon_object_type =
1135 UBUS_OBJECT_TYPE("hostapd", daemon_methods);
1136
1137 void hostapd_ubus_add(struct hapd_interfaces *interfaces)
1138 {
1139 struct ubus_object *obj = &interfaces->ubus;
1140 int ret;
1141
1142 if (!hostapd_ubus_init())
1143 return;
1144
1145 obj->name = strdup("hostapd");
1146
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();
1152 }
1153
1154 void hostapd_ubus_free(struct hapd_interfaces *interfaces)
1155 {
1156 struct ubus_object *obj = &interfaces->ubus;
1157 char *name = (char *) obj->name;
1158
1159 if (!ctx)
1160 return;
1161
1162 if (obj->id) {
1163 ubus_remove_object(ctx, obj);
1164 hostapd_ubus_ref_dec();
1165 }
1166
1167 free(name);
1168 }
1169
1170 struct ubus_event_req {
1171 struct ubus_notify_request nreq;
1172 int resp;
1173 };
1174
1175 static void
1176 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1177 {
1178 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1179
1180 ureq->resp = ret;
1181 }
1182
1183 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1184 {
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",
1190 };
1191 const char *type = "mgmt";
1192 struct ubus_event_req ureq = {};
1193 const u8 *addr;
1194
1195 if (req->mgmt_frame)
1196 addr = req->mgmt_frame->sa;
1197 else
1198 addr = req->addr;
1199
1200 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1201 if (ban)
1202 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1203
1204 if (!hapd->ubus.obj.has_subscribers)
1205 return WLAN_STATUS_SUCCESS;
1206
1207 if (req->type < ARRAY_SIZE(types))
1208 type = types[req->type];
1209
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);
1217
1218 if (req->elems) {
1219 if(req->elems->ht_capabilities)
1220 {
1221 struct ieee80211_ht_capabilities *ht_capabilities;
1222 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1223
1224
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]);
1236 }
1237 blobmsg_close_array(&b, mcs_set);
1238 blobmsg_close_table(&b, ht_cap_mcs_set);
1239 blobmsg_close_table(&b, ht_cap);
1240 }
1241 if(req->elems->vht_capabilities)
1242 {
1243 struct ieee80211_vht_capabilities *vht_capabilities;
1244 void *vht_cap, *vht_cap_mcs_set;
1245
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);
1256 }
1257 }
1258
1259 if (!hapd->ubus.notify_response) {
1260 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1261 return WLAN_STATUS_SUCCESS;
1262 }
1263
1264 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1265 return WLAN_STATUS_SUCCESS;
1266
1267 ureq.nreq.status_cb = ubus_event_cb;
1268 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1269
1270 if (ureq.resp)
1271 return ureq.resp;
1272
1273 return WLAN_STATUS_SUCCESS;
1274 }
1275
1276 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1277 {
1278 if (!hapd->ubus.obj.has_subscribers)
1279 return;
1280
1281 if (!addr)
1282 return;
1283
1284 blob_buf_init(&b, 0);
1285 blobmsg_add_macaddr(&b, "address", addr);
1286
1287 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1288 }
1289
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)
1293 {
1294 if (!hapd->ubus.obj.has_subscribers)
1295 return;
1296
1297 if (!addr || !rep)
1298 return;
1299
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);
1312
1313 ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
1314 }