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