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