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 + list = blobmsg_open_table(&b, "clients");
184 + for (sta = hapd->sta_list; sta; sta = sta->next) {
187 + sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
188 + c = blobmsg_open_table(&b, mac_buf);
189 + for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
190 + blobmsg_add_u8(&b, sta_flags[i].name,
191 + !!(sta->flags & sta_flags[i].flag));
192 + blobmsg_add_u32(&b, "aid", sta->aid);
193 + blobmsg_close_table(&b, c);
195 + blobmsg_close_array(&b, list);
196 + ubus_send_reply(ctx, req, b.head);
205 + DEL_CLIENT_BAN_TIME,
209 +static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
210 + [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
211 + [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
212 + [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
213 + [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
217 +hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
218 + struct ubus_request_data *req, const char *method,
219 + struct blob_attr *msg)
221 + struct blob_attr *tb[__DEL_CLIENT_MAX];
222 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
223 + struct sta_info *sta;
224 + bool deauth = false;
228 + blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
230 + if (!tb[DEL_CLIENT_ADDR])
231 + return UBUS_STATUS_INVALID_ARGUMENT;
233 + if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
234 + return UBUS_STATUS_INVALID_ARGUMENT;
236 + if (tb[DEL_CLIENT_REASON])
237 + reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
239 + if (tb[DEL_CLIENT_DEAUTH])
240 + deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
242 + sta = ap_get_sta(hapd, addr);
245 + hostapd_drv_sta_deauth(hapd, addr, reason);
246 + ap_sta_deauthenticate(hapd, sta, reason);
248 + hostapd_drv_sta_disassoc(hapd, addr, reason);
249 + ap_sta_disassociate(hapd, sta, reason);
253 + if (tb[DEL_CLIENT_BAN_TIME])
254 + hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
260 +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
264 + s = blobmsg_alloc_string_buffer(buf, name, 20);
265 + sprintf(s, MACSTR, MAC2STR(addr));
266 + blobmsg_add_string_buffer(buf);
270 +hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
271 + struct ubus_request_data *req, const char *method,
272 + struct blob_attr *msg)
274 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
275 + struct ubus_banned_client *ban;
278 + blob_buf_init(&b, 0);
279 + c = blobmsg_open_array(&b, "clients");
280 + avl_for_each_element(&hapd->ubus.banned, ban, avl)
281 + blobmsg_add_macaddr(&b, NULL, ban->addr);
282 + blobmsg_close_array(&b, c);
283 + ubus_send_reply(ctx, req, b.head);
288 +static const struct ubus_method bss_methods[] = {
289 + UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
290 + UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
291 + UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
294 +static struct ubus_object_type bss_object_type =
295 + UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
297 +static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
299 + return memcmp(k1, k2, ETH_ALEN);
302 +void hostapd_ubus_add_bss(struct hostapd_data *hapd)
304 + struct ubus_object *obj = &hapd->ubus.obj;
308 + if (!hostapd_ubus_init())
311 + if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
314 + avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
316 + obj->type = &bss_object_type;
317 + obj->methods = bss_object_type.methods;
318 + obj->n_methods = bss_object_type.n_methods;
319 + ret = ubus_add_object(ctx, obj);
320 + hostapd_ubus_ref_inc();
323 +void hostapd_ubus_free_bss(struct hostapd_data *hapd)
325 + struct ubus_object *obj = &hapd->ubus.obj;
326 + char *name = (char *) obj->name;
332 + ubus_remove_object(ctx, obj);
333 + hostapd_ubus_ref_dec();
339 +struct ubus_event_req {
340 + struct ubus_notify_request nreq;
345 +ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
347 + struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
353 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
355 + struct ubus_banned_client *ban;
356 + const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
357 + [HOSTAPD_UBUS_PROBE_REQ] = "probe",
358 + [HOSTAPD_UBUS_AUTH_REQ] = "auth",
359 + [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
361 + const char *type = "mgmt";
362 + struct ubus_event_req ureq = {};
365 + if (req->mgmt_frame)
366 + addr = req->mgmt_frame->sa;
370 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
374 + if (!hapd->ubus.obj.has_subscribers)
377 + if (req->type < ARRAY_SIZE(types))
378 + type = types[req->type];
380 + blob_buf_init(&b, 0);
381 + blobmsg_add_macaddr(&b, "address", addr);
382 + if (req->mgmt_frame)
383 + blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
384 + if (req->frame_info)
385 + blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
387 + if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
390 + ureq.nreq.status_cb = ubus_event_cb;
391 + ubus_complete_request(ctx, &ureq.nreq.req, 100);
402 + * hostapd / ubus support
403 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
405 + * This software may be distributed under the terms of the BSD license.
406 + * See README for more details.
408 +#ifndef __HOSTAPD_UBUS_H
409 +#define __HOSTAPD_UBUS_H
411 +enum hostapd_ubus_event_type {
412 + HOSTAPD_UBUS_PROBE_REQ,
413 + HOSTAPD_UBUS_AUTH_REQ,
414 + HOSTAPD_UBUS_ASSOC_REQ,
415 + HOSTAPD_UBUS_TYPE_MAX
418 +struct hostapd_ubus_request {
419 + enum hostapd_ubus_event_type type;
420 + const struct ieee80211_mgmt *mgmt_frame;
421 + const struct hostapd_frame_info *frame_info;
425 +struct hostapd_iface;
426 +struct hostapd_data;
430 +#include <libubox/avl.h>
431 +#include <libubus.h>
433 +struct hostapd_ubus_iface {
434 + struct ubus_object obj;
437 +struct hostapd_ubus_bss {
438 + struct ubus_object obj;
439 + struct avl_tree banned;
442 +void hostapd_ubus_add_iface(struct hostapd_iface *iface);
443 +void hostapd_ubus_free_iface(struct hostapd_iface *iface);
444 +void hostapd_ubus_add_bss(struct hostapd_data *hapd);
445 +void hostapd_ubus_free_bss(struct hostapd_data *hapd);
447 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req);
451 +struct hostapd_ubus_iface {};
453 +struct hostapd_ubus_bss {};
455 +static inline void hostapd_ubus_add_iface(struct hostapd_iface *iface)
459 +static inline void hostapd_ubus_free_iface(struct hostapd_iface *iface)
463 +static inline void hostapd_ubus_add_bss(struct hostapd_data *hapd)
467 +static inline void hostapd_ubus_free_bss(struct hostapd_data *hapd)
471 +static inline int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
479 --- a/src/ap/hostapd.c
480 +++ b/src/ap/hostapd.c
481 @@ -270,6 +270,7 @@ static int hostapd_broadcast_wep_set(str
483 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
485 + hostapd_ubus_free_bss(hapd);
486 iapp_deinit(hapd->iapp);
488 accounting_deinit(hapd);
489 @@ -834,6 +835,8 @@ static int hostapd_setup_bss(struct host
490 if (hapd->driver && hapd->driver->set_operstate)
491 hapd->driver->set_operstate(hapd->drv_priv, 1);
493 + hostapd_ubus_add_bss(hapd);
498 @@ -916,6 +919,7 @@ int hostapd_setup_interface_complete(str
502 + hostapd_ubus_add_iface(iface);
503 wpa_printf(MSG_DEBUG, "Completing interface initialization");
504 if (hapd->iconf->channel) {
505 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
506 @@ -1006,6 +1010,7 @@ int hostapd_setup_interface_complete(str
509 wpa_printf(MSG_ERROR, "Interface initialization failed");
510 + hostapd_ubus_free_iface(iface);
514 @@ -1104,6 +1109,8 @@ void hostapd_interface_deinit_free(struc
519 + hostapd_ubus_free_iface(iface);
520 driver = iface->bss[0]->driver;
521 drv_priv = iface->bss[0]->drv_priv;
522 hostapd_interface_deinit(iface);
523 --- a/src/ap/ieee802_11.c
524 +++ b/src/ap/ieee802_11.c
525 @@ -535,7 +535,8 @@ static void handle_auth_sae(struct hosta
528 static void handle_auth(struct hostapd_data *hapd,
529 - const struct ieee80211_mgmt *mgmt, size_t len)
530 + const struct ieee80211_mgmt *mgmt, size_t len,
531 + struct hostapd_frame_info *fi)
533 u16 auth_alg, auth_transaction, status_code;
534 u16 resp = WLAN_STATUS_SUCCESS;
535 @@ -550,6 +551,11 @@ static void handle_auth(struct hostapd_d
536 size_t resp_ies_len = 0;
537 char *identity = NULL;
538 char *radius_cui = NULL;
539 + struct hostapd_ubus_request req = {
540 + .type = HOSTAPD_UBUS_AUTH_REQ,
541 + .mgmt_frame = mgmt,
545 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
546 printf("handle_auth - too short payload (len=%lu)\n",
547 @@ -623,6 +629,14 @@ static void handle_auth(struct hostapd_d
548 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
552 + if (hostapd_ubus_handle_event(hapd, &req)) {
553 + wpa_printf(MSG_DEBUG, "Station " MACSTR " rejected by ubus handler.\n",
554 + MAC2STR(mgmt->sa));
555 + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
559 if (res == HOSTAPD_ACL_PENDING) {
560 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
561 " waiting for an external authentication",
562 @@ -1211,13 +1225,18 @@ static void send_assoc_resp(struct hosta
564 static void handle_assoc(struct hostapd_data *hapd,
565 const struct ieee80211_mgmt *mgmt, size_t len,
567 + int reassoc, struct hostapd_frame_info *fi)
569 u16 capab_info, listen_interval;
570 u16 resp = WLAN_STATUS_SUCCESS;
573 struct sta_info *sta;
574 + struct hostapd_ubus_request req = {
575 + .type = HOSTAPD_UBUS_ASSOC_REQ,
576 + .mgmt_frame = mgmt,
580 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
581 sizeof(mgmt->u.assoc_req))) {
582 @@ -1296,6 +1315,13 @@ static void handle_assoc(struct hostapd_
586 + if (hostapd_ubus_handle_event(hapd, &req)) {
587 + wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
588 + MAC2STR(mgmt->sa));
589 + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
593 sta->capability = capab_info;
594 sta->listen_interval = listen_interval;
596 @@ -1705,7 +1731,7 @@ void ieee802_11_mgmt(struct hostapd_data
599 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
600 - handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
601 + handle_probe_req(hapd, mgmt, len, fi);
605 @@ -1720,15 +1746,15 @@ void ieee802_11_mgmt(struct hostapd_data
607 case WLAN_FC_STYPE_AUTH:
608 wpa_printf(MSG_DEBUG, "mgmt::auth");
609 - handle_auth(hapd, mgmt, len);
610 + handle_auth(hapd, mgmt, len, fi);
612 case WLAN_FC_STYPE_ASSOC_REQ:
613 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
614 - handle_assoc(hapd, mgmt, len, 0);
615 + handle_assoc(hapd, mgmt, len, 0, fi);
617 case WLAN_FC_STYPE_REASSOC_REQ:
618 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
619 - handle_assoc(hapd, mgmt, len, 1);
620 + handle_assoc(hapd, mgmt, len, 1, fi);
622 case WLAN_FC_STYPE_DISASSOC:
623 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
624 --- a/src/ap/beacon.c
625 +++ b/src/ap/beacon.c
626 @@ -352,7 +352,7 @@ static enum ssid_match_result ssid_match
628 void handle_probe_req(struct hostapd_data *hapd,
629 const struct ieee80211_mgmt *mgmt, size_t len,
631 + struct hostapd_frame_info *fi)
634 struct ieee802_11_elems elems;
635 @@ -360,8 +360,14 @@ void handle_probe_req(struct hostapd_dat
637 struct sta_info *sta = NULL;
639 + int ssi_signal = fi->ssi_signal;
641 enum ssid_match_result res;
642 + struct hostapd_ubus_request req = {
643 + .type = HOSTAPD_UBUS_PROBE_REQ,
644 + .mgmt_frame = mgmt,
648 ie = mgmt->u.probe_req.variable;
649 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
650 @@ -489,6 +495,12 @@ void handle_probe_req(struct hostapd_dat
652 #endif /* CONFIG_INTERWORKING */
654 + if (hostapd_ubus_handle_event(hapd, &req)) {
655 + wpa_printf(MSG_DEBUG, "Probe request for " MACSTR " rejected by ubus handler.\n",
656 + MAC2STR(mgmt->sa));
660 /* TODO: verify that supp_rates contains at least one matching rate
661 * with AP configuration */
663 --- a/src/ap/beacon.h
664 +++ b/src/ap/beacon.h
665 @@ -20,7 +20,7 @@ struct ieee80211_mgmt;
667 void handle_probe_req(struct hostapd_data *hapd,
668 const struct ieee80211_mgmt *mgmt, size_t len,
670 + struct hostapd_frame_info *fi);
671 void ieee802_11_set_beacon(struct hostapd_data *hapd);
672 void ieee802_11_set_beacons(struct hostapd_iface *iface);
673 void ieee802_11_update_beacons(struct hostapd_iface *iface);