hostapd: report bssid, ssid and channel over ubus
[openwrt/staging/mkresin.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 char mac_buf[20];
412 size_t ssid_len = SSID_MAX_LEN;
413
414 if (hapd->conf->ssid.ssid_len < SSID_MAX_LEN)
415 ssid_len = hapd->conf->ssid.ssid_len;
416
417 blob_buf_init(&b, 0);
418 blobmsg_add_string(&b, "status", hostapd_state_text(hapd->iface->state));
419 blobmsg_printf(&b, "bssid", MACSTR, MAC2STR(hapd->conf->bssid));
420
421 memset(ssid, 0, SSID_MAX_LEN + 1);
422 memcpy(ssid, hapd->conf->ssid.ssid, ssid_len);
423 blobmsg_add_string(&b, "ssid", ssid);
424
425 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
426 blobmsg_add_u32(&b, "channel", ieee80211_frequency_to_channel(hapd->iface->freq));
427
428 snprintf(phy_name, 17, "%s", hapd->iface->phy);
429 blobmsg_add_string(&b, "phy", phy_name);
430
431 /* Airtime */
432 airtime_table = blobmsg_open_table(&b, "airtime");
433 blobmsg_add_u64(&b, "time", hapd->iface->last_channel_time);
434 blobmsg_add_u64(&b, "time_busy", hapd->iface->last_channel_time_busy);
435 blobmsg_add_u16(&b, "utilization", hapd->iface->channel_utilization);
436 blobmsg_close_table(&b, airtime_table);
437
438 /* DFS */
439 dfs_table = blobmsg_open_table(&b, "dfs");
440 blobmsg_add_u32(&b, "cac_seconds", hapd->iface->dfs_cac_ms / 1000);
441 blobmsg_add_u8(&b, "cac_active", !!(hapd->iface->cac_started));
442 os_reltime_age(&hapd->iface->dfs_cac_start, &now);
443 blobmsg_add_u32(&b, "cac_seconds_left",
444 hapd->iface->cac_started ? hapd->iface->dfs_cac_ms / 1000 - now.sec : 0);
445 blobmsg_close_table(&b, dfs_table);
446
447 ubus_send_reply(ctx, req, b.head);
448
449 return 0;
450 }
451
452 enum {
453 NOTIFY_RESPONSE,
454 __NOTIFY_MAX
455 };
456
457 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
458 [NOTIFY_RESPONSE] = { "notify_response", BLOBMSG_TYPE_INT32 },
459 };
460
461 static int
462 hostapd_notify_response(struct ubus_context *ctx, struct ubus_object *obj,
463 struct ubus_request_data *req, const char *method,
464 struct blob_attr *msg)
465 {
466 struct blob_attr *tb[__NOTIFY_MAX];
467 struct hostapd_data *hapd = get_hapd_from_object(obj);
468 struct wpabuf *elems;
469 const char *pos;
470 size_t len;
471
472 blobmsg_parse(notify_policy, __NOTIFY_MAX, tb,
473 blob_data(msg), blob_len(msg));
474
475 if (!tb[NOTIFY_RESPONSE])
476 return UBUS_STATUS_INVALID_ARGUMENT;
477
478 hapd->ubus.notify_response = blobmsg_get_u32(tb[NOTIFY_RESPONSE]);
479
480 return UBUS_STATUS_OK;
481 }
482
483 enum {
484 DEL_CLIENT_ADDR,
485 DEL_CLIENT_REASON,
486 DEL_CLIENT_DEAUTH,
487 DEL_CLIENT_BAN_TIME,
488 __DEL_CLIENT_MAX
489 };
490
491 static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
492 [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
493 [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
494 [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
495 [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
496 };
497
498 static int
499 hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
500 struct ubus_request_data *req, const char *method,
501 struct blob_attr *msg)
502 {
503 struct blob_attr *tb[__DEL_CLIENT_MAX];
504 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
505 struct sta_info *sta;
506 bool deauth = false;
507 int reason;
508 u8 addr[ETH_ALEN];
509
510 blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
511
512 if (!tb[DEL_CLIENT_ADDR])
513 return UBUS_STATUS_INVALID_ARGUMENT;
514
515 if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
516 return UBUS_STATUS_INVALID_ARGUMENT;
517
518 if (tb[DEL_CLIENT_REASON])
519 reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
520
521 if (tb[DEL_CLIENT_DEAUTH])
522 deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
523
524 sta = ap_get_sta(hapd, addr);
525 if (sta) {
526 if (deauth) {
527 hostapd_drv_sta_deauth(hapd, addr, reason);
528 ap_sta_deauthenticate(hapd, sta, reason);
529 } else {
530 hostapd_drv_sta_disassoc(hapd, addr, reason);
531 ap_sta_disassociate(hapd, sta, reason);
532 }
533 }
534
535 if (tb[DEL_CLIENT_BAN_TIME])
536 hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
537
538 return 0;
539 }
540
541 static void
542 blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
543 {
544 char *s;
545
546 s = blobmsg_alloc_string_buffer(buf, name, 20);
547 sprintf(s, MACSTR, MAC2STR(addr));
548 blobmsg_add_string_buffer(buf);
549 }
550
551 static int
552 hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
553 struct ubus_request_data *req, const char *method,
554 struct blob_attr *msg)
555 {
556 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
557 struct ubus_banned_client *ban;
558 void *c;
559
560 blob_buf_init(&b, 0);
561 c = blobmsg_open_array(&b, "clients");
562 avl_for_each_element(&hapd->ubus.banned, ban, avl)
563 blobmsg_add_macaddr(&b, NULL, ban->addr);
564 blobmsg_close_array(&b, c);
565 ubus_send_reply(ctx, req, b.head);
566
567 return 0;
568 }
569
570 #ifdef CONFIG_WPS
571 static int
572 hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
573 struct ubus_request_data *req, const char *method,
574 struct blob_attr *msg)
575 {
576 int rc;
577 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
578
579 rc = hostapd_wps_button_pushed(hapd, NULL);
580
581 if (rc != 0)
582 return UBUS_STATUS_NOT_SUPPORTED;
583
584 return 0;
585 }
586
587
588 static const char * pbc_status_enum_str(enum pbc_status status)
589 {
590 switch (status) {
591 case WPS_PBC_STATUS_DISABLE:
592 return "Disabled";
593 case WPS_PBC_STATUS_ACTIVE:
594 return "Active";
595 case WPS_PBC_STATUS_TIMEOUT:
596 return "Timed-out";
597 case WPS_PBC_STATUS_OVERLAP:
598 return "Overlap";
599 default:
600 return "Unknown";
601 }
602 }
603
604 static int
605 hostapd_bss_wps_status(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 blob_buf_init(&b, 0);
613
614 blobmsg_add_string(&b, "pbc_status", pbc_status_enum_str(hapd->wps_stats.pbc_status));
615 blobmsg_add_string(&b, "last_wps_result",
616 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
617 "Success":
618 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
619 "Failed" : "None")));
620
621 /* If status == Failure - Add possible Reasons */
622 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
623 hapd->wps_stats.failure_reason > 0)
624 blobmsg_add_string(&b, "reason", wps_ei_str(hapd->wps_stats.failure_reason));
625
626 if (hapd->wps_stats.status)
627 blobmsg_printf(&b, "peer_address", MACSTR, MAC2STR(hapd->wps_stats.peer_addr));
628
629 ubus_send_reply(ctx, req, b.head);
630
631 return 0;
632 }
633
634 static int
635 hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
636 struct ubus_request_data *req, const char *method,
637 struct blob_attr *msg)
638 {
639 int rc;
640 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
641
642 rc = hostapd_wps_cancel(hapd);
643
644 if (rc != 0)
645 return UBUS_STATUS_NOT_SUPPORTED;
646
647 return 0;
648 }
649 #endif /* CONFIG_WPS */
650
651 static int
652 hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
653 struct ubus_request_data *req, const char *method,
654 struct blob_attr *msg)
655 {
656 int rc;
657 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
658
659 rc = ieee802_11_set_beacon(hapd);
660
661 if (rc != 0)
662 return UBUS_STATUS_NOT_SUPPORTED;
663
664 return 0;
665 }
666
667 enum {
668 CONFIG_IFACE,
669 CONFIG_FILE,
670 __CONFIG_MAX
671 };
672
673 static const struct blobmsg_policy config_add_policy[__CONFIG_MAX] = {
674 [CONFIG_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
675 [CONFIG_FILE] = { "config", BLOBMSG_TYPE_STRING },
676 };
677
678 static int
679 hostapd_config_add(struct ubus_context *ctx, struct ubus_object *obj,
680 struct ubus_request_data *req, const char *method,
681 struct blob_attr *msg)
682 {
683 struct blob_attr *tb[__CONFIG_MAX];
684 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
685 char buf[128];
686
687 blobmsg_parse(config_add_policy, __CONFIG_MAX, tb, blob_data(msg), blob_len(msg));
688
689 if (!tb[CONFIG_FILE] || !tb[CONFIG_IFACE])
690 return UBUS_STATUS_INVALID_ARGUMENT;
691
692 snprintf(buf, sizeof(buf), "bss_config=%s:%s",
693 blobmsg_get_string(tb[CONFIG_IFACE]),
694 blobmsg_get_string(tb[CONFIG_FILE]));
695
696 if (hostapd_add_iface(interfaces, buf))
697 return UBUS_STATUS_INVALID_ARGUMENT;
698
699 blob_buf_init(&b, 0);
700 blobmsg_add_u32(&b, "pid", getpid());
701 ubus_send_reply(ctx, req, b.head);
702
703 return UBUS_STATUS_OK;
704 }
705
706 enum {
707 CONFIG_REM_IFACE,
708 __CONFIG_REM_MAX
709 };
710
711 static const struct blobmsg_policy config_remove_policy[__CONFIG_REM_MAX] = {
712 [CONFIG_REM_IFACE] = { "iface", BLOBMSG_TYPE_STRING },
713 };
714
715 static int
716 hostapd_config_remove(struct ubus_context *ctx, struct ubus_object *obj,
717 struct ubus_request_data *req, const char *method,
718 struct blob_attr *msg)
719 {
720 struct blob_attr *tb[__CONFIG_REM_MAX];
721 struct hapd_interfaces *interfaces = get_hapd_interfaces_from_object(obj);
722 char buf[128];
723
724 blobmsg_parse(config_remove_policy, __CONFIG_REM_MAX, tb, blob_data(msg), blob_len(msg));
725
726 if (!tb[CONFIG_REM_IFACE])
727 return UBUS_STATUS_INVALID_ARGUMENT;
728
729 if (hostapd_remove_iface(interfaces, blobmsg_get_string(tb[CONFIG_REM_IFACE])))
730 return UBUS_STATUS_INVALID_ARGUMENT;
731
732 return UBUS_STATUS_OK;
733 }
734
735 enum {
736 CSA_FREQ,
737 CSA_BCN_COUNT,
738 CSA_CENTER_FREQ1,
739 CSA_CENTER_FREQ2,
740 CSA_BANDWIDTH,
741 CSA_SEC_CHANNEL_OFFSET,
742 CSA_HT,
743 CSA_VHT,
744 CSA_BLOCK_TX,
745 __CSA_MAX
746 };
747
748 static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
749 [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
750 [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
751 [CSA_CENTER_FREQ1] = { "center_freq1", BLOBMSG_TYPE_INT32 },
752 [CSA_CENTER_FREQ2] = { "center_freq2", BLOBMSG_TYPE_INT32 },
753 [CSA_BANDWIDTH] = { "bandwidth", BLOBMSG_TYPE_INT32 },
754 [CSA_SEC_CHANNEL_OFFSET] = { "sec_channel_offset", BLOBMSG_TYPE_INT32 },
755 [CSA_HT] = { "ht", BLOBMSG_TYPE_BOOL },
756 [CSA_VHT] = { "vht", BLOBMSG_TYPE_BOOL },
757 [CSA_BLOCK_TX] = { "block_tx", BLOBMSG_TYPE_BOOL },
758 };
759
760 #ifdef NEED_AP_MLME
761 static int
762 hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
763 struct ubus_request_data *req, const char *method,
764 struct blob_attr *msg)
765 {
766 struct blob_attr *tb[__CSA_MAX];
767 struct hostapd_data *hapd = get_hapd_from_object(obj);
768 struct csa_settings css;
769 int ret = UBUS_STATUS_OK;
770 int i;
771
772 blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
773
774 if (!tb[CSA_FREQ])
775 return UBUS_STATUS_INVALID_ARGUMENT;
776
777 memset(&css, 0, sizeof(css));
778 css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
779
780 #define SET_CSA_SETTING(name, field, type) \
781 do { \
782 if (tb[name]) \
783 css.field = blobmsg_get_ ## type(tb[name]); \
784 } while(0)
785
786 SET_CSA_SETTING(CSA_BCN_COUNT, cs_count, u32);
787 SET_CSA_SETTING(CSA_CENTER_FREQ1, freq_params.center_freq1, u32);
788 SET_CSA_SETTING(CSA_CENTER_FREQ2, freq_params.center_freq2, u32);
789 SET_CSA_SETTING(CSA_BANDWIDTH, freq_params.bandwidth, u32);
790 SET_CSA_SETTING(CSA_SEC_CHANNEL_OFFSET, freq_params.sec_channel_offset, u32);
791 SET_CSA_SETTING(CSA_HT, freq_params.ht_enabled, bool);
792 SET_CSA_SETTING(CSA_VHT, freq_params.vht_enabled, bool);
793 SET_CSA_SETTING(CSA_BLOCK_TX, block_tx, bool);
794
795 for (i = 0; i < hapd->iface->num_bss; i++) {
796 struct hostapd_data *bss = hapd->iface->bss[i];
797
798 if (hostapd_switch_channel(bss, &css) != 0)
799 ret = UBUS_STATUS_NOT_SUPPORTED;
800 }
801
802 return ret;
803 #undef SET_CSA_SETTING
804 }
805 #endif
806
807 enum {
808 VENDOR_ELEMENTS,
809 __VENDOR_ELEMENTS_MAX
810 };
811
812 static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
813 /* vendor elements are provided as hex-string */
814 [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
815 };
816
817 static int
818 hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
819 struct ubus_request_data *req, const char *method,
820 struct blob_attr *msg)
821 {
822 struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
823 struct hostapd_data *hapd = get_hapd_from_object(obj);
824 struct hostapd_bss_config *bss = hapd->conf;
825 struct wpabuf *elems;
826 const char *pos;
827 size_t len;
828
829 blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
830 blob_data(msg), blob_len(msg));
831
832 if (!tb[VENDOR_ELEMENTS])
833 return UBUS_STATUS_INVALID_ARGUMENT;
834
835 pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
836 len = os_strlen(pos);
837 if (len & 0x01)
838 return UBUS_STATUS_INVALID_ARGUMENT;
839
840 len /= 2;
841 if (len == 0) {
842 wpabuf_free(bss->vendor_elements);
843 bss->vendor_elements = NULL;
844 return 0;
845 }
846
847 elems = wpabuf_alloc(len);
848 if (elems == NULL)
849 return 1;
850
851 if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
852 wpabuf_free(elems);
853 return UBUS_STATUS_INVALID_ARGUMENT;
854 }
855
856 wpabuf_free(bss->vendor_elements);
857 bss->vendor_elements = elems;
858
859 /* update beacons if vendor elements were set successfully */
860 if (ieee802_11_update_beacons(hapd->iface) != 0)
861 return UBUS_STATUS_NOT_SUPPORTED;
862 return UBUS_STATUS_OK;
863 }
864
865 static void
866 hostapd_rrm_print_nr(struct hostapd_neighbor_entry *nr)
867 {
868 const u8 *data;
869 char *str;
870 int len;
871
872 blobmsg_printf(&b, "", MACSTR, MAC2STR(nr->bssid));
873
874 str = blobmsg_alloc_string_buffer(&b, "", nr->ssid.ssid_len + 1);
875 memcpy(str, nr->ssid.ssid, nr->ssid.ssid_len);
876 str[nr->ssid.ssid_len] = 0;
877 blobmsg_add_string_buffer(&b);
878
879 len = wpabuf_len(nr->nr);
880 str = blobmsg_alloc_string_buffer(&b, "", 2 * len + 1);
881 wpa_snprintf_hex(str, 2 * len + 1, wpabuf_head_u8(nr->nr), len);
882 blobmsg_add_string_buffer(&b);
883 }
884
885 enum {
886 BSS_MGMT_EN_NEIGHBOR,
887 BSS_MGMT_EN_BEACON,
888 #ifdef CONFIG_WNM_AP
889 BSS_MGMT_EN_BSS_TRANSITION,
890 #endif
891 __BSS_MGMT_EN_MAX
892 };
893
894 static bool
895 __hostapd_bss_mgmt_enable_f(struct hostapd_data *hapd, int flag)
896 {
897 struct hostapd_bss_config *bss = hapd->conf;
898 uint32_t flags;
899
900 switch (flag) {
901 case BSS_MGMT_EN_NEIGHBOR:
902 if (bss->radio_measurements[0] &
903 WLAN_RRM_CAPS_NEIGHBOR_REPORT)
904 return false;
905
906 bss->radio_measurements[0] |=
907 WLAN_RRM_CAPS_NEIGHBOR_REPORT;
908 hostapd_neighbor_set_own_report(hapd);
909 return true;
910 case BSS_MGMT_EN_BEACON:
911 flags = WLAN_RRM_CAPS_BEACON_REPORT_PASSIVE |
912 WLAN_RRM_CAPS_BEACON_REPORT_ACTIVE |
913 WLAN_RRM_CAPS_BEACON_REPORT_TABLE;
914
915 if (bss->radio_measurements[0] & flags == flags)
916 return false;
917
918 bss->radio_measurements[0] |= (u8) flags;
919 return true;
920 #ifdef CONFIG_WNM_AP
921 case BSS_MGMT_EN_BSS_TRANSITION:
922 if (bss->bss_transition)
923 return false;
924
925 bss->bss_transition = 1;
926 return true;
927 #endif
928 }
929 }
930
931 static void
932 __hostapd_bss_mgmt_enable(struct hostapd_data *hapd, uint32_t flags)
933 {
934 bool update = false;
935 int i;
936
937 for (i = 0; i < __BSS_MGMT_EN_MAX; i++) {
938 if (!(flags & (1 << i)))
939 continue;
940
941 update |= __hostapd_bss_mgmt_enable_f(hapd, i);
942 }
943
944 if (update)
945 ieee802_11_update_beacons(hapd->iface);
946 }
947
948
949 static const struct blobmsg_policy bss_mgmt_enable_policy[__BSS_MGMT_EN_MAX] = {
950 [BSS_MGMT_EN_NEIGHBOR] = { "neighbor_report", BLOBMSG_TYPE_BOOL },
951 [BSS_MGMT_EN_BEACON] = { "beacon_report", BLOBMSG_TYPE_BOOL },
952 #ifdef CONFIG_WNM_AP
953 [BSS_MGMT_EN_BSS_TRANSITION] = { "bss_transition", BLOBMSG_TYPE_BOOL },
954 #endif
955 };
956
957 static int
958 hostapd_bss_mgmt_enable(struct ubus_context *ctx, struct ubus_object *obj,
959 struct ubus_request_data *req, const char *method,
960 struct blob_attr *msg)
961
962 {
963 struct hostapd_data *hapd = get_hapd_from_object(obj);
964 struct blob_attr *tb[__BSS_MGMT_EN_MAX];
965 struct blob_attr *cur;
966 uint32_t flags = 0;
967 int i;
968 bool neigh = false, beacon = false;
969
970 blobmsg_parse(bss_mgmt_enable_policy, __BSS_MGMT_EN_MAX, tb, blob_data(msg), blob_len(msg));
971
972 for (i = 0; i < ARRAY_SIZE(tb); i++) {
973 if (!tb[i] || !blobmsg_get_bool(tb[i]))
974 continue;
975
976 flags |= (1 << i);
977 }
978
979 __hostapd_bss_mgmt_enable(hapd, flags);
980 }
981
982
983 static void
984 hostapd_rrm_nr_enable(struct hostapd_data *hapd)
985 {
986 __hostapd_bss_mgmt_enable(hapd, 1 << BSS_MGMT_EN_NEIGHBOR);
987 }
988
989 static int
990 hostapd_rrm_nr_get_own(struct ubus_context *ctx, struct ubus_object *obj,
991 struct ubus_request_data *req, const char *method,
992 struct blob_attr *msg)
993 {
994 struct hostapd_data *hapd = get_hapd_from_object(obj);
995 struct hostapd_neighbor_entry *nr;
996 void *c;
997
998 hostapd_rrm_nr_enable(hapd);
999
1000 nr = hostapd_neighbor_get(hapd, hapd->own_addr, NULL);
1001 if (!nr)
1002 return UBUS_STATUS_NOT_FOUND;
1003
1004 blob_buf_init(&b, 0);
1005
1006 c = blobmsg_open_array(&b, "value");
1007 hostapd_rrm_print_nr(nr);
1008 blobmsg_close_array(&b, c);
1009
1010 ubus_send_reply(ctx, req, b.head);
1011
1012 return 0;
1013 }
1014
1015 static int
1016 hostapd_rrm_nr_list(struct ubus_context *ctx, struct ubus_object *obj,
1017 struct ubus_request_data *req, const char *method,
1018 struct blob_attr *msg)
1019 {
1020 struct hostapd_data *hapd = get_hapd_from_object(obj);
1021 struct hostapd_neighbor_entry *nr;
1022 void *c;
1023
1024 hostapd_rrm_nr_enable(hapd);
1025 blob_buf_init(&b, 0);
1026
1027 c = blobmsg_open_array(&b, "list");
1028 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
1029 void *cur;
1030
1031 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
1032 continue;
1033
1034 cur = blobmsg_open_array(&b, NULL);
1035 hostapd_rrm_print_nr(nr);
1036 blobmsg_close_array(&b, cur);
1037 }
1038 blobmsg_close_array(&b, c);
1039
1040 ubus_send_reply(ctx, req, b.head);
1041
1042 return 0;
1043 }
1044
1045 enum {
1046 NR_SET_LIST,
1047 __NR_SET_LIST_MAX
1048 };
1049
1050 static const struct blobmsg_policy nr_set_policy[__NR_SET_LIST_MAX] = {
1051 [NR_SET_LIST] = { "list", BLOBMSG_TYPE_ARRAY },
1052 };
1053
1054
1055 static void
1056 hostapd_rrm_nr_clear(struct hostapd_data *hapd)
1057 {
1058 struct hostapd_neighbor_entry *nr;
1059
1060 restart:
1061 dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry, list) {
1062 if (!memcmp(nr->bssid, hapd->own_addr, ETH_ALEN))
1063 continue;
1064
1065 hostapd_neighbor_remove(hapd, nr->bssid, &nr->ssid);
1066 goto restart;
1067 }
1068 }
1069
1070 static int
1071 hostapd_rrm_nr_set(struct ubus_context *ctx, struct ubus_object *obj,
1072 struct ubus_request_data *req, const char *method,
1073 struct blob_attr *msg)
1074 {
1075 static const struct blobmsg_policy nr_e_policy[] = {
1076 { .type = BLOBMSG_TYPE_STRING },
1077 { .type = BLOBMSG_TYPE_STRING },
1078 { .type = BLOBMSG_TYPE_STRING },
1079 };
1080 struct hostapd_data *hapd = get_hapd_from_object(obj);
1081 struct blob_attr *tb_l[__NR_SET_LIST_MAX];
1082 struct blob_attr *tb[ARRAY_SIZE(nr_e_policy)];
1083 struct blob_attr *cur;
1084 int rem;
1085
1086 hostapd_rrm_nr_enable(hapd);
1087
1088 blobmsg_parse(nr_set_policy, __NR_SET_LIST_MAX, tb_l, blob_data(msg), blob_len(msg));
1089 if (!tb_l[NR_SET_LIST])
1090 return UBUS_STATUS_INVALID_ARGUMENT;
1091
1092 hostapd_rrm_nr_clear(hapd);
1093 blobmsg_for_each_attr(cur, tb_l[NR_SET_LIST], rem) {
1094 struct wpa_ssid_value ssid;
1095 struct wpabuf *data;
1096 u8 bssid[ETH_ALEN];
1097 char *s, *nr_s;
1098
1099 blobmsg_parse_array(nr_e_policy, ARRAY_SIZE(nr_e_policy), tb, blobmsg_data(cur), blobmsg_data_len(cur));
1100 if (!tb[0] || !tb[1] || !tb[2])
1101 goto invalid;
1102
1103 /* Neighbor Report binary */
1104 nr_s = blobmsg_get_string(tb[2]);
1105 data = wpabuf_parse_bin(nr_s);
1106 if (!data)
1107 goto invalid;
1108
1109 /* BSSID */
1110 s = blobmsg_get_string(tb[0]);
1111 if (strlen(s) == 0) {
1112 /* Copy BSSID from neighbor report */
1113 if (hwaddr_compact_aton(nr_s, bssid))
1114 goto invalid;
1115 } else if (hwaddr_aton(s, bssid)) {
1116 goto invalid;
1117 }
1118
1119 /* SSID */
1120 s = blobmsg_get_string(tb[1]);
1121 if (strlen(s) == 0) {
1122 /* Copy SSID from hostapd BSS conf */
1123 memcpy(&ssid, &hapd->conf->ssid, sizeof(ssid));
1124 } else {
1125 ssid.ssid_len = strlen(s);
1126 if (ssid.ssid_len > sizeof(ssid.ssid))
1127 goto invalid;
1128
1129 memcpy(&ssid, s, ssid.ssid_len);
1130 }
1131
1132 hostapd_neighbor_set(hapd, bssid, &ssid, data, NULL, NULL, 0);
1133 wpabuf_free(data);
1134 continue;
1135
1136 invalid:
1137 return UBUS_STATUS_INVALID_ARGUMENT;
1138 }
1139
1140 return 0;
1141 }
1142
1143 enum {
1144 BEACON_REQ_ADDR,
1145 BEACON_REQ_MODE,
1146 BEACON_REQ_OP_CLASS,
1147 BEACON_REQ_CHANNEL,
1148 BEACON_REQ_DURATION,
1149 BEACON_REQ_BSSID,
1150 BEACON_REQ_SSID,
1151 __BEACON_REQ_MAX,
1152 };
1153
1154 static const struct blobmsg_policy beacon_req_policy[__BEACON_REQ_MAX] = {
1155 [BEACON_REQ_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1156 [BEACON_REQ_OP_CLASS] { "op_class", BLOBMSG_TYPE_INT32 },
1157 [BEACON_REQ_CHANNEL] { "channel", BLOBMSG_TYPE_INT32 },
1158 [BEACON_REQ_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1159 [BEACON_REQ_MODE] { "mode", BLOBMSG_TYPE_INT32 },
1160 [BEACON_REQ_BSSID] { "bssid", BLOBMSG_TYPE_STRING },
1161 [BEACON_REQ_SSID] { "ssid", BLOBMSG_TYPE_STRING },
1162 };
1163
1164 static int
1165 hostapd_rrm_beacon_req(struct ubus_context *ctx, struct ubus_object *obj,
1166 struct ubus_request_data *ureq, const char *method,
1167 struct blob_attr *msg)
1168 {
1169 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1170 struct blob_attr *tb[__BEACON_REQ_MAX];
1171 struct blob_attr *cur;
1172 struct wpabuf *req;
1173 u8 bssid[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1174 u8 addr[ETH_ALEN];
1175 int mode, rem, ret;
1176 int buf_len = 13;
1177
1178 blobmsg_parse(beacon_req_policy, __BEACON_REQ_MAX, tb, blob_data(msg), blob_len(msg));
1179
1180 if (!tb[BEACON_REQ_ADDR] || !tb[BEACON_REQ_MODE] || !tb[BEACON_REQ_DURATION] ||
1181 !tb[BEACON_REQ_OP_CLASS] || !tb[BEACON_REQ_CHANNEL])
1182 return UBUS_STATUS_INVALID_ARGUMENT;
1183
1184 if (tb[BEACON_REQ_SSID])
1185 buf_len += blobmsg_data_len(tb[BEACON_REQ_SSID]) + 2 - 1;
1186
1187 mode = blobmsg_get_u32(tb[BEACON_REQ_MODE]);
1188 if (hwaddr_aton(blobmsg_data(tb[BEACON_REQ_ADDR]), addr))
1189 return UBUS_STATUS_INVALID_ARGUMENT;
1190
1191 if (tb[BEACON_REQ_BSSID] &&
1192 hwaddr_aton(blobmsg_data(tb[BEACON_REQ_BSSID]), bssid))
1193 return UBUS_STATUS_INVALID_ARGUMENT;
1194
1195 req = wpabuf_alloc(buf_len);
1196 if (!req)
1197 return UBUS_STATUS_UNKNOWN_ERROR;
1198
1199 /* 1: regulatory class */
1200 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_OP_CLASS]));
1201
1202 /* 2: channel number */
1203 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_CHANNEL]));
1204
1205 /* 3-4: randomization interval */
1206 wpabuf_put_le16(req, 0);
1207
1208 /* 5-6: duration */
1209 wpabuf_put_le16(req, blobmsg_get_u32(tb[BEACON_REQ_DURATION]));
1210
1211 /* 7: mode */
1212 wpabuf_put_u8(req, blobmsg_get_u32(tb[BEACON_REQ_MODE]));
1213
1214 /* 8-13: BSSID */
1215 wpabuf_put_data(req, bssid, ETH_ALEN);
1216
1217 if ((cur = tb[BEACON_REQ_SSID]) != NULL) {
1218 wpabuf_put_u8(req, WLAN_EID_SSID);
1219 wpabuf_put_u8(req, blobmsg_data_len(cur) - 1);
1220 wpabuf_put_data(req, blobmsg_data(cur), blobmsg_data_len(cur) - 1);
1221 }
1222
1223 ret = hostapd_send_beacon_req(hapd, addr, 0, req);
1224 if (ret < 0)
1225 return -ret;
1226
1227 return 0;
1228 }
1229
1230
1231 #ifdef CONFIG_WNM_AP
1232 enum {
1233 WNM_DISASSOC_ADDR,
1234 WNM_DISASSOC_DURATION,
1235 WNM_DISASSOC_NEIGHBORS,
1236 WNM_DISASSOC_ABRIDGED,
1237 __WNM_DISASSOC_MAX,
1238 };
1239
1240 static const struct blobmsg_policy wnm_disassoc_policy[__WNM_DISASSOC_MAX] = {
1241 [WNM_DISASSOC_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
1242 [WNM_DISASSOC_DURATION] { "duration", BLOBMSG_TYPE_INT32 },
1243 [WNM_DISASSOC_NEIGHBORS] { "neighbors", BLOBMSG_TYPE_ARRAY },
1244 [WNM_DISASSOC_ABRIDGED] { "abridged", BLOBMSG_TYPE_BOOL },
1245 };
1246
1247 static int
1248 hostapd_wnm_disassoc_imminent(struct ubus_context *ctx, struct ubus_object *obj,
1249 struct ubus_request_data *ureq, const char *method,
1250 struct blob_attr *msg)
1251 {
1252 struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
1253 struct blob_attr *tb[__WNM_DISASSOC_MAX];
1254 struct blob_attr *cur;
1255 struct sta_info *sta;
1256 int duration = 10;
1257 int rem;
1258 int nr_len = 0;
1259 u8 *nr = NULL;
1260 u8 req_mode = WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1261 u8 addr[ETH_ALEN];
1262
1263 blobmsg_parse(wnm_disassoc_policy, __WNM_DISASSOC_MAX, tb, blob_data(msg), blob_len(msg));
1264
1265 if (!tb[WNM_DISASSOC_ADDR])
1266 return UBUS_STATUS_INVALID_ARGUMENT;
1267
1268 if (hwaddr_aton(blobmsg_data(tb[WNM_DISASSOC_ADDR]), addr))
1269 return UBUS_STATUS_INVALID_ARGUMENT;
1270
1271 if ((cur = tb[WNM_DISASSOC_DURATION]) != NULL)
1272 duration = blobmsg_get_u32(cur);
1273
1274 sta = ap_get_sta(hapd, addr);
1275 if (!sta)
1276 return UBUS_STATUS_NOT_FOUND;
1277
1278 if (tb[WNM_DISASSOC_NEIGHBORS]) {
1279 u8 *nr_cur;
1280
1281 if (blobmsg_check_array(tb[WNM_DISASSOC_NEIGHBORS],
1282 BLOBMSG_TYPE_STRING) < 0)
1283 return UBUS_STATUS_INVALID_ARGUMENT;
1284
1285 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1286 int len = strlen(blobmsg_get_string(cur));
1287
1288 if (len % 2)
1289 return UBUS_STATUS_INVALID_ARGUMENT;
1290
1291 nr_len += (len / 2) + 2;
1292 }
1293
1294 if (nr_len) {
1295 nr = os_zalloc(nr_len);
1296 if (!nr)
1297 return UBUS_STATUS_UNKNOWN_ERROR;
1298 }
1299
1300 nr_cur = nr;
1301 blobmsg_for_each_attr(cur, tb[WNM_DISASSOC_NEIGHBORS], rem) {
1302 int len = strlen(blobmsg_get_string(cur)) / 2;
1303
1304 *nr_cur++ = WLAN_EID_NEIGHBOR_REPORT;
1305 *nr_cur++ = (u8) len;
1306 if (hexstr2bin(blobmsg_data(cur), nr_cur, len)) {
1307 free(nr);
1308 return UBUS_STATUS_INVALID_ARGUMENT;
1309 }
1310
1311 nr_cur += len;
1312 }
1313 }
1314
1315 if (nr)
1316 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1317
1318 if (tb[WNM_DISASSOC_ABRIDGED] && blobmsg_get_bool(tb[WNM_DISASSOC_ABRIDGED]))
1319 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1320
1321 if (wnm_send_bss_tm_req(hapd, sta, req_mode, duration, duration, NULL,
1322 NULL, nr, nr_len, NULL, 0))
1323 return UBUS_STATUS_UNKNOWN_ERROR;
1324
1325 return 0;
1326 }
1327 #endif
1328
1329 static const struct ubus_method bss_methods[] = {
1330 UBUS_METHOD_NOARG("reload", hostapd_bss_reload),
1331 UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
1332 UBUS_METHOD_NOARG("get_status", hostapd_bss_get_status),
1333 UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
1334 UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
1335 #ifdef CONFIG_WPS
1336 UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
1337 UBUS_METHOD_NOARG("wps_status", hostapd_bss_wps_status),
1338 UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
1339 #endif
1340 UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
1341 UBUS_METHOD_NOARG("get_features", hostapd_bss_get_features),
1342 #ifdef NEED_AP_MLME
1343 UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
1344 #endif
1345 UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
1346 UBUS_METHOD("notify_response", hostapd_notify_response, notify_policy),
1347 UBUS_METHOD("bss_mgmt_enable", hostapd_bss_mgmt_enable, bss_mgmt_enable_policy),
1348 UBUS_METHOD_NOARG("rrm_nr_get_own", hostapd_rrm_nr_get_own),
1349 UBUS_METHOD_NOARG("rrm_nr_list", hostapd_rrm_nr_list),
1350 UBUS_METHOD("rrm_nr_set", hostapd_rrm_nr_set, nr_set_policy),
1351 UBUS_METHOD("rrm_beacon_req", hostapd_rrm_beacon_req, beacon_req_policy),
1352 #ifdef CONFIG_WNM_AP
1353 UBUS_METHOD("wnm_disassoc_imminent", hostapd_wnm_disassoc_imminent, wnm_disassoc_policy),
1354 #endif
1355 };
1356
1357 static struct ubus_object_type bss_object_type =
1358 UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
1359
1360 static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
1361 {
1362 return memcmp(k1, k2, ETH_ALEN);
1363 }
1364
1365 void hostapd_ubus_add_bss(struct hostapd_data *hapd)
1366 {
1367 struct ubus_object *obj = &hapd->ubus.obj;
1368 char *name;
1369 int ret;
1370
1371 #ifdef CONFIG_MESH
1372 if (hapd->conf->mesh & MESH_ENABLED)
1373 return;
1374 #endif
1375
1376 if (!hostapd_ubus_init())
1377 return;
1378
1379 if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
1380 return;
1381
1382 avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
1383 obj->name = name;
1384 obj->type = &bss_object_type;
1385 obj->methods = bss_object_type.methods;
1386 obj->n_methods = bss_object_type.n_methods;
1387 ret = ubus_add_object(ctx, obj);
1388 hostapd_ubus_ref_inc();
1389
1390 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "add");
1391 }
1392
1393 void hostapd_ubus_free_bss(struct hostapd_data *hapd)
1394 {
1395 struct ubus_object *obj = &hapd->ubus.obj;
1396 char *name = (char *) obj->name;
1397
1398 if (!ctx)
1399 return;
1400
1401 hostapd_send_shared_event(&hapd->iface->interfaces->ubus, hapd->conf->iface, "remove");
1402
1403 if (obj->id) {
1404 ubus_remove_object(ctx, obj);
1405 hostapd_ubus_ref_dec();
1406 }
1407
1408 free(name);
1409 }
1410
1411 static void
1412 hostapd_ubus_vlan_action(struct hostapd_data *hapd, struct hostapd_vlan *vlan,
1413 const char *action)
1414 {
1415 struct vlan_description *desc = &vlan->vlan_desc;
1416 void *c;
1417 int i;
1418
1419 if (!hapd->ubus.obj.has_subscribers)
1420 return;
1421
1422 blob_buf_init(&b, 0);
1423 blobmsg_add_string(&b, "ifname", vlan->ifname);
1424 blobmsg_add_string(&b, "bridge", vlan->bridge);
1425 blobmsg_add_u32(&b, "vlan_id", vlan->vlan_id);
1426
1427 if (desc->notempty) {
1428 blobmsg_add_u32(&b, "untagged", desc->untagged);
1429 c = blobmsg_open_array(&b, "tagged");
1430 for (i = 0; i < ARRAY_SIZE(desc->tagged) && desc->tagged[i]; i++)
1431 blobmsg_add_u32(&b, "", desc->tagged[i]);
1432 blobmsg_close_array(&b, c);
1433 }
1434
1435 ubus_notify(ctx, &hapd->ubus.obj, action, b.head, -1);
1436 }
1437
1438 void hostapd_ubus_add_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
1439 {
1440 hostapd_ubus_vlan_action(hapd, vlan, "vlan_add");
1441 }
1442
1443 void hostapd_ubus_remove_vlan(struct hostapd_data *hapd, struct hostapd_vlan *vlan)
1444 {
1445 hostapd_ubus_vlan_action(hapd, vlan, "vlan_remove");
1446 }
1447
1448 static const struct ubus_method daemon_methods[] = {
1449 UBUS_METHOD("config_add", hostapd_config_add, config_add_policy),
1450 UBUS_METHOD("config_remove", hostapd_config_remove, config_remove_policy),
1451 };
1452
1453 static struct ubus_object_type daemon_object_type =
1454 UBUS_OBJECT_TYPE("hostapd", daemon_methods);
1455
1456 void hostapd_ubus_add(struct hapd_interfaces *interfaces)
1457 {
1458 struct ubus_object *obj = &interfaces->ubus;
1459 int ret;
1460
1461 if (!hostapd_ubus_init())
1462 return;
1463
1464 obj->name = strdup("hostapd");
1465
1466 obj->type = &daemon_object_type;
1467 obj->methods = daemon_object_type.methods;
1468 obj->n_methods = daemon_object_type.n_methods;
1469 ret = ubus_add_object(ctx, obj);
1470 hostapd_ubus_ref_inc();
1471 }
1472
1473 void hostapd_ubus_free(struct hapd_interfaces *interfaces)
1474 {
1475 struct ubus_object *obj = &interfaces->ubus;
1476 char *name = (char *) obj->name;
1477
1478 if (!ctx)
1479 return;
1480
1481 if (obj->id) {
1482 ubus_remove_object(ctx, obj);
1483 hostapd_ubus_ref_dec();
1484 }
1485
1486 free(name);
1487 }
1488
1489 struct ubus_event_req {
1490 struct ubus_notify_request nreq;
1491 int resp;
1492 };
1493
1494 static void
1495 ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
1496 {
1497 struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
1498
1499 ureq->resp = ret;
1500 }
1501
1502 int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
1503 {
1504 struct ubus_banned_client *ban;
1505 const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
1506 [HOSTAPD_UBUS_PROBE_REQ] = "probe",
1507 [HOSTAPD_UBUS_AUTH_REQ] = "auth",
1508 [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
1509 };
1510 const char *type = "mgmt";
1511 struct ubus_event_req ureq = {};
1512 const u8 *addr;
1513
1514 if (req->mgmt_frame)
1515 addr = req->mgmt_frame->sa;
1516 else
1517 addr = req->addr;
1518
1519 ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
1520 if (ban)
1521 return WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1522
1523 if (!hapd->ubus.obj.has_subscribers)
1524 return WLAN_STATUS_SUCCESS;
1525
1526 if (req->type < ARRAY_SIZE(types))
1527 type = types[req->type];
1528
1529 blob_buf_init(&b, 0);
1530 blobmsg_add_macaddr(&b, "address", addr);
1531 if (req->mgmt_frame)
1532 blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
1533 if (req->ssi_signal)
1534 blobmsg_add_u32(&b, "signal", req->ssi_signal);
1535 blobmsg_add_u32(&b, "freq", hapd->iface->freq);
1536
1537 if (req->elems) {
1538 if(req->elems->ht_capabilities)
1539 {
1540 struct ieee80211_ht_capabilities *ht_capabilities;
1541 void *ht_cap, *ht_cap_mcs_set, *mcs_set;
1542
1543
1544 ht_capabilities = (struct ieee80211_ht_capabilities*) req->elems->ht_capabilities;
1545 ht_cap = blobmsg_open_table(&b, "ht_capabilities");
1546 blobmsg_add_u16(&b, "ht_capabilities_info", ht_capabilities->ht_capabilities_info);
1547 ht_cap_mcs_set = blobmsg_open_table(&b, "supported_mcs_set");
1548 blobmsg_add_u16(&b, "a_mpdu_params", ht_capabilities->a_mpdu_params);
1549 blobmsg_add_u16(&b, "ht_extended_capabilities", ht_capabilities->ht_extended_capabilities);
1550 blobmsg_add_u32(&b, "tx_bf_capability_info", ht_capabilities->tx_bf_capability_info);
1551 blobmsg_add_u16(&b, "asel_capabilities", ht_capabilities->asel_capabilities);
1552 mcs_set = blobmsg_open_array(&b, "supported_mcs_set");
1553 for (int i = 0; i < 16; i++) {
1554 blobmsg_add_u16(&b, NULL, (u16) ht_capabilities->supported_mcs_set[i]);
1555 }
1556 blobmsg_close_array(&b, mcs_set);
1557 blobmsg_close_table(&b, ht_cap_mcs_set);
1558 blobmsg_close_table(&b, ht_cap);
1559 }
1560 if(req->elems->vht_capabilities)
1561 {
1562 struct ieee80211_vht_capabilities *vht_capabilities;
1563 void *vht_cap, *vht_cap_mcs_set;
1564
1565 vht_capabilities = (struct ieee80211_vht_capabilities*) req->elems->vht_capabilities;
1566 vht_cap = blobmsg_open_table(&b, "vht_capabilities");
1567 blobmsg_add_u32(&b, "vht_capabilities_info", vht_capabilities->vht_capabilities_info);
1568 vht_cap_mcs_set = blobmsg_open_table(&b, "vht_supported_mcs_set");
1569 blobmsg_add_u16(&b, "rx_map", vht_capabilities->vht_supported_mcs_set.rx_map);
1570 blobmsg_add_u16(&b, "rx_highest", vht_capabilities->vht_supported_mcs_set.rx_highest);
1571 blobmsg_add_u16(&b, "tx_map", vht_capabilities->vht_supported_mcs_set.tx_map);
1572 blobmsg_add_u16(&b, "tx_highest", vht_capabilities->vht_supported_mcs_set.tx_highest);
1573 blobmsg_close_table(&b, vht_cap_mcs_set);
1574 blobmsg_close_table(&b, vht_cap);
1575 }
1576 }
1577
1578 if (!hapd->ubus.notify_response) {
1579 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1580 return WLAN_STATUS_SUCCESS;
1581 }
1582
1583 if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
1584 return WLAN_STATUS_SUCCESS;
1585
1586 ureq.nreq.status_cb = ubus_event_cb;
1587 ubus_complete_request(ctx, &ureq.nreq.req, 100);
1588
1589 if (ureq.resp)
1590 return ureq.resp;
1591
1592 return WLAN_STATUS_SUCCESS;
1593 }
1594
1595 void hostapd_ubus_notify(struct hostapd_data *hapd, const char *type, const u8 *addr)
1596 {
1597 if (!hapd->ubus.obj.has_subscribers)
1598 return;
1599
1600 if (!addr)
1601 return;
1602
1603 blob_buf_init(&b, 0);
1604 blobmsg_add_macaddr(&b, "address", addr);
1605
1606 ubus_notify(ctx, &hapd->ubus.obj, type, b.head, -1);
1607 }
1608
1609 void hostapd_ubus_notify_beacon_report(
1610 struct hostapd_data *hapd, const u8 *addr, u8 token, u8 rep_mode,
1611 struct rrm_measurement_beacon_report *rep, size_t len)
1612 {
1613 if (!hapd->ubus.obj.has_subscribers)
1614 return;
1615
1616 if (!addr || !rep)
1617 return;
1618
1619 blob_buf_init(&b, 0);
1620 blobmsg_add_macaddr(&b, "address", addr);
1621 blobmsg_add_u16(&b, "op-class", rep->op_class);
1622 blobmsg_add_u16(&b, "channel", rep->channel);
1623 blobmsg_add_u64(&b, "start-time", rep->start_time);
1624 blobmsg_add_u16(&b, "duration", rep->duration);
1625 blobmsg_add_u16(&b, "report-info", rep->report_info);
1626 blobmsg_add_u16(&b, "rcpi", rep->rcpi);
1627 blobmsg_add_u16(&b, "rsni", rep->rsni);
1628 blobmsg_add_macaddr(&b, "bssid", rep->bssid);
1629 blobmsg_add_u16(&b, "antenna-id", rep->antenna_id);
1630 blobmsg_add_u16(&b, "parent-tsf", rep->parent_tsf);
1631
1632 ubus_notify(ctx, &hapd->ubus.obj, "beacon-report", b.head, -1);
1633 }
1634
1635 void hostapd_ubus_notify_radar_detected(struct hostapd_iface *iface, int frequency,
1636 int chan_width, int cf1, int cf2)
1637 {
1638 struct hostapd_data *hapd;
1639 int i;
1640
1641 if (!hapd->ubus.obj.has_subscribers)
1642 return;
1643
1644 blob_buf_init(&b, 0);
1645 blobmsg_add_u16(&b, "frequency", frequency);
1646 blobmsg_add_u16(&b, "width", chan_width);
1647 blobmsg_add_u16(&b, "center1", cf1);
1648 blobmsg_add_u16(&b, "center2", cf2);
1649
1650 for (i = 0; i < iface->num_bss; i++) {
1651 hapd = iface->bss[i];
1652 ubus_notify(ctx, &hapd->ubus.obj, "radar-detected", b.head, -1);
1653 }
1654 }