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