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