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