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