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 "common/sae.h"
20 #include "common/hw_features_common.h"
21 #include "common/nan_de.h"
22 #include "crypto/random.h"
23 #include "p2p/p2p.h"
24 #include "wps/wps.h"
25 #include "fst/fst.h"
26 #include "wnm_ap.h"
27 #include "hostapd.h"
28 #include "ieee802_11.h"
29 #include "ieee802_11_auth.h"
30 #include "sta_info.h"
31 #include "accounting.h"
32 #include "tkip_countermeasures.h"
33 #include "ieee802_1x.h"
34 #include "wpa_auth.h"
35 #include "wps_hostapd.h"
36 #include "ap_drv_ops.h"
37 #include "ap_config.h"
38 #include "ap_mlme.h"
39 #include "hw_features.h"
40 #include "dfs.h"
41 #include "beacon.h"
42 #include "mbo_ap.h"
43 #include "dpp_hostapd.h"
44 #include "fils_hlp.h"
45 #include "neighbor_db.h"
46 #include "nan_usd_ap.h"
47 
48 
49 #ifdef CONFIG_FILS
hostapd_notify_assoc_fils_finish(struct hostapd_data * hapd,struct sta_info * sta)50 void hostapd_notify_assoc_fils_finish(struct hostapd_data *hapd,
51 				      struct sta_info *sta)
52 {
53 	u16 reply_res = WLAN_STATUS_SUCCESS;
54 	struct ieee802_11_elems elems;
55 	u8 buf[IEEE80211_MAX_MMPDU_SIZE], *p = buf;
56 	int new_assoc;
57 	bool updated;
58 
59 	wpa_printf(MSG_DEBUG, "%s FILS: Finish association with " MACSTR,
60 		   __func__, MAC2STR(sta->addr));
61 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
62 	if (!sta->fils_pending_assoc_req)
63 		return;
64 
65 	if (ieee802_11_parse_elems(sta->fils_pending_assoc_req,
66 				   sta->fils_pending_assoc_req_len, &elems,
67 				   0) == ParseFailed ||
68 	    !elems.fils_session) {
69 		wpa_printf(MSG_DEBUG, "%s failed to find FILS Session element",
70 			   __func__);
71 		return;
72 	}
73 
74 	p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
75 					   elems.fils_session,
76 					   sta->fils_hlp_resp);
77 	if (!p)
78 		return;
79 
80 	reply_res = hostapd_sta_assoc(hapd, sta->addr,
81 				      sta->fils_pending_assoc_is_reassoc,
82 				      WLAN_STATUS_SUCCESS,
83 				      buf, p - buf);
84 	updated = ap_sta_set_authorized_flag(hapd, sta, 1);
85 	new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
86 	sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
87 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
88 	hostapd_set_sta_flags(hapd, sta);
89 	if (updated)
90 		ap_sta_set_authorized_event(hapd, sta, 1);
91 	wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
92 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
93 	hostapd_new_assoc_sta(hapd, sta, !new_assoc);
94 	os_free(sta->fils_pending_assoc_req);
95 	sta->fils_pending_assoc_req = NULL;
96 	sta->fils_pending_assoc_req_len = 0;
97 	wpabuf_free(sta->fils_hlp_resp);
98 	sta->fils_hlp_resp = NULL;
99 	wpabuf_free(sta->hlp_dhcp_discover);
100 	sta->hlp_dhcp_discover = NULL;
101 	fils_hlp_deinit(hapd);
102 
103 	/*
104 	 * Remove the station in case transmission of a success response fails
105 	 * (the STA was added associated to the driver) or if the station was
106 	 * previously added unassociated.
107 	 */
108 	if (reply_res != WLAN_STATUS_SUCCESS || sta->added_unassoc) {
109 		hostapd_drv_sta_remove(hapd, sta->addr);
110 		sta->added_unassoc = 0;
111 	}
112 }
113 #endif /* CONFIG_FILS */
114 
115 
check_sa_query_need(struct hostapd_data * hapd,struct sta_info * sta)116 static bool check_sa_query_need(struct hostapd_data *hapd, struct sta_info *sta)
117 {
118 	if ((sta->flags &
119 	     (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED)) !=
120 	    (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED))
121 		return false;
122 
123 	if (!sta->sa_query_timed_out && sta->sa_query_count > 0)
124 		ap_check_sa_query_timeout(hapd, sta);
125 
126 	if (!sta->sa_query_timed_out && (sta->auth_alg != WLAN_AUTH_FT)) {
127 		/*
128 		 * STA has already been associated with MFP and SA Query timeout
129 		 * has not been reached. Reject the association attempt
130 		 * temporarily and start SA Query, if one is not pending.
131 		 */
132 		if (sta->sa_query_count == 0)
133 			ap_sta_start_sa_query(hapd, sta);
134 
135 		return true;
136 	}
137 
138 	return false;
139 }
140 
141 
142 #ifdef CONFIG_IEEE80211BE
hostapd_update_sta_links_status(struct hostapd_data * hapd,struct sta_info * sta,const u8 * resp_ies,size_t resp_ies_len)143 static int hostapd_update_sta_links_status(struct hostapd_data *hapd,
144 					   struct sta_info *sta,
145 					   const u8 *resp_ies,
146 					   size_t resp_ies_len)
147 {
148 	struct mld_info *info = &sta->mld_info;
149 	struct wpabuf *mlebuf;
150 	const u8 *mle, *pos;
151 	struct ieee802_11_elems elems;
152 	size_t mle_len, rem_len;
153 	int ret = 0;
154 
155 	if (!resp_ies) {
156 		wpa_printf(MSG_DEBUG,
157 			   "MLO: (Re)Association Response frame elements not available");
158 		return -1;
159 	}
160 
161 	if (ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 0) ==
162 	    ParseFailed) {
163 		wpa_printf(MSG_DEBUG,
164 			   "MLO: Failed to parse (Re)Association Response frame elements");
165 		return -1;
166 	}
167 
168 	mlebuf = ieee802_11_defrag(elems.basic_mle, elems.basic_mle_len, true);
169 	if (!mlebuf) {
170 		wpa_printf(MSG_ERROR,
171 			   "MLO: Basic Multi-Link element not found in (Re)Association Response frame");
172 		return -1;
173 	}
174 
175 	mle = wpabuf_head(mlebuf);
176 	mle_len = wpabuf_len(mlebuf);
177 	if (mle_len < MULTI_LINK_CONTROL_LEN + 1 ||
178 	    mle_len - MULTI_LINK_CONTROL_LEN < mle[MULTI_LINK_CONTROL_LEN]) {
179 		wpa_printf(MSG_ERROR,
180 			   "MLO: Invalid Multi-Link element in (Re)Association Response frame");
181 		ret = -1;
182 		goto out;
183 	}
184 
185 	/* Skip Common Info */
186 	pos = mle + MULTI_LINK_CONTROL_LEN + mle[MULTI_LINK_CONTROL_LEN];
187 	rem_len = mle_len -
188 		(MULTI_LINK_CONTROL_LEN + mle[MULTI_LINK_CONTROL_LEN]);
189 
190 	/* Parse Subelements */
191 	while (rem_len > 2) {
192 		size_t ie_len = 2 + pos[1];
193 
194 		if (rem_len < ie_len)
195 			break;
196 
197 		if (pos[0] == MULTI_LINK_SUB_ELEM_ID_PER_STA_PROFILE) {
198 			u8 link_id;
199 			const u8 *sta_profile;
200 			size_t sta_profile_len;
201 			u16 sta_ctrl;
202 
203 			if (pos[1] < BASIC_MLE_STA_CTRL_LEN + 1) {
204 				wpa_printf(MSG_DEBUG,
205 					   "MLO: Invalid per-STA profile IE");
206 				goto next_subelem;
207 			}
208 
209 			sta_profile_len = pos[1];
210 			sta_profile = &pos[2];
211 			sta_ctrl = WPA_GET_LE16(sta_profile);
212 			link_id = sta_ctrl & BASIC_MLE_STA_CTRL_LINK_ID_MASK;
213 			if (link_id >= MAX_NUM_MLD_LINKS) {
214 				wpa_printf(MSG_DEBUG,
215 					   "MLO: Invalid link ID in per-STA profile IE");
216 				goto next_subelem;
217 			}
218 
219 			/* Skip STA Control and STA Info */
220 			if (sta_profile_len - BASIC_MLE_STA_CTRL_LEN <
221 			    sta_profile[BASIC_MLE_STA_CTRL_LEN]) {
222 				wpa_printf(MSG_DEBUG,
223 					   "MLO: Invalid STA info in per-STA profile IE");
224 				goto next_subelem;
225 			}
226 
227 			sta_profile_len = sta_profile_len -
228 				(BASIC_MLE_STA_CTRL_LEN +
229 				 sta_profile[BASIC_MLE_STA_CTRL_LEN]);
230 			sta_profile = sta_profile + BASIC_MLE_STA_CTRL_LEN +
231 				sta_profile[BASIC_MLE_STA_CTRL_LEN];
232 
233 			/* Skip Capabilities Information field */
234 			if (sta_profile_len < 2)
235 				goto next_subelem;
236 			sta_profile_len -= 2;
237 			sta_profile += 2;
238 
239 			/* Get status of the link */
240 			info->links[link_id].status = WPA_GET_LE16(sta_profile);
241 		}
242 next_subelem:
243 		pos += ie_len;
244 		rem_len -= ie_len;
245 	}
246 
247 out:
248 	wpabuf_free(mlebuf);
249 	return ret;
250 }
251 #endif /* CONFIG_IEEE80211BE */
252 
253 
254 #if defined(HOSTAPD) || defined(CONFIG_IEEE80211BE)
hostapd_find_by_sta(struct hostapd_iface * iface,const u8 * src,bool rsn,struct sta_info ** sta_ret)255 static struct hostapd_data * hostapd_find_by_sta(struct hostapd_iface *iface,
256 						 const u8 *src, bool rsn,
257 						 struct sta_info **sta_ret)
258 {
259 	struct hostapd_data *hapd;
260 	struct sta_info *sta;
261 	unsigned int j;
262 
263 	if (sta_ret)
264 		*sta_ret = NULL;
265 
266 	for (j = 0; j < iface->num_bss; j++) {
267 		hapd = iface->bss[j];
268 		sta = ap_get_sta(hapd, src);
269 		if (sta && (sta->flags & WLAN_STA_ASSOC) &&
270 		    (!rsn || sta->wpa_sm)) {
271 			if (sta_ret)
272 				*sta_ret = sta;
273 			return hapd;
274 		}
275 #ifdef CONFIG_IEEE80211BE
276 		if (hapd->conf->mld_ap) {
277 			struct hostapd_data *p_hapd;
278 
279 			for_each_mld_link(p_hapd, hapd) {
280 				if (p_hapd == hapd)
281 					continue;
282 
283 				sta = ap_get_sta(p_hapd, src);
284 				if (sta && (sta->flags & WLAN_STA_ASSOC) &&
285 				    (!rsn || sta->wpa_sm)) {
286 					if (sta_ret)
287 						*sta_ret = sta;
288 					return p_hapd;
289 				}
290 			}
291 		}
292 #endif /* CONFIG_IEEE80211BE */
293 	}
294 
295 	return NULL;
296 }
297 #endif /* HOSTAPD || CONFIG_IEEE80211BE */
298 
299 
hostapd_notif_assoc(struct hostapd_data * hapd,const u8 * addr,const u8 * req_ies,size_t req_ies_len,const u8 * resp_ies,size_t resp_ies_len,const u8 * link_addr,int reassoc)300 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
301 			const u8 *req_ies, size_t req_ies_len,
302 			const u8 *resp_ies, size_t resp_ies_len,
303 			const u8 *link_addr, int reassoc)
304 {
305 	struct sta_info *sta;
306 	int new_assoc;
307 	enum wpa_validate_result res;
308 	struct ieee802_11_elems elems;
309 	const u8 *ie;
310 	size_t ielen;
311 	u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
312 	u8 *p = buf;
313 	u16 reason = WLAN_REASON_UNSPECIFIED;
314 	int status = WLAN_STATUS_SUCCESS;
315 	const u8 *p2p_dev_addr = NULL;
316 #ifdef CONFIG_OWE
317 	struct hostapd_iface *iface = hapd->iface;
318 #endif /* CONFIG_OWE */
319 	bool updated = false;
320 
321 	if (addr == NULL) {
322 		/*
323 		 * This could potentially happen with unexpected event from the
324 		 * driver wrapper. This was seen at least in one case where the
325 		 * driver ended up being set to station mode while hostapd was
326 		 * running, so better make sure we stop processing such an
327 		 * event here.
328 		 */
329 		wpa_printf(MSG_DEBUG,
330 			   "hostapd_notif_assoc: Skip event with no address");
331 		return -1;
332 	}
333 
334 	if (is_multicast_ether_addr(addr) ||
335 	    is_zero_ether_addr(addr) ||
336 	    ether_addr_equal(addr, hapd->own_addr)) {
337 		/* Do not process any frames with unexpected/invalid SA so that
338 		 * we do not add any state for unexpected STA addresses or end
339 		 * up sending out frames to unexpected destination. */
340 		wpa_printf(MSG_DEBUG, "%s: Invalid SA=" MACSTR
341 			   " in received indication - ignore this indication silently",
342 			   __func__, MAC2STR(addr));
343 		return 0;
344 	}
345 
346 	random_add_randomness(addr, ETH_ALEN);
347 
348 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
349 		       HOSTAPD_LEVEL_INFO, "associated");
350 
351 	if (ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0) ==
352 	    ParseFailed) {
353 		wpa_printf(MSG_DEBUG, "%s: Could not parse elements", __func__);
354 		return -1;
355 	}
356 
357 	if (elems.wps_ie) {
358 		ie = elems.wps_ie - 2;
359 		ielen = elems.wps_ie_len + 2;
360 		wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
361 	} else if (elems.rsn_ie) {
362 		ie = elems.rsn_ie - 2;
363 		ielen = elems.rsn_ie_len + 2;
364 		wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
365 	} else if (elems.wpa_ie) {
366 		ie = elems.wpa_ie - 2;
367 		ielen = elems.wpa_ie_len + 2;
368 		wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
369 	} else {
370 		ie = NULL;
371 		ielen = 0;
372 		wpa_printf(MSG_DEBUG,
373 			   "STA did not include WPS/RSN/WPA IE in (Re)AssocReq");
374 	}
375 
376 	sta = ap_get_sta(hapd, addr);
377 	if (sta) {
378 		ap_sta_no_session_timeout(hapd, sta);
379 		accounting_sta_stop(hapd, sta);
380 
381 		/*
382 		 * Make sure that the previously registered inactivity timer
383 		 * will not remove the STA immediately.
384 		 */
385 		sta->timeout_next = STA_NULLFUNC;
386 	} else {
387 		sta = ap_sta_add(hapd, addr);
388 		if (sta == NULL) {
389 			hostapd_drv_sta_disassoc(hapd, addr,
390 						 WLAN_REASON_DISASSOC_AP_BUSY);
391 			return -1;
392 		}
393 	}
394 
395 	if (hapd->conf->wpa && check_sa_query_need(hapd, sta)) {
396 		status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
397 		p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
398 		hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
399 
400 		return 0;
401 	}
402 
403 #ifdef CONFIG_IEEE80211BE
404 	if (link_addr) {
405 		struct mld_info *info = &sta->mld_info;
406 		int i, num_valid_links = 0;
407 		u8 link_id = hapd->mld_link_id;
408 
409 		ap_sta_set_mld(sta, true);
410 		sta->mld_assoc_link_id = link_id;
411 		os_memcpy(info->common_info.mld_addr, addr, ETH_ALEN);
412 		info->links[link_id].valid = true;
413 		os_memcpy(info->links[link_id].peer_addr, link_addr, ETH_ALEN);
414 		os_memcpy(info->links[link_id].local_addr, hapd->own_addr,
415 			  ETH_ALEN);
416 
417 		if (!elems.basic_mle ||
418 		    hostapd_process_ml_assoc_req(hapd, &elems, sta) !=
419 		    WLAN_STATUS_SUCCESS) {
420 			reason = WLAN_REASON_UNSPECIFIED;
421 			wpa_printf(MSG_DEBUG,
422 				   "Failed to get STA non-assoc links info");
423 			goto fail;
424 		}
425 
426 		for (i = 0 ; i < MAX_NUM_MLD_LINKS; i++) {
427 			if (info->links[i].valid)
428 				num_valid_links++;
429 		}
430 		if (num_valid_links > 1 &&
431 		    hostapd_update_sta_links_status(hapd, sta, resp_ies,
432 						    resp_ies_len)) {
433 			wpa_printf(MSG_DEBUG,
434 				   "Failed to get STA non-assoc links status info");
435 			reason = WLAN_REASON_UNSPECIFIED;
436 			goto fail;
437 		}
438 	}
439 #endif /* CONFIG_IEEE80211BE */
440 
441 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
442 
443 	/*
444 	 * ACL configurations to the drivers (implementing AP SME and ACL
445 	 * offload) without hostapd's knowledge, can result in a disconnection
446 	 * though the driver accepts the connection. Skip the hostapd check for
447 	 * ACL if the driver supports ACL offload to avoid potentially
448 	 * conflicting ACL rules.
449 	 */
450 	if (hapd->iface->drv_max_acl_mac_addrs == 0 &&
451 	    hostapd_check_acl(hapd, addr, NULL) != HOSTAPD_ACL_ACCEPT) {
452 		wpa_printf(MSG_INFO, "STA " MACSTR " not allowed to connect",
453 			   MAC2STR(addr));
454 		reason = WLAN_REASON_UNSPECIFIED;
455 		goto fail;
456 	}
457 
458 #ifdef CONFIG_P2P
459 	if (elems.p2p) {
460 		wpabuf_free(sta->p2p_ie);
461 		sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
462 							  P2P_IE_VENDOR_TYPE);
463 		if (sta->p2p_ie)
464 			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
465 	}
466 #endif /* CONFIG_P2P */
467 
468 #ifdef NEED_AP_MLME
469 	if (elems.ht_capabilities &&
470 	    (hapd->iface->conf->ht_capab &
471 	     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
472 		struct ieee80211_ht_capabilities *ht_cap =
473 			(struct ieee80211_ht_capabilities *)
474 			elems.ht_capabilities;
475 
476 		if (le_to_host16(ht_cap->ht_capabilities_info) &
477 		    HT_CAP_INFO_40MHZ_INTOLERANT)
478 			ht40_intolerant_add(hapd->iface, sta);
479 	}
480 #endif /* NEED_AP_MLME */
481 
482 	check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
483 
484 #ifdef CONFIG_HS20
485 	wpabuf_free(sta->hs20_ie);
486 	if (elems.hs20 && elems.hs20_len > 4) {
487 		sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
488 						 elems.hs20_len - 4);
489 	} else
490 		sta->hs20_ie = NULL;
491 
492 	wpabuf_free(sta->roaming_consortium);
493 	if (elems.roaming_cons_sel)
494 		sta->roaming_consortium = wpabuf_alloc_copy(
495 			elems.roaming_cons_sel + 4,
496 			elems.roaming_cons_sel_len - 4);
497 	else
498 		sta->roaming_consortium = NULL;
499 #endif /* CONFIG_HS20 */
500 
501 #ifdef CONFIG_FST
502 	wpabuf_free(sta->mb_ies);
503 	if (hapd->iface->fst)
504 		sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
505 	else
506 		sta->mb_ies = NULL;
507 #endif /* CONFIG_FST */
508 
509 	mbo_ap_check_sta_assoc(hapd, sta, &elems);
510 
511 	ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
512 				    elems.supp_op_classes_len);
513 
514 	if (hapd->conf->wpa) {
515 		if (ie == NULL || ielen == 0) {
516 #ifdef CONFIG_WPS
517 			if (hapd->conf->wps_state) {
518 				wpa_printf(MSG_DEBUG,
519 					   "STA did not include WPA/RSN IE in (Re)Association Request - possible WPS use");
520 				sta->flags |= WLAN_STA_MAYBE_WPS;
521 				goto skip_wpa_check;
522 			}
523 #endif /* CONFIG_WPS */
524 
525 			wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
526 			reason = WLAN_REASON_INVALID_IE;
527 			status = WLAN_STATUS_INVALID_IE;
528 			goto fail;
529 		}
530 #ifdef CONFIG_WPS
531 		if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
532 		    os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
533 			struct wpabuf *wps;
534 
535 			sta->flags |= WLAN_STA_WPS;
536 			wps = ieee802_11_vendor_ie_concat(ie, ielen,
537 							  WPS_IE_VENDOR_TYPE);
538 			if (wps) {
539 				if (wps_is_20(wps)) {
540 					wpa_printf(MSG_DEBUG,
541 						   "WPS: STA supports WPS 2.0");
542 					sta->flags |= WLAN_STA_WPS2;
543 				}
544 				wpabuf_free(wps);
545 			}
546 			goto skip_wpa_check;
547 		}
548 #endif /* CONFIG_WPS */
549 
550 		if (sta->wpa_sm == NULL)
551 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
552 							sta->addr,
553 							p2p_dev_addr);
554 		if (sta->wpa_sm == NULL) {
555 			wpa_printf(MSG_ERROR,
556 				   "Failed to initialize WPA state machine");
557 			return -1;
558 		}
559 		wpa_auth_set_rsn_selection(sta->wpa_sm, elems.rsn_selection,
560 					   elems.rsn_selection_len);
561 #ifdef CONFIG_IEEE80211BE
562 		if (ap_sta_is_mld(hapd, sta)) {
563 			wpa_printf(MSG_DEBUG,
564 				   "MLD: Set ML info in RSN Authenticator");
565 			wpa_auth_set_ml_info(sta->wpa_sm,
566 					     sta->mld_assoc_link_id,
567 					     &sta->mld_info);
568 		}
569 #endif /* CONFIG_IEEE80211BE */
570 		res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
571 					  hapd->iface->freq,
572 					  ie, ielen,
573 					  elems.rsnxe ? elems.rsnxe - 2 : NULL,
574 					  elems.rsnxe ? elems.rsnxe_len + 2 : 0,
575 					  elems.mdie, elems.mdie_len,
576 					  elems.owe_dh, elems.owe_dh_len, NULL,
577 					  ap_sta_is_mld(hapd, sta));
578 		reason = WLAN_REASON_INVALID_IE;
579 		status = WLAN_STATUS_INVALID_IE;
580 		switch (res) {
581 		case WPA_IE_OK:
582 			reason = WLAN_REASON_UNSPECIFIED;
583 			status = WLAN_STATUS_SUCCESS;
584 			break;
585 		case WPA_INVALID_IE:
586 			reason = WLAN_REASON_INVALID_IE;
587 			status = WLAN_STATUS_INVALID_IE;
588 			break;
589 		case WPA_INVALID_GROUP:
590 			reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
591 			status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
592 			break;
593 		case WPA_INVALID_PAIRWISE:
594 			reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
595 			status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
596 			break;
597 		case WPA_INVALID_AKMP:
598 			reason = WLAN_REASON_AKMP_NOT_VALID;
599 			status = WLAN_STATUS_AKMP_NOT_VALID;
600 			break;
601 		case WPA_NOT_ENABLED:
602 			reason = WLAN_REASON_INVALID_IE;
603 			status = WLAN_STATUS_INVALID_IE;
604 			break;
605 		case WPA_ALLOC_FAIL:
606 			reason = WLAN_REASON_UNSPECIFIED;
607 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
608 			break;
609 		case WPA_MGMT_FRAME_PROTECTION_VIOLATION:
610 			reason = WLAN_REASON_INVALID_IE;
611 			status = WLAN_STATUS_INVALID_IE;
612 			break;
613 		case WPA_INVALID_MGMT_GROUP_CIPHER:
614 			reason = WLAN_REASON_CIPHER_SUITE_REJECTED;
615 			status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
616 			break;
617 		case WPA_INVALID_MDIE:
618 			reason = WLAN_REASON_INVALID_MDE;
619 			status = WLAN_STATUS_INVALID_MDIE;
620 			break;
621 		case WPA_INVALID_PROTO:
622 			reason = WLAN_REASON_INVALID_IE;
623 			status = WLAN_STATUS_INVALID_IE;
624 			break;
625 		case WPA_INVALID_PMKID:
626 			reason = WLAN_REASON_INVALID_PMKID;
627 			status = WLAN_STATUS_INVALID_PMKID;
628 			break;
629 		case WPA_DENIED_OTHER_REASON:
630 			reason = WLAN_REASON_UNSPECIFIED;
631 			status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
632 			break;
633 		}
634 		if (status != WLAN_STATUS_SUCCESS) {
635 			wpa_printf(MSG_DEBUG,
636 				   "WPA/RSN information element rejected? (res %u)",
637 				   res);
638 			wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
639 			goto fail;
640 		}
641 
642 		if (wpa_auth_uses_mfp(sta->wpa_sm))
643 			sta->flags |= WLAN_STA_MFP;
644 		else
645 			sta->flags &= ~WLAN_STA_MFP;
646 
647 		if (wpa_auth_uses_spp_amsdu(sta->wpa_sm))
648 			sta->flags |= WLAN_STA_SPP_AMSDU;
649 		else
650 			sta->flags &= ~WLAN_STA_SPP_AMSDU;
651 
652 #ifdef CONFIG_IEEE80211R_AP
653 		if (sta->auth_alg == WLAN_AUTH_FT) {
654 			status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
655 							 req_ies_len);
656 			if (status != WLAN_STATUS_SUCCESS) {
657 				if (status == WLAN_STATUS_INVALID_PMKID)
658 					reason = WLAN_REASON_INVALID_IE;
659 				if (status == WLAN_STATUS_INVALID_MDIE)
660 					reason = WLAN_REASON_INVALID_IE;
661 				if (status == WLAN_STATUS_INVALID_FTIE)
662 					reason = WLAN_REASON_INVALID_IE;
663 				goto fail;
664 			}
665 		}
666 #endif /* CONFIG_IEEE80211R_AP */
667 #ifdef CONFIG_SAE
668 		if (hapd->conf->sae_pwe == SAE_PWE_BOTH &&
669 		    sta->auth_alg == WLAN_AUTH_SAE &&
670 		    sta->sae && !sta->sae->h2e &&
671 		    ieee802_11_rsnx_capab_len(elems.rsnxe, elems.rsnxe_len,
672 					      WLAN_RSNX_CAPAB_SAE_H2E)) {
673 			wpa_printf(MSG_INFO, "SAE: " MACSTR
674 				   " indicates support for SAE H2E, but did not use it",
675 				   MAC2STR(sta->addr));
676 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
677 			reason = WLAN_REASON_UNSPECIFIED;
678 			goto fail;
679 		}
680 #endif /* CONFIG_SAE */
681 
682 		wpa_auth_set_ssid_protection(
683 			sta->wpa_sm,
684 			hapd->conf->ssid_protection &&
685 			ieee802_11_rsnx_capab_len(
686 				elems.rsnxe, elems.rsnxe_len,
687 				WLAN_RSNX_CAPAB_SSID_PROTECTION));
688 	} else if (hapd->conf->wps_state) {
689 #ifdef CONFIG_WPS
690 		struct wpabuf *wps;
691 
692 		if (req_ies)
693 			wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
694 							  WPS_IE_VENDOR_TYPE);
695 		else
696 			wps = NULL;
697 #ifdef CONFIG_WPS_STRICT
698 		if (wps && wps_validate_assoc_req(wps) < 0) {
699 			reason = WLAN_REASON_INVALID_IE;
700 			status = WLAN_STATUS_INVALID_IE;
701 			wpabuf_free(wps);
702 			goto fail;
703 		}
704 #endif /* CONFIG_WPS_STRICT */
705 		if (wps) {
706 			sta->flags |= WLAN_STA_WPS;
707 			if (wps_is_20(wps)) {
708 				wpa_printf(MSG_DEBUG,
709 					   "WPS: STA supports WPS 2.0");
710 				sta->flags |= WLAN_STA_WPS2;
711 			}
712 		} else
713 			sta->flags |= WLAN_STA_MAYBE_WPS;
714 		wpabuf_free(wps);
715 #endif /* CONFIG_WPS */
716 	}
717 #ifdef CONFIG_WPS
718 skip_wpa_check:
719 #endif /* CONFIG_WPS */
720 
721 #ifdef CONFIG_MBO
722 	if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
723 	    elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
724 	    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
725 		wpa_printf(MSG_INFO,
726 			   "MBO: Reject WPA2 association without PMF");
727 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
728 	}
729 #endif /* CONFIG_MBO */
730 
731 #ifdef CONFIG_IEEE80211R_AP
732 	p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
733 					sta->auth_alg, req_ies, req_ies_len,
734 					!elems.rsnxe);
735 	if (!p) {
736 		wpa_printf(MSG_DEBUG, "FT: Failed to write AssocResp IEs");
737 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
738 	}
739 #endif /* CONFIG_IEEE80211R_AP */
740 
741 #ifdef CONFIG_FILS
742 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
743 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
744 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
745 		int delay_assoc = 0;
746 
747 		if (!req_ies)
748 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
749 
750 		if (!wpa_fils_validate_fils_session(sta->wpa_sm, req_ies,
751 						    req_ies_len,
752 						    sta->fils_session)) {
753 			wpa_printf(MSG_DEBUG,
754 				   "FILS: Session validation failed");
755 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
756 		}
757 
758 		res = wpa_fils_validate_key_confirm(sta->wpa_sm, req_ies,
759 						    req_ies_len);
760 		if (res < 0) {
761 			wpa_printf(MSG_DEBUG,
762 				   "FILS: Key Confirm validation failed");
763 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
764 		}
765 
766 		if (fils_process_hlp(hapd, sta, req_ies, req_ies_len) > 0) {
767 			wpa_printf(MSG_DEBUG,
768 				   "FILS: Delaying Assoc Response (HLP)");
769 			delay_assoc = 1;
770 		} else {
771 			wpa_printf(MSG_DEBUG,
772 				   "FILS: Going ahead with Assoc Response (no HLP)");
773 		}
774 
775 		if (sta) {
776 			wpa_printf(MSG_DEBUG, "FILS: HLP callback cleanup");
777 			eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
778 			os_free(sta->fils_pending_assoc_req);
779 			sta->fils_pending_assoc_req = NULL;
780 			sta->fils_pending_assoc_req_len = 0;
781 			wpabuf_free(sta->fils_hlp_resp);
782 			sta->fils_hlp_resp = NULL;
783 			sta->fils_drv_assoc_finish = 0;
784 		}
785 
786 		if (sta && delay_assoc && status == WLAN_STATUS_SUCCESS) {
787 			u8 *req_tmp;
788 
789 			req_tmp = os_malloc(req_ies_len);
790 			if (!req_tmp) {
791 				wpa_printf(MSG_DEBUG,
792 					   "FILS: buffer allocation failed for assoc req");
793 				goto fail;
794 			}
795 			os_memcpy(req_tmp, req_ies, req_ies_len);
796 			sta->fils_pending_assoc_req = req_tmp;
797 			sta->fils_pending_assoc_req_len = req_ies_len;
798 			sta->fils_pending_assoc_is_reassoc = reassoc;
799 			sta->fils_drv_assoc_finish = 1;
800 			wpa_printf(MSG_DEBUG,
801 				   "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
802 				   MACSTR, MAC2STR(sta->addr));
803 			eloop_register_timeout(
804 				0, hapd->conf->fils_hlp_wait_time * 1024,
805 				fils_hlp_timeout, hapd, sta);
806 			return 0;
807 		}
808 		p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
809 						   elems.fils_session,
810 						   sta->fils_hlp_resp);
811 		if (!p)
812 			goto fail;
813 
814 		wpa_hexdump(MSG_DEBUG, "FILS Assoc Resp BUF (IEs)",
815 			    buf, p - buf);
816 	}
817 #endif /* CONFIG_FILS */
818 
819 #ifdef CONFIG_OWE
820 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
821 	    !(iface->drv_flags2 & WPA_DRIVER_FLAGS2_OWE_OFFLOAD_AP) &&
822 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
823 	    elems.owe_dh) {
824 		u8 *npos;
825 		u16 ret_status;
826 
827 		npos = owe_assoc_req_process(hapd, sta,
828 					     elems.owe_dh, elems.owe_dh_len,
829 					     p, sizeof(buf) - (p - buf),
830 					     &ret_status);
831 		status = ret_status;
832 		if (npos)
833 			p = npos;
834 
835 		if (!npos &&
836 		    status == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
837 			hostapd_sta_assoc(hapd, addr, reassoc, ret_status, buf,
838 					  p - buf);
839 			return 0;
840 		}
841 
842 		if (!npos || status != WLAN_STATUS_SUCCESS)
843 			goto fail;
844 	}
845 #endif /* CONFIG_OWE */
846 
847 #ifdef CONFIG_DPP2
848 		dpp_pfs_free(sta->dpp_pfs);
849 		sta->dpp_pfs = NULL;
850 
851 		if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
852 		    hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
853 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
854 		    elems.owe_dh) {
855 			sta->dpp_pfs = dpp_pfs_init(
856 				wpabuf_head(hapd->conf->dpp_netaccesskey),
857 				wpabuf_len(hapd->conf->dpp_netaccesskey));
858 			if (!sta->dpp_pfs) {
859 				wpa_printf(MSG_DEBUG,
860 					   "DPP: Could not initialize PFS");
861 				/* Try to continue without PFS */
862 				goto pfs_fail;
863 			}
864 
865 			if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
866 					    elems.owe_dh_len) < 0) {
867 				dpp_pfs_free(sta->dpp_pfs);
868 				sta->dpp_pfs = NULL;
869 				reason = WLAN_REASON_UNSPECIFIED;
870 				goto fail;
871 			}
872 		}
873 
874 		wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
875 				   sta->dpp_pfs->secret : NULL);
876 	pfs_fail:
877 #endif /* CONFIG_DPP2 */
878 
879 	if (elems.rrm_enabled &&
880 	    elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
881 	    os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
882 		      sizeof(sta->rrm_enabled_capa));
883 
884 #if defined(CONFIG_IEEE80211R_AP) || defined(CONFIG_FILS) || defined(CONFIG_OWE)
885 	hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
886 
887 	if (sta->auth_alg == WLAN_AUTH_FT ||
888 	    sta->auth_alg == WLAN_AUTH_FILS_SK ||
889 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
890 	    sta->auth_alg == WLAN_AUTH_FILS_PK)
891 		updated = ap_sta_set_authorized_flag(hapd, sta, 1);
892 #else /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
893 	/* Keep compiler silent about unused variables */
894 	if (status) {
895 	}
896 #endif /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
897 
898 	new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
899 	sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
900 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
901 
902 	hostapd_set_sta_flags(hapd, sta);
903 
904 #ifdef CONFIG_IEEE80211BE
905 	if (hostapd_process_assoc_ml_info(hapd, sta, req_ies, req_ies_len,
906 					  !!reassoc, WLAN_STATUS_SUCCESS,
907 					  true)) {
908 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
909 		reason = WLAN_REASON_UNSPECIFIED;
910 		goto fail;
911 	}
912 #endif /* CONFIG_IEEE80211BE */
913 
914 	if (updated)
915 		ap_sta_set_authorized_event(hapd, sta, 1);
916 
917 	if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
918 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
919 #ifdef CONFIG_FILS
920 	else if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
921 		 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
922 		 sta->auth_alg == WLAN_AUTH_FILS_PK)
923 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
924 #endif /* CONFIG_FILS */
925 	else
926 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
927 
928 	hostapd_new_assoc_sta(hapd, sta, !new_assoc);
929 
930 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
931 
932 #ifdef CONFIG_P2P
933 	if (req_ies) {
934 		p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
935 				      req_ies, req_ies_len);
936 	}
937 #endif /* CONFIG_P2P */
938 
939 	if (elems.wfa_capab)
940 		hostapd_wfa_capab(hapd, sta, elems.wfa_capab,
941 				  elems.wfa_capab + elems.wfa_capab_len);
942 
943 	return 0;
944 
945 fail:
946 #ifdef CONFIG_IEEE80211R_AP
947 	if (status >= 0)
948 		hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
949 #endif /* CONFIG_IEEE80211R_AP */
950 	hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
951 	ap_free_sta(hapd, sta);
952 	return -1;
953 }
954 
955 
hostapd_remove_sta(struct hostapd_data * hapd,struct sta_info * sta)956 static void hostapd_remove_sta(struct hostapd_data *hapd, struct sta_info *sta)
957 {
958 	ap_sta_set_authorized(hapd, sta, 0);
959 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
960 	hostapd_set_sta_flags(hapd, sta);
961 	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
962 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
963 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
964 	ap_free_sta(hapd, sta);
965 }
966 
967 
968 #ifdef CONFIG_IEEE80211BE
hostapd_notif_disassoc_mld(struct hostapd_data * assoc_hapd,struct sta_info * sta,const u8 * addr)969 static void hostapd_notif_disassoc_mld(struct hostapd_data *assoc_hapd,
970 				       struct sta_info *sta,
971 				       const u8 *addr)
972 {
973 	unsigned int link_id, i;
974 	struct hostapd_data *tmp_hapd;
975 	struct hapd_interfaces *interfaces = assoc_hapd->iface->interfaces;
976 
977 	/* Remove STA entry in non-assoc links */
978 	for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
979 		if (!sta->mld_info.links[link_id].valid)
980 			continue;
981 
982 		for (i = 0; i < interfaces->count; i++) {
983 			struct sta_info *tmp_sta;
984 
985 			tmp_hapd = interfaces->iface[i]->bss[0];
986 
987 			if (!tmp_hapd->conf->mld_ap ||
988 			    assoc_hapd == tmp_hapd ||
989 			    assoc_hapd->conf->mld_id != tmp_hapd->conf->mld_id)
990 				continue;
991 
992 			tmp_sta = ap_get_sta(tmp_hapd, addr);
993 			if (tmp_sta)
994 				ap_free_sta(tmp_hapd, tmp_sta);
995 		}
996 	}
997 
998 	/* Remove STA in assoc link */
999 	hostapd_remove_sta(assoc_hapd, sta);
1000 }
1001 #endif /* CONFIG_IEEE80211BE */
1002 
1003 
hostapd_notif_disassoc(struct hostapd_data * hapd,const u8 * addr)1004 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
1005 {
1006 	struct sta_info *sta;
1007 
1008 	if (addr == NULL) {
1009 		/*
1010 		 * This could potentially happen with unexpected event from the
1011 		 * driver wrapper. This was seen at least in one case where the
1012 		 * driver ended up reporting a station mode event while hostapd
1013 		 * was running, so better make sure we stop processing such an
1014 		 * event here.
1015 		 */
1016 		wpa_printf(MSG_DEBUG,
1017 			   "hostapd_notif_disassoc: Skip event with no address");
1018 		return;
1019 	}
1020 
1021 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1022 		       HOSTAPD_LEVEL_INFO, "disassociated");
1023 
1024 	sta = ap_get_sta(hapd, addr);
1025 #ifdef CONFIG_IEEE80211BE
1026 	if (hostapd_is_mld_ap(hapd)) {
1027 		struct hostapd_data *assoc_hapd;
1028 		unsigned int i;
1029 
1030 		if (!sta) {
1031 			/* Find non-MLO cases from any of the affiliated AP
1032 			 * links. */
1033 			for (i = 0; i < hapd->iface->interfaces->count; ++i) {
1034 				struct hostapd_iface *h =
1035 					hapd->iface->interfaces->iface[i];
1036 				struct hostapd_data *h_hapd = h->bss[0];
1037 				struct hostapd_bss_config *hconf = h_hapd->conf;
1038 
1039 				if (!hconf->mld_ap ||
1040 				    hconf->mld_id != hapd->conf->mld_id)
1041 					continue;
1042 
1043 				sta = ap_get_sta(h_hapd, addr);
1044 				if (sta) {
1045 					if (!sta->mld_info.mld_sta) {
1046 						hapd = h_hapd;
1047 						goto legacy;
1048 					}
1049 					break;
1050 				}
1051 			}
1052 		} else if (!sta->mld_info.mld_sta) {
1053 			goto legacy;
1054 		}
1055 		if (!sta) {
1056 			wpa_printf(MSG_DEBUG,
1057 			   "Disassociation notification for unknown STA "
1058 			   MACSTR, MAC2STR(addr));
1059 			return;
1060 		}
1061 		sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
1062 		if (sta)
1063 			hostapd_notif_disassoc_mld(assoc_hapd, sta, addr);
1064 		return;
1065 	}
1066 
1067 legacy:
1068 #endif /* CONFIG_IEEE80211BE */
1069 	if (sta == NULL) {
1070 		wpa_printf(MSG_DEBUG,
1071 			   "Disassociation notification for unknown STA "
1072 			   MACSTR, MAC2STR(addr));
1073 		return;
1074 	}
1075 
1076 	hostapd_remove_sta(hapd, sta);
1077 }
1078 
1079 
hostapd_event_sta_low_ack(struct hostapd_data * hapd,const u8 * addr)1080 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
1081 {
1082 	struct sta_info *sta = ap_get_sta(hapd, addr);
1083 #ifdef CONFIG_IEEE80211BE
1084 	struct hostapd_data *orig_hapd = hapd;
1085 
1086 	if (!sta && hapd->conf->mld_ap) {
1087 		hapd = hostapd_find_by_sta(hapd->iface, addr, true, &sta);
1088 		if (!hapd) {
1089 			wpa_printf(MSG_DEBUG,
1090 				   "No partner link BSS found for STA " MACSTR
1091 				   " - fallback to received context",
1092 				   MAC2STR(addr));
1093 			hapd = orig_hapd;
1094 		}
1095 	}
1096 #endif /* CONFIG_IEEE80211BE */
1097 
1098 	if (!sta || !hapd->conf->disassoc_low_ack || sta->agreed_to_steer)
1099 		return;
1100 
1101 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
1102 		       HOSTAPD_LEVEL_INFO,
1103 		       "disconnected due to excessive missing ACKs");
1104 	hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
1105 	ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
1106 }
1107 
1108 
hostapd_event_sta_opmode_changed(struct hostapd_data * hapd,const u8 * addr,enum smps_mode smps_mode,enum chan_width chan_width,u8 rx_nss)1109 void hostapd_event_sta_opmode_changed(struct hostapd_data *hapd, const u8 *addr,
1110 				      enum smps_mode smps_mode,
1111 				      enum chan_width chan_width, u8 rx_nss)
1112 {
1113 	struct sta_info *sta = ap_get_sta(hapd, addr);
1114 	const char *txt;
1115 
1116 	if (!sta)
1117 		return;
1118 
1119 	switch (smps_mode) {
1120 	case SMPS_AUTOMATIC:
1121 		txt = "automatic";
1122 		break;
1123 	case SMPS_OFF:
1124 		txt = "off";
1125 		break;
1126 	case SMPS_DYNAMIC:
1127 		txt = "dynamic";
1128 		break;
1129 	case SMPS_STATIC:
1130 		txt = "static";
1131 		break;
1132 	default:
1133 		txt = NULL;
1134 		break;
1135 	}
1136 	if (txt) {
1137 		wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_SMPS_MODE_CHANGED
1138 			MACSTR " %s", MAC2STR(addr), txt);
1139 	}
1140 
1141 	switch (chan_width) {
1142 	case CHAN_WIDTH_20_NOHT:
1143 		txt = "20(no-HT)";
1144 		break;
1145 	case CHAN_WIDTH_20:
1146 		txt = "20";
1147 		break;
1148 	case CHAN_WIDTH_40:
1149 		txt = "40";
1150 		break;
1151 	case CHAN_WIDTH_80:
1152 		txt = "80";
1153 		break;
1154 	case CHAN_WIDTH_80P80:
1155 		txt = "80+80";
1156 		break;
1157 	case CHAN_WIDTH_160:
1158 		txt = "160";
1159 		break;
1160 	case CHAN_WIDTH_320:
1161 		txt = "320";
1162 		break;
1163 	default:
1164 		txt = NULL;
1165 		break;
1166 	}
1167 	if (txt) {
1168 		wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_MAX_BW_CHANGED
1169 			MACSTR " %s", MAC2STR(addr), txt);
1170 	}
1171 
1172 	if (rx_nss != 0xff) {
1173 		wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_N_SS_CHANGED
1174 			MACSTR " %d", MAC2STR(addr), rx_nss);
1175 	}
1176 }
1177 
1178 
hostapd_event_ch_switch(struct hostapd_data * hapd,int freq,int ht,int offset,int width,int cf1,int cf2,u16 punct_bitmap,int finished)1179 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
1180 			     int offset, int width, int cf1, int cf2,
1181 			     u16 punct_bitmap, int finished)
1182 {
1183 #ifdef NEED_AP_MLME
1184 	int channel, chwidth, is_dfs0, is_dfs;
1185 	u8 seg0_idx = 0, seg1_idx = 0, op_class, chan_no;
1186 	size_t i;
1187 
1188 	hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1189 		       HOSTAPD_LEVEL_INFO,
1190 		       "driver %s channel switch: iface->freq=%d, freq=%d, ht=%d, vht_ch=0x%x, he_ch=0x%x, eht_ch=0x%x, offset=%d, width=%d (%s), cf1=%d, cf2=%d, puncturing_bitmap=0x%x",
1191 		       finished ? "had" : "starting",
1192 		       hapd->iface->freq,
1193 		       freq, ht, hapd->iconf->ch_switch_vht_config,
1194 		       hapd->iconf->ch_switch_he_config,
1195 		       hapd->iconf->ch_switch_eht_config, offset,
1196 		       width, channel_width_to_string(width), cf1, cf2,
1197 		       punct_bitmap);
1198 
1199 	if (!hapd->iface->current_mode) {
1200 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1201 			       HOSTAPD_LEVEL_WARNING,
1202 			       "ignore channel switch since the interface is not yet ready");
1203 		return;
1204 	}
1205 
1206 	/* Check if any of configured channels require DFS */
1207 	is_dfs0 = hostapd_is_dfs_required(hapd->iface);
1208 	hapd->iface->freq = freq;
1209 
1210 	channel = hostapd_hw_get_channel(hapd, freq);
1211 	if (!channel) {
1212 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1213 			       HOSTAPD_LEVEL_WARNING,
1214 			       "driver switched to bad channel!");
1215 		return;
1216 	}
1217 
1218 	switch (width) {
1219 	case CHAN_WIDTH_80:
1220 		chwidth = CONF_OPER_CHWIDTH_80MHZ;
1221 		break;
1222 	case CHAN_WIDTH_80P80:
1223 		chwidth = CONF_OPER_CHWIDTH_80P80MHZ;
1224 		break;
1225 	case CHAN_WIDTH_160:
1226 		chwidth = CONF_OPER_CHWIDTH_160MHZ;
1227 		break;
1228 	case CHAN_WIDTH_320:
1229 		chwidth = CONF_OPER_CHWIDTH_320MHZ;
1230 		break;
1231 	case CHAN_WIDTH_20_NOHT:
1232 	case CHAN_WIDTH_20:
1233 	case CHAN_WIDTH_40:
1234 	default:
1235 		chwidth = CONF_OPER_CHWIDTH_USE_HT;
1236 		break;
1237 	}
1238 
1239 	/* The operating channel changed when CSA finished, so need to update
1240 	 * hw_mode for all following operations to cover the cases where the
1241 	 * driver changed the operating band. */
1242 	if (finished && hostapd_csa_update_hwmode(hapd->iface))
1243 		return;
1244 
1245 	switch (hapd->iface->current_mode->mode) {
1246 	case HOSTAPD_MODE_IEEE80211A:
1247 		if (cf1 == 5935)
1248 			seg0_idx = (cf1 - 5925) / 5;
1249 		else if (cf1 > 5950)
1250 			seg0_idx = (cf1 - 5950) / 5;
1251 		else if (cf1 > 5000)
1252 			seg0_idx = (cf1 - 5000) / 5;
1253 
1254 		if (cf2 == 5935)
1255 			seg1_idx = (cf2 - 5925) / 5;
1256 		else if (cf2 > 5950)
1257 			seg1_idx = (cf2 - 5950) / 5;
1258 		else if (cf2 > 5000)
1259 			seg1_idx = (cf2 - 5000) / 5;
1260 		break;
1261 	default:
1262 		ieee80211_freq_to_chan(cf1, &seg0_idx);
1263 		ieee80211_freq_to_chan(cf2, &seg1_idx);
1264 		break;
1265 	}
1266 
1267 	hapd->iconf->channel = channel;
1268 	hapd->iconf->ieee80211n = ht;
1269 	if (!ht)
1270 		hapd->iconf->ieee80211ac = 0;
1271 	if (hapd->iconf->ch_switch_vht_config) {
1272 		/* CHAN_SWITCH VHT config */
1273 		if (hapd->iconf->ch_switch_vht_config &
1274 		    CH_SWITCH_VHT_ENABLED)
1275 			hapd->iconf->ieee80211ac = 1;
1276 		else if (hapd->iconf->ch_switch_vht_config &
1277 			 CH_SWITCH_VHT_DISABLED)
1278 			hapd->iconf->ieee80211ac = 0;
1279 	}
1280 	if (hapd->iconf->ch_switch_he_config) {
1281 		/* CHAN_SWITCH HE config */
1282 		if (hapd->iconf->ch_switch_he_config &
1283 		    CH_SWITCH_HE_ENABLED) {
1284 			hapd->iconf->ieee80211ax = 1;
1285 			if (hapd->iface->freq > 4000 &&
1286 			    hapd->iface->freq < 5895)
1287 				hapd->iconf->ieee80211ac = 1;
1288 		}
1289 		else if (hapd->iconf->ch_switch_he_config &
1290 			 CH_SWITCH_HE_DISABLED)
1291 			hapd->iconf->ieee80211ax = 0;
1292 	}
1293 #ifdef CONFIG_IEEE80211BE
1294 	if (hapd->iconf->ch_switch_eht_config) {
1295 		/* CHAN_SWITCH EHT config */
1296 		if (hapd->iconf->ch_switch_eht_config &
1297 		    CH_SWITCH_EHT_ENABLED) {
1298 			hapd->iconf->ieee80211be = 1;
1299 			hapd->iconf->ieee80211ax = 1;
1300 			if (!is_6ghz_freq(hapd->iface->freq) &&
1301 			    hapd->iface->freq > 4000)
1302 				hapd->iconf->ieee80211ac = 1;
1303 		} else if (hapd->iconf->ch_switch_eht_config &
1304 			   CH_SWITCH_EHT_DISABLED)
1305 			hapd->iconf->ieee80211be = 0;
1306 	}
1307 #endif /* CONFIG_IEEE80211BE */
1308 	hapd->iconf->ch_switch_vht_config = 0;
1309 	hapd->iconf->ch_switch_he_config = 0;
1310 	hapd->iconf->ch_switch_eht_config = 0;
1311 
1312 	if (width == CHAN_WIDTH_40 || width == CHAN_WIDTH_80 ||
1313 	    width == CHAN_WIDTH_80P80 || width == CHAN_WIDTH_160 ||
1314 	    width == CHAN_WIDTH_320)
1315 		hapd->iconf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
1316 	else if (width == CHAN_WIDTH_20 || width == CHAN_WIDTH_20_NOHT)
1317 		hapd->iconf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
1318 
1319 	hapd->iconf->secondary_channel = offset;
1320 	if (ieee80211_freq_to_channel_ext(freq, offset, chwidth,
1321 					  &op_class, &chan_no) !=
1322 	    NUM_HOSTAPD_MODES)
1323 		hapd->iconf->op_class = op_class;
1324 	hostapd_set_oper_chwidth(hapd->iconf, chwidth);
1325 	hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, seg0_idx);
1326 	hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, seg1_idx);
1327 	/* Auto-detect new bw320_offset */
1328 	hostapd_set_and_check_bw320_offset(hapd->iconf, 0);
1329 #ifdef CONFIG_IEEE80211BE
1330 	hapd->iconf->punct_bitmap = punct_bitmap;
1331 #endif /* CONFIG_IEEE80211BE */
1332 	if (hapd->iconf->ieee80211ac) {
1333 		hapd->iconf->vht_capab &= ~VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1334 		if (chwidth == CONF_OPER_CHWIDTH_160MHZ)
1335 			hapd->iconf->vht_capab |=
1336 				VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
1337 		else if (chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
1338 			hapd->iconf->vht_capab |=
1339 				VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
1340 	}
1341 
1342 	is_dfs = ieee80211_is_dfs(freq, hapd->iface->hw_features,
1343 				  hapd->iface->num_hw_features);
1344 
1345 	wpa_msg(hapd->msg_ctx, MSG_INFO,
1346 		"%sfreq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d is_dfs0=%d dfs=%d puncturing_bitmap=0x%04x",
1347 		finished ? WPA_EVENT_CHANNEL_SWITCH :
1348 		WPA_EVENT_CHANNEL_SWITCH_STARTED,
1349 		freq, ht, offset, channel_width_to_string(width),
1350 		cf1, cf2, is_dfs0, is_dfs, punct_bitmap);
1351 	if (!finished)
1352 		return;
1353 
1354 	if (hapd->csa_in_progress &&
1355 	    freq == hapd->cs_freq_params.freq) {
1356 		hostapd_cleanup_cs_params(hapd);
1357 		ieee802_11_set_beacon(hapd);
1358 
1359 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
1360 			"freq=%d dfs=%d", freq, is_dfs);
1361 	} else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1362 		/* Complete AP configuration for the first bring up. */
1363 		if (is_dfs0 > 0 &&
1364 		    hostapd_is_dfs_required(hapd->iface) <= 0 &&
1365 		    hapd->iface->state != HAPD_IFACE_ENABLED) {
1366 			/* Fake a CAC start bit to skip setting channel */
1367 			hapd->iface->cac_started = 1;
1368 			hostapd_setup_interface_complete(hapd->iface, 0);
1369 		}
1370 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
1371 			"freq=%d dfs=%d", freq, is_dfs);
1372 	} else if (is_dfs &&
1373 		   hostapd_is_dfs_required(hapd->iface) &&
1374 		   !hostapd_is_dfs_chan_available(hapd->iface) &&
1375 		   !hapd->iface->cac_started) {
1376 		hostapd_disable_iface(hapd->iface);
1377 		hostapd_enable_iface(hapd->iface);
1378 	}
1379 
1380 	for (i = 0; i < hapd->iface->num_bss; i++)
1381 		hostapd_neighbor_set_own_report(hapd->iface->bss[i]);
1382 
1383 #ifdef CONFIG_OCV
1384 	if (hapd->conf->ocv &&
1385 	    !(hapd->iface->drv_flags2 &
1386 	      WPA_DRIVER_FLAGS2_SA_QUERY_OFFLOAD_AP)) {
1387 		struct sta_info *sta;
1388 		bool check_sa_query = false;
1389 
1390 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1391 			if (wpa_auth_uses_ocv(sta->wpa_sm) &&
1392 			    !(sta->flags & WLAN_STA_WNM_SLEEP_MODE)) {
1393 				sta->post_csa_sa_query = 1;
1394 				check_sa_query = true;
1395 			}
1396 		}
1397 
1398 		if (check_sa_query) {
1399 			wpa_printf(MSG_DEBUG,
1400 				   "OCV: Check post-CSA SA Query initiation in 15 seconds");
1401 			eloop_register_timeout(15, 0,
1402 					       hostapd_ocv_check_csa_sa_query,
1403 					       hapd, NULL);
1404 		}
1405 	}
1406 #endif /* CONFIG_OCV */
1407 #endif /* NEED_AP_MLME */
1408 }
1409 
1410 
hostapd_event_connect_failed_reason(struct hostapd_data * hapd,const u8 * addr,int reason_code)1411 void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
1412 					 const u8 *addr, int reason_code)
1413 {
1414 	switch (reason_code) {
1415 	case MAX_CLIENT_REACHED:
1416 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
1417 			MAC2STR(addr));
1418 		break;
1419 	case BLOCKED_CLIENT:
1420 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
1421 			MAC2STR(addr));
1422 		break;
1423 	}
1424 }
1425 
1426 
1427 #ifdef CONFIG_ACS
hostapd_acs_channel_selected(struct hostapd_data * hapd,struct acs_selected_channels * acs_res)1428 void hostapd_acs_channel_selected(struct hostapd_data *hapd,
1429 				  struct acs_selected_channels *acs_res)
1430 {
1431 	int ret, i;
1432 	int err = 0;
1433 	struct hostapd_channel_data *pri_chan;
1434 
1435 #ifdef CONFIG_IEEE80211BE
1436 	if (acs_res->link_id != -1) {
1437 		hapd = hostapd_mld_get_link_bss(hapd, acs_res->link_id);
1438 		if (!hapd) {
1439 			wpa_printf(MSG_ERROR,
1440 				   "MLD: Failed to get link BSS for EVENT_ACS_CHANNEL_SELECTED link_id=%d",
1441 				   acs_res->link_id);
1442 			return;
1443 		}
1444 	}
1445 #endif /* CONFIG_IEEE80211BE */
1446 
1447 	if (hapd->iconf->channel) {
1448 		wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
1449 			   hapd->iconf->channel);
1450 		return;
1451 	}
1452 
1453 	hapd->iface->freq = acs_res->pri_freq;
1454 
1455 	if (!hapd->iface->current_mode) {
1456 		for (i = 0; i < hapd->iface->num_hw_features; i++) {
1457 			struct hostapd_hw_modes *mode =
1458 				&hapd->iface->hw_features[i];
1459 
1460 			if (mode->mode == acs_res->hw_mode) {
1461 				if (hapd->iface->freq > 0 &&
1462 				    !hw_get_chan(mode->mode,
1463 						 hapd->iface->freq,
1464 						 hapd->iface->hw_features,
1465 						 hapd->iface->num_hw_features))
1466 					continue;
1467 				hapd->iface->current_mode = mode;
1468 				break;
1469 			}
1470 		}
1471 		if (!hapd->iface->current_mode) {
1472 			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1473 				       HOSTAPD_LEVEL_WARNING,
1474 				       "driver selected to bad hw_mode");
1475 			err = 1;
1476 			goto out;
1477 		}
1478 	}
1479 
1480 	if (!acs_res->pri_freq) {
1481 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1482 			       HOSTAPD_LEVEL_WARNING,
1483 			       "driver switched to bad channel");
1484 		err = 1;
1485 		goto out;
1486 	}
1487 	pri_chan = hw_get_channel_freq(hapd->iface->current_mode->mode,
1488 				       acs_res->pri_freq, NULL,
1489 				       hapd->iface->hw_features,
1490 				       hapd->iface->num_hw_features);
1491 	if (!pri_chan) {
1492 		wpa_printf(MSG_ERROR,
1493 			   "ACS: Could not determine primary channel number from pri_freq %u",
1494 			   acs_res->pri_freq);
1495 		err = 1;
1496 		goto out;
1497 	}
1498 
1499 	hapd->iconf->channel = pri_chan->chan;
1500 	hapd->iconf->acs = 1;
1501 
1502 	if (acs_res->sec_freq == 0)
1503 		hapd->iconf->secondary_channel = 0;
1504 	else if (acs_res->sec_freq < acs_res->pri_freq)
1505 		hapd->iconf->secondary_channel = -1;
1506 	else if (acs_res->sec_freq > acs_res->pri_freq)
1507 		hapd->iconf->secondary_channel = 1;
1508 	else {
1509 		wpa_printf(MSG_ERROR, "Invalid secondary channel!");
1510 		err = 1;
1511 		goto out;
1512 	}
1513 
1514 	hapd->iconf->edmg_channel = acs_res->edmg_channel;
1515 
1516 	if (hapd->iface->conf->ieee80211ac || hapd->iface->conf->ieee80211ax) {
1517 		/* set defaults for backwards compatibility */
1518 		hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, 0);
1519 		hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, 0);
1520 		hostapd_set_oper_chwidth(hapd->iconf, CONF_OPER_CHWIDTH_USE_HT);
1521 		if (acs_res->ch_width == 40) {
1522 			if (is_6ghz_freq(acs_res->pri_freq))
1523 				hostapd_set_oper_centr_freq_seg0_idx(
1524 					hapd->iconf,
1525 					acs_res->vht_seg0_center_ch);
1526 		} else if (acs_res->ch_width == 80) {
1527 			hostapd_set_oper_centr_freq_seg0_idx(
1528 				hapd->iconf, acs_res->vht_seg0_center_ch);
1529 			if (acs_res->vht_seg1_center_ch == 0) {
1530 				hostapd_set_oper_chwidth(
1531 					hapd->iconf, CONF_OPER_CHWIDTH_80MHZ);
1532 			} else {
1533 				hostapd_set_oper_chwidth(
1534 					hapd->iconf,
1535 					CONF_OPER_CHWIDTH_80P80MHZ);
1536 				hostapd_set_oper_centr_freq_seg1_idx(
1537 					hapd->iconf,
1538 					acs_res->vht_seg1_center_ch);
1539 			}
1540 		} else if (acs_res->ch_width == 160) {
1541 			hostapd_set_oper_chwidth(hapd->iconf,
1542 						 CONF_OPER_CHWIDTH_160MHZ);
1543 			hostapd_set_oper_centr_freq_seg0_idx(
1544 				hapd->iconf, acs_res->vht_seg1_center_ch);
1545 		}
1546 	}
1547 
1548 #ifdef CONFIG_IEEE80211BE
1549 	if (hapd->iface->conf->ieee80211be && acs_res->ch_width == 320) {
1550 		hostapd_set_oper_chwidth(hapd->iconf, CONF_OPER_CHWIDTH_320MHZ);
1551 		hostapd_set_oper_centr_freq_seg0_idx(
1552 			hapd->iconf, acs_res->vht_seg1_center_ch);
1553 		hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, 0);
1554 	}
1555 
1556 	if (hapd->iface->conf->ieee80211be && acs_res->puncture_bitmap)
1557 		hapd->iconf->punct_bitmap = acs_res->puncture_bitmap;
1558 #endif /* CONFIG_IEEE80211BE */
1559 
1560 out:
1561 	ret = hostapd_acs_completed(hapd->iface, err);
1562 	if (ret) {
1563 		wpa_printf(MSG_ERROR,
1564 			   "ACS: Possibly channel configuration is invalid");
1565 	}
1566 }
1567 #endif /* CONFIG_ACS */
1568 
1569 
hostapd_probe_req_rx(struct hostapd_data * hapd,const u8 * sa,const u8 * da,const u8 * bssid,const u8 * ie,size_t ie_len,int ssi_signal)1570 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
1571 			 const u8 *bssid, const u8 *ie, size_t ie_len,
1572 			 int ssi_signal)
1573 {
1574 	size_t i;
1575 	int ret = 0;
1576 
1577 	if (sa == NULL || ie == NULL)
1578 		return -1;
1579 
1580 	random_add_randomness(sa, ETH_ALEN);
1581 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
1582 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
1583 					    sa, da, bssid, ie, ie_len,
1584 					    ssi_signal) > 0) {
1585 			ret = 1;
1586 			break;
1587 		}
1588 	}
1589 	return ret;
1590 }
1591 
1592 
1593 #ifdef HOSTAPD
1594 
1595 #ifdef CONFIG_IEEE80211R_AP
hostapd_notify_auth_ft_finish(void * ctx,const u8 * dst,u16 auth_transaction,u16 status,const u8 * ies,size_t ies_len)1596 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
1597 					  u16 auth_transaction, u16 status,
1598 					  const u8 *ies, size_t ies_len)
1599 {
1600 	struct hostapd_data *hapd = ctx;
1601 	struct sta_info *sta;
1602 
1603 	sta = ap_get_sta(hapd, dst);
1604 	if (sta == NULL)
1605 		return;
1606 
1607 	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
1608 		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
1609 	sta->flags |= WLAN_STA_AUTH;
1610 
1611 	hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
1612 }
1613 #endif /* CONFIG_IEEE80211R_AP */
1614 
1615 
1616 #ifdef CONFIG_FILS
hostapd_notify_auth_fils_finish(struct hostapd_data * hapd,struct sta_info * sta,u16 resp,struct wpabuf * data,int pub)1617 static void hostapd_notify_auth_fils_finish(struct hostapd_data *hapd,
1618 					    struct sta_info *sta, u16 resp,
1619 					    struct wpabuf *data, int pub)
1620 {
1621 	if (resp == WLAN_STATUS_SUCCESS) {
1622 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1623 			       HOSTAPD_LEVEL_DEBUG, "authentication OK (FILS)");
1624 		sta->flags |= WLAN_STA_AUTH;
1625 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1626 		sta->auth_alg = WLAN_AUTH_FILS_SK;
1627 		mlme_authenticate_indication(hapd, sta);
1628 	} else {
1629 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1630 			       HOSTAPD_LEVEL_DEBUG,
1631 			       "authentication failed (FILS)");
1632 	}
1633 
1634 	hostapd_sta_auth(hapd, sta->addr, 2, resp,
1635 			 data ? wpabuf_head(data) : NULL,
1636 			 data ? wpabuf_len(data) : 0);
1637 	wpabuf_free(data);
1638 }
1639 #endif /* CONFIG_FILS */
1640 
1641 
hostapd_notif_auth(struct hostapd_data * hapd,struct auth_info * rx_auth)1642 static void hostapd_notif_auth(struct hostapd_data *hapd,
1643 			       struct auth_info *rx_auth)
1644 {
1645 	struct sta_info *sta;
1646 	u16 status = WLAN_STATUS_SUCCESS;
1647 	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
1648 	size_t resp_ies_len = 0;
1649 
1650 	sta = ap_get_sta(hapd, rx_auth->peer);
1651 	if (!sta) {
1652 		sta = ap_sta_add(hapd, rx_auth->peer);
1653 		if (sta == NULL) {
1654 			status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1655 			goto fail;
1656 		}
1657 	}
1658 	sta->flags &= ~WLAN_STA_PREAUTH;
1659 	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
1660 #ifdef CONFIG_IEEE80211R_AP
1661 	if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
1662 		sta->auth_alg = WLAN_AUTH_FT;
1663 		if (sta->wpa_sm == NULL)
1664 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1665 							sta->addr, NULL);
1666 		if (sta->wpa_sm == NULL) {
1667 			wpa_printf(MSG_DEBUG,
1668 				   "FT: Failed to initialize WPA state machine");
1669 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1670 			goto fail;
1671 		}
1672 		wpa_ft_process_auth(sta->wpa_sm,
1673 				    rx_auth->auth_transaction, rx_auth->ies,
1674 				    rx_auth->ies_len,
1675 				    hostapd_notify_auth_ft_finish, hapd);
1676 		return;
1677 	}
1678 #endif /* CONFIG_IEEE80211R_AP */
1679 
1680 #ifdef CONFIG_FILS
1681 	if (rx_auth->auth_type == WLAN_AUTH_FILS_SK) {
1682 		sta->auth_alg = WLAN_AUTH_FILS_SK;
1683 		handle_auth_fils(hapd, sta, rx_auth->ies, rx_auth->ies_len,
1684 				 rx_auth->auth_type, rx_auth->auth_transaction,
1685 				 rx_auth->status_code,
1686 				 hostapd_notify_auth_fils_finish);
1687 		return;
1688 	}
1689 #endif /* CONFIG_FILS */
1690 
1691 fail:
1692 	hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
1693 			 status, resp_ies, resp_ies_len);
1694 }
1695 
1696 
1697 #ifndef NEED_AP_MLME
hostapd_action_rx(struct hostapd_data * hapd,struct rx_mgmt * drv_mgmt)1698 static void hostapd_action_rx(struct hostapd_data *hapd,
1699 			      struct rx_mgmt *drv_mgmt)
1700 {
1701 	struct ieee80211_mgmt *mgmt;
1702 	struct sta_info *sta;
1703 	size_t plen __maybe_unused;
1704 	u16 fc;
1705 	u8 *action __maybe_unused;
1706 
1707 	if (drv_mgmt->frame_len < IEEE80211_HDRLEN + 2 + 1)
1708 		return;
1709 
1710 	plen = drv_mgmt->frame_len - IEEE80211_HDRLEN;
1711 
1712 	mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
1713 	fc = le_to_host16(mgmt->frame_control);
1714 	if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
1715 		return; /* handled by the driver */
1716 
1717 	action = (u8 *) &mgmt->u.action.u;
1718 	wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
1719 		   " da " MACSTR " plen %d",
1720 		   mgmt->u.action.category, *action,
1721 		   MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) plen);
1722 
1723 	sta = ap_get_sta(hapd, mgmt->sa);
1724 	if (sta == NULL) {
1725 		wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
1726 		return;
1727 	}
1728 #ifdef CONFIG_IEEE80211R_AP
1729 	if (mgmt->u.action.category == WLAN_ACTION_FT) {
1730 		wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action, plen);
1731 		return;
1732 	}
1733 #endif /* CONFIG_IEEE80211R_AP */
1734 	if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY) {
1735 		ieee802_11_sa_query_action(hapd, mgmt, drv_mgmt->frame_len);
1736 		return;
1737 	}
1738 #ifdef CONFIG_WNM_AP
1739 	if (mgmt->u.action.category == WLAN_ACTION_WNM) {
1740 		ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
1741 		return;
1742 	}
1743 #endif /* CONFIG_WNM_AP */
1744 #ifdef CONFIG_FST
1745 	if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
1746 		fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
1747 		return;
1748 	}
1749 #endif /* CONFIG_FST */
1750 #ifdef CONFIG_DPP
1751 	if (plen >= 2 + 4 &&
1752 	    mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
1753 	    mgmt->u.action.u.vs_public_action.action ==
1754 	    WLAN_PA_VENDOR_SPECIFIC &&
1755 	    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
1756 	    OUI_WFA &&
1757 	    mgmt->u.action.u.vs_public_action.variable[0] ==
1758 	    DPP_OUI_TYPE) {
1759 		const u8 *pos, *end;
1760 
1761 		pos = mgmt->u.action.u.vs_public_action.oui;
1762 		end = drv_mgmt->frame + drv_mgmt->frame_len;
1763 		hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
1764 				      drv_mgmt->freq);
1765 		return;
1766 	}
1767 #endif /* CONFIG_DPP */
1768 #ifdef CONFIG_NAN_USD
1769 	if (mgmt->u.action.category == WLAN_ACTION_PUBLIC && plen >= 5 &&
1770 	    mgmt->u.action.u.vs_public_action.action ==
1771 	    WLAN_PA_VENDOR_SPECIFIC &&
1772 	    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
1773 	    OUI_WFA &&
1774 	    mgmt->u.action.u.vs_public_action.variable[0] == NAN_OUI_TYPE) {
1775 		const u8 *pos, *end;
1776 
1777 		pos = mgmt->u.action.u.vs_public_action.variable;
1778 		end = drv_mgmt->frame + drv_mgmt->frame_len;
1779 		pos++;
1780 		hostapd_nan_usd_rx_sdf(hapd, mgmt->sa, mgmt->bssid,
1781 				       drv_mgmt->freq, pos, end - pos);
1782 		return;
1783 	}
1784 #endif /* CONFIG_NAN_USD */
1785 }
1786 #endif /* NEED_AP_MLME */
1787 
1788 
1789 #ifdef NEED_AP_MLME
1790 
1791 static struct hostapd_data *
switch_link_hapd(struct hostapd_data * hapd,int link_id)1792 switch_link_hapd(struct hostapd_data *hapd, int link_id)
1793 {
1794 #ifdef CONFIG_IEEE80211BE
1795 	if (hapd->conf->mld_ap && link_id >= 0) {
1796 		struct hostapd_data *link_bss;
1797 
1798 		link_bss = hostapd_mld_get_link_bss(hapd, link_id);
1799 		if (link_bss)
1800 			return link_bss;
1801 	}
1802 #endif /* CONFIG_IEEE80211BE */
1803 
1804 	return hapd;
1805 }
1806 
1807 
1808 static struct hostapd_data *
switch_link_scan(struct hostapd_data * hapd,u64 scan_cookie)1809 switch_link_scan(struct hostapd_data *hapd, u64 scan_cookie)
1810 {
1811 #ifdef CONFIG_IEEE80211BE
1812 	if (hapd->conf->mld_ap && scan_cookie != 0) {
1813 		unsigned int i;
1814 
1815 		for (i = 0; i < hapd->iface->interfaces->count; i++) {
1816 			struct hostapd_iface *h;
1817 			struct hostapd_data *h_hapd;
1818 
1819 			h = hapd->iface->interfaces->iface[i];
1820 			h_hapd = h->bss[0];
1821 			if (!hostapd_is_ml_partner(hapd, h_hapd))
1822 				continue;
1823 
1824 			if (h_hapd->scan_cookie == scan_cookie) {
1825 				h_hapd->scan_cookie = 0;
1826 				return h_hapd;
1827 			}
1828 		}
1829 	}
1830 #endif /* CONFIG_IEEE80211BE */
1831 
1832 	return hapd;
1833 }
1834 
1835 
1836 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
1837 
get_hapd_bssid(struct hostapd_iface * iface,const u8 * bssid,int link_id)1838 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
1839 					    const u8 *bssid, int link_id)
1840 {
1841 	size_t i;
1842 
1843 	if (bssid == NULL)
1844 		return NULL;
1845 	if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
1846 	    bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
1847 		return HAPD_BROADCAST;
1848 #ifdef CONFIG_NAN_USD
1849 	if (nan_de_is_nan_network_id(bssid))
1850 		return HAPD_BROADCAST; /* Process NAN Network ID like broadcast
1851 					*/
1852 #endif /* CONFIG_NAN_USD */
1853 
1854 	for (i = 0; i < iface->num_bss; i++) {
1855 		struct hostapd_data *hapd;
1856 #ifdef CONFIG_IEEE80211BE
1857 		struct hostapd_data *p_hapd;
1858 #endif /* CONFIG_IEEE80211BE */
1859 
1860 		hapd = iface->bss[i];
1861 		if (ether_addr_equal(bssid, hapd->own_addr))
1862 			return hapd;
1863 
1864 #ifdef CONFIG_IEEE80211BE
1865 		if (ether_addr_equal(bssid, hapd->own_addr) ||
1866 		    (hapd->conf->mld_ap &&
1867 		     ether_addr_equal(bssid, hapd->mld->mld_addr) &&
1868 		     link_id == hapd->mld_link_id))
1869 			return hapd;
1870 
1871 		if (!hapd->conf->mld_ap)
1872 			continue;
1873 
1874 		for_each_mld_link(p_hapd, hapd) {
1875 			if (p_hapd == hapd)
1876 				continue;
1877 
1878 			if (ether_addr_equal(bssid, p_hapd->own_addr) ||
1879 			    (ether_addr_equal(bssid, p_hapd->mld->mld_addr) &&
1880 			     link_id == p_hapd->mld_link_id))
1881 				return p_hapd;
1882 		}
1883 #endif /* CONFIG_IEEE80211BE */
1884 	}
1885 
1886 	return NULL;
1887 }
1888 
1889 
hostapd_rx_from_unknown_sta(struct hostapd_data * hapd,const u8 * bssid,const u8 * addr,int wds)1890 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
1891 					const u8 *bssid, const u8 *addr,
1892 					int wds)
1893 {
1894 	hapd = get_hapd_bssid(hapd->iface, bssid, -1);
1895 	if (hapd == NULL || hapd == HAPD_BROADCAST)
1896 		return;
1897 
1898 	ieee802_11_rx_from_unknown(hapd, addr, wds);
1899 }
1900 
1901 
hostapd_mgmt_rx(struct hostapd_data * hapd,struct rx_mgmt * rx_mgmt)1902 static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
1903 {
1904 	struct hostapd_iface *iface;
1905 	const struct ieee80211_hdr *hdr;
1906 	const u8 *bssid;
1907 	struct hostapd_frame_info fi;
1908 	int ret;
1909 
1910 	if (rx_mgmt->ctx)
1911 		hapd = rx_mgmt->ctx;
1912 	hapd = switch_link_hapd(hapd, rx_mgmt->link_id);
1913 	iface = hapd->iface;
1914 
1915 #ifdef CONFIG_TESTING_OPTIONS
1916 	if (hapd->ext_mgmt_frame_handling) {
1917 		size_t hex_len = 2 * rx_mgmt->frame_len + 1;
1918 		char *hex = os_malloc(hex_len);
1919 
1920 		if (hex) {
1921 			wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
1922 					 rx_mgmt->frame_len);
1923 			wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
1924 			os_free(hex);
1925 		}
1926 		return 1;
1927 	}
1928 #endif /* CONFIG_TESTING_OPTIONS */
1929 
1930 	hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
1931 	bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
1932 	if (bssid == NULL)
1933 		return 0;
1934 
1935 	hapd = get_hapd_bssid(iface, bssid, rx_mgmt->link_id);
1936 
1937 	if (!hapd) {
1938 		u16 fc = le_to_host16(hdr->frame_control);
1939 
1940 		/*
1941 		 * Drop frames to unknown BSSIDs except for Beacon frames which
1942 		 * could be used to update neighbor information.
1943 		 */
1944 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1945 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1946 			hapd = iface->bss[0];
1947 		else
1948 			return 0;
1949 	}
1950 
1951 	os_memset(&fi, 0, sizeof(fi));
1952 	fi.freq = rx_mgmt->freq;
1953 	fi.datarate = rx_mgmt->datarate;
1954 	fi.ssi_signal = rx_mgmt->ssi_signal;
1955 
1956 	if (hapd == HAPD_BROADCAST) {
1957 		size_t i;
1958 
1959 		ret = 0;
1960 		for (i = 0; i < iface->num_bss; i++) {
1961 			/* if bss is set, driver will call this function for
1962 			 * each bss individually. */
1963 			if (rx_mgmt->drv_priv &&
1964 			    (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
1965 				continue;
1966 
1967 			if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
1968 					    rx_mgmt->frame_len, &fi) > 0)
1969 				ret = 1;
1970 		}
1971 	} else
1972 		ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
1973 				      &fi);
1974 
1975 	random_add_randomness(&fi, sizeof(fi));
1976 
1977 	return ret;
1978 }
1979 
1980 
hostapd_mgmt_tx_cb(struct hostapd_data * hapd,const u8 * buf,size_t len,u16 stype,int ok,int link_id)1981 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
1982 			       size_t len, u16 stype, int ok, int link_id)
1983 {
1984 	struct ieee80211_hdr *hdr;
1985 	struct hostapd_data *orig_hapd, *tmp_hapd;
1986 	const u8 *bssid;
1987 
1988 	orig_hapd = hapd;
1989 
1990 	hdr = (struct ieee80211_hdr *) buf;
1991 	hapd = switch_link_hapd(hapd, link_id);
1992 	bssid = get_hdr_bssid(hdr, len);
1993 	tmp_hapd = get_hapd_bssid(hapd->iface, bssid, link_id);
1994 	if (tmp_hapd) {
1995 		hapd = tmp_hapd;
1996 #ifdef CONFIG_IEEE80211BE
1997 	} else if (hapd->conf->mld_ap && bssid &&
1998 		   ether_addr_equal(hapd->mld->mld_addr, bssid)) {
1999 		/* AP MLD address match - use hapd pointer as-is */
2000 #endif /* CONFIG_IEEE80211BE */
2001 	} else {
2002 		return;
2003 	}
2004 
2005 	if (hapd == HAPD_BROADCAST) {
2006 		if (stype != WLAN_FC_STYPE_ACTION || len <= 25 ||
2007 		    buf[24] != WLAN_ACTION_PUBLIC)
2008 			return;
2009 		hapd = get_hapd_bssid(orig_hapd->iface, hdr->addr2, link_id);
2010 		if (!hapd || hapd == HAPD_BROADCAST)
2011 			return;
2012 		/*
2013 		 * Allow processing of TX status for a Public Action frame that
2014 		 * used wildcard BBSID.
2015 		 */
2016 	}
2017 	ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
2018 }
2019 
2020 #endif /* NEED_AP_MLME */
2021 
2022 
hostapd_event_new_sta(struct hostapd_data * hapd,const u8 * addr)2023 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
2024 {
2025 	struct sta_info *sta = ap_get_sta(hapd, addr);
2026 
2027 	if (sta)
2028 		return 0;
2029 
2030 	wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
2031 		   " - adding a new STA", MAC2STR(addr));
2032 	sta = ap_sta_add(hapd, addr);
2033 	if (sta) {
2034 		hostapd_new_assoc_sta(hapd, sta, 0);
2035 	} else {
2036 		wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
2037 			   MAC2STR(addr));
2038 		return -1;
2039 	}
2040 
2041 	return 0;
2042 }
2043 
2044 
hostapd_event_eapol_rx(struct hostapd_data * hapd,const u8 * src,const u8 * data,size_t data_len,enum frame_encryption encrypted,int link_id)2045 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
2046 				   const u8 *data, size_t data_len,
2047 				   enum frame_encryption encrypted,
2048 				   int link_id)
2049 {
2050 	struct hostapd_data *orig_hapd = hapd;
2051 
2052 #ifdef CONFIG_IEEE80211BE
2053 	hapd = switch_link_hapd(hapd, link_id);
2054 	hapd = hostapd_find_by_sta(hapd->iface, src, true, NULL);
2055 #else /* CONFIG_IEEE80211BE */
2056 	hapd = hostapd_find_by_sta(hapd->iface, src, false, NULL);
2057 #endif /* CONFIG_IEEE80211BE */
2058 
2059 	if (!hapd) {
2060 		/* WLAN cases need to have an existing association, but non-WLAN
2061 		 * cases (mainly, wired IEEE 802.1X) need to be able to process
2062 		 * EAPOL frames from new devices that do not yet have a STA
2063 		 * entry and as such, do not get a match in
2064 		 * hostapd_find_by_sta(). */
2065 		wpa_printf(MSG_DEBUG,
2066 			   "No STA-specific hostapd instance for EAPOL RX found - fall back to initial context");
2067 		hapd = orig_hapd;
2068 	}
2069 
2070 	ieee802_1x_receive(hapd, src, data, data_len, encrypted);
2071 }
2072 
2073 #endif /* HOSTAPD */
2074 
2075 
2076 static struct hostapd_channel_data *
hostapd_get_mode_chan(struct hostapd_hw_modes * mode,unsigned int freq)2077 hostapd_get_mode_chan(struct hostapd_hw_modes *mode, unsigned int freq)
2078 {
2079 	int i;
2080 	struct hostapd_channel_data *chan;
2081 
2082 	for (i = 0; i < mode->num_channels; i++) {
2083 		chan = &mode->channels[i];
2084 		if ((unsigned int) chan->freq == freq)
2085 			return chan;
2086 	}
2087 
2088 	return NULL;
2089 }
2090 
2091 
hostapd_get_mode_channel(struct hostapd_iface * iface,unsigned int freq)2092 static struct hostapd_channel_data * hostapd_get_mode_channel(
2093 	struct hostapd_iface *iface, unsigned int freq)
2094 {
2095 	int i;
2096 	struct hostapd_channel_data *chan;
2097 
2098 	for (i = 0; i < iface->num_hw_features; i++) {
2099 		if (hostapd_hw_skip_mode(iface, &iface->hw_features[i]))
2100 			continue;
2101 		chan = hostapd_get_mode_chan(&iface->hw_features[i], freq);
2102 		if (chan)
2103 			return chan;
2104 	}
2105 
2106 	return NULL;
2107 }
2108 
2109 
hostapd_update_nf(struct hostapd_iface * iface,struct hostapd_channel_data * chan,struct freq_survey * survey)2110 static void hostapd_update_nf(struct hostapd_iface *iface,
2111 			      struct hostapd_channel_data *chan,
2112 			      struct freq_survey *survey)
2113 {
2114 	if (!iface->chans_surveyed) {
2115 		chan->min_nf = survey->nf;
2116 		iface->lowest_nf = survey->nf;
2117 	} else {
2118 		if (dl_list_empty(&chan->survey_list))
2119 			chan->min_nf = survey->nf;
2120 		else if (survey->nf < chan->min_nf)
2121 			chan->min_nf = survey->nf;
2122 		if (survey->nf < iface->lowest_nf)
2123 			iface->lowest_nf = survey->nf;
2124 	}
2125 }
2126 
2127 
hostapd_single_channel_get_survey(struct hostapd_iface * iface,struct survey_results * survey_res)2128 static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
2129 					      struct survey_results *survey_res)
2130 {
2131 	struct hostapd_channel_data *chan;
2132 	struct freq_survey *survey;
2133 	u64 divisor, dividend;
2134 
2135 	survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
2136 			       list);
2137 	if (!survey || !survey->freq)
2138 		return;
2139 
2140 	chan = hostapd_get_mode_channel(iface, survey->freq);
2141 	if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
2142 		return;
2143 
2144 	wpa_printf(MSG_DEBUG,
2145 		   "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
2146 		   survey->freq,
2147 		   (unsigned long int) survey->channel_time,
2148 		   (unsigned long int) survey->channel_time_busy);
2149 
2150 	if (survey->channel_time > iface->last_channel_time &&
2151 	    survey->channel_time > survey->channel_time_busy) {
2152 		dividend = survey->channel_time_busy -
2153 			iface->last_channel_time_busy;
2154 		divisor = survey->channel_time - iface->last_channel_time;
2155 
2156 		iface->channel_utilization = dividend * 255 / divisor;
2157 		wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
2158 			   iface->channel_utilization);
2159 	}
2160 	iface->last_channel_time = survey->channel_time;
2161 	iface->last_channel_time_busy = survey->channel_time_busy;
2162 }
2163 
2164 
hostapd_event_get_survey(struct hostapd_iface * iface,struct survey_results * survey_results)2165 void hostapd_event_get_survey(struct hostapd_iface *iface,
2166 			      struct survey_results *survey_results)
2167 {
2168 	struct freq_survey *survey, *tmp;
2169 	struct hostapd_channel_data *chan;
2170 
2171 	if (dl_list_empty(&survey_results->survey_list)) {
2172 		wpa_printf(MSG_DEBUG, "No survey data received");
2173 		return;
2174 	}
2175 
2176 	if (survey_results->freq_filter) {
2177 		hostapd_single_channel_get_survey(iface, survey_results);
2178 		return;
2179 	}
2180 
2181 	dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
2182 			      struct freq_survey, list) {
2183 		chan = hostapd_get_mode_channel(iface, survey->freq);
2184 		if (!chan)
2185 			continue;
2186 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
2187 			continue;
2188 
2189 		dl_list_del(&survey->list);
2190 		dl_list_add_tail(&chan->survey_list, &survey->list);
2191 
2192 		hostapd_update_nf(iface, chan, survey);
2193 
2194 		iface->chans_surveyed++;
2195 	}
2196 }
2197 
2198 
2199 #ifdef HOSTAPD
2200 #ifdef NEED_AP_MLME
2201 
hostapd_event_iface_unavailable(struct hostapd_data * hapd)2202 static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
2203 {
2204 	wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
2205 		   hapd->conf->iface);
2206 
2207 	if (hapd->csa_in_progress) {
2208 		wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
2209 			   hapd->conf->iface);
2210 		hostapd_switch_channel_fallback(hapd->iface,
2211 						&hapd->cs_freq_params);
2212 	}
2213 }
2214 
2215 
hostapd_event_dfs_radar_detected(struct hostapd_data * hapd,struct dfs_event * radar)2216 static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
2217 					     struct dfs_event *radar)
2218 {
2219 	wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
2220 	hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
2221 				   radar->chan_offset, radar->chan_width,
2222 				   radar->cf1, radar->cf2);
2223 }
2224 
2225 
hostapd_event_dfs_pre_cac_expired(struct hostapd_data * hapd,struct dfs_event * radar)2226 static void hostapd_event_dfs_pre_cac_expired(struct hostapd_data *hapd,
2227 					      struct dfs_event *radar)
2228 {
2229 	wpa_printf(MSG_DEBUG, "DFS Pre-CAC expired on %d MHz", radar->freq);
2230 	hostapd_dfs_pre_cac_expired(hapd->iface, radar->freq, radar->ht_enabled,
2231 				    radar->chan_offset, radar->chan_width,
2232 				    radar->cf1, radar->cf2);
2233 }
2234 
2235 
hostapd_event_dfs_cac_finished(struct hostapd_data * hapd,struct dfs_event * radar)2236 static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
2237 					   struct dfs_event *radar)
2238 {
2239 	wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
2240 	hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
2241 				 radar->chan_offset, radar->chan_width,
2242 				 radar->cf1, radar->cf2);
2243 }
2244 
2245 
hostapd_event_dfs_cac_aborted(struct hostapd_data * hapd,struct dfs_event * radar)2246 static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
2247 					  struct dfs_event *radar)
2248 {
2249 	wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
2250 	hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
2251 				 radar->chan_offset, radar->chan_width,
2252 				 radar->cf1, radar->cf2);
2253 }
2254 
2255 
hostapd_event_dfs_nop_finished(struct hostapd_data * hapd,struct dfs_event * radar)2256 static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
2257 					   struct dfs_event *radar)
2258 {
2259 	wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
2260 	hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
2261 				 radar->chan_offset, radar->chan_width,
2262 				 radar->cf1, radar->cf2);
2263 }
2264 
2265 
hostapd_event_dfs_cac_started(struct hostapd_data * hapd,struct dfs_event * radar)2266 static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
2267 					  struct dfs_event *radar)
2268 {
2269 	wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
2270 	hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
2271 			      radar->chan_offset, radar->chan_width,
2272 			      radar->cf1, radar->cf2);
2273 }
2274 
2275 #endif /* NEED_AP_MLME */
2276 
2277 
hostapd_event_wds_sta_interface_status(struct hostapd_data * hapd,int istatus,const char * ifname,const u8 * addr)2278 static void hostapd_event_wds_sta_interface_status(struct hostapd_data *hapd,
2279 						   int istatus,
2280 						   const char *ifname,
2281 						   const u8 *addr)
2282 {
2283 	struct sta_info *sta = ap_get_sta(hapd, addr);
2284 
2285 	if (sta) {
2286 		os_free(sta->ifname_wds);
2287 		if (istatus == INTERFACE_ADDED)
2288 			sta->ifname_wds = os_strdup(ifname);
2289 		else
2290 			sta->ifname_wds = NULL;
2291 	}
2292 
2293 	wpa_msg(hapd->msg_ctx, MSG_INFO, "%sifname=%s sta_addr=" MACSTR,
2294 		istatus == INTERFACE_ADDED ?
2295 		WDS_STA_INTERFACE_ADDED : WDS_STA_INTERFACE_REMOVED,
2296 		ifname, MAC2STR(addr));
2297 }
2298 
2299 
2300 #ifdef CONFIG_OWE
hostapd_notif_update_dh_ie(struct hostapd_data * hapd,const u8 * peer,const u8 * ie,size_t ie_len,const u8 * link_addr)2301 static int hostapd_notif_update_dh_ie(struct hostapd_data *hapd,
2302 				      const u8 *peer, const u8 *ie,
2303 				      size_t ie_len, const u8 *link_addr)
2304 {
2305 	u16 status;
2306 	struct sta_info *sta;
2307 	struct ieee802_11_elems elems;
2308 
2309 	if (!hapd || !hapd->wpa_auth) {
2310 		wpa_printf(MSG_DEBUG, "OWE: Invalid hapd context");
2311 		return -1;
2312 	}
2313 	if (!peer) {
2314 		wpa_printf(MSG_DEBUG, "OWE: Peer unknown");
2315 		return -1;
2316 	}
2317 	if (!(hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE)) {
2318 		wpa_printf(MSG_DEBUG, "OWE: No OWE AKM configured");
2319 		status = WLAN_STATUS_AKMP_NOT_VALID;
2320 		goto err;
2321 	}
2322 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 1) == ParseFailed) {
2323 		wpa_printf(MSG_DEBUG, "OWE: Failed to parse OWE IE for "
2324 			   MACSTR, MAC2STR(peer));
2325 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2326 		goto err;
2327 	}
2328 	status = owe_validate_request(hapd, peer, elems.rsn_ie,
2329 				      elems.rsn_ie_len,
2330 				      elems.owe_dh, elems.owe_dh_len);
2331 	if (status != WLAN_STATUS_SUCCESS)
2332 		goto err;
2333 
2334 	sta = ap_get_sta(hapd, peer);
2335 	if (sta) {
2336 		ap_sta_no_session_timeout(hapd, sta);
2337 		accounting_sta_stop(hapd, sta);
2338 
2339 		/*
2340 		 * Make sure that the previously registered inactivity timer
2341 		 * will not remove the STA immediately.
2342 		 */
2343 		sta->timeout_next = STA_NULLFUNC;
2344 	} else {
2345 		sta = ap_sta_add(hapd, peer);
2346 		if (!sta) {
2347 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2348 			goto err;
2349 		}
2350 	}
2351 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
2352 
2353 #ifdef CONFIG_IEEE80211BE
2354 	if (link_addr) {
2355 		struct mld_info *info = &sta->mld_info;
2356 		u8 link_id = hapd->mld_link_id;
2357 
2358 		ap_sta_set_mld(sta, true);
2359 		sta->mld_assoc_link_id = link_id;
2360 		os_memcpy(info->common_info.mld_addr, peer, ETH_ALEN);
2361 		info->links[link_id].valid = true;
2362 		os_memcpy(info->links[link_id].local_addr, hapd->own_addr,
2363 			  ETH_ALEN);
2364 		os_memcpy(info->links[link_id].peer_addr, link_addr, ETH_ALEN);
2365 	}
2366 #endif /* CONFIG_IEEE80211BE */
2367 
2368 	status = owe_process_rsn_ie(hapd, sta, elems.rsn_ie,
2369 				    elems.rsn_ie_len, elems.owe_dh,
2370 				    elems.owe_dh_len, link_addr);
2371 	if (status != WLAN_STATUS_SUCCESS)
2372 		ap_free_sta(hapd, sta);
2373 
2374 	return 0;
2375 err:
2376 	hostapd_drv_update_dh_ie(hapd, link_addr ? link_addr : peer, status,
2377 				 NULL, 0);
2378 	return 0;
2379 }
2380 #endif /* CONFIG_OWE */
2381 
2382 
2383 #ifdef NEED_AP_MLME
hostapd_eapol_tx_status(struct hostapd_data * hapd,const u8 * dst,const u8 * data,size_t len,int ack,int link_id)2384 static void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2385 				    const u8 *data, size_t len, int ack,
2386 				    int link_id)
2387 {
2388 	struct sta_info *sta;
2389 
2390 	hapd = switch_link_hapd(hapd, link_id);
2391 	hapd = hostapd_find_by_sta(hapd->iface, dst, false, &sta);
2392 
2393 	if (!sta) {
2394 		wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2395 			   MACSTR " that is not currently associated",
2396 			   MAC2STR(dst));
2397 		return;
2398 	}
2399 
2400 	ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2401 }
2402 #endif /* NEED_AP_MLME */
2403 
2404 
2405 #ifdef CONFIG_IEEE80211AX
hostapd_event_color_change(struct hostapd_data * hapd,bool success)2406 static void hostapd_event_color_change(struct hostapd_data *hapd, bool success)
2407 {
2408 	struct hostapd_data *bss;
2409 	size_t i;
2410 
2411 	for (i = 0; i < hapd->iface->num_bss; i++) {
2412 		bss = hapd->iface->bss[i];
2413 		if (bss->cca_color == 0)
2414 			continue;
2415 
2416 		if (success)
2417 			hapd->iface->conf->he_op.he_bss_color = bss->cca_color;
2418 
2419 		bss->cca_in_progress = 0;
2420 		if (ieee802_11_set_beacon(bss)) {
2421 			wpa_printf(MSG_ERROR, "Failed to remove BCCA element");
2422 			bss->cca_in_progress = 1;
2423 		} else {
2424 			hostapd_cleanup_cca_params(bss);
2425 		}
2426 	}
2427 }
2428 #endif  /* CONFIG_IEEE80211AX */
2429 
2430 
hostapd_iface_enable(struct hostapd_data * hapd)2431 static void hostapd_iface_enable(struct hostapd_data *hapd)
2432 {
2433 	wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
2434 	if (hapd->disabled && hapd->started) {
2435 		hapd->disabled = 0;
2436 		/*
2437 		 * Try to re-enable interface if the driver stopped it
2438 		 * when the interface got disabled.
2439 		 */
2440 		if (hapd->wpa_auth)
2441 			wpa_auth_reconfig_group_keys(hapd->wpa_auth);
2442 		else
2443 			hostapd_reconfig_encryption(hapd);
2444 		hapd->reenable_beacon = 1;
2445 		ieee802_11_set_beacon(hapd);
2446 #ifdef NEED_AP_MLME
2447 	} else if (hapd->disabled && hapd->iface->cac_started) {
2448 		wpa_printf(MSG_DEBUG, "DFS: restarting pending CAC");
2449 		hostapd_handle_dfs(hapd->iface);
2450 #endif /* NEED_AP_MLME */
2451 	}
2452 }
2453 
2454 
hostapd_iface_disable(struct hostapd_data * hapd)2455 static void hostapd_iface_disable(struct hostapd_data *hapd)
2456 {
2457 	hostapd_free_stas(hapd);
2458 	wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
2459 	hapd->disabled = 1;
2460 }
2461 
2462 
2463 #ifdef CONFIG_IEEE80211BE
2464 
hostapd_mld_iface_enable(struct hostapd_data * hapd)2465 static void hostapd_mld_iface_enable(struct hostapd_data *hapd)
2466 {
2467 	struct hostapd_data *first_link, *link_bss;
2468 
2469 	first_link = hostapd_mld_is_first_bss(hapd) ? hapd :
2470 		hostapd_mld_get_first_bss(hapd);
2471 
2472 	/* Links have been removed. Re-add all links and enable them, but
2473 	 * enable the first link BSS before doing that. */
2474 	if (hostapd_drv_link_add(first_link, first_link->mld_link_id,
2475 				 first_link->own_addr)) {
2476 		wpa_printf(MSG_ERROR, "MLD: Failed to re-add link %d in MLD %s",
2477 			   first_link->mld_link_id, first_link->conf->iface);
2478 		return;
2479 	}
2480 
2481 	hostapd_iface_enable(first_link);
2482 
2483 	/* Add other affiliated links */
2484 	for_each_mld_link(link_bss, first_link) {
2485 		if (link_bss == first_link)
2486 			continue;
2487 
2488 		if (hostapd_drv_link_add(link_bss, link_bss->mld_link_id,
2489 					 link_bss->own_addr)) {
2490 			wpa_printf(MSG_ERROR,
2491 				   "MLD: Failed to re-add link %d in MLD %s",
2492 				   link_bss->mld_link_id,
2493 				   link_bss->conf->iface);
2494 			continue;
2495 		}
2496 
2497 		hostapd_iface_enable(link_bss);
2498 	}
2499 }
2500 
2501 
hostapd_mld_iface_disable(struct hostapd_data * hapd)2502 static void hostapd_mld_iface_disable(struct hostapd_data *hapd)
2503 {
2504 	struct hostapd_data *link_bss;
2505 
2506 	for_each_mld_link(link_bss, hapd)
2507 		hostapd_iface_disable(link_bss);
2508 }
2509 
2510 #endif /* CONFIG_IEEE80211BE */
2511 
2512 
wpa_supplicant_event(void * ctx,enum wpa_event_type event,union wpa_event_data * data)2513 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2514 			  union wpa_event_data *data)
2515 {
2516 	struct hostapd_data *hapd = ctx;
2517 	struct sta_info *sta;
2518 #ifndef CONFIG_NO_STDOUT_DEBUG
2519 	int level = MSG_DEBUG;
2520 
2521 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
2522 	    data->rx_mgmt.frame_len >= 24) {
2523 		const struct ieee80211_hdr *hdr;
2524 		u16 fc;
2525 
2526 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2527 		fc = le_to_host16(hdr->frame_control);
2528 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2529 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2530 			level = MSG_EXCESSIVE;
2531 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2532 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
2533 			level = MSG_EXCESSIVE;
2534 	}
2535 
2536 	wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
2537 		event_to_string(event), event);
2538 #endif /* CONFIG_NO_STDOUT_DEBUG */
2539 
2540 	switch (event) {
2541 	case EVENT_MICHAEL_MIC_FAILURE:
2542 		michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
2543 		break;
2544 	case EVENT_SCAN_RESULTS:
2545 #ifdef NEED_AP_MLME
2546 		if (data)
2547 			hapd = switch_link_scan(hapd,
2548 						data->scan_info.scan_cookie);
2549 #endif /* NEED_AP_MLME */
2550 		if (hapd->iface->scan_cb)
2551 			hapd->iface->scan_cb(hapd->iface);
2552 #ifdef CONFIG_IEEE80211BE
2553 		if (!hapd->iface->scan_cb && hapd->conf->mld_ap) {
2554 			/* Other links may be waiting for HT scan result */
2555 			unsigned int i;
2556 
2557 			for (i = 0; i < hapd->iface->interfaces->count; i++) {
2558 				struct hostapd_iface *h =
2559 					hapd->iface->interfaces->iface[i];
2560 				struct hostapd_data *h_hapd = h->bss[0];
2561 
2562 				if (hostapd_is_ml_partner(hapd, h_hapd) &&
2563 				    h_hapd->iface->scan_cb)
2564 					h_hapd->iface->scan_cb(h_hapd->iface);
2565 			}
2566 		}
2567 #endif /* CONFIG_IEEE80211BE */
2568 		break;
2569 	case EVENT_WPS_BUTTON_PUSHED:
2570 		hostapd_wps_button_pushed(hapd, NULL);
2571 		break;
2572 #ifdef NEED_AP_MLME
2573 	case EVENT_TX_STATUS:
2574 		switch (data->tx_status.type) {
2575 		case WLAN_FC_TYPE_MGMT:
2576 			hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
2577 					   data->tx_status.data_len,
2578 					   data->tx_status.stype,
2579 					   data->tx_status.ack,
2580 					   data->tx_status.link_id);
2581 			break;
2582 		case WLAN_FC_TYPE_DATA:
2583 			hostapd_tx_status(hapd, data->tx_status.dst,
2584 					  data->tx_status.data,
2585 					  data->tx_status.data_len,
2586 					  data->tx_status.ack);
2587 			break;
2588 		}
2589 		break;
2590 	case EVENT_EAPOL_TX_STATUS:
2591 		hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
2592 					data->eapol_tx_status.data,
2593 					data->eapol_tx_status.data_len,
2594 					data->eapol_tx_status.ack,
2595 					data->eapol_tx_status.link_id);
2596 		break;
2597 	case EVENT_DRIVER_CLIENT_POLL_OK:
2598 		hostapd_client_poll_ok(hapd, data->client_poll.addr);
2599 		break;
2600 	case EVENT_RX_FROM_UNKNOWN:
2601 		hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
2602 					    data->rx_from_unknown.addr,
2603 					    data->rx_from_unknown.wds);
2604 		break;
2605 #endif /* NEED_AP_MLME */
2606 	case EVENT_RX_MGMT:
2607 		if (!data->rx_mgmt.frame)
2608 			break;
2609 #ifdef NEED_AP_MLME
2610 		hostapd_mgmt_rx(hapd, &data->rx_mgmt);
2611 #else /* NEED_AP_MLME */
2612 		hostapd_action_rx(hapd, &data->rx_mgmt);
2613 #endif /* NEED_AP_MLME */
2614 		break;
2615 	case EVENT_RX_PROBE_REQ:
2616 		if (data->rx_probe_req.sa == NULL ||
2617 		    data->rx_probe_req.ie == NULL)
2618 			break;
2619 		hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
2620 				     data->rx_probe_req.da,
2621 				     data->rx_probe_req.bssid,
2622 				     data->rx_probe_req.ie,
2623 				     data->rx_probe_req.ie_len,
2624 				     data->rx_probe_req.ssi_signal);
2625 		break;
2626 	case EVENT_NEW_STA:
2627 		hostapd_event_new_sta(hapd, data->new_sta.addr);
2628 		break;
2629 	case EVENT_EAPOL_RX:
2630 		hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
2631 				       data->eapol_rx.data,
2632 				       data->eapol_rx.data_len,
2633 				       data->eapol_rx.encrypted,
2634 				       data->eapol_rx.link_id);
2635 		break;
2636 	case EVENT_ASSOC:
2637 		if (!data)
2638 			return;
2639 #ifdef CONFIG_IEEE80211BE
2640 		if (data->assoc_info.assoc_link_id != -1) {
2641 			hapd = hostapd_mld_get_link_bss(
2642 				hapd, data->assoc_info.assoc_link_id);
2643 			if (!hapd) {
2644 				wpa_printf(MSG_ERROR,
2645 					   "MLD: Failed to get link BSS for EVENT_ASSOC");
2646 				return;
2647 			}
2648 		}
2649 #endif /* CONFIG_IEEE80211BE */
2650 		hostapd_notif_assoc(hapd, data->assoc_info.addr,
2651 				    data->assoc_info.req_ies,
2652 				    data->assoc_info.req_ies_len,
2653 				    data->assoc_info.resp_ies,
2654 				    data->assoc_info.resp_ies_len,
2655 				    data->assoc_info.link_addr,
2656 				    data->assoc_info.reassoc);
2657 		break;
2658 	case EVENT_PORT_AUTHORIZED:
2659 		/* Port authorized event for an associated STA */
2660 		sta = ap_get_sta(hapd, data->port_authorized.sta_addr);
2661 		if (sta)
2662 			ap_sta_set_authorized(hapd, sta, 1);
2663 		else
2664 			wpa_printf(MSG_DEBUG,
2665 				   "No STA info matching port authorized event found");
2666 		break;
2667 #ifdef CONFIG_OWE
2668 	case EVENT_UPDATE_DH:
2669 		if (!data)
2670 			return;
2671 #ifdef CONFIG_IEEE80211BE
2672 		if (data->update_dh.assoc_link_id != -1) {
2673 			hapd = hostapd_mld_get_link_bss(
2674 				hapd, data->update_dh.assoc_link_id);
2675 			if (!hapd) {
2676 				wpa_printf(MSG_ERROR,
2677 					   "MLD: Failed to get link BSS for EVENT_UPDATE_DH assoc_link_id=%d",
2678 					   data->update_dh.assoc_link_id);
2679 				return;
2680 			}
2681 		}
2682 #endif /* CONFIG_IEEE80211BE */
2683 		hostapd_notif_update_dh_ie(hapd, data->update_dh.peer,
2684 					   data->update_dh.ie,
2685 					   data->update_dh.ie_len,
2686 					   data->update_dh.link_addr);
2687 		break;
2688 #endif /* CONFIG_OWE */
2689 	case EVENT_DISASSOC:
2690 		if (data)
2691 			hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
2692 		break;
2693 	case EVENT_DEAUTH:
2694 		if (data)
2695 			hostapd_notif_disassoc(hapd, data->deauth_info.addr);
2696 		break;
2697 	case EVENT_STATION_LOW_ACK:
2698 		if (!data)
2699 			break;
2700 		hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
2701 		break;
2702 	case EVENT_AUTH:
2703 		hostapd_notif_auth(hapd, &data->auth);
2704 		break;
2705 	case EVENT_CH_SWITCH_STARTED:
2706 	case EVENT_CH_SWITCH:
2707 		if (!data)
2708 			break;
2709 #ifdef CONFIG_IEEE80211BE
2710 		if (data->ch_switch.link_id != -1) {
2711 			hapd = hostapd_mld_get_link_bss(
2712 				hapd, data->ch_switch.link_id);
2713 			if (!hapd) {
2714 				wpa_printf(MSG_ERROR,
2715 					   "MLD: Failed to get link (ID %d) BSS for EVENT_CH_SWITCH/EVENT_CH_SWITCH_STARTED",
2716 					   data->ch_switch.link_id);
2717 				break;
2718 			}
2719 		}
2720 #endif /* CONFIG_IEEE80211BE */
2721 		hostapd_event_ch_switch(hapd, data->ch_switch.freq,
2722 					data->ch_switch.ht_enabled,
2723 					data->ch_switch.ch_offset,
2724 					data->ch_switch.ch_width,
2725 					data->ch_switch.cf1,
2726 					data->ch_switch.cf2,
2727 					data->ch_switch.punct_bitmap,
2728 					event == EVENT_CH_SWITCH);
2729 		break;
2730 	case EVENT_CONNECT_FAILED_REASON:
2731 		if (!data)
2732 			break;
2733 		hostapd_event_connect_failed_reason(
2734 			hapd, data->connect_failed_reason.addr,
2735 			data->connect_failed_reason.code);
2736 		break;
2737 	case EVENT_SURVEY:
2738 		hostapd_event_get_survey(hapd->iface, &data->survey_results);
2739 		break;
2740 #ifdef NEED_AP_MLME
2741 	case EVENT_INTERFACE_UNAVAILABLE:
2742 		hostapd_event_iface_unavailable(hapd);
2743 		break;
2744 	case EVENT_DFS_RADAR_DETECTED:
2745 		if (!data)
2746 			break;
2747 		hapd = switch_link_hapd(hapd, data->dfs_event.link_id);
2748 		hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
2749 		break;
2750 	case EVENT_DFS_PRE_CAC_EXPIRED:
2751 		if (!data)
2752 			break;
2753 		hapd = switch_link_hapd(hapd, data->dfs_event.link_id);
2754 		hostapd_event_dfs_pre_cac_expired(hapd, &data->dfs_event);
2755 		break;
2756 	case EVENT_DFS_CAC_FINISHED:
2757 		if (!data)
2758 			break;
2759 		hapd = switch_link_hapd(hapd, data->dfs_event.link_id);
2760 		hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
2761 		break;
2762 	case EVENT_DFS_CAC_ABORTED:
2763 		if (!data)
2764 			break;
2765 		hapd = switch_link_hapd(hapd, data->dfs_event.link_id);
2766 		hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
2767 		break;
2768 	case EVENT_DFS_NOP_FINISHED:
2769 		if (!data)
2770 			break;
2771 		hapd = switch_link_hapd(hapd, data->dfs_event.link_id);
2772 		hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
2773 		break;
2774 	case EVENT_CHANNEL_LIST_CHANGED:
2775 		/* channel list changed (regulatory?), update channel list */
2776 		/* TODO: check this. hostapd_get_hw_features() initializes
2777 		 * too much stuff. */
2778 		/* hostapd_get_hw_features(hapd->iface); */
2779 		hostapd_channel_list_updated(
2780 			hapd->iface, data->channel_list_changed.initiator);
2781 		break;
2782 	case EVENT_DFS_CAC_STARTED:
2783 		if (!data)
2784 			break;
2785 		hapd = switch_link_hapd(hapd, data->dfs_event.link_id);
2786 		hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
2787 		break;
2788 #endif /* NEED_AP_MLME */
2789 	case EVENT_INTERFACE_ENABLED:
2790 #ifdef CONFIG_IEEE80211BE
2791 		if (hapd->conf->mld_ap) {
2792 			hostapd_mld_iface_enable(hapd);
2793 			break;
2794 		}
2795 #endif /* CONFIG_IEEE80211BE */
2796 		hostapd_iface_enable(hapd);
2797 		break;
2798 	case EVENT_INTERFACE_DISABLED:
2799 #ifdef CONFIG_IEEE80211BE
2800 		if (hapd->conf->mld_ap) {
2801 			hostapd_mld_iface_disable(hapd);
2802 			break;
2803 		}
2804 #endif /* CONFIG_IEEE80211BE */
2805 		hostapd_iface_disable(hapd);
2806 		break;
2807 #ifdef CONFIG_ACS
2808 	case EVENT_ACS_CHANNEL_SELECTED:
2809 		hostapd_acs_channel_selected(hapd,
2810 					     &data->acs_selected_channels);
2811 		break;
2812 #endif /* CONFIG_ACS */
2813 	case EVENT_STATION_OPMODE_CHANGED:
2814 		hostapd_event_sta_opmode_changed(hapd, data->sta_opmode.addr,
2815 						 data->sta_opmode.smps_mode,
2816 						 data->sta_opmode.chan_width,
2817 						 data->sta_opmode.rx_nss);
2818 		break;
2819 	case EVENT_WDS_STA_INTERFACE_STATUS:
2820 		hostapd_event_wds_sta_interface_status(
2821 			hapd, data->wds_sta_interface.istatus,
2822 			data->wds_sta_interface.ifname,
2823 			data->wds_sta_interface.sta_addr);
2824 		break;
2825 #ifdef CONFIG_IEEE80211AX
2826 	case EVENT_BSS_COLOR_COLLISION:
2827 		/* The BSS color is shared amongst all BBSs on a specific phy.
2828 		 * Therefore we always start the color change on the primary
2829 		 * BSS. */
2830 		hapd = switch_link_hapd(hapd,
2831 					data->bss_color_collision.link_id);
2832 		wpa_printf(MSG_DEBUG, "BSS color collision on %s",
2833 			   hapd->conf->iface);
2834 		hostapd_switch_color(hapd->iface->bss[0],
2835 				     data->bss_color_collision.bitmap);
2836 		break;
2837 	case EVENT_CCA_STARTED_NOTIFY:
2838 		hapd = switch_link_hapd(hapd,
2839 					data->bss_color_collision.link_id);
2840 		wpa_printf(MSG_DEBUG, "CCA started on %s",
2841 			   hapd->conf->iface);
2842 		break;
2843 	case EVENT_CCA_ABORTED_NOTIFY:
2844 		hapd = switch_link_hapd(hapd,
2845 					data->bss_color_collision.link_id);
2846 		wpa_printf(MSG_DEBUG, "CCA aborted on %s",
2847 			   hapd->conf->iface);
2848 		hostapd_event_color_change(hapd, false);
2849 		break;
2850 	case EVENT_CCA_NOTIFY:
2851 		hapd = switch_link_hapd(hapd,
2852 					data->bss_color_collision.link_id);
2853 		wpa_printf(MSG_DEBUG, "CCA finished on %s",
2854 			   hapd->conf->iface);
2855 		hostapd_event_color_change(hapd, true);
2856 		break;
2857 #endif /* CONFIG_IEEE80211AX */
2858 #ifdef CONFIG_IEEE80211BE
2859 	case EVENT_MLD_INTERFACE_FREED:
2860 		wpa_printf(MSG_DEBUG, "MLD: Interface %s freed",
2861 			   hapd->conf->iface);
2862 		hostapd_mld_interface_freed(hapd);
2863 		break;
2864 #endif /* CONFIG_IEEE80211BE */
2865 	default:
2866 		wpa_printf(MSG_DEBUG, "Unknown event %d", event);
2867 		break;
2868 	}
2869 }
2870 
2871 
wpa_supplicant_event_global(void * ctx,enum wpa_event_type event,union wpa_event_data * data)2872 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
2873 				 union wpa_event_data *data)
2874 {
2875 	struct hapd_interfaces *interfaces = ctx;
2876 	struct hostapd_data *hapd;
2877 
2878 	if (event != EVENT_INTERFACE_STATUS)
2879 		return;
2880 
2881 	hapd = hostapd_get_iface(interfaces, data->interface_status.ifname);
2882 	if (hapd && hapd->driver && hapd->driver->get_ifindex &&
2883 	    hapd->drv_priv) {
2884 		unsigned int ifindex;
2885 
2886 		ifindex = hapd->driver->get_ifindex(hapd->drv_priv);
2887 		if (ifindex != data->interface_status.ifindex) {
2888 			wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
2889 				"interface status ifindex %d mismatch (%d)",
2890 				ifindex, data->interface_status.ifindex);
2891 			return;
2892 		}
2893 	}
2894 	if (hapd)
2895 		wpa_supplicant_event(hapd, event, data);
2896 }
2897 
2898 #endif /* HOSTAPD */
2899