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