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