3 @@ -117,6 +117,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 @@ -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;
33 @@ -275,6 +277,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 "wps_hostapd.h"
59 +#include "sta_info.h"
62 +static struct ubus_context *ctx;
63 +static struct blob_buf b;
66 +struct ubus_banned_client {
67 + struct avl_node avl;
71 +static void ubus_receive(int sock, void *eloop_ctx, void *sock_ctx)
73 + struct ubus_context *ctx = eloop_ctx;
74 + ubus_handle_event(ctx);
77 +static void ubus_reconnect_timeout(void *eloop_data, void *user_ctx)
79 + if (ubus_reconnect(ctx, NULL)) {
80 + eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
84 + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
87 +static void hostapd_ubus_connection_lost(struct ubus_context *ctx)
89 + eloop_unregister_read_sock(ctx->sock.fd);
90 + eloop_register_timeout(1, 0, ubus_reconnect_timeout, ctx, NULL);
93 +static bool hostapd_ubus_init(void)
98 + ctx = ubus_connect(NULL);
102 + ctx->connection_lost = hostapd_ubus_connection_lost;
103 + eloop_register_read_sock(ctx->sock.fd, ubus_receive, ctx, NULL);
107 +static void hostapd_ubus_ref_inc(void)
112 +static void hostapd_ubus_ref_dec(void)
121 + eloop_unregister_read_sock(ctx->sock.fd);
126 +void hostapd_ubus_add_iface(struct hostapd_iface *iface)
128 + if (!hostapd_ubus_init())
132 +void hostapd_ubus_free_iface(struct hostapd_iface *iface)
139 +hostapd_bss_del_ban(void *eloop_data, void *user_ctx)
141 + struct ubus_banned_client *ban = eloop_data;
142 + struct hostapd_data *hapd = user_ctx;
144 + avl_delete(&hapd->ubus.banned, &ban->avl);
149 +hostapd_bss_ban_client(struct hostapd_data *hapd, u8 *addr, int time)
151 + struct ubus_banned_client *ban;
156 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
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);
166 + eloop_cancel_timeout(hostapd_bss_del_ban, ban, hapd);
168 + hostapd_bss_del_ban(ban, hapd);
173 + eloop_register_timeout(0, time * 1000, hostapd_bss_del_ban, ban, hapd);
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)
181 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
182 + struct sta_info *sta;
185 + static const struct {
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 },
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) {
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);
215 + blobmsg_close_array(&b, list);
216 + ubus_send_reply(ctx, req, b.head);
225 + DEL_CLIENT_BAN_TIME,
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 },
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)
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;
248 + blobmsg_parse(del_policy, __DEL_CLIENT_MAX, tb, blob_data(msg), blob_len(msg));
250 + if (!tb[DEL_CLIENT_ADDR])
251 + return UBUS_STATUS_INVALID_ARGUMENT;
253 + if (hwaddr_aton(blobmsg_data(tb[DEL_CLIENT_ADDR]), addr))
254 + return UBUS_STATUS_INVALID_ARGUMENT;
256 + if (tb[DEL_CLIENT_REASON])
257 + reason = blobmsg_get_u32(tb[DEL_CLIENT_REASON]);
259 + if (tb[DEL_CLIENT_DEAUTH])
260 + deauth = blobmsg_get_bool(tb[DEL_CLIENT_DEAUTH]);
262 + sta = ap_get_sta(hapd, addr);
265 + hostapd_drv_sta_deauth(hapd, addr, reason);
266 + ap_sta_deauthenticate(hapd, sta, reason);
268 + hostapd_drv_sta_disassoc(hapd, addr, reason);
269 + ap_sta_disassociate(hapd, sta, reason);
273 + if (tb[DEL_CLIENT_BAN_TIME])
274 + hostapd_bss_ban_client(hapd, addr, blobmsg_get_u32(tb[DEL_CLIENT_BAN_TIME]));
280 +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const u8 *addr)
284 + s = blobmsg_alloc_string_buffer(buf, name, 20);
285 + sprintf(s, MACSTR, MAC2STR(addr));
286 + blobmsg_add_string_buffer(buf);
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)
294 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
295 + struct ubus_banned_client *ban;
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);
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)
314 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
316 + rc = hostapd_wps_button_pushed(hapd, NULL);
319 + return UBUS_STATUS_NOT_SUPPORTED;
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)
330 + struct hostapd_data *hapd = container_of(obj, struct hostapd_data, ubus.obj);
332 + rc = hostapd_wps_cancel(hapd);
335 + return UBUS_STATUS_NOT_SUPPORTED;
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),
348 +static struct ubus_object_type bss_object_type =
349 + UBUS_OBJECT_TYPE("hostapd_bss", bss_methods);
351 +static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
353 + return memcmp(k1, k2, ETH_ALEN);
356 +void hostapd_ubus_add_bss(struct hostapd_data *hapd)
358 + struct ubus_object *obj = &hapd->ubus.obj;
362 + if (!hostapd_ubus_init())
365 + if (asprintf(&name, "hostapd.%s", hapd->conf->iface) < 0)
368 + avl_init(&hapd->ubus.banned, avl_compare_macaddr, false, NULL);
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();
377 +void hostapd_ubus_free_bss(struct hostapd_data *hapd)
379 + struct ubus_object *obj = &hapd->ubus.obj;
380 + char *name = (char *) obj->name;
386 + ubus_remove_object(ctx, obj);
387 + hostapd_ubus_ref_dec();
393 +struct ubus_event_req {
394 + struct ubus_notify_request nreq;
399 +ubus_event_cb(struct ubus_notify_request *req, int idx, int ret)
401 + struct ubus_event_req *ureq = container_of(req, struct ubus_event_req, nreq);
407 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
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",
415 + const char *type = "mgmt";
416 + struct ubus_event_req ureq = {};
419 + if (req->mgmt_frame)
420 + addr = req->mgmt_frame->sa;
424 + ban = avl_find_element(&hapd->ubus.banned, addr, ban, avl);
428 + if (!hapd->ubus.obj.has_subscribers)
431 + if (req->type < ARRAY_SIZE(types))
432 + type = types[req->type];
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);
442 + if (ubus_notify_async(ctx, &hapd->ubus.obj, type, b.head, &ureq.nreq))
445 + ureq.nreq.status_cb = ubus_event_cb;
446 + ubus_complete_request(ctx, &ureq.nreq.req, 100);
457 + * hostapd / ubus support
458 + * Copyright (c) 2013, Felix Fietkau <nbd@openwrt.org>
460 + * This software may be distributed under the terms of the BSD license.
461 + * See README for more details.
463 +#ifndef __HOSTAPD_UBUS_H
464 +#define __HOSTAPD_UBUS_H
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
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;
480 +struct hostapd_iface;
481 +struct hostapd_data;
485 +#include <libubox/avl.h>
486 +#include <libubus.h>
488 +struct hostapd_ubus_iface {
489 + struct ubus_object obj;
492 +struct hostapd_ubus_bss {
493 + struct ubus_object obj;
494 + struct avl_tree banned;
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);
502 +int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req);
506 +struct hostapd_ubus_iface {};
508 +struct hostapd_ubus_bss {};
510 +static inline void hostapd_ubus_add_iface(struct hostapd_iface *iface)
514 +static inline void hostapd_ubus_free_iface(struct hostapd_iface *iface)
518 +static inline void hostapd_ubus_add_bss(struct hostapd_data *hapd)
522 +static inline void hostapd_ubus_free_bss(struct hostapd_data *hapd)
526 +static inline int hostapd_ubus_handle_event(struct hostapd_data *hapd, struct hostapd_ubus_request *req)
534 --- a/src/ap/hostapd.c
535 +++ b/src/ap/hostapd.c
536 @@ -263,6 +263,7 @@ static void hostapd_free_hapd_data(struc
539 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
540 + hostapd_ubus_free_bss(hapd);
541 iapp_deinit(hapd->iapp);
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);
548 + hostapd_ubus_add_bss(hapd);
553 @@ -1192,6 +1195,7 @@ int hostapd_setup_interface_complete(str
557 + hostapd_ubus_add_iface(iface);
558 wpa_printf(MSG_DEBUG, "Completing interface initialization");
559 if (iface->conf->channel) {
561 @@ -1322,6 +1326,7 @@ int hostapd_setup_interface_complete(str
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:
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)
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,
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;
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;
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
618 static void handle_assoc(struct hostapd_data *hapd,
619 const struct ieee80211_mgmt *mgmt, size_t len,
621 + int reassoc, struct hostapd_frame_info *fi)
623 u16 capab_info, listen_interval;
624 u16 resp = WLAN_STATUS_SUCCESS;
627 struct sta_info *sta;
628 + struct hostapd_ubus_request req = {
629 + .type = HOSTAPD_UBUS_ASSOC_REQ,
630 + .mgmt_frame = mgmt,
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_
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;
647 sta->capability = capab_info;
648 sta->listen_interval = listen_interval;
650 @@ -1785,7 +1811,7 @@ int ieee802_11_mgmt(struct hostapd_data
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);
659 @@ -1800,17 +1826,17 @@ int ieee802_11_mgmt(struct hostapd_data
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);
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);
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);
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
684 void handle_probe_req(struct hostapd_data *hapd,
685 const struct ieee80211_mgmt *mgmt, size_t len,
687 + struct hostapd_frame_info *fi)
690 struct ieee802_11_elems elems;
691 @@ -521,8 +521,14 @@ void handle_probe_req(struct hostapd_dat
693 struct sta_info *sta = NULL;
695 + int ssi_signal = fi->ssi_signal;
697 enum ssid_match_result res;
698 + struct hostapd_ubus_request req = {
699 + .type = HOSTAPD_UBUS_PROBE_REQ,
700 + .mgmt_frame = mgmt,
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
708 #endif /* CONFIG_P2P */
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));
716 /* TODO: verify that supp_rates contains at least one matching rate
717 * with AP configuration */
719 --- a/src/ap/beacon.h
720 +++ b/src/ap/beacon.h
721 @@ -14,7 +14,7 @@ struct ieee80211_mgmt;
723 void handle_probe_req(struct hostapd_data *hapd,
724 const struct ieee80211_mgmt *mgmt, size_t len,
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);