xref: /aosp_15_r20/external/wpa_supplicant_8/wpa_supplicant/events.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2019, Jouni Malinen <[email protected]>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "utils/crc32.h"
13 #include "eapol_supp/eapol_supp_sm.h"
14 #include "rsn_supp/wpa.h"
15 #include "eloop.h"
16 #include "config.h"
17 #include "l2_packet/l2_packet.h"
18 #include "wpa_supplicant_i.h"
19 #include "driver_i.h"
20 #include "pcsc_funcs.h"
21 #include "rsn_supp/preauth.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "common/wpa_ctrl.h"
24 #include "eap_peer/eap.h"
25 #include "ap/hostapd.h"
26 #include "ap/sta_info.h"
27 #include "p2p/p2p.h"
28 #include "fst/fst.h"
29 #include "wnm_sta.h"
30 #include "notify.h"
31 #include "common/ieee802_11_defs.h"
32 #include "common/ieee802_11_common.h"
33 #include "common/gas_server.h"
34 #include "common/dpp.h"
35 #include "common/ptksa_cache.h"
36 #include "crypto/random.h"
37 #include "bssid_ignore.h"
38 #include "wpas_glue.h"
39 #include "wps_supplicant.h"
40 #include "ibss_rsn.h"
41 #include "sme.h"
42 #include "gas_query.h"
43 #include "p2p_supplicant.h"
44 #include "bgscan.h"
45 #include "autoscan.h"
46 #include "ap.h"
47 #include "bss.h"
48 #include "scan.h"
49 #include "offchannel.h"
50 #include "interworking.h"
51 #include "mesh.h"
52 #include "mesh_mpm.h"
53 #include "wmm_ac.h"
54 #include "nan_usd.h"
55 #include "dpp_supplicant.h"
56 #include "rsn_supp/wpa_i.h"
57 
58 
59 #define MAX_OWE_TRANSITION_BSS_SELECT_COUNT 5
60 
61 
62 #ifndef CONFIG_NO_SCAN_PROCESSING
63 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
64 					      int new_scan, int own_request,
65 					      bool trigger_6ghz_scan,
66 					      union wpa_event_data *data);
67 #endif /* CONFIG_NO_SCAN_PROCESSING */
68 
69 
wpas_temp_disabled(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)70 int wpas_temp_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
71 {
72 	struct os_reltime now;
73 
74 	if (ssid == NULL || ssid->disabled_until.sec == 0)
75 		return 0;
76 
77 	os_get_reltime(&now);
78 	if (ssid->disabled_until.sec > now.sec)
79 		return ssid->disabled_until.sec - now.sec;
80 
81 	wpas_clear_temp_disabled(wpa_s, ssid, 0);
82 
83 	return 0;
84 }
85 
86 
87 #ifndef CONFIG_NO_SCAN_PROCESSING
88 /**
89  * wpas_reenabled_network_time - Time until first network is re-enabled
90  * @wpa_s: Pointer to wpa_supplicant data
91  * Returns: If all enabled networks are temporarily disabled, returns the time
92  *	(in sec) until the first network is re-enabled. Otherwise returns 0.
93  *
94  * This function is used in case all enabled networks are temporarily disabled,
95  * in which case it returns the time (in sec) that the first network will be
96  * re-enabled. The function assumes that at least one network is enabled.
97  */
wpas_reenabled_network_time(struct wpa_supplicant * wpa_s)98 static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
99 {
100 	struct wpa_ssid *ssid;
101 	int disabled_for, res = 0;
102 
103 #ifdef CONFIG_INTERWORKING
104 	if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
105 	    wpa_s->conf->cred)
106 		return 0;
107 #endif /* CONFIG_INTERWORKING */
108 
109 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
110 		if (ssid->disabled)
111 			continue;
112 
113 		disabled_for = wpas_temp_disabled(wpa_s, ssid);
114 		if (!disabled_for)
115 			return 0;
116 
117 		if (!res || disabled_for < res)
118 			res = disabled_for;
119 	}
120 
121 	return res;
122 }
123 #endif /* CONFIG_NO_SCAN_PROCESSING */
124 
125 
wpas_network_reenabled(void * eloop_ctx,void * timeout_ctx)126 void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
127 {
128 	struct wpa_supplicant *wpa_s = eloop_ctx;
129 
130 	if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
131 		return;
132 
133 	wpa_dbg(wpa_s, MSG_DEBUG,
134 		"Try to associate due to network getting re-enabled");
135 	if (wpa_supplicant_fast_associate(wpa_s) != 1) {
136 		wpa_supplicant_cancel_sched_scan(wpa_s);
137 		wpa_supplicant_req_scan(wpa_s, 0, 0);
138 	}
139 }
140 
141 
__wpa_supplicant_get_new_bss(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * ssid,size_t ssid_len)142 static struct wpa_bss * __wpa_supplicant_get_new_bss(
143 	struct wpa_supplicant *wpa_s, const u8 *bssid, const u8 *ssid,
144 	size_t ssid_len)
145 {
146 	if (ssid && ssid_len > 0)
147 		return wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
148 	else
149 		return wpa_bss_get_bssid(wpa_s, bssid);
150 }
151 
152 
_wpa_supplicant_get_new_bss(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * ssid,size_t ssid_len,bool try_update_scan_results)153 static struct wpa_bss * _wpa_supplicant_get_new_bss(
154 	struct wpa_supplicant *wpa_s, const u8 *bssid, const u8 *ssid,
155 	size_t ssid_len, bool try_update_scan_results)
156 {
157 	struct wpa_bss *bss = __wpa_supplicant_get_new_bss(wpa_s, bssid, ssid,
158 							   ssid_len);
159 
160 	if (bss || !try_update_scan_results)
161 		return bss;
162 
163 	wpa_supplicant_update_scan_results(wpa_s, bssid);
164 
165 	return __wpa_supplicant_get_new_bss(wpa_s, bssid, ssid, ssid_len);
166 }
167 
168 
wpa_supplicant_get_new_bss(struct wpa_supplicant * wpa_s,const u8 * bssid)169 static struct wpa_bss * wpa_supplicant_get_new_bss(
170 	struct wpa_supplicant *wpa_s, const u8 *bssid)
171 {
172 	struct wpa_bss *bss = NULL;
173 	struct wpa_ssid *ssid = wpa_s->current_ssid;
174 	u8 drv_ssid[SSID_MAX_LEN];
175 	int res;
176 	bool try_update_scan_results = true;
177 
178 	res = wpa_drv_get_ssid(wpa_s, drv_ssid);
179 	if (res > 0) {
180 		bss = _wpa_supplicant_get_new_bss(wpa_s, bssid, drv_ssid, res,
181 						  try_update_scan_results);
182 		try_update_scan_results = false;
183 	}
184 	if (!bss && ssid && ssid->ssid_len > 0) {
185 		bss = _wpa_supplicant_get_new_bss(wpa_s, bssid, ssid->ssid,
186 						  ssid->ssid_len,
187 						  try_update_scan_results);
188 		try_update_scan_results = false;
189 	}
190 	if (!bss)
191 		bss = _wpa_supplicant_get_new_bss(wpa_s, bssid, NULL, 0,
192 						  try_update_scan_results);
193 
194 	return bss;
195 }
196 
197 
198 static struct wpa_bss *
wpa_supplicant_update_current_bss(struct wpa_supplicant * wpa_s,const u8 * bssid)199 wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s, const u8 *bssid)
200 {
201 	struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
202 
203 	if (bss)
204 		wpa_s->current_bss = bss;
205 
206 	return bss;
207 }
208 
209 
wpa_supplicant_update_link_bss(struct wpa_supplicant * wpa_s,u8 link_id,const u8 * bssid)210 static void wpa_supplicant_update_link_bss(struct wpa_supplicant *wpa_s,
211 					   u8 link_id, const u8 *bssid)
212 {
213 	struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
214 
215 	if (bss)
216 		wpa_s->links[link_id].bss = bss;
217 }
218 
219 
wpa_supplicant_select_config(struct wpa_supplicant * wpa_s,union wpa_event_data * data)220 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s,
221 					union wpa_event_data *data)
222 {
223 	struct wpa_ssid *ssid, *old_ssid;
224 	struct wpa_bss *bss;
225 	u8 drv_ssid[SSID_MAX_LEN];
226 	size_t drv_ssid_len;
227 	int res;
228 
229 	if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
230 		wpa_supplicant_update_current_bss(wpa_s, wpa_s->bssid);
231 
232 		if (wpa_s->current_ssid->ssid_len == 0)
233 			return 0; /* current profile still in use */
234 		res = wpa_drv_get_ssid(wpa_s, drv_ssid);
235 		if (res < 0) {
236 			wpa_msg(wpa_s, MSG_INFO,
237 				"Failed to read SSID from driver");
238 			return 0; /* try to use current profile */
239 		}
240 		drv_ssid_len = res;
241 
242 		if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
243 		    os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
244 			      drv_ssid_len) == 0)
245 			return 0; /* current profile still in use */
246 
247 #ifdef CONFIG_OWE
248 		if ((wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
249 		    wpa_s->current_bss &&
250 		    (wpa_s->current_bss->flags & WPA_BSS_OWE_TRANSITION) &&
251 		    drv_ssid_len == wpa_s->current_bss->ssid_len &&
252 		    os_memcmp(drv_ssid, wpa_s->current_bss->ssid,
253 			      drv_ssid_len) == 0)
254 			return 0; /* current profile still in use */
255 #endif /* CONFIG_OWE */
256 
257 		wpa_msg(wpa_s, MSG_DEBUG,
258 			"Driver-initiated BSS selection changed the SSID to %s",
259 			wpa_ssid_txt(drv_ssid, drv_ssid_len));
260 		/* continue selecting a new network profile */
261 	}
262 
263 	wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
264 		"information");
265 	ssid = wpa_supplicant_get_ssid(wpa_s);
266 	if (ssid == NULL) {
267 		wpa_msg(wpa_s, MSG_INFO,
268 			"No network configuration found for the current AP");
269 		return -1;
270 	}
271 
272 	if (wpas_network_disabled(wpa_s, ssid)) {
273 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
274 		return -1;
275 	}
276 
277 	if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
278 	    disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
279 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
280 		return -1;
281 	}
282 
283 	res = wpas_temp_disabled(wpa_s, ssid);
284 	if (res > 0) {
285 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
286 			"disabled for %d second(s)", res);
287 		return -1;
288 	}
289 
290 	wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
291 		"current AP");
292 	bss = wpa_supplicant_update_current_bss(wpa_s, wpa_s->bssid);
293 	if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
294 		u8 wpa_ie[80];
295 		size_t wpa_ie_len = sizeof(wpa_ie);
296 		bool skip_default_rsne;
297 
298 		/* Do not override RSNE/RSNXE with the default values if the
299 		 * driver indicated the actual values used in the
300 		 * (Re)Association Request frame. */
301 		skip_default_rsne = data && data->assoc_info.req_ies;
302 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
303 					      wpa_ie, &wpa_ie_len,
304 					      skip_default_rsne) < 0)
305 			wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
306 	} else {
307 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
308 	}
309 
310 	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
311 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
312 	old_ssid = wpa_s->current_ssid;
313 	wpa_s->current_ssid = ssid;
314 
315 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
316 	wpa_supplicant_initiate_eapol(wpa_s);
317 	if (old_ssid != wpa_s->current_ssid)
318 		wpas_notify_network_changed(wpa_s);
319 
320 	return 0;
321 }
322 
323 
wpa_supplicant_stop_countermeasures(void * eloop_ctx,void * sock_ctx)324 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
325 {
326 	struct wpa_supplicant *wpa_s = eloop_ctx;
327 
328 	if (wpa_s->countermeasures) {
329 		wpa_s->countermeasures = 0;
330 		wpa_drv_set_countermeasures(wpa_s, 0);
331 		wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
332 
333 		/*
334 		 * It is possible that the device is sched scanning, which means
335 		 * that a connection attempt will be done only when we receive
336 		 * scan results. However, in this case, it would be preferable
337 		 * to scan and connect immediately, so cancel the sched_scan and
338 		 * issue a regular scan flow.
339 		 */
340 		wpa_supplicant_cancel_sched_scan(wpa_s);
341 		wpa_supplicant_req_scan(wpa_s, 0, 0);
342 	}
343 }
344 
345 
wpas_reset_mlo_info(struct wpa_supplicant * wpa_s)346 void wpas_reset_mlo_info(struct wpa_supplicant *wpa_s)
347 {
348 	if (!wpa_s->valid_links)
349 		return;
350 
351 	wpa_s->valid_links = 0;
352 	wpa_s->mlo_assoc_link_id = 0;
353 	os_memset(wpa_s->ap_mld_addr, 0, ETH_ALEN);
354 	os_memset(wpa_s->links, 0, sizeof(wpa_s->links));
355 }
356 
357 
wpa_supplicant_mark_disassoc(struct wpa_supplicant * wpa_s)358 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
359 {
360 	int bssid_changed;
361 
362 	wnm_bss_keep_alive_deinit(wpa_s);
363 
364 #ifdef CONFIG_IBSS_RSN
365 	ibss_rsn_deinit(wpa_s->ibss_rsn);
366 	wpa_s->ibss_rsn = NULL;
367 #endif /* CONFIG_IBSS_RSN */
368 
369 #ifdef CONFIG_AP
370 	wpa_supplicant_ap_deinit(wpa_s);
371 #endif /* CONFIG_AP */
372 
373 #ifdef CONFIG_HS20
374 	/* Clear possibly configured frame filters */
375 	wpa_drv_configure_frame_filters(wpa_s, 0);
376 #endif /* CONFIG_HS20 */
377 
378 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
379 		return;
380 
381 	if (os_reltime_initialized(&wpa_s->session_start)) {
382 		os_reltime_age(&wpa_s->session_start, &wpa_s->session_length);
383 		wpa_s->session_start.sec = 0;
384 		wpa_s->session_start.usec = 0;
385 		wpas_notify_session_length(wpa_s);
386 	}
387 
388 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
389 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
390 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
391 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
392 	sme_clear_on_disassoc(wpa_s);
393 	wpa_s->current_bss = NULL;
394 	wpa_s->assoc_freq = 0;
395 
396 	if (bssid_changed)
397 		wpas_notify_bssid_changed(wpa_s);
398 
399 	eapol_sm_notify_portEnabled(wpa_s->eapol, false);
400 	eapol_sm_notify_portValid(wpa_s->eapol, false);
401 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
402 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
403 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP || wpa_s->drv_authorized_port)
404 		eapol_sm_notify_eap_success(wpa_s->eapol, false);
405 	wpa_s->drv_authorized_port = 0;
406 	wpa_s->ap_ies_from_associnfo = 0;
407 	wpa_s->current_ssid = NULL;
408 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
409 	wpa_s->key_mgmt = 0;
410 	wpa_s->allowed_key_mgmts = 0;
411 
412 #ifndef CONFIG_NO_RRM
413 	wpas_rrm_reset(wpa_s);
414 #endif /* CONFIG_NO_RRM */
415 	wpa_s->wnmsleep_used = 0;
416 #ifdef CONFIG_WNM
417 	wpa_s->wnm_mode = 0;
418 #endif /* CONFIG_WNM */
419 	wnm_clear_coloc_intf_reporting(wpa_s);
420 	wpa_s->disable_mbo_oce = 0;
421 
422 #ifdef CONFIG_TESTING_OPTIONS
423 	wpa_s->last_tk_alg = WPA_ALG_NONE;
424 	os_memset(wpa_s->last_tk, 0, sizeof(wpa_s->last_tk));
425 #endif /* CONFIG_TESTING_OPTIONS */
426 	wpa_s->ieee80211ac = 0;
427 
428 	if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
429 		wpa_s->enabled_4addr_mode = 0;
430 
431 	wpa_s->wps_scan_done = false;
432 	wpas_reset_mlo_info(wpa_s);
433 
434 #ifdef CONFIG_SME
435 	wpa_s->sme.bss_max_idle_period = 0;
436 #endif /* CONFIG_SME */
437 
438 	wpa_s->ssid_verified = false;
439 	wpa_s->bigtk_set = false;
440 }
441 
442 
wpa_find_assoc_pmkid(struct wpa_supplicant * wpa_s,bool authorized)443 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s, bool authorized)
444 {
445 	struct wpa_ie_data ie;
446 	int pmksa_set = -1;
447 	size_t i;
448 	struct rsn_pmksa_cache_entry *cur_pmksa;
449 
450 	/* Start with assumption of no PMKSA cache entry match for cases other
451 	 * than SAE. In particular, this is needed to generate the PMKSA cache
452 	 * entries for Suite B cases with driver-based roaming indication. */
453 	cur_pmksa = pmksa_cache_get_current(wpa_s->wpa);
454 	if (cur_pmksa && !wpa_key_mgmt_sae(cur_pmksa->akmp))
455 		pmksa_cache_clear_current(wpa_s->wpa);
456 
457 	if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
458 	    ie.pmkid == NULL)
459 		return;
460 
461 	for (i = 0; i < ie.num_pmkid; i++) {
462 		pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
463 						    ie.pmkid + i * PMKID_LEN,
464 						    NULL, NULL, 0, NULL, 0,
465 						    true);
466 		if (pmksa_set == 0) {
467 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
468 			if (authorized)
469 				wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
470 			break;
471 		}
472 	}
473 
474 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
475 		"PMKSA cache", pmksa_set == 0 ? "" : "not ");
476 }
477 
478 
wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant * wpa_s,union wpa_event_data * data)479 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
480 						 union wpa_event_data *data)
481 {
482 	if (data == NULL) {
483 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
484 			"event");
485 		return;
486 	}
487 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
488 		" index=%d preauth=%d",
489 		MAC2STR(data->pmkid_candidate.bssid),
490 		data->pmkid_candidate.index,
491 		data->pmkid_candidate.preauth);
492 
493 	pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
494 			    data->pmkid_candidate.index,
495 			    data->pmkid_candidate.preauth);
496 }
497 
498 
wpa_supplicant_dynamic_keys(struct wpa_supplicant * wpa_s)499 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
500 {
501 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
502 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
503 		return 0;
504 
505 #ifdef IEEE8021X_EAPOL
506 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
507 	    wpa_s->current_ssid &&
508 	    !(wpa_s->current_ssid->eapol_flags &
509 	      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
510 	       EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
511 		/* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
512 		 * plaintext or static WEP keys). */
513 		return 0;
514 	}
515 #endif /* IEEE8021X_EAPOL */
516 
517 	return 1;
518 }
519 
520 
521 /**
522  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
523  * @wpa_s: pointer to wpa_supplicant data
524  * @ssid: Configuration data for the network
525  * Returns: 0 on success, -1 on failure
526  *
527  * This function is called when starting authentication with a network that is
528  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
529  */
wpa_supplicant_scard_init(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)530 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
531 			      struct wpa_ssid *ssid)
532 {
533 #ifdef IEEE8021X_EAPOL
534 #ifdef PCSC_FUNCS
535 	int aka = 0, sim = 0;
536 
537 	if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
538 	    wpa_s->scard != NULL || wpa_s->conf->external_sim)
539 		return 0;
540 
541 	if (ssid == NULL || ssid->eap.eap_methods == NULL) {
542 		sim = 1;
543 		aka = 1;
544 	} else {
545 		struct eap_method_type *eap = ssid->eap.eap_methods;
546 		while (eap->vendor != EAP_VENDOR_IETF ||
547 		       eap->method != EAP_TYPE_NONE) {
548 			if (eap->vendor == EAP_VENDOR_IETF) {
549 				if (eap->method == EAP_TYPE_SIM)
550 					sim = 1;
551 				else if (eap->method == EAP_TYPE_AKA ||
552 					 eap->method == EAP_TYPE_AKA_PRIME)
553 					aka = 1;
554 			}
555 			eap++;
556 		}
557 	}
558 
559 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
560 		sim = 0;
561 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
562 	    eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
563 	    NULL)
564 		aka = 0;
565 
566 	if (!sim && !aka) {
567 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
568 			"use SIM, but neither EAP-SIM nor EAP-AKA are "
569 			"enabled");
570 		return 0;
571 	}
572 
573 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
574 		"(sim=%d aka=%d) - initialize PCSC", sim, aka);
575 
576 	wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
577 	if (wpa_s->scard == NULL) {
578 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
579 			"(pcsc-lite)");
580 		return -1;
581 	}
582 	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
583 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
584 #endif /* PCSC_FUNCS */
585 #endif /* IEEE8021X_EAPOL */
586 
587 	return 0;
588 }
589 
590 
591 #ifndef CONFIG_NO_SCAN_PROCESSING
592 
593 #ifdef CONFIG_WEP
has_wep_key(struct wpa_ssid * ssid)594 static int has_wep_key(struct wpa_ssid *ssid)
595 {
596 	int i;
597 
598 	for (i = 0; i < NUM_WEP_KEYS; i++) {
599 		if (ssid->wep_key_len[i])
600 			return 1;
601 	}
602 
603 	return 0;
604 }
605 #endif /* CONFIG_WEP */
606 
607 
wpa_supplicant_match_privacy(struct wpa_bss * bss,struct wpa_ssid * ssid)608 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
609 					struct wpa_ssid *ssid)
610 {
611 	int privacy = 0;
612 
613 	if (ssid->mixed_cell)
614 		return 1;
615 
616 #ifdef CONFIG_WPS
617 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
618 		return 1;
619 #endif /* CONFIG_WPS */
620 
621 #ifdef CONFIG_OWE
622 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only)
623 		return 1;
624 #endif /* CONFIG_OWE */
625 
626 #ifdef CONFIG_WEP
627 	if (has_wep_key(ssid))
628 		privacy = 1;
629 #endif /* CONFIG_WEP */
630 
631 #ifdef IEEE8021X_EAPOL
632 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
633 	    ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
634 				 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
635 		privacy = 1;
636 #endif /* IEEE8021X_EAPOL */
637 
638 	if (wpa_key_mgmt_wpa(ssid->key_mgmt))
639 		privacy = 1;
640 
641 	if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
642 		privacy = 1;
643 
644 	if (bss->caps & IEEE80211_CAP_PRIVACY)
645 		return privacy;
646 	return !privacy;
647 }
648 
649 
wpa_supplicant_ssid_bss_match(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss,int debug_print)650 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
651 					 struct wpa_ssid *ssid,
652 					 struct wpa_bss *bss, int debug_print)
653 {
654 	struct wpa_ie_data ie;
655 	int proto_match = 0;
656 	const u8 *rsn_ie, *wpa_ie;
657 	int ret;
658 #ifdef CONFIG_WEP
659 	int wep_ok;
660 #endif /* CONFIG_WEP */
661 	bool is_6ghz_bss = is_6ghz_freq(bss->freq);
662 
663 	ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
664 	if (ret >= 0)
665 		return ret;
666 
667 #ifdef CONFIG_WEP
668 	/* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
669 	wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
670 		(((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
671 		  ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
672 		 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
673 #endif /* CONFIG_WEP */
674 
675 	rsn_ie = wpa_bss_get_rsne(wpa_s, bss, ssid, false);
676 	if (is_6ghz_bss && !rsn_ie) {
677 		if (debug_print)
678 			wpa_dbg(wpa_s, MSG_DEBUG,
679 				"   skip - 6 GHz BSS without RSNE");
680 		return 0;
681 	}
682 
683 	while ((ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)) && rsn_ie) {
684 		proto_match++;
685 
686 		if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
687 			if (debug_print)
688 				wpa_dbg(wpa_s, MSG_DEBUG,
689 					"   skip RSN IE - parse failed");
690 			break;
691 		}
692 		if (!ie.has_pairwise)
693 			ie.pairwise_cipher = wpa_default_rsn_cipher(bss->freq);
694 		if (!ie.has_group)
695 			ie.group_cipher = wpa_default_rsn_cipher(bss->freq);
696 
697 		if (is_6ghz_bss || !is_zero_ether_addr(bss->mld_addr)) {
698 			/* WEP and TKIP are not allowed on 6 GHz/MLD */
699 			ie.pairwise_cipher &= ~(WPA_CIPHER_WEP40 |
700 						WPA_CIPHER_WEP104 |
701 						WPA_CIPHER_TKIP);
702 			ie.group_cipher &= ~(WPA_CIPHER_WEP40 |
703 					     WPA_CIPHER_WEP104 |
704 					     WPA_CIPHER_TKIP);
705 		}
706 
707 #ifdef CONFIG_WEP
708 		if (wep_ok &&
709 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
710 		{
711 			if (debug_print)
712 				wpa_dbg(wpa_s, MSG_DEBUG,
713 					"   selected based on TSN in RSN IE");
714 			return 1;
715 		}
716 #endif /* CONFIG_WEP */
717 
718 		if (!(ie.proto & ssid->proto) &&
719 		    !(ssid->proto & WPA_PROTO_OSEN)) {
720 			if (debug_print)
721 				wpa_dbg(wpa_s, MSG_DEBUG,
722 					"   skip RSN IE - proto mismatch");
723 			break;
724 		}
725 
726 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
727 			if (debug_print)
728 				wpa_dbg(wpa_s, MSG_DEBUG,
729 					"   skip RSN IE - PTK cipher mismatch");
730 			break;
731 		}
732 
733 		if (!(ie.group_cipher & ssid->group_cipher)) {
734 			if (debug_print)
735 				wpa_dbg(wpa_s, MSG_DEBUG,
736 					"   skip RSN IE - GTK cipher mismatch");
737 			break;
738 		}
739 
740 		if (ssid->group_mgmt_cipher &&
741 		    !(ie.mgmt_group_cipher & ssid->group_mgmt_cipher)) {
742 			if (debug_print)
743 				wpa_dbg(wpa_s, MSG_DEBUG,
744 					"   skip RSN IE - group mgmt cipher mismatch");
745 			break;
746 		}
747 
748 		if (is_6ghz_bss) {
749 			/* MFPC must be supported on 6 GHz */
750 			if (!(ie.capabilities & WPA_CAPABILITY_MFPC)) {
751 				if (debug_print)
752 					wpa_dbg(wpa_s, MSG_DEBUG,
753 						"   skip RSNE - 6 GHz without MFPC");
754 				break;
755 			}
756 
757 			/* WPA PSK is not allowed on the 6 GHz band */
758 			ie.key_mgmt &= ~(WPA_KEY_MGMT_PSK |
759 					 WPA_KEY_MGMT_FT_PSK |
760 					 WPA_KEY_MGMT_PSK_SHA256);
761 		}
762 
763 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
764 			if (debug_print)
765 				wpa_dbg(wpa_s, MSG_DEBUG,
766 					"   skip RSN IE - key mgmt mismatch");
767 			break;
768 		}
769 
770 		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
771 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
772 		    MGMT_FRAME_PROTECTION_REQUIRED) {
773 			if (debug_print)
774 				wpa_dbg(wpa_s, MSG_DEBUG,
775 					"   skip RSN IE - no mgmt frame protection");
776 			break;
777 		}
778 		if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
779 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
780 		    NO_MGMT_FRAME_PROTECTION) {
781 			if (debug_print)
782 				wpa_dbg(wpa_s, MSG_DEBUG,
783 					"   skip RSN IE - no mgmt frame protection enabled but AP requires it");
784 			break;
785 		}
786 
787 		if (debug_print)
788 			wpa_dbg(wpa_s, MSG_DEBUG,
789 				"   selected based on RSN IE");
790 		return 1;
791 	}
792 
793 	if (is_6ghz_bss) {
794 		if (debug_print)
795 			wpa_dbg(wpa_s, MSG_DEBUG,
796 				"   skip - 6 GHz BSS without matching RSNE");
797 		return 0;
798 	}
799 
800 	wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
801 
802 	if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED &&
803 	    (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE) || ssid->owe_only)) {
804 #ifdef CONFIG_OWE
805 		if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && ssid->owe_only &&
806 		    !wpa_ie && !rsn_ie &&
807 		    wpa_s->owe_transition_select &&
808 		    wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE) &&
809 		    ssid->owe_transition_bss_select_count + 1 <=
810 		    MAX_OWE_TRANSITION_BSS_SELECT_COUNT) {
811 			ssid->owe_transition_bss_select_count++;
812 			if (debug_print)
813 				wpa_dbg(wpa_s, MSG_DEBUG,
814 					"   skip OWE open BSS (selection count %d does not exceed %d)",
815 					ssid->owe_transition_bss_select_count,
816 					MAX_OWE_TRANSITION_BSS_SELECT_COUNT);
817 			wpa_s->owe_transition_search = 1;
818 			return 0;
819 		}
820 #endif /* CONFIG_OWE */
821 		if (debug_print)
822 			wpa_dbg(wpa_s, MSG_DEBUG,
823 				"   skip - MFP Required but network not MFP Capable");
824 		return 0;
825 	}
826 
827 	while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
828 		proto_match++;
829 
830 		if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
831 			if (debug_print)
832 				wpa_dbg(wpa_s, MSG_DEBUG,
833 					"   skip WPA IE - parse failed");
834 			break;
835 		}
836 
837 #ifdef CONFIG_WEP
838 		if (wep_ok &&
839 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
840 		{
841 			if (debug_print)
842 				wpa_dbg(wpa_s, MSG_DEBUG,
843 					"   selected based on TSN in WPA IE");
844 			return 1;
845 		}
846 #endif /* CONFIG_WEP */
847 
848 		if (!(ie.proto & ssid->proto)) {
849 			if (debug_print)
850 				wpa_dbg(wpa_s, MSG_DEBUG,
851 					"   skip WPA IE - proto mismatch");
852 			break;
853 		}
854 
855 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
856 			if (debug_print)
857 				wpa_dbg(wpa_s, MSG_DEBUG,
858 					"   skip WPA IE - PTK cipher mismatch");
859 			break;
860 		}
861 
862 		if (!(ie.group_cipher & ssid->group_cipher)) {
863 			if (debug_print)
864 				wpa_dbg(wpa_s, MSG_DEBUG,
865 					"   skip WPA IE - GTK cipher mismatch");
866 			break;
867 		}
868 
869 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
870 			if (debug_print)
871 				wpa_dbg(wpa_s, MSG_DEBUG,
872 					"   skip WPA IE - key mgmt mismatch");
873 			break;
874 		}
875 
876 		if (debug_print)
877 			wpa_dbg(wpa_s, MSG_DEBUG,
878 				"   selected based on WPA IE");
879 		return 1;
880 	}
881 
882 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
883 	    !rsn_ie) {
884 		if (debug_print)
885 			wpa_dbg(wpa_s, MSG_DEBUG,
886 				"   allow for non-WPA IEEE 802.1X");
887 		return 1;
888 	}
889 
890 #ifdef CONFIG_OWE
891 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only &&
892 	    !wpa_ie && !rsn_ie) {
893 		if (wpa_s->owe_transition_select &&
894 		    wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE) &&
895 		    ssid->owe_transition_bss_select_count + 1 <=
896 		    MAX_OWE_TRANSITION_BSS_SELECT_COUNT) {
897 			ssid->owe_transition_bss_select_count++;
898 			if (debug_print)
899 				wpa_dbg(wpa_s, MSG_DEBUG,
900 					"   skip OWE transition BSS (selection count %d does not exceed %d)",
901 					ssid->owe_transition_bss_select_count,
902 					MAX_OWE_TRANSITION_BSS_SELECT_COUNT);
903 			wpa_s->owe_transition_search = 1;
904 			return 0;
905 		}
906 		if (debug_print)
907 			wpa_dbg(wpa_s, MSG_DEBUG,
908 				"   allow in OWE transition mode");
909 		return 1;
910 	}
911 #endif /* CONFIG_OWE */
912 
913 	if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
914 	    wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
915 		if (debug_print)
916 			wpa_dbg(wpa_s, MSG_DEBUG,
917 				"   skip - no WPA/RSN proto match");
918 		return 0;
919 	}
920 
921 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
922 	    wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
923 		if (debug_print)
924 			wpa_dbg(wpa_s, MSG_DEBUG, "   allow in OSEN");
925 		return 1;
926 	}
927 
928 	if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
929 		if (debug_print)
930 			wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
931 		return 1;
932 	}
933 
934 	if (debug_print)
935 		wpa_dbg(wpa_s, MSG_DEBUG,
936 			"   reject due to mismatch with WPA/WPA2");
937 
938 	return 0;
939 }
940 
941 
freq_allowed(int * freqs,int freq)942 static int freq_allowed(int *freqs, int freq)
943 {
944 	int i;
945 
946 	if (freqs == NULL)
947 		return 1;
948 
949 	for (i = 0; freqs[i]; i++)
950 		if (freqs[i] == freq)
951 			return 1;
952 	return 0;
953 }
954 
955 
rate_match(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss,int debug_print)956 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
957 		      struct wpa_bss *bss, int debug_print)
958 {
959 	const struct hostapd_hw_modes *mode = NULL, *modes;
960 	const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
961 	const u8 *rate_ie;
962 	int i, j, k;
963 
964 	if (bss->freq == 0)
965 		return 1; /* Cannot do matching without knowing band */
966 
967 	modes = wpa_s->hw.modes;
968 	if (modes == NULL) {
969 		/*
970 		 * The driver does not provide any additional information
971 		 * about the utilized hardware, so allow the connection attempt
972 		 * to continue.
973 		 */
974 		return 1;
975 	}
976 
977 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
978 		for (j = 0; j < modes[i].num_channels; j++) {
979 			int freq = modes[i].channels[j].freq;
980 			if (freq == bss->freq) {
981 				if (mode &&
982 				    mode->mode == HOSTAPD_MODE_IEEE80211G)
983 					break; /* do not allow 802.11b replace
984 						* 802.11g */
985 				mode = &modes[i];
986 				break;
987 			}
988 		}
989 	}
990 
991 	if (mode == NULL)
992 		return 0;
993 
994 	for (i = 0; i < (int) sizeof(scan_ie); i++) {
995 		rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
996 		if (rate_ie == NULL)
997 			continue;
998 
999 		for (j = 2; j < rate_ie[1] + 2; j++) {
1000 			int flagged = !!(rate_ie[j] & 0x80);
1001 			int r = (rate_ie[j] & 0x7f) * 5;
1002 
1003 			/*
1004 			 * IEEE Std 802.11n-2009 7.3.2.2:
1005 			 * The new BSS Membership selector value is encoded
1006 			 * like a legacy basic rate, but it is not a rate and
1007 			 * only indicates if the BSS members are required to
1008 			 * support the mandatory features of Clause 20 [HT PHY]
1009 			 * in order to join the BSS.
1010 			 */
1011 			if (flagged && ((rate_ie[j] & 0x7f) ==
1012 					BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
1013 				if (!ht_supported(mode)) {
1014 					if (debug_print)
1015 						wpa_dbg(wpa_s, MSG_DEBUG,
1016 							"   hardware does not support HT PHY");
1017 					return 0;
1018 				}
1019 				continue;
1020 			}
1021 
1022 			/* There's also a VHT selector for 802.11ac */
1023 			if (flagged && ((rate_ie[j] & 0x7f) ==
1024 					BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
1025 				if (!vht_supported(mode)) {
1026 					if (debug_print)
1027 						wpa_dbg(wpa_s, MSG_DEBUG,
1028 							"   hardware does not support VHT PHY");
1029 					return 0;
1030 				}
1031 				continue;
1032 			}
1033 
1034 			if (flagged && ((rate_ie[j] & 0x7f) ==
1035 					BSS_MEMBERSHIP_SELECTOR_HE_PHY)) {
1036 				if (!he_supported(mode, IEEE80211_MODE_INFRA)) {
1037 					if (debug_print)
1038 						wpa_dbg(wpa_s, MSG_DEBUG,
1039 							"   hardware does not support HE PHY");
1040 					return 0;
1041 				}
1042 				continue;
1043 			}
1044 
1045 #ifdef CONFIG_SAE
1046 			if (flagged && ((rate_ie[j] & 0x7f) ==
1047 					BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY)) {
1048 				if (wpa_s->conf->sae_pwe ==
1049 				    SAE_PWE_HUNT_AND_PECK &&
1050 				    !ssid->sae_password_id &&
1051 				    !is_6ghz_freq(bss->freq) &&
1052 				    wpa_key_mgmt_sae(ssid->key_mgmt)) {
1053 					if (debug_print)
1054 						wpa_dbg(wpa_s, MSG_DEBUG,
1055 							"   SAE H2E disabled");
1056 #ifdef CONFIG_TESTING_OPTIONS
1057 					if (wpa_s->ignore_sae_h2e_only) {
1058 						wpa_dbg(wpa_s, MSG_DEBUG,
1059 							"TESTING: Ignore SAE H2E requirement mismatch");
1060 						continue;
1061 					}
1062 #endif /* CONFIG_TESTING_OPTIONS */
1063 					return 0;
1064 				}
1065 				continue;
1066 			}
1067 #endif /* CONFIG_SAE */
1068 
1069 			if (!flagged)
1070 				continue;
1071 
1072 			/* check for legacy basic rates */
1073 			for (k = 0; k < mode->num_rates; k++) {
1074 				if (mode->rates[k] == r)
1075 					break;
1076 			}
1077 			if (k == mode->num_rates) {
1078 				/*
1079 				 * IEEE Std 802.11-2007 7.3.2.2 demands that in
1080 				 * order to join a BSS all required rates
1081 				 * have to be supported by the hardware.
1082 				 */
1083 				if (debug_print)
1084 					wpa_dbg(wpa_s, MSG_DEBUG,
1085 						"   hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
1086 						r / 10, r % 10,
1087 						bss->freq, mode->mode, mode->num_rates);
1088 				return 0;
1089 			}
1090 		}
1091 	}
1092 
1093 	return 1;
1094 }
1095 
1096 
1097 /*
1098  * Test whether BSS is in an ESS.
1099  * This is done differently in DMG (60 GHz) and non-DMG bands
1100  */
bss_is_ess(struct wpa_bss * bss)1101 static int bss_is_ess(struct wpa_bss *bss)
1102 {
1103 	if (bss_is_dmg(bss)) {
1104 		return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
1105 			IEEE80211_CAP_DMG_AP;
1106 	}
1107 
1108 	return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
1109 		IEEE80211_CAP_ESS);
1110 }
1111 
1112 
match_mac_mask(const u8 * addr_a,const u8 * addr_b,const u8 * mask)1113 static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
1114 {
1115 	size_t i;
1116 
1117 	for (i = 0; i < ETH_ALEN; i++) {
1118 		if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
1119 			return 0;
1120 	}
1121 	return 1;
1122 }
1123 
1124 
addr_in_list(const u8 * addr,const u8 * list,size_t num)1125 static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
1126 {
1127 	size_t i;
1128 
1129 	for (i = 0; i < num; i++) {
1130 		const u8 *a = list + i * ETH_ALEN * 2;
1131 		const u8 *m = a + ETH_ALEN;
1132 
1133 		if (match_mac_mask(a, addr, m))
1134 			return 1;
1135 	}
1136 	return 0;
1137 }
1138 
1139 
owe_trans_ssid(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const u8 ** ret_ssid,size_t * ret_ssid_len)1140 static void owe_trans_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1141 			   const u8 **ret_ssid, size_t *ret_ssid_len)
1142 {
1143 #ifdef CONFIG_OWE
1144 	const u8 *owe, *bssid;
1145 
1146 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
1147 	if (!owe || !wpa_bss_get_rsne(wpa_s, bss, NULL, false))
1148 		return;
1149 
1150 	if (wpas_get_owe_trans_network(owe, &bssid, ret_ssid, ret_ssid_len))
1151 		return;
1152 
1153 	/* Match the profile SSID against the OWE transition mode SSID on the
1154 	 * open network. */
1155 	wpa_dbg(wpa_s, MSG_DEBUG, "OWE: transition mode BSSID: " MACSTR
1156 		" SSID: %s", MAC2STR(bssid),
1157 		wpa_ssid_txt(*ret_ssid, *ret_ssid_len));
1158 
1159 	if (!(bss->flags & WPA_BSS_OWE_TRANSITION)) {
1160 		struct wpa_ssid *ssid;
1161 
1162 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1163 			if (wpas_network_disabled(wpa_s, ssid))
1164 				continue;
1165 			if (ssid->ssid_len == *ret_ssid_len &&
1166 			    os_memcmp(ssid->ssid, ret_ssid, *ret_ssid_len) == 0) {
1167 				/* OWE BSS in transition mode for a currently
1168 				 * enabled OWE network. */
1169 				wpa_dbg(wpa_s, MSG_DEBUG,
1170 					"OWE: transition mode OWE SSID for active OWE profile");
1171 				bss->flags |= WPA_BSS_OWE_TRANSITION;
1172 				break;
1173 			}
1174 		}
1175 	}
1176 #endif /* CONFIG_OWE */
1177 }
1178 
1179 
wpas_valid_ml_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)1180 static bool wpas_valid_ml_bss(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
1181 {
1182 	u16 removed_links;
1183 
1184 	if (wpa_bss_parse_basic_ml_element(wpa_s, bss, NULL, NULL, NULL, NULL))
1185 		return true;
1186 
1187 	if (!bss->valid_links)
1188 		return true;
1189 
1190 	/* Check if the current BSS is going to be removed */
1191 	removed_links = wpa_bss_parse_reconf_ml_element(wpa_s, bss);
1192 	if (BIT(bss->mld_link_id) & removed_links)
1193 		return false;
1194 
1195 	return true;
1196 }
1197 
1198 
disabled_freq(struct wpa_supplicant * wpa_s,int freq)1199 int disabled_freq(struct wpa_supplicant *wpa_s, int freq)
1200 {
1201 	int i, j;
1202 
1203 	if (!wpa_s->hw.modes || !wpa_s->hw.num_modes)
1204 		return 0;
1205 
1206 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
1207 		struct hostapd_hw_modes *mode = &wpa_s->hw.modes[j];
1208 
1209 		for (i = 0; i < mode->num_channels; i++) {
1210 			struct hostapd_channel_data *chan = &mode->channels[i];
1211 
1212 			if (chan->freq == freq)
1213 				return !!(chan->flag & HOSTAPD_CHAN_DISABLED);
1214 		}
1215 	}
1216 
1217 	return 1;
1218 }
1219 
1220 
1221 static bool wpa_scan_res_ok(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
1222 			    const u8 *match_ssid, size_t match_ssid_len,
1223 			    struct wpa_bss *bss, int bssid_ignore_count,
1224 			    bool debug_print);
1225 
1226 
1227 #ifdef CONFIG_SAE_PK
sae_pk_acceptable_bss_with_pk(struct wpa_supplicant * wpa_s,struct wpa_bss * orig_bss,struct wpa_ssid * ssid,const u8 * match_ssid,size_t match_ssid_len)1228 static bool sae_pk_acceptable_bss_with_pk(struct wpa_supplicant *wpa_s,
1229 					  struct wpa_bss *orig_bss,
1230 					  struct wpa_ssid *ssid,
1231 					  const u8 *match_ssid,
1232 					  size_t match_ssid_len)
1233 {
1234 	struct wpa_bss *bss;
1235 
1236 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1237 		int count;
1238 		const u8 *ie;
1239 
1240 		if (bss == orig_bss)
1241 			continue;
1242 		ie = wpa_bss_get_rsnxe(wpa_s, bss, ssid, false);
1243 		if (!(ieee802_11_rsnx_capab(ie, WLAN_RSNX_CAPAB_SAE_PK)))
1244 			continue;
1245 
1246 		/* TODO: Could be more thorough in checking what kind of
1247 		 * signal strength or throughput estimate would be acceptable
1248 		 * compared to the originally selected BSS. */
1249 		if (bss->est_throughput < 2000)
1250 			return false;
1251 
1252 		count = wpa_bssid_ignore_is_listed(wpa_s, bss->bssid);
1253 		if (wpa_scan_res_ok(wpa_s, ssid, match_ssid, match_ssid_len,
1254 				    bss, count, 0))
1255 			return true;
1256 	}
1257 
1258 	return false;
1259 }
1260 #endif /* CONFIG_SAE_PK */
1261 
1262 
wpa_scan_res_ok(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const u8 * match_ssid,size_t match_ssid_len,struct wpa_bss * bss,int bssid_ignore_count,bool debug_print)1263 static bool wpa_scan_res_ok(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
1264 			    const u8 *match_ssid, size_t match_ssid_len,
1265 			    struct wpa_bss *bss, int bssid_ignore_count,
1266 			    bool debug_print)
1267 {
1268 	int res;
1269 	bool wpa, check_ssid, osen, rsn_osen = false;
1270 	struct wpa_ie_data data;
1271 #ifdef CONFIG_MBO
1272 	const u8 *assoc_disallow;
1273 #endif /* CONFIG_MBO */
1274 #ifdef CONFIG_SAE
1275 	u8 rsnxe_capa = 0;
1276 #endif /* CONFIG_SAE */
1277 	const u8 *ie;
1278 
1279 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1280 	wpa = ie && ie[1];
1281 	ie = wpa_bss_get_rsne(wpa_s, bss, ssid, false);
1282 	wpa |= ie && ie[1];
1283 	if (ie && wpa_parse_wpa_ie_rsn(ie, 2 + ie[1], &data) == 0 &&
1284 	    (data.key_mgmt & WPA_KEY_MGMT_OSEN))
1285 		rsn_osen = true;
1286 	ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
1287 	osen = ie != NULL;
1288 
1289 #ifdef CONFIG_SAE
1290 	ie = wpa_bss_get_rsnxe(wpa_s, bss, ssid, false);
1291 	if (ie && ie[0] == WLAN_EID_VENDOR_SPECIFIC && ie[1] >= 4 + 1)
1292 		rsnxe_capa = ie[4 + 2];
1293 	else if (ie && ie[1] >= 1)
1294 		rsnxe_capa = ie[2];
1295 #endif /* CONFIG_SAE */
1296 
1297 	check_ssid = wpa || ssid->ssid_len > 0;
1298 
1299 	if (wpas_network_disabled(wpa_s, ssid)) {
1300 		if (debug_print)
1301 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
1302 		return false;
1303 	}
1304 
1305 	res = wpas_temp_disabled(wpa_s, ssid);
1306 	if (res > 0) {
1307 		if (debug_print)
1308 			wpa_dbg(wpa_s, MSG_DEBUG,
1309 				"   skip - disabled temporarily for %d second(s)",
1310 				res);
1311 		return false;
1312 	}
1313 
1314 #ifdef CONFIG_WPS
1315 	if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && bssid_ignore_count) {
1316 		if (debug_print)
1317 			wpa_dbg(wpa_s, MSG_DEBUG,
1318 				"   skip - BSSID ignored (WPS)");
1319 		return false;
1320 	}
1321 
1322 	if (wpa && ssid->ssid_len == 0 &&
1323 	    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
1324 		check_ssid = false;
1325 
1326 	if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1327 		/* Only allow wildcard SSID match if an AP advertises active
1328 		 * WPS operation that matches our mode. */
1329 		check_ssid = ssid->ssid_len > 0 ||
1330 			!wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss);
1331 	}
1332 #endif /* CONFIG_WPS */
1333 
1334 	if (ssid->bssid_set && ssid->ssid_len == 0 &&
1335 	    ether_addr_equal(bss->bssid, ssid->bssid))
1336 		check_ssid = false;
1337 
1338 	if (check_ssid &&
1339 	    (match_ssid_len != ssid->ssid_len ||
1340 	     os_memcmp(match_ssid, ssid->ssid, match_ssid_len) != 0)) {
1341 		if (debug_print)
1342 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
1343 		return false;
1344 	}
1345 
1346 	if (ssid->bssid_set &&
1347 	    !ether_addr_equal(bss->bssid, ssid->bssid)) {
1348 		if (debug_print)
1349 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
1350 		return false;
1351 	}
1352 
1353 	/* check the list of BSSIDs to ignore */
1354 	if (ssid->num_bssid_ignore &&
1355 	    addr_in_list(bss->bssid, ssid->bssid_ignore,
1356 			 ssid->num_bssid_ignore)) {
1357 		if (debug_print)
1358 			wpa_dbg(wpa_s, MSG_DEBUG,
1359 				"   skip - BSSID configured to be ignored");
1360 		return false;
1361 	}
1362 
1363 	/* if there is a list of accepted BSSIDs, only accept those APs */
1364 	if (ssid->num_bssid_accept &&
1365 	    !addr_in_list(bss->bssid, ssid->bssid_accept,
1366 			  ssid->num_bssid_accept)) {
1367 		if (debug_print)
1368 			wpa_dbg(wpa_s, MSG_DEBUG,
1369 				"   skip - BSSID not in list of accepted values");
1370 		return false;
1371 	}
1372 
1373 	if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss, debug_print))
1374 		return false;
1375 
1376 	if (!osen && !wpa &&
1377 	    !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
1378 	    !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
1379 	    !(ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1380 	    !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
1381 		if (debug_print)
1382 			wpa_dbg(wpa_s, MSG_DEBUG,
1383 				"   skip - non-WPA network not allowed");
1384 		return false;
1385 	}
1386 
1387 #ifdef CONFIG_WEP
1388 	if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) && has_wep_key(ssid)) {
1389 		if (debug_print)
1390 			wpa_dbg(wpa_s, MSG_DEBUG,
1391 				"   skip - ignore WPA/WPA2 AP for WEP network block");
1392 		return false;
1393 	}
1394 #endif /* CONFIG_WEP */
1395 
1396 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen && !rsn_osen) {
1397 		if (debug_print)
1398 			wpa_dbg(wpa_s, MSG_DEBUG,
1399 				"   skip - non-OSEN network not allowed");
1400 		return false;
1401 	}
1402 
1403 	if (!wpa_supplicant_match_privacy(bss, ssid)) {
1404 		if (debug_print)
1405 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy mismatch");
1406 		return false;
1407 	}
1408 
1409 	if (ssid->mode != WPAS_MODE_MESH && !bss_is_ess(bss) &&
1410 	    !bss_is_pbss(bss)) {
1411 		if (debug_print)
1412 			wpa_dbg(wpa_s, MSG_DEBUG,
1413 				"   skip - not ESS, PBSS, or MBSS");
1414 		return false;
1415 	}
1416 
1417 	if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
1418 		if (debug_print)
1419 			wpa_dbg(wpa_s, MSG_DEBUG,
1420 				"   skip - PBSS mismatch (ssid %d bss %d)",
1421 				ssid->pbss, bss_is_pbss(bss));
1422 		return false;
1423 	}
1424 
1425 	if (!freq_allowed(ssid->freq_list, bss->freq)) {
1426 		if (debug_print)
1427 			wpa_dbg(wpa_s, MSG_DEBUG,
1428 				"   skip - frequency not allowed");
1429 		return false;
1430 	}
1431 
1432 #ifdef CONFIG_MESH
1433 	if (ssid->mode == WPAS_MODE_MESH && ssid->frequency > 0 &&
1434 	    ssid->frequency != bss->freq) {
1435 		if (debug_print)
1436 			wpa_dbg(wpa_s, MSG_DEBUG,
1437 				"   skip - frequency not allowed (mesh)");
1438 		return false;
1439 	}
1440 #endif /* CONFIG_MESH */
1441 
1442 	if (!rate_match(wpa_s, ssid, bss, debug_print)) {
1443 		if (debug_print)
1444 			wpa_dbg(wpa_s, MSG_DEBUG,
1445 				"   skip - rate sets do not match");
1446 		return false;
1447 	}
1448 
1449 #ifdef CONFIG_SAE
1450 	/* When using SAE Password Identifier and when operationg on the 6 GHz
1451 	 * band, only H2E is allowed. */
1452 	if ((wpa_s->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
1453 	     is_6ghz_freq(bss->freq) || ssid->sae_password_id) &&
1454 	    wpa_s->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK &&
1455 	    wpa_key_mgmt_sae(ssid->key_mgmt) &&
1456 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
1457 	    !(wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt)) &&
1458 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
1459 	    !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_H2E))) {
1460 		if (debug_print)
1461 			wpa_dbg(wpa_s, MSG_DEBUG,
1462 				"   skip - SAE H2E required, but not supported by the AP");
1463 		return false;
1464 	}
1465 #endif /* CONFIG_SAE */
1466 
1467 #ifdef CONFIG_SAE_PK
1468 	if (ssid->sae_pk == SAE_PK_MODE_ONLY &&
1469 	    !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_PK))) {
1470 		if (debug_print)
1471 			wpa_dbg(wpa_s, MSG_DEBUG,
1472 				"   skip - SAE-PK required, but not supported by the AP");
1473 		return false;
1474 	}
1475 #endif /* CONFIG_SAE_PK */
1476 
1477 #ifndef CONFIG_IBSS_RSN
1478 	if (ssid->mode == WPAS_MODE_IBSS &&
1479 	    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
1480 		if (debug_print)
1481 			wpa_dbg(wpa_s, MSG_DEBUG,
1482 				"   skip - IBSS RSN not supported in the build");
1483 		return false;
1484 	}
1485 #endif /* !CONFIG_IBSS_RSN */
1486 
1487 #ifdef CONFIG_P2P
1488 	if (ssid->p2p_group &&
1489 	    !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1490 	    !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1491 		if (debug_print)
1492 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no P2P IE seen");
1493 		return false;
1494 	}
1495 
1496 	if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1497 		struct wpabuf *p2p_ie;
1498 		u8 dev_addr[ETH_ALEN];
1499 
1500 		ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1501 		if (!ie) {
1502 			if (debug_print)
1503 				wpa_dbg(wpa_s, MSG_DEBUG,
1504 					"   skip - no P2P element");
1505 			return false;
1506 		}
1507 		p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1508 		if (!p2p_ie) {
1509 			if (debug_print)
1510 				wpa_dbg(wpa_s, MSG_DEBUG,
1511 					"   skip - could not fetch P2P element");
1512 			return false;
1513 		}
1514 
1515 		if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0 ||
1516 		    !ether_addr_equal(dev_addr, ssid->go_p2p_dev_addr)) {
1517 			if (debug_print)
1518 				wpa_dbg(wpa_s, MSG_DEBUG,
1519 					"   skip - no matching GO P2P Device Address in P2P element");
1520 			wpabuf_free(p2p_ie);
1521 			return false;
1522 		}
1523 		wpabuf_free(p2p_ie);
1524 	}
1525 
1526 	/*
1527 	 * TODO: skip the AP if its P2P IE has Group Formation bit set in the
1528 	 * P2P Group Capability Bitmap and we are not in Group Formation with
1529 	 * that device.
1530 	 */
1531 #endif /* CONFIG_P2P */
1532 
1533 	if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time)) {
1534 		struct os_reltime diff;
1535 
1536 		os_reltime_sub(&wpa_s->scan_min_time, &bss->last_update, &diff);
1537 		if (debug_print)
1538 			wpa_dbg(wpa_s, MSG_DEBUG,
1539 				"   skip - scan result not recent enough (%u.%06u seconds too old)",
1540 				(unsigned int) diff.sec,
1541 				(unsigned int) diff.usec);
1542 		return false;
1543 	}
1544 #ifdef CONFIG_MBO
1545 #ifdef CONFIG_TESTING_OPTIONS
1546 	if (wpa_s->ignore_assoc_disallow)
1547 		goto skip_assoc_disallow;
1548 #endif /* CONFIG_TESTING_OPTIONS */
1549 	assoc_disallow = wpas_mbo_check_assoc_disallow(bss);
1550 	if (assoc_disallow && assoc_disallow[1] >= 1) {
1551 		if (debug_print)
1552 			wpa_dbg(wpa_s, MSG_DEBUG,
1553 				"   skip - MBO association disallowed (reason %u)",
1554 				assoc_disallow[2]);
1555 		return false;
1556 	}
1557 
1558 	if (wpa_is_bss_tmp_disallowed(wpa_s, bss)) {
1559 		if (debug_print)
1560 			wpa_dbg(wpa_s, MSG_DEBUG,
1561 				"   skip - AP temporarily disallowed");
1562 		return false;
1563 	}
1564 #ifdef CONFIG_TESTING_OPTIONS
1565 skip_assoc_disallow:
1566 #endif /* CONFIG_TESTING_OPTIONS */
1567 #endif /* CONFIG_MBO */
1568 
1569 #ifdef CONFIG_DPP
1570 	if ((ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
1571 	    !wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, wpa_s->own_addr,
1572 				 ssid) &&
1573 	    (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1574 	     !ssid->dpp_csign)) {
1575 		if (debug_print)
1576 			wpa_dbg(wpa_s, MSG_DEBUG,
1577 				"   skip - no PMKSA entry for DPP");
1578 		return false;
1579 	}
1580 #endif /* CONFIG_DPP */
1581 
1582 #ifdef CONFIG_SAE_PK
1583 	if (ssid->sae_pk == SAE_PK_MODE_AUTOMATIC &&
1584 	    wpa_key_mgmt_sae(ssid->key_mgmt) &&
1585 	    ((ssid->sae_password &&
1586 	      sae_pk_valid_password(ssid->sae_password)) ||
1587 	     (!ssid->sae_password && ssid->passphrase &&
1588 	      sae_pk_valid_password(ssid->passphrase))) &&
1589 	    !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_PK)) &&
1590 	    sae_pk_acceptable_bss_with_pk(wpa_s, bss, ssid, match_ssid,
1591 					  match_ssid_len)) {
1592 		if (debug_print)
1593 			wpa_dbg(wpa_s, MSG_DEBUG,
1594 				"   skip - another acceptable BSS with SAE-PK in the same ESS");
1595 		return false;
1596 	}
1597 #endif /* CONFIG_SAE_PK */
1598 
1599 	if (bss->ssid_len == 0) {
1600 #ifdef CONFIG_OWE
1601 		const u8 *owe_ssid = NULL;
1602 		size_t owe_ssid_len = 0;
1603 
1604 		owe_trans_ssid(wpa_s, bss, &owe_ssid, &owe_ssid_len);
1605 		if (owe_ssid && owe_ssid_len &&
1606 		    owe_ssid_len == ssid->ssid_len &&
1607 		    os_memcmp(owe_ssid, ssid->ssid, owe_ssid_len) == 0) {
1608 			if (debug_print)
1609 				wpa_dbg(wpa_s, MSG_DEBUG,
1610 					"   skip - no SSID in BSS entry for a possible OWE transition mode BSS");
1611 			int_array_add_unique(&wpa_s->owe_trans_scan_freq,
1612 					     bss->freq);
1613 			return false;
1614 		}
1615 #endif /* CONFIG_OWE */
1616 		if (debug_print)
1617 			wpa_dbg(wpa_s, MSG_DEBUG,
1618 				"   skip - no SSID known for the BSS");
1619 		return false;
1620 	}
1621 
1622 	if (!wpas_valid_ml_bss(wpa_s, bss)) {
1623 		if (debug_print)
1624 			wpa_dbg(wpa_s, MSG_DEBUG,
1625 				"   skip - ML BSS going to be removed");
1626 		return false;
1627 	}
1628 
1629 	/* Matching configuration found */
1630 	return true;
1631 }
1632 
1633 
wpa_scan_res_match(struct wpa_supplicant * wpa_s,int i,struct wpa_bss * bss,struct wpa_ssid * group,int only_first_ssid,int debug_print)1634 struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
1635 				     int i, struct wpa_bss *bss,
1636 				     struct wpa_ssid *group,
1637 				     int only_first_ssid, int debug_print)
1638 {
1639 	u8 wpa_ie_len, rsn_ie_len;
1640 	const u8 *ie;
1641 	struct wpa_ssid *ssid;
1642 	int osen;
1643 	const u8 *match_ssid;
1644 	size_t match_ssid_len;
1645 	int bssid_ignore_count;
1646 
1647 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1648 	wpa_ie_len = ie ? ie[1] : 0;
1649 
1650 	ie = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
1651 	rsn_ie_len = ie ? ie[1] : 0;
1652 
1653 	ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
1654 	osen = ie != NULL;
1655 
1656 	if (debug_print) {
1657 		wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
1658 			" ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
1659 			i, MAC2STR(bss->bssid),
1660 			wpa_ssid_txt(bss->ssid, bss->ssid_len),
1661 			wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
1662 			bss->freq,
1663 			wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
1664 			" wps" : "",
1665 			(wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
1666 			 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
1667 			? " p2p" : "",
1668 			osen ? " osen=1" : "");
1669 	}
1670 
1671 	bssid_ignore_count = wpa_bssid_ignore_is_listed(wpa_s, bss->bssid);
1672 	if (bssid_ignore_count) {
1673 		int limit = 1;
1674 		if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
1675 			/*
1676 			 * When only a single network is enabled, we can
1677 			 * trigger BSSID ignoring on the first failure. This
1678 			 * should not be done with multiple enabled networks to
1679 			 * avoid getting forced to move into a worse ESS on
1680 			 * single error if there are no other BSSes of the
1681 			 * current ESS.
1682 			 */
1683 			limit = 0;
1684 		}
1685 		if (bssid_ignore_count > limit) {
1686 			if (debug_print) {
1687 				wpa_dbg(wpa_s, MSG_DEBUG,
1688 					"   skip - BSSID ignored (count=%d limit=%d)",
1689 					bssid_ignore_count, limit);
1690 			}
1691 			return NULL;
1692 		}
1693 	}
1694 
1695 	match_ssid = bss->ssid;
1696 	match_ssid_len = bss->ssid_len;
1697 	owe_trans_ssid(wpa_s, bss, &match_ssid, &match_ssid_len);
1698 
1699 	if (match_ssid_len == 0) {
1700 		if (debug_print)
1701 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
1702 		return NULL;
1703 	}
1704 
1705 	if (disallowed_bssid(wpa_s, bss->bssid)) {
1706 		if (debug_print)
1707 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
1708 		return NULL;
1709 	}
1710 
1711 	if (disallowed_ssid(wpa_s, match_ssid, match_ssid_len)) {
1712 		if (debug_print)
1713 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
1714 		return NULL;
1715 	}
1716 
1717 	if (disabled_freq(wpa_s, bss->freq)) {
1718 		if (debug_print)
1719 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - channel disabled");
1720 		return NULL;
1721 	}
1722 
1723 	if (wnm_is_bss_excluded(wpa_s, bss)) {
1724 		if (debug_print)
1725 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID excluded");
1726 		return NULL;
1727 	}
1728 
1729 	for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
1730 		if (wpa_scan_res_ok(wpa_s, ssid, match_ssid, match_ssid_len,
1731 				    bss, bssid_ignore_count, debug_print))
1732 			return ssid;
1733 	}
1734 
1735 	/* No matching configuration found */
1736 	return NULL;
1737 }
1738 
1739 
1740 static struct wpa_bss *
wpa_supplicant_select_bss(struct wpa_supplicant * wpa_s,struct wpa_ssid * group,struct wpa_ssid ** selected_ssid,int only_first_ssid)1741 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
1742 			  struct wpa_ssid *group,
1743 			  struct wpa_ssid **selected_ssid,
1744 			  int only_first_ssid)
1745 {
1746 	unsigned int i;
1747 
1748 	if (wpa_s->current_ssid) {
1749 		struct wpa_ssid *ssid;
1750 
1751 		wpa_dbg(wpa_s, MSG_DEBUG,
1752 			"Scan results matching the currently selected network");
1753 		for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1754 			struct wpa_bss *bss = wpa_s->last_scan_res[i];
1755 
1756 			ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1757 						  only_first_ssid, 0);
1758 			if (ssid != wpa_s->current_ssid)
1759 				continue;
1760 			wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
1761 				" freq=%d level=%d snr=%d est_throughput=%u",
1762 				i, MAC2STR(bss->bssid), bss->freq, bss->level,
1763 				bss->snr, bss->est_throughput);
1764 		}
1765 	}
1766 
1767 	if (only_first_ssid)
1768 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1769 			group->id);
1770 	else
1771 		wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1772 			group->priority);
1773 
1774 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1775 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
1776 
1777 		wpa_s->owe_transition_select = 1;
1778 		*selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1779 						    only_first_ssid, 1);
1780 		wpa_s->owe_transition_select = 0;
1781 		if (!*selected_ssid)
1782 			continue;
1783 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected %sBSS " MACSTR
1784 			" ssid='%s'",
1785 			bss == wpa_s->current_bss ? "current ": "",
1786 			MAC2STR(bss->bssid),
1787 			wpa_ssid_txt(bss->ssid, bss->ssid_len));
1788 		return bss;
1789 	}
1790 
1791 	return NULL;
1792 }
1793 
1794 
wpa_supplicant_pick_network(struct wpa_supplicant * wpa_s,struct wpa_ssid ** selected_ssid)1795 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1796 					     struct wpa_ssid **selected_ssid)
1797 {
1798 	struct wpa_bss *selected = NULL;
1799 	size_t prio;
1800 	struct wpa_ssid *next_ssid = NULL;
1801 	struct wpa_ssid *ssid;
1802 
1803 	if (wpa_s->last_scan_res == NULL ||
1804 	    wpa_s->last_scan_res_used == 0)
1805 		return NULL; /* no scan results from last update */
1806 
1807 	if (wpa_s->next_ssid) {
1808 		/* check that next_ssid is still valid */
1809 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1810 			if (ssid == wpa_s->next_ssid)
1811 				break;
1812 		}
1813 		next_ssid = ssid;
1814 		wpa_s->next_ssid = NULL;
1815 	}
1816 
1817 	while (selected == NULL) {
1818 		for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1819 			if (next_ssid && next_ssid->priority ==
1820 			    wpa_s->conf->pssid[prio]->priority) {
1821 				selected = wpa_supplicant_select_bss(
1822 					wpa_s, next_ssid, selected_ssid, 1);
1823 				if (selected)
1824 					break;
1825 			}
1826 			selected = wpa_supplicant_select_bss(
1827 				wpa_s, wpa_s->conf->pssid[prio],
1828 				selected_ssid, 0);
1829 			if (selected)
1830 				break;
1831 		}
1832 
1833 		if (!selected &&
1834 		    (wpa_s->bssid_ignore || wnm_active_bss_trans_mgmt(wpa_s)) &&
1835 		    !wpa_s->countermeasures) {
1836 			wpa_dbg(wpa_s, MSG_DEBUG,
1837 				"No APs found - clear BSSID ignore list and try again");
1838 			wnm_btm_reset(wpa_s);
1839 			wpa_bssid_ignore_clear(wpa_s);
1840 			wpa_s->bssid_ignore_cleared = true;
1841 		} else if (selected == NULL)
1842 			break;
1843 	}
1844 
1845 	ssid = *selected_ssid;
1846 	if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1847 	    !ssid->passphrase && !ssid->ext_psk) {
1848 		const char *field_name, *txt = NULL;
1849 
1850 		wpa_dbg(wpa_s, MSG_DEBUG,
1851 			"PSK/passphrase not yet available for the selected network");
1852 
1853 		wpas_notify_network_request(wpa_s, ssid,
1854 					    WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1855 
1856 		field_name = wpa_supplicant_ctrl_req_to_string(
1857 			WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1858 		if (field_name == NULL)
1859 			return NULL;
1860 
1861 		wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1862 
1863 		selected = NULL;
1864 	}
1865 
1866 	return selected;
1867 }
1868 
1869 
wpa_supplicant_req_new_scan(struct wpa_supplicant * wpa_s,int timeout_sec,int timeout_usec)1870 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1871 					int timeout_sec, int timeout_usec)
1872 {
1873 	if (!wpa_supplicant_enabled_networks(wpa_s)) {
1874 		/*
1875 		 * No networks are enabled; short-circuit request so
1876 		 * we don't wait timeout seconds before transitioning
1877 		 * to INACTIVE state.
1878 		 */
1879 		wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1880 			"since there are no enabled networks");
1881 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1882 		return;
1883 	}
1884 
1885 	wpa_s->scan_for_connection = 1;
1886 	wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1887 }
1888 
1889 
ml_link_probe_scan(struct wpa_supplicant * wpa_s)1890 static bool ml_link_probe_scan(struct wpa_supplicant *wpa_s)
1891 {
1892 	if (!wpa_s->ml_connect_probe_ssid || !wpa_s->ml_connect_probe_bss)
1893 		return false;
1894 
1895 	wpa_msg(wpa_s, MSG_DEBUG,
1896 		"Request association with " MACSTR " after ML probe",
1897 		MAC2STR(wpa_s->ml_connect_probe_bss->bssid));
1898 
1899 	wpa_supplicant_associate(wpa_s, wpa_s->ml_connect_probe_bss,
1900 				 wpa_s->ml_connect_probe_ssid);
1901 
1902 	wpa_s->ml_connect_probe_ssid = NULL;
1903 	wpa_s->ml_connect_probe_bss = NULL;
1904 
1905 	return true;
1906 }
1907 
1908 
wpa_supplicant_connect_ml_missing(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_ssid * ssid)1909 static int wpa_supplicant_connect_ml_missing(struct wpa_supplicant *wpa_s,
1910 					     struct wpa_bss *selected,
1911 					     struct wpa_ssid *ssid)
1912 {
1913 	int *freqs;
1914 	u16 missing_links = 0, removed_links;
1915 	u8 ap_mld_id;
1916 
1917 	if (!((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_MLO) &&
1918 	      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)))
1919 		return 0;
1920 
1921 	if (wpa_bss_parse_basic_ml_element(wpa_s, selected, NULL,
1922 					   &missing_links, ssid,
1923 					   &ap_mld_id) ||
1924 	    !missing_links)
1925 		return 0;
1926 
1927 	removed_links = wpa_bss_parse_reconf_ml_element(wpa_s, selected);
1928 	missing_links &= ~removed_links;
1929 
1930 	if (!missing_links)
1931 		return 0;
1932 
1933 	wpa_dbg(wpa_s, MSG_DEBUG,
1934 		"MLD: Doing an ML probe for missing links 0x%04x",
1935 		missing_links);
1936 
1937 	freqs = os_malloc(sizeof(int) * 2);
1938 	if (!freqs)
1939 		return 0;
1940 
1941 	wpa_s->ml_connect_probe_ssid = ssid;
1942 	wpa_s->ml_connect_probe_bss = selected;
1943 
1944 	freqs[0] = selected->freq;
1945 	freqs[1] = 0;
1946 
1947 	wpa_s->manual_scan_passive = 0;
1948 	wpa_s->manual_scan_use_id = 0;
1949 	wpa_s->manual_scan_only_new = 0;
1950 	wpa_s->scan_id_count = 0;
1951 	os_free(wpa_s->manual_scan_freqs);
1952 	wpa_s->manual_scan_freqs = freqs;
1953 
1954 	os_memcpy(wpa_s->ml_probe_bssid, selected->bssid, ETH_ALEN);
1955 
1956 	/*
1957 	 * In case the ML probe request is intended to retrieve information from
1958 	 * the transmitted BSS, the AP MLD ID should be included and should be
1959 	 * set to zero.
1960 	 * In case the ML probe requested is intended to retrieve information
1961 	 * from a non-transmitted BSS, the AP MLD ID should not be included.
1962 	 */
1963 	if (ap_mld_id)
1964 		wpa_s->ml_probe_mld_id = -1;
1965 	else
1966 		wpa_s->ml_probe_mld_id = 0;
1967 
1968 	if (ssid && ssid->ssid_len) {
1969 		os_free(wpa_s->ssids_from_scan_req);
1970 		wpa_s->num_ssids_from_scan_req = 0;
1971 
1972 		wpa_s->ssids_from_scan_req =
1973 			os_zalloc(sizeof(struct wpa_ssid_value));
1974 		if (wpa_s->ssids_from_scan_req) {
1975 			wpa_printf(MSG_DEBUG,
1976 				   "MLD: ML probe: With direct SSID");
1977 
1978 			wpa_s->num_ssids_from_scan_req = 1;
1979 			wpa_s->ssids_from_scan_req[0].ssid_len = ssid->ssid_len;
1980 			os_memcpy(wpa_s->ssids_from_scan_req[0].ssid,
1981 				  ssid->ssid, ssid->ssid_len);
1982 		}
1983 	}
1984 
1985 	wpa_s->ml_probe_links = missing_links;
1986 
1987 	wpa_s->normal_scans = 0;
1988 	wpa_s->scan_req = MANUAL_SCAN_REQ;
1989 	wpa_s->after_wps = 0;
1990 	wpa_s->known_wps_freq = 0;
1991 	wpa_supplicant_req_scan(wpa_s, 0, 0);
1992 
1993 	return 1;
1994 }
1995 
1996 
wpa_supplicant_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_ssid * ssid)1997 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
1998 			   struct wpa_bss *selected,
1999 			   struct wpa_ssid *ssid)
2000 {
2001 #ifdef IEEE8021X_EAPOL
2002 	if ((eap_is_wps_pbc_enrollee(&ssid->eap) &&
2003 	     wpas_wps_partner_link_overlap_detect(wpa_s)) ||
2004 	    wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
2005 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
2006 			"PBC session overlap");
2007 		wpas_notify_wps_event_pbc_overlap(wpa_s);
2008 		wpa_s->wps_overlap = true;
2009 #ifdef CONFIG_P2P
2010 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
2011 		    wpa_s->p2p_in_provisioning) {
2012 			eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
2013 					       wpa_s, NULL);
2014 			return -1;
2015 		}
2016 #endif /* CONFIG_P2P */
2017 
2018 #ifdef CONFIG_WPS
2019 		wpas_wps_pbc_overlap(wpa_s);
2020 		wpas_wps_cancel(wpa_s);
2021 #endif /* CONFIG_WPS */
2022 		return -1;
2023 	}
2024 #endif /* IEEE8021X_EAPOL */
2025 
2026 	wpa_msg(wpa_s, MSG_DEBUG,
2027 		"Considering connect request: reassociate: %d  selected: "
2028 		MACSTR "  bssid: " MACSTR "  pending: " MACSTR
2029 		"  wpa_state: %s  ssid=%p  current_ssid=%p",
2030 		wpa_s->reassociate, MAC2STR(selected->bssid),
2031 		MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
2032 		wpa_supplicant_state_txt(wpa_s->wpa_state),
2033 		ssid, wpa_s->current_ssid);
2034 
2035 	/*
2036 	 * Do not trigger new association unless the BSSID has changed or if
2037 	 * reassociation is requested. If we are in process of associating with
2038 	 * the selected BSSID, do not trigger new attempt.
2039 	 */
2040 	if (wpa_s->reassociate ||
2041 	    (!ether_addr_equal(selected->bssid, wpa_s->bssid) &&
2042 	     ((wpa_s->wpa_state != WPA_ASSOCIATING &&
2043 	       wpa_s->wpa_state != WPA_AUTHENTICATING) ||
2044 	      (!is_zero_ether_addr(wpa_s->pending_bssid) &&
2045 	       !ether_addr_equal(selected->bssid, wpa_s->pending_bssid)) ||
2046 	      (is_zero_ether_addr(wpa_s->pending_bssid) &&
2047 	       ssid != wpa_s->current_ssid)))) {
2048 		if (wpa_supplicant_scard_init(wpa_s, ssid)) {
2049 			wpa_supplicant_req_new_scan(wpa_s, 10, 0);
2050 			return 0;
2051 		}
2052 
2053 		if (wpa_supplicant_connect_ml_missing(wpa_s, selected, ssid))
2054 			return 0;
2055 
2056 		wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
2057 			MAC2STR(selected->bssid));
2058 		wpa_supplicant_associate(wpa_s, selected, ssid);
2059 	} else {
2060 		wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
2061 			"connect with the selected AP");
2062 	}
2063 
2064 	return 0;
2065 }
2066 
2067 
2068 static struct wpa_ssid *
wpa_supplicant_pick_new_network(struct wpa_supplicant * wpa_s)2069 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
2070 {
2071 	size_t prio;
2072 	struct wpa_ssid *ssid;
2073 
2074 	for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
2075 		for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
2076 		{
2077 			if (wpas_network_disabled(wpa_s, ssid))
2078 				continue;
2079 #ifndef CONFIG_IBSS_RSN
2080 			if (ssid->mode == WPAS_MODE_IBSS &&
2081 			    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
2082 						WPA_KEY_MGMT_WPA_NONE))) {
2083 				wpa_msg(wpa_s, MSG_INFO,
2084 					"IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
2085 					wpa_ssid_txt(ssid->ssid,
2086 						     ssid->ssid_len));
2087 				continue;
2088 			}
2089 #endif /* !CONFIG_IBSS_RSN */
2090 			if (ssid->mode == WPAS_MODE_IBSS ||
2091 			    ssid->mode == WPAS_MODE_AP ||
2092 			    ssid->mode == WPAS_MODE_MESH)
2093 				return ssid;
2094 		}
2095 	}
2096 	return NULL;
2097 }
2098 
2099 
2100 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
2101  * on BSS added and BSS changed events */
wpa_supplicant_rsn_preauth_scan_results(struct wpa_supplicant * wpa_s)2102 static void wpa_supplicant_rsn_preauth_scan_results(
2103 	struct wpa_supplicant *wpa_s)
2104 {
2105 	struct wpa_bss *bss;
2106 
2107 	if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
2108 		return;
2109 
2110 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2111 		const u8 *ssid, *rsn;
2112 
2113 		ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
2114 		if (ssid == NULL)
2115 			continue;
2116 
2117 		rsn = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
2118 		if (rsn == NULL)
2119 			continue;
2120 
2121 		rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
2122 	}
2123 
2124 }
2125 
2126 
2127 #ifndef CONFIG_NO_ROAMING
2128 
wpas_get_snr_signal_info(u32 frequency,int avg_signal,int noise)2129 static int wpas_get_snr_signal_info(u32 frequency, int avg_signal, int noise)
2130 {
2131 	if (noise == WPA_INVALID_NOISE) {
2132 		if (IS_5GHZ(frequency)) {
2133 			noise = DEFAULT_NOISE_FLOOR_5GHZ;
2134 		} else if (is_6ghz_freq(frequency)) {
2135 			noise = DEFAULT_NOISE_FLOOR_6GHZ;
2136 		} else {
2137 			noise = DEFAULT_NOISE_FLOOR_2GHZ;
2138 		}
2139 	}
2140 	return avg_signal - noise;
2141 }
2142 
2143 
2144 static unsigned int
wpas_get_est_throughput_from_bss_snr(const struct wpa_supplicant * wpa_s,const struct wpa_bss * bss,int snr)2145 wpas_get_est_throughput_from_bss_snr(const struct wpa_supplicant *wpa_s,
2146 				     const struct wpa_bss *bss, int snr)
2147 {
2148 	int rate = wpa_bss_get_max_rate(bss);
2149 	const u8 *ies = wpa_bss_ie_ptr(bss);
2150 	size_t ie_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
2151 	enum chan_width max_cw = CHAN_WIDTH_UNKNOWN;
2152 
2153 	return wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, bss->freq,
2154 				&max_cw);
2155 }
2156 
2157 
wpas_evaluate_band_score(int frequency)2158 static int wpas_evaluate_band_score(int frequency)
2159 {
2160 	if (is_6ghz_freq(frequency))
2161 		return 2;
2162 	if (IS_5GHZ(frequency))
2163 		return 1;
2164 	return 0;
2165 }
2166 
2167 
wpa_supplicant_need_to_roam_within_ess(struct wpa_supplicant * wpa_s,struct wpa_bss * current_bss,struct wpa_bss * selected)2168 int wpa_supplicant_need_to_roam_within_ess(struct wpa_supplicant *wpa_s,
2169 					   struct wpa_bss *current_bss,
2170 					   struct wpa_bss *selected)
2171 {
2172 	int min_diff, diff;
2173 	int cur_band_score, sel_band_score;
2174 	int to_5ghz, to_6ghz;
2175 	int cur_level, sel_level;
2176 	unsigned int cur_est, sel_est;
2177 	struct wpa_signal_info si;
2178 	int cur_snr = 0;
2179 	int ret = 0;
2180 	const u8 *cur_ies = wpa_bss_ie_ptr(current_bss);
2181 	const u8 *sel_ies = wpa_bss_ie_ptr(selected);
2182 	size_t cur_ie_len = current_bss->ie_len ? current_bss->ie_len :
2183 		current_bss->beacon_ie_len;
2184 	size_t sel_ie_len = selected->ie_len ? selected->ie_len :
2185 		selected->beacon_ie_len;
2186 
2187 	wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
2188 	wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
2189 		" freq=%d level=%d snr=%d est_throughput=%u",
2190 		MAC2STR(current_bss->bssid),
2191 		current_bss->freq, current_bss->level,
2192 		current_bss->snr, current_bss->est_throughput);
2193 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
2194 		" freq=%d level=%d snr=%d est_throughput=%u",
2195 		MAC2STR(selected->bssid), selected->freq, selected->level,
2196 		selected->snr, selected->est_throughput);
2197 
2198 	if (wpas_ap_link_address(wpa_s, selected->bssid)) {
2199 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: associated to selected BSS");
2200 		return 0;
2201 	}
2202 
2203 	if (wpa_s->current_ssid->bssid_set &&
2204 	    ether_addr_equal(selected->bssid, wpa_s->current_ssid->bssid)) {
2205 		wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
2206 			"has preferred BSSID");
2207 		return 1;
2208 	}
2209 
2210 	/*
2211 	 * Try to poll the signal from the driver since this will allow to get
2212 	 * more accurate values. In some cases, there can be big differences
2213 	 * between the RSSI of the Probe Response frames of the AP we are
2214 	 * associated with and the Beacon frames we hear from the same AP after
2215 	 * association. This can happen, e.g., when there are two antennas that
2216 	 * hear the AP very differently. If the driver chooses to hear the
2217 	 * Probe Response frames during the scan on the "bad" antenna because
2218 	 * it wants to save power, but knows to choose the other antenna after
2219 	 * association, we will hear our AP with a low RSSI as part of the
2220 	 * scan even when we can hear it decently on the other antenna. To cope
2221 	 * with this, ask the driver to teach us how it hears the AP. Also, the
2222 	 * scan results may be a bit old, since we can very quickly get fresh
2223 	 * information about our currently associated AP.
2224 	 */
2225 	if (wpa_drv_signal_poll(wpa_s, &si) == 0 &&
2226 	    (si.data.avg_beacon_signal || si.data.avg_signal)) {
2227 		/*
2228 		 * Normalize avg_signal to the RSSI over 20 MHz, as the
2229 		 * throughput is estimated based on the RSSI over 20 MHz
2230 		 */
2231 		cur_level = si.data.avg_beacon_signal ?
2232 			si.data.avg_beacon_signal :
2233 			(si.data.avg_signal -
2234 			 wpas_channel_width_rssi_bump(cur_ies, cur_ie_len,
2235 						      si.chanwidth));
2236 		cur_snr = wpas_get_snr_signal_info(si.frequency, cur_level,
2237 						   si.current_noise);
2238 
2239 		cur_est = wpas_get_est_throughput_from_bss_snr(wpa_s,
2240 							       current_bss,
2241 							       cur_snr);
2242 		wpa_dbg(wpa_s, MSG_DEBUG,
2243 			"Using signal poll values for the current BSS: level=%d snr=%d est_throughput=%u",
2244 			cur_level, cur_snr, cur_est);
2245 	} else {
2246 		/* Level and SNR are measured over 20 MHz channel */
2247 		cur_level = current_bss->level;
2248 		cur_snr = current_bss->snr;
2249 		cur_est = current_bss->est_throughput;
2250 	}
2251 
2252 	/* Adjust the SNR of BSSes based on the channel width. */
2253 	cur_level += wpas_channel_width_rssi_bump(cur_ies, cur_ie_len,
2254 						  current_bss->max_cw);
2255 	cur_snr = wpas_adjust_snr_by_chanwidth(cur_ies, cur_ie_len,
2256 					       current_bss->max_cw, cur_snr);
2257 
2258 	sel_est = selected->est_throughput;
2259 	sel_level = selected->level +
2260 		wpas_channel_width_rssi_bump(sel_ies, sel_ie_len,
2261 					     selected->max_cw);
2262 
2263 	if (sel_est > cur_est + 5000) {
2264 		wpa_dbg(wpa_s, MSG_DEBUG,
2265 			"Allow reassociation - selected BSS has better estimated throughput");
2266 		return 1;
2267 	}
2268 
2269 	to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
2270 	to_6ghz = is_6ghz_freq(selected->freq) &&
2271 		!is_6ghz_freq(current_bss->freq);
2272 
2273 	if (cur_level < 0 &&
2274 	    cur_level > sel_level + to_5ghz * 2 + to_6ghz * 2 &&
2275 	    sel_est < cur_est * 1.2) {
2276 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
2277 			"signal level");
2278 		return 0;
2279 	}
2280 
2281 	if (cur_est > sel_est + 5000) {
2282 		wpa_dbg(wpa_s, MSG_DEBUG,
2283 			"Skip roam - Current BSS has better estimated throughput");
2284 		return 0;
2285 	}
2286 
2287 	if (cur_snr > GREAT_SNR) {
2288 		wpa_dbg(wpa_s, MSG_DEBUG,
2289 			"Skip roam - Current BSS has good SNR (%u > %u)",
2290 			cur_snr, GREAT_SNR);
2291 		return 0;
2292 	}
2293 
2294 	if (cur_level < -85) /* ..-86 dBm */
2295 		min_diff = 1;
2296 	else if (cur_level < -80) /* -85..-81 dBm */
2297 		min_diff = 2;
2298 	else if (cur_level < -75) /* -80..-76 dBm */
2299 		min_diff = 3;
2300 	else if (cur_level < -70) /* -75..-71 dBm */
2301 		min_diff = 4;
2302 	else if (cur_level < 0) /* -70..-1 dBm */
2303 		min_diff = 5;
2304 	else /* unspecified units (not in dBm) */
2305 		min_diff = 2;
2306 
2307 	if (cur_est > sel_est * 1.5)
2308 		min_diff += 10;
2309 	else if (cur_est > sel_est * 1.2)
2310 		min_diff += 5;
2311 	else if (cur_est > sel_est * 1.1)
2312 		min_diff += 2;
2313 	else if (cur_est > sel_est)
2314 		min_diff++;
2315 	else if (sel_est > cur_est * 1.5)
2316 		min_diff -= 10;
2317 	else if (sel_est > cur_est * 1.2)
2318 		min_diff -= 5;
2319 	else if (sel_est > cur_est * 1.1)
2320 		min_diff -= 2;
2321 	else if (sel_est > cur_est)
2322 		min_diff--;
2323 
2324 	cur_band_score = wpas_evaluate_band_score(current_bss->freq);
2325 	sel_band_score = wpas_evaluate_band_score(selected->freq);
2326 	min_diff += (cur_band_score - sel_band_score) * 2;
2327 	if (wpa_s->signal_threshold && cur_level <= wpa_s->signal_threshold &&
2328 	    sel_level > wpa_s->signal_threshold)
2329 		min_diff -= 2;
2330 	diff = sel_level - cur_level;
2331 	if (diff < min_diff) {
2332 		wpa_dbg(wpa_s, MSG_DEBUG,
2333 			"Skip roam - too small difference in signal level (%d < %d)",
2334 			diff, min_diff);
2335 		ret = 0;
2336 	} else {
2337 		wpa_dbg(wpa_s, MSG_DEBUG,
2338 			"Allow reassociation due to difference in signal level (%d >= %d)",
2339 			diff, min_diff);
2340 		ret = 1;
2341 	}
2342 	wpa_msg_ctrl(wpa_s, MSG_INFO, "%scur_bssid=" MACSTR
2343 		     " cur_freq=%d cur_level=%d cur_est=%d sel_bssid=" MACSTR
2344 		     " sel_freq=%d sel_level=%d sel_est=%d",
2345 		     ret ? WPA_EVENT_DO_ROAM : WPA_EVENT_SKIP_ROAM,
2346 		     MAC2STR(current_bss->bssid),
2347 		     current_bss->freq, cur_level, cur_est,
2348 		     MAC2STR(selected->bssid),
2349 		     selected->freq, sel_level, sel_est);
2350 	return ret;
2351 }
2352 
2353 #endif /* CONFIG_NO_ROAMING */
2354 
2355 
wpa_supplicant_need_to_roam(struct wpa_supplicant * wpa_s,struct wpa_bss * selected,struct wpa_ssid * ssid)2356 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
2357 				       struct wpa_bss *selected,
2358 				       struct wpa_ssid *ssid)
2359 {
2360 	struct wpa_bss *current_bss = NULL;
2361 	const u8 *bssid;
2362 
2363 	if (wpa_s->reassociate)
2364 		return 1; /* explicit request to reassociate */
2365 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
2366 		return 1; /* we are not associated; continue */
2367 	if (wpa_s->current_ssid == NULL)
2368 		return 1; /* unknown current SSID */
2369 	if (wpa_s->current_ssid != ssid)
2370 		return 1; /* different network block */
2371 
2372 	if (wpas_driver_bss_selection(wpa_s))
2373 		return 0; /* Driver-based roaming */
2374 
2375 	if (wpa_s->valid_links)
2376 		bssid = wpa_s->links[wpa_s->mlo_assoc_link_id].bssid;
2377 	else
2378 		bssid = wpa_s->bssid;
2379 
2380 	if (wpa_s->current_ssid->ssid)
2381 		current_bss = wpa_bss_get(wpa_s, bssid,
2382 					  wpa_s->current_ssid->ssid,
2383 					  wpa_s->current_ssid->ssid_len);
2384 	if (!current_bss)
2385 		current_bss = wpa_bss_get_bssid(wpa_s, bssid);
2386 
2387 	if (!current_bss)
2388 		return 1; /* current BSS not seen in scan results */
2389 
2390 	if (current_bss == selected)
2391 		return 0;
2392 
2393 	if (selected->last_update_idx > current_bss->last_update_idx)
2394 		return 1; /* current BSS not seen in the last scan */
2395 
2396 #ifndef CONFIG_NO_ROAMING
2397 	return wpa_supplicant_need_to_roam_within_ess(wpa_s, current_bss,
2398 						      selected);
2399 #else /* CONFIG_NO_ROAMING */
2400 	return 0;
2401 #endif /* CONFIG_NO_ROAMING */
2402 }
2403 
2404 
wpas_trigger_6ghz_scan(struct wpa_supplicant * wpa_s,union wpa_event_data * data)2405 static int wpas_trigger_6ghz_scan(struct wpa_supplicant *wpa_s,
2406 				  union wpa_event_data *data)
2407 {
2408 	struct wpa_driver_scan_params params;
2409 	unsigned int j;
2410 
2411 	wpa_dbg(wpa_s, MSG_INFO, "Triggering 6GHz-only scan");
2412 	os_memset(&params, 0, sizeof(params));
2413 	params.non_coloc_6ghz = wpa_s->last_scan_non_coloc_6ghz;
2414 	for (j = 0; j < data->scan_info.num_ssids; j++)
2415 		params.ssids[j] = data->scan_info.ssids[j];
2416 	params.num_ssids = data->scan_info.num_ssids;
2417 	wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
2418 				true, false, false);
2419 	if (!wpa_supplicant_trigger_scan(wpa_s, &params, true, true)) {
2420 		wpa_s->scan_in_progress_6ghz = true;
2421 		wpas_notify_scan_in_progress_6ghz(wpa_s);
2422 		os_free(params.freqs);
2423 		return 1;
2424 	}
2425 	wpa_dbg(wpa_s, MSG_INFO, "Failed to trigger 6GHz-only scan");
2426 	os_free(params.freqs);
2427 	return 0;
2428 }
2429 
2430 
wpas_short_ssid_match(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)2431 static bool wpas_short_ssid_match(struct wpa_supplicant *wpa_s,
2432 				  struct wpa_scan_results *scan_res)
2433 {
2434 	size_t i;
2435 	struct wpa_ssid *ssid = wpa_s->current_ssid;
2436 	u32 current_ssid_short = ieee80211_crc32(ssid->ssid, ssid->ssid_len);
2437 
2438 	for (i = 0; i < scan_res->num; i++) {
2439 		struct wpa_scan_res *res = scan_res->res[i];
2440 		const u8 *rnr_ie, *ie_end;
2441 		const struct ieee80211_neighbor_ap_info *info;
2442 		size_t left;
2443 
2444 		rnr_ie = wpa_scan_get_ie(res, WLAN_EID_REDUCED_NEIGHBOR_REPORT);
2445 		if (!rnr_ie)
2446 			continue;
2447 
2448 		ie_end = rnr_ie + 2 + rnr_ie[1];
2449 		rnr_ie += 2;
2450 
2451 		left = ie_end - rnr_ie;
2452 		if (left < sizeof(struct ieee80211_neighbor_ap_info))
2453 			continue;
2454 
2455 		info = (const struct ieee80211_neighbor_ap_info *) rnr_ie;
2456 		if (info->tbtt_info_len < 11)
2457 			continue; /* short SSID not included */
2458 		left -= sizeof(struct ieee80211_neighbor_ap_info);
2459 		rnr_ie += sizeof(struct ieee80211_neighbor_ap_info);
2460 
2461 		while (left >= info->tbtt_info_len && rnr_ie + 11 <= ie_end) {
2462 			/* Skip TBTT offset and BSSID */
2463 			u32 short_ssid = WPA_GET_LE32(rnr_ie + 1 + ETH_ALEN);
2464 
2465 			if (short_ssid == current_ssid_short)
2466 				return true;
2467 
2468 			left -= info->tbtt_info_len;
2469 			rnr_ie += info->tbtt_info_len;
2470 		}
2471 	}
2472 
2473 	return false;
2474 }
2475 
2476 
2477 /*
2478  * Return a negative value if no scan results could be fetched or if scan
2479  * results should not be shared with other virtual interfaces.
2480  * Return 0 if scan results were fetched and may be shared with other
2481  * interfaces.
2482  * Return 1 if scan results may be shared with other virtual interfaces but may
2483  * not trigger any operations.
2484  * Return 2 if the interface was removed and cannot be used.
2485  */
_wpa_supplicant_event_scan_results(struct wpa_supplicant * wpa_s,union wpa_event_data * data,int own_request,int update_only)2486 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
2487 					      union wpa_event_data *data,
2488 					      int own_request, int update_only)
2489 {
2490 	struct wpa_scan_results *scan_res = NULL;
2491 	int ret = 0;
2492 	int ap = 0;
2493 	bool trigger_6ghz_scan;
2494 	bool short_ssid_match_found = false;
2495 #ifndef CONFIG_NO_RANDOM_POOL
2496 	size_t i, num;
2497 #endif /* CONFIG_NO_RANDOM_POOL */
2498 
2499 #ifdef CONFIG_AP
2500 	if (wpa_s->ap_iface)
2501 		ap = 1;
2502 #endif /* CONFIG_AP */
2503 
2504 	trigger_6ghz_scan = wpa_s->crossed_6ghz_dom &&
2505 		wpa_s->last_scan_all_chan;
2506 	wpa_s->crossed_6ghz_dom = false;
2507 	wpa_s->last_scan_all_chan = false;
2508 
2509 	wpa_supplicant_notify_scanning(wpa_s, 0);
2510 
2511 	scan_res = wpa_supplicant_get_scan_results(wpa_s,
2512 						   data ? &data->scan_info :
2513 						   NULL, 1, NULL);
2514 
2515 	if (wpa_s->scan_in_progress_6ghz) {
2516 		wpa_s->scan_in_progress_6ghz = false;
2517 		wpas_notify_scan_in_progress_6ghz(wpa_s);
2518 	}
2519 
2520 	if (scan_res == NULL) {
2521 		if (wpa_s->conf->ap_scan == 2 || ap ||
2522 		    wpa_s->scan_res_handler == scan_only_handler)
2523 			return -1;
2524 		if (!own_request)
2525 			return -1;
2526 		if (data && data->scan_info.external_scan)
2527 			return -1;
2528 		if (wpa_s->scan_res_fail_handler) {
2529 			void (*handler)(struct wpa_supplicant *wpa_s);
2530 
2531 			handler = wpa_s->scan_res_fail_handler;
2532 			wpa_s->scan_res_fail_handler = NULL;
2533 			handler(wpa_s);
2534 		} else {
2535 			wpa_dbg(wpa_s, MSG_DEBUG,
2536 				"Failed to get scan results - try scanning again");
2537 			wpa_supplicant_req_new_scan(wpa_s, 1, 0);
2538 		}
2539 
2540 		ret = -1;
2541 		goto scan_work_done;
2542 	}
2543 
2544 #ifndef CONFIG_NO_RANDOM_POOL
2545 	num = scan_res->num;
2546 	if (num > 10)
2547 		num = 10;
2548 	for (i = 0; i < num; i++) {
2549 		u8 buf[5];
2550 		struct wpa_scan_res *res = scan_res->res[i];
2551 		buf[0] = res->bssid[5];
2552 		buf[1] = res->qual & 0xff;
2553 		buf[2] = res->noise & 0xff;
2554 		buf[3] = res->level & 0xff;
2555 		buf[4] = res->tsf & 0xff;
2556 		random_add_randomness(buf, sizeof(buf));
2557 	}
2558 #endif /* CONFIG_NO_RANDOM_POOL */
2559 
2560 	wpa_s->last_scan_external = data && data->scan_info.external_scan;
2561 
2562 	if (update_only) {
2563 		ret = 1;
2564 		goto scan_work_done;
2565 	}
2566 
2567 	if (own_request && wpa_s->scan_res_handler &&
2568 	    !(data && data->scan_info.external_scan)) {
2569 		void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
2570 					 struct wpa_scan_results *scan_res);
2571 
2572 		scan_res_handler = wpa_s->scan_res_handler;
2573 		wpa_s->scan_res_handler = NULL;
2574 		scan_res_handler(wpa_s, scan_res);
2575 		ret = 1;
2576 		goto scan_work_done;
2577 	}
2578 
2579 	wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
2580 		wpa_s->own_scan_running,
2581 		data ? data->scan_info.external_scan : 0);
2582 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2583 	    wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
2584 	    own_request && !(data && data->scan_info.external_scan)) {
2585 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
2586 			     wpa_s->manual_scan_id);
2587 		wpa_s->manual_scan_use_id = 0;
2588 	} else {
2589 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
2590 	}
2591 	wpas_notify_scan_results(wpa_s);
2592 
2593 	wpas_notify_scan_done(wpa_s, 1);
2594 
2595 	if (ap) {
2596 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
2597 #ifdef CONFIG_AP
2598 		if (wpa_s->ap_iface->scan_cb)
2599 			wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
2600 #endif /* CONFIG_AP */
2601 		goto scan_work_done;
2602 	}
2603 
2604 	if (data && data->scan_info.external_scan) {
2605 		wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
2606 		wpa_scan_results_free(scan_res);
2607 		return 0;
2608 	}
2609 
2610 	if (sme_proc_obss_scan(wpa_s) > 0)
2611 		goto scan_work_done;
2612 
2613 #ifndef CONFIG_NO_RRM
2614 	if (own_request && data &&
2615 	    wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
2616 		goto scan_work_done;
2617 #endif /* CONFIG_NO_RRM */
2618 
2619 	if (ml_link_probe_scan(wpa_s))
2620 		goto scan_work_done;
2621 
2622 	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
2623 		goto scan_work_done;
2624 
2625 	if (autoscan_notify_scan(wpa_s, scan_res))
2626 		goto scan_work_done;
2627 
2628 	if (wpa_s->disconnected) {
2629 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2630 		goto scan_work_done;
2631 	}
2632 
2633 	if (!wpas_driver_bss_selection(wpa_s) &&
2634 	    bgscan_notify_scan(wpa_s, scan_res) == 1)
2635 		goto scan_work_done;
2636 
2637 	wpas_wps_update_ap_info(wpa_s, scan_res);
2638 
2639 	if (wnm_scan_process(wpa_s, false) > 0)
2640 		goto scan_work_done;
2641 
2642 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
2643 	    wpa_s->wpa_state < WPA_COMPLETED)
2644 		goto scan_work_done;
2645 
2646 	if (wpa_s->current_ssid && trigger_6ghz_scan && own_request && data &&
2647 	    wpas_short_ssid_match(wpa_s, scan_res)) {
2648 		wpa_dbg(wpa_s, MSG_INFO, "Short SSID match in scan results");
2649 		short_ssid_match_found = true;
2650 	}
2651 
2652 	wpa_scan_results_free(scan_res);
2653 
2654 	if (own_request && wpa_s->scan_work) {
2655 		struct wpa_radio_work *work = wpa_s->scan_work;
2656 		wpa_s->scan_work = NULL;
2657 		radio_work_done(work);
2658 	}
2659 
2660 	os_free(wpa_s->last_scan_freqs);
2661 	wpa_s->last_scan_freqs = NULL;
2662 	wpa_s->num_last_scan_freqs = 0;
2663 	if (own_request && data &&
2664 	    data->scan_info.freqs && data->scan_info.num_freqs) {
2665 		wpa_s->last_scan_freqs = os_malloc(sizeof(int) *
2666 						   data->scan_info.num_freqs);
2667 		if (wpa_s->last_scan_freqs) {
2668 			os_memcpy(wpa_s->last_scan_freqs,
2669 				  data->scan_info.freqs,
2670 				  sizeof(int) * data->scan_info.num_freqs);
2671 			wpa_s->num_last_scan_freqs = data->scan_info.num_freqs;
2672 		}
2673 	}
2674 
2675 	if (wpa_s->supp_pbc_active && !wpas_wps_partner_link_scan_done(wpa_s))
2676 		return ret;
2677 
2678 	if (short_ssid_match_found && wpas_trigger_6ghz_scan(wpa_s, data) > 0)
2679 		return 1;
2680 
2681 	return wpas_select_network_from_last_scan(wpa_s, 1, own_request,
2682 						  trigger_6ghz_scan, data);
2683 
2684 scan_work_done:
2685 	wpa_scan_results_free(scan_res);
2686 	if (own_request && wpa_s->scan_work) {
2687 		struct wpa_radio_work *work = wpa_s->scan_work;
2688 		wpa_s->scan_work = NULL;
2689 		radio_work_done(work);
2690 	}
2691 	return ret;
2692 }
2693 
2694 
2695 /**
2696  * Select a network from the last scan
2697  * @wpa_s: Pointer to wpa_supplicant data
2698  * @new_scan: Whether this function was called right after a scan has finished
2699  * @own_request: Whether the scan was requested by this interface
2700  * @trigger_6ghz_scan: Whether to trigger a 6ghz-only scan when applicable
2701  * @data: Scan data from scan that finished if applicable
2702  *
2703  * See _wpa_supplicant_event_scan_results() for return values.
2704  */
wpas_select_network_from_last_scan(struct wpa_supplicant * wpa_s,int new_scan,int own_request,bool trigger_6ghz_scan,union wpa_event_data * data)2705 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
2706 					      int new_scan, int own_request,
2707 					      bool trigger_6ghz_scan,
2708 					      union wpa_event_data *data)
2709 {
2710 	struct wpa_bss *selected;
2711 	struct wpa_ssid *ssid = NULL;
2712 	int time_to_reenable = wpas_reenabled_network_time(wpa_s);
2713 
2714 	if (time_to_reenable > 0) {
2715 		wpa_dbg(wpa_s, MSG_DEBUG,
2716 			"Postpone network selection by %d seconds since all networks are disabled",
2717 			time_to_reenable);
2718 		eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2719 		eloop_register_timeout(time_to_reenable, 0,
2720 				       wpas_network_reenabled, wpa_s, NULL);
2721 		return 0;
2722 	}
2723 
2724 	if (wpa_s->p2p_mgmt)
2725 		return 0; /* no normal connection on p2p_mgmt interface */
2726 
2727 	wpa_s->owe_transition_search = 0;
2728 #ifdef CONFIG_OWE
2729 	os_free(wpa_s->owe_trans_scan_freq);
2730 	wpa_s->owe_trans_scan_freq = NULL;
2731 #endif /* CONFIG_OWE */
2732 	selected = wpa_supplicant_pick_network(wpa_s, &ssid);
2733 
2734 #ifdef CONFIG_MESH
2735 	if (wpa_s->ifmsh) {
2736 		wpa_msg(wpa_s, MSG_INFO,
2737 			"Avoiding join because we already joined a mesh group");
2738 		return 0;
2739 	}
2740 #endif /* CONFIG_MESH */
2741 
2742 	if (selected) {
2743 		int skip;
2744 		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
2745 		if (skip) {
2746 			if (new_scan)
2747 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
2748 			return 0;
2749 		}
2750 
2751 		wpa_s->suitable_network++;
2752 
2753 		if (ssid != wpa_s->current_ssid &&
2754 		    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2755 			wpa_s->own_disconnect_req = 1;
2756 			wpa_supplicant_deauthenticate(
2757 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2758 		}
2759 
2760 		if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
2761 			wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
2762 			return -1;
2763 		}
2764 		wpa_s->supp_pbc_active = false;
2765 
2766 		if (new_scan)
2767 			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
2768 		/*
2769 		 * Do not allow other virtual radios to trigger operations based
2770 		 * on these scan results since we do not want them to start
2771 		 * other associations at the same time.
2772 		 */
2773 		return 1;
2774 	} else {
2775 		wpa_s->no_suitable_network++;
2776 		wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
2777 		ssid = wpa_supplicant_pick_new_network(wpa_s);
2778 		if (ssid) {
2779 			wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
2780 			wpa_supplicant_associate(wpa_s, NULL, ssid);
2781 			if (new_scan)
2782 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
2783 		} else if (own_request) {
2784 			if (wpa_s->support_6ghz && trigger_6ghz_scan && data &&
2785 			    wpas_trigger_6ghz_scan(wpa_s, data) > 0)
2786 				return 1;
2787 
2788 			/*
2789 			 * No SSID found. If SCAN results are as a result of
2790 			 * own scan request and not due to a scan request on
2791 			 * another shared interface, try another scan.
2792 			 */
2793 			int timeout_sec = wpa_s->scan_interval;
2794 			int timeout_usec = 0;
2795 #ifdef CONFIG_P2P
2796 			int res;
2797 
2798 			res = wpas_p2p_scan_no_go_seen(wpa_s);
2799 			if (res == 2)
2800 				return 2;
2801 			if (res == 1)
2802 				return 0;
2803 
2804 			if (wpas_p2p_retry_limit_exceeded(wpa_s))
2805 				return 0;
2806 
2807 			if (wpa_s->p2p_in_provisioning ||
2808 			    wpa_s->show_group_started ||
2809 			    wpa_s->p2p_in_invitation) {
2810 				/*
2811 				 * Use shorter wait during P2P Provisioning
2812 				 * state and during P2P join-a-group operation
2813 				 * to speed up group formation.
2814 				 */
2815 				timeout_sec = 0;
2816 				timeout_usec = 250000;
2817 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2818 							    timeout_usec);
2819 				return 0;
2820 			}
2821 #endif /* CONFIG_P2P */
2822 #ifdef CONFIG_INTERWORKING
2823 			if (wpa_s->conf->auto_interworking &&
2824 			    wpa_s->conf->interworking &&
2825 			    wpa_s->conf->cred) {
2826 				wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
2827 					"start ANQP fetch since no matching "
2828 					"networks found");
2829 				wpa_s->network_select = 1;
2830 				wpa_s->auto_network_select = 1;
2831 				interworking_start_fetch_anqp(wpa_s);
2832 				return 1;
2833 			}
2834 #endif /* CONFIG_INTERWORKING */
2835 #ifdef CONFIG_WPS
2836 			if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
2837 				wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
2838 				timeout_sec = 0;
2839 				timeout_usec = 500000;
2840 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2841 							    timeout_usec);
2842 				return 0;
2843 			}
2844 #endif /* CONFIG_WPS */
2845 #ifdef CONFIG_OWE
2846 			if (wpa_s->owe_transition_search) {
2847 				wpa_dbg(wpa_s, MSG_DEBUG,
2848 					"OWE: Use shorter wait during transition mode search");
2849 				timeout_sec = 0;
2850 				timeout_usec = 500000;
2851 				if (wpa_s->owe_trans_scan_freq) {
2852 					os_free(wpa_s->next_scan_freqs);
2853 					wpa_s->next_scan_freqs =
2854 						wpa_s->owe_trans_scan_freq;
2855 					wpa_s->owe_trans_scan_freq = NULL;
2856 					timeout_usec = 100000;
2857 				}
2858 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2859 							    timeout_usec);
2860 				return 0;
2861 			}
2862 #endif /* CONFIG_OWE */
2863 			if (wpa_supplicant_req_sched_scan(wpa_s))
2864 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2865 							    timeout_usec);
2866 
2867 			wpa_msg_ctrl(wpa_s, MSG_INFO,
2868 				     WPA_EVENT_NETWORK_NOT_FOUND);
2869 			wpas_notify_network_not_found(wpa_s);
2870 		}
2871 	}
2872 	return 0;
2873 }
2874 
2875 
wpa_supplicant_event_scan_results(struct wpa_supplicant * wpa_s,union wpa_event_data * data)2876 static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
2877 					     union wpa_event_data *data)
2878 {
2879 	struct wpa_supplicant *ifs;
2880 	int res;
2881 
2882 	res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
2883 	if (res == 2) {
2884 		/*
2885 		 * Interface may have been removed, so must not dereference
2886 		 * wpa_s after this.
2887 		 */
2888 		return 1;
2889 	}
2890 
2891 	if (res < 0) {
2892 		/*
2893 		 * If no scan results could be fetched, then no need to
2894 		 * notify those interfaces that did not actually request
2895 		 * this scan. Similarly, if scan results started a new operation on this
2896 		 * interface, do not notify other interfaces to avoid concurrent
2897 		 * operations during a connection attempt.
2898 		 */
2899 		return 0;
2900 	}
2901 
2902 	/*
2903 	 * Check other interfaces to see if they share the same radio. If
2904 	 * so, they get updated with this same scan info.
2905 	 */
2906 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2907 			 radio_list) {
2908 		if (ifs != wpa_s) {
2909 			wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
2910 				   "sibling", ifs->ifname);
2911 			res = _wpa_supplicant_event_scan_results(ifs, data, 0,
2912 								 res > 0);
2913 			if (res < 0)
2914 				return 0;
2915 		}
2916 	}
2917 
2918 	return 0;
2919 }
2920 
2921 #endif /* CONFIG_NO_SCAN_PROCESSING */
2922 
2923 
wpa_supplicant_fast_associate(struct wpa_supplicant * wpa_s)2924 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
2925 {
2926 #ifdef CONFIG_NO_SCAN_PROCESSING
2927 	return -1;
2928 #else /* CONFIG_NO_SCAN_PROCESSING */
2929 	struct os_reltime now;
2930 
2931 	wpa_s->ignore_post_flush_scan_res = 0;
2932 
2933 	if (wpa_s->last_scan_res_used == 0)
2934 		return -1;
2935 
2936 	os_get_reltime(&now);
2937 	if (os_reltime_expired(&now, &wpa_s->last_scan,
2938 			       wpa_s->conf->scan_res_valid_for_connect)) {
2939 		wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
2940 		return -1;
2941 	} else if (wpa_s->crossed_6ghz_dom) {
2942 		wpa_printf(MSG_DEBUG, "Fast associate: Crossed 6 GHz domain");
2943 		return -1;
2944 	}
2945 
2946 	return wpas_select_network_from_last_scan(wpa_s, 0, 1, false, NULL);
2947 #endif /* CONFIG_NO_SCAN_PROCESSING */
2948 }
2949 
2950 
wpa_wps_supplicant_fast_associate(struct wpa_supplicant * wpa_s)2951 int wpa_wps_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
2952 {
2953 #ifdef CONFIG_NO_SCAN_PROCESSING
2954 	return -1;
2955 #else /* CONFIG_NO_SCAN_PROCESSING */
2956 	return wpas_select_network_from_last_scan(wpa_s, 1, 1, false, NULL);
2957 #endif /* CONFIG_NO_SCAN_PROCESSING */
2958 }
2959 
2960 
2961 #ifdef CONFIG_WNM
2962 
wnm_bss_keep_alive(void * eloop_ctx,void * sock_ctx)2963 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
2964 {
2965 	struct wpa_supplicant *wpa_s = eloop_ctx;
2966 
2967 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
2968 		return;
2969 
2970 	if (!wpa_s->no_keep_alive) {
2971 		wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
2972 			   MAC2STR(wpa_s->bssid));
2973 		/* TODO: could skip this if normal data traffic has been sent */
2974 		/* TODO: Consider using some more appropriate data frame for
2975 		 * this */
2976 		if (wpa_s->l2)
2977 			l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
2978 				       (u8 *) "", 0);
2979 	}
2980 
2981 #ifdef CONFIG_SME
2982 	if (wpa_s->sme.bss_max_idle_period) {
2983 		unsigned int msec;
2984 		msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
2985 		if (msec > 100)
2986 			msec -= 100;
2987 		eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2988 				       wnm_bss_keep_alive, wpa_s, NULL);
2989 	}
2990 #endif /* CONFIG_SME */
2991 }
2992 
2993 
wnm_process_assoc_resp(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len)2994 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
2995 				   const u8 *ies, size_t ies_len)
2996 {
2997 	struct ieee802_11_elems elems;
2998 
2999 	if (ies == NULL)
3000 		return;
3001 
3002 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
3003 		return;
3004 
3005 #ifdef CONFIG_SME
3006 	if (elems.bss_max_idle_period) {
3007 		unsigned int msec;
3008 		wpa_s->sme.bss_max_idle_period =
3009 			WPA_GET_LE16(elems.bss_max_idle_period);
3010 		wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
3011 			   "TU)%s", wpa_s->sme.bss_max_idle_period,
3012 			   (elems.bss_max_idle_period[2] & 0x01) ?
3013 			   " (protected keep-live required)" : "");
3014 		if (wpa_s->sme.bss_max_idle_period == 0)
3015 			wpa_s->sme.bss_max_idle_period = 1;
3016 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
3017 			eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
3018 			 /* msec times 1000 */
3019 			msec = wpa_s->sme.bss_max_idle_period * 1024;
3020 			if (msec > 100)
3021 				msec -= 100;
3022 			eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
3023 					       wnm_bss_keep_alive, wpa_s,
3024 					       NULL);
3025 		}
3026 	} else {
3027 		wpa_s->sme.bss_max_idle_period = 0;
3028 	}
3029 #endif /* CONFIG_SME */
3030 }
3031 
3032 #endif /* CONFIG_WNM */
3033 
3034 
wnm_bss_keep_alive_deinit(struct wpa_supplicant * wpa_s)3035 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
3036 {
3037 #ifdef CONFIG_WNM
3038 	eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
3039 #endif /* CONFIG_WNM */
3040 }
3041 
3042 
wpas_qos_map_set(struct wpa_supplicant * wpa_s,const u8 * qos_map,size_t len)3043 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
3044 			    size_t len)
3045 {
3046 	int res;
3047 
3048 	wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
3049 	res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
3050 	if (res) {
3051 		wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
3052 	}
3053 
3054 	return res;
3055 }
3056 
3057 
interworking_process_assoc_resp(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len)3058 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
3059 					    const u8 *ies, size_t ies_len)
3060 {
3061 	struct ieee802_11_elems elems;
3062 
3063 	if (ies == NULL)
3064 		return;
3065 
3066 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
3067 		return;
3068 
3069 	if (elems.qos_map_set) {
3070 		wpas_qos_map_set(wpa_s, elems.qos_map_set,
3071 				 elems.qos_map_set_len);
3072 	}
3073 }
3074 
3075 
wpa_supplicant_set_4addr_mode(struct wpa_supplicant * wpa_s)3076 static void wpa_supplicant_set_4addr_mode(struct wpa_supplicant *wpa_s)
3077 {
3078 	if (wpa_s->enabled_4addr_mode) {
3079 		wpa_printf(MSG_DEBUG, "4addr mode already set");
3080 		return;
3081 	}
3082 
3083 	if (wpa_drv_set_4addr_mode(wpa_s, 1) < 0) {
3084 		wpa_msg(wpa_s, MSG_ERROR, "Failed to set 4addr mode");
3085 		goto fail;
3086 	}
3087 	wpa_s->enabled_4addr_mode = 1;
3088 	wpa_msg(wpa_s, MSG_INFO, "Successfully set 4addr mode");
3089 	return;
3090 
3091 fail:
3092 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3093 }
3094 
3095 
multi_ap_process_assoc_resp(struct wpa_supplicant * wpa_s,const u8 * ies,size_t ies_len)3096 static void multi_ap_process_assoc_resp(struct wpa_supplicant *wpa_s,
3097 					const u8 *ies, size_t ies_len)
3098 {
3099 	struct ieee802_11_elems elems;
3100 	struct multi_ap_params multi_ap;
3101 	u16 status;
3102 
3103 	wpa_s->multi_ap_ie = 0;
3104 
3105 	if (!ies ||
3106 	    ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed ||
3107 	    !elems.multi_ap)
3108 		return;
3109 
3110 	status = check_multi_ap_ie(elems.multi_ap + 4, elems.multi_ap_len - 4,
3111 				   &multi_ap);
3112 	if (status != WLAN_STATUS_SUCCESS)
3113 		return;
3114 
3115 	wpa_s->multi_ap_backhaul = !!(multi_ap.capability &
3116 				      MULTI_AP_BACKHAUL_BSS);
3117 	wpa_s->multi_ap_fronthaul = !!(multi_ap.capability &
3118 				       MULTI_AP_FRONTHAUL_BSS);
3119 	wpa_s->multi_ap_ie = 1;
3120 }
3121 
3122 
multi_ap_set_4addr_mode(struct wpa_supplicant * wpa_s)3123 static void multi_ap_set_4addr_mode(struct wpa_supplicant *wpa_s)
3124 {
3125 	if (!wpa_s->current_ssid ||
3126 	    !wpa_s->current_ssid->multi_ap_backhaul_sta)
3127 		return;
3128 
3129 	if (!wpa_s->multi_ap_ie) {
3130 		wpa_printf(MSG_INFO,
3131 			   "AP does not include valid Multi-AP element");
3132 		goto fail;
3133 	}
3134 
3135 	if (!wpa_s->multi_ap_backhaul) {
3136 		if (wpa_s->multi_ap_fronthaul &&
3137 		    wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
3138 			wpa_printf(MSG_INFO,
3139 				   "WPS active, accepting fronthaul-only BSS");
3140 			/* Don't set 4addr mode in this case, so just return */
3141 			return;
3142 		}
3143 		wpa_printf(MSG_INFO, "AP doesn't support backhaul BSS");
3144 		goto fail;
3145 	}
3146 
3147 	wpa_supplicant_set_4addr_mode(wpa_s);
3148 	return;
3149 
3150 fail:
3151 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3152 }
3153 
3154 
3155 #ifdef CONFIG_FST
wpas_fst_update_mbie(struct wpa_supplicant * wpa_s,const u8 * ie,size_t ie_len)3156 static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
3157 				const u8 *ie, size_t ie_len)
3158 {
3159 	struct mb_ies_info mb_ies;
3160 
3161 	if (!ie || !ie_len || !wpa_s->fst)
3162 	    return -ENOENT;
3163 
3164 	os_memset(&mb_ies, 0, sizeof(mb_ies));
3165 
3166 	while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
3167 		size_t len;
3168 
3169 		len = 2 + ie[1];
3170 		if (len > ie_len) {
3171 			wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
3172 				    ie, ie_len);
3173 			break;
3174 		}
3175 
3176 		if (ie[0] == WLAN_EID_MULTI_BAND) {
3177 			wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
3178 				   (unsigned int) len);
3179 			mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
3180 			mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
3181 			mb_ies.nof_ies++;
3182 		}
3183 
3184 		ie_len -= len;
3185 		ie += len;
3186 	}
3187 
3188 	if (mb_ies.nof_ies > 0) {
3189 		wpabuf_free(wpa_s->received_mb_ies);
3190 		wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
3191 		return 0;
3192 	}
3193 
3194 	return -ENOENT;
3195 }
3196 #endif /* CONFIG_FST */
3197 
3198 
wpa_supplicant_use_own_rsne_params(struct wpa_supplicant * wpa_s,union wpa_event_data * data)3199 static int wpa_supplicant_use_own_rsne_params(struct wpa_supplicant *wpa_s,
3200 					      union wpa_event_data *data)
3201 {
3202 	int sel;
3203 	const u8 *p;
3204 	int l, len;
3205 	bool found = false;
3206 	struct wpa_ie_data ie;
3207 	struct wpa_ssid *ssid = wpa_s->current_ssid;
3208 	struct wpa_bss *bss = wpa_s->current_bss;
3209 	int pmf;
3210 
3211 	if (!ssid)
3212 		return 0;
3213 
3214 	p = data->assoc_info.req_ies;
3215 	l = data->assoc_info.req_ies_len;
3216 
3217 	while (p && l >= 2) {
3218 		len = p[1] + 2;
3219 		if (len > l) {
3220 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
3221 				    p, l);
3222 			break;
3223 		}
3224 		if (((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
3225 		      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
3226 		     (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
3227 		      (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
3228 		     (p[0] == WLAN_EID_RSN && p[1] >= 2))) {
3229 			found = true;
3230 			break;
3231 		}
3232 		l -= len;
3233 		p += len;
3234 	}
3235 
3236 	if (!found || wpa_parse_wpa_ie(p, len, &ie) < 0) {
3237 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV, 0);
3238 		return 0;
3239 	}
3240 
3241 	wpa_hexdump(MSG_DEBUG,
3242 		    "WPA: Update cipher suite selection based on IEs in driver-generated WPA/RSNE in AssocReq",
3243 		    p, l);
3244 
3245 	/* Update proto from (Re)Association Request frame info */
3246 	wpa_s->wpa_proto = ie.proto;
3247 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, wpa_s->wpa_proto);
3248 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
3249 			 !!(wpa_s->wpa_proto &
3250 			    (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
3251 
3252 	/* Update AKMP suite from (Re)Association Request frame info */
3253 	sel = ie.key_mgmt;
3254 	if (ssid->key_mgmt)
3255 		sel &= ssid->key_mgmt;
3256 
3257 	wpa_dbg(wpa_s, MSG_DEBUG,
3258 		"WPA: AP key_mgmt 0x%x network key_mgmt 0x%x; available key_mgmt 0x%x",
3259 		ie.key_mgmt, ssid->key_mgmt, sel);
3260 	if (ie.key_mgmt && !sel) {
3261 		wpa_supplicant_deauthenticate(
3262 			wpa_s, WLAN_REASON_AKMP_NOT_VALID);
3263 		return -1;
3264 	}
3265 
3266 #ifdef CONFIG_OCV
3267 	if (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
3268 	     (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV)) && ssid->ocv)
3269 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV,
3270 				 !!(ie.capabilities & WPA_CAPABILITY_OCVC));
3271 #endif /* CONFIG_OCV */
3272 
3273 	/*
3274 	 * Update PMK in wpa_sm and the driver if roamed to WPA/WPA2 PSK from a
3275 	 * different AKM.
3276 	 */
3277 	if (wpa_s->key_mgmt != ie.key_mgmt &&
3278 	    wpa_key_mgmt_wpa_psk_no_sae(ie.key_mgmt)) {
3279 		if (!ssid->psk_set) {
3280 			wpa_dbg(wpa_s, MSG_INFO,
3281 				"No PSK available for association");
3282 			wpas_auth_failed(wpa_s, "NO_PSK_AVAILABLE", NULL);
3283 			return -1;
3284 		}
3285 
3286 		wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL, NULL);
3287 		if (wpa_s->conf->key_mgmt_offload &&
3288 		    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD) &&
3289 		    wpa_drv_set_key(wpa_s, -1, 0, NULL, 0, 0, NULL, 0,
3290 				    ssid->psk, PMK_LEN, KEY_FLAG_PMK))
3291 			wpa_dbg(wpa_s, MSG_ERROR,
3292 				"WPA: Cannot set PMK for key management offload");
3293 	}
3294 
3295 	wpa_s->key_mgmt = ie.key_mgmt;
3296 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
3297 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT %s and proto %d",
3298 		wpa_key_mgmt_txt(wpa_s->key_mgmt, wpa_s->wpa_proto),
3299 		wpa_s->wpa_proto);
3300 
3301 	/* Update pairwise cipher from (Re)Association Request frame info */
3302 	sel = ie.pairwise_cipher;
3303 	if (ssid->pairwise_cipher)
3304 		sel &= ssid->pairwise_cipher;
3305 
3306 	wpa_dbg(wpa_s, MSG_DEBUG,
3307 		"WPA: AP pairwise cipher 0x%x network pairwise cipher 0x%x; available pairwise cipher 0x%x",
3308 		ie.pairwise_cipher, ssid->pairwise_cipher, sel);
3309 	if (ie.pairwise_cipher && !sel) {
3310 		wpa_supplicant_deauthenticate(
3311 			wpa_s, WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID);
3312 		return -1;
3313 	}
3314 
3315 	wpa_s->pairwise_cipher = ie.pairwise_cipher;
3316 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
3317 			 wpa_s->pairwise_cipher);
3318 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
3319 		wpa_cipher_txt(wpa_s->pairwise_cipher));
3320 
3321 	/* Update other parameters based on AP's WPA IE/RSNE, if available */
3322 	if (!bss) {
3323 		wpa_dbg(wpa_s, MSG_DEBUG,
3324 			"WPA: current_bss == NULL - skip AP IE check");
3325 		return 0;
3326 	}
3327 
3328 	/* Update GTK and IGTK from AP's RSNE */
3329 	found = false;
3330 
3331 	if (wpa_s->wpa_proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)) {
3332 		const u8 *bss_rsn;
3333 
3334 		bss_rsn = wpa_bss_get_rsne(wpa_s, bss, ssid,
3335 					   wpa_s->valid_links);
3336 		if (bss_rsn) {
3337 			p = bss_rsn;
3338 			len = 2 + bss_rsn[1];
3339 			found = true;
3340 		}
3341 	} else if (wpa_s->wpa_proto & WPA_PROTO_WPA) {
3342 		const u8 *bss_wpa;
3343 
3344 		bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3345 		if (bss_wpa) {
3346 			p = bss_wpa;
3347 			len = 2 + bss_wpa[1];
3348 			found = true;
3349 		}
3350 	}
3351 
3352 	if (!found || wpa_parse_wpa_ie(p, len, &ie) < 0)
3353 		return 0;
3354 
3355 	pmf = wpas_get_ssid_pmf(wpa_s, ssid);
3356 	if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
3357 	    pmf == MGMT_FRAME_PROTECTION_REQUIRED) {
3358 		/* AP does not support MFP, local configuration requires it */
3359 		wpa_supplicant_deauthenticate(
3360 			wpa_s, WLAN_REASON_INVALID_RSN_IE_CAPAB);
3361 		return -1;
3362 	}
3363 	if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
3364 	    pmf == NO_MGMT_FRAME_PROTECTION) {
3365 		/* AP requires MFP, local configuration disables it */
3366 		wpa_supplicant_deauthenticate(
3367 			wpa_s, WLAN_REASON_INVALID_RSN_IE_CAPAB);
3368 		return -1;
3369 	}
3370 
3371 	/* Update PMF from local configuration now that MFP validation was done
3372 	 * above */
3373 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, pmf);
3374 
3375 	/* Update GTK from AP's RSNE */
3376 	sel = ie.group_cipher;
3377 	if (ssid->group_cipher)
3378 		sel &= ssid->group_cipher;
3379 
3380 	wpa_dbg(wpa_s, MSG_DEBUG,
3381 		"WPA: AP group cipher 0x%x network group cipher 0x%x; available group cipher 0x%x",
3382 		ie.group_cipher, ssid->group_cipher, sel);
3383 	if (ie.group_cipher && !sel) {
3384 		wpa_supplicant_deauthenticate(
3385 			wpa_s, WLAN_REASON_GROUP_CIPHER_NOT_VALID);
3386 		return -1;
3387 	}
3388 
3389 	wpa_s->group_cipher = ie.group_cipher;
3390 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
3391 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
3392 		wpa_cipher_txt(wpa_s->group_cipher));
3393 
3394 	/* Update IGTK from AP RSN IE */
3395 	sel = ie.mgmt_group_cipher;
3396 	if (ssid->group_mgmt_cipher)
3397 		sel &= ssid->group_mgmt_cipher;
3398 
3399 	wpa_dbg(wpa_s, MSG_DEBUG,
3400 		"WPA: AP mgmt_group_cipher 0x%x network mgmt_group_cipher 0x%x; available mgmt_group_cipher 0x%x",
3401 		ie.mgmt_group_cipher, ssid->group_mgmt_cipher, sel);
3402 
3403 	if (pmf == NO_MGMT_FRAME_PROTECTION ||
3404 	    !(ie.capabilities & WPA_CAPABILITY_MFPC)) {
3405 		wpa_dbg(wpa_s, MSG_DEBUG,
3406 			"WPA: STA/AP is not MFP capable; AP RSNE caps 0x%x",
3407 			ie.capabilities);
3408 		ie.mgmt_group_cipher = 0;
3409 	}
3410 
3411 	if (ie.mgmt_group_cipher && !sel) {
3412 		wpa_supplicant_deauthenticate(
3413 			wpa_s, WLAN_REASON_CIPHER_SUITE_REJECTED);
3414 		return -1;
3415 	}
3416 
3417 	wpa_s->mgmt_group_cipher = ie.mgmt_group_cipher;
3418 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
3419 			 wpa_s->mgmt_group_cipher);
3420 	if (wpa_s->mgmt_group_cipher)
3421 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher %s",
3422 			wpa_cipher_txt(wpa_s->mgmt_group_cipher));
3423 	else
3424 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
3425 
3426 	return 0;
3427 }
3428 
3429 
wpa_supplicant_event_associnfo(struct wpa_supplicant * wpa_s,union wpa_event_data * data)3430 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
3431 					  union wpa_event_data *data)
3432 {
3433 	int l, len, found = 0, wpa_found, rsn_found;
3434 #ifndef CONFIG_NO_WPA
3435 	int found_x = 0;
3436 #endif /* CONFIG_NO_WPA */
3437 	const u8 *p, *ie;
3438 	u8 bssid[ETH_ALEN];
3439 	bool bssid_known;
3440 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
3441 	struct wpa_ie_data wpa_ie;
3442 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3443 	enum wpa_rsn_override rsn_override;
3444 
3445 	wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
3446 	wpa_s->ssid_verified = false;
3447 	wpa_s->bigtk_set = false;
3448 #ifdef CONFIG_SAE
3449 #ifdef CONFIG_SME
3450 	/* SAE H2E binds the SSID into PT and that verifies the SSID
3451 	 * implicitly. */
3452 	if (wpa_s->sme.sae.state == SAE_ACCEPTED && wpa_s->sme.sae.h2e)
3453 		wpa_s->ssid_verified = true;
3454 #endif /* CONFIG_SME */
3455 #endif /* CONFIG_SAE */
3456 	bssid_known = wpa_drv_get_bssid(wpa_s, bssid) == 0;
3457 	if (data->assoc_info.req_ies)
3458 		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
3459 			    data->assoc_info.req_ies_len);
3460 	if (data->assoc_info.resp_ies) {
3461 		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
3462 			    data->assoc_info.resp_ies_len);
3463 #ifdef CONFIG_TDLS
3464 		wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
3465 					data->assoc_info.resp_ies_len);
3466 #endif /* CONFIG_TDLS */
3467 #ifdef CONFIG_WNM
3468 		wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
3469 				       data->assoc_info.resp_ies_len);
3470 #endif /* CONFIG_WNM */
3471 		interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
3472 						data->assoc_info.resp_ies_len);
3473 		if (wpa_s->hw_capab == CAPAB_VHT &&
3474 		    get_ie(data->assoc_info.resp_ies,
3475 			   data->assoc_info.resp_ies_len, WLAN_EID_VHT_CAP))
3476 			wpa_s->ieee80211ac = 1;
3477 
3478 		multi_ap_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
3479 					    data->assoc_info.resp_ies_len);
3480 	}
3481 	if (data->assoc_info.beacon_ies)
3482 		wpa_hexdump(MSG_DEBUG, "beacon_ies",
3483 			    data->assoc_info.beacon_ies,
3484 			    data->assoc_info.beacon_ies_len);
3485 	if (data->assoc_info.freq)
3486 		wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
3487 			data->assoc_info.freq);
3488 
3489 	wpa_s->connection_set = 0;
3490 	if (data->assoc_info.req_ies && data->assoc_info.resp_ies) {
3491 		struct ieee802_11_elems req_elems, resp_elems;
3492 
3493 		if (ieee802_11_parse_elems(data->assoc_info.req_ies,
3494 					   data->assoc_info.req_ies_len,
3495 					   &req_elems, 0) != ParseFailed &&
3496 		    ieee802_11_parse_elems(data->assoc_info.resp_ies,
3497 					   data->assoc_info.resp_ies_len,
3498 					   &resp_elems, 0) != ParseFailed) {
3499 			wpa_s->connection_set = 1;
3500 			wpa_s->connection_11b_only = supp_rates_11b_only(&req_elems) ||
3501 				supp_rates_11b_only(&resp_elems);
3502 			wpa_s->connection_ht = req_elems.ht_capabilities &&
3503 				resp_elems.ht_capabilities;
3504 			/* Do not include subset of VHT on 2.4 GHz vendor
3505 			 * extension in consideration for reporting VHT
3506 			 * association. */
3507 			wpa_s->connection_vht = req_elems.vht_capabilities &&
3508 				resp_elems.vht_capabilities &&
3509 				(!data->assoc_info.freq ||
3510 				 wpas_freq_to_band(data->assoc_info.freq) !=
3511 				 BAND_2_4_GHZ);
3512 			wpa_s->connection_he = req_elems.he_capabilities &&
3513 				resp_elems.he_capabilities;
3514 			wpa_s->connection_eht = req_elems.eht_capabilities &&
3515 				resp_elems.eht_capabilities;
3516 
3517 			int max_nss_rx_req = get_max_nss_capability(&req_elems, 1);
3518 			int max_nss_rx_resp = get_max_nss_capability(&resp_elems, 1);
3519 			wpa_s->connection_max_nss_rx = (max_nss_rx_resp > max_nss_rx_req) ?
3520 				max_nss_rx_req : max_nss_rx_resp;
3521 			int max_nss_tx_req = get_max_nss_capability(&req_elems, 0);
3522 			int max_nss_tx_resp = get_max_nss_capability(&resp_elems, 0);
3523 			wpa_s->connection_max_nss_tx = (max_nss_tx_resp > max_nss_tx_req) ?
3524 				max_nss_tx_req : max_nss_tx_resp;
3525 
3526 			struct supported_chan_width sta_supported_chan_width =
3527 				get_supported_channel_width(&req_elems);
3528 			enum chan_width ap_operation_chan_width =
3529 				get_operation_channel_width(&resp_elems);
3530 			if (wpa_s->connection_vht || wpa_s->connection_he ||
3531 			    wpa_s->connection_eht) {
3532 				wpa_s->connection_channel_bandwidth =
3533 					get_sta_operation_chan_width(ap_operation_chan_width,
3534 					sta_supported_chan_width);
3535 			} else if (wpa_s->connection_ht) {
3536 				wpa_s->connection_channel_bandwidth = (ap_operation_chan_width
3537 					== CHAN_WIDTH_40) ? CHAN_WIDTH_40 : CHAN_WIDTH_20;
3538 			} else {
3539 				wpa_s->connection_channel_bandwidth = CHAN_WIDTH_20;
3540 			}
3541 			if (req_elems.rrm_enabled)
3542 				wpa_s->rrm.rrm_used = 1;
3543 			wpa_s->ap_t2lm_negotiation_support =
3544 				is_ap_t2lm_negotiation_supported(resp_elems.basic_mle,
3545 				resp_elems.basic_mle_len);
3546 		}
3547 	}
3548 
3549 	p = data->assoc_info.req_ies;
3550 	l = data->assoc_info.req_ies_len;
3551 
3552 	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
3553 	while (p && l >= 2) {
3554 		len = p[1] + 2;
3555 		if (len > l) {
3556 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
3557 				    p, l);
3558 			break;
3559 		}
3560 		if (!found &&
3561 		    ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
3562 		      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
3563 		     (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
3564 		      (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
3565 		     (p[0] == WLAN_EID_RSN && p[1] >= 2))) {
3566 			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
3567 				break;
3568 			found = 1;
3569 			wpa_find_assoc_pmkid(wpa_s,
3570 					     data->assoc_info.authorized);
3571 		}
3572 #ifndef CONFIG_NO_WPA
3573 		if (!found_x && p[0] == WLAN_EID_RSNX) {
3574 			if (wpa_sm_set_assoc_rsnxe(wpa_s->wpa, p, len))
3575 				break;
3576 			found_x = 1;
3577 		}
3578 #endif /* CONFIG_NO_WPA */
3579 		l -= len;
3580 		p += len;
3581 	}
3582 	if (!found && data->assoc_info.req_ies)
3583 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
3584 #ifndef CONFIG_NO_WPA
3585 	if (!found_x && data->assoc_info.req_ies)
3586 		wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
3587 #endif /* CONFIG_NO_WPA */
3588 
3589 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
3590 	/* The WPA/RSN IE has been updated at this point. Since the Firmware could have roamed
3591 	 * to a different security type, update the current supplicant configuration to use the AKM
3592 	 * and pairwise suites from the assoc IE passed by the driver.
3593 	 */
3594 	if (wpas_driver_bss_selection(wpa_s)) {
3595 		if (!(wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &wpa_ie) < 0)) {
3596 			/* Check if firmware has roamed to a different security network */
3597 			if(wpa_s->key_mgmt != wpa_ie.key_mgmt) {
3598 				wpa_dbg(wpa_s, MSG_DEBUG, "Update to AKM suite 0x%x from Assoc IE",
3599 					wpa_ie.key_mgmt);
3600 				wpa_s->key_mgmt = wpa_ie.key_mgmt;
3601 				wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
3602 
3603 				if (wpa_key_mgmt_wpa_psk_no_sae(wpa_s->key_mgmt)) {
3604 					/* Restore PMK as it can get overwritten if the previous
3605 					* association was to 802.1X.
3606 					*/
3607 					if ((!(wpa_s->drv_flags &
3608 					    WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) &&
3609 					    (wpa_s->current_ssid) &&
3610 					    (wpa_s->current_ssid->psk_set)) {
3611 						if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
3612 							wpa_dbg(wpa_s, MSG_ERROR, "Failed to get "
3613 								"BSSID");
3614 							wpa_supplicant_deauthenticate(
3615 								wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3616 							return -1;
3617 						}
3618 						wpa_sm_set_pmk(wpa_s->wpa, wpa_s->current_ssid->psk,
3619 							PMK_LEN, NULL, bssid);
3620 					}
3621 				}
3622 			}
3623 			if(wpa_s->pairwise_cipher != wpa_ie.pairwise_cipher) {
3624 				wpa_dbg(wpa_s, MSG_DEBUG, "Update to pairwise cipher suite 0x%x "
3625 					"from Assoc IE", wpa_ie.pairwise_cipher);
3626 				wpa_s->pairwise_cipher = wpa_ie.pairwise_cipher;
3627 				wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
3628 					wpa_s->pairwise_cipher);
3629 			}
3630 			// TODO: Notify the framework about security type change b/230766005
3631 		}
3632 	}
3633 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3634 
3635 	rsn_override = RSN_OVERRIDE_NOT_USED;
3636 	ie = get_vendor_ie(data->assoc_info.req_ies,
3637 			   data->assoc_info.req_ies_len,
3638 			   RSN_SELECTION_IE_VENDOR_TYPE);
3639 	if (ie && ie[1] >= 4 + 1) {
3640 		switch (ie[2 + 4]) {
3641 		case RSN_SELECTION_RSNE:
3642 			rsn_override = RSN_OVERRIDE_RSNE;
3643 			break;
3644 		case RSN_SELECTION_RSNE_OVERRIDE:
3645 			rsn_override = RSN_OVERRIDE_RSNE_OVERRIDE;
3646 			break;
3647 		case RSN_SELECTION_RSNE_OVERRIDE_2:
3648 			rsn_override = RSN_OVERRIDE_RSNE_OVERRIDE_2;
3649 			break;
3650 		}
3651 	}
3652 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_OVERRIDE, rsn_override);
3653 
3654 #ifdef CONFIG_FILS
3655 #ifdef CONFIG_SME
3656 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
3657 	    wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS) {
3658 		if (!data->assoc_info.resp_frame ||
3659 		    fils_process_assoc_resp(wpa_s->wpa,
3660 					    data->assoc_info.resp_frame,
3661 					    data->assoc_info.resp_frame_len) <
3662 		    0) {
3663 			wpa_supplicant_deauthenticate(wpa_s,
3664 						      WLAN_REASON_UNSPECIFIED);
3665 			return -1;
3666 		}
3667 
3668 		/* FILS use of an AEAD cipher include the SSID element in
3669 		 * (Re)Association Request frame in the AAD and since the AP
3670 		 * accepted that, the SSID was verified. */
3671 		wpa_s->ssid_verified = true;
3672 	}
3673 #endif /* CONFIG_SME */
3674 
3675 	/* Additional processing for FILS when SME is in driver */
3676 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS &&
3677 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
3678 		wpa_sm_set_reset_fils_completed(wpa_s->wpa, 1);
3679 #endif /* CONFIG_FILS */
3680 
3681 #ifdef CONFIG_OWE
3682 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
3683 	    !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA) &&
3684 	    (!bssid_known ||
3685 	     owe_process_assoc_resp(wpa_s->wpa,
3686 				    wpa_s->valid_links ?
3687 				    wpa_s->ap_mld_addr : bssid,
3688 				    data->assoc_info.resp_ies,
3689 				    data->assoc_info.resp_ies_len) < 0)) {
3690 		wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
3691 		return -1;
3692 	}
3693 #endif /* CONFIG_OWE */
3694 
3695 #ifdef CONFIG_DPP2
3696 	wpa_sm_set_dpp_z(wpa_s->wpa, NULL);
3697 	if (DPP_VERSION > 1 && wpa_s->key_mgmt == WPA_KEY_MGMT_DPP &&
3698 	    wpa_s->dpp_pfs) {
3699 		struct ieee802_11_elems elems;
3700 
3701 		if (ieee802_11_parse_elems(data->assoc_info.resp_ies,
3702 					   data->assoc_info.resp_ies_len,
3703 					   &elems, 0) == ParseFailed ||
3704 		    !elems.owe_dh)
3705 			goto no_pfs;
3706 		if (dpp_pfs_process(wpa_s->dpp_pfs, elems.owe_dh,
3707 				    elems.owe_dh_len) < 0) {
3708 			wpa_supplicant_deauthenticate(wpa_s,
3709 						      WLAN_REASON_UNSPECIFIED);
3710 			return -1;
3711 		}
3712 
3713 		wpa_sm_set_dpp_z(wpa_s->wpa, wpa_s->dpp_pfs->secret);
3714 	}
3715 no_pfs:
3716 #endif /* CONFIG_DPP2 */
3717 
3718 #ifdef CONFIG_IEEE80211R
3719 #ifdef CONFIG_SME
3720 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
3721 		if (!bssid_known ||
3722 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
3723 						 data->assoc_info.resp_ies,
3724 						 data->assoc_info.resp_ies_len,
3725 						 bssid) < 0) {
3726 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
3727 				"Reassociation Response failed");
3728 			wpa_supplicant_deauthenticate(
3729 				wpa_s, WLAN_REASON_INVALID_IE);
3730 			return -1;
3731 		}
3732 		/* SSID is included in PMK-R0 derivation, so it is verified
3733 		 * implicitly. */
3734 		wpa_s->ssid_verified = true;
3735 	}
3736 
3737 	p = data->assoc_info.resp_ies;
3738 	l = data->assoc_info.resp_ies_len;
3739 
3740 #ifdef CONFIG_WPS_STRICT
3741 	if (p && wpa_s->current_ssid &&
3742 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
3743 		struct wpabuf *wps;
3744 		wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
3745 		if (wps == NULL) {
3746 			wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
3747 				"include WPS IE in (Re)Association Response");
3748 			return -1;
3749 		}
3750 
3751 		if (wps_validate_assoc_resp(wps) < 0) {
3752 			wpabuf_free(wps);
3753 			wpa_supplicant_deauthenticate(
3754 				wpa_s, WLAN_REASON_INVALID_IE);
3755 			return -1;
3756 		}
3757 		wpabuf_free(wps);
3758 	}
3759 #endif /* CONFIG_WPS_STRICT */
3760 
3761 	/* Go through the IEs and make a copy of the MDIE, if present. */
3762 	while (p && l >= 2) {
3763 		len = p[1] + 2;
3764 		if (len > l) {
3765 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
3766 				    p, l);
3767 			break;
3768 		}
3769 		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
3770 		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
3771 			wpa_s->sme.ft_used = 1;
3772 			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
3773 				  MOBILITY_DOMAIN_ID_LEN);
3774 			break;
3775 		}
3776 		l -= len;
3777 		p += len;
3778 	}
3779 #endif /* CONFIG_SME */
3780 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
3781 	if (((wpa_s->key_mgmt == WPA_KEY_MGMT_FT_PSK) ||
3782 		(wpa_s->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) ||
3783 		(wpa_s->key_mgmt == WPA_KEY_MGMT_FT_SAE) ||
3784 		(wpa_s->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X_SHA384) ||
3785 		(wpa_s->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY)) &&
3786 		wpa_ft_is_completed(wpa_s->wpa)) {
3787 		return 0;
3788 	}
3789 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3790 
3791 	/* Process FT when SME is in the driver */
3792 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3793 	    wpa_ft_is_completed(wpa_s->wpa)) {
3794 		if (!bssid_known ||
3795 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
3796 						 data->assoc_info.resp_ies,
3797 						 data->assoc_info.resp_ies_len,
3798 						 bssid) < 0) {
3799 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
3800 				"Reassociation Response failed");
3801 			wpa_supplicant_deauthenticate(
3802 				wpa_s, WLAN_REASON_INVALID_IE);
3803 			return -1;
3804 		}
3805 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
3806 		/* SSID is included in PMK-R0 derivation, so it is verified
3807 		 * implicitly. */
3808 		wpa_s->ssid_verified = true;
3809 	}
3810 
3811 	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
3812 			     data->assoc_info.resp_ies_len);
3813 #endif /* CONFIG_IEEE80211R */
3814 
3815 #ifndef CONFIG_NO_ROBUST_AV
3816 	if (bssid_known)
3817 		wpas_handle_assoc_resp_mscs(wpa_s, bssid,
3818 					    data->assoc_info.resp_ies,
3819 					    data->assoc_info.resp_ies_len);
3820 #endif /* CONFIG_NO_ROBUST_AV */
3821 
3822 	/* WPA/RSN IE from Beacon/ProbeResp */
3823 	p = data->assoc_info.beacon_ies;
3824 	l = data->assoc_info.beacon_ies_len;
3825 
3826 	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
3827 	 */
3828 	wpa_found = rsn_found = 0;
3829 	while (p && l >= 2) {
3830 		len = p[1] + 2;
3831 		if (len > l) {
3832 			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
3833 				    p, l);
3834 			break;
3835 		}
3836 		if (!wpa_found &&
3837 		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
3838 		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
3839 			wpa_found = 1;
3840 			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
3841 		}
3842 
3843 		if (!rsn_found &&
3844 		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
3845 			rsn_found = 1;
3846 			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
3847 		}
3848 
3849 		if (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
3850 		    WPA_GET_BE32(&p[2]) == RSNE_OVERRIDE_2_IE_VENDOR_TYPE)
3851 			wpa_sm_set_ap_rsne_override_2(wpa_s->wpa, p, len);
3852 
3853 		if (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
3854 		    WPA_GET_BE32(&p[2]) == RSNE_OVERRIDE_IE_VENDOR_TYPE)
3855 			wpa_sm_set_ap_rsne_override(wpa_s->wpa, p, len);
3856 
3857 		if (p[0] == WLAN_EID_RSNX && p[1] >= 1)
3858 			wpa_sm_set_ap_rsnxe(wpa_s->wpa, p, len);
3859 
3860 		if (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
3861 		    WPA_GET_BE32(&p[2]) == RSNXE_OVERRIDE_IE_VENDOR_TYPE)
3862 			wpa_sm_set_ap_rsnxe_override(wpa_s->wpa, p, len);
3863 
3864 		l -= len;
3865 		p += len;
3866 	}
3867 
3868 	if (!wpa_found && data->assoc_info.beacon_ies)
3869 		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
3870 	if (!rsn_found && data->assoc_info.beacon_ies) {
3871 		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
3872 		wpa_sm_set_ap_rsnxe(wpa_s->wpa, NULL, 0);
3873 		wpa_sm_set_ap_rsne_override(wpa_s->wpa, NULL, 0);
3874 		wpa_sm_set_ap_rsne_override_2(wpa_s->wpa, NULL, 0);
3875 		wpa_sm_set_ap_rsnxe_override(wpa_s->wpa, NULL, 0);
3876 	}
3877 	if (wpa_found || rsn_found)
3878 		wpa_s->ap_ies_from_associnfo = 1;
3879 
3880 	if (wpa_s->assoc_freq && data->assoc_info.freq) {
3881 		struct wpa_bss *bss;
3882 		unsigned int freq = 0;
3883 
3884 		if (bssid_known) {
3885 			bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
3886 			if (bss)
3887 				freq = bss->freq;
3888 		}
3889 		if (freq != data->assoc_info.freq) {
3890 			wpa_printf(MSG_DEBUG,
3891 				   "Operating frequency changed from %u to %u MHz",
3892 				   wpa_s->assoc_freq, data->assoc_info.freq);
3893 			wpa_supplicant_update_scan_results(wpa_s, bssid);
3894 		}
3895 	}
3896 
3897 	wpa_s->assoc_freq = data->assoc_info.freq;
3898 
3899 #ifndef CONFIG_NO_ROBUST_AV
3900 	wpas_handle_assoc_resp_qos_mgmt(wpa_s, data->assoc_info.resp_ies,
3901 					data->assoc_info.resp_ies_len);
3902 #endif /* CONFIG_NO_ROBUST_AV */
3903 
3904 	return 0;
3905 }
3906 
3907 
wpa_supplicant_assoc_update_ie(struct wpa_supplicant * wpa_s)3908 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
3909 {
3910 	const u8 *bss_wpa = NULL, *bss_rsn = NULL, *bss_rsnx = NULL;
3911 	const u8 *rsnoe, *rsno2e, *rsnxoe;
3912 
3913 	if (!wpa_s->current_bss || !wpa_s->current_ssid)
3914 		return -1;
3915 
3916 	if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
3917 		return 0;
3918 
3919 	bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
3920 					WPA_IE_VENDOR_TYPE);
3921 	bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
3922 	bss_rsnx = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSNX);
3923 	rsnoe = wpa_bss_get_vendor_ie(wpa_s->current_bss,
3924 				      RSNE_OVERRIDE_IE_VENDOR_TYPE);
3925 	rsno2e = wpa_bss_get_vendor_ie(wpa_s->current_bss,
3926 				       RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
3927 	rsnxoe = wpa_bss_get_vendor_ie(wpa_s->current_bss,
3928 				       RSNXE_OVERRIDE_IE_VENDOR_TYPE);
3929 
3930 	if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
3931 				 bss_wpa ? 2 + bss_wpa[1] : 0) ||
3932 	    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
3933 				 bss_rsn ? 2 + bss_rsn[1] : 0) ||
3934 	    wpa_sm_set_ap_rsnxe(wpa_s->wpa, bss_rsnx,
3935 				 bss_rsnx ? 2 + bss_rsnx[1] : 0) ||
3936 	    wpa_sm_set_ap_rsne_override(wpa_s->wpa, rsnoe,
3937 					rsnoe ? 2 + rsnoe[1] : 0) ||
3938 	    wpa_sm_set_ap_rsne_override_2(wpa_s->wpa, rsno2e,
3939 					  rsno2e ? 2 + rsno2e[1] : 0) ||
3940 	    wpa_sm_set_ap_rsnxe_override(wpa_s->wpa, rsnxoe,
3941 					 rsnxoe ? 2 + rsnxoe[1] : 0))
3942 		return -1;
3943 
3944 	return 0;
3945 }
3946 
3947 
wpas_fst_update_mb_assoc(struct wpa_supplicant * wpa_s,union wpa_event_data * data)3948 static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
3949 				     union wpa_event_data *data)
3950 {
3951 #ifdef CONFIG_FST
3952 	struct assoc_info *ai = data ? &data->assoc_info : NULL;
3953 	struct wpa_bss *bss = wpa_s->current_bss;
3954 	const u8 *ieprb, *iebcn;
3955 
3956 	wpabuf_free(wpa_s->received_mb_ies);
3957 	wpa_s->received_mb_ies = NULL;
3958 
3959 	if (ai &&
3960 	    !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
3961 		wpa_printf(MSG_DEBUG,
3962 			   "FST: MB IEs updated from Association Response frame");
3963 		return;
3964 	}
3965 
3966 	if (ai &&
3967 	    !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
3968 		wpa_printf(MSG_DEBUG,
3969 			   "FST: MB IEs updated from association event Beacon IEs");
3970 		return;
3971 	}
3972 
3973 	if (!bss)
3974 		return;
3975 
3976 	ieprb = wpa_bss_ie_ptr(bss);
3977 	iebcn = ieprb + bss->ie_len;
3978 
3979 	if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
3980 		wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
3981 	else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
3982 		wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
3983 #endif /* CONFIG_FST */
3984 }
3985 
3986 
wpas_ml_parse_assoc(struct wpa_supplicant * wpa_s,struct ieee802_11_elems * elems,struct ml_sta_link_info * ml_info)3987 static unsigned int wpas_ml_parse_assoc(struct wpa_supplicant *wpa_s,
3988 					struct ieee802_11_elems *elems,
3989 					struct ml_sta_link_info *ml_info)
3990 {
3991 	struct wpabuf *mlbuf;
3992 	struct ieee80211_eht_ml *ml;
3993 	size_t ml_len;
3994 	struct eht_ml_basic_common_info *common_info;
3995 	const u8 *pos;
3996 	u16 eml_capa = 0, mld_capa = 0;
3997 	const u16 control =
3998 		host_to_le16(MULTI_LINK_CONTROL_TYPE_BASIC |
3999 			     BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
4000 			     BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT);
4001 	u8 expected_common_info_len = 9;
4002 	unsigned int i = 0;
4003 	u16 ml_control;
4004 
4005 	if (!wpa_s->valid_links || !elems->basic_mle || !elems->basic_mle_len)
4006 		return 0;
4007 
4008 	mlbuf = ieee802_11_defrag(elems->basic_mle, elems->basic_mle_len, true);
4009 	if (!mlbuf)
4010 		return 0;
4011 
4012 	ml = (struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
4013 	ml_len = wpabuf_len(mlbuf);
4014 	if (ml_len < sizeof(*ml))
4015 		goto out;
4016 
4017 	os_memset(ml_info, 0, sizeof(*ml_info) * MAX_NUM_MLD_LINKS);
4018 
4019 	ml_control = le_to_host16(ml->ml_control);
4020 
4021 	if ((ml_control & control) != control) {
4022 		wpa_printf(MSG_DEBUG, "MLD: Invalid presence BM=0x%x",
4023 			   ml_control);
4024 		goto out;
4025 	}
4026 
4027 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
4028 		wpa_printf(MSG_DEBUG, "MLD: EML capabilities included");
4029 		expected_common_info_len += 2;
4030 	}
4031 
4032 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
4033 		wpa_printf(MSG_DEBUG, "MLD: MLD capabilities included");
4034 		expected_common_info_len += 2;
4035 	}
4036 
4037 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO) {
4038 		wpa_printf(MSG_DEBUG,
4039 			   "MLD: Unexpected: medium sync delay info present");
4040 		expected_common_info_len += 2;
4041 	}
4042 
4043 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID) {
4044 		wpa_printf(MSG_DEBUG,
4045 			   "MLD: Unexpected: MLD ID present");
4046 		expected_common_info_len++;
4047 	}
4048 
4049 	if (sizeof(*ml) + expected_common_info_len > ml_len) {
4050 		wpa_printf(MSG_DEBUG,
4051 			   "MLD: Not enough bytes for common info. ml_len=%zu",
4052 			   ml_len);
4053 		goto out;
4054 	}
4055 
4056 	common_info = (struct eht_ml_basic_common_info *) ml->variable;
4057 	if (common_info->len != expected_common_info_len) {
4058 		wpa_printf(MSG_DEBUG,
4059 			   "MLD: Invalid common info len=%u. expected=%u",
4060 			   common_info->len, expected_common_info_len);
4061 		goto out;
4062 	}
4063 
4064 	wpa_printf(MSG_DEBUG, "MLD: address: " MACSTR,
4065 		   MAC2STR(common_info->mld_addr));
4066 
4067 	if (!ether_addr_equal(wpa_s->ap_mld_addr, common_info->mld_addr)) {
4068 		wpa_printf(MSG_DEBUG, "MLD: Mismatching MLD address (expected "
4069 			   MACSTR ")", MAC2STR(wpa_s->ap_mld_addr));
4070 		goto out;
4071 	}
4072 
4073 	pos = common_info->variable;
4074 
4075 	/* Store the information for the association link */
4076 	ml_info[i].link_id = *pos;
4077 	pos++;
4078 
4079 	/* Skip the BSS Parameters Change Count */
4080 	pos++;
4081 
4082 	/* Skip the Medium Synchronization Delay Information if present  */
4083 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO)
4084 		pos += 2;
4085 
4086 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
4087 		eml_capa = WPA_GET_LE16(pos);
4088 		pos += 2;
4089 	}
4090 
4091 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
4092 		mld_capa = WPA_GET_LE16(pos);
4093 		pos += 2;
4094 	}
4095 
4096 	wpa_printf(MSG_DEBUG,
4097 		   "MLD: link_id=%u, eml=0x%x, mld=0x%x",
4098 		   ml_info[i].link_id, eml_capa, mld_capa);
4099 
4100 	i++;
4101 
4102 	pos = ((u8 *) common_info) + common_info->len;
4103 	ml_len -= sizeof(*ml) + common_info->len;
4104 	while (ml_len > 2 && i < MAX_NUM_MLD_LINKS) {
4105 		u8 sub_elem_len = pos[1];
4106 		u8 sta_info_len;
4107 		u8 nstr_bitmap_len = 0;
4108 		u16 ctrl;
4109 		const u8 *end;
4110 
4111 		wpa_printf(MSG_DEBUG, "MLD: Subelement len=%u", sub_elem_len);
4112 
4113 		if (sub_elem_len > ml_len - 2) {
4114 			wpa_printf(MSG_DEBUG,
4115 				   "MLD: Invalid link info len: %u > %zu",
4116 				   2 + sub_elem_len, ml_len);
4117 			goto out;
4118 		}
4119 
4120 		switch (*pos) {
4121 		case EHT_ML_SUB_ELEM_PER_STA_PROFILE:
4122 			break;
4123 		case EHT_ML_SUB_ELEM_FRAGMENT:
4124 		case EHT_ML_SUB_ELEM_VENDOR:
4125 			wpa_printf(MSG_DEBUG,
4126 				   "MLD: Skip subelement id=%u, len=%u",
4127 				   *pos, sub_elem_len);
4128 			pos += 2 + sub_elem_len;
4129 			ml_len -= 2 + sub_elem_len;
4130 			continue;
4131 		default:
4132 			wpa_printf(MSG_DEBUG, "MLD: Unknown subelement ID=%u",
4133 				   *pos);
4134 			goto out;
4135 		}
4136 
4137 		end = pos + 2 + sub_elem_len;
4138 
4139 		/* Skip the subelement ID and the length */
4140 		pos += 2;
4141 		ml_len -= 2;
4142 
4143 		if (end - pos < 2)
4144 			goto out;
4145 
4146 		/* Get the station control field */
4147 		ctrl = WPA_GET_LE16(pos);
4148 
4149 		pos += 2;
4150 		ml_len -= 2;
4151 
4152 		if (!(ctrl & EHT_PER_STA_CTRL_COMPLETE_PROFILE_MSK)) {
4153 			wpa_printf(MSG_DEBUG,
4154 				   "MLD: Per STA complete profile expected");
4155 			goto out;
4156 		}
4157 
4158 		if (!(ctrl & EHT_PER_STA_CTRL_MAC_ADDR_PRESENT_MSK)) {
4159 			wpa_printf(MSG_DEBUG,
4160 				   "MLD: Per STA MAC address not present");
4161 			goto out;
4162 		}
4163 
4164 		if (!(ctrl & EHT_PER_STA_CTRL_TSF_OFFSET_PRESENT_MSK)) {
4165 			wpa_printf(MSG_DEBUG,
4166 				   "MLD: Per STA TSF offset not present");
4167 			goto out;
4168 		}
4169 
4170 		if (!(ctrl & EHT_PER_STA_CTRL_BEACON_INTERVAL_PRESENT_MSK)) {
4171 			wpa_printf(MSG_DEBUG,
4172 				   "MLD: Beacon interval not present");
4173 			goto out;
4174 		}
4175 
4176 		if (!(ctrl & EHT_PER_STA_CTRL_DTIM_INFO_PRESENT_MSK)) {
4177 			wpa_printf(MSG_DEBUG,
4178 				   "MLD:  DTIM information not present");
4179 			goto out;
4180 		}
4181 
4182 		if (ctrl & EHT_PER_STA_CTRL_NSTR_LINK_PAIR_PRESENT_MSK) {
4183 			if (ctrl & EHT_PER_STA_CTRL_NSTR_BM_SIZE_MSK)
4184 				nstr_bitmap_len = 2;
4185 			else
4186 				nstr_bitmap_len = 1;
4187 		}
4188 
4189 		if (!(ctrl & EHT_PER_STA_CTRL_BSS_PARAM_CNT_PRESENT_MSK)) {
4190 			wpa_printf(MSG_DEBUG,
4191 				   "MLD:  BSS params change count not present");
4192 			goto out;
4193 		}
4194 
4195 		sta_info_len = 1 + ETH_ALEN + 8 + 2 + 2 + 1 + nstr_bitmap_len;
4196 		if (sta_info_len > ml_len || sta_info_len > end - pos ||
4197 		    sta_info_len + 2 > sub_elem_len ||
4198 		    sta_info_len != *pos) {
4199 			wpa_printf(MSG_DEBUG,
4200 				   "MLD: Invalid STA info len=%u, len=%u",
4201 				   sta_info_len, *pos);
4202 			goto out;
4203 		}
4204 
4205 		/* Get the link address */
4206 		wpa_printf(MSG_DEBUG,
4207 			   "MLD: link addr: " MACSTR " nstr BM len=%u",
4208 			   MAC2STR(pos + 1), nstr_bitmap_len);
4209 
4210 		ml_info[i].link_id = ctrl & EHT_PER_STA_CTRL_LINK_ID_MSK;
4211 		os_memcpy(ml_info[i].bssid, pos + 1, ETH_ALEN);
4212 
4213 		pos += sta_info_len;
4214 		ml_len -= sta_info_len;
4215 
4216 		wpa_printf(MSG_DEBUG, "MLD: sub_elem_len=%u, sta_info_len=%u",
4217 			   sub_elem_len, sta_info_len);
4218 
4219 		sub_elem_len -= sta_info_len + 2;
4220 		if (sub_elem_len < 4) {
4221 			wpa_printf(MSG_DEBUG, "MLD: Per STA profile too short");
4222 			goto out;
4223 		}
4224 
4225 		wpa_hexdump(MSG_MSGDUMP, "MLD: STA profile", pos, sub_elem_len);
4226 		ml_info[i].status = WPA_GET_LE16(pos + 2);
4227 
4228 		pos += sub_elem_len;
4229 		ml_len -= sub_elem_len;
4230 
4231 		i++;
4232 	}
4233 
4234 	wpabuf_free(mlbuf);
4235 	return i;
4236 out:
4237 	wpabuf_free(mlbuf);
4238 	return 0;
4239 }
4240 
4241 
wpa_drv_get_mlo_info(struct wpa_supplicant * wpa_s)4242 static int wpa_drv_get_mlo_info(struct wpa_supplicant *wpa_s)
4243 {
4244 	struct driver_sta_mlo_info mlo;
4245 	int i;
4246 
4247 	os_memset(&mlo, 0, sizeof(mlo));
4248 	if (wpas_drv_get_sta_mlo_info(wpa_s, &mlo)) {
4249 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get MLO link info");
4250 		wpa_supplicant_deauthenticate(wpa_s,
4251 					      WLAN_REASON_DEAUTH_LEAVING);
4252 		return -1;
4253 	}
4254 
4255 	if (wpa_s->valid_links == mlo.valid_links) {
4256 		bool match = true;
4257 
4258 		if (!mlo.valid_links)
4259 			return 0;
4260 
4261 		for_each_link(mlo.valid_links, i) {
4262 			if (!ether_addr_equal(wpa_s->links[i].addr,
4263 					      mlo.links[i].addr) ||
4264 			    !ether_addr_equal(wpa_s->links[i].bssid,
4265 					      mlo.links[i].bssid)) {
4266 				match = false;
4267 				break;
4268 			}
4269 		}
4270 
4271 		if (match && wpa_s->mlo_assoc_link_id == mlo.assoc_link_id &&
4272 		    ether_addr_equal(wpa_s->ap_mld_addr, mlo.ap_mld_addr))
4273 			return 0;
4274 	}
4275 
4276 	wpa_s->valid_links = mlo.valid_links;
4277 	wpa_s->mlo_assoc_link_id = mlo.assoc_link_id;
4278 	os_memcpy(wpa_s->ap_mld_addr, mlo.ap_mld_addr, ETH_ALEN);
4279 	for_each_link(wpa_s->valid_links, i) {
4280 		os_memcpy(wpa_s->links[i].addr, mlo.links[i].addr, ETH_ALEN);
4281 		os_memcpy(wpa_s->links[i].bssid, mlo.links[i].bssid, ETH_ALEN);
4282 		wpa_s->links[i].freq = mlo.links[i].freq;
4283 		wpa_supplicant_update_link_bss(wpa_s, i, mlo.links[i].bssid);
4284 	}
4285 
4286 	return 0;
4287 }
4288 
4289 
wpa_sm_set_ml_info(struct wpa_supplicant * wpa_s)4290 static int wpa_sm_set_ml_info(struct wpa_supplicant *wpa_s)
4291 {
4292 	struct driver_sta_mlo_info drv_mlo;
4293 	struct wpa_sm_mlo wpa_mlo;
4294 	int i;
4295 
4296 	os_memset(&drv_mlo, 0, sizeof(drv_mlo));
4297 	if (wpas_drv_get_sta_mlo_info(wpa_s, &drv_mlo)) {
4298 		wpa_dbg(wpa_s, MSG_INFO, "Failed to get MLO link info");
4299 		return -1;
4300 	}
4301 
4302 	os_memset(&wpa_mlo, 0, sizeof(wpa_mlo));
4303 	if (!drv_mlo.valid_links)
4304 		goto out;
4305 
4306 	os_memcpy(wpa_mlo.ap_mld_addr, drv_mlo.ap_mld_addr, ETH_ALEN);
4307 	wpa_mlo.assoc_link_id = drv_mlo.assoc_link_id;
4308 	wpa_mlo.valid_links = drv_mlo.valid_links;
4309 	wpa_mlo.req_links = drv_mlo.req_links;
4310 
4311 	for_each_link(drv_mlo.req_links, i) {
4312 		struct wpa_bss *bss;
4313 		const u8 *rsne, *rsnxe, *rsnoe, *rsno2e, *rsnxoe;
4314 
4315 		bss = wpa_supplicant_get_new_bss(wpa_s, drv_mlo.links[i].bssid);
4316 		if (!bss) {
4317 			wpa_dbg(wpa_s, MSG_INFO,
4318 				"Failed to get MLO link %d BSS", i);
4319 			return -1;
4320 		}
4321 
4322 		rsne = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4323 		rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
4324 		rsnoe = wpa_bss_get_vendor_ie(bss,
4325 					      RSNE_OVERRIDE_IE_VENDOR_TYPE);
4326 		rsno2e = wpa_bss_get_vendor_ie(bss,
4327 					       RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
4328 		rsnxoe = wpa_bss_get_vendor_ie(bss,
4329 					       RSNXE_OVERRIDE_IE_VENDOR_TYPE);
4330 
4331 		wpa_mlo.links[i].ap_rsne = rsne ? (u8 *) rsne : NULL;
4332 		wpa_mlo.links[i].ap_rsne_len = rsne ? 2 + rsne[1] : 0;
4333 		wpa_mlo.links[i].ap_rsnxe = rsnxe ? (u8 *) rsnxe : NULL;
4334 		wpa_mlo.links[i].ap_rsnxe_len = rsnxe ? 2 + rsnxe[1] : 0;
4335 		wpa_mlo.links[i].ap_rsnoe = rsnoe ? (u8 *) rsnoe : NULL;
4336 		wpa_mlo.links[i].ap_rsnoe_len = rsnoe ? 2 + rsnoe[1] : 0;
4337 		wpa_mlo.links[i].ap_rsno2e = rsno2e ? (u8 *) rsno2e : NULL;
4338 		wpa_mlo.links[i].ap_rsno2e_len = rsno2e ? 2 + rsno2e[1] : 0;
4339 		wpa_mlo.links[i].ap_rsnxoe = rsnxoe ? (u8 *) rsnxoe : NULL;
4340 		wpa_mlo.links[i].ap_rsnxoe_len = rsnxoe ? 2 + rsnxoe[1] : 0;
4341 
4342 		os_memcpy(wpa_mlo.links[i].bssid, drv_mlo.links[i].bssid,
4343 			  ETH_ALEN);
4344 		os_memcpy(wpa_mlo.links[i].addr, drv_mlo.links[i].addr,
4345 			  ETH_ALEN);
4346 	}
4347 
4348 out:
4349 	return wpa_sm_set_mlo_params(wpa_s->wpa, &wpa_mlo);
4350 }
4351 
4352 
wpa_supplicant_event_assoc(struct wpa_supplicant * wpa_s,union wpa_event_data * data)4353 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
4354 				       union wpa_event_data *data)
4355 {
4356 	u8 bssid[ETH_ALEN];
4357 	int ft_completed, already_authorized;
4358 	int new_bss = 0;
4359 #if defined(CONFIG_FILS) || defined(CONFIG_MBO)
4360 	struct wpa_bss *bss;
4361 #endif /* CONFIG_FILS || CONFIG_MBO */
4362 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
4363 	struct wpa_ie_data ie;
4364 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
4365 
4366 #ifdef CONFIG_AP
4367 	if (wpa_s->ap_iface) {
4368 		if (!data)
4369 			return;
4370 		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
4371 				    data->assoc_info.addr,
4372 				    data->assoc_info.req_ies,
4373 				    data->assoc_info.req_ies_len, NULL, 0,
4374 				    NULL, data->assoc_info.reassoc);
4375 		return;
4376 	}
4377 #endif /* CONFIG_AP */
4378 
4379 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
4380 	wpa_s->own_reconnect_req = 0;
4381 
4382 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
4383 	if (!(wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0)) {
4384 		struct wpa_ft_ies parse;
4385 		/* Check for FT reassociation is done by the driver */
4386 #ifdef CONFIG_IEEE80211R
4387 		int use_sha384 = wpa_key_mgmt_sha384(wpa_s->wpa->key_mgmt);
4388 		if (wpa_key_mgmt_ft(wpa_s->key_mgmt) && (wpa_s->key_mgmt == ie.key_mgmt)) {
4389 			if (wpa_ft_parse_ies(data->assoc_info.resp_ies,
4390 				data->assoc_info.resp_ies_len, &parse, use_sha384, false) < 0) {
4391 				wpa_printf(MSG_DEBUG, "Failed to parse FT IEs");
4392 				return;
4393 			}
4394 			if (parse.rsn_pmkid != NULL) {
4395 				wpa_set_ft_completed(wpa_s->wpa);
4396 				wpa_dbg(wpa_s, MSG_DEBUG, "Assume FT reassoc completed by the driver");
4397 			}
4398 		}
4399 #endif  /* CONFIG_IEEE80211R */
4400 	}
4401 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
4402 
4403 	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
4404 
4405 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
4406 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
4407 		wpa_supplicant_deauthenticate(
4408 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4409 		return;
4410 	}
4411 
4412 	if (wpa_drv_get_mlo_info(wpa_s) < 0) {
4413 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get MLO connection info");
4414 		wpa_supplicant_deauthenticate(wpa_s,
4415 					      WLAN_REASON_DEAUTH_LEAVING);
4416 		return;
4417 	}
4418 
4419 	if (ft_completed &&
4420 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION)) {
4421 		wpa_msg(wpa_s, MSG_INFO, "Attempt to roam to " MACSTR,
4422 			MAC2STR(bssid));
4423 		if (!wpa_supplicant_update_current_bss(wpa_s, bssid)) {
4424 			wpa_printf(MSG_ERROR,
4425 				   "Can't find target AP's information!");
4426 			return;
4427 		}
4428 		wpa_supplicant_assoc_update_ie(wpa_s);
4429 	}
4430 
4431 	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
4432 		return;
4433 	/*
4434 	 * FILS authentication can share the same mechanism to mark the
4435 	 * connection fully authenticated, so set ft_completed also based on
4436 	 * FILS result.
4437 	 */
4438 	if (!ft_completed)
4439 		ft_completed = wpa_fils_is_completed(wpa_s->wpa);
4440 
4441 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
4442 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
4443 		/*
4444 		 * For driver based roaming, insert PSK during
4445 		 * the initial association
4446 		 */
4447 		if (is_zero_ether_addr(wpa_s->bssid) &&
4448 			wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
4449 			/*
4450 			 * In case the driver wants to handle re-assocs,
4451 			 * pass it down the PMK.
4452 			 */
4453 			wpa_dbg(wpa_s, MSG_DEBUG, "Pass the PMK to the driver");
4454 			wpa_sm_install_pmk(wpa_s->wpa);
4455 		}
4456 	}
4457 #endif
4458 
4459 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
4460 	if (!ether_addr_equal(bssid, wpa_s->bssid)) {
4461 		if (os_reltime_initialized(&wpa_s->session_start)) {
4462 			os_reltime_age(&wpa_s->session_start,
4463 				       &wpa_s->session_length);
4464 			wpa_s->session_start.sec = 0;
4465 			wpa_s->session_start.usec = 0;
4466 			wpas_notify_session_length(wpa_s);
4467 		} else {
4468 			wpas_notify_auth_changed(wpa_s);
4469 			os_get_reltime(&wpa_s->session_start);
4470 		}
4471 		wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
4472 			MACSTR, MAC2STR(bssid));
4473 		new_bss = 1;
4474 		random_add_randomness(bssid, ETH_ALEN);
4475 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
4476 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
4477 		wpas_notify_bssid_changed(wpa_s);
4478 
4479 		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
4480 			wpa_clear_keys(wpa_s, bssid);
4481 		}
4482 		if (wpa_supplicant_select_config(wpa_s, data) < 0) {
4483 			wpa_supplicant_deauthenticate(
4484 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4485 			return;
4486 		}
4487 	}
4488 
4489 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
4490 	    data && wpa_supplicant_use_own_rsne_params(wpa_s, data) < 0)
4491 		return;
4492 
4493 	multi_ap_set_4addr_mode(wpa_s);
4494 
4495 	if (wpa_s->conf->ap_scan == 1 &&
4496 	    wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
4497 		if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
4498 			wpa_msg(wpa_s, MSG_WARNING,
4499 				"WPA/RSN IEs not updated");
4500 	}
4501 
4502 	wpas_fst_update_mb_assoc(wpa_s, data);
4503 
4504 #ifdef CONFIG_SME
4505 	/*
4506 	 * Cache the current AP's BSSID (for non-MLO connection) or MLD address
4507 	 * (for MLO connection) as the previous BSSID for subsequent
4508 	 * reassociation requests handled by SME-in-wpa_supplicant.
4509 	 */
4510 	os_memcpy(wpa_s->sme.prev_bssid,
4511 		  wpa_s->valid_links ? wpa_s->ap_mld_addr : bssid, ETH_ALEN);
4512 	wpa_s->sme.prev_bssid_set = 1;
4513 	wpa_s->sme.last_unprot_disconnect.sec = 0;
4514 #endif /* CONFIG_SME */
4515 
4516 	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
4517 	if (wpa_s->current_ssid) {
4518 		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
4519 		 * initialized before association, but for other modes,
4520 		 * initialize PC/SC here, if the current configuration needs
4521 		 * smartcard or SIM/USIM. */
4522 		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
4523 	}
4524 	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
4525 
4526 	if (wpa_sm_set_ml_info(wpa_s)) {
4527 		wpa_dbg(wpa_s, MSG_INFO,
4528 			"Failed to set MLO connection info to wpa_sm");
4529 		wpa_supplicant_deauthenticate(wpa_s,
4530 					      WLAN_REASON_DEAUTH_LEAVING);
4531 		return;
4532 	}
4533 
4534 	if (wpa_s->l2)
4535 		l2_packet_notify_auth_start(wpa_s->l2);
4536 
4537 	already_authorized = data && data->assoc_info.authorized;
4538 
4539 	/*
4540 	 * Set portEnabled first to false in order to get EAP state machine out
4541 	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
4542 	 * state machine may transit to AUTHENTICATING state based on obsolete
4543 	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
4544 	 * AUTHENTICATED without ever giving chance to EAP state machine to
4545 	 * reset the state.
4546 	 */
4547 	if (!ft_completed && !already_authorized) {
4548 		eapol_sm_notify_portEnabled(wpa_s->eapol, false);
4549 		eapol_sm_notify_portValid(wpa_s->eapol, false);
4550 	}
4551 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
4552 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
4553 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE || ft_completed ||
4554 	    already_authorized || wpa_s->drv_authorized_port)
4555 		eapol_sm_notify_eap_success(wpa_s->eapol, false);
4556 	/* 802.1X::portControl = Auto */
4557 	eapol_sm_notify_portEnabled(wpa_s->eapol, true);
4558 	wpa_s->eapol_received = 0;
4559 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
4560 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
4561 	    (wpa_s->current_ssid &&
4562 	     wpa_s->current_ssid->mode == WPAS_MODE_IBSS)) {
4563 		if (wpa_s->current_ssid &&
4564 		    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
4565 		    (wpa_s->drv_flags &
4566 		     WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
4567 			/*
4568 			 * Set the key after having received joined-IBSS event
4569 			 * from the driver.
4570 			 */
4571 			wpa_supplicant_set_wpa_none_key(wpa_s,
4572 							wpa_s->current_ssid);
4573 		}
4574 		wpa_supplicant_cancel_auth_timeout(wpa_s);
4575 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
4576 	} else if (!ft_completed) {
4577 		/* Timeout for receiving the first EAPOL packet */
4578 		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
4579 	}
4580 	wpa_supplicant_cancel_scan(wpa_s);
4581 
4582 	if (ft_completed) {
4583 		/*
4584 		 * FT protocol completed - make sure EAPOL state machine ends
4585 		 * up in authenticated.
4586 		 */
4587 		wpa_supplicant_cancel_auth_timeout(wpa_s);
4588 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
4589 		eapol_sm_notify_portValid(wpa_s->eapol, true);
4590 		eapol_sm_notify_eap_success(wpa_s->eapol, true);
4591 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
4592 		   wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
4593 		if (already_authorized) {
4594 			/*
4595 			 * We are done; the driver will take care of RSN 4-way
4596 			 * handshake.
4597 			 */
4598 			wpa_supplicant_cancel_auth_timeout(wpa_s);
4599 			wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
4600 			eapol_sm_notify_portValid(wpa_s->eapol, true);
4601 			eapol_sm_notify_eap_success(wpa_s->eapol, true);
4602 		} else {
4603 			/* Update port, WPA_COMPLETED state from the
4604 			 * EVENT_PORT_AUTHORIZED handler when the driver is done
4605 			 * with the 4-way handshake.
4606 			 */
4607 			wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
4608 			wpa_msg(wpa_s, MSG_INFO,
4609 				"ASSOC INFO: wait for driver port authorized indication");
4610 		}
4611 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
4612 		   wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
4613 		/*
4614 		 * The driver will take care of RSN 4-way handshake, so we need
4615 		 * to allow EAPOL supplicant to complete its work without
4616 		 * waiting for WPA supplicant.
4617 		 */
4618 		eapol_sm_notify_portValid(wpa_s->eapol, true);
4619 	}
4620 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
4621 	if (ft_completed && wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
4622 		if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
4623 			wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID, key_mgmt: 0x%0x",
4624 				wpa_s->key_mgmt);
4625 			wpa_supplicant_deauthenticate(
4626 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4627 			return;
4628 		}
4629 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
4630 		wpa_s->assoc_freq = data->assoc_info.freq;
4631 		wpa_sm_notify_brcm_ft_reassoc(wpa_s->wpa, bssid);
4632 	}
4633 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
4634 
4635 	wpa_s->last_eapol_matches_bssid = 0;
4636 
4637 #ifdef CONFIG_TESTING_OPTIONS
4638 	if (wpa_s->rsne_override_eapol) {
4639 		wpa_printf(MSG_DEBUG,
4640 			   "TESTING: RSNE EAPOL-Key msg 2/4 override");
4641 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa,
4642 					wpabuf_head(wpa_s->rsne_override_eapol),
4643 					wpabuf_len(wpa_s->rsne_override_eapol));
4644 	}
4645 	if (wpa_s->rsnxe_override_eapol) {
4646 		wpa_printf(MSG_DEBUG,
4647 			   "TESTING: RSNXE EAPOL-Key msg 2/4 override");
4648 		wpa_sm_set_assoc_rsnxe(wpa_s->wpa,
4649 				       wpabuf_head(wpa_s->rsnxe_override_eapol),
4650 				       wpabuf_len(wpa_s->rsnxe_override_eapol));
4651 	}
4652 #endif /* CONFIG_TESTING_OPTIONS */
4653 
4654 	if (wpa_s->pending_eapol_rx) {
4655 		struct os_reltime now, age;
4656 		os_get_reltime(&now);
4657 		os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
4658 		if (age.sec == 0 && age.usec < 200000 &&
4659 		    ether_addr_equal(wpa_s->pending_eapol_rx_src,
4660 				     wpa_s->valid_links ? wpa_s->ap_mld_addr :
4661 				     bssid)) {
4662 			wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
4663 				"frame that was received just before "
4664 				"association notification");
4665 			wpa_supplicant_rx_eapol(
4666 				wpa_s, wpa_s->pending_eapol_rx_src,
4667 				wpabuf_head(wpa_s->pending_eapol_rx),
4668 				wpabuf_len(wpa_s->pending_eapol_rx),
4669 				wpa_s->pending_eapol_encrypted);
4670 		}
4671 		wpabuf_free(wpa_s->pending_eapol_rx);
4672 		wpa_s->pending_eapol_rx = NULL;
4673 	}
4674 
4675 #ifdef CONFIG_WEP
4676 	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
4677 	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
4678 	    wpa_s->current_ssid &&
4679 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
4680 		/* Set static WEP keys again */
4681 		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
4682 	}
4683 #endif /* CONFIG_WEP */
4684 
4685 #ifdef CONFIG_IBSS_RSN
4686 	if (wpa_s->current_ssid &&
4687 	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
4688 	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
4689 	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
4690 	    wpa_s->ibss_rsn == NULL) {
4691 		wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
4692 		if (!wpa_s->ibss_rsn) {
4693 			wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
4694 			wpa_supplicant_deauthenticate(
4695 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4696 			return;
4697 		}
4698 
4699 		ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
4700 	}
4701 #endif /* CONFIG_IBSS_RSN */
4702 
4703 	wpas_wps_notify_assoc(wpa_s, bssid);
4704 
4705 #ifndef CONFIG_NO_WMM_AC
4706 	if (data) {
4707 		wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
4708 				    data->assoc_info.resp_ies_len,
4709 				    &data->assoc_info.wmm_params);
4710 
4711 		if (wpa_s->reassoc_same_bss)
4712 			wmm_ac_restore_tspecs(wpa_s);
4713 	}
4714 #endif /* CONFIG_NO_WMM_AC */
4715 
4716 #if defined(CONFIG_FILS) || defined(CONFIG_MBO)
4717 	bss = wpa_bss_get_bssid(wpa_s, bssid);
4718 #endif /* CONFIG_FILS || CONFIG_MBO */
4719 #ifdef CONFIG_FILS
4720 	if (wpa_key_mgmt_fils(wpa_s->key_mgmt)) {
4721 		const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
4722 
4723 		if (fils_cache_id)
4724 			wpa_sm_set_fils_cache_id(wpa_s->wpa, fils_cache_id);
4725 	}
4726 #endif /* CONFIG_FILS */
4727 
4728 #ifdef CONFIG_MBO
4729 	wpas_mbo_check_pmf(wpa_s, bss, wpa_s->current_ssid);
4730 #endif /* CONFIG_MBO */
4731 
4732 #ifdef CONFIG_DPP2
4733 	wpa_s->dpp_pfs_fallback = 0;
4734 #endif /* CONFIG_DPP2 */
4735 
4736 	if (wpa_s->current_ssid && wpa_s->current_ssid->enable_4addr_mode)
4737 		wpa_supplicant_set_4addr_mode(wpa_s);
4738 }
4739 
4740 
disconnect_reason_recoverable(u16 reason_code)4741 static int disconnect_reason_recoverable(u16 reason_code)
4742 {
4743 	return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
4744 		reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
4745 		reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
4746 }
4747 
4748 
wpa_supplicant_event_disassoc(struct wpa_supplicant * wpa_s,u16 reason_code,int locally_generated)4749 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
4750 					  u16 reason_code,
4751 					  int locally_generated)
4752 {
4753 	const u8 *bssid;
4754 
4755 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4756 		/*
4757 		 * At least Host AP driver and a Prism3 card seemed to be
4758 		 * generating streams of disconnected events when configuring
4759 		 * IBSS for WPA-None. Ignore them for now.
4760 		 */
4761 		return;
4762 	}
4763 
4764 	bssid = wpa_s->bssid;
4765 	if (is_zero_ether_addr(bssid))
4766 		bssid = wpa_s->pending_bssid;
4767 
4768 	if (!is_zero_ether_addr(bssid) ||
4769 	    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
4770 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
4771 			" reason=%d%s",
4772 			MAC2STR(bssid), reason_code,
4773 			locally_generated ? " locally_generated=1" : "");
4774 	}
4775 }
4776 
4777 
could_be_psk_mismatch(struct wpa_supplicant * wpa_s,u16 reason_code,int locally_generated)4778 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
4779 				 int locally_generated)
4780 {
4781 	if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
4782 	    !wpa_s->new_connection ||
4783 	    !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
4784 	    wpa_key_mgmt_sae(wpa_s->key_mgmt))
4785 		return 0; /* Not in initial 4-way handshake with PSK */
4786 
4787 	/*
4788 	 * It looks like connection was lost while trying to go through PSK
4789 	 * 4-way handshake. Filter out known disconnection cases that are caused
4790 	 * by something else than PSK mismatch to avoid confusing reports.
4791 	 */
4792 
4793 	if (locally_generated) {
4794 		if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
4795 			return 0;
4796 	}
4797 
4798 	return 1;
4799 }
4800 
4801 
wpa_supplicant_event_disassoc_finish(struct wpa_supplicant * wpa_s,u16 reason_code,int locally_generated)4802 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
4803 						 u16 reason_code,
4804 						 int locally_generated)
4805 {
4806 	const u8 *bssid;
4807 	struct wpa_bss *fast_reconnect = NULL;
4808 	struct wpa_ssid *fast_reconnect_ssid = NULL;
4809 	struct wpa_bss *curr = NULL;
4810 
4811 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4812 		/*
4813 		 * At least Host AP driver and a Prism3 card seemed to be
4814 		 * generating streams of disconnected events when configuring
4815 		 * IBSS for WPA-None. Ignore them for now.
4816 		 */
4817 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
4818 			"IBSS/WPA-None mode");
4819 		return;
4820 	}
4821 
4822 	if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
4823 	    reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
4824 	    locally_generated)
4825 		/*
4826 		 * Remove the inactive AP (which is probably out of range) from
4827 		 * the BSS list after marking disassociation. In particular
4828 		 * mac80211-based drivers use the
4829 		 * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
4830 		 * locally generated disconnection events for cases where the
4831 		 * AP does not reply anymore.
4832 		 */
4833 		curr = wpa_s->current_bss;
4834 
4835 	if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
4836 		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
4837 			"pre-shared key may be incorrect");
4838 		if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
4839 			return; /* P2P group removed */
4840 		wpas_auth_failed(wpa_s, "WRONG_KEY", wpa_s->pending_bssid);
4841 		wpas_notify_psk_mismatch(wpa_s);
4842 #ifdef CONFIG_DPP2
4843 		wpas_dpp_send_conn_status_result(wpa_s,
4844 						 DPP_STATUS_AUTH_FAILURE);
4845 #endif /* CONFIG_DPP2 */
4846 	}
4847 	if (!wpa_s->disconnected &&
4848 	    (!wpa_s->auto_reconnect_disabled ||
4849 	     wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
4850 	     wpas_wps_searching(wpa_s) ||
4851 	     wpas_wps_reenable_networks_pending(wpa_s))) {
4852 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
4853 			"reconnect (wps=%d/%d wpa_state=%d)",
4854 			wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
4855 			wpas_wps_searching(wpa_s),
4856 			wpa_s->wpa_state);
4857 		if (wpa_s->wpa_state == WPA_COMPLETED &&
4858 		    wpa_s->current_ssid &&
4859 		    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4860 		    (wpa_s->own_reconnect_req ||
4861 		     (!locally_generated &&
4862 		      disconnect_reason_recoverable(reason_code)))) {
4863 			/*
4864 			 * It looks like the AP has dropped association with
4865 			 * us, but could allow us to get back in. This is also
4866 			 * triggered for cases where local reconnection request
4867 			 * is used to force reassociation with the same BSS.
4868 			 * Try to reconnect to the same BSS without a full scan
4869 			 * to save time for some common cases.
4870 			 */
4871 			fast_reconnect = wpa_s->current_bss;
4872 			fast_reconnect_ssid = wpa_s->current_ssid;
4873 		} else if (wpa_s->wpa_state >= WPA_ASSOCIATING) {
4874 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
4875 		} else {
4876 			wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
4877 				"immediate scan");
4878 		}
4879 	} else {
4880 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
4881 			"try to re-connect");
4882 		wpa_s->reassociate = 0;
4883 		wpa_s->disconnected = 1;
4884 		if (!wpa_s->pno)
4885 			wpa_supplicant_cancel_sched_scan(wpa_s);
4886 	}
4887 	bssid = wpa_s->bssid;
4888 	if (is_zero_ether_addr(bssid))
4889 		bssid = wpa_s->pending_bssid;
4890 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4891 		wpas_connection_failed(wpa_s, bssid, NULL);
4892 	wpa_sm_notify_disassoc(wpa_s->wpa);
4893 	ptksa_cache_flush(wpa_s->ptksa, wpa_s->bssid, WPA_CIPHER_NONE);
4894 
4895 	if (locally_generated)
4896 		wpa_s->disconnect_reason = -reason_code;
4897 	else
4898 		wpa_s->disconnect_reason = reason_code;
4899 	wpas_notify_disconnect_reason(wpa_s);
4900 	if (wpa_supplicant_dynamic_keys(wpa_s)) {
4901 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
4902 		wpa_clear_keys(wpa_s, wpa_s->bssid);
4903 	}
4904 	wpa_supplicant_mark_disassoc(wpa_s);
4905 
4906 	if (curr)
4907 		wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
4908 
4909 	if (fast_reconnect &&
4910 	    !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
4911 	    !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
4912 	    !disallowed_ssid(wpa_s, fast_reconnect->ssid,
4913 			     fast_reconnect->ssid_len) &&
4914 	    !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
4915 	    !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect)) {
4916 #ifndef CONFIG_NO_SCAN_PROCESSING
4917 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
4918 		if (wpa_supplicant_connect(wpa_s, fast_reconnect,
4919 					   fast_reconnect_ssid) < 0) {
4920 			/* Recover through full scan */
4921 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
4922 		}
4923 #endif /* CONFIG_NO_SCAN_PROCESSING */
4924 	} else if (fast_reconnect) {
4925 		/*
4926 		 * Could not reconnect to the same BSS due to network being
4927 		 * disabled. Use a new scan to match the alternative behavior
4928 		 * above, i.e., to continue automatic reconnection attempt in a
4929 		 * way that enforces disabled network rules.
4930 		 */
4931 		wpa_supplicant_req_scan(wpa_s, 0, 100000);
4932 	}
4933 }
4934 
4935 
4936 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
wpa_supplicant_delayed_mic_error_report(void * eloop_ctx,void * sock_ctx)4937 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
4938 {
4939 	struct wpa_supplicant *wpa_s = eloop_ctx;
4940 
4941 	if (!wpa_s->pending_mic_error_report)
4942 		return;
4943 
4944 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
4945 	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
4946 	wpa_s->pending_mic_error_report = 0;
4947 }
4948 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
4949 
4950 
4951 static void
wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant * wpa_s,union wpa_event_data * data)4952 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
4953 					 union wpa_event_data *data)
4954 {
4955 	int pairwise;
4956 	struct os_reltime t;
4957 
4958 	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
4959 	pairwise = (data && data->michael_mic_failure.unicast);
4960 	os_get_reltime(&t);
4961 	if ((os_reltime_initialized(&wpa_s->last_michael_mic_error) &&
4962 	     !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
4963 	    wpa_s->pending_mic_error_report) {
4964 		if (wpa_s->pending_mic_error_report) {
4965 			/*
4966 			 * Send the pending MIC error report immediately since
4967 			 * we are going to start countermeasures and AP better
4968 			 * do the same.
4969 			 */
4970 			wpa_sm_key_request(wpa_s->wpa, 1,
4971 					   wpa_s->pending_mic_error_pairwise);
4972 		}
4973 
4974 		/* Send the new MIC error report immediately since we are going
4975 		 * to start countermeasures and AP better do the same.
4976 		 */
4977 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
4978 
4979 		/* initialize countermeasures */
4980 		wpa_s->countermeasures = 1;
4981 
4982 		wpa_bssid_ignore_add(wpa_s, wpa_s->bssid);
4983 
4984 		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
4985 
4986 		/*
4987 		 * Need to wait for completion of request frame. We do not get
4988 		 * any callback for the message completion, so just wait a
4989 		 * short while and hope for the best. */
4990 		os_sleep(0, 10000);
4991 
4992 		wpa_drv_set_countermeasures(wpa_s, 1);
4993 		wpa_supplicant_deauthenticate(wpa_s,
4994 					      WLAN_REASON_MICHAEL_MIC_FAILURE);
4995 		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
4996 				     wpa_s, NULL);
4997 		eloop_register_timeout(60, 0,
4998 				       wpa_supplicant_stop_countermeasures,
4999 				       wpa_s, NULL);
5000 		/* TODO: mark the AP rejected for 60 second. STA is
5001 		 * allowed to associate with another AP.. */
5002 	} else {
5003 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
5004 		if (wpa_s->mic_errors_seen) {
5005 			/*
5006 			 * Reduce the effectiveness of Michael MIC error
5007 			 * reports as a means for attacking against TKIP if
5008 			 * more than one MIC failure is noticed with the same
5009 			 * PTK. We delay the transmission of the reports by a
5010 			 * random time between 0 and 60 seconds in order to
5011 			 * force the attacker wait 60 seconds before getting
5012 			 * the information on whether a frame resulted in a MIC
5013 			 * failure.
5014 			 */
5015 			u8 rval[4];
5016 			int sec;
5017 
5018 			if (os_get_random(rval, sizeof(rval)) < 0)
5019 				sec = os_random() % 60;
5020 			else
5021 				sec = WPA_GET_BE32(rval) % 60;
5022 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
5023 				"report %d seconds", sec);
5024 			wpa_s->pending_mic_error_report = 1;
5025 			wpa_s->pending_mic_error_pairwise = pairwise;
5026 			eloop_cancel_timeout(
5027 				wpa_supplicant_delayed_mic_error_report,
5028 				wpa_s, NULL);
5029 			eloop_register_timeout(
5030 				sec, os_random() % 1000000,
5031 				wpa_supplicant_delayed_mic_error_report,
5032 				wpa_s, NULL);
5033 		} else {
5034 			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
5035 		}
5036 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
5037 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
5038 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
5039 	}
5040 	wpa_s->last_michael_mic_error = t;
5041 	wpa_s->mic_errors_seen++;
5042 }
5043 
5044 
5045 #ifdef CONFIG_TERMINATE_ONLASTIF
any_interfaces(struct wpa_supplicant * head)5046 static int any_interfaces(struct wpa_supplicant *head)
5047 {
5048 	struct wpa_supplicant *wpa_s;
5049 
5050 	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
5051 		if (!wpa_s->interface_removed)
5052 			return 1;
5053 	return 0;
5054 }
5055 #endif /* CONFIG_TERMINATE_ONLASTIF */
5056 
5057 
5058 static void
wpa_supplicant_event_interface_status(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5059 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
5060 				      union wpa_event_data *data)
5061 {
5062 	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
5063 		return;
5064 
5065 	switch (data->interface_status.ievent) {
5066 	case EVENT_INTERFACE_ADDED:
5067 		if (!wpa_s->interface_removed)
5068 			break;
5069 		wpa_s->interface_removed = 0;
5070 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
5071 		if (wpa_supplicant_driver_init(wpa_s) < 0) {
5072 			wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
5073 				"driver after interface was added");
5074 		}
5075 
5076 #ifdef CONFIG_P2P
5077 		if (!wpa_s->global->p2p &&
5078 		    !wpa_s->global->p2p_disabled &&
5079 		    !wpa_s->conf->p2p_disabled &&
5080 		    (wpa_s->drv_flags &
5081 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
5082 		    wpas_p2p_add_p2pdev_interface(
5083 			    wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
5084 			wpa_printf(MSG_INFO,
5085 				   "P2P: Failed to enable P2P Device interface");
5086 			/* Try to continue without. P2P will be disabled. */
5087 		}
5088 #endif /* CONFIG_P2P */
5089 
5090 		break;
5091 	case EVENT_INTERFACE_REMOVED:
5092 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
5093 		wpa_s->interface_removed = 1;
5094 		wpa_supplicant_mark_disassoc(wpa_s);
5095 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
5096 		l2_packet_deinit(wpa_s->l2);
5097 		wpa_s->l2 = NULL;
5098 
5099 #ifdef CONFIG_P2P
5100 		if (wpa_s->global->p2p &&
5101 		    wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
5102 		    (wpa_s->drv_flags &
5103 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
5104 			wpa_dbg(wpa_s, MSG_DEBUG,
5105 				"Removing P2P Device interface");
5106 			wpa_supplicant_remove_iface(
5107 				wpa_s->global, wpa_s->global->p2p_init_wpa_s,
5108 				0);
5109 			wpa_s->global->p2p_init_wpa_s = NULL;
5110 		}
5111 #endif /* CONFIG_P2P */
5112 
5113 #ifdef CONFIG_MATCH_IFACE
5114 		if (wpa_s->matched) {
5115 			wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
5116 			break;
5117 		}
5118 #endif /* CONFIG_MATCH_IFACE */
5119 
5120 #ifdef CONFIG_TERMINATE_ONLASTIF
5121 		/* check if last interface */
5122 		if (!any_interfaces(wpa_s->global->ifaces))
5123 			eloop_terminate();
5124 #endif /* CONFIG_TERMINATE_ONLASTIF */
5125 		break;
5126 	}
5127 }
5128 
5129 
5130 #ifdef CONFIG_TDLS
wpa_supplicant_event_tdls(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5131 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
5132 				      union wpa_event_data *data)
5133 {
5134 	if (data == NULL)
5135 		return;
5136 	switch (data->tdls.oper) {
5137 	case TDLS_REQUEST_SETUP:
5138 		wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
5139 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
5140 			wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
5141 		else
5142 			wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
5143 		break;
5144 	case TDLS_REQUEST_TEARDOWN:
5145 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
5146 			wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
5147 					       data->tdls.reason_code);
5148 		else
5149 			wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
5150 					  data->tdls.peer);
5151 		break;
5152 	case TDLS_REQUEST_DISCOVER:
5153 			wpa_tdls_send_discovery_request(wpa_s->wpa,
5154 							data->tdls.peer);
5155 		break;
5156 	}
5157 }
5158 #endif /* CONFIG_TDLS */
5159 
5160 
5161 #ifdef CONFIG_WNM
wpa_supplicant_event_wnm(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5162 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
5163 				     union wpa_event_data *data)
5164 {
5165 	if (data == NULL)
5166 		return;
5167 	switch (data->wnm.oper) {
5168 	case WNM_OPER_SLEEP:
5169 		wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
5170 			   "(action=%d, intval=%d)",
5171 			   data->wnm.sleep_action, data->wnm.sleep_intval);
5172 		ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
5173 					     data->wnm.sleep_intval, NULL);
5174 		break;
5175 	}
5176 }
5177 #endif /* CONFIG_WNM */
5178 
5179 
5180 #ifdef CONFIG_IEEE80211R
5181 static void
wpa_supplicant_event_ft_response(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5182 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
5183 				 union wpa_event_data *data)
5184 {
5185 	if (data == NULL)
5186 		return;
5187 
5188 	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
5189 				    data->ft_ies.ies_len,
5190 				    data->ft_ies.ft_action,
5191 				    data->ft_ies.target_ap,
5192 				    data->ft_ies.ric_ies,
5193 				    data->ft_ies.ric_ies_len) < 0) {
5194 		/* TODO: prevent MLME/driver from trying to associate? */
5195 	}
5196 }
5197 #endif /* CONFIG_IEEE80211R */
5198 
5199 
5200 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5201 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
5202 						union wpa_event_data *data)
5203 {
5204 	struct wpa_ssid *ssid;
5205 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
5206 		return;
5207 	if (data == NULL)
5208 		return;
5209 	ssid = wpa_s->current_ssid;
5210 	if (ssid == NULL)
5211 		return;
5212 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
5213 		return;
5214 
5215 	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
5216 }
5217 
5218 
wpa_supplicant_event_ibss_auth(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5219 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
5220 					   union wpa_event_data *data)
5221 {
5222 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5223 
5224 	if (ssid == NULL)
5225 		return;
5226 
5227 	/* check if the ssid is correctly configured as IBSS/RSN */
5228 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
5229 		return;
5230 
5231 	ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
5232 			     data->rx_mgmt.frame_len);
5233 }
5234 #endif /* CONFIG_IBSS_RSN */
5235 
5236 
5237 #ifdef CONFIG_IEEE80211R
ft_rx_action(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)5238 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
5239 			 size_t len)
5240 {
5241 	const u8 *sta_addr, *target_ap_addr;
5242 	u16 status;
5243 
5244 	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
5245 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
5246 		return; /* only SME case supported for now */
5247 	if (len < 1 + 2 * ETH_ALEN + 2)
5248 		return;
5249 	if (data[0] != 2)
5250 		return; /* Only FT Action Response is supported for now */
5251 	sta_addr = data + 1;
5252 	target_ap_addr = data + 1 + ETH_ALEN;
5253 	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
5254 	wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
5255 		MACSTR " TargetAP " MACSTR " status %u",
5256 		MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
5257 
5258 	if (!ether_addr_equal(sta_addr, wpa_s->own_addr)) {
5259 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
5260 			" in FT Action Response", MAC2STR(sta_addr));
5261 		return;
5262 	}
5263 
5264 	if (status) {
5265 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
5266 			"failure (status code %d)", status);
5267 		/* TODO: report error to FT code(?) */
5268 		return;
5269 	}
5270 
5271 	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
5272 				    len - (1 + 2 * ETH_ALEN + 2), 1,
5273 				    target_ap_addr, NULL, 0) < 0)
5274 		return;
5275 
5276 #ifdef CONFIG_SME
5277 	{
5278 		struct wpa_bss *bss;
5279 		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
5280 		if (bss)
5281 			wpa_s->sme.freq = bss->freq;
5282 		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
5283 		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
5284 			      WLAN_AUTH_FT);
5285 	}
5286 #endif /* CONFIG_SME */
5287 }
5288 #endif /* CONFIG_IEEE80211R */
5289 
5290 
wpa_supplicant_event_unprot_deauth(struct wpa_supplicant * wpa_s,struct unprot_deauth * e)5291 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
5292 					       struct unprot_deauth *e)
5293 {
5294 	wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
5295 		   "dropped: " MACSTR " -> " MACSTR
5296 		   " (reason code %u)",
5297 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
5298 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
5299 }
5300 
5301 
wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant * wpa_s,struct unprot_disassoc * e)5302 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
5303 						 struct unprot_disassoc *e)
5304 {
5305 	wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
5306 		   "dropped: " MACSTR " -> " MACSTR
5307 		   " (reason code %u)",
5308 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
5309 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
5310 }
5311 
5312 
wpas_event_disconnect(struct wpa_supplicant * wpa_s,const u8 * addr,u16 reason_code,int locally_generated,const u8 * ie,size_t ie_len,int deauth)5313 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
5314 				  u16 reason_code, int locally_generated,
5315 				  const u8 *ie, size_t ie_len, int deauth)
5316 {
5317 #ifdef CONFIG_AP
5318 	if (wpa_s->ap_iface && addr) {
5319 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
5320 		return;
5321 	}
5322 
5323 	if (wpa_s->ap_iface) {
5324 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
5325 		return;
5326 	}
5327 #endif /* CONFIG_AP */
5328 
5329 	if (!locally_generated)
5330 		wpa_s->own_disconnect_req = 0;
5331 
5332 	wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
5333 
5334 	if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
5335 	      ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
5336 		(wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
5337 	       eapol_sm_failed(wpa_s->eapol))) &&
5338 	     !wpa_s->eap_expected_failure))
5339 		wpas_auth_failed(wpa_s, "AUTH_FAILED", addr);
5340 
5341 #ifdef CONFIG_P2P
5342 	if (deauth && reason_code > 0) {
5343 		if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
5344 					  locally_generated) > 0) {
5345 			/*
5346 			 * The interface was removed, so cannot continue
5347 			 * processing any additional operations after this.
5348 			 */
5349 			return;
5350 		}
5351 	}
5352 #endif /* CONFIG_P2P */
5353 
5354 	wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
5355 					     locally_generated);
5356 }
5357 
5358 
wpas_event_disassoc(struct wpa_supplicant * wpa_s,struct disassoc_info * info)5359 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
5360 				struct disassoc_info *info)
5361 {
5362 	u16 reason_code = 0;
5363 	int locally_generated = 0;
5364 	const u8 *addr = NULL;
5365 	const u8 *ie = NULL;
5366 	size_t ie_len = 0;
5367 
5368 	wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
5369 
5370 	if (info) {
5371 		addr = info->addr;
5372 		ie = info->ie;
5373 		ie_len = info->ie_len;
5374 		reason_code = info->reason_code;
5375 		locally_generated = info->locally_generated;
5376 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u (%s)%s", reason_code,
5377 			reason2str(reason_code),
5378 			locally_generated ? " locally_generated=1" : "");
5379 		if (addr)
5380 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
5381 				MAC2STR(addr));
5382 		wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
5383 			    ie, ie_len);
5384 	}
5385 
5386 #ifdef CONFIG_AP
5387 	if (wpa_s->ap_iface && info && info->addr) {
5388 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
5389 		return;
5390 	}
5391 
5392 	if (wpa_s->ap_iface) {
5393 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
5394 		return;
5395 	}
5396 #endif /* CONFIG_AP */
5397 
5398 #ifdef CONFIG_P2P
5399 	if (info) {
5400 		wpas_p2p_disassoc_notif(
5401 			wpa_s, info->addr, reason_code, info->ie, info->ie_len,
5402 			locally_generated);
5403 	}
5404 #endif /* CONFIG_P2P */
5405 
5406 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
5407 		sme_event_disassoc(wpa_s, info);
5408 
5409 	wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
5410 			      ie, ie_len, 0);
5411 }
5412 
5413 
wpas_event_deauth(struct wpa_supplicant * wpa_s,struct deauth_info * info)5414 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
5415 			      struct deauth_info *info)
5416 {
5417 	u16 reason_code = 0;
5418 	int locally_generated = 0;
5419 	const u8 *addr = NULL;
5420 	const u8 *ie = NULL;
5421 	size_t ie_len = 0;
5422 
5423 	wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
5424 
5425 	if (info) {
5426 		addr = info->addr;
5427 		ie = info->ie;
5428 		ie_len = info->ie_len;
5429 		reason_code = info->reason_code;
5430 		locally_generated = info->locally_generated;
5431 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u (%s)%s",
5432 			reason_code, reason2str(reason_code),
5433 			locally_generated ? " locally_generated=1" : "");
5434 		if (addr) {
5435 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
5436 				MAC2STR(addr));
5437 		}
5438 		wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
5439 			    ie, ie_len);
5440 	}
5441 
5442 	wpa_reset_ft_completed(wpa_s->wpa);
5443 
5444 	wpas_event_disconnect(wpa_s, addr, reason_code,
5445 			      locally_generated, ie, ie_len, 1);
5446 }
5447 
5448 
reg_init_str(enum reg_change_initiator init)5449 static const char * reg_init_str(enum reg_change_initiator init)
5450 {
5451 	switch (init) {
5452 	case REGDOM_SET_BY_CORE:
5453 		return "CORE";
5454 	case REGDOM_SET_BY_USER:
5455 		return "USER";
5456 	case REGDOM_SET_BY_DRIVER:
5457 		return "DRIVER";
5458 	case REGDOM_SET_BY_COUNTRY_IE:
5459 		return "COUNTRY_IE";
5460 	case REGDOM_BEACON_HINT:
5461 		return "BEACON_HINT";
5462 	}
5463 	return "?";
5464 }
5465 
5466 
reg_type_str(enum reg_type type)5467 static const char * reg_type_str(enum reg_type type)
5468 {
5469 	switch (type) {
5470 	case REGDOM_TYPE_UNKNOWN:
5471 		return "UNKNOWN";
5472 	case REGDOM_TYPE_COUNTRY:
5473 		return "COUNTRY";
5474 	case REGDOM_TYPE_WORLD:
5475 		return "WORLD";
5476 	case REGDOM_TYPE_CUSTOM_WORLD:
5477 		return "CUSTOM_WORLD";
5478 	case REGDOM_TYPE_INTERSECTION:
5479 		return "INTERSECTION";
5480 	}
5481 	return "?";
5482 }
5483 
5484 
wpas_beacon_hint(struct wpa_supplicant * wpa_s,const char * title,struct frequency_attrs * attrs)5485 static void wpas_beacon_hint(struct wpa_supplicant *wpa_s, const char *title,
5486 			     struct frequency_attrs *attrs)
5487 {
5488 	if (!attrs->freq)
5489 		return;
5490 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_BEACON_HINT
5491 		"%s freq=%u max_tx_power=%u%s%s%s",
5492 		title, attrs->freq, attrs->max_tx_power,
5493 		attrs->disabled ? " disabled=1" : "",
5494 		attrs->no_ir ? " no_ir=1" : "",
5495 		attrs->radar ? " radar=1" : "");
5496 }
5497 
5498 
wpa_supplicant_update_channel_list(struct wpa_supplicant * wpa_s,struct channel_list_changed * info)5499 void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s,
5500 					struct channel_list_changed *info)
5501 {
5502 	struct wpa_supplicant *ifs;
5503 	u8 dfs_domain;
5504 
5505 	/*
5506 	 * To allow backwards compatibility with higher level layers that
5507 	 * assumed the REGDOM_CHANGE event is sent over the initially added
5508 	 * interface. Find the highest parent of this interface and use it to
5509 	 * send the event.
5510 	 */
5511 	for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
5512 		;
5513 
5514 	if (info) {
5515 		wpa_msg(ifs, MSG_INFO,
5516 			WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
5517 			reg_init_str(info->initiator), reg_type_str(info->type),
5518 			info->alpha2[0] ? " alpha2=" : "",
5519 			info->alpha2[0] ? info->alpha2 : "");
5520 
5521 		if (info->initiator == REGDOM_BEACON_HINT) {
5522 			wpas_beacon_hint(ifs, "before",
5523 					 &info->beacon_hint_before);
5524 			wpas_beacon_hint(ifs, "after",
5525 					 &info->beacon_hint_after);
5526 		}
5527 	}
5528 
5529 	if (wpa_s->drv_priv == NULL)
5530 		return; /* Ignore event during drv initialization */
5531 
5532 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
5533 			 radio_list) {
5534 		bool was_6ghz_enabled;
5535 
5536 		wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
5537 			   ifs->ifname);
5538 		free_hw_features(ifs);
5539 		ifs->hw.modes = wpa_drv_get_hw_feature_data(
5540 			ifs, &ifs->hw.num_modes, &ifs->hw.flags, &dfs_domain);
5541 
5542 		was_6ghz_enabled = ifs->is_6ghz_enabled;
5543 		ifs->is_6ghz_enabled = wpas_is_6ghz_supported(ifs, true);
5544 
5545 		/* Restart PNO/sched_scan with updated channel list */
5546 		if (ifs->pno) {
5547 			wpas_stop_pno(ifs);
5548 			wpas_start_pno(ifs);
5549 		} else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
5550 			wpa_dbg(ifs, MSG_DEBUG,
5551 				"Channel list changed - restart sched_scan");
5552 			wpas_scan_restart_sched_scan(ifs);
5553 		} else if (!was_6ghz_enabled && ifs->is_6ghz_enabled) {
5554 			wpa_dbg(ifs, MSG_INFO,
5555 				"Channel list changed: 6 GHz was enabled");
5556 
5557 			ifs->crossed_6ghz_dom = true;
5558 		}
5559 	}
5560 
5561 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
5562 }
5563 
5564 
wpas_event_rx_mgmt_action(struct wpa_supplicant * wpa_s,const u8 * frame,size_t len,int freq,int rssi)5565 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
5566 				      const u8 *frame, size_t len, int freq,
5567 				      int rssi)
5568 {
5569 	const struct ieee80211_mgmt *mgmt;
5570 	const u8 *payload;
5571 	size_t plen;
5572 	u8 category;
5573 
5574 	if (len < IEEE80211_HDRLEN + 2)
5575 		return;
5576 
5577 	mgmt = (const struct ieee80211_mgmt *) frame;
5578 	payload = frame + IEEE80211_HDRLEN;
5579 	category = *payload++;
5580 	plen = len - IEEE80211_HDRLEN - 1;
5581 
5582 	wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
5583 		" Category=%u DataLen=%d freq=%d MHz",
5584 		MAC2STR(mgmt->sa), category, (int) plen, freq);
5585 
5586 #ifndef CONFIG_NO_WMM_AC
5587 	if (category == WLAN_ACTION_WMM) {
5588 		wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
5589 		return;
5590 	}
5591 #endif /* CONFIG_NO_WMM_AC */
5592 
5593 #ifdef CONFIG_IEEE80211R
5594 	if (category == WLAN_ACTION_FT) {
5595 		ft_rx_action(wpa_s, payload, plen);
5596 		return;
5597 	}
5598 #endif /* CONFIG_IEEE80211R */
5599 
5600 #ifdef CONFIG_SME
5601 	if (category == WLAN_ACTION_SA_QUERY) {
5602 		sme_sa_query_rx(wpa_s, mgmt->da, mgmt->sa, payload, plen);
5603 		return;
5604 	}
5605 #endif /* CONFIG_SME */
5606 
5607 #ifdef CONFIG_WNM
5608 	if (mgmt->u.action.category == WLAN_ACTION_WNM) {
5609 		ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
5610 		return;
5611 	}
5612 #endif /* CONFIG_WNM */
5613 
5614 #ifdef CONFIG_GAS
5615 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
5616 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
5617 	    gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
5618 			 mgmt->u.action.category,
5619 			 payload, plen, freq) == 0)
5620 		return;
5621 #endif /* CONFIG_GAS */
5622 
5623 #ifdef CONFIG_GAS_SERVER
5624 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
5625 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
5626 	    gas_server_rx(wpa_s->gas_server, mgmt->da, mgmt->sa, mgmt->bssid,
5627 			  mgmt->u.action.category,
5628 			  payload, plen, freq) == 0)
5629 		return;
5630 #endif /* CONFIG_GAS_SERVER */
5631 
5632 #ifdef CONFIG_TDLS
5633 	if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
5634 	    payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
5635 		wpa_dbg(wpa_s, MSG_DEBUG,
5636 			"TDLS: Received Discovery Response from " MACSTR,
5637 			MAC2STR(mgmt->sa));
5638 		if (wpa_s->valid_links &&
5639 		    wpa_tdls_process_discovery_response(wpa_s->wpa, mgmt->sa,
5640 							&payload[1], plen - 1))
5641 			wpa_dbg(wpa_s, MSG_ERROR,
5642 				"TDLS: Discovery Response process failed for "
5643 				MACSTR, MAC2STR(mgmt->sa));
5644 		return;
5645 	}
5646 #endif /* CONFIG_TDLS */
5647 
5648 #ifdef CONFIG_INTERWORKING
5649 	if (category == WLAN_ACTION_QOS && plen >= 1 &&
5650 	    payload[0] == QOS_QOS_MAP_CONFIG) {
5651 		const u8 *pos = payload + 1;
5652 		size_t qlen = plen - 1;
5653 		wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
5654 			MACSTR, MAC2STR(mgmt->sa));
5655 		if (ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
5656 		    qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
5657 		    pos[1] <= qlen - 2 && pos[1] >= 16)
5658 			wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
5659 		return;
5660 	}
5661 #endif /* CONFIG_INTERWORKING */
5662 
5663 #ifndef CONFIG_NO_RRM
5664 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
5665 	    payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
5666 		wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
5667 							  mgmt->da,
5668 							  payload + 1,
5669 							  plen - 1);
5670 		return;
5671 	}
5672 
5673 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
5674 	    payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
5675 		wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
5676 		return;
5677 	}
5678 
5679 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
5680 	    payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
5681 		wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
5682 							 payload + 1, plen - 1,
5683 							 rssi);
5684 		return;
5685 	}
5686 #endif /* CONFIG_NO_RRM */
5687 
5688 #ifdef CONFIG_FST
5689 	if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
5690 		fst_rx_action(wpa_s->fst, mgmt, len);
5691 		return;
5692 	}
5693 #endif /* CONFIG_FST */
5694 
5695 #ifdef CONFIG_NAN_USD
5696 	if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
5697 	    payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
5698 	    WPA_GET_BE32(&payload[1]) == NAN_SDF_VENDOR_TYPE) {
5699 		payload += 5;
5700 		plen -= 5;
5701 		wpas_nan_usd_rx_sdf(wpa_s, mgmt->sa, freq, payload, plen);
5702 		return;
5703 	}
5704 #endif /* CONFIG_NAN_USD */
5705 
5706 #ifdef CONFIG_DPP
5707 	if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
5708 	    payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
5709 	    WPA_GET_BE24(&payload[1]) == OUI_WFA &&
5710 	    payload[4] == DPP_OUI_TYPE) {
5711 		payload++;
5712 		plen--;
5713 		wpas_dpp_rx_action(wpa_s, mgmt->sa, payload, plen, freq);
5714 		return;
5715 	}
5716 #endif /* CONFIG_DPP */
5717 
5718 #ifndef CONFIG_NO_ROBUST_AV
5719 	if (category == WLAN_ACTION_ROBUST_AV_STREAMING &&
5720 	    payload[0] == ROBUST_AV_SCS_RESP) {
5721 		wpas_handle_robust_av_scs_recv_action(wpa_s, mgmt->sa,
5722 						      payload + 1, plen - 1);
5723 		return;
5724 	}
5725 
5726 	if (category == WLAN_ACTION_ROBUST_AV_STREAMING &&
5727 	    payload[0] == ROBUST_AV_MSCS_RESP) {
5728 		wpas_handle_robust_av_recv_action(wpa_s, mgmt->sa,
5729 						  payload + 1, plen - 1);
5730 		return;
5731 	}
5732 
5733 	if (category == WLAN_ACTION_VENDOR_SPECIFIC_PROTECTED && plen > 4 &&
5734 	    WPA_GET_BE32(payload) == QM_ACTION_VENDOR_TYPE) {
5735 		wpas_handle_qos_mgmt_recv_action(wpa_s, mgmt->sa,
5736 						 payload + 4, plen - 4);
5737 		return;
5738 	}
5739 #endif /* CONFIG_NO_ROBUST_AV */
5740 
5741 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
5742 			   category, payload, plen, freq);
5743 	if (wpa_s->ifmsh)
5744 		mesh_mpm_action_rx(wpa_s, mgmt, len);
5745 }
5746 
5747 
wpa_supplicant_notify_avoid_freq(struct wpa_supplicant * wpa_s,union wpa_event_data * event)5748 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
5749 					     union wpa_event_data *event)
5750 {
5751 	struct wpa_freq_range_list *list;
5752 	char *str = NULL;
5753 
5754 	list = &event->freq_range;
5755 
5756 	if (list->num)
5757 		str = freq_range_list_str(list);
5758 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
5759 		str ? str : "");
5760 
5761 #ifdef CONFIG_P2P
5762 	if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
5763 		wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
5764 			__func__);
5765 	} else {
5766 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
5767 
5768 		/*
5769 		 * The update channel flow will also take care of moving a GO
5770 		 * from the unsafe frequency if needed.
5771 		 */
5772 		wpas_p2p_update_channel_list(wpa_s,
5773 					     WPAS_P2P_CHANNEL_UPDATE_AVOID);
5774 	}
5775 #endif /* CONFIG_P2P */
5776 
5777 	os_free(str);
5778 }
5779 
5780 
wpa_supplicant_event_port_authorized(struct wpa_supplicant * wpa_s)5781 static void wpa_supplicant_event_port_authorized(struct wpa_supplicant *wpa_s)
5782 {
5783 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
5784 		wpa_supplicant_cancel_auth_timeout(wpa_s);
5785 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
5786 		eapol_sm_notify_portValid(wpa_s->eapol, true);
5787 		eapol_sm_notify_eap_success(wpa_s->eapol, true);
5788 		wpa_s->drv_authorized_port = 1;
5789 	}
5790 }
5791 
5792 
wpas_event_cac_ms(const struct wpa_supplicant * wpa_s,int freq)5793 static unsigned int wpas_event_cac_ms(const struct wpa_supplicant *wpa_s,
5794 				      int freq)
5795 {
5796 	size_t i;
5797 	int j;
5798 
5799 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
5800 		const struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
5801 
5802 		for (j = 0; j < mode->num_channels; j++) {
5803 			const struct hostapd_channel_data *chan;
5804 
5805 			chan = &mode->channels[j];
5806 			if (chan->freq == freq)
5807 				return chan->dfs_cac_ms;
5808 		}
5809 	}
5810 
5811 	return 0;
5812 }
5813 
5814 
wpas_event_dfs_cac_started(struct wpa_supplicant * wpa_s,struct dfs_event * radar)5815 static void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
5816 				       struct dfs_event *radar)
5817 {
5818 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
5819 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
5820 		wpas_ap_event_dfs_cac_started(wpa_s, radar);
5821 	} else
5822 #endif /* NEED_AP_MLME && CONFIG_AP */
5823 	{
5824 		unsigned int cac_time = wpas_event_cac_ms(wpa_s, radar->freq);
5825 
5826 		cac_time /= 1000; /* convert from ms to sec */
5827 		if (!cac_time)
5828 			cac_time = 10 * 60; /* max timeout: 10 minutes */
5829 
5830 		/* Restart auth timeout: CAC time added to initial timeout */
5831 		wpas_auth_timeout_restart(wpa_s, cac_time);
5832 	}
5833 }
5834 
5835 
wpas_event_dfs_cac_finished(struct wpa_supplicant * wpa_s,struct dfs_event * radar)5836 static void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
5837 					struct dfs_event *radar)
5838 {
5839 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
5840 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
5841 		wpas_ap_event_dfs_cac_finished(wpa_s, radar);
5842 	} else
5843 #endif /* NEED_AP_MLME && CONFIG_AP */
5844 	{
5845 		/* Restart auth timeout with original value after CAC is
5846 		 * finished */
5847 		wpas_auth_timeout_restart(wpa_s, 0);
5848 	}
5849 }
5850 
5851 
wpas_event_dfs_cac_aborted(struct wpa_supplicant * wpa_s,struct dfs_event * radar)5852 static void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
5853 				       struct dfs_event *radar)
5854 {
5855 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
5856 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
5857 		wpas_ap_event_dfs_cac_aborted(wpa_s, radar);
5858 	} else
5859 #endif /* NEED_AP_MLME && CONFIG_AP */
5860 	{
5861 		/* Restart auth timeout with original value after CAC is
5862 		 * aborted */
5863 		wpas_auth_timeout_restart(wpa_s, 0);
5864 	}
5865 }
5866 
5867 
wpa_supplicant_event_assoc_auth(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5868 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
5869 					    union wpa_event_data *data)
5870 {
5871 	wpa_dbg(wpa_s, MSG_DEBUG,
5872 		"Connection authorized by device, previous state %d",
5873 		wpa_s->wpa_state);
5874 
5875 	wpa_supplicant_event_port_authorized(wpa_s);
5876 
5877 	wpa_s->last_eapol_matches_bssid = 1;
5878 
5879 	wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
5880 	wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
5881 			       data->assoc_info.ptk_kck_len,
5882 			       data->assoc_info.ptk_kek,
5883 			       data->assoc_info.ptk_kek_len);
5884 #ifdef CONFIG_FILS
5885 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
5886 		struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
5887 		const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
5888 
5889 		/* Update ERP next sequence number */
5890 		eapol_sm_update_erp_next_seq_num(
5891 			wpa_s->eapol, data->assoc_info.fils_erp_next_seq_num);
5892 
5893 		if (data->assoc_info.fils_pmk && data->assoc_info.fils_pmkid) {
5894 			/* Add the new PMK and PMKID to the PMKSA cache */
5895 			wpa_sm_pmksa_cache_add(wpa_s->wpa,
5896 					       data->assoc_info.fils_pmk,
5897 					       data->assoc_info.fils_pmk_len,
5898 					       data->assoc_info.fils_pmkid,
5899 					       wpa_s->valid_links ?
5900 					       wpa_s->ap_mld_addr :
5901 					       wpa_s->bssid,
5902 					       fils_cache_id);
5903 		} else if (data->assoc_info.fils_pmkid) {
5904 			/* Update the current PMKSA used for this connection */
5905 			pmksa_cache_set_current(wpa_s->wpa,
5906 						data->assoc_info.fils_pmkid,
5907 						NULL, NULL, 0, NULL, 0, true);
5908 		}
5909 	}
5910 #endif /* CONFIG_FILS */
5911 }
5912 
5913 
connect_fail_reason(enum sta_connect_fail_reason_codes code)5914 static const char * connect_fail_reason(enum sta_connect_fail_reason_codes code)
5915 {
5916 	switch (code) {
5917 	case STA_CONNECT_FAIL_REASON_UNSPECIFIED:
5918 		return "";
5919 	case STA_CONNECT_FAIL_REASON_NO_BSS_FOUND:
5920 		return "no_bss_found";
5921 	case STA_CONNECT_FAIL_REASON_AUTH_TX_FAIL:
5922 		return "auth_tx_fail";
5923 	case STA_CONNECT_FAIL_REASON_AUTH_NO_ACK_RECEIVED:
5924 		return "auth_no_ack_received";
5925 	case STA_CONNECT_FAIL_REASON_AUTH_NO_RESP_RECEIVED:
5926 		return "auth_no_resp_received";
5927 	case STA_CONNECT_FAIL_REASON_ASSOC_REQ_TX_FAIL:
5928 		return "assoc_req_tx_fail";
5929 	case STA_CONNECT_FAIL_REASON_ASSOC_NO_ACK_RECEIVED:
5930 		return "assoc_no_ack_received";
5931 	case STA_CONNECT_FAIL_REASON_ASSOC_NO_RESP_RECEIVED:
5932 		return "assoc_no_resp_received";
5933 	default:
5934 		return "unknown_reason";
5935 	}
5936 }
5937 
5938 
wpas_event_assoc_reject(struct wpa_supplicant * wpa_s,union wpa_event_data * data)5939 static void wpas_event_assoc_reject(struct wpa_supplicant *wpa_s,
5940 				    union wpa_event_data *data)
5941 {
5942 	const u8 *bssid = data->assoc_reject.bssid;
5943 	struct ieee802_11_elems elems;
5944 	struct ml_sta_link_info ml_info[MAX_NUM_MLD_LINKS];
5945 	const u8 *link_bssids[MAX_NUM_MLD_LINKS + 1];
5946 #ifdef CONFIG_MBO
5947 	struct wpa_bss *reject_bss;
5948 #endif /* CONFIG_MBO */
5949 
5950 	if (!bssid || is_zero_ether_addr(bssid))
5951 		bssid = wpa_s->pending_bssid;
5952 #ifdef CONFIG_MBO
5953 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
5954 		reject_bss = wpa_s->current_bss;
5955 	else
5956 		reject_bss = wpa_bss_get_bssid(wpa_s, bssid);
5957 #endif /* CONFIG_MBO */
5958 
5959 	if (data->assoc_reject.bssid)
5960 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
5961 			"bssid=" MACSTR	" status_code=%u%s%s%s%s%s",
5962 			MAC2STR(data->assoc_reject.bssid),
5963 			data->assoc_reject.status_code,
5964 			data->assoc_reject.timed_out ? " timeout" : "",
5965 			data->assoc_reject.timeout_reason ? "=" : "",
5966 			data->assoc_reject.timeout_reason ?
5967 			data->assoc_reject.timeout_reason : "",
5968 			data->assoc_reject.reason_code !=
5969 			STA_CONNECT_FAIL_REASON_UNSPECIFIED ?
5970 			" qca_driver_reason=" : "",
5971 			connect_fail_reason(data->assoc_reject.reason_code));
5972 	else
5973 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
5974 			"status_code=%u%s%s%s%s%s",
5975 			data->assoc_reject.status_code,
5976 			data->assoc_reject.timed_out ? " timeout" : "",
5977 			data->assoc_reject.timeout_reason ? "=" : "",
5978 			data->assoc_reject.timeout_reason ?
5979 			data->assoc_reject.timeout_reason : "",
5980 			data->assoc_reject.reason_code !=
5981 			STA_CONNECT_FAIL_REASON_UNSPECIFIED ?
5982 			" qca_driver_reason=" : "",
5983 			connect_fail_reason(data->assoc_reject.reason_code));
5984 	wpa_s->assoc_status_code = data->assoc_reject.status_code;
5985 	wpas_notify_assoc_status_code(wpa_s, bssid, data->assoc_reject.timed_out,
5986 				    data->assoc_reject.resp_ies, data->assoc_reject.resp_ies_len);
5987 
5988 #ifdef CONFIG_OWE
5989 	if (data->assoc_reject.status_code ==
5990 	    WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
5991 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
5992 	    wpa_s->current_ssid &&
5993 	    wpa_s->current_ssid->owe_group == 0 &&
5994 	    wpa_s->last_owe_group != 21) {
5995 		struct wpa_ssid *ssid = wpa_s->current_ssid;
5996 		struct wpa_bss *bss = wpa_s->current_bss;
5997 
5998 		if (!bss) {
5999 			bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
6000 			if (!bss) {
6001 				wpas_connection_failed(wpa_s, bssid, NULL);
6002 				wpa_supplicant_mark_disassoc(wpa_s);
6003 				return;
6004 			}
6005 		}
6006 		wpa_printf(MSG_DEBUG, "OWE: Try next supported DH group");
6007 		wpas_connect_work_done(wpa_s);
6008 		wpa_supplicant_mark_disassoc(wpa_s);
6009 		wpa_supplicant_connect(wpa_s, bss, ssid);
6010 		return;
6011 	}
6012 #endif /* CONFIG_OWE */
6013 
6014 #ifdef CONFIG_DPP2
6015 	/* Try to follow AP's PFS policy. WLAN_STATUS_ASSOC_DENIED_UNSPEC is
6016 	 * the status code defined in the DPP R2 tech spec.
6017 	 * WLAN_STATUS_AKMP_NOT_VALID is addressed in the same manner as an
6018 	 * interoperability workaround with older hostapd implementation. */
6019 	if (DPP_VERSION > 1 && wpa_s->current_ssid &&
6020 	    (wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_DPP ||
6021 	     ((wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
6022 	      wpa_s->key_mgmt == WPA_KEY_MGMT_DPP)) &&
6023 	    wpa_s->current_ssid->dpp_pfs == 0 &&
6024 	    (data->assoc_reject.status_code ==
6025 	     WLAN_STATUS_ASSOC_DENIED_UNSPEC ||
6026 	     data->assoc_reject.status_code == WLAN_STATUS_AKMP_NOT_VALID)) {
6027 		struct wpa_ssid *ssid = wpa_s->current_ssid;
6028 		struct wpa_bss *bss = wpa_s->current_bss;
6029 
6030 		wpa_s->current_ssid->dpp_pfs_fallback ^= 1;
6031 		if (!bss)
6032 			bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
6033 		if (!bss || wpa_s->dpp_pfs_fallback) {
6034 			wpa_printf(MSG_DEBUG,
6035 				   "DPP: Updated PFS policy for next try");
6036 			wpas_connection_failed(wpa_s, bssid, NULL);
6037 			wpa_supplicant_mark_disassoc(wpa_s);
6038 			return;
6039 		}
6040 		wpa_printf(MSG_DEBUG, "DPP: Try again with updated PFS policy");
6041 		wpa_s->dpp_pfs_fallback = 1;
6042 		wpas_connect_work_done(wpa_s);
6043 		wpa_supplicant_mark_disassoc(wpa_s);
6044 		wpa_supplicant_connect(wpa_s, bss, ssid);
6045 		return;
6046 	}
6047 #endif /* CONFIG_DPP2 */
6048 
6049 #ifdef CONFIG_MBO
6050 	if (data->assoc_reject.status_code ==
6051 	    WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
6052 	    reject_bss && data->assoc_reject.resp_ies) {
6053 		const u8 *rssi_rej;
6054 
6055 		rssi_rej = mbo_get_attr_from_ies(
6056 			data->assoc_reject.resp_ies,
6057 			data->assoc_reject.resp_ies_len,
6058 			OCE_ATTR_ID_RSSI_BASED_ASSOC_REJECT);
6059 		if (rssi_rej && rssi_rej[1] == 2) {
6060 			wpa_printf(MSG_DEBUG,
6061 				   "OCE: RSSI-based association rejection from "
6062 				   MACSTR " (Delta RSSI: %u, Retry Delay: %u)",
6063 				   MAC2STR(reject_bss->bssid),
6064 				   rssi_rej[2], rssi_rej[3]);
6065 			wpa_bss_tmp_disallow(wpa_s,
6066 					     reject_bss->bssid,
6067 					     rssi_rej[3],
6068 					     rssi_rej[2] + reject_bss->level);
6069 		}
6070 	}
6071 #endif /* CONFIG_MBO */
6072 
6073 	/* Check for other failed links in the response */
6074 	os_memset(link_bssids, 0, sizeof(link_bssids));
6075 	if (ieee802_11_parse_elems(data->assoc_reject.resp_ies,
6076 				   data->assoc_reject.resp_ies_len,
6077 				   &elems, 1) != ParseFailed) {
6078 		unsigned int n_links, i, idx;
6079 
6080 		idx = 0;
6081 		n_links = wpas_ml_parse_assoc(wpa_s, &elems, ml_info);
6082 
6083 		for (i = 1; i < n_links; i++) {
6084 			/* The status cannot be success here.
6085 			 * Add the link to the failed list if it is reporting
6086 			 * an error. The only valid "non-error" status is
6087 			 * TX_LINK_NOT_ACCEPTED as that means this link may
6088 			 * still accept an association from us.
6089 			 */
6090 			if (ml_info[i].status !=
6091 			    WLAN_STATUS_DENIED_TX_LINK_NOT_ACCEPTED) {
6092 				link_bssids[idx] = ml_info[i].bssid;
6093 				idx++;
6094 			}
6095 		}
6096 	}
6097 
6098 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
6099 		sme_event_assoc_reject(wpa_s, data, link_bssids);
6100 		return;
6101 	}
6102 
6103 	/* Driver-based SME cases */
6104 
6105 #ifdef CONFIG_SAE
6106 	if (wpa_s->current_ssid &&
6107 	    wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt) &&
6108 	    !data->assoc_reject.timed_out) {
6109 		wpa_dbg(wpa_s, MSG_DEBUG, "SAE: Drop PMKSA cache entry");
6110 		wpa_sm_aborted_cached(wpa_s->wpa);
6111 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
6112 	}
6113 #endif /* CONFIG_SAE */
6114 
6115 #ifdef CONFIG_DPP
6116 	if (wpa_s->current_ssid &&
6117 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_DPP &&
6118 	    !data->assoc_reject.timed_out) {
6119 		wpa_dbg(wpa_s, MSG_DEBUG, "DPP: Drop PMKSA cache entry");
6120 		wpa_sm_aborted_cached(wpa_s->wpa);
6121 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
6122 	}
6123 #endif /* CONFIG_DPP */
6124 
6125 #ifdef CONFIG_FILS
6126 	/* Update ERP next sequence number */
6127 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
6128 		fils_pmksa_cache_flush(wpa_s);
6129 		eapol_sm_update_erp_next_seq_num(
6130 			wpa_s->eapol,
6131 			data->assoc_reject.fils_erp_next_seq_num);
6132 		fils_connection_failure(wpa_s);
6133 	}
6134 #endif /* CONFIG_FILS */
6135 
6136 	wpas_connection_failed(wpa_s, bssid, link_bssids);
6137 	wpa_supplicant_mark_disassoc(wpa_s);
6138 }
6139 
6140 
wpas_event_unprot_beacon(struct wpa_supplicant * wpa_s,struct unprot_beacon * data)6141 static void wpas_event_unprot_beacon(struct wpa_supplicant *wpa_s,
6142 				     struct unprot_beacon *data)
6143 {
6144 	struct wpabuf *buf;
6145 	int res;
6146 
6147 	if (!data || wpa_s->wpa_state != WPA_COMPLETED ||
6148 	    !ether_addr_equal(data->sa, wpa_s->bssid))
6149 		return;
6150 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_UNPROT_BEACON MACSTR,
6151 		MAC2STR(data->sa));
6152 
6153 	buf = wpabuf_alloc(4);
6154 	if (!buf)
6155 		return;
6156 
6157 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
6158 	wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
6159 	wpabuf_put_u8(buf, 1); /* Dialog Token */
6160 	wpabuf_put_u8(buf, WNM_NOTIF_TYPE_BEACON_PROTECTION_FAILURE);
6161 
6162 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
6163 				  wpa_s->own_addr, wpa_s->bssid,
6164 				  wpabuf_head(buf), wpabuf_len(buf), 0);
6165 	if (res < 0)
6166 		wpa_printf(MSG_DEBUG,
6167 			   "Failed to send WNM-Notification Request frame");
6168 
6169 	wpabuf_free(buf);
6170 }
6171 
6172 
bitmap_to_str(u8 value,char * buf)6173 static const char * bitmap_to_str(u8 value, char *buf)
6174 {
6175 	char *pos = buf;
6176 	int i, k = 0;
6177 
6178 	for (i = 7; i >= 0; i--)
6179 		pos[k++] = (value & BIT(i)) ? '1' : '0';
6180 
6181 	pos[8] = '\0';
6182 	return pos;
6183 }
6184 
6185 
wpas_tid_link_map(struct wpa_supplicant * wpa_s,struct tid_link_map_info * info)6186 static void wpas_tid_link_map(struct wpa_supplicant *wpa_s,
6187 			      struct tid_link_map_info *info)
6188 {
6189 	char map_info[1000], *pos, *end;
6190 	int res, i;
6191 
6192 	pos = map_info;
6193 	end = pos + sizeof(map_info);
6194 	res = os_snprintf(map_info, sizeof(map_info), "default=%d",
6195 			  info->default_map);
6196 	if (os_snprintf_error(end - pos, res))
6197 		return;
6198 	pos += res;
6199 
6200 	if (!info->default_map) {
6201 		for_each_link(info->valid_links, i) {
6202 			char uplink_map_str[9];
6203 			char downlink_map_str[9];
6204 
6205 			bitmap_to_str(info->t2lmap[i].uplink, uplink_map_str);
6206 			bitmap_to_str(info->t2lmap[i].downlink,
6207 				      downlink_map_str);
6208 
6209 			res = os_snprintf(pos, end - pos,
6210 					  " link_id=%d up_link=%s down_link=%s",
6211 					  i, uplink_map_str,
6212 					  downlink_map_str);
6213 			if (os_snprintf_error(end - pos, res))
6214 				return;
6215 			pos += res;
6216 		}
6217 	}
6218 
6219 	wpas_notify_mlo_info_change_reason(wpa_s, MLO_TID_TO_LINK_MAP);
6220 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_T2LM_UPDATE "%s", map_info);
6221 }
6222 
6223 
wpas_link_reconfig(struct wpa_supplicant * wpa_s)6224 static void wpas_link_reconfig(struct wpa_supplicant *wpa_s)
6225 {
6226 	u8 bssid[ETH_ALEN];
6227 
6228 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
6229 		wpa_printf(MSG_ERROR, "LINK_RECONFIG: Failed to get BSSID");
6230 		wpa_supplicant_deauthenticate(wpa_s,
6231 					      WLAN_REASON_DEAUTH_LEAVING);
6232 		return;
6233 	}
6234 
6235 	if (!ether_addr_equal(bssid, wpa_s->bssid)) {
6236 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
6237 		wpa_supplicant_update_current_bss(wpa_s, wpa_s->bssid);
6238 		wpas_notify_bssid_changed(wpa_s);
6239 	}
6240 
6241 	if (wpa_drv_get_mlo_info(wpa_s) < 0) {
6242 		wpa_printf(MSG_ERROR,
6243 			   "LINK_RECONFIG: Failed to get MLO connection info");
6244 		wpa_supplicant_deauthenticate(wpa_s,
6245 					      WLAN_REASON_DEAUTH_LEAVING);
6246 		return;
6247 	}
6248 
6249 	if (wpa_sm_set_ml_info(wpa_s)) {
6250 		wpa_printf(MSG_ERROR,
6251 			   "LINK_RECONFIG: Failed to set MLO connection info to wpa_sm");
6252 		wpa_supplicant_deauthenticate(wpa_s,
6253 					      WLAN_REASON_DEAUTH_LEAVING);
6254 		return;
6255 	}
6256 
6257 	wpas_notify_mlo_info_change_reason(wpa_s, MLO_LINK_RECONFIG_AP_REMOVAL);
6258 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_LINK_RECONFIG "valid_links=0x%x",
6259 		wpa_s->valid_links);
6260 }
6261 
6262 #ifdef MAINLINE_SUPPLICANT
is_event_allowlisted(enum wpa_event_type event)6263 static bool is_event_allowlisted(enum wpa_event_type event) {
6264 	return event == EVENT_SCAN_STARTED ||
6265 	       event == EVENT_SCAN_RESULTS ||
6266 	       event == EVENT_RX_MGMT ||
6267 	       event == EVENT_REMAIN_ON_CHANNEL ||
6268 	       event == EVENT_CANCEL_REMAIN_ON_CHANNEL ||
6269 	       event == EVENT_TX_WAIT_EXPIRE;
6270 }
6271 #endif /* MAINLINE_SUPPLICANT */
6272 
6273 
wpa_supplicant_event(void * ctx,enum wpa_event_type event,union wpa_event_data * data)6274 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
6275 			  union wpa_event_data *data)
6276 {
6277 	struct wpa_supplicant *wpa_s = ctx;
6278 	int resched;
6279 	struct os_reltime age, clear_at;
6280 #ifndef CONFIG_NO_STDOUT_DEBUG
6281 	int level = MSG_DEBUG;
6282 #endif /* CONFIG_NO_STDOUT_DEBUG */
6283 
6284 #ifdef MAINLINE_SUPPLICANT
6285 	if (!is_event_allowlisted(event)) {
6286 		wpa_dbg(wpa_s, MSG_DEBUG,
6287 			"Ignore event %s (%d) which is not allowlisted",
6288 			event_to_string(event), event);
6289 		return;
6290 	}
6291 #endif /* MAINLINE_SUPPLICANT */
6292 
6293 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
6294 	    event != EVENT_INTERFACE_ENABLED &&
6295 	    event != EVENT_INTERFACE_STATUS &&
6296 	    event != EVENT_SCAN_RESULTS &&
6297 	    event != EVENT_SCHED_SCAN_STOPPED) {
6298 		wpa_dbg(wpa_s, MSG_DEBUG,
6299 			"Ignore event %s (%d) while interface is disabled",
6300 			event_to_string(event), event);
6301 		return;
6302 	}
6303 
6304 #ifndef CONFIG_NO_STDOUT_DEBUG
6305 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
6306 		const struct ieee80211_hdr *hdr;
6307 		u16 fc;
6308 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
6309 		fc = le_to_host16(hdr->frame_control);
6310 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
6311 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
6312 			level = MSG_EXCESSIVE;
6313 	}
6314 
6315 	wpa_dbg(wpa_s, level, "Event %s (%d) received",
6316 		event_to_string(event), event);
6317 #endif /* CONFIG_NO_STDOUT_DEBUG */
6318 
6319 	switch (event) {
6320 	case EVENT_AUTH:
6321 #ifdef CONFIG_FST
6322 		if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
6323 					  data->auth.ies_len))
6324 			wpa_printf(MSG_DEBUG,
6325 				   "FST: MB IEs updated from auth IE");
6326 #endif /* CONFIG_FST */
6327 		sme_event_auth(wpa_s, data);
6328 		wpa_s->auth_status_code = data->auth.status_code;
6329 		wpas_notify_auth_status_code(wpa_s);
6330 		break;
6331 	case EVENT_ASSOC:
6332 #ifdef CONFIG_TESTING_OPTIONS
6333 		if (wpa_s->ignore_auth_resp) {
6334 			wpa_printf(MSG_INFO,
6335 				   "EVENT_ASSOC - ignore_auth_resp active!");
6336 			break;
6337 		}
6338 		if (wpa_s->testing_resend_assoc) {
6339 			wpa_printf(MSG_INFO,
6340 				   "EVENT_DEAUTH - testing_resend_assoc");
6341 			break;
6342 		}
6343 #endif /* CONFIG_TESTING_OPTIONS */
6344 		if (wpa_s->disconnected) {
6345 			wpa_printf(MSG_INFO,
6346 				   "Ignore unexpected EVENT_ASSOC in disconnected state");
6347 			break;
6348 		}
6349 		wpa_supplicant_event_assoc(wpa_s, data);
6350 		wpa_s->assoc_status_code = WLAN_STATUS_SUCCESS;
6351 		if (data &&
6352 		    (data->assoc_info.authorized ||
6353 		     (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
6354 		      wpa_fils_is_completed(wpa_s->wpa))))
6355 			wpa_supplicant_event_assoc_auth(wpa_s, data);
6356 		if (data) {
6357 			wpa_msg(wpa_s, MSG_INFO,
6358 				WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
6359 				data->assoc_info.subnet_status);
6360 		}
6361 		break;
6362 	case EVENT_DISASSOC:
6363 		wpas_event_disassoc(wpa_s,
6364 				    data ? &data->disassoc_info : NULL);
6365 		break;
6366 	case EVENT_DEAUTH:
6367 #ifdef CONFIG_TESTING_OPTIONS
6368 		if (wpa_s->ignore_auth_resp) {
6369 			wpa_printf(MSG_INFO,
6370 				   "EVENT_DEAUTH - ignore_auth_resp active!");
6371 			break;
6372 		}
6373 		if (wpa_s->testing_resend_assoc) {
6374 			wpa_printf(MSG_INFO,
6375 				   "EVENT_DEAUTH - testing_resend_assoc");
6376 			break;
6377 		}
6378 #endif /* CONFIG_TESTING_OPTIONS */
6379 		wpas_event_deauth(wpa_s,
6380 				  data ? &data->deauth_info : NULL);
6381 		break;
6382 	case EVENT_LINK_RECONFIG:
6383 		wpas_link_reconfig(wpa_s);
6384 		break;
6385 	case EVENT_MICHAEL_MIC_FAILURE:
6386 		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
6387 		break;
6388 #ifndef CONFIG_NO_SCAN_PROCESSING
6389 	case EVENT_SCAN_STARTED:
6390 		if (wpa_s->own_scan_requested ||
6391 		    (data && !data->scan_info.external_scan)) {
6392 			struct os_reltime diff;
6393 
6394 			os_get_reltime(&wpa_s->scan_start_time);
6395 			os_reltime_sub(&wpa_s->scan_start_time,
6396 				       &wpa_s->scan_trigger_time, &diff);
6397 			wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
6398 				diff.sec, diff.usec);
6399 			wpa_s->own_scan_requested = 0;
6400 			wpa_s->own_scan_running = 1;
6401 			if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
6402 			    wpa_s->manual_scan_use_id) {
6403 				wpa_msg_ctrl(wpa_s, MSG_INFO,
6404 					     WPA_EVENT_SCAN_STARTED "id=%u",
6405 					     wpa_s->manual_scan_id);
6406 			} else {
6407 				wpa_msg_ctrl(wpa_s, MSG_INFO,
6408 					     WPA_EVENT_SCAN_STARTED);
6409 			}
6410 		} else {
6411 			wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
6412 			wpa_s->radio->external_scan_req_interface = wpa_s;
6413 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
6414 		}
6415 		break;
6416 	case EVENT_SCAN_RESULTS:
6417 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6418 			wpa_s->scan_res_handler = NULL;
6419 			wpa_s->own_scan_running = 0;
6420 			wpa_s->radio->external_scan_req_interface = NULL;
6421 			wpa_s->last_scan_req = NORMAL_SCAN_REQ;
6422 			break;
6423 		}
6424 
6425 		if (!(data && data->scan_info.external_scan) &&
6426 		    os_reltime_initialized(&wpa_s->scan_start_time)) {
6427 			struct os_reltime now, diff;
6428 			os_get_reltime(&now);
6429 			os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
6430 			wpa_s->scan_start_time.sec = 0;
6431 			wpa_s->scan_start_time.usec = 0;
6432 			wpa_s->wps_scan_done = true;
6433 			wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
6434 				diff.sec, diff.usec);
6435 		}
6436 		if (wpa_supplicant_event_scan_results(wpa_s, data))
6437 			break; /* interface may have been removed */
6438 		if (!(data && data->scan_info.external_scan))
6439 			wpa_s->own_scan_running = 0;
6440 		if (data && data->scan_info.nl_scan_event)
6441 			wpa_s->radio->external_scan_req_interface = NULL;
6442 		radio_work_check_next(wpa_s);
6443 		break;
6444 #endif /* CONFIG_NO_SCAN_PROCESSING */
6445 	case EVENT_ASSOCINFO:
6446 		wpa_supplicant_event_associnfo(wpa_s, data);
6447 		break;
6448 	case EVENT_INTERFACE_STATUS:
6449 		wpa_supplicant_event_interface_status(wpa_s, data);
6450 		break;
6451 	case EVENT_PMKID_CANDIDATE:
6452 		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
6453 		break;
6454 #ifdef CONFIG_TDLS
6455 	case EVENT_TDLS:
6456 		wpa_supplicant_event_tdls(wpa_s, data);
6457 		break;
6458 #endif /* CONFIG_TDLS */
6459 #ifdef CONFIG_WNM
6460 	case EVENT_WNM:
6461 		wpa_supplicant_event_wnm(wpa_s, data);
6462 		break;
6463 #endif /* CONFIG_WNM */
6464 #ifdef CONFIG_IEEE80211R
6465 	case EVENT_FT_RESPONSE:
6466 		wpa_supplicant_event_ft_response(wpa_s, data);
6467 		break;
6468 #endif /* CONFIG_IEEE80211R */
6469 #ifdef CONFIG_IBSS_RSN
6470 	case EVENT_IBSS_RSN_START:
6471 		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
6472 		break;
6473 #endif /* CONFIG_IBSS_RSN */
6474 	case EVENT_ASSOC_REJECT:
6475 		wpas_event_assoc_reject(wpa_s, data);
6476 		break;
6477 	case EVENT_AUTH_TIMED_OUT:
6478 		/* It is possible to get this event from earlier connection */
6479 		if (wpa_s->current_ssid &&
6480 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
6481 			wpa_dbg(wpa_s, MSG_DEBUG,
6482 				"Ignore AUTH_TIMED_OUT in mesh configuration");
6483 			break;
6484 		}
6485 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
6486 			sme_event_auth_timed_out(wpa_s, data);
6487 		break;
6488 	case EVENT_ASSOC_TIMED_OUT:
6489 		/* It is possible to get this event from earlier connection */
6490 		if (wpa_s->current_ssid &&
6491 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
6492 			wpa_dbg(wpa_s, MSG_DEBUG,
6493 				"Ignore ASSOC_TIMED_OUT in mesh configuration");
6494 			break;
6495 		}
6496 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
6497 			sme_event_assoc_timed_out(wpa_s, data);
6498 		break;
6499 	case EVENT_TX_STATUS:
6500 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
6501 			" type=%d stype=%d",
6502 			MAC2STR(data->tx_status.dst),
6503 			data->tx_status.type, data->tx_status.stype);
6504 #ifdef CONFIG_WNM
6505 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
6506 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
6507 		    wnm_btm_resp_tx_status(wpa_s, data->tx_status.data,
6508 					   data->tx_status.data_len) == 0)
6509 			break;
6510 #endif /* CONFIG_WNM */
6511 #ifdef CONFIG_PASN
6512 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
6513 		    data->tx_status.stype == WLAN_FC_STYPE_AUTH &&
6514 		    wpas_pasn_auth_tx_status(wpa_s, data->tx_status.data,
6515 					     data->tx_status.data_len,
6516 					     data->tx_status.ack) == 0)
6517 			break;
6518 #endif /* CONFIG_PASN */
6519 #ifdef CONFIG_AP
6520 		if (wpa_s->ap_iface == NULL) {
6521 #ifdef CONFIG_OFFCHANNEL
6522 			if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
6523 			    data->tx_status.stype == WLAN_FC_STYPE_ACTION)
6524 				offchannel_send_action_tx_status(
6525 					wpa_s, data->tx_status.dst,
6526 					data->tx_status.data,
6527 					data->tx_status.data_len,
6528 					data->tx_status.ack ?
6529 					OFFCHANNEL_SEND_ACTION_SUCCESS :
6530 					OFFCHANNEL_SEND_ACTION_NO_ACK);
6531 #endif /* CONFIG_OFFCHANNEL */
6532 			break;
6533 		}
6534 #endif /* CONFIG_AP */
6535 #ifdef CONFIG_OFFCHANNEL
6536 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
6537 			MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
6538 		/*
6539 		 * Catch TX status events for Action frames we sent via group
6540 		 * interface in GO mode, or via standalone AP interface.
6541 		 * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
6542 		 * except when the primary interface is used as a GO interface
6543 		 * (for drivers which do not have group interface concurrency)
6544 		 */
6545 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
6546 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
6547 		    ether_addr_equal(wpa_s->p2pdev->pending_action_dst,
6548 				     data->tx_status.dst)) {
6549 			offchannel_send_action_tx_status(
6550 				wpa_s->p2pdev, data->tx_status.dst,
6551 				data->tx_status.data,
6552 				data->tx_status.data_len,
6553 				data->tx_status.ack ?
6554 				OFFCHANNEL_SEND_ACTION_SUCCESS :
6555 				OFFCHANNEL_SEND_ACTION_NO_ACK);
6556 			break;
6557 		}
6558 #endif /* CONFIG_OFFCHANNEL */
6559 #ifdef CONFIG_AP
6560 		switch (data->tx_status.type) {
6561 		case WLAN_FC_TYPE_MGMT:
6562 			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
6563 				      data->tx_status.data_len,
6564 				      data->tx_status.stype,
6565 				      data->tx_status.ack);
6566 			break;
6567 		case WLAN_FC_TYPE_DATA:
6568 			ap_tx_status(wpa_s, data->tx_status.dst,
6569 				     data->tx_status.data,
6570 				     data->tx_status.data_len,
6571 				     data->tx_status.ack);
6572 			break;
6573 		}
6574 #endif /* CONFIG_AP */
6575 		break;
6576 #ifdef CONFIG_AP
6577 	case EVENT_EAPOL_TX_STATUS:
6578 		ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
6579 				   data->eapol_tx_status.data,
6580 				   data->eapol_tx_status.data_len,
6581 				   data->eapol_tx_status.ack);
6582 		break;
6583 	case EVENT_DRIVER_CLIENT_POLL_OK:
6584 		ap_client_poll_ok(wpa_s, data->client_poll.addr);
6585 		break;
6586 	case EVENT_RX_FROM_UNKNOWN:
6587 		if (wpa_s->ap_iface == NULL)
6588 			break;
6589 		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
6590 				       data->rx_from_unknown.wds);
6591 		break;
6592 #endif /* CONFIG_AP */
6593 
6594 	case EVENT_LINK_CH_SWITCH_STARTED:
6595 	case EVENT_LINK_CH_SWITCH:
6596 		if (!data || !wpa_s->current_ssid ||
6597 		    !(wpa_s->valid_links & BIT(data->ch_switch.link_id)))
6598 			break;
6599 
6600 		wpa_msg(wpa_s, MSG_INFO,
6601 			"%sfreq=%d link_id=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
6602 			event == EVENT_LINK_CH_SWITCH ?
6603 			WPA_EVENT_LINK_CHANNEL_SWITCH :
6604 			WPA_EVENT_LINK_CHANNEL_SWITCH_STARTED,
6605 			data->ch_switch.freq,
6606 			data->ch_switch.link_id,
6607 			data->ch_switch.ht_enabled,
6608 			data->ch_switch.ch_offset,
6609 			channel_width_to_string(data->ch_switch.ch_width),
6610 			data->ch_switch.cf1,
6611 			data->ch_switch.cf2);
6612 		if (event == EVENT_LINK_CH_SWITCH_STARTED)
6613 			break;
6614 
6615 		wpa_s->links[data->ch_switch.link_id].freq =
6616 			data->ch_switch.freq;
6617 		if (wpa_s->links[data->ch_switch.link_id].bss &&
6618 		    wpa_s->links[data->ch_switch.link_id].bss->freq !=
6619 		    data->ch_switch.freq) {
6620 			wpa_s->links[data->ch_switch.link_id].bss->freq =
6621 				data->ch_switch.freq;
6622 			notify_bss_changes(
6623 				wpa_s, WPA_BSS_FREQ_CHANGED_FLAG,
6624 				wpa_s->links[data->ch_switch.link_id].bss);
6625 			if (data->ch_switch.freq)
6626 				wpas_notify_frequency_changed(wpa_s, data->ch_switch.freq);
6627 		}
6628 		break;
6629 	case EVENT_CH_SWITCH_STARTED:
6630 	case EVENT_CH_SWITCH:
6631 		if (!data || !wpa_s->current_ssid)
6632 			break;
6633 
6634 		wpa_msg(wpa_s, MSG_INFO,
6635 			"%sfreq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
6636 			event == EVENT_CH_SWITCH ? WPA_EVENT_CHANNEL_SWITCH :
6637 			WPA_EVENT_CHANNEL_SWITCH_STARTED,
6638 			data->ch_switch.freq,
6639 			data->ch_switch.ht_enabled,
6640 			data->ch_switch.ch_offset,
6641 			channel_width_to_string(data->ch_switch.ch_width),
6642 			data->ch_switch.cf1,
6643 			data->ch_switch.cf2);
6644 		if (event == EVENT_CH_SWITCH_STARTED)
6645 			break;
6646 
6647 		if (wpa_s->assoc_freq && data->ch_switch.freq &&
6648 			    (int) wpa_s->assoc_freq != data->ch_switch.freq) {
6649 			wpas_notify_frequency_changed(wpa_s, data->ch_switch.freq);
6650 		}
6651 		wpa_s->assoc_freq = data->ch_switch.freq;
6652 		wpa_s->current_ssid->frequency = data->ch_switch.freq;
6653 		if (wpa_s->current_bss &&
6654 		    wpa_s->current_bss->freq != data->ch_switch.freq) {
6655 			wpa_s->current_bss->freq = data->ch_switch.freq;
6656 			notify_bss_changes(wpa_s, WPA_BSS_FREQ_CHANGED_FLAG,
6657 					   wpa_s->current_bss);
6658 		}
6659 
6660 #ifdef CONFIG_SME
6661 		switch (data->ch_switch.ch_offset) {
6662 		case 1:
6663 			wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
6664 			break;
6665 		case -1:
6666 			wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
6667 			break;
6668 		default:
6669 			wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
6670 			break;
6671 		}
6672 #endif /* CONFIG_SME */
6673 
6674 #ifdef CONFIG_AP
6675 		if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
6676 		    wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
6677 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH ||
6678 		    wpa_s->current_ssid->mode ==
6679 		    WPAS_MODE_P2P_GROUP_FORMATION) {
6680 			wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
6681 					  data->ch_switch.ht_enabled,
6682 					  data->ch_switch.ch_offset,
6683 					  data->ch_switch.ch_width,
6684 					  data->ch_switch.cf1,
6685 					  data->ch_switch.cf2,
6686 					  data->ch_switch.punct_bitmap,
6687 					  1);
6688 		}
6689 #endif /* CONFIG_AP */
6690 
6691 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
6692 			sme_event_ch_switch(wpa_s);
6693 
6694 		wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
6695 		wnm_clear_coloc_intf_reporting(wpa_s);
6696 		break;
6697 #ifdef CONFIG_AP
6698 #ifdef NEED_AP_MLME
6699 	case EVENT_DFS_RADAR_DETECTED:
6700 		if (data)
6701 			wpas_ap_event_dfs_radar_detected(wpa_s,
6702 							 &data->dfs_event);
6703 		break;
6704 	case EVENT_DFS_NOP_FINISHED:
6705 		if (data)
6706 			wpas_ap_event_dfs_cac_nop_finished(wpa_s,
6707 							   &data->dfs_event);
6708 		break;
6709 #endif /* NEED_AP_MLME */
6710 #endif /* CONFIG_AP */
6711 	case EVENT_DFS_CAC_STARTED:
6712 		if (data)
6713 			wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
6714 		break;
6715 	case EVENT_DFS_CAC_FINISHED:
6716 		if (data)
6717 			wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
6718 		break;
6719 	case EVENT_DFS_CAC_ABORTED:
6720 		if (data)
6721 			wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
6722 		break;
6723 	case EVENT_RX_MGMT: {
6724 		u16 fc, stype;
6725 		const struct ieee80211_mgmt *mgmt;
6726 
6727 #ifdef CONFIG_TESTING_OPTIONS
6728 		if (wpa_s->ext_mgmt_frame_handling) {
6729 			struct rx_mgmt *rx = &data->rx_mgmt;
6730 			size_t hex_len = 2 * rx->frame_len + 1;
6731 			char *hex = os_malloc(hex_len);
6732 			if (hex) {
6733 				wpa_snprintf_hex(hex, hex_len,
6734 						 rx->frame, rx->frame_len);
6735 				wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
6736 					rx->freq, rx->datarate, rx->ssi_signal,
6737 					hex);
6738 				os_free(hex);
6739 			}
6740 			break;
6741 		}
6742 #endif /* CONFIG_TESTING_OPTIONS */
6743 
6744 		mgmt = (const struct ieee80211_mgmt *)
6745 			data->rx_mgmt.frame;
6746 		fc = le_to_host16(mgmt->frame_control);
6747 		stype = WLAN_FC_GET_STYPE(fc);
6748 
6749 #ifdef CONFIG_AP
6750 		if (wpa_s->ap_iface == NULL) {
6751 #endif /* CONFIG_AP */
6752 #ifdef CONFIG_P2P
6753 			if (stype == WLAN_FC_STYPE_PROBE_REQ &&
6754 			    data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
6755 				const u8 *src = mgmt->sa;
6756 				const u8 *ie;
6757 				size_t ie_len;
6758 
6759 				ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
6760 				ie_len = data->rx_mgmt.frame_len -
6761 					IEEE80211_HDRLEN;
6762 				wpas_p2p_probe_req_rx(
6763 					wpa_s, src, mgmt->da,
6764 					mgmt->bssid, ie, ie_len,
6765 					data->rx_mgmt.freq,
6766 					data->rx_mgmt.ssi_signal);
6767 				break;
6768 			}
6769 #endif /* CONFIG_P2P */
6770 #ifdef CONFIG_IBSS_RSN
6771 			if (wpa_s->current_ssid &&
6772 			    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
6773 			    stype == WLAN_FC_STYPE_AUTH &&
6774 			    data->rx_mgmt.frame_len >= 30) {
6775 				wpa_supplicant_event_ibss_auth(wpa_s, data);
6776 				break;
6777 			}
6778 #endif /* CONFIG_IBSS_RSN */
6779 
6780 			if (stype == WLAN_FC_STYPE_ACTION) {
6781 				wpas_event_rx_mgmt_action(
6782 					wpa_s, data->rx_mgmt.frame,
6783 					data->rx_mgmt.frame_len,
6784 					data->rx_mgmt.freq,
6785 					data->rx_mgmt.ssi_signal);
6786 				break;
6787 			}
6788 
6789 			if (wpa_s->ifmsh) {
6790 				mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
6791 				break;
6792 			}
6793 #ifdef CONFIG_PASN
6794 			if (stype == WLAN_FC_STYPE_AUTH &&
6795 			    wpas_pasn_auth_rx(wpa_s, mgmt,
6796 					      data->rx_mgmt.frame_len) != -2)
6797 				break;
6798 #endif /* CONFIG_PASN */
6799 
6800 #ifdef CONFIG_SAE
6801 			if (stype == WLAN_FC_STYPE_AUTH &&
6802 			    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
6803 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
6804 				sme_external_auth_mgmt_rx(
6805 					wpa_s, data->rx_mgmt.frame,
6806 					data->rx_mgmt.frame_len);
6807 				break;
6808 			}
6809 #endif /* CONFIG_SAE */
6810 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
6811 				"management frame in non-AP mode");
6812 			break;
6813 #ifdef CONFIG_AP
6814 		}
6815 
6816 		if (stype == WLAN_FC_STYPE_PROBE_REQ &&
6817 		    data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
6818 			const u8 *ie;
6819 			size_t ie_len;
6820 
6821 			ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
6822 			ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
6823 
6824 			wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
6825 					 mgmt->bssid, ie, ie_len,
6826 					 data->rx_mgmt.ssi_signal);
6827 		}
6828 
6829 		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
6830 #endif /* CONFIG_AP */
6831 		break;
6832 		}
6833 	case EVENT_RX_PROBE_REQ:
6834 		if (data->rx_probe_req.sa == NULL ||
6835 		    data->rx_probe_req.ie == NULL)
6836 			break;
6837 #ifdef CONFIG_AP
6838 		if (wpa_s->ap_iface) {
6839 			hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
6840 					     data->rx_probe_req.sa,
6841 					     data->rx_probe_req.da,
6842 					     data->rx_probe_req.bssid,
6843 					     data->rx_probe_req.ie,
6844 					     data->rx_probe_req.ie_len,
6845 					     data->rx_probe_req.ssi_signal);
6846 			break;
6847 		}
6848 #endif /* CONFIG_AP */
6849 		wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
6850 				      data->rx_probe_req.da,
6851 				      data->rx_probe_req.bssid,
6852 				      data->rx_probe_req.ie,
6853 				      data->rx_probe_req.ie_len,
6854 				      0,
6855 				      data->rx_probe_req.ssi_signal);
6856 		break;
6857 	case EVENT_REMAIN_ON_CHANNEL:
6858 #ifdef CONFIG_OFFCHANNEL
6859 		offchannel_remain_on_channel_cb(
6860 			wpa_s, data->remain_on_channel.freq,
6861 			data->remain_on_channel.duration);
6862 #endif /* CONFIG_OFFCHANNEL */
6863 		wpas_p2p_remain_on_channel_cb(
6864 			wpa_s, data->remain_on_channel.freq,
6865 			data->remain_on_channel.duration);
6866 #ifdef CONFIG_DPP
6867 		wpas_dpp_remain_on_channel_cb(
6868 			wpa_s, data->remain_on_channel.freq,
6869 			data->remain_on_channel.duration);
6870 #endif /* CONFIG_DPP */
6871 #ifdef CONFIG_NAN_USD
6872 		wpas_nan_usd_remain_on_channel_cb(
6873 			wpa_s, data->remain_on_channel.freq,
6874 			data->remain_on_channel.duration);
6875 #endif /* CONFIG_NAN_USD */
6876 		break;
6877 	case EVENT_CANCEL_REMAIN_ON_CHANNEL:
6878 #ifdef CONFIG_OFFCHANNEL
6879 		offchannel_cancel_remain_on_channel_cb(
6880 			wpa_s, data->remain_on_channel.freq);
6881 #endif /* CONFIG_OFFCHANNEL */
6882 		wpas_p2p_cancel_remain_on_channel_cb(
6883 			wpa_s, data->remain_on_channel.freq);
6884 #ifdef CONFIG_DPP
6885 		wpas_dpp_cancel_remain_on_channel_cb(
6886 			wpa_s, data->remain_on_channel.freq);
6887 #endif /* CONFIG_DPP */
6888 #ifdef CONFIG_NAN_USD
6889 		wpas_nan_usd_cancel_remain_on_channel_cb(
6890 			wpa_s, data->remain_on_channel.freq);
6891 #endif /* CONFIG_NAN_USD */
6892 		break;
6893 	case EVENT_EAPOL_RX:
6894 		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
6895 					data->eapol_rx.data,
6896 					data->eapol_rx.data_len,
6897 					data->eapol_rx.encrypted);
6898 		break;
6899 	case EVENT_SIGNAL_CHANGE:
6900 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
6901 			"above=%d signal=%d noise=%d txrate=%lu",
6902 			data->signal_change.above_threshold,
6903 			data->signal_change.data.signal,
6904 			data->signal_change.current_noise,
6905 			data->signal_change.data.current_tx_rate);
6906 		wpa_bss_update_level(wpa_s->current_bss,
6907 				     data->signal_change.data.signal);
6908 		bgscan_notify_signal_change(
6909 			wpa_s, data->signal_change.above_threshold,
6910 			data->signal_change.data.signal,
6911 			data->signal_change.current_noise,
6912 			data->signal_change.data.current_tx_rate);
6913 		os_memcpy(&wpa_s->last_signal_info, data,
6914 			  sizeof(struct wpa_signal_info));
6915 		wpas_notify_signal_change(wpa_s);
6916 		break;
6917 	case EVENT_INTERFACE_MAC_CHANGED:
6918 		wpa_supplicant_update_mac_addr(wpa_s);
6919 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6920 		break;
6921 	case EVENT_INTERFACE_ENABLED:
6922 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
6923 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6924 			u8 addr[ETH_ALEN];
6925 
6926 			eloop_cancel_timeout(wpas_clear_disabled_interface,
6927 					     wpa_s, NULL);
6928 			os_memcpy(addr, wpa_s->own_addr, ETH_ALEN);
6929 			wpa_supplicant_update_mac_addr(wpa_s);
6930 			if (!ether_addr_equal(addr, wpa_s->own_addr))
6931 				wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6932 			else
6933 				wpa_sm_pmksa_cache_reconfig(wpa_s->wpa);
6934 			wpa_supplicant_set_default_scan_ies(wpa_s);
6935 			if (wpa_s->p2p_mgmt) {
6936 				wpa_supplicant_set_state(wpa_s,
6937 							 WPA_DISCONNECTED);
6938 				break;
6939 			}
6940 
6941 #ifdef CONFIG_AP
6942 			if (!wpa_s->ap_iface) {
6943 				wpa_supplicant_set_state(wpa_s,
6944 							 WPA_DISCONNECTED);
6945 				wpa_s->scan_req = NORMAL_SCAN_REQ;
6946 				wpa_supplicant_req_scan(wpa_s, 0, 0);
6947 			} else
6948 				wpa_supplicant_set_state(wpa_s,
6949 							 WPA_COMPLETED);
6950 #else /* CONFIG_AP */
6951 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6952 			wpa_supplicant_req_scan(wpa_s, 0, 0);
6953 #endif /* CONFIG_AP */
6954 		}
6955 		break;
6956 	case EVENT_INTERFACE_DISABLED:
6957 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
6958 #ifdef CONFIG_P2P
6959 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
6960 		    (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
6961 		     wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6962 			/*
6963 			 * Mark interface disabled if this happens to end up not
6964 			 * being removed as a separate P2P group interface.
6965 			 */
6966 			wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
6967 			/*
6968 			 * The interface was externally disabled. Remove
6969 			 * it assuming an external entity will start a
6970 			 * new session if needed.
6971 			 */
6972 			if (wpa_s->current_ssid &&
6973 			    wpa_s->current_ssid->p2p_group)
6974 				wpas_p2p_interface_unavailable(wpa_s);
6975 			else
6976 				wpas_p2p_disconnect(wpa_s);
6977 			/*
6978 			 * wpa_s instance may have been freed, so must not use
6979 			 * it here anymore.
6980 			 */
6981 			break;
6982 		}
6983 		if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
6984 		    p2p_in_progress(wpa_s->global->p2p) > 1) {
6985 			/* This radio work will be cancelled, so clear P2P
6986 			 * state as well.
6987 			 */
6988 			p2p_stop_find(wpa_s->global->p2p);
6989 		}
6990 #endif /* CONFIG_P2P */
6991 
6992 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
6993 			/*
6994 			 * Indicate disconnection to keep ctrl_iface events
6995 			 * consistent.
6996 			 */
6997 			wpa_supplicant_event_disassoc(
6998 				wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
6999 		}
7000 		wpa_supplicant_mark_disassoc(wpa_s);
7001 		os_reltime_age(&wpa_s->last_scan, &age);
7002 		if (age.sec >= wpa_s->conf->scan_res_valid_for_connect) {
7003 			clear_at.sec = wpa_s->conf->scan_res_valid_for_connect;
7004 			clear_at.usec = 0;
7005 		} else {
7006 			struct os_reltime tmp;
7007 
7008 			tmp.sec = wpa_s->conf->scan_res_valid_for_connect;
7009 			tmp.usec = 0;
7010 			os_reltime_sub(&tmp, &age, &clear_at);
7011 		}
7012 		eloop_register_timeout(clear_at.sec, clear_at.usec,
7013 				       wpas_clear_disabled_interface,
7014 				       wpa_s, NULL);
7015 		radio_remove_works(wpa_s, NULL, 0);
7016 
7017 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
7018 		break;
7019 	case EVENT_CHANNEL_LIST_CHANGED:
7020 		wpa_supplicant_update_channel_list(
7021 			wpa_s, &data->channel_list_changed);
7022 		break;
7023 	case EVENT_INTERFACE_UNAVAILABLE:
7024 		wpas_p2p_interface_unavailable(wpa_s);
7025 		break;
7026 	case EVENT_BEST_CHANNEL:
7027 		wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
7028 			"(%d %d %d)",
7029 			data->best_chan.freq_24, data->best_chan.freq_5,
7030 			data->best_chan.freq_overall);
7031 		wpa_s->best_24_freq = data->best_chan.freq_24;
7032 		wpa_s->best_5_freq = data->best_chan.freq_5;
7033 		wpa_s->best_overall_freq = data->best_chan.freq_overall;
7034 		wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
7035 					      data->best_chan.freq_5,
7036 					      data->best_chan.freq_overall);
7037 		break;
7038 	case EVENT_UNPROT_DEAUTH:
7039 		wpa_supplicant_event_unprot_deauth(wpa_s,
7040 						   &data->unprot_deauth);
7041 		break;
7042 	case EVENT_UNPROT_DISASSOC:
7043 		wpa_supplicant_event_unprot_disassoc(wpa_s,
7044 						     &data->unprot_disassoc);
7045 		break;
7046 	case EVENT_STATION_LOW_ACK:
7047 #ifdef CONFIG_AP
7048 		if (wpa_s->ap_iface && data)
7049 			hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
7050 						  data->low_ack.addr);
7051 #endif /* CONFIG_AP */
7052 #ifdef CONFIG_TDLS
7053 		if (data)
7054 			wpa_tdls_disable_unreachable_link(wpa_s->wpa,
7055 							  data->low_ack.addr);
7056 #endif /* CONFIG_TDLS */
7057 		break;
7058 	case EVENT_IBSS_PEER_LOST:
7059 #ifdef CONFIG_IBSS_RSN
7060 		ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
7061 #endif /* CONFIG_IBSS_RSN */
7062 		break;
7063 	case EVENT_DRIVER_GTK_REKEY:
7064 		if (!ether_addr_equal(data->driver_gtk_rekey.bssid,
7065 				      wpa_s->bssid))
7066 			break;
7067 		if (!wpa_s->wpa)
7068 			break;
7069 		wpa_sm_update_replay_ctr(wpa_s->wpa,
7070 					 data->driver_gtk_rekey.replay_ctr);
7071 		break;
7072 	case EVENT_SCHED_SCAN_STOPPED:
7073 		wpa_s->sched_scanning = 0;
7074 		resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
7075 		wpa_supplicant_notify_scanning(wpa_s, 0);
7076 
7077 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7078 			break;
7079 
7080 		/*
7081 		 * If the driver stopped scanning without being requested to,
7082 		 * request a new scan to continue scanning for networks.
7083 		 */
7084 		if (!wpa_s->sched_scan_stop_req &&
7085 		    wpa_s->wpa_state == WPA_SCANNING) {
7086 			wpa_dbg(wpa_s, MSG_DEBUG,
7087 				"Restart scanning after unexpected sched_scan stop event");
7088 			wpa_supplicant_req_scan(wpa_s, 1, 0);
7089 			break;
7090 		}
7091 
7092 		wpa_s->sched_scan_stop_req = 0;
7093 
7094 		/*
7095 		 * Start a new sched scan to continue searching for more SSIDs
7096 		 * either if timed out or PNO schedule scan is pending.
7097 		 */
7098 		if (wpa_s->sched_scan_timed_out) {
7099 			wpa_supplicant_req_sched_scan(wpa_s);
7100 		} else if (wpa_s->pno_sched_pending) {
7101 			wpa_s->pno_sched_pending = 0;
7102 			wpas_start_pno(wpa_s);
7103 		} else if (resched) {
7104 			wpa_supplicant_req_scan(wpa_s, 0, 0);
7105 		}
7106 
7107 		break;
7108 	case EVENT_WPS_BUTTON_PUSHED:
7109 #ifdef CONFIG_WPS
7110 		wpas_wps_start_pbc(wpa_s, NULL, 0, 0);
7111 #endif /* CONFIG_WPS */
7112 		break;
7113 	case EVENT_AVOID_FREQUENCIES:
7114 		wpa_supplicant_notify_avoid_freq(wpa_s, data);
7115 		break;
7116 	case EVENT_CONNECT_FAILED_REASON:
7117 #ifdef CONFIG_AP
7118 		if (!wpa_s->ap_iface || !data)
7119 			break;
7120 		hostapd_event_connect_failed_reason(
7121 			wpa_s->ap_iface->bss[0],
7122 			data->connect_failed_reason.addr,
7123 			data->connect_failed_reason.code);
7124 #endif /* CONFIG_AP */
7125 		break;
7126 	case EVENT_NEW_PEER_CANDIDATE:
7127 #ifdef CONFIG_MESH
7128 		if (!wpa_s->ifmsh || !data)
7129 			break;
7130 		wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
7131 				     data->mesh_peer.ies,
7132 				     data->mesh_peer.ie_len);
7133 #endif /* CONFIG_MESH */
7134 		break;
7135 	case EVENT_SURVEY:
7136 #ifdef CONFIG_AP
7137 		if (!wpa_s->ap_iface)
7138 			break;
7139 		hostapd_event_get_survey(wpa_s->ap_iface,
7140 					 &data->survey_results);
7141 #endif /* CONFIG_AP */
7142 		break;
7143 	case EVENT_ACS_CHANNEL_SELECTED:
7144 #ifdef CONFIG_AP
7145 #ifdef CONFIG_ACS
7146 		if (!wpa_s->ap_iface)
7147 			break;
7148 		hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
7149 					     &data->acs_selected_channels);
7150 #endif /* CONFIG_ACS */
7151 #endif /* CONFIG_AP */
7152 		break;
7153 	case EVENT_P2P_LO_STOP:
7154 #ifdef CONFIG_P2P
7155 		wpa_s->p2p_lo_started = 0;
7156 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
7157 			P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
7158 			data->p2p_lo_stop.reason_code);
7159 #endif /* CONFIG_P2P */
7160 		break;
7161 	case EVENT_BEACON_LOSS:
7162 		if (!wpa_s->current_bss || !wpa_s->current_ssid)
7163 			break;
7164 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
7165 		bgscan_notify_beacon_loss(wpa_s);
7166 		break;
7167 	case EVENT_EXTERNAL_AUTH:
7168 #ifdef CONFIG_SAE
7169 		if (!wpa_s->current_ssid) {
7170 			wpa_printf(MSG_DEBUG, "SAE: current_ssid is NULL");
7171 			break;
7172 		}
7173 		sme_external_auth_trigger(wpa_s, data);
7174 #endif /* CONFIG_SAE */
7175 		break;
7176 #ifdef CONFIG_PASN
7177 	case EVENT_PASN_AUTH:
7178 		wpas_pasn_auth_trigger(wpa_s, &data->pasn_auth);
7179 		break;
7180 #endif /* CONFIG_PASN */
7181 	case EVENT_PORT_AUTHORIZED:
7182 #ifdef CONFIG_AP
7183 		if (wpa_s->ap_iface && wpa_s->ap_iface->bss[0]) {
7184 			struct sta_info *sta;
7185 
7186 			sta = ap_get_sta(wpa_s->ap_iface->bss[0],
7187 					 data->port_authorized.sta_addr);
7188 			if (sta)
7189 				ap_sta_set_authorized(wpa_s->ap_iface->bss[0],
7190 						      sta, 1);
7191 			else
7192 				wpa_printf(MSG_DEBUG,
7193 					   "No STA info matching port authorized event found");
7194 			break;
7195 		}
7196 #endif /* CONFIG_AP */
7197 #ifndef CONFIG_NO_WPA
7198 		if (data->port_authorized.td_bitmap_len) {
7199 			wpa_printf(MSG_DEBUG,
7200 				   "WPA3: Transition Disable bitmap from the driver event: 0x%x",
7201 				   data->port_authorized.td_bitmap[0]);
7202 			wpas_transition_disable(
7203 				wpa_s, data->port_authorized.td_bitmap[0]);
7204 		}
7205 #endif /* CONFIG_NO_WPA */
7206 		wpa_supplicant_event_port_authorized(wpa_s);
7207 		break;
7208 	case EVENT_STATION_OPMODE_CHANGED:
7209 #ifdef CONFIG_AP
7210 		if (!wpa_s->ap_iface || !data)
7211 			break;
7212 
7213 		hostapd_event_sta_opmode_changed(wpa_s->ap_iface->bss[0],
7214 						 data->sta_opmode.addr,
7215 						 data->sta_opmode.smps_mode,
7216 						 data->sta_opmode.chan_width,
7217 						 data->sta_opmode.rx_nss);
7218 #endif /* CONFIG_AP */
7219 		break;
7220 	case EVENT_UNPROT_BEACON:
7221 		wpas_event_unprot_beacon(wpa_s, &data->unprot_beacon);
7222 		break;
7223 	case EVENT_TX_WAIT_EXPIRE:
7224 #ifdef CONFIG_DPP
7225 		wpas_dpp_tx_wait_expire(wpa_s);
7226 #endif /* CONFIG_DPP */
7227 #ifdef CONFIG_NAN_USD
7228 		wpas_nan_usd_tx_wait_expire(wpa_s);
7229 #endif /* CONFIG_NAN_USD */
7230 		break;
7231 	case EVENT_TID_LINK_MAP:
7232 		if (data)
7233 			wpas_tid_link_map(wpa_s, &data->t2l_map_info);
7234 		break;
7235 	default:
7236 		wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
7237 		break;
7238 	}
7239 }
7240 
7241 
wpa_supplicant_event_global(void * ctx,enum wpa_event_type event,union wpa_event_data * data)7242 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
7243 				 union wpa_event_data *data)
7244 {
7245 	struct wpa_supplicant *wpa_s;
7246 
7247 	if (event != EVENT_INTERFACE_STATUS)
7248 		return;
7249 
7250 	wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
7251 	if (wpa_s && wpa_s->driver->get_ifindex) {
7252 		unsigned int ifindex;
7253 
7254 		ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
7255 		if (ifindex != data->interface_status.ifindex) {
7256 			wpa_dbg(wpa_s, MSG_DEBUG,
7257 				"interface status ifindex %d mismatch (%d)",
7258 				ifindex, data->interface_status.ifindex);
7259 			return;
7260 		}
7261 	}
7262 #ifdef CONFIG_MATCH_IFACE
7263 	else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
7264 		struct wpa_interface *wpa_i;
7265 
7266 		wpa_i = wpa_supplicant_match_iface(
7267 			ctx, data->interface_status.ifname);
7268 		if (!wpa_i)
7269 			return;
7270 		wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
7271 		os_free(wpa_i);
7272 	}
7273 #endif /* CONFIG_MATCH_IFACE */
7274 
7275 	if (wpa_s)
7276 		wpa_supplicant_event(wpa_s, event, data);
7277 }
7278