e0d3d2ae909ab251b6dc99e317384ea265d87201
[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 #include "airtime_policy.h"
25
26 static struct ubus_context *ctx;
27 static struct blob_buf b;
28 static int ctx_ref;
29
30 static inline struct hapd_interfaces *get_hapd_interfaces_from_object(struct ubus_object *obj)
31 {
32 return container_of(obj, struct hapd_interfaces, ubus);
33 }
34
35 static inline struct hostapd_data *get_hapd_from_object(struct ubus_object *obj)
36 {
37 return container_of(obj, struct hostapd_data, ubus.obj);
38 }
39
40 struct ubus_banned_client {
41 struct avl_node avl;
42 u8 addr[ETH_ALEN];
43 };
44
45 static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
46 {
47 struct ubus_context *ctx = eloop_ctx;
48 ubus_handle_event(ctx);
49 }
50
51 static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
52 {
53 if (ubus_reconnect(ctx, NULL)) {
54 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
55 return;
56 }
57
58 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
59 }
60
61 static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
62 {
63 eloop_unregister_read_sock(ctx->sock.fd);
64 eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
65 }
66
67 static bool hostapd_ubus_init(void)
68 {
69 if (ctx)
70 return true;
71
72 ctx = ubus_connect(NULL);
73 if (!ctx)
74 return false;
75
76 ctx->connection_lost = hostapd_ubus_connection_lost;
77 eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
78 return true;
79 }
80
81 static void hostapd_ubus_ref_inc(void)
82 {
83 ctx_ref++;
84 }
85
86 static void hostapd_ubus_ref_dec(void)
87 {
88 ctx_ref--;
89 if (!ctx)
90 return;
91
92 if (ctx_ref)
93 return;
94
95 eloop_unregister_read_sock(ctx->sock.fd);
96 ubus_free(ctx);
97 ctx = NULL;
98 }
99
100 void hostapd_ubus_add_iface(struct hostapd_iface *iface)
101 {
102 if (!hostapd_ubus_init())
103 return;
104 }
105
106 void hostapd_ubus_free_iface(struct hostapd_iface *iface)
107 {
108 if (!ctx)
109 return;
110 }
111
112 static void hostapd_notify_ubus(struct ubus_object *obj, char *bssname, char *event)
113 {
114 char *event_type;
115
116 if (!ctx || !obj)
117 return;
118
119 if (asprintf(&event_type, "bss.%s", event) < 0)
120 return;
121
122 blob_buf_init(&b, 0);
123 blobmsg_add_string(&b, "name", bssname);
124 ubus_notify(ctx, obj, event_type, b.head, -1);
125 free(event_type);
126 }
127
128 static void hostapd_send_procd_event(char *bssname, char *event)
129 {
130 char *name, *s;
131 uint32_t id;
132 void *v;
133
134 if (!ctx || ubus_lookup_id(ctx, "service", &id))
135 return;
136
137 if (asprintf(&name, "hostapd.%s.%s", bssname, event) < 0)
138 return;
139
140 blob_buf_init(&b, 0);
141
142 s = blobmsg_alloc_string_buffer(&b, "type", strlen(name) + 1);
143 sprintf(s, "%s", name);
144 blobmsg_add_string_buffer(&b);
145
146 v = blobmsg_open_table(&b, "data");
147 blobmsg_close_table(&b, v);
148
149 ubus_invoke(ctx, id, "event", b.head, NULL, NULL, 1000);
150
151 free(name);
152 }
153
154 static void hostapd_send_shared_event(struct ubus_object *obj, char *bssname, char *event)
155 {
156 hostapd_send_procd_event(bssname, event);
157 hostapd_notify_ubus(obj, bssname, event);
158 }
159
160 static void
161 hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
162 {
163 struct ubus_banned_client *ban = eloop_data;
164 struct hostapd_data *hapd = user_ctx;
165
166 avl_delete(&hapd->ubus.banned, &ban->avl);
167 free(ban);
168 }
169
170 static void
171 hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
172 {
173 struct ubus_banned_client *ban;
174
175 if (time < 0)
176 time = 0;
177
178 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
179 if (!ban) {
180 if (!time)
181 return;
182
183 ban = os_zalloc(sizeof(*ban));
184 memcpy(ban->addr, addr, sizeof(ban->addr));
185 ban->avl.key = ban->addr;
186 avl_insert(&hapd->ubus.banned, &ban->avl);
187 } else {
188 eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
189 if (!time) {
190 hostapd_bss_del_ban(ban, hapd);
191 return;
192 }
193 }
194
195 eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
196 }
197
198 static int
199 hostapd_bss_reload(struct ubus_context *ctx, struct ubus_object *obj,
200 struct ubus_request_data *req, const char *method,
201 struct blob_attr *msg)
202 {
203 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
204 int ret = hostapd_reload_config(hapd->iface, 1);
205
206 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "reload");
207 return ret;
208 }
209
210
211 static void
212 hostapd_parse_vht_map_blobmsg(uint16_t map)
213 {
214 char label[4];
215 int16_t val;
216 int i;
217
218 for (i = 0; i < 8; i++) {
219 snprintf(label, 4, "%dss", i + 1);
220
221 val = (map & (BIT(1) | BIT(0))) + 7;
222 blobmsg_add_u16(&b, label, val == 10 ? -1 : val);
223 map = map >> 2;
224 }
225 }
226
227 static void
228 hostapd_parse_vht_capab_blobmsg(struct ieee80211_vht_capabilities *vhtc)
229 {
230 void *supported_mcs;
231 void *map;
232 int i;
233
234 static const struct {
235 const char *name;
236 uint32_t flag;
237 } vht_capas[] = {
238 { "su_beamformee", VHT_CAP_SU_BEAMFORMEE_CAPABLE },
239 { "mu_beamformee", VHT_CAP_MU_BEAMFORMEE_CAPABLE },
240 };
241
242 for (i = 0; i < ARRAY_SIZE(vht_capas); i++)
243 blobmsg_add_u8(&b, vht_capas[i].name,
244 !!(vhtc->vht_capabilities_info & vht_capas[i].flag));
245
246 supported_mcs = blobmsg_open_table(&b, "mcs_map");
247
248 /* RX map */
249 map = blobmsg_open_table(&b, "rx");
250 hostapd_parse_vht_map_blobmsg(le_to_host16(vhtc->vht_supported_mcs_set.rx_map));
251 blobmsg_close_table(&b, map);
252
253 /* TX map */
254 map = blobmsg_open_table(&b, "tx");
255 hostapd_parse_vht_map_blobmsg(le_to_host16(vhtc->vht_supported_mcs_set.tx_map));
256 blobmsg_close_table(&b, map);
257
258 blobmsg_close_table(&b, supported_mcs);
259 }
260
261 static void
262 hostapd_parse_capab_blobmsg(struct sta_info *sta)
263 {
264 void *r, *v;
265
266 v = blobmsg_open_table(&b, "capabilities");
267
268 if (sta->vht_capabilities) {
269 r = blobmsg_open_table(&b, "vht");
270 hostapd_parse_vht_capab_blobmsg(sta->vht_capabilities);
271 blobmsg_close_table(&b, r);
272 }
273
274 /* ToDo: Add HT / HE capability parsing */
275
276 blobmsg_close_table(&b, v);
277 }
278
279 static int
280 hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
281 struct ubus_request_data *req, const char *method,
282 struct blob_attr *msg)
283 {
284 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
285 struct hostap_sta_driver_data sta_driver_data;
286 struct sta_info *sta;
287 void *list, *c;
288 char mac_buf[20];
289 static const struct {
290 const char *name;
291 uint32_t flag;
292 } sta_flags[] = {
293 { "auth", WLAN_STA_AUTH },
294 { "assoc", WLAN_STA_ASSOC },
295 { "authorized", WLAN_STA_AUTHORIZED },
296 { "preauth", WLAN_STA_PREAUTH },
297 { "wds", WLAN_STA_WDS },
298 { "wmm", WLAN_STA_WMM },
299 { "ht", WLAN_STA_HT },
300 { "vht", WLAN_STA_VHT },
301 { "he", WLAN_STA_HE },
302 { "wps", WLAN_STA_WPS },
303 { "mfp", WLAN_STA_MFP },
304 };
305
306 blob_buf_init(&b, 0);
307 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
308 list = blobmsg_open_table(&b, "clients");
309 for (sta = hapd->sta_list; sta; sta = sta->next) {
310 void *r;
311 int i;
312
313 sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
314 c = blobmsg_open_table(&b, mac_buf);
315 for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
316 blobmsg_add_u8(&b, sta_flags[i].name,
317 !!(sta->flags & sta_flags[i].flag));
318
319 r = blobmsg_open_array(&b, "rrm");
320 for (i = 0; i < ARRAY_SIZE(sta->rrm_enabled_capa); i++)
321 blobmsg_add_u32(&b, "", sta->rrm_enabled_capa[i]);
322 blobmsg_close_array(&b, r);
323 blobmsg_add_u32(&b, "aid", sta->aid);
324 #ifdef CONFIG_TAXONOMY
325 r = blobmsg_alloc_string_buffer(&b, "signature", 1024);
326 if (retrieve_sta_taxonomy(hapd, sta, r, 1024) > 0)
327 blobmsg_add_string_buffer(&b);
328 #endif
329
330 /* Driver information */
331 if (hostapd_drv_read_sta_data(hapd, &sta_driver_data, sta->addr) >= 0) {
332 r = blobmsg_open_table(&b, "bytes");
333 blobmsg_add_u64(&b, "rx", sta_driver_data.rx_bytes);
334 blobmsg_add_u64(&b, "tx", sta_driver_data.tx_bytes);
335 blobmsg_close_table(&b, r);
336 r = blobmsg_open_table(&b, "airtime");
337 blobmsg_add_u64(&b, "rx", sta_driver_data.rx_airtime);
338 blobmsg_add_u64(&b, "tx", sta_driver_data.tx_airtime);
339 blobmsg_close_table(&b, r);
340 r = blobmsg_open_table(&b, "packets");
341 blobmsg_add_u32(&b, "rx", sta_driver_data.rx_packets);
342 blobmsg_add_u32(&b, "tx", sta_driver_data.tx_packets);
343 blobmsg_close_table(&b, r);
344 r = blobmsg_open_table(&b, "rate");
345 /* Rate in kbits */
346 blobmsg_add_u32(&b, "rx", sta_driver_data.current_rx_rate * 100);
347 blobmsg_add_u32(&b, "tx", sta_driver_data.current_tx_rate * 100);
348 blobmsg_close_table(&b, r);
349 blobmsg_add_u32(&b, "signal", sta_driver_data.signal);
350 }
351
352 hostapd_parse_capab_blobmsg(sta);
353
354 blobmsg_close_table(&b, c);
355 }
356 blobmsg_close_array(&b, list);
357 ubus_send_reply(ctx, req, b.head);
358
359 return 0;
360 }
361
362 static int
363 hostapd_bss_get_features(struct ubus_context *ctx, struct ubus_object *obj,
364 struct ubus_request_data *req, const char *method,
365 struct blob_attr *msg)
366 {
367 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
368
369 blob_buf_init(&b, 0);
370 blobmsg_add_u8(&b, "ht_supported", ht_supported(hapd->iface->hw_features));
371 blobmsg_add_u8(&b, "vht_supported", vht_supported(hapd->iface->hw_features));
372 ubus_send_reply(ctx, req, b.head);
373
374 return 0;
375 }
376
377 /* Imported from iw/util.c
378 * https://git.kernel.org/pub/scm/linux/kernel/git/jberg/iw.git/tree/util.c?id=4b25ae3537af48dbf9d0abf94132e5ba01b32c18#n200
379 */
380 int ieee80211_frequency_to_channel(int freq)
381 {
382 /* see 802.11-2007 17.3.8.3.2 and Annex J */
383 if (freq == 2484)
384 return 14;
385 /* see 802.11ax D6.1 27.3.23.2 and Annex E */
386 else if (freq == 5935)
387 return 2;
388 else if (freq < 2484)
389 return (freq - 2407) / 5;
390 else if (freq >= 4910 && freq <= 4980)
391 return (freq - 4000) / 5;
392 else if (freq < 5950)
393 return (freq - 5000) / 5;
394 else if (freq <= 45000) /* DMG band lower limit */
395 /* see 802.11ax D6.1 27.3.23.2 */
396 return (freq - 5950) / 5;
397 else if (freq >= 58320 && freq <= 70200)
398 return (freq - 56160) / 2160;
399 else
400 return 0;
401 }
402
403 static int
404 hostapd_bss_get_status(struct ubus_context *ctx, struct ubus_object *obj,
405 struct ubus_request_data *req, const char *method,
406 struct blob_attr *msg)
407 {
408 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
409 void *airtime_table, *dfs_table;
410 struct os_reltime now;
411 char ssid[SSID_MAX_LEN + 1];
412 char phy_name[17];
413 size_t ssid_len = SSID_MAX_LEN;
414
415 if (hapd->conf->ssid.ssid_len < SSID_MAX_LEN)
416 ssid_len = hapd->conf->ssid.ssid_len;
417
418 blob_buf_init(&b, 0);
419 blobmsg_add_string(&b, "status", hostapd_state_text(hapd->iface->state));
420 blobmsg_printf(&b, "bssid", MACSTR, MAC2STR(hapd->conf->bssid));
421
422 memset(ssid, 0, SSID_MAX_LEN + 1);
423 memcpy(ssid, hapd->conf->ssid.ssid, ssid_len);
424 blobmsg_add_string(&b, "ssid", ssid);
425
426 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
427 blobmsg_add_u32(&b, "channel", ieee80211_frequency_to_channel(hapd->iface->freq));
428 blobmsg_add_u32(&b, "beacon_interval", hapd->iconf->beacon_int);
429
430 snprintf(phy_name, 17, "%s", hapd->iface->phy);
431 blobmsg_add_string(&b, "phy", phy_name);
432
433 /* Airtime */
434 airtime_table = blobmsg_open_table(&b, "airtime");
435 blobmsg_add_u64(&b, "time", hapd->iface->last_channel_time);
436 blobmsg_add_u64(&b, "time_busy", hapd->iface->last_channel_time_busy);
437 blobmsg_add_u16(&b, "utilization", hapd->iface->channel_utilization);
438 blobmsg_close_table(&b, airtime_table);
439
440 /* DFS */
441 dfs_table = blobmsg_open_table(&b, "dfs");
442 blobmsg_add_u32(&b, "cac_seconds", hapd->iface->dfs_cac_ms / 1000);
443 blobmsg_add_u8(&b, "cac_active", !!(hapd->iface->cac_started));
444 os_reltime_age(&hapd->iface->dfs_cac_start, &now);
445 blobmsg_add_u32(&b, "cac_seconds_left",
446 hapd->iface->cac_started ? hapd->iface->dfs_cac_ms / 1000 - now.sec : 0);
447 blobmsg_close_table(&b, dfs_table);
448
449 ubus_send_reply(ctx, req, b.head);
450
451 return 0;
452 }
453
454 enum {
455 NOTIFY_RESPONSE,
456 __NOTIFY_MAX
457 };
458
459 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
460 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
461 };
462
463 static int
464 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
465 struct ubus_request_data *req, const char *method,
466 struct blob_attr *msg)
467 {
468 struct blob_attr *tb[__NOTIFY_MAX];
469 struct hostapd_data *hapd = get_hapd_from_object(obj);
470 struct wpabuf *elems;
471 const char *pos;
472 size_t len;
473
474 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
475 blob_data(msg), blob_len(msg));
476
477 if (!tb[NOTIFY_RESPONSE])
478 return UBUS_STATUS_INVALID_ARGUMENT;
479
480 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
481
482 return UBUS_STATUS_OK;
483 }
484
485 enum {
486 DEL_CLIENT_ADDR,
487 DEL_CLIENT_REASON,
488 DEL_CLIENT_DEAUTH,
489 DEL_CLIENT_BAN_TIME,
490 __DEL_CLIENT_MAX
491 };
492
493 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
494 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
495 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
496 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
497 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
498 };
499
500 static int
501 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
502 struct ubus_request_data *req, const char *method,
503 struct blob_attr *msg)
504 {
505 struct blob_attr *tb[__DEL_CLIENT_MAX];
506 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
507 struct sta_info *sta;
508 bool deauth = false;
509 int reason;
510 u8 addr[ETH_ALEN];
511
512 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
513
514 if (!tb[DEL_CLIENT_ADDR])
515 return UBUS_STATUS_INVALID_ARGUMENT;
516
517 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
518 return UBUS_STATUS_INVALID_ARGUMENT;
519
520 if (tb[DEL_CLIENT_REASON])
521 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
522
523 if (tb[DEL_CLIENT_DEAUTH])
524 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
525
526 sta = ap_get_sta(hapd, addr);
527 if (sta) {
528 if (deauth) {
529 hostapd_drv_sta_deauth(hapd, addr, reason);
530 ap_sta_deauthenticate(hapd, sta, reason);
531 } else {
532 hostapd_drv_sta_disassoc(hapd, addr, reason);
533 ap_sta_disassociate(hapd, sta, reason);
534 }
535 }
536
537 if (tb[DEL_CLIENT_BAN_TIME])
538 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
539
540 return 0;
541 }
542
543 static void
544 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
545 {
546 char *s;
547
548 s = blobmsg_alloc_string_buffer(buf, name, 20);
549 sprintf(s, MACSTR, MAC2STR(addr));
550 blobmsg_add_string_buffer(buf);
551 }
552
553 static int
554 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
555 struct ubus_request_data *req, const char *method,
556 struct blob_attr *msg)
557 {
558 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
559 struct ubus_banned_client *ban;
560 void *c;
561
562 blob_buf_init(&b, 0);
563 c = blobmsg_open_array(&b, "clients");
564 avl_for_each_element(&hapd->ubus.banned, ban, avl)
565 blobmsg_add_macaddr(&b, NULL, ban->addr);
566 blobmsg_close_array(&b, c);
567 ubus_send_reply(ctx, req, b.head);
568
569 return 0;
570 }
571
572 #ifdef CONFIG_WPS
573 static int
574 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
575 struct ubus_request_data *req, const char *method,
576 struct blob_attr *msg)
577 {
578 int rc;
579 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
580
581 rc = hostapd_wps_button_pushed(hapd, NULL);
582
583 if (rc != 0)
584 return UBUS_STATUS_NOT_SUPPORTED;
585
586 return 0;
587 }
588
589
590 static const char * pbc_status_enum_str(enum pbc_status status)
591 {
592 switch (status) {
593 case WPS_PBC_STATUS_DISABLE:
594 return "Disabled";
595 case WPS_PBC_STATUS_ACTIVE:
596 return "Active";
597 case WPS_PBC_STATUS_TIMEOUT:
598 return "Timed-out";
599 case WPS_PBC_STATUS_OVERLAP:
600 return "Overlap";
601 default:
602 return "Unknown";
603 }
604 }
605
606 static int
607 hostapd_bss_wps_status(struct ubus_context *ctx, struct ubus_object *obj,
608 struct ubus_request_data *req, const char *method,
609 struct blob_attr *msg)
610 {
611 int rc;
612 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
613
614 blob_buf_init(&b, 0);
615
616 blobmsg_add_string(&b, "pbc_status", pbc_status_enum_str(hapd->wps_stats.pbc_status));
617 blobmsg_add_string(&b, "last_wps_result",
618 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
619 "Success":
620 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
621 "Failed" : "None")));
622
623 /* If status == Failure - Add possible Reasons */
624 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
625 hapd->wps_stats.failure_reason > 0)
626 blobmsg_add_string(&b, "reason", wps_ei_str(hapd->wps_stats.failure_reason));
627
628 if (hapd->wps_stats.status)
629 blobmsg_printf(&b, "peer_address", MACSTR, MAC2STR(hapd->wps_stats.peer_addr));
630
631 ubus_send_reply(ctx, req, b.head);
632
633 return 0;
634 }
635
636 static int
637 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
638 struct ubus_request_data *req, const char *method,
639 struct blob_attr *msg)
640 {
641 int rc;
642 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
643
644 rc = hostapd_wps_cancel(hapd);
645
646 if (rc != 0)
647 return UBUS_STATUS_NOT_SUPPORTED;
648
649 return 0;
650 }
651 #endif /* CONFIG_WPS */
652
653 static int
654 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
655 struct ubus_request_data *req, const char *method,
656 struct blob_attr *msg)
657 {
658 int rc;
659 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
660
661 rc = ieee802_11_set_beacon(hapd);
662
663 if (rc != 0)
664 return UBUS_STATUS_NOT_SUPPORTED;
665
666 return 0;
667 }
668
669 enum {
670 CONFIG_IFACE,
671 CONFIG_FILE,
672 __CONFIG_MAX
673 };
674
675 static const struct blobmsg_policy config_add_policy[__CONFIG_MAX] = {
676 [CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
677 [CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
678 };
679
680 static int
681 hostapd_config_add(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[__CONFIG_MAX];
686 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
687 char buf[128];
688
689 blobmsg_parse(config_add_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
690
691 if (!tb[CONFIG_FILE] || !tb[CONFIG_IFACE])
692 return UBUS_STATUS_INVALID_ARGUMENT;
693
694 snprintf(buf, sizeof(buf), "bss_config=%s:%s",
695 blobmsg_get_string(tb[CONFIG_IFACE]),
696 blobmsg_get_string(tb[CONFIG_FILE]));
697
698 if (hostapd_add_iface(interfaces, buf))
699 return UBUS_STATUS_INVALID_ARGUMENT;
700
701 blob_buf_init(&b, 0);
702 blobmsg_add_u32(&b, "pid", getpid());
703 ubus_send_reply(ctx, req, b.head);
704
705 return UBUS_STATUS_OK;
706 }
707
708 enum {
709 CONFIG_REM_IFACE,
710 __CONFIG_REM_MAX
711 };
712
713 static const struct blobmsg_policy config_remove_policy[__CONFIG_REM_MAX] = {
714 [CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
715 };
716
717 static int
718 hostapd_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
719 struct ubus_request_data *req, const char *method,
720 struct blob_attr *msg)
721 {
722 struct blob_attr *tb[__CONFIG_REM_MAX];
723 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
724 char buf[128];
725
726 blobmsg_parse(config_remove_policy, __CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
727
728 if (!tb[CONFIG_REM_IFACE])
729 return UBUS_STATUS_INVALID_ARGUMENT;
730
731 if (hostapd_remove_iface(interfaces, blobmsg_get_string(tb[CONFIG_REM_IFACE])))
732 return UBUS_STATUS_INVALID_ARGUMENT;
733
734 return UBUS_STATUS_OK;
735 }
736
737 enum {
738 CSA_FREQ,
739 CSA_BCN_COUNT,
740 CSA_CENTER_FREQ1,
741 CSA_CENTER_FREQ2,
742 CSA_BANDWIDTH,
743 CSA_SEC_CHANNEL_OFFSET,
744 CSA_HT,
745 CSA_VHT,
746 CSA_HE,
747 CSA_BLOCK_TX,
748 CSA_FORCE,
749 __CSA_MAX
750 };
751
752 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
753 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
754 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
755 [CSA_CENTER_FREQ1] = { "center_freq1", BLOBMSG_TYPE_INT32 },
756 [CSA_CENTER_FREQ2] = { "center_freq2", BLOBMSG_TYPE_INT32 },
757 [CSA_BANDWIDTH] = { "bandwidth", BLOBMSG_TYPE_INT32 },
758 [CSA_SEC_CHANNEL_OFFSET] = { "sec_channel_offset", BLOBMSG_TYPE_INT32 },
759 [CSA_HT] = { "ht", BLOBMSG_TYPE_BOOL },
760 [CSA_VHT] = { "vht", BLOBMSG_TYPE_BOOL },
761 [CSA_HE] = { "he", BLOBMSG_TYPE_BOOL },
762 [CSA_BLOCK_TX] = { "block_tx", BLOBMSG_TYPE_BOOL },
763 [CSA_FORCE] = { "force", BLOBMSG_TYPE_BOOL },
764 };
765
766
767 static void switch_chan_fallback_cb(void *eloop_data, void *user_ctx)
768 {
769 struct hostapd_iface *iface = eloop_data;
770 struct hostapd_freq_params *freq_params = user_ctx;
771
772 hostapd_switch_channel_fallback(iface, freq_params);
773 }
774
775 #ifdef NEED_AP_MLME
776 static int
777 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
778 struct ubus_request_data *req, const char *method,
779 struct blob_attr *msg)
780 {
781 struct blob_attr *tb[__CSA_MAX];
782 struct hostapd_data *hapd = get_hapd_from_object(obj);
783 struct hostapd_config *iconf = hapd->iface->conf;
784 struct hostapd_freq_params *freq_params;
785 struct csa_settings css = {
786 .freq_params = {
787 .ht_enabled = iconf->ieee80211n,
788 .vht_enabled = iconf->ieee80211ac,
789 .he_enabled = iconf->ieee80211ax,
790 .sec_channel_offset = iconf->secondary_channel,
791 }
792 };
793 int ret = UBUS_STATUS_OK;
794 int i;
795
796 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
797
798 if (!tb[CSA_FREQ])
799 return UBUS_STATUS_INVALID_ARGUMENT;
800
801 switch (iconf->vht_oper_chwidth) {
802 case CHANWIDTH_USE_HT:
803 if (iconf->secondary_channel)
804 css.freq_params.bandwidth = 40;
805 else
806 css.freq_params.bandwidth = 20;
807 break;
808 case CHANWIDTH_160MHZ:
809 css.freq_params.bandwidth = 160;
810 break;
811 default:
812 css.freq_params.bandwidth = 80;
813 break;
814 }
815
816 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
817
818 #define SET_CSA_SETTING(name, field, type) \
819 do { \
820 if (tb[name]) \
821 css.field = blobmsg_get_ ## type(tb[name]); \
822 } while(0)
823
824 SET_CSA_SETTING(CSA_BCN_COUNT, cs_count, u32);
825 SET_CSA_SETTING(CSA_CENTER_FREQ1, freq_params.center_freq1, u32);
826 SET_CSA_SETTING(CSA_CENTER_FREQ2, freq_params.center_freq2, u32);
827 SET_CSA_SETTING(CSA_BANDWIDTH, freq_params.bandwidth, u32);
828 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET, freq_params.sec_channel_offset, u32);
829 SET_CSA_SETTING(CSA_HT, freq_params.ht_enabled, bool);
830 SET_CSA_SETTING(CSA_VHT, freq_params.vht_enabled, bool);
831 SET_CSA_SETTING(CSA_HE, freq_params.he_enabled, bool);
832 SET_CSA_SETTING(CSA_BLOCK_TX, block_tx, bool);
833
834 for (i = 0; i < hapd->iface->num_bss; i++) {
835 struct hostapd_data *bss = hapd->iface->bss[i];
836
837 if (hostapd_switch_channel(bss, &css) != 0)
838 ret = UBUS_STATUS_NOT_SUPPORTED;
839 }
840
841 if (!ret || !tb[CSA_FORCE] || !blobmsg_get_bool(tb[CSA_FORCE]))
842 return ret;
843
844 freq_params = malloc(sizeof(*freq_params));
845 memcpy(freq_params, &css.freq_params, sizeof(*freq_params));
846 eloop_register_timeout(0, 1, switch_chan_fallback_cb,
847 hapd->iface, freq_params);
848
849 return 0;
850 #undef SET_CSA_SETTING
851 }
852 #endif
853
854 enum {
855 VENDOR_ELEMENTS,
856 __VENDOR_ELEMENTS_MAX
857 };
858
859 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
860 /* vendor elements are provided as hex-string */
861 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
862 };
863
864 static int
865 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
866 struct ubus_request_data *req, const char *method,
867 struct blob_attr *msg)
868 {
869 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
870 struct hostapd_data *hapd = get_hapd_from_object(obj);
871 struct hostapd_bss_config *bss = hapd->conf;
872 struct wpabuf *elems;
873 const char *pos;
874 size_t len;
875
876 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
877 blob_data(msg), blob_len(msg));
878
879 if (!tb[VENDOR_ELEMENTS])
880 return UBUS_STATUS_INVALID_ARGUMENT;
881
882 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
883 len = os_strlen(pos);
884 if (len & 0x01)
885 return UBUS_STATUS_INVALID_ARGUMENT;
886
887 len /= 2;
888 if (len == 0) {
889 wpabuf_free(bss->vendor_elements);
890 bss->vendor_elements = NULL;
891 return 0;
892 }
893
894 elems = wpabuf_alloc(len);
895 if (elems == NULL)
896 return 1;
897
898 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
899 wpabuf_free(elems);
900 return UBUS_STATUS_INVALID_ARGUMENT;
901 }
902
903 wpabuf_free(bss->vendor_elements);
904 bss->vendor_elements = elems;
905
906 /* update beacons if vendor elements were set successfully */
907 if (ieee802_11_update_beacons(hapd->iface) != 0)
908 return UBUS_STATUS_NOT_SUPPORTED;
909 return UBUS_STATUS_OK;
910 }
911
912 static void
913 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
914 {
915 const u8 *data;
916 char *str;
917 int len;
918
919 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
920
921 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
922 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
923 str[nr->ssid.ssid_len] = 0;
924 blobmsg_add_string_buffer(&b);
925
926 len = wpabuf_len(nr->nr);
927 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
928 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
929 blobmsg_add_string_buffer(&b);
930 }
931
932 enum {
933 BSS_MGMT_EN_NEIGHBOR,
934 BSS_MGMT_EN_BEACON,
935 #ifdef CONFIG_WNM_AP
936 BSS_MGMT_EN_BSS_TRANSITION,
937 #endif
938 __BSS_MGMT_EN_MAX
939 };
940
941 static bool
942 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
943 {
944 struct hostapd_bss_config *bss = hapd->conf;
945 uint32_t flags;
946
947 switch (flag) {
948 case BSS_MGMT_EN_NEIGHBOR:
949 if (bss->radio_measurements[0] &
950 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
951 return false;
952
953 bss->radio_measurements[0] |=
954 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
955 hostapd_neighbor_set_own_report(hapd);
956 return true;
957 case BSS_MGMT_EN_BEACON:
958 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
959 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
960 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
961
962 if (bss->radio_measurements[0] & flags == flags)
963 return false;
964
965 bss->radio_measurements[0] |= (u8) flags;
966 return true;
967 #ifdef CONFIG_WNM_AP
968 case BSS_MGMT_EN_BSS_TRANSITION:
969 if (bss->bss_transition)
970 return false;
971
972 bss->bss_transition = 1;
973 return true;
974 #endif
975 }
976 }
977
978 static void
979 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
980 {
981 bool update = false;
982 int i;
983
984 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
985 if (!(flags & (1 << i)))
986 continue;
987
988 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
989 }
990
991 if (update)
992 ieee802_11_update_beacons(hapd->iface);
993 }
994
995
996 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
997 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
998 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
999 #ifdef CONFIG_WNM_AP
1000 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
1001 #endif
1002 };
1003
1004 static int
1005 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
1006 struct ubus_request_data *req, const char *method,
1007 struct blob_attr *msg)
1008
1009 {
1010 struct hostapd_data *hapd = get_hapd_from_object(obj);
1011 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
1012 struct blob_attr *cur;
1013 uint32_t flags = 0;
1014 int i;
1015 bool neigh = false, beacon = false;
1016
1017 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
1018
1019 for (i = 0; i < ARRAY_SIZE(tb); i++) {
1020 if (!tb[i] || !blobmsg_get_bool(tb[i]))
1021 continue;
1022
1023 flags |= (1 << i);
1024 }
1025
1026 __hostapd_bss_mgmt_enable(hapd, flags);
1027 }
1028
1029
1030 static void
1031 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
1032 {
1033 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
1034 }
1035
1036 static int
1037 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
1038 struct ubus_request_data *req, const char *method,
1039 struct blob_attr *msg)
1040 {
1041 struct hostapd_data *hapd = get_hapd_from_object(obj);
1042 struct hostapd_neighbor_entry *nr;
1043 void *c;
1044
1045 hostapd_rrm_nr_enable(hapd);
1046
1047 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
1048 if (!nr)
1049 return UBUS_STATUS_NOT_FOUND;
1050
1051 blob_buf_init(&b, 0);
1052
1053 c = blobmsg_open_array(&b, "value");
1054 hostapd_rrm_print_nr(nr);
1055 blobmsg_close_array(&b, c);
1056
1057 ubus_send_reply(ctx, req, b.head);
1058
1059 return 0;
1060 }
1061
1062 static int
1063 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
1064 struct ubus_request_data *req, const char *method,
1065 struct blob_attr *msg)
1066 {
1067 struct hostapd_data *hapd = get_hapd_from_object(obj);
1068 struct hostapd_neighbor_entry *nr;
1069 void *c;
1070
1071 hostapd_rrm_nr_enable(hapd);
1072 blob_buf_init(&b, 0);
1073
1074 c = blobmsg_open_array(&b, "list");
1075 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
1076 void *cur;
1077
1078 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
1079 continue;
1080
1081 cur = blobmsg_open_array(&b, NULL);
1082 hostapd_rrm_print_nr(nr);
1083 blobmsg_close_array(&b, cur);
1084 }
1085 blobmsg_close_array(&b, c);
1086
1087 ubus_send_reply(ctx, req, b.head);
1088
1089 return 0;
1090 }
1091
1092 enum {
1093 NR_SET_LIST,
1094 __NR_SET_LIST_MAX
1095 };
1096
1097 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
1098 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
1099 };
1100
1101
1102 static void
1103 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
1104 {
1105 struct hostapd_neighbor_entry *nr;
1106
1107 restart:
1108 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
1109 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
1110 continue;
1111
1112 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
1113 goto restart;
1114 }
1115 }
1116
1117 static int
1118 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
1119 struct ubus_request_data *req, const char *method,
1120 struct blob_attr *msg)
1121 {
1122 static const struct blobmsg_policy nr_e_policy[] = {
1123 { .type = BLOBMSG_TYPE_STRING },
1124 { .type = BLOBMSG_TYPE_STRING },
1125 { .type = BLOBMSG_TYPE_STRING },
1126 };
1127 struct hostapd_data *hapd = get_hapd_from_object(obj);
1128 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
1129 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
1130 struct blob_attr *cur;
1131 int rem;
1132
1133 hostapd_rrm_nr_enable(hapd);
1134
1135 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
1136 if (!tb_l[NR_SET_LIST])
1137 return UBUS_STATUS_INVALID_ARGUMENT;
1138
1139 hostapd_rrm_nr_clear(hapd);
1140 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
1141 struct wpa_ssid_value ssid;
1142 struct wpabuf *data;
1143 u8 bssid[ETH_ALEN];
1144 char *s, *nr_s;
1145
1146 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
1147 if (!tb[0] || !tb[1] || !tb[2])
1148 goto invalid;
1149
1150 /* Neighbor Report binary */
1151 nr_s = blobmsg_get_string(tb[2]);
1152 data = wpabuf_parse_bin(nr_s);
1153 if (!data)
1154 goto invalid;
1155
1156 /* BSSID */
1157 s = blobmsg_get_string(tb[0]);
1158 if (strlen(s) == 0) {
1159 /* Copy BSSID from neighbor report */
1160 if (hwaddr_compact_aton(nr_s, bssid))
1161 goto invalid;
1162 } else if (hwaddr_aton(s, bssid)) {
1163 goto invalid;
1164 }
1165
1166 /* SSID */
1167 s = blobmsg_get_string(tb[1]);
1168 if (strlen(s) == 0) {
1169 /* Copy SSID from hostapd BSS conf */
1170 memcpy(&ssid, &hapd->conf->ssid, sizeof(ssid));
1171 } else {
1172 ssid.ssid_len = strlen(s);
1173 if (ssid.ssid_len > sizeof(ssid.ssid))
1174 goto invalid;
1175
1176 memcpy(&ssid, s, ssid.ssid_len);
1177 }
1178
1179 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
1180 wpabuf_free(data);
1181 continue;
1182
1183 invalid:
1184 return UBUS_STATUS_INVALID_ARGUMENT;
1185 }
1186
1187 return 0;
1188 }
1189
1190 enum {
1191 BEACON_REQ_ADDR,
1192 BEACON_REQ_MODE,
1193 BEACON_REQ_OP_CLASS,
1194 BEACON_REQ_CHANNEL,
1195 BEACON_REQ_DURATION,
1196 BEACON_REQ_BSSID,
1197 BEACON_REQ_SSID,
1198 __BEACON_REQ_MAX,
1199 };
1200
1201 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
1202 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1203 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
1204 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
1205 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1206 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
1207 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
1208 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
1209 };
1210
1211 static int
1212 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
1213 struct ubus_request_data *ureq, const char *method,
1214 struct blob_attr *msg)
1215 {
1216 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1217 struct blob_attr *tb[__BEACON_REQ_MAX];
1218 struct blob_attr *cur;
1219 struct wpabuf *req;
1220 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1221 u8 addr[ETH_ALEN];
1222 int mode, rem, ret;
1223 int buf_len = 13;
1224
1225 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
1226
1227 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
1228 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
1229 return UBUS_STATUS_INVALID_ARGUMENT;
1230
1231 if (tb[BEACON_REQ_SSID])
1232 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
1233
1234 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
1235 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
1236 return UBUS_STATUS_INVALID_ARGUMENT;
1237
1238 if (tb[BEACON_REQ_BSSID] &&
1239 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
1240 return UBUS_STATUS_INVALID_ARGUMENT;
1241
1242 req = wpabuf_alloc(buf_len);
1243 if (!req)
1244 return UBUS_STATUS_UNKNOWN_ERROR;
1245
1246 /* 1: regulatory class */
1247 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
1248
1249 /* 2: channel number */
1250 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
1251
1252 /* 3-4: randomization interval */
1253 wpabuf_put_le16(req, 0);
1254
1255 /* 5-6: duration */
1256 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
1257
1258 /* 7: mode */
1259 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
1260
1261 /* 8-13: BSSID */
1262 wpabuf_put_data(req, bssid, ETH_ALEN);
1263
1264 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
1265 wpabuf_put_u8(req, WLAN_EID_SSID);
1266 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
1267 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
1268 }
1269
1270 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
1271 if (ret < 0)
1272 return -ret;
1273
1274 return 0;
1275 }
1276
1277
1278 #ifdef CONFIG_WNM_AP
1279
1280 static int
1281 hostapd_bss_tr_send(struct hostapd_data *hapd, u8 *addr, bool disassoc_imminent, bool abridged,
1282 u16 disassoc_timer, u8 validity_period, struct blob_attr *neighbors)
1283 {
1284 struct blob_attr *cur;
1285 struct sta_info *sta;
1286 int nr_len = 0;
1287 int rem;
1288 u8 *nr = NULL;
1289 u8 req_mode = 0;
1290
1291 sta = ap_get_sta(hapd, addr);
1292 if (!sta)
1293 return UBUS_STATUS_NOT_FOUND;
1294
1295 if (neighbors) {
1296 u8 *nr_cur;
1297
1298 if (blobmsg_check_array(neighbors,
1299 BLOBMSG_TYPE_STRING) < 0)
1300 return UBUS_STATUS_INVALID_ARGUMENT;
1301
1302 blobmsg_for_each_attr(cur, neighbors, rem) {
1303 int len = strlen(blobmsg_get_string(cur));
1304
1305 if (len % 2)
1306 return UBUS_STATUS_INVALID_ARGUMENT;
1307
1308 nr_len += (len / 2) + 2;
1309 }
1310
1311 if (nr_len) {
1312 nr = os_zalloc(nr_len);
1313 if (!nr)
1314 return UBUS_STATUS_UNKNOWN_ERROR;
1315 }
1316
1317 nr_cur = nr;
1318 blobmsg_for_each_attr(cur, neighbors, rem) {
1319 int len = strlen(blobmsg_get_string(cur)) / 2;
1320
1321 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
1322 *nr_cur++ = (u8) len;
1323 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
1324 free(nr);
1325 return UBUS_STATUS_INVALID_ARGUMENT;
1326 }
1327
1328 nr_cur += len;
1329 }
1330 }
1331
1332 if (nr)
1333 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1334
1335 if (abridged)
1336 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1337
1338 if (disassoc_imminent)
1339 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1340
1341 if (wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer, validity_period, NULL,
1342 NULL, nr, nr_len, NULL, 0))
1343 return UBUS_STATUS_UNKNOWN_ERROR;
1344
1345 return 0;
1346 }
1347
1348 enum {
1349 BSS_TR_ADDR,
1350 BSS_TR_DA_IMMINENT,
1351 BSS_TR_DA_TIMER,
1352 BSS_TR_VALID_PERIOD,
1353 BSS_TR_NEIGHBORS,
1354 BSS_TR_ABRIDGED,
1355 __BSS_TR_DISASSOC_MAX
1356 };
1357
1358 static const struct blobmsg_policy bss_tr_policy[__BSS_TR_DISASSOC_MAX] = {
1359 [BSS_TR_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1360 [BSS_TR_DA_IMMINENT] = { "disassociation_imminent", BLOBMSG_TYPE_BOOL },
1361 [BSS_TR_DA_TIMER] = { "disassociation_timer", BLOBMSG_TYPE_INT32 },
1362 [BSS_TR_VALID_PERIOD] = { "validity_period", BLOBMSG_TYPE_INT32 },
1363 [BSS_TR_NEIGHBORS] = { "neighbors", BLOBMSG_TYPE_ARRAY },
1364 [BSS_TR_ABRIDGED] = { "abridged", BLOBMSG_TYPE_BOOL },
1365 };
1366
1367 static int
1368 hostapd_bss_transition_request(struct ubus_context *ctx, struct ubus_object *obj,
1369 struct ubus_request_data *ureq, const char *method,
1370 struct blob_attr *msg)
1371 {
1372 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1373 struct blob_attr *tb[__BSS_TR_DISASSOC_MAX];
1374 struct sta_info *sta;
1375 u32 da_timer = 0;
1376 u32 valid_period = 0;
1377 u8 addr[ETH_ALEN];
1378 bool abridged;
1379 bool da_imminent;
1380
1381 blobmsg_parse(bss_tr_policy, __BSS_TR_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1382
1383 if (!tb[BSS_TR_ADDR])
1384 return UBUS_STATUS_INVALID_ARGUMENT;
1385
1386 if (hwaddr_aton(blobmsg_data(tb[BSS_TR_ADDR]), addr))
1387 return UBUS_STATUS_INVALID_ARGUMENT;
1388
1389 if (tb[BSS_TR_DA_TIMER])
1390 da_timer = blobmsg_get_u32(tb[BSS_TR_DA_TIMER]);
1391
1392 if (tb[BSS_TR_VALID_PERIOD])
1393 valid_period = blobmsg_get_u32(tb[BSS_TR_VALID_PERIOD]);
1394
1395 da_imminent = !!(tb[BSS_TR_DA_IMMINENT] && blobmsg_get_bool(tb[BSS_TR_DA_IMMINENT]));
1396 abridged = !!(tb[BSS_TR_ABRIDGED] && blobmsg_get_bool(tb[BSS_TR_ABRIDGED]));
1397
1398 return hostapd_bss_tr_send(hapd, addr, da_imminent, abridged, da_timer, valid_period,
1399 tb[BSS_TR_NEIGHBORS]);
1400 }
1401
1402 enum {
1403 WNM_DISASSOC_ADDR,
1404 WNM_DISASSOC_DURATION,
1405 WNM_DISASSOC_NEIGHBORS,
1406 WNM_DISASSOC_ABRIDGED,
1407 __WNM_DISASSOC_MAX,
1408 };
1409
1410 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
1411 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1412 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1413 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
1414 [WNM_DISASSOC_ABRIDGED] { "abridged", BLOBMSG_TYPE_BOOL },
1415 };
1416
1417 static int
1418 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
1419 struct ubus_request_data *ureq, const char *method,
1420 struct blob_attr *msg)
1421 {
1422 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1423 struct blob_attr *tb[__WNM_DISASSOC_MAX];
1424 struct sta_info *sta;
1425 int duration = 10;
1426 u8 addr[ETH_ALEN];
1427 bool abridged;
1428
1429 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1430
1431 if (!tb[WNM_DISASSOC_ADDR])
1432 return UBUS_STATUS_INVALID_ARGUMENT;
1433
1434 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
1435 return UBUS_STATUS_INVALID_ARGUMENT;
1436
1437 if (tb[WNM_DISASSOC_DURATION])
1438 duration = blobmsg_get_u32(tb[WNM_DISASSOC_DURATION]);
1439
1440 abridged = !!(tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]));
1441
1442 return hostapd_bss_tr_send(hapd, addr, true, abridged, duration, duration,
1443 tb[WNM_DISASSOC_NEIGHBORS]);
1444 }
1445 #endif
1446
1447 #ifdef CONFIG_AIRTIME_POLICY
1448 enum {
1449 UPDATE_AIRTIME_STA,
1450 UPDATE_AIRTIME_WEIGHT,
1451 __UPDATE_AIRTIME_MAX,
1452 };
1453
1454
1455 static const struct blobmsg_policy airtime_policy[__UPDATE_AIRTIME_MAX] = {
1456 [UPDATE_AIRTIME_STA] = { "sta", BLOBMSG_TYPE_STRING },
1457 [UPDATE_AIRTIME_WEIGHT] = { "weight", BLOBMSG_TYPE_INT32 },
1458 };
1459
1460 static int
1461 hostapd_bss_update_airtime(struct ubus_context *ctx, struct ubus_object *obj,
1462 struct ubus_request_data *ureq, const char *method,
1463 struct blob_attr *msg)
1464 {
1465 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1466 struct blob_attr *tb[__UPDATE_AIRTIME_MAX];
1467 struct sta_info *sta = NULL;
1468 u8 addr[ETH_ALEN];
1469 int weight;
1470
1471 blobmsg_parse(airtime_policy, __UPDATE_AIRTIME_MAX, tb, blob_data(msg), blob_len(msg));
1472
1473 if (!tb[UPDATE_AIRTIME_WEIGHT])
1474 return UBUS_STATUS_INVALID_ARGUMENT;
1475
1476 weight = blobmsg_get_u32(tb[UPDATE_AIRTIME_WEIGHT]);
1477
1478 if (!tb[UPDATE_AIRTIME_STA]) {
1479 if (!weight)
1480 return UBUS_STATUS_INVALID_ARGUMENT;
1481
1482 hapd->conf->airtime_weight = weight;
1483 return 0;
1484 }
1485
1486 if (hwaddr_aton(blobmsg_data(tb[UPDATE_AIRTIME_STA]), addr))
1487 return UBUS_STATUS_INVALID_ARGUMENT;
1488
1489 sta = ap_get_sta(hapd, addr);
1490 if (!sta)
1491 return UBUS_STATUS_NOT_FOUND;
1492
1493 sta->dyn_airtime_weight = weight;
1494 airtime_policy_new_sta(hapd, sta);
1495
1496 return 0;
1497 }
1498 #endif
1499
1500
1501 static const struct ubus_method bss_methods[] = {
1502 UBUS_METHOD_NOARG("reload", hostapd_bss_reload),
1503 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
1504 UBUS_METHOD_NOARG("get_status", hostapd_bss_get_status),
1505 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
1506 #ifdef CONFIG_AIRTIME_POLICY
1507 UBUS_METHOD("update_airtime", hostapd_bss_update_airtime, airtime_policy),
1508 #endif
1509 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
1510 #ifdef CONFIG_WPS
1511 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
1512 UBUS_METHOD_NOARG("wps_status", hostapd_bss_wps_status),
1513 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
1514 #endif
1515 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
1516 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features),
1517 #ifdef NEED_AP_MLME
1518 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
1519 #endif
1520 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
1521 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
1522 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
1523 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
1524 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
1525 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
1526 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
1527 #ifdef CONFIG_WNM_AP
1528 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
1529 UBUS_METHOD("bss_transition_request", hostapd_bss_transition_request, bss_tr_policy),
1530 #endif
1531 };
1532
1533 static struct ubus_object_type bss_object_type =
1534 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
1535
1536 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
1537 {
1538 return memcmp(k1, k2, ETH_ALEN);
1539 }
1540
1541 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
1542 {
1543 struct ubus_object *obj = &hapd->ubus.obj;
1544 char *name;
1545 int ret;
1546
1547 #ifdef CONFIG_MESH
1548 if (hapd->conf->mesh & MESH_ENABLED)
1549 return;
1550 #endif
1551
1552 if (!hostapd_ubus_init())
1553 return;
1554
1555 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
1556 return;
1557
1558 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
1559 obj->name = name;
1560 obj->type = &bss_object_type;
1561 obj->methods = bss_object_type.methods;
1562 obj->n_methods = bss_object_type.n_methods;
1563 ret = ubus_add_object(ctx, obj);
1564 hostapd_ubus_ref_inc();
1565
1566 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "add");
1567 }
1568
1569 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
1570 {
1571 struct ubus_object *obj = &hapd->ubus.obj;
1572 char *name = (char *) obj->name;
1573
1574 #ifdef CONFIG_MESH
1575 if (hapd->conf->mesh & MESH_ENABLED)
1576 return;
1577 #endif
1578
1579 if (!ctx)
1580 return;
1581
1582 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "remove");
1583
1584 if (obj->id) {
1585 ubus_remove_object(ctx, obj);
1586 hostapd_ubus_ref_dec();
1587 }
1588
1589 free(name);
1590 }
1591
1592 static void
1593 hostapd_ubus_vlan_action(struct hostapd_data *hapd, struct hostapd_vlan *vlan,
1594 const char *action)
1595 {
1596 struct vlan_description *desc = &vlan->vlan_desc;
1597 void *c;
1598 int i;
1599
1600 if (!hapd->ubus.obj.has_subscribers)
1601 return;
1602
1603 blob_buf_init(&b, 0);
1604 blobmsg_add_string(&b, "ifname", vlan->ifname);
1605 blobmsg_add_string(&b, "bridge", vlan->bridge);
1606 blobmsg_add_u32(&b, "vlan_id", vlan->vlan_id);
1607
1608 if (desc->notempty) {
1609 blobmsg_add_u32(&b, "untagged", desc->untagged);
1610 c = blobmsg_open_array(&b, "tagged");
1611 for (i = 0; i < ARRAY_SIZE(desc->tagged) && desc->tagged[i]; i++)
1612 blobmsg_add_u32(&b, "", desc->tagged[i]);
1613 blobmsg_close_array(&b, c);
1614 }
1615
1616 ubus_notify(ctx, &hapd->ubus.obj, action, b.head, -1);
1617 }
1618
1619 void hostapd_ubus_add_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
1620 {
1621 hostapd_ubus_vlan_action(hapd, vlan, "vlan_add");
1622 }
1623
1624 void hostapd_ubus_remove_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
1625 {
1626 hostapd_ubus_vlan_action(hapd, vlan, "vlan_remove");
1627 }
1628
1629 static const struct ubus_method daemon_methods[] = {
1630 UBUS_METHOD("config_add", hostapd_config_add, config_add_policy),
1631 UBUS_METHOD("config_remove", hostapd_config_remove, config_remove_policy),
1632 };
1633
1634 static struct ubus_object_type daemon_object_type =
1635 UBUS_OBJECT_TYPE("hostapd", daemon_methods);
1636
1637 void hostapd_ubus_add(struct hapd_interfaces *interfaces)
1638 {
1639 struct ubus_object *obj = &interfaces->ubus;
1640 int ret;
1641
1642 if (!hostapd_ubus_init())
1643 return;
1644
1645 obj->name = strdup("hostapd");
1646
1647 obj->type = &daemon_object_type;
1648 obj->methods = daemon_object_type.methods;
1649 obj->n_methods = daemon_object_type.n_methods;
1650 ret = ubus_add_object(ctx, obj);
1651 hostapd_ubus_ref_inc();
1652 }
1653
1654 void hostapd_ubus_free(struct hapd_interfaces *interfaces)
1655 {
1656 struct ubus_object *obj = &interfaces->ubus;
1657 char *name = (char *) obj->name;
1658
1659 if (!ctx)
1660 return;
1661
1662 if (obj->id) {
1663 ubus_remove_object(ctx, obj);
1664 hostapd_ubus_ref_dec();
1665 }
1666
1667 free(name);
1668 }
1669
1670 struct ubus_event_req {
1671 struct ubus_notify_request nreq;
1672 int resp;
1673 };
1674
1675 static void
1676 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1677 {
1678 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1679
1680 ureq->resp = ret;
1681 }
1682
1683 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1684 {
1685 struct ubus_banned_client *ban;
1686 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1687 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1688 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1689 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1690 };
1691 const char *type = "mgmt";
1692 struct ubus_event_req ureq = {};
1693 const u8 *addr;
1694
1695 if (req->mgmt_frame)
1696 addr = req->mgmt_frame->sa;
1697 else
1698 addr = req->addr;
1699
1700 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1701 if (ban)
1702 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1703
1704 if (!hapd->ubus.obj.has_subscribers)
1705 return WLAN_STATUS_SUCCESS;
1706
1707 if (req->type < ARRAY_SIZE(types))
1708 type = types[req->type];
1709
1710 blob_buf_init(&b, 0);
1711 blobmsg_add_macaddr(&b, "address", addr);
1712 if (req->mgmt_frame)
1713 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1714 if (req->ssi_signal)
1715 blobmsg_add_u32(&b, "signal", req->ssi_signal);
1716 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1717
1718 if (req->elems) {
1719 if(req->elems->ht_capabilities)
1720 {
1721 struct ieee80211_ht_capabilities *ht_capabilities;
1722 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1723
1724
1725 ht_capabilities = (struct ieee80211_ht_capabilities*) req->elems->ht_capabilities;
1726 ht_cap = blobmsg_open_table(&b, "ht_capabilities");
1727 blobmsg_add_u16(&b, "ht_capabilities_info", ht_capabilities->ht_capabilities_info);
1728 ht_cap_mcs_set = blobmsg_open_table(&b, "supported_mcs_set");
1729 blobmsg_add_u16(&b, "a_mpdu_params", ht_capabilities->a_mpdu_params);
1730 blobmsg_add_u16(&b, "ht_extended_capabilities", ht_capabilities->ht_extended_capabilities);
1731 blobmsg_add_u32(&b, "tx_bf_capability_info", ht_capabilities->tx_bf_capability_info);
1732 blobmsg_add_u16(&b, "asel_capabilities", ht_capabilities->asel_capabilities);
1733 mcs_set = blobmsg_open_array(&b, "supported_mcs_set");
1734 for (int i = 0; i < 16; i++) {
1735 blobmsg_add_u16(&b, NULL, (u16) ht_capabilities->supported_mcs_set[i]);
1736 }
1737 blobmsg_close_array(&b, mcs_set);
1738 blobmsg_close_table(&b, ht_cap_mcs_set);
1739 blobmsg_close_table(&b, ht_cap);
1740 }
1741 if(req->elems->vht_capabilities)
1742 {
1743 struct ieee80211_vht_capabilities *vht_capabilities;
1744 void *vht_cap, *vht_cap_mcs_set;
1745
1746 vht_capabilities = (struct ieee80211_vht_capabilities*) req->elems->vht_capabilities;
1747 vht_cap = blobmsg_open_table(&b, "vht_capabilities");
1748 blobmsg_add_u32(&b, "vht_capabilities_info", vht_capabilities->vht_capabilities_info);
1749 vht_cap_mcs_set = blobmsg_open_table(&b, "vht_supported_mcs_set");
1750 blobmsg_add_u16(&b, "rx_map", vht_capabilities->vht_supported_mcs_set.rx_map);
1751 blobmsg_add_u16(&b, "rx_highest", vht_capabilities->vht_supported_mcs_set.rx_highest);
1752 blobmsg_add_u16(&b, "tx_map", vht_capabilities->vht_supported_mcs_set.tx_map);
1753 blobmsg_add_u16(&b, "tx_highest", vht_capabilities->vht_supported_mcs_set.tx_highest);
1754 blobmsg_close_table(&b, vht_cap_mcs_set);
1755 blobmsg_close_table(&b, vht_cap);
1756 }
1757 }
1758
1759 if (!hapd->ubus.notify_response) {
1760 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1761 return WLAN_STATUS_SUCCESS;
1762 }
1763
1764 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1765 return WLAN_STATUS_SUCCESS;
1766
1767 ureq.nreq.status_cb = ubus_event_cb;
1768 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1769
1770 if (ureq.resp)
1771 return ureq.resp;
1772
1773 return WLAN_STATUS_SUCCESS;
1774 }
1775
1776 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1777 {
1778 if (!hapd->ubus.obj.has_subscribers)
1779 return;
1780
1781 if (!addr)
1782 return;
1783
1784 blob_buf_init(&b, 0);
1785 blobmsg_add_macaddr(&b, "address", addr);
1786
1787 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1788 }
1789
1790 void hostapd_ubus_notify_beacon_report(
1791 struct hostapd_data *hapd, const u8 *addr, u8 token, u8 rep_mode,
1792 struct rrm_measurement_beacon_report *rep, size_t len)
1793 {
1794 if (!hapd->ubus.obj.has_subscribers)
1795 return;
1796
1797 if (!addr || !rep)
1798 return;
1799
1800 blob_buf_init(&b, 0);
1801 blobmsg_add_macaddr(&b, "address", addr);
1802 blobmsg_add_u16(&b, "op-class", rep->op_class);
1803 blobmsg_add_u16(&b, "channel", rep->channel);
1804 blobmsg_add_u64(&b, "start-time", rep->start_time);
1805 blobmsg_add_u16(&b, "duration", rep->duration);
1806 blobmsg_add_u16(&b, "report-info", rep->report_info);
1807 blobmsg_add_u16(&b, "rcpi", rep->rcpi);
1808 blobmsg_add_u16(&b, "rsni", rep->rsni);
1809 blobmsg_add_macaddr(&b, "bssid", rep->bssid);
1810 blobmsg_add_u16(&b, "antenna-id", rep->antenna_id);
1811 blobmsg_add_u16(&b, "parent-tsf", rep->parent_tsf);
1812
1813 ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
1814 }
1815
1816 void hostapd_ubus_notify_radar_detected(struct hostapd_iface *iface, int frequency,
1817 int chan_width, int cf1, int cf2)
1818 {
1819 struct hostapd_data *hapd;
1820 int i;
1821
1822 blob_buf_init(&b, 0);
1823 blobmsg_add_u16(&b, "frequency", frequency);
1824 blobmsg_add_u16(&b, "width", chan_width);
1825 blobmsg_add_u16(&b, "center1", cf1);
1826 blobmsg_add_u16(&b, "center2", cf2);
1827
1828 for (i = 0; i < iface->num_bss; i++) {
1829 hapd = iface->bss[i];
1830 ubus_notify(ctx, &hapd->ubus.obj, "radar-detected", b.head, -1);
1831 }
1832 }
1833
1834 void hostapd_ubus_notify_bss_transition_response(
1835 struct hostapd_data *hapd, const u8 *addr, u8 dialog_token, u8 status_code,
1836 u8 bss_termination_delay, const u8 *target_bssid,
1837 const u8 *candidate_list, u16 candidate_list_len)
1838 {
1839 #ifdef CONFIG_WNM_AP
1840 char *cl_str;
1841 u16 i;
1842
1843 if (!hapd->ubus.obj.has_subscribers)
1844 return;
1845
1846 if (!addr)
1847 return;
1848
1849 blob_buf_init(&b, 0);
1850 blobmsg_add_macaddr(&b, "address", addr);
1851 blobmsg_add_u8(&b, "dialog-token", dialog_token);
1852 blobmsg_add_u8(&b, "status-code", status_code);
1853 blobmsg_add_u8(&b, "bss-termination-delay", bss_termination_delay);
1854 if (target_bssid)
1855 blobmsg_add_macaddr(&b, "target-bssid", target_bssid);
1856 if (candidate_list_len > 0) {
1857 cl_str = blobmsg_alloc_string_buffer(&b, "candidate-list", candidate_list_len * 2 + 1);
1858 for (i = 0; i < candidate_list_len; i++)
1859 snprintf(&cl_str[i*2], 3, "%02X", candidate_list[i]);
1860 blobmsg_add_string_buffer(&b);
1861 }
1862
1863 ubus_notify(ctx, &hapd->ubus.obj, "bss-transition-response", b.head, -1);
1864 #endif
1865 }