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