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