3c016124f99ed89f8bfd99f6cae57b9aa4693e78
[openwrt/openwrt.git] / package / network / services / hostapd / src / src / ap / ubus.c
1 /*
2 * hostapd / ubus support
3 * Copyright (c) 2013, Felix Fietkau <nbd@nbd.name>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "utils/eloop.h"
12 #include "utils/wpabuf.h"
13 #include "common/ieee802_11_defs.h"
14 #include "hostapd.h"
15 #include "neighbor_db.h"
16 #include "wps_hostapd.h"
17 #include "sta_info.h"
18 #include "ubus.h"
19 #include "ap_drv_ops.h"
20 #include "beacon.h"
21 #include "rrm.h"
22 #include "wnm_ap.h"
23 #include "taxonomy.h"
24 #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, *rrm_table, *wnm_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 u8 channel = 0, op_class = 0;
415
416 if (hapd->conf->ssid.ssid_len < SSID_MAX_LEN)
417 ssid_len = hapd->conf->ssid.ssid_len;
418
419 ieee80211_freq_to_channel_ext(hapd->iface->freq,
420 hapd->iconf->secondary_channel,
421 hostapd_get_oper_chwidth(hapd->iconf),
422 &op_class, &channel);
423
424 blob_buf_init(&b, 0);
425 blobmsg_add_string(&b, "status", hostapd_state_text(hapd->iface->state));
426 blobmsg_printf(&b, "bssid", MACSTR, MAC2STR(hapd->conf->bssid));
427
428 memset(ssid, 0, SSID_MAX_LEN + 1);
429 memcpy(ssid, hapd->conf->ssid.ssid, ssid_len);
430 blobmsg_add_string(&b, "ssid", ssid);
431
432 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
433 blobmsg_add_u32(&b, "channel", channel);
434 blobmsg_add_u32(&b, "op_class", op_class);
435 blobmsg_add_u32(&b, "beacon_interval", hapd->iconf->beacon_int);
436
437 snprintf(phy_name, 17, "%s", hapd->iface->phy);
438 blobmsg_add_string(&b, "phy", phy_name);
439
440 /* RRM */
441 rrm_table = blobmsg_open_table(&b, "rrm");
442 blobmsg_add_u64(&b, "neighbor_report_tx", hapd->openwrt_stats.rrm.neighbor_report_tx);
443 blobmsg_close_table(&b, rrm_table);
444
445 /* WNM */
446 wnm_table = blobmsg_open_table(&b, "wnm");
447 blobmsg_add_u64(&b, "bss_transition_query_rx", hapd->openwrt_stats.wnm.bss_transition_query_rx);
448 blobmsg_add_u64(&b, "bss_transition_request_tx", hapd->openwrt_stats.wnm.bss_transition_request_tx);
449 blobmsg_add_u64(&b, "bss_transition_response_rx", hapd->openwrt_stats.wnm.bss_transition_response_rx);
450 blobmsg_close_table(&b, wnm_table);
451
452 /* Airtime */
453 airtime_table = blobmsg_open_table(&b, "airtime");
454 blobmsg_add_u64(&b, "time", hapd->iface->last_channel_time);
455 blobmsg_add_u64(&b, "time_busy", hapd->iface->last_channel_time_busy);
456 blobmsg_add_u16(&b, "utilization", hapd->iface->channel_utilization);
457 blobmsg_close_table(&b, airtime_table);
458
459 /* DFS */
460 dfs_table = blobmsg_open_table(&b, "dfs");
461 blobmsg_add_u32(&b, "cac_seconds", hapd->iface->dfs_cac_ms / 1000);
462 blobmsg_add_u8(&b, "cac_active", !!(hapd->iface->cac_started));
463 os_reltime_age(&hapd->iface->dfs_cac_start, &now);
464 blobmsg_add_u32(&b, "cac_seconds_left",
465 hapd->iface->cac_started ? hapd->iface->dfs_cac_ms / 1000 - now.sec : 0);
466 blobmsg_close_table(&b, dfs_table);
467
468 ubus_send_reply(ctx, req, b.head);
469
470 return 0;
471 }
472
473 enum {
474 NOTIFY_RESPONSE,
475 __NOTIFY_MAX
476 };
477
478 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
479 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
480 };
481
482 static int
483 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
484 struct ubus_request_data *req, const char *method,
485 struct blob_attr *msg)
486 {
487 struct blob_attr *tb[__NOTIFY_MAX];
488 struct hostapd_data *hapd = get_hapd_from_object(obj);
489 struct wpabuf *elems;
490 const char *pos;
491 size_t len;
492
493 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
494 blob_data(msg), blob_len(msg));
495
496 if (!tb[NOTIFY_RESPONSE])
497 return UBUS_STATUS_INVALID_ARGUMENT;
498
499 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
500
501 return UBUS_STATUS_OK;
502 }
503
504 enum {
505 DEL_CLIENT_ADDR,
506 DEL_CLIENT_REASON,
507 DEL_CLIENT_DEAUTH,
508 DEL_CLIENT_BAN_TIME,
509 __DEL_CLIENT_MAX
510 };
511
512 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
513 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
514 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
515 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
516 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
517 };
518
519 static int
520 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
521 struct ubus_request_data *req, const char *method,
522 struct blob_attr *msg)
523 {
524 struct blob_attr *tb[__DEL_CLIENT_MAX];
525 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
526 struct sta_info *sta;
527 bool deauth = false;
528 int reason;
529 u8 addr[ETH_ALEN];
530
531 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
532
533 if (!tb[DEL_CLIENT_ADDR])
534 return UBUS_STATUS_INVALID_ARGUMENT;
535
536 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
537 return UBUS_STATUS_INVALID_ARGUMENT;
538
539 if (tb[DEL_CLIENT_REASON])
540 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
541
542 if (tb[DEL_CLIENT_DEAUTH])
543 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
544
545 sta = ap_get_sta(hapd, addr);
546 if (sta) {
547 if (deauth) {
548 hostapd_drv_sta_deauth(hapd, addr, reason);
549 ap_sta_deauthenticate(hapd, sta, reason);
550 } else {
551 hostapd_drv_sta_disassoc(hapd, addr, reason);
552 ap_sta_disassociate(hapd, sta, reason);
553 }
554 }
555
556 if (tb[DEL_CLIENT_BAN_TIME])
557 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
558
559 return 0;
560 }
561
562 static void
563 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
564 {
565 char *s;
566
567 s = blobmsg_alloc_string_buffer(buf, name, 20);
568 sprintf(s, MACSTR, MAC2STR(addr));
569 blobmsg_add_string_buffer(buf);
570 }
571
572 static int
573 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
574 struct ubus_request_data *req, const char *method,
575 struct blob_attr *msg)
576 {
577 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
578 struct ubus_banned_client *ban;
579 void *c;
580
581 blob_buf_init(&b, 0);
582 c = blobmsg_open_array(&b, "clients");
583 avl_for_each_element(&hapd->ubus.banned, ban, avl)
584 blobmsg_add_macaddr(&b, NULL, ban->addr);
585 blobmsg_close_array(&b, c);
586 ubus_send_reply(ctx, req, b.head);
587
588 return 0;
589 }
590
591 #ifdef CONFIG_WPS
592 static int
593 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
594 struct ubus_request_data *req, const char *method,
595 struct blob_attr *msg)
596 {
597 int rc;
598 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
599
600 rc = hostapd_wps_button_pushed(hapd, NULL);
601
602 if (rc != 0)
603 return UBUS_STATUS_NOT_SUPPORTED;
604
605 return 0;
606 }
607
608
609 static const char * pbc_status_enum_str(enum pbc_status status)
610 {
611 switch (status) {
612 case WPS_PBC_STATUS_DISABLE:
613 return "Disabled";
614 case WPS_PBC_STATUS_ACTIVE:
615 return "Active";
616 case WPS_PBC_STATUS_TIMEOUT:
617 return "Timed-out";
618 case WPS_PBC_STATUS_OVERLAP:
619 return "Overlap";
620 default:
621 return "Unknown";
622 }
623 }
624
625 static int
626 hostapd_bss_wps_status(struct ubus_context *ctx, struct ubus_object *obj,
627 struct ubus_request_data *req, const char *method,
628 struct blob_attr *msg)
629 {
630 int rc;
631 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
632
633 blob_buf_init(&b, 0);
634
635 blobmsg_add_string(&b, "pbc_status", pbc_status_enum_str(hapd->wps_stats.pbc_status));
636 blobmsg_add_string(&b, "last_wps_result",
637 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
638 "Success":
639 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
640 "Failed" : "None")));
641
642 /* If status == Failure - Add possible Reasons */
643 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
644 hapd->wps_stats.failure_reason > 0)
645 blobmsg_add_string(&b, "reason", wps_ei_str(hapd->wps_stats.failure_reason));
646
647 if (hapd->wps_stats.status)
648 blobmsg_printf(&b, "peer_address", MACSTR, MAC2STR(hapd->wps_stats.peer_addr));
649
650 ubus_send_reply(ctx, req, b.head);
651
652 return 0;
653 }
654
655 static int
656 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
657 struct ubus_request_data *req, const char *method,
658 struct blob_attr *msg)
659 {
660 int rc;
661 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
662
663 rc = hostapd_wps_cancel(hapd);
664
665 if (rc != 0)
666 return UBUS_STATUS_NOT_SUPPORTED;
667
668 return 0;
669 }
670 #endif /* CONFIG_WPS */
671
672 static int
673 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
674 struct ubus_request_data *req, const char *method,
675 struct blob_attr *msg)
676 {
677 int rc;
678 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
679
680 rc = ieee802_11_set_beacon(hapd);
681
682 if (rc != 0)
683 return UBUS_STATUS_NOT_SUPPORTED;
684
685 return 0;
686 }
687
688 enum {
689 CONFIG_IFACE,
690 CONFIG_FILE,
691 __CONFIG_MAX
692 };
693
694 static const struct blobmsg_policy config_add_policy[__CONFIG_MAX] = {
695 [CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
696 [CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
697 };
698
699 static int
700 hostapd_config_add(struct ubus_context *ctx, struct ubus_object *obj,
701 struct ubus_request_data *req, const char *method,
702 struct blob_attr *msg)
703 {
704 struct blob_attr *tb[__CONFIG_MAX];
705 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
706 char buf[128];
707
708 blobmsg_parse(config_add_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
709
710 if (!tb[CONFIG_FILE] || !tb[CONFIG_IFACE])
711 return UBUS_STATUS_INVALID_ARGUMENT;
712
713 snprintf(buf, sizeof(buf), "bss_config=%s:%s",
714 blobmsg_get_string(tb[CONFIG_IFACE]),
715 blobmsg_get_string(tb[CONFIG_FILE]));
716
717 if (hostapd_add_iface(interfaces, buf))
718 return UBUS_STATUS_INVALID_ARGUMENT;
719
720 blob_buf_init(&b, 0);
721 blobmsg_add_u32(&b, "pid", getpid());
722 ubus_send_reply(ctx, req, b.head);
723
724 return UBUS_STATUS_OK;
725 }
726
727 enum {
728 CONFIG_REM_IFACE,
729 __CONFIG_REM_MAX
730 };
731
732 static const struct blobmsg_policy config_remove_policy[__CONFIG_REM_MAX] = {
733 [CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
734 };
735
736 static int
737 hostapd_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
738 struct ubus_request_data *req, const char *method,
739 struct blob_attr *msg)
740 {
741 struct blob_attr *tb[__CONFIG_REM_MAX];
742 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
743 char buf[128];
744
745 blobmsg_parse(config_remove_policy, __CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
746
747 if (!tb[CONFIG_REM_IFACE])
748 return UBUS_STATUS_INVALID_ARGUMENT;
749
750 if (hostapd_remove_iface(interfaces, blobmsg_get_string(tb[CONFIG_REM_IFACE])))
751 return UBUS_STATUS_INVALID_ARGUMENT;
752
753 return UBUS_STATUS_OK;
754 }
755
756 enum {
757 CSA_FREQ,
758 CSA_BCN_COUNT,
759 CSA_CENTER_FREQ1,
760 CSA_CENTER_FREQ2,
761 CSA_BANDWIDTH,
762 CSA_SEC_CHANNEL_OFFSET,
763 CSA_HT,
764 CSA_VHT,
765 CSA_HE,
766 CSA_BLOCK_TX,
767 CSA_FORCE,
768 __CSA_MAX
769 };
770
771 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
772 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
773 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
774 [CSA_CENTER_FREQ1] = { "center_freq1", BLOBMSG_TYPE_INT32 },
775 [CSA_CENTER_FREQ2] = { "center_freq2", BLOBMSG_TYPE_INT32 },
776 [CSA_BANDWIDTH] = { "bandwidth", BLOBMSG_TYPE_INT32 },
777 [CSA_SEC_CHANNEL_OFFSET] = { "sec_channel_offset", BLOBMSG_TYPE_INT32 },
778 [CSA_HT] = { "ht", BLOBMSG_TYPE_BOOL },
779 [CSA_VHT] = { "vht", BLOBMSG_TYPE_BOOL },
780 [CSA_HE] = { "he", BLOBMSG_TYPE_BOOL },
781 [CSA_BLOCK_TX] = { "block_tx", BLOBMSG_TYPE_BOOL },
782 [CSA_FORCE] = { "force", BLOBMSG_TYPE_BOOL },
783 };
784
785
786 static void switch_chan_fallback_cb(void *eloop_data, void *user_ctx)
787 {
788 struct hostapd_iface *iface = eloop_data;
789 struct hostapd_freq_params *freq_params = user_ctx;
790
791 hostapd_switch_channel_fallback(iface, freq_params);
792 }
793
794 #ifdef NEED_AP_MLME
795 static int
796 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
797 struct ubus_request_data *req, const char *method,
798 struct blob_attr *msg)
799 {
800 struct blob_attr *tb[__CSA_MAX];
801 struct hostapd_data *hapd = get_hapd_from_object(obj);
802 struct hostapd_config *iconf = hapd->iface->conf;
803 struct hostapd_freq_params *freq_params;
804 struct csa_settings css = {
805 .freq_params = {
806 .ht_enabled = iconf->ieee80211n,
807 .vht_enabled = iconf->ieee80211ac,
808 .he_enabled = iconf->ieee80211ax,
809 .sec_channel_offset = iconf->secondary_channel,
810 }
811 };
812 int ret = UBUS_STATUS_OK;
813 int i;
814
815 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
816
817 if (!tb[CSA_FREQ])
818 return UBUS_STATUS_INVALID_ARGUMENT;
819
820 switch (iconf->vht_oper_chwidth) {
821 case CHANWIDTH_USE_HT:
822 if (iconf->secondary_channel)
823 css.freq_params.bandwidth = 40;
824 else
825 css.freq_params.bandwidth = 20;
826 break;
827 case CHANWIDTH_160MHZ:
828 css.freq_params.bandwidth = 160;
829 break;
830 default:
831 css.freq_params.bandwidth = 80;
832 break;
833 }
834
835 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
836
837 #define SET_CSA_SETTING(name, field, type) \
838 do { \
839 if (tb[name]) \
840 css.field = blobmsg_get_ ## type(tb[name]); \
841 } while(0)
842
843 SET_CSA_SETTING(CSA_BCN_COUNT, cs_count, u32);
844 SET_CSA_SETTING(CSA_CENTER_FREQ1, freq_params.center_freq1, u32);
845 SET_CSA_SETTING(CSA_CENTER_FREQ2, freq_params.center_freq2, u32);
846 SET_CSA_SETTING(CSA_BANDWIDTH, freq_params.bandwidth, u32);
847 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET, freq_params.sec_channel_offset, u32);
848 SET_CSA_SETTING(CSA_HT, freq_params.ht_enabled, bool);
849 SET_CSA_SETTING(CSA_VHT, freq_params.vht_enabled, bool);
850 SET_CSA_SETTING(CSA_HE, freq_params.he_enabled, bool);
851 SET_CSA_SETTING(CSA_BLOCK_TX, block_tx, bool);
852
853 for (i = 0; i < hapd->iface->num_bss; i++) {
854 struct hostapd_data *bss = hapd->iface->bss[i];
855
856 if (hostapd_switch_channel(bss, &css) != 0)
857 ret = UBUS_STATUS_NOT_SUPPORTED;
858 }
859
860 if (!ret || !tb[CSA_FORCE] || !blobmsg_get_bool(tb[CSA_FORCE]))
861 return ret;
862
863 freq_params = malloc(sizeof(*freq_params));
864 memcpy(freq_params, &css.freq_params, sizeof(*freq_params));
865 eloop_register_timeout(0, 1, switch_chan_fallback_cb,
866 hapd->iface, freq_params);
867
868 return 0;
869 #undef SET_CSA_SETTING
870 }
871 #endif
872
873 enum {
874 VENDOR_ELEMENTS,
875 __VENDOR_ELEMENTS_MAX
876 };
877
878 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
879 /* vendor elements are provided as hex-string */
880 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
881 };
882
883 static int
884 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
885 struct ubus_request_data *req, const char *method,
886 struct blob_attr *msg)
887 {
888 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
889 struct hostapd_data *hapd = get_hapd_from_object(obj);
890 struct hostapd_bss_config *bss = hapd->conf;
891 struct wpabuf *elems;
892 const char *pos;
893 size_t len;
894
895 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
896 blob_data(msg), blob_len(msg));
897
898 if (!tb[VENDOR_ELEMENTS])
899 return UBUS_STATUS_INVALID_ARGUMENT;
900
901 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
902 len = os_strlen(pos);
903 if (len & 0x01)
904 return UBUS_STATUS_INVALID_ARGUMENT;
905
906 len /= 2;
907 if (len == 0) {
908 wpabuf_free(bss->vendor_elements);
909 bss->vendor_elements = NULL;
910 return 0;
911 }
912
913 elems = wpabuf_alloc(len);
914 if (elems == NULL)
915 return 1;
916
917 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
918 wpabuf_free(elems);
919 return UBUS_STATUS_INVALID_ARGUMENT;
920 }
921
922 wpabuf_free(bss->vendor_elements);
923 bss->vendor_elements = elems;
924
925 /* update beacons if vendor elements were set successfully */
926 if (ieee802_11_update_beacons(hapd->iface) != 0)
927 return UBUS_STATUS_NOT_SUPPORTED;
928 return UBUS_STATUS_OK;
929 }
930
931 static void
932 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
933 {
934 const u8 *data;
935 char *str;
936 int len;
937
938 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
939
940 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
941 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
942 str[nr->ssid.ssid_len] = 0;
943 blobmsg_add_string_buffer(&b);
944
945 len = wpabuf_len(nr->nr);
946 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
947 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
948 blobmsg_add_string_buffer(&b);
949 }
950
951 enum {
952 BSS_MGMT_EN_NEIGHBOR,
953 BSS_MGMT_EN_BEACON,
954 #ifdef CONFIG_WNM_AP
955 BSS_MGMT_EN_BSS_TRANSITION,
956 #endif
957 __BSS_MGMT_EN_MAX
958 };
959
960 static bool
961 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
962 {
963 struct hostapd_bss_config *bss = hapd->conf;
964 uint32_t flags;
965
966 switch (flag) {
967 case BSS_MGMT_EN_NEIGHBOR:
968 if (bss->radio_measurements[0] &
969 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
970 return false;
971
972 bss->radio_measurements[0] |=
973 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
974 hostapd_neighbor_set_own_report(hapd);
975 return true;
976 case BSS_MGMT_EN_BEACON:
977 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
978 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
979 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
980
981 if (bss->radio_measurements[0] & flags == flags)
982 return false;
983
984 bss->radio_measurements[0] |= (u8) flags;
985 return true;
986 #ifdef CONFIG_WNM_AP
987 case BSS_MGMT_EN_BSS_TRANSITION:
988 if (bss->bss_transition)
989 return false;
990
991 bss->bss_transition = 1;
992 return true;
993 #endif
994 }
995 }
996
997 static void
998 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
999 {
1000 bool update = false;
1001 int i;
1002
1003 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
1004 if (!(flags & (1 << i)))
1005 continue;
1006
1007 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
1008 }
1009
1010 if (update)
1011 ieee802_11_update_beacons(hapd->iface);
1012 }
1013
1014
1015 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
1016 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
1017 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
1018 #ifdef CONFIG_WNM_AP
1019 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
1020 #endif
1021 };
1022
1023 static int
1024 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
1025 struct ubus_request_data *req, const char *method,
1026 struct blob_attr *msg)
1027
1028 {
1029 struct hostapd_data *hapd = get_hapd_from_object(obj);
1030 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
1031 struct blob_attr *cur;
1032 uint32_t flags = 0;
1033 int i;
1034 bool neigh = false, beacon = false;
1035
1036 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
1037
1038 for (i = 0; i < ARRAY_SIZE(tb); i++) {
1039 if (!tb[i] || !blobmsg_get_bool(tb[i]))
1040 continue;
1041
1042 flags |= (1 << i);
1043 }
1044
1045 __hostapd_bss_mgmt_enable(hapd, flags);
1046 }
1047
1048
1049 static void
1050 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
1051 {
1052 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
1053 }
1054
1055 static int
1056 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
1057 struct ubus_request_data *req, const char *method,
1058 struct blob_attr *msg)
1059 {
1060 struct hostapd_data *hapd = get_hapd_from_object(obj);
1061 struct hostapd_neighbor_entry *nr;
1062 void *c;
1063
1064 hostapd_rrm_nr_enable(hapd);
1065
1066 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
1067 if (!nr)
1068 return UBUS_STATUS_NOT_FOUND;
1069
1070 blob_buf_init(&b, 0);
1071
1072 c = blobmsg_open_array(&b, "value");
1073 hostapd_rrm_print_nr(nr);
1074 blobmsg_close_array(&b, c);
1075
1076 ubus_send_reply(ctx, req, b.head);
1077
1078 return 0;
1079 }
1080
1081 static int
1082 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
1083 struct ubus_request_data *req, const char *method,
1084 struct blob_attr *msg)
1085 {
1086 struct hostapd_data *hapd = get_hapd_from_object(obj);
1087 struct hostapd_neighbor_entry *nr;
1088 void *c;
1089
1090 hostapd_rrm_nr_enable(hapd);
1091 blob_buf_init(&b, 0);
1092
1093 c = blobmsg_open_array(&b, "list");
1094 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
1095 void *cur;
1096
1097 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
1098 continue;
1099
1100 cur = blobmsg_open_array(&b, NULL);
1101 hostapd_rrm_print_nr(nr);
1102 blobmsg_close_array(&b, cur);
1103 }
1104 blobmsg_close_array(&b, c);
1105
1106 ubus_send_reply(ctx, req, b.head);
1107
1108 return 0;
1109 }
1110
1111 enum {
1112 NR_SET_LIST,
1113 __NR_SET_LIST_MAX
1114 };
1115
1116 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
1117 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
1118 };
1119
1120
1121 static void
1122 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
1123 {
1124 struct hostapd_neighbor_entry *nr;
1125
1126 restart:
1127 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
1128 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
1129 continue;
1130
1131 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
1132 goto restart;
1133 }
1134 }
1135
1136 static int
1137 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
1138 struct ubus_request_data *req, const char *method,
1139 struct blob_attr *msg)
1140 {
1141 static const struct blobmsg_policy nr_e_policy[] = {
1142 { .type = BLOBMSG_TYPE_STRING },
1143 { .type = BLOBMSG_TYPE_STRING },
1144 { .type = BLOBMSG_TYPE_STRING },
1145 };
1146 struct hostapd_data *hapd = get_hapd_from_object(obj);
1147 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
1148 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
1149 struct blob_attr *cur;
1150 int rem;
1151
1152 hostapd_rrm_nr_enable(hapd);
1153
1154 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
1155 if (!tb_l[NR_SET_LIST])
1156 return UBUS_STATUS_INVALID_ARGUMENT;
1157
1158 hostapd_rrm_nr_clear(hapd);
1159 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
1160 struct wpa_ssid_value ssid;
1161 struct wpabuf *data;
1162 u8 bssid[ETH_ALEN];
1163 char *s, *nr_s;
1164
1165 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
1166 if (!tb[0] || !tb[1] || !tb[2])
1167 goto invalid;
1168
1169 /* Neighbor Report binary */
1170 nr_s = blobmsg_get_string(tb[2]);
1171 data = wpabuf_parse_bin(nr_s);
1172 if (!data)
1173 goto invalid;
1174
1175 /* BSSID */
1176 s = blobmsg_get_string(tb[0]);
1177 if (strlen(s) == 0) {
1178 /* Copy BSSID from neighbor report */
1179 if (hwaddr_compact_aton(nr_s, bssid))
1180 goto invalid;
1181 } else if (hwaddr_aton(s, bssid)) {
1182 goto invalid;
1183 }
1184
1185 /* SSID */
1186 s = blobmsg_get_string(tb[1]);
1187 if (strlen(s) == 0) {
1188 /* Copy SSID from hostapd BSS conf */
1189 memcpy(&ssid, &hapd->conf->ssid, sizeof(ssid));
1190 } else {
1191 ssid.ssid_len = strlen(s);
1192 if (ssid.ssid_len > sizeof(ssid.ssid))
1193 goto invalid;
1194
1195 memcpy(&ssid, s, ssid.ssid_len);
1196 }
1197
1198 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
1199 wpabuf_free(data);
1200 continue;
1201
1202 invalid:
1203 return UBUS_STATUS_INVALID_ARGUMENT;
1204 }
1205
1206 return 0;
1207 }
1208
1209 enum {
1210 BEACON_REQ_ADDR,
1211 BEACON_REQ_MODE,
1212 BEACON_REQ_OP_CLASS,
1213 BEACON_REQ_CHANNEL,
1214 BEACON_REQ_DURATION,
1215 BEACON_REQ_BSSID,
1216 BEACON_REQ_SSID,
1217 __BEACON_REQ_MAX,
1218 };
1219
1220 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
1221 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1222 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
1223 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
1224 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1225 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
1226 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
1227 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
1228 };
1229
1230 static int
1231 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
1232 struct ubus_request_data *ureq, const char *method,
1233 struct blob_attr *msg)
1234 {
1235 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1236 struct blob_attr *tb[__BEACON_REQ_MAX];
1237 struct blob_attr *cur;
1238 struct wpabuf *req;
1239 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1240 u8 addr[ETH_ALEN];
1241 int mode, rem, ret;
1242 int buf_len = 13;
1243
1244 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
1245
1246 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
1247 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
1248 return UBUS_STATUS_INVALID_ARGUMENT;
1249
1250 if (tb[BEACON_REQ_SSID])
1251 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
1252
1253 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
1254 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
1255 return UBUS_STATUS_INVALID_ARGUMENT;
1256
1257 if (tb[BEACON_REQ_BSSID] &&
1258 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
1259 return UBUS_STATUS_INVALID_ARGUMENT;
1260
1261 req = wpabuf_alloc(buf_len);
1262 if (!req)
1263 return UBUS_STATUS_UNKNOWN_ERROR;
1264
1265 /* 1: regulatory class */
1266 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
1267
1268 /* 2: channel number */
1269 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
1270
1271 /* 3-4: randomization interval */
1272 wpabuf_put_le16(req, 0);
1273
1274 /* 5-6: duration */
1275 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
1276
1277 /* 7: mode */
1278 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
1279
1280 /* 8-13: BSSID */
1281 wpabuf_put_data(req, bssid, ETH_ALEN);
1282
1283 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
1284 wpabuf_put_u8(req, WLAN_EID_SSID);
1285 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
1286 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
1287 }
1288
1289 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
1290 if (ret < 0)
1291 return -ret;
1292
1293 return 0;
1294 }
1295
1296
1297 #ifdef CONFIG_WNM_AP
1298
1299 static int
1300 hostapd_bss_tr_send(struct hostapd_data *hapd, u8 *addr, bool disassoc_imminent, bool abridged,
1301 u16 disassoc_timer, u8 validity_period, u8 dialog_token,
1302 struct blob_attr *neighbors)
1303 {
1304 struct blob_attr *cur;
1305 struct sta_info *sta;
1306 int nr_len = 0;
1307 int rem;
1308 u8 *nr = NULL;
1309 u8 req_mode = 0;
1310
1311 sta = ap_get_sta(hapd, addr);
1312 if (!sta)
1313 return UBUS_STATUS_NOT_FOUND;
1314
1315 if (neighbors) {
1316 u8 *nr_cur;
1317
1318 if (blobmsg_check_array(neighbors,
1319 BLOBMSG_TYPE_STRING) < 0)
1320 return UBUS_STATUS_INVALID_ARGUMENT;
1321
1322 blobmsg_for_each_attr(cur, neighbors, rem) {
1323 int len = strlen(blobmsg_get_string(cur));
1324
1325 if (len % 2)
1326 return UBUS_STATUS_INVALID_ARGUMENT;
1327
1328 nr_len += (len / 2) + 2;
1329 }
1330
1331 if (nr_len) {
1332 nr = os_zalloc(nr_len);
1333 if (!nr)
1334 return UBUS_STATUS_UNKNOWN_ERROR;
1335 }
1336
1337 nr_cur = nr;
1338 blobmsg_for_each_attr(cur, neighbors, rem) {
1339 int len = strlen(blobmsg_get_string(cur)) / 2;
1340
1341 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
1342 *nr_cur++ = (u8) len;
1343 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
1344 free(nr);
1345 return UBUS_STATUS_INVALID_ARGUMENT;
1346 }
1347
1348 nr_cur += len;
1349 }
1350 }
1351
1352 if (nr)
1353 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1354
1355 if (abridged)
1356 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1357
1358 if (disassoc_imminent)
1359 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1360
1361 if (wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer, validity_period, NULL,
1362 dialog_token, NULL, nr, nr_len, NULL, 0))
1363 return UBUS_STATUS_UNKNOWN_ERROR;
1364
1365 return 0;
1366 }
1367
1368 enum {
1369 BSS_TR_ADDR,
1370 BSS_TR_DA_IMMINENT,
1371 BSS_TR_DA_TIMER,
1372 BSS_TR_VALID_PERIOD,
1373 BSS_TR_NEIGHBORS,
1374 BSS_TR_ABRIDGED,
1375 BSS_TR_DIALOG_TOKEN,
1376 __BSS_TR_DISASSOC_MAX
1377 };
1378
1379 static const struct blobmsg_policy bss_tr_policy[__BSS_TR_DISASSOC_MAX] = {
1380 [BSS_TR_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1381 [BSS_TR_DA_IMMINENT] = { "disassociation_imminent", BLOBMSG_TYPE_BOOL },
1382 [BSS_TR_DA_TIMER] = { "disassociation_timer", BLOBMSG_TYPE_INT32 },
1383 [BSS_TR_VALID_PERIOD] = { "validity_period", BLOBMSG_TYPE_INT32 },
1384 [BSS_TR_NEIGHBORS] = { "neighbors", BLOBMSG_TYPE_ARRAY },
1385 [BSS_TR_ABRIDGED] = { "abridged", BLOBMSG_TYPE_BOOL },
1386 [BSS_TR_DIALOG_TOKEN] = { "dialog_token", BLOBMSG_TYPE_INT32 },
1387 };
1388
1389 static int
1390 hostapd_bss_transition_request(struct ubus_context *ctx, struct ubus_object *obj,
1391 struct ubus_request_data *ureq, const char *method,
1392 struct blob_attr *msg)
1393 {
1394 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1395 struct blob_attr *tb[__BSS_TR_DISASSOC_MAX];
1396 struct sta_info *sta;
1397 u32 da_timer = 0;
1398 u32 valid_period = 0;
1399 u8 addr[ETH_ALEN];
1400 u32 dialog_token = 1;
1401 bool abridged;
1402 bool da_imminent;
1403
1404 blobmsg_parse(bss_tr_policy, __BSS_TR_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1405
1406 if (!tb[BSS_TR_ADDR])
1407 return UBUS_STATUS_INVALID_ARGUMENT;
1408
1409 if (hwaddr_aton(blobmsg_data(tb[BSS_TR_ADDR]), addr))
1410 return UBUS_STATUS_INVALID_ARGUMENT;
1411
1412 if (tb[BSS_TR_DA_TIMER])
1413 da_timer = blobmsg_get_u32(tb[BSS_TR_DA_TIMER]);
1414
1415 if (tb[BSS_TR_VALID_PERIOD])
1416 valid_period = blobmsg_get_u32(tb[BSS_TR_VALID_PERIOD]);
1417
1418 if (tb[BSS_TR_DIALOG_TOKEN])
1419 dialog_token = blobmsg_get_u32(tb[BSS_TR_DIALOG_TOKEN]);
1420
1421 da_imminent = !!(tb[BSS_TR_DA_IMMINENT] && blobmsg_get_bool(tb[BSS_TR_DA_IMMINENT]));
1422 abridged = !!(tb[BSS_TR_ABRIDGED] && blobmsg_get_bool(tb[BSS_TR_ABRIDGED]));
1423
1424 return hostapd_bss_tr_send(hapd, addr, da_imminent, abridged, da_timer, valid_period,
1425 dialog_token, tb[BSS_TR_NEIGHBORS]);
1426 }
1427
1428 enum {
1429 WNM_DISASSOC_ADDR,
1430 WNM_DISASSOC_DURATION,
1431 WNM_DISASSOC_NEIGHBORS,
1432 WNM_DISASSOC_ABRIDGED,
1433 __WNM_DISASSOC_MAX,
1434 };
1435
1436 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
1437 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1438 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1439 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
1440 [WNM_DISASSOC_ABRIDGED] { "abridged", BLOBMSG_TYPE_BOOL },
1441 };
1442
1443 static int
1444 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
1445 struct ubus_request_data *ureq, const char *method,
1446 struct blob_attr *msg)
1447 {
1448 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1449 struct blob_attr *tb[__WNM_DISASSOC_MAX];
1450 struct sta_info *sta;
1451 int duration = 10;
1452 u8 addr[ETH_ALEN];
1453 bool abridged;
1454
1455 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1456
1457 if (!tb[WNM_DISASSOC_ADDR])
1458 return UBUS_STATUS_INVALID_ARGUMENT;
1459
1460 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
1461 return UBUS_STATUS_INVALID_ARGUMENT;
1462
1463 if (tb[WNM_DISASSOC_DURATION])
1464 duration = blobmsg_get_u32(tb[WNM_DISASSOC_DURATION]);
1465
1466 abridged = !!(tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]));
1467
1468 return hostapd_bss_tr_send(hapd, addr, true, abridged, duration, duration,
1469 1, tb[WNM_DISASSOC_NEIGHBORS]);
1470 }
1471 #endif
1472
1473 #ifdef CONFIG_AIRTIME_POLICY
1474 enum {
1475 UPDATE_AIRTIME_STA,
1476 UPDATE_AIRTIME_WEIGHT,
1477 __UPDATE_AIRTIME_MAX,
1478 };
1479
1480
1481 static const struct blobmsg_policy airtime_policy[__UPDATE_AIRTIME_MAX] = {
1482 [UPDATE_AIRTIME_STA] = { "sta", BLOBMSG_TYPE_STRING },
1483 [UPDATE_AIRTIME_WEIGHT] = { "weight", BLOBMSG_TYPE_INT32 },
1484 };
1485
1486 static int
1487 hostapd_bss_update_airtime(struct ubus_context *ctx, struct ubus_object *obj,
1488 struct ubus_request_data *ureq, const char *method,
1489 struct blob_attr *msg)
1490 {
1491 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1492 struct blob_attr *tb[__UPDATE_AIRTIME_MAX];
1493 struct sta_info *sta = NULL;
1494 u8 addr[ETH_ALEN];
1495 int weight;
1496
1497 blobmsg_parse(airtime_policy, __UPDATE_AIRTIME_MAX, tb, blob_data(msg), blob_len(msg));
1498
1499 if (!tb[UPDATE_AIRTIME_WEIGHT])
1500 return UBUS_STATUS_INVALID_ARGUMENT;
1501
1502 weight = blobmsg_get_u32(tb[UPDATE_AIRTIME_WEIGHT]);
1503
1504 if (!tb[UPDATE_AIRTIME_STA]) {
1505 if (!weight)
1506 return UBUS_STATUS_INVALID_ARGUMENT;
1507
1508 hapd->conf->airtime_weight = weight;
1509 return 0;
1510 }
1511
1512 if (hwaddr_aton(blobmsg_data(tb[UPDATE_AIRTIME_STA]), addr))
1513 return UBUS_STATUS_INVALID_ARGUMENT;
1514
1515 sta = ap_get_sta(hapd, addr);
1516 if (!sta)
1517 return UBUS_STATUS_NOT_FOUND;
1518
1519 sta->dyn_airtime_weight = weight;
1520 airtime_policy_new_sta(hapd, sta);
1521
1522 return 0;
1523 }
1524 #endif
1525
1526
1527 static const struct ubus_method bss_methods[] = {
1528 UBUS_METHOD_NOARG("reload", hostapd_bss_reload),
1529 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
1530 UBUS_METHOD_NOARG("get_status", hostapd_bss_get_status),
1531 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
1532 #ifdef CONFIG_AIRTIME_POLICY
1533 UBUS_METHOD("update_airtime", hostapd_bss_update_airtime, airtime_policy),
1534 #endif
1535 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
1536 #ifdef CONFIG_WPS
1537 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
1538 UBUS_METHOD_NOARG("wps_status", hostapd_bss_wps_status),
1539 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
1540 #endif
1541 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
1542 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features),
1543 #ifdef NEED_AP_MLME
1544 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
1545 #endif
1546 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
1547 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
1548 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
1549 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
1550 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
1551 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
1552 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
1553 #ifdef CONFIG_WNM_AP
1554 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
1555 UBUS_METHOD("bss_transition_request", hostapd_bss_transition_request, bss_tr_policy),
1556 #endif
1557 };
1558
1559 static struct ubus_object_type bss_object_type =
1560 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
1561
1562 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
1563 {
1564 return memcmp(k1, k2, ETH_ALEN);
1565 }
1566
1567 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
1568 {
1569 struct ubus_object *obj = &hapd->ubus.obj;
1570 char *name;
1571 int ret;
1572
1573 #ifdef CONFIG_MESH
1574 if (hapd->conf->mesh & MESH_ENABLED)
1575 return;
1576 #endif
1577
1578 if (!hostapd_ubus_init())
1579 return;
1580
1581 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
1582 return;
1583
1584 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
1585 obj->name = name;
1586 obj->type = &bss_object_type;
1587 obj->methods = bss_object_type.methods;
1588 obj->n_methods = bss_object_type.n_methods;
1589 ret = ubus_add_object(ctx, obj);
1590 hostapd_ubus_ref_inc();
1591
1592 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "add");
1593 }
1594
1595 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
1596 {
1597 struct ubus_object *obj = &hapd->ubus.obj;
1598 char *name = (char *) obj->name;
1599
1600 #ifdef CONFIG_MESH
1601 if (hapd->conf->mesh & MESH_ENABLED)
1602 return;
1603 #endif
1604
1605 if (!ctx)
1606 return;
1607
1608 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "remove");
1609
1610 if (obj->id) {
1611 ubus_remove_object(ctx, obj);
1612 hostapd_ubus_ref_dec();
1613 }
1614
1615 free(name);
1616 }
1617
1618 static void
1619 hostapd_ubus_vlan_action(struct hostapd_data *hapd, struct hostapd_vlan *vlan,
1620 const char *action)
1621 {
1622 struct vlan_description *desc = &vlan->vlan_desc;
1623 void *c;
1624 int i;
1625
1626 if (!hapd->ubus.obj.has_subscribers)
1627 return;
1628
1629 blob_buf_init(&b, 0);
1630 blobmsg_add_string(&b, "ifname", vlan->ifname);
1631 blobmsg_add_string(&b, "bridge", vlan->bridge);
1632 blobmsg_add_u32(&b, "vlan_id", vlan->vlan_id);
1633
1634 if (desc->notempty) {
1635 blobmsg_add_u32(&b, "untagged", desc->untagged);
1636 c = blobmsg_open_array(&b, "tagged");
1637 for (i = 0; i < ARRAY_SIZE(desc->tagged) && desc->tagged[i]; i++)
1638 blobmsg_add_u32(&b, "", desc->tagged[i]);
1639 blobmsg_close_array(&b, c);
1640 }
1641
1642 ubus_notify(ctx, &hapd->ubus.obj, action, b.head, -1);
1643 }
1644
1645 void hostapd_ubus_add_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
1646 {
1647 hostapd_ubus_vlan_action(hapd, vlan, "vlan_add");
1648 }
1649
1650 void hostapd_ubus_remove_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
1651 {
1652 hostapd_ubus_vlan_action(hapd, vlan, "vlan_remove");
1653 }
1654
1655 static const struct ubus_method daemon_methods[] = {
1656 UBUS_METHOD("config_add", hostapd_config_add, config_add_policy),
1657 UBUS_METHOD("config_remove", hostapd_config_remove, config_remove_policy),
1658 };
1659
1660 static struct ubus_object_type daemon_object_type =
1661 UBUS_OBJECT_TYPE("hostapd", daemon_methods);
1662
1663 void hostapd_ubus_add(struct hapd_interfaces *interfaces)
1664 {
1665 struct ubus_object *obj = &interfaces->ubus;
1666 int ret;
1667
1668 if (!hostapd_ubus_init())
1669 return;
1670
1671 obj->name = strdup("hostapd");
1672
1673 obj->type = &daemon_object_type;
1674 obj->methods = daemon_object_type.methods;
1675 obj->n_methods = daemon_object_type.n_methods;
1676 ret = ubus_add_object(ctx, obj);
1677 hostapd_ubus_ref_inc();
1678 }
1679
1680 void hostapd_ubus_free(struct hapd_interfaces *interfaces)
1681 {
1682 struct ubus_object *obj = &interfaces->ubus;
1683 char *name = (char *) obj->name;
1684
1685 if (!ctx)
1686 return;
1687
1688 if (obj->id) {
1689 ubus_remove_object(ctx, obj);
1690 hostapd_ubus_ref_dec();
1691 }
1692
1693 free(name);
1694 }
1695
1696 struct ubus_event_req {
1697 struct ubus_notify_request nreq;
1698 int resp;
1699 };
1700
1701 static void
1702 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1703 {
1704 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1705
1706 ureq->resp = ret;
1707 }
1708
1709 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1710 {
1711 struct ubus_banned_client *ban;
1712 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1713 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1714 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1715 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1716 };
1717 const char *type = "mgmt";
1718 struct ubus_event_req ureq = {};
1719 const u8 *addr;
1720
1721 if (req->mgmt_frame)
1722 addr = req->mgmt_frame->sa;
1723 else
1724 addr = req->addr;
1725
1726 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1727 if (ban)
1728 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1729
1730 if (!hapd->ubus.obj.has_subscribers)
1731 return WLAN_STATUS_SUCCESS;
1732
1733 if (req->type < ARRAY_SIZE(types))
1734 type = types[req->type];
1735
1736 blob_buf_init(&b, 0);
1737 blobmsg_add_macaddr(&b, "address", addr);
1738 if (req->mgmt_frame)
1739 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1740 if (req->ssi_signal)
1741 blobmsg_add_u32(&b, "signal", req->ssi_signal);
1742 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1743
1744 if (req->elems) {
1745 if(req->elems->ht_capabilities)
1746 {
1747 struct ieee80211_ht_capabilities *ht_capabilities;
1748 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1749
1750
1751 ht_capabilities = (struct ieee80211_ht_capabilities*) req->elems->ht_capabilities;
1752 ht_cap = blobmsg_open_table(&b, "ht_capabilities");
1753 blobmsg_add_u16(&b, "ht_capabilities_info", ht_capabilities->ht_capabilities_info);
1754 ht_cap_mcs_set = blobmsg_open_table(&b, "supported_mcs_set");
1755 blobmsg_add_u16(&b, "a_mpdu_params", ht_capabilities->a_mpdu_params);
1756 blobmsg_add_u16(&b, "ht_extended_capabilities", ht_capabilities->ht_extended_capabilities);
1757 blobmsg_add_u32(&b, "tx_bf_capability_info", ht_capabilities->tx_bf_capability_info);
1758 blobmsg_add_u16(&b, "asel_capabilities", ht_capabilities->asel_capabilities);
1759 mcs_set = blobmsg_open_array(&b, "supported_mcs_set");
1760 for (int i = 0; i < 16; i++) {
1761 blobmsg_add_u16(&b, NULL, (u16) ht_capabilities->supported_mcs_set[i]);
1762 }
1763 blobmsg_close_array(&b, mcs_set);
1764 blobmsg_close_table(&b, ht_cap_mcs_set);
1765 blobmsg_close_table(&b, ht_cap);
1766 }
1767 if(req->elems->vht_capabilities)
1768 {
1769 struct ieee80211_vht_capabilities *vht_capabilities;
1770 void *vht_cap, *vht_cap_mcs_set;
1771
1772 vht_capabilities = (struct ieee80211_vht_capabilities*) req->elems->vht_capabilities;
1773 vht_cap = blobmsg_open_table(&b, "vht_capabilities");
1774 blobmsg_add_u32(&b, "vht_capabilities_info", vht_capabilities->vht_capabilities_info);
1775 vht_cap_mcs_set = blobmsg_open_table(&b, "vht_supported_mcs_set");
1776 blobmsg_add_u16(&b, "rx_map", vht_capabilities->vht_supported_mcs_set.rx_map);
1777 blobmsg_add_u16(&b, "rx_highest", vht_capabilities->vht_supported_mcs_set.rx_highest);
1778 blobmsg_add_u16(&b, "tx_map", vht_capabilities->vht_supported_mcs_set.tx_map);
1779 blobmsg_add_u16(&b, "tx_highest", vht_capabilities->vht_supported_mcs_set.tx_highest);
1780 blobmsg_close_table(&b, vht_cap_mcs_set);
1781 blobmsg_close_table(&b, vht_cap);
1782 }
1783 }
1784
1785 if (!hapd->ubus.notify_response) {
1786 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1787 return WLAN_STATUS_SUCCESS;
1788 }
1789
1790 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1791 return WLAN_STATUS_SUCCESS;
1792
1793 ureq.nreq.status_cb = ubus_event_cb;
1794 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1795
1796 if (ureq.resp)
1797 return ureq.resp;
1798
1799 return WLAN_STATUS_SUCCESS;
1800 }
1801
1802 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1803 {
1804 if (!hapd->ubus.obj.has_subscribers)
1805 return;
1806
1807 if (!addr)
1808 return;
1809
1810 blob_buf_init(&b, 0);
1811 blobmsg_add_macaddr(&b, "address", addr);
1812
1813 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1814 }
1815
1816 void hostapd_ubus_notify_beacon_report(
1817 struct hostapd_data *hapd, const u8 *addr, u8 token, u8 rep_mode,
1818 struct rrm_measurement_beacon_report *rep, size_t len)
1819 {
1820 if (!hapd->ubus.obj.has_subscribers)
1821 return;
1822
1823 if (!addr || !rep)
1824 return;
1825
1826 blob_buf_init(&b, 0);
1827 blobmsg_add_macaddr(&b, "address", addr);
1828 blobmsg_add_u16(&b, "op-class", rep->op_class);
1829 blobmsg_add_u16(&b, "channel", rep->channel);
1830 blobmsg_add_u64(&b, "start-time", rep->start_time);
1831 blobmsg_add_u16(&b, "duration", rep->duration);
1832 blobmsg_add_u16(&b, "report-info", rep->report_info);
1833 blobmsg_add_u16(&b, "rcpi", rep->rcpi);
1834 blobmsg_add_u16(&b, "rsni", rep->rsni);
1835 blobmsg_add_macaddr(&b, "bssid", rep->bssid);
1836 blobmsg_add_u16(&b, "antenna-id", rep->antenna_id);
1837 blobmsg_add_u16(&b, "parent-tsf", rep->parent_tsf);
1838
1839 ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
1840 }
1841
1842 void hostapd_ubus_notify_radar_detected(struct hostapd_iface *iface, int frequency,
1843 int chan_width, int cf1, int cf2)
1844 {
1845 struct hostapd_data *hapd;
1846 int i;
1847
1848 blob_buf_init(&b, 0);
1849 blobmsg_add_u16(&b, "frequency", frequency);
1850 blobmsg_add_u16(&b, "width", chan_width);
1851 blobmsg_add_u16(&b, "center1", cf1);
1852 blobmsg_add_u16(&b, "center2", cf2);
1853
1854 for (i = 0; i < iface->num_bss; i++) {
1855 hapd = iface->bss[i];
1856 ubus_notify(ctx, &hapd->ubus.obj, "radar-detected", b.head, -1);
1857 }
1858 }
1859
1860 #ifdef CONFIG_WNM_AP
1861 static void hostapd_ubus_notify_bss_transition_add_candidate_list(
1862 const u8 *candidate_list, u16 candidate_list_len)
1863 {
1864 char *cl_str;
1865 int i;
1866
1867 if (candidate_list_len == 0)
1868 return;
1869
1870 cl_str = blobmsg_alloc_string_buffer(&b, "candidate-list", candidate_list_len * 2 + 1);
1871 for (i = 0; i < candidate_list_len; i++)
1872 snprintf(&cl_str[i*2], 3, "%02X", candidate_list[i]);
1873 blobmsg_add_string_buffer(&b);
1874
1875 }
1876 #endif
1877
1878 void hostapd_ubus_notify_bss_transition_response(
1879 struct hostapd_data *hapd, const u8 *addr, u8 dialog_token, u8 status_code,
1880 u8 bss_termination_delay, const u8 *target_bssid,
1881 const u8 *candidate_list, u16 candidate_list_len)
1882 {
1883 #ifdef CONFIG_WNM_AP
1884 u16 i;
1885
1886 if (!hapd->ubus.obj.has_subscribers)
1887 return;
1888
1889 if (!addr)
1890 return;
1891
1892 blob_buf_init(&b, 0);
1893 blobmsg_add_macaddr(&b, "address", addr);
1894 blobmsg_add_u8(&b, "dialog-token", dialog_token);
1895 blobmsg_add_u8(&b, "status-code", status_code);
1896 blobmsg_add_u8(&b, "bss-termination-delay", bss_termination_delay);
1897 if (target_bssid)
1898 blobmsg_add_macaddr(&b, "target-bssid", target_bssid);
1899
1900 hostapd_ubus_notify_bss_transition_add_candidate_list(candidate_list, candidate_list_len);
1901
1902 ubus_notify(ctx, &hapd->ubus.obj, "bss-transition-response", b.head, -1);
1903 #endif
1904 }
1905
1906 int hostapd_ubus_notify_bss_transition_query(
1907 struct hostapd_data *hapd, const u8 *addr, u8 dialog_token, u8 reason,
1908 const u8 *candidate_list, u16 candidate_list_len)
1909 {
1910 #ifdef CONFIG_WNM_AP
1911 struct ubus_event_req ureq = {};
1912 char *cl_str;
1913 u16 i;
1914
1915 if (!hapd->ubus.obj.has_subscribers)
1916 return 0;
1917
1918 if (!addr)
1919 return 0;
1920
1921 blob_buf_init(&b, 0);
1922 blobmsg_add_macaddr(&b, "address", addr);
1923 blobmsg_add_u8(&b, "dialog-token", dialog_token);
1924 blobmsg_add_u8(&b, "reason", reason);
1925 hostapd_ubus_notify_bss_transition_add_candidate_list(candidate_list, candidate_list_len);
1926
1927 if (!hapd->ubus.notify_response) {
1928 ubus_notify(ctx, &hapd->ubus.obj, "bss-transition-query", b.head, -1);
1929 return 0;
1930 }
1931
1932 if (ubus_notify_async(ctx, &hapd->ubus.obj, "bss-transition-query", b.head, &ureq.nreq))
1933 return 0;
1934
1935 ureq.nreq.status_cb = ubus_event_cb;
1936 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1937
1938 return ureq.resp;
1939 #endif
1940 }