hostapd: add ht and vht support in handle event function Add ht and vht capabilities...
[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 hostapd_data *get_hapd_from_object(struct ubus_object *obj)
30 {
31 return container_of(obj, struct hostapd_data, ubus.obj);
32 }
33
34
35 struct ubus_banned_client {
36 struct avl_node avl;
37 u8 addr[ETH_ALEN];
38 };
39
40 static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
41 {
42 struct ubus_context *ctx = eloop_ctx;
43 ubus_handle_event(ctx);
44 }
45
46 static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
47 {
48 if (ubus_reconnect(ctx, NULL)) {
49 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
50 return;
51 }
52
53 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
54 }
55
56 static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
57 {
58 eloop_unregister_read_sock(ctx->sock.fd);
59 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
60 }
61
62 static bool hostapd_ubus_init(void)
63 {
64 if (ctx)
65 return true;
66
67 ctx = ubus_connect(NULL);
68 if (!ctx)
69 return false;
70
71 ctx->connection_lost = hostapd_ubus_connection_lost;
72 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
73 return true;
74 }
75
76 static void hostapd_ubus_ref_inc(void)
77 {
78 ctx_ref++;
79 }
80
81 static void hostapd_ubus_ref_dec(void)
82 {
83 ctx_ref--;
84 if (!ctx)
85 return;
86
87 if (ctx_ref)
88 return;
89
90 eloop_unregister_read_sock(ctx->sock.fd);
91 ubus_free(ctx);
92 ctx = NULL;
93 }
94
95 void hostapd_ubus_add_iface(struct hostapd_iface *iface)
96 {
97 if (!hostapd_ubus_init())
98 return;
99 }
100
101 void hostapd_ubus_free_iface(struct hostapd_iface *iface)
102 {
103 if (!ctx)
104 return;
105 }
106
107 static void
108 hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
109 {
110 struct ubus_banned_client *ban = eloop_data;
111 struct hostapd_data *hapd = user_ctx;
112
113 avl_delete(&hapd->ubus.banned, &ban->avl);
114 free(ban);
115 }
116
117 static void
118 hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
119 {
120 struct ubus_banned_client *ban;
121
122 if (time < 0)
123 time = 0;
124
125 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
126 if (!ban) {
127 if (!time)
128 return;
129
130 ban = os_zalloc(sizeof(*ban));
131 memcpy(ban->addr, addr, sizeof(ban->addr));
132 ban->avl.key = ban->addr;
133 avl_insert(&hapd->ubus.banned, &ban->avl);
134 } else {
135 eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
136 if (!time) {
137 hostapd_bss_del_ban(ban, hapd);
138 return;
139 }
140 }
141
142 eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
143 }
144
145 static int
146 hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
147 struct ubus_request_data *req, const char *method,
148 struct blob_attr *msg)
149 {
150 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
151 struct sta_info *sta;
152 void *list, *c;
153 char mac_buf[20];
154 static const struct {
155 const char *name;
156 uint32_t flag;
157 } sta_flags[] = {
158 { "auth", WLAN_STA_AUTH },
159 { "assoc", WLAN_STA_ASSOC },
160 { "authorized", WLAN_STA_AUTHORIZED },
161 { "preauth", WLAN_STA_PREAUTH },
162 { "wds", WLAN_STA_WDS },
163 { "wmm", WLAN_STA_WMM },
164 { "ht", WLAN_STA_HT },
165 { "vht", WLAN_STA_VHT },
166 { "wps", WLAN_STA_WPS },
167 { "mfp", WLAN_STA_MFP },
168 };
169
170 blob_buf_init(&b, 0);
171 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
172 list = blobmsg_open_table(&b, "clients");
173 for (sta = hapd->sta_list; sta; sta = sta->next) {
174 void *r;
175 int i;
176
177 sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
178 c = blobmsg_open_table(&b, mac_buf);
179 for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
180 blobmsg_add_u8(&b, sta_flags[i].name,
181 !!(sta->flags & sta_flags[i].flag));
182
183 r = blobmsg_open_array(&b, "rrm");
184 for (i = 0; i < ARRAY_SIZE(sta->rrm_enabled_capa); i++)
185 blobmsg_add_u32(&b, "", sta->rrm_enabled_capa[i]);
186 blobmsg_close_array(&b, r);
187 blobmsg_add_u32(&b, "aid", sta->aid);
188 #ifdef CONFIG_TAXONOMY
189 r = blobmsg_alloc_string_buffer(&b, "signature", 1024);
190 if (retrieve_sta_taxonomy(hapd, sta, r, 1024) > 0)
191 blobmsg_add_string_buffer(&b);
192 #endif
193 blobmsg_close_table(&b, c);
194 }
195 blobmsg_close_array(&b, list);
196 ubus_send_reply(ctx, req, b.head);
197
198 return 0;
199 }
200
201 static int
202 hostapd_bss_get_features(struct ubus_context *ctx, struct ubus_object *obj,
203 struct ubus_request_data *req, const char *method,
204 struct blob_attr *msg)
205 {
206 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
207
208 blob_buf_init(&b, 0);
209 blobmsg_add_u8(&b, "ht_supported", ht_supported(hapd->iface->hw_features));
210 blobmsg_add_u8(&b, "vht_supported", vht_supported(hapd->iface->hw_features));
211 ubus_send_reply(ctx, req, b.head);
212
213 return 0;
214 }
215
216 enum {
217 NOTIFY_RESPONSE,
218 __NOTIFY_MAX
219 };
220
221 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
222 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
223 };
224
225 static int
226 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
227 struct ubus_request_data *req, const char *method,
228 struct blob_attr *msg)
229 {
230 struct blob_attr *tb[__NOTIFY_MAX];
231 struct hostapd_data *hapd = get_hapd_from_object(obj);
232 struct wpabuf *elems;
233 const char *pos;
234 size_t len;
235
236 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
237 blob_data(msg), blob_len(msg));
238
239 if (!tb[NOTIFY_RESPONSE])
240 return UBUS_STATUS_INVALID_ARGUMENT;
241
242 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
243
244 return UBUS_STATUS_OK;
245 }
246
247 enum {
248 DEL_CLIENT_ADDR,
249 DEL_CLIENT_REASON,
250 DEL_CLIENT_DEAUTH,
251 DEL_CLIENT_BAN_TIME,
252 __DEL_CLIENT_MAX
253 };
254
255 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
256 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
257 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
258 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
259 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
260 };
261
262 static int
263 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
264 struct ubus_request_data *req, const char *method,
265 struct blob_attr *msg)
266 {
267 struct blob_attr *tb[__DEL_CLIENT_MAX];
268 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
269 struct sta_info *sta;
270 bool deauth = false;
271 int reason;
272 u8 addr[ETH_ALEN];
273
274 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
275
276 if (!tb[DEL_CLIENT_ADDR])
277 return UBUS_STATUS_INVALID_ARGUMENT;
278
279 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
280 return UBUS_STATUS_INVALID_ARGUMENT;
281
282 if (tb[DEL_CLIENT_REASON])
283 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
284
285 if (tb[DEL_CLIENT_DEAUTH])
286 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
287
288 sta = ap_get_sta(hapd, addr);
289 if (sta) {
290 if (deauth) {
291 hostapd_drv_sta_deauth(hapd, addr, reason);
292 ap_sta_deauthenticate(hapd, sta, reason);
293 } else {
294 hostapd_drv_sta_disassoc(hapd, addr, reason);
295 ap_sta_disassociate(hapd, sta, reason);
296 }
297 }
298
299 if (tb[DEL_CLIENT_BAN_TIME])
300 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
301
302 return 0;
303 }
304
305 static void
306 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
307 {
308 char *s;
309
310 s = blobmsg_alloc_string_buffer(buf, name, 20);
311 sprintf(s, MACSTR, MAC2STR(addr));
312 blobmsg_add_string_buffer(buf);
313 }
314
315 static int
316 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
317 struct ubus_request_data *req, const char *method,
318 struct blob_attr *msg)
319 {
320 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
321 struct ubus_banned_client *ban;
322 void *c;
323
324 blob_buf_init(&b, 0);
325 c = blobmsg_open_array(&b, "clients");
326 avl_for_each_element(&hapd->ubus.banned, ban, avl)
327 blobmsg_add_macaddr(&b, NULL, ban->addr);
328 blobmsg_close_array(&b, c);
329 ubus_send_reply(ctx, req, b.head);
330
331 return 0;
332 }
333
334 static int
335 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
336 struct ubus_request_data *req, const char *method,
337 struct blob_attr *msg)
338 {
339 int rc;
340 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
341
342 rc = hostapd_wps_button_pushed(hapd, NULL);
343
344 if (rc != 0)
345 return UBUS_STATUS_NOT_SUPPORTED;
346
347 return 0;
348 }
349
350 static int
351 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
352 struct ubus_request_data *req, const char *method,
353 struct blob_attr *msg)
354 {
355 int rc;
356 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
357
358 rc = hostapd_wps_cancel(hapd);
359
360 if (rc != 0)
361 return UBUS_STATUS_NOT_SUPPORTED;
362
363 return 0;
364 }
365
366 static int
367 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
368 struct ubus_request_data *req, const char *method,
369 struct blob_attr *msg)
370 {
371 int rc;
372 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
373
374 rc = ieee802_11_set_beacon(hapd);
375
376 if (rc != 0)
377 return UBUS_STATUS_NOT_SUPPORTED;
378
379 return 0;
380 }
381
382 enum {
383 CSA_FREQ,
384 CSA_BCN_COUNT,
385 __CSA_MAX
386 };
387
388 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
389 /*
390 * for now, frequency and beacon count are enough, add more
391 * parameters on demand
392 */
393 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
394 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
395 };
396
397 #ifdef NEED_AP_MLME
398 static int
399 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
400 struct ubus_request_data *req, const char *method,
401 struct blob_attr *msg)
402 {
403 struct blob_attr *tb[__CSA_MAX];
404 struct hostapd_data *hapd = get_hapd_from_object(obj);
405 struct csa_settings css;
406
407 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
408
409 if (!tb[CSA_FREQ])
410 return UBUS_STATUS_INVALID_ARGUMENT;
411
412 memset(&css, 0, sizeof(css));
413 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
414 if (tb[CSA_BCN_COUNT])
415 css.cs_count = blobmsg_get_u32(tb[CSA_BCN_COUNT]);
416
417 if (hostapd_switch_channel(hapd, &css) != 0)
418 return UBUS_STATUS_NOT_SUPPORTED;
419 return UBUS_STATUS_OK;
420 }
421 #endif
422
423 enum {
424 VENDOR_ELEMENTS,
425 __VENDOR_ELEMENTS_MAX
426 };
427
428 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
429 /* vendor elements are provided as hex-string */
430 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
431 };
432
433 static int
434 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
435 struct ubus_request_data *req, const char *method,
436 struct blob_attr *msg)
437 {
438 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
439 struct hostapd_data *hapd = get_hapd_from_object(obj);
440 struct hostapd_bss_config *bss = hapd->conf;
441 struct wpabuf *elems;
442 const char *pos;
443 size_t len;
444
445 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
446 blob_data(msg), blob_len(msg));
447
448 if (!tb[VENDOR_ELEMENTS])
449 return UBUS_STATUS_INVALID_ARGUMENT;
450
451 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
452 len = os_strlen(pos);
453 if (len & 0x01)
454 return UBUS_STATUS_INVALID_ARGUMENT;
455
456 len /= 2;
457 if (len == 0) {
458 wpabuf_free(bss->vendor_elements);
459 bss->vendor_elements = NULL;
460 return 0;
461 }
462
463 elems = wpabuf_alloc(len);
464 if (elems == NULL)
465 return 1;
466
467 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
468 wpabuf_free(elems);
469 return UBUS_STATUS_INVALID_ARGUMENT;
470 }
471
472 wpabuf_free(bss->vendor_elements);
473 bss->vendor_elements = elems;
474
475 /* update beacons if vendor elements were set successfully */
476 if (ieee802_11_update_beacons(hapd->iface) != 0)
477 return UBUS_STATUS_NOT_SUPPORTED;
478 return UBUS_STATUS_OK;
479 }
480
481 static void
482 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
483 {
484 const u8 *data;
485 char *str;
486 int len;
487
488 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
489
490 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
491 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
492 str[nr->ssid.ssid_len] = 0;
493 blobmsg_add_string_buffer(&b);
494
495 len = wpabuf_len(nr->nr);
496 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
497 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
498 blobmsg_add_string_buffer(&b);
499 }
500
501 enum {
502 BSS_MGMT_EN_NEIGHBOR,
503 BSS_MGMT_EN_BEACON,
504 #ifdef CONFIG_WNM_AP
505 BSS_MGMT_EN_BSS_TRANSITION,
506 #endif
507 __BSS_MGMT_EN_MAX
508 };
509
510 static bool
511 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
512 {
513 struct hostapd_bss_config *bss = hapd->conf;
514 uint32_t flags;
515
516 switch (flag) {
517 case BSS_MGMT_EN_NEIGHBOR:
518 if (bss->radio_measurements[0] &
519 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
520 return false;
521
522 bss->radio_measurements[0] |=
523 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
524 hostapd_set_own_neighbor_report(hapd);
525 return true;
526 case BSS_MGMT_EN_BEACON:
527 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
528 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
529 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
530
531 if (bss->radio_measurements[0] & flags == flags)
532 return false;
533
534 bss->radio_measurements[0] |= (u8) flags;
535 return true;
536 #ifdef CONFIG_WNM_AP
537 case BSS_MGMT_EN_BSS_TRANSITION:
538 if (bss->bss_transition)
539 return false;
540
541 bss->bss_transition = 1;
542 return true;
543 #endif
544 }
545 }
546
547 static void
548 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
549 {
550 bool update = false;
551 int i;
552
553 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
554 if (!(flags & (1 << i)))
555 continue;
556
557 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
558 }
559
560 if (update)
561 ieee802_11_update_beacons(hapd->iface);
562 }
563
564
565 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
566 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
567 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
568 #ifdef CONFIG_WNM_AP
569 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
570 #endif
571 };
572
573 static int
574 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
575 struct ubus_request_data *req, const char *method,
576 struct blob_attr *msg)
577
578 {
579 struct hostapd_data *hapd = get_hapd_from_object(obj);
580 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
581 struct blob_attr *cur;
582 uint32_t flags = 0;
583 int i;
584 bool neigh = false, beacon = false;
585
586 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
587
588 for (i = 0; i < ARRAY_SIZE(tb); i++) {
589 if (!tb[i] || !blobmsg_get_bool(tb[i]))
590 continue;
591
592 flags |= (1 << i);
593 }
594
595 __hostapd_bss_mgmt_enable(hapd, flags);
596 }
597
598
599 static void
600 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
601 {
602 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
603 }
604
605 static int
606 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
607 struct ubus_request_data *req, const char *method,
608 struct blob_attr *msg)
609 {
610 struct hostapd_data *hapd = get_hapd_from_object(obj);
611 struct hostapd_neighbor_entry *nr;
612 void *c;
613
614 hostapd_rrm_nr_enable(hapd);
615
616 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
617 if (!nr)
618 return UBUS_STATUS_NOT_FOUND;
619
620 blob_buf_init(&b, 0);
621
622 c = blobmsg_open_array(&b, "value");
623 hostapd_rrm_print_nr(nr);
624 blobmsg_close_array(&b, c);
625
626 ubus_send_reply(ctx, req, b.head);
627
628 return 0;
629 }
630
631 static int
632 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
633 struct ubus_request_data *req, const char *method,
634 struct blob_attr *msg)
635 {
636 struct hostapd_data *hapd = get_hapd_from_object(obj);
637 struct hostapd_neighbor_entry *nr;
638 void *c;
639
640 hostapd_rrm_nr_enable(hapd);
641 blob_buf_init(&b, 0);
642
643 c = blobmsg_open_array(&b, "list");
644 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
645 void *cur;
646
647 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
648 continue;
649
650 cur = blobmsg_open_array(&b, NULL);
651 hostapd_rrm_print_nr(nr);
652 blobmsg_close_array(&b, cur);
653 }
654 blobmsg_close_array(&b, c);
655
656 ubus_send_reply(ctx, req, b.head);
657
658 return 0;
659 }
660
661 enum {
662 NR_SET_LIST,
663 __NR_SET_LIST_MAX
664 };
665
666 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
667 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
668 };
669
670
671 static void
672 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
673 {
674 struct hostapd_neighbor_entry *nr;
675
676 restart:
677 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
678 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
679 continue;
680
681 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
682 goto restart;
683 }
684 }
685
686 static int
687 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
688 struct ubus_request_data *req, const char *method,
689 struct blob_attr *msg)
690 {
691 static const struct blobmsg_policy nr_e_policy[] = {
692 { .type = BLOBMSG_TYPE_STRING },
693 { .type = BLOBMSG_TYPE_STRING },
694 { .type = BLOBMSG_TYPE_STRING },
695 };
696 struct hostapd_data *hapd = get_hapd_from_object(obj);
697 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
698 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
699 struct blob_attr *cur;
700 int ret = 0;
701 int rem;
702
703 hostapd_rrm_nr_enable(hapd);
704
705 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
706 if (!tb_l[NR_SET_LIST])
707 return UBUS_STATUS_INVALID_ARGUMENT;
708
709 hostapd_rrm_nr_clear(hapd);
710 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
711 struct wpa_ssid_value ssid;
712 struct wpabuf *data;
713 u8 bssid[ETH_ALEN];
714 char *s;
715
716 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
717 if (!tb[0] || !tb[1] || !tb[2])
718 goto invalid;
719
720 s = blobmsg_get_string(tb[0]);
721 if (hwaddr_aton(s, bssid))
722 goto invalid;
723
724 s = blobmsg_get_string(tb[1]);
725 ssid.ssid_len = strlen(s);
726 if (ssid.ssid_len > sizeof(ssid.ssid))
727 goto invalid;
728
729 memcpy(&ssid, s, ssid.ssid_len);
730 data = wpabuf_parse_bin(blobmsg_get_string(tb[2]));
731 if (!data)
732 goto invalid;
733
734 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
735 wpabuf_free(data);
736 continue;
737
738 invalid:
739 ret = UBUS_STATUS_INVALID_ARGUMENT;
740 }
741
742 return 0;
743 }
744
745 enum {
746 BEACON_REQ_ADDR,
747 BEACON_REQ_MODE,
748 BEACON_REQ_OP_CLASS,
749 BEACON_REQ_CHANNEL,
750 BEACON_REQ_DURATION,
751 BEACON_REQ_BSSID,
752 BEACON_REQ_SSID,
753 __BEACON_REQ_MAX,
754 };
755
756 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
757 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
758 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
759 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
760 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
761 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
762 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
763 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
764 };
765
766 static int
767 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
768 struct ubus_request_data *ureq, const char *method,
769 struct blob_attr *msg)
770 {
771 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
772 struct blob_attr *tb[__BEACON_REQ_MAX];
773 struct blob_attr *cur;
774 struct wpabuf *req;
775 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
776 u8 addr[ETH_ALEN];
777 int mode, rem, ret;
778 int buf_len = 13;
779
780 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
781
782 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
783 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
784 return UBUS_STATUS_INVALID_ARGUMENT;
785
786 if (tb[BEACON_REQ_SSID])
787 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
788
789 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
790 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
791 return UBUS_STATUS_INVALID_ARGUMENT;
792
793 if (tb[BEACON_REQ_BSSID] &&
794 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
795 return UBUS_STATUS_INVALID_ARGUMENT;
796
797 req = wpabuf_alloc(buf_len);
798 if (!req)
799 return UBUS_STATUS_UNKNOWN_ERROR;
800
801 /* 1: regulatory class */
802 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
803
804 /* 2: channel number */
805 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
806
807 /* 3-4: randomization interval */
808 wpabuf_put_le16(req, 0);
809
810 /* 5-6: duration */
811 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
812
813 /* 7: mode */
814 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
815
816 /* 8-13: BSSID */
817 wpabuf_put_data(req, bssid, ETH_ALEN);
818
819 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
820 wpabuf_put_u8(req, WLAN_EID_SSID);
821 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
822 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
823 }
824
825 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
826 if (ret < 0)
827 return -ret;
828
829 return 0;
830 }
831
832
833 #ifdef CONFIG_WNM_AP
834 enum {
835 WNM_DISASSOC_ADDR,
836 WNM_DISASSOC_DURATION,
837 WNM_DISASSOC_NEIGHBORS,
838 __WNM_DISASSOC_MAX,
839 };
840
841 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
842 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
843 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
844 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
845 };
846
847 static int
848 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
849 struct ubus_request_data *ureq, const char *method,
850 struct blob_attr *msg)
851 {
852 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
853 struct blob_attr *tb[__WNM_DISASSOC_MAX];
854 struct blob_attr *cur;
855 struct sta_info *sta;
856 int duration = 10;
857 int rem;
858 int nr_len = 0;
859 u8 *nr = NULL;
860 u8 req_mode = WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
861 u8 addr[ETH_ALEN];
862
863 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
864
865 if (!tb[WNM_DISASSOC_ADDR])
866 return UBUS_STATUS_INVALID_ARGUMENT;
867
868 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
869 return UBUS_STATUS_INVALID_ARGUMENT;
870
871 if ((cur = tb[WNM_DISASSOC_DURATION]) != NULL)
872 duration = blobmsg_get_u32(cur);
873
874 sta = ap_get_sta(hapd, addr);
875 if (!sta)
876 return UBUS_STATUS_NOT_FOUND;
877
878 if (tb[WNM_DISASSOC_NEIGHBORS]) {
879 u8 *nr_cur;
880
881 if (blobmsg_check_array(tb[WNM_DISASSOC_NEIGHBORS],
882 BLOBMSG_TYPE_STRING) < 0)
883 return UBUS_STATUS_INVALID_ARGUMENT;
884
885 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
886 int len = strlen(blobmsg_get_string(cur));
887
888 if (len % 2)
889 return UBUS_STATUS_INVALID_ARGUMENT;
890
891 nr_len += (len / 2) + 2;
892 }
893
894 if (nr_len) {
895 nr = os_zalloc(nr_len);
896 if (!nr)
897 return UBUS_STATUS_UNKNOWN_ERROR;
898 }
899
900 nr_cur = nr;
901 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
902 int len = strlen(blobmsg_get_string(cur)) / 2;
903
904 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
905 *nr_cur++ = (u8) len;
906 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
907 free(nr);
908 return UBUS_STATUS_INVALID_ARGUMENT;
909 }
910
911 nr_cur += len;
912 }
913 }
914
915 if (nr)
916 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
917
918 if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, 0, NULL,
919 NULL, nr, nr_len, NULL, 0))
920 return UBUS_STATUS_UNKNOWN_ERROR;
921
922 return 0;
923 }
924 #endif
925
926 static const struct ubus_method bss_methods[] = {
927 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
928 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
929 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
930 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
931 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
932 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
933 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features),
934 #ifdef NEED_AP_MLME
935 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
936 #endif
937 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
938 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
939 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
940 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
941 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
942 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
943 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
944 #ifdef CONFIG_WNM_AP
945 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
946 #endif
947 };
948
949 static struct ubus_object_type bss_object_type =
950 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
951
952 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
953 {
954 return memcmp(k1, k2, ETH_ALEN);
955 }
956
957 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
958 {
959 struct ubus_object *obj = &hapd->ubus.obj;
960 char *name;
961 int ret;
962
963 #ifdef CONFIG_MESH
964 if (hapd->conf->mesh & MESH_ENABLED)
965 return;
966 #endif
967
968 if (!hostapd_ubus_init())
969 return;
970
971 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
972 return;
973
974 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
975 obj->name = name;
976 obj->type = &bss_object_type;
977 obj->methods = bss_object_type.methods;
978 obj->n_methods = bss_object_type.n_methods;
979 ret = ubus_add_object(ctx, obj);
980 hostapd_ubus_ref_inc();
981 }
982
983 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
984 {
985 struct ubus_object *obj = &hapd->ubus.obj;
986 char *name = (char *) obj->name;
987
988 if (!ctx)
989 return;
990
991 if (obj->id) {
992 ubus_remove_object(ctx, obj);
993 hostapd_ubus_ref_dec();
994 }
995
996 free(name);
997 }
998
999 struct ubus_event_req {
1000 struct ubus_notify_request nreq;
1001 int resp;
1002 };
1003
1004 static void
1005 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1006 {
1007 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1008
1009 ureq->resp = ret;
1010 }
1011
1012 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1013 {
1014 struct ubus_banned_client *ban;
1015 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1016 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1017 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1018 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1019 };
1020 const char *type = "mgmt";
1021 struct ubus_event_req ureq = {};
1022 const u8 *addr;
1023
1024 if (req->mgmt_frame)
1025 addr = req->mgmt_frame->sa;
1026 else
1027 addr = req->addr;
1028
1029 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1030 if (ban)
1031 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1032
1033 if (!hapd->ubus.obj.has_subscribers)
1034 return WLAN_STATUS_SUCCESS;
1035
1036 if (req->type < ARRAY_SIZE(types))
1037 type = types[req->type];
1038
1039 blob_buf_init(&b, 0);
1040 blobmsg_add_macaddr(&b, "address", addr);
1041 if (req->mgmt_frame)
1042 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1043 if (req->frame_info)
1044 blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
1045 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1046
1047 if (req->elems) {
1048 if(req->elems->ht_capabilities)
1049 {
1050 struct ieee80211_ht_capabilities *ht_capabilities;
1051 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1052
1053
1054 ht_capabilities = (struct ieee80211_ht_capabilities*) req->elems->ht_capabilities;
1055 ht_cap = blobmsg_open_table(&b, "ht_capabilities");
1056 blobmsg_add_u16(&b, "ht_capabilities_info", ht_capabilities->ht_capabilities_info);
1057 ht_cap_mcs_set = blobmsg_open_table(&b, "supported_mcs_set");
1058 blobmsg_add_u16(&b, "a_mpdu_params", ht_capabilities->a_mpdu_params);
1059 blobmsg_add_u16(&b, "ht_extended_capabilities", ht_capabilities->ht_extended_capabilities);
1060 blobmsg_add_u32(&b, "tx_bf_capability_info", ht_capabilities->tx_bf_capability_info);
1061 blobmsg_add_u16(&b, "asel_capabilities", ht_capabilities->asel_capabilities);
1062 mcs_set = blobmsg_open_array(&b, "supported_mcs_set");
1063 for (int i = 0; i < 16; i++) {
1064 blobmsg_add_u16(&b, NULL, (u16) ht_capabilities->supported_mcs_set[i]);
1065 }
1066 blobmsg_close_array(&b, mcs_set);
1067 blobmsg_close_table(&b, ht_cap_mcs_set);
1068 blobmsg_close_table(&b, ht_cap);
1069 }
1070 if(req->elems->vht_capabilities)
1071 {
1072 struct ieee80211_vht_capabilities *vht_capabilities;
1073 void *vht_cap, *vht_cap_mcs_set;
1074
1075 vht_capabilities = (struct ieee80211_vht_capabilities*) req->elems->vht_capabilities;
1076 vht_cap = blobmsg_open_table(&b, "vht_capabilities");
1077 blobmsg_add_u32(&b, "vht_capabilities_info", vht_capabilities->vht_capabilities_info);
1078 vht_cap_mcs_set = blobmsg_open_table(&b, "vht_supported_mcs_set");
1079 blobmsg_add_u16(&b, "rx_map", vht_capabilities->vht_supported_mcs_set.rx_map);
1080 blobmsg_add_u16(&b, "rx_highest", vht_capabilities->vht_supported_mcs_set.rx_highest);
1081 blobmsg_add_u16(&b, "tx_map", vht_capabilities->vht_supported_mcs_set.tx_map);
1082 blobmsg_add_u16(&b, "tx_highest", vht_capabilities->vht_supported_mcs_set.tx_highest);
1083 blobmsg_close_table(&b, vht_cap_mcs_set);
1084 blobmsg_close_table(&b, vht_cap);
1085 }
1086 }
1087
1088 if (!hapd->ubus.notify_response) {
1089 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1090 return WLAN_STATUS_SUCCESS;
1091 }
1092
1093 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1094 return WLAN_STATUS_SUCCESS;
1095
1096 ureq.nreq.status_cb = ubus_event_cb;
1097 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1098
1099 if (ureq.resp)
1100 return ureq.resp;
1101
1102 return WLAN_STATUS_SUCCESS;
1103 }
1104
1105 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1106 {
1107 if (!hapd->ubus.obj.has_subscribers)
1108 return;
1109
1110 if (!addr)
1111 return;
1112
1113 blob_buf_init(&b, 0);
1114 blobmsg_add_macaddr(&b, "address", addr);
1115
1116 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1117 }