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