3 @@ -97,6 +97,11 @@ OBJS += ../src/common/wpa_common.o
5 OBJS += ../src/eapol_auth/eapol_auth_sm.o
8 +CFLAGS += -DUBUS_SUPPORT
9 +OBJS += ../src/ap/ubus.o
10 +LIBS += -lubox -lubus
13 ifndef CONFIG_NO_DUMP_STATE
14 # define HOSTAPD_DUMP_STATE to include SIGUSR1 handler for dumping state to
15 --- a/src/ap/hostapd.h
16 +++ b/src/ap/hostapd.h
19 #include "common/defs.h"
20 #include "ap_config.h"
23 struct wpa_driver_ops;
25 @@ -72,6 +73,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 */
32 u8 own_addr[ETH_ALEN];
33 @@ -213,6 +215,7 @@ struct hostapd_iface {
36 struct hostapd_config *conf;
37 + struct hostapd_ubus_iface ubus;
40 struct hostapd_data **bss;
45 + * hostapd / ubus support
46 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
48 + * This software may be distributed under the terms of the BSD license.
49 + * See README for more details.
52 +#include "utils/includes.h"
53 +#include "utils/common.h"
54 +#include "utils/eloop.h"
55 +#include "common/ieee802_11_defs.h"
57 +#include "sta_info.h"
60 +static struct ubus_context *ctx;
61 +static struct blob_buf b;
64 +struct ubus_banned_client {
65 + struct avl_node avl;
69 +static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
71 + struct ubus_context *ctx = eloop_ctx;
72 + ubus_handle_event(ctx);
75 +static bool hostapd_ubus_init(void)
80 + ctx = ubus_connect(NULL);
84 + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
88 +static void hostapd_ubus_ref_inc(void)
93 +static void hostapd_ubus_ref_dec(void)
102 + eloop_unregister_read_sock(ctx->sock.fd);
107 +void hostapd_ubus_add_iface(struct hostapd_iface *iface)
109 + if (!hostapd_ubus_init())
113 +void hostapd_ubus_free_iface(struct hostapd_iface *iface)
120 +hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
122 + struct ubus_banned_client *ban = eloop_data;
123 + struct hostapd_data *hapd = user_ctx;
125 + avl_delete(&hapd->ubus.banned, &ban->avl);
130 +hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
132 + struct ubus_banned_client *ban;
137 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
142 + ban = os_zalloc(sizeof(*ban));
143 + memcpy(ban->addr, addr, sizeof(ban->addr));
144 + ban->avl.key = ban->addr;
145 + avl_insert(&hapd->ubus.banned, &ban->avl);
147 + eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
149 + hostapd_bss_del_ban(ban, hapd);
154 + eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
158 +hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
159 + struct ubus_request_data *req, const char *method,
160 + struct blob_attr *msg)
162 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
163 + struct sta_info *sta;
166 + static const struct {
170 + { "auth", WLAN_STA_AUTH },
171 + { "assoc", WLAN_STA_ASSOC },
172 + { "authorized", WLAN_STA_AUTHORIZED },
173 + { "preauth", WLAN_STA_PREAUTH },
174 + { "wds", WLAN_STA_WDS },
175 + { "wmm", WLAN_STA_WMM },
176 + { "ht", WLAN_STA_HT },
177 + { "vht", WLAN_STA_VHT },
178 + { "wps", WLAN_STA_WPS },
179 + { "mfp", WLAN_STA_MFP },
182 + blob_buf_init(&b, 0);
183 + blobmsg_add_u32(&b, "freq", hapd->iface->freq);
184 + list = blobmsg_open_table(&b, "clients");
185 + for (sta = hapd->sta_list; sta; sta = sta->next) {
188 + sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
189 + c = blobmsg_open_table(&b, mac_buf);
190 + for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
191 + blobmsg_add_u8(&b, sta_flags[i].name,
192 + !!(sta->flags & sta_flags[i].flag));
193 + blobmsg_add_u32(&b, "aid", sta->aid);
194 + blobmsg_close_table(&b, c);
196 + blobmsg_close_array(&b, list);
197 + ubus_send_reply(ctx, req, b.head);
206 + DEL_CLIENT_BAN_TIME,
210 +static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
211 + [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
212 + [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
213 + [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
214 + [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
218 +hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
219 + struct ubus_request_data *req, const char *method,
220 + struct blob_attr *msg)
222 + struct blob_attr *tb[__DEL_CLIENT_MAX];
223 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
224 + struct sta_info *sta;
225 + bool deauth = false;
229 + blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
231 + if (!tb[DEL_CLIENT_ADDR])
232 + return UBUS_STATUS_INVALID_ARGUMENT;
234 + if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
235 + return UBUS_STATUS_INVALID_ARGUMENT;
237 + if (tb[DEL_CLIENT_REASON])
238 + reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
240 + if (tb[DEL_CLIENT_DEAUTH])
241 + deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
243 + sta = ap_get_sta(hapd, addr);
246 + hostapd_drv_sta_deauth(hapd, addr, reason);
247 + ap_sta_deauthenticate(hapd, sta, reason);
249 + hostapd_drv_sta_disassoc(hapd, addr, reason);
250 + ap_sta_disassociate(hapd, sta, reason);
254 + if (tb[DEL_CLIENT_BAN_TIME])
255 + hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
261 +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
265 + s = blobmsg_alloc_string_buffer(buf, name, 20);
266 + sprintf(s, MACSTR, MAC2STR(addr));
267 + blobmsg_add_string_buffer(buf);
271 +hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
272 + struct ubus_request_data *req, const char *method,
273 + struct blob_attr *msg)
275 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
276 + struct ubus_banned_client *ban;
279 + blob_buf_init(&b, 0);
280 + c = blobmsg_open_array(&b, "clients");
281 + avl_for_each_element(&hapd->ubus.banned, ban, avl)
282 + blobmsg_add_macaddr(&b, NULL, ban->addr);
283 + blobmsg_close_array(&b, c);
284 + ubus_send_reply(ctx, req, b.head);
289 +static const struct ubus_method bss_methods[] = {
290 + UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
291 + UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
292 + UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
295 +static struct ubus_object_type bss_object_type =
296 + UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
298 +static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
300 + return memcmp(k1, k2, ETH_ALEN);
303 +void hostapd_ubus_add_bss(struct hostapd_data *hapd)
305 + struct ubus_object *obj = &hapd->ubus.obj;
309 + if (!hostapd_ubus_init())
312 + if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
315 + avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
317 + obj->type = &bss_object_type;
318 + obj->methods = bss_object_type.methods;
319 + obj->n_methods = bss_object_type.n_methods;
320 + ret = ubus_add_object(ctx, obj);
321 + hostapd_ubus_ref_inc();
324 +void hostapd_ubus_free_bss(struct hostapd_data *hapd)
326 + struct ubus_object *obj = &hapd->ubus.obj;
327 + char *name = (char *) obj->name;
333 + ubus_remove_object(ctx, obj);
334 + hostapd_ubus_ref_dec();
340 +struct ubus_event_req {
341 + struct ubus_notify_request nreq;
346 +ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
348 + struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
354 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
356 + struct ubus_banned_client *ban;
357 + const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
358 + [HOSTAPD_UBUS_PROBE_REQ] = "probe",
359 + [HOSTAPD_UBUS_AUTH_REQ] = "auth",
360 + [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
362 + const char *type = "mgmt";
363 + struct ubus_event_req ureq = {};
366 + if (req->mgmt_frame)
367 + addr = req->mgmt_frame->sa;
371 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
375 + if (!hapd->ubus.obj.has_subscribers)
378 + if (req->type < ARRAY_SIZE(types))
379 + type = types[req->type];
381 + blob_buf_init(&b, 0);
382 + blobmsg_add_macaddr(&b, "address", addr);
383 + if (req->mgmt_frame)
384 + blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
385 + if (req->frame_info)
386 + blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
387 + blobmsg_add_u32(&b, "freq", hapd->iface->freq);
389 + if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
392 + ureq.nreq.status_cb = ubus_event_cb;
393 + ubus_complete_request(ctx, &ureq.nreq.req, 100);
404 + * hostapd / ubus support
405 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
407 + * This software may be distributed under the terms of the BSD license.
408 + * See README for more details.
410 +#ifndef __HOSTAPD_UBUS_H
411 +#define __HOSTAPD_UBUS_H
413 +enum hostapd_ubus_event_type {
414 + HOSTAPD_UBUS_PROBE_REQ,
415 + HOSTAPD_UBUS_AUTH_REQ,
416 + HOSTAPD_UBUS_ASSOC_REQ,
417 + HOSTAPD_UBUS_TYPE_MAX
420 +struct hostapd_ubus_request {
421 + enum hostapd_ubus_event_type type;
422 + const struct ieee80211_mgmt *mgmt_frame;
423 + const struct hostapd_frame_info *frame_info;
427 +struct hostapd_iface;
428 +struct hostapd_data;
432 +#include <libubox/avl.h>
433 +#include <libubus.h>
435 +struct hostapd_ubus_iface {
436 + struct ubus_object obj;
439 +struct hostapd_ubus_bss {
440 + struct ubus_object obj;
441 + struct avl_tree banned;
444 +void hostapd_ubus_add_iface(struct hostapd_iface *iface);
445 +void hostapd_ubus_free_iface(struct hostapd_iface *iface);
446 +void hostapd_ubus_add_bss(struct hostapd_data *hapd);
447 +void hostapd_ubus_free_bss(struct hostapd_data *hapd);
449 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req);
453 +struct hostapd_ubus_iface {};
455 +struct hostapd_ubus_bss {};
457 +static inline void hostapd_ubus_add_iface(struct hostapd_iface *iface)
461 +static inline void hostapd_ubus_free_iface(struct hostapd_iface *iface)
465 +static inline void hostapd_ubus_add_bss(struct hostapd_data *hapd)
469 +static inline void hostapd_ubus_free_bss(struct hostapd_data *hapd)
473 +static inline int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
481 --- a/src/ap/hostapd.c
482 +++ b/src/ap/hostapd.c
483 @@ -270,6 +270,7 @@ static int hostapd_broadcast_wep_set(str
485 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
487 + hostapd_ubus_free_bss(hapd);
488 iapp_deinit(hapd->iapp);
490 accounting_deinit(hapd);
491 @@ -834,6 +835,8 @@ static int hostapd_setup_bss(struct host
492 if (hapd->driver && hapd->driver->set_operstate)
493 hapd->driver->set_operstate(hapd->drv_priv, 1);
495 + hostapd_ubus_add_bss(hapd);
500 @@ -916,6 +919,7 @@ int hostapd_setup_interface_complete(str
504 + hostapd_ubus_add_iface(iface);
505 wpa_printf(MSG_DEBUG, "Completing interface initialization");
506 if (hapd->iconf->channel) {
507 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
508 @@ -1006,6 +1010,7 @@ int hostapd_setup_interface_complete(str
511 wpa_printf(MSG_ERROR, "Interface initialization failed");
512 + hostapd_ubus_free_iface(iface);
516 @@ -1104,6 +1109,8 @@ void hostapd_interface_deinit_free(struc
521 + hostapd_ubus_free_iface(iface);
522 driver = iface->bss[0]->driver;
523 drv_priv = iface->bss[0]->drv_priv;
524 hostapd_interface_deinit(iface);
525 --- a/src/ap/ieee802_11.c
526 +++ b/src/ap/ieee802_11.c
527 @@ -535,7 +535,8 @@ static void handle_auth_sae(struct hosta
530 static void handle_auth(struct hostapd_data *hapd,
531 - const struct ieee80211_mgmt *mgmt, size_t len)
532 + const struct ieee80211_mgmt *mgmt, size_t len,
533 + struct hostapd_frame_info *fi)
535 u16 auth_alg, auth_transaction, status_code;
536 u16 resp = WLAN_STATUS_SUCCESS;
537 @@ -550,6 +551,11 @@ static void handle_auth(struct hostapd_d
538 size_t resp_ies_len = 0;
539 char *identity = NULL;
540 char *radius_cui = NULL;
541 + struct hostapd_ubus_request req = {
542 + .type = HOSTAPD_UBUS_AUTH_REQ,
543 + .mgmt_frame = mgmt,
547 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
548 printf("handle_auth - too short payload (len=%lu)\n",
549 @@ -623,6 +629,14 @@ static void handle_auth(struct hostapd_d
550 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
554 + if (hostapd_ubus_handle_event(hapd, &req)) {
555 + wpa_printf(MSG_DEBUG, "Station " MACSTR " rejected by ubus handler.\n",
556 + MAC2STR(mgmt->sa));
557 + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
561 if (res == HOSTAPD_ACL_PENDING) {
562 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
563 " waiting for an external authentication",
564 @@ -1211,13 +1225,18 @@ static void send_assoc_resp(struct hosta
566 static void handle_assoc(struct hostapd_data *hapd,
567 const struct ieee80211_mgmt *mgmt, size_t len,
569 + int reassoc, struct hostapd_frame_info *fi)
571 u16 capab_info, listen_interval;
572 u16 resp = WLAN_STATUS_SUCCESS;
575 struct sta_info *sta;
576 + struct hostapd_ubus_request req = {
577 + .type = HOSTAPD_UBUS_ASSOC_REQ,
578 + .mgmt_frame = mgmt,
582 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
583 sizeof(mgmt->u.assoc_req))) {
584 @@ -1296,6 +1315,13 @@ static void handle_assoc(struct hostapd_
588 + if (hostapd_ubus_handle_event(hapd, &req)) {
589 + wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
590 + MAC2STR(mgmt->sa));
591 + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
595 sta->capability = capab_info;
596 sta->listen_interval = listen_interval;
598 @@ -1705,7 +1731,7 @@ void ieee802_11_mgmt(struct hostapd_data
601 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
602 - handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
603 + handle_probe_req(hapd, mgmt, len, fi);
607 @@ -1720,15 +1746,15 @@ void ieee802_11_mgmt(struct hostapd_data
609 case WLAN_FC_STYPE_AUTH:
610 wpa_printf(MSG_DEBUG, "mgmt::auth");
611 - handle_auth(hapd, mgmt, len);
612 + handle_auth(hapd, mgmt, len, fi);
614 case WLAN_FC_STYPE_ASSOC_REQ:
615 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
616 - handle_assoc(hapd, mgmt, len, 0);
617 + handle_assoc(hapd, mgmt, len, 0, fi);
619 case WLAN_FC_STYPE_REASSOC_REQ:
620 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
621 - handle_assoc(hapd, mgmt, len, 1);
622 + handle_assoc(hapd, mgmt, len, 1, fi);
624 case WLAN_FC_STYPE_DISASSOC:
625 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
626 --- a/src/ap/beacon.c
627 +++ b/src/ap/beacon.c
628 @@ -352,7 +352,7 @@ static enum ssid_match_result ssid_match
630 void handle_probe_req(struct hostapd_data *hapd,
631 const struct ieee80211_mgmt *mgmt, size_t len,
633 + struct hostapd_frame_info *fi)
636 struct ieee802_11_elems elems;
637 @@ -360,8 +360,14 @@ void handle_probe_req(struct hostapd_dat
639 struct sta_info *sta = NULL;
641 + int ssi_signal = fi->ssi_signal;
643 enum ssid_match_result res;
644 + struct hostapd_ubus_request req = {
645 + .type = HOSTAPD_UBUS_PROBE_REQ,
646 + .mgmt_frame = mgmt,
650 ie = mgmt->u.probe_req.variable;
651 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
652 @@ -489,6 +495,12 @@ void handle_probe_req(struct hostapd_dat
654 #endif /* CONFIG_INTERWORKING */
656 + if (hostapd_ubus_handle_event(hapd, &req)) {
657 + wpa_printf(MSG_DEBUG, "Probe request for " MACSTR " rejected by ubus handler.\n",
658 + MAC2STR(mgmt->sa));
662 /* TODO: verify that supp_rates contains at least one matching rate
663 * with AP configuration */
665 --- a/src/ap/beacon.h
666 +++ b/src/ap/beacon.h
667 @@ -20,7 +20,7 @@ struct ieee80211_mgmt;
669 void handle_probe_req(struct hostapd_data *hapd,
670 const struct ieee80211_mgmt *mgmt, size_t len,
672 + struct hostapd_frame_info *fi);
673 void ieee802_11_set_beacon(struct hostapd_data *hapd);
674 void ieee802_11_set_beacons(struct hostapd_iface *iface);
675 void ieee802_11_update_beacons(struct hostapd_iface *iface);