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