hostapd: ubus: add handler for wps_status and guard WPS calls
[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 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 #ifdef CONFIG_WPS
399 static int
400 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
401 struct ubus_request_data *req, const char *method,
402 struct blob_attr *msg)
403 {
404 int rc;
405 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
406
407 rc = hostapd_wps_button_pushed(hapd, NULL);
408
409 if (rc != 0)
410 return UBUS_STATUS_NOT_SUPPORTED;
411
412 return 0;
413 }
414
415
416 static const char * pbc_status_enum_str(enum pbc_status status)
417 {
418 switch (status) {
419 case WPS_PBC_STATUS_DISABLE:
420 return "Disabled";
421 case WPS_PBC_STATUS_ACTIVE:
422 return "Active";
423 case WPS_PBC_STATUS_TIMEOUT:
424 return "Timed-out";
425 case WPS_PBC_STATUS_OVERLAP:
426 return "Overlap";
427 default:
428 return "Unknown";
429 }
430 }
431
432 static int
433 hostapd_bss_wps_status(struct ubus_context *ctx, struct ubus_object *obj,
434 struct ubus_request_data *req, const char *method,
435 struct blob_attr *msg)
436 {
437 int rc;
438 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
439
440 blob_buf_init(&b, 0);
441
442 blobmsg_add_string(&b, "pbc_status", pbc_status_enum_str(hapd->wps_stats.pbc_status));
443 blobmsg_add_string(&b, "last_wps_result",
444 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
445 "Success":
446 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
447 "Failed" : "None")));
448
449 /* If status == Failure - Add possible Reasons */
450 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
451 hapd->wps_stats.failure_reason > 0)
452 blobmsg_add_string(&b, "reason", wps_ei_str(hapd->wps_stats.failure_reason));
453
454 if (hapd->wps_stats.status)
455 blobmsg_printf(&b, "peer_address", MACSTR, MAC2STR(hapd->wps_stats.peer_addr));
456
457 ubus_send_reply(ctx, req, b.head);
458
459 return 0;
460 }
461
462 static int
463 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
464 struct ubus_request_data *req, const char *method,
465 struct blob_attr *msg)
466 {
467 int rc;
468 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
469
470 rc = hostapd_wps_cancel(hapd);
471
472 if (rc != 0)
473 return UBUS_STATUS_NOT_SUPPORTED;
474
475 return 0;
476 }
477 #endif /* CONFIG_WPS */
478
479 static int
480 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
481 struct ubus_request_data *req, const char *method,
482 struct blob_attr *msg)
483 {
484 int rc;
485 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
486
487 rc = ieee802_11_set_beacon(hapd);
488
489 if (rc != 0)
490 return UBUS_STATUS_NOT_SUPPORTED;
491
492 return 0;
493 }
494
495 enum {
496 CONFIG_IFACE,
497 CONFIG_FILE,
498 __CONFIG_MAX
499 };
500
501 static const struct blobmsg_policy config_add_policy[__CONFIG_MAX] = {
502 [CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
503 [CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
504 };
505
506 static int
507 hostapd_config_add(struct ubus_context *ctx, struct ubus_object *obj,
508 struct ubus_request_data *req, const char *method,
509 struct blob_attr *msg)
510 {
511 struct blob_attr *tb[__CONFIG_MAX];
512 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
513 char buf[128];
514
515 blobmsg_parse(config_add_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
516
517 if (!tb[CONFIG_FILE] || !tb[CONFIG_IFACE])
518 return UBUS_STATUS_INVALID_ARGUMENT;
519
520 snprintf(buf, sizeof(buf), "bss_config=%s:%s",
521 blobmsg_get_string(tb[CONFIG_IFACE]),
522 blobmsg_get_string(tb[CONFIG_FILE]));
523
524 if (hostapd_add_iface(interfaces, buf))
525 return UBUS_STATUS_INVALID_ARGUMENT;
526
527 return UBUS_STATUS_OK;
528 }
529
530 enum {
531 CONFIG_REM_IFACE,
532 __CONFIG_REM_MAX
533 };
534
535 static const struct blobmsg_policy config_remove_policy[__CONFIG_REM_MAX] = {
536 [CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
537 };
538
539 static int
540 hostapd_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
541 struct ubus_request_data *req, const char *method,
542 struct blob_attr *msg)
543 {
544 struct blob_attr *tb[__CONFIG_REM_MAX];
545 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
546 char buf[128];
547
548 blobmsg_parse(config_remove_policy, __CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
549
550 if (!tb[CONFIG_REM_IFACE])
551 return UBUS_STATUS_INVALID_ARGUMENT;
552
553 if (hostapd_remove_iface(interfaces, blobmsg_get_string(tb[CONFIG_REM_IFACE])))
554 return UBUS_STATUS_INVALID_ARGUMENT;
555
556 return UBUS_STATUS_OK;
557 }
558
559 enum {
560 CSA_FREQ,
561 CSA_BCN_COUNT,
562 CSA_CENTER_FREQ1,
563 CSA_CENTER_FREQ2,
564 CSA_BANDWIDTH,
565 CSA_SEC_CHANNEL_OFFSET,
566 CSA_HT,
567 CSA_VHT,
568 CSA_BLOCK_TX,
569 __CSA_MAX
570 };
571
572 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
573 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
574 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
575 [CSA_CENTER_FREQ1] = { "center_freq1", BLOBMSG_TYPE_INT32 },
576 [CSA_CENTER_FREQ2] = { "center_freq2", BLOBMSG_TYPE_INT32 },
577 [CSA_BANDWIDTH] = { "bandwidth", BLOBMSG_TYPE_INT32 },
578 [CSA_SEC_CHANNEL_OFFSET] = { "sec_channel_offset", BLOBMSG_TYPE_INT32 },
579 [CSA_HT] = { "ht", BLOBMSG_TYPE_BOOL },
580 [CSA_VHT] = { "vht", BLOBMSG_TYPE_BOOL },
581 [CSA_BLOCK_TX] = { "block_tx", BLOBMSG_TYPE_BOOL },
582 };
583
584 #ifdef NEED_AP_MLME
585 static int
586 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
587 struct ubus_request_data *req, const char *method,
588 struct blob_attr *msg)
589 {
590 struct blob_attr *tb[__CSA_MAX];
591 struct hostapd_data *hapd = get_hapd_from_object(obj);
592 struct csa_settings css;
593
594 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
595
596 if (!tb[CSA_FREQ])
597 return UBUS_STATUS_INVALID_ARGUMENT;
598
599 memset(&css, 0, sizeof(css));
600 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
601
602 #define SET_CSA_SETTING(name, field, type) \
603 do { \
604 if (tb[name]) \
605 css.field = blobmsg_get_ ## type(tb[name]); \
606 } while(0)
607
608 SET_CSA_SETTING(CSA_BCN_COUNT, cs_count, u32);
609 SET_CSA_SETTING(CSA_CENTER_FREQ1, freq_params.center_freq1, u32);
610 SET_CSA_SETTING(CSA_CENTER_FREQ2, freq_params.center_freq2, u32);
611 SET_CSA_SETTING(CSA_BANDWIDTH, freq_params.bandwidth, u32);
612 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET, freq_params.sec_channel_offset, u32);
613 SET_CSA_SETTING(CSA_HT, freq_params.ht_enabled, bool);
614 SET_CSA_SETTING(CSA_VHT, freq_params.vht_enabled, bool);
615 SET_CSA_SETTING(CSA_BLOCK_TX, block_tx, bool);
616
617
618 if (hostapd_switch_channel(hapd, &css) != 0)
619 return UBUS_STATUS_NOT_SUPPORTED;
620 return UBUS_STATUS_OK;
621 #undef SET_CSA_SETTING
622 }
623 #endif
624
625 enum {
626 VENDOR_ELEMENTS,
627 __VENDOR_ELEMENTS_MAX
628 };
629
630 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
631 /* vendor elements are provided as hex-string */
632 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
633 };
634
635 static int
636 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
637 struct ubus_request_data *req, const char *method,
638 struct blob_attr *msg)
639 {
640 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
641 struct hostapd_data *hapd = get_hapd_from_object(obj);
642 struct hostapd_bss_config *bss = hapd->conf;
643 struct wpabuf *elems;
644 const char *pos;
645 size_t len;
646
647 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
648 blob_data(msg), blob_len(msg));
649
650 if (!tb[VENDOR_ELEMENTS])
651 return UBUS_STATUS_INVALID_ARGUMENT;
652
653 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
654 len = os_strlen(pos);
655 if (len & 0x01)
656 return UBUS_STATUS_INVALID_ARGUMENT;
657
658 len /= 2;
659 if (len == 0) {
660 wpabuf_free(bss->vendor_elements);
661 bss->vendor_elements = NULL;
662 return 0;
663 }
664
665 elems = wpabuf_alloc(len);
666 if (elems == NULL)
667 return 1;
668
669 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
670 wpabuf_free(elems);
671 return UBUS_STATUS_INVALID_ARGUMENT;
672 }
673
674 wpabuf_free(bss->vendor_elements);
675 bss->vendor_elements = elems;
676
677 /* update beacons if vendor elements were set successfully */
678 if (ieee802_11_update_beacons(hapd->iface) != 0)
679 return UBUS_STATUS_NOT_SUPPORTED;
680 return UBUS_STATUS_OK;
681 }
682
683 static void
684 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
685 {
686 const u8 *data;
687 char *str;
688 int len;
689
690 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
691
692 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
693 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
694 str[nr->ssid.ssid_len] = 0;
695 blobmsg_add_string_buffer(&b);
696
697 len = wpabuf_len(nr->nr);
698 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
699 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
700 blobmsg_add_string_buffer(&b);
701 }
702
703 enum {
704 BSS_MGMT_EN_NEIGHBOR,
705 BSS_MGMT_EN_BEACON,
706 #ifdef CONFIG_WNM_AP
707 BSS_MGMT_EN_BSS_TRANSITION,
708 #endif
709 __BSS_MGMT_EN_MAX
710 };
711
712 static bool
713 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
714 {
715 struct hostapd_bss_config *bss = hapd->conf;
716 uint32_t flags;
717
718 switch (flag) {
719 case BSS_MGMT_EN_NEIGHBOR:
720 if (bss->radio_measurements[0] &
721 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
722 return false;
723
724 bss->radio_measurements[0] |=
725 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
726 hostapd_neighbor_set_own_report(hapd);
727 return true;
728 case BSS_MGMT_EN_BEACON:
729 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
730 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
731 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
732
733 if (bss->radio_measurements[0] & flags == flags)
734 return false;
735
736 bss->radio_measurements[0] |= (u8) flags;
737 return true;
738 #ifdef CONFIG_WNM_AP
739 case BSS_MGMT_EN_BSS_TRANSITION:
740 if (bss->bss_transition)
741 return false;
742
743 bss->bss_transition = 1;
744 return true;
745 #endif
746 }
747 }
748
749 static void
750 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
751 {
752 bool update = false;
753 int i;
754
755 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
756 if (!(flags & (1 << i)))
757 continue;
758
759 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
760 }
761
762 if (update)
763 ieee802_11_update_beacons(hapd->iface);
764 }
765
766
767 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
768 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
769 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
770 #ifdef CONFIG_WNM_AP
771 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
772 #endif
773 };
774
775 static int
776 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
777 struct ubus_request_data *req, const char *method,
778 struct blob_attr *msg)
779
780 {
781 struct hostapd_data *hapd = get_hapd_from_object(obj);
782 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
783 struct blob_attr *cur;
784 uint32_t flags = 0;
785 int i;
786 bool neigh = false, beacon = false;
787
788 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
789
790 for (i = 0; i < ARRAY_SIZE(tb); i++) {
791 if (!tb[i] || !blobmsg_get_bool(tb[i]))
792 continue;
793
794 flags |= (1 << i);
795 }
796
797 __hostapd_bss_mgmt_enable(hapd, flags);
798 }
799
800
801 static void
802 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
803 {
804 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
805 }
806
807 static int
808 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
809 struct ubus_request_data *req, const char *method,
810 struct blob_attr *msg)
811 {
812 struct hostapd_data *hapd = get_hapd_from_object(obj);
813 struct hostapd_neighbor_entry *nr;
814 void *c;
815
816 hostapd_rrm_nr_enable(hapd);
817
818 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
819 if (!nr)
820 return UBUS_STATUS_NOT_FOUND;
821
822 blob_buf_init(&b, 0);
823
824 c = blobmsg_open_array(&b, "value");
825 hostapd_rrm_print_nr(nr);
826 blobmsg_close_array(&b, c);
827
828 ubus_send_reply(ctx, req, b.head);
829
830 return 0;
831 }
832
833 static int
834 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
835 struct ubus_request_data *req, const char *method,
836 struct blob_attr *msg)
837 {
838 struct hostapd_data *hapd = get_hapd_from_object(obj);
839 struct hostapd_neighbor_entry *nr;
840 void *c;
841
842 hostapd_rrm_nr_enable(hapd);
843 blob_buf_init(&b, 0);
844
845 c = blobmsg_open_array(&b, "list");
846 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
847 void *cur;
848
849 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
850 continue;
851
852 cur = blobmsg_open_array(&b, NULL);
853 hostapd_rrm_print_nr(nr);
854 blobmsg_close_array(&b, cur);
855 }
856 blobmsg_close_array(&b, c);
857
858 ubus_send_reply(ctx, req, b.head);
859
860 return 0;
861 }
862
863 enum {
864 NR_SET_LIST,
865 __NR_SET_LIST_MAX
866 };
867
868 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
869 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
870 };
871
872
873 static void
874 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
875 {
876 struct hostapd_neighbor_entry *nr;
877
878 restart:
879 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
880 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
881 continue;
882
883 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
884 goto restart;
885 }
886 }
887
888 static int
889 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
890 struct ubus_request_data *req, const char *method,
891 struct blob_attr *msg)
892 {
893 static const struct blobmsg_policy nr_e_policy[] = {
894 { .type = BLOBMSG_TYPE_STRING },
895 { .type = BLOBMSG_TYPE_STRING },
896 { .type = BLOBMSG_TYPE_STRING },
897 };
898 struct hostapd_data *hapd = get_hapd_from_object(obj);
899 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
900 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
901 struct blob_attr *cur;
902 int rem;
903
904 hostapd_rrm_nr_enable(hapd);
905
906 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
907 if (!tb_l[NR_SET_LIST])
908 return UBUS_STATUS_INVALID_ARGUMENT;
909
910 hostapd_rrm_nr_clear(hapd);
911 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
912 struct wpa_ssid_value ssid;
913 struct wpabuf *data;
914 u8 bssid[ETH_ALEN];
915 char *s, *nr_s;
916
917 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
918 if (!tb[0] || !tb[1] || !tb[2])
919 goto invalid;
920
921 /* Neighbor Report binary */
922 nr_s = blobmsg_get_string(tb[2]);
923 data = wpabuf_parse_bin(nr_s);
924 if (!data)
925 goto invalid;
926
927 /* BSSID */
928 s = blobmsg_get_string(tb[0]);
929 if (strlen(s) == 0) {
930 /* Copy BSSID from neighbor report */
931 if (hwaddr_compact_aton(nr_s, bssid))
932 goto invalid;
933 } else if (hwaddr_aton(s, bssid)) {
934 goto invalid;
935 }
936
937 /* SSID */
938 s = blobmsg_get_string(tb[1]);
939 if (strlen(s) == 0) {
940 /* Copy SSID from hostapd BSS conf */
941 memcpy(&ssid, &hapd->conf->ssid, sizeof(ssid));
942 } else {
943 ssid.ssid_len = strlen(s);
944 if (ssid.ssid_len > sizeof(ssid.ssid))
945 goto invalid;
946
947 memcpy(&ssid, s, ssid.ssid_len);
948 }
949
950 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
951 wpabuf_free(data);
952 continue;
953
954 invalid:
955 return UBUS_STATUS_INVALID_ARGUMENT;
956 }
957
958 return 0;
959 }
960
961 enum {
962 BEACON_REQ_ADDR,
963 BEACON_REQ_MODE,
964 BEACON_REQ_OP_CLASS,
965 BEACON_REQ_CHANNEL,
966 BEACON_REQ_DURATION,
967 BEACON_REQ_BSSID,
968 BEACON_REQ_SSID,
969 __BEACON_REQ_MAX,
970 };
971
972 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
973 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
974 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
975 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
976 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
977 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
978 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
979 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
980 };
981
982 static int
983 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
984 struct ubus_request_data *ureq, const char *method,
985 struct blob_attr *msg)
986 {
987 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
988 struct blob_attr *tb[__BEACON_REQ_MAX];
989 struct blob_attr *cur;
990 struct wpabuf *req;
991 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
992 u8 addr[ETH_ALEN];
993 int mode, rem, ret;
994 int buf_len = 13;
995
996 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
997
998 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
999 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
1000 return UBUS_STATUS_INVALID_ARGUMENT;
1001
1002 if (tb[BEACON_REQ_SSID])
1003 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
1004
1005 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
1006 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
1007 return UBUS_STATUS_INVALID_ARGUMENT;
1008
1009 if (tb[BEACON_REQ_BSSID] &&
1010 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
1011 return UBUS_STATUS_INVALID_ARGUMENT;
1012
1013 req = wpabuf_alloc(buf_len);
1014 if (!req)
1015 return UBUS_STATUS_UNKNOWN_ERROR;
1016
1017 /* 1: regulatory class */
1018 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
1019
1020 /* 2: channel number */
1021 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
1022
1023 /* 3-4: randomization interval */
1024 wpabuf_put_le16(req, 0);
1025
1026 /* 5-6: duration */
1027 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
1028
1029 /* 7: mode */
1030 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
1031
1032 /* 8-13: BSSID */
1033 wpabuf_put_data(req, bssid, ETH_ALEN);
1034
1035 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
1036 wpabuf_put_u8(req, WLAN_EID_SSID);
1037 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
1038 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
1039 }
1040
1041 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
1042 if (ret < 0)
1043 return -ret;
1044
1045 return 0;
1046 }
1047
1048
1049 #ifdef CONFIG_WNM_AP
1050 enum {
1051 WNM_DISASSOC_ADDR,
1052 WNM_DISASSOC_DURATION,
1053 WNM_DISASSOC_NEIGHBORS,
1054 WNM_DISASSOC_ABRIDGED,
1055 __WNM_DISASSOC_MAX,
1056 };
1057
1058 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
1059 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1060 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1061 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
1062 [WNM_DISASSOC_ABRIDGED] { "abridged", BLOBMSG_TYPE_BOOL },
1063 };
1064
1065 static int
1066 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
1067 struct ubus_request_data *ureq, const char *method,
1068 struct blob_attr *msg)
1069 {
1070 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1071 struct blob_attr *tb[__WNM_DISASSOC_MAX];
1072 struct blob_attr *cur;
1073 struct sta_info *sta;
1074 int duration = 10;
1075 int rem;
1076 int nr_len = 0;
1077 u8 *nr = NULL;
1078 u8 req_mode = WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1079 u8 addr[ETH_ALEN];
1080
1081 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1082
1083 if (!tb[WNM_DISASSOC_ADDR])
1084 return UBUS_STATUS_INVALID_ARGUMENT;
1085
1086 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
1087 return UBUS_STATUS_INVALID_ARGUMENT;
1088
1089 if ((cur = tb[WNM_DISASSOC_DURATION]) != NULL)
1090 duration = blobmsg_get_u32(cur);
1091
1092 sta = ap_get_sta(hapd, addr);
1093 if (!sta)
1094 return UBUS_STATUS_NOT_FOUND;
1095
1096 if (tb[WNM_DISASSOC_NEIGHBORS]) {
1097 u8 *nr_cur;
1098
1099 if (blobmsg_check_array(tb[WNM_DISASSOC_NEIGHBORS],
1100 BLOBMSG_TYPE_STRING) < 0)
1101 return UBUS_STATUS_INVALID_ARGUMENT;
1102
1103 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1104 int len = strlen(blobmsg_get_string(cur));
1105
1106 if (len % 2)
1107 return UBUS_STATUS_INVALID_ARGUMENT;
1108
1109 nr_len += (len / 2) + 2;
1110 }
1111
1112 if (nr_len) {
1113 nr = os_zalloc(nr_len);
1114 if (!nr)
1115 return UBUS_STATUS_UNKNOWN_ERROR;
1116 }
1117
1118 nr_cur = nr;
1119 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1120 int len = strlen(blobmsg_get_string(cur)) / 2;
1121
1122 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
1123 *nr_cur++ = (u8) len;
1124 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
1125 free(nr);
1126 return UBUS_STATUS_INVALID_ARGUMENT;
1127 }
1128
1129 nr_cur += len;
1130 }
1131 }
1132
1133 if (nr)
1134 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1135
1136 if (tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]))
1137 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1138
1139 if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, 0, NULL,
1140 NULL, nr, nr_len, NULL, 0))
1141 return UBUS_STATUS_UNKNOWN_ERROR;
1142
1143 return 0;
1144 }
1145 #endif
1146
1147 static const struct ubus_method bss_methods[] = {
1148 UBUS_METHOD_NOARG("reload", hostapd_bss_reload),
1149 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
1150 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
1151 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
1152 #ifdef CONFIG_WPS
1153 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
1154 UBUS_METHOD_NOARG("wps_status", hostapd_bss_wps_status),
1155 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
1156 #endif
1157 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
1158 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features),
1159 #ifdef NEED_AP_MLME
1160 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
1161 #endif
1162 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
1163 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
1164 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
1165 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
1166 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
1167 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
1168 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
1169 #ifdef CONFIG_WNM_AP
1170 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
1171 #endif
1172 };
1173
1174 static struct ubus_object_type bss_object_type =
1175 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
1176
1177 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
1178 {
1179 return memcmp(k1, k2, ETH_ALEN);
1180 }
1181
1182 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
1183 {
1184 struct ubus_object *obj = &hapd->ubus.obj;
1185 char *name;
1186 int ret;
1187
1188 #ifdef CONFIG_MESH
1189 if (hapd->conf->mesh & MESH_ENABLED)
1190 return;
1191 #endif
1192
1193 if (!hostapd_ubus_init())
1194 return;
1195
1196 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
1197 return;
1198
1199 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
1200 obj->name = name;
1201 obj->type = &bss_object_type;
1202 obj->methods = bss_object_type.methods;
1203 obj->n_methods = bss_object_type.n_methods;
1204 ret = ubus_add_object(ctx, obj);
1205 hostapd_ubus_ref_inc();
1206
1207 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "add");
1208 }
1209
1210 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
1211 {
1212 struct ubus_object *obj = &hapd->ubus.obj;
1213 char *name = (char *) obj->name;
1214
1215 if (!ctx)
1216 return;
1217
1218 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "remove");
1219
1220 if (obj->id) {
1221 ubus_remove_object(ctx, obj);
1222 hostapd_ubus_ref_dec();
1223 }
1224
1225 free(name);
1226 }
1227
1228 static const struct ubus_method daemon_methods[] = {
1229 UBUS_METHOD("config_add", hostapd_config_add, config_add_policy),
1230 UBUS_METHOD("config_remove", hostapd_config_remove, config_remove_policy),
1231 };
1232
1233 static struct ubus_object_type daemon_object_type =
1234 UBUS_OBJECT_TYPE("hostapd", daemon_methods);
1235
1236 void hostapd_ubus_add(struct hapd_interfaces *interfaces)
1237 {
1238 struct ubus_object *obj = &interfaces->ubus;
1239 int ret;
1240
1241 if (!hostapd_ubus_init())
1242 return;
1243
1244 obj->name = strdup("hostapd");
1245
1246 obj->type = &daemon_object_type;
1247 obj->methods = daemon_object_type.methods;
1248 obj->n_methods = daemon_object_type.n_methods;
1249 ret = ubus_add_object(ctx, obj);
1250 hostapd_ubus_ref_inc();
1251 }
1252
1253 void hostapd_ubus_free(struct hapd_interfaces *interfaces)
1254 {
1255 struct ubus_object *obj = &interfaces->ubus;
1256 char *name = (char *) obj->name;
1257
1258 if (!ctx)
1259 return;
1260
1261 if (obj->id) {
1262 ubus_remove_object(ctx, obj);
1263 hostapd_ubus_ref_dec();
1264 }
1265
1266 free(name);
1267 }
1268
1269 struct ubus_event_req {
1270 struct ubus_notify_request nreq;
1271 int resp;
1272 };
1273
1274 static void
1275 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1276 {
1277 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1278
1279 ureq->resp = ret;
1280 }
1281
1282 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1283 {
1284 struct ubus_banned_client *ban;
1285 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1286 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1287 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1288 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1289 };
1290 const char *type = "mgmt";
1291 struct ubus_event_req ureq = {};
1292 const u8 *addr;
1293
1294 if (req->mgmt_frame)
1295 addr = req->mgmt_frame->sa;
1296 else
1297 addr = req->addr;
1298
1299 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1300 if (ban)
1301 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1302
1303 if (!hapd->ubus.obj.has_subscribers)
1304 return WLAN_STATUS_SUCCESS;
1305
1306 if (req->type < ARRAY_SIZE(types))
1307 type = types[req->type];
1308
1309 blob_buf_init(&b, 0);
1310 blobmsg_add_macaddr(&b, "address", addr);
1311 if (req->mgmt_frame)
1312 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1313 if (req->ssi_signal)
1314 blobmsg_add_u32(&b, "signal", req->ssi_signal);
1315 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1316
1317 if (req->elems) {
1318 if(req->elems->ht_capabilities)
1319 {
1320 struct ieee80211_ht_capabilities *ht_capabilities;
1321 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1322
1323
1324 ht_capabilities = (struct ieee80211_ht_capabilities*) req->elems->ht_capabilities;
1325 ht_cap = blobmsg_open_table(&b, "ht_capabilities");
1326 blobmsg_add_u16(&b, "ht_capabilities_info", ht_capabilities->ht_capabilities_info);
1327 ht_cap_mcs_set = blobmsg_open_table(&b, "supported_mcs_set");
1328 blobmsg_add_u16(&b, "a_mpdu_params", ht_capabilities->a_mpdu_params);
1329 blobmsg_add_u16(&b, "ht_extended_capabilities", ht_capabilities->ht_extended_capabilities);
1330 blobmsg_add_u32(&b, "tx_bf_capability_info", ht_capabilities->tx_bf_capability_info);
1331 blobmsg_add_u16(&b, "asel_capabilities", ht_capabilities->asel_capabilities);
1332 mcs_set = blobmsg_open_array(&b, "supported_mcs_set");
1333 for (int i = 0; i < 16; i++) {
1334 blobmsg_add_u16(&b, NULL, (u16) ht_capabilities->supported_mcs_set[i]);
1335 }
1336 blobmsg_close_array(&b, mcs_set);
1337 blobmsg_close_table(&b, ht_cap_mcs_set);
1338 blobmsg_close_table(&b, ht_cap);
1339 }
1340 if(req->elems->vht_capabilities)
1341 {
1342 struct ieee80211_vht_capabilities *vht_capabilities;
1343 void *vht_cap, *vht_cap_mcs_set;
1344
1345 vht_capabilities = (struct ieee80211_vht_capabilities*) req->elems->vht_capabilities;
1346 vht_cap = blobmsg_open_table(&b, "vht_capabilities");
1347 blobmsg_add_u32(&b, "vht_capabilities_info", vht_capabilities->vht_capabilities_info);
1348 vht_cap_mcs_set = blobmsg_open_table(&b, "vht_supported_mcs_set");
1349 blobmsg_add_u16(&b, "rx_map", vht_capabilities->vht_supported_mcs_set.rx_map);
1350 blobmsg_add_u16(&b, "rx_highest", vht_capabilities->vht_supported_mcs_set.rx_highest);
1351 blobmsg_add_u16(&b, "tx_map", vht_capabilities->vht_supported_mcs_set.tx_map);
1352 blobmsg_add_u16(&b, "tx_highest", vht_capabilities->vht_supported_mcs_set.tx_highest);
1353 blobmsg_close_table(&b, vht_cap_mcs_set);
1354 blobmsg_close_table(&b, vht_cap);
1355 }
1356 }
1357
1358 if (!hapd->ubus.notify_response) {
1359 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1360 return WLAN_STATUS_SUCCESS;
1361 }
1362
1363 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1364 return WLAN_STATUS_SUCCESS;
1365
1366 ureq.nreq.status_cb = ubus_event_cb;
1367 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1368
1369 if (ureq.resp)
1370 return ureq.resp;
1371
1372 return WLAN_STATUS_SUCCESS;
1373 }
1374
1375 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1376 {
1377 if (!hapd->ubus.obj.has_subscribers)
1378 return;
1379
1380 if (!addr)
1381 return;
1382
1383 blob_buf_init(&b, 0);
1384 blobmsg_add_macaddr(&b, "address", addr);
1385
1386 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1387 }
1388
1389 void hostapd_ubus_notify_beacon_report(
1390 struct hostapd_data *hapd, const u8 *addr, u8 token, u8 rep_mode,
1391 struct rrm_measurement_beacon_report *rep, size_t len)
1392 {
1393 if (!hapd->ubus.obj.has_subscribers)
1394 return;
1395
1396 if (!addr || !rep)
1397 return;
1398
1399 blob_buf_init(&b, 0);
1400 blobmsg_add_macaddr(&b, "address", addr);
1401 blobmsg_add_u16(&b, "op-class", rep->op_class);
1402 blobmsg_add_u16(&b, "channel", rep->channel);
1403 blobmsg_add_u64(&b, "start-time", rep->start_time);
1404 blobmsg_add_u16(&b, "duration", rep->duration);
1405 blobmsg_add_u16(&b, "report-info", rep->report_info);
1406 blobmsg_add_u16(&b, "rcpi", rep->rcpi);
1407 blobmsg_add_u16(&b, "rsni", rep->rsni);
1408 blobmsg_add_macaddr(&b, "bssid", rep->bssid);
1409 blobmsg_add_u16(&b, "antenna-id", rep->antenna_id);
1410 blobmsg_add_u16(&b, "parent-tsf", rep->parent_tsf);
1411
1412 ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
1413 }