Merge pull request #580 from wigyori/cc-libpcap
[15.05/openwrt.git] / package / network / services / hostapd / patches / 600-ubus_support.patch
1 --- a/hostapd/Makefile
2 +++ b/hostapd/Makefile
3 @@ -157,6 +157,11 @@ OBJS += ../src/common/hw_features_common
4  
5  OBJS += ../src/eapol_auth/eapol_auth_sm.o
6  
7 +ifdef CONFIG_UBUS
8 +CFLAGS += -DUBUS_SUPPORT
9 +OBJS += ../src/ap/ubus.o
10 +LIBS += -lubox -lubus
11 +endif
12  
13  ifdef CONFIG_CODE_COVERAGE
14  CFLAGS += -O0 -fprofile-arcs -ftest-coverage
15 --- a/src/ap/hostapd.h
16 +++ b/src/ap/hostapd.h
17 @@ -13,6 +13,7 @@
18  #include "utils/list.h"
19  #include "ap_config.h"
20  #include "drivers/driver.h"
21 +#include "ubus.h"
22  
23  struct wpa_ctrl_dst;
24  struct radius_server_data;
25 @@ -118,6 +119,7 @@ struct hostapd_data {
26         struct hostapd_iface *iface;
27         struct hostapd_config *iconf;
28         struct hostapd_bss_config *conf;
29 +       struct hostapd_ubus_bss ubus;
30         int interface_added; /* virtual interface added for this BSS */
31         unsigned int started:1;
32         unsigned int disabled:1;
33 @@ -323,6 +325,8 @@ struct hostapd_iface {
34         struct hostapd_config *conf;
35         char phy[16]; /* Name of the PHY (radio) */
36  
37 +       struct hostapd_ubus_iface ubus;
38 +
39         enum hostapd_iface_state {
40                 HAPD_IFACE_UNINITIALIZED,
41                 HAPD_IFACE_DISABLED,
42 --- /dev/null
43 +++ b/src/ap/ubus.c
44 @@ -0,0 +1,536 @@
45 +/*
46 + * hostapd / ubus support
47 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
48 + *
49 + * This software may be distributed under the terms of the BSD license.
50 + * See README for more details.
51 + */
52 +
53 +#include "utils/includes.h"
54 +#include "utils/common.h"
55 +#include "utils/eloop.h"
56 +#include "common/ieee802_11_defs.h"
57 +#include "hostapd.h"
58 +#include "wps_hostapd.h"
59 +#include "sta_info.h"
60 +#include "ubus.h"
61 +#include "ap_drv_ops.h"
62 +#include "beacon.h"
63 +
64 +static struct ubus_context *ctx;
65 +static struct blob_buf b;
66 +static int ctx_ref;
67 +
68 +static inline struct hostapd_data *get_hapd_from_object(struct ubus_object *obj)
69 +{
70 +       return container_of(obj, struct hostapd_data, ubus.obj);
71 +}
72 +
73 +
74 +struct ubus_banned_client {
75 +       struct avl_node avl;
76 +       u8 addr[ETH_ALEN];
77 +};
78 +
79 +static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
80 +{
81 +       struct ubus_context *ctx = eloop_ctx;
82 +       ubus_handle_event(ctx);
83 +}
84 +
85 +static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
86 +{
87 +       if (ubus_reconnect(ctx, NULL)) {
88 +               eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
89 +               return;
90 +       }
91 +
92 +       eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
93 +}
94 +
95 +static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
96 +{
97 +       eloop_unregister_read_sock(ctx->sock.fd);
98 +       eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
99 +}
100 +
101 +static bool hostapd_ubus_init(void)
102 +{
103 +       if (ctx)
104 +               return true;
105 +
106 +       ctx = ubus_connect(NULL);
107 +       if (!ctx)
108 +               return false;
109 +
110 +       ctx->connection_lost = hostapd_ubus_connection_lost;
111 +       eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
112 +       return true;
113 +}
114 +
115 +static void hostapd_ubus_ref_inc(void)
116 +{
117 +       ctx_ref++;
118 +}
119 +
120 +static void hostapd_ubus_ref_dec(void)
121 +{
122 +       ctx_ref--;
123 +       if (!ctx)
124 +               return;
125 +
126 +       if (ctx_ref)
127 +               return;
128 +
129 +       eloop_unregister_read_sock(ctx->sock.fd);
130 +       ubus_free(ctx);
131 +       ctx = NULL;
132 +}
133 +
134 +void hostapd_ubus_add_iface(struct hostapd_iface *iface)
135 +{
136 +       if (!hostapd_ubus_init())
137 +               return;
138 +}
139 +
140 +void hostapd_ubus_free_iface(struct hostapd_iface *iface)
141 +{
142 +       if (!ctx)
143 +               return;
144 +}
145 +
146 +static void
147 +hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
148 +{
149 +       struct ubus_banned_client *ban = eloop_data;
150 +       struct hostapd_data *hapd = user_ctx;
151 +
152 +       avl_delete(&hapd->ubus.banned, &ban->avl);
153 +       free(ban);
154 +}
155 +
156 +static void
157 +hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
158 +{
159 +       struct ubus_banned_client *ban;
160 +
161 +       if (time < 0)
162 +               time = 0;
163 +
164 +       ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
165 +       if (!ban) {
166 +               if (!time)
167 +                       return;
168 +
169 +               ban = os_zalloc(sizeof(*ban));
170 +               memcpy(ban->addr, addr, sizeof(ban->addr));
171 +               ban->avl.key = ban->addr;
172 +               avl_insert(&hapd->ubus.banned, &ban->avl);
173 +       } else {
174 +               eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
175 +               if (!time) {
176 +                       hostapd_bss_del_ban(ban, hapd);
177 +                       return;
178 +               }
179 +       }
180 +
181 +       eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
182 +}
183 +
184 +static int
185 +hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
186 +                       struct ubus_request_data *req, const char *method,
187 +                       struct blob_attr *msg)
188 +{
189 +       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
190 +       struct sta_info *sta;
191 +       void *list, *c;
192 +       char mac_buf[20];
193 +       static const struct {
194 +               const char *name;
195 +               uint32_t flag;
196 +       } sta_flags[] = {
197 +               { "auth", WLAN_STA_AUTH },
198 +               { "assoc", WLAN_STA_ASSOC },
199 +               { "authorized", WLAN_STA_AUTHORIZED },
200 +               { "preauth", WLAN_STA_PREAUTH },
201 +               { "wds", WLAN_STA_WDS },
202 +               { "wmm", WLAN_STA_WMM },
203 +               { "ht", WLAN_STA_HT },
204 +               { "vht", WLAN_STA_VHT },
205 +               { "wps", WLAN_STA_WPS },
206 +               { "mfp", WLAN_STA_MFP },
207 +       };
208 +
209 +       blob_buf_init(&b, 0);
210 +       blobmsg_add_u32(&b, "freq", hapd->iface->freq);
211 +       list = blobmsg_open_table(&b, "clients");
212 +       for (sta = hapd->sta_list; sta; sta = sta->next) {
213 +               int i;
214 +
215 +               sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
216 +               c = blobmsg_open_table(&b, mac_buf);
217 +               for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
218 +                       blobmsg_add_u8(&b, sta_flags[i].name,
219 +                                      !!(sta->flags & sta_flags[i].flag));
220 +               blobmsg_add_u32(&b, "aid", sta->aid);
221 +               blobmsg_close_table(&b, c);
222 +       }
223 +       blobmsg_close_array(&b, list);
224 +       ubus_send_reply(ctx, req, b.head);
225 +
226 +       return 0;
227 +}
228 +
229 +enum {
230 +       DEL_CLIENT_ADDR,
231 +       DEL_CLIENT_REASON,
232 +       DEL_CLIENT_DEAUTH,
233 +       DEL_CLIENT_BAN_TIME,
234 +       __DEL_CLIENT_MAX
235 +};
236 +
237 +static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
238 +       [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
239 +       [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
240 +       [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
241 +       [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
242 +};
243 +
244 +static int
245 +hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
246 +                       struct ubus_request_data *req, const char *method,
247 +                       struct blob_attr *msg)
248 +{
249 +       struct blob_attr *tb[__DEL_CLIENT_MAX];
250 +       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
251 +       struct sta_info *sta;
252 +       bool deauth = false;
253 +       int reason;
254 +       u8 addr[ETH_ALEN];
255 +
256 +       blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
257 +
258 +       if (!tb[DEL_CLIENT_ADDR])
259 +               return UBUS_STATUS_INVALID_ARGUMENT;
260 +
261 +       if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
262 +               return UBUS_STATUS_INVALID_ARGUMENT;
263 +
264 +       if (tb[DEL_CLIENT_REASON])
265 +               reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
266 +
267 +       if (tb[DEL_CLIENT_DEAUTH])
268 +               deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
269 +
270 +       sta = ap_get_sta(hapd, addr);
271 +       if (sta) {
272 +               if (deauth) {
273 +                       hostapd_drv_sta_deauth(hapd, addr, reason);
274 +                       ap_sta_deauthenticate(hapd, sta, reason);
275 +               } else {
276 +                       hostapd_drv_sta_disassoc(hapd, addr, reason);
277 +                       ap_sta_disassociate(hapd, sta, reason);
278 +               }
279 +       }
280 +
281 +       if (tb[DEL_CLIENT_BAN_TIME])
282 +               hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
283 +
284 +       return 0;
285 +}
286 +
287 +static void
288 +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
289 +{
290 +       char *s;
291 +
292 +       s = blobmsg_alloc_string_buffer(buf, name, 20);
293 +       sprintf(s, MACSTR, MAC2STR(addr));
294 +       blobmsg_add_string_buffer(buf);
295 +}
296 +
297 +static int
298 +hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
299 +                     struct ubus_request_data *req, const char *method,
300 +                     struct blob_attr *msg)
301 +{
302 +       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
303 +       struct ubus_banned_client *ban;
304 +       void *c;
305 +
306 +       blob_buf_init(&b, 0);
307 +       c = blobmsg_open_array(&b, "clients");
308 +       avl_for_each_element(&hapd->ubus.banned, ban, avl)
309 +               blobmsg_add_macaddr(&b, NULL, ban->addr);
310 +       blobmsg_close_array(&b, c);
311 +       ubus_send_reply(ctx, req, b.head);
312 +
313 +       return 0;
314 +}
315 +
316 +static int
317 +hostapd_bss_wps_start(struct ubus_context *ctx, struct ubus_object *obj,
318 +                       struct ubus_request_data *req, const char *method,
319 +                       struct blob_attr *msg)
320 +{
321 +       int rc;
322 +       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
323 +
324 +       rc = hostapd_wps_button_pushed(hapd, NULL);
325 +
326 +       if (rc != 0)
327 +               return UBUS_STATUS_NOT_SUPPORTED;
328 +
329 +       return 0;
330 +}
331 +
332 +static int
333 +hostapd_bss_wps_cancel(struct ubus_context *ctx, struct ubus_object *obj,
334 +                       struct ubus_request_data *req, const char *method,
335 +                       struct blob_attr *msg)
336 +{
337 +       int rc;
338 +       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
339 +
340 +       rc = hostapd_wps_cancel(hapd);
341 +
342 +       if (rc != 0)
343 +               return UBUS_STATUS_NOT_SUPPORTED;
344 +
345 +       return 0;
346 +}
347 +
348 +static int
349 +hostapd_bss_update_beacon(struct ubus_context *ctx, struct ubus_object *obj,
350 +                       struct ubus_request_data *req, const char *method,
351 +                       struct blob_attr *msg)
352 +{
353 +       int rc;
354 +       struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
355 +
356 +       rc = ieee802_11_set_beacon(hapd);
357 +
358 +       if (rc != 0)
359 +               return UBUS_STATUS_NOT_SUPPORTED;
360 +
361 +       return 0;
362 +}
363 +
364 +enum {
365 +       CSA_FREQ,
366 +       CSA_BCN_COUNT,
367 +       __CSA_MAX
368 +};
369 +
370 +static const struct blobmsg_policy csa_policy[__CSA_MAX] = {
371 +       /*
372 +        * for now, frequency and beacon count are enough, add more
373 +        * parameters on demand
374 +        */
375 +       [CSA_FREQ] = { "freq", BLOBMSG_TYPE_INT32 },
376 +       [CSA_BCN_COUNT] = { "bcn_count", BLOBMSG_TYPE_INT32 },
377 +};
378 +
379 +#ifdef NEED_AP_MLME
380 +static int
381 +hostapd_switch_chan(struct ubus_context *ctx, struct ubus_object *obj,
382 +                   struct ubus_request_data *req, const char *method,
383 +                   struct blob_attr *msg)
384 +{
385 +       struct blob_attr *tb[__CSA_MAX];
386 +       struct hostapd_data *hapd = get_hapd_from_object(obj);
387 +       struct csa_settings css;
388 +
389 +       blobmsg_parse(csa_policy, __CSA_MAX, tb, blob_data(msg), blob_len(msg));
390 +
391 +       if (!tb[CSA_FREQ])
392 +               return UBUS_STATUS_INVALID_ARGUMENT;
393 +
394 +       memset(&css, 0, sizeof(css));
395 +       css.freq_params.freq = blobmsg_get_u32(tb[CSA_FREQ]);
396 +       if (tb[CSA_BCN_COUNT])
397 +               css.cs_count = blobmsg_get_u32(tb[CSA_BCN_COUNT]);
398 +
399 +       if (hostapd_switch_channel(hapd, &css) != 0)
400 +               return UBUS_STATUS_NOT_SUPPORTED;
401 +       return UBUS_STATUS_OK;
402 +}
403 +#endif
404 +
405 +enum {
406 +       VENDOR_ELEMENTS,
407 +       __VENDOR_ELEMENTS_MAX
408 +};
409 +
410 +static const struct blobmsg_policy ve_policy[__VENDOR_ELEMENTS_MAX] = {
411 +       /* vendor elements are provided as hex-string */
412 +       [VENDOR_ELEMENTS] = { "vendor_elements", BLOBMSG_TYPE_STRING },
413 +};
414 +
415 +static int
416 +hostapd_vendor_elements(struct ubus_context *ctx, struct ubus_object *obj,
417 +                       struct ubus_request_data *req, const char *method,
418 +                       struct blob_attr *msg)
419 +{
420 +       struct blob_attr *tb[__VENDOR_ELEMENTS_MAX];
421 +       struct hostapd_data *hapd = get_hapd_from_object(obj);
422 +       struct hostapd_bss_config *bss = hapd->conf;
423 +       struct wpabuf *elems;
424 +       const char *pos;
425 +       size_t len;
426 +
427 +       blobmsg_parse(ve_policy, __VENDOR_ELEMENTS_MAX, tb,
428 +                     blob_data(msg), blob_len(msg));
429 +
430 +       if (!tb[VENDOR_ELEMENTS])
431 +               return UBUS_STATUS_INVALID_ARGUMENT;
432 +
433 +       pos = blobmsg_data(tb[VENDOR_ELEMENTS]);
434 +       len = os_strlen(pos);
435 +       if (len & 0x01)
436 +                       return UBUS_STATUS_INVALID_ARGUMENT;
437 +
438 +       len /= 2;
439 +       if (len == 0) {
440 +               wpabuf_free(bss->vendor_elements);
441 +               bss->vendor_elements = NULL;
442 +               return 0;
443 +       }
444 +
445 +       elems = wpabuf_alloc(len);
446 +       if (elems == NULL)
447 +               return 1;
448 +
449 +       if (hexstr2bin(pos, wpabuf_put(elems, len), len)) {
450 +               wpabuf_free(elems);
451 +               return UBUS_STATUS_INVALID_ARGUMENT;
452 +       }
453 +
454 +       wpabuf_free(bss->vendor_elements);
455 +       bss->vendor_elements = elems;
456 +
457 +       /* update beacons if vendor elements were set successfully */
458 +       if (ieee802_11_update_beacons(hapd->iface) != 0)
459 +               return UBUS_STATUS_NOT_SUPPORTED;
460 +       return UBUS_STATUS_OK;
461 +}
462 +
463 +static const struct ubus_method bss_methods[] = {
464 +       UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
465 +       UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
466 +       UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
467 +       UBUS_METHOD_NOARG("wps_start", hostapd_bss_wps_start),
468 +       UBUS_METHOD_NOARG("wps_cancel", hostapd_bss_wps_cancel),
469 +       UBUS_METHOD_NOARG("update_beacon", hostapd_bss_update_beacon),
470 +#ifdef NEED_AP_MLME
471 +       UBUS_METHOD("switch_chan", hostapd_switch_chan, csa_policy),
472 +#endif
473 +       UBUS_METHOD("set_vendor_elements", hostapd_vendor_elements, ve_policy),
474 +};
475 +
476 +static struct ubus_object_type bss_object_type =
477 +       UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
478 +
479 +static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
480 +{
481 +       return memcmp(k1, k2, ETH_ALEN);
482 +}
483 +
484 +void hostapd_ubus_add_bss(struct hostapd_data *hapd)
485 +{
486 +       struct ubus_object *obj = &hapd->ubus.obj;
487 +       char *name;
488 +       int ret;
489 +
490 +       if (!hostapd_ubus_init())
491 +               return;
492 +
493 +       if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
494 +               return;
495 +
496 +       avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
497 +       obj->name = name;
498 +       obj->type = &bss_object_type;
499 +       obj->methods = bss_object_type.methods;
500 +       obj->n_methods = bss_object_type.n_methods;
501 +       ret = ubus_add_object(ctx, obj);
502 +       hostapd_ubus_ref_inc();
503 +}
504 +
505 +void hostapd_ubus_free_bss(struct hostapd_data *hapd)
506 +{
507 +       struct ubus_object *obj = &hapd->ubus.obj;
508 +       char *name = (char *) obj->name;
509 +
510 +       if (!ctx)
511 +               return;
512 +
513 +       if (obj->id) {
514 +               ubus_remove_object(ctx, obj);
515 +               hostapd_ubus_ref_dec();
516 +       }
517 +
518 +       free(name);
519 +}
520 +
521 +struct ubus_event_req {
522 +       struct ubus_notify_request nreq;
523 +       bool deny;
524 +};
525 +
526 +static void
527 +ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
528 +{
529 +       struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
530 +
531 +       if (ret)
532 +               ureq->deny = true;
533 +}
534 +
535 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
536 +{
537 +       struct ubus_banned_client *ban;
538 +       const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
539 +               [HOSTAPD_UBUS_PROBE_REQ] = "probe",
540 +               [HOSTAPD_UBUS_AUTH_REQ] = "auth",
541 +               [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
542 +       };
543 +       const char *type = "mgmt";
544 +       struct ubus_event_req ureq = {};
545 +       const u8 *addr;
546 +
547 +       if (req->mgmt_frame)
548 +               addr = req->mgmt_frame->sa;
549 +       else
550 +               addr = req->addr;
551 +
552 +       ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
553 +       if (ban)
554 +               return -2;
555 +
556 +       if (!hapd->ubus.obj.has_subscribers)
557 +               return 0;
558 +
559 +       if (req->type < ARRAY_SIZE(types))
560 +               type = types[req->type];
561 +
562 +       blob_buf_init(&b, 0);
563 +       blobmsg_add_macaddr(&b, "address", addr);
564 +       if (req->mgmt_frame)
565 +               blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
566 +       if (req->frame_info)
567 +               blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
568 +       blobmsg_add_u32(&b, "freq", hapd->iface->freq);
569 +
570 +       if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
571 +               return 0;
572 +
573 +       ureq.nreq.status_cb = ubus_event_cb;
574 +       ubus_complete_request(ctx, &ureq.nreq.req, 100);
575 +
576 +       if (ureq.deny)
577 +               return -1;
578 +
579 +       return 0;
580 +}
581 --- /dev/null
582 +++ b/src/ap/ubus.h
583 @@ -0,0 +1,78 @@
584 +/*
585 + * hostapd / ubus support
586 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
587 + *
588 + * This software may be distributed under the terms of the BSD license.
589 + * See README for more details.
590 + */
591 +#ifndef __HOSTAPD_UBUS_H
592 +#define __HOSTAPD_UBUS_H
593 +
594 +enum hostapd_ubus_event_type {
595 +       HOSTAPD_UBUS_PROBE_REQ,
596 +       HOSTAPD_UBUS_AUTH_REQ,
597 +       HOSTAPD_UBUS_ASSOC_REQ,
598 +       HOSTAPD_UBUS_TYPE_MAX
599 +};
600 +
601 +struct hostapd_ubus_request {
602 +       enum hostapd_ubus_event_type type;
603 +       const struct ieee80211_mgmt *mgmt_frame;
604 +       const struct hostapd_frame_info *frame_info;
605 +       const u8 *addr;
606 +};
607 +
608 +struct hostapd_iface;
609 +struct hostapd_data;
610 +
611 +#ifdef UBUS_SUPPORT
612 +
613 +#include <libubox/avl.h>
614 +#include <libubus.h>
615 +
616 +struct hostapd_ubus_iface {
617 +       struct ubus_object obj;
618 +};
619 +
620 +struct hostapd_ubus_bss {
621 +       struct ubus_object obj;
622 +       struct avl_tree banned;
623 +};
624 +
625 +void hostapd_ubus_add_iface(struct hostapd_iface *iface);
626 +void hostapd_ubus_free_iface(struct hostapd_iface *iface);
627 +void hostapd_ubus_add_bss(struct hostapd_data *hapd);
628 +void hostapd_ubus_free_bss(struct hostapd_data *hapd);
629 +
630 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req);
631 +
632 +#else
633 +
634 +struct hostapd_ubus_iface {};
635 +
636 +struct hostapd_ubus_bss {};
637 +
638 +static inline void hostapd_ubus_add_iface(struct hostapd_iface *iface)
639 +{
640 +}
641 +
642 +static inline void hostapd_ubus_free_iface(struct hostapd_iface *iface)
643 +{
644 +}
645 +
646 +static inline void hostapd_ubus_add_bss(struct hostapd_data *hapd)
647 +{
648 +}
649 +
650 +static inline void hostapd_ubus_free_bss(struct hostapd_data *hapd)
651 +{
652 +}
653 +
654 +static inline int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
655 +{
656 +       return 0;
657 +}
658 +
659 +#endif
660 +
661 +#endif
662 --- a/src/ap/hostapd.c
663 +++ b/src/ap/hostapd.c
664 @@ -284,6 +284,7 @@ static void hostapd_free_hapd_data(struc
665         hapd->started = 0;
666  
667         wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
668 +       hostapd_ubus_free_bss(hapd);
669         iapp_deinit(hapd->iapp);
670         hapd->iapp = NULL;
671         accounting_deinit(hapd);
672 @@ -1139,6 +1140,8 @@ static int hostapd_setup_bss(struct host
673         if (hapd->driver && hapd->driver->set_operstate)
674                 hapd->driver->set_operstate(hapd->drv_priv, 1);
675  
676 +       hostapd_ubus_add_bss(hapd);
677 +
678         return 0;
679  }
680  
681 @@ -1664,6 +1667,7 @@ static int hostapd_setup_interface_compl
682         if (err)
683                 goto fail;
684  
685 +       hostapd_ubus_add_iface(iface);
686         wpa_printf(MSG_DEBUG, "Completing interface initialization");
687         if (iface->conf->channel) {
688  #ifdef NEED_AP_MLME
689 @@ -1844,6 +1848,7 @@ dfs_offload:
690  
691  fail:
692         wpa_printf(MSG_ERROR, "Interface initialization failed");
693 +       hostapd_ubus_free_iface(iface);
694         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
695         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
696  #ifdef CONFIG_FST
697 @@ -2277,6 +2282,7 @@ void hostapd_interface_deinit_free(struc
698                    (unsigned int) iface->conf->num_bss);
699         driver = iface->bss[0]->driver;
700         drv_priv = iface->bss[0]->drv_priv;
701 +       hostapd_ubus_free_iface(iface);
702         hostapd_interface_deinit(iface);
703         wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
704                    __func__, driver, drv_priv);
705 --- a/src/ap/ieee802_11.c
706 +++ b/src/ap/ieee802_11.c
707 @@ -980,7 +980,8 @@ int auth_sae_init_committed(struct hosta
708  
709  
710  static void handle_auth(struct hostapd_data *hapd,
711 -                       const struct ieee80211_mgmt *mgmt, size_t len)
712 +                       const struct ieee80211_mgmt *mgmt, size_t len,
713 +                       struct hostapd_frame_info *fi)
714  {
715         u16 auth_alg, auth_transaction, status_code;
716         u16 resp = WLAN_STATUS_SUCCESS;
717 @@ -996,6 +997,11 @@ static void handle_auth(struct hostapd_d
718         char *identity = NULL;
719         char *radius_cui = NULL;
720         u16 seq_ctrl;
721 +       struct hostapd_ubus_request req = {
722 +               .type = HOSTAPD_UBUS_AUTH_REQ,
723 +               .mgmt_frame = mgmt,
724 +               .frame_info = fi,
725 +       };
726  
727         os_memset(&vlan_id, 0, sizeof(vlan_id));
728  
729 @@ -1149,6 +1155,14 @@ static void handle_auth(struct hostapd_d
730                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
731                 goto fail;
732         }
733 +
734 +       if (hostapd_ubus_handle_event(hapd, &req)) {
735 +               wpa_printf(MSG_DEBUG, "Station " MACSTR " rejected by ubus handler.\n",
736 +                      MAC2STR(mgmt->sa));
737 +               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
738 +               goto fail;
739 +       }
740 +
741         if (res == HOSTAPD_ACL_PENDING) {
742                 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
743                            " waiting for an external authentication",
744 @@ -2033,13 +2047,18 @@ static u16 send_assoc_resp(struct hostap
745  
746  static void handle_assoc(struct hostapd_data *hapd,
747                          const struct ieee80211_mgmt *mgmt, size_t len,
748 -                        int reassoc)
749 +                        int reassoc, struct hostapd_frame_info *fi)
750  {
751         u16 capab_info, listen_interval, seq_ctrl, fc;
752         u16 resp = WLAN_STATUS_SUCCESS, reply_res;
753         const u8 *pos;
754         int left, i;
755         struct sta_info *sta;
756 +       struct hostapd_ubus_request req = {
757 +               .type = HOSTAPD_UBUS_ASSOC_REQ,
758 +               .mgmt_frame = mgmt,
759 +               .frame_info = fi,
760 +       };
761  
762         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
763                                       sizeof(mgmt->u.assoc_req))) {
764 @@ -2159,6 +2178,13 @@ static void handle_assoc(struct hostapd_
765         }
766  #endif /* CONFIG_MBO */
767  
768 +       if (hostapd_ubus_handle_event(hapd, &req)) {
769 +               wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
770 +                      MAC2STR(mgmt->sa));
771 +               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
772 +               goto fail;
773 +       }
774 +
775         /*
776          * sta->capability is used in check_assoc_ies() for RRM enabled
777          * capability element.
778 @@ -2639,7 +2665,7 @@ int ieee802_11_mgmt(struct hostapd_data
779  
780  
781         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
782 -               handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
783 +               handle_probe_req(hapd, mgmt, len, fi);
784                 return 1;
785         }
786  
787 @@ -2657,17 +2683,17 @@ int ieee802_11_mgmt(struct hostapd_data
788         switch (stype) {
789         case WLAN_FC_STYPE_AUTH:
790                 wpa_printf(MSG_DEBUG, "mgmt::auth");
791 -               handle_auth(hapd, mgmt, len);
792 +               handle_auth(hapd, mgmt, len, fi);
793                 ret = 1;
794                 break;
795         case WLAN_FC_STYPE_ASSOC_REQ:
796                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
797 -               handle_assoc(hapd, mgmt, len, 0);
798 +               handle_assoc(hapd, mgmt, len, 0, fi);
799                 ret = 1;
800                 break;
801         case WLAN_FC_STYPE_REASSOC_REQ:
802                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
803 -               handle_assoc(hapd, mgmt, len, 1);
804 +               handle_assoc(hapd, mgmt, len, 1, fi);
805                 ret = 1;
806                 break;
807         case WLAN_FC_STYPE_DISASSOC:
808 --- a/src/ap/beacon.c
809 +++ b/src/ap/beacon.c
810 @@ -675,7 +675,7 @@ sta_track_seen_on(struct hostapd_iface *
811  
812  void handle_probe_req(struct hostapd_data *hapd,
813                       const struct ieee80211_mgmt *mgmt, size_t len,
814 -                     int ssi_signal)
815 +                     struct hostapd_frame_info *fi)
816  {
817         u8 *resp;
818         struct ieee802_11_elems elems;
819 @@ -684,9 +684,15 @@ void handle_probe_req(struct hostapd_dat
820         size_t i, resp_len;
821         int noack;
822         enum ssid_match_result res;
823 +       int ssi_signal = fi->ssi_signal;
824         int ret;
825         u16 csa_offs[2];
826         size_t csa_offs_len;
827 +       struct hostapd_ubus_request req = {
828 +               .type = HOSTAPD_UBUS_PROBE_REQ,
829 +               .mgmt_frame = mgmt,
830 +               .frame_info = fi,
831 +       };
832  
833         if (len < IEEE80211_HDRLEN)
834                 return;
835 @@ -838,6 +844,12 @@ void handle_probe_req(struct hostapd_dat
836         }
837  #endif /* CONFIG_P2P */
838  
839 +       if (hostapd_ubus_handle_event(hapd, &req)) {
840 +               wpa_printf(MSG_DEBUG, "Probe request for " MACSTR " rejected by ubus handler.\n",
841 +                      MAC2STR(mgmt->sa));
842 +               return;
843 +       }
844 +
845         /* TODO: verify that supp_rates contains at least one matching rate
846          * with AP configuration */
847  
848 --- a/src/ap/beacon.h
849 +++ b/src/ap/beacon.h
850 @@ -14,7 +14,7 @@ struct ieee80211_mgmt;
851  
852  void handle_probe_req(struct hostapd_data *hapd,
853                       const struct ieee80211_mgmt *mgmt, size_t len,
854 -                     int ssi_signal);
855 +                     struct hostapd_frame_info *fi);
856  int ieee802_11_set_beacon(struct hostapd_data *hapd);
857  int ieee802_11_set_beacons(struct hostapd_iface *iface);
858  int ieee802_11_update_beacons(struct hostapd_iface *iface);
859 --- a/src/ap/drv_callbacks.c
860 +++ b/src/ap/drv_callbacks.c
861 @@ -52,6 +52,10 @@ int hostapd_notif_assoc(struct hostapd_d
862         u16 reason = WLAN_REASON_UNSPECIFIED;
863         u16 status = WLAN_STATUS_SUCCESS;
864         const u8 *p2p_dev_addr = NULL;
865 +       struct hostapd_ubus_request req = {
866 +               .type = HOSTAPD_UBUS_ASSOC_REQ,
867 +               .addr = addr,
868 +       };
869  
870         if (addr == NULL) {
871                 /*
872 @@ -124,6 +128,12 @@ int hostapd_notif_assoc(struct hostapd_d
873                 goto fail;
874         }
875  
876 +       if (hostapd_ubus_handle_event(hapd, &req)) {
877 +               wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
878 +                          MAC2STR(req.addr));
879 +               goto fail;
880 +       }
881 +
882  #ifdef CONFIG_P2P
883         if (elems.p2p) {
884                 wpabuf_free(sta->p2p_ie);