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