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