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