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