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