hostapd: expose device taxonomy signature via ubus
[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 enum {
202 NOTIFY_RESPONSE,
203 __NOTIFY_MAX
204 };
205
206 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
207 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
208 };
209
210 static int
211 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
212 struct ubus_request_data *req, const char *method,
213 struct blob_attr *msg)
214 {
215 struct blob_attr *tb[__NOTIFY_MAX];
216 struct hostapd_data *hapd = get_hapd_from_object(obj);
217 struct wpabuf *elems;
218 const char *pos;
219 size_t len;
220
221 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
222 blob_data(msg), blob_len(msg));
223
224 if (!tb[NOTIFY_RESPONSE])
225 return UBUS_STATUS_INVALID_ARGUMENT;
226
227 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
228
229 return UBUS_STATUS_OK;
230 }
231
232 enum {
233 DEL_CLIENT_ADDR,
234 DEL_CLIENT_REASON,
235 DEL_CLIENT_DEAUTH,
236 DEL_CLIENT_BAN_TIME,
237 __DEL_CLIENT_MAX
238 };
239
240 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
241 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
242 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
243 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
244 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
245 };
246
247 static int
248 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
249 struct ubus_request_data *req, const char *method,
250 struct blob_attr *msg)
251 {
252 struct blob_attr *tb[__DEL_CLIENT_MAX];
253 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
254 struct sta_info *sta;
255 bool deauth = false;
256 int reason;
257 u8 addr[ETH_ALEN];
258
259 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
260
261 if (!tb[DEL_CLIENT_ADDR])
262 return UBUS_STATUS_INVALID_ARGUMENT;
263
264 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
265 return UBUS_STATUS_INVALID_ARGUMENT;
266
267 if (tb[DEL_CLIENT_REASON])
268 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
269
270 if (tb[DEL_CLIENT_DEAUTH])
271 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
272
273 sta = ap_get_sta(hapd, addr);
274 if (sta) {
275 if (deauth) {
276 hostapd_drv_sta_deauth(hapd, addr, reason);
277 ap_sta_deauthenticate(hapd, sta, reason);
278 } else {
279 hostapd_drv_sta_disassoc(hapd, addr, reason);
280 ap_sta_disassociate(hapd, sta, reason);
281 }
282 }
283
284 if (tb[DEL_CLIENT_BAN_TIME])
285 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
286
287 return 0;
288 }
289
290 static void
291 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
292 {
293 char *s;
294
295 s = blobmsg_alloc_string_buffer(buf, name, 20);
296 sprintf(s, MACSTR, MAC2STR(addr));
297 blobmsg_add_string_buffer(buf);
298 }
299
300 static int
301 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
302 struct ubus_request_data *req, const char *method,
303 struct blob_attr *msg)
304 {
305 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
306 struct ubus_banned_client *ban;
307 void *c;
308
309 blob_buf_init(&b, 0);
310 c = blobmsg_open_array(&b, "clients");
311 avl_for_each_element(&hapd->ubus.banned, ban, avl)
312 blobmsg_add_macaddr(&b, NULL, ban->addr);
313 blobmsg_close_array(&b, c);
314 ubus_send_reply(ctx, req, b.head);
315
316 return 0;
317 }
318
319 static int
320 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
321 struct ubus_request_data *req, const char *method,
322 struct blob_attr *msg)
323 {
324 int rc;
325 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
326
327 rc = hostapd_wps_button_pushed(hapd, NULL);
328
329 if (rc != 0)
330 return UBUS_STATUS_NOT_SUPPORTED;
331
332 return 0;
333 }
334
335 static int
336 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
337 struct ubus_request_data *req, const char *method,
338 struct blob_attr *msg)
339 {
340 int rc;
341 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
342
343 rc = hostapd_wps_cancel(hapd);
344
345 if (rc != 0)
346 return UBUS_STATUS_NOT_SUPPORTED;
347
348 return 0;
349 }
350
351 static int
352 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
353 struct ubus_request_data *req, const char *method,
354 struct blob_attr *msg)
355 {
356 int rc;
357 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
358
359 rc = ieee802_11_set_beacon(hapd);
360
361 if (rc != 0)
362 return UBUS_STATUS_NOT_SUPPORTED;
363
364 return 0;
365 }
366
367 enum {
368 CSA_FREQ,
369 CSA_BCN_COUNT,
370 __CSA_MAX
371 };
372
373 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
374 /*
375 * for now, frequency and beacon count are enough, add more
376 * parameters on demand
377 */
378 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
379 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
380 };
381
382 #ifdef NEED_AP_MLME
383 static int
384 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
385 struct ubus_request_data *req, const char *method,
386 struct blob_attr *msg)
387 {
388 struct blob_attr *tb[__CSA_MAX];
389 struct hostapd_data *hapd = get_hapd_from_object(obj);
390 struct csa_settings css;
391
392 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
393
394 if (!tb[CSA_FREQ])
395 return UBUS_STATUS_INVALID_ARGUMENT;
396
397 memset(&css, 0, sizeof(css));
398 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
399 if (tb[CSA_BCN_COUNT])
400 css.cs_count = blobmsg_get_u32(tb[CSA_BCN_COUNT]);
401
402 if (hostapd_switch_channel(hapd, &css) != 0)
403 return UBUS_STATUS_NOT_SUPPORTED;
404 return UBUS_STATUS_OK;
405 }
406 #endif
407
408 enum {
409 VENDOR_ELEMENTS,
410 __VENDOR_ELEMENTS_MAX
411 };
412
413 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
414 /* vendor elements are provided as hex-string */
415 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
416 };
417
418 static int
419 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
420 struct ubus_request_data *req, const char *method,
421 struct blob_attr *msg)
422 {
423 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
424 struct hostapd_data *hapd = get_hapd_from_object(obj);
425 struct hostapd_bss_config *bss = hapd->conf;
426 struct wpabuf *elems;
427 const char *pos;
428 size_t len;
429
430 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
431 blob_data(msg), blob_len(msg));
432
433 if (!tb[VENDOR_ELEMENTS])
434 return UBUS_STATUS_INVALID_ARGUMENT;
435
436 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
437 len = os_strlen(pos);
438 if (len & 0x01)
439 return UBUS_STATUS_INVALID_ARGUMENT;
440
441 len /= 2;
442 if (len == 0) {
443 wpabuf_free(bss->vendor_elements);
444 bss->vendor_elements = NULL;
445 return 0;
446 }
447
448 elems = wpabuf_alloc(len);
449 if (elems == NULL)
450 return 1;
451
452 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
453 wpabuf_free(elems);
454 return UBUS_STATUS_INVALID_ARGUMENT;
455 }
456
457 wpabuf_free(bss->vendor_elements);
458 bss->vendor_elements = elems;
459
460 /* update beacons if vendor elements were set successfully */
461 if (ieee802_11_update_beacons(hapd->iface) != 0)
462 return UBUS_STATUS_NOT_SUPPORTED;
463 return UBUS_STATUS_OK;
464 }
465
466 static void
467 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
468 {
469 const u8 *data;
470 char *str;
471 int len;
472
473 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
474
475 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
476 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
477 str[nr->ssid.ssid_len] = 0;
478 blobmsg_add_string_buffer(&b);
479
480 len = wpabuf_len(nr->nr);
481 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
482 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
483 blobmsg_add_string_buffer(&b);
484 }
485
486 enum {
487 BSS_MGMT_EN_NEIGHBOR,
488 BSS_MGMT_EN_BEACON,
489 #ifdef CONFIG_WNM_AP
490 BSS_MGMT_EN_BSS_TRANSITION,
491 #endif
492 __BSS_MGMT_EN_MAX
493 };
494
495 static bool
496 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
497 {
498 struct hostapd_bss_config *bss = hapd->conf;
499 uint32_t flags;
500
501 switch (flag) {
502 case BSS_MGMT_EN_NEIGHBOR:
503 if (bss->radio_measurements[0] &
504 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
505 return false;
506
507 bss->radio_measurements[0] |=
508 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
509 hostapd_set_own_neighbor_report(hapd);
510 return true;
511 case BSS_MGMT_EN_BEACON:
512 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
513 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
514 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
515
516 if (bss->radio_measurements[0] & flags == flags)
517 return false;
518
519 bss->radio_measurements[0] |= (u8) flags;
520 return true;
521 #ifdef CONFIG_WNM_AP
522 case BSS_MGMT_EN_BSS_TRANSITION:
523 if (bss->bss_transition)
524 return false;
525
526 bss->bss_transition = 1;
527 return true;
528 #endif
529 }
530 }
531
532 static void
533 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
534 {
535 bool update = false;
536 int i;
537
538 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
539 if (!(flags & (1 << i)))
540 continue;
541
542 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
543 }
544
545 if (update)
546 ieee802_11_update_beacons(hapd->iface);
547 }
548
549
550 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
551 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
552 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
553 #ifdef CONFIG_WNM_AP
554 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
555 #endif
556 };
557
558 static int
559 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
560 struct ubus_request_data *req, const char *method,
561 struct blob_attr *msg)
562
563 {
564 struct hostapd_data *hapd = get_hapd_from_object(obj);
565 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
566 struct blob_attr *cur;
567 uint32_t flags = 0;
568 int i;
569 bool neigh = false, beacon = false;
570
571 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
572
573 for (i = 0; i < ARRAY_SIZE(tb); i++) {
574 if (!tb[i] || !blobmsg_get_bool(tb[i]))
575 continue;
576
577 flags |= (1 << i);
578 }
579
580 __hostapd_bss_mgmt_enable(hapd, flags);
581 }
582
583
584 static void
585 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
586 {
587 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
588 }
589
590 static int
591 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
592 struct ubus_request_data *req, const char *method,
593 struct blob_attr *msg)
594 {
595 struct hostapd_data *hapd = get_hapd_from_object(obj);
596 struct hostapd_neighbor_entry *nr;
597 void *c;
598
599 hostapd_rrm_nr_enable(hapd);
600
601 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
602 if (!nr)
603 return UBUS_STATUS_NOT_FOUND;
604
605 blob_buf_init(&b, 0);
606
607 c = blobmsg_open_array(&b, "value");
608 hostapd_rrm_print_nr(nr);
609 blobmsg_close_array(&b, c);
610
611 ubus_send_reply(ctx, req, b.head);
612
613 return 0;
614 }
615
616 static int
617 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
618 struct ubus_request_data *req, const char *method,
619 struct blob_attr *msg)
620 {
621 struct hostapd_data *hapd = get_hapd_from_object(obj);
622 struct hostapd_neighbor_entry *nr;
623 void *c;
624
625 hostapd_rrm_nr_enable(hapd);
626 blob_buf_init(&b, 0);
627
628 c = blobmsg_open_array(&b, "list");
629 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
630 void *cur;
631
632 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
633 continue;
634
635 cur = blobmsg_open_array(&b, NULL);
636 hostapd_rrm_print_nr(nr);
637 blobmsg_close_array(&b, cur);
638 }
639 blobmsg_close_array(&b, c);
640
641 ubus_send_reply(ctx, req, b.head);
642
643 return 0;
644 }
645
646 enum {
647 NR_SET_LIST,
648 __NR_SET_LIST_MAX
649 };
650
651 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
652 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
653 };
654
655
656 static void
657 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
658 {
659 struct hostapd_neighbor_entry *nr;
660
661 restart:
662 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
663 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
664 continue;
665
666 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
667 goto restart;
668 }
669 }
670
671 static int
672 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
673 struct ubus_request_data *req, const char *method,
674 struct blob_attr *msg)
675 {
676 static const struct blobmsg_policy nr_e_policy[] = {
677 { .type = BLOBMSG_TYPE_STRING },
678 { .type = BLOBMSG_TYPE_STRING },
679 { .type = BLOBMSG_TYPE_STRING },
680 };
681 struct hostapd_data *hapd = get_hapd_from_object(obj);
682 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
683 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
684 struct blob_attr *cur;
685 int ret = 0;
686 int rem;
687
688 hostapd_rrm_nr_enable(hapd);
689
690 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
691 if (!tb_l[NR_SET_LIST])
692 return UBUS_STATUS_INVALID_ARGUMENT;
693
694 hostapd_rrm_nr_clear(hapd);
695 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
696 struct wpa_ssid_value ssid;
697 struct wpabuf *data;
698 u8 bssid[ETH_ALEN];
699 char *s;
700
701 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
702 if (!tb[0] || !tb[1] || !tb[2])
703 goto invalid;
704
705 s = blobmsg_get_string(tb[0]);
706 if (hwaddr_aton(s, bssid))
707 goto invalid;
708
709 s = blobmsg_get_string(tb[1]);
710 ssid.ssid_len = strlen(s);
711 if (ssid.ssid_len > sizeof(ssid.ssid))
712 goto invalid;
713
714 memcpy(&ssid, s, ssid.ssid_len);
715 data = wpabuf_parse_bin(blobmsg_get_string(tb[2]));
716 if (!data)
717 goto invalid;
718
719 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
720 wpabuf_free(data);
721 continue;
722
723 invalid:
724 ret = UBUS_STATUS_INVALID_ARGUMENT;
725 }
726
727 return 0;
728 }
729
730 enum {
731 BEACON_REQ_ADDR,
732 BEACON_REQ_MODE,
733 BEACON_REQ_OP_CLASS,
734 BEACON_REQ_CHANNEL,
735 BEACON_REQ_DURATION,
736 BEACON_REQ_BSSID,
737 BEACON_REQ_SSID,
738 __BEACON_REQ_MAX,
739 };
740
741 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
742 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
743 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
744 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
745 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
746 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
747 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
748 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
749 };
750
751 static int
752 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
753 struct ubus_request_data *ureq, const char *method,
754 struct blob_attr *msg)
755 {
756 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
757 struct blob_attr *tb[__BEACON_REQ_MAX];
758 struct blob_attr *cur;
759 struct wpabuf *req;
760 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
761 u8 addr[ETH_ALEN];
762 int mode, rem, ret;
763 int buf_len = 13;
764
765 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
766
767 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
768 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
769 return UBUS_STATUS_INVALID_ARGUMENT;
770
771 if (tb[BEACON_REQ_SSID])
772 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
773
774 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
775 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
776 return UBUS_STATUS_INVALID_ARGUMENT;
777
778 if (tb[BEACON_REQ_BSSID] &&
779 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
780 return UBUS_STATUS_INVALID_ARGUMENT;
781
782 req = wpabuf_alloc(buf_len);
783 if (!req)
784 return UBUS_STATUS_UNKNOWN_ERROR;
785
786 /* 1: regulatory class */
787 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
788
789 /* 2: channel number */
790 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
791
792 /* 3-4: randomization interval */
793 wpabuf_put_le16(req, 0);
794
795 /* 5-6: duration */
796 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
797
798 /* 7: mode */
799 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
800
801 /* 8-13: BSSID */
802 wpabuf_put_data(req, bssid, ETH_ALEN);
803
804 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
805 wpabuf_put_u8(req, WLAN_EID_SSID);
806 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
807 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
808 }
809
810 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
811 if (ret < 0)
812 return -ret;
813
814 return 0;
815 }
816
817
818 #ifdef CONFIG_WNM_AP
819 enum {
820 WNM_DISASSOC_ADDR,
821 WNM_DISASSOC_DURATION,
822 WNM_DISASSOC_NEIGHBORS,
823 __WNM_DISASSOC_MAX,
824 };
825
826 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
827 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
828 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
829 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
830 };
831
832 static int
833 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
834 struct ubus_request_data *ureq, const char *method,
835 struct blob_attr *msg)
836 {
837 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
838 struct blob_attr *tb[__WNM_DISASSOC_MAX];
839 struct blob_attr *cur;
840 struct sta_info *sta;
841 int duration = 10;
842 int rem;
843 int nr_len = 0;
844 u8 *nr = NULL;
845 u8 req_mode = WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
846 u8 addr[ETH_ALEN];
847
848 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
849
850 if (!tb[WNM_DISASSOC_ADDR])
851 return UBUS_STATUS_INVALID_ARGUMENT;
852
853 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
854 return UBUS_STATUS_INVALID_ARGUMENT;
855
856 if ((cur = tb[WNM_DISASSOC_DURATION]) != NULL)
857 duration = blobmsg_get_u32(cur);
858
859 sta = ap_get_sta(hapd, addr);
860 if (!sta)
861 return UBUS_STATUS_NOT_FOUND;
862
863 if (tb[WNM_DISASSOC_NEIGHBORS]) {
864 u8 *nr_cur;
865
866 if (blobmsg_check_array(tb[WNM_DISASSOC_NEIGHBORS],
867 BLOBMSG_TYPE_STRING) < 0)
868 return UBUS_STATUS_INVALID_ARGUMENT;
869
870 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
871 int len = strlen(blobmsg_get_string(cur));
872
873 if (len % 2)
874 return UBUS_STATUS_INVALID_ARGUMENT;
875
876 nr_len += (len / 2) + 2;
877 }
878
879 if (nr_len) {
880 nr = os_zalloc(nr_len);
881 if (!nr)
882 return UBUS_STATUS_UNKNOWN_ERROR;
883 }
884
885 nr_cur = nr;
886 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
887 int len = strlen(blobmsg_get_string(cur)) / 2;
888
889 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
890 *nr_cur++ = (u8) len;
891 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
892 free(nr);
893 return UBUS_STATUS_INVALID_ARGUMENT;
894 }
895
896 nr_cur += len;
897 }
898 }
899
900 if (nr)
901 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
902
903 if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, 0, NULL,
904 NULL, nr, nr_len, NULL, 0))
905 return UBUS_STATUS_UNKNOWN_ERROR;
906
907 return 0;
908 }
909 #endif
910
911 static const struct ubus_method bss_methods[] = {
912 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
913 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
914 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
915 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
916 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
917 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
918 #ifdef NEED_AP_MLME
919 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
920 #endif
921 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
922 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
923 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
924 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
925 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
926 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
927 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
928 #ifdef CONFIG_WNM_AP
929 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
930 #endif
931 };
932
933 static struct ubus_object_type bss_object_type =
934 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
935
936 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
937 {
938 return memcmp(k1, k2, ETH_ALEN);
939 }
940
941 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
942 {
943 struct ubus_object *obj = &hapd->ubus.obj;
944 char *name;
945 int ret;
946
947 #ifdef CONFIG_MESH
948 if (hapd->conf->mesh & MESH_ENABLED)
949 return;
950 #endif
951
952 if (!hostapd_ubus_init())
953 return;
954
955 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
956 return;
957
958 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
959 obj->name = name;
960 obj->type = &bss_object_type;
961 obj->methods = bss_object_type.methods;
962 obj->n_methods = bss_object_type.n_methods;
963 ret = ubus_add_object(ctx, obj);
964 hostapd_ubus_ref_inc();
965 }
966
967 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
968 {
969 struct ubus_object *obj = &hapd->ubus.obj;
970 char *name = (char *) obj->name;
971
972 if (!ctx)
973 return;
974
975 if (obj->id) {
976 ubus_remove_object(ctx, obj);
977 hostapd_ubus_ref_dec();
978 }
979
980 free(name);
981 }
982
983 struct ubus_event_req {
984 struct ubus_notify_request nreq;
985 int resp;
986 };
987
988 static void
989 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
990 {
991 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
992
993 ureq->resp = ret;
994 }
995
996 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
997 {
998 struct ubus_banned_client *ban;
999 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1000 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1001 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1002 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1003 };
1004 const char *type = "mgmt";
1005 struct ubus_event_req ureq = {};
1006 const u8 *addr;
1007
1008 if (req->mgmt_frame)
1009 addr = req->mgmt_frame->sa;
1010 else
1011 addr = req->addr;
1012
1013 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1014 if (ban)
1015 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1016
1017 if (!hapd->ubus.obj.has_subscribers)
1018 return WLAN_STATUS_SUCCESS;
1019
1020 if (req->type < ARRAY_SIZE(types))
1021 type = types[req->type];
1022
1023 blob_buf_init(&b, 0);
1024 blobmsg_add_macaddr(&b, "address", addr);
1025 if (req->mgmt_frame)
1026 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1027 if (req->frame_info)
1028 blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
1029 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1030
1031 if (!hapd->ubus.notify_response) {
1032 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1033 return WLAN_STATUS_SUCCESS;
1034 }
1035
1036 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1037 return WLAN_STATUS_SUCCESS;
1038
1039 ureq.nreq.status_cb = ubus_event_cb;
1040 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1041
1042 if (ureq.resp)
1043 return ureq.resp;
1044
1045 return WLAN_STATUS_SUCCESS;
1046 }
1047
1048 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1049 {
1050 if (!hapd->ubus.obj.has_subscribers)
1051 return;
1052
1053 if (!addr)
1054 return;
1055
1056 blob_buf_init(&b, 0);
1057 blobmsg_add_macaddr(&b, "address", addr);
1058
1059 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1060 }