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