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