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