hostapd: send procd event on BSS update
[openwrt/staging/noltari.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 hostapd_send_procd_event(char *bssname, char *event)
127 {
128 char *name, *s;
129 uint32_t id;
130 void *v;
131
132 if (!ctx || ubus_lookup_id(ctx, "service", &id))
133 return;
134
135 if (asprintf(&name, "hostapd.%s.%s", bssname, event) < 0)
136 return;
137
138 blob_buf_init(&b, 0);
139
140 s = blobmsg_alloc_string_buffer(&b, "type", strlen(name) + 1);
141 sprintf(s, "%s", name);
142 blobmsg_add_string_buffer(&b);
143
144 v = blobmsg_open_table(&b, "data");
145 blobmsg_close_table(&b, v);
146
147 ubus_invoke(ctx, id, "event", b.head, NULL, NULL, 1000);
148
149 free(name);
150 }
151
152 static void hostapd_send_shared_event(char *bssname, char *event)
153 {
154 hostapd_send_procd_event(bssname, event);
155 hostapd_send_ubus_event(bssname, event);
156 }
157
158 static void
159 hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
160 {
161 struct ubus_banned_client *ban = eloop_data;
162 struct hostapd_data *hapd = user_ctx;
163
164 avl_delete(&hapd->ubus.banned, &ban->avl);
165 free(ban);
166 }
167
168 static void
169 hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
170 {
171 struct ubus_banned_client *ban;
172
173 if (time < 0)
174 time = 0;
175
176 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
177 if (!ban) {
178 if (!time)
179 return;
180
181 ban = os_zalloc(sizeof(*ban));
182 memcpy(ban->addr, addr, sizeof(ban->addr));
183 ban->avl.key = ban->addr;
184 avl_insert(&hapd->ubus.banned, &ban->avl);
185 } else {
186 eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
187 if (!time) {
188 hostapd_bss_del_ban(ban, hapd);
189 return;
190 }
191 }
192
193 eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
194 }
195
196 static int
197 hostapd_bss_reload(struct ubus_context *ctx, struct ubus_object *obj,
198 struct ubus_request_data *req, const char *method,
199 struct blob_attr *msg)
200 {
201 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
202 int ret = hostapd_reload_config(hapd->iface, 1);
203
204 hostapd_send_shared_event(hapd->conf->iface, "reload");
205 return ret;
206 }
207
208 static int
209 hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
210 struct ubus_request_data *req, const char *method,
211 struct blob_attr *msg)
212 {
213 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
214 struct sta_info *sta;
215 void *list, *c;
216 char mac_buf[20];
217 static const struct {
218 const char *name;
219 uint32_t flag;
220 } sta_flags[] = {
221 { "auth", WLAN_STA_AUTH },
222 { "assoc", WLAN_STA_ASSOC },
223 { "authorized", WLAN_STA_AUTHORIZED },
224 { "preauth", WLAN_STA_PREAUTH },
225 { "wds", WLAN_STA_WDS },
226 { "wmm", WLAN_STA_WMM },
227 { "ht", WLAN_STA_HT },
228 { "vht", WLAN_STA_VHT },
229 { "wps", WLAN_STA_WPS },
230 { "mfp", WLAN_STA_MFP },
231 };
232
233 blob_buf_init(&b, 0);
234 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
235 list = blobmsg_open_table(&b, "clients");
236 for (sta = hapd->sta_list; sta; sta = sta->next) {
237 void *r;
238 int i;
239
240 sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
241 c = blobmsg_open_table(&b, mac_buf);
242 for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
243 blobmsg_add_u8(&b, sta_flags[i].name,
244 !!(sta->flags & sta_flags[i].flag));
245
246 r = blobmsg_open_array(&b, "rrm");
247 for (i = 0; i < ARRAY_SIZE(sta->rrm_enabled_capa); i++)
248 blobmsg_add_u32(&b, "", sta->rrm_enabled_capa[i]);
249 blobmsg_close_array(&b, r);
250 blobmsg_add_u32(&b, "aid", sta->aid);
251 #ifdef CONFIG_TAXONOMY
252 r = blobmsg_alloc_string_buffer(&b, "signature", 1024);
253 if (retrieve_sta_taxonomy(hapd, sta, r, 1024) > 0)
254 blobmsg_add_string_buffer(&b);
255 #endif
256 blobmsg_close_table(&b, c);
257 }
258 blobmsg_close_array(&b, list);
259 ubus_send_reply(ctx, req, b.head);
260
261 return 0;
262 }
263
264 static int
265 hostapd_bss_get_features(struct ubus_context *ctx, struct ubus_object *obj,
266 struct ubus_request_data *req, const char *method,
267 struct blob_attr *msg)
268 {
269 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
270
271 blob_buf_init(&b, 0);
272 blobmsg_add_u8(&b, "ht_supported", ht_supported(hapd->iface->hw_features));
273 blobmsg_add_u8(&b, "vht_supported", vht_supported(hapd->iface->hw_features));
274 ubus_send_reply(ctx, req, b.head);
275
276 return 0;
277 }
278
279 enum {
280 NOTIFY_RESPONSE,
281 __NOTIFY_MAX
282 };
283
284 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
285 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
286 };
287
288 static int
289 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
290 struct ubus_request_data *req, const char *method,
291 struct blob_attr *msg)
292 {
293 struct blob_attr *tb[__NOTIFY_MAX];
294 struct hostapd_data *hapd = get_hapd_from_object(obj);
295 struct wpabuf *elems;
296 const char *pos;
297 size_t len;
298
299 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
300 blob_data(msg), blob_len(msg));
301
302 if (!tb[NOTIFY_RESPONSE])
303 return UBUS_STATUS_INVALID_ARGUMENT;
304
305 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
306
307 return UBUS_STATUS_OK;
308 }
309
310 enum {
311 DEL_CLIENT_ADDR,
312 DEL_CLIENT_REASON,
313 DEL_CLIENT_DEAUTH,
314 DEL_CLIENT_BAN_TIME,
315 __DEL_CLIENT_MAX
316 };
317
318 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
319 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
320 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
321 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
322 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
323 };
324
325 static int
326 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
327 struct ubus_request_data *req, const char *method,
328 struct blob_attr *msg)
329 {
330 struct blob_attr *tb[__DEL_CLIENT_MAX];
331 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
332 struct sta_info *sta;
333 bool deauth = false;
334 int reason;
335 u8 addr[ETH_ALEN];
336
337 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
338
339 if (!tb[DEL_CLIENT_ADDR])
340 return UBUS_STATUS_INVALID_ARGUMENT;
341
342 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
343 return UBUS_STATUS_INVALID_ARGUMENT;
344
345 if (tb[DEL_CLIENT_REASON])
346 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
347
348 if (tb[DEL_CLIENT_DEAUTH])
349 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
350
351 sta = ap_get_sta(hapd, addr);
352 if (sta) {
353 if (deauth) {
354 hostapd_drv_sta_deauth(hapd, addr, reason);
355 ap_sta_deauthenticate(hapd, sta, reason);
356 } else {
357 hostapd_drv_sta_disassoc(hapd, addr, reason);
358 ap_sta_disassociate(hapd, sta, reason);
359 }
360 }
361
362 if (tb[DEL_CLIENT_BAN_TIME])
363 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
364
365 return 0;
366 }
367
368 static void
369 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
370 {
371 char *s;
372
373 s = blobmsg_alloc_string_buffer(buf, name, 20);
374 sprintf(s, MACSTR, MAC2STR(addr));
375 blobmsg_add_string_buffer(buf);
376 }
377
378 static int
379 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
380 struct ubus_request_data *req, const char *method,
381 struct blob_attr *msg)
382 {
383 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
384 struct ubus_banned_client *ban;
385 void *c;
386
387 blob_buf_init(&b, 0);
388 c = blobmsg_open_array(&b, "clients");
389 avl_for_each_element(&hapd->ubus.banned, ban, avl)
390 blobmsg_add_macaddr(&b, NULL, ban->addr);
391 blobmsg_close_array(&b, c);
392 ubus_send_reply(ctx, req, b.head);
393
394 return 0;
395 }
396
397 static int
398 hostapd_bss_wps_start(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 = hostapd_wps_button_pushed(hapd, NULL);
406
407 if (rc != 0)
408 return UBUS_STATUS_NOT_SUPPORTED;
409
410 return 0;
411 }
412
413 static int
414 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
415 struct ubus_request_data *req, const char *method,
416 struct blob_attr *msg)
417 {
418 int rc;
419 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
420
421 rc = hostapd_wps_cancel(hapd);
422
423 if (rc != 0)
424 return UBUS_STATUS_NOT_SUPPORTED;
425
426 return 0;
427 }
428
429 static int
430 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
431 struct ubus_request_data *req, const char *method,
432 struct blob_attr *msg)
433 {
434 int rc;
435 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
436
437 rc = ieee802_11_set_beacon(hapd);
438
439 if (rc != 0)
440 return UBUS_STATUS_NOT_SUPPORTED;
441
442 return 0;
443 }
444
445 enum {
446 CONFIG_IFACE,
447 CONFIG_FILE,
448 __CONFIG_MAX
449 };
450
451 static const struct blobmsg_policy config_add_policy[__CONFIG_MAX] = {
452 [CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
453 [CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
454 };
455
456 static int
457 hostapd_config_add(struct ubus_context *ctx, struct ubus_object *obj,
458 struct ubus_request_data *req, const char *method,
459 struct blob_attr *msg)
460 {
461 struct blob_attr *tb[__CONFIG_MAX];
462 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
463 char buf[128];
464
465 blobmsg_parse(config_add_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
466
467 if (!tb[CONFIG_FILE] || !tb[CONFIG_IFACE])
468 return UBUS_STATUS_INVALID_ARGUMENT;
469
470 snprintf(buf, sizeof(buf), "bss_config=%s:%s",
471 blobmsg_get_string(tb[CONFIG_IFACE]),
472 blobmsg_get_string(tb[CONFIG_FILE]));
473
474 if (hostapd_add_iface(interfaces, buf))
475 return UBUS_STATUS_INVALID_ARGUMENT;
476
477 return UBUS_STATUS_OK;
478 }
479
480 enum {
481 CONFIG_REM_IFACE,
482 __CONFIG_REM_MAX
483 };
484
485 static const struct blobmsg_policy config_remove_policy[__CONFIG_REM_MAX] = {
486 [CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
487 };
488
489 static int
490 hostapd_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
491 struct ubus_request_data *req, const char *method,
492 struct blob_attr *msg)
493 {
494 struct blob_attr *tb[__CONFIG_REM_MAX];
495 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
496 char buf[128];
497
498 blobmsg_parse(config_remove_policy, __CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
499
500 if (!tb[CONFIG_REM_IFACE])
501 return UBUS_STATUS_INVALID_ARGUMENT;
502
503 if (hostapd_remove_iface(interfaces, blobmsg_get_string(tb[CONFIG_REM_IFACE])))
504 return UBUS_STATUS_INVALID_ARGUMENT;
505
506 return UBUS_STATUS_OK;
507 }
508
509 enum {
510 CSA_FREQ,
511 CSA_BCN_COUNT,
512 CSA_CENTER_FREQ1,
513 CSA_CENTER_FREQ2,
514 CSA_BANDWIDTH,
515 CSA_SEC_CHANNEL_OFFSET,
516 CSA_HT,
517 CSA_VHT,
518 CSA_BLOCK_TX,
519 __CSA_MAX
520 };
521
522 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
523 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
524 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
525 [CSA_CENTER_FREQ1] = { "center_freq1", BLOBMSG_TYPE_INT32 },
526 [CSA_CENTER_FREQ2] = { "center_freq2", BLOBMSG_TYPE_INT32 },
527 [CSA_BANDWIDTH] = { "bandwidth", BLOBMSG_TYPE_INT32 },
528 [CSA_SEC_CHANNEL_OFFSET] = { "sec_channel_offset", BLOBMSG_TYPE_INT32 },
529 [CSA_HT] = { "ht", BLOBMSG_TYPE_BOOL },
530 [CSA_VHT] = { "vht", BLOBMSG_TYPE_BOOL },
531 [CSA_BLOCK_TX] = { "block_tx", BLOBMSG_TYPE_BOOL },
532 };
533
534 #ifdef NEED_AP_MLME
535 static int
536 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
537 struct ubus_request_data *req, const char *method,
538 struct blob_attr *msg)
539 {
540 struct blob_attr *tb[__CSA_MAX];
541 struct hostapd_data *hapd = get_hapd_from_object(obj);
542 struct csa_settings css;
543
544 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
545
546 if (!tb[CSA_FREQ])
547 return UBUS_STATUS_INVALID_ARGUMENT;
548
549 memset(&css, 0, sizeof(css));
550 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
551
552 #define SET_CSA_SETTING(name, field, type) \
553 do { \
554 if (tb[name]) \
555 css.field = blobmsg_get_ ## type(tb[name]); \
556 } while(0)
557
558 SET_CSA_SETTING(CSA_BCN_COUNT, cs_count, u32);
559 SET_CSA_SETTING(CSA_CENTER_FREQ1, freq_params.center_freq1, u32);
560 SET_CSA_SETTING(CSA_CENTER_FREQ2, freq_params.center_freq2, u32);
561 SET_CSA_SETTING(CSA_BANDWIDTH, freq_params.bandwidth, u32);
562 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET, freq_params.sec_channel_offset, u32);
563 SET_CSA_SETTING(CSA_HT, freq_params.ht_enabled, bool);
564 SET_CSA_SETTING(CSA_VHT, freq_params.vht_enabled, bool);
565 SET_CSA_SETTING(CSA_BLOCK_TX, block_tx, bool);
566
567
568 if (hostapd_switch_channel(hapd, &css) != 0)
569 return UBUS_STATUS_NOT_SUPPORTED;
570 return UBUS_STATUS_OK;
571 #undef SET_CSA_SETTING
572 }
573 #endif
574
575 enum {
576 VENDOR_ELEMENTS,
577 __VENDOR_ELEMENTS_MAX
578 };
579
580 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
581 /* vendor elements are provided as hex-string */
582 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
583 };
584
585 static int
586 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
587 struct ubus_request_data *req, const char *method,
588 struct blob_attr *msg)
589 {
590 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
591 struct hostapd_data *hapd = get_hapd_from_object(obj);
592 struct hostapd_bss_config *bss = hapd->conf;
593 struct wpabuf *elems;
594 const char *pos;
595 size_t len;
596
597 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
598 blob_data(msg), blob_len(msg));
599
600 if (!tb[VENDOR_ELEMENTS])
601 return UBUS_STATUS_INVALID_ARGUMENT;
602
603 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
604 len = os_strlen(pos);
605 if (len & 0x01)
606 return UBUS_STATUS_INVALID_ARGUMENT;
607
608 len /= 2;
609 if (len == 0) {
610 wpabuf_free(bss->vendor_elements);
611 bss->vendor_elements = NULL;
612 return 0;
613 }
614
615 elems = wpabuf_alloc(len);
616 if (elems == NULL)
617 return 1;
618
619 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
620 wpabuf_free(elems);
621 return UBUS_STATUS_INVALID_ARGUMENT;
622 }
623
624 wpabuf_free(bss->vendor_elements);
625 bss->vendor_elements = elems;
626
627 /* update beacons if vendor elements were set successfully */
628 if (ieee802_11_update_beacons(hapd->iface) != 0)
629 return UBUS_STATUS_NOT_SUPPORTED;
630 return UBUS_STATUS_OK;
631 }
632
633 static void
634 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
635 {
636 const u8 *data;
637 char *str;
638 int len;
639
640 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
641
642 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
643 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
644 str[nr->ssid.ssid_len] = 0;
645 blobmsg_add_string_buffer(&b);
646
647 len = wpabuf_len(nr->nr);
648 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
649 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
650 blobmsg_add_string_buffer(&b);
651 }
652
653 enum {
654 BSS_MGMT_EN_NEIGHBOR,
655 BSS_MGMT_EN_BEACON,
656 #ifdef CONFIG_WNM_AP
657 BSS_MGMT_EN_BSS_TRANSITION,
658 #endif
659 __BSS_MGMT_EN_MAX
660 };
661
662 static bool
663 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
664 {
665 struct hostapd_bss_config *bss = hapd->conf;
666 uint32_t flags;
667
668 switch (flag) {
669 case BSS_MGMT_EN_NEIGHBOR:
670 if (bss->radio_measurements[0] &
671 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
672 return false;
673
674 bss->radio_measurements[0] |=
675 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
676 hostapd_neighbor_set_own_report(hapd);
677 return true;
678 case BSS_MGMT_EN_BEACON:
679 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
680 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
681 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
682
683 if (bss->radio_measurements[0] & flags == flags)
684 return false;
685
686 bss->radio_measurements[0] |= (u8) flags;
687 return true;
688 #ifdef CONFIG_WNM_AP
689 case BSS_MGMT_EN_BSS_TRANSITION:
690 if (bss->bss_transition)
691 return false;
692
693 bss->bss_transition = 1;
694 return true;
695 #endif
696 }
697 }
698
699 static void
700 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
701 {
702 bool update = false;
703 int i;
704
705 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
706 if (!(flags & (1 << i)))
707 continue;
708
709 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
710 }
711
712 if (update)
713 ieee802_11_update_beacons(hapd->iface);
714 }
715
716
717 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
718 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
719 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
720 #ifdef CONFIG_WNM_AP
721 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
722 #endif
723 };
724
725 static int
726 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
727 struct ubus_request_data *req, const char *method,
728 struct blob_attr *msg)
729
730 {
731 struct hostapd_data *hapd = get_hapd_from_object(obj);
732 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
733 struct blob_attr *cur;
734 uint32_t flags = 0;
735 int i;
736 bool neigh = false, beacon = false;
737
738 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
739
740 for (i = 0; i < ARRAY_SIZE(tb); i++) {
741 if (!tb[i] || !blobmsg_get_bool(tb[i]))
742 continue;
743
744 flags |= (1 << i);
745 }
746
747 __hostapd_bss_mgmt_enable(hapd, flags);
748 }
749
750
751 static void
752 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
753 {
754 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
755 }
756
757 static int
758 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
759 struct ubus_request_data *req, const char *method,
760 struct blob_attr *msg)
761 {
762 struct hostapd_data *hapd = get_hapd_from_object(obj);
763 struct hostapd_neighbor_entry *nr;
764 void *c;
765
766 hostapd_rrm_nr_enable(hapd);
767
768 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
769 if (!nr)
770 return UBUS_STATUS_NOT_FOUND;
771
772 blob_buf_init(&b, 0);
773
774 c = blobmsg_open_array(&b, "value");
775 hostapd_rrm_print_nr(nr);
776 blobmsg_close_array(&b, c);
777
778 ubus_send_reply(ctx, req, b.head);
779
780 return 0;
781 }
782
783 static int
784 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
785 struct ubus_request_data *req, const char *method,
786 struct blob_attr *msg)
787 {
788 struct hostapd_data *hapd = get_hapd_from_object(obj);
789 struct hostapd_neighbor_entry *nr;
790 void *c;
791
792 hostapd_rrm_nr_enable(hapd);
793 blob_buf_init(&b, 0);
794
795 c = blobmsg_open_array(&b, "list");
796 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
797 void *cur;
798
799 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
800 continue;
801
802 cur = blobmsg_open_array(&b, NULL);
803 hostapd_rrm_print_nr(nr);
804 blobmsg_close_array(&b, cur);
805 }
806 blobmsg_close_array(&b, c);
807
808 ubus_send_reply(ctx, req, b.head);
809
810 return 0;
811 }
812
813 enum {
814 NR_SET_LIST,
815 __NR_SET_LIST_MAX
816 };
817
818 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
819 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
820 };
821
822
823 static void
824 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
825 {
826 struct hostapd_neighbor_entry *nr;
827
828 restart:
829 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
830 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
831 continue;
832
833 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
834 goto restart;
835 }
836 }
837
838 static int
839 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
840 struct ubus_request_data *req, const char *method,
841 struct blob_attr *msg)
842 {
843 static const struct blobmsg_policy nr_e_policy[] = {
844 { .type = BLOBMSG_TYPE_STRING },
845 { .type = BLOBMSG_TYPE_STRING },
846 { .type = BLOBMSG_TYPE_STRING },
847 };
848 struct hostapd_data *hapd = get_hapd_from_object(obj);
849 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
850 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
851 struct blob_attr *cur;
852 int ret = 0;
853 int rem;
854
855 hostapd_rrm_nr_enable(hapd);
856
857 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
858 if (!tb_l[NR_SET_LIST])
859 return UBUS_STATUS_INVALID_ARGUMENT;
860
861 hostapd_rrm_nr_clear(hapd);
862 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
863 struct wpa_ssid_value ssid;
864 struct wpabuf *data;
865 u8 bssid[ETH_ALEN];
866 char *s;
867
868 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
869 if (!tb[0] || !tb[1] || !tb[2])
870 goto invalid;
871
872 s = blobmsg_get_string(tb[0]);
873 if (hwaddr_aton(s, bssid))
874 goto invalid;
875
876 s = blobmsg_get_string(tb[1]);
877 ssid.ssid_len = strlen(s);
878 if (ssid.ssid_len > sizeof(ssid.ssid))
879 goto invalid;
880
881 memcpy(&ssid, s, ssid.ssid_len);
882 data = wpabuf_parse_bin(blobmsg_get_string(tb[2]));
883 if (!data)
884 goto invalid;
885
886 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
887 wpabuf_free(data);
888 continue;
889
890 invalid:
891 ret = UBUS_STATUS_INVALID_ARGUMENT;
892 }
893
894 return 0;
895 }
896
897 enum {
898 BEACON_REQ_ADDR,
899 BEACON_REQ_MODE,
900 BEACON_REQ_OP_CLASS,
901 BEACON_REQ_CHANNEL,
902 BEACON_REQ_DURATION,
903 BEACON_REQ_BSSID,
904 BEACON_REQ_SSID,
905 __BEACON_REQ_MAX,
906 };
907
908 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
909 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
910 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
911 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
912 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
913 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
914 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
915 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
916 };
917
918 static int
919 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
920 struct ubus_request_data *ureq, const char *method,
921 struct blob_attr *msg)
922 {
923 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
924 struct blob_attr *tb[__BEACON_REQ_MAX];
925 struct blob_attr *cur;
926 struct wpabuf *req;
927 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
928 u8 addr[ETH_ALEN];
929 int mode, rem, ret;
930 int buf_len = 13;
931
932 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
933
934 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
935 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
936 return UBUS_STATUS_INVALID_ARGUMENT;
937
938 if (tb[BEACON_REQ_SSID])
939 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
940
941 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
942 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
943 return UBUS_STATUS_INVALID_ARGUMENT;
944
945 if (tb[BEACON_REQ_BSSID] &&
946 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
947 return UBUS_STATUS_INVALID_ARGUMENT;
948
949 req = wpabuf_alloc(buf_len);
950 if (!req)
951 return UBUS_STATUS_UNKNOWN_ERROR;
952
953 /* 1: regulatory class */
954 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
955
956 /* 2: channel number */
957 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
958
959 /* 3-4: randomization interval */
960 wpabuf_put_le16(req, 0);
961
962 /* 5-6: duration */
963 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
964
965 /* 7: mode */
966 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
967
968 /* 8-13: BSSID */
969 wpabuf_put_data(req, bssid, ETH_ALEN);
970
971 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
972 wpabuf_put_u8(req, WLAN_EID_SSID);
973 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
974 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
975 }
976
977 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
978 if (ret < 0)
979 return -ret;
980
981 return 0;
982 }
983
984
985 #ifdef CONFIG_WNM_AP
986 enum {
987 WNM_DISASSOC_ADDR,
988 WNM_DISASSOC_DURATION,
989 WNM_DISASSOC_NEIGHBORS,
990 WNM_DISASSOC_ABRIDGED,
991 __WNM_DISASSOC_MAX,
992 };
993
994 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
995 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
996 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
997 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
998 [WNM_DISASSOC_ABRIDGED] { "abridged", BLOBMSG_TYPE_BOOL },
999 };
1000
1001 static int
1002 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
1003 struct ubus_request_data *ureq, const char *method,
1004 struct blob_attr *msg)
1005 {
1006 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1007 struct blob_attr *tb[__WNM_DISASSOC_MAX];
1008 struct blob_attr *cur;
1009 struct sta_info *sta;
1010 int duration = 10;
1011 int rem;
1012 int nr_len = 0;
1013 u8 *nr = NULL;
1014 u8 req_mode = WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1015 u8 addr[ETH_ALEN];
1016
1017 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1018
1019 if (!tb[WNM_DISASSOC_ADDR])
1020 return UBUS_STATUS_INVALID_ARGUMENT;
1021
1022 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
1023 return UBUS_STATUS_INVALID_ARGUMENT;
1024
1025 if ((cur = tb[WNM_DISASSOC_DURATION]) != NULL)
1026 duration = blobmsg_get_u32(cur);
1027
1028 sta = ap_get_sta(hapd, addr);
1029 if (!sta)
1030 return UBUS_STATUS_NOT_FOUND;
1031
1032 if (tb[WNM_DISASSOC_NEIGHBORS]) {
1033 u8 *nr_cur;
1034
1035 if (blobmsg_check_array(tb[WNM_DISASSOC_NEIGHBORS],
1036 BLOBMSG_TYPE_STRING) < 0)
1037 return UBUS_STATUS_INVALID_ARGUMENT;
1038
1039 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1040 int len = strlen(blobmsg_get_string(cur));
1041
1042 if (len % 2)
1043 return UBUS_STATUS_INVALID_ARGUMENT;
1044
1045 nr_len += (len / 2) + 2;
1046 }
1047
1048 if (nr_len) {
1049 nr = os_zalloc(nr_len);
1050 if (!nr)
1051 return UBUS_STATUS_UNKNOWN_ERROR;
1052 }
1053
1054 nr_cur = nr;
1055 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1056 int len = strlen(blobmsg_get_string(cur)) / 2;
1057
1058 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
1059 *nr_cur++ = (u8) len;
1060 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
1061 free(nr);
1062 return UBUS_STATUS_INVALID_ARGUMENT;
1063 }
1064
1065 nr_cur += len;
1066 }
1067 }
1068
1069 if (nr)
1070 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1071
1072 if (tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]))
1073 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1074
1075 if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, 0, NULL,
1076 NULL, nr, nr_len, NULL, 0))
1077 return UBUS_STATUS_UNKNOWN_ERROR;
1078
1079 return 0;
1080 }
1081 #endif
1082
1083 static const struct ubus_method bss_methods[] = {
1084 UBUS_METHOD_NOARG("reload", hostapd_bss_reload),
1085 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
1086 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
1087 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
1088 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
1089 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
1090 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
1091 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features),
1092 #ifdef NEED_AP_MLME
1093 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
1094 #endif
1095 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
1096 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
1097 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
1098 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
1099 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
1100 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
1101 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
1102 #ifdef CONFIG_WNM_AP
1103 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
1104 #endif
1105 };
1106
1107 static struct ubus_object_type bss_object_type =
1108 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
1109
1110 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
1111 {
1112 return memcmp(k1, k2, ETH_ALEN);
1113 }
1114
1115 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
1116 {
1117 struct ubus_object *obj = &hapd->ubus.obj;
1118 char *name;
1119 int ret;
1120
1121 #ifdef CONFIG_MESH
1122 if (hapd->conf->mesh & MESH_ENABLED)
1123 return;
1124 #endif
1125
1126 if (!hostapd_ubus_init())
1127 return;
1128
1129 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
1130 return;
1131
1132 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
1133 obj->name = name;
1134 obj->type = &bss_object_type;
1135 obj->methods = bss_object_type.methods;
1136 obj->n_methods = bss_object_type.n_methods;
1137 ret = ubus_add_object(ctx, obj);
1138 hostapd_ubus_ref_inc();
1139
1140 hostapd_send_shared_event(hapd->conf->iface, "add");
1141 }
1142
1143 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
1144 {
1145 struct ubus_object *obj = &hapd->ubus.obj;
1146 char *name = (char *) obj->name;
1147
1148 if (!ctx)
1149 return;
1150
1151 hostapd_send_shared_event(hapd->conf->iface, "remove");
1152
1153 if (obj->id) {
1154 ubus_remove_object(ctx, obj);
1155 hostapd_ubus_ref_dec();
1156 }
1157
1158 free(name);
1159 }
1160
1161 static const struct ubus_method daemon_methods[] = {
1162 UBUS_METHOD("config_add", hostapd_config_add, config_add_policy),
1163 UBUS_METHOD("config_remove", hostapd_config_remove, config_remove_policy),
1164 };
1165
1166 static struct ubus_object_type daemon_object_type =
1167 UBUS_OBJECT_TYPE("hostapd", daemon_methods);
1168
1169 void hostapd_ubus_add(struct hapd_interfaces *interfaces)
1170 {
1171 struct ubus_object *obj = &interfaces->ubus;
1172 int ret;
1173
1174 if (!hostapd_ubus_init())
1175 return;
1176
1177 obj->name = strdup("hostapd");
1178
1179 obj->type = &daemon_object_type;
1180 obj->methods = daemon_object_type.methods;
1181 obj->n_methods = daemon_object_type.n_methods;
1182 ret = ubus_add_object(ctx, obj);
1183 hostapd_ubus_ref_inc();
1184 }
1185
1186 void hostapd_ubus_free(struct hapd_interfaces *interfaces)
1187 {
1188 struct ubus_object *obj = &interfaces->ubus;
1189 char *name = (char *) obj->name;
1190
1191 if (!ctx)
1192 return;
1193
1194 if (obj->id) {
1195 ubus_remove_object(ctx, obj);
1196 hostapd_ubus_ref_dec();
1197 }
1198
1199 free(name);
1200 }
1201
1202 struct ubus_event_req {
1203 struct ubus_notify_request nreq;
1204 int resp;
1205 };
1206
1207 static void
1208 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1209 {
1210 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1211
1212 ureq->resp = ret;
1213 }
1214
1215 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1216 {
1217 struct ubus_banned_client *ban;
1218 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1219 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1220 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1221 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1222 };
1223 const char *type = "mgmt";
1224 struct ubus_event_req ureq = {};
1225 const u8 *addr;
1226
1227 if (req->mgmt_frame)
1228 addr = req->mgmt_frame->sa;
1229 else
1230 addr = req->addr;
1231
1232 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1233 if (ban)
1234 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1235
1236 if (!hapd->ubus.obj.has_subscribers)
1237 return WLAN_STATUS_SUCCESS;
1238
1239 if (req->type < ARRAY_SIZE(types))
1240 type = types[req->type];
1241
1242 blob_buf_init(&b, 0);
1243 blobmsg_add_macaddr(&b, "address", addr);
1244 if (req->mgmt_frame)
1245 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1246 if (req->ssi_signal)
1247 blobmsg_add_u32(&b, "signal", req->ssi_signal);
1248 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1249
1250 if (req->elems) {
1251 if(req->elems->ht_capabilities)
1252 {
1253 struct ieee80211_ht_capabilities *ht_capabilities;
1254 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1255
1256
1257 ht_capabilities = (struct ieee80211_ht_capabilities*) req->elems->ht_capabilities;
1258 ht_cap = blobmsg_open_table(&b, "ht_capabilities");
1259 blobmsg_add_u16(&b, "ht_capabilities_info", ht_capabilities->ht_capabilities_info);
1260 ht_cap_mcs_set = blobmsg_open_table(&b, "supported_mcs_set");
1261 blobmsg_add_u16(&b, "a_mpdu_params", ht_capabilities->a_mpdu_params);
1262 blobmsg_add_u16(&b, "ht_extended_capabilities", ht_capabilities->ht_extended_capabilities);
1263 blobmsg_add_u32(&b, "tx_bf_capability_info", ht_capabilities->tx_bf_capability_info);
1264 blobmsg_add_u16(&b, "asel_capabilities", ht_capabilities->asel_capabilities);
1265 mcs_set = blobmsg_open_array(&b, "supported_mcs_set");
1266 for (int i = 0; i < 16; i++) {
1267 blobmsg_add_u16(&b, NULL, (u16) ht_capabilities->supported_mcs_set[i]);
1268 }
1269 blobmsg_close_array(&b, mcs_set);
1270 blobmsg_close_table(&b, ht_cap_mcs_set);
1271 blobmsg_close_table(&b, ht_cap);
1272 }
1273 if(req->elems->vht_capabilities)
1274 {
1275 struct ieee80211_vht_capabilities *vht_capabilities;
1276 void *vht_cap, *vht_cap_mcs_set;
1277
1278 vht_capabilities = (struct ieee80211_vht_capabilities*) req->elems->vht_capabilities;
1279 vht_cap = blobmsg_open_table(&b, "vht_capabilities");
1280 blobmsg_add_u32(&b, "vht_capabilities_info", vht_capabilities->vht_capabilities_info);
1281 vht_cap_mcs_set = blobmsg_open_table(&b, "vht_supported_mcs_set");
1282 blobmsg_add_u16(&b, "rx_map", vht_capabilities->vht_supported_mcs_set.rx_map);
1283 blobmsg_add_u16(&b, "rx_highest", vht_capabilities->vht_supported_mcs_set.rx_highest);
1284 blobmsg_add_u16(&b, "tx_map", vht_capabilities->vht_supported_mcs_set.tx_map);
1285 blobmsg_add_u16(&b, "tx_highest", vht_capabilities->vht_supported_mcs_set.tx_highest);
1286 blobmsg_close_table(&b, vht_cap_mcs_set);
1287 blobmsg_close_table(&b, vht_cap);
1288 }
1289 }
1290
1291 if (!hapd->ubus.notify_response) {
1292 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1293 return WLAN_STATUS_SUCCESS;
1294 }
1295
1296 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1297 return WLAN_STATUS_SUCCESS;
1298
1299 ureq.nreq.status_cb = ubus_event_cb;
1300 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1301
1302 if (ureq.resp)
1303 return ureq.resp;
1304
1305 return WLAN_STATUS_SUCCESS;
1306 }
1307
1308 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1309 {
1310 if (!hapd->ubus.obj.has_subscribers)
1311 return;
1312
1313 if (!addr)
1314 return;
1315
1316 blob_buf_init(&b, 0);
1317 blobmsg_add_macaddr(&b, "address", addr);
1318
1319 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1320 }
1321
1322 void hostapd_ubus_notify_beacon_report(
1323 struct hostapd_data *hapd, const u8 *addr, u8 token, u8 rep_mode,
1324 struct rrm_measurement_beacon_report *rep, size_t len)
1325 {
1326 if (!hapd->ubus.obj.has_subscribers)
1327 return;
1328
1329 if (!addr || !rep)
1330 return;
1331
1332 blob_buf_init(&b, 0);
1333 blobmsg_add_macaddr(&b, "address", addr);
1334 blobmsg_add_u16(&b, "op-class", rep->op_class);
1335 blobmsg_add_u16(&b, "channel", rep->channel);
1336 blobmsg_add_u64(&b, "start-time", rep->start_time);
1337 blobmsg_add_u16(&b, "duration", rep->duration);
1338 blobmsg_add_u16(&b, "report-info", rep->report_info);
1339 blobmsg_add_u16(&b, "rcpi", rep->rcpi);
1340 blobmsg_add_u16(&b, "rsni", rep->rsni);
1341 blobmsg_add_macaddr(&b, "bssid", rep->bssid);
1342 blobmsg_add_u16(&b, "antenna-id", rep->antenna_id);
1343 blobmsg_add_u16(&b, "parent-tsf", rep->parent_tsf);
1344
1345 ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
1346 }