xref: /freebsd/contrib/wpa/wpa_supplicant/wpa_supplicant.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * WPA Supplicant
3  * Copyright (c) 2003-2024, 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  * This file implements functions for registering and unregistering
9  * %wpa_supplicant interfaces. In addition, this file contains number of
10  * functions for managing network connections.
11  */
12 
13 #include "includes.h"
14 #ifdef CONFIG_MATCH_IFACE
15 #include <net/if.h>
16 #include <fnmatch.h>
17 #endif /* CONFIG_MATCH_IFACE */
18 
19 #include "common.h"
20 #include "crypto/crypto.h"
21 #include "crypto/random.h"
22 #include "crypto/sha1.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "eap_peer/eap.h"
25 #include "eap_peer/eap_proxy.h"
26 #include "eap_server/eap_methods.h"
27 #include "rsn_supp/wpa.h"
28 #include "eloop.h"
29 #include "config.h"
30 #include "utils/ext_password.h"
31 #include "l2_packet/l2_packet.h"
32 #include "wpa_supplicant_i.h"
33 #include "driver_i.h"
34 #include "ctrl_iface.h"
35 #include "pcsc_funcs.h"
36 #include "common/version.h"
37 #include "rsn_supp/preauth.h"
38 #include "rsn_supp/pmksa_cache.h"
39 #include "common/wpa_ctrl.h"
40 #include "common/ieee802_11_common.h"
41 #include "common/ieee802_11_defs.h"
42 #include "common/hw_features_common.h"
43 #include "common/gas_server.h"
44 #include "common/dpp.h"
45 #include "common/ptksa_cache.h"
46 #include "common/proc_coord.h"
47 #include "p2p/p2p.h"
48 #include "fst/fst.h"
49 #include "bssid_ignore.h"
50 #include "wpas_glue.h"
51 #include "wps_supplicant.h"
52 #include "ibss_rsn.h"
53 #include "sme.h"
54 #include "gas_query.h"
55 #include "ap.h"
56 #include "p2p_supplicant.h"
57 #include "wifi_display.h"
58 #include "notify.h"
59 #include "bgscan.h"
60 #include "autoscan.h"
61 #include "bss.h"
62 #include "scan.h"
63 #include "offchannel.h"
64 #include "hs20_supplicant.h"
65 #include "wnm_sta.h"
66 #include "wpas_kay.h"
67 #include "mesh.h"
68 #include "dpp_supplicant.h"
69 #include "pr_supplicant.h"
70 #include "nan_supplicant.h"
71 #ifdef CONFIG_MESH
72 #include "ap/ap_config.h"
73 #include "ap/hostapd.h"
74 #endif /* CONFIG_MESH */
75 
76 const char *const wpa_supplicant_version =
77 "wpa_supplicant v" VERSION_STR "\n"
78 "Copyright (c) 2003-2026, Jouni Malinen <j@w1.fi> and contributors";
79 
80 const char *const wpa_supplicant_license =
81 "This software may be distributed under the terms of the BSD license.\n"
82 "See README for more details.\n"
83 #ifdef EAP_TLS_OPENSSL
84 "\nThis product includes software developed by the OpenSSL Project\n"
85 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
86 #endif /* EAP_TLS_OPENSSL */
87 ;
88 
89 #ifndef CONFIG_NO_STDOUT_DEBUG
90 /* Long text divided into parts in order to fit in C89 strings size limits. */
91 const char *const wpa_supplicant_full_license1 =
92 "";
93 const char *const wpa_supplicant_full_license2 =
94 "This software may be distributed under the terms of the BSD license.\n"
95 "\n"
96 "Redistribution and use in source and binary forms, with or without\n"
97 "modification, are permitted provided that the following conditions are\n"
98 "met:\n"
99 "\n";
100 const char *const wpa_supplicant_full_license3 =
101 "1. Redistributions of source code must retain the above copyright\n"
102 "   notice, this list of conditions and the following disclaimer.\n"
103 "\n"
104 "2. Redistributions in binary form must reproduce the above copyright\n"
105 "   notice, this list of conditions and the following disclaimer in the\n"
106 "   documentation and/or other materials provided with the distribution.\n"
107 "\n";
108 const char *const wpa_supplicant_full_license4 =
109 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
110 "   names of its contributors may be used to endorse or promote products\n"
111 "   derived from this software without specific prior written permission.\n"
112 "\n"
113 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
114 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
115 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
116 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
117 const char *const wpa_supplicant_full_license5 =
118 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
119 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
120 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
121 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
122 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
123 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
124 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
125 "\n";
126 #endif /* CONFIG_NO_STDOUT_DEBUG */
127 
128 
129 static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx);
130 static void wpas_verify_ssid_beacon(void *eloop_ctx, void *timeout_ctx);
131 #if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
132 static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s);
133 #endif /* CONFIG_FILS && IEEE8021X_EAPOL */
134 #ifdef CONFIG_OWE
135 static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s);
136 #endif /* CONFIG_OWE */
137 static void radio_remove_pending_connect(struct wpa_supplicant *wpa_s,
138 					 const struct wpa_ssid *ssid);
139 
140 
141 #ifdef CONFIG_WEP
142 /* Configure default/group WEP keys for static WEP */
wpa_set_wep_keys(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)143 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
144 {
145 	int i, set = 0;
146 
147 	for (i = 0; i < NUM_WEP_KEYS; i++) {
148 		if (ssid->wep_key_len[i] == 0)
149 			continue;
150 
151 		set = 1;
152 		wpa_drv_set_key(wpa_s, -1, WPA_ALG_WEP, NULL,
153 				i, i == ssid->wep_tx_keyidx, NULL, 0,
154 				ssid->wep_key[i], ssid->wep_key_len[i],
155 				i == ssid->wep_tx_keyidx ?
156 				KEY_FLAG_GROUP_RX_TX_DEFAULT :
157 				KEY_FLAG_GROUP_RX_TX);
158 	}
159 
160 	return set;
161 }
162 #endif /* CONFIG_WEP */
163 
164 
wpa_supplicant_set_wpa_none_key(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)165 int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
166 				    struct wpa_ssid *ssid)
167 {
168 	u8 key[32];
169 	size_t keylen;
170 	enum wpa_alg alg;
171 	u8 seq[6] = { 0 };
172 	int ret;
173 
174 	/* IBSS/WPA-None uses only one key (Group) for both receiving and
175 	 * sending unicast and multicast packets. */
176 
177 	if (ssid->mode != WPAS_MODE_IBSS) {
178 		wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
179 			"IBSS/ad-hoc) for WPA-None", ssid->mode);
180 		return -1;
181 	}
182 
183 	if (!ssid->psk_set) {
184 		wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
185 			"WPA-None");
186 		return -1;
187 	}
188 
189 	switch (wpa_s->group_cipher) {
190 	case WPA_CIPHER_CCMP:
191 		os_memcpy(key, ssid->psk, 16);
192 		keylen = 16;
193 		alg = WPA_ALG_CCMP;
194 		break;
195 	case WPA_CIPHER_GCMP:
196 		os_memcpy(key, ssid->psk, 16);
197 		keylen = 16;
198 		alg = WPA_ALG_GCMP;
199 		break;
200 	case WPA_CIPHER_TKIP:
201 		/* WPA-None uses the same Michael MIC key for both TX and RX */
202 		os_memcpy(key, ssid->psk, 16 + 8);
203 		os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
204 		keylen = 32;
205 		alg = WPA_ALG_TKIP;
206 		break;
207 	default:
208 		wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
209 			"WPA-None", wpa_s->group_cipher);
210 		return -1;
211 	}
212 
213 	/* TODO: should actually remember the previously used seq#, both for TX
214 	 * and RX from each STA.. */
215 
216 	ret = wpa_drv_set_key(wpa_s, -1, alg, NULL, 0, 1, seq, 6, key, keylen,
217 			      KEY_FLAG_GROUP_RX_TX_DEFAULT);
218 	os_memset(key, 0, sizeof(key));
219 	return ret;
220 }
221 
222 
wpa_supplicant_timeout(void * eloop_ctx,void * timeout_ctx)223 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
224 {
225 	struct wpa_supplicant *wpa_s = eloop_ctx;
226 	const u8 *bssid = wpa_s->bssid;
227 	if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
228 	    (wpa_s->wpa_state == WPA_AUTHENTICATING ||
229 	     wpa_s->wpa_state == WPA_ASSOCIATING))
230 		bssid = wpa_s->pending_bssid;
231 	wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
232 		MAC2STR(bssid));
233 	wpa_bssid_ignore_add(wpa_s, bssid);
234 	wpa_sm_notify_disassoc(wpa_s->wpa);
235 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
236 	wpa_s->reassociate = 1;
237 
238 	/*
239 	 * If we timed out, the AP or the local radio may be busy.
240 	 * So, wait a second until scanning again.
241 	 */
242 	wpa_supplicant_req_scan(wpa_s, 1, 0);
243 }
244 
245 
246 /**
247  * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
248  * @wpa_s: Pointer to wpa_supplicant data
249  * @sec: Number of seconds after which to time out authentication
250  * @usec: Number of microseconds after which to time out authentication
251  *
252  * This function is used to schedule a timeout for the current authentication
253  * attempt.
254  */
wpa_supplicant_req_auth_timeout(struct wpa_supplicant * wpa_s,int sec,int usec)255 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
256 				     int sec, int usec)
257 {
258 	if (wpa_s->conf->ap_scan == 0 &&
259 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
260 		return;
261 
262 	wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
263 		"%d usec", sec, usec);
264 	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
265 	wpa_s->last_auth_timeout_sec = sec;
266 	eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
267 }
268 
269 
270 /*
271  * wpas_auth_timeout_restart - Restart and change timeout for authentication
272  * @wpa_s: Pointer to wpa_supplicant data
273  * @sec_diff: difference in seconds applied to original timeout value
274  */
wpas_auth_timeout_restart(struct wpa_supplicant * wpa_s,int sec_diff)275 void wpas_auth_timeout_restart(struct wpa_supplicant *wpa_s, int sec_diff)
276 {
277 	int new_sec = wpa_s->last_auth_timeout_sec + sec_diff;
278 
279 	if (eloop_is_timeout_registered(wpa_supplicant_timeout, wpa_s, NULL)) {
280 		wpa_dbg(wpa_s, MSG_DEBUG,
281 			"Authentication timeout restart: %d sec", new_sec);
282 		eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
283 		eloop_register_timeout(new_sec, 0, wpa_supplicant_timeout,
284 				       wpa_s, NULL);
285 	}
286 }
287 
288 
289 /**
290  * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
291  * @wpa_s: Pointer to wpa_supplicant data
292  *
293  * This function is used to cancel authentication timeout scheduled with
294  * wpa_supplicant_req_auth_timeout() and it is called when authentication has
295  * been completed.
296  */
wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant * wpa_s)297 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
298 {
299 	wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
300 	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
301 	wpa_bssid_ignore_del(wpa_s, wpa_s->bssid);
302 	os_free(wpa_s->last_con_fail_realm);
303 	wpa_s->last_con_fail_realm = NULL;
304 	wpa_s->last_con_fail_realm_len = 0;
305 }
306 
307 
308 /**
309  * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
310  * @wpa_s: Pointer to wpa_supplicant data
311  *
312  * This function is used to configure EAPOL state machine based on the selected
313  * authentication mode.
314  */
wpa_supplicant_initiate_eapol(struct wpa_supplicant * wpa_s)315 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
316 {
317 #ifdef IEEE8021X_EAPOL
318 	struct eapol_config eapol_conf;
319 	struct wpa_ssid *ssid = wpa_s->current_ssid;
320 
321 #ifdef CONFIG_IBSS_RSN
322 	if (ssid->mode == WPAS_MODE_IBSS &&
323 	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
324 	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
325 		/*
326 		 * RSN IBSS authentication is per-STA and we can disable the
327 		 * per-BSSID EAPOL authentication.
328 		 */
329 		eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
330 		eapol_sm_notify_eap_success(wpa_s->eapol, true);
331 		eapol_sm_notify_eap_fail(wpa_s->eapol, false);
332 		return;
333 	}
334 #endif /* CONFIG_IBSS_RSN */
335 
336 	eapol_sm_notify_eap_success(wpa_s->eapol, false);
337 	eapol_sm_notify_eap_fail(wpa_s->eapol, false);
338 
339 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
340 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
341 		eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
342 	else
343 		eapol_sm_notify_portControl(wpa_s->eapol, Auto);
344 
345 	os_memset(&eapol_conf, 0, sizeof(eapol_conf));
346 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
347 		eapol_conf.accept_802_1x_keys = 1;
348 		eapol_conf.required_keys = 0;
349 		if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
350 			eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
351 		}
352 		if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
353 			eapol_conf.required_keys |=
354 				EAPOL_REQUIRE_KEY_BROADCAST;
355 		}
356 
357 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
358 			eapol_conf.required_keys = 0;
359 	}
360 	eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
361 	eapol_conf.workaround = ssid->eap_workaround;
362 	eapol_conf.eap_disabled =
363 		!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
364 		wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
365 		wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
366 	eapol_conf.external_sim = wpa_s->conf->external_sim;
367 
368 #ifdef CONFIG_WPS
369 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
370 		eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
371 		if (wpa_s->current_bss) {
372 			struct wpabuf *ie;
373 			ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
374 							 WPS_IE_VENDOR_TYPE);
375 			if (ie) {
376 				if (wps_is_20(ie))
377 					eapol_conf.wps |=
378 						EAPOL_PEER_IS_WPS20_AP;
379 				wpabuf_free(ie);
380 			}
381 		}
382 	}
383 #endif /* CONFIG_WPS */
384 
385 	eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
386 
387 #ifdef CONFIG_MACSEC
388 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE && ssid->mka_psk_set)
389 		ieee802_1x_create_preshared_mka(wpa_s, ssid);
390 	else
391 		ieee802_1x_alloc_kay_sm(wpa_s, ssid);
392 #endif /* CONFIG_MACSEC */
393 #endif /* IEEE8021X_EAPOL */
394 }
395 
396 
397 /**
398  * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
399  * @wpa_s: Pointer to wpa_supplicant data
400  * @ssid: Configuration data for the network
401  *
402  * This function is used to configure WPA state machine and related parameters
403  * to a mode where WPA is not enabled. This is called as part of the
404  * authentication configuration when the selected network does not use WPA.
405  */
wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)406 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
407 				       struct wpa_ssid *ssid)
408 {
409 #ifdef CONFIG_WEP
410 	int i;
411 #endif /* CONFIG_WEP */
412 	struct wpa_sm_mlo mlo;
413 
414 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
415 		wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
416 	else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
417 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
418 	else
419 		wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
420 	wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
421 	wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
422 	wpa_sm_set_ap_rsnxe(wpa_s->wpa, NULL, 0);
423 	wpa_sm_set_ap_rsne_override(wpa_s->wpa, NULL, 0);
424 	wpa_sm_set_ap_rsne_override_2(wpa_s->wpa, NULL, 0);
425 	wpa_sm_set_ap_rsnxe_override(wpa_s->wpa, NULL, 0);
426 	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
427 #ifndef CONFIG_NO_WPA
428 	wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
429 #endif /* CONFIG_NO_WPA */
430 	wpa_s->rsnxe_len = 0;
431 	wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
432 	wpa_s->group_cipher = WPA_CIPHER_NONE;
433 	wpa_s->mgmt_group_cipher = 0;
434 
435 #ifdef CONFIG_WEP
436 	for (i = 0; i < NUM_WEP_KEYS; i++) {
437 		if (ssid->wep_key_len[i] > 5) {
438 			wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
439 			wpa_s->group_cipher = WPA_CIPHER_WEP104;
440 			break;
441 		} else if (ssid->wep_key_len[i] > 0) {
442 			wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
443 			wpa_s->group_cipher = WPA_CIPHER_WEP40;
444 			break;
445 		}
446 	}
447 #endif /* CONFIG_WEP */
448 
449 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
450 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
451 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
452 			 wpa_s->pairwise_cipher);
453 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
454 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
455 			 wpa_s->mgmt_group_cipher);
456 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SSID_PROTECTION, 0);
457 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ASSOC_ENC, 0);
458 
459 	pmksa_cache_clear_current(wpa_s->wpa);
460 	os_memset(&mlo, 0, sizeof(mlo));
461 	wpa_sm_set_mlo_params(wpa_s->wpa, &mlo);
462 }
463 
464 
free_hw_features(struct wpa_supplicant * wpa_s)465 void free_hw_features(struct wpa_supplicant *wpa_s)
466 {
467 	int i;
468 	if (wpa_s->hw.modes == NULL)
469 		return;
470 
471 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
472 		os_free(wpa_s->hw.modes[i].channels);
473 		os_free(wpa_s->hw.modes[i].rates);
474 	}
475 
476 	os_free(wpa_s->hw.modes);
477 	wpa_s->hw.modes = NULL;
478 }
479 
480 
remove_bss_tmp_disallowed_entry(struct wpa_supplicant * wpa_s,struct wpa_bss_tmp_disallowed * bss)481 static void remove_bss_tmp_disallowed_entry(struct wpa_supplicant *wpa_s,
482 					    struct wpa_bss_tmp_disallowed *bss)
483 {
484 	eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
485 	dl_list_del(&bss->list);
486 	os_free(bss);
487 }
488 
489 
free_bss_tmp_disallowed(struct wpa_supplicant * wpa_s)490 void free_bss_tmp_disallowed(struct wpa_supplicant *wpa_s)
491 {
492 	struct wpa_bss_tmp_disallowed *bss, *prev;
493 
494 	dl_list_for_each_safe(bss, prev, &wpa_s->bss_tmp_disallowed,
495 			      struct wpa_bss_tmp_disallowed, list)
496 		remove_bss_tmp_disallowed_entry(wpa_s, bss);
497 }
498 
499 
wpas_flush_fils_hlp_req(struct wpa_supplicant * wpa_s)500 void wpas_flush_fils_hlp_req(struct wpa_supplicant *wpa_s)
501 {
502 	struct fils_hlp_req *req;
503 
504 	while ((req = dl_list_first(&wpa_s->fils_hlp_req, struct fils_hlp_req,
505 				    list)) != NULL) {
506 		dl_list_del(&req->list);
507 		wpabuf_free(req->pkt);
508 		os_free(req);
509 	}
510 }
511 
512 
wpas_wfa_gen_capab_attr(struct wpa_supplicant * wpa_s)513 static struct wpabuf * wpas_wfa_gen_capab_attr(struct wpa_supplicant *wpa_s)
514 {
515 	struct wpabuf *attr;
516 	size_t gen_len, supp_len;
517 	const u8 *supp;
518 	u8 supp_buf[1];
519 
520 	if (wpa_s->conf->wfa_gen_capa == WFA_GEN_CAPA_DISABLED)
521 		return NULL;
522 
523 	if (!wpa_s->conf->wfa_gen_capa_supp ||
524 	    wpabuf_len(wpa_s->conf->wfa_gen_capa_supp) == 0) {
525 		supp_len = 1;
526 		supp_buf[0] = 0;
527 		if (wpa_s->hw_capab & BIT(CAPAB_HT))
528 			supp_buf[0] |= BIT(0); /* Wi-Fi 4 */
529 		if (wpa_s->hw_capab & BIT(CAPAB_VHT))
530 			supp_buf[0] |= BIT(1); /* Wi-Fi 5 */
531 		if (wpa_s->hw_capab & BIT(CAPAB_HE))
532 			supp_buf[0] |= BIT(2); /* Wi-Fi 6 */
533 		if (wpa_s->hw_capab & BIT(CAPAB_EHT))
534 			supp_buf[0] |= BIT(3); /* Wi-Fi 7 */
535 		supp = supp_buf;
536 	} else {
537 		supp_len = wpabuf_len(wpa_s->conf->wfa_gen_capa_supp);
538 		supp = wpabuf_head(wpa_s->conf->wfa_gen_capa_supp);
539 	}
540 
541 	gen_len = 1 + supp_len;
542 
543 	attr = wpabuf_alloc(2 + gen_len);
544 	if (!attr)
545 		return NULL;
546 
547 	wpabuf_put_u8(attr, WFA_CAPA_ATTR_GENERATIONAL_CAPAB);
548 	wpabuf_put_u8(attr, gen_len);
549 	wpabuf_put_u8(attr, supp_len);
550 	wpabuf_put_data(attr, supp, supp_len);
551 
552 	return attr;
553 }
554 
555 
556 
wpas_wfa_capab_tx(void * eloop_ctx,void * timeout_ctx)557 static void wpas_wfa_capab_tx(void *eloop_ctx, void *timeout_ctx)
558 {
559 	struct wpa_supplicant *wpa_s = eloop_ctx;
560 	struct wpabuf *attr, *buf;
561 	size_t buf_len;
562 
563 	if (wpa_s->conf->wfa_gen_capa != WFA_GEN_CAPA_PROTECTED ||
564 	    wpa_s->wpa_state != WPA_COMPLETED ||
565 	    !pmf_in_use(wpa_s, wpa_s->bssid))
566 		return;
567 
568 	attr = wpas_wfa_gen_capab_attr(wpa_s);
569 	if (!attr)
570 		return;
571 
572 	buf_len = 1 + 3 + 1 + 1 + wpabuf_len(attr);
573 	buf = wpabuf_alloc(buf_len);
574 	if (!buf) {
575 		wpabuf_free(attr);
576 		return;
577 	}
578 
579 	wpabuf_put_u8(buf, WLAN_ACTION_VENDOR_SPECIFIC_PROTECTED);
580 	wpabuf_put_be32(buf, WFA_CAPAB_VENDOR_TYPE);
581 	wpabuf_put_u8(buf, 0); /* Capabilities Length */
582 	wpabuf_put_buf(buf, attr);
583 	wpabuf_free(attr);
584 
585 	wpa_printf(MSG_DEBUG, "WFA: Send WFA Capabilities frame");
586 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
587 				wpa_s->own_addr, wpa_s->bssid,
588 				wpabuf_head(buf), wpabuf_len(buf), 0) < 0)
589 		wpa_printf(MSG_DEBUG,
590 			   "WFA: Failed to send WFA Capabilities frame");
591 
592 	wpabuf_free(buf);
593 }
594 
595 
wpas_clear_disabled_interface(void * eloop_ctx,void * timeout_ctx)596 void wpas_clear_disabled_interface(void *eloop_ctx, void *timeout_ctx)
597 {
598 	struct wpa_supplicant *wpa_s = eloop_ctx;
599 
600 	if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
601 		return;
602 	wpa_dbg(wpa_s, MSG_DEBUG, "Clear cached state on disabled interface");
603 	wpa_bss_flush(wpa_s);
604 }
605 
606 
607 #ifdef CONFIG_TESTING_OPTIONS
wpas_clear_driver_signal_override(struct wpa_supplicant * wpa_s)608 void wpas_clear_driver_signal_override(struct wpa_supplicant *wpa_s)
609 {
610 	struct driver_signal_override *dso;
611 
612 	while ((dso = dl_list_first(&wpa_s->drv_signal_override,
613 				    struct driver_signal_override, list))) {
614 		dl_list_del(&dso->list);
615 		os_free(dso);
616 	}
617 }
618 #endif /* CONFIG_TESTING_OPTIONS */
619 
620 
wpa_supplicant_cleanup(struct wpa_supplicant * wpa_s)621 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
622 {
623 	int i;
624 
625 	bgscan_deinit(wpa_s);
626 	autoscan_deinit(wpa_s);
627 	scard_deinit(wpa_s->scard);
628 	wpa_s->scard = NULL;
629 	wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
630 	eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
631 	l2_packet_deinit(wpa_s->l2);
632 	wpa_s->l2 = NULL;
633 	if (wpa_s->l2_br) {
634 		l2_packet_deinit(wpa_s->l2_br);
635 		wpa_s->l2_br = NULL;
636 	}
637 #ifdef CONFIG_TESTING_OPTIONS
638 	l2_packet_deinit(wpa_s->l2_test);
639 	wpa_s->l2_test = NULL;
640 	os_free(wpa_s->get_pref_freq_list_override);
641 	wpa_s->get_pref_freq_list_override = NULL;
642 	wpabuf_free(wpa_s->last_assoc_req_wpa_ie);
643 	wpa_s->last_assoc_req_wpa_ie = NULL;
644 	os_free(wpa_s->extra_sae_rejected_groups);
645 	wpa_s->extra_sae_rejected_groups = NULL;
646 	wpabuf_free(wpa_s->rsne_override_eapol);
647 	wpa_s->rsne_override_eapol = NULL;
648 	wpabuf_free(wpa_s->rsnxe_override_assoc);
649 	wpa_s->rsnxe_override_assoc = NULL;
650 	wpabuf_free(wpa_s->rsnxe_override_eapol);
651 	wpa_s->rsnxe_override_eapol = NULL;
652 	wpas_clear_driver_signal_override(wpa_s);
653 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
654 		wpabuf_free(wpa_s->link_ies[i]);
655 		wpa_s->link_ies[i] = NULL;
656 	}
657 #endif /* CONFIG_TESTING_OPTIONS */
658 
659 	if (wpa_s->conf != NULL) {
660 		struct wpa_ssid *ssid;
661 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
662 			wpas_notify_network_removed(wpa_s, ssid);
663 	}
664 
665 	os_free(wpa_s->confname);
666 	wpa_s->confname = NULL;
667 
668 	os_free(wpa_s->confanother);
669 	wpa_s->confanother = NULL;
670 
671 	os_free(wpa_s->last_con_fail_realm);
672 	wpa_s->last_con_fail_realm = NULL;
673 	wpa_s->last_con_fail_realm_len = 0;
674 
675 	wpa_sm_set_eapol(wpa_s->wpa, NULL);
676 	eapol_sm_deinit(wpa_s->eapol);
677 	wpa_s->eapol = NULL;
678 
679 	rsn_preauth_deinit(wpa_s->wpa);
680 
681 #ifdef CONFIG_TDLS
682 	wpa_tdls_deinit(wpa_s->wpa);
683 #endif /* CONFIG_TDLS */
684 
685 #ifndef CONFIG_NO_WMM_AC
686 	wmm_ac_clear_saved_tspecs(wpa_s);
687 #endif /* CONFIG_NO_WMM_AC */
688 	pmksa_candidate_free(wpa_s->wpa);
689 	ptksa_cache_deinit(wpa_s->ptksa);
690 	wpa_s->ptksa = NULL;
691 	wpa_sm_deinit(wpa_s->wpa);
692 	wpa_s->wpa = NULL;
693 	wpa_bssid_ignore_clear(wpa_s);
694 
695 #ifdef CONFIG_PASN
696 	wpas_pasn_auth_stop(wpa_s);
697 #endif /* CONFIG_PASN */
698 
699 	wpa_bss_deinit(wpa_s);
700 
701 	wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
702 	wpa_supplicant_cancel_scan(wpa_s);
703 	wpa_supplicant_cancel_auth_timeout(wpa_s);
704 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
705 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
706 	eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
707 			     wpa_s, NULL);
708 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
709 
710 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
711 	eloop_cancel_timeout(wpas_clear_disabled_interface, wpa_s, NULL);
712 	eloop_cancel_timeout(wpas_verify_ssid_beacon, wpa_s, NULL);
713 	eloop_cancel_timeout(wpas_wfa_capab_tx, wpa_s, NULL);
714 
715 	wpas_wps_deinit(wpa_s);
716 
717 	wpabuf_free(wpa_s->pending_eapol_rx);
718 	wpa_s->pending_eapol_rx = NULL;
719 
720 #ifdef CONFIG_IBSS_RSN
721 	ibss_rsn_deinit(wpa_s->ibss_rsn);
722 	wpa_s->ibss_rsn = NULL;
723 #endif /* CONFIG_IBSS_RSN */
724 
725 	sme_deinit(wpa_s);
726 
727 #ifdef CONFIG_AP
728 	wpa_supplicant_ap_deinit(wpa_s);
729 #endif /* CONFIG_AP */
730 
731 	wpas_p2p_deinit(wpa_s);
732 
733 	wpas_pr_deinit(wpa_s);
734 
735 #ifdef CONFIG_OFFCHANNEL
736 	offchannel_deinit(wpa_s);
737 #endif /* CONFIG_OFFCHANNEL */
738 
739 	wpa_supplicant_cancel_sched_scan(wpa_s);
740 
741 	os_free(wpa_s->next_scan_freqs);
742 	wpa_s->next_scan_freqs = NULL;
743 
744 	os_free(wpa_s->manual_scan_freqs);
745 	wpa_s->manual_scan_freqs = NULL;
746 	os_free(wpa_s->select_network_scan_freqs);
747 	wpa_s->select_network_scan_freqs = NULL;
748 
749 	os_free(wpa_s->manual_sched_scan_freqs);
750 	wpa_s->manual_sched_scan_freqs = NULL;
751 
752 	wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
753 
754 	/*
755 	 * Need to remove any pending gas-query radio work before the
756 	 * gas_query_deinit() call because gas_query::work has not yet been set
757 	 * for works that have not been started. gas_query_free() will be unable
758 	 * to cancel such pending radio works and once the pending gas-query
759 	 * radio work eventually gets removed, the deinit notification call to
760 	 * gas_query_start_cb() would result in dereferencing freed memory.
761 	 */
762 	if (wpa_s->radio)
763 		radio_remove_works(wpa_s, "gas-query", 0);
764 	gas_query_deinit(wpa_s->gas);
765 	wpa_s->gas = NULL;
766 	gas_server_deinit(wpa_s->gas_server);
767 	wpa_s->gas_server = NULL;
768 
769 	free_hw_features(wpa_s);
770 
771 	ieee802_1x_dealloc_kay_sm(wpa_s);
772 
773 	os_free(wpa_s->bssid_filter);
774 	wpa_s->bssid_filter = NULL;
775 
776 	os_free(wpa_s->disallow_aps_bssid);
777 	wpa_s->disallow_aps_bssid = NULL;
778 	os_free(wpa_s->disallow_aps_ssid);
779 	wpa_s->disallow_aps_ssid = NULL;
780 
781 	wnm_bss_keep_alive_deinit(wpa_s);
782 	wnm_btm_reset(wpa_s);
783 
784 	ext_password_deinit(wpa_s->ext_pw);
785 	wpa_s->ext_pw = NULL;
786 
787 	wpabuf_free(wpa_s->last_gas_resp);
788 	wpa_s->last_gas_resp = NULL;
789 	wpabuf_free(wpa_s->prev_gas_resp);
790 	wpa_s->prev_gas_resp = NULL;
791 
792 	os_free(wpa_s->last_scan_res);
793 	wpa_s->last_scan_res = NULL;
794 
795 #ifdef CONFIG_P2P
796 	os_free(wpa_s->p2p_pmksa_entry);
797 	wpa_s->p2p_pmksa_entry = NULL;
798 #endif /* CONFIG_P2P */
799 
800 	if (wpa_s->drv_priv)
801 		wpa_drv_configure_frame_filters(wpa_s, 0);
802 
803 	for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
804 		wpabuf_free(wpa_s->vendor_elem[i]);
805 		wpa_s->vendor_elem[i] = NULL;
806 	}
807 
808 #ifndef CONFIG_NO_WMM_AC
809 	wmm_ac_notify_disassoc(wpa_s);
810 #endif /* CONFIG_NO_WMM_AC */
811 
812 	wpa_s->sched_scan_plans_num = 0;
813 	os_free(wpa_s->sched_scan_plans);
814 	wpa_s->sched_scan_plans = NULL;
815 
816 #ifdef CONFIG_MBO
817 	wpa_s->non_pref_chan_num = 0;
818 	os_free(wpa_s->non_pref_chan);
819 	wpa_s->non_pref_chan = NULL;
820 #endif /* CONFIG_MBO */
821 
822 	free_bss_tmp_disallowed(wpa_s);
823 
824 	wpabuf_free(wpa_s->lci);
825 	wpa_s->lci = NULL;
826 #ifndef CONFIG_NO_RRM
827 	wpas_clear_beacon_rep_data(wpa_s);
828 #endif /* CONFIG_NO_RRM */
829 
830 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
831 #ifdef CONFIG_MESH
832 	{
833 		struct external_pmksa_cache *entry;
834 
835 		while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
836 					     struct external_pmksa_cache,
837 					     list)) != NULL) {
838 			dl_list_del(&entry->list);
839 			os_free(entry->pmksa_cache);
840 			os_free(entry);
841 		}
842 	}
843 #endif /* CONFIG_MESH */
844 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
845 
846 	wpas_flush_fils_hlp_req(wpa_s);
847 
848 	wpabuf_free(wpa_s->ric_ies);
849 	wpa_s->ric_ies = NULL;
850 
851 #ifdef CONFIG_DPP
852 	wpas_dpp_deinit(wpa_s);
853 	dpp_global_deinit(wpa_s->dpp);
854 	wpa_s->dpp = NULL;
855 #endif /* CONFIG_DPP */
856 
857 	wpas_nan_de_deinit(wpa_s);
858 
859 #ifdef CONFIG_PASN
860 	wpas_pasn_auth_stop(wpa_s);
861 	wpas_pasn_free_params(wpa_s);
862 #endif /* CONFIG_PASN */
863 #ifndef CONFIG_NO_ROBUST_AV
864 	wpas_scs_deinit(wpa_s);
865 	wpas_dscp_deinit(wpa_s);
866 #endif /* CONFIG_NO_ROBUST_AV */
867 
868 #ifdef CONFIG_OWE
869 	os_free(wpa_s->owe_trans_scan_freq);
870 	wpa_s->owe_trans_scan_freq = NULL;
871 #endif /* CONFIG_OWE */
872 
873 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
874 		wpabuf_free(wpa_s->links[i].ies);
875 		wpa_s->links[i].ies = NULL;
876 	}
877 }
878 
879 
880 /**
881  * wpa_clear_keys - Clear keys configured for the driver
882  * @wpa_s: Pointer to wpa_supplicant data
883  * @addr: Previously used BSSID or %NULL if not available
884  *
885  * This function clears the encryption keys that has been previously configured
886  * for the driver.
887  */
wpa_clear_keys(struct wpa_supplicant * wpa_s,const u8 * addr)888 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
889 {
890 	int i, max = 6;
891 
892 	/* MLME-DELETEKEYS.request
893 	 *
894 	 * For still associated MLO connections, group keys are per-link and at
895 	 * least in the case of Linux nl80211 interface, the cfg80211 validator
896 	 * rejects DEL_KEY for group keys with link_id=-1 when wdev->valid_links
897 	 * is set. Iterate over each valid link and pass the corresponding
898 	 * link_id so the keys are properly cleared.
899 	 */
900 	if (wpa_s->valid_links && wpa_s->wpa_state > WPA_ASSOCIATED) {
901 		int link_id;
902 
903 		for_each_link(wpa_s->valid_links, link_id) {
904 			for (i = 0; i < max; i++) {
905 				if (wpa_s->keys_cleared & BIT(i))
906 					continue;
907 				wpa_drv_set_key(wpa_s, link_id, WPA_ALG_NONE,
908 						NULL, i, 0, NULL, 0,
909 						NULL, 0, KEY_FLAG_GROUP);
910 			}
911 		}
912 	} else {
913 		for (i = 0; i < max; i++) {
914 			if (wpa_s->keys_cleared & BIT(i))
915 				continue;
916 			wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, i, 0,
917 					NULL, 0, NULL, 0, KEY_FLAG_GROUP);
918 		}
919 	}
920 	/* Pairwise Key ID 1 for Extended Key ID is tracked in bit 15 */
921 	if (~wpa_s->keys_cleared & (BIT(0) | BIT(15)) && addr &&
922 	    !is_zero_ether_addr(addr)) {
923 		if (!(wpa_s->keys_cleared & BIT(0)))
924 			wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, addr, 0, 0,
925 					NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
926 		if (!(wpa_s->keys_cleared & BIT(15)))
927 			wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, addr, 1, 0,
928 					NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
929 		/* MLME-SETPROTECTION.request(None) */
930 		wpa_drv_mlme_setprotection(
931 			wpa_s, addr,
932 			MLME_SETPROTECTION_PROTECT_TYPE_NONE,
933 			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
934 	}
935 	wpa_s->keys_cleared = (u32) -1;
936 }
937 
938 
939 /**
940  * wpa_supplicant_state_txt - Get the connection state name as a text string
941  * @state: State (wpa_state; WPA_*)
942  * Returns: The state name as a printable text string
943  */
wpa_supplicant_state_txt(enum wpa_states state)944 const char * wpa_supplicant_state_txt(enum wpa_states state)
945 {
946 	switch (state) {
947 	case WPA_DISCONNECTED:
948 		return "DISCONNECTED";
949 	case WPA_INACTIVE:
950 		return "INACTIVE";
951 	case WPA_INTERFACE_DISABLED:
952 		return "INTERFACE_DISABLED";
953 	case WPA_SCANNING:
954 		return "SCANNING";
955 	case WPA_AUTHENTICATING:
956 		return "AUTHENTICATING";
957 	case WPA_ASSOCIATING:
958 		return "ASSOCIATING";
959 	case WPA_ASSOCIATED:
960 		return "ASSOCIATED";
961 	case WPA_4WAY_HANDSHAKE:
962 		return "4WAY_HANDSHAKE";
963 	case WPA_GROUP_HANDSHAKE:
964 		return "GROUP_HANDSHAKE";
965 	case WPA_COMPLETED:
966 		return "COMPLETED";
967 	default:
968 		return "UNKNOWN";
969 	}
970 }
971 
972 
973 #ifdef CONFIG_BGSCAN
974 
wpa_supplicant_stop_bgscan(struct wpa_supplicant * wpa_s)975 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
976 {
977 	if (wpa_s->bgscan_ssid) {
978 		bgscan_deinit(wpa_s);
979 		wpa_s->bgscan_ssid = NULL;
980 	}
981 }
982 
983 
984 /**
985  * wpa_supplicant_reset_bgscan - Reset the bgscan for the current SSID.
986  * @wpa_s: Pointer to the wpa_supplicant data
987  *
988  * Stop, start, or reconfigure the scan parameters depending on the method.
989  */
wpa_supplicant_reset_bgscan(struct wpa_supplicant * wpa_s)990 void wpa_supplicant_reset_bgscan(struct wpa_supplicant *wpa_s)
991 {
992 	const char *name;
993 
994 	if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
995 		name = wpa_s->current_ssid->bgscan;
996 	else
997 		name = wpa_s->conf->bgscan;
998 	if (!name || name[0] == '\0') {
999 		wpa_supplicant_stop_bgscan(wpa_s);
1000 		return;
1001 	}
1002 	if (wpas_driver_bss_selection(wpa_s))
1003 		return;
1004 #ifdef CONFIG_P2P
1005 	if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
1006 		return;
1007 #endif /* CONFIG_P2P */
1008 
1009 	bgscan_deinit(wpa_s);
1010 	if (wpa_s->current_ssid) {
1011 		if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
1012 			wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
1013 				"bgscan");
1014 			/*
1015 			 * Live without bgscan; it is only used as a roaming
1016 			 * optimization, so the initial connection is not
1017 			 * affected.
1018 			 */
1019 		} else {
1020 			struct wpa_scan_results *scan_res;
1021 			wpa_s->bgscan_ssid = wpa_s->current_ssid;
1022 			scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
1023 								   0, NULL);
1024 			if (scan_res) {
1025 				bgscan_notify_scan(wpa_s, scan_res);
1026 				wpa_scan_results_free(scan_res);
1027 			}
1028 		}
1029 	} else
1030 		wpa_s->bgscan_ssid = NULL;
1031 }
1032 
1033 #endif /* CONFIG_BGSCAN */
1034 
1035 
wpa_supplicant_start_autoscan(struct wpa_supplicant * wpa_s)1036 static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
1037 {
1038 	if (autoscan_init(wpa_s, 0))
1039 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
1040 }
1041 
1042 
wpa_supplicant_stop_autoscan(struct wpa_supplicant * wpa_s)1043 static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
1044 {
1045 	autoscan_deinit(wpa_s);
1046 }
1047 
1048 
wpa_supplicant_reinit_autoscan(struct wpa_supplicant * wpa_s)1049 void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
1050 {
1051 	if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1052 	    wpa_s->wpa_state == WPA_SCANNING) {
1053 		autoscan_deinit(wpa_s);
1054 		wpa_supplicant_start_autoscan(wpa_s);
1055 	}
1056 }
1057 
1058 
wpas_verify_ssid_beacon(void * eloop_ctx,void * timeout_ctx)1059 static void wpas_verify_ssid_beacon(void *eloop_ctx, void *timeout_ctx)
1060 {
1061 	struct wpa_supplicant *wpa_s = eloop_ctx;
1062 	struct wpa_bss *bss;
1063 	const u8 *ssid;
1064 	size_t ssid_len;
1065 
1066 	if (!wpa_s->current_ssid || !wpa_s->current_bss)
1067 		return;
1068 
1069 	ssid = wpa_s->current_bss->ssid;
1070 	ssid_len = wpa_s->current_bss->ssid_len;
1071 
1072 	if (wpa_s->current_ssid->ssid_len &&
1073 	    (wpa_s->current_ssid->ssid_len != ssid_len ||
1074 	     os_memcmp(wpa_s->current_ssid->ssid, ssid, ssid_len) != 0))
1075 		return;
1076 
1077 	if (wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
1078 	    !wpa_s->bigtk_set || wpa_s->ssid_verified)
1079 		return;
1080 
1081 	wpa_printf(MSG_DEBUG,
1082 		   "SSID not yet verified; check if the driver has received a verified Beacon frame");
1083 	if (wpa_supplicant_update_scan_results(wpa_s, wpa_s->bssid) < 0)
1084 		return;
1085 
1086 	/* wpa->current_bss might have changed due to memory reallocation, so
1087 	 * need to update ssid/ssid_len */
1088 	if (!wpa_s->current_bss)
1089 		return;
1090 	ssid = wpa_s->current_bss->ssid;
1091 	ssid_len = wpa_s->current_bss->ssid_len;
1092 
1093 	bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->bssid);
1094 	if (!bss)
1095 		return;
1096 	wpa_printf(MSG_DEBUG, "The current beacon time stamp: 0x%llx",
1097 		   (long long unsigned int) bss->tsf);
1098 	if (bss->tsf > wpa_s->first_beacon_tsf) {
1099 		const u8 *ie;
1100 
1101 		wpa_printf(MSG_DEBUG,
1102 			   "Verified Beacon frame has been received");
1103 		wpa_s->beacons_checked++;
1104 
1105 		ie = wpa_bss_get_ie_beacon(bss, WLAN_EID_SSID);
1106 		if (ie && ie[1] == ssid_len &&
1107 		    os_memcmp(&ie[2], ssid, ssid_len) == 0) {
1108 			wpa_printf(MSG_DEBUG,
1109 				   "SSID verified based on a Beacon frame and beacon protection");
1110 			wpa_s->ssid_verified = true;
1111 			return;
1112 		}
1113 
1114 		/* TODO: Multiple BSSID element */
1115 	}
1116 
1117 	if (wpa_s->beacons_checked < 16) {
1118 		eloop_register_timeout(wpa_s->next_beacon_check, 0,
1119 				       wpas_verify_ssid_beacon, wpa_s, NULL);
1120 		wpa_s->next_beacon_check++;
1121 	}
1122 }
1123 
1124 
wpas_verify_ssid_beacon_prot(struct wpa_supplicant * wpa_s)1125 static void wpas_verify_ssid_beacon_prot(struct wpa_supplicant *wpa_s)
1126 {
1127 	struct wpa_bss *bss;
1128 
1129 	wpa_printf(MSG_DEBUG,
1130 		   "SSID not yet verified; try to verify using beacon protection");
1131 	/* Fetch the current scan result which is likely based on not yet
1132 	 * verified payload since the current BIGTK was just received. Any
1133 	 * newer update in the future with a larger timestamp value is an
1134 	 * indication that a verified Beacon frame has been received. */
1135 	if (wpa_supplicant_update_scan_results(wpa_s, wpa_s->bssid) < 0)
1136 		return;
1137 
1138 	bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->bssid);
1139 	if (!bss)
1140 		return;
1141 	wpa_printf(MSG_DEBUG, "The initial beacon time stamp: 0x%llx",
1142 		   (long long unsigned int) bss->tsf);
1143 	wpa_s->first_beacon_tsf = bss->tsf;
1144 	wpa_s->beacons_checked = 0;
1145 	wpa_s->next_beacon_check = 1;
1146 	eloop_cancel_timeout(wpas_verify_ssid_beacon, wpa_s, NULL);
1147 	eloop_register_timeout(1, 0, wpas_verify_ssid_beacon, wpa_s, NULL);
1148 }
1149 
1150 
1151 /**
1152  * wpa_supplicant_set_state - Set current connection state
1153  * @wpa_s: Pointer to wpa_supplicant data
1154  * @state: The new connection state
1155  *
1156  * This function is called whenever the connection state changes, e.g.,
1157  * association is completed for WPA/WPA2 4-Way Handshake is started.
1158  */
wpa_supplicant_set_state(struct wpa_supplicant * wpa_s,enum wpa_states state)1159 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
1160 			      enum wpa_states state)
1161 {
1162 	enum wpa_states old_state = wpa_s->wpa_state;
1163 	int new_connection = wpa_s->new_connection;
1164 #if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1165 	bool update_fils_connect_params = false;
1166 #endif /* CONFIG_FILS && IEEE8021X_EAPOL */
1167 
1168 	wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
1169 		wpa_supplicant_state_txt(wpa_s->wpa_state),
1170 		wpa_supplicant_state_txt(state));
1171 
1172 	if (state == WPA_COMPLETED &&
1173 	    os_reltime_initialized(&wpa_s->roam_start)) {
1174 		os_reltime_age(&wpa_s->roam_start, &wpa_s->roam_time);
1175 		wpa_s->roam_start.sec = 0;
1176 		wpa_s->roam_start.usec = 0;
1177 		wpas_notify_auth_changed(wpa_s);
1178 		wpas_notify_roam_time(wpa_s);
1179 		wpas_notify_roam_complete(wpa_s);
1180 	} else if (state == WPA_DISCONNECTED &&
1181 		   os_reltime_initialized(&wpa_s->roam_start)) {
1182 		wpa_s->roam_start.sec = 0;
1183 		wpa_s->roam_start.usec = 0;
1184 		wpa_s->roam_time.sec = 0;
1185 		wpa_s->roam_time.usec = 0;
1186 		wpas_notify_roam_complete(wpa_s);
1187 	}
1188 
1189 	if (state == WPA_INTERFACE_DISABLED) {
1190 		/* Assure normal scan when interface is restored */
1191 		wpa_s->normal_scans = 0;
1192 
1193 		/*
1194 		 * A NAN management interface is not expected to be disabled. If
1195 		 * it disabled, it means that NAN functionality is no longer
1196 		 * possible so deinit (which would also stop any ongoing NAN
1197 		 * operations).
1198 		 */
1199 		if (wpa_s->nan_mgmt)
1200 			wpas_nan_deinit(wpa_s);
1201 	}
1202 
1203 	if (state == WPA_COMPLETED) {
1204 		wpas_connect_work_done(wpa_s);
1205 		/* Reinitialize normal_scan counter */
1206 		wpa_s->normal_scans = 0;
1207 	}
1208 
1209 #ifdef CONFIG_P2P
1210 	/*
1211 	 * P2PS client has to reply to Probe Request frames received on the
1212 	 * group operating channel. Enable Probe Request frame reporting for
1213 	 * P2P connected client in case p2p_cli_probe configuration property is
1214 	 * set to 1.
1215 	 */
1216 	if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
1217 	    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1218 	    wpa_s->current_ssid->p2p_group) {
1219 		if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
1220 			wpa_dbg(wpa_s, MSG_DEBUG,
1221 				"P2P: Enable CLI Probe Request RX reporting");
1222 			wpa_s->p2p_cli_probe =
1223 				wpa_drv_probe_req_report(wpa_s, 1) >= 0;
1224 		} else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
1225 			wpa_dbg(wpa_s, MSG_DEBUG,
1226 				"P2P: Disable CLI Probe Request RX reporting");
1227 			wpa_s->p2p_cli_probe = 0;
1228 			wpa_drv_probe_req_report(wpa_s, 0);
1229 		}
1230 	}
1231 #endif /* CONFIG_P2P */
1232 
1233 	if (state != WPA_SCANNING)
1234 		wpa_supplicant_notify_scanning(wpa_s, 0);
1235 
1236 	if (state == WPA_COMPLETED && wpa_s->new_connection) {
1237 		struct wpa_ssid *ssid = wpa_s->current_ssid;
1238 		int fils_hlp_sent = 0;
1239 		char mld_addr[50];
1240 
1241 		mld_addr[0] = '\0';
1242 		if (wpa_s->valid_links)
1243 			os_snprintf(mld_addr, sizeof(mld_addr),
1244 				    " ap_mld_addr=" MACSTR,
1245 				    MAC2STR(wpa_s->ap_mld_addr));
1246 
1247 #ifdef CONFIG_SME
1248 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1249 		    wpa_auth_alg_fils(wpa_s->sme.auth_alg))
1250 			fils_hlp_sent = 1;
1251 #endif /* CONFIG_SME */
1252 		if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1253 		    wpa_auth_alg_fils(wpa_s->auth_alg))
1254 			fils_hlp_sent = 1;
1255 
1256 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1257 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
1258 			MACSTR " completed [id=%d id_str=%s%s]%s",
1259 			MAC2STR(wpa_s->bssid),
1260 			ssid ? ssid->id : -1,
1261 			ssid && ssid->id_str ? ssid->id_str : "",
1262 			fils_hlp_sent ? " FILS_HLP_SENT" : "", mld_addr);
1263 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1264 		wpas_clear_temp_disabled(wpa_s, ssid, 1);
1265 		wpa_s->consecutive_conn_failures = 0;
1266 		wpa_s->new_connection = 0;
1267 		wpa_drv_set_operstate(wpa_s, 1);
1268 #ifndef IEEE8021X_EAPOL
1269 		wpa_drv_set_supp_port(wpa_s, 1);
1270 #endif /* IEEE8021X_EAPOL */
1271 		wpa_s->after_wps = 0;
1272 		wpa_s->known_wps_freq = 0;
1273 		wpas_p2p_completed(wpa_s);
1274 
1275 		sme_sched_obss_scan(wpa_s, 1);
1276 
1277 #if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1278 		if (!fils_hlp_sent && ssid && ssid->eap.erp)
1279 			update_fils_connect_params = true;
1280 #endif /* CONFIG_FILS && IEEE8021X_EAPOL */
1281 #ifdef CONFIG_OWE
1282 		if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_OWE))
1283 			wpas_update_owe_connect_params(wpa_s);
1284 #endif /* CONFIG_OWE */
1285 	} else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
1286 		   state == WPA_ASSOCIATED) {
1287 		wpa_s->new_connection = 1;
1288 		wpa_drv_set_operstate(wpa_s, 0);
1289 #ifndef IEEE8021X_EAPOL
1290 		wpa_drv_set_supp_port(wpa_s, 0);
1291 #endif /* IEEE8021X_EAPOL */
1292 		sme_sched_obss_scan(wpa_s, 0);
1293 	}
1294 	wpa_s->wpa_state = state;
1295 
1296 	if (state == WPA_COMPLETED && new_connection &&
1297 	    wpa_s->conf->wfa_gen_capa == WFA_GEN_CAPA_PROTECTED &&
1298 	    pmf_in_use(wpa_s, wpa_s->bssid)) {
1299 		eloop_cancel_timeout(wpas_wfa_capab_tx, wpa_s, NULL);
1300 		eloop_register_timeout(0, 100000, wpas_wfa_capab_tx,
1301 				       wpa_s, NULL);
1302 	}
1303 
1304 #ifndef CONFIG_NO_ROBUST_AV
1305 	if (state == WPA_COMPLETED && dl_list_len(&wpa_s->active_scs_ids) &&
1306 	    wpa_s->scs_reconfigure)
1307 		wpas_scs_reconfigure(wpa_s);
1308 #endif /* CONFIG_NO_ROBUST_AV */
1309 
1310 #ifdef CONFIG_BGSCAN
1311 	if (state == WPA_COMPLETED && wpa_s->current_ssid != wpa_s->bgscan_ssid)
1312 		wpa_supplicant_reset_bgscan(wpa_s);
1313 	else if (state < WPA_ASSOCIATED)
1314 		wpa_supplicant_stop_bgscan(wpa_s);
1315 #endif /* CONFIG_BGSCAN */
1316 
1317 	if (state > WPA_SCANNING)
1318 		wpa_supplicant_stop_autoscan(wpa_s);
1319 
1320 	if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
1321 		wpa_supplicant_start_autoscan(wpa_s);
1322 
1323 	if (state == WPA_COMPLETED || state == WPA_INTERFACE_DISABLED ||
1324 	    state == WPA_INACTIVE)
1325 		wnm_btm_reset(wpa_s);
1326 
1327 #ifndef CONFIG_NO_WMM_AC
1328 	if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
1329 		wmm_ac_notify_disassoc(wpa_s);
1330 #endif /* CONFIG_NO_WMM_AC */
1331 
1332 	if (wpa_s->wpa_state != old_state) {
1333 		wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1334 
1335 		/*
1336 		 * Notify the P2P Device interface about a state change in one
1337 		 * of the interfaces.
1338 		 */
1339 		wpas_p2p_indicate_state_change(wpa_s);
1340 
1341 		if (wpa_s->wpa_state == WPA_COMPLETED ||
1342 		    old_state == WPA_COMPLETED)
1343 			wpas_notify_auth_changed(wpa_s);
1344 #ifdef CONFIG_DPP2
1345 		if (wpa_s->wpa_state == WPA_COMPLETED)
1346 			wpas_dpp_connected(wpa_s);
1347 #endif /* CONFIG_DPP2 */
1348 
1349 		if (wpa_s->wpa_state == WPA_COMPLETED &&
1350 		    wpa_s->bigtk_set && !wpa_s->ssid_verified)
1351 			wpas_verify_ssid_beacon_prot(wpa_s);
1352 	}
1353 #if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1354 	if (update_fils_connect_params)
1355 		wpas_update_fils_connect_params(wpa_s);
1356 #endif /* CONFIG_FILS && IEEE8021X_EAPOL */
1357 
1358 	wpas_nan_usd_state_change_notif(wpa_s);
1359 }
1360 
1361 
wpa_supplicant_terminate_proc(struct wpa_global * global)1362 void wpa_supplicant_terminate_proc(struct wpa_global *global)
1363 {
1364 	int pending = 0;
1365 #ifdef CONFIG_WPS
1366 	struct wpa_supplicant *wpa_s = global->ifaces;
1367 	while (wpa_s) {
1368 		struct wpa_supplicant *next = wpa_s->next;
1369 		if (wpas_wps_terminate_pending(wpa_s) == 1)
1370 			pending = 1;
1371 #ifdef CONFIG_P2P
1372 		if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
1373 		    (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
1374 			wpas_p2p_disconnect(wpa_s);
1375 #endif /* CONFIG_P2P */
1376 		wpa_s = next;
1377 	}
1378 #endif /* CONFIG_WPS */
1379 	if (pending)
1380 		return;
1381 	eloop_terminate();
1382 }
1383 
1384 
wpa_supplicant_terminate(int sig,void * signal_ctx)1385 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
1386 {
1387 	struct wpa_global *global = signal_ctx;
1388 	wpa_supplicant_terminate_proc(global);
1389 }
1390 
1391 
wpa_supplicant_clear_status(struct wpa_supplicant * wpa_s)1392 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
1393 {
1394 	enum wpa_states old_state = wpa_s->wpa_state;
1395 	enum wpa_states new_state;
1396 
1397 	if (old_state == WPA_SCANNING)
1398 		new_state = WPA_SCANNING;
1399 	else
1400 		new_state = WPA_DISCONNECTED;
1401 
1402 	wpa_s->pairwise_cipher = 0;
1403 	wpa_s->group_cipher = 0;
1404 	wpa_s->mgmt_group_cipher = 0;
1405 	wpa_s->key_mgmt = 0;
1406 	wpa_s->allowed_key_mgmts = 0;
1407 	if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
1408 		wpa_supplicant_set_state(wpa_s, new_state);
1409 
1410 	if (wpa_s->wpa_state != old_state)
1411 		wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1412 }
1413 
1414 
1415 /**
1416  * wpa_supplicant_reload_configuration - Reload configuration data
1417  * @wpa_s: Pointer to wpa_supplicant data
1418  * Returns: 0 on success or -1 if configuration parsing failed
1419  *
1420  * This function can be used to request that the configuration data is reloaded
1421  * (e.g., after configuration file change). This function is reloading
1422  * configuration only for one interface, so this may need to be called multiple
1423  * times if %wpa_supplicant is controlling multiple interfaces and all
1424  * interfaces need reconfiguration.
1425  */
wpa_supplicant_reload_configuration(struct wpa_supplicant * wpa_s)1426 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
1427 {
1428 	struct wpa_config *conf;
1429 	int reconf_ctrl;
1430 	int old_ap_scan;
1431 
1432 	if (wpa_s->confname == NULL)
1433 		return -1;
1434 	conf = wpa_config_read(wpa_s->confname, NULL, false,
1435 			       wpa_s->global->params.show_details);
1436 	if (conf == NULL) {
1437 		wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
1438 			"file '%s' - exiting", wpa_s->confname);
1439 		return -1;
1440 	}
1441 	if (wpa_s->confanother &&
1442 	    !wpa_config_read(wpa_s->confanother, conf, true,
1443 			     wpa_s->global->params.show_details)) {
1444 		wpa_msg(wpa_s, MSG_ERROR,
1445 			"Failed to parse the configuration file '%s' - exiting",
1446 			wpa_s->confanother);
1447 		return -1;
1448 	}
1449 
1450 	conf->changed_parameters = (unsigned int) -1;
1451 
1452 	reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
1453 		|| (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
1454 		    os_strcmp(conf->ctrl_interface,
1455 			      wpa_s->conf->ctrl_interface) != 0);
1456 
1457 	if (reconf_ctrl) {
1458 		wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
1459 		wpa_s->ctrl_iface = NULL;
1460 	}
1461 
1462 	eapol_sm_invalidate_cached_session(wpa_s->eapol);
1463 	if (wpa_s->current_ssid) {
1464 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1465 			wpa_s->own_disconnect_req = 1;
1466 		wpa_supplicant_deauthenticate(wpa_s,
1467 					      WLAN_REASON_DEAUTH_LEAVING);
1468 	}
1469 
1470 	/*
1471 	 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
1472 	 * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
1473 	 */
1474 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1475 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
1476 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
1477 		/*
1478 		 * Clear forced success to clear EAP state for next
1479 		 * authentication.
1480 		 */
1481 		eapol_sm_notify_eap_success(wpa_s->eapol, false);
1482 	}
1483 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1484 	wpa_sm_set_config(wpa_s->wpa, NULL);
1485 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
1486 	wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1487 	rsn_preauth_deinit(wpa_s->wpa);
1488 
1489 	old_ap_scan = wpa_s->conf->ap_scan;
1490 	wpa_config_free(wpa_s->conf);
1491 	wpa_s->conf = conf;
1492 	if (old_ap_scan != wpa_s->conf->ap_scan)
1493 		wpas_notify_ap_scan_changed(wpa_s);
1494 
1495 	if (reconf_ctrl)
1496 		wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1497 
1498 	wpa_supplicant_update_config(wpa_s);
1499 
1500 	wpa_supplicant_clear_status(wpa_s);
1501 	if (wpa_supplicant_enabled_networks(wpa_s)) {
1502 		wpa_s->reassociate = 1;
1503 		wpa_supplicant_req_scan(wpa_s, 0, 0);
1504 	}
1505 	wpa_bssid_ignore_clear(wpa_s);
1506 	wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
1507 	return 0;
1508 }
1509 
1510 
wpa_supplicant_reconfig(int sig,void * signal_ctx)1511 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
1512 {
1513 	struct wpa_global *global = signal_ctx;
1514 	struct wpa_supplicant *wpa_s;
1515 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1516 		wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
1517 			sig);
1518 		if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1519 			wpa_supplicant_terminate_proc(global);
1520 		}
1521 	}
1522 
1523 	if (wpa_debug_reopen_file() < 0) {
1524 		/* Ignore errors since we cannot really do much to fix this */
1525 		wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
1526 	}
1527 }
1528 
1529 
wpa_supplicant_suites_from_ai(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_ie_data * ie)1530 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1531 					 struct wpa_ssid *ssid,
1532 					 struct wpa_ie_data *ie)
1533 {
1534 	int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1535 	if (ret) {
1536 		if (ret == -2) {
1537 			wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1538 				"from association info");
1539 		}
1540 		return -1;
1541 	}
1542 
1543 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
1544 		"cipher suites");
1545 	if (!(ie->group_cipher & ssid->group_cipher)) {
1546 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1547 			"cipher 0x%x (mask 0x%x) - reject",
1548 			ie->group_cipher, ssid->group_cipher);
1549 		return -1;
1550 	}
1551 	if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1552 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1553 			"cipher 0x%x (mask 0x%x) - reject",
1554 			ie->pairwise_cipher, ssid->pairwise_cipher);
1555 		return -1;
1556 	}
1557 	if (!(ie->key_mgmt & ssid->key_mgmt)) {
1558 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1559 			"management 0x%x (mask 0x%x) - reject",
1560 			ie->key_mgmt, ssid->key_mgmt);
1561 		return -1;
1562 	}
1563 
1564 	if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
1565 	    wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
1566 		wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1567 			"that does not support management frame protection - "
1568 			"reject");
1569 		return -1;
1570 	}
1571 
1572 	return 0;
1573 }
1574 
1575 
matching_ciphers(struct wpa_ssid * ssid,struct wpa_ie_data * ie,int freq)1576 static int matching_ciphers(struct wpa_ssid *ssid, struct wpa_ie_data *ie,
1577 			    int freq)
1578 {
1579 	if (!ie->has_group)
1580 		ie->group_cipher = wpa_default_rsn_cipher(freq);
1581 	if (!ie->has_pairwise)
1582 		ie->pairwise_cipher = wpa_default_rsn_cipher(freq);
1583 	return (ie->group_cipher & ssid->group_cipher) &&
1584 		(ie->pairwise_cipher & ssid->pairwise_cipher);
1585 }
1586 
1587 
wpas_set_mgmt_group_cipher(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_ie_data * ie)1588 void wpas_set_mgmt_group_cipher(struct wpa_supplicant *wpa_s,
1589 				struct wpa_ssid *ssid, struct wpa_ie_data *ie)
1590 {
1591 	int sel;
1592 
1593 	sel = ie->mgmt_group_cipher;
1594 	if (ssid->group_mgmt_cipher)
1595 		sel &= ssid->group_mgmt_cipher;
1596 	if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
1597 	    !(ie->capabilities & WPA_CAPABILITY_MFPC))
1598 		sel = 0;
1599 	wpa_dbg(wpa_s, MSG_DEBUG,
1600 		"WPA: AP mgmt_group_cipher 0x%x network profile mgmt_group_cipher 0x%x; available mgmt_group_cipher 0x%x",
1601 		ie->mgmt_group_cipher, ssid->group_mgmt_cipher, sel);
1602 	if (sel & WPA_CIPHER_AES_128_CMAC) {
1603 		wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1604 		wpa_dbg(wpa_s, MSG_DEBUG,
1605 			"WPA: using MGMT group cipher AES-128-CMAC");
1606 	} else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1607 		wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1608 		wpa_dbg(wpa_s, MSG_DEBUG,
1609 			"WPA: using MGMT group cipher BIP-GMAC-128");
1610 	} else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1611 		wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1612 		wpa_dbg(wpa_s, MSG_DEBUG,
1613 			"WPA: using MGMT group cipher BIP-GMAC-256");
1614 	} else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1615 		wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1616 		wpa_dbg(wpa_s, MSG_DEBUG,
1617 			"WPA: using MGMT group cipher BIP-CMAC-256");
1618 	} else {
1619 		wpa_s->mgmt_group_cipher = 0;
1620 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1621 	}
1622 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1623 			 wpa_s->mgmt_group_cipher);
1624 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1625 			 wpas_get_ssid_pmf(wpa_s, ssid));
1626 }
1627 
1628 /**
1629  * wpa_supplicant_get_psk - Get PSK from config or external database
1630  * @wpa_s: Pointer to wpa_supplicant data
1631  * @bss: Scan results for the selected BSS, or %NULL if not available
1632  * @ssid: Configuration data for the selected network
1633  * @psk: Buffer for the PSK
1634  * Returns: 0 on success or -1 if configuration parsing failed
1635  *
1636  * This function obtains the PSK for a network, either included inline in the
1637  * config or retrieved from an external database.
1638  */
wpa_supplicant_get_psk(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,u8 * psk)1639 static int wpa_supplicant_get_psk(struct wpa_supplicant *wpa_s,
1640 				  struct wpa_bss *bss, struct wpa_ssid *ssid,
1641 				  u8 *psk)
1642 {
1643 	if (ssid->psk_set) {
1644 		wpa_hexdump_key(MSG_MSGDUMP, "PSK (set in config)",
1645 				ssid->psk, PMK_LEN);
1646 		os_memcpy(psk, ssid->psk, PMK_LEN);
1647 		return 0;
1648 	}
1649 
1650 #ifndef CONFIG_NO_PBKDF2
1651 	if (bss && ssid->bssid_set && ssid->ssid_len == 0 && ssid->passphrase) {
1652 		if (pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1653 				4096, psk, PMK_LEN) != 0) {
1654 			wpa_msg(wpa_s, MSG_WARNING, "Error in pbkdf2_sha1()");
1655 			return -1;
1656 		}
1657 		wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1658 				psk, PMK_LEN);
1659 		return 0;
1660 	}
1661 #endif /* CONFIG_NO_PBKDF2 */
1662 
1663 #ifdef CONFIG_EXT_PASSWORD
1664 	if (ssid->ext_psk) {
1665 		struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1666 						     ssid->ext_psk);
1667 		char pw_str[64 + 1];
1668 
1669 		if (!pw) {
1670 			wpa_msg(wpa_s, MSG_INFO,
1671 				"EXT PW: No PSK found from external storage");
1672 			return -1;
1673 		}
1674 
1675 		if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1676 			wpa_msg(wpa_s, MSG_INFO,
1677 				"EXT PW: Unexpected PSK length %d in external storage",
1678 				(int) wpabuf_len(pw));
1679 			ext_password_free(pw);
1680 			return -1;
1681 		}
1682 
1683 		os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1684 		pw_str[wpabuf_len(pw)] = '\0';
1685 
1686 #ifndef CONFIG_NO_PBKDF2
1687 		if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1688 		{
1689 			if (pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1690 					4096, psk, PMK_LEN) != 0) {
1691 				wpa_msg(wpa_s, MSG_WARNING,
1692 					"Error in pbkdf2_sha1()");
1693 				forced_memzero(pw_str, sizeof(pw_str));
1694 				ext_password_free(pw);
1695 				return -1;
1696 			}
1697 			wpa_hexdump_key(MSG_MSGDUMP,
1698 					"PSK (from external passphrase)",
1699 					psk, PMK_LEN);
1700 		} else
1701 #endif /* CONFIG_NO_PBKDF2 */
1702 		if (wpabuf_len(pw) == 2 * PMK_LEN) {
1703 			if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1704 				wpa_msg(wpa_s, MSG_INFO,
1705 					"EXT PW: Invalid PSK hex string");
1706 				forced_memzero(pw_str, sizeof(pw_str));
1707 				ext_password_free(pw);
1708 				return -1;
1709 			}
1710 			wpa_hexdump_key(MSG_MSGDUMP, "PSK (from external PSK)",
1711 					psk, PMK_LEN);
1712 		} else {
1713 			wpa_msg(wpa_s, MSG_INFO,
1714 				"EXT PW: No suitable PSK available");
1715 			forced_memzero(pw_str, sizeof(pw_str));
1716 			ext_password_free(pw);
1717 			return -1;
1718 		}
1719 
1720 		forced_memzero(pw_str, sizeof(pw_str));
1721 		ext_password_free(pw);
1722 
1723 		return 0;
1724 	}
1725 #endif /* CONFIG_EXT_PASSWORD */
1726 
1727 	return -1;
1728 }
1729 
1730 
wpas_update_allowed_key_mgmt(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)1731 static void wpas_update_allowed_key_mgmt(struct wpa_supplicant *wpa_s,
1732 					 struct wpa_ssid *ssid)
1733 {
1734 	int akm_count = wpa_s->max_num_akms;
1735 	u8 capab = 0;
1736 #ifdef CONFIG_SAE
1737 	enum sae_pwe sae_pwe;
1738 #endif /* CONFIG_SAE */
1739 
1740 	if (akm_count < 2)
1741 		return;
1742 
1743 	akm_count--;
1744 	wpa_s->allowed_key_mgmts = 0;
1745 	switch (wpa_s->key_mgmt) {
1746 	case WPA_KEY_MGMT_PSK:
1747 		if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1748 			akm_count--;
1749 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1750 		}
1751 		if (!akm_count)
1752 			break;
1753 		if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1754 			akm_count--;
1755 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1756 		}
1757 		if (!akm_count)
1758 			break;
1759 		if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1760 			wpa_s->allowed_key_mgmts |=
1761 				WPA_KEY_MGMT_PSK_SHA256;
1762 		break;
1763 	case WPA_KEY_MGMT_PSK_SHA256:
1764 		if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1765 			akm_count--;
1766 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1767 		}
1768 		if (!akm_count)
1769 			break;
1770 		if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1771 			akm_count--;
1772 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1773 		}
1774 		if (!akm_count)
1775 			break;
1776 		if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1777 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1778 		break;
1779 	case WPA_KEY_MGMT_SAE:
1780 		if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1781 			akm_count--;
1782 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1783 		}
1784 		if (!akm_count)
1785 			break;
1786 		if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1787 			akm_count--;
1788 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1789 		}
1790 		if (!akm_count)
1791 			break;
1792 		if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1793 			wpa_s->allowed_key_mgmts |=
1794 				WPA_KEY_MGMT_PSK_SHA256;
1795 		break;
1796 	case WPA_KEY_MGMT_SAE_EXT_KEY:
1797 		if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1798 			akm_count--;
1799 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1800 		}
1801 		if (!akm_count)
1802 			break;
1803 		if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1804 			akm_count--;
1805 			wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1806 		}
1807 		if (!akm_count)
1808 			break;
1809 		if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1810 			wpa_s->allowed_key_mgmts |=
1811 				WPA_KEY_MGMT_PSK_SHA256;
1812 		break;
1813 	default:
1814 		return;
1815 	}
1816 
1817 #ifdef CONFIG_SAE
1818 	sae_pwe = wpas_get_ssid_sae_pwe(wpa_s, ssid);
1819 	if (sae_pwe != SAE_PWE_HUNT_AND_PECK &&
1820 	    sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1821 		capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
1822 #ifdef CONFIG_SAE_PK
1823 	if (ssid->sae_pk)
1824 		capab |= BIT(WLAN_RSNX_CAPAB_SAE_PK);
1825 #endif /* CONFIG_SAE_PK */
1826 #endif /* CONFIG_SAE */
1827 
1828 	if (!((wpa_s->allowed_key_mgmts &
1829 	       (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY)) && capab))
1830 		return;
1831 
1832 	if (!wpa_s->rsnxe_len) {
1833 		wpa_s->rsnxe_len = 3;
1834 		wpa_s->rsnxe[0] = WLAN_EID_RSNX;
1835 		wpa_s->rsnxe[1] = 1;
1836 		wpa_s->rsnxe[2] = 0;
1837 	}
1838 
1839 	wpa_s->rsnxe[2] |= capab;
1840 }
1841 
1842 
1843 /**
1844  * wpa_supplicant_set_suites - Set authentication and encryption parameters
1845  * @wpa_s: Pointer to wpa_supplicant data
1846  * @bss: Scan results for the selected BSS, or %NULL if not available
1847  * @ssid: Configuration data for the selected network
1848  * @wpa_ie: Buffer for the WPA/RSN IE
1849  * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1850  * used buffer length in case the functions returns success.
1851  * @skip_default_rsne: Whether to skip setting of the default RSNE/RSNXE
1852  * Returns: 0 on success or -1 on failure
1853  *
1854  * This function is used to configure authentication and encryption parameters
1855  * based on the network configuration and scan result for the selected BSS (if
1856  * available).
1857  */
wpa_supplicant_set_suites(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,u8 * wpa_ie,size_t * wpa_ie_len,bool skip_default_rsne)1858 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1859 			      struct wpa_bss *bss, struct wpa_ssid *ssid,
1860 			      u8 *wpa_ie, size_t *wpa_ie_len,
1861 			      bool skip_default_rsne)
1862 {
1863 	struct wpa_ie_data ie;
1864 	int sel, proto;
1865 #ifdef CONFIG_SAE
1866 	enum sae_pwe sae_pwe;
1867 #endif /* CONFIG_SAE */
1868 	const u8 *bss_wpa, *bss_rsn, *bss_rsnx;
1869 	bool wmm;
1870 	struct rsn_pmksa_cache_entry *pmksa;
1871 
1872 	if (bss) {
1873 		bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1874 		bss_rsn = wpa_bss_get_rsne(wpa_s, bss, ssid, false);
1875 		bss_rsnx = wpa_bss_get_rsnxe(wpa_s, bss, ssid, false);
1876 	} else {
1877 		bss_wpa = bss_rsn = bss_rsnx = NULL;
1878 	}
1879 
1880 	if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
1881 	    wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1882 	    matching_ciphers(ssid, &ie, bss->freq) &&
1883 	    (ie.key_mgmt & ssid->key_mgmt)) {
1884 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1885 		proto = WPA_PROTO_RSN;
1886 	} else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
1887 		   wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
1888 		   (ie.group_cipher & ssid->group_cipher) &&
1889 		   (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1890 		   (ie.key_mgmt & ssid->key_mgmt)) {
1891 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1892 		proto = WPA_PROTO_WPA;
1893 	} else if (bss) {
1894 		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1895 		wpa_dbg(wpa_s, MSG_DEBUG,
1896 			"WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1897 			ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
1898 			ssid->key_mgmt);
1899 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s",
1900 			MAC2STR(bss->bssid),
1901 			wpa_ssid_txt(bss->ssid, bss->ssid_len),
1902 			bss_wpa ? " WPA" : "",
1903 			bss_rsn ? " RSN" : "");
1904 		if (bss_rsn) {
1905 			wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
1906 			if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
1907 				wpa_dbg(wpa_s, MSG_DEBUG,
1908 					"Could not parse RSN element");
1909 			} else {
1910 				wpa_dbg(wpa_s, MSG_DEBUG,
1911 					"RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1912 					ie.pairwise_cipher, ie.group_cipher,
1913 					ie.key_mgmt);
1914 			}
1915 		}
1916 		if (bss_wpa) {
1917 			wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
1918 			if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
1919 				wpa_dbg(wpa_s, MSG_DEBUG,
1920 					"Could not parse WPA element");
1921 			} else {
1922 				wpa_dbg(wpa_s, MSG_DEBUG,
1923 					"WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1924 					ie.pairwise_cipher, ie.group_cipher,
1925 					ie.key_mgmt);
1926 			}
1927 		}
1928 		return -1;
1929 	} else {
1930 		if (ssid->proto & WPA_PROTO_RSN)
1931 			proto = WPA_PROTO_RSN;
1932 		else
1933 			proto = WPA_PROTO_WPA;
1934 		if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1935 			os_memset(&ie, 0, sizeof(ie));
1936 			ie.group_cipher = ssid->group_cipher;
1937 			ie.pairwise_cipher = ssid->pairwise_cipher;
1938 			ie.key_mgmt = ssid->key_mgmt;
1939 			ie.mgmt_group_cipher = 0;
1940 			if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1941 				if (ssid->group_mgmt_cipher &
1942 				    WPA_CIPHER_BIP_GMAC_256)
1943 					ie.mgmt_group_cipher =
1944 						WPA_CIPHER_BIP_GMAC_256;
1945 				else if (ssid->group_mgmt_cipher &
1946 					 WPA_CIPHER_BIP_CMAC_256)
1947 					ie.mgmt_group_cipher =
1948 						WPA_CIPHER_BIP_CMAC_256;
1949 				else if (ssid->group_mgmt_cipher &
1950 					 WPA_CIPHER_BIP_GMAC_128)
1951 					ie.mgmt_group_cipher =
1952 						WPA_CIPHER_BIP_GMAC_128;
1953 				else
1954 					ie.mgmt_group_cipher =
1955 						WPA_CIPHER_AES_128_CMAC;
1956 			}
1957 #ifdef CONFIG_OWE
1958 			if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1959 			    !ssid->owe_only &&
1960 			    !bss_wpa && !bss_rsn) {
1961 				wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1962 				wpa_s->wpa_proto = 0;
1963 				*wpa_ie_len = 0;
1964 				return 0;
1965 			}
1966 #endif /* CONFIG_OWE */
1967 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1968 				"based on configuration");
1969 		} else
1970 			proto = ie.proto;
1971 	}
1972 
1973 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1974 		"pairwise %d key_mgmt %d proto %d",
1975 		ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1976 	if (ssid->ieee80211w) {
1977 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1978 			ie.mgmt_group_cipher);
1979 	}
1980 
1981 	wpa_s->wpa_proto = proto;
1982 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1983 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
1984 			 !!(ssid->proto & WPA_PROTO_RSN));
1985 
1986 	if (bss || !wpa_s->ap_ies_from_associnfo) {
1987 		const u8 *rsnoe = NULL, *rsno2e = NULL, *rsnxoe = NULL;
1988 
1989 		if (bss) {
1990 			bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1991 			bss_rsnx = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
1992 			rsnoe = wpa_bss_get_vendor_ie(
1993 				bss, RSNE_OVERRIDE_IE_VENDOR_TYPE);
1994 			rsno2e = wpa_bss_get_vendor_ie(
1995 				bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
1996 			rsnxoe = wpa_bss_get_vendor_ie(
1997 				bss, RSNXE_OVERRIDE_IE_VENDOR_TYPE);
1998 		}
1999 
2000 		if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2001 					 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2002 		    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2003 					 bss_rsn ? 2 + bss_rsn[1] : 0) ||
2004 		    wpa_sm_set_ap_rsnxe(wpa_s->wpa, bss_rsnx,
2005 					bss_rsnx ? 2 + bss_rsnx[1] : 0) ||
2006 		    wpa_sm_set_ap_rsne_override(wpa_s->wpa, rsnoe,
2007 						rsnoe ? 2 + rsnoe[1] : 0) ||
2008 		    wpa_sm_set_ap_rsne_override_2(wpa_s->wpa, rsno2e,
2009 						  rsno2e ? 2 + rsno2e[1] : 0) ||
2010 		    wpa_sm_set_ap_rsnxe_override(wpa_s->wpa, rsnxoe,
2011 						 rsnxoe ? 2 + rsnxoe[1] : 0))
2012 			return -1;
2013 	}
2014 
2015 #ifdef CONFIG_NO_WPA
2016 	wpa_s->group_cipher = WPA_CIPHER_NONE;
2017 	wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
2018 #else /* CONFIG_NO_WPA */
2019 	sel = ie.group_cipher & ssid->group_cipher;
2020 	wpa_dbg(wpa_s, MSG_DEBUG,
2021 		"WPA: AP group 0x%x network profile group 0x%x; available group 0x%x",
2022 		ie.group_cipher, ssid->group_cipher, sel);
2023 	wpa_s->group_cipher = wpa_pick_group_cipher(sel);
2024 	if (wpa_s->group_cipher < 0) {
2025 		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
2026 			"cipher");
2027 		return -1;
2028 	}
2029 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
2030 		wpa_cipher_txt(wpa_s->group_cipher));
2031 
2032 	sel = ie.pairwise_cipher & ssid->pairwise_cipher;
2033 	wpa_dbg(wpa_s, MSG_DEBUG,
2034 		"WPA: AP pairwise 0x%x network profile pairwise 0x%x; available pairwise 0x%x",
2035 		ie.pairwise_cipher, ssid->pairwise_cipher, sel);
2036 	wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
2037 	if (wpa_s->pairwise_cipher < 0) {
2038 		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
2039 			"cipher");
2040 		return -1;
2041 	}
2042 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
2043 		wpa_cipher_txt(wpa_s->pairwise_cipher));
2044 #endif /* CONFIG_NO_WPA */
2045 
2046 	sel = ie.key_mgmt & ssid->key_mgmt;
2047 #ifdef CONFIG_SAE
2048 	if ((!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) &&
2049 	     !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA)) ||
2050 	    wpas_is_sae_avoided(wpa_s, ssid, &ie))
2051 		sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY |
2052 			 WPA_KEY_MGMT_FT_SAE | WPA_KEY_MGMT_FT_SAE_EXT_KEY);
2053 #endif /* CONFIG_SAE */
2054 #ifdef CONFIG_IEEE80211R
2055 	if (!(wpa_s->drv_flags & (WPA_DRIVER_FLAGS_SME |
2056 				  WPA_DRIVER_FLAGS_UPDATE_FT_IES)))
2057 		sel &= ~WPA_KEY_MGMT_FT;
2058 #endif /* CONFIG_IEEE80211R */
2059 	wpa_dbg(wpa_s, MSG_DEBUG,
2060 		"WPA: AP key_mgmt 0x%x network profile key_mgmt 0x%x; available key_mgmt 0x%x",
2061 		ie.key_mgmt, ssid->key_mgmt, sel);
2062 	if (0) {
2063 #ifdef CONFIG_IEEE80211R
2064 #ifdef CONFIG_SHA384
2065 	} else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
2066 		   os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
2067 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
2068 		wpa_dbg(wpa_s, MSG_DEBUG,
2069 			"WPA: using KEY_MGMT FT/802.1X-SHA384");
2070 		if (!ssid->ft_eap_pmksa_caching &&
2071 		    pmksa_cache_get_current(wpa_s->wpa)) {
2072 			/* PMKSA caching with FT may have interoperability
2073 			 * issues, so disable that case by default for now. */
2074 			wpa_dbg(wpa_s, MSG_DEBUG,
2075 				"WPA: Disable PMKSA caching for FT/802.1X connection");
2076 			pmksa_cache_clear_current(wpa_s->wpa);
2077 		}
2078 #endif /* CONFIG_SHA384 */
2079 #endif /* CONFIG_IEEE80211R */
2080 #ifdef CONFIG_SUITEB192
2081 	} else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2082 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
2083 		wpa_dbg(wpa_s, MSG_DEBUG,
2084 			"WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
2085 #endif /* CONFIG_SUITEB192 */
2086 #ifdef CONFIG_SUITEB
2087 	} else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2088 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
2089 		wpa_dbg(wpa_s, MSG_DEBUG,
2090 			"WPA: using KEY_MGMT 802.1X with Suite B");
2091 #endif /* CONFIG_SUITEB */
2092 #ifdef CONFIG_SHA384
2093 	} else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA384) {
2094 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA384;
2095 		wpa_dbg(wpa_s, MSG_DEBUG,
2096 			"WPA: using KEY_MGMT 802.1X with SHA384");
2097 #endif /* CONFIG_SHA384 */
2098 #ifdef CONFIG_FILS
2099 #ifdef CONFIG_IEEE80211R
2100 	} else if (sel & WPA_KEY_MGMT_FT_FILS_SHA384) {
2101 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
2102 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA384");
2103 #endif /* CONFIG_IEEE80211R */
2104 	} else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
2105 		wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
2106 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA384");
2107 #ifdef CONFIG_IEEE80211R
2108 	} else if (sel & WPA_KEY_MGMT_FT_FILS_SHA256) {
2109 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
2110 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA256");
2111 #endif /* CONFIG_IEEE80211R */
2112 	} else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
2113 		wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
2114 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA256");
2115 #endif /* CONFIG_FILS */
2116 #ifdef CONFIG_IEEE80211R
2117 	} else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X) &&
2118 		   os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
2119 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
2120 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
2121 		if (!ssid->ft_eap_pmksa_caching &&
2122 		    pmksa_cache_get_current(wpa_s->wpa)) {
2123 			/* PMKSA caching with FT may have interoperability
2124 			 * issues, so disable that case by default for now. */
2125 			wpa_dbg(wpa_s, MSG_DEBUG,
2126 				"WPA: Disable PMKSA caching for FT/802.1X connection");
2127 			pmksa_cache_clear_current(wpa_s->wpa);
2128 		}
2129 #endif /* CONFIG_IEEE80211R */
2130 #ifdef CONFIG_DPP
2131 	} else if (sel & WPA_KEY_MGMT_DPP) {
2132 		wpa_s->key_mgmt = WPA_KEY_MGMT_DPP;
2133 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT DPP");
2134 #endif /* CONFIG_DPP */
2135 #ifdef CONFIG_SAE
2136 	} else if (sel & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
2137 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE_EXT_KEY;
2138 		wpa_dbg(wpa_s, MSG_DEBUG,
2139 			"RSN: using KEY_MGMT FT/SAE (ext key)");
2140 	} else if (sel & WPA_KEY_MGMT_SAE_EXT_KEY) {
2141 		wpa_s->key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
2142 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE (ext key)");
2143 	} else if (sel & WPA_KEY_MGMT_FT_SAE) {
2144 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
2145 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
2146 	} else if (sel & WPA_KEY_MGMT_SAE) {
2147 		wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
2148 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
2149 #endif /* CONFIG_SAE */
2150 #ifdef CONFIG_IEEE80211R
2151 	} else if (sel & WPA_KEY_MGMT_FT_PSK) {
2152 		wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
2153 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
2154 #endif /* CONFIG_IEEE80211R */
2155 	} else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2156 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
2157 		wpa_dbg(wpa_s, MSG_DEBUG,
2158 			"WPA: using KEY_MGMT 802.1X with SHA256");
2159 	} else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
2160 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
2161 		wpa_dbg(wpa_s, MSG_DEBUG,
2162 			"WPA: using KEY_MGMT PSK with SHA256");
2163 	} else if (sel & WPA_KEY_MGMT_IEEE8021X) {
2164 		wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
2165 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
2166 	} else if (sel & WPA_KEY_MGMT_PSK) {
2167 		wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
2168 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
2169 	} else if (sel & WPA_KEY_MGMT_WPA_NONE) {
2170 		wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
2171 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
2172 #ifdef CONFIG_OWE
2173 	} else if (sel & WPA_KEY_MGMT_OWE) {
2174 		wpa_s->key_mgmt = WPA_KEY_MGMT_OWE;
2175 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT OWE");
2176 #endif /* CONFIG_OWE */
2177 #ifdef CONFIG_ENC_ASSOC
2178 	} else if (sel & WPA_KEY_MGMT_EPPKE) {
2179 		wpa_s->key_mgmt = WPA_KEY_MGMT_EPPKE;
2180 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT EPPKE");
2181 #endif /* CONFIG_ENC_ASSOC */
2182 	} else {
2183 		wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
2184 			"authenticated key management type");
2185 		return -1;
2186 	}
2187 
2188 	/* There might be a PMKSA cache entry for the target AP, but the current
2189 	 * connection is trying to use PSK (which does not use PMKSA caching) or
2190 	 * an AKM that does not match the one that was used to generate the
2191 	 * selected PMKSA entry. The previously selected PMKSA cache entry needs
2192 	 * to be cleared in such cases to avoid indicating an incorrect PMKID
2193 	 * and exchange that would likely end up failing with the AP attempting
2194 	 * to use a different PMK. This is not really supposed to happen in
2195 	 * normal use cases, but it is possible that some corner cases of the AP
2196 	 * changing its configuration might trigger a failure due to mismatching
2197 	 * PMK. */
2198 	pmksa = pmksa_cache_get_current(wpa_s->wpa);
2199 	if (pmksa &&
2200 	    (wpa_key_mgmt_wpa_psk_no_sae(wpa_s->key_mgmt) ||
2201 	     (pmksa->akmp && pmksa->akmp != wpa_s->key_mgmt))) {
2202 		wpa_printf(MSG_DEBUG,
2203 			   "RSN: Disable PMKSA caching due to incompatible AKMP (PMKSA: 0x%x, selected: 0x%x)",
2204 			   pmksa->akmp, wpa_s->key_mgmt);
2205 		pmksa_cache_clear_current(wpa_s->wpa);
2206 	}
2207 
2208 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
2209 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
2210 			 wpa_s->pairwise_cipher);
2211 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
2212 
2213 	if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
2214 	    (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED ||
2215 	     (bss && is_6ghz_freq(bss->freq)))) {
2216 		wpa_msg(wpa_s, MSG_INFO,
2217 			"RSN: Management frame protection required but the selected AP does not enable it");
2218 		return -1;
2219 	}
2220 
2221 	wpas_set_mgmt_group_cipher(wpa_s, ssid, &ie);
2222 #ifdef CONFIG_OCV
2223 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
2224 	    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
2225 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV, ssid->ocv);
2226 #endif /* CONFIG_OCV */
2227 #ifdef CONFIG_SAE
2228 	sae_pwe = wpas_get_ssid_sae_pwe(wpa_s, ssid);
2229 	if ((ssid->sae_password_id ||
2230 	     wpa_key_mgmt_sae_ext_key(wpa_s->key_mgmt)) &&
2231 	    sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
2232 		sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
2233 	if (bss && is_6ghz_freq(bss->freq) &&
2234 	    sae_pwe == SAE_PWE_HUNT_AND_PECK) {
2235 		wpa_dbg(wpa_s, MSG_DEBUG,
2236 			"RSN: Enable SAE hash-to-element mode for 6 GHz BSS");
2237 		sae_pwe = SAE_PWE_BOTH;
2238 	}
2239 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PWE, sae_pwe);
2240 #ifdef CONFIG_SAE_PK
2241 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PK,
2242 			 wpa_key_mgmt_sae(ssid->key_mgmt) &&
2243 			 ssid->sae_pk != SAE_PK_MODE_DISABLED &&
2244 			 ((ssid->sae_password &&
2245 			   sae_pk_valid_password(ssid->sae_password)) ||
2246 			  (!ssid->sae_password && ssid->passphrase &&
2247 			   sae_pk_valid_password(ssid->passphrase))));
2248 #endif /* CONFIG_SAE_PK */
2249 #endif /* CONFIG_SAE */
2250 	if (bss && is_6ghz_freq(bss->freq) &&
2251 	    wpas_get_ssid_pmf(wpa_s, ssid) != MGMT_FRAME_PROTECTION_REQUIRED) {
2252 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Force MFPR=1 on 6 GHz");
2253 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
2254 				 MGMT_FRAME_PROTECTION_REQUIRED);
2255 	}
2256 #ifdef CONFIG_TESTING_OPTIONS
2257 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_FT_RSNXE_USED,
2258 			 wpa_s->ft_rsnxe_used);
2259 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL,
2260 			 wpa_s->oci_freq_override_eapol);
2261 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
2262 			 wpa_s->oci_freq_override_eapol_g2);
2263 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
2264 			 wpa_s->oci_freq_override_ft_assoc);
2265 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FILS_ASSOC,
2266 			 wpa_s->oci_freq_override_fils_assoc);
2267 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DISABLE_EAPOL_G2_TX,
2268 			 wpa_s->disable_eapol_g2_tx);
2269 	wpa_sm_set_param(wpa_s->wpa,
2270 			 WPA_PARAM_EAPOL_2_KEY_INFO_SET_MASK,
2271 			 wpa_s->eapol_2_key_info_set_mask);
2272 #endif /* CONFIG_TESTING_OPTIONS */
2273 
2274 	/* Extended Key ID is only supported in infrastructure BSS so far */
2275 	if (ssid->mode == WPAS_MODE_INFRA && wpa_s->conf->extended_key_id &&
2276 	    (ssid->proto & WPA_PROTO_RSN) &&
2277 	    ssid->pairwise_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_CCMP_256 |
2278 				     WPA_CIPHER_GCMP | WPA_CIPHER_GCMP_256) &&
2279 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_EXTENDED_KEY_ID)) {
2280 		int use_ext_key_id = 0;
2281 
2282 		wpa_msg(wpa_s, MSG_DEBUG,
2283 			"WPA: Enable Extended Key ID support");
2284 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_EXT_KEY_ID,
2285 				 wpa_s->conf->extended_key_id);
2286 		if (bss_rsn &&
2287 		    wpa_s->conf->extended_key_id &&
2288 		    wpa_s->pairwise_cipher != WPA_CIPHER_TKIP &&
2289 		    (ie.capabilities & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST))
2290 			use_ext_key_id = 1;
2291 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_USE_EXT_KEY_ID,
2292 				 use_ext_key_id);
2293 	} else {
2294 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_EXT_KEY_ID, 0);
2295 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_USE_EXT_KEY_ID, 0);
2296 	}
2297 
2298 	/* Mark WMM enabled for any HT/VHT/HE/EHT association to get more
2299 	 * appropriate advertisement of the supported number of PTKSA receive
2300 	 * counters. In theory, this could be based on a driver capability, but
2301 	 * in practice all cases using WMM support at least eight replay
2302 	 * counters, so use a hardcoded value for now since there is no explicit
2303 	 * driver capability indication for this.
2304 	 *
2305 	 * In addition, claim WMM to be enabled if the AP supports it since it
2306 	 * is far more likely for any current device to support WMM. */
2307 	wmm = wpa_s->connection_set &&
2308 		(wpa_s->connection_ht || wpa_s->connection_vht ||
2309 		 wpa_s->connection_he || wpa_s->connection_eht);
2310 	if (!wmm && bss)
2311 		wmm = !!wpa_bss_get_vendor_ie(bss, WMM_IE_VENDOR_TYPE);
2312 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_WMM_ENABLED, wmm);
2313 
2314 	if (ssid->ssid_protection && proto == WPA_PROTO_RSN) {
2315 		bool ssid_prot;
2316 
2317 		/* Enable SSID protection based on the AP advertising support
2318 		 * for it to avoid potential interoperability issues with
2319 		 * incorrect AP behavior if we were to send an "unexpected"
2320 		 * RSNXE with multiple octets of payload. */
2321 		ssid_prot = ieee802_11_rsnx_capab(
2322 			bss_rsnx, WLAN_RSNX_CAPAB_SSID_PROTECTION);
2323 		if (!skip_default_rsne)
2324 			wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SSID_PROTECTION,
2325 					 proto == WPA_PROTO_RSN && ssid_prot);
2326 	} else {
2327 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SSID_PROTECTION, false);
2328 	}
2329 
2330 #ifdef CONFIG_ENC_ASSOC
2331 	if (proto == WPA_PROTO_RSN &&
2332 	    (wpa_s->drv_flags2 &
2333 	     WPA_DRIVER_FLAGS2_ASSOCIATION_FRAME_ENCRYPTION)) {
2334 		bool assoc_enc;
2335 
2336 		/* Enable association frame encryption based on the AP
2337 		 * advertising support for it to avoid potential
2338 		 * interoperability issues with incorrect AP behavior if we
2339 		 * were to send an "unexpected" RSNXE with multiple octets of
2340 		 * payload. */
2341 		assoc_enc = ieee802_11_rsnx_capab(
2342 			bss_rsnx, WLAN_RSNX_CAPAB_ASSOC_FRAME_ENCRYPTION);
2343 		if (!skip_default_rsne)
2344 			wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ASSOC_ENC,
2345 					 assoc_enc);
2346 	} else {
2347 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ASSOC_ENC, false);
2348 	}
2349 #endif /* CONFIG_ENC_ASSOC */
2350 
2351 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SPP_AMSDU,
2352 			 (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SPP_AMSDU) &&
2353 			 ieee802_11_rsnx_capab(bss_rsnx,
2354 					       WLAN_RSNX_CAPAB_SPP_A_MSDU) &&
2355 			 wpa_s->pairwise_cipher & (WPA_CIPHER_CCMP_256 |
2356 						   WPA_CIPHER_GCMP_256 |
2357 						   WPA_CIPHER_CCMP |
2358 						   WPA_CIPHER_GCMP) &&
2359 			 (wpa_s->wpa_proto & WPA_PROTO_RSN));
2360 
2361 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PW_ID_CHANGE,
2362 			 ssid->sae_password_id && ssid->sae_password_id_change);
2363 #ifdef CONFIG_PMKSA_PRIVACY
2364 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PMKSA_CACHING_PRIVACY,
2365 			 ssid->pmksa_privacy);
2366 #endif /* CONFIG_PMKSA_PRIVACY */
2367 
2368 	if (!skip_default_rsne) {
2369 		if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie,
2370 						    wpa_ie_len)) {
2371 			wpa_msg(wpa_s, MSG_WARNING,
2372 				"RSN: Failed to generate RSNE/WPA IE");
2373 			return -1;
2374 		}
2375 
2376 #ifndef CONFIG_NO_WPA
2377 		wpa_s->rsnxe_len = sizeof(wpa_s->rsnxe);
2378 		if (wpa_sm_set_assoc_rsnxe_default(wpa_s->wpa, wpa_s->rsnxe,
2379 						   &wpa_s->rsnxe_len)) {
2380 			wpa_msg(wpa_s, MSG_WARNING,
2381 				"RSN: Failed to generate RSNXE");
2382 			return -1;
2383 		}
2384 #endif /* CONFIG_NO_WPA */
2385 	}
2386 
2387 	if (0) {
2388 #ifdef CONFIG_DPP
2389 	} else if (wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
2390 		/* Use PMK from DPP network introduction (PMKSA entry) */
2391 		wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
2392 #ifdef CONFIG_DPP2
2393 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DPP_PFS, ssid->dpp_pfs);
2394 #endif /* CONFIG_DPP2 */
2395 #endif /* CONFIG_DPP */
2396 	} else if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
2397 		int psk_set = 0;
2398 
2399 		if (wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt)) {
2400 			u8 psk[PMK_LEN];
2401 
2402 			if (wpa_supplicant_get_psk(wpa_s, bss, ssid,
2403 						   psk) == 0) {
2404 				wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
2405 					       NULL);
2406 				psk_set = 1;
2407 			}
2408 			forced_memzero(psk, sizeof(psk));
2409 		}
2410 
2411 		if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
2412 		    (ssid->sae_password || ssid->passphrase || ssid->ext_psk))
2413 			psk_set = 1;
2414 
2415 		if (!psk_set && !ssid->pmk_valid) {
2416 			wpa_msg(wpa_s, MSG_INFO,
2417 				"No PSK/PMK available for association");
2418 			wpas_auth_failed(wpa_s, "NO_PSK_AVAILABLE", NULL);
2419 			return -1;
2420 		}
2421 #ifdef CONFIG_OWE
2422 	} else if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
2423 		/* OWE Diffie-Hellman exchange in (Re)Association
2424 		 * Request/Response frames set the PMK, so do not override it
2425 		 * here. */
2426 #endif /* CONFIG_OWE */
2427 	} else
2428 		wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
2429 
2430 	if (ssid->mode != WPAS_MODE_IBSS &&
2431 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED) &&
2432 	    (ssid->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_NEVER ||
2433 	     (ssid->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_LOCAL_OK &&
2434 	      !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS)))) {
2435 		wpa_msg(wpa_s, MSG_INFO,
2436 			"Disable PTK0 rekey support - replaced with reconnect");
2437 		wpa_s->deny_ptk0_rekey = 1;
2438 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DENY_PTK0_REKEY, 1);
2439 	} else {
2440 		wpa_s->deny_ptk0_rekey = 0;
2441 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DENY_PTK0_REKEY, 0);
2442 	}
2443 
2444 	if (wpa_key_mgmt_cross_akm(wpa_s->key_mgmt) &&
2445 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2446 		wpas_update_allowed_key_mgmt(wpa_s, ssid);
2447 
2448 	return 0;
2449 }
2450 
2451 
wpas_ext_capab_byte(struct wpa_supplicant * wpa_s,u8 * pos,int idx,struct wpa_bss * bss)2452 static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx,
2453 				struct wpa_bss *bss)
2454 {
2455 #ifndef CONFIG_NO_ROBUST_AV
2456 	bool scs = true, mscs = true;
2457 #endif /* CONFIG_NO_ROBUST_AV */
2458 
2459 	*pos = 0x00;
2460 
2461 	switch (idx) {
2462 	case 0: /* Bits 0-7 */
2463 		break;
2464 	case 1: /* Bits 8-15 */
2465 		if (wpa_s->conf->coloc_intf_reporting) {
2466 			/* Bit 13 - Collocated Interference Reporting */
2467 			*pos |= 0x20;
2468 		}
2469 		break;
2470 	case 2: /* Bits 16-23 */
2471 #ifdef CONFIG_WNM
2472 		*pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
2473 		if ((wpas_driver_bss_selection(wpa_s) ||
2474 		     !wpa_s->disable_mbo_oce) &&
2475 		    !wpa_s->conf->disable_btm)
2476 			*pos |= 0x08; /* Bit 19 - BSS Transition */
2477 #endif /* CONFIG_WNM */
2478 		break;
2479 	case 3: /* Bits 24-31 */
2480 #ifdef CONFIG_WNM
2481 		*pos |= 0x02; /* Bit 25 - SSID List */
2482 #endif /* CONFIG_WNM */
2483 #ifdef CONFIG_INTERWORKING
2484 		if (wpa_s->conf->interworking)
2485 			*pos |= 0x80; /* Bit 31 - Interworking */
2486 #endif /* CONFIG_INTERWORKING */
2487 		break;
2488 	case 4: /* Bits 32-39 */
2489 #ifdef CONFIG_INTERWORKING
2490 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_QOS_MAPPING)
2491 			*pos |= 0x01; /* Bit 32 - QoS Map */
2492 #endif /* CONFIG_INTERWORKING */
2493 		break;
2494 	case 5: /* Bits 40-47 */
2495 #ifdef CONFIG_HS20
2496 		if (wpa_s->conf->hs20)
2497 			*pos |= 0x40; /* Bit 46 - WNM-Notification */
2498 #endif /* CONFIG_HS20 */
2499 #ifdef CONFIG_MBO
2500 		*pos |= 0x40; /* Bit 46 - WNM-Notification */
2501 #endif /* CONFIG_MBO */
2502 		break;
2503 	case 6: /* Bits 48-55 */
2504 #ifndef CONFIG_NO_ROBUST_AV
2505 #ifdef CONFIG_TESTING_OPTIONS
2506 		if (wpa_s->disable_scs_support)
2507 			scs = false;
2508 #endif /* CONFIG_TESTING_OPTIONS */
2509 		if (bss && !wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_SCS)) {
2510 			/* Drop own SCS capability indication since the AP does
2511 			 * not support it. This is needed to avoid
2512 			 * interoperability issues with APs that get confused
2513 			 * with Extended Capabilities element. */
2514 			scs = false;
2515 		}
2516 		if (scs)
2517 			*pos |= 0x40; /* Bit 54 - SCS */
2518 #endif /* CONFIG_NO_ROBUST_AV */
2519 		break;
2520 	case 7: /* Bits 56-63 */
2521 		break;
2522 	case 8: /* Bits 64-71 */
2523 		if (wpa_s->conf->ftm_responder)
2524 			*pos |= 0x40; /* Bit 70 - FTM responder */
2525 		if (wpa_s->conf->ftm_initiator)
2526 			*pos |= 0x80; /* Bit 71 - FTM initiator */
2527 		break;
2528 	case 9: /* Bits 72-79 */
2529 #ifdef CONFIG_FILS
2530 		if (!wpa_s->disable_fils)
2531 			*pos |= 0x01;
2532 #endif /* CONFIG_FILS */
2533 		if (wpa_s->conf->twt_requester)
2534 			*pos |= 0x20; /* Bit 77 - TWT Requester Support */
2535 		break;
2536 	case 10: /* Bits 80-87 */
2537 #ifndef CONFIG_NO_ROBUST_AV
2538 #ifdef CONFIG_TESTING_OPTIONS
2539 		if (wpa_s->disable_mscs_support)
2540 			mscs = false;
2541 #endif /* CONFIG_TESTING_OPTIONS */
2542 		if (bss && !wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_MSCS)) {
2543 			/* Drop own MSCS capability indication since the AP does
2544 			 * not support it. This is needed to avoid
2545 			 * interoperability issues with APs that get confused
2546 			 * with Extended Capabilities element. */
2547 			mscs = false;
2548 		}
2549 		if (mscs)
2550 			*pos |= 0x20; /* Bit 85 - Mirrored SCS */
2551 #endif /* CONFIG_NO_ROBUST_AV */
2552 		break;
2553 	}
2554 }
2555 
2556 
wpas_build_ext_capab(struct wpa_supplicant * wpa_s,u8 * buf,size_t buflen,struct wpa_bss * bss)2557 int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf,
2558 			  size_t buflen, struct wpa_bss *bss)
2559 {
2560 	u8 *pos = buf;
2561 	u8 len = 11, i;
2562 
2563 	if (len < wpa_s->extended_capa_len)
2564 		len = wpa_s->extended_capa_len;
2565 	if (buflen < (size_t) len + 2) {
2566 		wpa_printf(MSG_INFO,
2567 			   "Not enough room for building extended capabilities element");
2568 		return -1;
2569 	}
2570 
2571 	*pos++ = WLAN_EID_EXT_CAPAB;
2572 	*pos++ = len;
2573 	for (i = 0; i < len; i++, pos++) {
2574 		wpas_ext_capab_byte(wpa_s, pos, i, bss);
2575 
2576 		if (i < wpa_s->extended_capa_len) {
2577 			*pos &= ~wpa_s->extended_capa_mask[i];
2578 			*pos |= wpa_s->extended_capa[i];
2579 		}
2580 	}
2581 
2582 	while (len > 0 && buf[1 + len] == 0) {
2583 		len--;
2584 		buf[1] = len;
2585 	}
2586 	if (len == 0)
2587 		return 0;
2588 
2589 	return 2 + len;
2590 }
2591 
2592 
wpas_valid_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * test_bss)2593 static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
2594 			  struct wpa_bss *test_bss)
2595 {
2596 	struct wpa_bss *bss;
2597 
2598 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2599 		if (bss == test_bss)
2600 			return 1;
2601 	}
2602 
2603 	return 0;
2604 }
2605 
2606 
wpas_valid_ssid(struct wpa_supplicant * wpa_s,struct wpa_ssid * test_ssid)2607 static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
2608 			   struct wpa_ssid *test_ssid)
2609 {
2610 	struct wpa_ssid *ssid;
2611 
2612 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2613 		if (ssid == test_ssid)
2614 			return 1;
2615 	}
2616 
2617 	return 0;
2618 }
2619 
2620 
wpas_valid_bss_ssid(struct wpa_supplicant * wpa_s,struct wpa_bss * test_bss,struct wpa_ssid * test_ssid)2621 int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
2622 			struct wpa_ssid *test_ssid)
2623 {
2624 	if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
2625 		return 0;
2626 
2627 	return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
2628 }
2629 
2630 
wpas_connect_work_free(struct wpa_connect_work * cwork)2631 void wpas_connect_work_free(struct wpa_connect_work *cwork)
2632 {
2633 	if (cwork == NULL)
2634 		return;
2635 	os_free(cwork);
2636 }
2637 
2638 
wpas_connect_work_done(struct wpa_supplicant * wpa_s)2639 void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
2640 {
2641 	struct wpa_connect_work *cwork;
2642 	struct wpa_radio_work *work = wpa_s->connect_work;
2643 
2644 	if (!work)
2645 		return;
2646 
2647 	wpa_s->connect_work = NULL;
2648 	cwork = work->ctx;
2649 	work->ctx = NULL;
2650 	wpas_connect_work_free(cwork);
2651 	radio_work_done(work);
2652 }
2653 
2654 
wpas_update_random_addr(struct wpa_supplicant * wpa_s,enum wpas_mac_addr_style style,struct wpa_ssid * ssid)2655 int wpas_update_random_addr(struct wpa_supplicant *wpa_s,
2656 			    enum wpas_mac_addr_style style,
2657 			    struct wpa_ssid *ssid)
2658 {
2659 	struct os_reltime now;
2660 	u8 addr[ETH_ALEN];
2661 
2662 	os_get_reltime(&now);
2663 	/* Random addresses are valid within a given ESS so check
2664 	 * expiration/value only when continuing to use the same ESS. */
2665 	if (wpa_s->last_mac_addr_style == style && wpa_s->reassoc_same_ess) {
2666 		if (style == WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS) {
2667 			/* Pregenerated addresses do not expire but their value
2668 			 * might have changed, so let's check that. */
2669 			if (ssid &&
2670 			    ether_addr_equal(wpa_s->own_addr, ssid->mac_value))
2671 				return 0;
2672 		} else if ((wpa_s->last_mac_addr_change.sec != 0 ||
2673 			    wpa_s->last_mac_addr_change.usec != 0) &&
2674 			   !os_reltime_expired(
2675 				   &now,
2676 				   &wpa_s->last_mac_addr_change,
2677 				   wpa_s->conf->rand_addr_lifetime)) {
2678 			wpa_msg(wpa_s, MSG_DEBUG,
2679 				"Previously selected random MAC address has not yet expired");
2680 			return 0;
2681 		}
2682 	}
2683 
2684 	switch (style) {
2685 	case WPAS_MAC_ADDR_STYLE_RANDOM:
2686 		if (random_mac_addr(addr) < 0)
2687 			return -1;
2688 		break;
2689 	case WPAS_MAC_ADDR_STYLE_RANDOM_SAME_OUI:
2690 		os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
2691 		if (random_mac_addr_keep_oui(addr) < 0)
2692 			return -1;
2693 		break;
2694 	case WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS:
2695 		if (!ssid) {
2696 			wpa_msg(wpa_s, MSG_INFO,
2697 				"Invalid 'ssid' for address policy 3");
2698 			return -1;
2699 		}
2700 		os_memcpy(addr, ssid->mac_value, ETH_ALEN);
2701 		break;
2702 	default:
2703 		return -1;
2704 	}
2705 
2706 	if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
2707 		wpa_msg(wpa_s, MSG_INFO,
2708 			"Failed to set random MAC address");
2709 		return -1;
2710 	}
2711 
2712 	os_get_reltime(&wpa_s->last_mac_addr_change);
2713 	wpa_s->mac_addr_changed = 1;
2714 	wpa_s->last_mac_addr_style = style;
2715 
2716 	if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2717 		wpa_msg(wpa_s, MSG_INFO,
2718 			"Could not update MAC address information");
2719 		return -1;
2720 	}
2721 
2722 	wpas_p2p_update_dev_addr(wpa_s);
2723 	wpas_pr_update_dev_addr(wpa_s);
2724 
2725 	wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
2726 		MAC2STR(addr));
2727 
2728 	return 1;
2729 }
2730 
2731 
wpas_update_random_addr_disassoc(struct wpa_supplicant * wpa_s)2732 int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
2733 {
2734 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
2735 	    !wpa_s->conf->preassoc_mac_addr)
2736 		return 0;
2737 
2738 	return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr,
2739 				       NULL);
2740 }
2741 
2742 
wpa_s_setup_sae_pt(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,bool force)2743 void wpa_s_setup_sae_pt(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2744 			bool force)
2745 {
2746 #ifdef CONFIG_SAE
2747 	struct wpa_config *conf = wpa_s->conf;
2748 	int *groups = conf->sae_groups;
2749 	int default_groups[] = { 19, 20, 21, 0 };
2750 	const char *password;
2751 	enum sae_pwe sae_pwe;
2752 	const u8 *password_id = (const u8 *) ssid->sae_password_id;
2753 	size_t password_id_len = ssid->sae_password_id ?
2754 		os_strlen(ssid->sae_password_id) : 0;
2755 	struct wpabuf_array *ids;
2756 
2757 	if (!groups || groups[0] <= 0)
2758 		groups = default_groups;
2759 
2760 	password = ssid->sae_password;
2761 	if (!password)
2762 		password = ssid->passphrase;
2763 
2764 	sae_pwe = wpas_get_ssid_sae_pwe(wpa_s, ssid);
2765 
2766 	if (!password ||
2767 	    !wpa_key_mgmt_sae(ssid->key_mgmt) ||
2768 	    (sae_pwe == SAE_PWE_HUNT_AND_PECK && !ssid->sae_password_id &&
2769 	     !wpa_key_mgmt_sae_ext_key(ssid->key_mgmt) &&
2770 	     !force &&
2771 	     !sae_pk_valid_password(password)) ||
2772 	    sae_pwe == SAE_PWE_FORCE_HUNT_AND_PECK) {
2773 		/* PT derivation not needed */
2774 		sae_deinit_pt(ssid->pt);
2775 		ssid->pt = NULL;
2776 		return;
2777 	}
2778 
2779 	ids = ssid->alt_sae_password_ids;
2780 	if (ids && ids->num) {
2781 		unsigned int idx = os_random() % ids->num;
2782 		struct wpabuf *id = ids->buf[idx];
2783 
2784 		password_id = wpabuf_head(id);
2785 		password_id_len = wpabuf_len(id);
2786 		wpa_hexdump(MSG_DEBUG,
2787 			    "SAE: Prepare PT for alternative password ID",
2788 			    password_id, password_id_len);
2789 		ssid->alt_sae_passwords_ids_idx = idx;
2790 		ssid->alt_sae_passwords_ids_used = true;
2791 	}
2792 
2793 	if (ssid->pt) {
2794 		if (!password_id && !ssid->pt->password_id)
2795 			return; /* PT already derived for no PW ID */
2796 		if (password_id && ssid->pt->password_id &&
2797 		    password_id_len == wpabuf_len(ssid->pt->password_id) &&
2798 		    os_memcmp(password_id, wpabuf_head(ssid->pt->password_id),
2799 			      password_id_len) == 0)
2800 			return; /* PT already derived for same PW ID */
2801 
2802 		/* PT was derived for another password identifier */
2803 		sae_deinit_pt(ssid->pt);
2804 		ssid->pt = NULL;
2805 	}
2806 	ssid->pt = sae_derive_pt(groups, ssid->ssid, ssid->ssid_len,
2807 				 (const u8 *) password, os_strlen(password),
2808 				 password_id, password_id_len);
2809 #endif /* CONFIG_SAE */
2810 }
2811 
2812 
wpa_s_clear_sae_rejected(struct wpa_supplicant * wpa_s)2813 void wpa_s_clear_sae_rejected(struct wpa_supplicant *wpa_s)
2814 {
2815 #if defined(CONFIG_SAE) && defined(CONFIG_SME)
2816 	os_free(wpa_s->sme.sae_rejected_groups);
2817 	wpa_s->sme.sae_rejected_groups = NULL;
2818 #ifdef CONFIG_TESTING_OPTIONS
2819 	if (wpa_s->extra_sae_rejected_groups) {
2820 		int i, *groups = wpa_s->extra_sae_rejected_groups;
2821 
2822 		for (i = 0; groups[i]; i++) {
2823 			wpa_printf(MSG_DEBUG,
2824 				   "TESTING: Indicate rejection of an extra SAE group %d",
2825 				   groups[i]);
2826 			int_array_add_unique(&wpa_s->sme.sae_rejected_groups,
2827 					     groups[i]);
2828 		}
2829 	}
2830 #endif /* CONFIG_TESTING_OPTIONS */
2831 #endif /* CONFIG_SAE && CONFIG_SME */
2832 }
2833 
2834 
wpas_restore_permanent_mac_addr(struct wpa_supplicant * wpa_s)2835 int wpas_restore_permanent_mac_addr(struct wpa_supplicant *wpa_s)
2836 {
2837 	if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
2838 		wpa_msg(wpa_s, MSG_INFO,
2839 			"Could not restore permanent MAC address");
2840 		return -1;
2841 	}
2842 	wpa_s->mac_addr_changed = 0;
2843 	if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2844 		wpa_msg(wpa_s, MSG_INFO,
2845 			"Could not update MAC address information");
2846 		return -1;
2847 	}
2848 
2849 	wpas_p2p_update_dev_addr(wpa_s);
2850 
2851 	wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
2852 	return 0;
2853 }
2854 
2855 
2856 static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
2857 
2858 /**
2859  * wpa_supplicant_associate - Request association
2860  * @wpa_s: Pointer to wpa_supplicant data
2861  * @bss: Scan results for the selected BSS, or %NULL if not available
2862  * @ssid: Configuration data for the selected network
2863  *
2864  * This function is used to request %wpa_supplicant to associate with a BSS.
2865  */
wpa_supplicant_associate(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid)2866 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
2867 			      struct wpa_bss *bss, struct wpa_ssid *ssid)
2868 {
2869 	bool clear_rejected = true;
2870 	struct wpa_connect_work *cwork;
2871 	enum wpas_mac_addr_style rand_style;
2872 
2873 	wpa_s->own_disconnect_req = 0;
2874 	wpa_s->own_reconnect_req = 0;
2875 
2876 	/*
2877 	 * If we are starting a new connection, any previously pending EAPOL
2878 	 * RX cannot be valid anymore.
2879 	 */
2880 	wpabuf_free(wpa_s->pending_eapol_rx);
2881 	wpa_s->pending_eapol_rx = NULL;
2882 
2883 	if (ssid->mac_addr == WPAS_MAC_ADDR_STYLE_NOT_SET)
2884 		rand_style = wpa_s->conf->mac_addr;
2885 	else
2886 		rand_style = ssid->mac_addr;
2887 
2888 	wpa_s->eapol_failed = 0;
2889 	wpa_s->multi_ap_ie = 0;
2890 #ifndef CONFIG_NO_WMM_AC
2891 	wmm_ac_clear_saved_tspecs(wpa_s);
2892 #endif /* CONFIG_NO_WMM_AC */
2893 #ifdef CONFIG_WNM
2894 	wpa_s->wnm_mode = 0;
2895 	wpa_s->wnm_target_bss = NULL;
2896 #endif /* CONFIG_WNM */
2897 	wpa_s->reassoc_same_bss = 0;
2898 	wpa_s->reassoc_same_ess = 0;
2899 #ifdef CONFIG_TESTING_OPTIONS
2900 	wpa_s->testing_resend_assoc = 0;
2901 #endif /* CONFIG_TESTING_OPTIONS */
2902 
2903 	if (wpa_s->last_ssid == ssid) {
2904 		wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
2905 		wpa_s->reassoc_same_ess = 1;
2906 		if (wpa_s->current_bss && wpa_s->current_bss == bss) {
2907 #ifndef CONFIG_NO_WMM_AC
2908 			wmm_ac_save_tspecs(wpa_s);
2909 #endif /* CONFIG_NO_WMM_AC */
2910 			wpa_s->reassoc_same_bss = 1;
2911 			clear_rejected = false;
2912 		} else if (wpa_s->current_bss && wpa_s->current_bss != bss) {
2913 			os_get_reltime(&wpa_s->roam_start);
2914 		}
2915 	}
2916 
2917 	if (clear_rejected)
2918 		wpa_s_clear_sae_rejected(wpa_s);
2919 
2920 #ifdef CONFIG_SAE
2921 	wpa_s_setup_sae_pt(wpa_s, ssid, false);
2922 #endif /* CONFIG_SAE */
2923 
2924 	if (rand_style > WPAS_MAC_ADDR_STYLE_PERMANENT) {
2925 		int status = wpas_update_random_addr(wpa_s, rand_style, ssid);
2926 
2927 		if (status < 0)
2928 			return;
2929 		if (rand_style != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS &&
2930 		    status > 0) /* MAC changed */
2931 			wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2932 	} else if (rand_style == WPAS_MAC_ADDR_STYLE_PERMANENT &&
2933 		   wpa_s->mac_addr_changed) {
2934 		if (wpas_restore_permanent_mac_addr(wpa_s) < 0)
2935 			return;
2936 	}
2937 	wpa_s->last_ssid = ssid;
2938 
2939 #ifdef CONFIG_IBSS_RSN
2940 	ibss_rsn_deinit(wpa_s->ibss_rsn);
2941 	wpa_s->ibss_rsn = NULL;
2942 #else /* CONFIG_IBSS_RSN */
2943 	if (ssid->mode == WPAS_MODE_IBSS &&
2944 	    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
2945 		wpa_msg(wpa_s, MSG_INFO,
2946 			"IBSS RSN not supported in the build");
2947 		return;
2948 	}
2949 #endif /* CONFIG_IBSS_RSN */
2950 
2951 	if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
2952 	    ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
2953 #ifdef CONFIG_AP
2954 		if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
2955 			wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
2956 				"mode");
2957 			return;
2958 		}
2959 		if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
2960 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2961 			if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
2962 				wpas_p2p_ap_setup_failed(wpa_s);
2963 			return;
2964 		}
2965 		wpa_s->current_bss = bss;
2966 #else /* CONFIG_AP */
2967 		wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
2968 			"the build");
2969 #endif /* CONFIG_AP */
2970 		return;
2971 	}
2972 
2973 	if (ssid->mode == WPAS_MODE_MESH) {
2974 #ifdef CONFIG_MESH
2975 		if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
2976 			wpa_msg(wpa_s, MSG_INFO,
2977 				"Driver does not support mesh mode");
2978 			return;
2979 		}
2980 		if (bss)
2981 			ssid->frequency = bss->freq;
2982 		if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
2983 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2984 			wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
2985 			return;
2986 		}
2987 		wpa_s->current_bss = bss;
2988 #else /* CONFIG_MESH */
2989 		wpa_msg(wpa_s, MSG_ERROR,
2990 			"mesh mode support not included in the build");
2991 #endif /* CONFIG_MESH */
2992 		return;
2993 	}
2994 
2995 	/*
2996 	 * Set WPA state machine configuration to match the selected network now
2997 	 * so that the information is available before wpas_start_assoc_cb()
2998 	 * gets called. This is needed at least for RSN pre-authentication where
2999 	 * candidate APs are added to a list based on scan result processing
3000 	 * before completion of the first association.
3001 	 */
3002 	wpa_supplicant_rsn_supp_set_config(wpa_s, ssid);
3003 
3004 #ifdef CONFIG_DPP
3005 	if (wpas_dpp_check_connect(wpa_s, ssid, bss) != 0)
3006 		return;
3007 #endif /* CONFIG_DPP */
3008 
3009 #ifdef CONFIG_TDLS
3010 	if (bss)
3011 		wpa_tdls_ap_ies(wpa_s->wpa, wpa_bss_ie_ptr(bss), bss->ie_len);
3012 #endif /* CONFIG_TDLS */
3013 
3014 #ifdef CONFIG_MBO
3015 	wpas_mbo_check_pmf(wpa_s, bss, ssid);
3016 #endif /* CONFIG_MBO */
3017 
3018 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3019 	    ssid->mode == WPAS_MODE_INFRA) {
3020 		sme_authenticate(wpa_s, bss, ssid);
3021 		return;
3022 	}
3023 
3024 	if (wpa_s->connect_work) {
3025 		wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
3026 		return;
3027 	}
3028 
3029 	if (radio_work_pending(wpa_s, "connect")) {
3030 		wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
3031 		return;
3032 	}
3033 
3034 #ifdef CONFIG_SME
3035 	if (ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) {
3036 		/* Clear possibly set auth_alg, if any, from last attempt. */
3037 		wpa_s->sme.auth_alg = WPA_AUTH_ALG_OPEN;
3038 	}
3039 #endif /* CONFIG_SME */
3040 
3041 	wpas_abort_ongoing_scan(wpa_s);
3042 
3043 	cwork = os_zalloc(sizeof(*cwork));
3044 	if (cwork == NULL)
3045 		return;
3046 
3047 	cwork->bss = bss;
3048 	cwork->ssid = ssid;
3049 
3050 	if (!radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
3051 			    wpas_start_assoc_cb, cwork)) {
3052 		os_free(cwork);
3053 	}
3054 }
3055 
3056 
bss_is_ibss(struct wpa_bss * bss)3057 static int bss_is_ibss(struct wpa_bss *bss)
3058 {
3059 	return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
3060 		IEEE80211_CAP_IBSS;
3061 }
3062 
3063 
drv_supports_vht(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid)3064 static int drv_supports_vht(struct wpa_supplicant *wpa_s,
3065 			    const struct wpa_ssid *ssid)
3066 {
3067 	enum hostapd_hw_mode hw_mode;
3068 	struct hostapd_hw_modes *mode = NULL;
3069 	u8 channel;
3070 	int i;
3071 
3072 	hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
3073 	if (hw_mode == NUM_HOSTAPD_MODES)
3074 		return 0;
3075 	for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
3076 		if (wpa_s->hw.modes[i].mode == hw_mode) {
3077 			mode = &wpa_s->hw.modes[i];
3078 			break;
3079 		}
3080 	}
3081 
3082 	if (!mode)
3083 		return 0;
3084 
3085 	return mode->vht_capab != 0;
3086 }
3087 
3088 
ibss_mesh_is_80mhz_avail(int channel,struct hostapd_hw_modes * mode)3089 static bool ibss_mesh_is_80mhz_avail(int channel, struct hostapd_hw_modes *mode)
3090 {
3091 	int i;
3092 
3093 	for (i = channel; i < channel + 16; i += 4) {
3094 		struct hostapd_channel_data *chan;
3095 
3096 		chan = hw_get_channel_chan(mode, i, NULL);
3097 		if (!chan ||
3098 		    chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
3099 			return false;
3100 	}
3101 
3102 	return true;
3103 }
3104 
3105 
ibss_find_existing_bss(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid)3106 static struct wpa_bss * ibss_find_existing_bss(struct wpa_supplicant *wpa_s,
3107 					       const struct wpa_ssid *ssid)
3108 {
3109 	unsigned int j;
3110 
3111 	for (j = 0; j < wpa_s->last_scan_res_used; j++) {
3112 		struct wpa_bss *bss = wpa_s->last_scan_res[j];
3113 
3114 		if (!bss_is_ibss(bss))
3115 			continue;
3116 
3117 		if (ssid->ssid_len == bss->ssid_len &&
3118 		    os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0)
3119 			return bss;
3120 	}
3121 	return NULL;
3122 }
3123 
3124 
ibss_mesh_can_use_ht(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,struct hostapd_hw_modes * mode)3125 static bool ibss_mesh_can_use_ht(struct wpa_supplicant *wpa_s,
3126 				 const struct wpa_ssid *ssid,
3127 				 struct hostapd_hw_modes *mode)
3128 {
3129 	/* For IBSS check HT_IBSS flag */
3130 	if (ssid->mode == WPAS_MODE_IBSS &&
3131 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
3132 		return false;
3133 
3134 	if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
3135 	    wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
3136 	    wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
3137 		wpa_printf(MSG_DEBUG,
3138 			   "IBSS: WEP/TKIP detected, do not try to enable HT");
3139 		return false;
3140 	}
3141 
3142 	if (!ht_supported(mode))
3143 		return false;
3144 
3145 #ifdef CONFIG_HT_OVERRIDES
3146 	if (ssid->disable_ht)
3147 		return false;
3148 #endif /* CONFIG_HT_OVERRIDES */
3149 
3150 	return true;
3151 }
3152 
3153 
ibss_mesh_can_use_vht(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,struct hostapd_hw_modes * mode)3154 static bool ibss_mesh_can_use_vht(struct wpa_supplicant *wpa_s,
3155 				  const struct wpa_ssid *ssid,
3156 				  struct hostapd_hw_modes *mode)
3157 {
3158 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3159 		return false;
3160 
3161 	if (!drv_supports_vht(wpa_s, ssid))
3162 		return false;
3163 
3164 	/* For IBSS check VHT_IBSS flag */
3165 	if (ssid->mode == WPAS_MODE_IBSS &&
3166 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
3167 		return false;
3168 
3169 	if (!vht_supported(mode))
3170 		return false;
3171 
3172 #ifdef CONFIG_VHT_OVERRIDES
3173 	if (ssid->disable_vht)
3174 		return false;
3175 #endif /* CONFIG_VHT_OVERRIDES */
3176 
3177 	return true;
3178 }
3179 
3180 
ibss_mesh_can_use_he(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,const struct hostapd_hw_modes * mode,int ieee80211_mode)3181 static bool ibss_mesh_can_use_he(struct wpa_supplicant *wpa_s,
3182 				 const struct wpa_ssid *ssid,
3183 				 const struct hostapd_hw_modes *mode,
3184 				 int ieee80211_mode)
3185 {
3186 #ifdef CONFIG_HE_OVERRIDES
3187 	if (ssid->disable_he)
3188 		return false;
3189 #endif /* CONFIG_HE_OVERRIDES */
3190 
3191 	switch (mode->mode) {
3192 	case HOSTAPD_MODE_IEEE80211G:
3193 	case HOSTAPD_MODE_IEEE80211B:
3194 	case HOSTAPD_MODE_IEEE80211A:
3195 		return mode->he_capab[ieee80211_mode].he_supported;
3196 	default:
3197 		return false;
3198 	}
3199 }
3200 
3201 
ibss_mesh_can_use_eht(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,const struct hostapd_hw_modes * mode,int ieee80211_mode)3202 static bool ibss_mesh_can_use_eht(struct wpa_supplicant *wpa_s,
3203 				  const struct wpa_ssid *ssid,
3204 				  const struct hostapd_hw_modes *mode,
3205 				  int ieee80211_mode)
3206 {
3207 	if (ssid->disable_eht)
3208 		return false;
3209 
3210 	switch(mode->mode) {
3211 	case HOSTAPD_MODE_IEEE80211G:
3212 	case HOSTAPD_MODE_IEEE80211B:
3213 	case HOSTAPD_MODE_IEEE80211A:
3214 		return mode->eht_capab[ieee80211_mode].eht_supported;
3215 	default:
3216 		return false;
3217 	}
3218 }
3219 
3220 
ibss_mesh_select_40mhz(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,struct hostapd_hw_modes * mode,struct hostapd_freq_params * freq,int obss_scan,bool is_6ghz)3221 static void ibss_mesh_select_40mhz(struct wpa_supplicant *wpa_s,
3222 				   const struct wpa_ssid *ssid,
3223 				   struct hostapd_hw_modes *mode,
3224 				   struct hostapd_freq_params *freq,
3225 				   int obss_scan, bool is_6ghz)
3226 {
3227 	int chan_idx;
3228 	struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
3229 	int i, res;
3230 	unsigned int j;
3231 	static const int ht40plus_5ghz[] = {
3232 		36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
3233 		149, 157, 165, 173, 184, 192
3234 	};
3235 	static const int ht40plus_6ghz[] = {
3236 		1, 9, 17, 25, 33, 41, 49, 57, 65, 73,
3237 		81, 89, 97, 105, 113, 121, 129, 137, 145, 153,
3238 		161, 169, 177, 185, 193, 201, 209, 217, 225
3239 	};
3240 
3241 	int ht40 = -1;
3242 
3243 	if (!freq->ht_enabled && !is_6ghz)
3244 		return;
3245 
3246 	for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
3247 		pri_chan = &mode->channels[chan_idx];
3248 		if (pri_chan->chan == freq->channel)
3249 			break;
3250 		pri_chan = NULL;
3251 	}
3252 	if (!pri_chan)
3253 		return;
3254 
3255 	/* Check primary channel flags */
3256 	if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
3257 		return;
3258 
3259 #ifdef CONFIG_HT_OVERRIDES
3260 	if (ssid->disable_ht40)
3261 		return;
3262 #endif
3263 
3264 	/* Check/setup HT40+/HT40- */
3265 	if (is_6ghz) {
3266 		for (j = 0; j < ARRAY_SIZE(ht40plus_6ghz); j++) {
3267 			if (ht40plus_6ghz[j] == freq->channel) {
3268 				ht40 = 1;
3269 				break;
3270 			}
3271 		}
3272 	} else {
3273 		for (j = 0; j < ARRAY_SIZE(ht40plus_5ghz); j++) {
3274 			if (ht40plus_5ghz[j] == freq->channel) {
3275 				ht40 = 1;
3276 				break;
3277 			}
3278 		}
3279 	}
3280 
3281 	/* Find secondary channel */
3282 	for (i = 0; i < mode->num_channels; i++) {
3283 		sec_chan = &mode->channels[i];
3284 		if (sec_chan->chan == freq->channel + ht40 * 4)
3285 			break;
3286 		sec_chan = NULL;
3287 	}
3288 	if (!sec_chan)
3289 		return;
3290 
3291 	/* Check secondary channel flags */
3292 	if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
3293 		return;
3294 
3295 	if (freq->ht_enabled) {
3296 		if (ht40 == -1) {
3297 			if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
3298 				return;
3299 		} else {
3300 			if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
3301 				return;
3302 		}
3303 	}
3304 	freq->sec_channel_offset = ht40;
3305 
3306 	if (obss_scan) {
3307 		struct wpa_scan_results *scan_res;
3308 
3309 		scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0,
3310 							   NULL);
3311 		if (scan_res == NULL) {
3312 			/* Back to HT20 */
3313 			freq->sec_channel_offset = 0;
3314 			return;
3315 		}
3316 
3317 		res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
3318 		switch (res) {
3319 		case 0:
3320 			/* Back to HT20 */
3321 			freq->sec_channel_offset = 0;
3322 			break;
3323 		case 1:
3324 			/* Configuration allowed */
3325 			break;
3326 		case 2:
3327 			/* Switch pri/sec channels */
3328 			freq->freq = hw_get_freq(mode, sec_chan->chan);
3329 			freq->sec_channel_offset = -freq->sec_channel_offset;
3330 			freq->channel = sec_chan->chan;
3331 			break;
3332 		default:
3333 			freq->sec_channel_offset = 0;
3334 			break;
3335 		}
3336 
3337 		wpa_scan_results_free(scan_res);
3338 	}
3339 
3340 	wpa_printf(MSG_DEBUG,
3341 		   "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
3342 		   freq->channel, freq->sec_channel_offset);
3343 }
3344 
3345 
ibss_get_center_320mhz(int channel)3346 static int ibss_get_center_320mhz(int channel)
3347 {
3348 	int seg0;
3349 
3350 	if (channel >= 1 && channel <= 45)
3351 		seg0 = 31;
3352 	else if (channel >= 49 && channel <= 77)
3353 		seg0 = 63;
3354 	else if (channel >= 81 && channel <= 109)
3355 		seg0 = 95;
3356 	else if (channel >= 113 && channel <= 141)
3357 		seg0 = 127;
3358 	else if (channel >= 145 && channel <= 173)
3359 		seg0 = 159;
3360 	else
3361 		seg0 = 191;
3362 
3363 	return seg0;
3364 }
3365 
3366 
ibss_mesh_select_80_160mhz(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,struct hostapd_hw_modes * mode,struct hostapd_freq_params * freq,int ieee80211_mode,bool is_6ghz)3367 static bool ibss_mesh_select_80_160mhz(struct wpa_supplicant *wpa_s,
3368 				       const struct wpa_ssid *ssid,
3369 				       struct hostapd_hw_modes *mode,
3370 				       struct hostapd_freq_params *freq,
3371 				       int ieee80211_mode, bool is_6ghz) {
3372 	static const int bw80[] = {
3373 		5180, 5260, 5500, 5580, 5660, 5745, 5825,
3374 		5955, 6035, 6115, 6195, 6275, 6355, 6435,
3375 		6515, 6595, 6675, 6755, 6835, 6915, 6995
3376 	};
3377 	static const int bw160[] = {
3378 		5180, 5500, 5745, 5955, 6115, 6275, 6435,
3379 		6595, 6755, 6915
3380 	};
3381 	static const int bw320[]= {
3382 		5955, 6255, 6115, 6415, 6275, 6575, 6435,
3383 		6735, 6595, 6895, 6755, 7055
3384 	};
3385 
3386 	struct hostapd_freq_params vht_freq;
3387 	int i;
3388 	unsigned int j, k;
3389 	int chwidth, seg0, seg1;
3390 	int offset_in_160 = 1;
3391 	int offset_in_320 = 0;
3392 	u32 vht_caps = 0;
3393 	u8 channel = freq->channel;
3394 
3395 	if (!freq->vht_enabled && !freq->he_enabled)
3396 		return true;
3397 
3398 	vht_freq = *freq;
3399 
3400 	chwidth = CONF_OPER_CHWIDTH_USE_HT;
3401 	seg0 = freq->channel + 2 * freq->sec_channel_offset;
3402 	seg1 = 0;
3403 	if (freq->sec_channel_offset == 0) {
3404 		seg0 = 0;
3405 		/* Don't try 80 MHz if 40 MHz failed, except in 6 GHz */
3406 		if (freq->ht_enabled && !is_6ghz)
3407 			goto skip_80mhz;
3408 	}
3409 	if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
3410 		goto skip_80mhz;
3411 
3412 	/* setup center_freq1, bandwidth */
3413 	for (j = 0; j < ARRAY_SIZE(bw80); j++) {
3414 		if (freq->freq >= bw80[j] &&
3415 		    freq->freq < bw80[j] + 80)
3416 			break;
3417 	}
3418 
3419 	if (j == ARRAY_SIZE(bw80) ||
3420 	    ieee80211_freq_to_chan(bw80[j], &channel) == NUM_HOSTAPD_MODES)
3421 		goto skip_80mhz;
3422 
3423 	/* Use 40 MHz if channel not usable */
3424 	if (!ibss_mesh_is_80mhz_avail(channel, mode))
3425 		goto skip_80mhz;
3426 
3427 	chwidth = CONF_OPER_CHWIDTH_80MHZ;
3428 	seg0 = channel + 6;
3429 	seg1 = 0;
3430 
3431 	for (k = 0; k < ARRAY_SIZE(bw160); k++) {
3432 		if (bw80[j] >= bw160[k] &&
3433 		    bw80[j] < bw160[k] + 160) {
3434 			if (bw80[j] == bw160[k])
3435 				offset_in_160 = 1;
3436 			else
3437 				offset_in_160 = -1;
3438 			break;
3439 		}
3440 	}
3441 
3442 	for (k = 0; k < ARRAY_SIZE(bw320); k++) {
3443 		if (bw80[j] >= bw320[k] &&
3444 		    bw80[j] < bw320[k] + 320) {
3445 			if (bw80[j] == bw320[k])
3446 				offset_in_320 = 0;
3447 			else if (bw80[j] == bw320[k] + 80)
3448 				offset_in_320 = 1;
3449 			else if (bw80[j] == bw320[k] + 160)
3450 				offset_in_320 = 2;
3451 			else
3452 				offset_in_320 = 3;
3453 			break;
3454 		}
3455 	}
3456 
3457 	/* In 160 MHz, the initial four 20 MHz channels were validated
3458 	 * above. If 160 MHz is supported, check the remaining four 20 MHz
3459 	 * channels for the total of 160 MHz bandwidth.
3460 	 */
3461 	if ((mode->he_capab[ieee80211_mode].phy_cap[
3462 		     HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
3463 	     HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G) &&
3464 	    (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_160MHZ ||
3465 	     ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_320MHZ) &&
3466 	    ibss_mesh_is_80mhz_avail(channel + 16 * offset_in_160, mode)) {
3467 		for (j = 0; j < ARRAY_SIZE(bw160); j++) {
3468 			u8 start_chan;
3469 
3470 			if (freq->freq >= bw160[j] &&
3471 			    freq->freq < bw160[j] + 160) {
3472 				chwidth = CONF_OPER_CHWIDTH_160MHZ;
3473 				ieee80211_freq_to_chan(bw160[j], &start_chan);
3474 				seg0 = start_chan + 14;
3475 				break;
3476 			}
3477 		}
3478 	}
3479 
3480 	/* In 320 MHz, the initial four 20 MHz channels were validated
3481 	 * above. If 320 MHz is supported, check the remaining 12 20 MHz
3482 	 * channels for the total of 320 MHz bandwidth for 6 GHz.
3483 	 */
3484 	if ((mode->eht_capab[ieee80211_mode].phy_cap[
3485 		     EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
3486 	     EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK) && is_6ghz &&
3487 	    ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_320MHZ &&
3488 	    ibss_mesh_is_80mhz_avail(channel + 16 -
3489 				     64 * ((offset_in_320 + 1) / 4), mode) &&
3490 	    ibss_mesh_is_80mhz_avail(channel + 32 -
3491 				     64 * ((offset_in_320 + 2) / 4), mode) &&
3492 	    ibss_mesh_is_80mhz_avail(channel + 48 -
3493 				     64 * ((offset_in_320 + 3) / 4), mode)) {
3494 		for (j = 0; j < ARRAY_SIZE(bw320); j += 2) {
3495 			if (freq->freq >= bw320[j] &&
3496 			    freq->freq <= bw320[j + 1]) {
3497 				chwidth = CONF_OPER_CHWIDTH_320MHZ;
3498 				seg0 = ibss_get_center_320mhz(freq->channel);
3499 				break;
3500 			}
3501 		}
3502 	}
3503 
3504 	if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ) {
3505 		/* setup center_freq2, bandwidth */
3506 		for (k = 0; k < ARRAY_SIZE(bw80); k++) {
3507 			/* Only accept 80 MHz segments separated by a gap */
3508 			if (j == k || abs(bw80[j] - bw80[k]) == 80)
3509 				continue;
3510 
3511 			if (ieee80211_freq_to_chan(bw80[k], &channel) ==
3512 			    NUM_HOSTAPD_MODES)
3513 				break;
3514 
3515 			for (i = channel; i < channel + 16; i += 4) {
3516 				struct hostapd_channel_data *chan;
3517 
3518 				chan = hw_get_channel_chan(mode, i, NULL);
3519 				if (!chan)
3520 					continue;
3521 
3522 				if (chan->flag & (HOSTAPD_CHAN_DISABLED |
3523 						  HOSTAPD_CHAN_NO_IR |
3524 						  HOSTAPD_CHAN_RADAR))
3525 					continue;
3526 
3527 				/* Found a suitable second segment for 80+80 */
3528 				chwidth = CONF_OPER_CHWIDTH_80P80MHZ;
3529 				if (!is_6ghz)
3530 					vht_caps |=
3531 						VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
3532 				seg1 = channel + 6;
3533 			}
3534 
3535 			if (chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
3536 				break;
3537 		}
3538 	} else if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_160MHZ) {
3539 		if (freq->freq == 5180) {
3540 			chwidth = CONF_OPER_CHWIDTH_160MHZ;
3541 			vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
3542 			seg0 = 50;
3543 		} else if (freq->freq == 5520) {
3544 			chwidth = CONF_OPER_CHWIDTH_160MHZ;
3545 			vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
3546 			seg0 = 114;
3547 		}
3548 	}
3549 
3550 skip_80mhz:
3551 	if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
3552 				    freq->channel, ssid->enable_edmg,
3553 				    ssid->edmg_channel, freq->ht_enabled,
3554 				    freq->vht_enabled, freq->he_enabled,
3555 				    freq->eht_enabled,
3556 				    freq->sec_channel_offset,
3557 				    chwidth, seg0, seg1, vht_caps,
3558 				    &mode->he_capab[ieee80211_mode],
3559 				    &mode->eht_capab[ieee80211_mode], 0) != 0)
3560 		return false;
3561 
3562 	*freq = vht_freq;
3563 
3564 	wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
3565 		   freq->center_freq1, freq->center_freq2, freq->bandwidth);
3566 	return true;
3567 }
3568 
3569 
ibss_mesh_setup_freq(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid,struct hostapd_freq_params * freq)3570 void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
3571 			  const struct wpa_ssid *ssid,
3572 			  struct hostapd_freq_params *freq)
3573 {
3574 	int ieee80211_mode = wpas_mode_to_ieee80211_mode(ssid->mode);
3575 	enum hostapd_hw_mode hw_mode;
3576 	struct hostapd_hw_modes *mode = NULL;
3577 	int obss_scan = 1;
3578 	u8 channel;
3579 	bool is_6ghz, is_24ghz;
3580 
3581 	freq->freq = ssid->frequency;
3582 
3583 	if (ssid->mode == WPAS_MODE_IBSS && !ssid->fixed_freq) {
3584 		struct wpa_bss *bss = ibss_find_existing_bss(wpa_s, ssid);
3585 
3586 		if (bss) {
3587 			wpa_printf(MSG_DEBUG,
3588 				   "IBSS already found in scan results, adjust control freq: %d",
3589 				   bss->freq);
3590 			freq->freq = bss->freq;
3591 			obss_scan = 0;
3592 		}
3593 	}
3594 
3595 	hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
3596 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
3597 			hw_mode, is_6ghz_freq(ssid->frequency));
3598 
3599 	if (!mode)
3600 		return;
3601 
3602 	is_24ghz = hw_mode == HOSTAPD_MODE_IEEE80211G ||
3603 		hw_mode == HOSTAPD_MODE_IEEE80211B;
3604 
3605 	is_6ghz = is_6ghz_freq(freq->freq);
3606 
3607 	freq->ht_enabled = 0;
3608 	freq->vht_enabled = 0;
3609 	freq->he_enabled = 0;
3610 	freq->eht_enabled = 0;
3611 
3612 	if (!is_6ghz)
3613 		freq->ht_enabled = ibss_mesh_can_use_ht(wpa_s, ssid, mode);
3614 	if (freq->ht_enabled)
3615 		freq->vht_enabled = ibss_mesh_can_use_vht(wpa_s, ssid, mode);
3616 	if (freq->vht_enabled || (freq->ht_enabled && is_24ghz) || is_6ghz)
3617 		freq->he_enabled = ibss_mesh_can_use_he(wpa_s, ssid, mode,
3618 							ieee80211_mode);
3619 	freq->channel = channel;
3620 	/* Setup higher BW only for 5 and 6 GHz */
3621 	if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
3622 		ibss_mesh_select_40mhz(wpa_s, ssid, mode, freq, obss_scan,
3623 				       is_6ghz);
3624 		if (!ibss_mesh_select_80_160mhz(wpa_s, ssid, mode, freq,
3625 						ieee80211_mode, is_6ghz))
3626 			freq->he_enabled = freq->vht_enabled = false;
3627 	}
3628 
3629 	if (freq->he_enabled)
3630 		freq->eht_enabled = ibss_mesh_can_use_eht(wpa_s, ssid, mode,
3631 							  ieee80211_mode);
3632 }
3633 
3634 
3635 #ifdef CONFIG_FILS
wpas_add_fils_hlp_req(struct wpa_supplicant * wpa_s,u8 * ie_buf,size_t ie_buf_len)3636 static size_t wpas_add_fils_hlp_req(struct wpa_supplicant *wpa_s, u8 *ie_buf,
3637 				    size_t ie_buf_len)
3638 {
3639 	struct fils_hlp_req *req;
3640 	size_t rem_len, hdr_len, hlp_len, len, ie_len = 0;
3641 	const u8 *pos;
3642 	u8 *buf = ie_buf;
3643 
3644 	dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
3645 			 list) {
3646 		rem_len = ie_buf_len - ie_len;
3647 		pos = wpabuf_head(req->pkt);
3648 		hdr_len = 1 + 2 * ETH_ALEN + 6;
3649 		hlp_len = wpabuf_len(req->pkt);
3650 
3651 		if (rem_len < 2 + hdr_len + hlp_len) {
3652 			wpa_printf(MSG_ERROR,
3653 				   "FILS: Cannot fit HLP - rem_len=%lu to_fill=%lu",
3654 				   (unsigned long) rem_len,
3655 				   (unsigned long) (2 + hdr_len + hlp_len));
3656 			break;
3657 		}
3658 
3659 		len = (hdr_len + hlp_len) > 255 ? 255 : hdr_len + hlp_len;
3660 		/* Element ID */
3661 		*buf++ = WLAN_EID_EXTENSION;
3662 		/* Length */
3663 		*buf++ = len;
3664 		/* Element ID Extension */
3665 		*buf++ = WLAN_EID_EXT_FILS_HLP_CONTAINER;
3666 		/* Destination MAC address */
3667 		os_memcpy(buf, req->dst, ETH_ALEN);
3668 		buf += ETH_ALEN;
3669 		/* Source MAC address */
3670 		os_memcpy(buf, wpa_s->own_addr, ETH_ALEN);
3671 		buf += ETH_ALEN;
3672 		/* LLC/SNAP Header */
3673 		os_memcpy(buf, "\xaa\xaa\x03\x00\x00\x00", 6);
3674 		buf += 6;
3675 		/* HLP Packet */
3676 		os_memcpy(buf, pos, len - hdr_len);
3677 		buf += len - hdr_len;
3678 		pos += len - hdr_len;
3679 
3680 		hlp_len -= len - hdr_len;
3681 		ie_len += 2 + len;
3682 		rem_len -= 2 + len;
3683 
3684 		while (hlp_len) {
3685 			len = (hlp_len > 255) ? 255 : hlp_len;
3686 			if (rem_len < 2 + len)
3687 				break;
3688 			*buf++ = WLAN_EID_FRAGMENT;
3689 			*buf++ = len;
3690 			os_memcpy(buf, pos, len);
3691 			buf += len;
3692 			pos += len;
3693 
3694 			hlp_len -= len;
3695 			ie_len += 2 + len;
3696 			rem_len -= 2 + len;
3697 		}
3698 	}
3699 
3700 	return ie_len;
3701 }
3702 
3703 
wpa_is_fils_supported(struct wpa_supplicant * wpa_s)3704 int wpa_is_fils_supported(struct wpa_supplicant *wpa_s)
3705 {
3706 	return (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3707 		 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) ||
3708 		(!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3709 		 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)));
3710 }
3711 
3712 
wpa_is_fils_sk_pfs_supported(struct wpa_supplicant * wpa_s)3713 int wpa_is_fils_sk_pfs_supported(struct wpa_supplicant *wpa_s)
3714 {
3715 #ifdef CONFIG_FILS_SK_PFS
3716 	return (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3717 		(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS);
3718 #else /* CONFIG_FILS_SK_PFS */
3719 	return 0;
3720 #endif /* CONFIG_FILS_SK_PFS */
3721 }
3722 
3723 #endif /* CONFIG_FILS */
3724 
3725 
wpa_is_non_eht_scs_traffic_desc_supported(struct wpa_bss * bss)3726 bool wpa_is_non_eht_scs_traffic_desc_supported(struct wpa_bss *bss)
3727 {
3728 	const u8 *wfa_capa;
3729 
3730 	if (!bss)
3731 		return false;
3732 
3733 	/* Get WFA capability from Beacon or Probe Response frame elements */
3734 	wfa_capa = wpa_bss_get_vendor_ie(bss, WFA_CAPA_IE_VENDOR_TYPE);
3735 	if (!wfa_capa)
3736 		wfa_capa = wpa_bss_get_vendor_ie_beacon(
3737 			bss, WFA_CAPA_IE_VENDOR_TYPE);
3738 
3739 	if (!wfa_capa || wfa_capa[1] < 6 || wfa_capa[6] < 1 ||
3740 	    !(wfa_capa[7] & WFA_CAPA_QM_NON_EHT_SCS_TRAFFIC_DESC)) {
3741 		/* AP does not enable QM non EHT traffic description policy */
3742 		return false;
3743 	}
3744 
3745 	return true;
3746 }
3747 
3748 
wpas_populate_wfa_capa(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,u8 * wpa_ie,size_t wpa_ie_len,size_t max_wpa_ie_len)3749 int wpas_populate_wfa_capa(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3750 			   u8 *wpa_ie, size_t wpa_ie_len, size_t max_wpa_ie_len)
3751 {
3752 	struct wpabuf *wfa_ie = NULL, *attr = NULL;
3753 	u8 wfa_capa[1];
3754 	u8 capab_len = 0;
3755 	size_t wfa_ie_len, buf_len;
3756 
3757 	os_memset(wfa_capa, 0, sizeof(wfa_capa));
3758 #ifndef CONFIG_NO_ROBUST_AV
3759 	if (wpa_s->enable_dscp_policy_capa)
3760 		wfa_capa[0] |= WFA_CAPA_QM_DSCP_POLICY;
3761 #endif /* CONFIG_NO_ROBUST_AV */
3762 
3763 	if (wpa_is_non_eht_scs_traffic_desc_supported(bss))
3764 		wfa_capa[0] |= WFA_CAPA_QM_NON_EHT_SCS_TRAFFIC_DESC;
3765 
3766 	if (wfa_capa[0])
3767 		capab_len = 1;
3768 
3769 	if (wpa_s->conf->wfa_gen_capa == WFA_GEN_CAPA_UNPROTECTED)
3770 		attr = wpas_wfa_gen_capab_attr(wpa_s);
3771 
3772 	if (capab_len == 0 && !attr)
3773 		return wpa_ie_len;
3774 
3775 	/* Wi-Fi Alliance element */
3776 	buf_len = 1 +	/* Element ID */
3777 		  1 +	/* Length */
3778 		  3 +	/* OUI */
3779 		  1 +	/* OUI Type */
3780 		  1 +	/* Capabilities Length */
3781 		  capab_len +	/* Capabilities */
3782 		  (attr ? wpabuf_len(attr) : 0) /* Attributes */;
3783 	wfa_ie = wpabuf_alloc(buf_len);
3784 	if (!wfa_ie) {
3785 		wpabuf_free(attr);
3786 		return wpa_ie_len;
3787 	}
3788 
3789 	wpabuf_put_u8(wfa_ie, WLAN_EID_VENDOR_SPECIFIC);
3790 	wpabuf_put_u8(wfa_ie, buf_len - 2);
3791 	wpabuf_put_be24(wfa_ie, OUI_WFA);
3792 	wpabuf_put_u8(wfa_ie, WFA_CAPA_OUI_TYPE);
3793 	wpabuf_put_u8(wfa_ie, capab_len);
3794 	wpabuf_put_data(wfa_ie, wfa_capa, capab_len);
3795 	if (attr)
3796 		wpabuf_put_buf(wfa_ie, attr);
3797 	wpabuf_free(attr);
3798 
3799 	wfa_ie_len = wpabuf_len(wfa_ie);
3800 	if (wpa_ie_len + wfa_ie_len <= max_wpa_ie_len) {
3801 		wpa_hexdump_buf(MSG_MSGDUMP, "WFA Capabilities element",
3802 				wfa_ie);
3803 		os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(wfa_ie),
3804 			  wfa_ie_len);
3805 		wpa_ie_len += wfa_ie_len;
3806 	}
3807 
3808 	wpabuf_free(wfa_ie);
3809 	return wpa_ie_len;
3810 }
3811 
3812 
3813 #ifdef CONFIG_IEEE8021X_AUTH
wpas_set_802_1x_auth_alg(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params)3814 static bool wpas_set_802_1x_auth_alg(struct wpa_supplicant *wpa_s,
3815 				     struct wpa_bss *bss,
3816 				     struct wpa_ssid *ssid,
3817 				     struct wpa_driver_associate_params *params)
3818 {
3819 	const u8 *rsnxe;
3820 
3821 	if (!ssid->eap_over_auth_frame ||
3822 	    !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_802_1X_AUTH) ||
3823 	    !wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt &
3824 					~WPA_KEY_MGMT_IEEE8021X))
3825 		return false;
3826 
3827 	params->ieee8021x_auth_supported = true;
3828 
3829 	if (!bss)
3830 		return false;
3831 
3832 	if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt &
3833 					~WPA_KEY_MGMT_IEEE8021X))
3834 		return false;
3835 
3836 	rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
3837 	if (ieee802_11_rsnx_capab(rsnxe,
3838 				  WLAN_RSNX_CAPAB_802_1X_IN_AUTH_FRAMES)) {
3839 		wpa_dbg(wpa_s, MSG_DEBUG,
3840 			"Using IEEE 802.1X authentication using Authentication frames");
3841 		return true;
3842 	}
3843 
3844 	return false;
3845 }
3846 #endif /* CONFIG_IEEE8021X_AUTH */
3847 
3848 
3849 #ifdef CONFIG_ENC_ASSOC
3850 
wpas_eppke_ap_capable(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,bool unauth_eppke)3851 bool wpas_eppke_ap_capable(struct wpa_supplicant *wpa_s,
3852 				  struct wpa_bss *bss, bool unauth_eppke)
3853 {
3854 	const u8 *ap_rsnxe;
3855 
3856 	if (!(wpa_s->drv_flags2 &
3857 	      WPA_DRIVER_FLAGS2_ASSOCIATION_FRAME_ENCRYPTION)) {
3858 		wpa_printf(MSG_DEBUG,
3859 			   "EPPKE: Driver does not support association frame encryption");
3860 		return false;
3861 	}
3862 
3863 	ap_rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
3864 
3865 	if (!ieee802_11_rsnx_capab(ap_rsnxe, WLAN_RSNX_CAPAB_KEK_IN_PASN)) {
3866 		wpa_printf(MSG_DEBUG, "EPPKE: AP does not support KEK_IN_PASN");
3867 		return false;
3868 	}
3869 
3870 	if (!ieee802_11_rsnx_capab(ap_rsnxe,
3871 				   WLAN_RSNX_CAPAB_ASSOC_FRAME_ENCRYPTION)) {
3872 		wpa_printf(MSG_DEBUG,
3873 			   "EPPKE: AP does not support association frame encryption");
3874 		return false;
3875 	}
3876 
3877 	if (unauth_eppke &&
3878 	    !ieee802_11_rsnx_capab(ap_rsnxe, WLAN_RSNX_CAPAB_UNAUTH_EPPKE)) {
3879 		wpa_printf(MSG_DEBUG,
3880 			   "EPPKE: AP does not support unauthenticated EPPKE");
3881 		return false;
3882 	}
3883 
3884 	return true;
3885 }
3886 
3887 
wpas_set_eppke_auth_alg(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params)3888 static bool wpas_set_eppke_auth_alg(struct wpa_supplicant *wpa_s,
3889 				    struct wpa_bss *bss,
3890 				    struct wpa_ssid *ssid,
3891 				    struct wpa_driver_associate_params *params)
3892 {
3893 	const u8 *rsn;
3894 	struct wpa_ie_data ied;
3895 
3896 	if (!wpa_key_mgmt_eppke(ssid->key_mgmt) ||
3897 	    !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_EPPKE))
3898 		return false;
3899 
3900 	params->eppke_supported = true;
3901 
3902 	if (!bss)
3903 		return false;
3904 
3905 	rsn = wpa_bss_get_rsne(wpa_s, bss, ssid, false);
3906 	if (!rsn) {
3907 		wpa_dbg(wpa_s, MSG_DEBUG,
3908 			"EPPKE: Target BSS does not advertise RSN");
3909 		return false;
3910 	}
3911 
3912 	if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied)) {
3913 		wpa_printf(MSG_DEBUG, "EPPKE: Failed parsing RSNE data");
3914 		return false;
3915 	}
3916 
3917 	if (!(ied.key_mgmt & WPA_KEY_MGMT_EPPKE)) {
3918 		wpa_dbg(wpa_s, MSG_DEBUG,
3919 			"EPPKE: Target BSS does not advertise EPPKE AKM");
3920 		return false;
3921 	}
3922 
3923 	if (!wpa_key_mgmt_eppke(wpa_s->key_mgmt) &&
3924 	    !wpa_key_mgmt_sae_ext_key(wpa_s->key_mgmt)) {
3925 		wpa_dbg(wpa_s, MSG_DEBUG,
3926 			"EPPKE: Negotiated AKM is not an EPPKE Authentication AKM");
3927 		return false;
3928 	}
3929 
3930 	if (!wpas_eppke_ap_capable(wpa_s, bss,
3931 				   !!wpa_key_mgmt_eppke(wpa_s->key_mgmt))) {
3932 		wpa_dbg(wpa_s, MSG_DEBUG,
3933 			"EPPKE: Target BSS does not indicate support for EPPKE");
3934 		return false;
3935 	}
3936 
3937 	wpa_dbg(wpa_s, MSG_DEBUG, "Using EPPKE Authentication");
3938 
3939 	return true;
3940 }
3941 
3942 #endif /* CONFIG_ENC_ASSOC */
3943 
3944 
wpas_populate_assoc_ies(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params,enum wpa_drv_update_connect_params_mask * mask)3945 static u8 * wpas_populate_assoc_ies(
3946 	struct wpa_supplicant *wpa_s,
3947 	struct wpa_bss *bss, struct wpa_ssid *ssid,
3948 	struct wpa_driver_associate_params *params,
3949 	enum wpa_drv_update_connect_params_mask *mask)
3950 {
3951 	u8 *wpa_ie;
3952 	size_t max_wpa_ie_len = 500;
3953 	size_t wpa_ie_len;
3954 	int algs = WPA_AUTH_ALG_OPEN;
3955 #ifdef CONFIG_MBO
3956 	const u8 *mbo_ie;
3957 #endif
3958 #if defined(CONFIG_SAE) || defined(CONFIG_FILS)
3959 	int pmksa_cached = 0;
3960 #endif /* CONFIG_SAE || CONFIG_FILS */
3961 #ifdef CONFIG_FILS
3962 	const u8 *realm, *username, *rrk;
3963 	size_t realm_len, username_len, rrk_len;
3964 	u16 next_seq_num;
3965 	struct fils_hlp_req *req;
3966 
3967 	dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
3968 			 list) {
3969 		max_wpa_ie_len += 3 + 2 * ETH_ALEN + 6 + wpabuf_len(req->pkt) +
3970 				  2 + 2 * wpabuf_len(req->pkt) / 255;
3971 	}
3972 #endif /* CONFIG_FILS */
3973 
3974 	wpa_ie = os_malloc(max_wpa_ie_len);
3975 	if (!wpa_ie) {
3976 		wpa_printf(MSG_ERROR,
3977 			   "Failed to allocate connect IE buffer for %lu bytes",
3978 			   (unsigned long) max_wpa_ie_len);
3979 		return NULL;
3980 	}
3981 
3982 	if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
3983 		    wpa_bss_get_rsne(wpa_s, bss, ssid, false)) &&
3984 	    wpa_key_mgmt_wpa(ssid->key_mgmt)) {
3985 		int try_opportunistic;
3986 		const u8 *cache_id = NULL;
3987 		const u8 *addr = bss->bssid;
3988 
3989 		if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3990 		    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_MLO) &&
3991 		    !is_zero_ether_addr(bss->mld_addr))
3992 			addr = bss->mld_addr;
3993 
3994 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3995 		    wpa_s->valid_links)
3996 			addr = wpa_s->ap_mld_addr;
3997 
3998 		try_opportunistic = (ssid->proactive_key_caching < 0 ?
3999 				     wpa_s->conf->okc :
4000 				     ssid->proactive_key_caching) &&
4001 			(ssid->proto & WPA_PROTO_RSN);
4002 #ifdef CONFIG_FILS
4003 		if (wpa_key_mgmt_fils(ssid->key_mgmt))
4004 			cache_id = wpa_bss_get_fils_cache_id(bss);
4005 #endif /* CONFIG_FILS */
4006 		if (pmksa_cache_set_current(wpa_s->wpa, NULL, addr,
4007 					    ssid, try_opportunistic,
4008 					    cache_id, 0, false) == 0) {
4009 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
4010 #if defined(CONFIG_SAE) || defined(CONFIG_FILS)
4011 			pmksa_cached = 1;
4012 #endif /* CONFIG_SAE || CONFIG_FILS */
4013 		}
4014 		wpa_ie_len = max_wpa_ie_len;
4015 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
4016 					      wpa_ie, &wpa_ie_len, false)) {
4017 			wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
4018 				"key management and encryption suites");
4019 			os_free(wpa_ie);
4020 			return NULL;
4021 		}
4022 	} else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
4023 		   wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
4024 		/*
4025 		 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
4026 		 * use non-WPA since the scan results did not indicate that the
4027 		 * AP is using WPA or WPA2.
4028 		 */
4029 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
4030 		wpa_ie_len = 0;
4031 		wpa_s->wpa_proto = 0;
4032 	} else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
4033 		wpa_ie_len = max_wpa_ie_len;
4034 		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
4035 					      wpa_ie, &wpa_ie_len, false)) {
4036 			wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
4037 				"key management and encryption suites (no "
4038 				"scan results)");
4039 			os_free(wpa_ie);
4040 			return NULL;
4041 		}
4042 #ifdef CONFIG_WPS
4043 	} else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
4044 		struct wpabuf *wps_ie;
4045 		wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
4046 		if (wps_ie && wpabuf_len(wps_ie) <= max_wpa_ie_len) {
4047 			wpa_ie_len = wpabuf_len(wps_ie);
4048 			os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
4049 		} else
4050 			wpa_ie_len = 0;
4051 		wpabuf_free(wps_ie);
4052 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
4053 		if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
4054 			params->wps = WPS_MODE_PRIVACY;
4055 		else
4056 			params->wps = WPS_MODE_OPEN;
4057 		wpa_s->wpa_proto = 0;
4058 #endif /* CONFIG_WPS */
4059 	} else {
4060 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
4061 		wpa_ie_len = 0;
4062 		wpa_s->wpa_proto = 0;
4063 	}
4064 
4065 #ifdef IEEE8021X_EAPOL
4066 	if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4067 		if (ssid->leap) {
4068 			if (ssid->non_leap == 0)
4069 				algs = WPA_AUTH_ALG_LEAP;
4070 			else
4071 				algs |= WPA_AUTH_ALG_LEAP;
4072 		}
4073 	}
4074 
4075 #ifdef CONFIG_FILS
4076 	/* Clear FILS association */
4077 	wpa_sm_set_reset_fils_completed(wpa_s->wpa, 0);
4078 
4079 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
4080 	    ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
4081 	    eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap, &username,
4082 				  &username_len, &realm, &realm_len,
4083 				  &next_seq_num, &rrk, &rrk_len) == 0 &&
4084 	    (!wpa_s->last_con_fail_realm ||
4085 	     wpa_s->last_con_fail_realm_len != realm_len ||
4086 	     os_memcmp(wpa_s->last_con_fail_realm, realm, realm_len) != 0)) {
4087 		algs = WPA_AUTH_ALG_FILS;
4088 		params->fils_erp_username = username;
4089 		params->fils_erp_username_len = username_len;
4090 		params->fils_erp_realm = realm;
4091 		params->fils_erp_realm_len = realm_len;
4092 		params->fils_erp_next_seq_num = next_seq_num;
4093 		params->fils_erp_rrk = rrk;
4094 		params->fils_erp_rrk_len = rrk_len;
4095 
4096 		if (mask)
4097 			*mask |= WPA_DRV_UPDATE_FILS_ERP_INFO;
4098 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
4099 		   ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
4100 		   pmksa_cached) {
4101 		algs = WPA_AUTH_ALG_FILS;
4102 	}
4103 #endif /* CONFIG_FILS */
4104 #endif /* IEEE8021X_EAPOL */
4105 #ifdef CONFIG_SAE
4106 	if (wpa_key_mgmt_sae(wpa_s->key_mgmt))
4107 		algs = WPA_AUTH_ALG_SAE;
4108 #endif /* CONFIG_SAE */
4109 
4110 #ifdef CONFIG_IEEE8021X_AUTH
4111 	if (wpas_set_802_1x_auth_alg(wpa_s, bss, ssid, params))
4112 		algs = WPA_AUTH_ALG_802_1X;
4113 #endif /* CONFIG_IEEE8021X_AUTH */
4114 
4115 #ifdef CONFIG_ENC_ASSOC
4116 	if (wpas_set_eppke_auth_alg(wpa_s, bss, ssid, params))
4117 		algs = WPA_AUTH_ALG_EPPKE;
4118 #endif /* CONFIG_ENC_ASSOC */
4119 
4120 	wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
4121 	if (ssid->auth_alg) {
4122 		algs = ssid->auth_alg;
4123 		wpa_dbg(wpa_s, MSG_DEBUG,
4124 			"Overriding auth_alg selection: 0x%x", algs);
4125 	}
4126 
4127 #ifdef CONFIG_SAE
4128 	if (pmksa_cached && algs == WPA_AUTH_ALG_SAE) {
4129 		wpa_dbg(wpa_s, MSG_DEBUG,
4130 			"SAE: Use WPA_AUTH_ALG_OPEN for PMKSA caching attempt");
4131 		algs = WPA_AUTH_ALG_OPEN;
4132 	}
4133 #endif /* CONFIG_SAE */
4134 
4135 #ifdef CONFIG_P2P
4136 	if (wpa_s->global->p2p) {
4137 		u8 *pos;
4138 		size_t len;
4139 		int res;
4140 		pos = wpa_ie + wpa_ie_len;
4141 		len = max_wpa_ie_len - wpa_ie_len;
4142 		res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
4143 					    ssid->p2p_group);
4144 		if (res >= 0)
4145 			wpa_ie_len += res;
4146 	}
4147 
4148 	wpa_s->cross_connect_disallowed = 0;
4149 	if (bss) {
4150 		struct wpabuf *p2p;
4151 		p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4152 		if (p2p) {
4153 			wpa_s->cross_connect_disallowed =
4154 				p2p_get_cross_connect_disallowed(p2p);
4155 			wpabuf_free(p2p);
4156 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
4157 				"connection",
4158 				wpa_s->cross_connect_disallowed ?
4159 				"disallows" : "allows");
4160 		}
4161 	}
4162 
4163 	os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
4164 #endif /* CONFIG_P2P */
4165 
4166 #ifndef CONFIG_NO_RRM
4167 	if (bss) {
4168 		wpa_ie_len += wpas_supp_op_class_ie(wpa_s, ssid, bss,
4169 						    wpa_ie + wpa_ie_len,
4170 						    max_wpa_ie_len -
4171 						    wpa_ie_len);
4172 	}
4173 #endif /* CONFIG_NO_RRM */
4174 
4175 	/*
4176 	 * Workaround: Add Extended Capabilities element only if the AP
4177 	 * included this element in Beacon/Probe Response frames. Some older
4178 	 * APs seem to have interoperability issues if this element is
4179 	 * included, so while the standard may require us to include the
4180 	 * element in all cases, it is justifiable to skip it to avoid
4181 	 * interoperability issues.
4182 	 */
4183 	if (ssid->p2p_group)
4184 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
4185 	else
4186 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
4187 
4188 	if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
4189 		u8 ext_capab[18];
4190 		int ext_capab_len;
4191 		ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
4192 						     sizeof(ext_capab), bss);
4193 		if (ext_capab_len > 0 &&
4194 		    wpa_ie_len + ext_capab_len <= max_wpa_ie_len) {
4195 			u8 *pos = wpa_ie;
4196 			if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
4197 				pos += 2 + pos[1];
4198 			os_memmove(pos + ext_capab_len, pos,
4199 				   wpa_ie_len - (pos - wpa_ie));
4200 			wpa_ie_len += ext_capab_len;
4201 			os_memcpy(pos, ext_capab, ext_capab_len);
4202 		}
4203 	}
4204 
4205 	if (ssid->max_idle && wpa_ie_len + 5 <= max_wpa_ie_len) {
4206 		u8 *pos = wpa_ie;
4207 
4208 		*pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD;
4209 		*pos++ = 3;
4210 		WPA_PUT_LE16(pos, ssid->max_idle);
4211 		pos += 2;
4212 		*pos = 0; /* Idle Options */
4213 		wpa_ie_len += 5;
4214 	}
4215 
4216 #ifdef CONFIG_HS20
4217 	if (is_hs20_network(wpa_s, ssid, bss)) {
4218 		struct wpabuf *hs20;
4219 
4220 		hs20 = wpabuf_alloc(20 + MAX_ROAMING_CONS_OI_LEN);
4221 		if (hs20) {
4222 			int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
4223 			size_t len;
4224 
4225 			wpas_hs20_add_indication(hs20, pps_mo_id,
4226 						 get_hs20_version(bss));
4227 			wpas_hs20_add_roam_cons_sel(hs20, ssid);
4228 			len = max_wpa_ie_len - wpa_ie_len;
4229 			if (wpabuf_len(hs20) <= len) {
4230 				os_memcpy(wpa_ie + wpa_ie_len,
4231 					  wpabuf_head(hs20), wpabuf_len(hs20));
4232 				wpa_ie_len += wpabuf_len(hs20);
4233 			}
4234 			wpabuf_free(hs20);
4235 		}
4236 	}
4237 #endif /* CONFIG_HS20 */
4238 
4239 	wpas_configure_frame_filters(wpa_s);
4240 
4241 	if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
4242 		struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
4243 		size_t len;
4244 
4245 		len = max_wpa_ie_len - wpa_ie_len;
4246 		if (wpabuf_len(buf) <= len) {
4247 			os_memcpy(wpa_ie + wpa_ie_len,
4248 				  wpabuf_head(buf), wpabuf_len(buf));
4249 			wpa_ie_len += wpabuf_len(buf);
4250 		}
4251 	}
4252 
4253 #ifdef CONFIG_FST
4254 	if (wpa_s->fst_ies) {
4255 		int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
4256 
4257 		if (wpa_ie_len + fst_ies_len <= max_wpa_ie_len) {
4258 			os_memcpy(wpa_ie + wpa_ie_len,
4259 				  wpabuf_head(wpa_s->fst_ies), fst_ies_len);
4260 			wpa_ie_len += fst_ies_len;
4261 		}
4262 	}
4263 #endif /* CONFIG_FST */
4264 
4265 #ifdef CONFIG_MBO
4266 	mbo_ie = bss ? wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE) : NULL;
4267 	if (!wpa_s->disable_mbo_oce && mbo_ie) {
4268 		int len;
4269 
4270 		len = wpas_mbo_ie(wpa_s, wpa_ie + wpa_ie_len,
4271 				  max_wpa_ie_len - wpa_ie_len,
4272 				  !!mbo_attr_from_mbo_ie(mbo_ie,
4273 							 OCE_ATTR_ID_CAPA_IND));
4274 		if (len >= 0)
4275 			wpa_ie_len += len;
4276 	}
4277 #endif /* CONFIG_MBO */
4278 
4279 #ifdef CONFIG_FILS
4280 	if (algs == WPA_AUTH_ALG_FILS) {
4281 		size_t len;
4282 
4283 		len = wpas_add_fils_hlp_req(wpa_s, wpa_ie + wpa_ie_len,
4284 					    max_wpa_ie_len - wpa_ie_len);
4285 		wpa_ie_len += len;
4286 	}
4287 #endif /* CONFIG_FILS */
4288 
4289 #ifdef CONFIG_OWE
4290 #ifdef CONFIG_TESTING_OPTIONS
4291 	if (get_ie_ext(wpa_ie, wpa_ie_len, WLAN_EID_EXT_OWE_DH_PARAM)) {
4292 		wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
4293 	} else
4294 #endif /* CONFIG_TESTING_OPTIONS */
4295 	if (algs == WPA_AUTH_ALG_OPEN &&
4296 	    ssid->key_mgmt == WPA_KEY_MGMT_OWE &&
4297 	    !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA)) {
4298 		struct wpabuf *owe_ie;
4299 		u16 group;
4300 
4301 		if (ssid->owe_group) {
4302 			group = ssid->owe_group;
4303 		} else if (wpa_s->assoc_status_code ==
4304 			   WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
4305 			if (wpa_s->last_owe_group == 19)
4306 				group = 20;
4307 			else if (wpa_s->last_owe_group == 20)
4308 				group = 21;
4309 			else
4310 				group = OWE_DH_GROUP;
4311 		} else {
4312 			group = OWE_DH_GROUP;
4313 		}
4314 
4315 		wpa_s->last_owe_group = group;
4316 		wpa_printf(MSG_DEBUG, "OWE: Try to use group %u", group);
4317 		owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
4318 		if (owe_ie &&
4319 		    wpabuf_len(owe_ie) <= max_wpa_ie_len - wpa_ie_len) {
4320 			os_memcpy(wpa_ie + wpa_ie_len,
4321 				  wpabuf_head(owe_ie), wpabuf_len(owe_ie));
4322 			wpa_ie_len += wpabuf_len(owe_ie);
4323 		}
4324 		wpabuf_free(owe_ie);
4325 	}
4326 #endif /* CONFIG_OWE */
4327 
4328 #ifdef CONFIG_DPP2
4329 	if (DPP_VERSION > 1 &&
4330 	    wpa_sm_get_key_mgmt(wpa_s->wpa) == WPA_KEY_MGMT_DPP &&
4331 	    ssid->dpp_netaccesskey &&
4332 	    ssid->dpp_pfs != 2 && !ssid->dpp_pfs_fallback) {
4333 		struct rsn_pmksa_cache_entry *pmksa;
4334 
4335 		pmksa = pmksa_cache_get_current(wpa_s->wpa);
4336 		if (!pmksa || !pmksa->dpp_pfs)
4337 			goto pfs_fail;
4338 
4339 		dpp_pfs_free(wpa_s->dpp_pfs);
4340 		wpa_s->dpp_pfs = dpp_pfs_init(ssid->dpp_netaccesskey,
4341 					      ssid->dpp_netaccesskey_len);
4342 		if (!wpa_s->dpp_pfs) {
4343 			wpa_printf(MSG_DEBUG, "DPP: Could not initialize PFS");
4344 			/* Try to continue without PFS */
4345 			goto pfs_fail;
4346 		}
4347 		if (wpabuf_len(wpa_s->dpp_pfs->ie) <=
4348 		    max_wpa_ie_len - wpa_ie_len) {
4349 			os_memcpy(wpa_ie + wpa_ie_len,
4350 				  wpabuf_head(wpa_s->dpp_pfs->ie),
4351 				  wpabuf_len(wpa_s->dpp_pfs->ie));
4352 			wpa_ie_len += wpabuf_len(wpa_s->dpp_pfs->ie);
4353 		}
4354 	}
4355 pfs_fail:
4356 #endif /* CONFIG_DPP2 */
4357 
4358 #ifdef CONFIG_IEEE80211R
4359 	/*
4360 	 * Add MDIE under these conditions: the network profile allows FT,
4361 	 * the AP supports FT, and the mobility domain ID matches.
4362 	 */
4363 	if (bss && wpa_key_mgmt_ft(wpa_sm_get_key_mgmt(wpa_s->wpa))) {
4364 		const u8 *mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
4365 
4366 		if (mdie && mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
4367 			size_t len = 0;
4368 			const u8 *md = mdie + 2;
4369 			const u8 *wpa_md = wpa_sm_get_ft_md(wpa_s->wpa);
4370 
4371 			if (os_memcmp(md, wpa_md,
4372 				      MOBILITY_DOMAIN_ID_LEN) == 0) {
4373 				/* Add mobility domain IE */
4374 				len = wpa_ft_add_mdie(
4375 					wpa_s->wpa, wpa_ie + wpa_ie_len,
4376 					max_wpa_ie_len - wpa_ie_len, mdie);
4377 				wpa_ie_len += len;
4378 			}
4379 #ifdef CONFIG_SME
4380 			if (len > 0 && wpa_s->sme.ft_used &&
4381 			    wpa_sm_has_ft_keys(wpa_s->wpa, md)) {
4382 				wpa_dbg(wpa_s, MSG_DEBUG,
4383 					"SME: Trying to use FT over-the-air");
4384 				algs |= WPA_AUTH_ALG_FT;
4385 			}
4386 #endif /* CONFIG_SME */
4387 		}
4388 	}
4389 #endif /* CONFIG_IEEE80211R */
4390 
4391 #ifdef CONFIG_TESTING_OPTIONS
4392 	if (wpa_s->rsnxe_override_assoc &&
4393 	    wpabuf_len(wpa_s->rsnxe_override_assoc) <=
4394 	    max_wpa_ie_len - wpa_ie_len) {
4395 		wpa_printf(MSG_DEBUG, "TESTING: RSNXE AssocReq override");
4396 		os_memcpy(wpa_ie + wpa_ie_len,
4397 			  wpabuf_head(wpa_s->rsnxe_override_assoc),
4398 			  wpabuf_len(wpa_s->rsnxe_override_assoc));
4399 		wpa_ie_len += wpabuf_len(wpa_s->rsnxe_override_assoc);
4400 	} else
4401 #endif /* CONFIG_TESTING_OPTIONS */
4402 	if (wpa_s->rsnxe_len > 0 &&
4403 	    wpa_s->rsnxe_len <= max_wpa_ie_len - wpa_ie_len) {
4404 		os_memcpy(wpa_ie + wpa_ie_len, wpa_s->rsnxe, wpa_s->rsnxe_len);
4405 		wpa_ie_len += wpa_s->rsnxe_len;
4406 	}
4407 
4408 #ifndef CONFIG_NO_ROBUST_AV
4409 #ifdef CONFIG_TESTING_OPTIONS
4410 	if (wpa_s->disable_mscs_support)
4411 		goto mscs_end;
4412 #endif /* CONFIG_TESTING_OPTIONS */
4413 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_MSCS) &&
4414 	    wpa_s->robust_av.valid_config) {
4415 		struct wpabuf *mscs_ie;
4416 		size_t mscs_ie_len, buf_len;
4417 
4418 		buf_len = 3 +	/* MSCS descriptor IE header */
4419 			  1 +	/* Request type */
4420 			  2 +	/* User priority control */
4421 			  4 +	/* Stream timeout */
4422 			  3 +	/* TCLAS Mask IE header */
4423 			  wpa_s->robust_av.frame_classifier_len;
4424 		mscs_ie = wpabuf_alloc(buf_len);
4425 		if (!mscs_ie) {
4426 			wpa_printf(MSG_INFO,
4427 				   "MSCS: Failed to allocate MSCS IE");
4428 			goto mscs_end;
4429 		}
4430 
4431 		wpas_populate_mscs_descriptor_ie(&wpa_s->robust_av, mscs_ie);
4432 		if ((wpa_ie_len + wpabuf_len(mscs_ie)) <= max_wpa_ie_len) {
4433 			wpa_hexdump_buf(MSG_MSGDUMP, "MSCS IE", mscs_ie);
4434 			mscs_ie_len = wpabuf_len(mscs_ie);
4435 			os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(mscs_ie),
4436 				  mscs_ie_len);
4437 			wpa_ie_len += mscs_ie_len;
4438 		}
4439 
4440 		wpabuf_free(mscs_ie);
4441 	}
4442 mscs_end:
4443 #endif /* CONFIG_NO_ROBUST_AV */
4444 
4445 	wpa_ie_len = wpas_populate_wfa_capa(wpa_s, bss, wpa_ie, wpa_ie_len,
4446 					    max_wpa_ie_len);
4447 
4448 	if (ssid->multi_ap_backhaul_sta) {
4449 		size_t multi_ap_ie_len;
4450 		struct multi_ap_params multi_ap = { 0 };
4451 
4452 		multi_ap.capability = MULTI_AP_BACKHAUL_STA;
4453 		multi_ap.profile = ssid->multi_ap_profile;
4454 
4455 		multi_ap_ie_len = add_multi_ap_ie(wpa_ie + wpa_ie_len,
4456 						  max_wpa_ie_len - wpa_ie_len,
4457 						  &multi_ap);
4458 		if (multi_ap_ie_len == 0) {
4459 			wpa_printf(MSG_ERROR,
4460 				   "Multi-AP: Failed to build Multi-AP IE");
4461 			os_free(wpa_ie);
4462 			return NULL;
4463 		}
4464 		wpa_ie_len += multi_ap_ie_len;
4465 	}
4466 
4467 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_OVERRIDE_SUPPORT,
4468 			 wpas_rsn_overriding(wpa_s, ssid));
4469 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_OVERRIDE,
4470 			 RSN_OVERRIDE_NOT_USED);
4471 	if (wpas_rsn_overriding(wpa_s, ssid) &&
4472 	    wpas_ap_supports_rsn_overriding(wpa_s, bss) &&
4473 	    wpa_ie_len + 2 + 4 + 1 <= max_wpa_ie_len) {
4474 		u8 *pos = wpa_ie + wpa_ie_len, *start = pos;
4475 		const u8 *ie;
4476 		enum rsn_selection_variant variant = RSN_SELECTION_RSNE;
4477 
4478 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_OVERRIDE,
4479 				 RSN_OVERRIDE_RSNE);
4480 		ie = wpa_bss_get_rsne(wpa_s, bss, ssid, wpa_s->valid_links);
4481 		if (ie && ie[0] == WLAN_EID_VENDOR_SPECIFIC && ie[1] >= 4) {
4482 			u32 type;
4483 
4484 			type = WPA_GET_BE32(&ie[2]);
4485 			if (type == RSNE_OVERRIDE_IE_VENDOR_TYPE) {
4486 				variant = RSN_SELECTION_RSNE_OVERRIDE;
4487 				wpa_sm_set_param(wpa_s->wpa,
4488 						 WPA_PARAM_RSN_OVERRIDE,
4489 						 RSN_OVERRIDE_RSNE_OVERRIDE);
4490 			} else if (type == RSNE_OVERRIDE_2_IE_VENDOR_TYPE) {
4491 				variant = RSN_SELECTION_RSNE_OVERRIDE_2;
4492 				wpa_sm_set_param(wpa_s->wpa,
4493 						 WPA_PARAM_RSN_OVERRIDE,
4494 						 RSN_OVERRIDE_RSNE_OVERRIDE_2);
4495 			}
4496 		}
4497 
4498 		/* Indicate which RSNE variant was used */
4499 		*pos++ = WLAN_EID_VENDOR_SPECIFIC;
4500 		*pos++ = 4 + 1;
4501 		WPA_PUT_BE32(pos, RSN_SELECTION_IE_VENDOR_TYPE);
4502 		pos += 4;
4503 		*pos++ = variant;
4504 		wpa_hexdump(MSG_MSGDUMP, "RSN Selection", start, pos - start);
4505 		wpa_ie_len += pos - start;
4506 	}
4507 
4508 	params->rsn_overriding = wpas_rsn_overriding(wpa_s, ssid);
4509 	params->wpa_ie = wpa_ie;
4510 	params->wpa_ie_len = wpa_ie_len;
4511 	params->auth_alg = algs;
4512 	if (mask)
4513 		*mask |= WPA_DRV_UPDATE_ASSOC_IES | WPA_DRV_UPDATE_AUTH_TYPE;
4514 
4515 	return wpa_ie;
4516 }
4517 
4518 
4519 #ifdef CONFIG_OWE
wpas_update_owe_connect_params(struct wpa_supplicant * wpa_s)4520 static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s)
4521 {
4522 	struct wpa_driver_associate_params params;
4523 	u8 *wpa_ie;
4524 
4525 	os_memset(&params, 0, sizeof(params));
4526 	wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
4527 					 wpa_s->current_ssid, &params, NULL);
4528 	if (!wpa_ie)
4529 		return;
4530 
4531 	wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
4532 	os_free(wpa_ie);
4533 }
4534 #endif /* CONFIG_OWE */
4535 
4536 
4537 #if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
wpas_update_fils_connect_params(struct wpa_supplicant * wpa_s)4538 static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s)
4539 {
4540 	struct wpa_driver_associate_params params;
4541 	enum wpa_drv_update_connect_params_mask mask = 0;
4542 	u8 *wpa_ie;
4543 
4544 	if (wpa_s->auth_alg != WPA_AUTH_ALG_OPEN)
4545 		return; /* nothing to do */
4546 
4547 	os_memset(&params, 0, sizeof(params));
4548 	wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
4549 					 wpa_s->current_ssid, &params, &mask);
4550 	if (!wpa_ie)
4551 		return;
4552 
4553 	if (params.auth_alg == WPA_AUTH_ALG_FILS) {
4554 		wpa_s->auth_alg = params.auth_alg;
4555 		wpa_drv_update_connect_params(wpa_s, &params, mask);
4556 	}
4557 
4558 	os_free(wpa_ie);
4559 }
4560 #endif /* CONFIG_FILS && IEEE8021X_EAPOL */
4561 
4562 
wpa_ie_get_edmg_oper_chans(const u8 * edmg_ie)4563 static u8 wpa_ie_get_edmg_oper_chans(const u8 *edmg_ie)
4564 {
4565 	if (!edmg_ie || edmg_ie[1] < 6)
4566 		return 0;
4567 	return edmg_ie[EDMG_BSS_OPERATING_CHANNELS_OFFSET];
4568 }
4569 
4570 
wpa_ie_get_edmg_oper_chan_width(const u8 * edmg_ie)4571 static u8 wpa_ie_get_edmg_oper_chan_width(const u8 *edmg_ie)
4572 {
4573 	if (!edmg_ie || edmg_ie[1] < 6)
4574 		return 0;
4575 	return edmg_ie[EDMG_OPERATING_CHANNEL_WIDTH_OFFSET];
4576 }
4577 
4578 
4579 /* Returns the intersection of two EDMG configurations.
4580  * Note: The current implementation is limited to CB2 only (CB1 included),
4581  * i.e., the implementation supports up to 2 contiguous channels.
4582  * For supporting non-contiguous (aggregated) channels and for supporting
4583  * CB3 and above, this function will need to be extended.
4584  */
4585 static struct ieee80211_edmg_config
get_edmg_intersection(struct ieee80211_edmg_config a,struct ieee80211_edmg_config b,u8 primary_channel)4586 get_edmg_intersection(struct ieee80211_edmg_config a,
4587 		      struct ieee80211_edmg_config b,
4588 		      u8 primary_channel)
4589 {
4590 	struct ieee80211_edmg_config result;
4591 	int i, contiguous = 0;
4592 	int max_contiguous = 0;
4593 
4594 	result.channels = b.channels & a.channels;
4595 	if (!result.channels) {
4596 		wpa_printf(MSG_DEBUG,
4597 			   "EDMG not possible: cannot intersect channels 0x%x and 0x%x",
4598 			   a.channels, b.channels);
4599 		goto fail;
4600 	}
4601 
4602 	if (!(result.channels & BIT(primary_channel - 1))) {
4603 		wpa_printf(MSG_DEBUG,
4604 			   "EDMG not possible: the primary channel %d is not one of the intersected channels 0x%x",
4605 			   primary_channel, result.channels);
4606 		goto fail;
4607 	}
4608 
4609 	/* Find max contiguous channels */
4610 	for (i = 0; i < 6; i++) {
4611 		if (result.channels & BIT(i))
4612 			contiguous++;
4613 		else
4614 			contiguous = 0;
4615 
4616 		if (contiguous > max_contiguous)
4617 			max_contiguous = contiguous;
4618 	}
4619 
4620 	/* Assuming AP and STA supports ONLY contiguous channels,
4621 	 * bw configuration can have value between 4-7.
4622 	 */
4623 	if ((b.bw_config < a.bw_config))
4624 		result.bw_config = b.bw_config;
4625 	else
4626 		result.bw_config = a.bw_config;
4627 
4628 	if ((max_contiguous >= 2 && result.bw_config < EDMG_BW_CONFIG_5) ||
4629 	    (max_contiguous >= 1 && result.bw_config < EDMG_BW_CONFIG_4)) {
4630 		wpa_printf(MSG_DEBUG,
4631 			   "EDMG not possible: not enough contiguous channels %d for supporting CB1 or CB2",
4632 			   max_contiguous);
4633 		goto fail;
4634 	}
4635 
4636 	return result;
4637 
4638 fail:
4639 	result.channels = 0;
4640 	result.bw_config = 0;
4641 	return result;
4642 }
4643 
4644 
4645 static struct ieee80211_edmg_config
get_supported_edmg(struct wpa_supplicant * wpa_s,struct hostapd_freq_params * freq,struct ieee80211_edmg_config request_edmg)4646 get_supported_edmg(struct wpa_supplicant *wpa_s,
4647 		   struct hostapd_freq_params *freq,
4648 		   struct ieee80211_edmg_config request_edmg)
4649 {
4650 	enum hostapd_hw_mode hw_mode;
4651 	struct hostapd_hw_modes *mode = NULL;
4652 	u8 primary_channel;
4653 
4654 	if (!wpa_s->hw.modes)
4655 		goto fail;
4656 
4657 	hw_mode = ieee80211_freq_to_chan(freq->freq, &primary_channel);
4658 	if (hw_mode == NUM_HOSTAPD_MODES)
4659 		goto fail;
4660 
4661 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, hw_mode, false);
4662 	if (!mode)
4663 		goto fail;
4664 
4665 	return get_edmg_intersection(mode->edmg, request_edmg, primary_channel);
4666 
4667 fail:
4668 	request_edmg.channels = 0;
4669 	request_edmg.bw_config = 0;
4670 	return request_edmg;
4671 }
4672 
4673 
4674 #ifdef CONFIG_MBO
wpas_update_mbo_connect_params(struct wpa_supplicant * wpa_s)4675 void wpas_update_mbo_connect_params(struct wpa_supplicant *wpa_s)
4676 {
4677 	struct wpa_driver_associate_params params;
4678 	u8 *wpa_ie;
4679 
4680 	/*
4681 	 * Update MBO connect params only in case of change of MBO attributes
4682 	 * when connected, if the AP support MBO.
4683 	 */
4684 
4685 	if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_ssid ||
4686 	    !wpa_s->current_bss ||
4687 	    !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
4688 		return;
4689 
4690 	os_memset(&params, 0, sizeof(params));
4691 	wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
4692 					 wpa_s->current_ssid, &params, NULL);
4693 	if (!wpa_ie)
4694 		return;
4695 
4696 	wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
4697 	os_free(wpa_ie);
4698 }
4699 #endif /* CONFIG_MBO */
4700 
4701 
wpas_start_assoc_cb(struct wpa_radio_work * work,int deinit)4702 static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
4703 {
4704 	struct wpa_connect_work *cwork = work->ctx;
4705 	struct wpa_bss *bss = cwork->bss;
4706 	struct wpa_ssid *ssid = cwork->ssid;
4707 	struct wpa_supplicant *wpa_s = work->wpa_s;
4708 	u8 *wpa_ie;
4709 	const u8 *edmg_ie_oper;
4710 	int use_crypt, ret, bssid_changed;
4711 	unsigned int cipher_pairwise, cipher_group, cipher_group_mgmt;
4712 	struct wpa_driver_associate_params params;
4713 	u8 psk[PMK_LEN];
4714 #if defined(CONFIG_WEP) || defined(IEEE8021X_EAPOL)
4715 	int wep_keys_set = 0;
4716 #endif /* CONFIG_WEP || IEEE8021X_EAPOL */
4717 	int assoc_failed = 0;
4718 	struct wpa_ssid *old_ssid;
4719 	u8 prev_bssid[ETH_ALEN];
4720 #ifdef CONFIG_HT_OVERRIDES
4721 	struct ieee80211_ht_capabilities htcaps;
4722 	struct ieee80211_ht_capabilities htcaps_mask;
4723 #endif /* CONFIG_HT_OVERRIDES */
4724 #ifdef CONFIG_VHT_OVERRIDES
4725        struct ieee80211_vht_capabilities vhtcaps;
4726        struct ieee80211_vht_capabilities vhtcaps_mask;
4727 #endif /* CONFIG_VHT_OVERRIDES */
4728 
4729 	wpa_s->roam_in_progress = false;
4730 #ifdef CONFIG_WNM
4731 	wpa_s->bss_trans_mgmt_in_progress = false;
4732 #endif /* CONFIG_WNM */
4733 	wpa_s->no_suitable_network = 0;
4734 
4735 	if (deinit) {
4736 		if (work->started) {
4737 			wpa_s->connect_work = NULL;
4738 
4739 			/* cancel possible auth. timeout */
4740 			eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
4741 					     NULL);
4742 		}
4743 		wpas_connect_work_free(cwork);
4744 		return;
4745 	}
4746 
4747 	wpa_s->connect_work = work;
4748 
4749 	if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
4750 	    wpas_network_disabled(wpa_s, ssid)) {
4751 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
4752 		wpas_connect_work_done(wpa_s);
4753 		return;
4754 	}
4755 
4756 	/*
4757 	 * Set the current AP's BSSID (for non-MLO connection) or MLD address
4758 	 * (for MLO connection) as the previous BSSID for reassociation requests
4759 	 * handled by SME-in-driver. If wpa_supplicant is in disconnected state,
4760 	 * prev_bssid will be zero as both wpa_s->valid_links and wpa_s->bssid
4761 	 * will be zero.
4762 	 */
4763 	os_memcpy(prev_bssid,
4764 		  wpa_s->valid_links ? wpa_s->ap_mld_addr : wpa_s->bssid,
4765 		  ETH_ALEN);
4766 	os_memset(&params, 0, sizeof(params));
4767 	wpa_s->reassociate = 0;
4768 	wpa_s->eap_expected_failure = 0;
4769 
4770 	/* Starting new association, so clear the possibly used WPA IE from the
4771 	 * previous association. */
4772 	wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
4773 #ifndef CONFIG_NO_WPA
4774 	wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
4775 #endif /* CONFIG_NO_WPA */
4776 	wpa_s->rsnxe_len = 0;
4777 #ifndef CONFIG_NO_ROBUST_AV
4778 	wpa_s->mscs_setup_done = false;
4779 #endif /* CONFIG_NO_ROBUST_AV */
4780 
4781 	wpa_ie = wpas_populate_assoc_ies(wpa_s, bss, ssid, &params, NULL);
4782 	if (!wpa_ie) {
4783 		wpas_connect_work_done(wpa_s);
4784 		return;
4785 	}
4786 
4787 	if (bss &&
4788 	    (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
4789 #ifdef CONFIG_IEEE80211R
4790 		const u8 *ie, *md = NULL;
4791 #endif /* CONFIG_IEEE80211R */
4792 		wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
4793 			" (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
4794 			wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
4795 		bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
4796 		os_memset(wpa_s->bssid, 0, ETH_ALEN);
4797 		os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
4798 		if (bssid_changed)
4799 			wpas_notify_bssid_changed(wpa_s);
4800 #ifdef CONFIG_IEEE80211R
4801 		ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
4802 		if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
4803 			md = ie + 2;
4804 		wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
4805 		if (md) {
4806 			/* Prepare for the next transition */
4807 			wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
4808 		}
4809 #endif /* CONFIG_IEEE80211R */
4810 #ifdef CONFIG_WPS
4811 	} else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
4812 		   wpa_s->conf->ap_scan == 2 &&
4813 		   (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
4814 		/* Use ap_scan==1 style network selection to find the network
4815 		 */
4816 		wpas_connect_work_done(wpa_s);
4817 		wpa_s->scan_req = MANUAL_SCAN_REQ;
4818 		wpa_s->reassociate = 1;
4819 		wpa_supplicant_req_scan(wpa_s, 0, 0);
4820 		os_free(wpa_ie);
4821 		return;
4822 #endif /* CONFIG_WPS */
4823 	} else {
4824 		wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
4825 			wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4826 		if (bss)
4827 			os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
4828 		else
4829 			os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
4830 	}
4831 	if (!wpa_s->pno)
4832 		wpa_supplicant_cancel_sched_scan(wpa_s);
4833 
4834 	wpa_supplicant_cancel_scan(wpa_s);
4835 
4836 	wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
4837 	use_crypt = 1;
4838 	cipher_pairwise = wpa_s->pairwise_cipher;
4839 	cipher_group = wpa_s->group_cipher;
4840 	cipher_group_mgmt = wpa_s->mgmt_group_cipher;
4841 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
4842 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4843 		if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
4844 			use_crypt = 0;
4845 #ifdef CONFIG_WEP
4846 		if (wpa_set_wep_keys(wpa_s, ssid)) {
4847 			use_crypt = 1;
4848 			wep_keys_set = 1;
4849 		}
4850 #endif /* CONFIG_WEP */
4851 	}
4852 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
4853 		use_crypt = 0;
4854 
4855 #ifdef IEEE8021X_EAPOL
4856 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4857 		if ((ssid->eapol_flags &
4858 		     (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
4859 		      EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
4860 		    !wep_keys_set) {
4861 			use_crypt = 0;
4862 		} else {
4863 			/* Assume that dynamic WEP-104 keys will be used and
4864 			 * set cipher suites in order for drivers to expect
4865 			 * encryption. */
4866 			cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
4867 		}
4868 	}
4869 #endif /* IEEE8021X_EAPOL */
4870 
4871 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4872 		/* Set the key before (and later after) association */
4873 		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
4874 	}
4875 
4876 	/* Set current_ssid before changing state to ASSOCIATING, so that the
4877 	 * selected SSID is available to wpas_notify_state_changed(). */
4878 	old_ssid = wpa_s->current_ssid;
4879 	wpa_s->current_ssid = ssid;
4880 
4881 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
4882 	if (bss) {
4883 		params.ssid = bss->ssid;
4884 		params.ssid_len = bss->ssid_len;
4885 		if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set ||
4886 		    wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
4887 			wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
4888 				   MACSTR " freq=%u MHz based on scan results "
4889 				   "(bssid_set=%d wps=%d)",
4890 				   MAC2STR(bss->bssid), bss->freq,
4891 				   ssid->bssid_set,
4892 				   wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
4893 			params.bssid = bss->bssid;
4894 			params.freq.freq = bss->freq;
4895 		}
4896 		params.bssid_hint = bss->bssid;
4897 		params.freq_hint = bss->freq;
4898 		params.pbss = bss_is_pbss(bss);
4899 	} else {
4900 		if (ssid->bssid_hint_set)
4901 			params.bssid_hint = ssid->bssid_hint;
4902 
4903 		params.ssid = ssid->ssid;
4904 		params.ssid_len = ssid->ssid_len;
4905 		params.pbss = (ssid->pbss != 2) ? ssid->pbss : 0;
4906 	}
4907 
4908 	params.bssid_filter = wpa_s->bssid_filter;
4909 	params.bssid_filter_count = wpa_s->bssid_filter_count;
4910 
4911 	if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
4912 	    wpa_s->conf->ap_scan == 2) {
4913 		params.bssid = ssid->bssid;
4914 		params.fixed_bssid = 1;
4915 	}
4916 
4917 	/* Initial frequency for IBSS/mesh */
4918 	if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
4919 	    ssid->frequency > 0 && params.freq.freq == 0)
4920 		ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
4921 
4922 	if (ssid->mode == WPAS_MODE_IBSS) {
4923 		params.fixed_freq = ssid->fixed_freq;
4924 		if (ssid->beacon_int)
4925 			params.beacon_int = ssid->beacon_int;
4926 		else
4927 			params.beacon_int = wpa_s->conf->beacon_int;
4928 	}
4929 
4930 	if (bss && ssid->enable_edmg)
4931 		edmg_ie_oper = wpa_bss_get_ie_ext(bss,
4932 						  WLAN_EID_EXT_EDMG_OPERATION);
4933 	else
4934 		edmg_ie_oper = NULL;
4935 
4936 	if (edmg_ie_oper) {
4937 		params.freq.edmg.channels =
4938 			wpa_ie_get_edmg_oper_chans(edmg_ie_oper);
4939 		params.freq.edmg.bw_config =
4940 			wpa_ie_get_edmg_oper_chan_width(edmg_ie_oper);
4941 		wpa_printf(MSG_DEBUG,
4942 			   "AP supports EDMG channels 0x%x, bw_config %d",
4943 			   params.freq.edmg.channels,
4944 			   params.freq.edmg.bw_config);
4945 
4946 		/* User may ask for specific EDMG channel for EDMG connection
4947 		 * (must be supported by AP)
4948 		 */
4949 		if (ssid->edmg_channel) {
4950 			struct ieee80211_edmg_config configured_edmg;
4951 			enum hostapd_hw_mode hw_mode;
4952 			u8 primary_channel;
4953 
4954 			hw_mode = ieee80211_freq_to_chan(bss->freq,
4955 							 &primary_channel);
4956 			if (hw_mode == NUM_HOSTAPD_MODES)
4957 				goto edmg_fail;
4958 
4959 			hostapd_encode_edmg_chan(ssid->enable_edmg,
4960 						 ssid->edmg_channel,
4961 						 primary_channel,
4962 						 &configured_edmg);
4963 
4964 			if (ieee802_edmg_is_allowed(params.freq.edmg,
4965 						    configured_edmg)) {
4966 				params.freq.edmg = configured_edmg;
4967 				wpa_printf(MSG_DEBUG,
4968 					   "Use EDMG channel %d for connection",
4969 					   ssid->edmg_channel);
4970 			} else {
4971 			edmg_fail:
4972 				params.freq.edmg.channels = 0;
4973 				params.freq.edmg.bw_config = 0;
4974 				wpa_printf(MSG_WARNING,
4975 					   "EDMG channel %d not supported by AP, fallback to DMG",
4976 					   ssid->edmg_channel);
4977 			}
4978 		}
4979 
4980 		if (params.freq.edmg.channels) {
4981 			wpa_printf(MSG_DEBUG,
4982 				   "EDMG before: channels 0x%x, bw_config %d",
4983 				   params.freq.edmg.channels,
4984 				   params.freq.edmg.bw_config);
4985 			params.freq.edmg = get_supported_edmg(wpa_s,
4986 							      &params.freq,
4987 							      params.freq.edmg);
4988 			wpa_printf(MSG_DEBUG,
4989 				   "EDMG after: channels 0x%x, bw_config %d",
4990 				   params.freq.edmg.channels,
4991 				   params.freq.edmg.bw_config);
4992 		}
4993 	}
4994 
4995 	params.pairwise_suite = cipher_pairwise;
4996 	params.group_suite = cipher_group;
4997 	params.mgmt_group_suite = cipher_group_mgmt;
4998 	params.key_mgmt_suite = wpa_s->key_mgmt;
4999 	params.allowed_key_mgmts = wpa_s->allowed_key_mgmts;
5000 	params.wpa_proto = wpa_s->wpa_proto;
5001 	wpa_s->auth_alg = params.auth_alg;
5002 	params.mode = ssid->mode;
5003 	params.bg_scan_period = ssid->bg_scan_period;
5004 #ifdef CONFIG_WEP
5005 	{
5006 		int i;
5007 
5008 		for (i = 0; i < NUM_WEP_KEYS; i++) {
5009 			if (ssid->wep_key_len[i])
5010 				params.wep_key[i] = ssid->wep_key[i];
5011 			params.wep_key_len[i] = ssid->wep_key_len[i];
5012 		}
5013 		params.wep_tx_keyidx = ssid->wep_tx_keyidx;
5014 	}
5015 #endif /* CONFIG_WEP */
5016 
5017 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
5018 	    (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5019 	     params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
5020 	     (params.allowed_key_mgmts &
5021 	      (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK)))) {
5022 		params.passphrase = ssid->passphrase;
5023 		if (wpa_supplicant_get_psk(wpa_s, bss, ssid, psk) == 0)
5024 			params.psk = psk;
5025 	}
5026 
5027 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
5028 	    (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5029 	     params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5030 	     params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5031 	     params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5032 	     params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384))
5033 		params.req_handshake_offload = 1;
5034 
5035 	if (wpa_s->conf->key_mgmt_offload) {
5036 		if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5037 		    params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5038 		    params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5039 		    params.key_mgmt_suite ==
5040 		    WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
5041 		    params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384)
5042 			params.req_key_mgmt_offload =
5043 				ssid->proactive_key_caching < 0 ?
5044 				wpa_s->conf->okc : ssid->proactive_key_caching;
5045 		else
5046 			params.req_key_mgmt_offload = 1;
5047 
5048 		if ((wpa_key_mgmt_wpa_psk_no_sae(params.key_mgmt_suite) ||
5049 		     wpa_key_mgmt_wpa_psk_no_sae(params.allowed_key_mgmts)) &&
5050 		    wpa_supplicant_get_psk(wpa_s, bss, ssid, psk) == 0)
5051 			params.psk = psk;
5052 	}
5053 
5054 	if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA) &&
5055 	    wpa_key_mgmt_sae(params.key_mgmt_suite)) {
5056 		params.auth_alg = WPA_AUTH_ALG_SAE;
5057 		if (ssid->sae_password) {
5058 			params.sae_password = ssid->sae_password;
5059 			params.sae_password_id = ssid->sae_password_id;
5060 		} else if (ssid->passphrase) {
5061 			params.passphrase = ssid->passphrase;
5062 		}
5063 	}
5064 
5065 	params.drop_unencrypted = use_crypt;
5066 
5067 	params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
5068 	if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
5069 		const u8 *rsn = wpa_bss_get_rsne(wpa_s, bss, ssid, false);
5070 		struct wpa_ie_data ie;
5071 		if (!wpas_driver_bss_selection(wpa_s) && rsn &&
5072 		    wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
5073 		    ie.capabilities &
5074 		    (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
5075 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
5076 				"MFP: require MFP");
5077 			params.mgmt_frame_protection =
5078 				MGMT_FRAME_PROTECTION_REQUIRED;
5079 #ifdef CONFIG_OWE
5080 		} else if (!rsn && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
5081 			   !ssid->owe_only) {
5082 			params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
5083 #endif /* CONFIG_OWE */
5084 		}
5085 	}
5086 
5087 	params.p2p = ssid->p2p_group;
5088 
5089 	if (wpa_s->p2pdev->set_sta_uapsd)
5090 		params.uapsd = wpa_s->p2pdev->sta_uapsd;
5091 	else
5092 		params.uapsd = -1;
5093 
5094 #ifdef CONFIG_HT_OVERRIDES
5095 	os_memset(&htcaps, 0, sizeof(htcaps));
5096 	os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
5097 	params.htcaps = (u8 *) &htcaps;
5098 	params.htcaps_mask = (u8 *) &htcaps_mask;
5099 	wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
5100 #endif /* CONFIG_HT_OVERRIDES */
5101 #ifdef CONFIG_VHT_OVERRIDES
5102 	os_memset(&vhtcaps, 0, sizeof(vhtcaps));
5103 	os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
5104 	params.vhtcaps = &vhtcaps;
5105 	params.vhtcaps_mask = &vhtcaps_mask;
5106 	wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
5107 #endif /* CONFIG_VHT_OVERRIDES */
5108 #ifdef CONFIG_HE_OVERRIDES
5109 	wpa_supplicant_apply_he_overrides(wpa_s, ssid, &params);
5110 #endif /* CONFIG_HE_OVERRIDES */
5111 	wpa_supplicant_apply_eht_overrides(wpa_s, ssid, &params);
5112 
5113 #ifdef CONFIG_P2P
5114 	/*
5115 	 * If multi-channel concurrency is not supported, check for any
5116 	 * frequency conflict. In case of any frequency conflict, remove the
5117 	 * least prioritized connection.
5118 	 */
5119 	if (wpa_s->num_multichan_concurrent < 2) {
5120 		int freq, num;
5121 		num = get_shared_radio_freqs(wpa_s, &freq, 1, false);
5122 		if (num > 0 && freq > 0 && freq != params.freq.freq) {
5123 			wpa_printf(MSG_DEBUG,
5124 				   "Assoc conflicting freq found (%d != %d)",
5125 				   freq, params.freq.freq);
5126 			if (wpas_p2p_handle_frequency_conflicts(
5127 				    wpa_s, params.freq.freq, ssid) < 0) {
5128 				wpas_connect_work_done(wpa_s);
5129 				os_free(wpa_ie);
5130 				return;
5131 			}
5132 		}
5133 	}
5134 #endif /* CONFIG_P2P */
5135 
5136 	if (wpa_s->reassoc_same_ess && !is_zero_ether_addr(prev_bssid) &&
5137 	    old_ssid)
5138 		params.prev_bssid = prev_bssid;
5139 
5140 #ifdef CONFIG_SAE
5141 	params.sae_pwe = wpas_get_ssid_sae_pwe(wpa_s, ssid);
5142 #endif /* CONFIG_SAE */
5143 
5144 	ret = wpa_drv_associate(wpa_s, &params);
5145 	forced_memzero(psk, sizeof(psk));
5146 	os_free(wpa_ie);
5147 	if (ret < 0) {
5148 		wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
5149 			"failed");
5150 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_VALID_ERROR_CODES) {
5151 			/*
5152 			 * The driver is known to mean what is saying, so we
5153 			 * can stop right here; the association will not
5154 			 * succeed.
5155 			 */
5156 			wpas_connection_failed(wpa_s, wpa_s->pending_bssid,
5157 					       NULL);
5158 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
5159 			os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
5160 			return;
5161 		}
5162 		/* try to continue anyway; new association will be tried again
5163 		 * after timeout */
5164 		assoc_failed = 1;
5165 	}
5166 
5167 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
5168 		/* Set the key after the association just in case association
5169 		 * cleared the previously configured key. */
5170 		wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
5171 		/* No need to timeout authentication since there is no key
5172 		 * management. */
5173 		wpa_supplicant_cancel_auth_timeout(wpa_s);
5174 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
5175 #ifdef CONFIG_IBSS_RSN
5176 	} else if (ssid->mode == WPAS_MODE_IBSS &&
5177 		   wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
5178 		   wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
5179 		/*
5180 		 * RSN IBSS authentication is per-STA and we can disable the
5181 		 * per-BSSID authentication.
5182 		 */
5183 		wpa_supplicant_cancel_auth_timeout(wpa_s);
5184 #endif /* CONFIG_IBSS_RSN */
5185 	} else {
5186 		/* Timeout for IEEE 802.11 authentication and association */
5187 		int timeout = 60;
5188 
5189 		if (assoc_failed) {
5190 			/* give IBSS a bit more time */
5191 			timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
5192 		} else if (wpa_s->conf->ap_scan == 1) {
5193 			/* give IBSS a bit more time */
5194 			timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
5195 		}
5196 		wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
5197 	}
5198 
5199 #ifdef CONFIG_P2P
5200 	if (ssid->pmk_valid && wpa_s->p2p_pmksa_entry &&
5201 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
5202 		wpa_sm_pmksa_cache_add_entry(wpa_s->wpa,
5203 					     wpa_s->p2p_pmksa_entry);
5204 		wpa_s->p2p_pmksa_entry = NULL;
5205 	}
5206 #endif /* CONFIG_P2P */
5207 
5208 #ifdef CONFIG_WEP
5209 	if (wep_keys_set &&
5210 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
5211 		/* Set static WEP keys again */
5212 		wpa_set_wep_keys(wpa_s, ssid);
5213 	}
5214 #endif /* CONFIG_WEP */
5215 
5216 	if (old_ssid && old_ssid != ssid) {
5217 		/*
5218 		 * Do not allow EAP session resumption between different
5219 		 * network configurations.
5220 		 */
5221 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
5222 	}
5223 
5224 	if (!wpas_driver_bss_selection(wpa_s) ||
5225 #ifdef CONFIG_P2P
5226 	    wpa_s->p2p_in_invitation ||
5227 #endif /* CONFIG_P2P */
5228 	    ssid->bssid_set) {
5229 		wpa_s->current_bss = bss;
5230 		wpas_configure_frame_filters(wpa_s);
5231 	}
5232 
5233 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
5234 	if (bss)
5235 		wpa_sm_set_ssid(wpa_s->wpa, bss->ssid, bss->ssid_len);
5236 	wpa_supplicant_initiate_eapol(wpa_s);
5237 	if (old_ssid != wpa_s->current_ssid)
5238 		wpas_notify_network_changed(wpa_s);
5239 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
5240 		wpas_notify_auth_changed(wpa_s);
5241 }
5242 
5243 
wpa_supplicant_clear_connection(struct wpa_supplicant * wpa_s,const u8 * addr)5244 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
5245 					    const u8 *addr)
5246 {
5247 	struct wpa_ssid *old_ssid;
5248 
5249 	wpa_s->ml_connect_probe_ssid = NULL;
5250 	wpa_s->ml_connect_probe_bss = NULL;
5251 	wpas_connect_work_done(wpa_s);
5252 	wpa_clear_keys(wpa_s, addr);
5253 	old_ssid = wpa_s->current_ssid;
5254 	wpa_supplicant_mark_disassoc(wpa_s);
5255 	wpa_sm_set_config(wpa_s->wpa, NULL);
5256 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
5257 	if (old_ssid != wpa_s->current_ssid)
5258 		wpas_notify_network_changed(wpa_s);
5259 
5260 #ifndef CONFIG_NO_ROBUST_AV
5261 	wpas_scs_deinit(wpa_s);
5262 	wpas_dscp_deinit(wpa_s);
5263 #endif /* CONFIG_NO_ROBUST_AV */
5264 	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
5265 }
5266 
5267 
5268 /**
5269  * wpa_supplicant_deauthenticate - Deauthenticate the current connection
5270  * @wpa_s: Pointer to wpa_supplicant data
5271  * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
5272  *
5273  * This function is used to request %wpa_supplicant to deauthenticate from the
5274  * current AP.
5275  */
wpa_supplicant_deauthenticate(struct wpa_supplicant * wpa_s,u16 reason_code)5276 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
5277 				   u16 reason_code)
5278 {
5279 	u8 *addr = NULL;
5280 	union wpa_event_data event;
5281 	int zero_addr = 0;
5282 
5283 	wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
5284 		" pending_bssid=" MACSTR
5285 		" reason=%d (%s) state=%s valid_links=0x%x ap_mld_addr=" MACSTR,
5286 		MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
5287 		reason_code, reason2str(reason_code),
5288 		wpa_supplicant_state_txt(wpa_s->wpa_state), wpa_s->valid_links,
5289 		MAC2STR(wpa_s->ap_mld_addr));
5290 
5291 	if (wpa_s->valid_links && !is_zero_ether_addr(wpa_s->ap_mld_addr))
5292 		addr = wpa_s->ap_mld_addr;
5293 	else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
5294 		 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
5295 		  wpa_s->wpa_state == WPA_ASSOCIATING))
5296 		addr = wpa_s->pending_bssid;
5297 	else if (!is_zero_ether_addr(wpa_s->bssid))
5298 		addr = wpa_s->bssid;
5299 	else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
5300 		/*
5301 		 * When using driver-based BSS selection, we may not know the
5302 		 * BSSID with which we are currently trying to associate. We
5303 		 * need to notify the driver of this disconnection even in such
5304 		 * a case, so use the all zeros address here.
5305 		 */
5306 		addr = wpa_s->bssid;
5307 		zero_addr = 1;
5308 	}
5309 
5310 	if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
5311 		wpa_s->enabled_4addr_mode = 0;
5312 
5313 #ifdef CONFIG_TDLS
5314 	wpa_tdls_teardown_peers(wpa_s->wpa);
5315 #endif /* CONFIG_TDLS */
5316 
5317 #ifdef CONFIG_MESH
5318 	if (wpa_s->ifmsh) {
5319 		struct mesh_conf *mconf;
5320 
5321 		mconf = wpa_s->ifmsh->mconf;
5322 		wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
5323 			wpa_s->ifname);
5324 		wpas_notify_mesh_group_removed(wpa_s, mconf->meshid,
5325 					       mconf->meshid_len, reason_code);
5326 		wpa_supplicant_leave_mesh(wpa_s, true);
5327 	}
5328 #endif /* CONFIG_MESH */
5329 
5330 	if (addr) {
5331 		wpa_drv_deauthenticate(wpa_s, addr, reason_code);
5332 		os_memset(&event, 0, sizeof(event));
5333 		event.deauth_info.reason_code = reason_code;
5334 		event.deauth_info.locally_generated = 1;
5335 		wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
5336 		if (zero_addr)
5337 			addr = NULL;
5338 	}
5339 
5340 	wpa_supplicant_clear_connection(wpa_s, addr);
5341 }
5342 
5343 
wpa_supplicant_reconnect(struct wpa_supplicant * wpa_s)5344 void wpa_supplicant_reconnect(struct wpa_supplicant *wpa_s)
5345 {
5346 	wpa_s->own_reconnect_req = 1;
5347 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
5348 
5349 }
5350 
5351 
wpa_supplicant_enable_one_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)5352 static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
5353 					      struct wpa_ssid *ssid)
5354 {
5355 	if (!ssid || !ssid->disabled || ssid->disabled == 2)
5356 		return;
5357 
5358 	ssid->disabled = 0;
5359 	ssid->owe_transition_bss_select_count = 0;
5360 	wpas_clear_temp_disabled(wpa_s, ssid, 1);
5361 	wpas_notify_network_enabled_changed(wpa_s, ssid);
5362 
5363 	/*
5364 	 * Try to reassociate since there is no current configuration and a new
5365 	 * network was made available.
5366 	 */
5367 	if (!wpa_s->current_ssid && !wpa_s->disconnected)
5368 		wpa_s->reassociate = 1;
5369 }
5370 
5371 
5372 /**
5373  * wpa_supplicant_add_network - Add a new network
5374  * @wpa_s: wpa_supplicant structure for a network interface
5375  * Returns: The new network configuration or %NULL if operation failed
5376  *
5377  * This function performs the following operations:
5378  * 1. Adds a new network.
5379  * 2. Send network addition notification.
5380  * 3. Marks the network disabled.
5381  * 4. Set network default parameters.
5382  */
wpa_supplicant_add_network(struct wpa_supplicant * wpa_s)5383 struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
5384 {
5385 	struct wpa_ssid *ssid;
5386 
5387 	ssid = wpa_config_add_network(wpa_s->conf);
5388 	if (!ssid)
5389 		return NULL;
5390 	wpas_notify_network_added(wpa_s, ssid);
5391 	ssid->disabled = 1;
5392 	wpa_config_set_network_defaults(ssid);
5393 
5394 	return ssid;
5395 }
5396 
5397 
5398 /**
5399  * wpa_supplicant_remove_network - Remove a configured network based on id
5400  * @wpa_s: wpa_supplicant structure for a network interface
5401  * @id: Unique network id to search for
5402  * Returns: 0 on success, or -1 if the network was not found, -2 if the network
5403  * could not be removed
5404  *
5405  * This function performs the following operations:
5406  * 1. Removes the network.
5407  * 2. Send network removal notification.
5408  * 3. Update internal state machines.
5409  * 4. Stop any running sched scans.
5410  */
wpa_supplicant_remove_network(struct wpa_supplicant * wpa_s,int id)5411 int wpa_supplicant_remove_network(struct wpa_supplicant *wpa_s, int id)
5412 {
5413 	struct wpa_ssid *ssid, *prev = wpa_s->current_ssid;
5414 	int was_disabled;
5415 
5416 	ssid = wpa_config_get_network(wpa_s->conf, id);
5417 	if (!ssid)
5418 		return -1;
5419 	wpas_notify_network_removed(wpa_s, ssid);
5420 	radio_remove_pending_connect(wpa_s, ssid);
5421 
5422 	if (ssid == prev || !prev) {
5423 #ifdef CONFIG_SME
5424 		wpa_s->sme.prev_bssid_set = 0;
5425 #endif /* CONFIG_SME */
5426 		/*
5427 		 * Invalidate the EAP session cache if the current or
5428 		 * previously used network is removed.
5429 		 */
5430 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
5431 	}
5432 
5433 	if (ssid == prev) {
5434 		wpa_sm_set_config(wpa_s->wpa, NULL);
5435 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
5436 
5437 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5438 			wpa_s->own_disconnect_req = 1;
5439 		wpa_supplicant_deauthenticate(wpa_s,
5440 					      WLAN_REASON_DEAUTH_LEAVING);
5441 	}
5442 
5443 	was_disabled = ssid->disabled;
5444 
5445 	if (wpa_config_remove_network(wpa_s->conf, id) < 0)
5446 		return -2;
5447 
5448 	if (!was_disabled && wpa_s->sched_scanning) {
5449 		wpa_printf(MSG_DEBUG,
5450 			   "Stop ongoing sched_scan to remove network from filters");
5451 		wpa_supplicant_cancel_sched_scan(wpa_s);
5452 		wpa_supplicant_req_scan(wpa_s, 0, 0);
5453 	}
5454 
5455 	return 0;
5456 }
5457 
5458 
5459 /**
5460  * wpa_supplicant_remove_all_networks - Remove all configured networks
5461  * @wpa_s: wpa_supplicant structure for a network interface
5462  * Returns: 0 on success (errors are currently ignored)
5463  *
5464  * This function performs the following operations:
5465  * 1. Remove all networks.
5466  * 2. Send network removal notifications.
5467  * 3. Update internal state machines.
5468  * 4. Stop any running sched scans.
5469  */
wpa_supplicant_remove_all_networks(struct wpa_supplicant * wpa_s)5470 int wpa_supplicant_remove_all_networks(struct wpa_supplicant *wpa_s)
5471 {
5472 	struct wpa_ssid *ssid;
5473 
5474 	if (wpa_s->drv_flags2 &
5475 	    (WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA |
5476 	     WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA))
5477 		wpa_drv_flush_pmkid(wpa_s);
5478 
5479 	if (wpa_s->sched_scanning)
5480 		wpa_supplicant_cancel_sched_scan(wpa_s);
5481 
5482 	eapol_sm_invalidate_cached_session(wpa_s->eapol);
5483 	if (wpa_s->current_ssid) {
5484 #ifdef CONFIG_SME
5485 		wpa_s->sme.prev_bssid_set = 0;
5486 #endif /* CONFIG_SME */
5487 		wpa_sm_set_config(wpa_s->wpa, NULL);
5488 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
5489 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5490 			wpa_s->own_disconnect_req = 1;
5491 		wpa_supplicant_deauthenticate(
5492 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5493 	}
5494 	ssid = wpa_s->conf->ssid;
5495 	while (ssid) {
5496 		struct wpa_ssid *remove_ssid = ssid;
5497 		int id;
5498 
5499 		id = ssid->id;
5500 		ssid = ssid->next;
5501 		wpas_notify_network_removed(wpa_s, remove_ssid);
5502 		wpa_config_remove_network(wpa_s->conf, id);
5503 	}
5504 	return 0;
5505 }
5506 
5507 
5508 /**
5509  * wpa_supplicant_enable_network - Mark a configured network as enabled
5510  * @wpa_s: wpa_supplicant structure for a network interface
5511  * @ssid: wpa_ssid structure for a configured network or %NULL
5512  *
5513  * Enables the specified network or all networks if no network specified.
5514  */
wpa_supplicant_enable_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)5515 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
5516 				   struct wpa_ssid *ssid)
5517 {
5518 	if (ssid == NULL) {
5519 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
5520 			wpa_supplicant_enable_one_network(wpa_s, ssid);
5521 	} else
5522 		wpa_supplicant_enable_one_network(wpa_s, ssid);
5523 
5524 	if (wpa_s->reassociate && !wpa_s->disconnected &&
5525 	    (!wpa_s->current_ssid ||
5526 	     wpa_s->wpa_state == WPA_DISCONNECTED ||
5527 	     wpa_s->wpa_state == WPA_SCANNING)) {
5528 		if (wpa_s->sched_scanning) {
5529 			wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
5530 				   "new network to scan filters");
5531 			wpa_supplicant_cancel_sched_scan(wpa_s);
5532 		}
5533 
5534 		if (wpa_supplicant_fast_associate(wpa_s) != 1) {
5535 			wpa_s->scan_req = NORMAL_SCAN_REQ;
5536 			wpa_supplicant_req_scan(wpa_s, 0, 0);
5537 		}
5538 	}
5539 }
5540 
5541 
5542 /**
5543  * wpa_supplicant_disable_network - Mark a configured network as disabled
5544  * @wpa_s: wpa_supplicant structure for a network interface
5545  * @ssid: wpa_ssid structure for a configured network or %NULL
5546  *
5547  * Disables the specified network or all networks if no network specified.
5548  */
wpa_supplicant_disable_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)5549 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
5550 				    struct wpa_ssid *ssid)
5551 {
5552 	struct wpa_ssid *other_ssid;
5553 	int was_disabled;
5554 
5555 	if (ssid == NULL) {
5556 		if (wpa_s->sched_scanning)
5557 			wpa_supplicant_cancel_sched_scan(wpa_s);
5558 
5559 		for (other_ssid = wpa_s->conf->ssid; other_ssid;
5560 		     other_ssid = other_ssid->next) {
5561 			was_disabled = other_ssid->disabled;
5562 			if (was_disabled == 2)
5563 				continue; /* do not change persistent P2P group
5564 					   * data */
5565 
5566 			other_ssid->disabled = 1;
5567 
5568 			if (was_disabled != other_ssid->disabled)
5569 				wpas_notify_network_enabled_changed(
5570 					wpa_s, other_ssid);
5571 		}
5572 		if (wpa_s->current_ssid) {
5573 			if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5574 				wpa_s->own_disconnect_req = 1;
5575 			wpa_supplicant_deauthenticate(
5576 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5577 		}
5578 	} else if (ssid->disabled != 2) {
5579 		if (ssid == wpa_s->current_ssid) {
5580 			if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5581 				wpa_s->own_disconnect_req = 1;
5582 			wpa_supplicant_deauthenticate(
5583 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5584 		}
5585 
5586 		was_disabled = ssid->disabled;
5587 
5588 		ssid->disabled = 1;
5589 
5590 		if (was_disabled != ssid->disabled) {
5591 			wpas_notify_network_enabled_changed(wpa_s, ssid);
5592 			if (wpa_s->sched_scanning) {
5593 				wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
5594 					   "to remove network from filters");
5595 				wpa_supplicant_cancel_sched_scan(wpa_s);
5596 				wpa_supplicant_req_scan(wpa_s, 0, 0);
5597 			}
5598 		}
5599 	}
5600 }
5601 
5602 
ssid_in_last_scan(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)5603 static bool ssid_in_last_scan(struct wpa_supplicant *wpa_s,
5604 			      struct wpa_ssid *ssid)
5605 {
5606 	size_t i;
5607 
5608 	/* Check if the previous scan included the selected network */
5609 	if (wpa_s->last_scan_num_ssids <= 1 ||
5610 	    !ssid->ssid || ssid->ssid_len == 0)
5611 		return false;
5612 
5613 	/* Iterate through the previous scan SSIDs */
5614 	for (i = 0; i < wpa_s->last_scan_num_ssids;  i++) {
5615 		if (os_memcmp(wpa_s->last_scan_ssids[i].ssid, ssid->ssid,
5616 			      ssid->ssid_len) == 0)
5617 			return true;
5618 	}
5619 
5620 	return false;
5621 }
5622 
5623 
5624 /**
5625  * Checks whether an SSID was discovered in the last scan.
5626  * @wpa_s: wpa_supplicant structure for a network interface.
5627  * @ssid: wpa_ssid structure for a configured network.
5628  * Returns: true if ssid found, false otherwise.
5629  */
ssid_in_last_scan_res(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)5630 static bool ssid_in_last_scan_res(struct wpa_supplicant *wpa_s,
5631 				  struct wpa_ssid *ssid)
5632 {
5633 	size_t i;
5634 
5635 	if (!wpa_s->last_scan_res || !ssid->ssid || ssid->ssid_len == 0)
5636 		return false;
5637 
5638 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
5639 		if (os_memcmp(wpa_s->last_scan_res[i]->ssid,
5640 			      ssid->ssid, ssid->ssid_len) == 0)
5641 			return true;
5642 	}
5643 
5644 	return false;
5645 }
5646 
5647 
5648 /**
5649  * wpa_supplicant_select_network - Attempt association with a network
5650  * @wpa_s: wpa_supplicant structure for a network interface
5651  * @ssid: wpa_ssid structure for a configured network or %NULL for any network
5652  */
wpa_supplicant_select_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)5653 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
5654 				   struct wpa_ssid *ssid)
5655 {
5656 
5657 	struct wpa_ssid *other_ssid;
5658 	int disconnected = 0;
5659 	bool request_new_scan = false;
5660 
5661 	if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
5662 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5663 			wpa_s->own_disconnect_req = 1;
5664 		wpa_supplicant_deauthenticate(
5665 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5666 		disconnected = 1;
5667 	}
5668 
5669 	if (ssid)
5670 		wpas_clear_temp_disabled(wpa_s, ssid, 1);
5671 
5672 	/*
5673 	 * Mark all other networks disabled or mark all networks enabled if no
5674 	 * network specified.
5675 	 */
5676 	for (other_ssid = wpa_s->conf->ssid; other_ssid;
5677 	     other_ssid = other_ssid->next) {
5678 		int was_disabled = other_ssid->disabled;
5679 		if (was_disabled == 2)
5680 			continue; /* do not change persistent P2P group data */
5681 
5682 		other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
5683 		if (was_disabled && !other_ssid->disabled)
5684 			wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
5685 
5686 		if (was_disabled != other_ssid->disabled)
5687 			wpas_notify_network_enabled_changed(wpa_s, other_ssid);
5688 	}
5689 
5690 	if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
5691 	    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
5692 		/* We are already associated with the selected network */
5693 		wpa_printf(MSG_DEBUG, "Already associated with the "
5694 			   "selected network - do nothing");
5695 		return;
5696 	}
5697 
5698 	if (ssid) {
5699 		wpa_s->current_ssid = ssid;
5700 		eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
5701 		wpa_s->connect_without_scan =
5702 			(ssid->mode == WPAS_MODE_MESH ||
5703 			 ssid->mode == WPAS_MODE_AP) ? ssid : NULL;
5704 
5705 		if (ssid->scan_ssid) {
5706 			if (ssid_in_last_scan(wpa_s, ssid)) {
5707 				wpa_printf(MSG_DEBUG,
5708 					   "Hidden network was scanned for in last scan");
5709 			} else if (ssid_in_last_scan_res(wpa_s, ssid)) {
5710 				wpa_printf(MSG_DEBUG,
5711 					   "Hidden network was found in last scan results");
5712 			} else {
5713 				request_new_scan = true;
5714 				wpa_printf(MSG_DEBUG,
5715 					   "Request a new scan for hidden network");
5716 			}
5717 		}
5718 
5719 		if (!request_new_scan && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
5720 		    !ssid->owe_only) {
5721 			wpa_printf(MSG_DEBUG,
5722 				   "Request a new scan for OWE transition SSID");
5723 			request_new_scan = true;
5724 		}
5725 
5726 		/*
5727 		 * Don't optimize next scan freqs since a new ESS has been
5728 		 * selected.
5729 		 */
5730 		os_free(wpa_s->next_scan_freqs);
5731 		wpa_s->next_scan_freqs = NULL;
5732 	} else {
5733 		wpa_s->connect_without_scan = NULL;
5734 	}
5735 
5736 	wpa_s->disconnected = 0;
5737 	wpa_s->reassociate = 1;
5738 	wpa_s_clear_sae_rejected(wpa_s);
5739 	wpa_s->last_owe_group = 0;
5740 	if (ssid) {
5741 		ssid->owe_transition_bss_select_count = 0;
5742 		wpa_s_setup_sae_pt(wpa_s, ssid, false);
5743 	}
5744 
5745 	if (wpa_s->connect_without_scan || request_new_scan ||
5746 	    wpa_supplicant_fast_associate(wpa_s) != 1) {
5747 		wpa_s->scan_req = NORMAL_SCAN_REQ;
5748 		wpas_scan_reset_sched_scan(wpa_s);
5749 		wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
5750 	}
5751 
5752 	if (ssid)
5753 		wpas_notify_network_selected(wpa_s, ssid);
5754 }
5755 
5756 
5757 /**
5758  * wpas_remove_cred - Remove the specified credential and all the network
5759  * entries created based on the removed credential
5760  * @wpa_s: wpa_supplicant structure for a network interface
5761  * @cred: The credential to remove
5762  * Returns: 0 on success, -1 on failure
5763  */
wpas_remove_cred(struct wpa_supplicant * wpa_s,struct wpa_cred * cred)5764 int wpas_remove_cred(struct wpa_supplicant *wpa_s, struct wpa_cred *cred)
5765 {
5766 	struct wpa_ssid *ssid, *next;
5767 	int id;
5768 
5769 	if (!cred) {
5770 		wpa_printf(MSG_DEBUG, "Could not find cred");
5771 		return -1;
5772 	}
5773 
5774 	id = cred->id;
5775 	if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
5776 		wpa_printf(MSG_DEBUG, "Could not find cred %d", id);
5777 		return -1;
5778 	}
5779 
5780 	wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
5781 
5782 	/* Remove any network entry created based on the removed credential */
5783 	ssid = wpa_s->conf->ssid;
5784 	while (ssid) {
5785 		next = ssid->next;
5786 
5787 		if (ssid->parent_cred == cred) {
5788 			wpa_printf(MSG_DEBUG,
5789 				   "Remove network id %d since it used the removed credential",
5790 				   ssid->id);
5791 			if (wpa_supplicant_remove_network(wpa_s, ssid->id) ==
5792 			    -1) {
5793 				wpa_printf(MSG_DEBUG,
5794 					   "Could not find network id=%d",
5795 					   ssid->id);
5796 			}
5797 		}
5798 
5799 		ssid = next;
5800 	}
5801 
5802 	return 0;
5803 }
5804 
5805 
5806 /**
5807  * wpas_remove_cred - Remove all the Interworking credentials
5808  * @wpa_s: wpa_supplicant structure for a network interface
5809  * Returns: 0 on success, -1 on failure
5810  */
wpas_remove_all_creds(struct wpa_supplicant * wpa_s)5811 int wpas_remove_all_creds(struct wpa_supplicant *wpa_s)
5812 {
5813 	int res, ret = 0;
5814 	struct wpa_cred *cred, *prev;
5815 
5816 	cred = wpa_s->conf->cred;
5817 	while (cred) {
5818 		prev = cred;
5819 		cred = cred->next;
5820 		res = wpas_remove_cred(wpa_s, prev);
5821 		if (res < 0) {
5822 			wpa_printf(MSG_DEBUG,
5823 				   "Removal of all credentials failed - failed to remove credential id=%d",
5824 				   prev->id);
5825 			ret = -1;
5826 		}
5827 	}
5828 
5829 	return ret;
5830 }
5831 
5832 
5833 /**
5834  * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
5835  * @wpa_s: wpa_supplicant structure for a network interface
5836  * @pkcs11_engine_path: PKCS #11 engine path or NULL
5837  * @pkcs11_module_path: PKCS #11 module path or NULL
5838  * Returns: 0 on success; -1 on failure
5839  *
5840  * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
5841  * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
5842  * module path fails the paths will be reset to the default value (NULL).
5843  */
wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant * wpa_s,const char * pkcs11_engine_path,const char * pkcs11_module_path)5844 int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
5845 					   const char *pkcs11_engine_path,
5846 					   const char *pkcs11_module_path)
5847 {
5848 	char *pkcs11_engine_path_copy = NULL;
5849 	char *pkcs11_module_path_copy = NULL;
5850 
5851 	if (pkcs11_engine_path != NULL) {
5852 		pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
5853 		if (pkcs11_engine_path_copy == NULL)
5854 			return -1;
5855 	}
5856 	if (pkcs11_module_path != NULL) {
5857 		pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
5858 		if (pkcs11_module_path_copy == NULL) {
5859 			os_free(pkcs11_engine_path_copy);
5860 			return -1;
5861 		}
5862 	}
5863 
5864 #ifndef CONFIG_PKCS11_ENGINE_PATH
5865 	os_free(wpa_s->conf->pkcs11_engine_path);
5866 	wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
5867 #endif /* CONFIG_PKCS11_ENGINE_PATH */
5868 #ifndef CONFIG_PKCS11_MODULE_PATH
5869 	os_free(wpa_s->conf->pkcs11_module_path);
5870 	wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
5871 #endif /* CONFIG_PKCS11_MODULE_PATH */
5872 
5873 	wpa_sm_set_eapol(wpa_s->wpa, NULL);
5874 	eapol_sm_deinit(wpa_s->eapol);
5875 	wpa_s->eapol = NULL;
5876 	if (wpa_supplicant_init_eapol(wpa_s)) {
5877 		/* Error -> Reset paths to the default value (NULL) once. */
5878 		if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
5879 			wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
5880 							       NULL);
5881 
5882 		return -1;
5883 	}
5884 	wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
5885 
5886 	return 0;
5887 }
5888 
5889 
5890 /**
5891  * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
5892  * @wpa_s: wpa_supplicant structure for a network interface
5893  * @ap_scan: AP scan mode
5894  * Returns: 0 if succeed or -1 if ap_scan has an invalid value
5895  *
5896  */
wpa_supplicant_set_ap_scan(struct wpa_supplicant * wpa_s,int ap_scan)5897 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
5898 {
5899 
5900 	int old_ap_scan;
5901 
5902 	if (ap_scan < 0 || ap_scan > 2)
5903 		return -1;
5904 
5905 	if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
5906 		wpa_printf(MSG_INFO,
5907 			   "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
5908 	}
5909 
5910 #ifdef ANDROID
5911 	if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
5912 	    wpa_s->wpa_state >= WPA_ASSOCIATING &&
5913 	    wpa_s->wpa_state < WPA_COMPLETED) {
5914 		wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
5915 			   "associating", wpa_s->conf->ap_scan, ap_scan);
5916 		return 0;
5917 	}
5918 #endif /* ANDROID */
5919 
5920 	old_ap_scan = wpa_s->conf->ap_scan;
5921 	wpa_s->conf->ap_scan = ap_scan;
5922 
5923 	if (old_ap_scan != wpa_s->conf->ap_scan)
5924 		wpas_notify_ap_scan_changed(wpa_s);
5925 
5926 	return 0;
5927 }
5928 
5929 
5930 /**
5931  * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
5932  * @wpa_s: wpa_supplicant structure for a network interface
5933  * @expire_age: Expiration age in seconds
5934  * Returns: 0 if succeed or -1 if expire_age has an invalid value
5935  *
5936  */
wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant * wpa_s,unsigned int bss_expire_age)5937 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
5938 					  unsigned int bss_expire_age)
5939 {
5940 	if (bss_expire_age < 10) {
5941 		wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
5942 			bss_expire_age);
5943 		return -1;
5944 	}
5945 	wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
5946 		bss_expire_age);
5947 	wpa_s->conf->bss_expiration_age = bss_expire_age;
5948 
5949 	return 0;
5950 }
5951 
5952 
5953 /**
5954  * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
5955  * @wpa_s: wpa_supplicant structure for a network interface
5956  * @expire_count: number of scans after which an unseen BSS is reclaimed
5957  * Returns: 0 if succeed or -1 if expire_count has an invalid value
5958  *
5959  */
wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant * wpa_s,unsigned int bss_expire_count)5960 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
5961 					    unsigned int bss_expire_count)
5962 {
5963 	if (bss_expire_count < 1) {
5964 		wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
5965 			bss_expire_count);
5966 		return -1;
5967 	}
5968 	wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
5969 		bss_expire_count);
5970 	wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
5971 
5972 	return 0;
5973 }
5974 
5975 
5976 /**
5977  * wpa_supplicant_set_scan_interval - Set scan interval
5978  * @wpa_s: wpa_supplicant structure for a network interface
5979  * @scan_interval: scan interval in seconds
5980  * Returns: 0 if succeed or -1 if scan_interval has an invalid value
5981  *
5982  */
wpa_supplicant_set_scan_interval(struct wpa_supplicant * wpa_s,int scan_interval)5983 int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
5984 				     int scan_interval)
5985 {
5986 	if (scan_interval < 0) {
5987 		wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
5988 			scan_interval);
5989 		return -1;
5990 	}
5991 	wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
5992 		scan_interval);
5993 	wpa_supplicant_update_scan_int(wpa_s, scan_interval);
5994 
5995 	return 0;
5996 }
5997 
5998 
5999 /**
6000  * wpa_supplicant_set_debug_params - Set global debug params
6001  * @global: wpa_global structure
6002  * @debug_level: debug level
6003  * @debug_timestamp: determines if show timestamp in debug data
6004  * @debug_show_keys: determines if show keys in debug data
6005  * Returns: 0 if succeed or -1 if debug_level has wrong value
6006  */
wpa_supplicant_set_debug_params(struct wpa_global * global,int debug_level,int debug_timestamp,int debug_show_keys)6007 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
6008 				    int debug_timestamp, int debug_show_keys)
6009 {
6010 
6011 	int old_level, old_timestamp, old_show_keys;
6012 
6013 	/* check for allowed debuglevels */
6014 	if (debug_level != MSG_EXCESSIVE &&
6015 	    debug_level != MSG_MSGDUMP &&
6016 	    debug_level != MSG_DEBUG &&
6017 	    debug_level != MSG_INFO &&
6018 	    debug_level != MSG_WARNING &&
6019 	    debug_level != MSG_ERROR)
6020 		return -1;
6021 
6022 	old_level = wpa_debug_level;
6023 	old_timestamp = wpa_debug_timestamp;
6024 	old_show_keys = wpa_debug_show_keys;
6025 
6026 	wpa_debug_level = debug_level;
6027 	wpa_debug_timestamp = debug_timestamp ? 1 : 0;
6028 	wpa_debug_show_keys = debug_show_keys ? 1 : 0;
6029 
6030 	if (wpa_debug_level != old_level)
6031 		wpas_notify_debug_level_changed(global);
6032 	if (wpa_debug_timestamp != old_timestamp)
6033 		wpas_notify_debug_timestamp_changed(global);
6034 	if (wpa_debug_show_keys != old_show_keys)
6035 		wpas_notify_debug_show_keys_changed(global);
6036 
6037 	return 0;
6038 }
6039 
6040 
6041 #ifdef CONFIG_OWE
owe_trans_ssid_match(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * entry_ssid,size_t entry_ssid_len)6042 static int owe_trans_ssid_match(struct wpa_supplicant *wpa_s, const u8 *bssid,
6043 				const u8 *entry_ssid, size_t entry_ssid_len)
6044 {
6045 	const u8 *owe, *owe_bssid, *owe_ssid;
6046 	size_t owe_ssid_len;
6047 	struct wpa_bss *bss;
6048 
6049 	/* Check network profile SSID aganst the SSID in the
6050 	 * OWE Transition Mode element. */
6051 
6052 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
6053 	if (!bss)
6054 		return 0;
6055 
6056 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
6057 	if (!owe)
6058 		return 0;
6059 
6060 	if (wpas_get_owe_trans_network(owe, &owe_bssid, &owe_ssid,
6061 				       &owe_ssid_len))
6062 		return 0;
6063 
6064 	return entry_ssid_len == owe_ssid_len &&
6065 		os_memcmp(owe_ssid, entry_ssid, owe_ssid_len) == 0;
6066 }
6067 #endif /* CONFIG_OWE */
6068 
6069 
6070 /**
6071  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
6072  * @wpa_s: Pointer to wpa_supplicant data
6073  * Returns: A pointer to the current network structure or %NULL on failure
6074  */
wpa_supplicant_get_ssid(struct wpa_supplicant * wpa_s)6075 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
6076 {
6077 	struct wpa_ssid *entry;
6078 	u8 ssid[SSID_MAX_LEN];
6079 	int res;
6080 	size_t ssid_len;
6081 	u8 bssid[ETH_ALEN];
6082 	int wired;
6083 
6084 	res = wpa_drv_get_ssid(wpa_s, ssid);
6085 	if (res < 0) {
6086 		wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
6087 			"driver");
6088 		return NULL;
6089 	}
6090 	ssid_len = res;
6091 
6092 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
6093 		wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
6094 			"driver");
6095 		return NULL;
6096 	}
6097 
6098 	wired = wpa_s->conf->ap_scan == 0 &&
6099 		(wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
6100 
6101 	entry = wpa_s->conf->ssid;
6102 	while (entry) {
6103 		if (!wpas_network_disabled(wpa_s, entry) &&
6104 		    ((ssid_len == entry->ssid_len &&
6105 		      (!entry->ssid ||
6106 		       os_memcmp(ssid, entry->ssid, ssid_len) == 0)) ||
6107 		     wired) &&
6108 		    (wpa_s->valid_links || !entry->bssid_set ||
6109 		     ether_addr_equal(bssid, entry->bssid)))
6110 			return entry;
6111 #ifdef CONFIG_WPS
6112 		if (!wpas_network_disabled(wpa_s, entry) &&
6113 		    (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
6114 		    (entry->ssid == NULL || entry->ssid_len == 0) &&
6115 		    (wpa_s->valid_links || !entry->bssid_set ||
6116 		     ether_addr_equal(bssid, entry->bssid)))
6117 			return entry;
6118 #endif /* CONFIG_WPS */
6119 
6120 #ifdef CONFIG_OWE
6121 		if (!wpas_network_disabled(wpa_s, entry) &&
6122 		    (entry->ssid &&
6123 		     owe_trans_ssid_match(wpa_s, bssid, entry->ssid,
6124 					  entry->ssid_len)) &&
6125 		    (wpa_s->valid_links || !entry->bssid_set ||
6126 		     ether_addr_equal(bssid, entry->bssid)))
6127 			return entry;
6128 #endif /* CONFIG_OWE */
6129 
6130 		if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
6131 		    entry->ssid_len == 0 &&
6132 		    ether_addr_equal(bssid, entry->bssid))
6133 			return entry;
6134 
6135 		entry = entry->next;
6136 	}
6137 
6138 	return NULL;
6139 }
6140 
6141 
select_driver(struct wpa_supplicant * wpa_s,int i)6142 static int select_driver(struct wpa_supplicant *wpa_s, int i)
6143 {
6144 	struct wpa_global *global = wpa_s->global;
6145 
6146 	if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
6147 		global->drv_priv[i] = wpa_drivers[i]->global_init(global);
6148 		if (global->drv_priv[i] == NULL) {
6149 			wpa_printf(MSG_ERROR, "Failed to initialize driver "
6150 				   "'%s'", wpa_drivers[i]->name);
6151 			return -1;
6152 		}
6153 	}
6154 
6155 	wpa_s->driver = wpa_drivers[i];
6156 	wpa_s->global_drv_priv = global->drv_priv[i];
6157 
6158 	return 0;
6159 }
6160 
6161 
wpa_supplicant_set_driver(struct wpa_supplicant * wpa_s,const char * name)6162 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
6163 				     const char *name)
6164 {
6165 	int i;
6166 	size_t len;
6167 	const char *pos, *driver = name;
6168 
6169 	if (wpa_s == NULL)
6170 		return -1;
6171 
6172 	if (wpa_drivers[0] == NULL) {
6173 		wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
6174 			"wpa_supplicant");
6175 		return -1;
6176 	}
6177 
6178 	if (name == NULL) {
6179 		/* Default to first successful driver in the list */
6180 		for (i = 0; wpa_drivers[i]; i++) {
6181 			if (select_driver(wpa_s, i) == 0)
6182 				return 0;
6183 		}
6184 		/* Drivers have each reported failure, so no wpa_msg() here. */
6185 		return -1;
6186 	}
6187 
6188 	do {
6189 		pos = os_strchr(driver, ',');
6190 		if (pos)
6191 			len = pos - driver;
6192 		else
6193 			len = os_strlen(driver);
6194 
6195 		for (i = 0; wpa_drivers[i]; i++) {
6196 			if (os_strlen(wpa_drivers[i]->name) == len &&
6197 			    os_strncmp(driver, wpa_drivers[i]->name, len) ==
6198 			    0) {
6199 				/* First driver that succeeds wins */
6200 				if (select_driver(wpa_s, i) == 0)
6201 					return 0;
6202 			}
6203 		}
6204 
6205 		driver = pos + 1;
6206 	} while (pos);
6207 
6208 	wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
6209 	return -1;
6210 }
6211 
6212 
6213 /**
6214  * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
6215  * @ctx: Context pointer (wpa_s); this is the ctx variable registered
6216  *	with struct wpa_driver_ops::init()
6217  * @src_addr: Source address of the EAPOL frame
6218  * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
6219  * @len: Length of the EAPOL data
6220  * @encrypted: Whether the frame was encrypted
6221  *
6222  * This function is called for each received EAPOL frame. Most driver
6223  * interfaces rely on more generic OS mechanism for receiving frames through
6224  * l2_packet, but if such a mechanism is not available, the driver wrapper may
6225  * take care of received EAPOL frames and deliver them to the core supplicant
6226  * code by calling this function.
6227  */
wpa_supplicant_rx_eapol(void * ctx,const u8 * src_addr,const u8 * buf,size_t len,enum frame_encryption encrypted)6228 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
6229 			     const u8 *buf, size_t len,
6230 			     enum frame_encryption encrypted)
6231 {
6232 	struct wpa_supplicant *wpa_s = ctx;
6233 	const u8 *connected_addr = wpa_s->valid_links ?
6234 		wpa_s->ap_mld_addr : wpa_s->bssid;
6235 
6236 	wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " (encrypted=%d)",
6237 		MAC2STR(src_addr), encrypted);
6238 	wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
6239 
6240 	if (wpa_s->own_disconnect_req) {
6241 		wpa_printf(MSG_DEBUG,
6242 			   "Drop received EAPOL frame as we are disconnecting");
6243 		return;
6244 	}
6245 
6246 #ifdef CONFIG_TESTING_OPTIONS
6247 	wpa_msg_ctrl(wpa_s, MSG_INFO, "EAPOL-RX " MACSTR " %zu",
6248 		     MAC2STR(src_addr), len);
6249 	if (wpa_s->ignore_auth_resp) {
6250 		wpa_printf(MSG_INFO, "RX EAPOL - ignore_auth_resp active!");
6251 		return;
6252 	}
6253 #endif /* CONFIG_TESTING_OPTIONS */
6254 
6255 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6256 	    wpa_s->ext_auth_to_same_bss ||
6257 	    (wpa_s->last_eapol_matches_bssid &&
6258 #ifdef CONFIG_AP
6259 	     !wpa_s->ap_iface &&
6260 #endif /* CONFIG_AP */
6261 	     !ether_addr_equal(src_addr, connected_addr))) {
6262 		/*
6263 		 * There is possible race condition between receiving the
6264 		 * association event and the EAPOL frame since they are coming
6265 		 * through different paths from the driver. In order to avoid
6266 		 * issues in trying to process the EAPOL frame before receiving
6267 		 * association information, lets queue it for processing until
6268 		 * the association event is received. This may also be needed in
6269 		 * driver-based roaming case, so also use src_addr != BSSID as a
6270 		 * trigger if we have previously confirmed that the
6271 		 * Authenticator uses BSSID as the src_addr (which is not the
6272 		 * case with wired IEEE 802.1X).
6273 		 */
6274 		wpa_dbg(wpa_s, MSG_DEBUG,
6275 			"Not associated - Delay processing of received EAPOL frame (state=%s connected_addr="
6276 			MACSTR ")",
6277 			wpa_supplicant_state_txt(wpa_s->wpa_state),
6278 			MAC2STR(connected_addr));
6279 	delay_processing:
6280 		wpabuf_free(wpa_s->pending_eapol_rx);
6281 		wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
6282 		if (wpa_s->pending_eapol_rx) {
6283 			os_get_reltime(&wpa_s->pending_eapol_rx_time);
6284 			os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
6285 				  ETH_ALEN);
6286 			wpa_s->pending_eapol_encrypted = encrypted;
6287 		}
6288 		return;
6289 	}
6290 
6291 	wpa_s->last_eapol_matches_bssid =
6292 		ether_addr_equal(src_addr, connected_addr);
6293 
6294 #ifdef CONFIG_AP
6295 	if (wpa_s->ap_iface) {
6296 		wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len,
6297 					   encrypted);
6298 		return;
6299 	}
6300 #endif /* CONFIG_AP */
6301 
6302 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
6303 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
6304 			"no key management is configured");
6305 		return;
6306 	}
6307 
6308 	if (wpa_s->eapol_received == 0 &&
6309 	    (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) ||
6310 	     !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
6311 	     wpa_s->wpa_state != WPA_COMPLETED) &&
6312 	    (wpa_s->current_ssid == NULL ||
6313 	     wpa_s->current_ssid->mode != WPAS_MODE_IBSS)) {
6314 		/* Timeout for completing IEEE 802.1X and WPA authentication */
6315 		int timeout = 10;
6316 
6317 		if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
6318 		    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
6319 		    wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
6320 			/* Use longer timeout for IEEE 802.1X/EAP */
6321 			timeout = 70;
6322 		}
6323 
6324 #ifdef CONFIG_WPS
6325 		if (wpa_s->current_ssid && wpa_s->current_bss &&
6326 		    (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
6327 		    eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
6328 			/*
6329 			 * Use shorter timeout if going through WPS AP iteration
6330 			 * for PIN config method with an AP that does not
6331 			 * advertise Selected Registrar.
6332 			 */
6333 			struct wpabuf *wps_ie;
6334 
6335 			wps_ie = wpa_bss_get_vendor_ie_multi(
6336 				wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
6337 			if (wps_ie &&
6338 			    !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
6339 				timeout = 10;
6340 			wpabuf_free(wps_ie);
6341 		}
6342 #endif /* CONFIG_WPS */
6343 
6344 		wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
6345 	}
6346 	wpa_s->eapol_received++;
6347 
6348 	if (wpa_s->countermeasures) {
6349 		wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
6350 			"EAPOL packet");
6351 		return;
6352 	}
6353 
6354 #ifdef CONFIG_IBSS_RSN
6355 	if (wpa_s->current_ssid &&
6356 	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
6357 		ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len,
6358 				  encrypted);
6359 		return;
6360 	}
6361 #endif /* CONFIG_IBSS_RSN */
6362 
6363 	/* Source address of the incoming EAPOL frame could be compared to the
6364 	 * current BSSID. However, it is possible that a centralized
6365 	 * Authenticator could be using another MAC address than the BSSID of
6366 	 * an AP, so just allow any address to be used for now. The replies are
6367 	 * still sent to the current BSSID (if available), though. */
6368 
6369 	os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
6370 	if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
6371 	    wpa_s->key_mgmt != WPA_KEY_MGMT_OWE &&
6372 	    wpa_s->key_mgmt != WPA_KEY_MGMT_DPP &&
6373 	    eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len,
6374 			      encrypted) > 0)
6375 		return;
6376 	wpa_drv_poll(wpa_s);
6377 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK)) {
6378 		if (wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len,
6379 				    encrypted) == -2 &&
6380 #ifdef CONFIG_AP
6381 		    !wpa_s->ap_iface &&
6382 #endif /* CONFIG_AP */
6383 		    wpa_s->last_eapol_matches_bssid) {
6384 			/* Handle the case where reassociation occurs to the
6385 			 * current connected AP */
6386 			wpa_dbg(wpa_s, MSG_DEBUG,
6387 				"Delay processing of received EAPOL frame for reassociation to the current connected AP (state=%s connected_addr="
6388 				MACSTR ")",
6389 				wpa_supplicant_state_txt(wpa_s->wpa_state),
6390 				MAC2STR(connected_addr));
6391 			goto delay_processing;
6392 		}
6393 	} else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6394 		/*
6395 		 * Set portValid = true here since we are going to skip 4-way
6396 		 * handshake processing which would normally set portValid. We
6397 		 * need this to allow the EAPOL state machines to be completed
6398 		 * without going through EAPOL-Key handshake.
6399 		 */
6400 		eapol_sm_notify_portValid(wpa_s->eapol, true);
6401 	}
6402 }
6403 
6404 
wpa_supplicant_rx_eapol_cb(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)6405 static void wpa_supplicant_rx_eapol_cb(void *ctx, const u8 *src_addr,
6406 				       const u8 *buf, size_t len)
6407 {
6408 	wpa_supplicant_rx_eapol(ctx, src_addr, buf, len,
6409 				FRAME_ENCRYPTION_UNKNOWN);
6410 }
6411 
6412 
wpas_eapol_needs_l2_packet(struct wpa_supplicant * wpa_s)6413 static int wpas_eapol_needs_l2_packet(struct wpa_supplicant *wpa_s)
6414 {
6415 	return !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT) ||
6416 		!(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX);
6417 }
6418 
6419 
wpa_supplicant_update_mac_addr(struct wpa_supplicant * wpa_s)6420 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
6421 {
6422 	u8 prev_mac_addr[ETH_ALEN];
6423 
6424 	os_memcpy(prev_mac_addr, wpa_s->own_addr, ETH_ALEN);
6425 
6426 	if ((!wpa_s->p2p_mgmt ||
6427 	     !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
6428 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) &&
6429 	    !wpa_s->nan_mgmt) {
6430 		l2_packet_deinit(wpa_s->l2);
6431 		wpa_s->l2 = l2_packet_init(wpa_s->ifname,
6432 					   wpa_drv_get_mac_addr(wpa_s),
6433 					   ETH_P_EAPOL,
6434 					   wpas_eapol_needs_l2_packet(wpa_s) ?
6435 					   wpa_supplicant_rx_eapol_cb : NULL,
6436 					   wpa_s, 0);
6437 		if (wpa_s->l2 == NULL)
6438 			return -1;
6439 
6440 		if (l2_packet_set_packet_filter(wpa_s->l2,
6441 						L2_PACKET_FILTER_PKTTYPE))
6442 			wpa_dbg(wpa_s, MSG_DEBUG,
6443 				"Failed to attach pkt_type filter");
6444 
6445 		if (l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
6446 			wpa_msg(wpa_s, MSG_ERROR,
6447 				"Failed to get own L2 address");
6448 			return -1;
6449 		}
6450 	} else {
6451 		const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
6452 		if (addr)
6453 			os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
6454 	}
6455 
6456 	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
6457 	wpas_wps_update_mac_addr(wpa_s);
6458 
6459 #ifdef CONFIG_NAN_USD
6460 	if (wpa_s->nan_de)
6461 		nan_de_update_nmi(wpa_s->nan_de, wpa_s->own_addr);
6462 #endif /* CONFIG_NAN_USD */
6463 
6464 #ifdef CONFIG_FST
6465 	if (wpa_s->fst)
6466 		fst_update_mac_addr(wpa_s->fst, wpa_s->own_addr);
6467 #endif /* CONFIG_FST */
6468 
6469 	if (!ether_addr_equal(prev_mac_addr, wpa_s->own_addr))
6470 		wpas_notify_mac_address_changed(wpa_s);
6471 
6472 	return 0;
6473 }
6474 
6475 
wpa_supplicant_rx_eapol_bridge(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)6476 static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
6477 					   const u8 *buf, size_t len)
6478 {
6479 	struct wpa_supplicant *wpa_s = ctx;
6480 	const struct l2_ethhdr *eth;
6481 
6482 	if (len < sizeof(*eth))
6483 		return;
6484 	eth = (const struct l2_ethhdr *) buf;
6485 
6486 	if (!ether_addr_equal(eth->h_dest, wpa_s->own_addr) &&
6487 	    !(eth->h_dest[0] & 0x01)) {
6488 		wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
6489 			" (bridge - not for this interface - ignore)",
6490 			MAC2STR(src_addr), MAC2STR(eth->h_dest));
6491 		return;
6492 	}
6493 
6494 	wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
6495 		" (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
6496 	wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
6497 				len - sizeof(*eth), FRAME_ENCRYPTION_UNKNOWN);
6498 }
6499 
6500 
wpa_supplicant_update_bridge_ifname(struct wpa_supplicant * wpa_s,const char * bridge_ifname)6501 int wpa_supplicant_update_bridge_ifname(struct wpa_supplicant *wpa_s,
6502 					const char *bridge_ifname)
6503 {
6504 	if (wpa_s->wpa_state > WPA_SCANNING)
6505 		return -EBUSY;
6506 
6507 	if (bridge_ifname &&
6508 	    os_strlen(bridge_ifname) >= sizeof(wpa_s->bridge_ifname))
6509 		return -EINVAL;
6510 
6511 	if (!bridge_ifname)
6512 		bridge_ifname = "";
6513 
6514 	if (os_strcmp(wpa_s->bridge_ifname, bridge_ifname) == 0)
6515 		return 0;
6516 
6517 	if (wpa_s->l2_br) {
6518 		l2_packet_deinit(wpa_s->l2_br);
6519 		wpa_s->l2_br = NULL;
6520 	}
6521 
6522 	os_strlcpy(wpa_s->bridge_ifname, bridge_ifname,
6523 		   sizeof(wpa_s->bridge_ifname));
6524 
6525 	if (wpa_s->bridge_ifname[0]) {
6526 		wpa_dbg(wpa_s, MSG_DEBUG,
6527 			"Receiving packets from bridge interface '%s'",
6528 			wpa_s->bridge_ifname);
6529 		wpa_s->l2_br = l2_packet_init_bridge(
6530 			wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
6531 			ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
6532 		if (!wpa_s->l2_br) {
6533 			wpa_msg(wpa_s, MSG_ERROR,
6534 				"Failed to open l2_packet connection for the bridge interface '%s'",
6535 				wpa_s->bridge_ifname);
6536 			goto fail;
6537 		}
6538 	}
6539 
6540 #ifdef CONFIG_TDLS
6541 	if (!wpa_s->p2p_mgmt && wpa_tdls_init(wpa_s->wpa))
6542 		goto fail;
6543 #endif /* CONFIG_TDLS */
6544 
6545 	return 0;
6546 fail:
6547 	wpa_s->bridge_ifname[0] = 0;
6548 	if (wpa_s->l2_br) {
6549 		l2_packet_deinit(wpa_s->l2_br);
6550 		wpa_s->l2_br = NULL;
6551 	}
6552 #ifdef CONFIG_TDLS
6553 	if (!wpa_s->p2p_mgmt)
6554 		wpa_tdls_init(wpa_s->wpa);
6555 #endif /* CONFIG_TDLS */
6556 	return -EIO;
6557 }
6558 
6559 
6560 /**
6561  * wpa_supplicant_driver_init - Initialize driver interface parameters
6562  * @wpa_s: Pointer to wpa_supplicant data
6563  * Returns: 0 on success, -1 on failure
6564  *
6565  * This function is called to initialize driver interface parameters.
6566  * wpa_drv_init() must have been called before this function to initialize the
6567  * driver interface.
6568  */
wpa_supplicant_driver_init(struct wpa_supplicant * wpa_s)6569 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
6570 {
6571 	static int interface_count = 0;
6572 
6573 	if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
6574 		return -1;
6575 
6576 	wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
6577 		MAC2STR(wpa_s->own_addr));
6578 	os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
6579 	wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
6580 
6581 	if (wpa_s->bridge_ifname[0] && wpas_eapol_needs_l2_packet(wpa_s)) {
6582 		wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
6583 			"interface '%s'", wpa_s->bridge_ifname);
6584 		wpa_s->l2_br = l2_packet_init_bridge(
6585 			wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
6586 			ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
6587 		if (wpa_s->l2_br == NULL) {
6588 			wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
6589 				"connection for the bridge interface '%s'",
6590 				wpa_s->bridge_ifname);
6591 			return -1;
6592 		}
6593 	}
6594 
6595 	if (wpa_s->conf->ap_scan == 2 &&
6596 	    os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
6597 		wpa_printf(MSG_INFO,
6598 			   "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
6599 	}
6600 
6601 	wpa_clear_keys(wpa_s, NULL);
6602 
6603 	/* Make sure that TKIP countermeasures are not left enabled (could
6604 	 * happen if wpa_supplicant is killed during countermeasures. */
6605 	wpa_drv_set_countermeasures(wpa_s, 0);
6606 
6607 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
6608 	wpa_drv_flush_pmkid(wpa_s);
6609 
6610 	wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
6611 	wpa_s->prev_scan_wildcard = 0;
6612 
6613 	if (wpa_supplicant_enabled_networks(wpa_s)) {
6614 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6615 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6616 			interface_count = 0;
6617 		}
6618 #ifndef ANDROID
6619 		if (!wpa_s->p2p_mgmt &&
6620 		    wpa_supplicant_delayed_sched_scan(wpa_s,
6621 						      interface_count % 3,
6622 						      100000))
6623 			wpa_supplicant_req_scan(wpa_s, interface_count % 3,
6624 						100000);
6625 #endif /* ANDROID */
6626 		interface_count++;
6627 	} else
6628 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
6629 
6630 	return 0;
6631 }
6632 
6633 
wpa_supplicant_daemon(const char * pid_file)6634 static int wpa_supplicant_daemon(const char *pid_file)
6635 {
6636 	wpa_printf(MSG_DEBUG, "Daemonize..");
6637 	return os_daemonize(pid_file);
6638 }
6639 
6640 
6641 static struct wpa_supplicant *
wpa_supplicant_alloc(struct wpa_supplicant * parent)6642 wpa_supplicant_alloc(struct wpa_supplicant *parent)
6643 {
6644 	struct wpa_supplicant *wpa_s;
6645 
6646 	wpa_s = os_zalloc(sizeof(*wpa_s));
6647 	if (wpa_s == NULL)
6648 		return NULL;
6649 	wpa_s->scan_req = INITIAL_SCAN_REQ;
6650 	wpa_s->scan_interval = 5;
6651 	wpa_s->new_connection = 1;
6652 	wpa_s->parent = parent ? parent : wpa_s;
6653 	wpa_s->p2pdev = wpa_s->parent;
6654 #ifdef CONFIG_P2P
6655 	if (parent)
6656 		wpa_s->p2p_mode = parent->p2p_mode;
6657 #endif /* CONFIG_P2P */
6658 	wpa_s->sched_scanning = 0;
6659 	wpa_s->setband_mask = WPA_SETBAND_AUTO;
6660 
6661 	dl_list_init(&wpa_s->bss_tmp_disallowed);
6662 	dl_list_init(&wpa_s->fils_hlp_req);
6663 #ifdef CONFIG_TESTING_OPTIONS
6664 	dl_list_init(&wpa_s->drv_signal_override);
6665 	wpa_s->test_assoc_comeback_type = -1;
6666 #endif /* CONFIG_TESTING_OPTIONS */
6667 #ifndef CONFIG_NO_ROBUST_AV
6668 	dl_list_init(&wpa_s->active_scs_ids);
6669 #endif /* CONFIG_NO_ROBUST_AV */
6670 	wpa_s->ml_probe_mld_id = -1;
6671 
6672 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
6673 #ifdef CONFIG_MESH
6674 	dl_list_init(&wpa_s->mesh_external_pmksa_cache);
6675 #endif /* CONFIG_MESH */
6676 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
6677 
6678 	return wpa_s;
6679 }
6680 
6681 
6682 #ifdef CONFIG_HT_OVERRIDES
6683 
wpa_set_htcap_mcs(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,const char * ht_mcs)6684 static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
6685 			     struct ieee80211_ht_capabilities *htcaps,
6686 			     struct ieee80211_ht_capabilities *htcaps_mask,
6687 			     const char *ht_mcs)
6688 {
6689 	/* parse ht_mcs into hex array */
6690 	int i;
6691 	const char *tmp = ht_mcs;
6692 	char *end = NULL;
6693 
6694 	/* If ht_mcs is null, do not set anything */
6695 	if (!ht_mcs)
6696 		return 0;
6697 
6698 	/* This is what we are setting in the kernel */
6699 	os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
6700 
6701 	wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
6702 
6703 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
6704 		long v;
6705 
6706 		errno = 0;
6707 		v = strtol(tmp, &end, 16);
6708 
6709 		if (errno == 0) {
6710 			wpa_msg(wpa_s, MSG_DEBUG,
6711 				"htcap value[%i]: %ld end: %p  tmp: %p",
6712 				i, v, end, tmp);
6713 			if (end == tmp)
6714 				break;
6715 
6716 			htcaps->supported_mcs_set[i] = v;
6717 			tmp = end;
6718 		} else {
6719 			wpa_msg(wpa_s, MSG_ERROR,
6720 				"Failed to parse ht-mcs: %s, error: %s\n",
6721 				ht_mcs, strerror(errno));
6722 			return -1;
6723 		}
6724 	}
6725 
6726 	/*
6727 	 * If we were able to parse any values, then set mask for the MCS set.
6728 	 */
6729 	if (i) {
6730 		os_memset(&htcaps_mask->supported_mcs_set, 0xff,
6731 			  IEEE80211_HT_MCS_MASK_LEN - 1);
6732 		/* skip the 3 reserved bits */
6733 		htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
6734 			0x1f;
6735 	}
6736 
6737 	return 0;
6738 }
6739 
6740 
wpa_disable_max_amsdu(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int disabled)6741 static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
6742 				 struct ieee80211_ht_capabilities *htcaps,
6743 				 struct ieee80211_ht_capabilities *htcaps_mask,
6744 				 int disabled)
6745 {
6746 	le16 msk;
6747 
6748 	if (disabled == -1)
6749 		return 0;
6750 
6751 	wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
6752 
6753 	msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
6754 	htcaps_mask->ht_capabilities_info |= msk;
6755 	if (disabled)
6756 		htcaps->ht_capabilities_info &= msk;
6757 	else
6758 		htcaps->ht_capabilities_info |= msk;
6759 
6760 	return 0;
6761 }
6762 
6763 
wpa_set_ampdu_factor(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int factor)6764 static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
6765 				struct ieee80211_ht_capabilities *htcaps,
6766 				struct ieee80211_ht_capabilities *htcaps_mask,
6767 				int factor)
6768 {
6769 	if (factor == -1)
6770 		return 0;
6771 
6772 	wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
6773 
6774 	if (factor < 0 || factor > 3) {
6775 		wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
6776 			"Must be 0-3 or -1", factor);
6777 		return -EINVAL;
6778 	}
6779 
6780 	htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
6781 	htcaps->a_mpdu_params &= ~0x3;
6782 	htcaps->a_mpdu_params |= factor & 0x3;
6783 
6784 	return 0;
6785 }
6786 
6787 
wpa_set_ampdu_density(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int density)6788 static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
6789 				 struct ieee80211_ht_capabilities *htcaps,
6790 				 struct ieee80211_ht_capabilities *htcaps_mask,
6791 				 int density)
6792 {
6793 	if (density == -1)
6794 		return 0;
6795 
6796 	wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
6797 
6798 	if (density < 0 || density > 7) {
6799 		wpa_msg(wpa_s, MSG_ERROR,
6800 			"ampdu_density: %d out of range. Must be 0-7 or -1.",
6801 			density);
6802 		return -EINVAL;
6803 	}
6804 
6805 	htcaps_mask->a_mpdu_params |= 0x1C;
6806 	htcaps->a_mpdu_params &= ~(0x1C);
6807 	htcaps->a_mpdu_params |= (density << 2) & 0x1C;
6808 
6809 	return 0;
6810 }
6811 
6812 
wpa_set_disable_ht40(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int disabled)6813 static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
6814 				struct ieee80211_ht_capabilities *htcaps,
6815 				struct ieee80211_ht_capabilities *htcaps_mask,
6816 				int disabled)
6817 {
6818 	if (disabled)
6819 		wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
6820 
6821 	set_disable_ht40(htcaps, disabled);
6822 	set_disable_ht40(htcaps_mask, 0);
6823 
6824 	return 0;
6825 }
6826 
6827 
wpa_set_disable_sgi(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int disabled)6828 static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
6829 			       struct ieee80211_ht_capabilities *htcaps,
6830 			       struct ieee80211_ht_capabilities *htcaps_mask,
6831 			       int disabled)
6832 {
6833 	/* Masking these out disables SGI */
6834 	le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
6835 				HT_CAP_INFO_SHORT_GI40MHZ);
6836 
6837 	if (disabled)
6838 		wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
6839 
6840 	if (disabled)
6841 		htcaps->ht_capabilities_info &= ~msk;
6842 	else
6843 		htcaps->ht_capabilities_info |= msk;
6844 
6845 	htcaps_mask->ht_capabilities_info |= msk;
6846 
6847 	return 0;
6848 }
6849 
6850 
wpa_set_disable_ldpc(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int disabled)6851 static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
6852 			       struct ieee80211_ht_capabilities *htcaps,
6853 			       struct ieee80211_ht_capabilities *htcaps_mask,
6854 			       int disabled)
6855 {
6856 	/* Masking these out disables LDPC */
6857 	le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
6858 
6859 	if (disabled)
6860 		wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
6861 
6862 	if (disabled)
6863 		htcaps->ht_capabilities_info &= ~msk;
6864 	else
6865 		htcaps->ht_capabilities_info |= msk;
6866 
6867 	htcaps_mask->ht_capabilities_info |= msk;
6868 
6869 	return 0;
6870 }
6871 
6872 
wpa_set_tx_stbc(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int tx_stbc)6873 static int wpa_set_tx_stbc(struct wpa_supplicant *wpa_s,
6874 			   struct ieee80211_ht_capabilities *htcaps,
6875 			   struct ieee80211_ht_capabilities *htcaps_mask,
6876 			   int tx_stbc)
6877 {
6878 	le16 msk = host_to_le16(HT_CAP_INFO_TX_STBC);
6879 
6880 	if (tx_stbc == -1)
6881 		return 0;
6882 
6883 	wpa_msg(wpa_s, MSG_DEBUG, "set_tx_stbc: %d", tx_stbc);
6884 
6885 	if (tx_stbc < 0 || tx_stbc > 1) {
6886 		wpa_msg(wpa_s, MSG_ERROR,
6887 			"tx_stbc: %d out of range. Must be 0-1 or -1", tx_stbc);
6888 		return -EINVAL;
6889 	}
6890 
6891 	htcaps_mask->ht_capabilities_info |= msk;
6892 	htcaps->ht_capabilities_info &= ~msk;
6893 	htcaps->ht_capabilities_info |= host_to_le16(tx_stbc << 7) & msk;
6894 
6895 	return 0;
6896 }
6897 
6898 
wpa_set_rx_stbc(struct wpa_supplicant * wpa_s,struct ieee80211_ht_capabilities * htcaps,struct ieee80211_ht_capabilities * htcaps_mask,int rx_stbc)6899 static int wpa_set_rx_stbc(struct wpa_supplicant *wpa_s,
6900 			   struct ieee80211_ht_capabilities *htcaps,
6901 			   struct ieee80211_ht_capabilities *htcaps_mask,
6902 			   int rx_stbc)
6903 {
6904 	le16 msk = host_to_le16(HT_CAP_INFO_RX_STBC_MASK);
6905 
6906 	if (rx_stbc == -1)
6907 		return 0;
6908 
6909 	wpa_msg(wpa_s, MSG_DEBUG, "set_rx_stbc: %d", rx_stbc);
6910 
6911 	if (rx_stbc < 0 || rx_stbc > 3) {
6912 		wpa_msg(wpa_s, MSG_ERROR,
6913 			"rx_stbc: %d out of range. Must be 0-3 or -1", rx_stbc);
6914 		return -EINVAL;
6915 	}
6916 
6917 	htcaps_mask->ht_capabilities_info |= msk;
6918 	htcaps->ht_capabilities_info &= ~msk;
6919 	htcaps->ht_capabilities_info |= host_to_le16(rx_stbc << 8) & msk;
6920 
6921 	return 0;
6922 }
6923 
6924 
wpa_supplicant_apply_ht_overrides(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params)6925 void wpa_supplicant_apply_ht_overrides(
6926 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6927 	struct wpa_driver_associate_params *params)
6928 {
6929 	struct ieee80211_ht_capabilities *htcaps;
6930 	struct ieee80211_ht_capabilities *htcaps_mask;
6931 
6932 	if (!ssid)
6933 		return;
6934 
6935 	params->disable_ht = ssid->disable_ht;
6936 	if (!params->htcaps || !params->htcaps_mask)
6937 		return;
6938 
6939 	htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
6940 	htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
6941 	wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
6942 	wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
6943 			      ssid->disable_max_amsdu);
6944 	wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
6945 	wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
6946 	wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
6947 	wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
6948 	wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
6949 	wpa_set_rx_stbc(wpa_s, htcaps, htcaps_mask, ssid->rx_stbc);
6950 	wpa_set_tx_stbc(wpa_s, htcaps, htcaps_mask, ssid->tx_stbc);
6951 
6952 	if (ssid->ht40_intolerant) {
6953 		le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
6954 		htcaps->ht_capabilities_info |= bit;
6955 		htcaps_mask->ht_capabilities_info |= bit;
6956 	}
6957 }
6958 
6959 #endif /* CONFIG_HT_OVERRIDES */
6960 
6961 
6962 #ifdef CONFIG_VHT_OVERRIDES
wpa_supplicant_apply_vht_overrides(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params)6963 void wpa_supplicant_apply_vht_overrides(
6964 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6965 	struct wpa_driver_associate_params *params)
6966 {
6967 	struct ieee80211_vht_capabilities *vhtcaps;
6968 	struct ieee80211_vht_capabilities *vhtcaps_mask;
6969 
6970 	if (!ssid)
6971 		return;
6972 
6973 	params->disable_vht = ssid->disable_vht;
6974 
6975 	vhtcaps = (void *) params->vhtcaps;
6976 	vhtcaps_mask = (void *) params->vhtcaps_mask;
6977 
6978 	if (!vhtcaps || !vhtcaps_mask)
6979 		return;
6980 
6981 	vhtcaps->vht_capabilities_info = host_to_le32(ssid->vht_capa);
6982 	vhtcaps_mask->vht_capabilities_info = host_to_le32(ssid->vht_capa_mask);
6983 
6984 #ifdef CONFIG_HT_OVERRIDES
6985 	if (ssid->disable_sgi) {
6986 		vhtcaps_mask->vht_capabilities_info |=
6987 			host_to_le32(VHT_CAP_SHORT_GI_80 |
6988 				     VHT_CAP_SHORT_GI_160);
6989 		vhtcaps->vht_capabilities_info &=
6990 			host_to_le32(~(VHT_CAP_SHORT_GI_80 |
6991 				       VHT_CAP_SHORT_GI_160));
6992 		wpa_msg(wpa_s, MSG_DEBUG,
6993 			"disable-sgi override specified, vht-caps: 0x%x",
6994 			le_to_host32(vhtcaps->vht_capabilities_info));
6995 	}
6996 
6997 	/* if max ampdu is <= 3, we have to make the HT cap the same */
6998 	if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
6999 		int max_ampdu;
7000 
7001 		max_ampdu = (ssid->vht_capa &
7002 			     VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
7003 			VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
7004 
7005 		max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
7006 		wpa_set_ampdu_factor(wpa_s,
7007 				     (void *) params->htcaps,
7008 				     (void *) params->htcaps_mask,
7009 				     max_ampdu);
7010 	}
7011 #endif /* CONFIG_HT_OVERRIDES */
7012 
7013 #define OVERRIDE_MCS(i)							\
7014 	if (ssid->vht_tx_mcs_nss_ ##i >= 0) {				\
7015 		vhtcaps_mask->vht_supported_mcs_set.tx_map |=		\
7016 			host_to_le16(3 << 2 * (i - 1));			\
7017 		vhtcaps->vht_supported_mcs_set.tx_map |=		\
7018 			host_to_le16(ssid->vht_tx_mcs_nss_ ##i <<	\
7019 				     2 * (i - 1));			\
7020 	}								\
7021 	if (ssid->vht_rx_mcs_nss_ ##i >= 0) {				\
7022 		vhtcaps_mask->vht_supported_mcs_set.rx_map |=		\
7023 			host_to_le16(3 << 2 * (i - 1));			\
7024 		vhtcaps->vht_supported_mcs_set.rx_map |=		\
7025 			host_to_le16(ssid->vht_rx_mcs_nss_ ##i <<	\
7026 				     2 * (i - 1));			\
7027 	}
7028 
7029 	OVERRIDE_MCS(1);
7030 	OVERRIDE_MCS(2);
7031 	OVERRIDE_MCS(3);
7032 	OVERRIDE_MCS(4);
7033 	OVERRIDE_MCS(5);
7034 	OVERRIDE_MCS(6);
7035 	OVERRIDE_MCS(7);
7036 	OVERRIDE_MCS(8);
7037 }
7038 #endif /* CONFIG_VHT_OVERRIDES */
7039 
7040 
7041 #ifdef CONFIG_HE_OVERRIDES
wpa_supplicant_apply_he_overrides(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params)7042 void wpa_supplicant_apply_he_overrides(
7043 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
7044 	struct wpa_driver_associate_params *params)
7045 {
7046 	if (!ssid)
7047 		return;
7048 
7049 	params->disable_he = ssid->disable_he;
7050 }
7051 #endif /* CONFIG_HE_OVERRIDES */
7052 
7053 
wpa_supplicant_apply_eht_overrides(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_driver_associate_params * params)7054 void wpa_supplicant_apply_eht_overrides(
7055 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
7056 	struct wpa_driver_associate_params *params)
7057 {
7058 	if (!ssid)
7059 		return;
7060 
7061 	params->disable_eht = ssid->disable_eht;
7062 }
7063 
7064 
pcsc_reader_init(struct wpa_supplicant * wpa_s)7065 static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
7066 {
7067 #ifdef PCSC_FUNCS
7068 	size_t len;
7069 
7070 	if (!wpa_s->conf->pcsc_reader)
7071 		return 0;
7072 
7073 	wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
7074 	if (!wpa_s->scard)
7075 		return 1;
7076 
7077 	if (wpa_s->conf->pcsc_pin &&
7078 	    scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
7079 		scard_deinit(wpa_s->scard);
7080 		wpa_s->scard = NULL;
7081 		wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
7082 		return -1;
7083 	}
7084 
7085 	len = sizeof(wpa_s->imsi) - 1;
7086 	if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
7087 		scard_deinit(wpa_s->scard);
7088 		wpa_s->scard = NULL;
7089 		wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
7090 		return -1;
7091 	}
7092 	wpa_s->imsi[len] = '\0';
7093 
7094 	wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
7095 
7096 	wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
7097 		   wpa_s->imsi, wpa_s->mnc_len);
7098 
7099 	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
7100 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
7101 #endif /* PCSC_FUNCS */
7102 
7103 	return 0;
7104 }
7105 
7106 
wpas_init_ext_pw(struct wpa_supplicant * wpa_s)7107 int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
7108 {
7109 	char *val, *pos;
7110 
7111 	ext_password_deinit(wpa_s->ext_pw);
7112 	wpa_s->ext_pw = NULL;
7113 	eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
7114 
7115 	if (!wpa_s->conf->ext_password_backend)
7116 		return 0;
7117 
7118 	val = os_strdup(wpa_s->conf->ext_password_backend);
7119 	if (val == NULL)
7120 		return -1;
7121 	pos = os_strchr(val, ':');
7122 	if (pos)
7123 		*pos++ = '\0';
7124 
7125 	wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
7126 
7127 	wpa_s->ext_pw = ext_password_init(val, pos);
7128 	os_free(val);
7129 	if (wpa_s->ext_pw == NULL) {
7130 		wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
7131 		return -1;
7132 	}
7133 	eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
7134 
7135 	return 0;
7136 }
7137 
7138 
7139 #ifdef CONFIG_FST
7140 
wpas_fst_get_bssid_cb(void * ctx)7141 static const u8 * wpas_fst_get_bssid_cb(void *ctx)
7142 {
7143 	struct wpa_supplicant *wpa_s = ctx;
7144 
7145 	return (is_zero_ether_addr(wpa_s->bssid) ||
7146 		wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
7147 }
7148 
7149 
wpas_fst_get_channel_info_cb(void * ctx,enum hostapd_hw_mode * hw_mode,u8 * channel)7150 static void wpas_fst_get_channel_info_cb(void *ctx,
7151 					 enum hostapd_hw_mode *hw_mode,
7152 					 u8 *channel)
7153 {
7154 	struct wpa_supplicant *wpa_s = ctx;
7155 
7156 	if (wpa_s->current_bss) {
7157 		*hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
7158 						  channel);
7159 	} else if (wpa_s->hw.num_modes) {
7160 		*hw_mode = wpa_s->hw.modes[0].mode;
7161 	} else {
7162 		WPA_ASSERT(0);
7163 		*hw_mode = 0;
7164 	}
7165 }
7166 
7167 
wpas_fst_get_hw_modes(void * ctx,struct hostapd_hw_modes ** modes)7168 static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
7169 {
7170 	struct wpa_supplicant *wpa_s = ctx;
7171 
7172 	*modes = wpa_s->hw.modes;
7173 	return wpa_s->hw.num_modes;
7174 }
7175 
7176 
wpas_fst_set_ies_cb(void * ctx,const struct wpabuf * fst_ies)7177 static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
7178 {
7179 	struct wpa_supplicant *wpa_s = ctx;
7180 
7181 	wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
7182 	wpa_s->fst_ies = fst_ies;
7183 }
7184 
7185 
wpas_fst_send_action_cb(void * ctx,const u8 * da,struct wpabuf * data)7186 static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
7187 {
7188 	struct wpa_supplicant *wpa_s = ctx;
7189 
7190 	if (!ether_addr_equal(wpa_s->bssid, da)) {
7191 		wpa_printf(MSG_INFO, "FST:%s:bssid=" MACSTR " != da=" MACSTR,
7192 			   __func__, MAC2STR(wpa_s->bssid), MAC2STR(da));
7193 		return -1;
7194 	}
7195 	return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
7196 				   wpa_s->own_addr, wpa_s->bssid,
7197 				   wpabuf_head(data), wpabuf_len(data),
7198 				   0);
7199 }
7200 
7201 
wpas_fst_get_mb_ie_cb(void * ctx,const u8 * addr)7202 static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
7203 {
7204 	struct wpa_supplicant *wpa_s = ctx;
7205 
7206 	WPA_ASSERT(ether_addr_equal(wpa_s->bssid, addr));
7207 	return wpa_s->received_mb_ies;
7208 }
7209 
7210 
wpas_fst_update_mb_ie_cb(void * ctx,const u8 * addr,const u8 * buf,size_t size)7211 static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
7212 				     const u8 *buf, size_t size)
7213 {
7214 	struct wpa_supplicant *wpa_s = ctx;
7215 	struct mb_ies_info info;
7216 
7217 	WPA_ASSERT(ether_addr_equal(wpa_s->bssid, addr));
7218 
7219 	if (!mb_ies_info_by_ies(&info, buf, size)) {
7220 		wpabuf_free(wpa_s->received_mb_ies);
7221 		wpa_s->received_mb_ies = mb_ies_by_info(&info);
7222 	}
7223 }
7224 
7225 
wpas_fst_get_peer_first(void * ctx,struct fst_get_peer_ctx ** get_ctx,bool mb_only)7226 static const u8 * wpas_fst_get_peer_first(void *ctx,
7227 					  struct fst_get_peer_ctx **get_ctx,
7228 					  bool mb_only)
7229 {
7230 	struct wpa_supplicant *wpa_s = ctx;
7231 
7232 	*get_ctx = NULL;
7233 	if (!is_zero_ether_addr(wpa_s->bssid))
7234 		return (wpa_s->received_mb_ies || !mb_only) ?
7235 			wpa_s->bssid : NULL;
7236 	return NULL;
7237 }
7238 
7239 
wpas_fst_get_peer_next(void * ctx,struct fst_get_peer_ctx ** get_ctx,bool mb_only)7240 static const u8 * wpas_fst_get_peer_next(void *ctx,
7241 					 struct fst_get_peer_ctx **get_ctx,
7242 					 bool mb_only)
7243 {
7244 	return NULL;
7245 }
7246 
fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant * wpa_s,struct fst_wpa_obj * iface_obj)7247 void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
7248 				       struct fst_wpa_obj *iface_obj)
7249 {
7250 	os_memset(iface_obj, 0, sizeof(*iface_obj));
7251 	iface_obj->ctx              = wpa_s;
7252 	iface_obj->get_bssid        = wpas_fst_get_bssid_cb;
7253 	iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
7254 	iface_obj->get_hw_modes     = wpas_fst_get_hw_modes;
7255 	iface_obj->set_ies          = wpas_fst_set_ies_cb;
7256 	iface_obj->send_action      = wpas_fst_send_action_cb;
7257 	iface_obj->get_mb_ie        = wpas_fst_get_mb_ie_cb;
7258 	iface_obj->update_mb_ie     = wpas_fst_update_mb_ie_cb;
7259 	iface_obj->get_peer_first   = wpas_fst_get_peer_first;
7260 	iface_obj->get_peer_next    = wpas_fst_get_peer_next;
7261 }
7262 #endif /* CONFIG_FST */
7263 
wpas_set_wowlan_triggers(struct wpa_supplicant * wpa_s,const struct wpa_driver_capa * capa)7264 static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
7265 				    const struct wpa_driver_capa *capa)
7266 {
7267 	struct wowlan_triggers *triggers;
7268 	int ret = 0;
7269 
7270 	if (!wpa_s->conf->wowlan_triggers)
7271 		return 0;
7272 
7273 	triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
7274 	if (triggers) {
7275 		ret = wpa_drv_wowlan(wpa_s, triggers);
7276 		os_free(triggers);
7277 	}
7278 	return ret;
7279 }
7280 
7281 
wpas_freq_to_band(int freq)7282 enum wpa_radio_work_band wpas_freq_to_band(int freq)
7283 {
7284 	if (freq < 3000)
7285 		return BAND_2_4_GHZ;
7286 	if (freq > 50000)
7287 		return BAND_60_GHZ;
7288 	return BAND_5_GHZ;
7289 }
7290 
7291 
wpas_get_bands(struct wpa_supplicant * wpa_s,const int * freqs)7292 unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s, const int *freqs)
7293 {
7294 	int i;
7295 	unsigned int band = 0;
7296 
7297 	if (freqs) {
7298 		/* freqs are specified for the radio work */
7299 		for (i = 0; freqs[i]; i++)
7300 			band |= wpas_freq_to_band(freqs[i]);
7301 	} else {
7302 		/*
7303 		 * freqs are not specified, implies all
7304 		 * the supported freqs by HW
7305 		 */
7306 		for (i = 0; i < wpa_s->hw.num_modes; i++) {
7307 			if (wpa_s->hw.modes[i].num_channels != 0) {
7308 				if (wpa_s->hw.modes[i].mode ==
7309 				    HOSTAPD_MODE_IEEE80211B ||
7310 				    wpa_s->hw.modes[i].mode ==
7311 				    HOSTAPD_MODE_IEEE80211G)
7312 					band |= BAND_2_4_GHZ;
7313 				else if (wpa_s->hw.modes[i].mode ==
7314 					 HOSTAPD_MODE_IEEE80211A)
7315 					band |= BAND_5_GHZ;
7316 				else if (wpa_s->hw.modes[i].mode ==
7317 					 HOSTAPD_MODE_IEEE80211AD)
7318 					band |= BAND_60_GHZ;
7319 				else if (wpa_s->hw.modes[i].mode ==
7320 					 HOSTAPD_MODE_IEEE80211ANY)
7321 					band = BAND_2_4_GHZ | BAND_5_GHZ |
7322 						BAND_60_GHZ;
7323 			}
7324 		}
7325 	}
7326 
7327 	return band;
7328 }
7329 
7330 
radio_add_interface(struct wpa_supplicant * wpa_s,const char * rn)7331 static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
7332 					      const char *rn)
7333 {
7334 	struct wpa_supplicant *iface = wpa_s->global->ifaces;
7335 	struct wpa_radio *radio;
7336 
7337 	while (rn && iface) {
7338 		radio = iface->radio;
7339 		if (radio && os_strcmp(rn, radio->name) == 0) {
7340 			wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
7341 				   wpa_s->ifname, rn);
7342 			dl_list_add(&radio->ifaces, &wpa_s->radio_list);
7343 			return radio;
7344 		}
7345 
7346 		iface = iface->next;
7347 	}
7348 
7349 	wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
7350 		   wpa_s->ifname, rn ? rn : "N/A");
7351 	radio = os_zalloc(sizeof(*radio));
7352 	if (radio == NULL)
7353 		return NULL;
7354 
7355 	if (rn)
7356 		os_strlcpy(radio->name, rn, sizeof(radio->name));
7357 	dl_list_init(&radio->ifaces);
7358 	dl_list_init(&radio->work);
7359 	dl_list_add(&radio->ifaces, &wpa_s->radio_list);
7360 
7361 	return radio;
7362 }
7363 
7364 
radio_work_free(struct wpa_radio_work * work)7365 static void radio_work_free(struct wpa_radio_work *work)
7366 {
7367 	if (work->wpa_s->scan_work == work) {
7368 		/* This should not really happen. */
7369 		wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
7370 			work->type, work, work->started);
7371 		work->wpa_s->scan_work = NULL;
7372 	}
7373 
7374 #ifdef CONFIG_P2P
7375 	if (work->wpa_s->p2p_scan_work == work) {
7376 		/* This should not really happen. */
7377 		wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
7378 			work->type, work, work->started);
7379 		work->wpa_s->p2p_scan_work = NULL;
7380 	}
7381 #endif /* CONFIG_P2P */
7382 
7383 	if (work->started) {
7384 		work->wpa_s->radio->num_active_works--;
7385 		wpa_dbg(work->wpa_s, MSG_DEBUG,
7386 			"radio_work_free('%s'@%p): num_active_works --> %u",
7387 			work->type, work,
7388 			work->wpa_s->radio->num_active_works);
7389 	}
7390 
7391 	os_free(work);
7392 }
7393 
7394 
radio_work_is_connect(struct wpa_radio_work * work)7395 static int radio_work_is_connect(struct wpa_radio_work *work)
7396 {
7397 	return os_strcmp(work->type, "sme-connect") == 0 ||
7398 		os_strcmp(work->type, "connect") == 0;
7399 }
7400 
7401 
radio_work_is_scan(struct wpa_radio_work * work)7402 static int radio_work_is_scan(struct wpa_radio_work *work)
7403 {
7404 	return os_strcmp(work->type, "scan") == 0 ||
7405 		os_strcmp(work->type, "p2p-scan") == 0;
7406 }
7407 
7408 
radio_work_get_next_work(struct wpa_radio * radio)7409 static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
7410 {
7411 	struct wpa_radio_work *active_work = NULL;
7412 	struct wpa_radio_work *tmp;
7413 
7414 	/* Get the active work to know the type and band. */
7415 	dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
7416 		if (tmp->started) {
7417 			active_work = tmp;
7418 			break;
7419 		}
7420 	}
7421 
7422 	if (!active_work) {
7423 		/* No active work, start one */
7424 		radio->num_active_works = 0;
7425 		dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
7426 				 list) {
7427 			if (os_strcmp(tmp->type, "scan") == 0 &&
7428 			    external_scan_running(radio) &&
7429 			    (((struct wpa_driver_scan_params *)
7430 			      tmp->ctx)->only_new_results ||
7431 			     tmp->wpa_s->clear_driver_scan_cache))
7432 				continue;
7433 			return tmp;
7434 		}
7435 		return NULL;
7436 	}
7437 
7438 	if (radio_work_is_connect(active_work)) {
7439 		/*
7440 		 * If the active work is either connect or sme-connect,
7441 		 * do not parallelize them with other radio works.
7442 		 */
7443 		wpa_dbg(active_work->wpa_s, MSG_DEBUG,
7444 			"Do not parallelize radio work with %s",
7445 			active_work->type);
7446 		return NULL;
7447 	}
7448 
7449 	dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
7450 		if (tmp->started)
7451 			continue;
7452 
7453 		/*
7454 		 * If connect or sme-connect are enqueued, parallelize only
7455 		 * those operations ahead of them in the queue.
7456 		 */
7457 		if (radio_work_is_connect(tmp))
7458 			break;
7459 
7460 		/* Serialize parallel scan and p2p_scan operations on the same
7461 		 * interface since the driver_nl80211 mechanism for tracking
7462 		 * scan cookies does not yet have support for this. */
7463 		if (active_work->wpa_s == tmp->wpa_s &&
7464 		    radio_work_is_scan(active_work) &&
7465 		    radio_work_is_scan(tmp)) {
7466 			wpa_dbg(active_work->wpa_s, MSG_DEBUG,
7467 				"Do not start work '%s' when another work '%s' is already scheduled",
7468 				tmp->type, active_work->type);
7469 			continue;
7470 		}
7471 		/*
7472 		 * Check that the radio works are distinct and
7473 		 * on different bands.
7474 		 */
7475 		if (os_strcmp(active_work->type, tmp->type) != 0 &&
7476 		    (active_work->bands != tmp->bands)) {
7477 			/*
7478 			 * If a scan has to be scheduled through nl80211 scan
7479 			 * interface and if an external scan is already running,
7480 			 * do not schedule the scan since it is likely to get
7481 			 * rejected by kernel.
7482 			 */
7483 			if (os_strcmp(tmp->type, "scan") == 0 &&
7484 			    external_scan_running(radio) &&
7485 			    (((struct wpa_driver_scan_params *)
7486 			      tmp->ctx)->only_new_results ||
7487 			     tmp->wpa_s->clear_driver_scan_cache))
7488 				continue;
7489 
7490 			wpa_dbg(active_work->wpa_s, MSG_DEBUG,
7491 				"active_work:%s new_work:%s",
7492 				active_work->type, tmp->type);
7493 			return tmp;
7494 		}
7495 	}
7496 
7497 	/* Did not find a radio work to schedule in parallel. */
7498 	return NULL;
7499 }
7500 
7501 
radio_start_next_work(void * eloop_ctx,void * timeout_ctx)7502 static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
7503 {
7504 	struct wpa_radio *radio = eloop_ctx;
7505 	struct wpa_radio_work *work;
7506 	struct os_reltime now, diff;
7507 	struct wpa_supplicant *wpa_s;
7508 
7509 	work = dl_list_first(&radio->work, struct wpa_radio_work, list);
7510 	if (work == NULL) {
7511 		radio->num_active_works = 0;
7512 		return;
7513 	}
7514 
7515 	wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
7516 			      radio_list);
7517 
7518 	if (!(wpa_s &&
7519 	      wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
7520 		if (work->started)
7521 			return; /* already started and still in progress */
7522 
7523 		if (wpa_s && external_scan_running(wpa_s->radio)) {
7524 			wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
7525 			return;
7526 		}
7527 	} else {
7528 		work = NULL;
7529 		if (radio->num_active_works < MAX_ACTIVE_WORKS) {
7530 			/* get the work to schedule next */
7531 			work = radio_work_get_next_work(radio);
7532 		}
7533 		if (!work)
7534 			return;
7535 	}
7536 
7537 	wpa_s = work->wpa_s;
7538 	os_get_reltime(&now);
7539 	os_reltime_sub(&now, &work->time, &diff);
7540 	wpa_dbg(wpa_s, MSG_DEBUG,
7541 		"Starting radio work '%s'@%p after %ld.%06ld second wait",
7542 		work->type, work, diff.sec, diff.usec);
7543 	work->started = 1;
7544 	work->time = now;
7545 	radio->num_active_works++;
7546 
7547 	work->cb(work, 0);
7548 
7549 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
7550 	    radio->num_active_works < MAX_ACTIVE_WORKS)
7551 		radio_work_check_next(wpa_s);
7552 }
7553 
7554 
7555 /*
7556  * This function removes both started and pending radio works running on
7557  * the provided interface's radio.
7558  * Prior to the removal of the radio work, its callback (cb) is called with
7559  * deinit set to be 1. Each work's callback is responsible for clearing its
7560  * internal data and restoring to a correct state.
7561  * @wpa_s: wpa_supplicant data
7562  * @type: type of works to be removed
7563  * @remove_all: 1 to remove all the works on this radio, 0 to remove only
7564  * this interface's works.
7565  */
radio_remove_works(struct wpa_supplicant * wpa_s,const char * type,int remove_all)7566 void radio_remove_works(struct wpa_supplicant *wpa_s,
7567 			const char *type, int remove_all)
7568 {
7569 	struct wpa_radio_work *work, *tmp;
7570 	struct wpa_radio *radio = wpa_s->radio;
7571 
7572 	dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
7573 			      list) {
7574 		if (type && os_strcmp(type, work->type) != 0)
7575 			continue;
7576 
7577 		/* skip other ifaces' works */
7578 		if (!remove_all && work->wpa_s != wpa_s)
7579 			continue;
7580 
7581 		wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
7582 			work->type, work, work->started ? " (started)" : "");
7583 		dl_list_del(&work->list);
7584 		work->cb(work, 1);
7585 		radio_work_free(work);
7586 	}
7587 
7588 	/* in case we removed the started work */
7589 	radio_work_check_next(wpa_s);
7590 }
7591 
7592 
radio_remove_pending_connect(struct wpa_supplicant * wpa_s,const struct wpa_ssid * ssid)7593 static void radio_remove_pending_connect(struct wpa_supplicant *wpa_s,
7594 					 const struct wpa_ssid *ssid)
7595 {
7596 	struct wpa_radio_work *work, *tmp;
7597 	struct wpa_radio *radio = wpa_s->radio;
7598 	struct wpa_connect_work *cwork;
7599 
7600 	dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
7601 			      list) {
7602 		if (!radio_work_is_connect(work))
7603 			continue;
7604 
7605 		cwork = work->ctx;
7606 		if (cwork->ssid != ssid)
7607 			continue;
7608 
7609 		wpa_printf(MSG_DEBUG, "Remove radio work '%s'@%p ssid=%s",
7610 			   work->type, work,
7611 			   wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
7612 		dl_list_del(&work->list);
7613 		work->cb(work, 1);
7614 		radio_work_free(work);
7615 	}
7616 }
7617 
7618 
radio_remove_interface(struct wpa_supplicant * wpa_s)7619 static void radio_remove_interface(struct wpa_supplicant *wpa_s)
7620 {
7621 	struct wpa_radio *radio = wpa_s->radio;
7622 
7623 	if (!radio)
7624 		return;
7625 
7626 	wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
7627 		   wpa_s->ifname, radio->name);
7628 	dl_list_del(&wpa_s->radio_list);
7629 	radio_remove_works(wpa_s, NULL, 0);
7630 	/* If the interface that triggered the external scan was removed, the
7631 	 * external scan is no longer running. */
7632 	if (wpa_s == radio->external_scan_req_interface)
7633 		radio->external_scan_req_interface = NULL;
7634 	wpa_s->radio = NULL;
7635 	if (!dl_list_empty(&radio->ifaces))
7636 		return; /* Interfaces remain for this radio */
7637 
7638 	wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
7639 	eloop_cancel_timeout(radio_start_next_work, radio, NULL);
7640 	os_free(radio);
7641 }
7642 
7643 
radio_work_check_next(struct wpa_supplicant * wpa_s)7644 void radio_work_check_next(struct wpa_supplicant *wpa_s)
7645 {
7646 	struct wpa_radio *radio = wpa_s->radio;
7647 
7648 	if (dl_list_empty(&radio->work))
7649 		return;
7650 	if (wpa_s->ext_work_in_progress) {
7651 		wpa_printf(MSG_DEBUG,
7652 			   "External radio work in progress - delay start of pending item");
7653 		return;
7654 	}
7655 	eloop_cancel_timeout(radio_start_next_work, radio, NULL);
7656 	eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
7657 }
7658 
7659 
7660 /**
7661  * radio_add_work - Add a radio work item
7662  * @wpa_s: Pointer to wpa_supplicant data
7663  * @freq: Frequency of the offchannel operation in MHz or 0
7664  * @type: Unique identifier for each type of work
7665  * @next: Force as the next work to be executed
7666  * @cb: Callback function for indicating when radio is available
7667  * @ctx: Context pointer for the work (work->ctx in cb())
7668  * Returns: Pointer to the newly created work, or %NULL on failure
7669  *
7670  * This function is used to request time for an operation that requires
7671  * exclusive radio control. Once the radio is available, the registered callback
7672  * function will be called. radio_work_done() must be called once the exclusive
7673  * radio operation has been completed, so that the radio is freed for other
7674  * operations. The special case of deinit=1 is used to free the context data
7675  * during interface removal. That does not allow the callback function to start
7676  * the radio operation, i.e., it must free any resources allocated for the radio
7677  * work and return.
7678  *
7679  * The @freq parameter can be used to indicate a single channel on which the
7680  * offchannel operation will occur. This may allow multiple radio work
7681  * operations to be performed in parallel if they apply for the same channel.
7682  * Setting this to 0 indicates that the work item may use multiple channels or
7683  * requires exclusive control of the radio.
7684  */
7685 struct wpa_radio_work *
radio_add_work(struct wpa_supplicant * wpa_s,unsigned int freq,const char * type,int next,void (* cb)(struct wpa_radio_work * work,int deinit),void * ctx)7686 radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
7687 	       const char *type, int next,
7688 	       void (*cb)(struct wpa_radio_work *work, int deinit),
7689 	       void *ctx)
7690 {
7691 	struct wpa_radio *radio = wpa_s->radio;
7692 	struct wpa_radio_work *work;
7693 	int was_empty;
7694 
7695 	work = os_zalloc(sizeof(*work));
7696 	if (work == NULL)
7697 		return NULL;
7698 	wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
7699 	os_get_reltime(&work->time);
7700 	work->freq = freq;
7701 	work->type = type;
7702 	work->wpa_s = wpa_s;
7703 	work->cb = cb;
7704 	work->ctx = ctx;
7705 
7706 	if (freq)
7707 		work->bands = wpas_freq_to_band(freq);
7708 	else if (os_strcmp(type, "scan") == 0 ||
7709 		 os_strcmp(type, "p2p-scan") == 0)
7710 		work->bands = wpas_get_bands(wpa_s,
7711 					     ((struct wpa_driver_scan_params *)
7712 					      ctx)->freqs);
7713 	else
7714 		work->bands = wpas_get_bands(wpa_s, NULL);
7715 
7716 	was_empty = dl_list_empty(&wpa_s->radio->work);
7717 	if (next)
7718 		dl_list_add(&wpa_s->radio->work, &work->list);
7719 	else
7720 		dl_list_add_tail(&wpa_s->radio->work, &work->list);
7721 	if (was_empty) {
7722 		wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
7723 		radio_work_check_next(wpa_s);
7724 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
7725 		   && radio->num_active_works < MAX_ACTIVE_WORKS) {
7726 		wpa_dbg(wpa_s, MSG_DEBUG,
7727 			"Try to schedule a radio work (num_active_works=%u)",
7728 			radio->num_active_works);
7729 		radio_work_check_next(wpa_s);
7730 	}
7731 
7732 	return work;
7733 }
7734 
7735 
7736 /**
7737  * radio_work_done - Indicate that a radio work item has been completed
7738  * @work: Completed work
7739  *
7740  * This function is called once the callback function registered with
7741  * radio_add_work() has completed its work.
7742  */
radio_work_done(struct wpa_radio_work * work)7743 void radio_work_done(struct wpa_radio_work *work)
7744 {
7745 	struct wpa_supplicant *wpa_s = work->wpa_s;
7746 	struct os_reltime now, diff;
7747 	unsigned int started = work->started;
7748 
7749 	/* If next is poisoned, then we are free'ing it already */
7750 	if (work->list.next == NULL)
7751 		return;
7752 
7753 	os_get_reltime(&now);
7754 	os_reltime_sub(&now, &work->time, &diff);
7755 	wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
7756 		work->type, work, started ? "done" : "canceled",
7757 		diff.sec, diff.usec);
7758 	dl_list_del(&work->list);
7759 	radio_work_free(work);
7760 	if (started)
7761 		radio_work_check_next(wpa_s);
7762 }
7763 
7764 
7765 struct wpa_radio_work *
radio_work_pending(struct wpa_supplicant * wpa_s,const char * type)7766 radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
7767 {
7768 	struct wpa_radio_work *work;
7769 	struct wpa_radio *radio = wpa_s->radio;
7770 
7771 	if (!radio)
7772 		return NULL;
7773 
7774 	dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
7775 		if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
7776 			return work;
7777 	}
7778 
7779 	return NULL;
7780 }
7781 
7782 
wpas_init_driver(struct wpa_supplicant * wpa_s,const struct wpa_interface * iface)7783 static int wpas_init_driver(struct wpa_supplicant *wpa_s,
7784 			    const struct wpa_interface *iface)
7785 {
7786 	const char *ifname, *driver, *rn;
7787 
7788 	driver = iface->driver;
7789 next_driver:
7790 	if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
7791 		return -1;
7792 
7793 	wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
7794 	if (wpa_s->drv_priv == NULL) {
7795 		const char *pos;
7796 		int level = MSG_ERROR;
7797 
7798 		pos = driver ? os_strchr(driver, ',') : NULL;
7799 		if (pos) {
7800 			wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
7801 				"driver interface - try next driver wrapper");
7802 			driver = pos + 1;
7803 			goto next_driver;
7804 		}
7805 
7806 #ifdef CONFIG_MATCH_IFACE
7807 		if (wpa_s->matched == WPA_IFACE_MATCHED_NULL)
7808 			level = MSG_DEBUG;
7809 #endif /* CONFIG_MATCH_IFACE */
7810 		wpa_msg(wpa_s, level, "Failed to initialize driver interface");
7811 		return -1;
7812 	}
7813 	if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
7814 		wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
7815 			"driver_param '%s'", wpa_s->conf->driver_param);
7816 		return -1;
7817 	}
7818 
7819 	ifname = wpa_drv_get_ifname(wpa_s);
7820 	if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
7821 		wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
7822 			"interface name with '%s'", ifname);
7823 		os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
7824 	}
7825 
7826 	rn = wpa_driver_get_radio_name(wpa_s);
7827 	if (rn && rn[0] == '\0')
7828 		rn = NULL;
7829 
7830 	wpa_s->radio = radio_add_interface(wpa_s, rn);
7831 	if (wpa_s->radio == NULL)
7832 		return -1;
7833 
7834 	return 0;
7835 }
7836 
7837 
7838 #ifdef CONFIG_GAS_SERVER
7839 
wpas_gas_server_tx_status(struct wpa_supplicant * wpa_s,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,enum offchannel_send_action_result result)7840 static void wpas_gas_server_tx_status(struct wpa_supplicant *wpa_s,
7841 				      unsigned int freq, const u8 *dst,
7842 				      const u8 *src, const u8 *bssid,
7843 				      const u8 *data, size_t data_len,
7844 				      enum offchannel_send_action_result result)
7845 {
7846 	wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
7847 		   " result=%s",
7848 		   freq, MAC2STR(dst),
7849 		   result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
7850 		   (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
7851 		    "FAILED"));
7852 	gas_server_tx_status(wpa_s->gas_server, dst, data, data_len,
7853 			     result == OFFCHANNEL_SEND_ACTION_SUCCESS);
7854 }
7855 
7856 
wpas_gas_server_tx(void * ctx,int freq,const u8 * da,struct wpabuf * buf,unsigned int wait_time)7857 static void wpas_gas_server_tx(void *ctx, int freq, const u8 *da,
7858 			       struct wpabuf *buf, unsigned int wait_time)
7859 {
7860 	struct wpa_supplicant *wpa_s = ctx;
7861 	const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7862 
7863 	if (wait_time > wpa_s->max_remain_on_chan)
7864 		wait_time = wpa_s->max_remain_on_chan;
7865 
7866 	offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, broadcast,
7867 			       wpabuf_head(buf), wpabuf_len(buf),
7868 			       wait_time, wpas_gas_server_tx_status, 0);
7869 }
7870 
7871 #endif /* CONFIG_GAS_SERVER */
7872 
wpa_supplicant_init_iface(struct wpa_supplicant * wpa_s,const struct wpa_interface * iface)7873 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
7874 				     const struct wpa_interface *iface)
7875 {
7876 	struct wpa_driver_capa capa;
7877 	int capa_res;
7878 	u8 dfs_domain;
7879 
7880 	wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
7881 		   "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
7882 		   iface->confname ? iface->confname : "N/A",
7883 		   iface->driver ? iface->driver : "default",
7884 		   iface->ctrl_interface ? iface->ctrl_interface : "N/A",
7885 		   iface->bridge_ifname ? iface->bridge_ifname : "N/A");
7886 
7887 	if (iface->confname) {
7888 #ifdef CONFIG_BACKEND_FILE
7889 		wpa_s->confname = os_rel2abs_path(iface->confname);
7890 		if (wpa_s->confname == NULL) {
7891 			wpa_printf(MSG_ERROR, "Failed to get absolute path "
7892 				   "for configuration file '%s'.",
7893 				   iface->confname);
7894 			return -1;
7895 		}
7896 		wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
7897 			   iface->confname, wpa_s->confname);
7898 #else /* CONFIG_BACKEND_FILE */
7899 		wpa_s->confname = os_strdup(iface->confname);
7900 #endif /* CONFIG_BACKEND_FILE */
7901 		wpa_s->conf = wpa_config_read(
7902 			wpa_s->confname, NULL, false,
7903 			wpa_s->global->params.show_details);
7904 		if (wpa_s->conf == NULL) {
7905 			wpa_printf(MSG_ERROR, "Failed to read or parse "
7906 				   "configuration '%s'.", wpa_s->confname);
7907 			return -1;
7908 		}
7909 		wpa_s->confanother = os_rel2abs_path(iface->confanother);
7910 		if (wpa_s->confanother &&
7911 		    !wpa_config_read(wpa_s->confanother, wpa_s->conf, true,
7912 				     wpa_s->global->params.show_details)) {
7913 			wpa_printf(MSG_ERROR,
7914 				   "Failed to read or parse configuration '%s'.",
7915 				   wpa_s->confanother);
7916 			return -1;
7917 		}
7918 
7919 		/*
7920 		 * Override ctrl_interface and driver_param if set on command
7921 		 * line.
7922 		 */
7923 		if (iface->ctrl_interface) {
7924 			os_free(wpa_s->conf->ctrl_interface);
7925 			wpa_s->conf->ctrl_interface =
7926 				os_strdup(iface->ctrl_interface);
7927 			if (!wpa_s->conf->ctrl_interface) {
7928 				wpa_printf(MSG_ERROR,
7929 					   "Failed to duplicate control interface '%s'.",
7930 					   iface->ctrl_interface);
7931 				return -1;
7932 			}
7933 		}
7934 
7935 		if (iface->driver_param) {
7936 			os_free(wpa_s->conf->driver_param);
7937 			wpa_s->conf->driver_param =
7938 				os_strdup(iface->driver_param);
7939 			if (!wpa_s->conf->driver_param) {
7940 				wpa_printf(MSG_ERROR,
7941 					   "Failed to duplicate driver param '%s'.",
7942 					   iface->driver_param);
7943 				return -1;
7944 			}
7945 		}
7946 
7947 		if (iface->p2p_mgmt && !iface->ctrl_interface) {
7948 			os_free(wpa_s->conf->ctrl_interface);
7949 			wpa_s->conf->ctrl_interface = NULL;
7950 		}
7951 	} else
7952 		wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
7953 						     iface->driver_param);
7954 
7955 	if (wpa_s->conf == NULL) {
7956 		wpa_printf(MSG_ERROR, "\nNo configuration found.");
7957 		return -1;
7958 	}
7959 
7960 	if (iface->ifname == NULL) {
7961 		wpa_printf(MSG_ERROR, "\nInterface name is required.");
7962 		return -1;
7963 	}
7964 	if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
7965 		wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
7966 			   iface->ifname);
7967 		return -1;
7968 	}
7969 	os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
7970 #ifdef CONFIG_MATCH_IFACE
7971 	wpa_s->matched = iface->matched;
7972 #endif /* CONFIG_MATCH_IFACE */
7973 
7974 	if (iface->bridge_ifname) {
7975 		if (os_strlen(iface->bridge_ifname) >=
7976 		    sizeof(wpa_s->bridge_ifname)) {
7977 			wpa_printf(MSG_ERROR, "\nToo long bridge interface "
7978 				   "name '%s'.", iface->bridge_ifname);
7979 			return -1;
7980 		}
7981 		os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
7982 			   sizeof(wpa_s->bridge_ifname));
7983 	}
7984 
7985 	/* RSNA Supplicant Key Management - INITIALIZE */
7986 	eapol_sm_notify_portEnabled(wpa_s->eapol, false);
7987 	eapol_sm_notify_portValid(wpa_s->eapol, false);
7988 
7989 	/* Initialize driver interface and register driver event handler before
7990 	 * L2 receive handler so that association events are processed before
7991 	 * EAPOL-Key packets if both become available for the same select()
7992 	 * call. */
7993 	if (wpas_init_driver(wpa_s, iface) < 0)
7994 		return -1;
7995 
7996 	if (wpa_supplicant_init_wpa(wpa_s) < 0)
7997 		return -1;
7998 
7999 	wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
8000 			  wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
8001 			  NULL);
8002 	wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
8003 
8004 	if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
8005 	    wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
8006 			     wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
8007 		wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
8008 			"dot11RSNAConfigPMKLifetime");
8009 		return -1;
8010 	}
8011 
8012 	if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
8013 	    wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
8014 			     wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
8015 		wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
8016 			"dot11RSNAConfigPMKReauthThreshold");
8017 		return -1;
8018 	}
8019 
8020 	if (wpa_s->conf->dot11RSNAConfigSATimeout &&
8021 	    wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
8022 			     wpa_s->conf->dot11RSNAConfigSATimeout)) {
8023 		wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
8024 			"dot11RSNAConfigSATimeout");
8025 		return -1;
8026 	}
8027 
8028 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_FT_PREPEND_PMKID,
8029 			 wpa_s->conf->ft_prepend_pmkid);
8030 
8031 	wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
8032 		wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags, &dfs_domain,
8033 		wpa_s->device_country, sizeof(wpa_s->device_country));
8034 	wpa_s->hw_dfs_domain = dfs_domain;
8035 	if (!wpa_s->device_country_set &&
8036 	    wpa_s->device_country[0] && wpa_s->device_country[1]) {
8037 		wpa_s->device_country_set = true;
8038 		wpa_printf(MSG_DEBUG,
8039 			   "Device country code set to '%s' from hw feature data at init",
8040 			   wpa_s->device_country);
8041 	}
8042 	if (wpa_s->hw.modes) {
8043 		u16 i;
8044 
8045 		for (i = 0; i < wpa_s->hw.num_modes; i++) {
8046 			if (wpa_s->hw.modes[i].eht_capab[IEEE80211_MODE_INFRA].
8047 			    eht_supported)
8048 				wpa_s->hw_capab |= BIT(CAPAB_EHT);
8049 			if (wpa_s->hw.modes[i].he_capab[IEEE80211_MODE_INFRA].
8050 			    he_supported)
8051 				wpa_s->hw_capab |= BIT(CAPAB_HE);
8052 			if (wpa_s->hw.modes[i].vht_capab)
8053 				wpa_s->hw_capab |= BIT(CAPAB_VHT);
8054 			if (wpa_s->hw.modes[i].ht_capab)
8055 				wpa_s->hw_capab |= BIT(CAPAB_HT);
8056 		}
8057 		wpa_s->support_6ghz = wpas_is_6ghz_supported(wpa_s, false);
8058 	}
8059 
8060 	capa_res = wpa_drv_get_capa(wpa_s, &capa);
8061 	if (capa_res == 0) {
8062 		u16 eml_capa, mld_capa;
8063 
8064 		wpa_s->drv_capa_known = 1;
8065 		wpa_s->drv_flags = capa.flags;
8066 		wpa_s->drv_flags2 = capa.flags2;
8067 		wpa_s->drv_enc = capa.enc;
8068 		wpa_s->drv_key_mgmt = capa.key_mgmt;
8069 		wpa_s->drv_rrm_flags = capa.rrm_flags;
8070 		wpa_s->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
8071 		wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
8072 		wpa_s->max_scan_ssids = capa.max_scan_ssids;
8073 		wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
8074 		wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
8075 		wpa_s->max_sched_scan_plan_interval =
8076 			capa.max_sched_scan_plan_interval;
8077 		wpa_s->max_sched_scan_plan_iterations =
8078 			capa.max_sched_scan_plan_iterations;
8079 		wpa_s->sched_scan_supported = capa.sched_scan_supported;
8080 		wpa_s->max_match_sets = capa.max_match_sets;
8081 		wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
8082 		wpa_s->max_stations = capa.max_stations;
8083 		wpa_s->extended_capa = capa.extended_capa;
8084 		wpa_s->extended_capa_mask = capa.extended_capa_mask;
8085 		wpa_s->extended_capa_len = capa.extended_capa_len;
8086 		wpa_s->num_multichan_concurrent =
8087 			capa.num_multichan_concurrent;
8088 #ifndef CONFIG_NO_WMM_AC
8089 		wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
8090 #endif /* CONFIG_NO_WMM_AC */
8091 		wpa_s->max_num_akms = capa.max_num_akms;
8092 
8093 		if (capa.mac_addr_rand_scan_supported)
8094 			wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
8095 		if (wpa_s->sched_scan_supported &&
8096 		    capa.mac_addr_rand_sched_scan_supported)
8097 			wpa_s->mac_addr_rand_supported |=
8098 				(MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
8099 		wpa_s->drv_max_probe_req_ie_len = capa.max_probe_req_ie_len;
8100 
8101 		wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
8102 		if (wpa_s->extended_capa &&
8103 		    wpa_s->extended_capa_len >= 3 &&
8104 		    wpa_s->extended_capa[2] & 0x40)
8105 			wpa_s->multi_bss_support = 1;
8106 
8107 		if (wpa_drv_get_mld_capa(wpa_s, WPA_IF_STATION,
8108 					 &eml_capa, &mld_capa) == 0) {
8109 			wpa_s->eml_capa = eml_capa;
8110 			wpa_s->mld_capa = mld_capa;
8111 		}
8112 	} else {
8113 		wpa_s->drv_max_probe_req_ie_len = 1500;
8114 	}
8115 #ifdef CONFIG_PASN
8116 	wpa_pasn_sm_set_caps(wpa_s->wpa, wpa_s->drv_flags2);
8117 #endif /* CONFIG_PASN */
8118 
8119 #ifdef CONFIG_IEEE8021X_AUTH
8120 	wpa_sm_set_802_1x_auth_caps(wpa_s->wpa, wpa_s->drv_flags2);
8121 #endif /* CONFIG_IEEE8021X_AUTH */
8122 
8123 	wpa_sm_set_driver_bss_selection(wpa_s->wpa,
8124 					!!(wpa_s->drv_flags &
8125 					   WPA_DRIVER_FLAGS_BSS_SELECTION));
8126 	if (wpa_s->max_remain_on_chan == 0)
8127 		wpa_s->max_remain_on_chan = 1000;
8128 
8129 	/*
8130 	 * Only take p2p_mgmt parameters when P2P Device is supported.
8131 	 * Doing it here as it determines whether l2_packet_init() will be done
8132 	 * during wpa_supplicant_driver_init().
8133 	 */
8134 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
8135 		wpa_s->p2p_mgmt = iface->p2p_mgmt;
8136 
8137 	wpa_s->nan_mgmt = iface->nan_mgmt;
8138 	wpa_s->nan_data = iface->nan_data;
8139 
8140 	if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_P2P_ASSISTED_DFS) &&
8141 	    wpa_s->conf->p2p_assisted_dfs_chan_enable)
8142 		wpa_s->allow_p2p_assisted_dfs = true;
8143 
8144 	if (wpa_s->num_multichan_concurrent == 0)
8145 		wpa_s->num_multichan_concurrent = 1;
8146 
8147 	if (wpa_supplicant_driver_init(wpa_s) < 0)
8148 		return -1;
8149 
8150 #ifdef CONFIG_TDLS
8151 	if (!iface->p2p_mgmt && !iface->nan_mgmt && wpa_tdls_init(wpa_s->wpa))
8152 		return -1;
8153 #endif /* CONFIG_TDLS */
8154 
8155 	if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
8156 	    wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
8157 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
8158 		return -1;
8159 	}
8160 
8161 #ifdef CONFIG_FST
8162 	if (wpa_s->conf->fst_group_id) {
8163 		struct fst_iface_cfg cfg;
8164 		struct fst_wpa_obj iface_obj;
8165 
8166 		fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
8167 		os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
8168 			   sizeof(cfg.group_id));
8169 		cfg.priority = wpa_s->conf->fst_priority;
8170 		cfg.llt = wpa_s->conf->fst_llt;
8171 
8172 		wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
8173 					&iface_obj, &cfg);
8174 		if (!wpa_s->fst) {
8175 			wpa_msg(wpa_s, MSG_ERROR,
8176 				"FST: Cannot attach iface %s to group %s",
8177 				wpa_s->ifname, cfg.group_id);
8178 			return -1;
8179 		}
8180 	}
8181 #endif /* CONFIG_FST */
8182 
8183 	if (wpas_wps_init(wpa_s))
8184 		return -1;
8185 
8186 #ifdef CONFIG_GAS_SERVER
8187 	wpa_s->gas_server = gas_server_init(wpa_s, wpas_gas_server_tx);
8188 	if (!wpa_s->gas_server) {
8189 		wpa_printf(MSG_ERROR, "Failed to initialize GAS server");
8190 		return -1;
8191 	}
8192 #endif /* CONFIG_GAS_SERVER */
8193 
8194 #ifdef CONFIG_DPP
8195 	if (wpas_dpp_init(wpa_s) < 0)
8196 		return -1;
8197 #endif /* CONFIG_DPP */
8198 
8199 	if (wpas_nan_de_init(wpa_s) < 0)
8200 		return -1;
8201 
8202 #ifdef CONFIG_NAN
8203 	os_memcpy(&wpa_s->nan_capa, &capa.nan_capa,
8204 		  sizeof(wpa_s->nan_capa));
8205 #endif /* CONFIG_NAN */
8206 
8207 	if (wpa_supplicant_init_eapol(wpa_s) < 0)
8208 		return -1;
8209 	wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
8210 
8211 	wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
8212 	if (wpa_s->ctrl_iface == NULL) {
8213 		wpa_printf(MSG_ERROR,
8214 			   "Failed to initialize control interface '%s'.\n"
8215 			   "You may have another wpa_supplicant process "
8216 			   "already running or the file was\n"
8217 			   "left by an unclean termination of wpa_supplicant "
8218 			   "in which case you will need\n"
8219 			   "to manually remove this file before starting "
8220 			   "wpa_supplicant again.\n",
8221 			   wpa_s->conf->ctrl_interface);
8222 		return -1;
8223 	}
8224 
8225 	wpa_s->gas = gas_query_init(wpa_s);
8226 	if (wpa_s->gas == NULL) {
8227 		wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
8228 		return -1;
8229 	}
8230 
8231 #ifdef CONFIG_P2P
8232 	if (wpa_s->drv_flags2 & (WPA_DRIVER_FLAGS2_P2P_FEATURE_V2 |
8233 				 WPA_DRIVER_FLAGS2_P2P_FEATURE_PCC_MODE)) {
8234 		wpa_s->p2p_pairing_setup = true;
8235 		wpa_s->p2p_pairing_cache = true;
8236 	}
8237 #endif /* CONFIG_P2P */
8238 
8239 	if ((!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) ||
8240 	     wpa_s->p2p_mgmt) &&
8241 	    wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
8242 		wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
8243 		return -1;
8244 	}
8245 
8246 	if (!capa.ranging_type.pd_support) {
8247 		wpa_printf(MSG_DEBUG,
8248 			   "PR: Driver does not support Proximity Ranging - PR disabled");
8249 	} else if (wpas_pr_init(wpa_s->global, wpa_s, &capa) < 0) {
8250 		return -1;
8251 	}
8252 
8253 	if (wpa_bss_init(wpa_s) < 0)
8254 		return -1;
8255 
8256 	/*
8257 	 * Set Wake-on-WLAN triggers, if configured.
8258 	 * Note: We don't restore/remove the triggers on shutdown (it doesn't
8259 	 * have effect anyway when the interface is down).
8260 	 */
8261 	if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
8262 		return -1;
8263 
8264 #ifdef CONFIG_EAP_PROXY
8265 {
8266 	size_t len;
8267 	wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
8268 						     wpa_s->imsi, &len);
8269 	if (wpa_s->mnc_len > 0) {
8270 		wpa_s->imsi[len] = '\0';
8271 		wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
8272 			   wpa_s->imsi, wpa_s->mnc_len);
8273 	} else {
8274 		wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
8275 	}
8276 }
8277 #endif /* CONFIG_EAP_PROXY */
8278 
8279 	if (pcsc_reader_init(wpa_s) < 0)
8280 		return -1;
8281 
8282 	if (wpas_init_ext_pw(wpa_s) < 0)
8283 		return -1;
8284 
8285 #ifndef CONFIG_NO_RRM
8286 	wpas_rrm_reset(wpa_s);
8287 #endif /* CONFIG_NO_RRM */
8288 
8289 	wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
8290 
8291 #ifdef CONFIG_MBO
8292 	if (!wpa_s->disable_mbo_oce && wpa_s->conf->oce) {
8293 		if ((wpa_s->conf->oce & OCE_STA) &&
8294 		    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
8295 			wpa_s->enable_oce = OCE_STA;
8296 		if ((wpa_s->conf->oce & OCE_STA_CFON) &&
8297 		    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
8298 			/* TODO: Need to add STA-CFON support */
8299 			wpa_printf(MSG_ERROR,
8300 				   "OCE STA-CFON feature is not yet supported");
8301 		}
8302 	}
8303 	wpas_mbo_update_non_pref_chan(wpa_s, wpa_s->conf->non_pref_chan);
8304 #endif /* CONFIG_MBO */
8305 
8306 	wpa_supplicant_set_default_scan_ies(wpa_s);
8307 
8308 	if (wpa_s->nan_mgmt && wpas_nan_init(wpa_s) < 0) {
8309 		wpa_msg(wpa_s, MSG_ERROR, "Failed to init NAN");
8310 		return -1;
8311 	}
8312 
8313 	return 0;
8314 }
8315 
8316 
wpa_supplicant_deinit_iface(struct wpa_supplicant * wpa_s,int notify,int terminate)8317 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
8318 					int notify, int terminate)
8319 {
8320 	struct wpa_global *global = wpa_s->global;
8321 	struct wpa_supplicant *iface, *prev;
8322 
8323 	if (wpa_s == wpa_s->parent)
8324 		wpas_p2p_group_remove(wpa_s, "*");
8325 
8326 	iface = global->ifaces;
8327 	while (iface) {
8328 		if (iface->p2pdev == wpa_s)
8329 			iface->p2pdev = iface->parent;
8330 		if (iface == wpa_s || iface->parent != wpa_s) {
8331 			iface = iface->next;
8332 			continue;
8333 		}
8334 		wpa_printf(MSG_DEBUG,
8335 			   "Remove remaining child interface %s from parent %s",
8336 			   iface->ifname, wpa_s->ifname);
8337 		prev = iface;
8338 		iface = iface->next;
8339 		wpa_supplicant_remove_iface(global, prev, terminate);
8340 	}
8341 
8342 	wpa_s->disconnected = 1;
8343 	if (wpa_s->drv_priv) {
8344 		/*
8345 		 * Don't deauthenticate if WoWLAN is enable and not explicitly
8346 		 * been configured to disconnect.
8347 		 */
8348 		if (!wpa_drv_get_wowlan(wpa_s) ||
8349 		    wpa_s->conf->wowlan_disconnect_on_deinit) {
8350 			wpa_supplicant_deauthenticate(
8351 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
8352 
8353 			wpa_drv_set_countermeasures(wpa_s, 0);
8354 			wpa_clear_keys(wpa_s, NULL);
8355 		} else {
8356 			wpa_msg(wpa_s, MSG_INFO,
8357 				"Do not deauthenticate as part of interface deinit since WoWLAN is enabled");
8358 		}
8359 	}
8360 
8361 	wpa_supplicant_cleanup(wpa_s);
8362 	wpas_p2p_deinit_iface(wpa_s);
8363 
8364 	wpas_nan_deinit(wpa_s);
8365 
8366 	wpas_ctrl_radio_work_flush(wpa_s);
8367 	radio_remove_interface(wpa_s);
8368 
8369 #ifdef CONFIG_FST
8370 	if (wpa_s->fst) {
8371 		fst_detach(wpa_s->fst);
8372 		wpa_s->fst = NULL;
8373 	}
8374 	if (wpa_s->received_mb_ies) {
8375 		wpabuf_free(wpa_s->received_mb_ies);
8376 		wpa_s->received_mb_ies = NULL;
8377 	}
8378 #endif /* CONFIG_FST */
8379 
8380 	if (wpa_s->drv_priv)
8381 		wpa_drv_deinit(wpa_s);
8382 
8383 	if (notify)
8384 		wpas_notify_iface_removed(wpa_s);
8385 
8386 	if (terminate)
8387 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
8388 
8389 	wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
8390 	wpa_s->ctrl_iface = NULL;
8391 
8392 #ifdef CONFIG_MESH
8393 	if (wpa_s->ifmsh) {
8394 		wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh, true);
8395 		wpa_s->ifmsh = NULL;
8396 	}
8397 #endif /* CONFIG_MESH */
8398 
8399 	if (wpa_s->conf != NULL) {
8400 		wpa_config_free(wpa_s->conf);
8401 		wpa_s->conf = NULL;
8402 	}
8403 
8404 	os_free(wpa_s->ssids_from_scan_req);
8405 	os_free(wpa_s->last_scan_freqs);
8406 
8407 	os_free(wpa_s);
8408 }
8409 
8410 
8411 #ifdef CONFIG_MATCH_IFACE
8412 
8413 /**
8414  * wpa_supplicant_match_iface - Match an interface description to a name
8415  * @global: Pointer to global data from wpa_supplicant_init()
8416  * @ifname: Name of the interface to match
8417  * Returns: Pointer to the created interface description or %NULL on failure
8418  */
wpa_supplicant_match_iface(struct wpa_global * global,const char * ifname)8419 struct wpa_interface * wpa_supplicant_match_iface(struct wpa_global *global,
8420 						  const char *ifname)
8421 {
8422 	int i;
8423 	struct wpa_interface *iface, *miface;
8424 
8425 	for (i = 0; i < global->params.match_iface_count; i++) {
8426 		miface = &global->params.match_ifaces[i];
8427 		if (!miface->ifname ||
8428 		    fnmatch(miface->ifname, ifname, 0) == 0) {
8429 			iface = os_zalloc(sizeof(*iface));
8430 			if (!iface)
8431 				return NULL;
8432 			*iface = *miface;
8433 			if (!miface->ifname)
8434 				iface->matched = WPA_IFACE_MATCHED_NULL;
8435 			else
8436 				iface->matched = WPA_IFACE_MATCHED;
8437 			iface->ifname = ifname;
8438 			return iface;
8439 		}
8440 	}
8441 
8442 	return NULL;
8443 }
8444 
8445 
8446 /**
8447  * wpa_supplicant_match_existing - Match existing interfaces
8448  * @global: Pointer to global data from wpa_supplicant_init()
8449  * Returns: 0 on success, -1 on failure
8450  */
wpa_supplicant_match_existing(struct wpa_global * global)8451 static int wpa_supplicant_match_existing(struct wpa_global *global)
8452 {
8453 	struct if_nameindex *ifi, *ifp;
8454 	struct wpa_supplicant *wpa_s;
8455 	struct wpa_interface *iface;
8456 
8457 	ifp = if_nameindex();
8458 	if (!ifp) {
8459 		wpa_printf(MSG_ERROR, "if_nameindex: %s", strerror(errno));
8460 		return -1;
8461 	}
8462 
8463 	for (ifi = ifp; ifi->if_name; ifi++) {
8464 		wpa_s = wpa_supplicant_get_iface(global, ifi->if_name);
8465 		if (wpa_s)
8466 			continue;
8467 		iface = wpa_supplicant_match_iface(global, ifi->if_name);
8468 		if (iface) {
8469 			wpa_supplicant_add_iface(global, iface, NULL);
8470 			os_free(iface);
8471 		}
8472 	}
8473 
8474 	if_freenameindex(ifp);
8475 	return 0;
8476 }
8477 
8478 #endif /* CONFIG_MATCH_IFACE */
8479 
8480 
8481 /**
8482  * wpa_supplicant_add_iface - Add a new network interface
8483  * @global: Pointer to global data from wpa_supplicant_init()
8484  * @iface: Interface configuration options
8485  * @parent: Parent interface or %NULL to assign new interface as parent
8486  * Returns: Pointer to the created interface or %NULL on failure
8487  *
8488  * This function is used to add new network interfaces for %wpa_supplicant.
8489  * This can be called before wpa_supplicant_run() to add interfaces before the
8490  * main event loop has been started. In addition, new interfaces can be added
8491  * dynamically while %wpa_supplicant is already running. This could happen,
8492  * e.g., when a hotplug network adapter is inserted.
8493  */
wpa_supplicant_add_iface(struct wpa_global * global,struct wpa_interface * iface,struct wpa_supplicant * parent)8494 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
8495 						 struct wpa_interface *iface,
8496 						 struct wpa_supplicant *parent)
8497 {
8498 	struct wpa_supplicant *wpa_s;
8499 	struct wpa_interface t_iface;
8500 	struct wpa_ssid *ssid;
8501 
8502 	if (global == NULL || iface == NULL)
8503 		return NULL;
8504 
8505 	wpa_s = wpa_supplicant_alloc(parent);
8506 	if (wpa_s == NULL)
8507 		return NULL;
8508 
8509 	wpa_s->global = global;
8510 
8511 	t_iface = *iface;
8512 	if (global->params.override_driver) {
8513 		wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
8514 			   "('%s' -> '%s')",
8515 			   iface->driver, global->params.override_driver);
8516 		t_iface.driver = global->params.override_driver;
8517 	}
8518 	if (global->params.override_ctrl_interface) {
8519 		wpa_printf(MSG_DEBUG, "Override interface parameter: "
8520 			   "ctrl_interface ('%s' -> '%s')",
8521 			   iface->ctrl_interface,
8522 			   global->params.override_ctrl_interface);
8523 		t_iface.ctrl_interface =
8524 			global->params.override_ctrl_interface;
8525 	}
8526 	if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
8527 		wpa_printf(MSG_DEBUG, "Failed to add interface %s",
8528 			   iface->ifname);
8529 		wpa_supplicant_deinit_iface(wpa_s, 0, 0);
8530 		return NULL;
8531 	}
8532 
8533 	if (iface->p2p_mgmt == 0 && !iface->nan_mgmt) {
8534 		/* Notify the control interfaces about new iface */
8535 		if (wpas_notify_iface_added(wpa_s)) {
8536 			wpa_supplicant_deinit_iface(wpa_s, 1, 0);
8537 			return NULL;
8538 		}
8539 
8540 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
8541 			wpas_notify_network_added(wpa_s, ssid);
8542 	}
8543 
8544 	wpa_s->next = global->ifaces;
8545 	global->ifaces = wpa_s;
8546 
8547 	wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
8548 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8549 
8550 #ifdef CONFIG_P2P
8551 	if (!wpa_s->global->p2p && !wpas_is_nan_iface(wpa_s) &&
8552 	    !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
8553 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
8554 	    wpas_p2p_add_p2pdev_interface(
8555 		    wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
8556 		wpa_printf(MSG_INFO,
8557 			   "P2P: Failed to enable P2P Device interface");
8558 		/* Try to continue without. P2P will be disabled. */
8559 	}
8560 #endif /* CONFIG_P2P */
8561 
8562 	return wpa_s;
8563 }
8564 
8565 
8566 /**
8567  * wpa_supplicant_remove_iface - Remove a network interface
8568  * @global: Pointer to global data from wpa_supplicant_init()
8569  * @wpa_s: Pointer to the network interface to be removed
8570  * Returns: 0 if interface was removed, -1 if interface was not found
8571  *
8572  * This function can be used to dynamically remove network interfaces from
8573  * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
8574  * addition, this function is used to remove all remaining interfaces when
8575  * %wpa_supplicant is terminated.
8576  */
wpa_supplicant_remove_iface(struct wpa_global * global,struct wpa_supplicant * wpa_s,int terminate)8577 int wpa_supplicant_remove_iface(struct wpa_global *global,
8578 				struct wpa_supplicant *wpa_s,
8579 				int terminate)
8580 {
8581 	struct wpa_supplicant *prev;
8582 #ifdef CONFIG_MESH
8583 	unsigned int mesh_if_created = wpa_s->mesh_if_created;
8584 	char *ifname = NULL;
8585 	struct wpa_supplicant *parent = wpa_s->parent;
8586 #endif /* CONFIG_MESH */
8587 
8588 	/* Remove interface from the global list of interfaces */
8589 	prev = global->ifaces;
8590 	if (prev == wpa_s) {
8591 		global->ifaces = wpa_s->next;
8592 	} else {
8593 		while (prev && prev->next != wpa_s)
8594 			prev = prev->next;
8595 		if (prev == NULL)
8596 			return -1;
8597 		prev->next = wpa_s->next;
8598 	}
8599 
8600 	wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
8601 
8602 #ifdef CONFIG_MESH
8603 	if (mesh_if_created) {
8604 		ifname = os_strdup(wpa_s->ifname);
8605 		if (ifname == NULL) {
8606 			wpa_dbg(wpa_s, MSG_ERROR,
8607 				"mesh: Failed to malloc ifname");
8608 			return -1;
8609 		}
8610 	}
8611 #endif /* CONFIG_MESH */
8612 
8613 	if (global->p2p_group_formation == wpa_s)
8614 		global->p2p_group_formation = NULL;
8615 	if (global->p2p_invite_group == wpa_s)
8616 		global->p2p_invite_group = NULL;
8617 	wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
8618 
8619 #ifdef CONFIG_MESH
8620 	if (mesh_if_created) {
8621 		wpa_drv_if_remove(parent, WPA_IF_MESH, ifname);
8622 		os_free(ifname);
8623 	}
8624 #endif /* CONFIG_MESH */
8625 
8626 	return 0;
8627 }
8628 
8629 
8630 /**
8631  * wpa_supplicant_get_eap_mode - Get the current EAP mode
8632  * @wpa_s: Pointer to the network interface
8633  * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
8634  */
wpa_supplicant_get_eap_mode(struct wpa_supplicant * wpa_s)8635 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
8636 {
8637 	const char *eapol_method;
8638 
8639         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
8640             wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
8641 		return "NO-EAP";
8642 	}
8643 
8644 	eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
8645 	if (eapol_method == NULL)
8646 		return "UNKNOWN-EAP";
8647 
8648 	return eapol_method;
8649 }
8650 
8651 
8652 /**
8653  * wpa_supplicant_get_iface - Get a new network interface
8654  * @global: Pointer to global data from wpa_supplicant_init()
8655  * @ifname: Interface name
8656  * Returns: Pointer to the interface or %NULL if not found
8657  */
wpa_supplicant_get_iface(struct wpa_global * global,const char * ifname)8658 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
8659 						 const char *ifname)
8660 {
8661 	struct wpa_supplicant *wpa_s;
8662 
8663 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8664 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
8665 			return wpa_s;
8666 	}
8667 	return NULL;
8668 }
8669 
8670 
8671 #ifndef CONFIG_NO_WPA_MSG
wpa_supplicant_msg_ifname_cb(void * ctx)8672 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
8673 {
8674 	struct wpa_supplicant *wpa_s = ctx;
8675 	if (wpa_s == NULL)
8676 		return NULL;
8677 	return wpa_s->ifname;
8678 }
8679 #endif /* CONFIG_NO_WPA_MSG */
8680 
8681 
8682 #ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
8683 #define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
8684 #endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
8685 
8686 /* Periodic cleanup tasks */
wpas_periodic(void * eloop_ctx,void * timeout_ctx)8687 static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
8688 {
8689 	struct wpa_global *global = eloop_ctx;
8690 	struct wpa_supplicant *wpa_s;
8691 
8692 	eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
8693 			       wpas_periodic, global, NULL);
8694 
8695 #ifdef CONFIG_P2P
8696 	if (global->p2p)
8697 		p2p_expire_peers(global->p2p);
8698 #endif /* CONFIG_P2P */
8699 
8700 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8701 		wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
8702 #ifdef CONFIG_AP
8703 		ap_periodic(wpa_s);
8704 #endif /* CONFIG_AP */
8705 	}
8706 }
8707 
8708 
8709 /**
8710  * wpa_supplicant_init - Initialize %wpa_supplicant
8711  * @params: Parameters for %wpa_supplicant
8712  * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
8713  *
8714  * This function is used to initialize %wpa_supplicant. After successful
8715  * initialization, the returned data pointer can be used to add and remove
8716  * network interfaces, and eventually, to deinitialize %wpa_supplicant.
8717  */
wpa_supplicant_init(struct wpa_params * params)8718 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
8719 {
8720 	struct wpa_global *global;
8721 	int ret, i;
8722 
8723 	if (params == NULL)
8724 		return NULL;
8725 
8726 #ifndef CONFIG_NO_WPA_MSG
8727 	wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
8728 #endif /* CONFIG_NO_WPA_MSG */
8729 
8730 	if (params->wpa_debug_file_path)
8731 		wpa_debug_open_file(params->wpa_debug_file_path);
8732 	if (!params->wpa_debug_file_path && !params->wpa_debug_syslog)
8733 		wpa_debug_setup_stdout();
8734 	if (params->wpa_debug_syslog)
8735 		wpa_debug_open_syslog();
8736 	if (params->wpa_debug_tracing) {
8737 		ret = wpa_debug_open_linux_tracing();
8738 		if (ret) {
8739 			wpa_printf(MSG_ERROR,
8740 				   "Failed to enable trace logging");
8741 			return NULL;
8742 		}
8743 	}
8744 
8745 	ret = eap_register_methods();
8746 	if (ret) {
8747 		wpa_printf(MSG_ERROR, "Failed to register EAP methods");
8748 		if (ret == -2)
8749 			wpa_printf(MSG_ERROR, "Two or more EAP methods used "
8750 				   "the same EAP type.");
8751 		return NULL;
8752 	}
8753 
8754 	global = os_zalloc(sizeof(*global));
8755 	if (global == NULL)
8756 		return NULL;
8757 	dl_list_init(&global->p2p_srv_bonjour);
8758 	dl_list_init(&global->p2p_srv_upnp);
8759 	global->params.daemonize = params->daemonize;
8760 	global->params.wait_for_monitor = params->wait_for_monitor;
8761 	global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
8762 	global->params.show_details = params->show_details;
8763 
8764 	if (params->pid_file) {
8765 		global->params.pid_file = os_strdup(params->pid_file);
8766 		if (!global->params.pid_file) {
8767 			wpa_supplicant_deinit(global);
8768 			return NULL;
8769 		}
8770 	}
8771 
8772 	if (params->ctrl_interface) {
8773 		global->params.ctrl_interface =
8774 			os_strdup(params->ctrl_interface);
8775 		if (!global->params.ctrl_interface) {
8776 			wpa_supplicant_deinit(global);
8777 			return NULL;
8778 		}
8779 	}
8780 
8781 	if (params->ctrl_interface_group) {
8782 		global->params.ctrl_interface_group =
8783 			os_strdup(params->ctrl_interface_group);
8784 		if (!global->params.ctrl_interface_group) {
8785 			wpa_supplicant_deinit(global);
8786 			return NULL;
8787 		}
8788 	}
8789 
8790 	if (params->override_driver) {
8791 		global->params.override_driver =
8792 			os_strdup(params->override_driver);
8793 		if (!global->params.override_driver) {
8794 			wpa_supplicant_deinit(global);
8795 			return NULL;
8796 		}
8797 	}
8798 
8799 	if (params->override_ctrl_interface) {
8800 		global->params.override_ctrl_interface =
8801 			os_strdup(params->override_ctrl_interface);
8802 		if (!global->params.override_ctrl_interface) {
8803 			wpa_supplicant_deinit(global);
8804 			return NULL;
8805 		}
8806 	}
8807 
8808 #ifdef CONFIG_MATCH_IFACE
8809 	global->params.match_iface_count = params->match_iface_count;
8810 	if (params->match_iface_count) {
8811 		global->params.match_ifaces =
8812 			os_calloc(params->match_iface_count,
8813 				  sizeof(struct wpa_interface));
8814 		if (!global->params.match_ifaces) {
8815 			wpa_printf(MSG_ERROR,
8816 				   "Failed to allocate match interfaces");
8817 			wpa_supplicant_deinit(global);
8818 			return NULL;
8819 		}
8820 		os_memcpy(global->params.match_ifaces,
8821 			  params->match_ifaces,
8822 			  params->match_iface_count *
8823 			  sizeof(struct wpa_interface));
8824 	}
8825 #endif /* CONFIG_MATCH_IFACE */
8826 #ifdef CONFIG_P2P
8827 	if (params->conf_p2p_dev) {
8828 		global->params.conf_p2p_dev =
8829 			os_strdup(params->conf_p2p_dev);
8830 		if (!global->params.conf_p2p_dev) {
8831 			wpa_printf(MSG_ERROR, "Failed to allocate conf p2p");
8832 			wpa_supplicant_deinit(global);
8833 			return NULL;
8834 		}
8835 	}
8836 #endif /* CONFIG_P2P */
8837 	wpa_debug_level = global->params.wpa_debug_level =
8838 		params->wpa_debug_level;
8839 	wpa_debug_show_keys = global->params.wpa_debug_show_keys =
8840 		params->wpa_debug_show_keys;
8841 	wpa_debug_timestamp = global->params.wpa_debug_timestamp =
8842 		params->wpa_debug_timestamp;
8843 
8844 	wpa_printf(MSG_DEBUG, "wpa_supplicant v%s", VERSION_STR);
8845 
8846 	if (eloop_init()) {
8847 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
8848 		wpa_supplicant_deinit(global);
8849 		return NULL;
8850 	}
8851 
8852 	random_init(params->entropy_file);
8853 
8854 #ifdef CONFIG_PROCESS_COORDINATION
8855 	if (params->proc_coord_dir) {
8856 		global->pc = proc_coord_init(params->proc_coord_dir);
8857 		if (!global->pc) {
8858 			wpa_supplicant_deinit(global);
8859 			return NULL;
8860 		}
8861 	}
8862 #endif /* CONFIG_PROCESS_COORDINATION */
8863 
8864 	global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
8865 	if (global->ctrl_iface == NULL) {
8866 		wpa_supplicant_deinit(global);
8867 		return NULL;
8868 	}
8869 
8870 	if (wpas_notify_supplicant_initialized(global)) {
8871 		wpa_supplicant_deinit(global);
8872 		return NULL;
8873 	}
8874 
8875 	for (i = 0; wpa_drivers[i]; i++)
8876 		global->drv_count++;
8877 	if (global->drv_count == 0) {
8878 		wpa_printf(MSG_ERROR, "No drivers enabled");
8879 		wpa_supplicant_deinit(global);
8880 		return NULL;
8881 	}
8882 	global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
8883 	if (global->drv_priv == NULL) {
8884 		wpa_supplicant_deinit(global);
8885 		return NULL;
8886 	}
8887 
8888 #ifdef CONFIG_WIFI_DISPLAY
8889 	if (wifi_display_init(global) < 0) {
8890 		wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
8891 		wpa_supplicant_deinit(global);
8892 		return NULL;
8893 	}
8894 #endif /* CONFIG_WIFI_DISPLAY */
8895 
8896 	eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
8897 			       wpas_periodic, global, NULL);
8898 
8899 	return global;
8900 }
8901 
8902 
8903 /**
8904  * wpa_supplicant_run - Run the %wpa_supplicant main event loop
8905  * @global: Pointer to global data from wpa_supplicant_init()
8906  * Returns: 0 after successful event loop run, -1 on failure
8907  *
8908  * This function starts the main event loop and continues running as long as
8909  * there are any remaining events. In most cases, this function is running as
8910  * long as the %wpa_supplicant process in still in use.
8911  */
wpa_supplicant_run(struct wpa_global * global)8912 int wpa_supplicant_run(struct wpa_global *global)
8913 {
8914 	struct wpa_supplicant *wpa_s;
8915 
8916 	if (global->params.daemonize &&
8917 	    (wpa_supplicant_daemon(global->params.pid_file) ||
8918 	     eloop_sock_requeue()))
8919 		return -1;
8920 
8921 #ifdef CONFIG_MATCH_IFACE
8922 	if (wpa_supplicant_match_existing(global))
8923 		return -1;
8924 #endif
8925 
8926 	if (global->params.wait_for_monitor) {
8927 		for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
8928 			if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
8929 				wpa_supplicant_ctrl_iface_wait(
8930 					wpa_s->ctrl_iface);
8931 	}
8932 
8933 	eloop_register_signal_terminate(wpa_supplicant_terminate, global);
8934 	eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
8935 
8936 	eloop_run();
8937 
8938 	return 0;
8939 }
8940 
8941 
8942 /**
8943  * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
8944  * @global: Pointer to global data from wpa_supplicant_init()
8945  *
8946  * This function is called to deinitialize %wpa_supplicant and to free all
8947  * allocated resources. Remaining network interfaces will also be removed.
8948  */
wpa_supplicant_deinit(struct wpa_global * global)8949 void wpa_supplicant_deinit(struct wpa_global *global)
8950 {
8951 	int i;
8952 
8953 	if (global == NULL)
8954 		return;
8955 
8956 	eloop_cancel_timeout(wpas_periodic, global, NULL);
8957 
8958 #ifdef CONFIG_WIFI_DISPLAY
8959 	wifi_display_deinit(global);
8960 #endif /* CONFIG_WIFI_DISPLAY */
8961 
8962 	while (global->ifaces)
8963 		wpa_supplicant_remove_iface(global, global->ifaces, 1);
8964 
8965 	if (global->ctrl_iface)
8966 		wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
8967 
8968 	wpas_notify_supplicant_deinitialized(global);
8969 
8970 	eap_peer_unregister_methods();
8971 #ifdef CONFIG_AP
8972 	eap_server_unregister_methods();
8973 #endif /* CONFIG_AP */
8974 
8975 	for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
8976 		if (!global->drv_priv[i])
8977 			continue;
8978 		wpa_drivers[i]->global_deinit(global->drv_priv[i]);
8979 	}
8980 	os_free(global->drv_priv);
8981 
8982 	random_deinit();
8983 
8984 #ifdef CONFIG_PROCESS_COORDINATION
8985 	proc_coord_deinit(global->pc);
8986 #endif /* CONFIG_PROCESS_COORDINATION */
8987 
8988 	eloop_destroy();
8989 
8990 	if (global->params.pid_file) {
8991 		os_daemonize_terminate(global->params.pid_file);
8992 		os_free(global->params.pid_file);
8993 	}
8994 	os_free(global->params.ctrl_interface);
8995 	os_free(global->params.ctrl_interface_group);
8996 	os_free(global->params.override_driver);
8997 	os_free(global->params.override_ctrl_interface);
8998 #ifdef CONFIG_MATCH_IFACE
8999 	os_free(global->params.match_ifaces);
9000 #endif /* CONFIG_MATCH_IFACE */
9001 #ifdef CONFIG_P2P
9002 	os_free(global->params.conf_p2p_dev);
9003 #endif /* CONFIG_P2P */
9004 
9005 	os_free(global->p2p_disallow_freq.range);
9006 	os_free(global->p2p_go_avoid_freq.range);
9007 	os_free(global->add_psk);
9008 
9009 	os_free(global);
9010 	wpa_debug_close_syslog();
9011 	wpa_debug_close_file();
9012 	wpa_debug_close_linux_tracing();
9013 }
9014 
9015 
wpa_supplicant_parse_config(const char * fname)9016 int wpa_supplicant_parse_config(const char *fname)
9017 {
9018 	struct wpa_config *conf;
9019 	int ret = -1;
9020 
9021 	wpa_printf(MSG_INFO, "Validating parsing of %s", fname);
9022 	conf = wpa_config_read(fname, NULL, false, true);
9023 	if (conf) {
9024 		wpa_printf(MSG_INFO, "Parsing succeeded");
9025 		ret = 0;
9026 		wpa_config_free(conf);
9027 	} else {
9028 		wpa_printf(MSG_INFO, "Parsing failed");
9029 	}
9030 	return ret;
9031 }
9032 
9033 
wpa_supplicant_update_config(struct wpa_supplicant * wpa_s)9034 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
9035 {
9036 	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
9037 	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
9038 		char country[3];
9039 		country[0] = wpa_s->conf->country[0];
9040 		country[1] = wpa_s->conf->country[1];
9041 		country[2] = '\0';
9042 		if (wpa_drv_set_country(wpa_s, country) < 0) {
9043 			wpa_printf(MSG_ERROR, "Failed to set country code "
9044 				   "'%s'", country);
9045 		}
9046 	}
9047 
9048 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
9049 		wpas_init_ext_pw(wpa_s);
9050 
9051 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
9052 		wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
9053 
9054 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_WOWLAN_TRIGGERS) {
9055 		struct wpa_driver_capa capa;
9056 		int res = wpa_drv_get_capa(wpa_s, &capa);
9057 
9058 		if (res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
9059 			wpa_printf(MSG_ERROR,
9060 				   "Failed to update wowlan_triggers to '%s'",
9061 				   wpa_s->conf->wowlan_triggers);
9062 	}
9063 
9064 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DISABLE_BTM)
9065 		wpa_supplicant_set_default_scan_ies(wpa_s);
9066 
9067 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_FT_PREPEND_PMKID)
9068 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_FT_PREPEND_PMKID,
9069 				 wpa_s->conf->ft_prepend_pmkid);
9070 
9071 #ifdef CONFIG_P2P
9072 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_DISABLED)
9073 		wpas_p2p_disabled_changed(wpa_s);
9074 #endif /* CONFIG_P2P */
9075 
9076 #ifdef CONFIG_BGSCAN
9077 	/*
9078 	 * We default to global bgscan parameters only when per-network bgscan
9079 	 * parameters aren't set. Only bother resetting bgscan parameters if
9080 	 * this is the case.
9081 	 */
9082 	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_BGSCAN) &&
9083 	    wpa_s->current_ssid && !wpa_s->current_ssid->bgscan &&
9084 	    wpa_s->wpa_state == WPA_COMPLETED)
9085 		wpa_supplicant_reset_bgscan(wpa_s);
9086 #endif /* CONFIG_BGSCAN */
9087 
9088 #ifdef CONFIG_WPS
9089 	wpas_wps_update_config(wpa_s);
9090 #endif /* CONFIG_WPS */
9091 	wpas_p2p_update_config(wpa_s);
9092 	wpa_s->conf->changed_parameters = 0;
9093 }
9094 
9095 
wpas_connection_failed(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 ** link_bssids)9096 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid,
9097 			    const u8 **link_bssids)
9098 {
9099 	int timeout;
9100 	int count;
9101 
9102 	wpas_connect_work_done(wpa_s);
9103 
9104 	/*
9105 	 * Remove possible authentication timeout since the connection failed.
9106 	 */
9107 	eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
9108 
9109 	/*
9110 	 * There is no point in ignoring the AP temporarily if this event is
9111 	 * generated based on local request to disconnect.
9112 	 */
9113 	if (wpa_s->own_disconnect_req || wpa_s->own_reconnect_req) {
9114 		wpa_s->own_disconnect_req = 0;
9115 		wpa_dbg(wpa_s, MSG_DEBUG,
9116 			"Ignore connection failure due to local request to disconnect");
9117 		return;
9118 	}
9119 	if (wpa_s->disconnected) {
9120 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
9121 			"indication since interface has been put into "
9122 			"disconnected state");
9123 		return;
9124 	}
9125 
9126 	/* Also mark links as failed */
9127 	while (link_bssids && *link_bssids) {
9128 		wpa_bssid_ignore_add(wpa_s, *link_bssids);
9129 		link_bssids++;
9130 	}
9131 
9132 	/*
9133 	 * Add the failed BSSID into the ignore list and speed up next scan
9134 	 * attempt if there could be other APs that could accept association.
9135 	 */
9136 	count = wpa_bssid_ignore_add(wpa_s, bssid);
9137 
9138 	/*
9139 	 * This BSS was not in the ignore list before. If there is
9140 	 * another BSS available for the same ESS, we should try that
9141 	 * next. Otherwise, we may as well try this one once more
9142 	 * before allowing other, likely worse, ESSes to be considered.
9143 	 */
9144 	if (count == 1 && wpa_supplicant_fast_associate(wpa_s) == 1)
9145 		return;
9146 
9147 	wpa_s->consecutive_conn_failures++;
9148 
9149 	if (wpa_s->consecutive_conn_failures > 3 && wpa_s->current_ssid) {
9150 		wpa_printf(MSG_DEBUG, "Continuous association failures - "
9151 			   "consider temporary network disabling");
9152 		wpas_auth_failed(wpa_s, "CONN_FAILED", bssid);
9153 	}
9154 	/*
9155 	 * Multiple consecutive connection failures mean that other APs are
9156 	 * either not available or have already been tried, so we can start
9157 	 * increasing the delay here to avoid constant scanning.
9158 	 */
9159 	switch (wpa_s->consecutive_conn_failures) {
9160 	case 1:
9161 		timeout = 100;
9162 		break;
9163 	case 2:
9164 		timeout = 500;
9165 		break;
9166 	case 3:
9167 		timeout = 1000;
9168 		break;
9169 	case 4:
9170 		timeout = 5000;
9171 		break;
9172 	default:
9173 		timeout = 10000;
9174 		break;
9175 	}
9176 
9177 	wpa_dbg(wpa_s, MSG_DEBUG,
9178 		"Consecutive connection failures: %d --> request scan in %d ms",
9179 		wpa_s->consecutive_conn_failures, timeout);
9180 
9181 	/* speed up the connection attempt with normal scan */
9182 	wpa_s->normal_scans = 0;
9183 	wpa_supplicant_req_scan(wpa_s, timeout / 1000,
9184 				1000 * (timeout % 1000));
9185 }
9186 
9187 
9188 #ifdef CONFIG_FILS
9189 
fils_pmksa_cache_flush(struct wpa_supplicant * wpa_s)9190 void fils_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
9191 {
9192 	struct wpa_ssid *ssid = wpa_s->current_ssid;
9193 	const u8 *realm, *username, *rrk;
9194 	size_t realm_len, username_len, rrk_len;
9195 	u16 next_seq_num;
9196 
9197 	/* Clear the PMKSA cache entry if FILS authentication was rejected.
9198 	 * Check for ERP keys existing to limit when this can be done since
9199 	 * the rejection response is not protected and such triggers should
9200 	 * really not allow internal state to be modified unless required to
9201 	 * avoid significant issues in functionality. In addition, drop
9202 	 * externally configure PMKSA entries even without ERP keys since it
9203 	 * is possible for an external component to add PMKSA entries for FILS
9204 	 * authentication without restoring previously generated ERP keys.
9205 	 *
9206 	 * In this case, this is needed to allow recovery from cases where the
9207 	 * AP or authentication server has dropped PMKSAs and ERP keys. */
9208 	if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt))
9209 		return;
9210 
9211 	if (eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
9212 				  &username, &username_len,
9213 				  &realm, &realm_len, &next_seq_num,
9214 				  &rrk, &rrk_len) != 0 ||
9215 	    !realm) {
9216 		wpa_dbg(wpa_s, MSG_DEBUG,
9217 			"FILS: Drop external PMKSA cache entry");
9218 		wpa_sm_aborted_external_cached(wpa_s->wpa);
9219 		wpa_sm_external_pmksa_cache_flush(wpa_s->wpa, ssid);
9220 		return;
9221 	}
9222 
9223 	wpa_dbg(wpa_s, MSG_DEBUG, "FILS: Drop PMKSA cache entry");
9224 	wpa_sm_aborted_cached(wpa_s->wpa);
9225 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
9226 }
9227 
9228 
fils_connection_failure(struct wpa_supplicant * wpa_s)9229 void fils_connection_failure(struct wpa_supplicant *wpa_s)
9230 {
9231 	struct wpa_ssid *ssid = wpa_s->current_ssid;
9232 	const u8 *realm, *username, *rrk;
9233 	size_t realm_len, username_len, rrk_len;
9234 	u16 next_seq_num;
9235 
9236 	if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt) ||
9237 	    eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
9238 				  &username, &username_len,
9239 				  &realm, &realm_len, &next_seq_num,
9240 				  &rrk, &rrk_len) != 0 ||
9241 	    !realm)
9242 		return;
9243 
9244 	wpa_hexdump_ascii(MSG_DEBUG,
9245 			  "FILS: Store last connection failure realm",
9246 			  realm, realm_len);
9247 	os_free(wpa_s->last_con_fail_realm);
9248 	wpa_s->last_con_fail_realm = os_malloc(realm_len);
9249 	if (wpa_s->last_con_fail_realm) {
9250 		wpa_s->last_con_fail_realm_len = realm_len;
9251 		os_memcpy(wpa_s->last_con_fail_realm, realm, realm_len);
9252 	}
9253 }
9254 #endif /* CONFIG_FILS */
9255 
9256 
wpas_driver_bss_selection(struct wpa_supplicant * wpa_s)9257 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
9258 {
9259 	return wpa_s->conf->ap_scan == 2 ||
9260 		(wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
9261 }
9262 
9263 
wpas_driver_rsn_override(struct wpa_supplicant * wpa_s)9264 static bool wpas_driver_rsn_override(struct wpa_supplicant *wpa_s)
9265 {
9266 	return !!(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_RSN_OVERRIDE_STA);
9267 }
9268 
9269 
wpas_rsn_overriding(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)9270 bool wpas_rsn_overriding(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
9271 {
9272 	enum wpas_rsn_overriding rsno;
9273 
9274 	if (ssid && ssid->rsn_overriding != RSN_OVERRIDING_NOT_SET)
9275 		rsno = ssid->rsn_overriding;
9276 	else
9277 		rsno = wpa_s->conf->rsn_overriding;
9278 
9279 	if (rsno == RSN_OVERRIDING_DISABLED)
9280 		return false;
9281 
9282 	if (rsno == RSN_OVERRIDING_ENABLED)
9283 		return true;
9284 
9285 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
9286 	    wpas_driver_bss_selection(wpa_s))
9287 		return wpas_driver_rsn_override(wpa_s);
9288 
9289 	return true;
9290 }
9291 
9292 
9293 #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const char * field,const char * value)9294 int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
9295 					      struct wpa_ssid *ssid,
9296 					      const char *field,
9297 					      const char *value)
9298 {
9299 #ifdef IEEE8021X_EAPOL
9300 	struct eap_peer_config *eap = &ssid->eap;
9301 
9302 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
9303 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
9304 			      (const u8 *) value, os_strlen(value));
9305 
9306 	switch (wpa_supplicant_ctrl_req_from_string(field)) {
9307 	case WPA_CTRL_REQ_EAP_IDENTITY:
9308 		os_free(eap->identity);
9309 		eap->identity = (u8 *) os_strdup(value);
9310 		if (!eap->identity)
9311 			return -1;
9312 		eap->identity_len = os_strlen(value);
9313 		eap->pending_req_identity = 0;
9314 		if (ssid == wpa_s->current_ssid)
9315 			wpa_s->reassociate = 1;
9316 		break;
9317 	case WPA_CTRL_REQ_EAP_PASSWORD:
9318 		bin_clear_free(eap->password, eap->password_len);
9319 		eap->password = (u8 *) os_strdup(value);
9320 		if (!eap->password)
9321 			return -1;
9322 		eap->password_len = os_strlen(value);
9323 		eap->pending_req_password = 0;
9324 		if (ssid == wpa_s->current_ssid)
9325 			wpa_s->reassociate = 1;
9326 		break;
9327 	case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
9328 		bin_clear_free(eap->new_password, eap->new_password_len);
9329 		eap->new_password = (u8 *) os_strdup(value);
9330 		if (!eap->new_password)
9331 			return -1;
9332 		eap->new_password_len = os_strlen(value);
9333 		eap->pending_req_new_password = 0;
9334 		if (ssid == wpa_s->current_ssid)
9335 			wpa_s->reassociate = 1;
9336 		break;
9337 	case WPA_CTRL_REQ_EAP_PIN:
9338 		str_clear_free(eap->cert.pin);
9339 		eap->cert.pin = os_strdup(value);
9340 		if (!eap->cert.pin)
9341 			return -1;
9342 		eap->pending_req_pin = 0;
9343 		if (ssid == wpa_s->current_ssid)
9344 			wpa_s->reassociate = 1;
9345 		break;
9346 	case WPA_CTRL_REQ_EAP_OTP:
9347 		bin_clear_free(eap->otp, eap->otp_len);
9348 		eap->otp = (u8 *) os_strdup(value);
9349 		if (!eap->otp)
9350 			return -1;
9351 		eap->otp_len = os_strlen(value);
9352 		os_free(eap->pending_req_otp);
9353 		eap->pending_req_otp = NULL;
9354 		eap->pending_req_otp_len = 0;
9355 		break;
9356 	case WPA_CTRL_REQ_EAP_PASSPHRASE:
9357 		str_clear_free(eap->cert.private_key_passwd);
9358 		eap->cert.private_key_passwd = os_strdup(value);
9359 		if (!eap->cert.private_key_passwd)
9360 			return -1;
9361 		eap->pending_req_passphrase = 0;
9362 		if (ssid == wpa_s->current_ssid)
9363 			wpa_s->reassociate = 1;
9364 		break;
9365 	case WPA_CTRL_REQ_SIM:
9366 		str_clear_free(eap->external_sim_resp);
9367 		eap->external_sim_resp = os_strdup(value);
9368 		if (!eap->external_sim_resp)
9369 			return -1;
9370 		eap->pending_req_sim = 0;
9371 		break;
9372 	case WPA_CTRL_REQ_PSK_PASSPHRASE:
9373 		if (wpa_config_set(ssid, "psk", value, 0) < 0)
9374 			return -1;
9375 		ssid->mem_only_psk = 1;
9376 		if (ssid->passphrase)
9377 			wpa_config_update_psk(ssid);
9378 		if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
9379 			wpa_supplicant_req_scan(wpa_s, 0, 0);
9380 		break;
9381 	case WPA_CTRL_REQ_EXT_CERT_CHECK:
9382 		if (eap->pending_ext_cert_check != PENDING_CHECK)
9383 			return -1;
9384 		if (os_strcmp(value, "good") == 0)
9385 			eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
9386 		else if (os_strcmp(value, "bad") == 0)
9387 			eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
9388 		else
9389 			return -1;
9390 		break;
9391 	default:
9392 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
9393 		return -1;
9394 	}
9395 
9396 	return 0;
9397 #else /* IEEE8021X_EAPOL */
9398 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
9399 	return -1;
9400 #endif /* IEEE8021X_EAPOL */
9401 }
9402 #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
9403 
9404 
wpas_network_disabled(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)9405 int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
9406 {
9407 #ifdef CONFIG_WEP
9408 	int i;
9409 	unsigned int drv_enc;
9410 #endif /* CONFIG_WEP */
9411 
9412 	if (wpa_s->p2p_mgmt)
9413 		return 1; /* no normal network profiles on p2p_mgmt interface */
9414 
9415 	if (ssid == NULL)
9416 		return 1;
9417 
9418 	if (ssid->disabled)
9419 		return 1;
9420 
9421 #ifdef CONFIG_WEP
9422 	if (wpa_s->drv_capa_known)
9423 		drv_enc = wpa_s->drv_enc;
9424 	else
9425 		drv_enc = (unsigned int) -1;
9426 
9427 	for (i = 0; i < NUM_WEP_KEYS; i++) {
9428 		size_t len = ssid->wep_key_len[i];
9429 		if (len == 0)
9430 			continue;
9431 		if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
9432 			continue;
9433 		if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
9434 			continue;
9435 		if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
9436 			continue;
9437 		return 1; /* invalid WEP key */
9438 	}
9439 #endif /* CONFIG_WEP */
9440 
9441 	if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
9442 	    (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
9443 	    !(wpa_key_mgmt_sae(ssid->key_mgmt) &&
9444 	      (ssid->passphrase || ssid->sae_password || ssid->pmk_valid)) &&
9445 	    !ssid->mem_only_psk)
9446 		return 1;
9447 
9448 #ifdef IEEE8021X_EAPOL
9449 #ifdef CRYPTO_RSA_OAEP_SHA256
9450 	if (ssid->eap.imsi_privacy_cert) {
9451 		struct crypto_rsa_key *key;
9452 		bool failed = false;
9453 
9454 		key = crypto_rsa_key_read(ssid->eap.imsi_privacy_cert, false);
9455 		if (!key)
9456 			failed = true;
9457 		crypto_rsa_key_free(key);
9458 		if (failed) {
9459 			wpa_printf(MSG_DEBUG,
9460 				   "Invalid imsi_privacy_cert (%s) - disable network",
9461 				   ssid->eap.imsi_privacy_cert);
9462 			return 1;
9463 		}
9464 	}
9465 #endif /* CRYPTO_RSA_OAEP_SHA256 */
9466 #endif /* IEEE8021X_EAPOL */
9467 
9468 	return 0;
9469 }
9470 
9471 
wpas_get_ssid_pmf(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)9472 int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
9473 {
9474 	if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
9475 		if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
9476 		    !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
9477 			/*
9478 			 * Driver does not support BIP -- ignore pmf=1 default
9479 			 * since the connection with PMF would fail and the
9480 			 * configuration does not require PMF to be enabled.
9481 			 */
9482 			return NO_MGMT_FRAME_PROTECTION;
9483 		}
9484 
9485 		if (ssid &&
9486 		    (ssid->key_mgmt &
9487 		     ~(WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPS |
9488 		       WPA_KEY_MGMT_IEEE8021X_NO_WPA)) == 0) {
9489 			/*
9490 			 * Do not use the default PMF value for non-RSN networks
9491 			 * since PMF is available only with RSN and pmf=2
9492 			 * configuration would otherwise prevent connections to
9493 			 * all open networks.
9494 			 */
9495 			return NO_MGMT_FRAME_PROTECTION;
9496 		}
9497 
9498 #ifdef CONFIG_OCV
9499 		/* Enable PMF if OCV is being enabled */
9500 		if (wpa_s->conf->pmf == NO_MGMT_FRAME_PROTECTION &&
9501 		    ssid && ssid->ocv)
9502 			return MGMT_FRAME_PROTECTION_OPTIONAL;
9503 #endif /* CONFIG_OCV */
9504 
9505 		return wpa_s->conf->pmf;
9506 	}
9507 
9508 	return ssid->ieee80211w;
9509 }
9510 
9511 
9512 #ifdef CONFIG_SAE
9513 
wpas_get_ssid_sae_pwe(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)9514 enum sae_pwe wpas_get_ssid_sae_pwe(struct wpa_supplicant *wpa_s,
9515 				   struct wpa_ssid *ssid)
9516 {
9517 	if (!ssid || ssid->sae_pwe == DEFAULT_SAE_PWE)
9518 		return wpa_s->conf->sae_pwe;
9519 	return ssid->sae_pwe;
9520 }
9521 
9522 
wpas_is_sae_avoided(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const struct wpa_ie_data * ie)9523 bool wpas_is_sae_avoided(struct wpa_supplicant *wpa_s,
9524 			 struct wpa_ssid *ssid,
9525 			 const struct wpa_ie_data *ie)
9526 {
9527 	return wpa_s->conf->sae_check_mfp &&
9528 		(!(ie->capabilities &
9529 		   (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) ||
9530 		 wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION);
9531 }
9532 
9533 #endif /* CONFIG_SAE */
9534 
9535 
pmf_in_use(struct wpa_supplicant * wpa_s,const u8 * addr)9536 int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
9537 {
9538 	if (wpa_s->current_ssid == NULL ||
9539 	    wpa_s->wpa_state < WPA_4WAY_HANDSHAKE)
9540 		return 0;
9541 	if (wpa_s->valid_links) {
9542 		if (!ether_addr_equal(addr, wpa_s->ap_mld_addr) &&
9543 		    !wpas_ap_link_address(wpa_s, addr))
9544 			return 0;
9545 	} else {
9546 		if (!ether_addr_equal(addr, wpa_s->bssid))
9547 			return 0;
9548 	}
9549 	return wpa_sm_pmf_enabled(wpa_s->wpa);
9550 }
9551 
9552 
wpas_is_p2p_prioritized(struct wpa_supplicant * wpa_s)9553 int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
9554 {
9555 	if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
9556 		return 1;
9557 	if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
9558 		return 0;
9559 	return -1;
9560 }
9561 
9562 
wpas_auth_failed(struct wpa_supplicant * wpa_s,const char * reason,const u8 * bssid)9563 void wpas_auth_failed(struct wpa_supplicant *wpa_s, const char *reason,
9564 		      const u8 *bssid)
9565 {
9566 	struct wpa_ssid *ssid = wpa_s->current_ssid;
9567 	int dur;
9568 	struct os_reltime now;
9569 
9570 	if (ssid == NULL) {
9571 		wpa_printf(MSG_DEBUG, "Authentication failure but no known "
9572 			   "SSID block");
9573 		return;
9574 	}
9575 
9576 	if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
9577 		return;
9578 
9579 	ssid->auth_failures++;
9580 
9581 #ifdef CONFIG_P2P
9582 	if (ssid->p2p_group &&
9583 	    (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
9584 		/*
9585 		 * Skip the wait time since there is a short timeout on the
9586 		 * connection to a P2P group.
9587 		 */
9588 		return;
9589 	}
9590 #endif /* CONFIG_P2P */
9591 
9592 	if (ssid->auth_failures > 50)
9593 		dur = 300;
9594 	else if (ssid->auth_failures > 10)
9595 		dur = 120;
9596 	else if (ssid->auth_failures > 5)
9597 		dur = 90;
9598 	else if (ssid->auth_failures > 3)
9599 		dur = 60;
9600 	else if (ssid->auth_failures > 2)
9601 		dur = 30;
9602 	else if (ssid->auth_failures > 1)
9603 		dur = 20;
9604 	else
9605 		dur = 10;
9606 
9607 	if (ssid->auth_failures > 1 &&
9608 	    wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
9609 		dur += os_random() % (ssid->auth_failures * 10);
9610 
9611 	os_get_reltime(&now);
9612 	if (now.sec + dur <= ssid->disabled_until.sec)
9613 		return;
9614 
9615 	ssid->disabled_until.sec = now.sec + dur;
9616 
9617 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
9618 		"id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
9619 		ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
9620 		ssid->auth_failures, dur, reason);
9621 
9622 	if (bssid)
9623 		os_memcpy(ssid->disabled_due_to, bssid, ETH_ALEN);
9624 }
9625 
9626 
wpas_clear_temp_disabled(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,int clear_failures)9627 void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
9628 			      struct wpa_ssid *ssid, int clear_failures)
9629 {
9630 	if (ssid == NULL)
9631 		return;
9632 
9633 	if (ssid->disabled_until.sec) {
9634 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
9635 			"id=%d ssid=\"%s\"",
9636 			ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
9637 	}
9638 	ssid->disabled_until.sec = 0;
9639 	ssid->disabled_until.usec = 0;
9640 	if (clear_failures) {
9641 		ssid->auth_failures = 0;
9642 	} else if (!is_zero_ether_addr(ssid->disabled_due_to)) {
9643 		wpa_printf(MSG_DEBUG, "Mark BSSID " MACSTR
9644 			   " ignored to allow a lower priority BSS, if any, to be tried next",
9645 			   MAC2STR(ssid->disabled_due_to));
9646 		wpa_bssid_ignore_add(wpa_s, ssid->disabled_due_to);
9647 		os_memset(ssid->disabled_due_to, 0, ETH_ALEN);
9648 	}
9649 }
9650 
9651 
disallowed_bssid(struct wpa_supplicant * wpa_s,const u8 * bssid)9652 int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
9653 {
9654 	size_t i;
9655 
9656 	if (wpa_s->disallow_aps_bssid == NULL)
9657 		return 0;
9658 
9659 	for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
9660 		if (ether_addr_equal(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
9661 				     bssid))
9662 			return 1;
9663 	}
9664 
9665 	return 0;
9666 }
9667 
9668 
disallowed_ssid(struct wpa_supplicant * wpa_s,const u8 * ssid,size_t ssid_len)9669 int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
9670 		    size_t ssid_len)
9671 {
9672 	size_t i;
9673 
9674 	if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
9675 		return 0;
9676 
9677 	for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
9678 		struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
9679 		if (ssid_len == s->ssid_len &&
9680 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
9681 			return 1;
9682 	}
9683 
9684 	return 0;
9685 }
9686 
9687 
9688 /**
9689  * wpas_request_connection - Request a new connection
9690  * @wpa_s: Pointer to the network interface
9691  *
9692  * This function is used to request a new connection to be found. It will mark
9693  * the interface to allow reassociation and request a new scan to find a
9694  * suitable network to connect to.
9695  */
wpas_request_connection(struct wpa_supplicant * wpa_s)9696 void wpas_request_connection(struct wpa_supplicant *wpa_s)
9697 {
9698 	wpa_s->normal_scans = 0;
9699 	wpa_s->scan_req = NORMAL_SCAN_REQ;
9700 	wpa_supplicant_reinit_autoscan(wpa_s);
9701 	wpa_s->disconnected = 0;
9702 	wpa_s->reassociate = 1;
9703 	wpa_s->last_owe_group = 0;
9704 #ifdef CONFIG_PASN
9705 	wpa_pasn_reset(&wpa_s->pasn);
9706 #endif /* CONFIG_PASN */
9707 
9708 	if (wpa_supplicant_fast_associate(wpa_s) != 1)
9709 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9710 	else
9711 		wpa_s->reattach = 0;
9712 }
9713 
9714 
9715 /**
9716  * wpas_request_disconnection - Request disconnection
9717  * @wpa_s: Pointer to the network interface
9718  *
9719  * This function is used to request disconnection from the currently connected
9720  * network. This will stop any ongoing scans and initiate deauthentication.
9721  */
wpas_request_disconnection(struct wpa_supplicant * wpa_s)9722 void wpas_request_disconnection(struct wpa_supplicant *wpa_s)
9723 {
9724 #ifdef CONFIG_SME
9725 	wpa_s->sme.prev_bssid_set = 0;
9726 #endif /* CONFIG_SME */
9727 	wpa_s->reassociate = 0;
9728 	wpa_s->disconnected = 1;
9729 	wpa_supplicant_cancel_sched_scan(wpa_s);
9730 	wpa_supplicant_cancel_scan(wpa_s);
9731 	wpas_abort_ongoing_scan(wpa_s);
9732 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
9733 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
9734 	radio_remove_works(wpa_s, "connect", 0);
9735 	radio_remove_works(wpa_s, "sme-connect", 0);
9736 	wpa_s->roam_in_progress = false;
9737 #ifdef CONFIG_WNM
9738 	wpa_s->bss_trans_mgmt_in_progress = false;
9739 #endif /* CONFIG_WNM */
9740 }
9741 
9742 
dump_freq_data(struct wpa_supplicant * wpa_s,const char * title,struct wpa_used_freq_data * freqs_data,unsigned int len)9743 void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
9744 		    struct wpa_used_freq_data *freqs_data,
9745 		    unsigned int len)
9746 {
9747 	unsigned int i;
9748 
9749 	wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
9750 		len, title);
9751 	for (i = 0; i < len; i++) {
9752 		struct wpa_used_freq_data *cur = &freqs_data[i];
9753 		wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
9754 			i, cur->freq, cur->flags);
9755 	}
9756 }
9757 
9758 
9759 /*
9760  * Find the operating frequencies of any of the virtual interfaces that
9761  * are using the same radio as the current interface, and in addition, get
9762  * information about the interface types that are using the frequency.
9763  */
get_shared_radio_freqs_data(struct wpa_supplicant * wpa_s,struct wpa_used_freq_data * freqs_data,unsigned int len,bool exclude_current)9764 int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
9765 				struct wpa_used_freq_data *freqs_data,
9766 				unsigned int len, bool exclude_current)
9767 {
9768 	struct wpa_supplicant *ifs;
9769 	u8 bssid[ETH_ALEN];
9770 	unsigned int idx = 0, i;
9771 
9772 	wpa_dbg(wpa_s, MSG_DEBUG,
9773 		"Determining shared radio frequencies (max len %u)", len);
9774 	os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
9775 
9776 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9777 			 radio_list) {
9778 		int freqs[MAX_NUM_MLD_LINKS];
9779 		unsigned int j, n_freqs = 0;
9780 
9781 		if (idx == len)
9782 			break;
9783 
9784 		if (exclude_current && ifs == wpa_s)
9785 			continue;
9786 
9787 		if (!ifs->current_ssid ||
9788 		    (!ifs->assoc_freq && !ifs->valid_links))
9789 			continue;
9790 
9791 		if (ifs->current_ssid->mode == WPAS_MODE_AP ||
9792 		    ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
9793 		    ifs->current_ssid->mode == WPAS_MODE_MESH) {
9794 			freqs[n_freqs++] = ifs->current_ssid->frequency;
9795 		} else if (ifs->valid_links) {
9796 			struct driver_sta_mlo_info drv_mlo;
9797 
9798 			os_memset(&drv_mlo, 0, sizeof(drv_mlo));
9799 
9800 			if (wpas_drv_get_sta_mlo_info(ifs, &drv_mlo)) {
9801 				wpa_dbg(wpa_s, MSG_INFO,
9802 					"Failed to get MLO link info");
9803 				continue;
9804 			}
9805 
9806 			if (!drv_mlo.valid_links)
9807 				continue;
9808 
9809 			for_each_link(drv_mlo.valid_links, j) {
9810 				if (!drv_mlo.links[j].freq)
9811 					continue;
9812 
9813 				freqs[n_freqs++] = drv_mlo.links[j].freq;
9814 			}
9815 		} else if (wpa_drv_get_bssid(ifs, bssid) == 0) {
9816 			freqs[n_freqs++] = ifs->assoc_freq;
9817 		} else {
9818 			continue;
9819 		}
9820 
9821 		/* Hold only distinct freqs */
9822 		for (j = 0; j < n_freqs && idx < len; j++) {
9823 			for (i = 0; i < idx; i++)
9824 				if (freqs_data[i].freq == freqs[j])
9825 					break;
9826 
9827 			if (i == idx)
9828 				freqs_data[idx++].freq = freqs[j];
9829 
9830 			if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
9831 				freqs_data[i].flags |=
9832 					ifs->current_ssid->p2p_group ?
9833 					WPA_FREQ_USED_BY_P2P_CLIENT :
9834 					WPA_FREQ_USED_BY_INFRA_STATION;
9835 			}
9836 		}
9837 	}
9838 
9839 	dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
9840 	return idx;
9841 }
9842 
9843 
9844 /*
9845  * Find the operating frequencies of any of the virtual interfaces that
9846  * are using the same radio as the current interface.
9847  */
get_shared_radio_freqs(struct wpa_supplicant * wpa_s,int * freq_array,unsigned int len,bool exclude_current)9848 int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
9849 			   int *freq_array, unsigned int len,
9850 			   bool exclude_current)
9851 {
9852 	struct wpa_used_freq_data *freqs_data;
9853 	int num, i;
9854 
9855 	os_memset(freq_array, 0, sizeof(int) * len);
9856 
9857 	freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
9858 	if (!freqs_data)
9859 		return -1;
9860 
9861 	num = get_shared_radio_freqs_data(wpa_s, freqs_data, len,
9862 					  exclude_current);
9863 	for (i = 0; i < num; i++)
9864 		freq_array[i] = freqs_data[i].freq;
9865 
9866 	os_free(freqs_data);
9867 
9868 	return num;
9869 }
9870 
9871 
9872 struct wpa_supplicant *
wpas_vendor_elem(struct wpa_supplicant * wpa_s,enum wpa_vendor_elem_frame frame)9873 wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
9874 {
9875 	switch (frame) {
9876 #ifdef CONFIG_P2P
9877 	case VENDOR_ELEM_PROBE_REQ_P2P:
9878 	case VENDOR_ELEM_PROBE_RESP_P2P:
9879 	case VENDOR_ELEM_PROBE_RESP_P2P_GO:
9880 	case VENDOR_ELEM_BEACON_P2P_GO:
9881 	case VENDOR_ELEM_P2P_PD_REQ:
9882 	case VENDOR_ELEM_P2P_PD_RESP:
9883 	case VENDOR_ELEM_P2P_GO_NEG_REQ:
9884 	case VENDOR_ELEM_P2P_GO_NEG_RESP:
9885 	case VENDOR_ELEM_P2P_GO_NEG_CONF:
9886 	case VENDOR_ELEM_P2P_INV_REQ:
9887 	case VENDOR_ELEM_P2P_INV_RESP:
9888 	case VENDOR_ELEM_P2P_ASSOC_REQ:
9889 	case VENDOR_ELEM_P2P_ASSOC_RESP:
9890 		return wpa_s->p2pdev;
9891 #endif /* CONFIG_P2P */
9892 	default:
9893 		return wpa_s;
9894 	}
9895 }
9896 
9897 
wpas_vendor_elem_update(struct wpa_supplicant * wpa_s)9898 void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
9899 {
9900 	unsigned int i;
9901 	char buf[30];
9902 
9903 	wpa_printf(MSG_DEBUG, "Update vendor elements");
9904 
9905 	for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
9906 		if (wpa_s->vendor_elem[i]) {
9907 			int res;
9908 
9909 			res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
9910 			if (!os_snprintf_error(sizeof(buf), res)) {
9911 				wpa_hexdump_buf(MSG_DEBUG, buf,
9912 						wpa_s->vendor_elem[i]);
9913 			}
9914 		}
9915 	}
9916 
9917 #ifdef CONFIG_P2P
9918 	if (wpa_s->parent == wpa_s &&
9919 	    wpa_s->global->p2p &&
9920 	    !wpa_s->global->p2p_disabled)
9921 		p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
9922 #endif /* CONFIG_P2P */
9923 }
9924 
9925 
wpas_vendor_elem_remove(struct wpa_supplicant * wpa_s,int frame,const u8 * elem,size_t len)9926 int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
9927 			    const u8 *elem, size_t len)
9928 {
9929 	u8 *ie, *end;
9930 
9931 	ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
9932 	end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
9933 
9934 	for (; ie + 1 < end; ie += 2 + ie[1]) {
9935 		if (ie + len > end)
9936 			break;
9937 		if (os_memcmp(ie, elem, len) != 0)
9938 			continue;
9939 
9940 		if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
9941 			wpabuf_free(wpa_s->vendor_elem[frame]);
9942 			wpa_s->vendor_elem[frame] = NULL;
9943 		} else {
9944 			os_memmove(ie, ie + len, end - (ie + len));
9945 			wpa_s->vendor_elem[frame]->used -= len;
9946 		}
9947 		wpas_vendor_elem_update(wpa_s);
9948 		return 0;
9949 	}
9950 
9951 	return -1;
9952 }
9953 
9954 
get_mode(struct hostapd_hw_modes * modes,u16 num_modes,enum hostapd_hw_mode mode,bool is_6ghz)9955 struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
9956 				   u16 num_modes, enum hostapd_hw_mode mode,
9957 				   bool is_6ghz)
9958 {
9959 	u16 i;
9960 
9961 	if (!modes)
9962 		return NULL;
9963 
9964 	for (i = 0; i < num_modes; i++) {
9965 		if (modes[i].mode != mode ||
9966 		    !modes[i].num_channels || !modes[i].channels)
9967 			continue;
9968 		if (is_6ghz == modes[i].is_6ghz)
9969 			return &modes[i];
9970 	}
9971 
9972 	return NULL;
9973 }
9974 
9975 
get_mode_with_freq(struct hostapd_hw_modes * modes,u16 num_modes,int freq)9976 struct hostapd_hw_modes * get_mode_with_freq(struct hostapd_hw_modes *modes,
9977 					     u16 num_modes, int freq)
9978 {
9979 	int i, j;
9980 
9981 	for (i = 0; i < num_modes; i++) {
9982 		for (j = 0; j < modes[i].num_channels; j++) {
9983 			if (freq == modes[i].channels[j].freq)
9984 				return &modes[i];
9985 		}
9986 	}
9987 
9988 	return NULL;
9989 }
9990 
9991 
9992 static struct
wpas_get_disallowed_bss(struct wpa_supplicant * wpa_s,const u8 * bssid)9993 wpa_bss_tmp_disallowed * wpas_get_disallowed_bss(struct wpa_supplicant *wpa_s,
9994 						 const u8 *bssid)
9995 {
9996 	struct wpa_bss_tmp_disallowed *bss;
9997 
9998 	dl_list_for_each(bss, &wpa_s->bss_tmp_disallowed,
9999 			 struct wpa_bss_tmp_disallowed, list) {
10000 		if (ether_addr_equal(bssid, bss->bssid))
10001 			return bss;
10002 	}
10003 
10004 	return NULL;
10005 }
10006 
10007 
wpa_set_driver_tmp_disallow_list(struct wpa_supplicant * wpa_s)10008 static int wpa_set_driver_tmp_disallow_list(struct wpa_supplicant *wpa_s)
10009 {
10010 	struct wpa_bss_tmp_disallowed *tmp;
10011 	unsigned int num_bssid = 0;
10012 	u8 *bssids;
10013 	int ret;
10014 
10015 	bssids = os_malloc(dl_list_len(&wpa_s->bss_tmp_disallowed) * ETH_ALEN);
10016 	if (!bssids)
10017 		return -1;
10018 	dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
10019 			 struct wpa_bss_tmp_disallowed, list) {
10020 		os_memcpy(&bssids[num_bssid * ETH_ALEN], tmp->bssid,
10021 			  ETH_ALEN);
10022 		num_bssid++;
10023 	}
10024 	ret = wpa_drv_set_bssid_tmp_disallow(wpa_s, num_bssid, bssids);
10025 	os_free(bssids);
10026 	return ret;
10027 }
10028 
10029 
wpa_bss_tmp_disallow_timeout(void * eloop_ctx,void * timeout_ctx)10030 static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx)
10031 {
10032 	struct wpa_supplicant *wpa_s = eloop_ctx;
10033 	struct wpa_bss_tmp_disallowed *tmp, *bss = timeout_ctx;
10034 
10035 	/* Make sure the bss is not already freed */
10036 	dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
10037 			 struct wpa_bss_tmp_disallowed, list) {
10038 		if (bss == tmp) {
10039 			remove_bss_tmp_disallowed_entry(wpa_s, tmp);
10040 			wpa_set_driver_tmp_disallow_list(wpa_s);
10041 			break;
10042 		}
10043 	}
10044 }
10045 
10046 
wpa_bss_tmp_disallow(struct wpa_supplicant * wpa_s,const u8 * bssid,unsigned int sec,int rssi_threshold)10047 void wpa_bss_tmp_disallow(struct wpa_supplicant *wpa_s, const u8 *bssid,
10048 			  unsigned int sec, int rssi_threshold)
10049 {
10050 	struct wpa_bss_tmp_disallowed *bss;
10051 
10052 	bss = wpas_get_disallowed_bss(wpa_s, bssid);
10053 	if (bss) {
10054 		eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
10055 		goto finish;
10056 	}
10057 
10058 	bss = os_malloc(sizeof(*bss));
10059 	if (!bss) {
10060 		wpa_printf(MSG_DEBUG,
10061 			   "Failed to allocate memory for temp disallow BSS");
10062 		return;
10063 	}
10064 
10065 	os_memcpy(bss->bssid, bssid, ETH_ALEN);
10066 	dl_list_add(&wpa_s->bss_tmp_disallowed, &bss->list);
10067 	wpa_set_driver_tmp_disallow_list(wpa_s);
10068 
10069 finish:
10070 	bss->rssi_threshold = rssi_threshold;
10071 	eloop_register_timeout(sec, 0, wpa_bss_tmp_disallow_timeout,
10072 			       wpa_s, bss);
10073 }
10074 
10075 
wpa_is_bss_tmp_disallowed(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)10076 int wpa_is_bss_tmp_disallowed(struct wpa_supplicant *wpa_s,
10077 			      struct wpa_bss *bss)
10078 {
10079 	struct wpa_bss_tmp_disallowed *disallowed = NULL, *tmp, *prev;
10080 
10081 	dl_list_for_each_safe(tmp, prev, &wpa_s->bss_tmp_disallowed,
10082 			 struct wpa_bss_tmp_disallowed, list) {
10083 		if (ether_addr_equal(bss->bssid, tmp->bssid)) {
10084 			disallowed = tmp;
10085 			break;
10086 		}
10087 	}
10088 	if (!disallowed)
10089 		return 0;
10090 
10091 	if (disallowed->rssi_threshold != 0 &&
10092 	    bss->level > disallowed->rssi_threshold) {
10093 		remove_bss_tmp_disallowed_entry(wpa_s, disallowed);
10094 		wpa_set_driver_tmp_disallow_list(wpa_s);
10095 		return 0;
10096 	}
10097 
10098 	return 1;
10099 }
10100 
10101 
wpas_enable_mac_addr_randomization(struct wpa_supplicant * wpa_s,unsigned int type,const u8 * addr,const u8 * mask)10102 int wpas_enable_mac_addr_randomization(struct wpa_supplicant *wpa_s,
10103 				       unsigned int type, const u8 *addr,
10104 				       const u8 *mask)
10105 {
10106 	if ((addr && !mask) || (!addr && mask)) {
10107 		wpa_printf(MSG_INFO,
10108 			   "MAC_ADDR_RAND_SCAN invalid addr/mask combination");
10109 		return -1;
10110 	}
10111 
10112 	if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
10113 		wpa_printf(MSG_INFO,
10114 			   "MAC_ADDR_RAND_SCAN cannot allow multicast address");
10115 		return -1;
10116 	}
10117 
10118 	if (type & MAC_ADDR_RAND_SCAN) {
10119 		if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
10120 						addr, mask))
10121 			return -1;
10122 	}
10123 
10124 	if (type & MAC_ADDR_RAND_SCHED_SCAN) {
10125 		if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
10126 						addr, mask))
10127 			return -1;
10128 
10129 		if (wpa_s->sched_scanning && !wpa_s->pno)
10130 			wpas_scan_restart_sched_scan(wpa_s);
10131 	}
10132 
10133 	if (type & MAC_ADDR_RAND_PNO) {
10134 		if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
10135 						addr, mask))
10136 			return -1;
10137 
10138 		if (wpa_s->pno) {
10139 			wpas_stop_pno(wpa_s);
10140 			wpas_start_pno(wpa_s);
10141 		}
10142 	}
10143 
10144 	return 0;
10145 }
10146 
10147 
wpas_disable_mac_addr_randomization(struct wpa_supplicant * wpa_s,unsigned int type)10148 int wpas_disable_mac_addr_randomization(struct wpa_supplicant *wpa_s,
10149 					unsigned int type)
10150 {
10151 	wpas_mac_addr_rand_scan_clear(wpa_s, type);
10152 	if (wpa_s->pno) {
10153 		if (type & MAC_ADDR_RAND_PNO) {
10154 			wpas_stop_pno(wpa_s);
10155 			wpas_start_pno(wpa_s);
10156 		}
10157 	} else if (wpa_s->sched_scanning && (type & MAC_ADDR_RAND_SCHED_SCAN)) {
10158 		wpas_scan_restart_sched_scan(wpa_s);
10159 	}
10160 
10161 	return 0;
10162 }
10163 
10164 
wpa_drv_signal_poll(struct wpa_supplicant * wpa_s,struct wpa_signal_info * si)10165 int wpa_drv_signal_poll(struct wpa_supplicant *wpa_s,
10166 			struct wpa_signal_info *si)
10167 {
10168 	int res;
10169 
10170 	if (!wpa_s->driver->signal_poll)
10171 		return -1;
10172 
10173 	res = wpa_s->driver->signal_poll(wpa_s->drv_priv, si);
10174 
10175 #ifdef CONFIG_TESTING_OPTIONS
10176 	if (res == 0) {
10177 		struct driver_signal_override *dso;
10178 
10179 		dl_list_for_each(dso, &wpa_s->drv_signal_override,
10180 				 struct driver_signal_override, list) {
10181 			if (!ether_addr_equal(wpa_s->bssid, dso->bssid))
10182 				continue;
10183 			wpa_printf(MSG_DEBUG,
10184 				   "Override driver signal_poll information: current_signal: %d->%d avg_signal: %d->%d avg_beacon_signal: %d->%d current_noise: %d->%d",
10185 				   si->data.signal,
10186 				   dso->si_current_signal,
10187 				   si->data.avg_signal,
10188 				   dso->si_avg_signal,
10189 				   si->data.avg_beacon_signal,
10190 				   dso->si_avg_beacon_signal,
10191 				   si->current_noise,
10192 				   dso->si_current_noise);
10193 			si->data.signal = dso->si_current_signal;
10194 			si->data.avg_signal = dso->si_avg_signal;
10195 			si->data.avg_beacon_signal = dso->si_avg_beacon_signal;
10196 			si->current_noise = dso->si_current_noise;
10197 			break;
10198 		}
10199 	}
10200 #endif /* CONFIG_TESTING_OPTIONS */
10201 
10202 	return res;
10203 }
10204 
10205 
10206 struct wpa_scan_results *
wpa_drv_get_scan_results(struct wpa_supplicant * wpa_s,const u8 * bssid)10207 wpa_drv_get_scan_results(struct wpa_supplicant *wpa_s, const u8 *bssid)
10208 {
10209 	struct wpa_scan_results *scan_res;
10210 #ifdef CONFIG_TESTING_OPTIONS
10211 	size_t idx;
10212 #endif /* CONFIG_TESTING_OPTIONS */
10213 
10214 	if (wpa_s->driver->get_scan_results)
10215 		scan_res = wpa_s->driver->get_scan_results(wpa_s->drv_priv,
10216 							   bssid);
10217 	else if (wpa_s->driver->get_scan_results2)
10218 		scan_res = wpa_s->driver->get_scan_results2(wpa_s->drv_priv);
10219 	else
10220 		return NULL;
10221 
10222 
10223 #ifdef CONFIG_TESTING_OPTIONS
10224 	for (idx = 0; scan_res && idx < scan_res->num; idx++) {
10225 		struct driver_signal_override *dso;
10226 		struct wpa_scan_res *res = scan_res->res[idx];
10227 
10228 		dl_list_for_each(dso, &wpa_s->drv_signal_override,
10229 				 struct driver_signal_override, list) {
10230 			if (!ether_addr_equal(res->bssid, dso->bssid))
10231 				continue;
10232 			wpa_printf(MSG_DEBUG,
10233 				   "Override driver scan signal level %d->%d for "
10234 				   MACSTR,
10235 				   res->level, dso->scan_level,
10236 				   MAC2STR(res->bssid));
10237 			res->flags |= WPA_SCAN_QUAL_INVALID;
10238 			if (dso->scan_level < 0)
10239 				res->flags |= WPA_SCAN_LEVEL_DBM;
10240 			else
10241 				res->flags &= ~WPA_SCAN_LEVEL_DBM;
10242 			res->level = dso->scan_level;
10243 			break;
10244 		}
10245 	}
10246 #endif /* CONFIG_TESTING_OPTIONS */
10247 
10248 	return scan_res;
10249 }
10250 
10251 
wpas_ap_link_address(struct wpa_supplicant * wpa_s,const u8 * addr)10252 bool wpas_ap_link_address(struct wpa_supplicant *wpa_s, const u8 *addr)
10253 {
10254 	int i;
10255 
10256 	if (!wpa_s->valid_links)
10257 		return false;
10258 
10259 	for_each_link(wpa_s->valid_links, i) {
10260 		if (ether_addr_equal(wpa_s->links[i].bssid, addr))
10261 			return true;
10262 	}
10263 
10264 	return false;
10265 }
10266 
10267 
wpa_drv_send_action(struct wpa_supplicant * wpa_s,unsigned int freq,unsigned int wait,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,int no_cck)10268 int wpa_drv_send_action(struct wpa_supplicant *wpa_s, unsigned int freq,
10269 			unsigned int wait, const u8 *dst, const u8 *src,
10270 			const u8 *bssid, const u8 *data, size_t data_len,
10271 			int no_cck)
10272 {
10273 	if (!wpa_s->driver->send_action)
10274 		return -1;
10275 
10276 	if (data_len > 0 && data[0] != WLAN_ACTION_PUBLIC) {
10277 		if (wpas_ap_link_address(wpa_s, dst))
10278 			dst = wpa_s->ap_mld_addr;
10279 
10280 		if (wpas_ap_link_address(wpa_s, bssid))
10281 			bssid = wpa_s->ap_mld_addr;
10282 	}
10283 
10284 	return wpa_s->driver->send_action(wpa_s->drv_priv, freq, wait, dst, src,
10285 					  bssid, data, data_len, no_cck, -1);
10286 }
10287 
10288 
wpas_is_6ghz_supported(struct wpa_supplicant * wpa_s,bool only_enabled)10289 bool wpas_is_6ghz_supported(struct wpa_supplicant *wpa_s, bool only_enabled)
10290 {
10291 	struct hostapd_channel_data *chnl;
10292 	int i, j;
10293 
10294 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
10295 		if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
10296 			chnl = wpa_s->hw.modes[i].channels;
10297 			for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
10298 				if (only_enabled &&
10299 				    (chnl[j].flag & HOSTAPD_CHAN_DISABLED))
10300 					continue;
10301 				if (is_6ghz_freq(chnl[j].freq))
10302 					return true;
10303 			}
10304 		}
10305 	}
10306 
10307 	return false;
10308 }
10309 
10310 
wpas_ap_supports_rsn_overriding(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)10311 bool wpas_ap_supports_rsn_overriding(struct wpa_supplicant *wpa_s,
10312 				     struct wpa_bss *bss)
10313 {
10314 	int i;
10315 
10316 	if (!bss)
10317 		return false;
10318 	if (wpa_bss_get_vendor_ie(bss, RSNE_OVERRIDE_IE_VENDOR_TYPE) ||
10319 	    wpa_bss_get_vendor_ie(bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE))
10320 		return true;
10321 
10322 	if (!wpa_s->valid_links)
10323 		return false;
10324 
10325 	for_each_link(wpa_s->valid_links, i) {
10326 		if (wpa_s->links[i].bss &&
10327 		    (wpa_bss_get_vendor_ie(wpa_s->links[i].bss,
10328 					   RSNE_OVERRIDE_IE_VENDOR_TYPE) ||
10329 		     wpa_bss_get_vendor_ie(wpa_s->links[i].bss,
10330 					   RSNE_OVERRIDE_2_IE_VENDOR_TYPE)))
10331 			return true;
10332 	}
10333 
10334 	return false;
10335 }
10336 
10337 
wpas_ap_supports_rsn_overriding_2(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)10338 bool wpas_ap_supports_rsn_overriding_2(struct wpa_supplicant *wpa_s,
10339 				       struct wpa_bss *bss)
10340 {
10341 	int i;
10342 
10343 	if (!bss)
10344 		return false;
10345 	if (wpa_bss_get_vendor_ie(bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE))
10346 		return true;
10347 
10348 	if (!wpa_s->valid_links)
10349 		return false;
10350 
10351 	for_each_link(wpa_s->valid_links, i) {
10352 		if (wpa_s->links[i].bss &&
10353 		    wpa_bss_get_vendor_ie(wpa_s->links[i].bss,
10354 					  RSNE_OVERRIDE_2_IE_VENDOR_TYPE))
10355 			return true;
10356 	}
10357 
10358 	return false;
10359 }
10360 
10361 
wpas_get_owe_trans_network(const u8 * owe_ie,const u8 ** bssid,const u8 ** ssid,size_t * ssid_len)10362 int wpas_get_owe_trans_network(const u8 *owe_ie, const u8 **bssid,
10363 			       const u8 **ssid, size_t *ssid_len)
10364 {
10365 #ifdef CONFIG_OWE
10366 	const u8 *pos, *end;
10367 	u8 ssid_len_tmp;
10368 
10369 	if (!owe_ie)
10370 		return -1;
10371 
10372 	pos = owe_ie + 6;
10373 	end = owe_ie + 2 + owe_ie[1];
10374 
10375 	if (end - pos < ETH_ALEN + 1)
10376 		return -1;
10377 	*bssid = pos;
10378 	pos += ETH_ALEN;
10379 	ssid_len_tmp = *pos++;
10380 	if (end - pos < ssid_len_tmp || ssid_len_tmp > SSID_MAX_LEN)
10381 		return -1;
10382 
10383 	*ssid = pos;
10384 	*ssid_len = ssid_len_tmp;
10385 
10386 	return 0;
10387 #else /* CONFIG_OWE */
10388 	return -1;
10389 #endif /* CONFIG_OWE */
10390 }
10391 
10392 
wpas_update_dfs_ap_info(struct wpa_supplicant * wpa_s,int freq,enum chan_width ap_ch_width,bool disconnect_evt)10393 void wpas_update_dfs_ap_info(struct wpa_supplicant *wpa_s, int freq,
10394 			     enum chan_width ap_ch_width,
10395 			     bool disconnect_evt)
10396 {
10397 	if (disconnect_evt) {
10398 		wpa_printf(MSG_DEBUG, "Disconnect event of DFS AP");
10399 		wpa_s->sta_connected_freq = 0;
10400 		wpa_s->sta_connected_chan_width = CHAN_WIDTH_UNKNOWN;
10401 	} else {
10402 		wpa_s->sta_connected_freq = freq;
10403 		wpa_s->sta_connected_chan_width = ap_ch_width;
10404 	}
10405 	wpa_s->dfs_ap_connected = !disconnect_evt;
10406 
10407 #ifdef CONFIG_P2P
10408 	if (wpa_s->global->p2p)
10409 		p2p_update_dfs_ap_info(wpa_s->global->p2p, freq, ap_ch_width,
10410 				       disconnect_evt);
10411 #endif /* CONFIG_P2P */
10412 }
10413 
10414 
wpas_configure_frame_filters(struct wpa_supplicant * wpa_s)10415 void wpas_configure_frame_filters(struct wpa_supplicant *wpa_s)
10416 {
10417 	struct wpa_bss *bss = wpa_s->current_bss;
10418 	u32 filter = 0;
10419 	bool hs20, proxy_arp_capa;
10420 
10421 	if (!bss)
10422 		return;
10423 
10424 #ifdef CONFIG_HS20
10425 	hs20 = is_hs20_network(wpa_s, wpa_s->current_ssid, bss);
10426 #else /* CONFIG_HS20 */
10427 	hs20 = false;
10428 #endif /* CONFIG_HS20 */
10429 
10430 	if (wpa_s->current_ssid &&
10431 	    wpa_s->current_ssid->drop_unicast_ip_in_l2_multicast) {
10432 		filter |= WPA_DATA_FRAME_FILTER_FLAG_GTK;
10433 	} else if (!hs20) {
10434 		/* Not configuring frame filtering - BSS is not a Hotspot 2.0
10435 		 * network */
10436 		return;
10437 	} else {
10438 #ifdef CONFIG_HS20
10439 		const u8 *ie;
10440 
10441 		ie = wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE);
10442 
10443 		/* Check if DGAF disabled bit is zero (5th byte in the IE) */
10444 		if (!ie || ie[1] < 5)
10445 			wpa_printf(MSG_DEBUG,
10446 				   "Not configuring frame filtering - Can't extract DGAF bit");
10447 		else if (!(ie[6] & HS20_DGAF_DISABLED))
10448 			filter |= WPA_DATA_FRAME_FILTER_FLAG_GTK;
10449 #endif /* CONFIG_HS20 */
10450 	}
10451 
10452 	proxy_arp_capa = wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_PROXY_ARP);
10453 
10454 	if ((hs20 && proxy_arp_capa) ||
10455 	    (wpa_s->current_ssid &&
10456 	     wpa_s->current_ssid->always_use_proxy_arp == 2) ||
10457 	    (proxy_arp_capa && wpa_s->current_ssid &&
10458 	     wpa_s->current_ssid->always_use_proxy_arp == 1))
10459 		filter |= WPA_DATA_FRAME_FILTER_FLAG_ARP |
10460 			WPA_DATA_FRAME_FILTER_FLAG_NA;
10461 
10462 	wpa_drv_configure_frame_filters(wpa_s, filter);
10463 }
10464