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