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