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