3dfc0a268fdd4a74f2ab9ac1c0eb3f87223d0664
[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
22 static struct ubus_context *ctx;
23 static struct blob_buf b;
24 static int ctx_ref;
25
26 static inline struct hostapd_data *get_hapd_from_object(struct ubus_object *obj)
27 {
28 return container_of(obj, struct hostapd_data, ubus.obj);
29 }
30
31
32 struct ubus_banned_client {
33 struct avl_node avl;
34 u8 addr[ETH_ALEN];
35 };
36
37 static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
38 {
39 struct ubus_context *ctx = eloop_ctx;
40 ubus_handle_event(ctx);
41 }
42
43 static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
44 {
45 if (ubus_reconnect(ctx, NULL)) {
46 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
47 return;
48 }
49
50 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
51 }
52
53 static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
54 {
55 eloop_unregister_read_sock(ctx->sock.fd);
56 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
57 }
58
59 static bool hostapd_ubus_init(void)
60 {
61 if (ctx)
62 return true;
63
64 ctx = ubus_connect(NULL);
65 if (!ctx)
66 return false;
67
68 ctx->connection_lost = hostapd_ubus_connection_lost;
69 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
70 return true;
71 }
72
73 static void hostapd_ubus_ref_inc(void)
74 {
75 ctx_ref++;
76 }
77
78 static void hostapd_ubus_ref_dec(void)
79 {
80 ctx_ref--;
81 if (!ctx)
82 return;
83
84 if (ctx_ref)
85 return;
86
87 eloop_unregister_read_sock(ctx->sock.fd);
88 ubus_free(ctx);
89 ctx = NULL;
90 }
91
92 void hostapd_ubus_add_iface(struct hostapd_iface *iface)
93 {
94 if (!hostapd_ubus_init())
95 return;
96 }
97
98 void hostapd_ubus_free_iface(struct hostapd_iface *iface)
99 {
100 if (!ctx)
101 return;
102 }
103
104 static void
105 hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
106 {
107 struct ubus_banned_client *ban = eloop_data;
108 struct hostapd_data *hapd = user_ctx;
109
110 avl_delete(&hapd->ubus.banned, &ban->avl);
111 free(ban);
112 }
113
114 static void
115 hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
116 {
117 struct ubus_banned_client *ban;
118
119 if (time < 0)
120 time = 0;
121
122 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
123 if (!ban) {
124 if (!time)
125 return;
126
127 ban = os_zalloc(sizeof(*ban));
128 memcpy(ban->addr, addr, sizeof(ban->addr));
129 ban->avl.key = ban->addr;
130 avl_insert(&hapd->ubus.banned, &ban->avl);
131 } else {
132 eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
133 if (!time) {
134 hostapd_bss_del_ban(ban, hapd);
135 return;
136 }
137 }
138
139 eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
140 }
141
142 static int
143 hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
144 struct ubus_request_data *req, const char *method,
145 struct blob_attr *msg)
146 {
147 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
148 struct sta_info *sta;
149 void *list, *c;
150 char mac_buf[20];
151 static const struct {
152 const char *name;
153 uint32_t flag;
154 } sta_flags[] = {
155 { "auth", WLAN_STA_AUTH },
156 { "assoc", WLAN_STA_ASSOC },
157 { "authorized", WLAN_STA_AUTHORIZED },
158 { "preauth", WLAN_STA_PREAUTH },
159 { "wds", WLAN_STA_WDS },
160 { "wmm", WLAN_STA_WMM },
161 { "ht", WLAN_STA_HT },
162 { "vht", WLAN_STA_VHT },
163 { "wps", WLAN_STA_WPS },
164 { "mfp", WLAN_STA_MFP },
165 };
166
167 blob_buf_init(&b, 0);
168 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
169 list = blobmsg_open_table(&b, "clients");
170 for (sta = hapd->sta_list; sta; sta = sta->next) {
171 int i;
172
173 sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
174 c = blobmsg_open_table(&b, mac_buf);
175 for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
176 blobmsg_add_u8(&b, sta_flags[i].name,
177 !!(sta->flags & sta_flags[i].flag));
178 blobmsg_add_u32(&b, "aid", sta->aid);
179 blobmsg_close_table(&b, c);
180 }
181 blobmsg_close_array(&b, list);
182 ubus_send_reply(ctx, req, b.head);
183
184 return 0;
185 }
186
187 enum {
188 NOTIFY_RESPONSE,
189 __NOTIFY_MAX
190 };
191
192 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
193 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
194 };
195
196 static int
197 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
198 struct ubus_request_data *req, const char *method,
199 struct blob_attr *msg)
200 {
201 struct blob_attr *tb[__NOTIFY_MAX];
202 struct hostapd_data *hapd = get_hapd_from_object(obj);
203 struct wpabuf *elems;
204 const char *pos;
205 size_t len;
206
207 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
208 blob_data(msg), blob_len(msg));
209
210 if (!tb[NOTIFY_RESPONSE])
211 return UBUS_STATUS_INVALID_ARGUMENT;
212
213 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
214
215 return UBUS_STATUS_OK;
216 }
217
218 enum {
219 DEL_CLIENT_ADDR,
220 DEL_CLIENT_REASON,
221 DEL_CLIENT_DEAUTH,
222 DEL_CLIENT_BAN_TIME,
223 __DEL_CLIENT_MAX
224 };
225
226 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
227 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
228 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
229 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
230 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
231 };
232
233 static int
234 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
235 struct ubus_request_data *req, const char *method,
236 struct blob_attr *msg)
237 {
238 struct blob_attr *tb[__DEL_CLIENT_MAX];
239 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
240 struct sta_info *sta;
241 bool deauth = false;
242 int reason;
243 u8 addr[ETH_ALEN];
244
245 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
246
247 if (!tb[DEL_CLIENT_ADDR])
248 return UBUS_STATUS_INVALID_ARGUMENT;
249
250 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
251 return UBUS_STATUS_INVALID_ARGUMENT;
252
253 if (tb[DEL_CLIENT_REASON])
254 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
255
256 if (tb[DEL_CLIENT_DEAUTH])
257 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
258
259 sta = ap_get_sta(hapd, addr);
260 if (sta) {
261 if (deauth) {
262 hostapd_drv_sta_deauth(hapd, addr, reason);
263 ap_sta_deauthenticate(hapd, sta, reason);
264 } else {
265 hostapd_drv_sta_disassoc(hapd, addr, reason);
266 ap_sta_disassociate(hapd, sta, reason);
267 }
268 }
269
270 if (tb[DEL_CLIENT_BAN_TIME])
271 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
272
273 return 0;
274 }
275
276 static void
277 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
278 {
279 char *s;
280
281 s = blobmsg_alloc_string_buffer(buf, name, 20);
282 sprintf(s, MACSTR, MAC2STR(addr));
283 blobmsg_add_string_buffer(buf);
284 }
285
286 static int
287 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
288 struct ubus_request_data *req, const char *method,
289 struct blob_attr *msg)
290 {
291 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
292 struct ubus_banned_client *ban;
293 void *c;
294
295 blob_buf_init(&b, 0);
296 c = blobmsg_open_array(&b, "clients");
297 avl_for_each_element(&hapd->ubus.banned, ban, avl)
298 blobmsg_add_macaddr(&b, NULL, ban->addr);
299 blobmsg_close_array(&b, c);
300 ubus_send_reply(ctx, req, b.head);
301
302 return 0;
303 }
304
305 static int
306 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
307 struct ubus_request_data *req, const char *method,
308 struct blob_attr *msg)
309 {
310 int rc;
311 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
312
313 rc = hostapd_wps_button_pushed(hapd, NULL);
314
315 if (rc != 0)
316 return UBUS_STATUS_NOT_SUPPORTED;
317
318 return 0;
319 }
320
321 static int
322 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
323 struct ubus_request_data *req, const char *method,
324 struct blob_attr *msg)
325 {
326 int rc;
327 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
328
329 rc = hostapd_wps_cancel(hapd);
330
331 if (rc != 0)
332 return UBUS_STATUS_NOT_SUPPORTED;
333
334 return 0;
335 }
336
337 static int
338 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
339 struct ubus_request_data *req, const char *method,
340 struct blob_attr *msg)
341 {
342 int rc;
343 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
344
345 rc = ieee802_11_set_beacon(hapd);
346
347 if (rc != 0)
348 return UBUS_STATUS_NOT_SUPPORTED;
349
350 return 0;
351 }
352
353 enum {
354 CSA_FREQ,
355 CSA_BCN_COUNT,
356 __CSA_MAX
357 };
358
359 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
360 /*
361 * for now, frequency and beacon count are enough, add more
362 * parameters on demand
363 */
364 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
365 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
366 };
367
368 #ifdef NEED_AP_MLME
369 static int
370 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
371 struct ubus_request_data *req, const char *method,
372 struct blob_attr *msg)
373 {
374 struct blob_attr *tb[__CSA_MAX];
375 struct hostapd_data *hapd = get_hapd_from_object(obj);
376 struct csa_settings css;
377
378 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
379
380 if (!tb[CSA_FREQ])
381 return UBUS_STATUS_INVALID_ARGUMENT;
382
383 memset(&css, 0, sizeof(css));
384 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
385 if (tb[CSA_BCN_COUNT])
386 css.cs_count = blobmsg_get_u32(tb[CSA_BCN_COUNT]);
387
388 if (hostapd_switch_channel(hapd, &css) != 0)
389 return UBUS_STATUS_NOT_SUPPORTED;
390 return UBUS_STATUS_OK;
391 }
392 #endif
393
394 enum {
395 VENDOR_ELEMENTS,
396 __VENDOR_ELEMENTS_MAX
397 };
398
399 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
400 /* vendor elements are provided as hex-string */
401 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
402 };
403
404 static int
405 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
406 struct ubus_request_data *req, const char *method,
407 struct blob_attr *msg)
408 {
409 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
410 struct hostapd_data *hapd = get_hapd_from_object(obj);
411 struct hostapd_bss_config *bss = hapd->conf;
412 struct wpabuf *elems;
413 const char *pos;
414 size_t len;
415
416 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
417 blob_data(msg), blob_len(msg));
418
419 if (!tb[VENDOR_ELEMENTS])
420 return UBUS_STATUS_INVALID_ARGUMENT;
421
422 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
423 len = os_strlen(pos);
424 if (len & 0x01)
425 return UBUS_STATUS_INVALID_ARGUMENT;
426
427 len /= 2;
428 if (len == 0) {
429 wpabuf_free(bss->vendor_elements);
430 bss->vendor_elements = NULL;
431 return 0;
432 }
433
434 elems = wpabuf_alloc(len);
435 if (elems == NULL)
436 return 1;
437
438 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
439 wpabuf_free(elems);
440 return UBUS_STATUS_INVALID_ARGUMENT;
441 }
442
443 wpabuf_free(bss->vendor_elements);
444 bss->vendor_elements = elems;
445
446 /* update beacons if vendor elements were set successfully */
447 if (ieee802_11_update_beacons(hapd->iface) != 0)
448 return UBUS_STATUS_NOT_SUPPORTED;
449 return UBUS_STATUS_OK;
450 }
451
452 static void
453 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
454 {
455 const u8 *data;
456 char *str;
457 int len;
458
459 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
460
461 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
462 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
463 str[nr->ssid.ssid_len] = 0;
464 blobmsg_add_string_buffer(&b);
465
466 len = wpabuf_len(nr->nr);
467 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
468 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
469 blobmsg_add_string_buffer(&b);
470 }
471
472 enum {
473 BSS_MGMT_EN_NEIGHBOR,
474 BSS_MGMT_EN_BEACON,
475 #ifdef CONFIG_WNM_AP
476 BSS_MGMT_EN_BSS_TRANSITION,
477 #endif
478 __BSS_MGMT_EN_MAX
479 };
480
481 static bool
482 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
483 {
484 struct hostapd_bss_config *bss = hapd->conf;
485 uint32_t flags;
486
487 switch (flag) {
488 case BSS_MGMT_EN_NEIGHBOR:
489 if (bss->radio_measurements[0] &
490 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
491 return false;
492
493 bss->radio_measurements[0] |=
494 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
495 hostapd_set_own_neighbor_report(hapd);
496 return true;
497 case BSS_MGMT_EN_BEACON:
498 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
499 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
500 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
501
502 if (bss->radio_measurements[0] & flags == flags)
503 return false;
504
505 bss->radio_measurements[0] |= (u8) flags;
506 return true;
507 #ifdef CONFIG_WNM_AP
508 case BSS_MGMT_EN_BSS_TRANSITION:
509 if (bss->bss_transition)
510 return false;
511
512 bss->bss_transition = 1;
513 return true;
514 #endif
515 }
516 }
517
518 static void
519 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
520 {
521 bool update = false;
522 int i;
523
524 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
525 if (!(flags & (1 << i)))
526 continue;
527
528 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
529 }
530
531 if (update)
532 ieee802_11_update_beacons(hapd->iface);
533 }
534
535
536 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
537 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
538 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
539 #ifdef CONFIG_WNM_AP
540 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
541 #endif
542 };
543
544 static int
545 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
546 struct ubus_request_data *req, const char *method,
547 struct blob_attr *msg)
548
549 {
550 struct hostapd_data *hapd = get_hapd_from_object(obj);
551 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
552 struct blob_attr *cur;
553 uint32_t flags = 0;
554 int i;
555 bool neigh = false, beacon = false;
556
557 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
558
559 for (i = 0; i < ARRAY_SIZE(tb); i++) {
560 if (!tb[i] || !blobmsg_get_bool(tb[i]))
561 continue;
562
563 flags |= (1 << i);
564 }
565
566 __hostapd_bss_mgmt_enable(hapd, flags);
567 }
568
569
570 static void
571 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
572 {
573 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
574 }
575
576 static int
577 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
578 struct ubus_request_data *req, const char *method,
579 struct blob_attr *msg)
580 {
581 struct hostapd_data *hapd = get_hapd_from_object(obj);
582 struct hostapd_neighbor_entry *nr;
583 void *c;
584
585 hostapd_rrm_nr_enable(hapd);
586
587 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
588 if (!nr)
589 return UBUS_STATUS_NOT_FOUND;
590
591 blob_buf_init(&b, 0);
592
593 c = blobmsg_open_array(&b, "value");
594 hostapd_rrm_print_nr(nr);
595 blobmsg_close_array(&b, c);
596
597 ubus_send_reply(ctx, req, b.head);
598
599 return 0;
600 }
601
602 static int
603 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
604 struct ubus_request_data *req, const char *method,
605 struct blob_attr *msg)
606 {
607 struct hostapd_data *hapd = get_hapd_from_object(obj);
608 struct hostapd_neighbor_entry *nr;
609 void *c;
610
611 hostapd_rrm_nr_enable(hapd);
612 blob_buf_init(&b, 0);
613
614 c = blobmsg_open_array(&b, "list");
615 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
616 void *cur;
617
618 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
619 continue;
620
621 cur = blobmsg_open_array(&b, NULL);
622 hostapd_rrm_print_nr(nr);
623 blobmsg_close_array(&b, cur);
624 }
625 blobmsg_close_array(&b, c);
626
627 ubus_send_reply(ctx, req, b.head);
628
629 return 0;
630 }
631
632 enum {
633 NR_SET_LIST,
634 __NR_SET_LIST_MAX
635 };
636
637 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
638 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
639 };
640
641
642 static void
643 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
644 {
645 struct hostapd_neighbor_entry *nr;
646
647 restart:
648 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
649 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
650 continue;
651
652 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
653 goto restart;
654 }
655 }
656
657 static int
658 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
659 struct ubus_request_data *req, const char *method,
660 struct blob_attr *msg)
661 {
662 static const struct blobmsg_policy nr_e_policy[] = {
663 { .type = BLOBMSG_TYPE_STRING },
664 { .type = BLOBMSG_TYPE_STRING },
665 { .type = BLOBMSG_TYPE_STRING },
666 };
667 struct hostapd_data *hapd = get_hapd_from_object(obj);
668 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
669 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
670 struct blob_attr *cur;
671 int ret = 0;
672 int rem;
673
674 hostapd_rrm_nr_enable(hapd);
675
676 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
677 if (!tb_l[NR_SET_LIST])
678 return UBUS_STATUS_INVALID_ARGUMENT;
679
680 hostapd_rrm_nr_clear(hapd);
681 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
682 struct wpa_ssid_value ssid;
683 struct wpabuf *data;
684 u8 bssid[ETH_ALEN];
685 char *s;
686
687 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
688 if (!tb[0] || !tb[1] || !tb[2])
689 goto invalid;
690
691 s = blobmsg_get_string(tb[0]);
692 if (hwaddr_aton(s, bssid))
693 goto invalid;
694
695 s = blobmsg_get_string(tb[1]);
696 ssid.ssid_len = strlen(s);
697 if (ssid.ssid_len > sizeof(ssid.ssid))
698 goto invalid;
699
700 memcpy(&ssid, s, ssid.ssid_len);
701 data = wpabuf_parse_bin(blobmsg_get_string(tb[2]));
702 if (!data)
703 goto invalid;
704
705 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
706 wpabuf_free(data);
707 continue;
708
709 invalid:
710 ret = UBUS_STATUS_INVALID_ARGUMENT;
711 }
712
713 return 0;
714 }
715
716 static const struct ubus_method bss_methods[] = {
717 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
718 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
719 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
720 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
721 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
722 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
723 #ifdef NEED_AP_MLME
724 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
725 #endif
726 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
727 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
728 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
729 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
730 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
731 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
732 };
733
734 static struct ubus_object_type bss_object_type =
735 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
736
737 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
738 {
739 return memcmp(k1, k2, ETH_ALEN);
740 }
741
742 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
743 {
744 struct ubus_object *obj = &hapd->ubus.obj;
745 char *name;
746 int ret;
747
748 if (!hostapd_ubus_init())
749 return;
750
751 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
752 return;
753
754 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
755 obj->name = name;
756 obj->type = &bss_object_type;
757 obj->methods = bss_object_type.methods;
758 obj->n_methods = bss_object_type.n_methods;
759 ret = ubus_add_object(ctx, obj);
760 hostapd_ubus_ref_inc();
761 }
762
763 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
764 {
765 struct ubus_object *obj = &hapd->ubus.obj;
766 char *name = (char *) obj->name;
767
768 if (!ctx)
769 return;
770
771 if (obj->id) {
772 ubus_remove_object(ctx, obj);
773 hostapd_ubus_ref_dec();
774 }
775
776 free(name);
777 }
778
779 struct ubus_event_req {
780 struct ubus_notify_request nreq;
781 int resp;
782 };
783
784 static void
785 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
786 {
787 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
788
789 ureq->resp = ret;
790 }
791
792 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
793 {
794 struct ubus_banned_client *ban;
795 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
796 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
797 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
798 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
799 };
800 const char *type = "mgmt";
801 struct ubus_event_req ureq = {};
802 const u8 *addr;
803
804 if (req->mgmt_frame)
805 addr = req->mgmt_frame->sa;
806 else
807 addr = req->addr;
808
809 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
810 if (ban)
811 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
812
813 if (!hapd->ubus.obj.has_subscribers)
814 return WLAN_STATUS_SUCCESS;
815
816 if (req->type < ARRAY_SIZE(types))
817 type = types[req->type];
818
819 blob_buf_init(&b, 0);
820 blobmsg_add_macaddr(&b, "address", addr);
821 if (req->mgmt_frame)
822 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
823 if (req->frame_info)
824 blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
825 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
826
827 if (!hapd->ubus.notify_response) {
828 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
829 return WLAN_STATUS_SUCCESS;
830 }
831
832 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
833 return WLAN_STATUS_SUCCESS;
834
835 ureq.nreq.status_cb = ubus_event_cb;
836 ubus_complete_request(ctx, &ureq.nreq.req, 100);
837
838 if (ureq.resp)
839 return ureq.resp;
840
841 return WLAN_STATUS_SUCCESS;
842 }
843
844 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
845 {
846 if (!hapd->ubus.obj.has_subscribers)
847 return;
848
849 if (!addr)
850 return;
851
852 blob_buf_init(&b, 0);
853 blobmsg_add_macaddr(&b, "address", addr);
854
855 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
856 }