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