hostapd: return with 80211 codes in handle event function
[oweals/openwrt.git] / package / network / services / hostapd / patches / 600-ubus_support.patch
1 --- a/hostapd/Makefile
2 +++ b/hostapd/Makefile
3 @@ -165,6 +165,11 @@ OBJS += ../src/common/hw_features_common
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 @@ -13,6 +13,7 @@
18  #include "utils/list.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 @@ -122,6 +123,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         unsigned int disabled:1;
33 @@ -370,6 +372,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 @@ -518,6 +522,7 @@ hostapd_alloc_bss_data(struct hostapd_if
43                        struct hostapd_bss_config *bss);
44  int hostapd_setup_interface(struct hostapd_iface *iface);
45  int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err);
46 +void hostapd_set_own_neighbor_report(struct hostapd_data *hapd);
47  void hostapd_interface_deinit(struct hostapd_iface *iface);
48  void hostapd_interface_free(struct hostapd_iface *iface);
49  struct hostapd_iface * hostapd_alloc_iface(void);
50 --- a/src/ap/hostapd.c
51 +++ b/src/ap/hostapd.c
52 @@ -309,6 +309,7 @@ static void hostapd_free_hapd_data(struc
53         hapd->started = 0;
54  
55         wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
56 +       hostapd_ubus_free_bss(hapd);
57         iapp_deinit(hapd->iapp);
58         hapd->iapp = NULL;
59         accounting_deinit(hapd);
60 @@ -1186,6 +1187,8 @@ static int hostapd_setup_bss(struct host
61         if (hapd->driver && hapd->driver->set_operstate)
62                 hapd->driver->set_operstate(hapd->drv_priv, 1);
63  
64 +       hostapd_ubus_add_bss(hapd);
65 +
66         return 0;
67  }
68  
69 @@ -1600,7 +1603,7 @@ static enum nr_chan_width hostapd_get_nr
70  #endif /* NEED_AP_MLME */
71  
72  
73 -static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
74 +void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
75  {
76  #ifdef NEED_AP_MLME
77         u16 capab = hostapd_own_capab_info(hapd);
78 @@ -1711,6 +1714,7 @@ static int hostapd_setup_interface_compl
79         if (err)
80                 goto fail;
81  
82 +       hostapd_ubus_add_iface(iface);
83         wpa_printf(MSG_DEBUG, "Completing interface initialization");
84         if (iface->conf->channel) {
85  #ifdef NEED_AP_MLME
86 @@ -1890,6 +1894,7 @@ dfs_offload:
87  
88  fail:
89         wpa_printf(MSG_ERROR, "Interface initialization failed");
90 +       hostapd_ubus_free_iface(iface);
91         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
92         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
93  #ifdef CONFIG_FST
94 @@ -2344,6 +2349,7 @@ void hostapd_interface_deinit_free(struc
95                    (unsigned int) iface->conf->num_bss);
96         driver = iface->bss[0]->driver;
97         drv_priv = iface->bss[0]->drv_priv;
98 +       hostapd_ubus_free_iface(iface);
99         hostapd_interface_deinit(iface);
100         wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
101                    __func__, driver, drv_priv);
102 --- a/src/ap/ieee802_11.c
103 +++ b/src/ap/ieee802_11.c
104 @@ -1587,12 +1587,13 @@ ieee802_11_set_radius_info(struct hostap
105  
106  
107  static void handle_auth(struct hostapd_data *hapd,
108 -                       const struct ieee80211_mgmt *mgmt, size_t len)
109 +                       const struct ieee80211_mgmt *mgmt, size_t len,
110 +                       struct hostapd_frame_info *fi)
111  {
112         u16 auth_alg, auth_transaction, status_code;
113         u16 resp = WLAN_STATUS_SUCCESS;
114         struct sta_info *sta = NULL;
115 -       int res, reply_res;
116 +       int res, reply_res, ubus_resp;
117         u16 fc;
118         const u8 *challenge = NULL;
119         u32 session_timeout, acct_interim_interval;
120 @@ -1603,6 +1604,11 @@ static void handle_auth(struct hostapd_d
121         char *identity = NULL;
122         char *radius_cui = NULL;
123         u16 seq_ctrl;
124 +       struct hostapd_ubus_request req = {
125 +               .type = HOSTAPD_UBUS_AUTH_REQ,
126 +               .mgmt_frame = mgmt,
127 +               .frame_info = fi,
128 +       };
129  
130         if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
131                 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
132 @@ -1757,6 +1763,13 @@ static void handle_auth(struct hostapd_d
133                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
134                 goto fail;
135         }
136 +       ubus_resp = hostapd_ubus_handle_event(hapd, &req);
137 +       if (ubus_resp) {
138 +               wpa_printf(MSG_DEBUG, "Station " MACSTR " rejected by ubus handler.\n",
139 +                       MAC2STR(mgmt->sa));
140 +               resp = ubus_resp > 0 ? (u16) ubus_resp : WLAN_STATUS_UNSPECIFIED_FAILURE;
141 +               goto fail;
142 +       }
143         if (res == HOSTAPD_ACL_PENDING)
144                 return;
145  
146 @@ -2870,12 +2883,12 @@ void fils_hlp_timeout(void *eloop_ctx, v
147  
148  static void handle_assoc(struct hostapd_data *hapd,
149                          const struct ieee80211_mgmt *mgmt, size_t len,
150 -                        int reassoc)
151 +                        int reassoc, struct hostapd_frame_info *fi)
152  {
153         u16 capab_info, listen_interval, seq_ctrl, fc;
154         u16 resp = WLAN_STATUS_SUCCESS, reply_res;
155         const u8 *pos;
156 -       int left, i;
157 +       int left, i, ubus_resp;
158         struct sta_info *sta;
159         u8 *tmp = NULL;
160         struct hostapd_sta_wpa_psk_short *psk = NULL;
161 @@ -2884,6 +2897,11 @@ static void handle_assoc(struct hostapd_
162  #ifdef CONFIG_FILS
163         int delay_assoc = 0;
164  #endif /* CONFIG_FILS */
165 +       struct hostapd_ubus_request req = {
166 +               .type = HOSTAPD_UBUS_ASSOC_REQ,
167 +               .mgmt_frame = mgmt,
168 +               .frame_info = fi,
169 +       };
170  
171         if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
172                                       sizeof(mgmt->u.assoc_req))) {
173 @@ -3051,6 +3069,14 @@ static void handle_assoc(struct hostapd_
174         }
175  #endif /* CONFIG_MBO */
176  
177 +       ubus_resp = hostapd_ubus_handle_event(hapd, &req);
178 +       if (ubus_resp) {
179 +               wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
180 +                      MAC2STR(mgmt->sa));
181 +               resp = ubus_resp > 0 ? (u16) ubus_resp : WLAN_STATUS_UNSPECIFIED_FAILURE;
182 +               goto fail;
183 +       }
184 +
185         /*
186          * sta->capability is used in check_assoc_ies() for RRM enabled
187          * capability element.
188 @@ -3258,6 +3284,7 @@ static void handle_disassoc(struct hosta
189         wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
190                    MAC2STR(mgmt->sa),
191                    le_to_host16(mgmt->u.disassoc.reason_code));
192 +       hostapd_ubus_notify(hapd, "disassoc", mgmt->sa);
193  
194         sta = ap_get_sta(hapd, mgmt->sa);
195         if (sta == NULL) {
196 @@ -3323,6 +3350,8 @@ static void handle_deauth(struct hostapd
197                 " reason_code=%d",
198                 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
199  
200 +       hostapd_ubus_notify(hapd, "deauth", mgmt->sa);
201 +
202         sta = ap_get_sta(hapd, mgmt->sa);
203         if (sta == NULL) {
204                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
205 @@ -3637,7 +3666,7 @@ int ieee802_11_mgmt(struct hostapd_data
206  
207  
208         if (stype == WLAN_FC_STYPE_PROBE_REQ) {
209 -               handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
210 +               handle_probe_req(hapd, mgmt, len, fi);
211                 return 1;
212         }
213  
214 @@ -3657,17 +3686,17 @@ int ieee802_11_mgmt(struct hostapd_data
215         switch (stype) {
216         case WLAN_FC_STYPE_AUTH:
217                 wpa_printf(MSG_DEBUG, "mgmt::auth");
218 -               handle_auth(hapd, mgmt, len);
219 +               handle_auth(hapd, mgmt, len, fi);
220                 ret = 1;
221                 break;
222         case WLAN_FC_STYPE_ASSOC_REQ:
223                 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
224 -               handle_assoc(hapd, mgmt, len, 0);
225 +               handle_assoc(hapd, mgmt, len, 0, fi);
226                 ret = 1;
227                 break;
228         case WLAN_FC_STYPE_REASSOC_REQ:
229                 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
230 -               handle_assoc(hapd, mgmt, len, 1);
231 +               handle_assoc(hapd, mgmt, len, 1, fi);
232                 ret = 1;
233                 break;
234         case WLAN_FC_STYPE_DISASSOC:
235 --- a/src/ap/beacon.c
236 +++ b/src/ap/beacon.c
237 @@ -716,7 +716,7 @@ void sta_track_claim_taxonomy_info(struc
238  
239  void handle_probe_req(struct hostapd_data *hapd,
240                       const struct ieee80211_mgmt *mgmt, size_t len,
241 -                     int ssi_signal)
242 +                     struct hostapd_frame_info *fi)
243  {
244         u8 *resp;
245         struct ieee802_11_elems elems;
246 @@ -725,9 +725,15 @@ void handle_probe_req(struct hostapd_dat
247         size_t i, resp_len;
248         int noack;
249         enum ssid_match_result res;
250 +       int ssi_signal = fi->ssi_signal;
251         int ret;
252         u16 csa_offs[2];
253         size_t csa_offs_len;
254 +       struct hostapd_ubus_request req = {
255 +               .type = HOSTAPD_UBUS_PROBE_REQ,
256 +               .mgmt_frame = mgmt,
257 +               .frame_info = fi,
258 +       };
259  
260         if (len < IEEE80211_HDRLEN)
261                 return;
262 @@ -894,6 +900,12 @@ void handle_probe_req(struct hostapd_dat
263         }
264  #endif /* CONFIG_P2P */
265  
266 +       if (hostapd_ubus_handle_event(hapd, &req)) {
267 +               wpa_printf(MSG_DEBUG, "Probe request for " MACSTR " rejected by ubus handler.\n",
268 +                      MAC2STR(mgmt->sa));
269 +               return;
270 +       }
271 +
272         /* TODO: verify that supp_rates contains at least one matching rate
273          * with AP configuration */
274  
275 --- a/src/ap/beacon.h
276 +++ b/src/ap/beacon.h
277 @@ -14,7 +14,7 @@ struct ieee80211_mgmt;
278  
279  void handle_probe_req(struct hostapd_data *hapd,
280                       const struct ieee80211_mgmt *mgmt, size_t len,
281 -                     int ssi_signal);
282 +                     struct hostapd_frame_info *fi);
283  int ieee802_11_set_beacon(struct hostapd_data *hapd);
284  int ieee802_11_set_beacons(struct hostapd_iface *iface);
285  int ieee802_11_update_beacons(struct hostapd_iface *iface);
286 --- a/src/ap/drv_callbacks.c
287 +++ b/src/ap/drv_callbacks.c
288 @@ -116,6 +116,10 @@ int hostapd_notif_assoc(struct hostapd_d
289         u16 reason = WLAN_REASON_UNSPECIFIED;
290         u16 status = WLAN_STATUS_SUCCESS;
291         const u8 *p2p_dev_addr = NULL;
292 +       struct hostapd_ubus_request req = {
293 +               .type = HOSTAPD_UBUS_ASSOC_REQ,
294 +               .addr = addr,
295 +       };
296  
297         if (addr == NULL) {
298                 /*
299 @@ -195,6 +199,12 @@ int hostapd_notif_assoc(struct hostapd_d
300                 goto fail;
301         }
302  
303 +       if (hostapd_ubus_handle_event(hapd, &req)) {
304 +               wpa_printf(MSG_DEBUG, "Station " MACSTR " assoc rejected by ubus handler.\n",
305 +                          MAC2STR(req.addr));
306 +               goto fail;
307 +       }
308 +
309  #ifdef CONFIG_P2P
310         if (elems.p2p) {
311                 wpabuf_free(sta->p2p_ie);
312 --- a/src/ap/sta_info.c
313 +++ b/src/ap/sta_info.c
314 @@ -404,6 +404,7 @@ void ap_handle_timer(void *eloop_ctx, vo
315                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
316                                "local deauth request");
317                 ap_free_sta(hapd, sta);
318 +               hostapd_ubus_notify(hapd, "local-deauth", sta->addr);
319                 return;
320         }
321  
322 @@ -551,6 +552,7 @@ skip_poll:
323                         hapd, sta,
324                         WLAN_REASON_PREV_AUTH_NOT_VALID);
325                 ap_free_sta(hapd, sta);
326 +               hostapd_ubus_notify(hapd, "inactive-deauth", sta->addr);
327                 break;
328         }
329  }
330 @@ -1212,6 +1214,7 @@ void ap_sta_set_authorized(struct hostap
331                                           buf, ip_addr);
332         } else {
333                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
334 +               hostapd_ubus_notify(hapd, "disassoc", sta->addr);
335  
336                 if (hapd->msg_ctx_parent &&
337                     hapd->msg_ctx_parent != hapd->msg_ctx)
338 --- a/src/ap/wpa_auth_glue.c
339 +++ b/src/ap/wpa_auth_glue.c
340 @@ -175,6 +175,7 @@ static void hostapd_wpa_auth_psk_failure
341         struct hostapd_data *hapd = ctx;
342         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
343                 MAC2STR(addr));
344 +       hostapd_ubus_notify(hapd, "key-mismatch", addr);
345  }
346  
347