]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/drv_callbacks.c
FT: Omit RSNXE from FT protocol Reassociation Request when needed
[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
SP
485 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
486 sta->auth_alg, req_ies, req_ies_len);
2cf36d60
JM
487 if (!p) {
488 wpa_printf(MSG_DEBUG, "FT: Failed to write AssocResp IEs");
489 return WLAN_STATUS_UNSPECIFIED_FAILURE;
490 }
cc20edc9 491#endif /* CONFIG_IEEE80211R_AP */
88b32a99 492
fa61bff6
JM
493#ifdef CONFIG_FILS
494 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
495 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
496 sta->auth_alg == WLAN_AUTH_FILS_PK) {
8b5ddda5
JM
497 int delay_assoc = 0;
498
3de1566d
PX
499 if (!req_ies)
500 return WLAN_STATUS_UNSPECIFIED_FAILURE;
501
fa61bff6
JM
502 if (!wpa_fils_validate_fils_session(sta->wpa_sm, req_ies,
503 req_ies_len,
504 sta->fils_session)) {
505 wpa_printf(MSG_DEBUG,
506 "FILS: Session validation failed");
507 return WLAN_STATUS_UNSPECIFIED_FAILURE;
508 }
509
510 res = wpa_fils_validate_key_confirm(sta->wpa_sm, req_ies,
511 req_ies_len);
512 if (res < 0) {
513 wpa_printf(MSG_DEBUG,
514 "FILS: Key Confirm validation failed");
515 return WLAN_STATUS_UNSPECIFIED_FAILURE;
516 }
517
8b5ddda5 518 if (fils_process_hlp(hapd, sta, req_ies, req_ies_len) > 0) {
fa61bff6 519 wpa_printf(MSG_DEBUG,
8b5ddda5
JM
520 "FILS: Delaying Assoc Response (HLP)");
521 delay_assoc = 1;
522 } else {
523 wpa_printf(MSG_DEBUG,
524 "FILS: Going ahead with Assoc Response (no HLP)");
525 }
526
527 if (sta) {
528 wpa_printf(MSG_DEBUG, "FILS: HLP callback cleanup");
529 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
530 os_free(sta->fils_pending_assoc_req);
531 sta->fils_pending_assoc_req = NULL;
532 sta->fils_pending_assoc_req_len = 0;
533 wpabuf_free(sta->fils_hlp_resp);
534 sta->fils_hlp_resp = NULL;
535 sta->fils_drv_assoc_finish = 0;
fa61bff6
JM
536 }
537
8b5ddda5
JM
538 if (sta && delay_assoc && status == WLAN_STATUS_SUCCESS) {
539 u8 *req_tmp;
540
541 req_tmp = os_malloc(req_ies_len);
542 if (!req_tmp) {
543 wpa_printf(MSG_DEBUG,
544 "FILS: buffer allocation failed for assoc req");
545 goto fail;
546 }
547 os_memcpy(req_tmp, req_ies, req_ies_len);
548 sta->fils_pending_assoc_req = req_tmp;
549 sta->fils_pending_assoc_req_len = req_ies_len;
550 sta->fils_pending_assoc_is_reassoc = reassoc;
551 sta->fils_drv_assoc_finish = 1;
552 wpa_printf(MSG_DEBUG,
553 "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
554 MACSTR, MAC2STR(sta->addr));
555 eloop_register_timeout(
556 0, hapd->conf->fils_hlp_wait_time * 1024,
557 fils_hlp_timeout, hapd, sta);
558 return 0;
559 }
fa61bff6 560 p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
8b5ddda5
JM
561 elems.fils_session,
562 sta->fils_hlp_resp);
fa61bff6
JM
563 wpa_hexdump(MSG_DEBUG, "FILS Assoc Resp BUF (IEs)",
564 buf, p - buf);
565 }
566#endif /* CONFIG_FILS */
567
33c8bbd8
AKP
568#ifdef CONFIG_OWE
569 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
570 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
571 elems.owe_dh) {
04ded82e
JM
572 u8 *npos;
573
79ce2d51
AP
574 npos = owe_assoc_req_process(hapd, sta,
575 elems.owe_dh, elems.owe_dh_len,
576 p, sizeof(buf) - (p - buf),
e7d8842e 577 &status);
a4668c68
AP
578 if (npos)
579 p = npos;
e7d8842e 580
a4668c68 581 if (!npos &&
e7d8842e 582 status == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
a4668c68
AP
583 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
584 p - buf);
585 return 0;
586 }
587
e7d8842e 588 if (!npos || status != WLAN_STATUS_SUCCESS)
33c8bbd8
AKP
589 goto fail;
590 }
591#endif /* CONFIG_OWE */
592
10ec6a5f
JM
593#ifdef CONFIG_DPP2
594 dpp_pfs_free(sta->dpp_pfs);
595 sta->dpp_pfs = NULL;
596
597 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
598 hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
599 wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
600 elems.owe_dh) {
601 sta->dpp_pfs = dpp_pfs_init(
602 wpabuf_head(hapd->conf->dpp_netaccesskey),
603 wpabuf_len(hapd->conf->dpp_netaccesskey));
604 if (!sta->dpp_pfs) {
605 wpa_printf(MSG_DEBUG,
606 "DPP: Could not initialize PFS");
607 /* Try to continue without PFS */
608 goto pfs_fail;
609 }
610
611 if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
612 elems.owe_dh_len) < 0) {
613 dpp_pfs_free(sta->dpp_pfs);
614 sta->dpp_pfs = NULL;
615 reason = WLAN_REASON_UNSPECIFIED;
616 goto fail;
617 }
618 }
619
620 wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
621 sta->dpp_pfs->secret : NULL);
622 pfs_fail:
623#endif /* CONFIG_DPP2 */
624
fae7e64a
O
625 if (elems.rrm_enabled &&
626 elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
627 os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
628 sizeof(sta->rrm_enabled_capa));
629
33c8bbd8 630#if defined(CONFIG_IEEE80211R_AP) || defined(CONFIG_FILS) || defined(CONFIG_OWE)
88b32a99 631 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
e4474c1c 632
cc20edc9
JM
633 if (sta->auth_alg == WLAN_AUTH_FT ||
634 sta->auth_alg == WLAN_AUTH_FILS_SK ||
635 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
636 sta->auth_alg == WLAN_AUTH_FILS_PK)
e4474c1c 637 ap_sta_set_authorized(hapd, sta, 1);
cc20edc9 638#else /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
88b32a99
SP
639 /* Keep compiler silent about unused variables */
640 if (status) {
641 }
cc20edc9 642#endif /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
88b32a99 643
b5b969e9
JM
644 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
645 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
3578e665 646 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
88b32a99 647
e4474c1c
DPS
648 hostapd_set_sta_flags(hapd, sta);
649
88b32a99
SP
650 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
651 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
957bff83
JM
652#ifdef CONFIG_FILS
653 else if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
654 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
655 sta->auth_alg == WLAN_AUTH_FILS_PK)
656 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
657#endif /* CONFIG_FILS */
88b32a99
SP
658 else
659 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
b5b969e9
JM
660
661 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
662
663 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
664
ef796391 665#ifdef CONFIG_P2P
99c01af9
JM
666 if (req_ies) {
667 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
668 req_ies, req_ies_len);
669 }
ef796391
JM
670#endif /* CONFIG_P2P */
671
b5b969e9 672 return 0;
08a74e6a
JM
673
674fail:
4ec1fd8e 675#ifdef CONFIG_IEEE80211R_AP
88b32a99 676 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
4ec1fd8e 677#endif /* CONFIG_IEEE80211R_AP */
08a74e6a
JM
678 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
679 ap_free_sta(hapd, sta);
680 return -1;
b5b969e9
JM
681}
682
683
684void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
685{
686 struct sta_info *sta;
687
83e843e8
JM
688 if (addr == NULL) {
689 /*
690 * This could potentially happen with unexpected event from the
691 * driver wrapper. This was seen at least in one case where the
692 * driver ended up reporting a station mode event while hostapd
693 * was running, so better make sure we stop processing such an
694 * event here.
695 */
48b06c17
JM
696 wpa_printf(MSG_DEBUG,
697 "hostapd_notif_disassoc: Skip event with no address");
1f4c7b6b 698 return;
83e843e8
JM
699 }
700
b5b969e9
JM
701 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
702 HOSTAPD_LEVEL_INFO, "disassociated");
703
704 sta = ap_get_sta(hapd, addr);
705 if (sta == NULL) {
48b06c17
JM
706 wpa_printf(MSG_DEBUG,
707 "Disassociation notification for unknown STA "
708 MACSTR, MAC2STR(addr));
b5b969e9
JM
709 return;
710 }
711
ae055af4 712 ap_sta_set_authorized(hapd, sta, 0);
b5b969e9
JM
713 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
714 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
715 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
716 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
717 ap_free_sta(hapd, sta);
718}
719
720
0d7e5a3a
JB
721void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
722{
723 struct sta_info *sta = ap_get_sta(hapd, addr);
724
d58c3bd8 725 if (!sta || !hapd->conf->disassoc_low_ack || sta->agreed_to_steer)
0d7e5a3a
JB
726 return;
727
728 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
48b06c17
JM
729 HOSTAPD_LEVEL_INFO,
730 "disconnected due to excessive missing ACKs");
0d7e5a3a 731 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
5f99d962 732 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
0d7e5a3a
JB
733}
734
735
ec2b5173
T
736void hostapd_event_sta_opmode_changed(struct hostapd_data *hapd, const u8 *addr,
737 enum smps_mode smps_mode,
738 enum chan_width chan_width, u8 rx_nss)
739{
740 struct sta_info *sta = ap_get_sta(hapd, addr);
741 const char *txt;
742
743 if (!sta)
744 return;
745
746 switch (smps_mode) {
747 case SMPS_AUTOMATIC:
748 txt = "automatic";
749 break;
750 case SMPS_OFF:
751 txt = "off";
752 break;
753 case SMPS_DYNAMIC:
754 txt = "dynamic";
755 break;
756 case SMPS_STATIC:
757 txt = "static";
758 break;
759 default:
760 txt = NULL;
761 break;
762 }
763 if (txt) {
764 wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_SMPS_MODE_CHANGED
765 MACSTR " %s", MAC2STR(addr), txt);
766 }
767
768 switch (chan_width) {
769 case CHAN_WIDTH_20_NOHT:
770 txt = "20(no-HT)";
771 break;
772 case CHAN_WIDTH_20:
773 txt = "20";
774 break;
775 case CHAN_WIDTH_40:
776 txt = "40";
777 break;
778 case CHAN_WIDTH_80:
779 txt = "80";
780 break;
781 case CHAN_WIDTH_80P80:
782 txt = "80+80";
783 break;
784 case CHAN_WIDTH_160:
785 txt = "160";
786 break;
787 default:
788 txt = NULL;
789 break;
790 }
791 if (txt) {
792 wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_MAX_BW_CHANGED
793 MACSTR " %s", MAC2STR(addr), txt);
794 }
795
796 if (rx_nss != 0xff) {
797 wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_N_SS_CHANGED
798 MACSTR " %d", MAC2STR(addr), rx_nss);
799 }
800}
801
802
1b487b8b 803void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
95f556f3
OD
804 int offset, int width, int cf1, int cf2,
805 int finished)
1b487b8b 806{
f91e68e9
MV
807 /* TODO: If OCV is enabled deauth STAs that don't perform a SA Query */
808
c7803a02 809#ifdef NEED_AP_MLME
d308a44f
LC
810 int channel, chwidth, is_dfs;
811 u8 seg0_idx = 0, seg1_idx = 0;
6ca4949a 812 size_t i;
1b487b8b
TP
813
814 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2f06bcb3 815 HOSTAPD_LEVEL_INFO,
95f556f3
OD
816 "driver %s channel switch: freq=%d, ht=%d, vht_ch=0x%x, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
817 finished ? "had" : "starting",
bda9c085
SM
818 freq, ht, hapd->iconf->ch_switch_vht_config, offset,
819 width, channel_width_to_string(width), cf1, cf2);
1b487b8b 820
c3805fb6
JM
821 if (!hapd->iface->current_mode) {
822 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
823 HOSTAPD_LEVEL_WARNING,
824 "ignore channel switch since the interface is not yet ready");
825 return;
826 }
827
1b487b8b
TP
828 hapd->iface->freq = freq;
829
830 channel = hostapd_hw_get_channel(hapd, freq);
831 if (!channel) {
832 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
48b06c17
JM
833 HOSTAPD_LEVEL_WARNING,
834 "driver switched to bad channel!");
1b487b8b
TP
835 return;
836 }
837
8d1fdde7
JD
838 switch (width) {
839 case CHAN_WIDTH_80:
464dcfd0 840 chwidth = CHANWIDTH_80MHZ;
8d1fdde7
JD
841 break;
842 case CHAN_WIDTH_80P80:
464dcfd0 843 chwidth = CHANWIDTH_80P80MHZ;
8d1fdde7
JD
844 break;
845 case CHAN_WIDTH_160:
464dcfd0 846 chwidth = CHANWIDTH_160MHZ;
8d1fdde7
JD
847 break;
848 case CHAN_WIDTH_20_NOHT:
849 case CHAN_WIDTH_20:
850 case CHAN_WIDTH_40:
851 default:
464dcfd0 852 chwidth = CHANWIDTH_USE_HT;
8d1fdde7
JD
853 break;
854 }
855
856 switch (hapd->iface->current_mode->mode) {
857 case HOSTAPD_MODE_IEEE80211A:
858 if (cf1 > 5000)
859 seg0_idx = (cf1 - 5000) / 5;
860 if (cf2 > 5000)
861 seg1_idx = (cf2 - 5000) / 5;
862 break;
863 default:
d308a44f
LC
864 ieee80211_freq_to_chan(cf1, &seg0_idx);
865 ieee80211_freq_to_chan(cf2, &seg1_idx);
8d1fdde7
JD
866 break;
867 }
868
1b487b8b
TP
869 hapd->iconf->channel = channel;
870 hapd->iconf->ieee80211n = ht;
bda9c085 871 if (!ht) {
5de74818 872 hapd->iconf->ieee80211ac = 0;
bda9c085
SM
873 } else if (hapd->iconf->ch_switch_vht_config) {
874 /* CHAN_SWITCH VHT config */
875 if (hapd->iconf->ch_switch_vht_config &
876 CH_SWITCH_VHT_ENABLED)
877 hapd->iconf->ieee80211ac = 1;
878 else if (hapd->iconf->ch_switch_vht_config &
879 CH_SWITCH_VHT_DISABLED)
880 hapd->iconf->ieee80211ac = 0;
881 }
882 hapd->iconf->ch_switch_vht_config = 0;
883
1b487b8b 884 hapd->iconf->secondary_channel = offset;
c6b7ac07
JC
885 hostapd_set_oper_chwidth(hapd->iconf, chwidth);
886 hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, seg0_idx);
887 hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, seg1_idx);
bf281c12 888
d239ab39 889 is_dfs = ieee80211_is_dfs(freq, hapd->iface->hw_features,
890 hapd->iface->num_hw_features);
1e2aaffb 891
95f556f3
OD
892 wpa_msg(hapd->msg_ctx, MSG_INFO,
893 "%sfreq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d dfs=%d",
894 finished ? WPA_EVENT_CHANNEL_SWITCH :
895 WPA_EVENT_CHANNEL_SWITCH_STARTED,
896 freq, ht, offset, channel_width_to_string(width),
897 cf1, cf2, is_dfs);
898 if (!finished)
899 return;
900
6782b684
MK
901 if (hapd->csa_in_progress &&
902 freq == hapd->cs_freq_params.freq) {
bf281c12 903 hostapd_cleanup_cs_params(hapd);
6782b684 904 ieee802_11_set_beacon(hapd);
bf281c12 905
1e2aaffb
AK
906 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
907 "freq=%d dfs=%d", freq, is_dfs);
908 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
909 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
910 "freq=%d dfs=%d", freq, is_dfs);
bf281c12 911 }
6ca4949a
MT
912
913 for (i = 0; i < hapd->iface->num_bss; i++)
914 hostapd_neighbor_set_own_report(hapd->iface->bss[i]);
c7803a02 915#endif /* NEED_AP_MLME */
1b487b8b
TP
916}
917
918
3140803b
RM
919void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
920 const u8 *addr, int reason_code)
921{
922 switch (reason_code) {
923 case MAX_CLIENT_REACHED:
924 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
925 MAC2STR(addr));
926 break;
927 case BLOCKED_CLIENT:
928 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
929 MAC2STR(addr));
930 break;
931 }
932}
933
934
16689c7c 935#ifdef CONFIG_ACS
d9909717
TB
936void hostapd_acs_channel_selected(struct hostapd_data *hapd,
937 struct acs_selected_channels *acs_res)
16689c7c 938{
3784c058 939 int ret, i;
e1d00d47 940 int err = 0;
41cac481 941 struct hostapd_channel_data *pri_chan;
16689c7c
PX
942
943 if (hapd->iconf->channel) {
944 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
945 hapd->iconf->channel);
946 return;
947 }
948
59e33b4a
AB
949 hapd->iface->freq = acs_res->pri_freq;
950
3784c058
PX
951 if (!hapd->iface->current_mode) {
952 for (i = 0; i < hapd->iface->num_hw_features; i++) {
953 struct hostapd_hw_modes *mode =
954 &hapd->iface->hw_features[i];
955
956 if (mode->mode == acs_res->hw_mode) {
59e33b4a
AB
957 if (hapd->iface->freq > 0 &&
958 !hw_get_chan(mode->mode,
959 hapd->iface->freq,
960 hapd->iface->hw_features,
961 hapd->iface->num_hw_features))
962 continue;
3784c058
PX
963 hapd->iface->current_mode = mode;
964 break;
965 }
966 }
967 if (!hapd->iface->current_mode) {
968 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
969 HOSTAPD_LEVEL_WARNING,
970 "driver selected to bad hw_mode");
e1d00d47
PX
971 err = 1;
972 goto out;
3784c058
PX
973 }
974 }
975
41cac481 976 if (!acs_res->pri_freq) {
16689c7c
PX
977 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
978 HOSTAPD_LEVEL_WARNING,
979 "driver switched to bad channel");
e1d00d47
PX
980 err = 1;
981 goto out;
16689c7c 982 }
41cac481
AB
983 pri_chan = hw_get_channel_freq(hapd->iface->current_mode->mode,
984 acs_res->pri_freq, NULL,
985 hapd->iface->hw_features,
986 hapd->iface->num_hw_features);
987 if (!pri_chan) {
988 wpa_printf(MSG_ERROR,
989 "ACS: Could not determine primary channel number from pri_freq %u",
990 acs_res->pri_freq);
991 err = 1;
992 goto out;
993 }
16689c7c 994
41cac481 995 hapd->iconf->channel = pri_chan->chan;
857d9422 996 hapd->iconf->acs = 1;
16689c7c 997
41cac481 998 if (acs_res->sec_freq == 0)
16689c7c 999 hapd->iconf->secondary_channel = 0;
41cac481 1000 else if (acs_res->sec_freq < acs_res->pri_freq)
16689c7c 1001 hapd->iconf->secondary_channel = -1;
41cac481 1002 else if (acs_res->sec_freq > acs_res->pri_freq)
16689c7c
PX
1003 hapd->iconf->secondary_channel = 1;
1004 else {
1005 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
e1d00d47
PX
1006 err = 1;
1007 goto out;
16689c7c
PX
1008 }
1009
1d2c45ec 1010 if (hapd->iface->conf->ieee80211ac || hapd->iface->conf->ieee80211ax) {
857d9422 1011 /* set defaults for backwards compatibility */
c6b7ac07
JC
1012 hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, 0);
1013 hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, 0);
1014 hostapd_set_oper_chwidth(hapd->iconf, CHANWIDTH_USE_HT);
4bf78a79
VK
1015 if (acs_res->ch_width == 40) {
1016 if (is_6ghz_freq(acs_res->pri_freq))
c6b7ac07
JC
1017 hostapd_set_oper_centr_freq_seg0_idx(
1018 hapd->iconf,
1019 acs_res->vht_seg0_center_ch);
4bf78a79
VK
1020 } else if (acs_res->ch_width == 80) {
1021 hostapd_set_oper_centr_freq_seg0_idx(
1022 hapd->iconf, acs_res->vht_seg0_center_ch);
1023 if (acs_res->vht_seg1_center_ch == 0) {
c6b7ac07 1024 hostapd_set_oper_chwidth(hapd->iconf,
4bf78a79 1025 CHANWIDTH_80MHZ);
857d9422 1026 } else {
4bf78a79
VK
1027 hostapd_set_oper_chwidth(hapd->iconf,
1028 CHANWIDTH_80P80MHZ);
c6b7ac07
JC
1029 hostapd_set_oper_centr_freq_seg1_idx(
1030 hapd->iconf,
1031 acs_res->vht_seg1_center_ch);
857d9422 1032 }
4bf78a79
VK
1033 } else if (acs_res->ch_width == 160) {
1034 hostapd_set_oper_chwidth(hapd->iconf, CHANWIDTH_160MHZ);
1035 hostapd_set_oper_centr_freq_seg0_idx(
1036 hapd->iconf, acs_res->vht_seg1_center_ch);
857d9422
MM
1037 }
1038 }
1039
e1d00d47
PX
1040out:
1041 ret = hostapd_acs_completed(hapd->iface, err);
16689c7c
PX
1042 if (ret) {
1043 wpa_printf(MSG_ERROR,
1044 "ACS: Possibly channel configuration is invalid");
1045 }
1046}
1047#endif /* CONFIG_ACS */
1048
1049
04a85e44 1050int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
baf513d6
JB
1051 const u8 *bssid, const u8 *ie, size_t ie_len,
1052 int ssi_signal)
e67b55fb
JM
1053{
1054 size_t i;
1055 int ret = 0;
1056
b211f3eb
JM
1057 if (sa == NULL || ie == NULL)
1058 return -1;
1059
1060 random_add_randomness(sa, ETH_ALEN);
e67b55fb
JM
1061 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
1062 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
baf513d6
JB
1063 sa, da, bssid, ie, ie_len,
1064 ssi_signal) > 0) {
e67b55fb
JM
1065 ret = 1;
1066 break;
1067 }
1068 }
1069 return ret;
1070}
1071
1072
5c61a282
JM
1073#ifdef HOSTAPD
1074
4ec1fd8e 1075#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
1076static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
1077 const u8 *bssid,
1078 u16 auth_transaction, u16 status,
1079 const u8 *ies, size_t ies_len)
1080{
1081 struct hostapd_data *hapd = ctx;
1082 struct sta_info *sta;
1083
1084 sta = ap_get_sta(hapd, dst);
1085 if (sta == NULL)
1086 return;
1087
1088 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
1089 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
1090 sta->flags |= WLAN_STA_AUTH;
1091
1092 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
1093}
4ec1fd8e 1094#endif /* CONFIG_IEEE80211R_AP */
88b32a99
SP
1095
1096
fa61bff6
JM
1097#ifdef CONFIG_FILS
1098static void hostapd_notify_auth_fils_finish(struct hostapd_data *hapd,
1099 struct sta_info *sta, u16 resp,
1100 struct wpabuf *data, int pub)
1101{
1102 if (resp == WLAN_STATUS_SUCCESS) {
1103 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1104 HOSTAPD_LEVEL_DEBUG, "authentication OK (FILS)");
1105 sta->flags |= WLAN_STA_AUTH;
1106 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1107 sta->auth_alg = WLAN_AUTH_FILS_SK;
1108 mlme_authenticate_indication(hapd, sta);
1109 } else {
1110 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1111 HOSTAPD_LEVEL_DEBUG,
1112 "authentication failed (FILS)");
1113 }
1114
1115 hostapd_sta_auth(hapd, sta->addr, 2, resp,
1116 data ? wpabuf_head(data) : NULL,
1117 data ? wpabuf_len(data) : 0);
1118 wpabuf_free(data);
1119}
1120#endif /* CONFIG_FILS */
1121
1122
88b32a99
SP
1123static void hostapd_notif_auth(struct hostapd_data *hapd,
1124 struct auth_info *rx_auth)
1125{
1126 struct sta_info *sta;
1127 u16 status = WLAN_STATUS_SUCCESS;
1128 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
1129 size_t resp_ies_len = 0;
1130
1131 sta = ap_get_sta(hapd, rx_auth->peer);
1132 if (!sta) {
1133 sta = ap_sta_add(hapd, rx_auth->peer);
1134 if (sta == NULL) {
728d9717 1135 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
88b32a99
SP
1136 goto fail;
1137 }
1138 }
1139 sta->flags &= ~WLAN_STA_PREAUTH;
1140 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
4ec1fd8e 1141#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
1142 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
1143 sta->auth_alg = WLAN_AUTH_FT;
1144 if (sta->wpa_sm == NULL)
1145 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e 1146 sta->addr, NULL);
88b32a99 1147 if (sta->wpa_sm == NULL) {
48b06c17
JM
1148 wpa_printf(MSG_DEBUG,
1149 "FT: Failed to initialize WPA state machine");
88b32a99
SP
1150 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1151 goto fail;
1152 }
1153 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
1154 rx_auth->auth_transaction, rx_auth->ies,
1155 rx_auth->ies_len,
1156 hostapd_notify_auth_ft_finish, hapd);
1157 return;
1158 }
4ec1fd8e 1159#endif /* CONFIG_IEEE80211R_AP */
fa61bff6
JM
1160
1161#ifdef CONFIG_FILS
1162 if (rx_auth->auth_type == WLAN_AUTH_FILS_SK) {
1163 sta->auth_alg = WLAN_AUTH_FILS_SK;
1164 handle_auth_fils(hapd, sta, rx_auth->ies, rx_auth->ies_len,
1165 rx_auth->auth_type, rx_auth->auth_transaction,
1166 rx_auth->status_code,
1167 hostapd_notify_auth_fils_finish);
1168 return;
1169 }
1170#endif /* CONFIG_FILS */
1171
88b32a99
SP
1172fail:
1173 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
1174 status, resp_ies, resp_ies_len);
1175}
1176
1177
4d379be4 1178#ifndef NEED_AP_MLME
88b32a99 1179static void hostapd_action_rx(struct hostapd_data *hapd,
dbfb8e82 1180 struct rx_mgmt *drv_mgmt)
88b32a99 1181{
dbfb8e82 1182 struct ieee80211_mgmt *mgmt;
88b32a99 1183 struct sta_info *sta;
dbfb8e82
JM
1184 size_t plen __maybe_unused;
1185 u16 fc;
e0785ebb 1186 u8 *action __maybe_unused;
dbfb8e82 1187
e0785ebb 1188 if (drv_mgmt->frame_len < IEEE80211_HDRLEN + 2 + 1)
dbfb8e82
JM
1189 return;
1190
002edb63 1191 plen = drv_mgmt->frame_len - IEEE80211_HDRLEN;
dbfb8e82
JM
1192
1193 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
1194 fc = le_to_host16(mgmt->frame_control);
1195 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
1196 return; /* handled by the driver */
88b32a99 1197
e0785ebb
JM
1198 action = (u8 *) &mgmt->u.action.u;
1199 wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
1200 " da " MACSTR " plen %d",
1201 mgmt->u.action.category, *action,
1202 MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) plen);
7d9c0cd3 1203
dbfb8e82 1204 sta = ap_get_sta(hapd, mgmt->sa);
88b32a99
SP
1205 if (sta == NULL) {
1206 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
1207 return;
1208 }
4ec1fd8e 1209#ifdef CONFIG_IEEE80211R_AP
dbfb8e82 1210 if (mgmt->u.action.category == WLAN_ACTION_FT) {
002edb63
JM
1211 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action, plen);
1212 return;
88b32a99 1213 }
4ec1fd8e 1214#endif /* CONFIG_IEEE80211R_AP */
700b3f39 1215 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY) {
f9da7505 1216 ieee802_11_sa_query_action(hapd, mgmt, drv_mgmt->frame_len);
700b3f39 1217 return;
7d9c0cd3 1218 }
b5bf84ba 1219#ifdef CONFIG_WNM_AP
dbfb8e82
JM
1220 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
1221 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
cc833a23 1222 return;
d32d94db 1223 }
b5bf84ba 1224#endif /* CONFIG_WNM_AP */
037378ff
AN
1225#ifdef CONFIG_FST
1226 if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
1227 fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
1228 return;
1229 }
1230#endif /* CONFIG_FST */
9c2b8204 1231#ifdef CONFIG_DPP
002edb63 1232 if (plen >= 2 + 4 &&
9c2b8204
JM
1233 mgmt->u.action.u.vs_public_action.action ==
1234 WLAN_PA_VENDOR_SPECIFIC &&
1235 WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
1236 OUI_WFA &&
1237 mgmt->u.action.u.vs_public_action.variable[0] ==
1238 DPP_OUI_TYPE) {
1239 const u8 *pos, *end;
1240
dc4d271c 1241 pos = mgmt->u.action.u.vs_public_action.oui;
9c2b8204
JM
1242 end = drv_mgmt->frame + drv_mgmt->frame_len;
1243 hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
1244 drv_mgmt->freq);
1245 return;
1246 }
1247#endif /* CONFIG_DPP */
88b32a99 1248}
4d379be4 1249#endif /* NEED_AP_MLME */
88b32a99
SP
1250
1251
fe6bdb77 1252#ifdef NEED_AP_MLME
f8b1f695 1253
f8b1f695
JM
1254#define HAPD_BROADCAST ((struct hostapd_data *) -1)
1255
1256static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
1257 const u8 *bssid)
1258{
1259 size_t i;
1260
1261 if (bssid == NULL)
1262 return NULL;
1263 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
1264 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
1265 return HAPD_BROADCAST;
1266
1267 for (i = 0; i < iface->num_bss; i++) {
1268 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
1269 return iface->bss[i];
1270 }
1271
1272 return NULL;
1273}
1274
1275
1276static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
9b90955e
JB
1277 const u8 *bssid, const u8 *addr,
1278 int wds)
f8b1f695 1279{
9b90955e 1280 hapd = get_hapd_bssid(hapd->iface, bssid);
f8b1f695
JM
1281 if (hapd == NULL || hapd == HAPD_BROADCAST)
1282 return;
1283
9b90955e 1284 ieee802_11_rx_from_unknown(hapd, addr, wds);
f8b1f695
JM
1285}
1286
1287
912b34f0 1288static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
b5b969e9 1289{
4b9841d3 1290 struct hostapd_iface *iface = hapd->iface;
b57e086c 1291 const struct ieee80211_hdr *hdr;
4b9841d3 1292 const u8 *bssid;
2a8b7416 1293 struct hostapd_frame_info fi;
912b34f0 1294 int ret;
4b9841d3 1295
5f7e1c06
JM
1296#ifdef CONFIG_TESTING_OPTIONS
1297 if (hapd->ext_mgmt_frame_handling) {
1298 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
1299 char *hex = os_malloc(hex_len);
48b06c17 1300
5f7e1c06
JM
1301 if (hex) {
1302 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
1303 rx_mgmt->frame_len);
1304 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
1305 os_free(hex);
1306 }
1307 return 1;
1308 }
1309#endif /* CONFIG_TESTING_OPTIONS */
1310
2a8b7416
JM
1311 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
1312 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
4b9841d3 1313 if (bssid == NULL)
912b34f0 1314 return 0;
4b9841d3
JM
1315
1316 hapd = get_hapd_bssid(iface, bssid);
1317 if (hapd == NULL) {
48b06c17 1318 u16 fc = le_to_host16(hdr->frame_control);
4b9841d3
JM
1319
1320 /*
1321 * Drop frames to unknown BSSIDs except for Beacon frames which
1322 * could be used to update neighbor information.
1323 */
1324 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1325 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1326 hapd = iface->bss[0];
1327 else
912b34f0 1328 return 0;
4b9841d3
JM
1329 }
1330
2a8b7416 1331 os_memset(&fi, 0, sizeof(fi));
c5cc7a59 1332 fi.freq = rx_mgmt->freq;
2a8b7416
JM
1333 fi.datarate = rx_mgmt->datarate;
1334 fi.ssi_signal = rx_mgmt->ssi_signal;
1335
4b9841d3
JM
1336 if (hapd == HAPD_BROADCAST) {
1337 size_t i;
48b06c17 1338
912b34f0
JM
1339 ret = 0;
1340 for (i = 0; i < iface->num_bss; i++) {
1d91f504
SW
1341 /* if bss is set, driver will call this function for
1342 * each bss individually. */
1343 if (rx_mgmt->drv_priv &&
1344 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
1345 continue;
1346
912b34f0
JM
1347 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
1348 rx_mgmt->frame_len, &fi) > 0)
1349 ret = 1;
1350 }
4b9841d3 1351 } else
912b34f0
JM
1352 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
1353 &fi);
bbb921da
JM
1354
1355 random_add_randomness(&fi, sizeof(fi));
912b34f0
JM
1356
1357 return ret;
b5b969e9
JM
1358}
1359
1360
f8b1f695
JM
1361static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
1362 size_t len, u16 stype, int ok)
b5b969e9 1363{
4b9841d3 1364 struct ieee80211_hdr *hdr;
6996ff7b 1365 struct hostapd_data *orig_hapd = hapd;
48b06c17 1366
4b9841d3
JM
1367 hdr = (struct ieee80211_hdr *) buf;
1368 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
6996ff7b 1369 if (!hapd)
4b9841d3 1370 return;
6996ff7b
JM
1371 if (hapd == HAPD_BROADCAST) {
1372 if (stype != WLAN_FC_STYPE_ACTION || len <= 25 ||
1373 buf[24] != WLAN_ACTION_PUBLIC)
1374 return;
1375 hapd = get_hapd_bssid(orig_hapd->iface, hdr->addr2);
1376 if (!hapd || hapd == HAPD_BROADCAST)
1377 return;
1378 /*
1379 * Allow processing of TX status for a Public Action frame that
1380 * used wildcard BBSID.
1381 */
1382 }
b5b969e9
JM
1383 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1384}
f82ef4d8 1385
f8b1f695 1386#endif /* NEED_AP_MLME */
ad1e68e6
JM
1387
1388
a8e0505b
JM
1389static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
1390{
1391 struct sta_info *sta = ap_get_sta(hapd, addr);
48b06c17 1392
a8e0505b
JM
1393 if (sta)
1394 return 0;
1395
1396 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
1397 " - adding a new STA", MAC2STR(addr));
1398 sta = ap_sta_add(hapd, addr);
1399 if (sta) {
1400 hostapd_new_assoc_sta(hapd, sta, 0);
1401 } else {
1402 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
1403 MAC2STR(addr));
1404 return -1;
1405 }
1406
1407 return 0;
1408}
1409
1410
1411static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
1412 const u8 *data, size_t data_len)
1413{
1414 struct hostapd_iface *iface = hapd->iface;
f826635c 1415 struct sta_info *sta;
a8e0505b
JM
1416 size_t j;
1417
1418 for (j = 0; j < iface->num_bss; j++) {
48b06c17
JM
1419 sta = ap_get_sta(iface->bss[j], src);
1420 if (sta && sta->flags & WLAN_STA_ASSOC) {
1421 hapd = iface->bss[j];
1422 break;
a8e0505b
JM
1423 }
1424 }
1425
1426 ieee802_1x_receive(hapd, src, data, data_len);
1427}
1428
96bc5086
TB
1429#endif /* HOSTAPD */
1430
a8e0505b 1431
499c37b7
NJ
1432static struct hostapd_channel_data *
1433hostapd_get_mode_chan(struct hostapd_hw_modes *mode, unsigned int freq)
1434{
1435 int i;
1436 struct hostapd_channel_data *chan;
1437
1438 for (i = 0; i < mode->num_channels; i++) {
1439 chan = &mode->channels[i];
1440 if ((unsigned int) chan->freq == freq)
1441 return chan;
1442 }
1443
1444 return NULL;
1445}
1446
1447
0185007c
MK
1448static struct hostapd_channel_data * hostapd_get_mode_channel(
1449 struct hostapd_iface *iface, unsigned int freq)
1450{
1451 int i;
1452 struct hostapd_channel_data *chan;
1453
499c37b7
NJ
1454 for (i = 0; i < iface->num_hw_features; i++) {
1455 if (hostapd_hw_skip_mode(iface, &iface->hw_features[i]))
1456 continue;
1457 chan = hostapd_get_mode_chan(&iface->hw_features[i], freq);
1458 if (chan)
0185007c
MK
1459 return chan;
1460 }
1461
1462 return NULL;
1463}
1464
1465
1466static void hostapd_update_nf(struct hostapd_iface *iface,
1467 struct hostapd_channel_data *chan,
1468 struct freq_survey *survey)
1469{
1470 if (!iface->chans_surveyed) {
1471 chan->min_nf = survey->nf;
1472 iface->lowest_nf = survey->nf;
1473 } else {
1474 if (dl_list_empty(&chan->survey_list))
1475 chan->min_nf = survey->nf;
1476 else if (survey->nf < chan->min_nf)
1477 chan->min_nf = survey->nf;
1478 if (survey->nf < iface->lowest_nf)
1479 iface->lowest_nf = survey->nf;
1480 }
1481}
1482
1483
ec8f36af
KP
1484static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
1485 struct survey_results *survey_res)
1486{
1487 struct hostapd_channel_data *chan;
1488 struct freq_survey *survey;
1489 u64 divisor, dividend;
1490
1491 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
1492 list);
1493 if (!survey || !survey->freq)
1494 return;
1495
1496 chan = hostapd_get_mode_channel(iface, survey->freq);
1497 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
1498 return;
1499
48b06c17
JM
1500 wpa_printf(MSG_DEBUG,
1501 "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
ec8f36af
KP
1502 survey->freq,
1503 (unsigned long int) survey->channel_time,
1504 (unsigned long int) survey->channel_time_busy);
1505
1506 if (survey->channel_time > iface->last_channel_time &&
1507 survey->channel_time > survey->channel_time_busy) {
1508 dividend = survey->channel_time_busy -
1509 iface->last_channel_time_busy;
1510 divisor = survey->channel_time - iface->last_channel_time;
1511
1512 iface->channel_utilization = dividend * 255 / divisor;
1513 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
1514 iface->channel_utilization);
1515 }
1516 iface->last_channel_time = survey->channel_time;
1517 iface->last_channel_time_busy = survey->channel_time_busy;
1518}
1519
1520
96bc5086
TB
1521void hostapd_event_get_survey(struct hostapd_iface *iface,
1522 struct survey_results *survey_results)
0185007c 1523{
0185007c
MK
1524 struct freq_survey *survey, *tmp;
1525 struct hostapd_channel_data *chan;
1526
1527 if (dl_list_empty(&survey_results->survey_list)) {
1528 wpa_printf(MSG_DEBUG, "No survey data received");
1529 return;
1530 }
1531
ec8f36af
KP
1532 if (survey_results->freq_filter) {
1533 hostapd_single_channel_get_survey(iface, survey_results);
1534 return;
1535 }
1536
0185007c
MK
1537 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1538 struct freq_survey, list) {
1539 chan = hostapd_get_mode_channel(iface, survey->freq);
1540 if (!chan)
1541 continue;
1542 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1543 continue;
1544
1545 dl_list_del(&survey->list);
1546 dl_list_add_tail(&chan->survey_list, &survey->list);
1547
1548 hostapd_update_nf(iface, chan, survey);
1549
1550 iface->chans_surveyed++;
1551 }
1552}
1553
1554
96bc5086 1555#ifdef HOSTAPD
e76da505
JD
1556#ifdef NEED_AP_MLME
1557
5841958f
MK
1558static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1559{
1560 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1561 hapd->conf->iface);
1562
1563 if (hapd->csa_in_progress) {
1564 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1565 hapd->conf->iface);
1566 hostapd_switch_channel_fallback(hapd->iface,
1567 &hapd->cs_freq_params);
1568 }
1569}
1570
1571
e76da505
JD
1572static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1573 struct dfs_event *radar)
1574{
e76da505 1575 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
dc036d9e 1576 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
1577 radar->chan_offset, radar->chan_width,
1578 radar->cf1, radar->cf2);
e76da505
JD
1579}
1580
1581
7cbb5f1a
VT
1582static void hostapd_event_dfs_pre_cac_expired(struct hostapd_data *hapd,
1583 struct dfs_event *radar)
1584{
1585 wpa_printf(MSG_DEBUG, "DFS Pre-CAC expired on %d MHz", radar->freq);
1586 hostapd_dfs_pre_cac_expired(hapd->iface, radar->freq, radar->ht_enabled,
1587 radar->chan_offset, radar->chan_width,
1588 radar->cf1, radar->cf2);
1589}
1590
1591
e76da505
JD
1592static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1593 struct dfs_event *radar)
1594{
1595 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
dc036d9e 1596 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
58b73e3d
JD
1597 radar->chan_offset, radar->chan_width,
1598 radar->cf1, radar->cf2);
e76da505
JD
1599}
1600
1601
1602static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1603 struct dfs_event *radar)
1604{
1605 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
dc036d9e 1606 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
58b73e3d
JD
1607 radar->chan_offset, radar->chan_width,
1608 radar->cf1, radar->cf2);
e76da505
JD
1609}
1610
1611
1612static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1613 struct dfs_event *radar)
1614{
1615 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
dc036d9e 1616 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
1617 radar->chan_offset, radar->chan_width,
1618 radar->cf1, radar->cf2);
e76da505
JD
1619}
1620
c13578c3
AK
1621
1622static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1623 struct dfs_event *radar)
1624{
1625 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1626 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1627 radar->chan_offset, radar->chan_width,
1628 radar->cf1, radar->cf2);
1629}
1630
e76da505
JD
1631#endif /* NEED_AP_MLME */
1632
1633
1952b626
BP
1634static void hostapd_event_wds_sta_interface_status(struct hostapd_data *hapd,
1635 int istatus,
1636 const char *ifname,
1637 const u8 *addr)
1638{
1639 struct sta_info *sta = ap_get_sta(hapd, addr);
1640
1641 if (sta) {
1642 os_free(sta->ifname_wds);
1643 if (istatus == INTERFACE_ADDED)
1644 sta->ifname_wds = os_strdup(ifname);
1645 else
1646 sta->ifname_wds = NULL;
1647 }
1648
1649 wpa_msg(hapd->msg_ctx, MSG_INFO, "%sifname=%s sta_addr=" MACSTR,
1650 istatus == INTERFACE_ADDED ?
1651 WDS_STA_INTERFACE_ADDED : WDS_STA_INTERFACE_REMOVED,
1652 ifname, MAC2STR(addr));
1653}
1654
1655
ef60f012
LD
1656#ifdef CONFIG_OWE
1657static int hostapd_notif_update_dh_ie(struct hostapd_data *hapd,
1658 const u8 *peer, const u8 *ie,
1659 size_t ie_len)
1660{
1661 u16 status;
1662 struct sta_info *sta;
1663 struct ieee802_11_elems elems;
1664
1665 if (!hapd || !hapd->wpa_auth) {
1666 wpa_printf(MSG_DEBUG, "OWE: Invalid hapd context");
1667 return -1;
1668 }
1669 if (!peer) {
1670 wpa_printf(MSG_DEBUG, "OWE: Peer unknown");
1671 return -1;
1672 }
1673 if (!(hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE)) {
1674 wpa_printf(MSG_DEBUG, "OWE: No OWE AKM configured");
1675 status = WLAN_STATUS_AKMP_NOT_VALID;
1676 goto err;
1677 }
1678 if (ieee802_11_parse_elems(ie, ie_len, &elems, 1) == ParseFailed) {
1679 wpa_printf(MSG_DEBUG, "OWE: Failed to parse OWE IE for "
1680 MACSTR, MAC2STR(peer));
1681 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1682 goto err;
1683 }
1684 status = owe_validate_request(hapd, peer, elems.rsn_ie,
1685 elems.rsn_ie_len,
1686 elems.owe_dh, elems.owe_dh_len);
1687 if (status != WLAN_STATUS_SUCCESS)
1688 goto err;
1689
1690 sta = ap_get_sta(hapd, peer);
1691 if (sta) {
1692 ap_sta_no_session_timeout(hapd, sta);
1693 accounting_sta_stop(hapd, sta);
1694
1695 /*
1696 * Make sure that the previously registered inactivity timer
1697 * will not remove the STA immediately.
1698 */
1699 sta->timeout_next = STA_NULLFUNC;
1700 } else {
1701 sta = ap_sta_add(hapd, peer);
1702 if (!sta) {
1703 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1704 goto err;
1705 }
1706 }
1707 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1708
1709 status = owe_process_rsn_ie(hapd, sta, elems.rsn_ie,
1710 elems.rsn_ie_len, elems.owe_dh,
1711 elems.owe_dh_len);
1712 if (status != WLAN_STATUS_SUCCESS)
1713 ap_free_sta(hapd, sta);
1714
1715 return 0;
1716err:
1717 hostapd_drv_update_dh_ie(hapd, peer, status, NULL, 0);
1718 return 0;
1719}
1720#endif /* CONFIG_OWE */
1721
1722
9646a8ab 1723void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
ad1e68e6
JM
1724 union wpa_event_data *data)
1725{
1726 struct hostapd_data *hapd = ctx;
74781dfc
JM
1727#ifndef CONFIG_NO_STDOUT_DEBUG
1728 int level = MSG_DEBUG;
ad1e68e6 1729
34caf71a 1730 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
74781dfc
JM
1731 data->rx_mgmt.frame_len >= 24) {
1732 const struct ieee80211_hdr *hdr;
1733 u16 fc;
48b06c17 1734
74781dfc
JM
1735 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1736 fc = le_to_host16(hdr->frame_control);
1737 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1738 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1739 level = MSG_EXCESSIVE;
cc2ada86
JM
1740 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1741 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1742 level = MSG_EXCESSIVE;
74781dfc
JM
1743 }
1744
1745 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
e26cd1a1 1746 event_to_string(event), event);
74781dfc 1747#endif /* CONFIG_NO_STDOUT_DEBUG */
e26cd1a1 1748
ad1e68e6
JM
1749 switch (event) {
1750 case EVENT_MICHAEL_MIC_FAILURE:
1751 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1752 break;
1753 case EVENT_SCAN_RESULTS:
1754 if (hapd->iface->scan_cb)
1755 hapd->iface->scan_cb(hapd->iface);
1756 break;
fcf0f87d 1757 case EVENT_WPS_BUTTON_PUSHED:
d601247c 1758 hostapd_wps_button_pushed(hapd, NULL);
fcf0f87d 1759 break;
f8b1f695
JM
1760#ifdef NEED_AP_MLME
1761 case EVENT_TX_STATUS:
1762 switch (data->tx_status.type) {
1763 case WLAN_FC_TYPE_MGMT:
1764 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1765 data->tx_status.data_len,
1766 data->tx_status.stype,
1767 data->tx_status.ack);
1768 break;
1769 case WLAN_FC_TYPE_DATA:
1770 hostapd_tx_status(hapd, data->tx_status.dst,
1771 data->tx_status.data,
1772 data->tx_status.data_len,
1773 data->tx_status.ack);
1774 break;
1775 }
1776 break;
dd840f79
JB
1777 case EVENT_EAPOL_TX_STATUS:
1778 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1779 data->eapol_tx_status.data,
1780 data->eapol_tx_status.data_len,
1781 data->eapol_tx_status.ack);
1782 break;
bcf24348
JB
1783 case EVENT_DRIVER_CLIENT_POLL_OK:
1784 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1785 break;
f8b1f695 1786 case EVENT_RX_FROM_UNKNOWN:
9b90955e
JB
1787 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1788 data->rx_from_unknown.addr,
1789 data->rx_from_unknown.wds);
f8b1f695 1790 break;
dbfb8e82 1791#endif /* NEED_AP_MLME */
f8b1f695 1792 case EVENT_RX_MGMT:
c0333c8d
JM
1793 if (!data->rx_mgmt.frame)
1794 break;
dbfb8e82 1795#ifdef NEED_AP_MLME
4d379be4
JM
1796 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
1797#else /* NEED_AP_MLME */
dbfb8e82 1798 hostapd_action_rx(hapd, &data->rx_mgmt);
4d379be4 1799#endif /* NEED_AP_MLME */
dbfb8e82 1800 break;
a0e0d3bb 1801 case EVENT_RX_PROBE_REQ:
b211f3eb
JM
1802 if (data->rx_probe_req.sa == NULL ||
1803 data->rx_probe_req.ie == NULL)
1804 break;
a0e0d3bb 1805 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
04a85e44
JM
1806 data->rx_probe_req.da,
1807 data->rx_probe_req.bssid,
a0e0d3bb 1808 data->rx_probe_req.ie,
baf513d6
JB
1809 data->rx_probe_req.ie_len,
1810 data->rx_probe_req.ssi_signal);
a0e0d3bb 1811 break;
a70a5d6d 1812 case EVENT_NEW_STA:
a8e0505b
JM
1813 hostapd_event_new_sta(hapd, data->new_sta.addr);
1814 break;
1815 case EVENT_EAPOL_RX:
1816 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1817 data->eapol_rx.data,
1818 data->eapol_rx.data_len);
1819 break;
1d041bec 1820 case EVENT_ASSOC:
04a258e7
JM
1821 if (!data)
1822 return;
1d041bec
JM
1823 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1824 data->assoc_info.req_ies,
39b08b5f
SP
1825 data->assoc_info.req_ies_len,
1826 data->assoc_info.reassoc);
1d041bec 1827 break;
ef60f012
LD
1828#ifdef CONFIG_OWE
1829 case EVENT_UPDATE_DH:
1830 if (!data)
1831 return;
1832 hostapd_notif_update_dh_ie(hapd, data->update_dh.peer,
1833 data->update_dh.ie,
1834 data->update_dh.ie_len);
1835 break;
1836#endif /* CONFIG_OWE */
1d041bec
JM
1837 case EVENT_DISASSOC:
1838 if (data)
1839 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1840 break;
1841 case EVENT_DEAUTH:
1842 if (data)
1843 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1844 break;
0d7e5a3a
JB
1845 case EVENT_STATION_LOW_ACK:
1846 if (!data)
1847 break;
1848 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1849 break;
88b32a99
SP
1850 case EVENT_AUTH:
1851 hostapd_notif_auth(hapd, &data->auth);
1852 break;
95f556f3 1853 case EVENT_CH_SWITCH_STARTED:
1b487b8b
TP
1854 case EVENT_CH_SWITCH:
1855 if (!data)
1856 break;
1857 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1858 data->ch_switch.ht_enabled,
8d1fdde7
JD
1859 data->ch_switch.ch_offset,
1860 data->ch_switch.ch_width,
1861 data->ch_switch.cf1,
95f556f3
OD
1862 data->ch_switch.cf2,
1863 event == EVENT_CH_SWITCH);
1b487b8b 1864 break;
3140803b
RM
1865 case EVENT_CONNECT_FAILED_REASON:
1866 if (!data)
1867 break;
1868 hostapd_event_connect_failed_reason(
1869 hapd, data->connect_failed_reason.addr,
1870 data->connect_failed_reason.code);
1871 break;
0185007c 1872 case EVENT_SURVEY:
96bc5086 1873 hostapd_event_get_survey(hapd->iface, &data->survey_results);
0185007c 1874 break;
e76da505 1875#ifdef NEED_AP_MLME
5841958f
MK
1876 case EVENT_INTERFACE_UNAVAILABLE:
1877 hostapd_event_iface_unavailable(hapd);
1878 break;
e76da505
JD
1879 case EVENT_DFS_RADAR_DETECTED:
1880 if (!data)
1881 break;
1882 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1883 break;
7cbb5f1a
VT
1884 case EVENT_DFS_PRE_CAC_EXPIRED:
1885 if (!data)
1886 break;
1887 hostapd_event_dfs_pre_cac_expired(hapd, &data->dfs_event);
1888 break;
e76da505
JD
1889 case EVENT_DFS_CAC_FINISHED:
1890 if (!data)
1891 break;
1892 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1893 break;
1894 case EVENT_DFS_CAC_ABORTED:
1895 if (!data)
1896 break;
1897 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1898 break;
1899 case EVENT_DFS_NOP_FINISHED:
1900 if (!data)
1901 break;
1902 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1903 break;
1904 case EVENT_CHANNEL_LIST_CHANGED:
1905 /* channel list changed (regulatory?), update channel list */
1906 /* TODO: check this. hostapd_get_hw_features() initializes
1907 * too much stuff. */
1908 /* hostapd_get_hw_features(hapd->iface); */
795baf77
AS
1909 hostapd_channel_list_updated(
1910 hapd->iface, data->channel_list_changed.initiator);
e76da505 1911 break;
c13578c3
AK
1912 case EVENT_DFS_CAC_STARTED:
1913 if (!data)
1914 break;
1915 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1916 break;
e76da505 1917#endif /* NEED_AP_MLME */
ab93fdeb
JM
1918 case EVENT_INTERFACE_ENABLED:
1919 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
f33c8606
JM
1920 if (hapd->disabled && hapd->started) {
1921 hapd->disabled = 0;
1922 /*
1923 * Try to re-enable interface if the driver stopped it
1924 * when the interface got disabled.
1925 */
567df550
HW
1926 if (hapd->wpa_auth)
1927 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1928 else
1929 hostapd_reconfig_encryption(hapd);
f33c8606
JM
1930 hapd->reenable_beacon = 1;
1931 ieee802_11_set_beacon(hapd);
10de40c0
ZK
1932#ifdef NEED_AP_MLME
1933 } else if (hapd->disabled && hapd->iface->cac_started) {
1934 wpa_printf(MSG_DEBUG, "DFS: restarting pending CAC");
1935 hostapd_handle_dfs(hapd->iface);
1936#endif /* NEED_AP_MLME */
f33c8606 1937 }
ab93fdeb
JM
1938 break;
1939 case EVENT_INTERFACE_DISABLED:
c165cb40 1940 hostapd_free_stas(hapd);
ab93fdeb 1941 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
f33c8606 1942 hapd->disabled = 1;
ab93fdeb 1943 break;
16689c7c
PX
1944#ifdef CONFIG_ACS
1945 case EVENT_ACS_CHANNEL_SELECTED:
857d9422
MM
1946 hostapd_acs_channel_selected(hapd,
1947 &data->acs_selected_channels);
16689c7c
PX
1948 break;
1949#endif /* CONFIG_ACS */
ec2b5173
T
1950 case EVENT_STATION_OPMODE_CHANGED:
1951 hostapd_event_sta_opmode_changed(hapd, data->sta_opmode.addr,
1952 data->sta_opmode.smps_mode,
1953 data->sta_opmode.chan_width,
1954 data->sta_opmode.rx_nss);
1955 break;
1952b626
BP
1956 case EVENT_WDS_STA_INTERFACE_STATUS:
1957 hostapd_event_wds_sta_interface_status(
1958 hapd, data->wds_sta_interface.istatus,
1959 data->wds_sta_interface.ifname,
1960 data->wds_sta_interface.sta_addr);
1961 break;
ad1e68e6
JM
1962 default:
1963 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1964 break;
1965 }
1966}
f8b1f695 1967
45e3fc72
RM
1968
1969void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
1970 union wpa_event_data *data)
1971{
1972 struct hapd_interfaces *interfaces = ctx;
1973 struct hostapd_data *hapd;
1974
1975 if (event != EVENT_INTERFACE_STATUS)
1976 return;
1977
1978 hapd = hostapd_get_iface(interfaces, data->interface_status.ifname);
1979 if (hapd && hapd->driver && hapd->driver->get_ifindex &&
1980 hapd->drv_priv) {
1981 unsigned int ifindex;
1982
1983 ifindex = hapd->driver->get_ifindex(hapd->drv_priv);
1984 if (ifindex != data->interface_status.ifindex) {
1985 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1986 "interface status ifindex %d mismatch (%d)",
1987 ifindex, data->interface_status.ifindex);
1988 return;
1989 }
1990 }
1991 if (hapd)
1992 wpa_supplicant_event(hapd, event, data);
1993}
1994
f8b1f695 1995#endif /* HOSTAPD */