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