3 @@ -116,6 +116,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 ifdef CONFIG_CODE_COVERAGE
14 CFLAGS += -O0 -fprofile-arcs -ftest-coverage
15 --- a/src/ap/hostapd.h
16 +++ b/src/ap/hostapd.h
18 #include "common/defs.h"
19 #include "ap_config.h"
20 #include "drivers/driver.h"
24 struct radius_server_data;
25 @@ -99,6 +100,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;
33 @@ -254,6 +256,8 @@ struct hostapd_iface {
34 struct hostapd_config *conf;
35 char phy[16]; /* Name of the PHY (radio) */
37 + struct hostapd_ubus_iface ubus;
39 enum hostapd_iface_state {
40 HAPD_IFACE_UNINITIALIZED,
46 + * hostapd / ubus support
47 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
49 + * This software may be distributed under the terms of the BSD license.
50 + * See README for more details.
53 +#include "utils/includes.h"
54 +#include "utils/common.h"
55 +#include "utils/eloop.h"
56 +#include "common/ieee802_11_defs.h"
58 +#include "sta_info.h"
61 +static struct ubus_context *ctx;
62 +static struct blob_buf b;
65 +struct ubus_banned_client {
66 + struct avl_node avl;
70 +static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
72 + struct ubus_context *ctx = eloop_ctx;
73 + ubus_handle_event(ctx);
76 +static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
78 + if (ubus_reconnect(ctx, NULL)) {
79 + eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
83 + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
86 +static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
88 + eloop_unregister_read_sock(ctx->sock.fd);
89 + eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
92 +static bool hostapd_ubus_init(void)
97 + ctx = ubus_connect(NULL);
101 + ctx->connection_lost = hostapd_ubus_connection_lost;
102 + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
106 +static void hostapd_ubus_ref_inc(void)
111 +static void hostapd_ubus_ref_dec(void)
120 + eloop_unregister_read_sock(ctx->sock.fd);
125 +void hostapd_ubus_add_iface(struct hostapd_iface *iface)
127 + if (!hostapd_ubus_init())
131 +void hostapd_ubus_free_iface(struct hostapd_iface *iface)
138 +hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
140 + struct ubus_banned_client *ban = eloop_data;
141 + struct hostapd_data *hapd = user_ctx;
143 + avl_delete(&hapd->ubus.banned, &ban->avl);
148 +hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
150 + struct ubus_banned_client *ban;
155 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
160 + ban = os_zalloc(sizeof(*ban));
161 + memcpy(ban->addr, addr, sizeof(ban->addr));
162 + ban->avl.key = ban->addr;
163 + avl_insert(&hapd->ubus.banned, &ban->avl);
165 + eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
167 + hostapd_bss_del_ban(ban, hapd);
172 + eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
176 +hostapd_bss_get_clients(struct ubus_context *ctx, struct ubus_object *obj,
177 + struct ubus_request_data *req, const char *method,
178 + struct blob_attr *msg)
180 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
181 + struct sta_info *sta;
184 + static const struct {
188 + { "auth", WLAN_STA_AUTH },
189 + { "assoc", WLAN_STA_ASSOC },
190 + { "authorized", WLAN_STA_AUTHORIZED },
191 + { "preauth", WLAN_STA_PREAUTH },
192 + { "wds", WLAN_STA_WDS },
193 + { "wmm", WLAN_STA_WMM },
194 + { "ht", WLAN_STA_HT },
195 + { "vht", WLAN_STA_VHT },
196 + { "wps", WLAN_STA_WPS },
197 + { "mfp", WLAN_STA_MFP },
200 + blob_buf_init(&b, 0);
201 + blobmsg_add_u32(&b, "freq", hapd->iface->freq);
202 + list = blobmsg_open_table(&b, "clients");
203 + for (sta = hapd->sta_list; sta; sta = sta->next) {
206 + sprintf(mac_buf, MACSTR, MAC2STR(sta->addr));
207 + c = blobmsg_open_table(&b, mac_buf);
208 + for (i = 0; i < ARRAY_SIZE(sta_flags); i++)
209 + blobmsg_add_u8(&b, sta_flags[i].name,
210 + !!(sta->flags & sta_flags[i].flag));
211 + blobmsg_add_u32(&b, "aid", sta->aid);
212 + blobmsg_close_table(&b, c);
214 + blobmsg_close_array(&b, list);
215 + ubus_send_reply(ctx, req, b.head);
224 + DEL_CLIENT_BAN_TIME,
228 +static const struct blobmsg_policy del_policy[__DEL_CLIENT_MAX] = {
229 + [DEL_CLIENT_ADDR] = { "addr", BLOBMSG_TYPE_STRING },
230 + [DEL_CLIENT_REASON] = { "reason", BLOBMSG_TYPE_INT32 },
231 + [DEL_CLIENT_DEAUTH] = { "deauth", BLOBMSG_TYPE_INT8 },
232 + [DEL_CLIENT_BAN_TIME] = { "ban_time", BLOBMSG_TYPE_INT32 },
236 +hostapd_bss_del_client(struct ubus_context *ctx, struct ubus_object *obj,
237 + struct ubus_request_data *req, const char *method,
238 + struct blob_attr *msg)
240 + struct blob_attr *tb[__DEL_CLIENT_MAX];
241 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
242 + struct sta_info *sta;
243 + bool deauth = false;
247 + blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
249 + if (!tb[DEL_CLIENT_ADDR])
250 + return UBUS_STATUS_INVALID_ARGUMENT;
252 + if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
253 + return UBUS_STATUS_INVALID_ARGUMENT;
255 + if (tb[DEL_CLIENT_REASON])
256 + reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
258 + if (tb[DEL_CLIENT_DEAUTH])
259 + deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
261 + sta = ap_get_sta(hapd, addr);
264 + hostapd_drv_sta_deauth(hapd, addr, reason);
265 + ap_sta_deauthenticate(hapd, sta, reason);
267 + hostapd_drv_sta_disassoc(hapd, addr, reason);
268 + ap_sta_disassociate(hapd, sta, reason);
272 + if (tb[DEL_CLIENT_BAN_TIME])
273 + hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
279 +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
283 + s = blobmsg_alloc_string_buffer(buf, name, 20);
284 + sprintf(s, MACSTR, MAC2STR(addr));
285 + blobmsg_add_string_buffer(buf);
289 +hostapd_bss_list_bans(struct ubus_context *ctx, struct ubus_object *obj,
290 + struct ubus_request_data *req, const char *method,
291 + struct blob_attr *msg)
293 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
294 + struct ubus_banned_client *ban;
297 + blob_buf_init(&b, 0);
298 + c = blobmsg_open_array(&b, "clients");
299 + avl_for_each_element(&hapd->ubus.banned, ban, avl)
300 + blobmsg_add_macaddr(&b, NULL, ban->addr);
301 + blobmsg_close_array(&b, c);
302 + ubus_send_reply(ctx, req, b.head);
307 +static const struct ubus_method bss_methods[] = {
308 + UBUS_METHOD_NOARG("get_clients", hostapd_bss_get_clients),
309 + UBUS_METHOD("del_client", hostapd_bss_del_client, del_policy),
310 + UBUS_METHOD_NOARG("list_bans", hostapd_bss_list_bans),
313 +static struct ubus_object_type bss_object_type =
314 + UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
316 +static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
318 + return memcmp(k1, k2, ETH_ALEN);
321 +void hostapd_ubus_add_bss(struct hostapd_data *hapd)
323 + struct ubus_object *obj = &hapd->ubus.obj;
327 + if (!hostapd_ubus_init())
330 + if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
333 + avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
335 + obj->type = &bss_object_type;
336 + obj->methods = bss_object_type.methods;
337 + obj->n_methods = bss_object_type.n_methods;
338 + ret = ubus_add_object(ctx, obj);
339 + hostapd_ubus_ref_inc();
342 +void hostapd_ubus_free_bss(struct hostapd_data *hapd)
344 + struct ubus_object *obj = &hapd->ubus.obj;
345 + char *name = (char *) obj->name;
351 + ubus_remove_object(ctx, obj);
352 + hostapd_ubus_ref_dec();
358 +struct ubus_event_req {
359 + struct ubus_notify_request nreq;
364 +ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
366 + struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
372 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
374 + struct ubus_banned_client *ban;
375 + const char *types[HOSTAPD_UBUS_TYPE_MAX] = {
376 + [HOSTAPD_UBUS_PROBE_REQ] = "probe",
377 + [HOSTAPD_UBUS_AUTH_REQ] = "auth",
378 + [HOSTAPD_UBUS_ASSOC_REQ] = "assoc",
380 + const char *type = "mgmt";
381 + struct ubus_event_req ureq = {};
384 + if (req->mgmt_frame)
385 + addr = req->mgmt_frame->sa;
389 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
393 + if (!hapd->ubus.obj.has_subscribers)
396 + if (req->type < ARRAY_SIZE(types))
397 + type = types[req->type];
399 + blob_buf_init(&b, 0);
400 + blobmsg_add_macaddr(&b, "address", addr);
401 + if (req->mgmt_frame)
402 + blobmsg_add_macaddr(&b, "target", req->mgmt_frame->da);
403 + if (req->frame_info)
404 + blobmsg_add_u32(&b, "signal", req->frame_info->ssi_signal);
405 + blobmsg_add_u32(&b, "freq", hapd->iface->freq);
407 + if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
410 + ureq.nreq.status_cb = ubus_event_cb;
411 + ubus_complete_request(ctx, &ureq.nreq.req, 100);
422 + * hostapd / ubus support
423 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
425 + * This software may be distributed under the terms of the BSD license.
426 + * See README for more details.
428 +#ifndef __HOSTAPD_UBUS_H
429 +#define __HOSTAPD_UBUS_H
431 +enum hostapd_ubus_event_type {
432 + HOSTAPD_UBUS_PROBE_REQ,
433 + HOSTAPD_UBUS_AUTH_REQ,
434 + HOSTAPD_UBUS_ASSOC_REQ,
435 + HOSTAPD_UBUS_TYPE_MAX
438 +struct hostapd_ubus_request {
439 + enum hostapd_ubus_event_type type;
440 + const struct ieee80211_mgmt *mgmt_frame;
441 + const struct hostapd_frame_info *frame_info;
445 +struct hostapd_iface;
446 +struct hostapd_data;
450 +#include <libubox/avl.h>
451 +#include <libubus.h>
453 +struct hostapd_ubus_iface {
454 + struct ubus_object obj;
457 +struct hostapd_ubus_bss {
458 + struct ubus_object obj;
459 + struct avl_tree banned;
462 +void hostapd_ubus_add_iface(struct hostapd_iface *iface);
463 +void hostapd_ubus_free_iface(struct hostapd_iface *iface);
464 +void hostapd_ubus_add_bss(struct hostapd_data *hapd);
465 +void hostapd_ubus_free_bss(struct hostapd_data *hapd);
467 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req);
471 +struct hostapd_ubus_iface {};
473 +struct hostapd_ubus_bss {};
475 +static inline void hostapd_ubus_add_iface(struct hostapd_iface *iface)
479 +static inline void hostapd_ubus_free_iface(struct hostapd_iface *iface)
483 +static inline void hostapd_ubus_add_bss(struct hostapd_data *hapd)
487 +static inline void hostapd_ubus_free_bss(struct hostapd_data *hapd)
491 +static inline int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
499 --- a/src/ap/hostapd.c
500 +++ b/src/ap/hostapd.c
501 @@ -261,6 +261,7 @@ static void hostapd_free_hapd_data(struc
504 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
505 + hostapd_ubus_free_bss(hapd);
506 iapp_deinit(hapd->iapp);
508 accounting_deinit(hapd);
509 @@ -864,6 +865,8 @@ static int hostapd_setup_bss(struct host
510 if (hapd->driver && hapd->driver->set_operstate)
511 hapd->driver->set_operstate(hapd->drv_priv, 1);
513 + hostapd_ubus_add_bss(hapd);
518 @@ -1154,6 +1157,7 @@ int hostapd_setup_interface_complete(str
522 + hostapd_ubus_add_iface(iface);
523 wpa_printf(MSG_DEBUG, "Completing interface initialization");
524 if (iface->conf->channel) {
526 @@ -1265,6 +1269,7 @@ int hostapd_setup_interface_complete(str
529 wpa_printf(MSG_ERROR, "Interface initialization failed");
530 + hostapd_ubus_free_iface(iface);
531 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
532 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
533 if (iface->interfaces && iface->interfaces->terminate_on_error)
534 @@ -1587,6 +1592,7 @@ void hostapd_interface_deinit_free(struc
535 (unsigned int) iface->conf->num_bss);
536 driver = iface->bss[0]->driver;
537 drv_priv = iface->bss[0]->drv_priv;
538 + hostapd_ubus_free_iface(iface);
539 hostapd_interface_deinit(iface);
540 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
541 __func__, driver, drv_priv);
542 --- a/src/ap/ieee802_11.c
543 +++ b/src/ap/ieee802_11.c
544 @@ -542,7 +542,8 @@ failed:
547 static void handle_auth(struct hostapd_data *hapd,
548 - const struct ieee80211_mgmt *mgmt, size_t len)
549 + const struct ieee80211_mgmt *mgmt, size_t len,
550 + struct hostapd_frame_info *fi)
552 u16 auth_alg, auth_transaction, status_code;
553 u16 resp = WLAN_STATUS_SUCCESS;
554 @@ -557,6 +558,11 @@ static void handle_auth(struct hostapd_d
555 size_t resp_ies_len = 0;
556 char *identity = NULL;
557 char *radius_cui = NULL;
558 + struct hostapd_ubus_request req = {
559 + .type = HOSTAPD_UBUS_AUTH_REQ,
560 + .mgmt_frame = mgmt,
564 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
565 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
566 @@ -640,6 +646,14 @@ static void handle_auth(struct hostapd_d
567 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
571 + if (hostapd_ubus_handle_event(hapd, &req)) {
572 + wpa_printf(MSG_DEBUG, "Station " MACSTR " rejected by ubus handler.\n",
573 + MAC2STR(mgmt->sa));
574 + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
578 if (res == HOSTAPD_ACL_PENDING) {
579 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
580 " waiting for an external authentication",
581 @@ -1274,13 +1288,18 @@ static void send_assoc_resp(struct hosta
583 static void handle_assoc(struct hostapd_data *hapd,
584 const struct ieee80211_mgmt *mgmt, size_t len,
586 + int reassoc, struct hostapd_frame_info *fi)
588 u16 capab_info, listen_interval;
589 u16 resp = WLAN_STATUS_SUCCESS;
592 struct sta_info *sta;
593 + struct hostapd_ubus_request req = {
594 + .type = HOSTAPD_UBUS_ASSOC_REQ,
595 + .mgmt_frame = mgmt,
599 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
600 sizeof(mgmt->u.assoc_req))) {
601 @@ -1379,6 +1398,13 @@ static void handle_assoc(struct hostapd_
605 + if (hostapd_ubus_handle_event(hapd, &req)) {
606 + wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
607 + MAC2STR(mgmt->sa));
608 + resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
612 sta->capability = capab_info;
613 sta->listen_interval = listen_interval;
615 @@ -1760,7 +1786,7 @@ int ieee802_11_mgmt(struct hostapd_data
618 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
619 - handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
620 + handle_probe_req(hapd, mgmt, len, fi);
624 @@ -1775,17 +1801,17 @@ int ieee802_11_mgmt(struct hostapd_data
626 case WLAN_FC_STYPE_AUTH:
627 wpa_printf(MSG_DEBUG, "mgmt::auth");
628 - handle_auth(hapd, mgmt, len);
629 + handle_auth(hapd, mgmt, len, fi);
632 case WLAN_FC_STYPE_ASSOC_REQ:
633 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
634 - handle_assoc(hapd, mgmt, len, 0);
635 + handle_assoc(hapd, mgmt, len, 0, fi);
638 case WLAN_FC_STYPE_REASSOC_REQ:
639 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
640 - handle_assoc(hapd, mgmt, len, 1);
641 + handle_assoc(hapd, mgmt, len, 1, fi);
644 case WLAN_FC_STYPE_DISASSOC:
645 --- a/src/ap/beacon.c
646 +++ b/src/ap/beacon.c
647 @@ -498,7 +498,7 @@ static enum ssid_match_result ssid_match
649 void handle_probe_req(struct hostapd_data *hapd,
650 const struct ieee80211_mgmt *mgmt, size_t len,
652 + struct hostapd_frame_info *fi)
655 struct ieee802_11_elems elems;
656 @@ -506,8 +506,14 @@ void handle_probe_req(struct hostapd_dat
658 struct sta_info *sta = NULL;
660 + int ssi_signal = fi->ssi_signal;
662 enum ssid_match_result res;
663 + struct hostapd_ubus_request req = {
664 + .type = HOSTAPD_UBUS_PROBE_REQ,
665 + .mgmt_frame = mgmt,
669 ie = mgmt->u.probe_req.variable;
670 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
671 @@ -644,6 +650,12 @@ void handle_probe_req(struct hostapd_dat
673 #endif /* CONFIG_P2P */
675 + if (hostapd_ubus_handle_event(hapd, &req)) {
676 + wpa_printf(MSG_DEBUG, "Probe request for " MACSTR " rejected by ubus handler.\n",
677 + MAC2STR(mgmt->sa));
681 /* TODO: verify that supp_rates contains at least one matching rate
682 * with AP configuration */
684 --- a/src/ap/beacon.h
685 +++ b/src/ap/beacon.h
686 @@ -14,7 +14,7 @@ struct ieee80211_mgmt;
688 void handle_probe_req(struct hostapd_data *hapd,
689 const struct ieee80211_mgmt *mgmt, size_t len,
691 + struct hostapd_frame_info *fi);
692 int ieee802_11_set_beacon(struct hostapd_data *hapd);
693 int ieee802_11_set_beacons(struct hostapd_iface *iface);
694 int ieee802_11_update_beacons(struct hostapd_iface *iface);