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