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