]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/drv_callbacks.c
Update STA flags to the driver immediately on disconnection
[thirdparty/hostap.git] / src / ap / drv_callbacks.c
CommitLineData
b5b969e9
JM
1/*
2 * hostapd / Callback functions for driver wrappers
94ddef3e 3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
b5b969e9 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
b5b969e9
JM
7 */
8
6226e38d 9#include "utils/includes.h"
b5b969e9 10
6226e38d 11#include "utils/common.h"
9c47f6a2 12#include "utils/eloop.h"
b5b969e9 13#include "radius/radius.h"
6e6e8c31 14#include "drivers/driver.h"
81f4f619 15#include "common/ieee802_11_defs.h"
c41a1095 16#include "common/ieee802_11_common.h"
3140803b 17#include "common/wpa_ctrl.h"
10ec6a5f 18#include "common/dpp.h"
898b6d58 19#include "common/sae.h"
41cac481 20#include "common/hw_features_common.h"
bbb921da 21#include "crypto/random.h"
ef796391 22#include "p2p/p2p.h"
54f489be 23#include "wps/wps.h"
037378ff 24#include "fst/fst.h"
d32d94db 25#include "wnm_ap.h"
6226e38d
JM
26#include "hostapd.h"
27#include "ieee802_11.h"
0603bcb7 28#include "ieee802_11_auth.h"
6226e38d
JM
29#include "sta_info.h"
30#include "accounting.h"
31#include "tkip_countermeasures.h"
6226e38d
JM
32#include "ieee802_1x.h"
33#include "wpa_auth.h"
6226e38d 34#include "wps_hostapd.h"
51e2a27a 35#include "ap_drv_ops.h"
8b06c1ed 36#include "ap_config.h"
fa61bff6 37#include "ap_mlme.h"
1b487b8b 38#include "hw_features.h"
e76da505 39#include "dfs.h"
9c47f6a2 40#include "beacon.h"
6332aaf3 41#include "mbo_ap.h"
9c2b8204 42#include "dpp_hostapd.h"
8b5ddda5 43#include "fils_hlp.h"
6ca4949a 44#include "neighbor_db.h"
8b5ddda5
JM
45
46
47#ifdef CONFIG_FILS
48void hostapd_notify_assoc_fils_finish(struct hostapd_data *hapd,
49 struct sta_info *sta)
50{
51 u16 reply_res = WLAN_STATUS_SUCCESS;
52 struct ieee802_11_elems elems;
53 u8 buf[IEEE80211_MAX_MMPDU_SIZE], *p = buf;
54 int new_assoc;
55
56 wpa_printf(MSG_DEBUG, "%s FILS: Finish association with " MACSTR,
57 __func__, MAC2STR(sta->addr));
58 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
59 if (!sta->fils_pending_assoc_req)
60 return;
61
62 ieee802_11_parse_elems(sta->fils_pending_assoc_req,
63 sta->fils_pending_assoc_req_len, &elems, 0);
64 if (!elems.fils_session) {
65 wpa_printf(MSG_DEBUG, "%s failed to find FILS Session element",
66 __func__);
67 return;
68 }
69
70 p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
71 elems.fils_session,
72 sta->fils_hlp_resp);
73
74 reply_res = hostapd_sta_assoc(hapd, sta->addr,
75 sta->fils_pending_assoc_is_reassoc,
76 WLAN_STATUS_SUCCESS,
77 buf, p - buf);
78 ap_sta_set_authorized(hapd, sta, 1);
79 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
80 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
81 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
82 hostapd_set_sta_flags(hapd, sta);
83 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
84 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
85 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
86 os_free(sta->fils_pending_assoc_req);
87 sta->fils_pending_assoc_req = NULL;
88 sta->fils_pending_assoc_req_len = 0;
89 wpabuf_free(sta->fils_hlp_resp);
90 sta->fils_hlp_resp = NULL;
91 wpabuf_free(sta->hlp_dhcp_discover);
92 sta->hlp_dhcp_discover = NULL;
93 fils_hlp_deinit(hapd);
94
95 /*
96 * Remove the station in case transmission of a success response fails
97 * (the STA was added associated to the driver) or if the station was
98 * previously added unassociated.
99 */
100 if (reply_res != WLAN_STATUS_SUCCESS || sta->added_unassoc) {
101 hostapd_drv_sta_remove(hapd, sta->addr);
102 sta->added_unassoc = 0;
103 }
104}
105#endif /* CONFIG_FILS */
b5b969e9
JM
106
107
b5b969e9 108int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
2bb20281 109 const u8 *req_ies, size_t req_ies_len, int reassoc)
b5b969e9
JM
110{
111 struct sta_info *sta;
112 int new_assoc, res;
c41a1095 113 struct ieee802_11_elems elems;
2bb20281
JM
114 const u8 *ie;
115 size_t ielen;
88b32a99
SP
116 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
117 u8 *p = buf;
08a74e6a 118 u16 reason = WLAN_REASON_UNSPECIFIED;
88b32a99 119 u16 status = WLAN_STATUS_SUCCESS;
94ddef3e 120 const u8 *p2p_dev_addr = NULL;
b5b969e9 121
68532a9c
JM
122 if (addr == NULL) {
123 /*
124 * This could potentially happen with unexpected event from the
125 * driver wrapper. This was seen at least in one case where the
126 * driver ended up being set to station mode while hostapd was
127 * running, so better make sure we stop processing such an
128 * event here.
129 */
48b06c17
JM
130 wpa_printf(MSG_DEBUG,
131 "hostapd_notif_assoc: Skip event with no address");
68532a9c
JM
132 return -1;
133 }
d86d66dc
JM
134
135 if (is_multicast_ether_addr(addr) ||
136 is_zero_ether_addr(addr) ||
137 os_memcmp(addr, hapd->own_addr, ETH_ALEN) == 0) {
138 /* Do not process any frames with unexpected/invalid SA so that
139 * we do not add any state for unexpected STA addresses or end
140 * up sending out frames to unexpected destination. */
141 wpa_printf(MSG_DEBUG, "%s: Invalid SA=" MACSTR
142 " in received indication - ignore this indication silently",
143 __func__, MAC2STR(addr));
144 return 0;
145 }
146
bbb921da 147 random_add_randomness(addr, ETH_ALEN);
68532a9c 148
b5b969e9
JM
149 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
150 HOSTAPD_LEVEL_INFO, "associated");
151
2bb20281 152 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
c41a1095
JM
153 if (elems.wps_ie) {
154 ie = elems.wps_ie - 2;
155 ielen = elems.wps_ie_len + 2;
156 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
157 } else if (elems.rsn_ie) {
158 ie = elems.rsn_ie - 2;
159 ielen = elems.rsn_ie_len + 2;
160 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
161 } else if (elems.wpa_ie) {
162 ie = elems.wpa_ie - 2;
163 ielen = elems.wpa_ie_len + 2;
164 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
a14896e8
JM
165#ifdef CONFIG_HS20
166 } else if (elems.osen) {
167 ie = elems.osen - 2;
168 ielen = elems.osen_len + 2;
169 wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
170#endif /* CONFIG_HS20 */
c41a1095
JM
171 } else {
172 ie = NULL;
173 ielen = 0;
48b06c17
JM
174 wpa_printf(MSG_DEBUG,
175 "STA did not include WPS/RSN/WPA IE in (Re)AssocReq");
c41a1095
JM
176 }
177
b5b969e9
JM
178 sta = ap_get_sta(hapd, addr);
179 if (sta) {
4331263b 180 ap_sta_no_session_timeout(hapd, sta);
b5b969e9 181 accounting_sta_stop(hapd, sta);
c72bd6d4
JM
182
183 /*
184 * Make sure that the previously registered inactivity timer
185 * will not remove the STA immediately.
186 */
187 sta->timeout_next = STA_NULLFUNC;
b5b969e9
JM
188 } else {
189 sta = ap_sta_add(hapd, addr);
8bd0fc0e
JM
190 if (sta == NULL) {
191 hostapd_drv_sta_disassoc(hapd, addr,
192 WLAN_REASON_DISASSOC_AP_BUSY);
b5b969e9 193 return -1;
8bd0fc0e 194 }
b5b969e9 195 }
17f6b900 196 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
b5b969e9 197
bb4e19e3
SD
198 /*
199 * ACL configurations to the drivers (implementing AP SME and ACL
200 * offload) without hostapd's knowledge, can result in a disconnection
201 * though the driver accepts the connection. Skip the hostapd check for
202 * ACL if the driver supports ACL offload to avoid potentially
203 * conflicting ACL rules.
204 */
205 if (hapd->iface->drv_max_acl_mac_addrs == 0 &&
206 hostapd_check_acl(hapd, addr, NULL) != HOSTAPD_ACL_ACCEPT) {
0603bcb7
AN
207 wpa_printf(MSG_INFO, "STA " MACSTR " not allowed to connect",
208 MAC2STR(addr));
209 reason = WLAN_REASON_UNSPECIFIED;
210 goto fail;
211 }
212
b305c684
JM
213#ifdef CONFIG_P2P
214 if (elems.p2p) {
215 wpabuf_free(sta->p2p_ie);
2bb20281 216 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
b305c684 217 P2P_IE_VENDOR_TYPE);
94ddef3e
JM
218 if (sta->p2p_ie)
219 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
b305c684
JM
220 }
221#endif /* CONFIG_P2P */
222
9c47f6a2
PX
223#ifdef NEED_AP_MLME
224 if (elems.ht_capabilities &&
9c47f6a2
PX
225 (hapd->iface->conf->ht_capab &
226 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
227 struct ieee80211_ht_capabilities *ht_cap =
228 (struct ieee80211_ht_capabilities *)
229 elems.ht_capabilities;
230
231 if (le_to_host16(ht_cap->ht_capabilities_info) &
232 HT_CAP_INFO_40MHZ_INTOLERANT)
233 ht40_intolerant_add(hapd->iface, sta);
234 }
235#endif /* NEED_AP_MLME */
9c47f6a2 236
c551700f
KP
237#ifdef CONFIG_INTERWORKING
238 if (elems.ext_capab && elems.ext_capab_len > 4) {
239 if (elems.ext_capab[4] & 0x01)
240 sta->qos_map_enabled = 1;
241 }
242#endif /* CONFIG_INTERWORKING */
243
f403dcd6
JM
244#ifdef CONFIG_HS20
245 wpabuf_free(sta->hs20_ie);
246 if (elems.hs20 && elems.hs20_len > 4) {
247 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
248 elems.hs20_len - 4);
249 } else
250 sta->hs20_ie = NULL;
67cca346
JM
251
252 wpabuf_free(sta->roaming_consortium);
253 if (elems.roaming_cons_sel)
254 sta->roaming_consortium = wpabuf_alloc_copy(
255 elems.roaming_cons_sel + 4,
256 elems.roaming_cons_sel_len - 4);
257 else
258 sta->roaming_consortium = NULL;
f403dcd6
JM
259#endif /* CONFIG_HS20 */
260
ae667c08
AN
261#ifdef CONFIG_FST
262 wpabuf_free(sta->mb_ies);
263 if (hapd->iface->fst)
264 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
265 else
266 sta->mb_ies = NULL;
267#endif /* CONFIG_FST */
268
6332aaf3
JM
269 mbo_ap_check_sta_assoc(hapd, sta, &elems);
270
adf0478e
JM
271 ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
272 elems.supp_op_classes_len);
273
b5b969e9
JM
274 if (hapd->conf->wpa) {
275 if (ie == NULL || ielen == 0) {
633d4469 276#ifdef CONFIG_WPS
b5b969e9 277 if (hapd->conf->wps_state) {
48b06c17
JM
278 wpa_printf(MSG_DEBUG,
279 "STA did not include WPA/RSN IE in (Re)Association Request - possible WPS use");
b5b969e9
JM
280 sta->flags |= WLAN_STA_MAYBE_WPS;
281 goto skip_wpa_check;
282 }
633d4469 283#endif /* CONFIG_WPS */
b5b969e9
JM
284
285 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
63dc0f9c
HW
286 reason = WLAN_REASON_INVALID_IE;
287 status = WLAN_STATUS_INVALID_IE;
288 goto fail;
b5b969e9 289 }
633d4469 290#ifdef CONFIG_WPS
b5b969e9
JM
291 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
292 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
17f6b900 293 struct wpabuf *wps;
48b06c17 294
b5b969e9 295 sta->flags |= WLAN_STA_WPS;
17f6b900
JM
296 wps = ieee802_11_vendor_ie_concat(ie, ielen,
297 WPS_IE_VENDOR_TYPE);
298 if (wps) {
299 if (wps_is_20(wps)) {
48b06c17
JM
300 wpa_printf(MSG_DEBUG,
301 "WPS: STA supports WPS 2.0");
17f6b900
JM
302 sta->flags |= WLAN_STA_WPS2;
303 }
304 wpabuf_free(wps);
305 }
b5b969e9
JM
306 goto skip_wpa_check;
307 }
633d4469 308#endif /* CONFIG_WPS */
b5b969e9
JM
309
310 if (sta->wpa_sm == NULL)
311 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e
JM
312 sta->addr,
313 p2p_dev_addr);
b5b969e9 314 if (sta->wpa_sm == NULL) {
48b06c17
JM
315 wpa_printf(MSG_ERROR,
316 "Failed to initialize WPA state machine");
b5b969e9
JM
317 return -1;
318 }
319 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
2c129a1b 320 hapd->iface->freq,
88b32a99 321 ie, ielen,
d3516cad
JM
322 elems.rsnxe ? elems.rsnxe - 2 : NULL,
323 elems.rsnxe ? elems.rsnxe_len + 2 : 0,
09368515
JM
324 elems.mdie, elems.mdie_len,
325 elems.owe_dh, elems.owe_dh_len);
b5b969e9 326 if (res != WPA_IE_OK) {
48b06c17
JM
327 wpa_printf(MSG_DEBUG,
328 "WPA/RSN information element rejected? (res %u)",
329 res);
b5b969e9 330 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
88b32a99 331 if (res == WPA_INVALID_GROUP) {
08a74e6a 332 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
333 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
334 } else if (res == WPA_INVALID_PAIRWISE) {
08a74e6a 335 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
88b32a99
SP
336 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
337 } else if (res == WPA_INVALID_AKMP) {
08a74e6a 338 reason = WLAN_REASON_AKMP_NOT_VALID;
88b32a99 339 status = WLAN_STATUS_AKMP_NOT_VALID;
7d2ed8ba 340 } else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
08a74e6a 341 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
342 status = WLAN_STATUS_INVALID_IE;
343 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
feba5848
JM
344 reason = WLAN_REASON_CIPHER_SUITE_REJECTED;
345 status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
93ba13bc
JM
346 } else if (res == WPA_INVALID_PMKID) {
347 reason = WLAN_REASON_INVALID_PMKID;
348 status = WLAN_STATUS_INVALID_PMKID;
7d2ed8ba 349 } else {
08a74e6a 350 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
351 status = WLAN_STATUS_INVALID_IE;
352 }
08a74e6a 353 goto fail;
b5b969e9 354 }
7d2ed8ba 355
edb28006
AK
356 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
357 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
358 !sta->sa_query_timed_out &&
7d9c0cd3
MP
359 sta->sa_query_count > 0)
360 ap_check_sa_query_timeout(hapd, sta);
edb28006
AK
361 if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_MFP)) ==
362 (WLAN_STA_ASSOC | WLAN_STA_MFP) &&
363 !sta->sa_query_timed_out &&
7d9c0cd3
MP
364 (sta->auth_alg != WLAN_AUTH_FT)) {
365 /*
366 * STA has already been associated with MFP and SA
367 * Query timeout has not been reached. Reject the
368 * association attempt temporarily and start SA Query,
369 * if one is not pending.
370 */
371
372 if (sta->sa_query_count == 0)
373 ap_sta_start_sa_query(hapd, sta);
374
7d9c0cd3
MP
375 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
376
377 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
378
379 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
380 p - buf);
7d9c0cd3
MP
381 return 0;
382 }
383
384 if (wpa_auth_uses_mfp(sta->wpa_sm))
385 sta->flags |= WLAN_STA_MFP;
386 else
387 sta->flags &= ~WLAN_STA_MFP;
7d9c0cd3 388
4ec1fd8e 389#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
390 if (sta->auth_alg == WLAN_AUTH_FT) {
391 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
392 req_ies_len);
393 if (status != WLAN_STATUS_SUCCESS) {
394 if (status == WLAN_STATUS_INVALID_PMKID)
395 reason = WLAN_REASON_INVALID_IE;
396 if (status == WLAN_STATUS_INVALID_MDIE)
397 reason = WLAN_REASON_INVALID_IE;
398 if (status == WLAN_STATUS_INVALID_FTIE)
399 reason = WLAN_REASON_INVALID_IE;
400 goto fail;
401 }
402 }
4ec1fd8e 403#endif /* CONFIG_IEEE80211R_AP */
898b6d58
JM
404#ifdef CONFIG_SAE
405 if (hapd->conf->sae_pwe == 2 &&
406 sta->auth_alg == WLAN_AUTH_SAE &&
407 sta->sae && sta->sae->tmp && !sta->sae->tmp->h2e &&
408 elems.rsnxe && elems.rsnxe_len >= 1 &&
409 (elems.rsnxe[0] & BIT(WLAN_RSNX_CAPAB_SAE_H2E))) {
410 wpa_printf(MSG_INFO, "SAE: " MACSTR
411 " indicates support for SAE H2E, but did not use it",
412 MAC2STR(sta->addr));
413 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
414 reason = WLAN_REASON_UNSPECIFIED;
415 goto fail;
416 }
417#endif /* CONFIG_SAE */
a9aca28b 418 } else if (hapd->conf->wps_state) {
633d4469 419#ifdef CONFIG_WPS
17f6b900 420 struct wpabuf *wps;
48b06c17 421
2bb20281
JM
422 if (req_ies)
423 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
fa15d405
JM
424 WPS_IE_VENDOR_TYPE);
425 else
426 wps = NULL;
54f489be 427#ifdef CONFIG_WPS_STRICT
fa15d405 428 if (wps && wps_validate_assoc_req(wps) < 0) {
08a74e6a 429 reason = WLAN_REASON_INVALID_IE;
88b32a99 430 status = WLAN_STATUS_INVALID_IE;
fa15d405 431 wpabuf_free(wps);
08a74e6a 432 goto fail;
54f489be 433 }
54f489be 434#endif /* CONFIG_WPS_STRICT */
fa15d405 435 if (wps) {
a9aca28b 436 sta->flags |= WLAN_STA_WPS;
fa15d405 437 if (wps_is_20(wps)) {
48b06c17
JM
438 wpa_printf(MSG_DEBUG,
439 "WPS: STA supports WPS 2.0");
17f6b900
JM
440 sta->flags |= WLAN_STA_WPS2;
441 }
a9aca28b
JM
442 } else
443 sta->flags |= WLAN_STA_MAYBE_WPS;
17f6b900 444 wpabuf_free(wps);
633d4469 445#endif /* CONFIG_WPS */
a14896e8
JM
446#ifdef CONFIG_HS20
447 } else if (hapd->conf->osen) {
448 if (elems.osen == NULL) {
449 hostapd_logger(
450 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
451 HOSTAPD_LEVEL_INFO,
452 "No HS 2.0 OSEN element in association request");
453 return WLAN_STATUS_INVALID_IE;
454 }
455
456 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
457 if (sta->wpa_sm == NULL)
458 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
459 sta->addr, NULL);
460 if (sta->wpa_sm == NULL) {
48b06c17
JM
461 wpa_printf(MSG_WARNING,
462 "Failed to initialize WPA state machine");
a14896e8
JM
463 return WLAN_STATUS_UNSPECIFIED_FAILURE;
464 }
465 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
466 elems.osen - 2, elems.osen_len + 2) < 0)
467 return WLAN_STATUS_INVALID_IE;
468#endif /* CONFIG_HS20 */
b5b969e9 469 }
7b26238d
JM
470#ifdef CONFIG_WPS
471skip_wpa_check:
472#endif /* CONFIG_WPS */
4c572281
JM
473
474#ifdef CONFIG_MBO
475 if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
476 elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
477 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
478 wpa_printf(MSG_INFO,
479 "MBO: Reject WPA2 association without PMF");
480 return WLAN_STATUS_UNSPECIFIED_FAILURE;
481 }
482#endif /* CONFIG_MBO */
483
4ec1fd8e 484#ifdef CONFIG_IEEE80211R_AP
88b32a99 485 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
b7366a94
JM
486 sta->auth_alg, req_ies, req_ies_len,
487 !elems.rsnxe);
2cf36d60
JM
488 if (!p) {
489 wpa_printf(MSG_DEBUG, "FT: Failed to write AssocResp IEs");
490 return WLAN_STATUS_UNSPECIFIED_FAILURE;
491 }
cc20edc9 492#endif /* CONFIG_IEEE80211R_AP */
88b32a99 493
fa61bff6
JM
494#ifdef CONFIG_FILS
495 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
496 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
497 sta->auth_alg == WLAN_AUTH_FILS_PK) {
8b5ddda5
JM
498 int delay_assoc = 0;
499
3de1566d
PX
500 if (!req_ies)
501 return WLAN_STATUS_UNSPECIFIED_FAILURE;
502
fa61bff6
JM
503 if (!wpa_fils_validate_fils_session(sta->wpa_sm, req_ies,
504 req_ies_len,
505 sta->fils_session)) {
506 wpa_printf(MSG_DEBUG,
507 "FILS: Session validation failed");
508 return WLAN_STATUS_UNSPECIFIED_FAILURE;
509 }
510
511 res = wpa_fils_validate_key_confirm(sta->wpa_sm, req_ies,
512 req_ies_len);
513 if (res < 0) {
514 wpa_printf(MSG_DEBUG,
515 "FILS: Key Confirm validation failed");
516 return WLAN_STATUS_UNSPECIFIED_FAILURE;
517 }
518
8b5ddda5 519 if (fils_process_hlp(hapd, sta, req_ies, req_ies_len) > 0) {
fa61bff6 520 wpa_printf(MSG_DEBUG,
8b5ddda5
JM
521 "FILS: Delaying Assoc Response (HLP)");
522 delay_assoc = 1;
523 } else {
524 wpa_printf(MSG_DEBUG,
525 "FILS: Going ahead with Assoc Response (no HLP)");
526 }
527
528 if (sta) {
529 wpa_printf(MSG_DEBUG, "FILS: HLP callback cleanup");
530 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
531 os_free(sta->fils_pending_assoc_req);
532 sta->fils_pending_assoc_req = NULL;
533 sta->fils_pending_assoc_req_len = 0;
534 wpabuf_free(sta->fils_hlp_resp);
535 sta->fils_hlp_resp = NULL;
536 sta->fils_drv_assoc_finish = 0;
fa61bff6
JM
537 }
538
8b5ddda5
JM
539 if (sta && delay_assoc && status == WLAN_STATUS_SUCCESS) {
540 u8 *req_tmp;
541
542 req_tmp = os_malloc(req_ies_len);
543 if (!req_tmp) {
544 wpa_printf(MSG_DEBUG,
545 "FILS: buffer allocation failed for assoc req");
546 goto fail;
547 }
548 os_memcpy(req_tmp, req_ies, req_ies_len);
549 sta->fils_pending_assoc_req = req_tmp;
550 sta->fils_pending_assoc_req_len = req_ies_len;
551 sta->fils_pending_assoc_is_reassoc = reassoc;
552 sta->fils_drv_assoc_finish = 1;
553 wpa_printf(MSG_DEBUG,
554 "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
555 MACSTR, MAC2STR(sta->addr));
556 eloop_register_timeout(
557 0, hapd->conf->fils_hlp_wait_time * 1024,
558 fils_hlp_timeout, hapd, sta);
559 return 0;
560 }
fa61bff6 561 p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
8b5ddda5
JM
562 elems.fils_session,
563 sta->fils_hlp_resp);
fa61bff6
JM
564 wpa_hexdump(MSG_DEBUG, "FILS Assoc Resp BUF (IEs)",
565 buf, p - buf);
566 }
567#endif /* CONFIG_FILS */
568
33c8bbd8
AKP
569#ifdef CONFIG_OWE
570 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
571 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
572 elems.owe_dh) {
04ded82e
JM
573 u8 *npos;
574
79ce2d51
AP
575 npos = owe_assoc_req_process(hapd, sta,
576 elems.owe_dh, elems.owe_dh_len,
577 p, sizeof(buf) - (p - buf),
e7d8842e 578 &status);
a4668c68
AP
579 if (npos)
580 p = npos;
e7d8842e 581
a4668c68 582 if (!npos &&
e7d8842e 583 status == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
a4668c68
AP
584 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
585 p - buf);
586 return 0;
587 }
588
e7d8842e 589 if (!npos || status != WLAN_STATUS_SUCCESS)
33c8bbd8
AKP
590 goto fail;
591 }
592#endif /* CONFIG_OWE */
593
10ec6a5f
JM
594#ifdef CONFIG_DPP2
595 dpp_pfs_free(sta->dpp_pfs);
596 sta->dpp_pfs = NULL;
597
598 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
599 hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
600 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
601 elems.owe_dh) {
602 sta->dpp_pfs = dpp_pfs_init(
603 wpabuf_head(hapd->conf->dpp_netaccesskey),
604 wpabuf_len(hapd->conf->dpp_netaccesskey));
605 if (!sta->dpp_pfs) {
606 wpa_printf(MSG_DEBUG,
607 "DPP: Could not initialize PFS");
608 /* Try to continue without PFS */
609 goto pfs_fail;
610 }
611
612 if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
613 elems.owe_dh_len) < 0) {
614 dpp_pfs_free(sta->dpp_pfs);
615 sta->dpp_pfs = NULL;
616 reason = WLAN_REASON_UNSPECIFIED;
617 goto fail;
618 }
619 }
620
621 wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
622 sta->dpp_pfs->secret : NULL);
623 pfs_fail:
624#endif /* CONFIG_DPP2 */
625
fae7e64a
O
626 if (elems.rrm_enabled &&
627 elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
628 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
629 sizeof(sta->rrm_enabled_capa));
630
33c8bbd8 631#if defined(CONFIG_IEEE80211R_AP) || defined(CONFIG_FILS) || defined(CONFIG_OWE)
88b32a99 632 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
e4474c1c 633
cc20edc9
JM
634 if (sta->auth_alg == WLAN_AUTH_FT ||
635 sta->auth_alg == WLAN_AUTH_FILS_SK ||
636 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
637 sta->auth_alg == WLAN_AUTH_FILS_PK)
e4474c1c 638 ap_sta_set_authorized(hapd, sta, 1);
cc20edc9 639#else /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
88b32a99
SP
640 /* Keep compiler silent about unused variables */
641 if (status) {
642 }
cc20edc9 643#endif /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
88b32a99 644
b5b969e9
JM
645 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
646 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
3578e665 647 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
88b32a99 648
e4474c1c
DPS
649 hostapd_set_sta_flags(hapd, sta);
650
88b32a99
SP
651 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
652 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
957bff83
JM
653#ifdef CONFIG_FILS
654 else if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
655 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
656 sta->auth_alg == WLAN_AUTH_FILS_PK)
657 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
658#endif /* CONFIG_FILS */
88b32a99
SP
659 else
660 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
b5b969e9
JM
661
662 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
663
664 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
665
ef796391 666#ifdef CONFIG_P2P
99c01af9
JM
667 if (req_ies) {
668 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
669 req_ies, req_ies_len);
670 }
ef796391
JM
671#endif /* CONFIG_P2P */
672
b5b969e9 673 return 0;
08a74e6a
JM
674
675fail:
4ec1fd8e 676#ifdef CONFIG_IEEE80211R_AP
88b32a99 677 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
4ec1fd8e 678#endif /* CONFIG_IEEE80211R_AP */
08a74e6a
JM
679 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
680 ap_free_sta(hapd, sta);
681 return -1;
b5b969e9
JM
682}
683
684
685void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
686{
687 struct sta_info *sta;
688
83e843e8
JM
689 if (addr == NULL) {
690 /*
691 * This could potentially happen with unexpected event from the
692 * driver wrapper. This was seen at least in one case where the
693 * driver ended up reporting a station mode event while hostapd
694 * was running, so better make sure we stop processing such an
695 * event here.
696 */
48b06c17
JM
697 wpa_printf(MSG_DEBUG,
698 "hostapd_notif_disassoc: Skip event with no address");
1f4c7b6b 699 return;
83e843e8
JM
700 }
701
b5b969e9
JM
702 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
703 HOSTAPD_LEVEL_INFO, "disassociated");
704
705 sta = ap_get_sta(hapd, addr);
706 if (sta == NULL) {
48b06c17
JM
707 wpa_printf(MSG_DEBUG,
708 "Disassociation notification for unknown STA "
709 MACSTR, MAC2STR(addr));
b5b969e9
JM
710 return;
711 }
712
ae055af4 713 ap_sta_set_authorized(hapd, sta, 0);
b5b969e9 714 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
b7275a81 715 hostapd_set_sta_flags(hapd, sta);
b5b969e9
JM
716 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
717 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
718 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
719 ap_free_sta(hapd, sta);
720}
721
722
0d7e5a3a
JB
723void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
724{
725 struct sta_info *sta = ap_get_sta(hapd, addr);
726
d58c3bd8 727 if (!sta || !hapd->conf->disassoc_low_ack || sta->agreed_to_steer)
0d7e5a3a
JB
728 return;
729
730 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
48b06c17
JM
731 HOSTAPD_LEVEL_INFO,
732 "disconnected due to excessive missing ACKs");
0d7e5a3a 733 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
5f99d962 734 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
0d7e5a3a
JB
735}
736
737
ec2b5173
T
738void hostapd_event_sta_opmode_changed(struct hostapd_data *hapd, const u8 *addr,
739 enum smps_mode smps_mode,
740 enum chan_width chan_width, u8 rx_nss)
741{
742 struct sta_info *sta = ap_get_sta(hapd, addr);
743 const char *txt;
744
745 if (!sta)
746 return;
747
748 switch (smps_mode) {
749 case SMPS_AUTOMATIC:
750 txt = "automatic";
751 break;
752 case SMPS_OFF:
753 txt = "off";
754 break;
755 case SMPS_DYNAMIC:
756 txt = "dynamic";
757 break;
758 case SMPS_STATIC:
759 txt = "static";
760 break;
761 default:
762 txt = NULL;
763 break;
764 }
765 if (txt) {
766 wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_SMPS_MODE_CHANGED
767 MACSTR " %s", MAC2STR(addr), txt);
768 }
769
770 switch (chan_width) {
771 case CHAN_WIDTH_20_NOHT:
772 txt = "20(no-HT)";
773 break;
774 case CHAN_WIDTH_20:
775 txt = "20";
776 break;
777 case CHAN_WIDTH_40:
778 txt = "40";
779 break;
780 case CHAN_WIDTH_80:
781 txt = "80";
782 break;
783 case CHAN_WIDTH_80P80:
784 txt = "80+80";
785 break;
786 case CHAN_WIDTH_160:
787 txt = "160";
788 break;
789 default:
790 txt = NULL;
791 break;
792 }
793 if (txt) {
794 wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_MAX_BW_CHANGED
795 MACSTR " %s", MAC2STR(addr), txt);
796 }
797
798 if (rx_nss != 0xff) {
799 wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_N_SS_CHANGED
800 MACSTR " %d", MAC2STR(addr), rx_nss);
801 }
802}
803
804
1b487b8b 805void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
95f556f3
OD
806 int offset, int width, int cf1, int cf2,
807 int finished)
1b487b8b 808{
f91e68e9
MV
809 /* TODO: If OCV is enabled deauth STAs that don't perform a SA Query */
810
c7803a02 811#ifdef NEED_AP_MLME
d308a44f
LC
812 int channel, chwidth, is_dfs;
813 u8 seg0_idx = 0, seg1_idx = 0;
6ca4949a 814 size_t i;
1b487b8b
TP
815
816 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2f06bcb3 817 HOSTAPD_LEVEL_INFO,
95f556f3
OD
818 "driver %s channel switch: freq=%d, ht=%d, vht_ch=0x%x, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
819 finished ? "had" : "starting",
bda9c085
SM
820 freq, ht, hapd->iconf->ch_switch_vht_config, offset,
821 width, channel_width_to_string(width), cf1, cf2);
1b487b8b 822
c3805fb6
JM
823 if (!hapd->iface->current_mode) {
824 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
825 HOSTAPD_LEVEL_WARNING,
826 "ignore channel switch since the interface is not yet ready");
827 return;
828 }
829
1b487b8b
TP
830 hapd->iface->freq = freq;
831
832 channel = hostapd_hw_get_channel(hapd, freq);
833 if (!channel) {
834 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
48b06c17
JM
835 HOSTAPD_LEVEL_WARNING,
836 "driver switched to bad channel!");
1b487b8b
TP
837 return;
838 }
839
8d1fdde7
JD
840 switch (width) {
841 case CHAN_WIDTH_80:
464dcfd0 842 chwidth = CHANWIDTH_80MHZ;
8d1fdde7
JD
843 break;
844 case CHAN_WIDTH_80P80:
464dcfd0 845 chwidth = CHANWIDTH_80P80MHZ;
8d1fdde7
JD
846 break;
847 case CHAN_WIDTH_160:
464dcfd0 848 chwidth = CHANWIDTH_160MHZ;
8d1fdde7
JD
849 break;
850 case CHAN_WIDTH_20_NOHT:
851 case CHAN_WIDTH_20:
852 case CHAN_WIDTH_40:
853 default:
464dcfd0 854 chwidth = CHANWIDTH_USE_HT;
8d1fdde7
JD
855 break;
856 }
857
858 switch (hapd->iface->current_mode->mode) {
859 case HOSTAPD_MODE_IEEE80211A:
860 if (cf1 > 5000)
861 seg0_idx = (cf1 - 5000) / 5;
862 if (cf2 > 5000)
863 seg1_idx = (cf2 - 5000) / 5;
864 break;
865 default:
d308a44f
LC
866 ieee80211_freq_to_chan(cf1, &seg0_idx);
867 ieee80211_freq_to_chan(cf2, &seg1_idx);
8d1fdde7
JD
868 break;
869 }
870
1b487b8b
TP
871 hapd->iconf->channel = channel;
872 hapd->iconf->ieee80211n = ht;
bda9c085 873 if (!ht) {
5de74818 874 hapd->iconf->ieee80211ac = 0;
bda9c085
SM
875 } else if (hapd->iconf->ch_switch_vht_config) {
876 /* CHAN_SWITCH VHT config */
877 if (hapd->iconf->ch_switch_vht_config &
878 CH_SWITCH_VHT_ENABLED)
879 hapd->iconf->ieee80211ac = 1;
880 else if (hapd->iconf->ch_switch_vht_config &
881 CH_SWITCH_VHT_DISABLED)
882 hapd->iconf->ieee80211ac = 0;
883 }
884 hapd->iconf->ch_switch_vht_config = 0;
885
1b487b8b 886 hapd->iconf->secondary_channel = offset;
c6b7ac07
JC
887 hostapd_set_oper_chwidth(hapd->iconf, chwidth);
888 hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, seg0_idx);
889 hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, seg1_idx);
bf281c12 890
d239ab39 891 is_dfs = ieee80211_is_dfs(freq, hapd->iface->hw_features,
892 hapd->iface->num_hw_features);
1e2aaffb 893
95f556f3
OD
894 wpa_msg(hapd->msg_ctx, MSG_INFO,
895 "%sfreq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d dfs=%d",
896 finished ? WPA_EVENT_CHANNEL_SWITCH :
897 WPA_EVENT_CHANNEL_SWITCH_STARTED,
898 freq, ht, offset, channel_width_to_string(width),
899 cf1, cf2, is_dfs);
900 if (!finished)
901 return;
902
6782b684
MK
903 if (hapd->csa_in_progress &&
904 freq == hapd->cs_freq_params.freq) {
bf281c12 905 hostapd_cleanup_cs_params(hapd);
6782b684 906 ieee802_11_set_beacon(hapd);
bf281c12 907
1e2aaffb
AK
908 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
909 "freq=%d dfs=%d", freq, is_dfs);
910 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
911 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
912 "freq=%d dfs=%d", freq, is_dfs);
bf281c12 913 }
6ca4949a
MT
914
915 for (i = 0; i < hapd->iface->num_bss; i++)
916 hostapd_neighbor_set_own_report(hapd->iface->bss[i]);
c7803a02 917#endif /* NEED_AP_MLME */
1b487b8b
TP
918}
919
920
3140803b
RM
921void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
922 const u8 *addr, int reason_code)
923{
924 switch (reason_code) {
925 case MAX_CLIENT_REACHED:
926 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
927 MAC2STR(addr));
928 break;
929 case BLOCKED_CLIENT:
930 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
931 MAC2STR(addr));
932 break;
933 }
934}
935
936
16689c7c 937#ifdef CONFIG_ACS
d9909717
TB
938void hostapd_acs_channel_selected(struct hostapd_data *hapd,
939 struct acs_selected_channels *acs_res)
16689c7c 940{
3784c058 941 int ret, i;
e1d00d47 942 int err = 0;
41cac481 943 struct hostapd_channel_data *pri_chan;
16689c7c
PX
944
945 if (hapd->iconf->channel) {
946 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
947 hapd->iconf->channel);
948 return;
949 }
950
59e33b4a
AB
951 hapd->iface->freq = acs_res->pri_freq;
952
3784c058
PX
953 if (!hapd->iface->current_mode) {
954 for (i = 0; i < hapd->iface->num_hw_features; i++) {
955 struct hostapd_hw_modes *mode =
956 &hapd->iface->hw_features[i];
957
958 if (mode->mode == acs_res->hw_mode) {
59e33b4a
AB
959 if (hapd->iface->freq > 0 &&
960 !hw_get_chan(mode->mode,
961 hapd->iface->freq,
962 hapd->iface->hw_features,
963 hapd->iface->num_hw_features))
964 continue;
3784c058
PX
965 hapd->iface->current_mode = mode;
966 break;
967 }
968 }
969 if (!hapd->iface->current_mode) {
970 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
971 HOSTAPD_LEVEL_WARNING,
972 "driver selected to bad hw_mode");
e1d00d47
PX
973 err = 1;
974 goto out;
3784c058
PX
975 }
976 }
977
41cac481 978 if (!acs_res->pri_freq) {
16689c7c
PX
979 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
980 HOSTAPD_LEVEL_WARNING,
981 "driver switched to bad channel");
e1d00d47
PX
982 err = 1;
983 goto out;
16689c7c 984 }
41cac481
AB
985 pri_chan = hw_get_channel_freq(hapd->iface->current_mode->mode,
986 acs_res->pri_freq, NULL,
987 hapd->iface->hw_features,
988 hapd->iface->num_hw_features);
989 if (!pri_chan) {
990 wpa_printf(MSG_ERROR,
991 "ACS: Could not determine primary channel number from pri_freq %u",
992 acs_res->pri_freq);
993 err = 1;
994 goto out;
995 }
16689c7c 996
41cac481 997 hapd->iconf->channel = pri_chan->chan;
857d9422 998 hapd->iconf->acs = 1;
16689c7c 999
41cac481 1000 if (acs_res->sec_freq == 0)
16689c7c 1001 hapd->iconf->secondary_channel = 0;
41cac481 1002 else if (acs_res->sec_freq < acs_res->pri_freq)
16689c7c 1003 hapd->iconf->secondary_channel = -1;
41cac481 1004 else if (acs_res->sec_freq > acs_res->pri_freq)
16689c7c
PX
1005 hapd->iconf->secondary_channel = 1;
1006 else {
1007 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
e1d00d47
PX
1008 err = 1;
1009 goto out;
16689c7c
PX
1010 }
1011
e520de8d
NS
1012 hapd->iconf->edmg_channel = acs_res->edmg_channel;
1013
1d2c45ec 1014 if (hapd->iface->conf->ieee80211ac || hapd->iface->conf->ieee80211ax) {
857d9422 1015 /* set defaults for backwards compatibility */
c6b7ac07
JC
1016 hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, 0);
1017 hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, 0);
1018 hostapd_set_oper_chwidth(hapd->iconf, CHANWIDTH_USE_HT);
4bf78a79
VK
1019 if (acs_res->ch_width == 40) {
1020 if (is_6ghz_freq(acs_res->pri_freq))
c6b7ac07
JC
1021 hostapd_set_oper_centr_freq_seg0_idx(
1022 hapd->iconf,
1023 acs_res->vht_seg0_center_ch);
4bf78a79
VK
1024 } else if (acs_res->ch_width == 80) {
1025 hostapd_set_oper_centr_freq_seg0_idx(
1026 hapd->iconf, acs_res->vht_seg0_center_ch);
1027 if (acs_res->vht_seg1_center_ch == 0) {
c6b7ac07 1028 hostapd_set_oper_chwidth(hapd->iconf,
4bf78a79 1029 CHANWIDTH_80MHZ);
857d9422 1030 } else {
4bf78a79
VK
1031 hostapd_set_oper_chwidth(hapd->iconf,
1032 CHANWIDTH_80P80MHZ);
c6b7ac07
JC
1033 hostapd_set_oper_centr_freq_seg1_idx(
1034 hapd->iconf,
1035 acs_res->vht_seg1_center_ch);
857d9422 1036 }
4bf78a79
VK
1037 } else if (acs_res->ch_width == 160) {
1038 hostapd_set_oper_chwidth(hapd->iconf, CHANWIDTH_160MHZ);
1039 hostapd_set_oper_centr_freq_seg0_idx(
1040 hapd->iconf, acs_res->vht_seg1_center_ch);
857d9422
MM
1041 }
1042 }
1043
e1d00d47
PX
1044out:
1045 ret = hostapd_acs_completed(hapd->iface, err);
16689c7c
PX
1046 if (ret) {
1047 wpa_printf(MSG_ERROR,
1048 "ACS: Possibly channel configuration is invalid");
1049 }
1050}
1051#endif /* CONFIG_ACS */
1052
1053
04a85e44 1054int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
baf513d6
JB
1055 const u8 *bssid, const u8 *ie, size_t ie_len,
1056 int ssi_signal)
e67b55fb
JM
1057{
1058 size_t i;
1059 int ret = 0;
1060
b211f3eb
JM
1061 if (sa == NULL || ie == NULL)
1062 return -1;
1063
1064 random_add_randomness(sa, ETH_ALEN);
e67b55fb
JM
1065 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
1066 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
baf513d6
JB
1067 sa, da, bssid, ie, ie_len,
1068 ssi_signal) > 0) {
e67b55fb
JM
1069 ret = 1;
1070 break;
1071 }
1072 }
1073 return ret;
1074}
1075
1076
5c61a282
JM
1077#ifdef HOSTAPD
1078
4ec1fd8e 1079#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
1080static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
1081 const u8 *bssid,
1082 u16 auth_transaction, u16 status,
1083 const u8 *ies, size_t ies_len)
1084{
1085 struct hostapd_data *hapd = ctx;
1086 struct sta_info *sta;
1087
1088 sta = ap_get_sta(hapd, dst);
1089 if (sta == NULL)
1090 return;
1091
1092 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
1093 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
1094 sta->flags |= WLAN_STA_AUTH;
1095
1096 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
1097}
4ec1fd8e 1098#endif /* CONFIG_IEEE80211R_AP */
88b32a99
SP
1099
1100
fa61bff6
JM
1101#ifdef CONFIG_FILS
1102static void hostapd_notify_auth_fils_finish(struct hostapd_data *hapd,
1103 struct sta_info *sta, u16 resp,
1104 struct wpabuf *data, int pub)
1105{
1106 if (resp == WLAN_STATUS_SUCCESS) {
1107 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1108 HOSTAPD_LEVEL_DEBUG, "authentication OK (FILS)");
1109 sta->flags |= WLAN_STA_AUTH;
1110 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1111 sta->auth_alg = WLAN_AUTH_FILS_SK;
1112 mlme_authenticate_indication(hapd, sta);
1113 } else {
1114 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1115 HOSTAPD_LEVEL_DEBUG,
1116 "authentication failed (FILS)");
1117 }
1118
1119 hostapd_sta_auth(hapd, sta->addr, 2, resp,
1120 data ? wpabuf_head(data) : NULL,
1121 data ? wpabuf_len(data) : 0);
1122 wpabuf_free(data);
1123}
1124#endif /* CONFIG_FILS */
1125
1126
88b32a99
SP
1127static void hostapd_notif_auth(struct hostapd_data *hapd,
1128 struct auth_info *rx_auth)
1129{
1130 struct sta_info *sta;
1131 u16 status = WLAN_STATUS_SUCCESS;
1132 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
1133 size_t resp_ies_len = 0;
1134
1135 sta = ap_get_sta(hapd, rx_auth->peer);
1136 if (!sta) {
1137 sta = ap_sta_add(hapd, rx_auth->peer);
1138 if (sta == NULL) {
728d9717 1139 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
88b32a99
SP
1140 goto fail;
1141 }
1142 }
1143 sta->flags &= ~WLAN_STA_PREAUTH;
1144 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
4ec1fd8e 1145#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
1146 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
1147 sta->auth_alg = WLAN_AUTH_FT;
1148 if (sta->wpa_sm == NULL)
1149 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e 1150 sta->addr, NULL);
88b32a99 1151 if (sta->wpa_sm == NULL) {
48b06c17
JM
1152 wpa_printf(MSG_DEBUG,
1153 "FT: Failed to initialize WPA state machine");
88b32a99
SP
1154 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1155 goto fail;
1156 }
1157 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
1158 rx_auth->auth_transaction, rx_auth->ies,
1159 rx_auth->ies_len,
1160 hostapd_notify_auth_ft_finish, hapd);
1161 return;
1162 }
4ec1fd8e 1163#endif /* CONFIG_IEEE80211R_AP */
fa61bff6
JM
1164
1165#ifdef CONFIG_FILS
1166 if (rx_auth->auth_type == WLAN_AUTH_FILS_SK) {
1167 sta->auth_alg = WLAN_AUTH_FILS_SK;
1168 handle_auth_fils(hapd, sta, rx_auth->ies, rx_auth->ies_len,
1169 rx_auth->auth_type, rx_auth->auth_transaction,
1170 rx_auth->status_code,
1171 hostapd_notify_auth_fils_finish);
1172 return;
1173 }
1174#endif /* CONFIG_FILS */
1175
88b32a99
SP
1176fail:
1177 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
1178 status, resp_ies, resp_ies_len);
1179}
1180
1181
4d379be4 1182#ifndef NEED_AP_MLME
88b32a99 1183static void hostapd_action_rx(struct hostapd_data *hapd,
dbfb8e82 1184 struct rx_mgmt *drv_mgmt)
88b32a99 1185{
dbfb8e82 1186 struct ieee80211_mgmt *mgmt;
88b32a99 1187 struct sta_info *sta;
dbfb8e82
JM
1188 size_t plen __maybe_unused;
1189 u16 fc;
e0785ebb 1190 u8 *action __maybe_unused;
dbfb8e82 1191
e0785ebb 1192 if (drv_mgmt->frame_len < IEEE80211_HDRLEN + 2 + 1)
dbfb8e82
JM
1193 return;
1194
002edb63 1195 plen = drv_mgmt->frame_len - IEEE80211_HDRLEN;
dbfb8e82
JM
1196
1197 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
1198 fc = le_to_host16(mgmt->frame_control);
1199 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
1200 return; /* handled by the driver */
88b32a99 1201
e0785ebb
JM
1202 action = (u8 *) &mgmt->u.action.u;
1203 wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
1204 " da " MACSTR " plen %d",
1205 mgmt->u.action.category, *action,
1206 MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) plen);
7d9c0cd3 1207
dbfb8e82 1208 sta = ap_get_sta(hapd, mgmt->sa);
88b32a99
SP
1209 if (sta == NULL) {
1210 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
1211 return;
1212 }
4ec1fd8e 1213#ifdef CONFIG_IEEE80211R_AP
dbfb8e82 1214 if (mgmt->u.action.category == WLAN_ACTION_FT) {
002edb63
JM
1215 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action, plen);
1216 return;
88b32a99 1217 }
4ec1fd8e 1218#endif /* CONFIG_IEEE80211R_AP */
700b3f39 1219 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY) {
f9da7505 1220 ieee802_11_sa_query_action(hapd, mgmt, drv_mgmt->frame_len);
700b3f39 1221 return;
7d9c0cd3 1222 }
b5bf84ba 1223#ifdef CONFIG_WNM_AP
dbfb8e82
JM
1224 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
1225 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
cc833a23 1226 return;
d32d94db 1227 }
b5bf84ba 1228#endif /* CONFIG_WNM_AP */
037378ff
AN
1229#ifdef CONFIG_FST
1230 if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
1231 fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
1232 return;
1233 }
1234#endif /* CONFIG_FST */
9c2b8204 1235#ifdef CONFIG_DPP
002edb63 1236 if (plen >= 2 + 4 &&
9c2b8204
JM
1237 mgmt->u.action.u.vs_public_action.action ==
1238 WLAN_PA_VENDOR_SPECIFIC &&
1239 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
1240 OUI_WFA &&
1241 mgmt->u.action.u.vs_public_action.variable[0] ==
1242 DPP_OUI_TYPE) {
1243 const u8 *pos, *end;
1244
dc4d271c 1245 pos = mgmt->u.action.u.vs_public_action.oui;
9c2b8204
JM
1246 end = drv_mgmt->frame + drv_mgmt->frame_len;
1247 hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
1248 drv_mgmt->freq);
1249 return;
1250 }
1251#endif /* CONFIG_DPP */
88b32a99 1252}
4d379be4 1253#endif /* NEED_AP_MLME */
88b32a99
SP
1254
1255
fe6bdb77 1256#ifdef NEED_AP_MLME
f8b1f695 1257
f8b1f695
JM
1258#define HAPD_BROADCAST ((struct hostapd_data *) -1)
1259
1260static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
1261 const u8 *bssid)
1262{
1263 size_t i;
1264
1265 if (bssid == NULL)
1266 return NULL;
1267 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
1268 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
1269 return HAPD_BROADCAST;
1270
1271 for (i = 0; i < iface->num_bss; i++) {
1272 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
1273 return iface->bss[i];
1274 }
1275
1276 return NULL;
1277}
1278
1279
1280static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
9b90955e
JB
1281 const u8 *bssid, const u8 *addr,
1282 int wds)
f8b1f695 1283{
9b90955e 1284 hapd = get_hapd_bssid(hapd->iface, bssid);
f8b1f695
JM
1285 if (hapd == NULL || hapd == HAPD_BROADCAST)
1286 return;
1287
9b90955e 1288 ieee802_11_rx_from_unknown(hapd, addr, wds);
f8b1f695
JM
1289}
1290
1291
912b34f0 1292static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
b5b969e9 1293{
4b9841d3 1294 struct hostapd_iface *iface = hapd->iface;
b57e086c 1295 const struct ieee80211_hdr *hdr;
4b9841d3 1296 const u8 *bssid;
2a8b7416 1297 struct hostapd_frame_info fi;
912b34f0 1298 int ret;
4b9841d3 1299
5f7e1c06
JM
1300#ifdef CONFIG_TESTING_OPTIONS
1301 if (hapd->ext_mgmt_frame_handling) {
1302 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
1303 char *hex = os_malloc(hex_len);
48b06c17 1304
5f7e1c06
JM
1305 if (hex) {
1306 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
1307 rx_mgmt->frame_len);
1308 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
1309 os_free(hex);
1310 }
1311 return 1;
1312 }
1313#endif /* CONFIG_TESTING_OPTIONS */
1314
2a8b7416
JM
1315 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
1316 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
4b9841d3 1317 if (bssid == NULL)
912b34f0 1318 return 0;
4b9841d3
JM
1319
1320 hapd = get_hapd_bssid(iface, bssid);
1321 if (hapd == NULL) {
48b06c17 1322 u16 fc = le_to_host16(hdr->frame_control);
4b9841d3
JM
1323
1324 /*
1325 * Drop frames to unknown BSSIDs except for Beacon frames which
1326 * could be used to update neighbor information.
1327 */
1328 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1329 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1330 hapd = iface->bss[0];
1331 else
912b34f0 1332 return 0;
4b9841d3
JM
1333 }
1334
2a8b7416 1335 os_memset(&fi, 0, sizeof(fi));
c5cc7a59 1336 fi.freq = rx_mgmt->freq;
2a8b7416
JM
1337 fi.datarate = rx_mgmt->datarate;
1338 fi.ssi_signal = rx_mgmt->ssi_signal;
1339
4b9841d3
JM
1340 if (hapd == HAPD_BROADCAST) {
1341 size_t i;
48b06c17 1342
912b34f0
JM
1343 ret = 0;
1344 for (i = 0; i < iface->num_bss; i++) {
1d91f504
SW
1345 /* if bss is set, driver will call this function for
1346 * each bss individually. */
1347 if (rx_mgmt->drv_priv &&
1348 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
1349 continue;
1350
912b34f0
JM
1351 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
1352 rx_mgmt->frame_len, &fi) > 0)
1353 ret = 1;
1354 }
4b9841d3 1355 } else
912b34f0
JM
1356 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
1357 &fi);
bbb921da
JM
1358
1359 random_add_randomness(&fi, sizeof(fi));
912b34f0
JM
1360
1361 return ret;
b5b969e9
JM
1362}
1363
1364
f8b1f695
JM
1365static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
1366 size_t len, u16 stype, int ok)
b5b969e9 1367{
4b9841d3 1368 struct ieee80211_hdr *hdr;
6996ff7b 1369 struct hostapd_data *orig_hapd = hapd;
48b06c17 1370
4b9841d3
JM
1371 hdr = (struct ieee80211_hdr *) buf;
1372 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
6996ff7b 1373 if (!hapd)
4b9841d3 1374 return;
6996ff7b
JM
1375 if (hapd == HAPD_BROADCAST) {
1376 if (stype != WLAN_FC_STYPE_ACTION || len <= 25 ||
1377 buf[24] != WLAN_ACTION_PUBLIC)
1378 return;
1379 hapd = get_hapd_bssid(orig_hapd->iface, hdr->addr2);
1380 if (!hapd || hapd == HAPD_BROADCAST)
1381 return;
1382 /*
1383 * Allow processing of TX status for a Public Action frame that
1384 * used wildcard BBSID.
1385 */
1386 }
b5b969e9
JM
1387 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1388}
f82ef4d8 1389
f8b1f695 1390#endif /* NEED_AP_MLME */
ad1e68e6
JM
1391
1392
a8e0505b
JM
1393static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
1394{
1395 struct sta_info *sta = ap_get_sta(hapd, addr);
48b06c17 1396
a8e0505b
JM
1397 if (sta)
1398 return 0;
1399
1400 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
1401 " - adding a new STA", MAC2STR(addr));
1402 sta = ap_sta_add(hapd, addr);
1403 if (sta) {
1404 hostapd_new_assoc_sta(hapd, sta, 0);
1405 } else {
1406 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
1407 MAC2STR(addr));
1408 return -1;
1409 }
1410
1411 return 0;
1412}
1413
1414
1415static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
1416 const u8 *data, size_t data_len)
1417{
1418 struct hostapd_iface *iface = hapd->iface;
f826635c 1419 struct sta_info *sta;
a8e0505b
JM
1420 size_t j;
1421
1422 for (j = 0; j < iface->num_bss; j++) {
48b06c17
JM
1423 sta = ap_get_sta(iface->bss[j], src);
1424 if (sta && sta->flags & WLAN_STA_ASSOC) {
1425 hapd = iface->bss[j];
1426 break;
a8e0505b
JM
1427 }
1428 }
1429
1430 ieee802_1x_receive(hapd, src, data, data_len);
1431}
1432
96bc5086
TB
1433#endif /* HOSTAPD */
1434
a8e0505b 1435
499c37b7
NJ
1436static struct hostapd_channel_data *
1437hostapd_get_mode_chan(struct hostapd_hw_modes *mode, unsigned int freq)
1438{
1439 int i;
1440 struct hostapd_channel_data *chan;
1441
1442 for (i = 0; i < mode->num_channels; i++) {
1443 chan = &mode->channels[i];
1444 if ((unsigned int) chan->freq == freq)
1445 return chan;
1446 }
1447
1448 return NULL;
1449}
1450
1451
0185007c
MK
1452static struct hostapd_channel_data * hostapd_get_mode_channel(
1453 struct hostapd_iface *iface, unsigned int freq)
1454{
1455 int i;
1456 struct hostapd_channel_data *chan;
1457
499c37b7
NJ
1458 for (i = 0; i < iface->num_hw_features; i++) {
1459 if (hostapd_hw_skip_mode(iface, &iface->hw_features[i]))
1460 continue;
1461 chan = hostapd_get_mode_chan(&iface->hw_features[i], freq);
1462 if (chan)
0185007c
MK
1463 return chan;
1464 }
1465
1466 return NULL;
1467}
1468
1469
1470static void hostapd_update_nf(struct hostapd_iface *iface,
1471 struct hostapd_channel_data *chan,
1472 struct freq_survey *survey)
1473{
1474 if (!iface->chans_surveyed) {
1475 chan->min_nf = survey->nf;
1476 iface->lowest_nf = survey->nf;
1477 } else {
1478 if (dl_list_empty(&chan->survey_list))
1479 chan->min_nf = survey->nf;
1480 else if (survey->nf < chan->min_nf)
1481 chan->min_nf = survey->nf;
1482 if (survey->nf < iface->lowest_nf)
1483 iface->lowest_nf = survey->nf;
1484 }
1485}
1486
1487
ec8f36af
KP
1488static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
1489 struct survey_results *survey_res)
1490{
1491 struct hostapd_channel_data *chan;
1492 struct freq_survey *survey;
1493 u64 divisor, dividend;
1494
1495 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
1496 list);
1497 if (!survey || !survey->freq)
1498 return;
1499
1500 chan = hostapd_get_mode_channel(iface, survey->freq);
1501 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
1502 return;
1503
48b06c17
JM
1504 wpa_printf(MSG_DEBUG,
1505 "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
ec8f36af
KP
1506 survey->freq,
1507 (unsigned long int) survey->channel_time,
1508 (unsigned long int) survey->channel_time_busy);
1509
1510 if (survey->channel_time > iface->last_channel_time &&
1511 survey->channel_time > survey->channel_time_busy) {
1512 dividend = survey->channel_time_busy -
1513 iface->last_channel_time_busy;
1514 divisor = survey->channel_time - iface->last_channel_time;
1515
1516 iface->channel_utilization = dividend * 255 / divisor;
1517 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
1518 iface->channel_utilization);
1519 }
1520 iface->last_channel_time = survey->channel_time;
1521 iface->last_channel_time_busy = survey->channel_time_busy;
1522}
1523
1524
96bc5086
TB
1525void hostapd_event_get_survey(struct hostapd_iface *iface,
1526 struct survey_results *survey_results)
0185007c 1527{
0185007c
MK
1528 struct freq_survey *survey, *tmp;
1529 struct hostapd_channel_data *chan;
1530
1531 if (dl_list_empty(&survey_results->survey_list)) {
1532 wpa_printf(MSG_DEBUG, "No survey data received");
1533 return;
1534 }
1535
ec8f36af
KP
1536 if (survey_results->freq_filter) {
1537 hostapd_single_channel_get_survey(iface, survey_results);
1538 return;
1539 }
1540
0185007c
MK
1541 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1542 struct freq_survey, list) {
1543 chan = hostapd_get_mode_channel(iface, survey->freq);
1544 if (!chan)
1545 continue;
1546 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1547 continue;
1548
1549 dl_list_del(&survey->list);
1550 dl_list_add_tail(&chan->survey_list, &survey->list);
1551
1552 hostapd_update_nf(iface, chan, survey);
1553
1554 iface->chans_surveyed++;
1555 }
1556}
1557
1558
96bc5086 1559#ifdef HOSTAPD
e76da505
JD
1560#ifdef NEED_AP_MLME
1561
5841958f
MK
1562static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1563{
1564 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1565 hapd->conf->iface);
1566
1567 if (hapd->csa_in_progress) {
1568 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1569 hapd->conf->iface);
1570 hostapd_switch_channel_fallback(hapd->iface,
1571 &hapd->cs_freq_params);
1572 }
1573}
1574
1575
e76da505
JD
1576static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1577 struct dfs_event *radar)
1578{
e76da505 1579 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
dc036d9e 1580 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
1581 radar->chan_offset, radar->chan_width,
1582 radar->cf1, radar->cf2);
e76da505
JD
1583}
1584
1585
7cbb5f1a
VT
1586static void hostapd_event_dfs_pre_cac_expired(struct hostapd_data *hapd,
1587 struct dfs_event *radar)
1588{
1589 wpa_printf(MSG_DEBUG, "DFS Pre-CAC expired on %d MHz", radar->freq);
1590 hostapd_dfs_pre_cac_expired(hapd->iface, radar->freq, radar->ht_enabled,
1591 radar->chan_offset, radar->chan_width,
1592 radar->cf1, radar->cf2);
1593}
1594
1595
e76da505
JD
1596static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1597 struct dfs_event *radar)
1598{
1599 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
dc036d9e 1600 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
58b73e3d
JD
1601 radar->chan_offset, radar->chan_width,
1602 radar->cf1, radar->cf2);
e76da505
JD
1603}
1604
1605
1606static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1607 struct dfs_event *radar)
1608{
1609 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
dc036d9e 1610 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
58b73e3d
JD
1611 radar->chan_offset, radar->chan_width,
1612 radar->cf1, radar->cf2);
e76da505
JD
1613}
1614
1615
1616static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1617 struct dfs_event *radar)
1618{
1619 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
dc036d9e 1620 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
1621 radar->chan_offset, radar->chan_width,
1622 radar->cf1, radar->cf2);
e76da505
JD
1623}
1624
c13578c3
AK
1625
1626static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1627 struct dfs_event *radar)
1628{
1629 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1630 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1631 radar->chan_offset, radar->chan_width,
1632 radar->cf1, radar->cf2);
1633}
1634
e76da505
JD
1635#endif /* NEED_AP_MLME */
1636
1637
1952b626
BP
1638static void hostapd_event_wds_sta_interface_status(struct hostapd_data *hapd,
1639 int istatus,
1640 const char *ifname,
1641 const u8 *addr)
1642{
1643 struct sta_info *sta = ap_get_sta(hapd, addr);
1644
1645 if (sta) {
1646 os_free(sta->ifname_wds);
1647 if (istatus == INTERFACE_ADDED)
1648 sta->ifname_wds = os_strdup(ifname);
1649 else
1650 sta->ifname_wds = NULL;
1651 }
1652
1653 wpa_msg(hapd->msg_ctx, MSG_INFO, "%sifname=%s sta_addr=" MACSTR,
1654 istatus == INTERFACE_ADDED ?
1655 WDS_STA_INTERFACE_ADDED : WDS_STA_INTERFACE_REMOVED,
1656 ifname, MAC2STR(addr));
1657}
1658
1659
ef60f012
LD
1660#ifdef CONFIG_OWE
1661static int hostapd_notif_update_dh_ie(struct hostapd_data *hapd,
1662 const u8 *peer, const u8 *ie,
1663 size_t ie_len)
1664{
1665 u16 status;
1666 struct sta_info *sta;
1667 struct ieee802_11_elems elems;
1668
1669 if (!hapd || !hapd->wpa_auth) {
1670 wpa_printf(MSG_DEBUG, "OWE: Invalid hapd context");
1671 return -1;
1672 }
1673 if (!peer) {
1674 wpa_printf(MSG_DEBUG, "OWE: Peer unknown");
1675 return -1;
1676 }
1677 if (!(hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE)) {
1678 wpa_printf(MSG_DEBUG, "OWE: No OWE AKM configured");
1679 status = WLAN_STATUS_AKMP_NOT_VALID;
1680 goto err;
1681 }
1682 if (ieee802_11_parse_elems(ie, ie_len, &elems, 1) == ParseFailed) {
1683 wpa_printf(MSG_DEBUG, "OWE: Failed to parse OWE IE for "
1684 MACSTR, MAC2STR(peer));
1685 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1686 goto err;
1687 }
1688 status = owe_validate_request(hapd, peer, elems.rsn_ie,
1689 elems.rsn_ie_len,
1690 elems.owe_dh, elems.owe_dh_len);
1691 if (status != WLAN_STATUS_SUCCESS)
1692 goto err;
1693
1694 sta = ap_get_sta(hapd, peer);
1695 if (sta) {
1696 ap_sta_no_session_timeout(hapd, sta);
1697 accounting_sta_stop(hapd, sta);
1698
1699 /*
1700 * Make sure that the previously registered inactivity timer
1701 * will not remove the STA immediately.
1702 */
1703 sta->timeout_next = STA_NULLFUNC;
1704 } else {
1705 sta = ap_sta_add(hapd, peer);
1706 if (!sta) {
1707 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1708 goto err;
1709 }
1710 }
1711 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1712
1713 status = owe_process_rsn_ie(hapd, sta, elems.rsn_ie,
1714 elems.rsn_ie_len, elems.owe_dh,
1715 elems.owe_dh_len);
1716 if (status != WLAN_STATUS_SUCCESS)
1717 ap_free_sta(hapd, sta);
1718
1719 return 0;
1720err:
1721 hostapd_drv_update_dh_ie(hapd, peer, status, NULL, 0);
1722 return 0;
1723}
1724#endif /* CONFIG_OWE */
1725
1726
9646a8ab 1727void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
ad1e68e6
JM
1728 union wpa_event_data *data)
1729{
1730 struct hostapd_data *hapd = ctx;
74781dfc
JM
1731#ifndef CONFIG_NO_STDOUT_DEBUG
1732 int level = MSG_DEBUG;
ad1e68e6 1733
34caf71a 1734 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
74781dfc
JM
1735 data->rx_mgmt.frame_len >= 24) {
1736 const struct ieee80211_hdr *hdr;
1737 u16 fc;
48b06c17 1738
74781dfc
JM
1739 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1740 fc = le_to_host16(hdr->frame_control);
1741 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1742 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1743 level = MSG_EXCESSIVE;
cc2ada86
JM
1744 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1745 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1746 level = MSG_EXCESSIVE;
74781dfc
JM
1747 }
1748
1749 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
e26cd1a1 1750 event_to_string(event), event);
74781dfc 1751#endif /* CONFIG_NO_STDOUT_DEBUG */
e26cd1a1 1752
ad1e68e6
JM
1753 switch (event) {
1754 case EVENT_MICHAEL_MIC_FAILURE:
1755 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1756 break;
1757 case EVENT_SCAN_RESULTS:
1758 if (hapd->iface->scan_cb)
1759 hapd->iface->scan_cb(hapd->iface);
1760 break;
fcf0f87d 1761 case EVENT_WPS_BUTTON_PUSHED:
d601247c 1762 hostapd_wps_button_pushed(hapd, NULL);
fcf0f87d 1763 break;
f8b1f695
JM
1764#ifdef NEED_AP_MLME
1765 case EVENT_TX_STATUS:
1766 switch (data->tx_status.type) {
1767 case WLAN_FC_TYPE_MGMT:
1768 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1769 data->tx_status.data_len,
1770 data->tx_status.stype,
1771 data->tx_status.ack);
1772 break;
1773 case WLAN_FC_TYPE_DATA:
1774 hostapd_tx_status(hapd, data->tx_status.dst,
1775 data->tx_status.data,
1776 data->tx_status.data_len,
1777 data->tx_status.ack);
1778 break;
1779 }
1780 break;
dd840f79
JB
1781 case EVENT_EAPOL_TX_STATUS:
1782 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1783 data->eapol_tx_status.data,
1784 data->eapol_tx_status.data_len,
1785 data->eapol_tx_status.ack);
1786 break;
bcf24348
JB
1787 case EVENT_DRIVER_CLIENT_POLL_OK:
1788 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1789 break;
f8b1f695 1790 case EVENT_RX_FROM_UNKNOWN:
9b90955e
JB
1791 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1792 data->rx_from_unknown.addr,
1793 data->rx_from_unknown.wds);
f8b1f695 1794 break;
dbfb8e82 1795#endif /* NEED_AP_MLME */
f8b1f695 1796 case EVENT_RX_MGMT:
c0333c8d
JM
1797 if (!data->rx_mgmt.frame)
1798 break;
dbfb8e82 1799#ifdef NEED_AP_MLME
4d379be4
JM
1800 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
1801#else /* NEED_AP_MLME */
dbfb8e82 1802 hostapd_action_rx(hapd, &data->rx_mgmt);
4d379be4 1803#endif /* NEED_AP_MLME */
dbfb8e82 1804 break;
a0e0d3bb 1805 case EVENT_RX_PROBE_REQ:
b211f3eb
JM
1806 if (data->rx_probe_req.sa == NULL ||
1807 data->rx_probe_req.ie == NULL)
1808 break;
a0e0d3bb 1809 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
04a85e44
JM
1810 data->rx_probe_req.da,
1811 data->rx_probe_req.bssid,
a0e0d3bb 1812 data->rx_probe_req.ie,
baf513d6
JB
1813 data->rx_probe_req.ie_len,
1814 data->rx_probe_req.ssi_signal);
a0e0d3bb 1815 break;
a70a5d6d 1816 case EVENT_NEW_STA:
a8e0505b
JM
1817 hostapd_event_new_sta(hapd, data->new_sta.addr);
1818 break;
1819 case EVENT_EAPOL_RX:
1820 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1821 data->eapol_rx.data,
1822 data->eapol_rx.data_len);
1823 break;
1d041bec 1824 case EVENT_ASSOC:
04a258e7
JM
1825 if (!data)
1826 return;
1d041bec
JM
1827 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1828 data->assoc_info.req_ies,
39b08b5f
SP
1829 data->assoc_info.req_ies_len,
1830 data->assoc_info.reassoc);
1d041bec 1831 break;
ef60f012
LD
1832#ifdef CONFIG_OWE
1833 case EVENT_UPDATE_DH:
1834 if (!data)
1835 return;
1836 hostapd_notif_update_dh_ie(hapd, data->update_dh.peer,
1837 data->update_dh.ie,
1838 data->update_dh.ie_len);
1839 break;
1840#endif /* CONFIG_OWE */
1d041bec
JM
1841 case EVENT_DISASSOC:
1842 if (data)
1843 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1844 break;
1845 case EVENT_DEAUTH:
1846 if (data)
1847 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1848 break;
0d7e5a3a
JB
1849 case EVENT_STATION_LOW_ACK:
1850 if (!data)
1851 break;
1852 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1853 break;
88b32a99
SP
1854 case EVENT_AUTH:
1855 hostapd_notif_auth(hapd, &data->auth);
1856 break;
95f556f3 1857 case EVENT_CH_SWITCH_STARTED:
1b487b8b
TP
1858 case EVENT_CH_SWITCH:
1859 if (!data)
1860 break;
1861 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1862 data->ch_switch.ht_enabled,
8d1fdde7
JD
1863 data->ch_switch.ch_offset,
1864 data->ch_switch.ch_width,
1865 data->ch_switch.cf1,
95f556f3
OD
1866 data->ch_switch.cf2,
1867 event == EVENT_CH_SWITCH);
1b487b8b 1868 break;
3140803b
RM
1869 case EVENT_CONNECT_FAILED_REASON:
1870 if (!data)
1871 break;
1872 hostapd_event_connect_failed_reason(
1873 hapd, data->connect_failed_reason.addr,
1874 data->connect_failed_reason.code);
1875 break;
0185007c 1876 case EVENT_SURVEY:
96bc5086 1877 hostapd_event_get_survey(hapd->iface, &data->survey_results);
0185007c 1878 break;
e76da505 1879#ifdef NEED_AP_MLME
5841958f
MK
1880 case EVENT_INTERFACE_UNAVAILABLE:
1881 hostapd_event_iface_unavailable(hapd);
1882 break;
e76da505
JD
1883 case EVENT_DFS_RADAR_DETECTED:
1884 if (!data)
1885 break;
1886 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1887 break;
7cbb5f1a
VT
1888 case EVENT_DFS_PRE_CAC_EXPIRED:
1889 if (!data)
1890 break;
1891 hostapd_event_dfs_pre_cac_expired(hapd, &data->dfs_event);
1892 break;
e76da505
JD
1893 case EVENT_DFS_CAC_FINISHED:
1894 if (!data)
1895 break;
1896 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1897 break;
1898 case EVENT_DFS_CAC_ABORTED:
1899 if (!data)
1900 break;
1901 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1902 break;
1903 case EVENT_DFS_NOP_FINISHED:
1904 if (!data)
1905 break;
1906 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1907 break;
1908 case EVENT_CHANNEL_LIST_CHANGED:
1909 /* channel list changed (regulatory?), update channel list */
1910 /* TODO: check this. hostapd_get_hw_features() initializes
1911 * too much stuff. */
1912 /* hostapd_get_hw_features(hapd->iface); */
795baf77
AS
1913 hostapd_channel_list_updated(
1914 hapd->iface, data->channel_list_changed.initiator);
e76da505 1915 break;
c13578c3
AK
1916 case EVENT_DFS_CAC_STARTED:
1917 if (!data)
1918 break;
1919 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1920 break;
e76da505 1921#endif /* NEED_AP_MLME */
ab93fdeb
JM
1922 case EVENT_INTERFACE_ENABLED:
1923 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
f33c8606
JM
1924 if (hapd->disabled && hapd->started) {
1925 hapd->disabled = 0;
1926 /*
1927 * Try to re-enable interface if the driver stopped it
1928 * when the interface got disabled.
1929 */
567df550
HW
1930 if (hapd->wpa_auth)
1931 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1932 else
1933 hostapd_reconfig_encryption(hapd);
f33c8606
JM
1934 hapd->reenable_beacon = 1;
1935 ieee802_11_set_beacon(hapd);
10de40c0
ZK
1936#ifdef NEED_AP_MLME
1937 } else if (hapd->disabled && hapd->iface->cac_started) {
1938 wpa_printf(MSG_DEBUG, "DFS: restarting pending CAC");
1939 hostapd_handle_dfs(hapd->iface);
1940#endif /* NEED_AP_MLME */
f33c8606 1941 }
ab93fdeb
JM
1942 break;
1943 case EVENT_INTERFACE_DISABLED:
c165cb40 1944 hostapd_free_stas(hapd);
ab93fdeb 1945 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
f33c8606 1946 hapd->disabled = 1;
ab93fdeb 1947 break;
16689c7c
PX
1948#ifdef CONFIG_ACS
1949 case EVENT_ACS_CHANNEL_SELECTED:
857d9422
MM
1950 hostapd_acs_channel_selected(hapd,
1951 &data->acs_selected_channels);
16689c7c
PX
1952 break;
1953#endif /* CONFIG_ACS */
ec2b5173
T
1954 case EVENT_STATION_OPMODE_CHANGED:
1955 hostapd_event_sta_opmode_changed(hapd, data->sta_opmode.addr,
1956 data->sta_opmode.smps_mode,
1957 data->sta_opmode.chan_width,
1958 data->sta_opmode.rx_nss);
1959 break;
1952b626
BP
1960 case EVENT_WDS_STA_INTERFACE_STATUS:
1961 hostapd_event_wds_sta_interface_status(
1962 hapd, data->wds_sta_interface.istatus,
1963 data->wds_sta_interface.ifname,
1964 data->wds_sta_interface.sta_addr);
1965 break;
ad1e68e6
JM
1966 default:
1967 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1968 break;
1969 }
1970}
f8b1f695 1971
45e3fc72
RM
1972
1973void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
1974 union wpa_event_data *data)
1975{
1976 struct hapd_interfaces *interfaces = ctx;
1977 struct hostapd_data *hapd;
1978
1979 if (event != EVENT_INTERFACE_STATUS)
1980 return;
1981
1982 hapd = hostapd_get_iface(interfaces, data->interface_status.ifname);
1983 if (hapd && hapd->driver && hapd->driver->get_ifindex &&
1984 hapd->drv_priv) {
1985 unsigned int ifindex;
1986
1987 ifindex = hapd->driver->get_ifindex(hapd->drv_priv);
1988 if (ifindex != data->interface_status.ifindex) {
1989 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1990 "interface status ifindex %d mismatch (%d)",
1991 ifindex, data->interface_status.ifindex);
1992 return;
1993 }
1994 }
1995 if (hapd)
1996 wpa_supplicant_event(hapd, event, data);
1997}
1998
f8b1f695 1999#endif /* HOSTAPD */