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