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