blob: d9291f487c8d8db13c87a9b545afc4e86a9a12e3 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant
Hai Shalomc1a21442022-02-04 13:43:00 -08003 * Copyright (c) 2003-2022, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This file implements functions for registering and unregistering
9 * %wpa_supplicant interfaces. In addition, this file contains number of
10 * functions for managing network connections.
11 */
12
13#include "includes.h"
Dmitry Shmidte4663042016-04-04 10:07:49 -070014#ifdef CONFIG_MATCH_IFACE
15#include <net/if.h>
16#include <fnmatch.h>
17#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018
19#include "common.h"
Sunil8cd6f4d2022-06-28 18:40:46 +000020#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "crypto/random.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080022#include "crypto/sha1.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#include "eapol_supp/eapol_supp_sm.h"
24#include "eap_peer/eap.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070025#include "eap_peer/eap_proxy.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "eap_server/eap_methods.h"
27#include "rsn_supp/wpa.h"
28#include "eloop.h"
29#include "config.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070030#include "utils/ext_password.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031#include "l2_packet/l2_packet.h"
32#include "wpa_supplicant_i.h"
33#include "driver_i.h"
34#include "ctrl_iface.h"
35#include "pcsc_funcs.h"
36#include "common/version.h"
37#include "rsn_supp/preauth.h"
38#include "rsn_supp/pmksa_cache.h"
39#include "common/wpa_ctrl.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070040#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "common/ieee802_11_defs.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080042#include "common/hw_features_common.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070043#include "common/gas_server.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070044#include "common/dpp.h"
Hai Shalom60840252021-02-19 19:02:11 -080045#include "common/ptksa_cache.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047#include "fst/fst.h"
Hai Shalom60840252021-02-19 19:02:11 -080048#include "bssid_ignore.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049#include "wpas_glue.h"
50#include "wps_supplicant.h"
51#include "ibss_rsn.h"
52#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080053#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054#include "ap.h"
55#include "p2p_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070056#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057#include "notify.h"
58#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070059#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060#include "bss.h"
61#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080062#include "offchannel.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070063#include "hs20_supplicant.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070064#include "wnm_sta.h"
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070065#include "wpas_kay.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080066#include "mesh.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070067#include "dpp_supplicant.h"
68#ifdef CONFIG_MESH
69#include "ap/ap_config.h"
70#include "ap/hostapd.h"
71#endif /* CONFIG_MESH */
Kiran Kumar Lokerea9f98eb2023-03-17 13:01:12 -070072#include "aidl/aidl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070074const char *const wpa_supplicant_version =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075"wpa_supplicant v" VERSION_STR "\n"
Hai Shalomc1a21442022-02-04 13:43:00 -080076"Copyright (c) 2003-2022, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070078const char *const wpa_supplicant_license =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080079"This software may be distributed under the terms of the BSD license.\n"
80"See README for more details.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081#ifdef EAP_TLS_OPENSSL
82"\nThis product includes software developed by the OpenSSL Project\n"
83"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
84#endif /* EAP_TLS_OPENSSL */
85;
86
87#ifndef CONFIG_NO_STDOUT_DEBUG
88/* Long text divided into parts in order to fit in C89 strings size limits. */
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070089const char *const wpa_supplicant_full_license1 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080090"";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070091const char *const wpa_supplicant_full_license2 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080092"This software may be distributed under the terms of the BSD license.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070093"\n"
94"Redistribution and use in source and binary forms, with or without\n"
95"modification, are permitted provided that the following conditions are\n"
96"met:\n"
97"\n";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070098const char *const wpa_supplicant_full_license3 =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099"1. Redistributions of source code must retain the above copyright\n"
100" notice, this list of conditions and the following disclaimer.\n"
101"\n"
102"2. Redistributions in binary form must reproduce the above copyright\n"
103" notice, this list of conditions and the following disclaimer in the\n"
104" documentation and/or other materials provided with the distribution.\n"
105"\n";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700106const char *const wpa_supplicant_full_license4 =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
108" names of its contributors may be used to endorse or promote products\n"
109" derived from this software without specific prior written permission.\n"
110"\n"
111"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
112"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
113"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
114"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700115const char *const wpa_supplicant_full_license5 =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
117"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
118"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
119"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
120"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
121"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
122"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
123"\n";
124#endif /* CONFIG_NO_STDOUT_DEBUG */
125
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700126
127static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx);
128#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
129static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s);
130#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
Hai Shalomc3565922019-10-28 11:58:20 -0700131#ifdef CONFIG_OWE
132static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s);
133#endif /* CONFIG_OWE */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700134
135
Hai Shalomfdcde762020-04-02 11:19:20 -0700136#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137/* Configure default/group WEP keys for static WEP */
138int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
139{
140 int i, set = 0;
141
142 for (i = 0; i < NUM_WEP_KEYS; i++) {
143 if (ssid->wep_key_len[i] == 0)
144 continue;
145
146 set = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000147 wpa_drv_set_key(wpa_s, -1, WPA_ALG_WEP, NULL,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148 i, i == ssid->wep_tx_keyidx, NULL, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -0700149 ssid->wep_key[i], ssid->wep_key_len[i],
150 i == ssid->wep_tx_keyidx ?
151 KEY_FLAG_GROUP_RX_TX_DEFAULT :
152 KEY_FLAG_GROUP_RX_TX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 }
154
155 return set;
156}
Hai Shalomfdcde762020-04-02 11:19:20 -0700157#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700160int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
161 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162{
163 u8 key[32];
164 size_t keylen;
165 enum wpa_alg alg;
166 u8 seq[6] = { 0 };
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800167 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
169 /* IBSS/WPA-None uses only one key (Group) for both receiving and
170 * sending unicast and multicast packets. */
171
172 if (ssid->mode != WPAS_MODE_IBSS) {
173 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
174 "IBSS/ad-hoc) for WPA-None", ssid->mode);
175 return -1;
176 }
177
178 if (!ssid->psk_set) {
179 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
180 "WPA-None");
181 return -1;
182 }
183
184 switch (wpa_s->group_cipher) {
185 case WPA_CIPHER_CCMP:
186 os_memcpy(key, ssid->psk, 16);
187 keylen = 16;
188 alg = WPA_ALG_CCMP;
189 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700190 case WPA_CIPHER_GCMP:
191 os_memcpy(key, ssid->psk, 16);
192 keylen = 16;
193 alg = WPA_ALG_GCMP;
194 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 case WPA_CIPHER_TKIP:
196 /* WPA-None uses the same Michael MIC key for both TX and RX */
197 os_memcpy(key, ssid->psk, 16 + 8);
198 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
199 keylen = 32;
200 alg = WPA_ALG_TKIP;
201 break;
202 default:
203 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
204 "WPA-None", wpa_s->group_cipher);
205 return -1;
206 }
207
208 /* TODO: should actually remember the previously used seq#, both for TX
209 * and RX from each STA.. */
210
Sunil Ravi77d572f2023-01-17 23:58:31 +0000211 ret = wpa_drv_set_key(wpa_s, -1, alg, NULL, 0, 1, seq, 6, key, keylen,
Hai Shalomfdcde762020-04-02 11:19:20 -0700212 KEY_FLAG_GROUP_RX_TX_DEFAULT);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800213 os_memset(key, 0, sizeof(key));
214 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215}
216
217
218static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
219{
220 struct wpa_supplicant *wpa_s = eloop_ctx;
221 const u8 *bssid = wpa_s->bssid;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700222 if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
223 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
224 wpa_s->wpa_state == WPA_ASSOCIATING))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 bssid = wpa_s->pending_bssid;
226 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
227 MAC2STR(bssid));
Hai Shalom60840252021-02-19 19:02:11 -0800228 wpa_bssid_ignore_add(wpa_s, bssid);
Roshan Pius92cbe442020-10-08 16:15:13 -0700229 wpas_notify_auth_timeout(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800231 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 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 */
251void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
252 int sec, int usec)
253{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700254 if (wpa_s->conf->ap_scan == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 (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);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700261 wpa_s->last_auth_timeout_sec = sec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
263}
264
265
Roshan Pius3a1667e2018-07-03 15:17:14 -0700266/*
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 */
271void 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285/**
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 */
293void 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);
Hai Shalom60840252021-02-19 19:02:11 -0800297 wpa_bssid_ignore_del(wpa_s, wpa_s->bssid);
Hai Shalomce48b4a2018-09-05 11:41:35 -0700298 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301}
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 */
311void 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);
Hai Shalome21d4e82020-04-29 16:34:06 -0700326 eapol_sm_notify_eap_success(wpa_s->eapol, true);
327 eapol_sm_notify_eap_fail(wpa_s->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 return;
329 }
330#endif /* CONFIG_IBSS_RSN */
331
Hai Shalome21d4e82020-04-29 16:34:06 -0700332 eapol_sm_notify_eap_success(wpa_s->eapol, false);
333 eapol_sm_notify_eap_fail(wpa_s->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334
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
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700353 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 eapol_conf.required_keys = 0;
355 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700356 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 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;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700362 eapol_conf.external_sim = wpa_s->conf->external_sim;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800363
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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700382
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800383#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 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800389#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390}
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 */
402void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
403 struct wpa_ssid *ssid)
404{
Hai Shalomfdcde762020-04-02 11:19:20 -0700405#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 int i;
Hai Shalomfdcde762020-04-02 11:19:20 -0700407#endif /* CONFIG_WEP */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000408 struct wpa_sm_mlo mlo;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409
410 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
411 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
412 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
413 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
414 else
415 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
416 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
417 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
Hai Shalomc3565922019-10-28 11:58:20 -0700418 wpa_sm_set_ap_rsnxe(wpa_s->wpa, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
Hai Shalomc3565922019-10-28 11:58:20 -0700420 wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
421 wpa_s->rsnxe_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
423 wpa_s->group_cipher = WPA_CIPHER_NONE;
424 wpa_s->mgmt_group_cipher = 0;
425
Hai Shalomfdcde762020-04-02 11:19:20 -0700426#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 for (i = 0; i < NUM_WEP_KEYS; i++) {
428 if (ssid->wep_key_len[i] > 5) {
429 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
430 wpa_s->group_cipher = WPA_CIPHER_WEP104;
431 break;
432 } else if (ssid->wep_key_len[i] > 0) {
433 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
434 wpa_s->group_cipher = WPA_CIPHER_WEP40;
435 break;
436 }
437 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700438#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439
440 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
441 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
442 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
443 wpa_s->pairwise_cipher);
444 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
446 wpa_s->mgmt_group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447
448 pmksa_cache_clear_current(wpa_s->wpa);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000449 os_memset(&mlo, 0, sizeof(mlo));
450 wpa_sm_set_mlo_params(wpa_s->wpa, &mlo);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451}
452
453
Dmitry Shmidt04949592012-07-19 12:16:46 -0700454void free_hw_features(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800455{
456 int i;
457 if (wpa_s->hw.modes == NULL)
458 return;
459
460 for (i = 0; i < wpa_s->hw.num_modes; i++) {
461 os_free(wpa_s->hw.modes[i].channels);
462 os_free(wpa_s->hw.modes[i].rates);
463 }
464
465 os_free(wpa_s->hw.modes);
466 wpa_s->hw.modes = NULL;
467}
468
469
Hai Shalomc1a21442022-02-04 13:43:00 -0800470static void remove_bss_tmp_disallowed_entry(struct wpa_supplicant *wpa_s,
471 struct wpa_bss_tmp_disallowed *bss)
472{
473 eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
474 dl_list_del(&bss->list);
475 os_free(bss);
476}
477
478
Hai Shalom74f70d42019-02-11 14:42:39 -0800479void free_bss_tmp_disallowed(struct wpa_supplicant *wpa_s)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800480{
481 struct wpa_bss_tmp_disallowed *bss, *prev;
482
483 dl_list_for_each_safe(bss, prev, &wpa_s->bss_tmp_disallowed,
Hai Shalomc1a21442022-02-04 13:43:00 -0800484 struct wpa_bss_tmp_disallowed, list)
485 remove_bss_tmp_disallowed_entry(wpa_s, bss);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800486}
487
488
Paul Stewart092955c2017-02-06 09:13:09 -0800489void wpas_flush_fils_hlp_req(struct wpa_supplicant *wpa_s)
490{
491 struct fils_hlp_req *req;
492
493 while ((req = dl_list_first(&wpa_s->fils_hlp_req, struct fils_hlp_req,
494 list)) != NULL) {
495 dl_list_del(&req->list);
496 wpabuf_free(req->pkt);
497 os_free(req);
498 }
499}
500
501
Hai Shalomfdcde762020-04-02 11:19:20 -0700502void wpas_clear_disabled_interface(void *eloop_ctx, void *timeout_ctx)
503{
504 struct wpa_supplicant *wpa_s = eloop_ctx;
505
506 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
507 return;
508 wpa_dbg(wpa_s, MSG_DEBUG, "Clear cached state on disabled interface");
509 wpa_bss_flush(wpa_s);
510}
511
512
513#ifdef CONFIG_TESTING_OPTIONS
514void wpas_clear_driver_signal_override(struct wpa_supplicant *wpa_s)
515{
516 struct driver_signal_override *dso;
517
518 while ((dso = dl_list_first(&wpa_s->drv_signal_override,
519 struct driver_signal_override, list))) {
520 dl_list_del(&dso->list);
521 os_free(dso);
522 }
523}
524#endif /* CONFIG_TESTING_OPTIONS */
525
526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
528{
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700529 int i;
530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 bgscan_deinit(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700532 autoscan_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 scard_deinit(wpa_s->scard);
534 wpa_s->scard = NULL;
535 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
536 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
537 l2_packet_deinit(wpa_s->l2);
538 wpa_s->l2 = NULL;
539 if (wpa_s->l2_br) {
540 l2_packet_deinit(wpa_s->l2_br);
541 wpa_s->l2_br = NULL;
542 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800543#ifdef CONFIG_TESTING_OPTIONS
544 l2_packet_deinit(wpa_s->l2_test);
545 wpa_s->l2_test = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800546 os_free(wpa_s->get_pref_freq_list_override);
547 wpa_s->get_pref_freq_list_override = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700548 wpabuf_free(wpa_s->last_assoc_req_wpa_ie);
549 wpa_s->last_assoc_req_wpa_ie = NULL;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800550 os_free(wpa_s->extra_sae_rejected_groups);
551 wpa_s->extra_sae_rejected_groups = NULL;
Hai Shalomfdcde762020-04-02 11:19:20 -0700552 wpabuf_free(wpa_s->rsne_override_eapol);
553 wpa_s->rsne_override_eapol = NULL;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800554 wpabuf_free(wpa_s->rsnxe_override_assoc);
555 wpa_s->rsnxe_override_assoc = NULL;
556 wpabuf_free(wpa_s->rsnxe_override_eapol);
557 wpa_s->rsnxe_override_eapol = NULL;
Hai Shalomfdcde762020-04-02 11:19:20 -0700558 wpas_clear_driver_signal_override(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800559#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 if (wpa_s->conf != NULL) {
562 struct wpa_ssid *ssid;
563 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
564 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 }
566
567 os_free(wpa_s->confname);
568 wpa_s->confname = NULL;
569
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -0700570 os_free(wpa_s->confanother);
571 wpa_s->confanother = NULL;
572
Hai Shalomce48b4a2018-09-05 11:41:35 -0700573 os_free(wpa_s->last_con_fail_realm);
574 wpa_s->last_con_fail_realm = NULL;
575 wpa_s->last_con_fail_realm_len = 0;
576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577 wpa_sm_set_eapol(wpa_s->wpa, NULL);
578 eapol_sm_deinit(wpa_s->eapol);
579 wpa_s->eapol = NULL;
580
581 rsn_preauth_deinit(wpa_s->wpa);
582
583#ifdef CONFIG_TDLS
584 wpa_tdls_deinit(wpa_s->wpa);
585#endif /* CONFIG_TDLS */
586
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800587 wmm_ac_clear_saved_tspecs(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 pmksa_candidate_free(wpa_s->wpa);
Hai Shalom60840252021-02-19 19:02:11 -0800589 ptksa_cache_deinit(wpa_s->ptksa);
590 wpa_s->ptksa = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591 wpa_sm_deinit(wpa_s->wpa);
592 wpa_s->wpa = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800593 wpa_bssid_ignore_clear(wpa_s);
594
595#ifdef CONFIG_PASN
596 wpas_pasn_auth_stop(wpa_s);
597#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598
599 wpa_bss_deinit(wpa_s);
600
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700601 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 wpa_supplicant_cancel_scan(wpa_s);
603 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
605#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
606 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
607 wpa_s, NULL);
608#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700610 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -0700611 eloop_cancel_timeout(wpas_clear_disabled_interface, wpa_s, NULL);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 wpas_wps_deinit(wpa_s);
614
615 wpabuf_free(wpa_s->pending_eapol_rx);
616 wpa_s->pending_eapol_rx = NULL;
617
618#ifdef CONFIG_IBSS_RSN
619 ibss_rsn_deinit(wpa_s->ibss_rsn);
620 wpa_s->ibss_rsn = NULL;
621#endif /* CONFIG_IBSS_RSN */
622
623 sme_deinit(wpa_s);
624
625#ifdef CONFIG_AP
626 wpa_supplicant_ap_deinit(wpa_s);
627#endif /* CONFIG_AP */
628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 wpas_p2p_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800631#ifdef CONFIG_OFFCHANNEL
632 offchannel_deinit(wpa_s);
633#endif /* CONFIG_OFFCHANNEL */
634
635 wpa_supplicant_cancel_sched_scan(wpa_s);
636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 os_free(wpa_s->next_scan_freqs);
638 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800639
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800640 os_free(wpa_s->manual_scan_freqs);
641 wpa_s->manual_scan_freqs = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700642 os_free(wpa_s->select_network_scan_freqs);
643 wpa_s->select_network_scan_freqs = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800644
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700645 os_free(wpa_s->manual_sched_scan_freqs);
646 wpa_s->manual_sched_scan_freqs = NULL;
647
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800648 wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
649
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700650 /*
651 * Need to remove any pending gas-query radio work before the
652 * gas_query_deinit() call because gas_query::work has not yet been set
653 * for works that have not been started. gas_query_free() will be unable
654 * to cancel such pending radio works and once the pending gas-query
655 * radio work eventually gets removed, the deinit notification call to
656 * gas_query_start_cb() would result in dereferencing freed memory.
657 */
658 if (wpa_s->radio)
659 radio_remove_works(wpa_s, "gas-query", 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660 gas_query_deinit(wpa_s->gas);
661 wpa_s->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700662 gas_server_deinit(wpa_s->gas_server);
663 wpa_s->gas_server = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800664
665 free_hw_features(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700666
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700667 ieee802_1x_dealloc_kay_sm(wpa_s);
668
Dmitry Shmidt04949592012-07-19 12:16:46 -0700669 os_free(wpa_s->bssid_filter);
670 wpa_s->bssid_filter = NULL;
671
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800672 os_free(wpa_s->disallow_aps_bssid);
673 wpa_s->disallow_aps_bssid = NULL;
674 os_free(wpa_s->disallow_aps_ssid);
675 wpa_s->disallow_aps_ssid = NULL;
676
Dmitry Shmidt04949592012-07-19 12:16:46 -0700677 wnm_bss_keep_alive_deinit(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700678#ifdef CONFIG_WNM
679 wnm_deallocate_memory(wpa_s);
680#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700681
682 ext_password_deinit(wpa_s->ext_pw);
683 wpa_s->ext_pw = NULL;
684
685 wpabuf_free(wpa_s->last_gas_resp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800686 wpa_s->last_gas_resp = NULL;
687 wpabuf_free(wpa_s->prev_gas_resp);
688 wpa_s->prev_gas_resp = NULL;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700689
690 os_free(wpa_s->last_scan_res);
691 wpa_s->last_scan_res = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800692
693#ifdef CONFIG_HS20
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700694 if (wpa_s->drv_priv)
695 wpa_drv_configure_frame_filters(wpa_s, 0);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700696 hs20_deinit(wpa_s);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800697#endif /* CONFIG_HS20 */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700698
699 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
700 wpabuf_free(wpa_s->vendor_elem[i]);
701 wpa_s->vendor_elem[i] = NULL;
702 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800703
704 wmm_ac_notify_disassoc(wpa_s);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800705
706 wpa_s->sched_scan_plans_num = 0;
707 os_free(wpa_s->sched_scan_plans);
708 wpa_s->sched_scan_plans = NULL;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800709
710#ifdef CONFIG_MBO
711 wpa_s->non_pref_chan_num = 0;
712 os_free(wpa_s->non_pref_chan);
713 wpa_s->non_pref_chan = NULL;
714#endif /* CONFIG_MBO */
715
716 free_bss_tmp_disallowed(wpa_s);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700717
718 wpabuf_free(wpa_s->lci);
719 wpa_s->lci = NULL;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800720 wpas_clear_beacon_rep_data(wpa_s);
Paul Stewart092955c2017-02-06 09:13:09 -0800721
722#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
723#ifdef CONFIG_MESH
724 {
725 struct external_pmksa_cache *entry;
726
727 while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
728 struct external_pmksa_cache,
729 list)) != NULL) {
730 dl_list_del(&entry->list);
731 os_free(entry->pmksa_cache);
732 os_free(entry);
733 }
734 }
735#endif /* CONFIG_MESH */
736#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
737
738 wpas_flush_fils_hlp_req(wpa_s);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800739
740 wpabuf_free(wpa_s->ric_ies);
741 wpa_s->ric_ies = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700742
743#ifdef CONFIG_DPP
744 wpas_dpp_deinit(wpa_s);
Hai Shalom021b0b52019-04-10 11:17:58 -0700745 dpp_global_deinit(wpa_s->dpp);
746 wpa_s->dpp = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700747#endif /* CONFIG_DPP */
Hai Shalom60840252021-02-19 19:02:11 -0800748
749#ifdef CONFIG_PASN
750 wpas_pasn_auth_stop(wpa_s);
751#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -0800752 wpas_scs_deinit(wpa_s);
753 wpas_dscp_deinit(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000754
755#ifdef CONFIG_OWE
756 os_free(wpa_s->owe_trans_scan_freq);
757 wpa_s->owe_trans_scan_freq = NULL;
758#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759}
760
761
762/**
763 * wpa_clear_keys - Clear keys configured for the driver
764 * @wpa_s: Pointer to wpa_supplicant data
765 * @addr: Previously used BSSID or %NULL if not available
766 *
767 * This function clears the encryption keys that has been previously configured
768 * for the driver.
769 */
770void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
771{
Hai Shalomc3565922019-10-28 11:58:20 -0700772 int i, max = 6;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773
774 /* MLME-DELETEKEYS.request */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800775 for (i = 0; i < max; i++) {
776 if (wpa_s->keys_cleared & BIT(i))
777 continue;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000778 wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
Hai Shalomfdcde762020-04-02 11:19:20 -0700779 NULL, 0, KEY_FLAG_GROUP);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800780 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700781 /* Pairwise Key ID 1 for Extended Key ID is tracked in bit 15 */
782 if (~wpa_s->keys_cleared & (BIT(0) | BIT(15)) && addr &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800783 !is_zero_ether_addr(addr)) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700784 if (!(wpa_s->keys_cleared & BIT(0)))
Sunil Ravi77d572f2023-01-17 23:58:31 +0000785 wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, addr, 0, 0,
786 NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
Hai Shalomfdcde762020-04-02 11:19:20 -0700787 if (!(wpa_s->keys_cleared & BIT(15)))
Sunil Ravi77d572f2023-01-17 23:58:31 +0000788 wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, addr, 1, 0,
789 NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 /* MLME-SETPROTECTION.request(None) */
791 wpa_drv_mlme_setprotection(
792 wpa_s, addr,
793 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
794 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
795 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800796 wpa_s->keys_cleared = (u32) -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797}
798
799
800/**
801 * wpa_supplicant_state_txt - Get the connection state name as a text string
802 * @state: State (wpa_state; WPA_*)
803 * Returns: The state name as a printable text string
804 */
805const char * wpa_supplicant_state_txt(enum wpa_states state)
806{
807 switch (state) {
808 case WPA_DISCONNECTED:
809 return "DISCONNECTED";
810 case WPA_INACTIVE:
811 return "INACTIVE";
812 case WPA_INTERFACE_DISABLED:
813 return "INTERFACE_DISABLED";
814 case WPA_SCANNING:
815 return "SCANNING";
816 case WPA_AUTHENTICATING:
817 return "AUTHENTICATING";
818 case WPA_ASSOCIATING:
819 return "ASSOCIATING";
820 case WPA_ASSOCIATED:
821 return "ASSOCIATED";
822 case WPA_4WAY_HANDSHAKE:
823 return "4WAY_HANDSHAKE";
824 case WPA_GROUP_HANDSHAKE:
825 return "GROUP_HANDSHAKE";
826 case WPA_COMPLETED:
827 return "COMPLETED";
828 default:
829 return "UNKNOWN";
830 }
831}
832
833
834#ifdef CONFIG_BGSCAN
835
Hai Shalom899fcc72020-10-19 14:38:18 -0700836static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
837{
838 if (wpa_s->bgscan_ssid) {
839 bgscan_deinit(wpa_s);
840 wpa_s->bgscan_ssid = NULL;
841 }
842}
843
844
845/**
846 * wpa_supplicant_reset_bgscan - Reset the bgscan for the current SSID.
847 * @wpa_s: Pointer to the wpa_supplicant data
848 *
849 * Stop, start, or reconfigure the scan parameters depending on the method.
850 */
851void wpa_supplicant_reset_bgscan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852{
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800853 const char *name;
854
855 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
856 name = wpa_s->current_ssid->bgscan;
857 else
858 name = wpa_s->conf->bgscan;
Hai Shalom899fcc72020-10-19 14:38:18 -0700859 if (!name || name[0] == '\0') {
860 wpa_supplicant_stop_bgscan(wpa_s);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800861 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700862 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800863 if (wpas_driver_bss_selection(wpa_s))
864 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800865#ifdef CONFIG_P2P
866 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
867 return;
868#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869
870 bgscan_deinit(wpa_s);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800871 if (wpa_s->current_ssid) {
872 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
874 "bgscan");
875 /*
876 * Live without bgscan; it is only used as a roaming
877 * optimization, so the initial connection is not
878 * affected.
879 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700880 } else {
881 struct wpa_scan_results *scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 wpa_s->bgscan_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700883 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
884 0);
885 if (scan_res) {
886 bgscan_notify_scan(wpa_s, scan_res);
887 wpa_scan_results_free(scan_res);
888 }
889 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 } else
891 wpa_s->bgscan_ssid = NULL;
892}
893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894#endif /* CONFIG_BGSCAN */
895
896
Dmitry Shmidt04949592012-07-19 12:16:46 -0700897static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
898{
899 if (autoscan_init(wpa_s, 0))
900 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
901}
902
903
904static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
905{
906 autoscan_deinit(wpa_s);
907}
908
909
910void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
911{
912 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
913 wpa_s->wpa_state == WPA_SCANNING) {
914 autoscan_deinit(wpa_s);
915 wpa_supplicant_start_autoscan(wpa_s);
916 }
917}
918
919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920/**
921 * wpa_supplicant_set_state - Set current connection state
922 * @wpa_s: Pointer to wpa_supplicant data
923 * @state: The new connection state
924 *
925 * This function is called whenever the connection state changes, e.g.,
926 * association is completed for WPA/WPA2 4-Way Handshake is started.
927 */
928void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
929 enum wpa_states state)
930{
931 enum wpa_states old_state = wpa_s->wpa_state;
Hai Shalomc3565922019-10-28 11:58:20 -0700932#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
Hai Shalome21d4e82020-04-29 16:34:06 -0700933 bool update_fils_connect_params = false;
Hai Shalomc3565922019-10-28 11:58:20 -0700934#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935
936 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
937 wpa_supplicant_state_txt(wpa_s->wpa_state),
938 wpa_supplicant_state_txt(state));
939
Hai Shalom74f70d42019-02-11 14:42:39 -0800940 if (state == WPA_COMPLETED &&
941 os_reltime_initialized(&wpa_s->roam_start)) {
942 os_reltime_age(&wpa_s->roam_start, &wpa_s->roam_time);
943 wpa_s->roam_start.sec = 0;
944 wpa_s->roam_start.usec = 0;
945 wpas_notify_auth_changed(wpa_s);
946 wpas_notify_roam_time(wpa_s);
947 wpas_notify_roam_complete(wpa_s);
948 } else if (state == WPA_DISCONNECTED &&
949 os_reltime_initialized(&wpa_s->roam_start)) {
950 wpa_s->roam_start.sec = 0;
951 wpa_s->roam_start.usec = 0;
952 wpa_s->roam_time.sec = 0;
953 wpa_s->roam_time.usec = 0;
954 wpas_notify_roam_complete(wpa_s);
955 }
956
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -0800957 if (state == WPA_INTERFACE_DISABLED) {
958 /* Assure normal scan when interface is restored */
959 wpa_s->normal_scans = 0;
960 }
961
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700962 if (state == WPA_COMPLETED) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800963 wpas_connect_work_done(wpa_s);
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700964 /* Reinitialize normal_scan counter */
965 wpa_s->normal_scans = 0;
966 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800967
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700968#ifdef CONFIG_P2P
969 /*
970 * P2PS client has to reply to Probe Request frames received on the
971 * group operating channel. Enable Probe Request frame reporting for
972 * P2P connected client in case p2p_cli_probe configuration property is
973 * set to 1.
974 */
975 if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
976 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
977 wpa_s->current_ssid->p2p_group) {
978 if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
979 wpa_dbg(wpa_s, MSG_DEBUG,
980 "P2P: Enable CLI Probe Request RX reporting");
981 wpa_s->p2p_cli_probe =
982 wpa_drv_probe_req_report(wpa_s, 1) >= 0;
983 } else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
984 wpa_dbg(wpa_s, MSG_DEBUG,
985 "P2P: Disable CLI Probe Request RX reporting");
986 wpa_s->p2p_cli_probe = 0;
987 wpa_drv_probe_req_report(wpa_s, 0);
988 }
989 }
990#endif /* CONFIG_P2P */
991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 if (state != WPA_SCANNING)
993 wpa_supplicant_notify_scanning(wpa_s, 0);
994
995 if (state == WPA_COMPLETED && wpa_s->new_connection) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700997 int fils_hlp_sent = 0;
Sunil Ravi89eba102022-09-13 21:04:37 -0700998 char mld_addr[50];
999
1000 mld_addr[0] = '\0';
1001 if (wpa_s->valid_links)
1002 os_snprintf(mld_addr, sizeof(mld_addr),
1003 " ap_mld_addr=" MACSTR,
1004 MAC2STR(wpa_s->ap_mld_addr));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001005
1006#ifdef CONFIG_SME
1007 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1008 wpa_auth_alg_fils(wpa_s->sme.auth_alg))
1009 fils_hlp_sent = 1;
1010#endif /* CONFIG_SME */
1011 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1012 wpa_auth_alg_fils(wpa_s->auth_alg))
1013 fils_hlp_sent = 1;
1014
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001015#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
Sunil Ravi89eba102022-09-13 21:04:37 -07001017 MACSTR " completed [id=%d id_str=%s%s]%s",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001018 MAC2STR(wpa_s->bssid),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 ssid ? ssid->id : -1,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001020 ssid && ssid->id_str ? ssid->id_str : "",
Sunil Ravi89eba102022-09-13 21:04:37 -07001021 fils_hlp_sent ? " FILS_HLP_SENT" : "", mld_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001023 wpas_clear_temp_disabled(wpa_s, ssid, 1);
Hai Shalom899fcc72020-10-19 14:38:18 -07001024 wpa_s->consecutive_conn_failures = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 wpa_s->new_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 wpa_drv_set_operstate(wpa_s, 1);
1027#ifndef IEEE8021X_EAPOL
1028 wpa_drv_set_supp_port(wpa_s, 1);
1029#endif /* IEEE8021X_EAPOL */
1030 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001031 wpa_s->known_wps_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 wpas_p2p_completed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001033
1034 sme_sched_obss_scan(wpa_s, 1);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001035
1036#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1037 if (!fils_hlp_sent && ssid && ssid->eap.erp)
Hai Shalome21d4e82020-04-29 16:34:06 -07001038 update_fils_connect_params = true;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001039#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
Hai Shalomc3565922019-10-28 11:58:20 -07001040#ifdef CONFIG_OWE
1041 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_OWE))
1042 wpas_update_owe_connect_params(wpa_s);
1043#endif /* CONFIG_OWE */
Hai Shalom2cbbcd12021-03-08 18:33:38 -08001044#ifdef CONFIG_HS20
1045 hs20_configure_frame_filters(wpa_s);
1046#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
1048 state == WPA_ASSOCIATED) {
1049 wpa_s->new_connection = 1;
1050 wpa_drv_set_operstate(wpa_s, 0);
1051#ifndef IEEE8021X_EAPOL
1052 wpa_drv_set_supp_port(wpa_s, 0);
1053#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001054 sme_sched_obss_scan(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 }
1056 wpa_s->wpa_state = state;
1057
1058#ifdef CONFIG_BGSCAN
Hai Shalom899fcc72020-10-19 14:38:18 -07001059 if (state == WPA_COMPLETED && wpa_s->current_ssid != wpa_s->bgscan_ssid)
1060 wpa_supplicant_reset_bgscan(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001061 else if (state < WPA_ASSOCIATED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 wpa_supplicant_stop_bgscan(wpa_s);
1063#endif /* CONFIG_BGSCAN */
1064
Hai Shalom5f92bc92019-04-18 11:54:11 -07001065 if (state > WPA_SCANNING)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001066 wpa_supplicant_stop_autoscan(wpa_s);
1067
1068 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
1069 wpa_supplicant_start_autoscan(wpa_s);
1070
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001071 if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
1072 wmm_ac_notify_disassoc(wpa_s);
1073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 if (wpa_s->wpa_state != old_state) {
1075 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1076
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001077 /*
1078 * Notify the P2P Device interface about a state change in one
1079 * of the interfaces.
1080 */
1081 wpas_p2p_indicate_state_change(wpa_s);
1082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 if (wpa_s->wpa_state == WPA_COMPLETED ||
1084 old_state == WPA_COMPLETED)
1085 wpas_notify_auth_changed(wpa_s);
Hai Shalomc3565922019-10-28 11:58:20 -07001086#ifdef CONFIG_DPP2
1087 if (wpa_s->wpa_state == WPA_COMPLETED)
1088 wpas_dpp_connected(wpa_s);
1089#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 }
Hai Shalomc3565922019-10-28 11:58:20 -07001091#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
1092 if (update_fils_connect_params)
1093 wpas_update_fils_connect_params(wpa_s);
1094#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095}
1096
1097
1098void wpa_supplicant_terminate_proc(struct wpa_global *global)
1099{
1100 int pending = 0;
1101#ifdef CONFIG_WPS
1102 struct wpa_supplicant *wpa_s = global->ifaces;
1103 while (wpa_s) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001104 struct wpa_supplicant *next = wpa_s->next;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001105 if (wpas_wps_terminate_pending(wpa_s) == 1)
1106 pending = 1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001107#ifdef CONFIG_P2P
1108 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
1109 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
1110 wpas_p2p_disconnect(wpa_s);
1111#endif /* CONFIG_P2P */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001112 wpa_s = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 }
1114#endif /* CONFIG_WPS */
1115 if (pending)
1116 return;
1117 eloop_terminate();
1118}
1119
1120
1121static void wpa_supplicant_terminate(int sig, void *signal_ctx)
1122{
1123 struct wpa_global *global = signal_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 wpa_supplicant_terminate_proc(global);
1125}
1126
1127
1128void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
1129{
1130 enum wpa_states old_state = wpa_s->wpa_state;
Hai Shalom60840252021-02-19 19:02:11 -08001131 enum wpa_states new_state;
1132
1133 if (old_state == WPA_SCANNING)
1134 new_state = WPA_SCANNING;
1135 else
1136 new_state = WPA_DISCONNECTED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137
1138 wpa_s->pairwise_cipher = 0;
1139 wpa_s->group_cipher = 0;
1140 wpa_s->mgmt_group_cipher = 0;
1141 wpa_s->key_mgmt = 0;
Sunil Ravi89eba102022-09-13 21:04:37 -07001142 wpa_s->allowed_key_mgmts = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
Hai Shalom60840252021-02-19 19:02:11 -08001144 wpa_supplicant_set_state(wpa_s, new_state);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145
1146 if (wpa_s->wpa_state != old_state)
1147 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1148}
1149
1150
1151/**
1152 * wpa_supplicant_reload_configuration - Reload configuration data
1153 * @wpa_s: Pointer to wpa_supplicant data
1154 * Returns: 0 on success or -1 if configuration parsing failed
1155 *
1156 * This function can be used to request that the configuration data is reloaded
1157 * (e.g., after configuration file change). This function is reloading
1158 * configuration only for one interface, so this may need to be called multiple
1159 * times if %wpa_supplicant is controlling multiple interfaces and all
1160 * interfaces need reconfiguration.
1161 */
1162int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
1163{
1164 struct wpa_config *conf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 int reconf_ctrl;
1166 int old_ap_scan;
1167
1168 if (wpa_s->confname == NULL)
1169 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001170 conf = wpa_config_read(wpa_s->confname, NULL, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 if (conf == NULL) {
1172 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
1173 "file '%s' - exiting", wpa_s->confname);
1174 return -1;
1175 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001176 if (wpa_s->confanother &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00001177 !wpa_config_read(wpa_s->confanother, conf, true)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001178 wpa_msg(wpa_s, MSG_ERROR,
1179 "Failed to parse the configuration file '%s' - exiting",
1180 wpa_s->confanother);
1181 return -1;
1182 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07001183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184 conf->changed_parameters = (unsigned int) -1;
1185
1186 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
1187 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
1188 os_strcmp(conf->ctrl_interface,
1189 wpa_s->conf->ctrl_interface) != 0);
1190
Jouni Malinenf3f8d3c2021-02-05 00:28:17 +02001191 if (reconf_ctrl) {
1192 wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 wpa_s->ctrl_iface = NULL;
1194 }
1195
1196 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 if (wpa_s->current_ssid) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001198 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1199 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001200 wpa_supplicant_deauthenticate(wpa_s,
1201 WLAN_REASON_DEAUTH_LEAVING);
1202 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203
1204 /*
1205 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001206 * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001208 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1209 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
1210 wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 /*
1212 * Clear forced success to clear EAP state for next
1213 * authentication.
1214 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001215 eapol_sm_notify_eap_success(wpa_s->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 }
1217 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1218 wpa_sm_set_config(wpa_s->wpa, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1221 rsn_preauth_deinit(wpa_s->wpa);
1222
1223 old_ap_scan = wpa_s->conf->ap_scan;
1224 wpa_config_free(wpa_s->conf);
1225 wpa_s->conf = conf;
1226 if (old_ap_scan != wpa_s->conf->ap_scan)
1227 wpas_notify_ap_scan_changed(wpa_s);
1228
1229 if (reconf_ctrl)
1230 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1231
1232 wpa_supplicant_update_config(wpa_s);
1233
1234 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001235 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 wpa_s->reassociate = 1;
1237 wpa_supplicant_req_scan(wpa_s, 0, 0);
1238 }
Hai Shalom60840252021-02-19 19:02:11 -08001239 wpa_bssid_ignore_clear(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
1241 return 0;
1242}
1243
1244
1245static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
1246{
1247 struct wpa_global *global = signal_ctx;
1248 struct wpa_supplicant *wpa_s;
1249 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1250 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
1251 sig);
1252 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1253 wpa_supplicant_terminate_proc(global);
1254 }
1255 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001256
1257 if (wpa_debug_reopen_file() < 0) {
1258 /* Ignore errors since we cannot really do much to fix this */
1259 wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
1260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261}
1262
1263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1265 struct wpa_ssid *ssid,
1266 struct wpa_ie_data *ie)
1267{
1268 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1269 if (ret) {
1270 if (ret == -2) {
1271 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1272 "from association info");
1273 }
1274 return -1;
1275 }
1276
1277 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
1278 "cipher suites");
1279 if (!(ie->group_cipher & ssid->group_cipher)) {
1280 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1281 "cipher 0x%x (mask 0x%x) - reject",
1282 ie->group_cipher, ssid->group_cipher);
1283 return -1;
1284 }
1285 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1286 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1287 "cipher 0x%x (mask 0x%x) - reject",
1288 ie->pairwise_cipher, ssid->pairwise_cipher);
1289 return -1;
1290 }
1291 if (!(ie->key_mgmt & ssid->key_mgmt)) {
1292 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1293 "management 0x%x (mask 0x%x) - reject",
1294 ie->key_mgmt, ssid->key_mgmt);
1295 return -1;
1296 }
1297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001299 wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1301 "that does not support management frame protection - "
1302 "reject");
1303 return -1;
1304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305
1306 return 0;
1307}
1308
1309
Hai Shalom021b0b52019-04-10 11:17:58 -07001310static int matching_ciphers(struct wpa_ssid *ssid, struct wpa_ie_data *ie,
1311 int freq)
1312{
1313 if (!ie->has_group)
1314 ie->group_cipher = wpa_default_rsn_cipher(freq);
1315 if (!ie->has_pairwise)
1316 ie->pairwise_cipher = wpa_default_rsn_cipher(freq);
1317 return (ie->group_cipher & ssid->group_cipher) &&
1318 (ie->pairwise_cipher & ssid->pairwise_cipher);
1319}
1320
1321
Hai Shalomc1a21442022-02-04 13:43:00 -08001322void wpas_set_mgmt_group_cipher(struct wpa_supplicant *wpa_s,
1323 struct wpa_ssid *ssid, struct wpa_ie_data *ie)
1324{
1325 int sel;
1326
1327 sel = ie->mgmt_group_cipher;
1328 if (ssid->group_mgmt_cipher)
1329 sel &= ssid->group_mgmt_cipher;
1330 if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
1331 !(ie->capabilities & WPA_CAPABILITY_MFPC))
1332 sel = 0;
1333 wpa_dbg(wpa_s, MSG_DEBUG,
1334 "WPA: AP mgmt_group_cipher 0x%x network profile mgmt_group_cipher 0x%x; available mgmt_group_cipher 0x%x",
1335 ie->mgmt_group_cipher, ssid->group_mgmt_cipher, sel);
1336 if (sel & WPA_CIPHER_AES_128_CMAC) {
1337 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1338 wpa_dbg(wpa_s, MSG_DEBUG,
1339 "WPA: using MGMT group cipher AES-128-CMAC");
1340 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1341 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1342 wpa_dbg(wpa_s, MSG_DEBUG,
1343 "WPA: using MGMT group cipher BIP-GMAC-128");
1344 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1345 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1346 wpa_dbg(wpa_s, MSG_DEBUG,
1347 "WPA: using MGMT group cipher BIP-GMAC-256");
1348 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1349 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1350 wpa_dbg(wpa_s, MSG_DEBUG,
1351 "WPA: using MGMT group cipher BIP-CMAC-256");
1352 } else {
1353 wpa_s->mgmt_group_cipher = 0;
1354 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1355 }
1356 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1357 wpa_s->mgmt_group_cipher);
1358 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1359 wpas_get_ssid_pmf(wpa_s, ssid));
1360}
1361
Sunil Ravi77d572f2023-01-17 23:58:31 +00001362/**
1363 * wpa_supplicant_get_psk - Get PSK from config or external database
1364 * @wpa_s: Pointer to wpa_supplicant data
1365 * @bss: Scan results for the selected BSS, or %NULL if not available
1366 * @ssid: Configuration data for the selected network
1367 * @psk: Buffer for the PSK
1368 * Returns: 0 on success or -1 if configuration parsing failed
1369 *
1370 * This function obtains the PSK for a network, either included inline in the
1371 * config or retrieved from an external database.
1372 */
1373static int wpa_supplicant_get_psk(struct wpa_supplicant *wpa_s,
1374 struct wpa_bss *bss, struct wpa_ssid *ssid,
1375 u8 *psk)
1376{
1377 if (ssid->psk_set) {
1378 wpa_hexdump_key(MSG_MSGDUMP, "PSK (set in config)",
1379 ssid->psk, PMK_LEN);
1380 os_memcpy(psk, ssid->psk, PMK_LEN);
1381 return 0;
1382 }
1383
1384#ifndef CONFIG_NO_PBKDF2
1385 if (bss && ssid->bssid_set && ssid->ssid_len == 0 && ssid->passphrase) {
1386 if (pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1387 4096, psk, PMK_LEN) != 0) {
1388 wpa_msg(wpa_s, MSG_WARNING, "Error in pbkdf2_sha1()");
1389 return -1;
1390 }
1391 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1392 psk, PMK_LEN);
1393 return 0;
1394 }
1395#endif /* CONFIG_NO_PBKDF2 */
1396
1397#ifdef CONFIG_EXT_PASSWORD
1398 if (ssid->ext_psk) {
1399 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1400 ssid->ext_psk);
1401 char pw_str[64 + 1];
1402
1403 if (!pw) {
1404 wpa_msg(wpa_s, MSG_INFO,
1405 "EXT PW: No PSK found from external storage");
1406 return -1;
1407 }
1408
1409 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1410 wpa_msg(wpa_s, MSG_INFO,
1411 "EXT PW: Unexpected PSK length %d in external storage",
1412 (int) wpabuf_len(pw));
1413 ext_password_free(pw);
1414 return -1;
1415 }
1416
1417 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1418 pw_str[wpabuf_len(pw)] = '\0';
1419
1420#ifndef CONFIG_NO_PBKDF2
1421 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1422 {
1423 if (pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1424 4096, psk, PMK_LEN) != 0) {
1425 wpa_msg(wpa_s, MSG_WARNING,
1426 "Error in pbkdf2_sha1()");
1427 forced_memzero(pw_str, sizeof(pw_str));
1428 ext_password_free(pw);
1429 return -1;
1430 }
1431 wpa_hexdump_key(MSG_MSGDUMP,
1432 "PSK (from external passphrase)",
1433 psk, PMK_LEN);
1434 } else
1435#endif /* CONFIG_NO_PBKDF2 */
1436 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1437 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1438 wpa_msg(wpa_s, MSG_INFO,
1439 "EXT PW: Invalid PSK hex string");
1440 forced_memzero(pw_str, sizeof(pw_str));
1441 ext_password_free(pw);
1442 return -1;
1443 }
1444 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from external PSK)",
1445 psk, PMK_LEN);
1446 } else {
1447 wpa_msg(wpa_s, MSG_INFO,
1448 "EXT PW: No suitable PSK available");
1449 forced_memzero(pw_str, sizeof(pw_str));
1450 ext_password_free(pw);
1451 return -1;
1452 }
1453
1454 forced_memzero(pw_str, sizeof(pw_str));
1455 ext_password_free(pw);
1456
1457 return 0;
1458 }
1459#endif /* CONFIG_EXT_PASSWORD */
1460
1461 return -1;
1462}
1463
Hai Shalomc1a21442022-02-04 13:43:00 -08001464
Sunil Ravi89eba102022-09-13 21:04:37 -07001465static void wpas_update_allowed_key_mgmt(struct wpa_supplicant *wpa_s,
1466 struct wpa_ssid *ssid)
1467{
1468 int akm_count = wpa_s->max_num_akms;
1469 u8 capab = 0;
1470
1471 if (akm_count < 2)
1472 return;
1473
1474 akm_count--;
1475 wpa_s->allowed_key_mgmts = 0;
1476 switch (wpa_s->key_mgmt) {
1477 case WPA_KEY_MGMT_PSK:
1478 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1479 akm_count--;
1480 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1481 }
1482 if (!akm_count)
1483 break;
1484 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1485 akm_count--;
1486 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1487 }
1488 if (!akm_count)
1489 break;
1490 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1491 wpa_s->allowed_key_mgmts |=
1492 WPA_KEY_MGMT_PSK_SHA256;
1493 break;
1494 case WPA_KEY_MGMT_PSK_SHA256:
1495 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1496 akm_count--;
1497 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1498 }
1499 if (!akm_count)
1500 break;
1501 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1502 akm_count--;
1503 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1504 }
1505 if (!akm_count)
1506 break;
1507 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1508 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1509 break;
1510 case WPA_KEY_MGMT_SAE:
1511 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1512 akm_count--;
1513 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1514 }
1515 if (!akm_count)
1516 break;
1517 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1518 akm_count--;
1519 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE_EXT_KEY;
1520 }
1521 if (!akm_count)
1522 break;
1523 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1524 wpa_s->allowed_key_mgmts |=
1525 WPA_KEY_MGMT_PSK_SHA256;
1526 break;
1527 case WPA_KEY_MGMT_SAE_EXT_KEY:
1528 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1529 akm_count--;
1530 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_SAE;
1531 }
1532 if (!akm_count)
1533 break;
1534 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
1535 akm_count--;
1536 wpa_s->allowed_key_mgmts |= WPA_KEY_MGMT_PSK;
1537 }
1538 if (!akm_count)
1539 break;
1540 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
1541 wpa_s->allowed_key_mgmts |=
1542 WPA_KEY_MGMT_PSK_SHA256;
1543 break;
1544 default:
1545 return;
1546 }
1547
Sunil Ravi77d572f2023-01-17 23:58:31 +00001548 if (wpa_s->conf->sae_pwe != SAE_PWE_HUNT_AND_PECK &&
1549 wpa_s->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
Sunil Ravi89eba102022-09-13 21:04:37 -07001550 capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
1551#ifdef CONFIG_SAE_PK
1552 if (ssid->sae_pk)
1553 capab |= BIT(WLAN_RSNX_CAPAB_SAE_PK);
1554#endif /* CONFIG_SAE_PK */
1555
1556 if (!((wpa_s->allowed_key_mgmts &
1557 (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY)) && capab))
1558 return;
1559
1560 if (!wpa_s->rsnxe_len) {
1561 wpa_s->rsnxe_len = 3;
1562 wpa_s->rsnxe[0] = WLAN_EID_RSNX;
1563 wpa_s->rsnxe[1] = 1;
1564 wpa_s->rsnxe[2] = 0;
1565 }
1566
1567 wpa_s->rsnxe[2] |= capab;
1568}
1569
1570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571/**
1572 * wpa_supplicant_set_suites - Set authentication and encryption parameters
1573 * @wpa_s: Pointer to wpa_supplicant data
1574 * @bss: Scan results for the selected BSS, or %NULL if not available
1575 * @ssid: Configuration data for the selected network
1576 * @wpa_ie: Buffer for the WPA/RSN IE
1577 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1578 * used buffer length in case the functions returns success.
Sunil Ravi77d572f2023-01-17 23:58:31 +00001579 * @skip_default_rsne: Whether to skip setting of the default RSNE/RSNXE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 * Returns: 0 on success or -1 on failure
1581 *
1582 * This function is used to configure authentication and encryption parameters
1583 * based on the network configuration and scan result for the selected BSS (if
1584 * available).
1585 */
1586int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1587 struct wpa_bss *bss, struct wpa_ssid *ssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001588 u8 *wpa_ie, size_t *wpa_ie_len,
1589 bool skip_default_rsne)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590{
1591 struct wpa_ie_data ie;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001592 int sel, proto;
1593 enum sae_pwe sae_pwe;
Hai Shalomc3565922019-10-28 11:58:20 -07001594 const u8 *bss_wpa, *bss_rsn, *bss_rsnx, *bss_osen;
Sunil Ravi640215c2023-06-28 23:08:09 +00001595 bool wmm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596
1597 if (bss) {
1598 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1599 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Hai Shalomc3565922019-10-28 11:58:20 -07001600 bss_rsnx = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001601 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
Hai Shalomc3565922019-10-28 11:58:20 -07001602 } else {
1603 bss_wpa = bss_rsn = bss_rsnx = bss_osen = NULL;
1604 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605
1606 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
1607 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07001608 matching_ciphers(ssid, &ie, bss->freq) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 (ie.key_mgmt & ssid->key_mgmt)) {
1610 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1611 proto = WPA_PROTO_RSN;
1612 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001613 wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 (ie.group_cipher & ssid->group_cipher) &&
1615 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1616 (ie.key_mgmt & ssid->key_mgmt)) {
1617 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1618 proto = WPA_PROTO_WPA;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001619#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -08001620 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN) &&
1621 wpa_parse_wpa_ie(bss_osen, 2 + bss_osen[1], &ie) == 0 &&
1622 (ie.group_cipher & ssid->group_cipher) &&
1623 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1624 (ie.key_mgmt & ssid->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001625 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001626 proto = WPA_PROTO_OSEN;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001627 } else if (bss_rsn && (ssid->proto & WPA_PROTO_OSEN) &&
1628 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1629 (ie.group_cipher & ssid->group_cipher) &&
1630 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1631 (ie.key_mgmt & ssid->key_mgmt)) {
1632 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using OSEN (within RSN)");
1633 proto = WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001634#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 } else if (bss) {
1636 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001637 wpa_dbg(wpa_s, MSG_DEBUG,
1638 "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1639 ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
1640 ssid->key_mgmt);
1641 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
1642 MAC2STR(bss->bssid),
1643 wpa_ssid_txt(bss->ssid, bss->ssid_len),
1644 bss_wpa ? " WPA" : "",
1645 bss_rsn ? " RSN" : "",
1646 bss_osen ? " OSEN" : "");
1647 if (bss_rsn) {
1648 wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
1649 if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
1650 wpa_dbg(wpa_s, MSG_DEBUG,
1651 "Could not parse RSN element");
1652 } else {
1653 wpa_dbg(wpa_s, MSG_DEBUG,
1654 "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1655 ie.pairwise_cipher, ie.group_cipher,
1656 ie.key_mgmt);
1657 }
1658 }
1659 if (bss_wpa) {
1660 wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
1661 if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
1662 wpa_dbg(wpa_s, MSG_DEBUG,
1663 "Could not parse WPA element");
1664 } else {
1665 wpa_dbg(wpa_s, MSG_DEBUG,
1666 "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1667 ie.pairwise_cipher, ie.group_cipher,
1668 ie.key_mgmt);
1669 }
1670 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 return -1;
1672 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001673 if (ssid->proto & WPA_PROTO_OSEN)
1674 proto = WPA_PROTO_OSEN;
1675 else if (ssid->proto & WPA_PROTO_RSN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 proto = WPA_PROTO_RSN;
1677 else
1678 proto = WPA_PROTO_WPA;
1679 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1680 os_memset(&ie, 0, sizeof(ie));
1681 ie.group_cipher = ssid->group_cipher;
1682 ie.pairwise_cipher = ssid->pairwise_cipher;
1683 ie.key_mgmt = ssid->key_mgmt;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001684 ie.mgmt_group_cipher = 0;
1685 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1686 if (ssid->group_mgmt_cipher &
1687 WPA_CIPHER_BIP_GMAC_256)
1688 ie.mgmt_group_cipher =
1689 WPA_CIPHER_BIP_GMAC_256;
1690 else if (ssid->group_mgmt_cipher &
1691 WPA_CIPHER_BIP_CMAC_256)
1692 ie.mgmt_group_cipher =
1693 WPA_CIPHER_BIP_CMAC_256;
1694 else if (ssid->group_mgmt_cipher &
1695 WPA_CIPHER_BIP_GMAC_128)
1696 ie.mgmt_group_cipher =
1697 WPA_CIPHER_BIP_GMAC_128;
1698 else
1699 ie.mgmt_group_cipher =
1700 WPA_CIPHER_AES_128_CMAC;
1701 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001702#ifdef CONFIG_OWE
1703 if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1704 !ssid->owe_only &&
1705 !bss_wpa && !bss_rsn && !bss_osen) {
1706 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1707 wpa_s->wpa_proto = 0;
1708 *wpa_ie_len = 0;
1709 return 0;
1710 }
1711#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1713 "based on configuration");
1714 } else
1715 proto = ie.proto;
1716 }
1717
1718 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1719 "pairwise %d key_mgmt %d proto %d",
1720 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 if (ssid->ieee80211w) {
1722 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1723 ie.mgmt_group_cipher);
1724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001726 wpa_s->wpa_proto = proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1728 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001729 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730
1731 if (bss || !wpa_s->ap_ies_from_associnfo) {
1732 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1733 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1734 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
Hai Shalomc3565922019-10-28 11:58:20 -07001735 bss_rsn ? 2 + bss_rsn[1] : 0) ||
1736 wpa_sm_set_ap_rsnxe(wpa_s->wpa, bss_rsnx,
1737 bss_rsnx ? 2 + bss_rsnx[1] : 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 return -1;
1739 }
1740
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001741#ifdef CONFIG_NO_WPA
1742 wpa_s->group_cipher = WPA_CIPHER_NONE;
1743 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1744#else /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 sel = ie.group_cipher & ssid->group_cipher;
Hai Shalom021b0b52019-04-10 11:17:58 -07001746 wpa_dbg(wpa_s, MSG_DEBUG,
1747 "WPA: AP group 0x%x network profile group 0x%x; available group 0x%x",
1748 ie.group_cipher, ssid->group_cipher, sel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001749 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1750 if (wpa_s->group_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1752 "cipher");
1753 return -1;
1754 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001755 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1756 wpa_cipher_txt(wpa_s->group_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757
1758 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
Hai Shalom021b0b52019-04-10 11:17:58 -07001759 wpa_dbg(wpa_s, MSG_DEBUG,
1760 "WPA: AP pairwise 0x%x network profile pairwise 0x%x; available pairwise 0x%x",
1761 ie.pairwise_cipher, ssid->pairwise_cipher, sel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001762 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1763 if (wpa_s->pairwise_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1765 "cipher");
1766 return -1;
1767 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001768 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1769 wpa_cipher_txt(wpa_s->pairwise_cipher));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001770#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
1772 sel = ie.key_mgmt & ssid->key_mgmt;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001773#ifdef CONFIG_SAE
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001774 if ((!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) &&
1775 !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA)) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00001776 wpas_is_sae_avoided(wpa_s, ssid, &ie))
Sunil Ravi89eba102022-09-13 21:04:37 -07001777 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY |
1778 WPA_KEY_MGMT_FT_SAE | WPA_KEY_MGMT_FT_SAE_EXT_KEY);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001779#endif /* CONFIG_SAE */
Hai Shalomfdcde762020-04-02 11:19:20 -07001780#ifdef CONFIG_IEEE80211R
1781 if (!(wpa_s->drv_flags & (WPA_DRIVER_FLAGS_SME |
1782 WPA_DRIVER_FLAGS_UPDATE_FT_IES)))
1783 sel &= ~WPA_KEY_MGMT_FT;
1784#endif /* CONFIG_IEEE80211R */
1785 wpa_dbg(wpa_s, MSG_DEBUG,
1786 "WPA: AP key_mgmt 0x%x network profile key_mgmt 0x%x; available key_mgmt 0x%x",
1787 ie.key_mgmt, ssid->key_mgmt, sel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 if (0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001789#ifdef CONFIG_IEEE80211R
1790#ifdef CONFIG_SHA384
Hai Shalomc3565922019-10-28 11:58:20 -07001791 } else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
1792 os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001793 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
1794 wpa_dbg(wpa_s, MSG_DEBUG,
1795 "WPA: using KEY_MGMT FT/802.1X-SHA384");
Hai Shalom81f62d82019-07-22 12:10:00 -07001796 if (!ssid->ft_eap_pmksa_caching &&
1797 pmksa_cache_get_current(wpa_s->wpa)) {
1798 /* PMKSA caching with FT may have interoperability
1799 * issues, so disable that case by default for now. */
Hai Shalom021b0b52019-04-10 11:17:58 -07001800 wpa_dbg(wpa_s, MSG_DEBUG,
1801 "WPA: Disable PMKSA caching for FT/802.1X connection");
1802 pmksa_cache_clear_current(wpa_s->wpa);
1803 }
1804#endif /* CONFIG_SHA384 */
1805#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001806#ifdef CONFIG_SUITEB192
1807 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1808 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
1809 wpa_dbg(wpa_s, MSG_DEBUG,
1810 "WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
1811#endif /* CONFIG_SUITEB192 */
1812#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001813 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1814 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1815 wpa_dbg(wpa_s, MSG_DEBUG,
1816 "WPA: using KEY_MGMT 802.1X with Suite B");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001817#endif /* CONFIG_SUITEB */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001818#ifdef CONFIG_SHA384
1819 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA384) {
1820 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA384;
1821 wpa_dbg(wpa_s, MSG_DEBUG,
1822 "WPA: using KEY_MGMT 802.1X with SHA384");
1823#endif /* CONFIG_SHA384 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001824#ifdef CONFIG_FILS
1825#ifdef CONFIG_IEEE80211R
1826 } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA384) {
1827 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
1828 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA384");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001829#endif /* CONFIG_IEEE80211R */
1830 } else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
1831 wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
1832 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA384");
Sunil Ravi89eba102022-09-13 21:04:37 -07001833#ifdef CONFIG_IEEE80211R
1834 } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA256) {
1835 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
1836 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA256");
1837#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001838 } else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
1839 wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
1840 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA256");
1841#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842#ifdef CONFIG_IEEE80211R
Hai Shalomc3565922019-10-28 11:58:20 -07001843 } else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X) &&
1844 os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1846 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
Hai Shalom81f62d82019-07-22 12:10:00 -07001847 if (!ssid->ft_eap_pmksa_caching &&
1848 pmksa_cache_get_current(wpa_s->wpa)) {
1849 /* PMKSA caching with FT may have interoperability
1850 * issues, so disable that case by default for now. */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001851 wpa_dbg(wpa_s, MSG_DEBUG,
1852 "WPA: Disable PMKSA caching for FT/802.1X connection");
1853 pmksa_cache_clear_current(wpa_s->wpa);
1854 }
Hai Shalom021b0b52019-04-10 11:17:58 -07001855#endif /* CONFIG_IEEE80211R */
1856#ifdef CONFIG_DPP
1857 } else if (sel & WPA_KEY_MGMT_DPP) {
1858 wpa_s->key_mgmt = WPA_KEY_MGMT_DPP;
1859 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT DPP");
1860#endif /* CONFIG_DPP */
1861#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07001862 } else if (sel & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1863 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE_EXT_KEY;
1864 wpa_dbg(wpa_s, MSG_DEBUG,
1865 "RSN: using KEY_MGMT FT/SAE (ext key)");
1866 } else if (sel & WPA_KEY_MGMT_SAE_EXT_KEY) {
1867 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
1868 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE (ext key)");
Hai Shalom021b0b52019-04-10 11:17:58 -07001869 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1870 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1871 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1872 } else if (sel & WPA_KEY_MGMT_SAE) {
1873 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1874 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1875#endif /* CONFIG_SAE */
1876#ifdef CONFIG_IEEE80211R
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1878 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1879 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1880#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1882 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1883 wpa_dbg(wpa_s, MSG_DEBUG,
1884 "WPA: using KEY_MGMT 802.1X with SHA256");
1885 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1886 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1887 wpa_dbg(wpa_s, MSG_DEBUG,
1888 "WPA: using KEY_MGMT PSK with SHA256");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1890 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1891 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1892 } else if (sel & WPA_KEY_MGMT_PSK) {
1893 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1894 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1895 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1896 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1897 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001898#ifdef CONFIG_HS20
1899 } else if (sel & WPA_KEY_MGMT_OSEN) {
1900 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1901 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1902#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001903#ifdef CONFIG_OWE
1904 } else if (sel & WPA_KEY_MGMT_OWE) {
1905 wpa_s->key_mgmt = WPA_KEY_MGMT_OWE;
1906 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT OWE");
1907#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 } else {
1909 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1910 "authenticated key management type");
1911 return -1;
1912 }
1913
1914 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1915 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1916 wpa_s->pairwise_cipher);
1917 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1918
Hai Shalomc3565922019-10-28 11:58:20 -07001919 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00001920 (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED ||
1921 (bss && is_6ghz_freq(bss->freq)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001922 wpa_msg(wpa_s, MSG_INFO,
1923 "RSN: Management frame protection required but the selected AP does not enable it");
1924 return -1;
1925 }
1926
Hai Shalomc1a21442022-02-04 13:43:00 -08001927 wpas_set_mgmt_group_cipher(wpa_s, ssid, &ie);
Hai Shalom74f70d42019-02-11 14:42:39 -08001928#ifdef CONFIG_OCV
Hai Shalom60840252021-02-19 19:02:11 -08001929 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
1930 (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
1931 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV, ssid->ocv);
Hai Shalom74f70d42019-02-11 14:42:39 -08001932#endif /* CONFIG_OCV */
Hai Shalomfdcde762020-04-02 11:19:20 -07001933 sae_pwe = wpa_s->conf->sae_pwe;
Sunil Ravi89eba102022-09-13 21:04:37 -07001934 if ((ssid->sae_password_id ||
1935 wpa_key_mgmt_sae_ext_key(wpa_s->key_mgmt)) &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00001936 sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1937 sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
1938 if (bss && is_6ghz_freq(bss->freq) &&
1939 sae_pwe == SAE_PWE_HUNT_AND_PECK) {
1940 wpa_dbg(wpa_s, MSG_DEBUG,
1941 "RSN: Enable SAE hash-to-element mode for 6 GHz BSS");
1942 sae_pwe = SAE_PWE_BOTH;
Jimmy Chenaace8cd2021-04-13 14:55:52 +08001943 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001944 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PWE, sae_pwe);
Hai Shalom899fcc72020-10-19 14:38:18 -07001945#ifdef CONFIG_SAE_PK
1946 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_SAE_PK,
1947 wpa_key_mgmt_sae(ssid->key_mgmt) &&
1948 ssid->sae_pk != SAE_PK_MODE_DISABLED &&
1949 ((ssid->sae_password &&
1950 sae_pk_valid_password(ssid->sae_password)) ||
1951 (!ssid->sae_password && ssid->passphrase &&
1952 sae_pk_valid_password(ssid->passphrase))));
1953#endif /* CONFIG_SAE_PK */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001954 if (bss && is_6ghz_freq(bss->freq) &&
1955 wpas_get_ssid_pmf(wpa_s, ssid) != MGMT_FRAME_PROTECTION_REQUIRED) {
1956 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Force MFPR=1 on 6 GHz");
1957 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1958 MGMT_FRAME_PROTECTION_REQUIRED);
1959 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001960#ifdef CONFIG_TESTING_OPTIONS
1961 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_FT_RSNXE_USED,
1962 wpa_s->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001963 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL,
1964 wpa_s->oci_freq_override_eapol);
1965 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
1966 wpa_s->oci_freq_override_eapol_g2);
1967 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
1968 wpa_s->oci_freq_override_ft_assoc);
1969 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FILS_ASSOC,
1970 wpa_s->oci_freq_override_fils_assoc);
Sunil Ravia04bd252022-05-02 22:54:18 -07001971 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DISABLE_EAPOL_G2_TX,
1972 wpa_s->disable_eapol_g2_tx);
Hai Shalomb755a2a2020-04-23 21:49:02 -07001973#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalomfdcde762020-04-02 11:19:20 -07001974
1975 /* Extended Key ID is only supported in infrastructure BSS so far */
1976 if (ssid->mode == WPAS_MODE_INFRA && wpa_s->conf->extended_key_id &&
1977 (ssid->proto & WPA_PROTO_RSN) &&
1978 ssid->pairwise_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_CCMP_256 |
1979 WPA_CIPHER_GCMP | WPA_CIPHER_GCMP_256) &&
1980 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_EXTENDED_KEY_ID)) {
1981 int use_ext_key_id = 0;
1982
1983 wpa_msg(wpa_s, MSG_DEBUG,
1984 "WPA: Enable Extended Key ID support");
1985 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_EXT_KEY_ID,
1986 wpa_s->conf->extended_key_id);
1987 if (bss_rsn &&
1988 wpa_s->conf->extended_key_id &&
1989 wpa_s->pairwise_cipher != WPA_CIPHER_TKIP &&
1990 (ie.capabilities & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST))
1991 use_ext_key_id = 1;
1992 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_USE_EXT_KEY_ID,
1993 use_ext_key_id);
1994 } else {
1995 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_EXT_KEY_ID, 0);
1996 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_USE_EXT_KEY_ID, 0);
1997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998
Sunil Ravi640215c2023-06-28 23:08:09 +00001999 /* Mark WMM enabled for any HT/VHT/HE/EHT association to get more
2000 * appropriate advertisement of the supported number of PTKSA receive
2001 * counters. In theory, this could be based on a driver capability, but
2002 * in practice all cases using WMM support at least eight replay
2003 * counters, so use a hardcoded value for now since there is no explicit
2004 * driver capability indication for this.
2005 *
2006 * In addition, claim WMM to be enabled if the AP supports it since it
2007 * is far more likely for any current device to support WMM. */
2008 wmm = wpa_s->connection_set &&
2009 (wpa_s->connection_ht || wpa_s->connection_vht ||
2010 wpa_s->connection_he || wpa_s->connection_eht);
2011 if (!wmm && bss)
2012 wmm = wpa_bss_get_vendor_ie(bss, WMM_IE_VENDOR_TYPE);
2013 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_WMM_ENABLED, wmm);
2014
Sunil Ravi77d572f2023-01-17 23:58:31 +00002015 if (!skip_default_rsne) {
2016 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie,
2017 wpa_ie_len)) {
2018 wpa_msg(wpa_s, MSG_WARNING,
2019 "RSN: Failed to generate RSNE/WPA IE");
2020 return -1;
2021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022
Sunil Ravi77d572f2023-01-17 23:58:31 +00002023 wpa_s->rsnxe_len = sizeof(wpa_s->rsnxe);
2024 if (wpa_sm_set_assoc_rsnxe_default(wpa_s->wpa, wpa_s->rsnxe,
2025 &wpa_s->rsnxe_len)) {
2026 wpa_msg(wpa_s, MSG_WARNING,
2027 "RSN: Failed to generate RSNXE");
2028 return -1;
2029 }
Hai Shalomc3565922019-10-28 11:58:20 -07002030 }
2031
Hai Shalom021b0b52019-04-10 11:17:58 -07002032 if (0) {
2033#ifdef CONFIG_DPP
2034 } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
2035 /* Use PMK from DPP network introduction (PMKSA entry) */
2036 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002037#ifdef CONFIG_DPP2
2038 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DPP_PFS, ssid->dpp_pfs);
2039#endif /* CONFIG_DPP2 */
Hai Shalom021b0b52019-04-10 11:17:58 -07002040#endif /* CONFIG_DPP */
2041 } else if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002042 int psk_set = 0;
2043
Sunil Ravi77d572f2023-01-17 23:58:31 +00002044 if (wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt)) {
2045 u8 psk[PMK_LEN];
Roshan Pius3a1667e2018-07-03 15:17:14 -07002046
Sunil Ravi77d572f2023-01-17 23:58:31 +00002047 if (wpa_supplicant_get_psk(wpa_s, bss, ssid,
2048 psk) == 0) {
2049 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
2050 NULL);
2051 psk_set = 1;
2052 }
2053 forced_memzero(psk, sizeof(psk));
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002054 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002055
Roshan Pius3a1667e2018-07-03 15:17:14 -07002056 if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00002057 (ssid->sae_password || ssid->passphrase || ssid->ext_psk))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002058 psk_set = 1;
2059
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002060 if (!psk_set) {
2061 wpa_msg(wpa_s, MSG_INFO,
2062 "No PSK available for association");
Sunil Ravi77d572f2023-01-17 23:58:31 +00002063 wpas_auth_failed(wpa_s, "NO_PSK_AVAILABLE", NULL);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002064 return -1;
2065 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002066#ifdef CONFIG_OWE
2067 } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
2068 /* OWE Diffie-Hellman exchange in (Re)Association
2069 * Request/Response frames set the PMK, so do not override it
2070 * here. */
2071#endif /* CONFIG_OWE */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002072 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
2074
Hai Shalomfdcde762020-04-02 11:19:20 -07002075 if (ssid->mode != WPAS_MODE_IBSS &&
2076 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED) &&
2077 (ssid->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_NEVER ||
2078 (ssid->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_LOCAL_OK &&
2079 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS)))) {
2080 wpa_msg(wpa_s, MSG_INFO,
2081 "Disable PTK0 rekey support - replaced with reconnect");
2082 wpa_s->deny_ptk0_rekey = 1;
2083 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DENY_PTK0_REKEY, 1);
2084 } else {
2085 wpa_s->deny_ptk0_rekey = 0;
2086 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DENY_PTK0_REKEY, 0);
2087 }
2088
Winnie Chen4138eec2022-11-10 16:32:53 +08002089#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Vinayak Yadawad14709082022-03-17 14:25:11 +05302090 if ((wpa_s->key_mgmt & WPA_KEY_MGMT_CROSS_AKM_ROAM) &&
Dennis Jeon2a960552022-07-05 13:56:50 +09002091 IS_CROSS_AKM_ROAM_KEY_MGMT(ssid->key_mgmt) &&
2092 (wpa_s->group_cipher == WPA_CIPHER_CCMP) &&
Dennis Jeona3930812022-10-24 15:31:04 +09002093 (wpa_s->pairwise_cipher == WPA_CIPHER_CCMP) &&
2094 (wpa_s->wpa_proto == WPA_PROTO_RSN)) {
Vinayak Yadawad14709082022-03-17 14:25:11 +05302095 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PSK;
2096 wpa_dbg(wpa_s, MSG_INFO,
2097 "WPA: Updating to KEY_MGMT SAE+PSK for seamless roaming");
2098 }
Sunil Ravif42be322022-11-04 03:31:21 +00002099#else
2100 if (wpa_key_mgmt_cross_akm(wpa_s->key_mgmt) &&
2101 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2102 wpas_update_allowed_key_mgmt(wpa_s, ssid);
Winnie Chen4138eec2022-11-10 16:32:53 +08002103#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Vinayak Yadawad14709082022-03-17 14:25:11 +05302104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 return 0;
2106}
2107
2108
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002109static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx,
2110 struct wpa_bss *bss)
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002111{
Hai Shalomc1a21442022-02-04 13:43:00 -08002112 bool scs = true, mscs = true;
2113
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002114 *pos = 0x00;
2115
2116 switch (idx) {
2117 case 0: /* Bits 0-7 */
2118 break;
2119 case 1: /* Bits 8-15 */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002120 if (wpa_s->conf->coloc_intf_reporting) {
2121 /* Bit 13 - Collocated Interference Reporting */
2122 *pos |= 0x20;
2123 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002124 break;
2125 case 2: /* Bits 16-23 */
2126#ifdef CONFIG_WNM
2127 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
Hai Shalomc3565922019-10-28 11:58:20 -07002128 if (!wpa_s->disable_mbo_oce && !wpa_s->conf->disable_btm)
Hai Shalom81f62d82019-07-22 12:10:00 -07002129 *pos |= 0x08; /* Bit 19 - BSS Transition */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002130#endif /* CONFIG_WNM */
2131 break;
2132 case 3: /* Bits 24-31 */
2133#ifdef CONFIG_WNM
2134 *pos |= 0x02; /* Bit 25 - SSID List */
2135#endif /* CONFIG_WNM */
2136#ifdef CONFIG_INTERWORKING
2137 if (wpa_s->conf->interworking)
2138 *pos |= 0x80; /* Bit 31 - Interworking */
2139#endif /* CONFIG_INTERWORKING */
2140 break;
2141 case 4: /* Bits 32-39 */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002142#ifdef CONFIG_INTERWORKING
Hai Shalomce48b4a2018-09-05 11:41:35 -07002143 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_QOS_MAPPING)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002144 *pos |= 0x01; /* Bit 32 - QoS Map */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002145#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002146 break;
2147 case 5: /* Bits 40-47 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002148#ifdef CONFIG_HS20
2149 if (wpa_s->conf->hs20)
2150 *pos |= 0x40; /* Bit 46 - WNM-Notification */
2151#endif /* CONFIG_HS20 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002152#ifdef CONFIG_MBO
2153 *pos |= 0x40; /* Bit 46 - WNM-Notification */
2154#endif /* CONFIG_MBO */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002155 break;
2156 case 6: /* Bits 48-55 */
Hai Shalomc1a21442022-02-04 13:43:00 -08002157#ifdef CONFIG_TESTING_OPTIONS
2158 if (wpa_s->disable_scs_support)
2159 scs = false;
2160#endif /* CONFIG_TESTING_OPTIONS */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002161 if (bss && !wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_SCS)) {
2162 /* Drop own SCS capability indication since the AP does
2163 * not support it. This is needed to avoid
2164 * interoperability issues with APs that get confused
2165 * with Extended Capabilities element. */
2166 scs = false;
2167 }
Hai Shalomc1a21442022-02-04 13:43:00 -08002168 if (scs)
2169 *pos |= 0x40; /* Bit 54 - SCS */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002170 break;
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002171 case 7: /* Bits 56-63 */
2172 break;
2173 case 8: /* Bits 64-71 */
2174 if (wpa_s->conf->ftm_responder)
2175 *pos |= 0x40; /* Bit 70 - FTM responder */
2176 if (wpa_s->conf->ftm_initiator)
2177 *pos |= 0x80; /* Bit 71 - FTM initiator */
2178 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002179 case 9: /* Bits 72-79 */
2180#ifdef CONFIG_FILS
Roshan Pius3a1667e2018-07-03 15:17:14 -07002181 if (!wpa_s->disable_fils)
2182 *pos |= 0x01;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002183#endif /* CONFIG_FILS */
2184 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07002185 case 10: /* Bits 80-87 */
Hai Shalomc1a21442022-02-04 13:43:00 -08002186#ifdef CONFIG_TESTING_OPTIONS
2187 if (wpa_s->disable_mscs_support)
2188 mscs = false;
2189#endif /* CONFIG_TESTING_OPTIONS */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002190 if (bss && !wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_MSCS)) {
2191 /* Drop own MSCS capability indication since the AP does
2192 * not support it. This is needed to avoid
2193 * interoperability issues with APs that get confused
2194 * with Extended Capabilities element. */
2195 mscs = false;
2196 }
Hai Shalomc1a21442022-02-04 13:43:00 -08002197 if (mscs)
2198 *pos |= 0x20; /* Bit 85 - Mirrored SCS */
Hai Shalom899fcc72020-10-19 14:38:18 -07002199 break;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002200 }
2201}
2202
2203
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002204int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf,
2205 size_t buflen, struct wpa_bss *bss)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002206{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002207 u8 *pos = buf;
Hai Shalom899fcc72020-10-19 14:38:18 -07002208 u8 len = 11, i;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002209
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002210 if (len < wpa_s->extended_capa_len)
2211 len = wpa_s->extended_capa_len;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002212 if (buflen < (size_t) len + 2) {
2213 wpa_printf(MSG_INFO,
2214 "Not enough room for building extended capabilities element");
2215 return -1;
2216 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002217
2218 *pos++ = WLAN_EID_EXT_CAPAB;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002219 *pos++ = len;
2220 for (i = 0; i < len; i++, pos++) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002221 wpas_ext_capab_byte(wpa_s, pos, i, bss);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002222
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002223 if (i < wpa_s->extended_capa_len) {
2224 *pos &= ~wpa_s->extended_capa_mask[i];
2225 *pos |= wpa_s->extended_capa[i];
2226 }
2227 }
2228
2229 while (len > 0 && buf[1 + len] == 0) {
2230 len--;
2231 buf[1] = len;
2232 }
2233 if (len == 0)
2234 return 0;
2235
2236 return 2 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002237}
2238
2239
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002240static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
2241 struct wpa_bss *test_bss)
2242{
2243 struct wpa_bss *bss;
2244
2245 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2246 if (bss == test_bss)
2247 return 1;
2248 }
2249
2250 return 0;
2251}
2252
2253
2254static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
2255 struct wpa_ssid *test_ssid)
2256{
2257 struct wpa_ssid *ssid;
2258
2259 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2260 if (ssid == test_ssid)
2261 return 1;
2262 }
2263
2264 return 0;
2265}
2266
2267
2268int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
2269 struct wpa_ssid *test_ssid)
2270{
2271 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
2272 return 0;
2273
2274 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
2275}
2276
2277
2278void wpas_connect_work_free(struct wpa_connect_work *cwork)
2279{
2280 if (cwork == NULL)
2281 return;
2282 os_free(cwork);
2283}
2284
2285
2286void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
2287{
2288 struct wpa_connect_work *cwork;
2289 struct wpa_radio_work *work = wpa_s->connect_work;
2290
2291 if (!work)
2292 return;
2293
2294 wpa_s->connect_work = NULL;
2295 cwork = work->ctx;
2296 work->ctx = NULL;
2297 wpas_connect_work_free(cwork);
2298 radio_work_done(work);
2299}
2300
2301
Sunil Ravi77d572f2023-01-17 23:58:31 +00002302int wpas_update_random_addr(struct wpa_supplicant *wpa_s,
2303 enum wpas_mac_addr_style style,
2304 struct wpa_ssid *ssid)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002305{
2306 struct os_reltime now;
2307 u8 addr[ETH_ALEN];
2308
2309 os_get_reltime(&now);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002310 /* Random addresses are valid within a given ESS so check
2311 * expiration/value only when continuing to use the same ESS. */
2312 if (wpa_s->last_mac_addr_style == style && wpa_s->reassoc_same_ess) {
2313 if (style == WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS) {
2314 /* Pregenerated addresses do not expire but their value
2315 * might have changed, so let's check that. */
2316 if (os_memcmp(wpa_s->own_addr, ssid->mac_value,
2317 ETH_ALEN) == 0)
2318 return 0;
2319 } else if ((wpa_s->last_mac_addr_change.sec != 0 ||
2320 wpa_s->last_mac_addr_change.usec != 0) &&
2321 !os_reltime_expired(
2322 &now,
2323 &wpa_s->last_mac_addr_change,
2324 wpa_s->conf->rand_addr_lifetime)) {
2325 wpa_msg(wpa_s, MSG_DEBUG,
2326 "Previously selected random MAC address has not yet expired");
2327 return 0;
2328 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002329 }
2330
2331 switch (style) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002332 case WPAS_MAC_ADDR_STYLE_RANDOM:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002333 if (random_mac_addr(addr) < 0)
2334 return -1;
2335 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002336 case WPAS_MAC_ADDR_STYLE_RANDOM_SAME_OUI:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002337 os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
2338 if (random_mac_addr_keep_oui(addr) < 0)
2339 return -1;
2340 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002341 case WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS:
2342 if (!ssid) {
2343 wpa_msg(wpa_s, MSG_INFO,
2344 "Invalid 'ssid' for address policy 3");
2345 return -1;
2346 }
2347 os_memcpy(addr, ssid->mac_value, ETH_ALEN);
2348 break;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002349 default:
2350 return -1;
2351 }
2352
2353 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
2354 wpa_msg(wpa_s, MSG_INFO,
2355 "Failed to set random MAC address");
2356 return -1;
2357 }
2358
2359 os_get_reltime(&wpa_s->last_mac_addr_change);
2360 wpa_s->mac_addr_changed = 1;
2361 wpa_s->last_mac_addr_style = style;
2362
2363 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2364 wpa_msg(wpa_s, MSG_INFO,
2365 "Could not update MAC address information");
2366 return -1;
2367 }
2368
2369 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
2370 MAC2STR(addr));
2371
Sunil Ravi77d572f2023-01-17 23:58:31 +00002372 return 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002373}
2374
2375
2376int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
2377{
2378 if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
2379 !wpa_s->conf->preassoc_mac_addr)
2380 return 0;
2381
Sunil Ravi77d572f2023-01-17 23:58:31 +00002382 return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr,
2383 NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002384}
2385
2386
Sunil Ravi036cec52023-03-29 11:35:17 -07002387void wpa_s_setup_sae_pt(struct wpa_config *conf, struct wpa_ssid *ssid,
2388 bool force)
Hai Shalomc3565922019-10-28 11:58:20 -07002389{
2390#ifdef CONFIG_SAE
2391 int *groups = conf->sae_groups;
2392 int default_groups[] = { 19, 20, 21, 0 };
2393 const char *password;
2394
2395 if (!groups || groups[0] <= 0)
2396 groups = default_groups;
2397
2398 password = ssid->sae_password;
2399 if (!password)
2400 password = ssid->passphrase;
2401
Hai Shalom899fcc72020-10-19 14:38:18 -07002402 if (!password ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002403 (conf->sae_pwe == SAE_PWE_HUNT_AND_PECK && !ssid->sae_password_id &&
Sunil Ravi89eba102022-09-13 21:04:37 -07002404 !wpa_key_mgmt_sae_ext_key(ssid->key_mgmt) &&
Sunil Ravi036cec52023-03-29 11:35:17 -07002405 !force &&
Hai Shalom899fcc72020-10-19 14:38:18 -07002406 !sae_pk_valid_password(password)) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002407 conf->sae_pwe == SAE_PWE_FORCE_HUNT_AND_PECK) {
Hai Shalomc3565922019-10-28 11:58:20 -07002408 /* PT derivation not needed */
2409 sae_deinit_pt(ssid->pt);
2410 ssid->pt = NULL;
2411 return;
2412 }
2413
2414 if (ssid->pt)
2415 return; /* PT already derived */
2416 ssid->pt = sae_derive_pt(groups, ssid->ssid, ssid->ssid_len,
2417 (const u8 *) password, os_strlen(password),
2418 ssid->sae_password_id);
2419#endif /* CONFIG_SAE */
2420}
2421
2422
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002423static void wpa_s_clear_sae_rejected(struct wpa_supplicant *wpa_s)
2424{
2425#if defined(CONFIG_SAE) && defined(CONFIG_SME)
2426 os_free(wpa_s->sme.sae_rejected_groups);
2427 wpa_s->sme.sae_rejected_groups = NULL;
2428#ifdef CONFIG_TESTING_OPTIONS
2429 if (wpa_s->extra_sae_rejected_groups) {
2430 int i, *groups = wpa_s->extra_sae_rejected_groups;
2431
2432 for (i = 0; groups[i]; i++) {
2433 wpa_printf(MSG_DEBUG,
2434 "TESTING: Indicate rejection of an extra SAE group %d",
2435 groups[i]);
2436 int_array_add_unique(&wpa_s->sme.sae_rejected_groups,
2437 groups[i]);
2438 }
2439 }
2440#endif /* CONFIG_TESTING_OPTIONS */
2441#endif /* CONFIG_SAE && CONFIG_SME */
2442}
2443
2444
Hai Shalom60840252021-02-19 19:02:11 -08002445int wpas_restore_permanent_mac_addr(struct wpa_supplicant *wpa_s)
2446{
2447 if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
2448 wpa_msg(wpa_s, MSG_INFO,
2449 "Could not restore permanent MAC address");
2450 return -1;
2451 }
2452 wpa_s->mac_addr_changed = 0;
2453 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2454 wpa_msg(wpa_s, MSG_INFO,
2455 "Could not update MAC address information");
2456 return -1;
2457 }
2458 wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
2459 return 0;
2460}
2461
2462
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002463static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
2464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465/**
2466 * wpa_supplicant_associate - Request association
2467 * @wpa_s: Pointer to wpa_supplicant data
2468 * @bss: Scan results for the selected BSS, or %NULL if not available
2469 * @ssid: Configuration data for the selected network
2470 *
2471 * This function is used to request %wpa_supplicant to associate with a BSS.
2472 */
2473void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
2474 struct wpa_bss *bss, struct wpa_ssid *ssid)
2475{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002476 struct wpa_connect_work *cwork;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002477 enum wpas_mac_addr_style rand_style;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002478
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002479 wpa_s->own_disconnect_req = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07002480 wpa_s->own_reconnect_req = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002481
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002482 /*
2483 * If we are starting a new connection, any previously pending EAPOL
2484 * RX cannot be valid anymore.
2485 */
2486 wpabuf_free(wpa_s->pending_eapol_rx);
2487 wpa_s->pending_eapol_rx = NULL;
2488
Sunil Ravi77d572f2023-01-17 23:58:31 +00002489 if (ssid->mac_addr == WPAS_MAC_ADDR_STYLE_NOT_SET)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002490 rand_style = wpa_s->conf->mac_addr;
2491 else
2492 rand_style = ssid->mac_addr;
2493
Sunil Ravia04bd252022-05-02 22:54:18 -07002494 wpa_s->eapol_failed = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07002495 wpa_s->multi_ap_ie = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002496 wmm_ac_clear_saved_tspecs(wpa_s);
2497 wpa_s->reassoc_same_bss = 0;
Dmitry Shmidte4663042016-04-04 10:07:49 -07002498 wpa_s->reassoc_same_ess = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002499#ifdef CONFIG_TESTING_OPTIONS
2500 wpa_s->testing_resend_assoc = 0;
2501#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002502
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002503 if (wpa_s->last_ssid == ssid) {
2504 wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
Dmitry Shmidte4663042016-04-04 10:07:49 -07002505 wpa_s->reassoc_same_ess = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002506 if (wpa_s->current_bss && wpa_s->current_bss == bss) {
2507 wmm_ac_save_tspecs(wpa_s);
2508 wpa_s->reassoc_same_bss = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08002509 } else if (wpa_s->current_bss && wpa_s->current_bss != bss) {
2510 os_get_reltime(&wpa_s->roam_start);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002511 }
Hai Shalomc3565922019-10-28 11:58:20 -07002512 } else {
2513#ifdef CONFIG_SAE
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002514 wpa_s_clear_sae_rejected(wpa_s);
Hai Shalomc3565922019-10-28 11:58:20 -07002515#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002516 }
Hai Shalomc1a21442022-02-04 13:43:00 -08002517#ifdef CONFIG_SAE
Sunil Ravi036cec52023-03-29 11:35:17 -07002518 wpa_s_setup_sae_pt(wpa_s->conf, ssid, false);
Hai Shalomc1a21442022-02-04 13:43:00 -08002519#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002520
Sunil Ravi77d572f2023-01-17 23:58:31 +00002521 if (rand_style > WPAS_MAC_ADDR_STYLE_PERMANENT) {
2522 int status = wpas_update_random_addr(wpa_s, rand_style, ssid);
2523
2524 if (status < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002525 return;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002526 if (rand_style != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS &&
2527 status > 0) /* MAC changed */
2528 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2529 } else if (rand_style == WPAS_MAC_ADDR_STYLE_PERMANENT &&
2530 wpa_s->mac_addr_changed) {
Hai Shalom60840252021-02-19 19:02:11 -08002531 if (wpas_restore_permanent_mac_addr(wpa_s) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002532 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002533 }
2534 wpa_s->last_ssid = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535
2536#ifdef CONFIG_IBSS_RSN
2537 ibss_rsn_deinit(wpa_s->ibss_rsn);
2538 wpa_s->ibss_rsn = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002539#else /* CONFIG_IBSS_RSN */
2540 if (ssid->mode == WPAS_MODE_IBSS &&
2541 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
2542 wpa_msg(wpa_s, MSG_INFO,
2543 "IBSS RSN not supported in the build");
2544 return;
2545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546#endif /* CONFIG_IBSS_RSN */
2547
2548 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
2549 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
2550#ifdef CONFIG_AP
2551 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
2552 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
2553 "mode");
2554 return;
2555 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002556 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
2557 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Ajay Davanageri10b3c662024-05-02 19:19:24 +05302558 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
2559 ssid->mode == WPAS_MODE_P2P_GO) {
2560 wpa_msg(wpa_s, MSG_ERROR, "create ap failed. clean up the states");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002561 wpas_p2p_ap_setup_failed(wpa_s);
Ajay Davanageri10b3c662024-05-02 19:19:24 +05302562 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002563 return;
2564 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 wpa_s->current_bss = bss;
2566#else /* CONFIG_AP */
2567 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
2568 "the build");
2569#endif /* CONFIG_AP */
2570 return;
2571 }
2572
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002573 if (ssid->mode == WPAS_MODE_MESH) {
2574#ifdef CONFIG_MESH
2575 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
2576 wpa_msg(wpa_s, MSG_INFO,
2577 "Driver does not support mesh mode");
2578 return;
2579 }
2580 if (bss)
2581 ssid->frequency = bss->freq;
2582 if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002583 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002584 wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
2585 return;
2586 }
2587 wpa_s->current_bss = bss;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002588#else /* CONFIG_MESH */
2589 wpa_msg(wpa_s, MSG_ERROR,
2590 "mesh mode support not included in the build");
2591#endif /* CONFIG_MESH */
2592 return;
2593 }
2594
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002595 /*
2596 * Set WPA state machine configuration to match the selected network now
2597 * so that the information is available before wpas_start_assoc_cb()
2598 * gets called. This is needed at least for RSN pre-authentication where
2599 * candidate APs are added to a list based on scan result processing
2600 * before completion of the first association.
2601 */
2602 wpa_supplicant_rsn_supp_set_config(wpa_s, ssid);
2603
2604#ifdef CONFIG_DPP
2605 if (wpas_dpp_check_connect(wpa_s, ssid, bss) != 0)
2606 return;
2607#endif /* CONFIG_DPP */
2608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609#ifdef CONFIG_TDLS
2610 if (bss)
Hai Shalom60840252021-02-19 19:02:11 -08002611 wpa_tdls_ap_ies(wpa_s->wpa, wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612#endif /* CONFIG_TDLS */
2613
Hai Shalomc3565922019-10-28 11:58:20 -07002614#ifdef CONFIG_MBO
2615 wpas_mbo_check_pmf(wpa_s, bss, ssid);
2616#endif /* CONFIG_MBO */
2617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
Hai Shalom81f62d82019-07-22 12:10:00 -07002619 ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 sme_authenticate(wpa_s, bss, ssid);
2621 return;
2622 }
2623
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002624 if (wpa_s->connect_work) {
2625 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
2626 return;
2627 }
2628
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002629 if (radio_work_pending(wpa_s, "connect")) {
2630 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
2631 return;
2632 }
2633
Dmitry Shmidt29333592017-01-09 12:27:11 -08002634#ifdef CONFIG_SME
2635 if (ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) {
2636 /* Clear possibly set auth_alg, if any, from last attempt. */
2637 wpa_s->sme.auth_alg = WPA_AUTH_ALG_OPEN;
2638 }
2639#endif /* CONFIG_SME */
2640
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002641 wpas_abort_ongoing_scan(wpa_s);
2642
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002643 cwork = os_zalloc(sizeof(*cwork));
2644 if (cwork == NULL)
2645 return;
2646
2647 cwork->bss = bss;
2648 cwork->ssid = ssid;
2649
2650 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
2651 wpas_start_assoc_cb, cwork) < 0) {
2652 os_free(cwork);
2653 }
2654}
2655
2656
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002657static int bss_is_ibss(struct wpa_bss *bss)
2658{
2659 return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
2660 IEEE80211_CAP_IBSS;
2661}
2662
2663
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002664static int drv_supports_vht(struct wpa_supplicant *wpa_s,
2665 const struct wpa_ssid *ssid)
2666{
2667 enum hostapd_hw_mode hw_mode;
2668 struct hostapd_hw_modes *mode = NULL;
2669 u8 channel;
2670 int i;
2671
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002672 hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
2673 if (hw_mode == NUM_HOSTAPD_MODES)
2674 return 0;
2675 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
2676 if (wpa_s->hw.modes[i].mode == hw_mode) {
2677 mode = &wpa_s->hw.modes[i];
2678 break;
2679 }
2680 }
2681
2682 if (!mode)
2683 return 0;
2684
2685 return mode->vht_capab != 0;
2686}
2687
2688
Hai Shalomc1a21442022-02-04 13:43:00 -08002689static bool ibss_mesh_is_80mhz_avail(int channel, struct hostapd_hw_modes *mode)
2690{
2691 int i;
2692
2693 for (i = channel; i < channel + 16; i += 4) {
2694 struct hostapd_channel_data *chan;
2695
2696 chan = hw_get_channel_chan(mode, i, NULL);
2697 if (!chan ||
2698 chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2699 return false;
2700 }
2701
2702 return true;
2703}
2704
2705
Sunil Ravi036cec52023-03-29 11:35:17 -07002706static struct wpa_bss * ibss_find_existing_bss(struct wpa_supplicant *wpa_s,
2707 const struct wpa_ssid *ssid)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002708{
Sunil Ravi036cec52023-03-29 11:35:17 -07002709 unsigned int j;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002710
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002711 for (j = 0; j < wpa_s->last_scan_res_used; j++) {
2712 struct wpa_bss *bss = wpa_s->last_scan_res[j];
2713
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002714 if (!bss_is_ibss(bss))
2715 continue;
2716
2717 if (ssid->ssid_len == bss->ssid_len &&
Sunil Ravi036cec52023-03-29 11:35:17 -07002718 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0)
2719 return bss;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002720 }
Sunil Ravi036cec52023-03-29 11:35:17 -07002721 return NULL;
2722}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002723
Sunil Ravi036cec52023-03-29 11:35:17 -07002724
2725static bool ibss_mesh_can_use_ht(struct wpa_supplicant *wpa_s,
2726 const struct wpa_ssid *ssid,
2727 struct hostapd_hw_modes *mode)
2728{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002729 /* For IBSS check HT_IBSS flag */
2730 if (ssid->mode == WPAS_MODE_IBSS &&
2731 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
Sunil Ravi036cec52023-03-29 11:35:17 -07002732 return false;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002733
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002734 if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
2735 wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
2736 wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
2737 wpa_printf(MSG_DEBUG,
2738 "IBSS: WEP/TKIP detected, do not try to enable HT");
Sunil Ravi036cec52023-03-29 11:35:17 -07002739 return false;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002740 }
2741
Sunil Ravi036cec52023-03-29 11:35:17 -07002742 if (!ht_supported(mode))
2743 return false;
Hai Shalomc1a21442022-02-04 13:43:00 -08002744
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002745#ifdef CONFIG_HT_OVERRIDES
Sunil Ravi036cec52023-03-29 11:35:17 -07002746 if (ssid->disable_ht)
2747 return false;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002748#endif /* CONFIG_HT_OVERRIDES */
2749
Sunil Ravi036cec52023-03-29 11:35:17 -07002750 return true;
2751}
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002752
Sunil Ravi036cec52023-03-29 11:35:17 -07002753
2754static bool ibss_mesh_can_use_vht(struct wpa_supplicant *wpa_s,
2755 const struct wpa_ssid *ssid,
2756 struct hostapd_hw_modes *mode)
2757{
2758 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
2759 return false;
2760
2761 if (!drv_supports_vht(wpa_s, ssid))
2762 return false;
2763
2764 /* For IBSS check VHT_IBSS flag */
2765 if (ssid->mode == WPAS_MODE_IBSS &&
2766 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
2767 return false;
2768
2769 if (!vht_supported(mode))
2770 return false;
2771
2772#ifdef CONFIG_VHT_OVERRIDES
2773 if (ssid->disable_vht)
2774 return false;
2775#endif /* CONFIG_VHT_OVERRIDES */
2776
2777 return true;
2778}
2779
2780
2781static bool ibss_mesh_can_use_he(struct wpa_supplicant *wpa_s,
2782 const struct wpa_ssid *ssid,
2783 const struct hostapd_hw_modes *mode,
2784 int ieee80211_mode)
2785{
Hai Shalomfdcde762020-04-02 11:19:20 -07002786#ifdef CONFIG_HE_OVERRIDES
Sunil Ravi036cec52023-03-29 11:35:17 -07002787 if (ssid->disable_he)
2788 return false;
Hai Shalomfdcde762020-04-02 11:19:20 -07002789#endif /* CONFIG_HE_OVERRIDES */
Hai Shalomc3565922019-10-28 11:58:20 -07002790
Sunil Ravi036cec52023-03-29 11:35:17 -07002791 switch (mode->mode) {
2792 case HOSTAPD_MODE_IEEE80211G:
2793 case HOSTAPD_MODE_IEEE80211B:
2794 case HOSTAPD_MODE_IEEE80211A:
2795 return mode->he_capab[ieee80211_mode].he_supported;
2796 default:
2797 return false;
2798 }
2799}
2800
2801
2802static bool ibss_mesh_can_use_eht(struct wpa_supplicant *wpa_s,
2803 const struct wpa_ssid *ssid,
2804 const struct hostapd_hw_modes *mode,
2805 int ieee80211_mode)
2806{
2807 if (ssid->disable_eht)
2808 return false;
2809
2810 switch(mode->mode) {
2811 case HOSTAPD_MODE_IEEE80211G:
2812 case HOSTAPD_MODE_IEEE80211B:
2813 case HOSTAPD_MODE_IEEE80211A:
2814 return mode->eht_capab[ieee80211_mode].eht_supported;
2815 default:
2816 return false;
2817 }
2818}
2819
2820
2821static void ibss_mesh_select_40mhz(struct wpa_supplicant *wpa_s,
2822 const struct wpa_ssid *ssid,
2823 struct hostapd_hw_modes *mode,
2824 struct hostapd_freq_params *freq,
2825 int obss_scan) {
2826 int chan_idx;
2827 struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
2828 int i, res;
2829 unsigned int j;
2830 static const int ht40plus[] = {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002831 36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
2832 149, 157, 165, 173, 184, 192
Sunil Ravi036cec52023-03-29 11:35:17 -07002833 };
2834 int ht40 = -1;
2835
2836 if (!freq->ht_enabled)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002837 return;
2838
2839 for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
2840 pri_chan = &mode->channels[chan_idx];
Sunil Ravi036cec52023-03-29 11:35:17 -07002841 if (pri_chan->chan == freq->channel)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002842 break;
2843 pri_chan = NULL;
2844 }
2845 if (!pri_chan)
2846 return;
2847
2848 /* Check primary channel flags */
2849 if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2850 return;
2851
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002852#ifdef CONFIG_HT_OVERRIDES
Sunil Ravi036cec52023-03-29 11:35:17 -07002853 if (ssid->disable_ht40)
2854 return;
2855#endif
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002856
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002857 /* Check/setup HT40+/HT40- */
2858 for (j = 0; j < ARRAY_SIZE(ht40plus); j++) {
Sunil Ravi036cec52023-03-29 11:35:17 -07002859 if (ht40plus[j] == freq->channel) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002860 ht40 = 1;
2861 break;
2862 }
2863 }
2864
2865 /* Find secondary channel */
2866 for (i = 0; i < mode->num_channels; i++) {
2867 sec_chan = &mode->channels[i];
Sunil Ravi036cec52023-03-29 11:35:17 -07002868 if (sec_chan->chan == freq->channel + ht40 * 4)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002869 break;
2870 sec_chan = NULL;
2871 }
2872 if (!sec_chan)
2873 return;
2874
2875 /* Check secondary channel flags */
2876 if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2877 return;
2878
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002879 if (ht40 == -1) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002880 if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
2881 return;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002882 } else {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002883 if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
2884 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002885 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002886 freq->sec_channel_offset = ht40;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002887
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002888 if (obss_scan) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002889 struct wpa_scan_results *scan_res;
2890
2891 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2892 if (scan_res == NULL) {
2893 /* Back to HT20 */
2894 freq->sec_channel_offset = 0;
2895 return;
2896 }
2897
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002898 res = check_40mhz_5g(scan_res, pri_chan, sec_chan);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002899 switch (res) {
2900 case 0:
2901 /* Back to HT20 */
2902 freq->sec_channel_offset = 0;
2903 break;
2904 case 1:
2905 /* Configuration allowed */
2906 break;
2907 case 2:
2908 /* Switch pri/sec channels */
2909 freq->freq = hw_get_freq(mode, sec_chan->chan);
2910 freq->sec_channel_offset = -freq->sec_channel_offset;
2911 freq->channel = sec_chan->chan;
2912 break;
2913 default:
2914 freq->sec_channel_offset = 0;
2915 break;
2916 }
2917
2918 wpa_scan_results_free(scan_res);
2919 }
2920
2921 wpa_printf(MSG_DEBUG,
2922 "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
2923 freq->channel, freq->sec_channel_offset);
Sunil Ravi036cec52023-03-29 11:35:17 -07002924}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002925
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002926
Sunil Ravi036cec52023-03-29 11:35:17 -07002927static bool ibss_mesh_select_80_160mhz(struct wpa_supplicant *wpa_s,
2928 const struct wpa_ssid *ssid,
2929 struct hostapd_hw_modes *mode,
2930 struct hostapd_freq_params *freq,
2931 int ieee80211_mode, bool is_6ghz) {
2932 static const int bw80[] = {
2933 5180, 5260, 5500, 5580, 5660, 5745, 5825,
2934 5955, 6035, 6115, 6195, 6275, 6355, 6435,
2935 6515, 6595, 6675, 6755, 6835, 6915, 6995
2936 };
2937 static const int bw160[] = {
2938 5955, 6115, 6275, 6435, 6595, 6755, 6915
2939 };
2940 struct hostapd_freq_params vht_freq;
2941 int i;
2942 unsigned int j, k;
2943 int chwidth, seg0, seg1;
2944 u32 vht_caps = 0;
2945 u8 channel = freq->channel;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002946
Sunil Ravi036cec52023-03-29 11:35:17 -07002947 if (!freq->vht_enabled && !freq->he_enabled)
2948 return true;
Paul Stewart092955c2017-02-06 09:13:09 -08002949
Hai Shalomc1a21442022-02-04 13:43:00 -08002950 vht_freq = *freq;
2951
Sunil Ravi036cec52023-03-29 11:35:17 -07002952 chwidth = CONF_OPER_CHWIDTH_USE_HT;
2953 seg0 = freq->channel + 2 * freq->sec_channel_offset;
2954 seg1 = 0;
2955 if (freq->sec_channel_offset == 0) {
2956 seg0 = 0;
2957 /* Don't try 80 MHz if 40 MHz failed, except in 6 GHz */
2958 if (freq->ht_enabled && !is_6ghz)
2959 goto skip_80mhz;
2960 }
2961 if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_USE_HT)
2962 goto skip_80mhz;
Hai Shalomc3565922019-10-28 11:58:20 -07002963
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002964 /* setup center_freq1, bandwidth */
Hai Shalomc1a21442022-02-04 13:43:00 -08002965 for (j = 0; j < ARRAY_SIZE(bw80); j++) {
2966 if (freq->freq >= bw80[j] &&
2967 freq->freq < bw80[j] + 80)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002968 break;
2969 }
2970
Hai Shalomc1a21442022-02-04 13:43:00 -08002971 if (j == ARRAY_SIZE(bw80) ||
2972 ieee80211_freq_to_chan(bw80[j], &channel) == NUM_HOSTAPD_MODES)
Sunil Ravi036cec52023-03-29 11:35:17 -07002973 goto skip_80mhz;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002974
Sunil Ravi036cec52023-03-29 11:35:17 -07002975 /* Use 40 MHz if channel not usable */
Hai Shalomc1a21442022-02-04 13:43:00 -08002976 if (!ibss_mesh_is_80mhz_avail(channel, mode))
Sunil Ravi036cec52023-03-29 11:35:17 -07002977 goto skip_80mhz;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002978
Sunil8cd6f4d2022-06-28 18:40:46 +00002979 chwidth = CONF_OPER_CHWIDTH_80MHZ;
Hai Shalomc1a21442022-02-04 13:43:00 -08002980 seg0 = channel + 6;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002981 seg1 = 0;
2982
Sunil Ravi036cec52023-03-29 11:35:17 -07002983 /* In 160 MHz, the initial four 20 MHz channels were validated
2984 * above. If 160 MHz is supported, check the remaining four 20 MHz
2985 * channels for the total of 160 MHz bandwidth for 6 GHz.
2986 */
Hai Shalomc1a21442022-02-04 13:43:00 -08002987 if ((mode->he_capab[ieee80211_mode].phy_cap[
2988 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
Sunil Ravi036cec52023-03-29 11:35:17 -07002989 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G) && is_6ghz &&
2990 ibss_mesh_is_80mhz_avail(channel + 16, mode)) {
Hai Shalomc1a21442022-02-04 13:43:00 -08002991 for (j = 0; j < ARRAY_SIZE(bw160); j++) {
2992 if (freq->freq == bw160[j]) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002993 chwidth = CONF_OPER_CHWIDTH_160MHZ;
Hai Shalomc1a21442022-02-04 13:43:00 -08002994 seg0 = channel + 14;
2995 break;
2996 }
2997 }
2998 }
2999
Sunil8cd6f4d2022-06-28 18:40:46 +00003000 if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003001 /* setup center_freq2, bandwidth */
Hai Shalomc1a21442022-02-04 13:43:00 -08003002 for (k = 0; k < ARRAY_SIZE(bw80); k++) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003003 /* Only accept 80 MHz segments separated by a gap */
Hai Shalomc1a21442022-02-04 13:43:00 -08003004 if (j == k || abs(bw80[j] - bw80[k]) == 80)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003005 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08003006
3007 if (ieee80211_freq_to_chan(bw80[k], &channel) ==
3008 NUM_HOSTAPD_MODES)
Sunil Ravi036cec52023-03-29 11:35:17 -07003009 break;
Hai Shalomc1a21442022-02-04 13:43:00 -08003010
3011 for (i = channel; i < channel + 16; i += 4) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003012 struct hostapd_channel_data *chan;
3013
3014 chan = hw_get_channel_chan(mode, i, NULL);
3015 if (!chan)
3016 continue;
3017
3018 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
3019 HOSTAPD_CHAN_NO_IR |
3020 HOSTAPD_CHAN_RADAR))
3021 continue;
3022
3023 /* Found a suitable second segment for 80+80 */
Sunil8cd6f4d2022-06-28 18:40:46 +00003024 chwidth = CONF_OPER_CHWIDTH_80P80MHZ;
Hai Shalomc1a21442022-02-04 13:43:00 -08003025 if (!is_6ghz)
3026 vht_caps |=
3027 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
3028 seg1 = channel + 6;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003029 }
3030
Sunil8cd6f4d2022-06-28 18:40:46 +00003031 if (chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003032 break;
3033 }
Sunil8cd6f4d2022-06-28 18:40:46 +00003034 } else if (ssid->max_oper_chwidth == CONF_OPER_CHWIDTH_160MHZ) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003035 if (freq->freq == 5180) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003036 chwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003037 vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
3038 seg0 = 50;
3039 } else if (freq->freq == 5520) {
Sunil8cd6f4d2022-06-28 18:40:46 +00003040 chwidth = CONF_OPER_CHWIDTH_160MHZ;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003041 vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
3042 seg0 = 114;
3043 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003044 }
3045
Sunil Ravi036cec52023-03-29 11:35:17 -07003046skip_80mhz:
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003047 if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
Hai Shalomc3565922019-10-28 11:58:20 -07003048 freq->channel, ssid->enable_edmg,
3049 ssid->edmg_channel, freq->ht_enabled,
Sunil Ravi036cec52023-03-29 11:35:17 -07003050 freq->vht_enabled, freq->he_enabled,
3051 freq->eht_enabled,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003052 freq->sec_channel_offset,
Hai Shalom81f62d82019-07-22 12:10:00 -07003053 chwidth, seg0, seg1, vht_caps,
Sunil Ravia04bd252022-05-02 22:54:18 -07003054 &mode->he_capab[ieee80211_mode],
Sunil Ravi036cec52023-03-29 11:35:17 -07003055 &mode->eht_capab[ieee80211_mode]) != 0)
3056 return false;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003057
3058 *freq = vht_freq;
3059
3060 wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
3061 freq->center_freq1, freq->center_freq2, freq->bandwidth);
Sunil Ravi036cec52023-03-29 11:35:17 -07003062 return true;
3063}
3064
3065
3066void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
3067 const struct wpa_ssid *ssid,
3068 struct hostapd_freq_params *freq)
3069{
3070 int ieee80211_mode = wpas_mode_to_ieee80211_mode(ssid->mode);
3071 enum hostapd_hw_mode hw_mode;
3072 struct hostapd_hw_modes *mode = NULL;
3073 int i, obss_scan = 1;
3074 u8 channel;
3075 bool is_6ghz;
3076
3077 freq->freq = ssid->frequency;
3078
3079 if (ssid->mode == WPAS_MODE_IBSS && !ssid->fixed_freq) {
3080 struct wpa_bss *bss = ibss_find_existing_bss(wpa_s, ssid);
3081
3082 if (bss) {
3083 wpa_printf(MSG_DEBUG,
3084 "IBSS already found in scan results, adjust control freq: %d",
3085 bss->freq);
3086 freq->freq = bss->freq;
3087 obss_scan = 0;
3088 }
3089 }
3090
3091 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
3092 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
3093 if (wpa_s->hw.modes[i].mode == hw_mode &&
3094 hw_mode_get_channel(&wpa_s->hw.modes[i], freq->freq,
3095 NULL) != NULL) {
3096 mode = &wpa_s->hw.modes[i];
3097 break;
3098 }
3099 }
3100
3101 if (!mode)
3102 return;
3103
3104 is_6ghz = is_6ghz_freq(freq->freq);
3105
3106 freq->ht_enabled = 0;
3107 freq->vht_enabled = 0;
3108 freq->he_enabled = 0;
3109 freq->eht_enabled = 0;
3110
3111 if (!is_6ghz)
3112 freq->ht_enabled = ibss_mesh_can_use_ht(wpa_s, ssid, mode);
3113 if (freq->ht_enabled)
3114 freq->vht_enabled = ibss_mesh_can_use_vht(wpa_s, ssid, mode);
3115 if (freq->vht_enabled || is_6ghz)
3116 freq->he_enabled = ibss_mesh_can_use_he(wpa_s, ssid, mode,
3117 ieee80211_mode);
3118 freq->channel = channel;
3119 /* Setup higher BW only for 5 GHz */
3120 if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
3121 ibss_mesh_select_40mhz(wpa_s, ssid, mode, freq, obss_scan);
3122 if (!ibss_mesh_select_80_160mhz(wpa_s, ssid, mode, freq,
3123 ieee80211_mode, is_6ghz))
3124 freq->he_enabled = freq->vht_enabled = false;
3125 }
3126
3127 if (freq->he_enabled)
3128 freq->eht_enabled = ibss_mesh_can_use_eht(wpa_s, ssid, mode,
3129 ieee80211_mode);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003130}
3131
3132
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003133#ifdef CONFIG_FILS
3134static size_t wpas_add_fils_hlp_req(struct wpa_supplicant *wpa_s, u8 *ie_buf,
3135 size_t ie_buf_len)
3136{
3137 struct fils_hlp_req *req;
3138 size_t rem_len, hdr_len, hlp_len, len, ie_len = 0;
3139 const u8 *pos;
3140 u8 *buf = ie_buf;
3141
3142 dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
3143 list) {
3144 rem_len = ie_buf_len - ie_len;
3145 pos = wpabuf_head(req->pkt);
3146 hdr_len = 1 + 2 * ETH_ALEN + 6;
3147 hlp_len = wpabuf_len(req->pkt);
3148
3149 if (rem_len < 2 + hdr_len + hlp_len) {
3150 wpa_printf(MSG_ERROR,
3151 "FILS: Cannot fit HLP - rem_len=%lu to_fill=%lu",
3152 (unsigned long) rem_len,
3153 (unsigned long) (2 + hdr_len + hlp_len));
3154 break;
3155 }
3156
3157 len = (hdr_len + hlp_len) > 255 ? 255 : hdr_len + hlp_len;
3158 /* Element ID */
3159 *buf++ = WLAN_EID_EXTENSION;
3160 /* Length */
3161 *buf++ = len;
3162 /* Element ID Extension */
3163 *buf++ = WLAN_EID_EXT_FILS_HLP_CONTAINER;
3164 /* Destination MAC address */
3165 os_memcpy(buf, req->dst, ETH_ALEN);
3166 buf += ETH_ALEN;
3167 /* Source MAC address */
3168 os_memcpy(buf, wpa_s->own_addr, ETH_ALEN);
3169 buf += ETH_ALEN;
3170 /* LLC/SNAP Header */
3171 os_memcpy(buf, "\xaa\xaa\x03\x00\x00\x00", 6);
3172 buf += 6;
3173 /* HLP Packet */
3174 os_memcpy(buf, pos, len - hdr_len);
3175 buf += len - hdr_len;
3176 pos += len - hdr_len;
3177
3178 hlp_len -= len - hdr_len;
3179 ie_len += 2 + len;
3180 rem_len -= 2 + len;
3181
3182 while (hlp_len) {
3183 len = (hlp_len > 255) ? 255 : hlp_len;
3184 if (rem_len < 2 + len)
3185 break;
3186 *buf++ = WLAN_EID_FRAGMENT;
3187 *buf++ = len;
3188 os_memcpy(buf, pos, len);
3189 buf += len;
3190 pos += len;
3191
3192 hlp_len -= len;
3193 ie_len += 2 + len;
3194 rem_len -= 2 + len;
3195 }
3196 }
3197
3198 return ie_len;
3199}
Roshan Pius3a1667e2018-07-03 15:17:14 -07003200
3201
3202int wpa_is_fils_supported(struct wpa_supplicant *wpa_s)
3203{
3204 return (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3205 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) ||
3206 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3207 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)));
3208}
3209
3210
3211int wpa_is_fils_sk_pfs_supported(struct wpa_supplicant *wpa_s)
3212{
3213#ifdef CONFIG_FILS_SK_PFS
3214 return (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3215 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS);
3216#else /* CONFIG_FILS_SK_PFS */
3217 return 0;
3218#endif /* CONFIG_FILS_SK_PFS */
3219}
3220
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003221#endif /* CONFIG_FILS */
3222
3223
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003224bool wpa_is_non_eht_scs_traffic_desc_supported(struct wpa_bss *bss)
3225{
3226 const u8 *wfa_capa;
3227
3228 if (!bss)
3229 return false;
3230
3231 /* Get WFA capability from Beacon or Probe Response frame elements */
3232 wfa_capa = wpa_bss_get_vendor_ie(bss, WFA_CAPA_IE_VENDOR_TYPE);
3233 if (!wfa_capa)
3234 wfa_capa = wpa_bss_get_vendor_ie_beacon(
3235 bss, WFA_CAPA_IE_VENDOR_TYPE);
3236
3237 if (!wfa_capa || wfa_capa[1] < 6 || wfa_capa[6] < 1 ||
3238 !(wfa_capa[7] & WFA_CAPA_QM_NON_EHT_SCS_TRAFFIC_DESC)) {
3239 /* AP does not enable QM non EHT traffic description policy */
3240 return false;
3241 }
3242
3243 return true;
3244}
3245
3246
Hai Shalomc1a21442022-02-04 13:43:00 -08003247static int wpas_populate_wfa_capa(struct wpa_supplicant *wpa_s,
3248 struct wpa_bss *bss,
3249 u8 *wpa_ie, size_t wpa_ie_len,
3250 size_t max_wpa_ie_len)
3251{
3252 struct wpabuf *wfa_ie = NULL;
3253 u8 wfa_capa[1];
3254 size_t wfa_ie_len, buf_len;
3255
3256 os_memset(wfa_capa, 0, sizeof(wfa_capa));
3257 if (wpa_s->enable_dscp_policy_capa)
3258 wfa_capa[0] |= WFA_CAPA_QM_DSCP_POLICY;
3259
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003260 if (wpa_is_non_eht_scs_traffic_desc_supported(bss))
3261 wfa_capa[0] |= WFA_CAPA_QM_NON_EHT_SCS_TRAFFIC_DESC;
3262
Hai Shalomc1a21442022-02-04 13:43:00 -08003263 if (!wfa_capa[0])
3264 return wpa_ie_len;
3265
3266 /* Wi-Fi Alliance element */
3267 buf_len = 1 + /* Element ID */
3268 1 + /* Length */
3269 3 + /* OUI */
3270 1 + /* OUI Type */
3271 1 + /* Capabilities Length */
3272 sizeof(wfa_capa); /* Capabilities */
3273 wfa_ie = wpabuf_alloc(buf_len);
3274 if (!wfa_ie)
3275 return wpa_ie_len;
3276
3277 wpabuf_put_u8(wfa_ie, WLAN_EID_VENDOR_SPECIFIC);
3278 wpabuf_put_u8(wfa_ie, buf_len - 2);
3279 wpabuf_put_be24(wfa_ie, OUI_WFA);
3280 wpabuf_put_u8(wfa_ie, WFA_CAPA_OUI_TYPE);
3281 wpabuf_put_u8(wfa_ie, sizeof(wfa_capa));
3282 wpabuf_put_data(wfa_ie, wfa_capa, sizeof(wfa_capa));
3283
3284 wfa_ie_len = wpabuf_len(wfa_ie);
3285 if (wpa_ie_len + wfa_ie_len <= max_wpa_ie_len) {
3286 wpa_hexdump_buf(MSG_MSGDUMP, "WFA Capabilities element",
3287 wfa_ie);
3288 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(wfa_ie),
3289 wfa_ie_len);
3290 wpa_ie_len += wfa_ie_len;
3291 }
3292
3293 wpabuf_free(wfa_ie);
3294 return wpa_ie_len;
3295}
3296
3297
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003298static u8 * wpas_populate_assoc_ies(
3299 struct wpa_supplicant *wpa_s,
3300 struct wpa_bss *bss, struct wpa_ssid *ssid,
3301 struct wpa_driver_associate_params *params,
3302 enum wpa_drv_update_connect_params_mask *mask)
3303{
3304 u8 *wpa_ie;
3305 size_t max_wpa_ie_len = 500;
3306 size_t wpa_ie_len;
3307 int algs = WPA_AUTH_ALG_OPEN;
Hai Shalomce48b4a2018-09-05 11:41:35 -07003308#ifdef CONFIG_MBO
3309 const u8 *mbo_ie;
3310#endif
Vinita S. Maloofb3a4442020-05-19 17:43:22 +05303311#if defined(CONFIG_SAE) || defined(CONFIG_FILS)
3312 int pmksa_cached = 0;
3313#endif /* CONFIG_SAE || CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003314#ifdef CONFIG_FILS
3315 const u8 *realm, *username, *rrk;
3316 size_t realm_len, username_len, rrk_len;
3317 u16 next_seq_num;
3318 struct fils_hlp_req *req;
3319
3320 dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
3321 list) {
3322 max_wpa_ie_len += 3 + 2 * ETH_ALEN + 6 + wpabuf_len(req->pkt) +
3323 2 + 2 * wpabuf_len(req->pkt) / 255;
3324 }
3325#endif /* CONFIG_FILS */
3326
3327 wpa_ie = os_malloc(max_wpa_ie_len);
3328 if (!wpa_ie) {
3329 wpa_printf(MSG_ERROR,
3330 "Failed to allocate connect IE buffer for %lu bytes",
3331 (unsigned long) max_wpa_ie_len);
3332 return NULL;
3333 }
3334
3335 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
3336 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
3337 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
3338 int try_opportunistic;
3339 const u8 *cache_id = NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003340 const u8 *addr = bss->bssid;
3341
Sunil Ravi036cec52023-03-29 11:35:17 -07003342 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3343 (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_MLO) &&
3344 !is_zero_ether_addr(bss->mld_addr))
3345 addr = bss->mld_addr;
3346
3347 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3348 wpa_s->valid_links)
Sunil Ravi77d572f2023-01-17 23:58:31 +00003349 addr = wpa_s->ap_mld_addr;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003350
3351 try_opportunistic = (ssid->proactive_key_caching < 0 ?
3352 wpa_s->conf->okc :
3353 ssid->proactive_key_caching) &&
3354 (ssid->proto & WPA_PROTO_RSN);
3355#ifdef CONFIG_FILS
3356 if (wpa_key_mgmt_fils(ssid->key_mgmt))
3357 cache_id = wpa_bss_get_fils_cache_id(bss);
3358#endif /* CONFIG_FILS */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003359 if (pmksa_cache_set_current(wpa_s->wpa, NULL, addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003360 ssid, try_opportunistic,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003361 cache_id, 0, false) == 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003362 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Vinita S. Maloofb3a4442020-05-19 17:43:22 +05303363#if defined(CONFIG_SAE) || defined(CONFIG_FILS)
3364 pmksa_cached = 1;
3365#endif /* CONFIG_SAE || CONFIG_FILS */
Hai Shalom5f92bc92019-04-18 11:54:11 -07003366 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003367 wpa_ie_len = max_wpa_ie_len;
3368 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003369 wpa_ie, &wpa_ie_len, false)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003370 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
3371 "key management and encryption suites");
3372 os_free(wpa_ie);
3373 return NULL;
3374 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003375#ifdef CONFIG_HS20
3376 } else if (bss && wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE) &&
3377 (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)) {
3378 /* No PMKSA caching, but otherwise similar to RSN/WPA */
3379 wpa_ie_len = max_wpa_ie_len;
3380 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003381 wpa_ie, &wpa_ie_len, false)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003382 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
3383 "key management and encryption suites");
3384 os_free(wpa_ie);
3385 return NULL;
3386 }
3387#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003388 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
3389 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
3390 /*
3391 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
3392 * use non-WPA since the scan results did not indicate that the
3393 * AP is using WPA or WPA2.
3394 */
3395 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
3396 wpa_ie_len = 0;
3397 wpa_s->wpa_proto = 0;
3398 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
3399 wpa_ie_len = max_wpa_ie_len;
3400 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003401 wpa_ie, &wpa_ie_len, false)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003402 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
3403 "key management and encryption suites (no "
3404 "scan results)");
3405 os_free(wpa_ie);
3406 return NULL;
3407 }
3408#ifdef CONFIG_WPS
3409 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
3410 struct wpabuf *wps_ie;
3411 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
3412 if (wps_ie && wpabuf_len(wps_ie) <= max_wpa_ie_len) {
3413 wpa_ie_len = wpabuf_len(wps_ie);
3414 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
3415 } else
3416 wpa_ie_len = 0;
3417 wpabuf_free(wps_ie);
3418 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
3419 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
3420 params->wps = WPS_MODE_PRIVACY;
3421 else
3422 params->wps = WPS_MODE_OPEN;
3423 wpa_s->wpa_proto = 0;
3424#endif /* CONFIG_WPS */
3425 } else {
3426 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
3427 wpa_ie_len = 0;
3428 wpa_s->wpa_proto = 0;
3429 }
3430
3431#ifdef IEEE8021X_EAPOL
3432 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3433 if (ssid->leap) {
3434 if (ssid->non_leap == 0)
3435 algs = WPA_AUTH_ALG_LEAP;
3436 else
3437 algs |= WPA_AUTH_ALG_LEAP;
3438 }
3439 }
3440
3441#ifdef CONFIG_FILS
3442 /* Clear FILS association */
3443 wpa_sm_set_reset_fils_completed(wpa_s->wpa, 0);
3444
3445 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
3446 ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
3447 eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap, &username,
3448 &username_len, &realm, &realm_len,
Hai Shalomce48b4a2018-09-05 11:41:35 -07003449 &next_seq_num, &rrk, &rrk_len) == 0 &&
3450 (!wpa_s->last_con_fail_realm ||
3451 wpa_s->last_con_fail_realm_len != realm_len ||
3452 os_memcmp(wpa_s->last_con_fail_realm, realm, realm_len) != 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003453 algs = WPA_AUTH_ALG_FILS;
3454 params->fils_erp_username = username;
3455 params->fils_erp_username_len = username_len;
3456 params->fils_erp_realm = realm;
3457 params->fils_erp_realm_len = realm_len;
3458 params->fils_erp_next_seq_num = next_seq_num;
3459 params->fils_erp_rrk = rrk;
3460 params->fils_erp_rrk_len = rrk_len;
3461
3462 if (mask)
3463 *mask |= WPA_DRV_UPDATE_FILS_ERP_INFO;
Vinita S. Maloofb3a4442020-05-19 17:43:22 +05303464 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
3465 ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
3466 pmksa_cached) {
3467 algs = WPA_AUTH_ALG_FILS;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003468 }
3469#endif /* CONFIG_FILS */
3470#endif /* IEEE8021X_EAPOL */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003471#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -07003472 if (wpa_key_mgmt_sae(wpa_s->key_mgmt))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003473 algs = WPA_AUTH_ALG_SAE;
3474#endif /* CONFIG_SAE */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003475
3476 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
3477 if (ssid->auth_alg) {
3478 algs = ssid->auth_alg;
3479 wpa_dbg(wpa_s, MSG_DEBUG,
3480 "Overriding auth_alg selection: 0x%x", algs);
3481 }
3482
Hai Shalom5f92bc92019-04-18 11:54:11 -07003483#ifdef CONFIG_SAE
Vinita S. Maloofb3a4442020-05-19 17:43:22 +05303484 if (pmksa_cached && algs == WPA_AUTH_ALG_SAE) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07003485 wpa_dbg(wpa_s, MSG_DEBUG,
3486 "SAE: Use WPA_AUTH_ALG_OPEN for PMKSA caching attempt");
3487 algs = WPA_AUTH_ALG_OPEN;
3488 }
3489#endif /* CONFIG_SAE */
3490
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003491#ifdef CONFIG_P2P
3492 if (wpa_s->global->p2p) {
3493 u8 *pos;
3494 size_t len;
3495 int res;
3496 pos = wpa_ie + wpa_ie_len;
3497 len = max_wpa_ie_len - wpa_ie_len;
3498 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
3499 ssid->p2p_group);
3500 if (res >= 0)
3501 wpa_ie_len += res;
3502 }
3503
3504 wpa_s->cross_connect_disallowed = 0;
3505 if (bss) {
3506 struct wpabuf *p2p;
3507 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3508 if (p2p) {
3509 wpa_s->cross_connect_disallowed =
3510 p2p_get_cross_connect_disallowed(p2p);
3511 wpabuf_free(p2p);
3512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
3513 "connection",
3514 wpa_s->cross_connect_disallowed ?
3515 "disallows" : "allows");
3516 }
3517 }
3518
3519 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
3520#endif /* CONFIG_P2P */
3521
3522 if (bss) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003523 wpa_ie_len += wpas_supp_op_class_ie(wpa_s, ssid, bss,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003524 wpa_ie + wpa_ie_len,
3525 max_wpa_ie_len -
3526 wpa_ie_len);
3527 }
3528
3529 /*
3530 * Workaround: Add Extended Capabilities element only if the AP
3531 * included this element in Beacon/Probe Response frames. Some older
3532 * APs seem to have interoperability issues if this element is
3533 * included, so while the standard may require us to include the
3534 * element in all cases, it is justifiable to skip it to avoid
3535 * interoperability issues.
3536 */
3537 if (ssid->p2p_group)
3538 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
3539 else
3540 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
3541
3542 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
3543 u8 ext_capab[18];
3544 int ext_capab_len;
3545 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003546 sizeof(ext_capab), bss);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003547 if (ext_capab_len > 0 &&
3548 wpa_ie_len + ext_capab_len <= max_wpa_ie_len) {
3549 u8 *pos = wpa_ie;
3550 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
3551 pos += 2 + pos[1];
3552 os_memmove(pos + ext_capab_len, pos,
3553 wpa_ie_len - (pos - wpa_ie));
3554 wpa_ie_len += ext_capab_len;
3555 os_memcpy(pos, ext_capab, ext_capab_len);
3556 }
3557 }
3558
3559#ifdef CONFIG_HS20
Hai Shalomcded4e22022-01-28 15:29:52 -08003560 if (is_hs20_network(wpa_s, ssid, bss)
3561#ifndef ANDROID /* Android does not use the native HS 2.0 config */
3562 && is_hs20_config(wpa_s)
3563#endif /* ANDROID */
3564 ) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003565 struct wpabuf *hs20;
3566
Roshan Pius3a1667e2018-07-03 15:17:14 -07003567 hs20 = wpabuf_alloc(20 + MAX_ROAMING_CONS_OI_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003568 if (hs20) {
3569 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
3570 size_t len;
3571
Hai Shalom74f70d42019-02-11 14:42:39 -08003572 wpas_hs20_add_indication(hs20, pps_mo_id,
3573 get_hs20_version(bss));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003574 wpas_hs20_add_roam_cons_sel(hs20, ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003575 len = max_wpa_ie_len - wpa_ie_len;
3576 if (wpabuf_len(hs20) <= len) {
3577 os_memcpy(wpa_ie + wpa_ie_len,
3578 wpabuf_head(hs20), wpabuf_len(hs20));
3579 wpa_ie_len += wpabuf_len(hs20);
3580 }
3581 wpabuf_free(hs20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003582 }
3583 }
Hai Shalom2cbbcd12021-03-08 18:33:38 -08003584 hs20_configure_frame_filters(wpa_s);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003585#endif /* CONFIG_HS20 */
3586
3587 if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
3588 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
3589 size_t len;
3590
3591 len = max_wpa_ie_len - wpa_ie_len;
3592 if (wpabuf_len(buf) <= len) {
3593 os_memcpy(wpa_ie + wpa_ie_len,
3594 wpabuf_head(buf), wpabuf_len(buf));
3595 wpa_ie_len += wpabuf_len(buf);
3596 }
3597 }
3598
3599#ifdef CONFIG_FST
3600 if (wpa_s->fst_ies) {
3601 int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
3602
3603 if (wpa_ie_len + fst_ies_len <= max_wpa_ie_len) {
3604 os_memcpy(wpa_ie + wpa_ie_len,
3605 wpabuf_head(wpa_s->fst_ies), fst_ies_len);
3606 wpa_ie_len += fst_ies_len;
3607 }
3608 }
3609#endif /* CONFIG_FST */
3610
3611#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -07003612 mbo_ie = bss ? wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE) : NULL;
Hai Shalomc3565922019-10-28 11:58:20 -07003613 if (!wpa_s->disable_mbo_oce && mbo_ie) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003614 int len;
3615
3616 len = wpas_mbo_ie(wpa_s, wpa_ie + wpa_ie_len,
Hai Shalomce48b4a2018-09-05 11:41:35 -07003617 max_wpa_ie_len - wpa_ie_len,
3618 !!mbo_attr_from_mbo_ie(mbo_ie,
3619 OCE_ATTR_ID_CAPA_IND));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003620 if (len >= 0)
3621 wpa_ie_len += len;
3622 }
3623#endif /* CONFIG_MBO */
3624
3625#ifdef CONFIG_FILS
3626 if (algs == WPA_AUTH_ALG_FILS) {
3627 size_t len;
3628
3629 len = wpas_add_fils_hlp_req(wpa_s, wpa_ie + wpa_ie_len,
3630 max_wpa_ie_len - wpa_ie_len);
3631 wpa_ie_len += len;
3632 }
3633#endif /* CONFIG_FILS */
3634
3635#ifdef CONFIG_OWE
Roshan Pius3a1667e2018-07-03 15:17:14 -07003636#ifdef CONFIG_TESTING_OPTIONS
3637 if (get_ie_ext(wpa_ie, wpa_ie_len, WLAN_EID_EXT_OWE_DH_PARAM)) {
3638 wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
3639 } else
3640#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003641 if (algs == WPA_AUTH_ALG_OPEN &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003642 ssid->key_mgmt == WPA_KEY_MGMT_OWE &&
3643 !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003644 struct wpabuf *owe_ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003645 u16 group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003646
Roshan Pius3a1667e2018-07-03 15:17:14 -07003647 if (ssid->owe_group) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003648 group = ssid->owe_group;
Hai Shalom74f70d42019-02-11 14:42:39 -08003649 } else if (wpa_s->assoc_status_code ==
3650 WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003651 if (wpa_s->last_owe_group == 19)
3652 group = 20;
3653 else if (wpa_s->last_owe_group == 20)
3654 group = 21;
3655 else
3656 group = OWE_DH_GROUP;
Hai Shalom74f70d42019-02-11 14:42:39 -08003657 } else {
3658 group = OWE_DH_GROUP;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003659 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003660
Roshan Pius3a1667e2018-07-03 15:17:14 -07003661 wpa_s->last_owe_group = group;
3662 wpa_printf(MSG_DEBUG, "OWE: Try to use group %u", group);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003663 owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
3664 if (owe_ie &&
3665 wpabuf_len(owe_ie) <= max_wpa_ie_len - wpa_ie_len) {
3666 os_memcpy(wpa_ie + wpa_ie_len,
3667 wpabuf_head(owe_ie), wpabuf_len(owe_ie));
3668 wpa_ie_len += wpabuf_len(owe_ie);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003669 }
Hai Shalom021b0b52019-04-10 11:17:58 -07003670 wpabuf_free(owe_ie);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003671 }
3672#endif /* CONFIG_OWE */
3673
Hai Shalom021b0b52019-04-10 11:17:58 -07003674#ifdef CONFIG_DPP2
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003675 if (DPP_VERSION > 1 &&
3676 wpa_sm_get_key_mgmt(wpa_s->wpa) == WPA_KEY_MGMT_DPP &&
Hai Shalomfdcde762020-04-02 11:19:20 -07003677 ssid->dpp_netaccesskey &&
3678 ssid->dpp_pfs != 2 && !ssid->dpp_pfs_fallback) {
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003679 struct rsn_pmksa_cache_entry *pmksa;
3680
3681 pmksa = pmksa_cache_get_current(wpa_s->wpa);
3682 if (!pmksa || !pmksa->dpp_pfs)
3683 goto pfs_fail;
3684
Hai Shalom021b0b52019-04-10 11:17:58 -07003685 dpp_pfs_free(wpa_s->dpp_pfs);
3686 wpa_s->dpp_pfs = dpp_pfs_init(ssid->dpp_netaccesskey,
3687 ssid->dpp_netaccesskey_len);
3688 if (!wpa_s->dpp_pfs) {
3689 wpa_printf(MSG_DEBUG, "DPP: Could not initialize PFS");
3690 /* Try to continue without PFS */
3691 goto pfs_fail;
3692 }
3693 if (wpabuf_len(wpa_s->dpp_pfs->ie) <=
3694 max_wpa_ie_len - wpa_ie_len) {
3695 os_memcpy(wpa_ie + wpa_ie_len,
3696 wpabuf_head(wpa_s->dpp_pfs->ie),
3697 wpabuf_len(wpa_s->dpp_pfs->ie));
3698 wpa_ie_len += wpabuf_len(wpa_s->dpp_pfs->ie);
3699 }
3700 }
3701pfs_fail:
3702#endif /* CONFIG_DPP2 */
3703
Roshan Pius3a1667e2018-07-03 15:17:14 -07003704#ifdef CONFIG_IEEE80211R
3705 /*
3706 * Add MDIE under these conditions: the network profile allows FT,
3707 * the AP supports FT, and the mobility domain ID matches.
3708 */
3709 if (bss && wpa_key_mgmt_ft(wpa_sm_get_key_mgmt(wpa_s->wpa))) {
3710 const u8 *mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
3711
3712 if (mdie && mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
3713 size_t len = 0;
3714 const u8 *md = mdie + 2;
3715 const u8 *wpa_md = wpa_sm_get_ft_md(wpa_s->wpa);
3716
3717 if (os_memcmp(md, wpa_md,
3718 MOBILITY_DOMAIN_ID_LEN) == 0) {
3719 /* Add mobility domain IE */
3720 len = wpa_ft_add_mdie(
3721 wpa_s->wpa, wpa_ie + wpa_ie_len,
3722 max_wpa_ie_len - wpa_ie_len, mdie);
3723 wpa_ie_len += len;
3724 }
3725#ifdef CONFIG_SME
3726 if (len > 0 && wpa_s->sme.ft_used &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00003727 wpa_sm_has_ft_keys(wpa_s->wpa, md)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003728 wpa_dbg(wpa_s, MSG_DEBUG,
3729 "SME: Trying to use FT over-the-air");
3730 algs |= WPA_AUTH_ALG_FT;
3731 }
3732#endif /* CONFIG_SME */
3733 }
3734 }
3735#endif /* CONFIG_IEEE80211R */
3736
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003737#ifdef CONFIG_TESTING_OPTIONS
3738 if (wpa_s->rsnxe_override_assoc &&
3739 wpabuf_len(wpa_s->rsnxe_override_assoc) <=
3740 max_wpa_ie_len - wpa_ie_len) {
3741 wpa_printf(MSG_DEBUG, "TESTING: RSNXE AssocReq override");
3742 os_memcpy(wpa_ie + wpa_ie_len,
3743 wpabuf_head(wpa_s->rsnxe_override_assoc),
3744 wpabuf_len(wpa_s->rsnxe_override_assoc));
3745 wpa_ie_len += wpabuf_len(wpa_s->rsnxe_override_assoc);
3746 } else
3747#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalomc3565922019-10-28 11:58:20 -07003748 if (wpa_s->rsnxe_len > 0 &&
3749 wpa_s->rsnxe_len <= max_wpa_ie_len - wpa_ie_len) {
3750 os_memcpy(wpa_ie + wpa_ie_len, wpa_s->rsnxe, wpa_s->rsnxe_len);
3751 wpa_ie_len += wpa_s->rsnxe_len;
3752 }
3753
Hai Shalomc1a21442022-02-04 13:43:00 -08003754#ifdef CONFIG_TESTING_OPTIONS
3755 if (wpa_s->disable_mscs_support)
3756 goto mscs_end;
3757#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom60840252021-02-19 19:02:11 -08003758 if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_MSCS) &&
3759 wpa_s->robust_av.valid_config) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003760 struct wpabuf *mscs_ie;
3761 size_t mscs_ie_len, buf_len;
3762
Hai Shalom899fcc72020-10-19 14:38:18 -07003763 buf_len = 3 + /* MSCS descriptor IE header */
3764 1 + /* Request type */
3765 2 + /* User priority control */
3766 4 + /* Stream timeout */
3767 3 + /* TCLAS Mask IE header */
3768 wpa_s->robust_av.frame_classifier_len;
3769 mscs_ie = wpabuf_alloc(buf_len);
3770 if (!mscs_ie) {
3771 wpa_printf(MSG_INFO,
3772 "MSCS: Failed to allocate MSCS IE");
Hai Shalomc1a21442022-02-04 13:43:00 -08003773 goto mscs_end;
Hai Shalom899fcc72020-10-19 14:38:18 -07003774 }
3775
3776 wpas_populate_mscs_descriptor_ie(&wpa_s->robust_av, mscs_ie);
3777 if ((wpa_ie_len + wpabuf_len(mscs_ie)) <= max_wpa_ie_len) {
3778 wpa_hexdump_buf(MSG_MSGDUMP, "MSCS IE", mscs_ie);
3779 mscs_ie_len = wpabuf_len(mscs_ie);
3780 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(mscs_ie),
3781 mscs_ie_len);
3782 wpa_ie_len += mscs_ie_len;
3783 }
3784
3785 wpabuf_free(mscs_ie);
3786 }
Hai Shalomc1a21442022-02-04 13:43:00 -08003787mscs_end:
3788
3789 wpa_ie_len = wpas_populate_wfa_capa(wpa_s, bss, wpa_ie, wpa_ie_len,
3790 max_wpa_ie_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07003791
Hai Shalom74f70d42019-02-11 14:42:39 -08003792 if (ssid->multi_ap_backhaul_sta) {
3793 size_t multi_ap_ie_len;
3794
3795 multi_ap_ie_len = add_multi_ap_ie(wpa_ie + wpa_ie_len,
3796 max_wpa_ie_len - wpa_ie_len,
3797 MULTI_AP_BACKHAUL_STA);
3798 if (multi_ap_ie_len == 0) {
3799 wpa_printf(MSG_ERROR,
3800 "Multi-AP: Failed to build Multi-AP IE");
3801 os_free(wpa_ie);
3802 return NULL;
3803 }
3804 wpa_ie_len += multi_ap_ie_len;
3805 }
3806
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003807 params->wpa_ie = wpa_ie;
3808 params->wpa_ie_len = wpa_ie_len;
3809 params->auth_alg = algs;
3810 if (mask)
3811 *mask |= WPA_DRV_UPDATE_ASSOC_IES | WPA_DRV_UPDATE_AUTH_TYPE;
3812
3813 return wpa_ie;
3814}
3815
3816
Hai Shalomc3565922019-10-28 11:58:20 -07003817#ifdef CONFIG_OWE
3818static void wpas_update_owe_connect_params(struct wpa_supplicant *wpa_s)
3819{
3820 struct wpa_driver_associate_params params;
3821 u8 *wpa_ie;
3822
3823 os_memset(&params, 0, sizeof(params));
3824 wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
3825 wpa_s->current_ssid, &params, NULL);
3826 if (!wpa_ie)
3827 return;
3828
3829 wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
3830 os_free(wpa_ie);
3831}
3832#endif /* CONFIG_OWE */
3833
3834
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003835#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
3836static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s)
3837{
3838 struct wpa_driver_associate_params params;
3839 enum wpa_drv_update_connect_params_mask mask = 0;
3840 u8 *wpa_ie;
3841
3842 if (wpa_s->auth_alg != WPA_AUTH_ALG_OPEN)
3843 return; /* nothing to do */
3844
3845 os_memset(&params, 0, sizeof(params));
3846 wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
3847 wpa_s->current_ssid, &params, &mask);
3848 if (!wpa_ie)
3849 return;
3850
Hai Shalomc1a21442022-02-04 13:43:00 -08003851 if (params.auth_alg == WPA_AUTH_ALG_FILS) {
3852 wpa_s->auth_alg = params.auth_alg;
3853 wpa_drv_update_connect_params(wpa_s, &params, mask);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003854 }
3855
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003856 os_free(wpa_ie);
3857}
3858#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
3859
3860
Hai Shalomc3565922019-10-28 11:58:20 -07003861static u8 wpa_ie_get_edmg_oper_chans(const u8 *edmg_ie)
3862{
3863 if (!edmg_ie || edmg_ie[1] < 6)
3864 return 0;
3865 return edmg_ie[EDMG_BSS_OPERATING_CHANNELS_OFFSET];
3866}
3867
3868
3869static u8 wpa_ie_get_edmg_oper_chan_width(const u8 *edmg_ie)
3870{
3871 if (!edmg_ie || edmg_ie[1] < 6)
3872 return 0;
3873 return edmg_ie[EDMG_OPERATING_CHANNEL_WIDTH_OFFSET];
3874}
3875
3876
3877/* Returns the intersection of two EDMG configurations.
3878 * Note: The current implementation is limited to CB2 only (CB1 included),
3879 * i.e., the implementation supports up to 2 contiguous channels.
3880 * For supporting non-contiguous (aggregated) channels and for supporting
3881 * CB3 and above, this function will need to be extended.
3882 */
3883static struct ieee80211_edmg_config
3884get_edmg_intersection(struct ieee80211_edmg_config a,
3885 struct ieee80211_edmg_config b,
3886 u8 primary_channel)
3887{
3888 struct ieee80211_edmg_config result;
3889 int i, contiguous = 0;
3890 int max_contiguous = 0;
3891
3892 result.channels = b.channels & a.channels;
3893 if (!result.channels) {
3894 wpa_printf(MSG_DEBUG,
3895 "EDMG not possible: cannot intersect channels 0x%x and 0x%x",
3896 a.channels, b.channels);
3897 goto fail;
3898 }
3899
3900 if (!(result.channels & BIT(primary_channel - 1))) {
3901 wpa_printf(MSG_DEBUG,
3902 "EDMG not possible: the primary channel %d is not one of the intersected channels 0x%x",
3903 primary_channel, result.channels);
3904 goto fail;
3905 }
3906
3907 /* Find max contiguous channels */
3908 for (i = 0; i < 6; i++) {
3909 if (result.channels & BIT(i))
3910 contiguous++;
3911 else
3912 contiguous = 0;
3913
3914 if (contiguous > max_contiguous)
3915 max_contiguous = contiguous;
3916 }
3917
3918 /* Assuming AP and STA supports ONLY contiguous channels,
3919 * bw configuration can have value between 4-7.
3920 */
3921 if ((b.bw_config < a.bw_config))
3922 result.bw_config = b.bw_config;
3923 else
3924 result.bw_config = a.bw_config;
3925
3926 if ((max_contiguous >= 2 && result.bw_config < EDMG_BW_CONFIG_5) ||
3927 (max_contiguous >= 1 && result.bw_config < EDMG_BW_CONFIG_4)) {
3928 wpa_printf(MSG_DEBUG,
3929 "EDMG not possible: not enough contiguous channels %d for supporting CB1 or CB2",
3930 max_contiguous);
3931 goto fail;
3932 }
3933
3934 return result;
3935
3936fail:
3937 result.channels = 0;
3938 result.bw_config = 0;
3939 return result;
3940}
3941
3942
3943static struct ieee80211_edmg_config
3944get_supported_edmg(struct wpa_supplicant *wpa_s,
3945 struct hostapd_freq_params *freq,
3946 struct ieee80211_edmg_config request_edmg)
3947{
3948 enum hostapd_hw_mode hw_mode;
3949 struct hostapd_hw_modes *mode = NULL;
3950 u8 primary_channel;
3951
3952 if (!wpa_s->hw.modes)
3953 goto fail;
3954
3955 hw_mode = ieee80211_freq_to_chan(freq->freq, &primary_channel);
3956 if (hw_mode == NUM_HOSTAPD_MODES)
3957 goto fail;
3958
Hai Shalom60840252021-02-19 19:02:11 -08003959 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, hw_mode, false);
Hai Shalomc3565922019-10-28 11:58:20 -07003960 if (!mode)
3961 goto fail;
3962
3963 return get_edmg_intersection(mode->edmg, request_edmg, primary_channel);
3964
3965fail:
3966 request_edmg.channels = 0;
3967 request_edmg.bw_config = 0;
3968 return request_edmg;
3969}
3970
3971
Hai Shalom021b0b52019-04-10 11:17:58 -07003972#ifdef CONFIG_MBO
3973void wpas_update_mbo_connect_params(struct wpa_supplicant *wpa_s)
3974{
3975 struct wpa_driver_associate_params params;
3976 u8 *wpa_ie;
3977
3978 /*
3979 * Update MBO connect params only in case of change of MBO attributes
3980 * when connected, if the AP support MBO.
3981 */
3982
3983 if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_ssid ||
3984 !wpa_s->current_bss ||
3985 !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
3986 return;
3987
3988 os_memset(&params, 0, sizeof(params));
3989 wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
3990 wpa_s->current_ssid, &params, NULL);
3991 if (!wpa_ie)
3992 return;
3993
3994 wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
3995 os_free(wpa_ie);
3996}
3997#endif /* CONFIG_MBO */
3998
3999
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004000static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
4001{
4002 struct wpa_connect_work *cwork = work->ctx;
4003 struct wpa_bss *bss = cwork->bss;
4004 struct wpa_ssid *ssid = cwork->ssid;
4005 struct wpa_supplicant *wpa_s = work->wpa_s;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004006 u8 *wpa_ie;
Hai Shalomc3565922019-10-28 11:58:20 -07004007 const u8 *edmg_ie_oper;
Hai Shalomfdcde762020-04-02 11:19:20 -07004008 int use_crypt, ret, bssid_changed;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004009 unsigned int cipher_pairwise, cipher_group, cipher_group_mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004010 struct wpa_driver_associate_params params;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004011 u8 psk[PMK_LEN];
Hai Shalomfdcde762020-04-02 11:19:20 -07004012#if defined(CONFIG_WEP) || defined(IEEE8021X_EAPOL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004013 int wep_keys_set = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07004014#endif /* CONFIG_WEP || IEEE8021X_EAPOL */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004015 int assoc_failed = 0;
4016 struct wpa_ssid *old_ssid;
Dmitry Shmidte4663042016-04-04 10:07:49 -07004017 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004018#ifdef CONFIG_HT_OVERRIDES
4019 struct ieee80211_ht_capabilities htcaps;
4020 struct ieee80211_ht_capabilities htcaps_mask;
4021#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07004022#ifdef CONFIG_VHT_OVERRIDES
4023 struct ieee80211_vht_capabilities vhtcaps;
4024 struct ieee80211_vht_capabilities vhtcaps_mask;
4025#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004026
Hai Shalomc1a21442022-02-04 13:43:00 -08004027 wpa_s->roam_in_progress = false;
4028#ifdef CONFIG_WNM
4029 wpa_s->bss_trans_mgmt_in_progress = false;
4030#endif /* CONFIG_WNM */
4031
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004032 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004033 if (work->started) {
4034 wpa_s->connect_work = NULL;
4035
4036 /* cancel possible auth. timeout */
4037 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
4038 NULL);
4039 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004040 wpas_connect_work_free(cwork);
4041 return;
4042 }
4043
4044 wpa_s->connect_work = work;
4045
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004046 if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
4047 wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004048 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
4049 wpas_connect_work_done(wpa_s);
4050 return;
4051 }
4052
Sunil Ravi640215c2023-06-28 23:08:09 +00004053 /*
4054 * Set the current AP's BSSID (for non-MLO connection) or MLD address
4055 * (for MLO connection) as the previous BSSID for reassociation requests
4056 * handled by SME-in-driver. If wpa_supplicant is in disconnected state,
4057 * prev_bssid will be zero as both wpa_s->valid_links and wpa_s->bssid
4058 * will be zero.
4059 */
4060 os_memcpy(prev_bssid,
4061 wpa_s->valid_links ? wpa_s->ap_mld_addr : wpa_s->bssid,
4062 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063 os_memset(&params, 0, sizeof(params));
4064 wpa_s->reassociate = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004065 wpa_s->eap_expected_failure = 0;
Hai Shalom60840252021-02-19 19:02:11 -08004066
4067 /* Starting new association, so clear the possibly used WPA IE from the
4068 * previous association. */
4069 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
4070 wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
4071 wpa_s->rsnxe_len = 0;
4072 wpa_s->mscs_setup_done = false;
4073
4074 wpa_ie = wpas_populate_assoc_ies(wpa_s, bss, ssid, &params, NULL);
4075 if (!wpa_ie) {
4076 wpas_connect_work_done(wpa_s);
4077 return;
4078 }
4079
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080 if (bss &&
4081 (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082#ifdef CONFIG_IEEE80211R
4083 const u8 *ie, *md = NULL;
4084#endif /* CONFIG_IEEE80211R */
4085 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
4086 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
4087 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
4088 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
4089 os_memset(wpa_s->bssid, 0, ETH_ALEN);
4090 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
4091 if (bssid_changed)
4092 wpas_notify_bssid_changed(wpa_s);
4093#ifdef CONFIG_IEEE80211R
4094 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
4095 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
4096 md = ie + 2;
4097 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
4098 if (md) {
4099 /* Prepare for the next transition */
4100 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
4101 }
4102#endif /* CONFIG_IEEE80211R */
4103#ifdef CONFIG_WPS
4104 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
4105 wpa_s->conf->ap_scan == 2 &&
4106 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
4107 /* Use ap_scan==1 style network selection to find the network
4108 */
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08004109 wpas_connect_work_done(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004110 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 wpa_s->reassociate = 1;
4112 wpa_supplicant_req_scan(wpa_s, 0, 0);
Hai Shalomc1a21442022-02-04 13:43:00 -08004113 os_free(wpa_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 return;
4115#endif /* CONFIG_WPS */
4116 } else {
4117 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
4118 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004119 if (bss)
4120 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
4121 else
4122 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004124 if (!wpa_s->pno)
4125 wpa_supplicant_cancel_sched_scan(wpa_s);
4126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127 wpa_supplicant_cancel_scan(wpa_s);
4128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
4130 use_crypt = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004131 cipher_pairwise = wpa_s->pairwise_cipher;
4132 cipher_group = wpa_s->group_cipher;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004133 cipher_group_mgmt = wpa_s->mgmt_group_cipher;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
4135 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4136 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
4137 use_crypt = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07004138#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 if (wpa_set_wep_keys(wpa_s, ssid)) {
4140 use_crypt = 1;
4141 wep_keys_set = 1;
4142 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004143#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 }
4145 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
4146 use_crypt = 0;
4147
4148#ifdef IEEE8021X_EAPOL
4149 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4150 if ((ssid->eapol_flags &
4151 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
4152 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
4153 !wep_keys_set) {
4154 use_crypt = 0;
4155 } else {
4156 /* Assume that dynamic WEP-104 keys will be used and
4157 * set cipher suites in order for drivers to expect
4158 * encryption. */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004159 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160 }
4161 }
4162#endif /* IEEE8021X_EAPOL */
4163
4164 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4165 /* Set the key before (and later after) association */
4166 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
4167 }
4168
Sunil8cd6f4d2022-06-28 18:40:46 +00004169 /* Set current_ssid before changing state to ASSOCIATING, so that the
4170 * selected SSID is available to wpas_notify_state_changed(). */
4171 old_ssid = wpa_s->current_ssid;
4172 wpa_s->current_ssid = ssid;
4173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
4175 if (bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 params.ssid = bss->ssid;
4177 params.ssid_len = bss->ssid_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004178 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set ||
4179 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
4181 MACSTR " freq=%u MHz based on scan results "
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004182 "(bssid_set=%d wps=%d)",
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183 MAC2STR(bss->bssid), bss->freq,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004184 ssid->bssid_set,
4185 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186 params.bssid = bss->bssid;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004187 params.freq.freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004188 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004189 params.bssid_hint = bss->bssid;
4190 params.freq_hint = bss->freq;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004191 params.pbss = bss_is_pbss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004193 if (ssid->bssid_hint_set)
4194 params.bssid_hint = ssid->bssid_hint;
4195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004196 params.ssid = ssid->ssid;
4197 params.ssid_len = ssid->ssid_len;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004198 params.pbss = (ssid->pbss != 2) ? ssid->pbss : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004200
4201 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
4202 wpa_s->conf->ap_scan == 2) {
4203 params.bssid = ssid->bssid;
4204 params.fixed_bssid = 1;
4205 }
4206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004207 /* Initial frequency for IBSS/mesh */
4208 if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004209 ssid->frequency > 0 && params.freq.freq == 0)
4210 ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004211
4212 if (ssid->mode == WPAS_MODE_IBSS) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004213 params.fixed_freq = ssid->fixed_freq;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004214 if (ssid->beacon_int)
4215 params.beacon_int = ssid->beacon_int;
4216 else
4217 params.beacon_int = wpa_s->conf->beacon_int;
4218 }
4219
Hai Shalomc3565922019-10-28 11:58:20 -07004220 if (bss && ssid->enable_edmg)
Hai Shalom60840252021-02-19 19:02:11 -08004221 edmg_ie_oper = wpa_bss_get_ie_ext(bss,
4222 WLAN_EID_EXT_EDMG_OPERATION);
Hai Shalomc3565922019-10-28 11:58:20 -07004223 else
4224 edmg_ie_oper = NULL;
4225
4226 if (edmg_ie_oper) {
4227 params.freq.edmg.channels =
4228 wpa_ie_get_edmg_oper_chans(edmg_ie_oper);
4229 params.freq.edmg.bw_config =
4230 wpa_ie_get_edmg_oper_chan_width(edmg_ie_oper);
4231 wpa_printf(MSG_DEBUG,
4232 "AP supports EDMG channels 0x%x, bw_config %d",
4233 params.freq.edmg.channels,
4234 params.freq.edmg.bw_config);
4235
4236 /* User may ask for specific EDMG channel for EDMG connection
4237 * (must be supported by AP)
4238 */
4239 if (ssid->edmg_channel) {
4240 struct ieee80211_edmg_config configured_edmg;
4241 enum hostapd_hw_mode hw_mode;
4242 u8 primary_channel;
4243
4244 hw_mode = ieee80211_freq_to_chan(bss->freq,
4245 &primary_channel);
4246 if (hw_mode == NUM_HOSTAPD_MODES)
4247 goto edmg_fail;
4248
4249 hostapd_encode_edmg_chan(ssid->enable_edmg,
4250 ssid->edmg_channel,
4251 primary_channel,
4252 &configured_edmg);
4253
4254 if (ieee802_edmg_is_allowed(params.freq.edmg,
4255 configured_edmg)) {
4256 params.freq.edmg = configured_edmg;
4257 wpa_printf(MSG_DEBUG,
4258 "Use EDMG channel %d for connection",
4259 ssid->edmg_channel);
4260 } else {
4261 edmg_fail:
4262 params.freq.edmg.channels = 0;
4263 params.freq.edmg.bw_config = 0;
4264 wpa_printf(MSG_WARNING,
4265 "EDMG channel %d not supported by AP, fallback to DMG",
4266 ssid->edmg_channel);
4267 }
4268 }
4269
4270 if (params.freq.edmg.channels) {
4271 wpa_printf(MSG_DEBUG,
4272 "EDMG before: channels 0x%x, bw_config %d",
4273 params.freq.edmg.channels,
4274 params.freq.edmg.bw_config);
4275 params.freq.edmg = get_supported_edmg(wpa_s,
4276 &params.freq,
4277 params.freq.edmg);
4278 wpa_printf(MSG_DEBUG,
4279 "EDMG after: channels 0x%x, bw_config %d",
4280 params.freq.edmg.channels,
4281 params.freq.edmg.bw_config);
4282 }
4283 }
4284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 params.pairwise_suite = cipher_pairwise;
4286 params.group_suite = cipher_group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004287 params.mgmt_group_suite = cipher_group_mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004288 params.key_mgmt_suite = wpa_s->key_mgmt;
Sunil Ravi89eba102022-09-13 21:04:37 -07004289 params.allowed_key_mgmts = wpa_s->allowed_key_mgmts;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004290 params.wpa_proto = wpa_s->wpa_proto;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004291 wpa_s->auth_alg = params.auth_alg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 params.mode = ssid->mode;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004293 params.bg_scan_period = ssid->bg_scan_period;
Hai Shalomfdcde762020-04-02 11:19:20 -07004294#ifdef CONFIG_WEP
4295 {
4296 int i;
4297
4298 for (i = 0; i < NUM_WEP_KEYS; i++) {
4299 if (ssid->wep_key_len[i])
4300 params.wep_key[i] = ssid->wep_key[i];
4301 params.wep_key_len[i] = ssid->wep_key_len[i];
4302 }
4303 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004305#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306
Hai Shalom74f70d42019-02-11 14:42:39 -08004307 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
Winnie Chen4138eec2022-11-10 16:32:53 +08004308#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Sunil Ravi89eba102022-09-13 21:04:37 -07004309 ((params.key_mgmt_suite & WPA_KEY_MGMT_PSK) ||
4310 (params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK))) {
Vinayak Yadawad14709082022-03-17 14:25:11 +05304311#else
Sunil Ravi89eba102022-09-13 21:04:37 -07004312 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4313 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
4314 (params.allowed_key_mgmts &
4315 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK)))) {
Winnie Chen4138eec2022-11-10 16:32:53 +08004316#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 params.passphrase = ssid->passphrase;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004318 if (wpa_supplicant_get_psk(wpa_s, bss, ssid, psk) == 0)
4319 params.psk = psk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 }
4321
Hai Shalom74f70d42019-02-11 14:42:39 -08004322 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
4323 (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4324 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4325 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004326 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
4327 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384))
Hai Shalomc3565922019-10-28 11:58:20 -07004328 params.req_handshake_offload = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08004329
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004330 if (wpa_s->conf->key_mgmt_offload) {
4331 if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4332 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004333 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004334 params.key_mgmt_suite ==
4335 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ||
4336 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA384)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004337 params.req_key_mgmt_offload =
4338 ssid->proactive_key_caching < 0 ?
4339 wpa_s->conf->okc : ssid->proactive_key_caching;
4340 else
4341 params.req_key_mgmt_offload = 1;
4342
Winnie Chen4138eec2022-11-10 16:32:53 +08004343#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Sunil Ravi89eba102022-09-13 21:04:37 -07004344 if (((params.key_mgmt_suite & WPA_KEY_MGMT_PSK) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004345 params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4346 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
Sunil Ravi89eba102022-09-13 21:04:37 -07004347#else
4348 if ((wpa_key_mgmt_wpa_psk_no_sae(params.key_mgmt_suite) ||
4349 wpa_key_mgmt_wpa_psk_no_sae(params.allowed_key_mgmts)) &&
Winnie Chen4138eec2022-11-10 16:32:53 +08004350#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
Sunil Ravi77d572f2023-01-17 23:58:31 +00004351 wpa_supplicant_get_psk(wpa_s, bss, ssid, psk) == 0)
4352 params.psk = psk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004353 }
4354
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004355 if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA) &&
4356 wpa_key_mgmt_sae(params.key_mgmt_suite)) {
4357 params.auth_alg = WPA_AUTH_ALG_SAE;
4358 if (ssid->sae_password) {
4359 params.sae_password = ssid->sae_password;
4360 params.sae_password_id = ssid->sae_password_id;
4361 } else if (ssid->passphrase) {
4362 params.passphrase = ssid->passphrase;
4363 }
4364 }
4365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 params.drop_unencrypted = use_crypt;
4367
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004368 params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004369 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4371 struct wpa_ie_data ie;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004372 if (!wpas_driver_bss_selection(wpa_s) && rsn &&
4373 wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004374 ie.capabilities &
4375 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
4376 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
4377 "MFP: require MFP");
4378 params.mgmt_frame_protection =
4379 MGMT_FRAME_PROTECTION_REQUIRED;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004380#ifdef CONFIG_OWE
4381 } else if (!rsn && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
4382 !ssid->owe_only) {
4383 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
4384#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 }
4386 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004388 params.p2p = ssid->p2p_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004390 if (wpa_s->p2pdev->set_sta_uapsd)
4391 params.uapsd = wpa_s->p2pdev->sta_uapsd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392 else
4393 params.uapsd = -1;
4394
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004395#ifdef CONFIG_HT_OVERRIDES
4396 os_memset(&htcaps, 0, sizeof(htcaps));
4397 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
4398 params.htcaps = (u8 *) &htcaps;
4399 params.htcaps_mask = (u8 *) &htcaps_mask;
4400 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
4401#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07004402#ifdef CONFIG_VHT_OVERRIDES
4403 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
4404 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
4405 params.vhtcaps = &vhtcaps;
4406 params.vhtcaps_mask = &vhtcaps_mask;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004407 wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07004408#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -07004409#ifdef CONFIG_HE_OVERRIDES
4410 wpa_supplicant_apply_he_overrides(wpa_s, ssid, &params);
4411#endif /* CONFIG_HE_OVERRIDES */
Sunil Ravi77d572f2023-01-17 23:58:31 +00004412 wpa_supplicant_apply_eht_overrides(wpa_s, ssid, &params);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004413
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004414#ifdef CONFIG_P2P
4415 /*
4416 * If multi-channel concurrency is not supported, check for any
4417 * frequency conflict. In case of any frequency conflict, remove the
4418 * least prioritized connection.
4419 */
4420 if (wpa_s->num_multichan_concurrent < 2) {
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004421 int freq, num;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004422 num = get_shared_radio_freqs(wpa_s, &freq, 1, false);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004423 if (num > 0 && freq > 0 && freq != params.freq.freq) {
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004424 wpa_printf(MSG_DEBUG,
4425 "Assoc conflicting freq found (%d != %d)",
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004426 freq, params.freq.freq);
4427 if (wpas_p2p_handle_frequency_conflicts(
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08004428 wpa_s, params.freq.freq, ssid) < 0) {
4429 wpas_connect_work_done(wpa_s);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004430 os_free(wpa_ie);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004431 return;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08004432 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004433 }
4434 }
4435#endif /* CONFIG_P2P */
4436
Dmitry Shmidte4663042016-04-04 10:07:49 -07004437 if (wpa_s->reassoc_same_ess && !is_zero_ether_addr(prev_bssid) &&
Sunil8cd6f4d2022-06-28 18:40:46 +00004438 old_ssid)
Dmitry Shmidte4663042016-04-04 10:07:49 -07004439 params.prev_bssid = prev_bssid;
4440
Hai Shalom60840252021-02-19 19:02:11 -08004441#ifdef CONFIG_SAE
4442 params.sae_pwe = wpa_s->conf->sae_pwe;
4443#endif /* CONFIG_SAE */
4444
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004445 ret = wpa_drv_associate(wpa_s, &params);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004446 forced_memzero(psk, sizeof(psk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004447 os_free(wpa_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 if (ret < 0) {
4449 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
4450 "failed");
Hai Shalomc1a21442022-02-04 13:43:00 -08004451 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_VALID_ERROR_CODES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452 /*
4453 * The driver is known to mean what is saying, so we
4454 * can stop right here; the association will not
4455 * succeed.
4456 */
4457 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Roger Wang4c09cc92020-11-05 18:57:12 +08004458 wpa_s->assoc_status_code = WLAN_STATUS_UNSPECIFIED_FAILURE;
Sunil Ravie06118e2021-01-03 08:39:46 -08004459 wpas_notify_assoc_status_code(wpa_s, wpa_s->pending_bssid, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004460 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
4462 return;
4463 }
4464 /* try to continue anyway; new association will be tried again
4465 * after timeout */
4466 assoc_failed = 1;
4467 }
4468
4469 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
4470 /* Set the key after the association just in case association
4471 * cleared the previously configured key. */
4472 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
4473 /* No need to timeout authentication since there is no key
4474 * management. */
4475 wpa_supplicant_cancel_auth_timeout(wpa_s);
4476 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
4477#ifdef CONFIG_IBSS_RSN
4478 } else if (ssid->mode == WPAS_MODE_IBSS &&
4479 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
4480 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
4481 /*
4482 * RSN IBSS authentication is per-STA and we can disable the
4483 * per-BSSID authentication.
4484 */
4485 wpa_supplicant_cancel_auth_timeout(wpa_s);
4486#endif /* CONFIG_IBSS_RSN */
4487 } else {
4488 /* Timeout for IEEE 802.11 authentication and association */
4489 int timeout = 60;
4490
4491 if (assoc_failed) {
4492 /* give IBSS a bit more time */
4493 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
4494 } else if (wpa_s->conf->ap_scan == 1) {
4495 /* give IBSS a bit more time */
4496 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
4497 }
4498 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
4499 }
4500
Hai Shalomfdcde762020-04-02 11:19:20 -07004501#ifdef CONFIG_WEP
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004502 if (wep_keys_set &&
4503 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 /* Set static WEP keys again */
4505 wpa_set_wep_keys(wpa_s, ssid);
4506 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004507#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508
Sunil8cd6f4d2022-06-28 18:40:46 +00004509 if (old_ssid && old_ssid != ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510 /*
4511 * Do not allow EAP session resumption between different
4512 * network configurations.
4513 */
4514 eapol_sm_invalidate_cached_session(wpa_s->eapol);
4515 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004516
4517 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07004518 wpa_s->current_bss = bss;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004519#ifdef CONFIG_HS20
4520 hs20_configure_frame_filters(wpa_s);
4521#endif /* CONFIG_HS20 */
4522 }
4523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
4525 wpa_supplicant_initiate_eapol(wpa_s);
4526 if (old_ssid != wpa_s->current_ssid)
4527 wpas_notify_network_changed(wpa_s);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004528 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
4529 wpas_notify_auth_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530}
4531
4532
4533static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
4534 const u8 *addr)
4535{
4536 struct wpa_ssid *old_ssid;
4537
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004538 wpa_s->ml_connect_probe_ssid = NULL;
4539 wpa_s->ml_connect_probe_bss = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004540 wpas_connect_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541 wpa_clear_keys(wpa_s, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 old_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004543 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 wpa_sm_set_config(wpa_s->wpa, NULL);
4545 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4546 if (old_ssid != wpa_s->current_ssid)
4547 wpas_notify_network_changed(wpa_s);
Hai Shalomc1a21442022-02-04 13:43:00 -08004548
4549 wpas_scs_deinit(wpa_s);
4550 wpas_dscp_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004551 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4552}
4553
4554
4555/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
4557 * @wpa_s: Pointer to wpa_supplicant data
4558 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
4559 *
4560 * This function is used to request %wpa_supplicant to deauthenticate from the
4561 * current AP.
4562 */
4563void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
Hai Shalom81f62d82019-07-22 12:10:00 -07004564 u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565{
4566 u8 *addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004567 union wpa_event_data event;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004568 int zero_addr = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004570 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
Hai Shalom81f62d82019-07-22 12:10:00 -07004571 " pending_bssid=" MACSTR " reason=%d (%s) state=%s",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004572 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
Hai Shalom81f62d82019-07-22 12:10:00 -07004573 reason_code, reason2str(reason_code),
4574 wpa_supplicant_state_txt(wpa_s->wpa_state));
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004575
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004576 if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
4577 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
4578 wpa_s->wpa_state == WPA_ASSOCIATING))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004579 addr = wpa_s->pending_bssid;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004580 else if (!is_zero_ether_addr(wpa_s->bssid))
4581 addr = wpa_s->bssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004582 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
4583 /*
4584 * When using driver-based BSS selection, we may not know the
4585 * BSSID with which we are currently trying to associate. We
4586 * need to notify the driver of this disconnection even in such
4587 * a case, so use the all zeros address here.
4588 */
4589 addr = wpa_s->bssid;
4590 zero_addr = 1;
4591 }
4592
Hai Shalom74f70d42019-02-11 14:42:39 -08004593 if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
4594 wpa_s->enabled_4addr_mode = 0;
4595
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004596#ifdef CONFIG_TDLS
4597 wpa_tdls_teardown_peers(wpa_s->wpa);
4598#endif /* CONFIG_TDLS */
4599
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004600#ifdef CONFIG_MESH
4601 if (wpa_s->ifmsh) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004602 struct mesh_conf *mconf;
4603
4604 mconf = wpa_s->ifmsh->mconf;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004605 wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
4606 wpa_s->ifname);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004607 wpas_notify_mesh_group_removed(wpa_s, mconf->meshid,
4608 mconf->meshid_len, reason_code);
Hai Shalom60840252021-02-19 19:02:11 -08004609 wpa_supplicant_leave_mesh(wpa_s, true);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004610 }
4611#endif /* CONFIG_MESH */
4612
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004613 if (addr) {
4614 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004615 os_memset(&event, 0, sizeof(event));
Hai Shalom81f62d82019-07-22 12:10:00 -07004616 event.deauth_info.reason_code = reason_code;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004617 event.deauth_info.locally_generated = 1;
4618 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004619 if (zero_addr)
4620 addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621 }
4622
4623 wpa_supplicant_clear_connection(wpa_s, addr);
4624}
4625
Hai Shalomfdcde762020-04-02 11:19:20 -07004626
4627void wpa_supplicant_reconnect(struct wpa_supplicant *wpa_s)
4628{
4629 wpa_s->own_reconnect_req = 1;
4630 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
4631
4632}
4633
4634
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004635static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
4636 struct wpa_ssid *ssid)
4637{
4638 if (!ssid || !ssid->disabled || ssid->disabled == 2)
4639 return;
4640
4641 ssid->disabled = 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004642 ssid->owe_transition_bss_select_count = 0;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004643 wpas_clear_temp_disabled(wpa_s, ssid, 1);
4644 wpas_notify_network_enabled_changed(wpa_s, ssid);
4645
4646 /*
4647 * Try to reassociate since there is no current configuration and a new
4648 * network was made available.
4649 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004650 if (!wpa_s->current_ssid && !wpa_s->disconnected)
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004651 wpa_s->reassociate = 1;
4652}
4653
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004654
Roshan Pius950bec92016-07-19 09:49:24 -07004655/**
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004656 * wpa_supplicant_add_network - Add a new network
Roshan Pius950bec92016-07-19 09:49:24 -07004657 * @wpa_s: wpa_supplicant structure for a network interface
4658 * Returns: The new network configuration or %NULL if operation failed
4659 *
4660 * This function performs the following operations:
4661 * 1. Adds a new network.
4662 * 2. Send network addition notification.
4663 * 3. Marks the network disabled.
4664 * 4. Set network default parameters.
4665 */
4666struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
4667{
4668 struct wpa_ssid *ssid;
4669
4670 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004671 if (!ssid)
Roshan Pius950bec92016-07-19 09:49:24 -07004672 return NULL;
Roshan Pius950bec92016-07-19 09:49:24 -07004673 wpas_notify_network_added(wpa_s, ssid);
4674 ssid->disabled = 1;
4675 wpa_config_set_network_defaults(ssid);
4676
4677 return ssid;
4678}
4679
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004680
Roshan Pius950bec92016-07-19 09:49:24 -07004681/**
4682 * wpa_supplicant_remove_network - Remove a configured network based on id
4683 * @wpa_s: wpa_supplicant structure for a network interface
4684 * @id: Unique network id to search for
4685 * Returns: 0 on success, or -1 if the network was not found, -2 if the network
4686 * could not be removed
4687 *
4688 * This function performs the following operations:
4689 * 1. Removes the network.
4690 * 2. Send network removal notification.
4691 * 3. Update internal state machines.
4692 * 4. Stop any running sched scans.
4693 */
4694int wpa_supplicant_remove_network(struct wpa_supplicant *wpa_s, int id)
4695{
Sunil Ravia04bd252022-05-02 22:54:18 -07004696 struct wpa_ssid *ssid, *prev = wpa_s->current_ssid;
Roshan Pius950bec92016-07-19 09:49:24 -07004697 int was_disabled;
4698
4699 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004700 if (!ssid)
Roshan Pius950bec92016-07-19 09:49:24 -07004701 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004702 wpas_notify_network_removed(wpa_s, ssid);
Roshan Pius950bec92016-07-19 09:49:24 -07004703
Sunil Ravia04bd252022-05-02 22:54:18 -07004704 if (ssid == prev || !prev) {
Roshan Pius950bec92016-07-19 09:49:24 -07004705#ifdef CONFIG_SME
4706 wpa_s->sme.prev_bssid_set = 0;
4707#endif /* CONFIG_SME */
4708 /*
4709 * Invalidate the EAP session cache if the current or
4710 * previously used network is removed.
4711 */
4712 eapol_sm_invalidate_cached_session(wpa_s->eapol);
4713 }
4714
Sunil Ravia04bd252022-05-02 22:54:18 -07004715 if (ssid == prev) {
Roshan Pius950bec92016-07-19 09:49:24 -07004716 wpa_sm_set_config(wpa_s->wpa, NULL);
4717 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4718
4719 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4720 wpa_s->own_disconnect_req = 1;
4721 wpa_supplicant_deauthenticate(wpa_s,
4722 WLAN_REASON_DEAUTH_LEAVING);
4723 }
4724
4725 was_disabled = ssid->disabled;
4726
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004727 if (wpa_config_remove_network(wpa_s->conf, id) < 0)
Roshan Pius950bec92016-07-19 09:49:24 -07004728 return -2;
Roshan Pius950bec92016-07-19 09:49:24 -07004729
4730 if (!was_disabled && wpa_s->sched_scanning) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004731 wpa_printf(MSG_DEBUG,
4732 "Stop ongoing sched_scan to remove network from filters");
Roshan Pius950bec92016-07-19 09:49:24 -07004733 wpa_supplicant_cancel_sched_scan(wpa_s);
4734 wpa_supplicant_req_scan(wpa_s, 0, 0);
4735 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004736
Roshan Pius950bec92016-07-19 09:49:24 -07004737 return 0;
4738}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741/**
Hai Shalom899fcc72020-10-19 14:38:18 -07004742 * wpa_supplicant_remove_all_networks - Remove all configured networks
4743 * @wpa_s: wpa_supplicant structure for a network interface
4744 * Returns: 0 on success (errors are currently ignored)
4745 *
4746 * This function performs the following operations:
4747 * 1. Remove all networks.
4748 * 2. Send network removal notifications.
4749 * 3. Update internal state machines.
4750 * 4. Stop any running sched scans.
4751 */
4752int wpa_supplicant_remove_all_networks(struct wpa_supplicant *wpa_s)
4753{
4754 struct wpa_ssid *ssid;
4755
Vinayak Yadawad4222acc2023-12-15 17:39:27 +05304756 if (wpa_s->drv_flags2 &
4757 (WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA |
4758 WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA))
4759 wpa_drv_flush_pmkid(wpa_s);
4760
Hai Shalom899fcc72020-10-19 14:38:18 -07004761 if (wpa_s->sched_scanning)
4762 wpa_supplicant_cancel_sched_scan(wpa_s);
4763
4764 eapol_sm_invalidate_cached_session(wpa_s->eapol);
4765 if (wpa_s->current_ssid) {
4766#ifdef CONFIG_SME
4767 wpa_s->sme.prev_bssid_set = 0;
4768#endif /* CONFIG_SME */
4769 wpa_sm_set_config(wpa_s->wpa, NULL);
4770 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
4771 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4772 wpa_s->own_disconnect_req = 1;
4773 wpa_supplicant_deauthenticate(
4774 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4775 }
4776 ssid = wpa_s->conf->ssid;
4777 while (ssid) {
4778 struct wpa_ssid *remove_ssid = ssid;
4779 int id;
4780
4781 id = ssid->id;
4782 ssid = ssid->next;
Hai Shalom899fcc72020-10-19 14:38:18 -07004783 wpas_notify_network_removed(wpa_s, remove_ssid);
4784 wpa_config_remove_network(wpa_s->conf, id);
4785 }
4786 return 0;
4787}
4788
4789
4790/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791 * wpa_supplicant_enable_network - Mark a configured network as enabled
4792 * @wpa_s: wpa_supplicant structure for a network interface
4793 * @ssid: wpa_ssid structure for a configured network or %NULL
4794 *
4795 * Enables the specified network or all networks if no network specified.
4796 */
4797void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
4798 struct wpa_ssid *ssid)
4799{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004801 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
4802 wpa_supplicant_enable_one_network(wpa_s, ssid);
4803 } else
4804 wpa_supplicant_enable_one_network(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004806 if (wpa_s->reassociate && !wpa_s->disconnected &&
4807 (!wpa_s->current_ssid ||
4808 wpa_s->wpa_state == WPA_DISCONNECTED ||
4809 wpa_s->wpa_state == WPA_SCANNING)) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004810 if (wpa_s->sched_scanning) {
4811 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
4812 "new network to scan filters");
4813 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 }
4815
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004816 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
4817 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004818 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004819 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 }
4821}
4822
4823
4824/**
4825 * wpa_supplicant_disable_network - Mark a configured network as disabled
4826 * @wpa_s: wpa_supplicant structure for a network interface
4827 * @ssid: wpa_ssid structure for a configured network or %NULL
4828 *
4829 * Disables the specified network or all networks if no network specified.
4830 */
4831void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
4832 struct wpa_ssid *ssid)
4833{
4834 struct wpa_ssid *other_ssid;
4835 int was_disabled;
4836
4837 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004838 if (wpa_s->sched_scanning)
4839 wpa_supplicant_cancel_sched_scan(wpa_s);
4840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841 for (other_ssid = wpa_s->conf->ssid; other_ssid;
4842 other_ssid = other_ssid->next) {
4843 was_disabled = other_ssid->disabled;
4844 if (was_disabled == 2)
4845 continue; /* do not change persistent P2P group
4846 * data */
4847
4848 other_ssid->disabled = 1;
4849
4850 if (was_disabled != other_ssid->disabled)
4851 wpas_notify_network_enabled_changed(
4852 wpa_s, other_ssid);
4853 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004854 if (wpa_s->current_ssid) {
4855 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4856 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004857 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 } else if (ssid->disabled != 2) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004861 if (ssid == wpa_s->current_ssid) {
4862 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4863 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004864 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004866 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867
4868 was_disabled = ssid->disabled;
4869
4870 ssid->disabled = 1;
4871
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004872 if (was_disabled != ssid->disabled) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004874 if (wpa_s->sched_scanning) {
4875 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
4876 "to remove network from filters");
4877 wpa_supplicant_cancel_sched_scan(wpa_s);
4878 wpa_supplicant_req_scan(wpa_s, 0, 0);
4879 }
4880 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 }
4882}
4883
4884
4885/**
4886 * wpa_supplicant_select_network - Attempt association with a network
4887 * @wpa_s: wpa_supplicant structure for a network interface
4888 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
4889 */
4890void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
4891 struct wpa_ssid *ssid)
4892{
4893
4894 struct wpa_ssid *other_ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004895 int disconnected = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004897 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004898 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4899 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004900 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004902 disconnected = 1;
4903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004905 if (ssid)
4906 wpas_clear_temp_disabled(wpa_s, ssid, 1);
4907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 /*
4909 * Mark all other networks disabled or mark all networks enabled if no
4910 * network specified.
4911 */
4912 for (other_ssid = wpa_s->conf->ssid; other_ssid;
4913 other_ssid = other_ssid->next) {
4914 int was_disabled = other_ssid->disabled;
4915 if (was_disabled == 2)
4916 continue; /* do not change persistent P2P group data */
4917
4918 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004919 if (was_disabled && !other_ssid->disabled)
4920 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921
4922 if (was_disabled != other_ssid->disabled)
4923 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
4924 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004925
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004926 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
4927 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004928 /* We are already associated with the selected network */
4929 wpa_printf(MSG_DEBUG, "Already associated with the "
4930 "selected network - do nothing");
4931 return;
4932 }
4933
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004934 if (ssid) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004935 wpa_s->current_ssid = ssid;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004936 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004937 wpa_s->connect_without_scan =
Sunil Ravi640215c2023-06-28 23:08:09 +00004938 (ssid->mode == WPAS_MODE_MESH ||
4939 ssid->mode == WPAS_MODE_AP) ? ssid : NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07004940
4941 /*
4942 * Don't optimize next scan freqs since a new ESS has been
4943 * selected.
4944 */
4945 os_free(wpa_s->next_scan_freqs);
4946 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004947 } else {
4948 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004949 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 wpa_s->disconnected = 0;
4952 wpa_s->reassociate = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004953 wpa_s_clear_sae_rejected(wpa_s);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004954 wpa_s->last_owe_group = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004955 if (ssid) {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004956 ssid->owe_transition_bss_select_count = 0;
Sunil Ravi036cec52023-03-29 11:35:17 -07004957 wpa_s_setup_sae_pt(wpa_s->conf, ssid, false);
Hai Shalomc3565922019-10-28 11:58:20 -07004958 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004960 if (wpa_s->connect_without_scan ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004961 wpa_supplicant_fast_associate(wpa_s) != 1) {
4962 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004963 wpas_scan_reset_sched_scan(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004964 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966
4967 if (ssid)
4968 wpas_notify_network_selected(wpa_s, ssid);
4969}
4970
4971
4972/**
Hai Shalomc1a21442022-02-04 13:43:00 -08004973 * wpas_remove_cred - Remove the specified credential and all the network
4974 * entries created based on the removed credential
4975 * @wpa_s: wpa_supplicant structure for a network interface
4976 * @cred: The credential to remove
4977 * Returns: 0 on success, -1 on failure
4978 */
4979int wpas_remove_cred(struct wpa_supplicant *wpa_s, struct wpa_cred *cred)
4980{
4981 struct wpa_ssid *ssid, *next;
4982 int id;
4983
4984 if (!cred) {
4985 wpa_printf(MSG_DEBUG, "Could not find cred");
4986 return -1;
4987 }
4988
4989 id = cred->id;
4990 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
4991 wpa_printf(MSG_DEBUG, "Could not find cred %d", id);
4992 return -1;
4993 }
4994
4995 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
4996
4997 /* Remove any network entry created based on the removed credential */
4998 ssid = wpa_s->conf->ssid;
4999 while (ssid) {
5000 next = ssid->next;
5001
5002 if (ssid->parent_cred == cred) {
5003 wpa_printf(MSG_DEBUG,
5004 "Remove network id %d since it used the removed credential",
5005 ssid->id);
5006 if (wpa_supplicant_remove_network(wpa_s, ssid->id) ==
5007 -1) {
5008 wpa_printf(MSG_DEBUG,
5009 "Could not find network id=%d",
5010 ssid->id);
5011 }
5012 }
5013
5014 ssid = next;
5015 }
5016
5017 return 0;
5018}
5019
5020
5021/**
5022 * wpas_remove_cred - Remove all the Interworking credentials
5023 * @wpa_s: wpa_supplicant structure for a network interface
5024 * Returns: 0 on success, -1 on failure
5025 */
5026int wpas_remove_all_creds(struct wpa_supplicant *wpa_s)
5027{
5028 int res, ret = 0;
5029 struct wpa_cred *cred, *prev;
5030
5031 cred = wpa_s->conf->cred;
5032 while (cred) {
5033 prev = cred;
5034 cred = cred->next;
5035 res = wpas_remove_cred(wpa_s, prev);
5036 if (res < 0) {
5037 wpa_printf(MSG_DEBUG,
5038 "Removal of all credentials failed - failed to remove credential id=%d",
5039 prev->id);
5040 ret = -1;
5041 }
5042 }
5043
5044 return ret;
5045}
5046
5047
5048/**
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005049 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
5050 * @wpa_s: wpa_supplicant structure for a network interface
5051 * @pkcs11_engine_path: PKCS #11 engine path or NULL
5052 * @pkcs11_module_path: PKCS #11 module path or NULL
5053 * Returns: 0 on success; -1 on failure
5054 *
5055 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
5056 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
5057 * module path fails the paths will be reset to the default value (NULL).
5058 */
5059int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
5060 const char *pkcs11_engine_path,
5061 const char *pkcs11_module_path)
5062{
5063 char *pkcs11_engine_path_copy = NULL;
5064 char *pkcs11_module_path_copy = NULL;
5065
5066 if (pkcs11_engine_path != NULL) {
5067 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
5068 if (pkcs11_engine_path_copy == NULL)
5069 return -1;
5070 }
5071 if (pkcs11_module_path != NULL) {
5072 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
Dmitry Shmidt97672262014-02-03 13:02:54 -08005073 if (pkcs11_module_path_copy == NULL) {
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005074 os_free(pkcs11_engine_path_copy);
5075 return -1;
5076 }
5077 }
5078
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005079#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005080 os_free(wpa_s->conf->pkcs11_engine_path);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005081 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005082#endif /* CONFIG_PKCS11_ENGINE_PATH */
5083#ifndef CONFIG_PKCS11_MODULE_PATH
5084 os_free(wpa_s->conf->pkcs11_module_path);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005085 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005086#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005087
5088 wpa_sm_set_eapol(wpa_s->wpa, NULL);
5089 eapol_sm_deinit(wpa_s->eapol);
5090 wpa_s->eapol = NULL;
5091 if (wpa_supplicant_init_eapol(wpa_s)) {
5092 /* Error -> Reset paths to the default value (NULL) once. */
5093 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
5094 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
5095 NULL);
5096
5097 return -1;
5098 }
5099 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
5100
5101 return 0;
5102}
5103
5104
5105/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
5107 * @wpa_s: wpa_supplicant structure for a network interface
5108 * @ap_scan: AP scan mode
5109 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
5110 *
5111 */
5112int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
5113{
5114
5115 int old_ap_scan;
5116
5117 if (ap_scan < 0 || ap_scan > 2)
5118 return -1;
5119
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005120 if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
5121 wpa_printf(MSG_INFO,
5122 "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
5123 }
5124
Dmitry Shmidt114c3862011-08-16 11:52:06 -07005125#ifdef ANDROID
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005126 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
5127 wpa_s->wpa_state >= WPA_ASSOCIATING &&
5128 wpa_s->wpa_state < WPA_COMPLETED) {
5129 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
5130 "associating", wpa_s->conf->ap_scan, ap_scan);
Dmitry Shmidt114c3862011-08-16 11:52:06 -07005131 return 0;
5132 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005133#endif /* ANDROID */
Dmitry Shmidt114c3862011-08-16 11:52:06 -07005134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 old_ap_scan = wpa_s->conf->ap_scan;
5136 wpa_s->conf->ap_scan = ap_scan;
5137
5138 if (old_ap_scan != wpa_s->conf->ap_scan)
5139 wpas_notify_ap_scan_changed(wpa_s);
5140
5141 return 0;
5142}
5143
5144
5145/**
5146 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
5147 * @wpa_s: wpa_supplicant structure for a network interface
5148 * @expire_age: Expiration age in seconds
5149 * Returns: 0 if succeed or -1 if expire_age has an invalid value
5150 *
5151 */
5152int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
5153 unsigned int bss_expire_age)
5154{
5155 if (bss_expire_age < 10) {
5156 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
5157 bss_expire_age);
5158 return -1;
5159 }
5160 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
5161 bss_expire_age);
5162 wpa_s->conf->bss_expiration_age = bss_expire_age;
5163
5164 return 0;
5165}
5166
5167
5168/**
5169 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
5170 * @wpa_s: wpa_supplicant structure for a network interface
5171 * @expire_count: number of scans after which an unseen BSS is reclaimed
5172 * Returns: 0 if succeed or -1 if expire_count has an invalid value
5173 *
5174 */
5175int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
5176 unsigned int bss_expire_count)
5177{
5178 if (bss_expire_count < 1) {
5179 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
5180 bss_expire_count);
5181 return -1;
5182 }
5183 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
5184 bss_expire_count);
5185 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
5186
5187 return 0;
5188}
5189
5190
5191/**
Dmitry Shmidt04949592012-07-19 12:16:46 -07005192 * wpa_supplicant_set_scan_interval - Set scan interval
5193 * @wpa_s: wpa_supplicant structure for a network interface
5194 * @scan_interval: scan interval in seconds
5195 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
5196 *
5197 */
5198int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
5199 int scan_interval)
5200{
5201 if (scan_interval < 0) {
5202 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
5203 scan_interval);
5204 return -1;
5205 }
5206 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
5207 scan_interval);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005208 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005209
5210 return 0;
5211}
5212
5213
5214/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005215 * wpa_supplicant_set_debug_params - Set global debug params
5216 * @global: wpa_global structure
5217 * @debug_level: debug level
5218 * @debug_timestamp: determines if show timestamp in debug data
5219 * @debug_show_keys: determines if show keys in debug data
5220 * Returns: 0 if succeed or -1 if debug_level has wrong value
5221 */
5222int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
5223 int debug_timestamp, int debug_show_keys)
5224{
5225
5226 int old_level, old_timestamp, old_show_keys;
5227
5228 /* check for allowed debuglevels */
5229 if (debug_level != MSG_EXCESSIVE &&
5230 debug_level != MSG_MSGDUMP &&
5231 debug_level != MSG_DEBUG &&
5232 debug_level != MSG_INFO &&
5233 debug_level != MSG_WARNING &&
5234 debug_level != MSG_ERROR)
5235 return -1;
5236
5237 old_level = wpa_debug_level;
5238 old_timestamp = wpa_debug_timestamp;
5239 old_show_keys = wpa_debug_show_keys;
5240
5241 wpa_debug_level = debug_level;
5242 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
5243 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
5244
5245 if (wpa_debug_level != old_level)
5246 wpas_notify_debug_level_changed(global);
5247 if (wpa_debug_timestamp != old_timestamp)
5248 wpas_notify_debug_timestamp_changed(global);
5249 if (wpa_debug_show_keys != old_show_keys)
5250 wpas_notify_debug_show_keys_changed(global);
5251
5252 return 0;
5253}
5254
5255
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005256#ifdef CONFIG_OWE
5257static int owe_trans_ssid_match(struct wpa_supplicant *wpa_s, const u8 *bssid,
5258 const u8 *entry_ssid, size_t entry_ssid_len)
5259{
5260 const u8 *owe, *pos, *end;
5261 u8 ssid_len;
5262 struct wpa_bss *bss;
5263
5264 /* Check network profile SSID aganst the SSID in the
5265 * OWE Transition Mode element. */
5266
5267 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
5268 if (!bss)
5269 return 0;
5270
5271 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5272 if (!owe)
5273 return 0;
5274
5275 pos = owe + 6;
5276 end = owe + 2 + owe[1];
5277
5278 if (end - pos < ETH_ALEN + 1)
5279 return 0;
5280 pos += ETH_ALEN;
5281 ssid_len = *pos++;
5282 if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
5283 return 0;
5284
5285 return entry_ssid_len == ssid_len &&
5286 os_memcmp(pos, entry_ssid, ssid_len) == 0;
5287}
5288#endif /* CONFIG_OWE */
5289
5290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005291/**
5292 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
5293 * @wpa_s: Pointer to wpa_supplicant data
5294 * Returns: A pointer to the current network structure or %NULL on failure
5295 */
5296struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
5297{
5298 struct wpa_ssid *entry;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07005299 u8 ssid[SSID_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300 int res;
5301 size_t ssid_len;
5302 u8 bssid[ETH_ALEN];
5303 int wired;
5304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005305 res = wpa_drv_get_ssid(wpa_s, ssid);
5306 if (res < 0) {
5307 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
5308 "driver");
5309 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005311 ssid_len = res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005313 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
5315 "driver");
5316 return NULL;
5317 }
5318
5319 wired = wpa_s->conf->ap_scan == 0 &&
5320 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
5321
5322 entry = wpa_s->conf->ssid;
5323 while (entry) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005324 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325 ((ssid_len == entry->ssid_len &&
Hai Shalom021b0b52019-04-10 11:17:58 -07005326 (!entry->ssid ||
5327 os_memcmp(ssid, entry->ssid, ssid_len) == 0)) ||
5328 wired) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329 (!entry->bssid_set ||
5330 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
5331 return entry;
5332#ifdef CONFIG_WPS
Dmitry Shmidt04949592012-07-19 12:16:46 -07005333 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
5335 (entry->ssid == NULL || entry->ssid_len == 0) &&
5336 (!entry->bssid_set ||
5337 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
5338 return entry;
5339#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005340
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005341#ifdef CONFIG_OWE
5342 if (!wpas_network_disabled(wpa_s, entry) &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005343 (entry->ssid &&
5344 owe_trans_ssid_match(wpa_s, bssid, entry->ssid,
5345 entry->ssid_len)) &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005346 (!entry->bssid_set ||
5347 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
5348 return entry;
5349#endif /* CONFIG_OWE */
5350
Dmitry Shmidt04949592012-07-19 12:16:46 -07005351 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005352 entry->ssid_len == 0 &&
5353 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
5354 return entry;
5355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356 entry = entry->next;
5357 }
5358
5359 return NULL;
5360}
5361
5362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363static int select_driver(struct wpa_supplicant *wpa_s, int i)
5364{
5365 struct wpa_global *global = wpa_s->global;
5366
5367 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07005368 global->drv_priv[i] = wpa_drivers[i]->global_init(global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005369 if (global->drv_priv[i] == NULL) {
5370 wpa_printf(MSG_ERROR, "Failed to initialize driver "
5371 "'%s'", wpa_drivers[i]->name);
5372 return -1;
5373 }
5374 }
5375
5376 wpa_s->driver = wpa_drivers[i];
5377 wpa_s->global_drv_priv = global->drv_priv[i];
5378
5379 return 0;
5380}
5381
5382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
5384 const char *name)
5385{
5386 int i;
5387 size_t len;
5388 const char *pos, *driver = name;
5389
5390 if (wpa_s == NULL)
5391 return -1;
5392
5393 if (wpa_drivers[0] == NULL) {
5394 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
5395 "wpa_supplicant");
5396 return -1;
5397 }
5398
5399 if (name == NULL) {
Hai Shalomc1a21442022-02-04 13:43:00 -08005400 /* Default to first successful driver in the list */
5401 for (i = 0; wpa_drivers[i]; i++) {
5402 if (select_driver(wpa_s, i) == 0)
5403 return 0;
5404 }
5405 /* Drivers have each reported failure, so no wpa_msg() here. */
5406 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005407 }
5408
5409 do {
5410 pos = os_strchr(driver, ',');
5411 if (pos)
5412 len = pos - driver;
5413 else
5414 len = os_strlen(driver);
5415
5416 for (i = 0; wpa_drivers[i]; i++) {
5417 if (os_strlen(wpa_drivers[i]->name) == len &&
5418 os_strncmp(driver, wpa_drivers[i]->name, len) ==
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005419 0) {
5420 /* First driver that succeeds wins */
5421 if (select_driver(wpa_s, i) == 0)
5422 return 0;
5423 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424 }
5425
5426 driver = pos + 1;
5427 } while (pos);
5428
5429 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
5430 return -1;
5431}
5432
5433
5434/**
5435 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
5436 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
5437 * with struct wpa_driver_ops::init()
5438 * @src_addr: Source address of the EAPOL frame
5439 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
5440 * @len: Length of the EAPOL data
Sunil8cd6f4d2022-06-28 18:40:46 +00005441 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 *
5443 * This function is called for each received EAPOL frame. Most driver
5444 * interfaces rely on more generic OS mechanism for receiving frames through
5445 * l2_packet, but if such a mechanism is not available, the driver wrapper may
5446 * take care of received EAPOL frames and deliver them to the core supplicant
5447 * code by calling this function.
5448 */
5449void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00005450 const u8 *buf, size_t len,
5451 enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452{
5453 struct wpa_supplicant *wpa_s = ctx;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005454 const u8 *connected_addr = wpa_s->valid_links ?
5455 wpa_s->ap_mld_addr : wpa_s->bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456
Sunil8cd6f4d2022-06-28 18:40:46 +00005457 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " (encrypted=%d)",
5458 MAC2STR(src_addr), encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
5460
Hai Shalomc1a21442022-02-04 13:43:00 -08005461 if (wpa_s->own_disconnect_req) {
5462 wpa_printf(MSG_DEBUG,
5463 "Drop received EAPOL frame as we are disconnecting");
5464 return;
5465 }
5466
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07005467#ifdef CONFIG_TESTING_OPTIONS
Hai Shalomc1a21442022-02-04 13:43:00 -08005468 wpa_msg_ctrl(wpa_s, MSG_INFO, "EAPOL-RX " MACSTR " %zu",
5469 MAC2STR(src_addr), len);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07005470 if (wpa_s->ignore_auth_resp) {
5471 wpa_printf(MSG_INFO, "RX EAPOL - ignore_auth_resp active!");
5472 return;
5473 }
5474#endif /* CONFIG_TESTING_OPTIONS */
5475
Jouni Malinena05074c2012-12-21 21:35:35 +02005476 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5477 (wpa_s->last_eapol_matches_bssid &&
5478#ifdef CONFIG_AP
5479 !wpa_s->ap_iface &&
5480#endif /* CONFIG_AP */
Sunil Ravi77d572f2023-01-17 23:58:31 +00005481 os_memcmp(src_addr, connected_addr, ETH_ALEN) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 /*
5483 * There is possible race condition between receiving the
5484 * association event and the EAPOL frame since they are coming
5485 * through different paths from the driver. In order to avoid
5486 * issues in trying to process the EAPOL frame before receiving
5487 * association information, lets queue it for processing until
Jouni Malinena05074c2012-12-21 21:35:35 +02005488 * the association event is received. This may also be needed in
5489 * driver-based roaming case, so also use src_addr != BSSID as a
5490 * trigger if we have previously confirmed that the
5491 * Authenticator uses BSSID as the src_addr (which is not the
5492 * case with wired IEEE 802.1X).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00005494 wpa_dbg(wpa_s, MSG_DEBUG,
5495 "Not associated - Delay processing of received EAPOL frame (state=%s connected_addr="
5496 MACSTR ")",
Jouni Malinena05074c2012-12-21 21:35:35 +02005497 wpa_supplicant_state_txt(wpa_s->wpa_state),
Sunil Ravi77d572f2023-01-17 23:58:31 +00005498 MAC2STR(connected_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 wpabuf_free(wpa_s->pending_eapol_rx);
5500 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
5501 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005502 os_get_reltime(&wpa_s->pending_eapol_rx_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
5504 ETH_ALEN);
Sunil8cd6f4d2022-06-28 18:40:46 +00005505 wpa_s->pending_eapol_encrypted = encrypted;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 }
5507 return;
5508 }
5509
Jouni Malinena05074c2012-12-21 21:35:35 +02005510 wpa_s->last_eapol_matches_bssid =
Sunil Ravi77d572f2023-01-17 23:58:31 +00005511 os_memcmp(src_addr, connected_addr, ETH_ALEN) == 0;
Jouni Malinena05074c2012-12-21 21:35:35 +02005512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513#ifdef CONFIG_AP
5514 if (wpa_s->ap_iface) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005515 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len,
5516 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 return;
5518 }
5519#endif /* CONFIG_AP */
5520
5521 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
5522 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
5523 "no key management is configured");
5524 return;
5525 }
5526
5527 if (wpa_s->eapol_received == 0 &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005528 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
5530 wpa_s->wpa_state != WPA_COMPLETED) &&
5531 (wpa_s->current_ssid == NULL ||
Hai Shalom81f62d82019-07-22 12:10:00 -07005532 wpa_s->current_ssid->mode != WPAS_MODE_IBSS)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533 /* Timeout for completing IEEE 802.1X and WPA authentication */
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005534 int timeout = 10;
5535
5536 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
5537 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
5538 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
5539 /* Use longer timeout for IEEE 802.1X/EAP */
5540 timeout = 70;
5541 }
5542
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07005543#ifdef CONFIG_WPS
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005544 if (wpa_s->current_ssid && wpa_s->current_bss &&
5545 (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
5546 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
5547 /*
5548 * Use shorter timeout if going through WPS AP iteration
5549 * for PIN config method with an AP that does not
5550 * advertise Selected Registrar.
5551 */
5552 struct wpabuf *wps_ie;
5553
5554 wps_ie = wpa_bss_get_vendor_ie_multi(
5555 wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
5556 if (wps_ie &&
5557 !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
5558 timeout = 10;
5559 wpabuf_free(wps_ie);
5560 }
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07005561#endif /* CONFIG_WPS */
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005562
5563 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564 }
5565 wpa_s->eapol_received++;
5566
5567 if (wpa_s->countermeasures) {
5568 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
5569 "EAPOL packet");
5570 return;
5571 }
5572
5573#ifdef CONFIG_IBSS_RSN
5574 if (wpa_s->current_ssid &&
5575 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005576 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len,
5577 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 return;
5579 }
5580#endif /* CONFIG_IBSS_RSN */
5581
5582 /* Source address of the incoming EAPOL frame could be compared to the
5583 * current BSSID. However, it is possible that a centralized
5584 * Authenticator could be using another MAC address than the BSSID of
5585 * an AP, so just allow any address to be used for now. The replies are
5586 * still sent to the current BSSID (if available), though. */
5587
5588 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
5589 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005590 wpa_s->key_mgmt != WPA_KEY_MGMT_OWE &&
5591 wpa_s->key_mgmt != WPA_KEY_MGMT_DPP &&
Sunil8cd6f4d2022-06-28 18:40:46 +00005592 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len,
5593 encrypted) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594 return;
5595 wpa_drv_poll(wpa_s);
Hai Shalom74f70d42019-02-11 14:42:39 -08005596 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK))
Sunil8cd6f4d2022-06-28 18:40:46 +00005597 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len, encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
5599 /*
Hai Shalome21d4e82020-04-29 16:34:06 -07005600 * Set portValid = true here since we are going to skip 4-way
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601 * handshake processing which would normally set portValid. We
5602 * need this to allow the EAPOL state machines to be completed
5603 * without going through EAPOL-Key handshake.
5604 */
Hai Shalome21d4e82020-04-29 16:34:06 -07005605 eapol_sm_notify_portValid(wpa_s->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606 }
5607}
5608
5609
Sunil8cd6f4d2022-06-28 18:40:46 +00005610static void wpa_supplicant_rx_eapol_cb(void *ctx, const u8 *src_addr,
5611 const u8 *buf, size_t len)
5612{
5613 wpa_supplicant_rx_eapol(ctx, src_addr, buf, len,
5614 FRAME_ENCRYPTION_UNKNOWN);
5615}
5616
5617
Hai Shalomb755a2a2020-04-23 21:49:02 -07005618static int wpas_eapol_needs_l2_packet(struct wpa_supplicant *wpa_s)
5619{
5620 return !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT) ||
5621 !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_CONTROL_PORT_RX);
5622}
5623
5624
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005625int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005627 u8 prev_mac_addr[ETH_ALEN];
5628
5629 os_memcpy(prev_mac_addr, wpa_s->own_addr, ETH_ALEN);
5630
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005631 if ((!wpa_s->p2p_mgmt ||
5632 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
5633 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005634 l2_packet_deinit(wpa_s->l2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
5636 wpa_drv_get_mac_addr(wpa_s),
5637 ETH_P_EAPOL,
Hai Shalomb755a2a2020-04-23 21:49:02 -07005638 wpas_eapol_needs_l2_packet(wpa_s) ?
Sunil8cd6f4d2022-06-28 18:40:46 +00005639 wpa_supplicant_rx_eapol_cb : NULL,
Hai Shalomb755a2a2020-04-23 21:49:02 -07005640 wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 if (wpa_s->l2 == NULL)
5642 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005643
5644 if (l2_packet_set_packet_filter(wpa_s->l2,
5645 L2_PACKET_FILTER_PKTTYPE))
5646 wpa_dbg(wpa_s, MSG_DEBUG,
5647 "Failed to attach pkt_type filter");
Hai Shalomb755a2a2020-04-23 21:49:02 -07005648
5649 if (l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
5650 wpa_msg(wpa_s, MSG_ERROR,
5651 "Failed to get own L2 address");
5652 return -1;
5653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 } else {
5655 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
5656 if (addr)
5657 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
5658 }
5659
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005660 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
Mikael Kanstrupcc779b82019-08-16 08:50:54 +02005661 wpas_wps_update_mac_addr(wpa_s);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005662
Hai Shalomc3565922019-10-28 11:58:20 -07005663#ifdef CONFIG_FST
5664 if (wpa_s->fst)
5665 fst_update_mac_addr(wpa_s->fst, wpa_s->own_addr);
5666#endif /* CONFIG_FST */
5667
Sunil Ravi77d572f2023-01-17 23:58:31 +00005668 if (os_memcmp(prev_mac_addr, wpa_s->own_addr, ETH_ALEN) != 0)
5669 wpas_notify_mac_address_changed(wpa_s);
5670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005671 return 0;
5672}
5673
5674
Dmitry Shmidt04949592012-07-19 12:16:46 -07005675static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
5676 const u8 *buf, size_t len)
5677{
5678 struct wpa_supplicant *wpa_s = ctx;
5679 const struct l2_ethhdr *eth;
5680
5681 if (len < sizeof(*eth))
5682 return;
5683 eth = (const struct l2_ethhdr *) buf;
5684
5685 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
5686 !(eth->h_dest[0] & 0x01)) {
5687 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
5688 " (bridge - not for this interface - ignore)",
5689 MAC2STR(src_addr), MAC2STR(eth->h_dest));
5690 return;
5691 }
5692
5693 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
5694 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
5695 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
Sunil8cd6f4d2022-06-28 18:40:46 +00005696 len - sizeof(*eth), FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005697}
5698
5699
Hai Shalom899fcc72020-10-19 14:38:18 -07005700int wpa_supplicant_update_bridge_ifname(struct wpa_supplicant *wpa_s,
5701 const char *bridge_ifname)
5702{
5703 if (wpa_s->wpa_state > WPA_SCANNING)
5704 return -EBUSY;
5705
5706 if (bridge_ifname &&
5707 os_strlen(bridge_ifname) >= sizeof(wpa_s->bridge_ifname))
5708 return -EINVAL;
5709
5710 if (!bridge_ifname)
5711 bridge_ifname = "";
5712
5713 if (os_strcmp(wpa_s->bridge_ifname, bridge_ifname) == 0)
5714 return 0;
5715
5716 if (wpa_s->l2_br) {
5717 l2_packet_deinit(wpa_s->l2_br);
5718 wpa_s->l2_br = NULL;
5719 }
5720
5721 os_strlcpy(wpa_s->bridge_ifname, bridge_ifname,
5722 sizeof(wpa_s->bridge_ifname));
5723
5724 if (wpa_s->bridge_ifname[0]) {
5725 wpa_dbg(wpa_s, MSG_DEBUG,
5726 "Receiving packets from bridge interface '%s'",
5727 wpa_s->bridge_ifname);
5728 wpa_s->l2_br = l2_packet_init_bridge(
5729 wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
5730 ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
5731 if (!wpa_s->l2_br) {
5732 wpa_msg(wpa_s, MSG_ERROR,
5733 "Failed to open l2_packet connection for the bridge interface '%s'",
5734 wpa_s->bridge_ifname);
5735 goto fail;
5736 }
5737 }
5738
5739#ifdef CONFIG_TDLS
5740 if (!wpa_s->p2p_mgmt && wpa_tdls_init(wpa_s->wpa))
5741 goto fail;
5742#endif /* CONFIG_TDLS */
5743
5744 return 0;
5745fail:
5746 wpa_s->bridge_ifname[0] = 0;
5747 if (wpa_s->l2_br) {
5748 l2_packet_deinit(wpa_s->l2_br);
5749 wpa_s->l2_br = NULL;
5750 }
5751#ifdef CONFIG_TDLS
5752 if (!wpa_s->p2p_mgmt)
5753 wpa_tdls_init(wpa_s->wpa);
5754#endif /* CONFIG_TDLS */
5755 return -EIO;
5756}
5757
5758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005759/**
5760 * wpa_supplicant_driver_init - Initialize driver interface parameters
5761 * @wpa_s: Pointer to wpa_supplicant data
5762 * Returns: 0 on success, -1 on failure
5763 *
5764 * This function is called to initialize driver interface parameters.
5765 * wpa_drv_init() must have been called before this function to initialize the
5766 * driver interface.
5767 */
5768int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
5769{
5770 static int interface_count = 0;
5771
5772 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
5773 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005775 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
5776 MAC2STR(wpa_s->own_addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005777 os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005778 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
5779
Hai Shalomb755a2a2020-04-23 21:49:02 -07005780 if (wpa_s->bridge_ifname[0] && wpas_eapol_needs_l2_packet(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
5782 "interface '%s'", wpa_s->bridge_ifname);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005783 wpa_s->l2_br = l2_packet_init_bridge(
5784 wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
5785 ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005786 if (wpa_s->l2_br == NULL) {
5787 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
5788 "connection for the bridge interface '%s'",
5789 wpa_s->bridge_ifname);
5790 return -1;
5791 }
5792 }
5793
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005794 if (wpa_s->conf->ap_scan == 2 &&
5795 os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
5796 wpa_printf(MSG_INFO,
5797 "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
5798 }
5799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 wpa_clear_keys(wpa_s, NULL);
5801
5802 /* Make sure that TKIP countermeasures are not left enabled (could
5803 * happen if wpa_supplicant is killed during countermeasures. */
5804 wpa_drv_set_countermeasures(wpa_s, 0);
5805
5806 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
5807 wpa_drv_flush_pmkid(wpa_s);
5808
5809 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005810 wpa_s->prev_scan_wildcard = 0;
5811
Dmitry Shmidt04949592012-07-19 12:16:46 -07005812 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005813 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5814 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
5815 interface_count = 0;
5816 }
Keith Mok4389c282022-11-23 21:36:48 +00005817#ifndef CONFIG_CTRL_IFACE_AIDL
Dmitry Shmidta38abf92014-03-06 13:38:44 -08005818 if (!wpa_s->p2p_mgmt &&
Dmitry Shmidt98660862014-03-11 17:26:21 -07005819 wpa_supplicant_delayed_sched_scan(wpa_s,
5820 interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005821 100000))
Dmitry Shmidt98660862014-03-11 17:26:21 -07005822 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005823 100000);
Keith Mok4389c282022-11-23 21:36:48 +00005824#endif /* CONFIG_CTRL_IFACE_AIDL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 interface_count++;
5826 } else
5827 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
5828
5829 return 0;
5830}
5831
5832
5833static int wpa_supplicant_daemon(const char *pid_file)
5834{
5835 wpa_printf(MSG_DEBUG, "Daemonize..");
5836 return os_daemonize(pid_file);
5837}
5838
5839
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005840static struct wpa_supplicant *
5841wpa_supplicant_alloc(struct wpa_supplicant *parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842{
5843 struct wpa_supplicant *wpa_s;
5844
5845 wpa_s = os_zalloc(sizeof(*wpa_s));
5846 if (wpa_s == NULL)
5847 return NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005848 wpa_s->scan_req = INITIAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005849 wpa_s->scan_interval = 5;
5850 wpa_s->new_connection = 1;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005851 wpa_s->parent = parent ? parent : wpa_s;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005852 wpa_s->p2pdev = wpa_s->parent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005853 wpa_s->sched_scanning = 0;
Hai Shalom60840252021-02-19 19:02:11 -08005854 wpa_s->setband_mask = WPA_SETBAND_AUTO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005856 dl_list_init(&wpa_s->bss_tmp_disallowed);
Paul Stewart092955c2017-02-06 09:13:09 -08005857 dl_list_init(&wpa_s->fils_hlp_req);
Hai Shalomfdcde762020-04-02 11:19:20 -07005858#ifdef CONFIG_TESTING_OPTIONS
5859 dl_list_init(&wpa_s->drv_signal_override);
5860#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalomc1a21442022-02-04 13:43:00 -08005861 dl_list_init(&wpa_s->active_scs_ids);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005862 wpa_s->ml_probe_mld_id = -1;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 return wpa_s;
5865}
5866
5867
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005868#ifdef CONFIG_HT_OVERRIDES
5869
5870static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
5871 struct ieee80211_ht_capabilities *htcaps,
5872 struct ieee80211_ht_capabilities *htcaps_mask,
5873 const char *ht_mcs)
5874{
5875 /* parse ht_mcs into hex array */
5876 int i;
5877 const char *tmp = ht_mcs;
5878 char *end = NULL;
5879
5880 /* If ht_mcs is null, do not set anything */
5881 if (!ht_mcs)
5882 return 0;
5883
5884 /* This is what we are setting in the kernel */
5885 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
5886
5887 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
5888
5889 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
Paul Stewart092955c2017-02-06 09:13:09 -08005890 long v;
5891
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005892 errno = 0;
Paul Stewart092955c2017-02-06 09:13:09 -08005893 v = strtol(tmp, &end, 16);
5894
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005895 if (errno == 0) {
5896 wpa_msg(wpa_s, MSG_DEBUG,
5897 "htcap value[%i]: %ld end: %p tmp: %p",
5898 i, v, end, tmp);
5899 if (end == tmp)
5900 break;
5901
5902 htcaps->supported_mcs_set[i] = v;
5903 tmp = end;
5904 } else {
5905 wpa_msg(wpa_s, MSG_ERROR,
5906 "Failed to parse ht-mcs: %s, error: %s\n",
5907 ht_mcs, strerror(errno));
5908 return -1;
5909 }
5910 }
5911
5912 /*
5913 * If we were able to parse any values, then set mask for the MCS set.
5914 */
5915 if (i) {
5916 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
5917 IEEE80211_HT_MCS_MASK_LEN - 1);
5918 /* skip the 3 reserved bits */
5919 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
5920 0x1f;
5921 }
5922
5923 return 0;
5924}
5925
5926
5927static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
5928 struct ieee80211_ht_capabilities *htcaps,
5929 struct ieee80211_ht_capabilities *htcaps_mask,
5930 int disabled)
5931{
Dmitry Shmidtc2817022014-07-02 10:32:10 -07005932 le16 msk;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005933
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005934 if (disabled == -1)
5935 return 0;
5936
Hai Shalom74f70d42019-02-11 14:42:39 -08005937 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
5938
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005939 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
5940 htcaps_mask->ht_capabilities_info |= msk;
5941 if (disabled)
5942 htcaps->ht_capabilities_info &= msk;
5943 else
5944 htcaps->ht_capabilities_info |= msk;
5945
5946 return 0;
5947}
5948
5949
5950static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
5951 struct ieee80211_ht_capabilities *htcaps,
5952 struct ieee80211_ht_capabilities *htcaps_mask,
5953 int factor)
5954{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005955 if (factor == -1)
5956 return 0;
5957
Hai Shalom74f70d42019-02-11 14:42:39 -08005958 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
5959
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005960 if (factor < 0 || factor > 3) {
5961 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
5962 "Must be 0-3 or -1", factor);
5963 return -EINVAL;
5964 }
5965
5966 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
5967 htcaps->a_mpdu_params &= ~0x3;
5968 htcaps->a_mpdu_params |= factor & 0x3;
5969
5970 return 0;
5971}
5972
5973
5974static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
5975 struct ieee80211_ht_capabilities *htcaps,
5976 struct ieee80211_ht_capabilities *htcaps_mask,
5977 int density)
5978{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005979 if (density == -1)
5980 return 0;
5981
Hai Shalom74f70d42019-02-11 14:42:39 -08005982 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
5983
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005984 if (density < 0 || density > 7) {
5985 wpa_msg(wpa_s, MSG_ERROR,
5986 "ampdu_density: %d out of range. Must be 0-7 or -1.",
5987 density);
5988 return -EINVAL;
5989 }
5990
5991 htcaps_mask->a_mpdu_params |= 0x1C;
5992 htcaps->a_mpdu_params &= ~(0x1C);
5993 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
5994
5995 return 0;
5996}
5997
5998
5999static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
6000 struct ieee80211_ht_capabilities *htcaps,
6001 struct ieee80211_ht_capabilities *htcaps_mask,
6002 int disabled)
6003{
Hai Shalom74f70d42019-02-11 14:42:39 -08006004 if (disabled)
6005 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006006
Paul Stewart092955c2017-02-06 09:13:09 -08006007 set_disable_ht40(htcaps, disabled);
6008 set_disable_ht40(htcaps_mask, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006009
6010 return 0;
6011}
6012
6013
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006014static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
6015 struct ieee80211_ht_capabilities *htcaps,
6016 struct ieee80211_ht_capabilities *htcaps_mask,
6017 int disabled)
6018{
6019 /* Masking these out disables SGI */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006020 le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
6021 HT_CAP_INFO_SHORT_GI40MHZ);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006022
Hai Shalom74f70d42019-02-11 14:42:39 -08006023 if (disabled)
6024 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006025
6026 if (disabled)
6027 htcaps->ht_capabilities_info &= ~msk;
6028 else
6029 htcaps->ht_capabilities_info |= msk;
6030
6031 htcaps_mask->ht_capabilities_info |= msk;
6032
6033 return 0;
6034}
6035
6036
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006037static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
6038 struct ieee80211_ht_capabilities *htcaps,
6039 struct ieee80211_ht_capabilities *htcaps_mask,
6040 int disabled)
6041{
6042 /* Masking these out disables LDPC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006043 le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006044
Hai Shalom74f70d42019-02-11 14:42:39 -08006045 if (disabled)
6046 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006047
6048 if (disabled)
6049 htcaps->ht_capabilities_info &= ~msk;
6050 else
6051 htcaps->ht_capabilities_info |= msk;
6052
6053 htcaps_mask->ht_capabilities_info |= msk;
6054
6055 return 0;
6056}
6057
6058
Hai Shalom74f70d42019-02-11 14:42:39 -08006059static int wpa_set_tx_stbc(struct wpa_supplicant *wpa_s,
6060 struct ieee80211_ht_capabilities *htcaps,
6061 struct ieee80211_ht_capabilities *htcaps_mask,
6062 int tx_stbc)
6063{
6064 le16 msk = host_to_le16(HT_CAP_INFO_TX_STBC);
6065
6066 if (tx_stbc == -1)
6067 return 0;
6068
6069 wpa_msg(wpa_s, MSG_DEBUG, "set_tx_stbc: %d", tx_stbc);
6070
6071 if (tx_stbc < 0 || tx_stbc > 1) {
6072 wpa_msg(wpa_s, MSG_ERROR,
6073 "tx_stbc: %d out of range. Must be 0-1 or -1", tx_stbc);
6074 return -EINVAL;
6075 }
6076
6077 htcaps_mask->ht_capabilities_info |= msk;
6078 htcaps->ht_capabilities_info &= ~msk;
6079 htcaps->ht_capabilities_info |= (tx_stbc << 7) & msk;
6080
6081 return 0;
6082}
6083
6084
6085static int wpa_set_rx_stbc(struct wpa_supplicant *wpa_s,
6086 struct ieee80211_ht_capabilities *htcaps,
6087 struct ieee80211_ht_capabilities *htcaps_mask,
6088 int rx_stbc)
6089{
6090 le16 msk = host_to_le16(HT_CAP_INFO_RX_STBC_MASK);
6091
6092 if (rx_stbc == -1)
6093 return 0;
6094
6095 wpa_msg(wpa_s, MSG_DEBUG, "set_rx_stbc: %d", rx_stbc);
6096
6097 if (rx_stbc < 0 || rx_stbc > 3) {
6098 wpa_msg(wpa_s, MSG_ERROR,
6099 "rx_stbc: %d out of range. Must be 0-3 or -1", rx_stbc);
6100 return -EINVAL;
6101 }
6102
6103 htcaps_mask->ht_capabilities_info |= msk;
6104 htcaps->ht_capabilities_info &= ~msk;
6105 htcaps->ht_capabilities_info |= (rx_stbc << 8) & msk;
6106
6107 return 0;
6108}
6109
6110
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006111void wpa_supplicant_apply_ht_overrides(
6112 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6113 struct wpa_driver_associate_params *params)
6114{
6115 struct ieee80211_ht_capabilities *htcaps;
6116 struct ieee80211_ht_capabilities *htcaps_mask;
6117
6118 if (!ssid)
6119 return;
6120
6121 params->disable_ht = ssid->disable_ht;
6122 if (!params->htcaps || !params->htcaps_mask)
6123 return;
6124
6125 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
6126 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
6127 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
6128 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
6129 ssid->disable_max_amsdu);
6130 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
6131 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
6132 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006133 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006134 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
Hai Shalom74f70d42019-02-11 14:42:39 -08006135 wpa_set_rx_stbc(wpa_s, htcaps, htcaps_mask, ssid->rx_stbc);
6136 wpa_set_tx_stbc(wpa_s, htcaps, htcaps_mask, ssid->tx_stbc);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07006137
6138 if (ssid->ht40_intolerant) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006139 le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07006140 htcaps->ht_capabilities_info |= bit;
6141 htcaps_mask->ht_capabilities_info |= bit;
6142 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006143}
6144
6145#endif /* CONFIG_HT_OVERRIDES */
6146
6147
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006148#ifdef CONFIG_VHT_OVERRIDES
6149void wpa_supplicant_apply_vht_overrides(
6150 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6151 struct wpa_driver_associate_params *params)
6152{
6153 struct ieee80211_vht_capabilities *vhtcaps;
6154 struct ieee80211_vht_capabilities *vhtcaps_mask;
6155
6156 if (!ssid)
6157 return;
6158
6159 params->disable_vht = ssid->disable_vht;
6160
6161 vhtcaps = (void *) params->vhtcaps;
6162 vhtcaps_mask = (void *) params->vhtcaps_mask;
6163
6164 if (!vhtcaps || !vhtcaps_mask)
6165 return;
6166
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006167 vhtcaps->vht_capabilities_info = host_to_le32(ssid->vht_capa);
6168 vhtcaps_mask->vht_capabilities_info = host_to_le32(ssid->vht_capa_mask);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006169
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006170#ifdef CONFIG_HT_OVERRIDES
Hai Shalom74f70d42019-02-11 14:42:39 -08006171 if (ssid->disable_sgi) {
6172 vhtcaps_mask->vht_capabilities_info |= (VHT_CAP_SHORT_GI_80 |
6173 VHT_CAP_SHORT_GI_160);
6174 vhtcaps->vht_capabilities_info &= ~(VHT_CAP_SHORT_GI_80 |
6175 VHT_CAP_SHORT_GI_160);
6176 wpa_msg(wpa_s, MSG_DEBUG,
6177 "disable-sgi override specified, vht-caps: 0x%x",
6178 vhtcaps->vht_capabilities_info);
6179 }
6180
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006181 /* if max ampdu is <= 3, we have to make the HT cap the same */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006182 if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
6183 int max_ampdu;
6184
6185 max_ampdu = (ssid->vht_capa &
6186 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
6187 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006188
6189 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
6190 wpa_set_ampdu_factor(wpa_s,
6191 (void *) params->htcaps,
6192 (void *) params->htcaps_mask,
6193 max_ampdu);
6194 }
6195#endif /* CONFIG_HT_OVERRIDES */
6196
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006197#define OVERRIDE_MCS(i) \
6198 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
6199 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006200 host_to_le16(3 << 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006201 vhtcaps->vht_supported_mcs_set.tx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006202 host_to_le16(ssid->vht_tx_mcs_nss_ ##i << \
6203 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006204 } \
6205 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
6206 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006207 host_to_le16(3 << 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006208 vhtcaps->vht_supported_mcs_set.rx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006209 host_to_le16(ssid->vht_rx_mcs_nss_ ##i << \
6210 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07006211 }
6212
6213 OVERRIDE_MCS(1);
6214 OVERRIDE_MCS(2);
6215 OVERRIDE_MCS(3);
6216 OVERRIDE_MCS(4);
6217 OVERRIDE_MCS(5);
6218 OVERRIDE_MCS(6);
6219 OVERRIDE_MCS(7);
6220 OVERRIDE_MCS(8);
6221}
6222#endif /* CONFIG_VHT_OVERRIDES */
6223
6224
Hai Shalomfdcde762020-04-02 11:19:20 -07006225#ifdef CONFIG_HE_OVERRIDES
6226void wpa_supplicant_apply_he_overrides(
6227 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6228 struct wpa_driver_associate_params *params)
6229{
6230 if (!ssid)
6231 return;
6232
6233 params->disable_he = ssid->disable_he;
6234}
6235#endif /* CONFIG_HE_OVERRIDES */
6236
6237
Sunil Ravi77d572f2023-01-17 23:58:31 +00006238void wpa_supplicant_apply_eht_overrides(
6239 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
6240 struct wpa_driver_associate_params *params)
6241{
6242 if (!ssid)
6243 return;
6244
6245 params->disable_eht = ssid->disable_eht;
6246}
6247
6248
Dmitry Shmidt04949592012-07-19 12:16:46 -07006249static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
6250{
6251#ifdef PCSC_FUNCS
6252 size_t len;
6253
6254 if (!wpa_s->conf->pcsc_reader)
6255 return 0;
6256
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006257 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006258 if (!wpa_s->scard)
6259 return 1;
6260
6261 if (wpa_s->conf->pcsc_pin &&
6262 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
6263 scard_deinit(wpa_s->scard);
6264 wpa_s->scard = NULL;
6265 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
6266 return -1;
6267 }
6268
6269 len = sizeof(wpa_s->imsi) - 1;
6270 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
6271 scard_deinit(wpa_s->scard);
6272 wpa_s->scard = NULL;
6273 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
6274 return -1;
6275 }
6276 wpa_s->imsi[len] = '\0';
6277
6278 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
6279
6280 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
6281 wpa_s->imsi, wpa_s->mnc_len);
6282
6283 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
6284 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
6285#endif /* PCSC_FUNCS */
6286
6287 return 0;
6288}
6289
6290
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006291int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
6292{
6293 char *val, *pos;
6294
6295 ext_password_deinit(wpa_s->ext_pw);
6296 wpa_s->ext_pw = NULL;
6297 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
6298
6299 if (!wpa_s->conf->ext_password_backend)
6300 return 0;
6301
6302 val = os_strdup(wpa_s->conf->ext_password_backend);
6303 if (val == NULL)
6304 return -1;
6305 pos = os_strchr(val, ':');
6306 if (pos)
6307 *pos++ = '\0';
6308
6309 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
6310
6311 wpa_s->ext_pw = ext_password_init(val, pos);
6312 os_free(val);
6313 if (wpa_s->ext_pw == NULL) {
6314 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
6315 return -1;
6316 }
6317 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
6318
6319 return 0;
6320}
6321
6322
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006323#ifdef CONFIG_FST
6324
6325static const u8 * wpas_fst_get_bssid_cb(void *ctx)
6326{
6327 struct wpa_supplicant *wpa_s = ctx;
6328
6329 return (is_zero_ether_addr(wpa_s->bssid) ||
6330 wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
6331}
6332
6333
6334static void wpas_fst_get_channel_info_cb(void *ctx,
6335 enum hostapd_hw_mode *hw_mode,
6336 u8 *channel)
6337{
6338 struct wpa_supplicant *wpa_s = ctx;
6339
6340 if (wpa_s->current_bss) {
6341 *hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
6342 channel);
6343 } else if (wpa_s->hw.num_modes) {
6344 *hw_mode = wpa_s->hw.modes[0].mode;
6345 } else {
6346 WPA_ASSERT(0);
6347 *hw_mode = 0;
6348 }
6349}
6350
6351
6352static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
6353{
6354 struct wpa_supplicant *wpa_s = ctx;
6355
6356 *modes = wpa_s->hw.modes;
6357 return wpa_s->hw.num_modes;
6358}
6359
6360
6361static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
6362{
6363 struct wpa_supplicant *wpa_s = ctx;
6364
6365 wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
6366 wpa_s->fst_ies = fst_ies;
6367}
6368
6369
6370static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
6371{
6372 struct wpa_supplicant *wpa_s = ctx;
6373
Paul Stewart092955c2017-02-06 09:13:09 -08006374 if (os_memcmp(wpa_s->bssid, da, ETH_ALEN) != 0) {
6375 wpa_printf(MSG_INFO, "FST:%s:bssid=" MACSTR " != da=" MACSTR,
6376 __func__, MAC2STR(wpa_s->bssid), MAC2STR(da));
6377 return -1;
6378 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006379 return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
Paul Stewart092955c2017-02-06 09:13:09 -08006380 wpa_s->own_addr, wpa_s->bssid,
6381 wpabuf_head(data), wpabuf_len(data),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006382 0);
6383}
6384
6385
6386static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
6387{
6388 struct wpa_supplicant *wpa_s = ctx;
6389
6390 WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
6391 return wpa_s->received_mb_ies;
6392}
6393
6394
6395static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
6396 const u8 *buf, size_t size)
6397{
6398 struct wpa_supplicant *wpa_s = ctx;
6399 struct mb_ies_info info;
6400
6401 WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
6402
6403 if (!mb_ies_info_by_ies(&info, buf, size)) {
6404 wpabuf_free(wpa_s->received_mb_ies);
6405 wpa_s->received_mb_ies = mb_ies_by_info(&info);
6406 }
6407}
6408
6409
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006410static const u8 * wpas_fst_get_peer_first(void *ctx,
6411 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07006412 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006413{
6414 struct wpa_supplicant *wpa_s = ctx;
6415
6416 *get_ctx = NULL;
6417 if (!is_zero_ether_addr(wpa_s->bssid))
6418 return (wpa_s->received_mb_ies || !mb_only) ?
6419 wpa_s->bssid : NULL;
6420 return NULL;
6421}
6422
6423
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07006424static const u8 * wpas_fst_get_peer_next(void *ctx,
6425 struct fst_get_peer_ctx **get_ctx,
Hai Shalome21d4e82020-04-29 16:34:06 -07006426 bool mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006427{
6428 return NULL;
6429}
6430
6431void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
6432 struct fst_wpa_obj *iface_obj)
6433{
Sunil8cd6f4d2022-06-28 18:40:46 +00006434 os_memset(iface_obj, 0, sizeof(*iface_obj));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006435 iface_obj->ctx = wpa_s;
6436 iface_obj->get_bssid = wpas_fst_get_bssid_cb;
6437 iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
6438 iface_obj->get_hw_modes = wpas_fst_get_hw_modes;
6439 iface_obj->set_ies = wpas_fst_set_ies_cb;
6440 iface_obj->send_action = wpas_fst_send_action_cb;
6441 iface_obj->get_mb_ie = wpas_fst_get_mb_ie_cb;
6442 iface_obj->update_mb_ie = wpas_fst_update_mb_ie_cb;
6443 iface_obj->get_peer_first = wpas_fst_get_peer_first;
6444 iface_obj->get_peer_next = wpas_fst_get_peer_next;
6445}
6446#endif /* CONFIG_FST */
6447
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006448static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006449 const struct wpa_driver_capa *capa)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006450{
Dmitry Shmidt0207e232014-09-03 14:58:37 -07006451 struct wowlan_triggers *triggers;
6452 int ret = 0;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006453
6454 if (!wpa_s->conf->wowlan_triggers)
6455 return 0;
6456
Dmitry Shmidt0207e232014-09-03 14:58:37 -07006457 triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
6458 if (triggers) {
6459 ret = wpa_drv_wowlan(wpa_s, triggers);
6460 os_free(triggers);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006461 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07006462 return ret;
6463}
6464
6465
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006466enum wpa_radio_work_band wpas_freq_to_band(int freq)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006467{
6468 if (freq < 3000)
6469 return BAND_2_4_GHZ;
6470 if (freq > 50000)
6471 return BAND_60_GHZ;
6472 return BAND_5_GHZ;
6473}
6474
6475
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006476unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s, const int *freqs)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006477{
6478 int i;
6479 unsigned int band = 0;
6480
6481 if (freqs) {
6482 /* freqs are specified for the radio work */
6483 for (i = 0; freqs[i]; i++)
6484 band |= wpas_freq_to_band(freqs[i]);
6485 } else {
6486 /*
6487 * freqs are not specified, implies all
6488 * the supported freqs by HW
6489 */
6490 for (i = 0; i < wpa_s->hw.num_modes; i++) {
6491 if (wpa_s->hw.modes[i].num_channels != 0) {
6492 if (wpa_s->hw.modes[i].mode ==
6493 HOSTAPD_MODE_IEEE80211B ||
6494 wpa_s->hw.modes[i].mode ==
6495 HOSTAPD_MODE_IEEE80211G)
6496 band |= BAND_2_4_GHZ;
6497 else if (wpa_s->hw.modes[i].mode ==
6498 HOSTAPD_MODE_IEEE80211A)
6499 band |= BAND_5_GHZ;
6500 else if (wpa_s->hw.modes[i].mode ==
6501 HOSTAPD_MODE_IEEE80211AD)
6502 band |= BAND_60_GHZ;
6503 else if (wpa_s->hw.modes[i].mode ==
6504 HOSTAPD_MODE_IEEE80211ANY)
6505 band = BAND_2_4_GHZ | BAND_5_GHZ |
6506 BAND_60_GHZ;
6507 }
6508 }
6509 }
6510
6511 return band;
6512}
6513
6514
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006515static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
6516 const char *rn)
6517{
6518 struct wpa_supplicant *iface = wpa_s->global->ifaces;
6519 struct wpa_radio *radio;
6520
6521 while (rn && iface) {
6522 radio = iface->radio;
6523 if (radio && os_strcmp(rn, radio->name) == 0) {
6524 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
6525 wpa_s->ifname, rn);
6526 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
6527 return radio;
6528 }
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08006529
6530 iface = iface->next;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006531 }
6532
6533 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
6534 wpa_s->ifname, rn ? rn : "N/A");
6535 radio = os_zalloc(sizeof(*radio));
6536 if (radio == NULL)
6537 return NULL;
6538
6539 if (rn)
6540 os_strlcpy(radio->name, rn, sizeof(radio->name));
6541 dl_list_init(&radio->ifaces);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006542 dl_list_init(&radio->work);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006543 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
6544
6545 return radio;
6546}
6547
6548
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006549static void radio_work_free(struct wpa_radio_work *work)
6550{
6551 if (work->wpa_s->scan_work == work) {
6552 /* This should not really happen. */
6553 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
6554 work->type, work, work->started);
6555 work->wpa_s->scan_work = NULL;
6556 }
6557
6558#ifdef CONFIG_P2P
6559 if (work->wpa_s->p2p_scan_work == work) {
6560 /* This should not really happen. */
6561 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
6562 work->type, work, work->started);
6563 work->wpa_s->p2p_scan_work = NULL;
6564 }
6565#endif /* CONFIG_P2P */
6566
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006567 if (work->started) {
6568 work->wpa_s->radio->num_active_works--;
6569 wpa_dbg(work->wpa_s, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006570 "radio_work_free('%s'@%p): num_active_works --> %u",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006571 work->type, work,
6572 work->wpa_s->radio->num_active_works);
6573 }
6574
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006575 dl_list_del(&work->list);
6576 os_free(work);
6577}
6578
6579
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006580static int radio_work_is_connect(struct wpa_radio_work *work)
6581{
6582 return os_strcmp(work->type, "sme-connect") == 0 ||
6583 os_strcmp(work->type, "connect") == 0;
6584}
6585
6586
6587static int radio_work_is_scan(struct wpa_radio_work *work)
6588{
6589 return os_strcmp(work->type, "scan") == 0 ||
6590 os_strcmp(work->type, "p2p-scan") == 0;
6591}
6592
6593
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006594static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
6595{
6596 struct wpa_radio_work *active_work = NULL;
6597 struct wpa_radio_work *tmp;
6598
6599 /* Get the active work to know the type and band. */
6600 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
6601 if (tmp->started) {
6602 active_work = tmp;
6603 break;
6604 }
6605 }
6606
6607 if (!active_work) {
6608 /* No active work, start one */
6609 radio->num_active_works = 0;
6610 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
6611 list) {
6612 if (os_strcmp(tmp->type, "scan") == 0 &&
Hai Shalom60840252021-02-19 19:02:11 -08006613 external_scan_running(radio) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006614 (((struct wpa_driver_scan_params *)
6615 tmp->ctx)->only_new_results ||
6616 tmp->wpa_s->clear_driver_scan_cache))
6617 continue;
6618 return tmp;
6619 }
6620 return NULL;
6621 }
6622
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006623 if (radio_work_is_connect(active_work)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006624 /*
6625 * If the active work is either connect or sme-connect,
6626 * do not parallelize them with other radio works.
6627 */
6628 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
6629 "Do not parallelize radio work with %s",
6630 active_work->type);
6631 return NULL;
6632 }
6633
6634 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
6635 if (tmp->started)
6636 continue;
6637
6638 /*
6639 * If connect or sme-connect are enqueued, parallelize only
6640 * those operations ahead of them in the queue.
6641 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006642 if (radio_work_is_connect(tmp))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006643 break;
6644
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006645 /* Serialize parallel scan and p2p_scan operations on the same
6646 * interface since the driver_nl80211 mechanism for tracking
6647 * scan cookies does not yet have support for this. */
6648 if (active_work->wpa_s == tmp->wpa_s &&
6649 radio_work_is_scan(active_work) &&
6650 radio_work_is_scan(tmp)) {
6651 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
6652 "Do not start work '%s' when another work '%s' is already scheduled",
6653 tmp->type, active_work->type);
6654 continue;
6655 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006656 /*
6657 * Check that the radio works are distinct and
6658 * on different bands.
6659 */
6660 if (os_strcmp(active_work->type, tmp->type) != 0 &&
6661 (active_work->bands != tmp->bands)) {
6662 /*
6663 * If a scan has to be scheduled through nl80211 scan
6664 * interface and if an external scan is already running,
6665 * do not schedule the scan since it is likely to get
6666 * rejected by kernel.
6667 */
6668 if (os_strcmp(tmp->type, "scan") == 0 &&
Hai Shalom60840252021-02-19 19:02:11 -08006669 external_scan_running(radio) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006670 (((struct wpa_driver_scan_params *)
6671 tmp->ctx)->only_new_results ||
6672 tmp->wpa_s->clear_driver_scan_cache))
6673 continue;
6674
6675 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
6676 "active_work:%s new_work:%s",
6677 active_work->type, tmp->type);
6678 return tmp;
6679 }
6680 }
6681
6682 /* Did not find a radio work to schedule in parallel. */
6683 return NULL;
6684}
6685
6686
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006687static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
6688{
6689 struct wpa_radio *radio = eloop_ctx;
6690 struct wpa_radio_work *work;
6691 struct os_reltime now, diff;
6692 struct wpa_supplicant *wpa_s;
6693
6694 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006695 if (work == NULL) {
6696 radio->num_active_works = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006697 return;
6698 }
6699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006700 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
6701 radio_list);
6702
6703 if (!(wpa_s &&
6704 wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
6705 if (work->started)
6706 return; /* already started and still in progress */
6707
Hai Shalom60840252021-02-19 19:02:11 -08006708 if (wpa_s && external_scan_running(wpa_s->radio)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006709 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
6710 return;
6711 }
6712 } else {
6713 work = NULL;
6714 if (radio->num_active_works < MAX_ACTIVE_WORKS) {
6715 /* get the work to schedule next */
6716 work = radio_work_get_next_work(radio);
6717 }
6718 if (!work)
6719 return;
6720 }
6721
6722 wpa_s = work->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006723 os_get_reltime(&now);
6724 os_reltime_sub(&now, &work->time, &diff);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006725 wpa_dbg(wpa_s, MSG_DEBUG,
6726 "Starting radio work '%s'@%p after %ld.%06ld second wait",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006727 work->type, work, diff.sec, diff.usec);
6728 work->started = 1;
6729 work->time = now;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006730 radio->num_active_works++;
6731
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006732 work->cb(work, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006733
6734 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
6735 radio->num_active_works < MAX_ACTIVE_WORKS)
6736 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006737}
6738
6739
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006740/*
6741 * This function removes both started and pending radio works running on
6742 * the provided interface's radio.
6743 * Prior to the removal of the radio work, its callback (cb) is called with
6744 * deinit set to be 1. Each work's callback is responsible for clearing its
6745 * internal data and restoring to a correct state.
6746 * @wpa_s: wpa_supplicant data
6747 * @type: type of works to be removed
6748 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
6749 * this interface's works.
6750 */
6751void radio_remove_works(struct wpa_supplicant *wpa_s,
6752 const char *type, int remove_all)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006753{
6754 struct wpa_radio_work *work, *tmp;
6755 struct wpa_radio *radio = wpa_s->radio;
6756
6757 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
6758 list) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006759 if (type && os_strcmp(type, work->type) != 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006760 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006761
6762 /* skip other ifaces' works */
6763 if (!remove_all && work->wpa_s != wpa_s)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006764 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006765
6766 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
6767 work->type, work, work->started ? " (started)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006768 work->cb(work, 1);
6769 radio_work_free(work);
6770 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08006771
6772 /* in case we removed the started work */
6773 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006774}
6775
6776
Roshan Pius3a1667e2018-07-03 15:17:14 -07006777void radio_remove_pending_work(struct wpa_supplicant *wpa_s, void *ctx)
6778{
6779 struct wpa_radio_work *work;
6780 struct wpa_radio *radio = wpa_s->radio;
6781
6782 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
6783 if (work->ctx != ctx)
6784 continue;
6785 wpa_dbg(wpa_s, MSG_DEBUG, "Free pending radio work '%s'@%p%s",
6786 work->type, work, work->started ? " (started)" : "");
6787 radio_work_free(work);
6788 break;
6789 }
6790}
6791
6792
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006793static void radio_remove_interface(struct wpa_supplicant *wpa_s)
6794{
6795 struct wpa_radio *radio = wpa_s->radio;
6796
6797 if (!radio)
6798 return;
6799
6800 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
6801 wpa_s->ifname, radio->name);
6802 dl_list_del(&wpa_s->radio_list);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07006803 radio_remove_works(wpa_s, NULL, 0);
Hai Shalom60840252021-02-19 19:02:11 -08006804 /* If the interface that triggered the external scan was removed, the
6805 * external scan is no longer running. */
6806 if (wpa_s == radio->external_scan_req_interface)
6807 radio->external_scan_req_interface = NULL;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07006808 wpa_s->radio = NULL;
6809 if (!dl_list_empty(&radio->ifaces))
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006810 return; /* Interfaces remain for this radio */
6811
6812 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006813 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006814 os_free(radio);
6815}
6816
6817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006818void radio_work_check_next(struct wpa_supplicant *wpa_s)
6819{
6820 struct wpa_radio *radio = wpa_s->radio;
6821
6822 if (dl_list_empty(&radio->work))
6823 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006824 if (wpa_s->ext_work_in_progress) {
6825 wpa_printf(MSG_DEBUG,
6826 "External radio work in progress - delay start of pending item");
6827 return;
6828 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006829 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
6830 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
6831}
6832
6833
6834/**
6835 * radio_add_work - Add a radio work item
6836 * @wpa_s: Pointer to wpa_supplicant data
6837 * @freq: Frequency of the offchannel operation in MHz or 0
6838 * @type: Unique identifier for each type of work
6839 * @next: Force as the next work to be executed
6840 * @cb: Callback function for indicating when radio is available
6841 * @ctx: Context pointer for the work (work->ctx in cb())
6842 * Returns: 0 on success, -1 on failure
6843 *
6844 * This function is used to request time for an operation that requires
6845 * exclusive radio control. Once the radio is available, the registered callback
6846 * function will be called. radio_work_done() must be called once the exclusive
6847 * radio operation has been completed, so that the radio is freed for other
6848 * operations. The special case of deinit=1 is used to free the context data
6849 * during interface removal. That does not allow the callback function to start
6850 * the radio operation, i.e., it must free any resources allocated for the radio
6851 * work and return.
6852 *
6853 * The @freq parameter can be used to indicate a single channel on which the
6854 * offchannel operation will occur. This may allow multiple radio work
6855 * operations to be performed in parallel if they apply for the same channel.
6856 * Setting this to 0 indicates that the work item may use multiple channels or
6857 * requires exclusive control of the radio.
6858 */
6859int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
6860 const char *type, int next,
6861 void (*cb)(struct wpa_radio_work *work, int deinit),
6862 void *ctx)
6863{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006864 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006865 struct wpa_radio_work *work;
6866 int was_empty;
6867
6868 work = os_zalloc(sizeof(*work));
6869 if (work == NULL)
6870 return -1;
6871 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
6872 os_get_reltime(&work->time);
6873 work->freq = freq;
6874 work->type = type;
6875 work->wpa_s = wpa_s;
6876 work->cb = cb;
6877 work->ctx = ctx;
6878
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006879 if (freq)
6880 work->bands = wpas_freq_to_band(freq);
6881 else if (os_strcmp(type, "scan") == 0 ||
6882 os_strcmp(type, "p2p-scan") == 0)
6883 work->bands = wpas_get_bands(wpa_s,
6884 ((struct wpa_driver_scan_params *)
6885 ctx)->freqs);
6886 else
6887 work->bands = wpas_get_bands(wpa_s, NULL);
6888
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006889 was_empty = dl_list_empty(&wpa_s->radio->work);
6890 if (next)
6891 dl_list_add(&wpa_s->radio->work, &work->list);
6892 else
6893 dl_list_add_tail(&wpa_s->radio->work, &work->list);
6894 if (was_empty) {
6895 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
6896 radio_work_check_next(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006897 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
6898 && radio->num_active_works < MAX_ACTIVE_WORKS) {
6899 wpa_dbg(wpa_s, MSG_DEBUG,
6900 "Try to schedule a radio work (num_active_works=%u)",
6901 radio->num_active_works);
6902 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006903 }
6904
6905 return 0;
6906}
6907
6908
6909/**
6910 * radio_work_done - Indicate that a radio work item has been completed
6911 * @work: Completed work
6912 *
6913 * This function is called once the callback function registered with
6914 * radio_add_work() has completed its work.
6915 */
6916void radio_work_done(struct wpa_radio_work *work)
6917{
6918 struct wpa_supplicant *wpa_s = work->wpa_s;
6919 struct os_reltime now, diff;
6920 unsigned int started = work->started;
6921
6922 os_get_reltime(&now);
6923 os_reltime_sub(&now, &work->time, &diff);
6924 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
6925 work->type, work, started ? "done" : "canceled",
6926 diff.sec, diff.usec);
6927 radio_work_free(work);
6928 if (started)
6929 radio_work_check_next(wpa_s);
6930}
6931
6932
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08006933struct wpa_radio_work *
6934radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006935{
6936 struct wpa_radio_work *work;
6937 struct wpa_radio *radio = wpa_s->radio;
6938
6939 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
6940 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08006941 return work;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006942 }
6943
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08006944 return NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006945}
6946
6947
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006948static int wpas_init_driver(struct wpa_supplicant *wpa_s,
Roshan Pius3a1667e2018-07-03 15:17:14 -07006949 const struct wpa_interface *iface)
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006950{
6951 const char *ifname, *driver, *rn;
6952
6953 driver = iface->driver;
6954next_driver:
6955 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
6956 return -1;
6957
6958 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
6959 if (wpa_s->drv_priv == NULL) {
6960 const char *pos;
Hai Shalom899fcc72020-10-19 14:38:18 -07006961 int level = MSG_ERROR;
6962
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006963 pos = driver ? os_strchr(driver, ',') : NULL;
6964 if (pos) {
6965 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
6966 "driver interface - try next driver wrapper");
6967 driver = pos + 1;
6968 goto next_driver;
6969 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006970
6971#ifdef CONFIG_MATCH_IFACE
6972 if (wpa_s->matched == WPA_IFACE_MATCHED_NULL)
6973 level = MSG_DEBUG;
6974#endif /* CONFIG_MATCH_IFACE */
6975 wpa_msg(wpa_s, level, "Failed to initialize driver interface");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006976 return -1;
6977 }
6978 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
6979 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
6980 "driver_param '%s'", wpa_s->conf->driver_param);
6981 return -1;
6982 }
6983
6984 ifname = wpa_drv_get_ifname(wpa_s);
6985 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
6986 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
6987 "interface name with '%s'", ifname);
6988 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
6989 }
6990
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07006991 rn = wpa_driver_get_radio_name(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006992 if (rn && rn[0] == '\0')
6993 rn = NULL;
6994
6995 wpa_s->radio = radio_add_interface(wpa_s, rn);
6996 if (wpa_s->radio == NULL)
6997 return -1;
6998
6999 return 0;
7000}
7001
7002
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007003#ifdef CONFIG_GAS_SERVER
7004
7005static void wpas_gas_server_tx_status(struct wpa_supplicant *wpa_s,
7006 unsigned int freq, const u8 *dst,
7007 const u8 *src, const u8 *bssid,
7008 const u8 *data, size_t data_len,
7009 enum offchannel_send_action_result result)
7010{
7011 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
7012 " result=%s",
7013 freq, MAC2STR(dst),
7014 result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
7015 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
7016 "FAILED"));
7017 gas_server_tx_status(wpa_s->gas_server, dst, data, data_len,
7018 result == OFFCHANNEL_SEND_ACTION_SUCCESS);
7019}
7020
7021
7022static void wpas_gas_server_tx(void *ctx, int freq, const u8 *da,
7023 struct wpabuf *buf, unsigned int wait_time)
7024{
7025 struct wpa_supplicant *wpa_s = ctx;
7026 const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7027
7028 if (wait_time > wpa_s->max_remain_on_chan)
7029 wait_time = wpa_s->max_remain_on_chan;
7030
7031 offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, broadcast,
7032 wpabuf_head(buf), wpabuf_len(buf),
7033 wait_time, wpas_gas_server_tx_status, 0);
7034}
7035
7036#endif /* CONFIG_GAS_SERVER */
7037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
Roshan Pius3a1667e2018-07-03 15:17:14 -07007039 const struct wpa_interface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007040{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007041 struct wpa_driver_capa capa;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007042 int capa_res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007043 u8 dfs_domain;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007044
7045 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
7046 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
7047 iface->confname ? iface->confname : "N/A",
7048 iface->driver ? iface->driver : "default",
7049 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
7050 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
7051
7052 if (iface->confname) {
7053#ifdef CONFIG_BACKEND_FILE
7054 wpa_s->confname = os_rel2abs_path(iface->confname);
7055 if (wpa_s->confname == NULL) {
7056 wpa_printf(MSG_ERROR, "Failed to get absolute path "
7057 "for configuration file '%s'.",
7058 iface->confname);
7059 return -1;
7060 }
7061 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
7062 iface->confname, wpa_s->confname);
7063#else /* CONFIG_BACKEND_FILE */
7064 wpa_s->confname = os_strdup(iface->confname);
7065#endif /* CONFIG_BACKEND_FILE */
Sunil Ravi77d572f2023-01-17 23:58:31 +00007066 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067 if (wpa_s->conf == NULL) {
7068 wpa_printf(MSG_ERROR, "Failed to read or parse "
7069 "configuration '%s'.", wpa_s->confname);
7070 return -1;
7071 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07007072 wpa_s->confanother = os_rel2abs_path(iface->confanother);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007073 if (wpa_s->confanother &&
Sunil Ravi77d572f2023-01-17 23:58:31 +00007074 !wpa_config_read(wpa_s->confanother, wpa_s->conf, true)) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07007075 wpa_printf(MSG_ERROR,
7076 "Failed to read or parse configuration '%s'.",
7077 wpa_s->confanother);
7078 return -1;
7079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080
7081 /*
7082 * Override ctrl_interface and driver_param if set on command
7083 * line.
7084 */
7085 if (iface->ctrl_interface) {
7086 os_free(wpa_s->conf->ctrl_interface);
7087 wpa_s->conf->ctrl_interface =
7088 os_strdup(iface->ctrl_interface);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007089 if (!wpa_s->conf->ctrl_interface) {
7090 wpa_printf(MSG_ERROR,
7091 "Failed to duplicate control interface '%s'.",
7092 iface->ctrl_interface);
7093 return -1;
7094 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007095 }
7096
7097 if (iface->driver_param) {
7098 os_free(wpa_s->conf->driver_param);
7099 wpa_s->conf->driver_param =
7100 os_strdup(iface->driver_param);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007101 if (!wpa_s->conf->driver_param) {
7102 wpa_printf(MSG_ERROR,
7103 "Failed to duplicate driver param '%s'.",
7104 iface->driver_param);
7105 return -1;
7106 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007108
7109 if (iface->p2p_mgmt && !iface->ctrl_interface) {
7110 os_free(wpa_s->conf->ctrl_interface);
7111 wpa_s->conf->ctrl_interface = NULL;
7112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007113 } else
7114 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
7115 iface->driver_param);
7116
7117 if (wpa_s->conf == NULL) {
7118 wpa_printf(MSG_ERROR, "\nNo configuration found.");
7119 return -1;
7120 }
7121
7122 if (iface->ifname == NULL) {
7123 wpa_printf(MSG_ERROR, "\nInterface name is required.");
7124 return -1;
7125 }
7126 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
7127 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
7128 iface->ifname);
7129 return -1;
7130 }
7131 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
Hai Shalom899fcc72020-10-19 14:38:18 -07007132#ifdef CONFIG_MATCH_IFACE
7133 wpa_s->matched = iface->matched;
7134#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007135
7136 if (iface->bridge_ifname) {
7137 if (os_strlen(iface->bridge_ifname) >=
7138 sizeof(wpa_s->bridge_ifname)) {
7139 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
7140 "name '%s'.", iface->bridge_ifname);
7141 return -1;
7142 }
7143 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
7144 sizeof(wpa_s->bridge_ifname));
7145 }
7146
7147 /* RSNA Supplicant Key Management - INITIALIZE */
Hai Shalome21d4e82020-04-29 16:34:06 -07007148 eapol_sm_notify_portEnabled(wpa_s->eapol, false);
7149 eapol_sm_notify_portValid(wpa_s->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007150
7151 /* Initialize driver interface and register driver event handler before
7152 * L2 receive handler so that association events are processed before
7153 * EAPOL-Key packets if both become available for the same select()
7154 * call. */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007155 if (wpas_init_driver(wpa_s, iface) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156 return -1;
7157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158 if (wpa_supplicant_init_wpa(wpa_s) < 0)
7159 return -1;
7160
7161 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
7162 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
7163 NULL);
7164 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
7165
7166 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
7167 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
7168 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
7169 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
7170 "dot11RSNAConfigPMKLifetime");
7171 return -1;
7172 }
7173
7174 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
7175 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
7176 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
7177 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
7178 "dot11RSNAConfigPMKReauthThreshold");
7179 return -1;
7180 }
7181
7182 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
7183 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
7184 wpa_s->conf->dot11RSNAConfigSATimeout)) {
7185 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
7186 "dot11RSNAConfigSATimeout");
7187 return -1;
7188 }
7189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007190 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
7191 &wpa_s->hw.num_modes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007192 &wpa_s->hw.flags,
7193 &dfs_domain);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007194 if (wpa_s->hw.modes) {
7195 u16 i;
7196
7197 for (i = 0; i < wpa_s->hw.num_modes; i++) {
7198 if (wpa_s->hw.modes[i].vht_capab) {
7199 wpa_s->hw_capab = CAPAB_VHT;
7200 break;
7201 }
7202
7203 if (wpa_s->hw.modes[i].ht_capab &
7204 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
7205 wpa_s->hw_capab = CAPAB_HT40;
7206 else if (wpa_s->hw.modes[i].ht_capab &&
7207 wpa_s->hw_capab == CAPAB_NO_HT_VHT)
7208 wpa_s->hw_capab = CAPAB_HT;
7209 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007210 wpa_s->support_6ghz = wpas_is_6ghz_supported(wpa_s, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007211 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007213 capa_res = wpa_drv_get_capa(wpa_s, &capa);
7214 if (capa_res == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007215 wpa_s->drv_capa_known = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007216 wpa_s->drv_flags = capa.flags;
Hai Shalomb755a2a2020-04-23 21:49:02 -07007217 wpa_s->drv_flags2 = capa.flags2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007218 wpa_s->drv_enc = capa.enc;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007219 wpa_s->drv_rrm_flags = capa.rrm_flags;
Sunil Ravia04bd252022-05-02 22:54:18 -07007220 wpa_s->drv_max_acl_mac_addrs = capa.max_acl_mac_addrs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007221 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007222 wpa_s->max_scan_ssids = capa.max_scan_ssids;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007223 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007224 wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
7225 wpa_s->max_sched_scan_plan_interval =
7226 capa.max_sched_scan_plan_interval;
7227 wpa_s->max_sched_scan_plan_iterations =
7228 capa.max_sched_scan_plan_iterations;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007229 wpa_s->sched_scan_supported = capa.sched_scan_supported;
7230 wpa_s->max_match_sets = capa.max_match_sets;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
7232 wpa_s->max_stations = capa.max_stations;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07007233 wpa_s->extended_capa = capa.extended_capa;
7234 wpa_s->extended_capa_mask = capa.extended_capa_mask;
7235 wpa_s->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007236 wpa_s->num_multichan_concurrent =
7237 capa.num_multichan_concurrent;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007238 wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
Sunil Ravi89eba102022-09-13 21:04:37 -07007239 wpa_s->max_num_akms = capa.max_num_akms;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007240
7241 if (capa.mac_addr_rand_scan_supported)
7242 wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
7243 if (wpa_s->sched_scan_supported &&
7244 capa.mac_addr_rand_sched_scan_supported)
7245 wpa_s->mac_addr_rand_supported |=
7246 (MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
Hai Shalom74f70d42019-02-11 14:42:39 -08007247
7248 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
7249 if (wpa_s->extended_capa &&
7250 wpa_s->extended_capa_len >= 3 &&
7251 wpa_s->extended_capa[2] & 0x40)
7252 wpa_s->multi_bss_support = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 }
Sunil Ravi89eba102022-09-13 21:04:37 -07007254#ifdef CONFIG_PASN
7255 wpa_pasn_sm_set_caps(wpa_s->wpa, wpa_s->drv_flags2);
7256#endif /* CONFIG_PASN */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007257 wpa_sm_set_driver_bss_selection(wpa_s->wpa,
7258 !!(wpa_s->drv_flags &
7259 WPA_DRIVER_FLAGS_BSS_SELECTION));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007260 if (wpa_s->max_remain_on_chan == 0)
7261 wpa_s->max_remain_on_chan = 1000;
7262
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007263 /*
7264 * Only take p2p_mgmt parameters when P2P Device is supported.
7265 * Doing it here as it determines whether l2_packet_init() will be done
7266 * during wpa_supplicant_driver_init().
7267 */
7268 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
7269 wpa_s->p2p_mgmt = iface->p2p_mgmt;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007270
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007271 if (wpa_s->num_multichan_concurrent == 0)
7272 wpa_s->num_multichan_concurrent = 1;
7273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007274 if (wpa_supplicant_driver_init(wpa_s) < 0)
7275 return -1;
7276
7277#ifdef CONFIG_TDLS
Roshan Pius3a1667e2018-07-03 15:17:14 -07007278 if (!iface->p2p_mgmt && wpa_tdls_init(wpa_s->wpa))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007279 return -1;
7280#endif /* CONFIG_TDLS */
7281
7282 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
7283 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
7284 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
7285 return -1;
7286 }
7287
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007288#ifdef CONFIG_FST
7289 if (wpa_s->conf->fst_group_id) {
7290 struct fst_iface_cfg cfg;
7291 struct fst_wpa_obj iface_obj;
7292
7293 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
7294 os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
7295 sizeof(cfg.group_id));
7296 cfg.priority = wpa_s->conf->fst_priority;
7297 cfg.llt = wpa_s->conf->fst_llt;
7298
7299 wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
7300 &iface_obj, &cfg);
7301 if (!wpa_s->fst) {
7302 wpa_msg(wpa_s, MSG_ERROR,
7303 "FST: Cannot attach iface %s to group %s",
7304 wpa_s->ifname, cfg.group_id);
7305 return -1;
7306 }
7307 }
7308#endif /* CONFIG_FST */
7309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007310 if (wpas_wps_init(wpa_s))
7311 return -1;
7312
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007313#ifdef CONFIG_GAS_SERVER
7314 wpa_s->gas_server = gas_server_init(wpa_s, wpas_gas_server_tx);
7315 if (!wpa_s->gas_server) {
7316 wpa_printf(MSG_ERROR, "Failed to initialize GAS server");
7317 return -1;
7318 }
7319#endif /* CONFIG_GAS_SERVER */
7320
7321#ifdef CONFIG_DPP
7322 if (wpas_dpp_init(wpa_s) < 0)
7323 return -1;
7324#endif /* CONFIG_DPP */
7325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007326 if (wpa_supplicant_init_eapol(wpa_s) < 0)
7327 return -1;
7328 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
7329
7330 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
7331 if (wpa_s->ctrl_iface == NULL) {
7332 wpa_printf(MSG_ERROR,
7333 "Failed to initialize control interface '%s'.\n"
7334 "You may have another wpa_supplicant process "
7335 "already running or the file was\n"
7336 "left by an unclean termination of wpa_supplicant "
7337 "in which case you will need\n"
7338 "to manually remove this file before starting "
7339 "wpa_supplicant again.\n",
7340 wpa_s->conf->ctrl_interface);
7341 return -1;
7342 }
7343
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007344 wpa_s->gas = gas_query_init(wpa_s);
7345 if (wpa_s->gas == NULL) {
7346 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
7347 return -1;
7348 }
7349
Roshan Pius3a1667e2018-07-03 15:17:14 -07007350 if ((!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) ||
7351 wpa_s->p2p_mgmt) &&
7352 wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007353 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
7354 return -1;
7355 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356
7357 if (wpa_bss_init(wpa_s) < 0)
7358 return -1;
7359
Paul Stewart092955c2017-02-06 09:13:09 -08007360#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
7361#ifdef CONFIG_MESH
7362 dl_list_init(&wpa_s->mesh_external_pmksa_cache);
7363#endif /* CONFIG_MESH */
7364#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
7365
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007366 /*
7367 * Set Wake-on-WLAN triggers, if configured.
7368 * Note: We don't restore/remove the triggers on shutdown (it doesn't
7369 * have effect anyway when the interface is down).
7370 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007371 if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07007372 return -1;
7373
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007374#ifdef CONFIG_EAP_PROXY
7375{
7376 size_t len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007377 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
7378 wpa_s->imsi, &len);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007379 if (wpa_s->mnc_len > 0) {
7380 wpa_s->imsi[len] = '\0';
7381 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
7382 wpa_s->imsi, wpa_s->mnc_len);
7383 } else {
7384 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
7385 }
7386}
7387#endif /* CONFIG_EAP_PROXY */
7388
Dmitry Shmidt04949592012-07-19 12:16:46 -07007389 if (pcsc_reader_init(wpa_s) < 0)
7390 return -1;
7391
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007392 if (wpas_init_ext_pw(wpa_s) < 0)
7393 return -1;
7394
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007395 wpas_rrm_reset(wpa_s);
7396
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007397 wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
7398
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007399#ifdef CONFIG_HS20
7400 hs20_init(wpa_s);
7401#endif /* CONFIG_HS20 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007402#ifdef CONFIG_MBO
Hai Shalomc3565922019-10-28 11:58:20 -07007403 if (!wpa_s->disable_mbo_oce && wpa_s->conf->oce) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007404 if ((wpa_s->conf->oce & OCE_STA) &&
7405 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
7406 wpa_s->enable_oce = OCE_STA;
7407 if ((wpa_s->conf->oce & OCE_STA_CFON) &&
7408 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
7409 /* TODO: Need to add STA-CFON support */
7410 wpa_printf(MSG_ERROR,
7411 "OCE STA-CFON feature is not yet supported");
7412 }
7413 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007414 wpas_mbo_update_non_pref_chan(wpa_s, wpa_s->conf->non_pref_chan);
7415#endif /* CONFIG_MBO */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007416
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07007417 wpa_supplicant_set_default_scan_ies(wpa_s);
7418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007419 return 0;
7420}
7421
7422
7423static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007424 int notify, int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007425{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007426 struct wpa_global *global = wpa_s->global;
7427 struct wpa_supplicant *iface, *prev;
7428
Jimmy Chen0e73c002021-08-18 13:21:30 +08007429 if (wpa_s == wpa_s->parent || (wpa_s == wpa_s->p2pdev && wpa_s->p2p_mgmt))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007430 wpas_p2p_group_remove(wpa_s, "*");
7431
7432 iface = global->ifaces;
7433 while (iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007434 if (iface->p2pdev == wpa_s)
7435 iface->p2pdev = iface->parent;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007436 if (iface == wpa_s || iface->parent != wpa_s) {
7437 iface = iface->next;
7438 continue;
7439 }
7440 wpa_printf(MSG_DEBUG,
7441 "Remove remaining child interface %s from parent %s",
7442 iface->ifname, wpa_s->ifname);
7443 prev = iface;
7444 iface = iface->next;
7445 wpa_supplicant_remove_iface(global, prev, terminate);
7446 }
7447
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007448 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007449 if (wpa_s->drv_priv) {
Hai Shalom60840252021-02-19 19:02:11 -08007450 /*
7451 * Don't deauthenticate if WoWLAN is enable and not explicitly
7452 * been configured to disconnect.
7453 */
7454 if (!wpa_drv_get_wowlan(wpa_s) ||
7455 wpa_s->conf->wowlan_disconnect_on_deinit) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007456 wpa_supplicant_deauthenticate(
7457 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458
Hai Shalomfdcde762020-04-02 11:19:20 -07007459 wpa_drv_set_countermeasures(wpa_s, 0);
7460 wpa_clear_keys(wpa_s, NULL);
7461 } else {
7462 wpa_msg(wpa_s, MSG_INFO,
7463 "Do not deauthenticate as part of interface deinit since WoWLAN is enabled");
7464 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465 }
7466
7467 wpa_supplicant_cleanup(wpa_s);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007468 wpas_p2p_deinit_iface(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007469
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007470 wpas_ctrl_radio_work_flush(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007471 radio_remove_interface(wpa_s);
7472
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007473#ifdef CONFIG_FST
7474 if (wpa_s->fst) {
7475 fst_detach(wpa_s->fst);
7476 wpa_s->fst = NULL;
7477 }
7478 if (wpa_s->received_mb_ies) {
7479 wpabuf_free(wpa_s->received_mb_ies);
7480 wpa_s->received_mb_ies = NULL;
7481 }
7482#endif /* CONFIG_FST */
7483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484 if (wpa_s->drv_priv)
7485 wpa_drv_deinit(wpa_s);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07007486
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007487 if (notify)
7488 wpas_notify_iface_removed(wpa_s);
7489
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007490 if (terminate)
7491 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07007492
Jouni Malinenf3f8d3c2021-02-05 00:28:17 +02007493 wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
7494 wpa_s->ctrl_iface = NULL;
Irfan Sheriff622b66d2011-08-03 09:11:49 -07007495
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007496#ifdef CONFIG_MESH
7497 if (wpa_s->ifmsh) {
Hai Shalom60840252021-02-19 19:02:11 -08007498 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh, true);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007499 wpa_s->ifmsh = NULL;
7500 }
7501#endif /* CONFIG_MESH */
7502
Irfan Sheriff622b66d2011-08-03 09:11:49 -07007503 if (wpa_s->conf != NULL) {
Irfan Sheriff622b66d2011-08-03 09:11:49 -07007504 wpa_config_free(wpa_s->conf);
7505 wpa_s->conf = NULL;
7506 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007507
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07007508 os_free(wpa_s->ssids_from_scan_req);
Hai Shalomc3565922019-10-28 11:58:20 -07007509 os_free(wpa_s->last_scan_freqs);
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07007510
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007511 os_free(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007512}
7513
7514
Dmitry Shmidte4663042016-04-04 10:07:49 -07007515#ifdef CONFIG_MATCH_IFACE
7516
7517/**
7518 * wpa_supplicant_match_iface - Match an interface description to a name
7519 * @global: Pointer to global data from wpa_supplicant_init()
7520 * @ifname: Name of the interface to match
7521 * Returns: Pointer to the created interface description or %NULL on failure
7522 */
7523struct wpa_interface * wpa_supplicant_match_iface(struct wpa_global *global,
7524 const char *ifname)
7525{
7526 int i;
7527 struct wpa_interface *iface, *miface;
7528
7529 for (i = 0; i < global->params.match_iface_count; i++) {
7530 miface = &global->params.match_ifaces[i];
7531 if (!miface->ifname ||
7532 fnmatch(miface->ifname, ifname, 0) == 0) {
7533 iface = os_zalloc(sizeof(*iface));
7534 if (!iface)
7535 return NULL;
7536 *iface = *miface;
Hai Shalom899fcc72020-10-19 14:38:18 -07007537 if (!miface->ifname)
7538 iface->matched = WPA_IFACE_MATCHED_NULL;
7539 else
7540 iface->matched = WPA_IFACE_MATCHED;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007541 iface->ifname = ifname;
7542 return iface;
7543 }
7544 }
7545
7546 return NULL;
7547}
7548
7549
7550/**
7551 * wpa_supplicant_match_existing - Match existing interfaces
7552 * @global: Pointer to global data from wpa_supplicant_init()
7553 * Returns: 0 on success, -1 on failure
7554 */
7555static int wpa_supplicant_match_existing(struct wpa_global *global)
7556{
7557 struct if_nameindex *ifi, *ifp;
7558 struct wpa_supplicant *wpa_s;
7559 struct wpa_interface *iface;
7560
7561 ifp = if_nameindex();
7562 if (!ifp) {
7563 wpa_printf(MSG_ERROR, "if_nameindex: %s", strerror(errno));
7564 return -1;
7565 }
7566
7567 for (ifi = ifp; ifi->if_name; ifi++) {
7568 wpa_s = wpa_supplicant_get_iface(global, ifi->if_name);
7569 if (wpa_s)
7570 continue;
7571 iface = wpa_supplicant_match_iface(global, ifi->if_name);
7572 if (iface) {
Hai Shalom60840252021-02-19 19:02:11 -08007573 wpa_supplicant_add_iface(global, iface, NULL);
Dmitry Shmidte4663042016-04-04 10:07:49 -07007574 os_free(iface);
Dmitry Shmidte4663042016-04-04 10:07:49 -07007575 }
7576 }
7577
7578 if_freenameindex(ifp);
7579 return 0;
7580}
7581
7582#endif /* CONFIG_MATCH_IFACE */
7583
7584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007585/**
7586 * wpa_supplicant_add_iface - Add a new network interface
7587 * @global: Pointer to global data from wpa_supplicant_init()
7588 * @iface: Interface configuration options
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007589 * @parent: Parent interface or %NULL to assign new interface as parent
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007590 * Returns: Pointer to the created interface or %NULL on failure
7591 *
7592 * This function is used to add new network interfaces for %wpa_supplicant.
7593 * This can be called before wpa_supplicant_run() to add interfaces before the
7594 * main event loop has been started. In addition, new interfaces can be added
7595 * dynamically while %wpa_supplicant is already running. This could happen,
7596 * e.g., when a hotplug network adapter is inserted.
7597 */
7598struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007599 struct wpa_interface *iface,
7600 struct wpa_supplicant *parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601{
7602 struct wpa_supplicant *wpa_s;
7603 struct wpa_interface t_iface;
7604 struct wpa_ssid *ssid;
7605
7606 if (global == NULL || iface == NULL)
7607 return NULL;
7608
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007609 wpa_s = wpa_supplicant_alloc(parent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007610 if (wpa_s == NULL)
7611 return NULL;
7612
7613 wpa_s->global = global;
7614
7615 t_iface = *iface;
7616 if (global->params.override_driver) {
7617 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
7618 "('%s' -> '%s')",
7619 iface->driver, global->params.override_driver);
7620 t_iface.driver = global->params.override_driver;
7621 }
7622 if (global->params.override_ctrl_interface) {
7623 wpa_printf(MSG_DEBUG, "Override interface parameter: "
7624 "ctrl_interface ('%s' -> '%s')",
7625 iface->ctrl_interface,
7626 global->params.override_ctrl_interface);
7627 t_iface.ctrl_interface =
7628 global->params.override_ctrl_interface;
7629 }
7630 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
7631 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
7632 iface->ifname);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007633 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007634 return NULL;
7635 }
7636
Roshan Piusd6d8b8d2016-11-08 14:45:26 -08007637 /* Notify the control interfaces about new iface */
7638 if (wpas_notify_iface_added(wpa_s)) {
7639 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
7640 return NULL;
7641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007642
Jimmy Chene2206be2022-07-10 10:25:21 +08007643 /* Notify the control interfaces about new networks */
7644 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
7645 if (iface->p2p_mgmt == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007646 wpas_notify_network_added(wpa_s, ssid);
Jimmy Chene2206be2022-07-10 10:25:21 +08007647 } else if (ssid->ssid_len > P2P_WILDCARD_SSID_LEN
7648 && os_strncmp((const char *) ssid->ssid,
7649 P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0) {
7650 wpas_notify_persistent_group_added(wpa_s, ssid);
7651 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007652 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007653
7654 wpa_s->next = global->ifaces;
7655 global->ifaces = wpa_s;
7656
7657 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007658 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007659
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007660#ifdef CONFIG_P2P
7661 if (wpa_s->global->p2p == NULL &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07007662 !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007663 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007664 wpas_p2p_add_p2pdev_interface(
7665 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007666 wpa_printf(MSG_INFO,
7667 "P2P: Failed to enable P2P Device interface");
7668 /* Try to continue without. P2P will be disabled. */
7669 }
7670#endif /* CONFIG_P2P */
7671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007672 return wpa_s;
7673}
7674
7675
7676/**
7677 * wpa_supplicant_remove_iface - Remove a network interface
7678 * @global: Pointer to global data from wpa_supplicant_init()
7679 * @wpa_s: Pointer to the network interface to be removed
7680 * Returns: 0 if interface was removed, -1 if interface was not found
7681 *
7682 * This function can be used to dynamically remove network interfaces from
7683 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
7684 * addition, this function is used to remove all remaining interfaces when
7685 * %wpa_supplicant is terminated.
7686 */
7687int wpa_supplicant_remove_iface(struct wpa_global *global,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007688 struct wpa_supplicant *wpa_s,
7689 int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007690{
7691 struct wpa_supplicant *prev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007692#ifdef CONFIG_MESH
7693 unsigned int mesh_if_created = wpa_s->mesh_if_created;
7694 char *ifname = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007695 struct wpa_supplicant *parent = wpa_s->parent;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007696#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697
7698 /* Remove interface from the global list of interfaces */
7699 prev = global->ifaces;
7700 if (prev == wpa_s) {
7701 global->ifaces = wpa_s->next;
7702 } else {
7703 while (prev && prev->next != wpa_s)
7704 prev = prev->next;
7705 if (prev == NULL)
7706 return -1;
7707 prev->next = wpa_s->next;
7708 }
7709
7710 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
7711
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007712#ifdef CONFIG_MESH
7713 if (mesh_if_created) {
7714 ifname = os_strdup(wpa_s->ifname);
7715 if (ifname == NULL) {
7716 wpa_dbg(wpa_s, MSG_ERROR,
7717 "mesh: Failed to malloc ifname");
7718 return -1;
7719 }
7720 }
7721#endif /* CONFIG_MESH */
7722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723 if (global->p2p_group_formation == wpa_s)
7724 global->p2p_group_formation = NULL;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007725 if (global->p2p_invite_group == wpa_s)
7726 global->p2p_invite_group = NULL;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07007727 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007728
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007729#ifdef CONFIG_MESH
7730 if (mesh_if_created) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08007731 wpa_drv_if_remove(parent, WPA_IF_MESH, ifname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007732 os_free(ifname);
7733 }
7734#endif /* CONFIG_MESH */
7735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736 return 0;
7737}
7738
7739
7740/**
7741 * wpa_supplicant_get_eap_mode - Get the current EAP mode
7742 * @wpa_s: Pointer to the network interface
7743 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
7744 */
7745const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
7746{
7747 const char *eapol_method;
7748
7749 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
7750 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
7751 return "NO-EAP";
7752 }
7753
7754 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
7755 if (eapol_method == NULL)
7756 return "UNKNOWN-EAP";
7757
7758 return eapol_method;
7759}
7760
7761
7762/**
7763 * wpa_supplicant_get_iface - Get a new network interface
7764 * @global: Pointer to global data from wpa_supplicant_init()
7765 * @ifname: Interface name
7766 * Returns: Pointer to the interface or %NULL if not found
7767 */
7768struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
7769 const char *ifname)
7770{
7771 struct wpa_supplicant *wpa_s;
7772
7773 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7774 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7775 return wpa_s;
7776 }
7777 return NULL;
7778}
7779
7780
7781#ifndef CONFIG_NO_WPA_MSG
7782static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
7783{
7784 struct wpa_supplicant *wpa_s = ctx;
7785 if (wpa_s == NULL)
7786 return NULL;
7787 return wpa_s->ifname;
7788}
7789#endif /* CONFIG_NO_WPA_MSG */
7790
7791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007792#ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
7793#define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
7794#endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
7795
7796/* Periodic cleanup tasks */
7797static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
7798{
7799 struct wpa_global *global = eloop_ctx;
7800 struct wpa_supplicant *wpa_s;
7801
7802 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
7803 wpas_periodic, global, NULL);
7804
7805#ifdef CONFIG_P2P
7806 if (global->p2p)
7807 p2p_expire_peers(global->p2p);
7808#endif /* CONFIG_P2P */
7809
7810 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7811 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
7812#ifdef CONFIG_AP
7813 ap_periodic(wpa_s);
7814#endif /* CONFIG_AP */
7815 }
7816}
7817
7818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007819/**
7820 * wpa_supplicant_init - Initialize %wpa_supplicant
7821 * @params: Parameters for %wpa_supplicant
7822 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
7823 *
7824 * This function is used to initialize %wpa_supplicant. After successful
7825 * initialization, the returned data pointer can be used to add and remove
7826 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
7827 */
7828struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
7829{
7830 struct wpa_global *global;
7831 int ret, i;
7832
7833 if (params == NULL)
7834 return NULL;
7835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007836#ifdef CONFIG_DRIVER_NDIS
7837 {
7838 void driver_ndis_init_ops(void);
7839 driver_ndis_init_ops();
7840 }
7841#endif /* CONFIG_DRIVER_NDIS */
7842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007843#ifndef CONFIG_NO_WPA_MSG
7844 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
7845#endif /* CONFIG_NO_WPA_MSG */
7846
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007847 if (params->wpa_debug_file_path)
7848 wpa_debug_open_file(params->wpa_debug_file_path);
Hai Shalomfdcde762020-04-02 11:19:20 -07007849 if (!params->wpa_debug_file_path && !params->wpa_debug_syslog)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007850 wpa_debug_setup_stdout();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007851 if (params->wpa_debug_syslog)
7852 wpa_debug_open_syslog();
Dmitry Shmidt04949592012-07-19 12:16:46 -07007853 if (params->wpa_debug_tracing) {
7854 ret = wpa_debug_open_linux_tracing();
7855 if (ret) {
7856 wpa_printf(MSG_ERROR,
7857 "Failed to enable trace logging");
7858 return NULL;
7859 }
7860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007861
7862 ret = eap_register_methods();
7863 if (ret) {
7864 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
7865 if (ret == -2)
7866 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
7867 "the same EAP type.");
7868 return NULL;
7869 }
7870
7871 global = os_zalloc(sizeof(*global));
7872 if (global == NULL)
7873 return NULL;
7874 dl_list_init(&global->p2p_srv_bonjour);
7875 dl_list_init(&global->p2p_srv_upnp);
7876 global->params.daemonize = params->daemonize;
7877 global->params.wait_for_monitor = params->wait_for_monitor;
7878 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
Sunil Ravi77d572f2023-01-17 23:58:31 +00007879
7880 if (params->pid_file) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007881 global->params.pid_file = os_strdup(params->pid_file);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007882 if (!global->params.pid_file) {
7883 wpa_supplicant_deinit(global);
7884 return NULL;
7885 }
7886 }
7887
7888 if (params->ctrl_interface) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889 global->params.ctrl_interface =
7890 os_strdup(params->ctrl_interface);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007891 if (!global->params.ctrl_interface) {
7892 wpa_supplicant_deinit(global);
7893 return NULL;
7894 }
7895 }
7896
7897 if (params->ctrl_interface_group) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007898 global->params.ctrl_interface_group =
7899 os_strdup(params->ctrl_interface_group);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007900 if (!global->params.ctrl_interface_group) {
7901 wpa_supplicant_deinit(global);
7902 return NULL;
7903 }
7904 }
7905
7906 if (params->override_driver) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007907 global->params.override_driver =
7908 os_strdup(params->override_driver);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007909 if (!global->params.override_driver) {
7910 wpa_supplicant_deinit(global);
7911 return NULL;
7912 }
7913 }
7914
7915 if (params->override_ctrl_interface) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916 global->params.override_ctrl_interface =
7917 os_strdup(params->override_ctrl_interface);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007918 if (!global->params.override_ctrl_interface) {
7919 wpa_supplicant_deinit(global);
7920 return NULL;
7921 }
7922 }
7923
Dmitry Shmidte4663042016-04-04 10:07:49 -07007924#ifdef CONFIG_MATCH_IFACE
7925 global->params.match_iface_count = params->match_iface_count;
7926 if (params->match_iface_count) {
7927 global->params.match_ifaces =
7928 os_calloc(params->match_iface_count,
7929 sizeof(struct wpa_interface));
Sunil Ravi77d572f2023-01-17 23:58:31 +00007930 if (!global->params.match_ifaces) {
7931 wpa_printf(MSG_ERROR,
7932 "Failed to allocate match interfaces");
7933 wpa_supplicant_deinit(global);
7934 return NULL;
7935 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07007936 os_memcpy(global->params.match_ifaces,
7937 params->match_ifaces,
7938 params->match_iface_count *
7939 sizeof(struct wpa_interface));
7940 }
7941#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007942#ifdef CONFIG_P2P
Sunil Ravi77d572f2023-01-17 23:58:31 +00007943 if (params->conf_p2p_dev) {
Sasha Levitskiydaa60e52015-08-05 13:02:59 -07007944 global->params.conf_p2p_dev =
7945 os_strdup(params->conf_p2p_dev);
Sunil Ravi77d572f2023-01-17 23:58:31 +00007946 if (!global->params.conf_p2p_dev) {
7947 wpa_printf(MSG_ERROR, "Failed to allocate conf p2p");
7948 wpa_supplicant_deinit(global);
7949 return NULL;
7950 }
7951 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007952#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007953 wpa_debug_level = global->params.wpa_debug_level =
7954 params->wpa_debug_level;
7955 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
7956 params->wpa_debug_show_keys;
7957 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
7958 params->wpa_debug_timestamp;
7959
Hai Shalomfdcde762020-04-02 11:19:20 -07007960 wpa_printf(MSG_DEBUG, "wpa_supplicant v%s", VERSION_STR);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007961
7962 if (eloop_init()) {
7963 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
7964 wpa_supplicant_deinit(global);
7965 return NULL;
7966 }
7967
Jouni Malinen75ecf522011-06-27 15:19:46 -07007968 random_init(params->entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007969
7970 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
7971 if (global->ctrl_iface == NULL) {
7972 wpa_supplicant_deinit(global);
7973 return NULL;
7974 }
7975
7976 if (wpas_notify_supplicant_initialized(global)) {
7977 wpa_supplicant_deinit(global);
7978 return NULL;
7979 }
7980
7981 for (i = 0; wpa_drivers[i]; i++)
7982 global->drv_count++;
7983 if (global->drv_count == 0) {
7984 wpa_printf(MSG_ERROR, "No drivers enabled");
7985 wpa_supplicant_deinit(global);
7986 return NULL;
7987 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007988 global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007989 if (global->drv_priv == NULL) {
7990 wpa_supplicant_deinit(global);
7991 return NULL;
7992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007993
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007994#ifdef CONFIG_WIFI_DISPLAY
7995 if (wifi_display_init(global) < 0) {
7996 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
7997 wpa_supplicant_deinit(global);
7998 return NULL;
7999 }
8000#endif /* CONFIG_WIFI_DISPLAY */
8001
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008002 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
8003 wpas_periodic, global, NULL);
8004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008005 return global;
8006}
8007
8008
8009/**
8010 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
8011 * @global: Pointer to global data from wpa_supplicant_init()
8012 * Returns: 0 after successful event loop run, -1 on failure
8013 *
8014 * This function starts the main event loop and continues running as long as
8015 * there are any remaining events. In most cases, this function is running as
8016 * long as the %wpa_supplicant process in still in use.
8017 */
8018int wpa_supplicant_run(struct wpa_global *global)
8019{
8020 struct wpa_supplicant *wpa_s;
8021
8022 if (global->params.daemonize &&
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08008023 (wpa_supplicant_daemon(global->params.pid_file) ||
8024 eloop_sock_requeue()))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008025 return -1;
8026
Dmitry Shmidte4663042016-04-04 10:07:49 -07008027#ifdef CONFIG_MATCH_IFACE
8028 if (wpa_supplicant_match_existing(global))
8029 return -1;
8030#endif
8031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008032 if (global->params.wait_for_monitor) {
8033 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08008034 if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008035 wpa_supplicant_ctrl_iface_wait(
8036 wpa_s->ctrl_iface);
8037 }
8038
Kiran Kumar Lokerea9f98eb2023-03-17 13:01:12 -07008039#ifdef CONFIG_AIDL
Gabriel Biren7a30e7f2023-06-02 20:11:28 +00008040 // If daemonize is enabled, initialize AIDL here.
8041 if (global->params.daemonize) {
8042 global->aidl = wpas_aidl_init(global);
8043 if (!global->aidl)
8044 return -1;
8045 }
Kiran Kumar Lokerea9f98eb2023-03-17 13:01:12 -07008046#endif /* CONFIG_AIDL */
8047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008048 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
8049 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
8050
8051 eloop_run();
8052
8053 return 0;
8054}
8055
8056
8057/**
8058 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
8059 * @global: Pointer to global data from wpa_supplicant_init()
8060 *
8061 * This function is called to deinitialize %wpa_supplicant and to free all
8062 * allocated resources. Remaining network interfaces will also be removed.
8063 */
8064void wpa_supplicant_deinit(struct wpa_global *global)
8065{
8066 int i;
8067
8068 if (global == NULL)
8069 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008070
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008071 eloop_cancel_timeout(wpas_periodic, global, NULL);
8072
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008073#ifdef CONFIG_WIFI_DISPLAY
8074 wifi_display_deinit(global);
8075#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008076
8077 while (global->ifaces)
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07008078 wpa_supplicant_remove_iface(global, global->ifaces, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008079
8080 if (global->ctrl_iface)
8081 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
8082
8083 wpas_notify_supplicant_deinitialized(global);
8084
8085 eap_peer_unregister_methods();
8086#ifdef CONFIG_AP
8087 eap_server_unregister_methods();
8088#endif /* CONFIG_AP */
8089
8090 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
8091 if (!global->drv_priv[i])
8092 continue;
8093 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
8094 }
8095 os_free(global->drv_priv);
8096
8097 random_deinit();
8098
8099 eloop_destroy();
8100
8101 if (global->params.pid_file) {
8102 os_daemonize_terminate(global->params.pid_file);
8103 os_free(global->params.pid_file);
8104 }
8105 os_free(global->params.ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008106 os_free(global->params.ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008107 os_free(global->params.override_driver);
8108 os_free(global->params.override_ctrl_interface);
Dmitry Shmidte4663042016-04-04 10:07:49 -07008109#ifdef CONFIG_MATCH_IFACE
8110 os_free(global->params.match_ifaces);
8111#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008112#ifdef CONFIG_P2P
Sasha Levitskiydaa60e52015-08-05 13:02:59 -07008113 os_free(global->params.conf_p2p_dev);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008114#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008115
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07008116 os_free(global->p2p_disallow_freq.range);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008117 os_free(global->p2p_go_avoid_freq.range);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008118 os_free(global->add_psk);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008120 os_free(global);
8121 wpa_debug_close_syslog();
8122 wpa_debug_close_file();
Dmitry Shmidt04949592012-07-19 12:16:46 -07008123 wpa_debug_close_linux_tracing();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008124}
8125
8126
8127void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
8128{
8129 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8130 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8131 char country[3];
8132 country[0] = wpa_s->conf->country[0];
8133 country[1] = wpa_s->conf->country[1];
8134 country[2] = '\0';
8135 if (wpa_drv_set_country(wpa_s, country) < 0) {
8136 wpa_printf(MSG_ERROR, "Failed to set country code "
8137 "'%s'", country);
8138 }
8139 }
8140
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008141 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
8142 wpas_init_ext_pw(wpa_s);
8143
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008144 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
8145 wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
8146
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008147 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WOWLAN_TRIGGERS) {
8148 struct wpa_driver_capa capa;
8149 int res = wpa_drv_get_capa(wpa_s, &capa);
8150
8151 if (res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
8152 wpa_printf(MSG_ERROR,
8153 "Failed to update wowlan_triggers to '%s'",
8154 wpa_s->conf->wowlan_triggers);
8155 }
8156
Hai Shalom81f62d82019-07-22 12:10:00 -07008157 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DISABLE_BTM)
8158 wpa_supplicant_set_default_scan_ies(wpa_s);
8159
Hai Shalom899fcc72020-10-19 14:38:18 -07008160#ifdef CONFIG_BGSCAN
8161 /*
8162 * We default to global bgscan parameters only when per-network bgscan
8163 * parameters aren't set. Only bother resetting bgscan parameters if
8164 * this is the case.
8165 */
8166 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_BGSCAN) &&
8167 wpa_s->current_ssid && !wpa_s->current_ssid->bgscan &&
8168 wpa_s->wpa_state == WPA_COMPLETED)
8169 wpa_supplicant_reset_bgscan(wpa_s);
8170#endif /* CONFIG_BGSCAN */
8171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008172#ifdef CONFIG_WPS
8173 wpas_wps_update_config(wpa_s);
8174#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008175 wpas_p2p_update_config(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008176 wpa_s->conf->changed_parameters = 0;
8177}
8178
8179
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008180void add_freq(int *freqs, int *num_freqs, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008181{
8182 int i;
8183
8184 for (i = 0; i < *num_freqs; i++) {
8185 if (freqs[i] == freq)
8186 return;
8187 }
8188
8189 freqs[*num_freqs] = freq;
8190 (*num_freqs)++;
8191}
8192
8193
8194static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
8195{
8196 struct wpa_bss *bss, *cbss;
8197 const int max_freqs = 10;
8198 int *freqs;
8199 int num_freqs = 0;
8200
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008201 freqs = os_calloc(max_freqs + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008202 if (freqs == NULL)
8203 return NULL;
8204
8205 cbss = wpa_s->current_bss;
8206
8207 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
8208 if (bss == cbss)
8209 continue;
8210 if (bss->ssid_len == cbss->ssid_len &&
8211 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
Hai Shalom60840252021-02-19 19:02:11 -08008212 !wpa_bssid_ignore_is_listed(wpa_s, bss->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008213 add_freq(freqs, &num_freqs, bss->freq);
8214 if (num_freqs == max_freqs)
8215 break;
8216 }
8217 }
8218
8219 if (num_freqs == 0) {
8220 os_free(freqs);
8221 freqs = NULL;
8222 }
8223
8224 return freqs;
8225}
8226
8227
8228void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
8229{
8230 int timeout;
8231 int count;
8232 int *freqs = NULL;
8233
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008234 wpas_connect_work_done(wpa_s);
8235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008236 /*
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008237 * Remove possible authentication timeout since the connection failed.
8238 */
8239 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
8240
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08008241 /*
Hai Shalom60840252021-02-19 19:02:11 -08008242 * There is no point in ignoring the AP temporarily if this event is
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08008243 * generated based on local request to disconnect.
8244 */
Hai Shalomfdcde762020-04-02 11:19:20 -07008245 if (wpa_s->own_disconnect_req || wpa_s->own_reconnect_req) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08008246 wpa_s->own_disconnect_req = 0;
8247 wpa_dbg(wpa_s, MSG_DEBUG,
8248 "Ignore connection failure due to local request to disconnect");
8249 return;
8250 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008251 if (wpa_s->disconnected) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008252 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
8253 "indication since interface has been put into "
8254 "disconnected state");
8255 return;
8256 }
Roshan Piusb1ae0fe2019-02-15 08:05:38 -08008257 if (wpa_s->auto_reconnect_disabled) {
8258 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
8259 "indication since auto connect is disabled");
8260 return;
8261 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008262
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008263 /*
Hai Shalom60840252021-02-19 19:02:11 -08008264 * Add the failed BSSID into the ignore list and speed up next scan
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008265 * attempt if there could be other APs that could accept association.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008266 */
Hai Shalom60840252021-02-19 19:02:11 -08008267 count = wpa_bssid_ignore_add(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008268 if (count == 1 && wpa_s->current_bss) {
8269 /*
Hai Shalom60840252021-02-19 19:02:11 -08008270 * This BSS was not in the ignore list before. If there is
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008271 * another BSS available for the same ESS, we should try that
8272 * next. Otherwise, we may as well try this one once more
8273 * before allowing other, likely worse, ESSes to be considered.
8274 */
8275 freqs = get_bss_freqs_in_ess(wpa_s);
8276 if (freqs) {
8277 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
8278 "has been seen; try it next");
Hai Shalom60840252021-02-19 19:02:11 -08008279 wpa_bssid_ignore_add(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008280 /*
8281 * On the next scan, go through only the known channels
8282 * used in this ESS based on previous scans to speed up
8283 * common load balancing use case.
8284 */
8285 os_free(wpa_s->next_scan_freqs);
8286 wpa_s->next_scan_freqs = freqs;
8287 }
8288 }
8289
Hai Shalom899fcc72020-10-19 14:38:18 -07008290 wpa_s->consecutive_conn_failures++;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008291
Hai Shalom899fcc72020-10-19 14:38:18 -07008292 if (wpa_s->consecutive_conn_failures > 3 && wpa_s->current_ssid) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07008293 wpa_printf(MSG_DEBUG, "Continuous association failures - "
8294 "consider temporary network disabling");
Sunil Ravi77d572f2023-01-17 23:58:31 +00008295 wpas_auth_failed(wpa_s, "CONN_FAILED", bssid);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07008296 }
Hai Shalom899fcc72020-10-19 14:38:18 -07008297 /*
8298 * Multiple consecutive connection failures mean that other APs are
8299 * either not available or have already been tried, so we can start
8300 * increasing the delay here to avoid constant scanning.
8301 */
8302 switch (wpa_s->consecutive_conn_failures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008303 case 1:
8304 timeout = 100;
8305 break;
8306 case 2:
8307 timeout = 500;
8308 break;
8309 case 3:
8310 timeout = 1000;
8311 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008312 case 4:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008313 timeout = 5000;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008314 break;
8315 default:
8316 timeout = 10000;
8317 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008318 }
8319
Hai Shalom899fcc72020-10-19 14:38:18 -07008320 wpa_dbg(wpa_s, MSG_DEBUG,
8321 "Consecutive connection failures: %d --> request scan in %d ms",
8322 wpa_s->consecutive_conn_failures, timeout);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324 /*
8325 * TODO: if more than one possible AP is available in scan results,
8326 * could try the other ones before requesting a new scan.
8327 */
Hai Shalom021b0b52019-04-10 11:17:58 -07008328
8329 /* speed up the connection attempt with normal scan */
8330 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008331 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
8332 1000 * (timeout % 1000));
8333}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008334
8335
Hai Shalomce48b4a2018-09-05 11:41:35 -07008336#ifdef CONFIG_FILS
Hai Shalomc1a21442022-02-04 13:43:00 -08008337
8338void fils_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
8339{
8340 struct wpa_ssid *ssid = wpa_s->current_ssid;
8341 const u8 *realm, *username, *rrk;
8342 size_t realm_len, username_len, rrk_len;
8343 u16 next_seq_num;
8344
8345 /* Clear the PMKSA cache entry if FILS authentication was rejected.
8346 * Check for ERP keys existing to limit when this can be done since
8347 * the rejection response is not protected and such triggers should
8348 * really not allow internal state to be modified unless required to
8349 * avoid significant issues in functionality. In addition, drop
8350 * externally configure PMKSA entries even without ERP keys since it
8351 * is possible for an external component to add PMKSA entries for FILS
8352 * authentication without restoring previously generated ERP keys.
8353 *
8354 * In this case, this is needed to allow recovery from cases where the
8355 * AP or authentication server has dropped PMKSAs and ERP keys. */
8356 if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt))
8357 return;
8358
8359 if (eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
8360 &username, &username_len,
8361 &realm, &realm_len, &next_seq_num,
8362 &rrk, &rrk_len) != 0 ||
8363 !realm) {
8364 wpa_dbg(wpa_s, MSG_DEBUG,
8365 "FILS: Drop external PMKSA cache entry");
8366 wpa_sm_aborted_external_cached(wpa_s->wpa);
8367 wpa_sm_external_pmksa_cache_flush(wpa_s->wpa, ssid);
8368 return;
8369 }
8370
8371 wpa_dbg(wpa_s, MSG_DEBUG, "FILS: Drop PMKSA cache entry");
8372 wpa_sm_aborted_cached(wpa_s->wpa);
8373 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
8374}
8375
8376
Hai Shalomce48b4a2018-09-05 11:41:35 -07008377void fils_connection_failure(struct wpa_supplicant *wpa_s)
8378{
8379 struct wpa_ssid *ssid = wpa_s->current_ssid;
8380 const u8 *realm, *username, *rrk;
8381 size_t realm_len, username_len, rrk_len;
8382 u16 next_seq_num;
8383
8384 if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt) ||
8385 eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
8386 &username, &username_len,
8387 &realm, &realm_len, &next_seq_num,
8388 &rrk, &rrk_len) != 0 ||
8389 !realm)
8390 return;
8391
8392 wpa_hexdump_ascii(MSG_DEBUG,
8393 "FILS: Store last connection failure realm",
8394 realm, realm_len);
8395 os_free(wpa_s->last_con_fail_realm);
8396 wpa_s->last_con_fail_realm = os_malloc(realm_len);
8397 if (wpa_s->last_con_fail_realm) {
8398 wpa_s->last_con_fail_realm_len = realm_len;
8399 os_memcpy(wpa_s->last_con_fail_realm, realm, realm_len);
8400 }
8401}
8402#endif /* CONFIG_FILS */
8403
8404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008405int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
8406{
8407 return wpa_s->conf->ap_scan == 2 ||
8408 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
8409}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008410
Dmitry Shmidt04949592012-07-19 12:16:46 -07008411
Gabriel Biren57ededa2021-09-03 16:08:50 +00008412#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW) || defined (CONFIG_CTRL_IFACE_AIDL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008413int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
8414 struct wpa_ssid *ssid,
8415 const char *field,
8416 const char *value)
8417{
8418#ifdef IEEE8021X_EAPOL
Roshan Pius71a6b8a2016-08-17 13:04:08 -07008419 enum wpa_ctrl_req_type rtype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008420
8421 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
8422 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
8423 (const u8 *) value, os_strlen(value));
8424
Roshan Pius71a6b8a2016-08-17 13:04:08 -07008425 rtype = wpa_supplicant_ctrl_req_from_string(field);
pkanwareb9203e2017-10-26 16:00:35 -07008426 return wpa_supplicant_ctrl_rsp_handle(wpa_s, ssid, rtype, value, strlen(value));
Roshan Pius71a6b8a2016-08-17 13:04:08 -07008427#else /* IEEE8021X_EAPOL */
8428 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
8429 return -1;
8430#endif /* IEEE8021X_EAPOL */
8431}
8432
8433int wpa_supplicant_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
8434 struct wpa_ssid *ssid,
8435 enum wpa_ctrl_req_type rtype,
pkanwareb9203e2017-10-26 16:00:35 -07008436 const char *value, int value_len)
Roshan Pius71a6b8a2016-08-17 13:04:08 -07008437{
8438#ifdef IEEE8021X_EAPOL
8439 struct eap_peer_config *eap = &ssid->eap;
Ecco Park00a7b212018-01-26 13:44:44 -08008440 char *identity, *imsi_identity;
Roshan Pius71a6b8a2016-08-17 13:04:08 -07008441
8442 switch (rtype) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008443 case WPA_CTRL_REQ_EAP_IDENTITY:
8444 os_free(eap->identity);
Ecco Park00a7b212018-01-26 13:44:44 -08008445 os_free(eap->imsi_identity);
8446 if (value == NULL)
8447 return -1;
8448 identity = os_strchr(value, ':');
8449 if (identity == NULL) {
8450 /* plain identity */
8451 eap->identity = (u8 *)os_strdup(value);
8452 eap->identity_len = os_strlen(value);
8453 } else {
8454 /* have both plain identity and encrypted identity */
8455 imsi_identity = value;
8456 *identity++ = '\0';
8457 /* plain identity */
8458 eap->imsi_identity = (u8 *)dup_binstr(imsi_identity, strlen(imsi_identity));
8459 eap->imsi_identity_len = strlen(imsi_identity);
8460 /* encrypted identity */
8461 eap->identity = (u8 *)dup_binstr(identity,
8462 value_len - strlen(imsi_identity) - 1);
8463 eap->identity_len = value_len - strlen(imsi_identity) - 1;
8464 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008465 eap->pending_req_identity = 0;
8466 if (ssid == wpa_s->current_ssid)
8467 wpa_s->reassociate = 1;
8468 break;
8469 case WPA_CTRL_REQ_EAP_PASSWORD:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07008470 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008471 eap->password = (u8 *) os_strdup(value);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008472 if (!eap->password)
8473 return -1;
pkanwareb9203e2017-10-26 16:00:35 -07008474 eap->password_len = value_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008475 eap->pending_req_password = 0;
8476 if (ssid == wpa_s->current_ssid)
8477 wpa_s->reassociate = 1;
8478 break;
8479 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07008480 bin_clear_free(eap->new_password, eap->new_password_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008481 eap->new_password = (u8 *) os_strdup(value);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008482 if (!eap->new_password)
8483 return -1;
pkanwareb9203e2017-10-26 16:00:35 -07008484 eap->new_password_len = value_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008485 eap->pending_req_new_password = 0;
8486 if (ssid == wpa_s->current_ssid)
8487 wpa_s->reassociate = 1;
8488 break;
8489 case WPA_CTRL_REQ_EAP_PIN:
Hai Shalomc3565922019-10-28 11:58:20 -07008490 str_clear_free(eap->cert.pin);
8491 eap->cert.pin = os_strdup(value);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008492 if (!eap->cert.pin)
8493 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008494 eap->pending_req_pin = 0;
8495 if (ssid == wpa_s->current_ssid)
8496 wpa_s->reassociate = 1;
8497 break;
8498 case WPA_CTRL_REQ_EAP_OTP:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07008499 bin_clear_free(eap->otp, eap->otp_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008500 eap->otp = (u8 *) os_strdup(value);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008501 if (!eap->otp)
8502 return -1;
pkanwareb9203e2017-10-26 16:00:35 -07008503 eap->otp_len = value_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008504 os_free(eap->pending_req_otp);
8505 eap->pending_req_otp = NULL;
8506 eap->pending_req_otp_len = 0;
8507 break;
8508 case WPA_CTRL_REQ_EAP_PASSPHRASE:
Hai Shalomc3565922019-10-28 11:58:20 -07008509 str_clear_free(eap->cert.private_key_passwd);
8510 eap->cert.private_key_passwd = os_strdup(value);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008511 if (!eap->cert.private_key_passwd)
8512 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008513 eap->pending_req_passphrase = 0;
8514 if (ssid == wpa_s->current_ssid)
8515 wpa_s->reassociate = 1;
8516 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008517 case WPA_CTRL_REQ_SIM:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07008518 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt051af732013-10-22 13:52:46 -07008519 eap->external_sim_resp = os_strdup(value);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008520 if (!eap->external_sim_resp)
8521 return -1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08008522 eap->pending_req_sim = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008523 break;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008524 case WPA_CTRL_REQ_PSK_PASSPHRASE:
8525 if (wpa_config_set(ssid, "psk", value, 0) < 0)
8526 return -1;
8527 ssid->mem_only_psk = 1;
8528 if (ssid->passphrase)
8529 wpa_config_update_psk(ssid);
8530 if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
8531 wpa_supplicant_req_scan(wpa_s, 0, 0);
8532 break;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008533 case WPA_CTRL_REQ_EXT_CERT_CHECK:
8534 if (eap->pending_ext_cert_check != PENDING_CHECK)
8535 return -1;
8536 if (os_strcmp(value, "good") == 0)
8537 eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
8538 else if (os_strcmp(value, "bad") == 0)
8539 eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
8540 else
8541 return -1;
8542 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008543 default:
Roshan Pius71a6b8a2016-08-17 13:04:08 -07008544 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown type %d", rtype);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008545 return -1;
8546 }
8547
8548 return 0;
8549#else /* IEEE8021X_EAPOL */
8550 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
8551 return -1;
8552#endif /* IEEE8021X_EAPOL */
8553}
Gabriel Biren57ededa2021-09-03 16:08:50 +00008554#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW || CONFIG_CTRL_IFACE_AIDL */
Dmitry Shmidt04949592012-07-19 12:16:46 -07008555
8556
8557int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
8558{
Hai Shalomfdcde762020-04-02 11:19:20 -07008559#ifdef CONFIG_WEP
Dmitry Shmidt04949592012-07-19 12:16:46 -07008560 int i;
8561 unsigned int drv_enc;
Hai Shalomfdcde762020-04-02 11:19:20 -07008562#endif /* CONFIG_WEP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07008563
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08008564 if (wpa_s->p2p_mgmt)
8565 return 1; /* no normal network profiles on p2p_mgmt interface */
8566
Dmitry Shmidt04949592012-07-19 12:16:46 -07008567 if (ssid == NULL)
8568 return 1;
8569
8570 if (ssid->disabled)
8571 return 1;
8572
Hai Shalomfdcde762020-04-02 11:19:20 -07008573#ifdef CONFIG_WEP
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08008574 if (wpa_s->drv_capa_known)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008575 drv_enc = wpa_s->drv_enc;
8576 else
8577 drv_enc = (unsigned int) -1;
8578
8579 for (i = 0; i < NUM_WEP_KEYS; i++) {
8580 size_t len = ssid->wep_key_len[i];
8581 if (len == 0)
8582 continue;
8583 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
8584 continue;
8585 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
8586 continue;
8587 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
8588 continue;
8589 return 1; /* invalid WEP key */
8590 }
Hai Shalomfdcde762020-04-02 11:19:20 -07008591#endif /* CONFIG_WEP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07008592
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008593 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008594 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008595 !(wpa_key_mgmt_sae(ssid->key_mgmt) && ssid->sae_password) &&
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008596 !ssid->mem_only_psk)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008597 return 1;
8598
Sunil Ravi89eba102022-09-13 21:04:37 -07008599#ifdef IEEE8021X_EAPOL
Sunil8cd6f4d2022-06-28 18:40:46 +00008600#ifdef CRYPTO_RSA_OAEP_SHA256
8601 if (ssid->eap.imsi_privacy_cert) {
8602 struct crypto_rsa_key *key;
8603 bool failed = false;
8604
8605 key = crypto_rsa_key_read(ssid->eap.imsi_privacy_cert, false);
8606 if (!key)
8607 failed = true;
8608 crypto_rsa_key_free(key);
8609 if (failed) {
8610 wpa_printf(MSG_DEBUG,
8611 "Invalid imsi_privacy_cert (%s) - disable network",
8612 ssid->eap.imsi_privacy_cert);
8613 return 1;
8614 }
8615 }
8616#endif /* CRYPTO_RSA_OAEP_SHA256 */
Sunil Ravi89eba102022-09-13 21:04:37 -07008617#endif /* IEEE8021X_EAPOL */
Sunil8cd6f4d2022-06-28 18:40:46 +00008618
Dmitry Shmidt04949592012-07-19 12:16:46 -07008619 return 0;
8620}
8621
8622
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008623int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
8624{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008625 if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
8626 if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
8627 !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
8628 /*
8629 * Driver does not support BIP -- ignore pmf=1 default
8630 * since the connection with PMF would fail and the
8631 * configuration does not require PMF to be enabled.
8632 */
8633 return NO_MGMT_FRAME_PROTECTION;
8634 }
8635
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008636 if (ssid &&
8637 (ssid->key_mgmt &
8638 ~(WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPS |
8639 WPA_KEY_MGMT_IEEE8021X_NO_WPA)) == 0) {
8640 /*
8641 * Do not use the default PMF value for non-RSN networks
8642 * since PMF is available only with RSN and pmf=2
8643 * configuration would otherwise prevent connections to
8644 * all open networks.
8645 */
8646 return NO_MGMT_FRAME_PROTECTION;
8647 }
8648
Sunil Ravi77d572f2023-01-17 23:58:31 +00008649#ifdef CONFIG_OCV
8650 /* Enable PMF if OCV is being enabled */
8651 if (wpa_s->conf->pmf == NO_MGMT_FRAME_PROTECTION &&
8652 ssid && ssid->ocv)
8653 return MGMT_FRAME_PROTECTION_OPTIONAL;
8654#endif /* CONFIG_OCV */
8655
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008656 return wpa_s->conf->pmf;
8657 }
8658
8659 return ssid->ieee80211w;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08008660}
8661
8662
Sunil Ravi77d572f2023-01-17 23:58:31 +00008663#ifdef CONFIG_SAE
8664bool wpas_is_sae_avoided(struct wpa_supplicant *wpa_s,
8665 struct wpa_ssid *ssid,
8666 const struct wpa_ie_data *ie)
8667{
8668 return wpa_s->conf->sae_check_mfp &&
8669 (!(ie->capabilities &
8670 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) ||
8671 wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION);
8672}
8673#endif /* CONFIG_SAE */
8674
8675
Hai Shalomc1a21442022-02-04 13:43:00 -08008676int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
8677{
8678 if (wpa_s->current_ssid == NULL ||
8679 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
8680 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
8681 return 0;
8682 return wpa_sm_pmf_enabled(wpa_s->wpa);
8683}
8684
8685
Dmitry Shmidt687922c2012-03-26 14:02:32 -07008686int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008687{
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07008688 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008689 return 1;
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07008690 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
Dmitry Shmidt687922c2012-03-26 14:02:32 -07008691 return 0;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07008692 return -1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008693}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008694
8695
Sunil Ravi77d572f2023-01-17 23:58:31 +00008696void wpas_auth_failed(struct wpa_supplicant *wpa_s, const char *reason,
8697 const u8 *bssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008698{
8699 struct wpa_ssid *ssid = wpa_s->current_ssid;
8700 int dur;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008701 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008702
8703 if (ssid == NULL) {
8704 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
8705 "SSID block");
8706 return;
8707 }
8708
8709 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
8710 return;
8711
8712 ssid->auth_failures++;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07008713
8714#ifdef CONFIG_P2P
8715 if (ssid->p2p_group &&
8716 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
8717 /*
8718 * Skip the wait time since there is a short timeout on the
8719 * connection to a P2P group.
8720 */
8721 return;
8722 }
8723#endif /* CONFIG_P2P */
8724
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008725 if (ssid->auth_failures > 50)
8726 dur = 300;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008727 else if (ssid->auth_failures > 10)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008728 dur = 120;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008729 else if (ssid->auth_failures > 5)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008730 dur = 90;
8731 else if (ssid->auth_failures > 3)
8732 dur = 60;
8733 else if (ssid->auth_failures > 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008734 dur = 30;
8735 else if (ssid->auth_failures > 1)
8736 dur = 20;
8737 else
8738 dur = 10;
8739
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008740 if (ssid->auth_failures > 1 &&
8741 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
8742 dur += os_random() % (ssid->auth_failures * 10);
8743
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008744 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008745 if (now.sec + dur <= ssid->disabled_until.sec)
8746 return;
8747
8748 ssid->disabled_until.sec = now.sec + dur;
8749
8750 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07008751 "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008752 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07008753 ssid->auth_failures, dur, reason);
Gabriel Biren3a2ec2c2022-03-07 17:59:41 +00008754
8755 char *format_str = "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s";
8756 int msg_len = snprintf(NULL, 0, format_str,
8757 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
8758 ssid->auth_failures, dur, reason) + 1;
8759 char *msg = os_malloc(msg_len);
Narasimha Rao PVSc28a5132022-12-02 18:36:38 +05308760 if (!msg)
8761 return;
Gabriel Biren3a2ec2c2022-03-07 17:59:41 +00008762 snprintf(msg, msg_len, format_str,
8763 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
8764 ssid->auth_failures, dur, reason);
8765 wpas_notify_ssid_temp_disabled(wpa_s, msg);
8766 os_free(msg);
Sunil Ravi77d572f2023-01-17 23:58:31 +00008767
8768 if (bssid)
8769 os_memcpy(ssid->disabled_due_to, bssid, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008770}
8771
8772
8773void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
8774 struct wpa_ssid *ssid, int clear_failures)
8775{
8776 if (ssid == NULL)
8777 return;
8778
8779 if (ssid->disabled_until.sec) {
8780 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
8781 "id=%d ssid=\"%s\"",
8782 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
8783 }
8784 ssid->disabled_until.sec = 0;
8785 ssid->disabled_until.usec = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008786 if (clear_failures) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008787 ssid->auth_failures = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008788 } else if (!is_zero_ether_addr(ssid->disabled_due_to)) {
8789 wpa_printf(MSG_DEBUG, "Mark BSSID " MACSTR
8790 " ignored to allow a lower priority BSS, if any, to be tried next",
8791 MAC2STR(ssid->disabled_due_to));
8792 wpa_bssid_ignore_add(wpa_s, ssid->disabled_due_to);
8793 os_memset(ssid->disabled_due_to, 0, ETH_ALEN);
8794 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008795}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008796
8797
8798int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
8799{
8800 size_t i;
8801
8802 if (wpa_s->disallow_aps_bssid == NULL)
8803 return 0;
8804
8805 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
8806 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
8807 bssid, ETH_ALEN) == 0)
8808 return 1;
8809 }
8810
8811 return 0;
8812}
8813
8814
8815int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
8816 size_t ssid_len)
8817{
8818 size_t i;
8819
8820 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
8821 return 0;
8822
8823 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
8824 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
8825 if (ssid_len == s->ssid_len &&
8826 os_memcmp(ssid, s->ssid, ssid_len) == 0)
8827 return 1;
8828 }
8829
8830 return 0;
8831}
8832
8833
8834/**
8835 * wpas_request_connection - Request a new connection
8836 * @wpa_s: Pointer to the network interface
8837 *
8838 * This function is used to request a new connection to be found. It will mark
8839 * the interface to allow reassociation and request a new scan to find a
8840 * suitable network to connect to.
8841 */
8842void wpas_request_connection(struct wpa_supplicant *wpa_s)
8843{
8844 wpa_s->normal_scans = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008845 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008846 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008847 wpa_s->disconnected = 0;
8848 wpa_s->reassociate = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07008849 wpa_s->last_owe_group = 0;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08008850
8851 if (wpa_supplicant_fast_associate(wpa_s) != 1)
8852 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008853 else
8854 wpa_s->reattach = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008855}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008856
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008857
Roshan Pius02242d72016-08-09 15:31:48 -07008858/**
8859 * wpas_request_disconnection - Request disconnection
8860 * @wpa_s: Pointer to the network interface
8861 *
8862 * This function is used to request disconnection from the currently connected
8863 * network. This will stop any ongoing scans and initiate deauthentication.
8864 */
8865void wpas_request_disconnection(struct wpa_supplicant *wpa_s)
8866{
8867#ifdef CONFIG_SME
8868 wpa_s->sme.prev_bssid_set = 0;
8869#endif /* CONFIG_SME */
8870 wpa_s->reassociate = 0;
8871 wpa_s->disconnected = 1;
8872 wpa_supplicant_cancel_sched_scan(wpa_s);
8873 wpa_supplicant_cancel_scan(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00008874 wpas_abort_ongoing_scan(wpa_s);
Roshan Pius02242d72016-08-09 15:31:48 -07008875 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
8876 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
Hai Shalom021b0b52019-04-10 11:17:58 -07008877 radio_remove_works(wpa_s, "connect", 0);
8878 radio_remove_works(wpa_s, "sme-connect", 0);
Hai Shalomc1a21442022-02-04 13:43:00 -08008879 wpa_s->roam_in_progress = false;
8880#ifdef CONFIG_WNM
8881 wpa_s->bss_trans_mgmt_in_progress = false;
8882#endif /* CONFIG_WNM */
Roshan Pius02242d72016-08-09 15:31:48 -07008883}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07008884
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07008885
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008886void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
8887 struct wpa_used_freq_data *freqs_data,
8888 unsigned int len)
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08008889{
8890 unsigned int i;
8891
8892 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
8893 len, title);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008894 for (i = 0; i < len; i++) {
8895 struct wpa_used_freq_data *cur = &freqs_data[i];
8896 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
8897 i, cur->freq, cur->flags);
8898 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08008899}
8900
8901
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008902/*
8903 * Find the operating frequencies of any of the virtual interfaces that
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008904 * are using the same radio as the current interface, and in addition, get
8905 * information about the interface types that are using the frequency.
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008906 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008907int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
8908 struct wpa_used_freq_data *freqs_data,
Sunil Ravi77d572f2023-01-17 23:58:31 +00008909 unsigned int len, bool exclude_current)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008910{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008911 struct wpa_supplicant *ifs;
8912 u8 bssid[ETH_ALEN];
8913 int freq;
8914 unsigned int idx = 0, i;
8915
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08008916 wpa_dbg(wpa_s, MSG_DEBUG,
8917 "Determining shared radio frequencies (max len %u)", len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008918 os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008919
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008920 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8921 radio_list) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008922 if (idx == len)
8923 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008924
Sunil Ravi77d572f2023-01-17 23:58:31 +00008925 if (exclude_current && ifs == wpa_s)
8926 continue;
8927
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008928 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
8929 continue;
8930
8931 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008932 ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
8933 ifs->current_ssid->mode == WPAS_MODE_MESH)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008934 freq = ifs->current_ssid->frequency;
8935 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
8936 freq = ifs->assoc_freq;
8937 else
8938 continue;
8939
8940 /* Hold only distinct freqs */
8941 for (i = 0; i < idx; i++)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008942 if (freqs_data[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008943 break;
8944
8945 if (i == idx)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008946 freqs_data[idx++].freq = freq;
8947
8948 if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008949 freqs_data[i].flags |= ifs->current_ssid->p2p_group ?
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008950 WPA_FREQ_USED_BY_P2P_CLIENT :
8951 WPA_FREQ_USED_BY_INFRA_STATION;
8952 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008953 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08008954
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008955 dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008956 return idx;
8957}
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008958
8959
8960/*
8961 * Find the operating frequencies of any of the virtual interfaces that
8962 * are using the same radio as the current interface.
8963 */
8964int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
Sunil Ravi77d572f2023-01-17 23:58:31 +00008965 int *freq_array, unsigned int len,
8966 bool exclude_current)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008967{
8968 struct wpa_used_freq_data *freqs_data;
8969 int num, i;
8970
8971 os_memset(freq_array, 0, sizeof(int) * len);
8972
8973 freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
8974 if (!freqs_data)
8975 return -1;
8976
Sunil Ravi77d572f2023-01-17 23:58:31 +00008977 num = get_shared_radio_freqs_data(wpa_s, freqs_data, len,
8978 exclude_current);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008979 for (i = 0; i < num; i++)
8980 freq_array[i] = freqs_data[i].freq;
8981
8982 os_free(freqs_data);
8983
8984 return num;
8985}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008986
8987
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08008988struct wpa_supplicant *
8989wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
8990{
8991 switch (frame) {
8992#ifdef CONFIG_P2P
8993 case VENDOR_ELEM_PROBE_REQ_P2P:
8994 case VENDOR_ELEM_PROBE_RESP_P2P:
8995 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
8996 case VENDOR_ELEM_BEACON_P2P_GO:
8997 case VENDOR_ELEM_P2P_PD_REQ:
8998 case VENDOR_ELEM_P2P_PD_RESP:
8999 case VENDOR_ELEM_P2P_GO_NEG_REQ:
9000 case VENDOR_ELEM_P2P_GO_NEG_RESP:
9001 case VENDOR_ELEM_P2P_GO_NEG_CONF:
9002 case VENDOR_ELEM_P2P_INV_REQ:
9003 case VENDOR_ELEM_P2P_INV_RESP:
9004 case VENDOR_ELEM_P2P_ASSOC_REQ:
9005 case VENDOR_ELEM_P2P_ASSOC_RESP:
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009006 return wpa_s->p2pdev;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08009007#endif /* CONFIG_P2P */
9008 default:
9009 return wpa_s;
9010 }
9011}
9012
9013
9014void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
9015{
9016 unsigned int i;
9017 char buf[30];
9018
9019 wpa_printf(MSG_DEBUG, "Update vendor elements");
9020
9021 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
9022 if (wpa_s->vendor_elem[i]) {
9023 int res;
9024
9025 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
9026 if (!os_snprintf_error(sizeof(buf), res)) {
9027 wpa_hexdump_buf(MSG_DEBUG, buf,
9028 wpa_s->vendor_elem[i]);
9029 }
9030 }
9031 }
9032
9033#ifdef CONFIG_P2P
Jimmy Chen48b484b2022-01-25 00:17:50 +08009034 if ((wpa_s->parent == wpa_s || (wpa_s == wpa_s->p2pdev && wpa_s->p2p_mgmt)) &&
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08009035 wpa_s->global->p2p &&
9036 !wpa_s->global->p2p_disabled)
9037 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
9038#endif /* CONFIG_P2P */
9039}
9040
9041
9042int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
9043 const u8 *elem, size_t len)
9044{
9045 u8 *ie, *end;
9046
9047 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
9048 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
9049
9050 for (; ie + 1 < end; ie += 2 + ie[1]) {
9051 if (ie + len > end)
9052 break;
9053 if (os_memcmp(ie, elem, len) != 0)
9054 continue;
9055
9056 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
9057 wpabuf_free(wpa_s->vendor_elem[frame]);
9058 wpa_s->vendor_elem[frame] = NULL;
9059 } else {
9060 os_memmove(ie, ie + len, end - (ie + len));
9061 wpa_s->vendor_elem[frame]->used -= len;
9062 }
9063 wpas_vendor_elem_update(wpa_s);
9064 return 0;
9065 }
9066
9067 return -1;
9068}
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009069
9070
9071struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07009072 u16 num_modes, enum hostapd_hw_mode mode,
Hai Shalom60840252021-02-19 19:02:11 -08009073 bool is_6ghz)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009074{
9075 u16 i;
9076
Hai Shalomc1a21442022-02-04 13:43:00 -08009077 if (!modes)
9078 return NULL;
9079
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009080 for (i = 0; i < num_modes; i++) {
Hai Shalomfdcde762020-04-02 11:19:20 -07009081 if (modes[i].mode != mode ||
9082 !modes[i].num_channels || !modes[i].channels)
9083 continue;
9084 if ((!is_6ghz && !is_6ghz_freq(modes[i].channels[0].freq)) ||
9085 (is_6ghz && is_6ghz_freq(modes[i].channels[0].freq)))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009086 return &modes[i];
9087 }
9088
9089 return NULL;
9090}
9091
9092
Hai Shalomc1a21442022-02-04 13:43:00 -08009093struct hostapd_hw_modes * get_mode_with_freq(struct hostapd_hw_modes *modes,
9094 u16 num_modes, int freq)
9095{
9096 int i, j;
9097
9098 for (i = 0; i < num_modes; i++) {
9099 for (j = 0; j < modes[i].num_channels; j++) {
9100 if (freq == modes[i].channels[j].freq)
9101 return &modes[i];
9102 }
9103 }
9104
9105 return NULL;
9106}
9107
9108
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009109static struct
9110wpa_bss_tmp_disallowed * wpas_get_disallowed_bss(struct wpa_supplicant *wpa_s,
9111 const u8 *bssid)
9112{
9113 struct wpa_bss_tmp_disallowed *bss;
9114
9115 dl_list_for_each(bss, &wpa_s->bss_tmp_disallowed,
9116 struct wpa_bss_tmp_disallowed, list) {
9117 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) == 0)
9118 return bss;
9119 }
9120
9121 return NULL;
9122}
9123
9124
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009125static int wpa_set_driver_tmp_disallow_list(struct wpa_supplicant *wpa_s)
9126{
9127 struct wpa_bss_tmp_disallowed *tmp;
9128 unsigned int num_bssid = 0;
9129 u8 *bssids;
9130 int ret;
9131
9132 bssids = os_malloc(dl_list_len(&wpa_s->bss_tmp_disallowed) * ETH_ALEN);
9133 if (!bssids)
9134 return -1;
9135 dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
9136 struct wpa_bss_tmp_disallowed, list) {
9137 os_memcpy(&bssids[num_bssid * ETH_ALEN], tmp->bssid,
9138 ETH_ALEN);
9139 num_bssid++;
9140 }
Hai Shalom899fcc72020-10-19 14:38:18 -07009141 ret = wpa_drv_set_bssid_tmp_disallow(wpa_s, num_bssid, bssids);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009142 os_free(bssids);
9143 return ret;
9144}
9145
9146
9147static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx)
9148{
9149 struct wpa_supplicant *wpa_s = eloop_ctx;
9150 struct wpa_bss_tmp_disallowed *tmp, *bss = timeout_ctx;
9151
9152 /* Make sure the bss is not already freed */
9153 dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
9154 struct wpa_bss_tmp_disallowed, list) {
9155 if (bss == tmp) {
Hai Shalomc1a21442022-02-04 13:43:00 -08009156 remove_bss_tmp_disallowed_entry(wpa_s, tmp);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009157 wpa_set_driver_tmp_disallow_list(wpa_s);
9158 break;
9159 }
9160 }
9161}
9162
9163
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009164void wpa_bss_tmp_disallow(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom74f70d42019-02-11 14:42:39 -08009165 unsigned int sec, int rssi_threshold)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009166{
9167 struct wpa_bss_tmp_disallowed *bss;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009168
9169 bss = wpas_get_disallowed_bss(wpa_s, bssid);
9170 if (bss) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009171 eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
Hai Shalom74f70d42019-02-11 14:42:39 -08009172 goto finish;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009173 }
9174
9175 bss = os_malloc(sizeof(*bss));
9176 if (!bss) {
9177 wpa_printf(MSG_DEBUG,
9178 "Failed to allocate memory for temp disallow BSS");
9179 return;
9180 }
9181
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009182 os_memcpy(bss->bssid, bssid, ETH_ALEN);
9183 dl_list_add(&wpa_s->bss_tmp_disallowed, &bss->list);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009184 wpa_set_driver_tmp_disallow_list(wpa_s);
Hai Shalom74f70d42019-02-11 14:42:39 -08009185
9186finish:
9187 bss->rssi_threshold = rssi_threshold;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009188 eloop_register_timeout(sec, 0, wpa_bss_tmp_disallow_timeout,
9189 wpa_s, bss);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009190}
9191
9192
Hai Shalom74f70d42019-02-11 14:42:39 -08009193int wpa_is_bss_tmp_disallowed(struct wpa_supplicant *wpa_s,
9194 struct wpa_bss *bss)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009195{
Hai Shalom74f70d42019-02-11 14:42:39 -08009196 struct wpa_bss_tmp_disallowed *disallowed = NULL, *tmp, *prev;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009197
9198 dl_list_for_each_safe(tmp, prev, &wpa_s->bss_tmp_disallowed,
9199 struct wpa_bss_tmp_disallowed, list) {
Hai Shalom74f70d42019-02-11 14:42:39 -08009200 if (os_memcmp(bss->bssid, tmp->bssid, ETH_ALEN) == 0) {
9201 disallowed = tmp;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009202 break;
9203 }
9204 }
Hai Shalom74f70d42019-02-11 14:42:39 -08009205 if (!disallowed)
9206 return 0;
9207
9208 if (disallowed->rssi_threshold != 0 &&
Hai Shalomc1a21442022-02-04 13:43:00 -08009209 bss->level > disallowed->rssi_threshold) {
9210 remove_bss_tmp_disallowed_entry(wpa_s, disallowed);
9211 wpa_set_driver_tmp_disallow_list(wpa_s);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009212 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08009213 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009214
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08009215 return 1;
9216}
Hai Shalom81f62d82019-07-22 12:10:00 -07009217
9218
9219int wpas_enable_mac_addr_randomization(struct wpa_supplicant *wpa_s,
9220 unsigned int type, const u8 *addr,
9221 const u8 *mask)
9222{
9223 if ((addr && !mask) || (!addr && mask)) {
9224 wpa_printf(MSG_INFO,
9225 "MAC_ADDR_RAND_SCAN invalid addr/mask combination");
9226 return -1;
9227 }
9228
9229 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
9230 wpa_printf(MSG_INFO,
9231 "MAC_ADDR_RAND_SCAN cannot allow multicast address");
9232 return -1;
9233 }
9234
9235 if (type & MAC_ADDR_RAND_SCAN) {
9236 if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
9237 addr, mask))
9238 return -1;
9239 }
9240
9241 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
9242 if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
9243 addr, mask))
9244 return -1;
9245
9246 if (wpa_s->sched_scanning && !wpa_s->pno)
9247 wpas_scan_restart_sched_scan(wpa_s);
9248 }
9249
9250 if (type & MAC_ADDR_RAND_PNO) {
9251 if (wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
9252 addr, mask))
9253 return -1;
9254
9255 if (wpa_s->pno) {
9256 wpas_stop_pno(wpa_s);
9257 wpas_start_pno(wpa_s);
9258 }
9259 }
9260
9261 return 0;
9262}
9263
9264
9265int wpas_disable_mac_addr_randomization(struct wpa_supplicant *wpa_s,
9266 unsigned int type)
9267{
9268 wpas_mac_addr_rand_scan_clear(wpa_s, type);
9269 if (wpa_s->pno) {
9270 if (type & MAC_ADDR_RAND_PNO) {
9271 wpas_stop_pno(wpa_s);
9272 wpas_start_pno(wpa_s);
9273 }
9274 } else if (wpa_s->sched_scanning && (type & MAC_ADDR_RAND_SCHED_SCAN)) {
9275 wpas_scan_restart_sched_scan(wpa_s);
9276 }
9277
9278 return 0;
9279}
Hai Shalomfdcde762020-04-02 11:19:20 -07009280
9281
9282int wpa_drv_signal_poll(struct wpa_supplicant *wpa_s,
9283 struct wpa_signal_info *si)
9284{
9285 int res;
9286
9287 if (!wpa_s->driver->signal_poll)
9288 return -1;
9289
9290 res = wpa_s->driver->signal_poll(wpa_s->drv_priv, si);
9291
9292#ifdef CONFIG_TESTING_OPTIONS
9293 if (res == 0) {
9294 struct driver_signal_override *dso;
9295
9296 dl_list_for_each(dso, &wpa_s->drv_signal_override,
9297 struct driver_signal_override, list) {
9298 if (os_memcmp(wpa_s->bssid, dso->bssid,
9299 ETH_ALEN) != 0)
9300 continue;
9301 wpa_printf(MSG_DEBUG,
9302 "Override driver signal_poll information: current_signal: %d->%d avg_signal: %d->%d avg_beacon_signal: %d->%d current_noise: %d->%d",
Sunil Ravi77d572f2023-01-17 23:58:31 +00009303 si->data.signal,
Hai Shalomfdcde762020-04-02 11:19:20 -07009304 dso->si_current_signal,
Sunil Ravi77d572f2023-01-17 23:58:31 +00009305 si->data.avg_signal,
Hai Shalomfdcde762020-04-02 11:19:20 -07009306 dso->si_avg_signal,
Sunil Ravi77d572f2023-01-17 23:58:31 +00009307 si->data.avg_beacon_signal,
Hai Shalomfdcde762020-04-02 11:19:20 -07009308 dso->si_avg_beacon_signal,
9309 si->current_noise,
9310 dso->si_current_noise);
Sunil Ravi77d572f2023-01-17 23:58:31 +00009311 si->data.signal = dso->si_current_signal;
9312 si->data.avg_signal = dso->si_avg_signal;
9313 si->data.avg_beacon_signal = dso->si_avg_beacon_signal;
Hai Shalomfdcde762020-04-02 11:19:20 -07009314 si->current_noise = dso->si_current_noise;
9315 break;
9316 }
9317 }
9318#endif /* CONFIG_TESTING_OPTIONS */
9319
9320 return res;
9321}
9322
9323
9324struct wpa_scan_results *
9325wpa_drv_get_scan_results2(struct wpa_supplicant *wpa_s)
9326{
9327 struct wpa_scan_results *scan_res;
9328#ifdef CONFIG_TESTING_OPTIONS
9329 size_t idx;
9330#endif /* CONFIG_TESTING_OPTIONS */
9331
9332 if (!wpa_s->driver->get_scan_results2)
9333 return NULL;
9334
9335 scan_res = wpa_s->driver->get_scan_results2(wpa_s->drv_priv);
9336
9337#ifdef CONFIG_TESTING_OPTIONS
9338 for (idx = 0; scan_res && idx < scan_res->num; idx++) {
9339 struct driver_signal_override *dso;
9340 struct wpa_scan_res *res = scan_res->res[idx];
9341
9342 dl_list_for_each(dso, &wpa_s->drv_signal_override,
9343 struct driver_signal_override, list) {
9344 if (os_memcmp(res->bssid, dso->bssid, ETH_ALEN) != 0)
9345 continue;
9346 wpa_printf(MSG_DEBUG,
9347 "Override driver scan signal level %d->%d for "
9348 MACSTR,
9349 res->level, dso->scan_level,
9350 MAC2STR(res->bssid));
9351 res->flags |= WPA_SCAN_QUAL_INVALID;
9352 if (dso->scan_level < 0)
9353 res->flags |= WPA_SCAN_LEVEL_DBM;
9354 else
9355 res->flags &= ~WPA_SCAN_LEVEL_DBM;
9356 res->level = dso->scan_level;
9357 break;
9358 }
9359 }
9360#endif /* CONFIG_TESTING_OPTIONS */
9361
9362 return scan_res;
9363}
Sunil Ravi77d572f2023-01-17 23:58:31 +00009364
9365
9366static bool wpas_ap_link_address(struct wpa_supplicant *wpa_s, const u8 *addr)
9367{
9368 int i;
9369
9370 if (!wpa_s->valid_links)
9371 return false;
9372
9373 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
9374 if (!(wpa_s->valid_links & BIT(i)))
9375 continue;
9376
9377 if (os_memcmp(wpa_s->links[i].bssid, addr, ETH_ALEN) == 0)
9378 return true;
9379 }
9380
9381 return false;
9382}
9383
9384
9385int wpa_drv_send_action(struct wpa_supplicant *wpa_s, unsigned int freq,
9386 unsigned int wait, const u8 *dst, const u8 *src,
9387 const u8 *bssid, const u8 *data, size_t data_len,
9388 int no_cck)
9389{
9390 if (!wpa_s->driver->send_action)
9391 return -1;
9392
9393 if (data_len > 0 && data[0] != WLAN_ACTION_PUBLIC) {
9394 if (wpas_ap_link_address(wpa_s, dst))
9395 dst = wpa_s->ap_mld_addr;
9396
9397 if (wpas_ap_link_address(wpa_s, bssid))
9398 bssid = wpa_s->ap_mld_addr;
9399 }
9400
9401 return wpa_s->driver->send_action(wpa_s->drv_priv, freq, wait, dst, src,
9402 bssid, data, data_len, no_cck);
9403}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00009404
9405
9406bool wpas_is_6ghz_supported(struct wpa_supplicant *wpa_s, bool only_enabled)
9407{
9408 struct hostapd_channel_data *chnl;
9409 int i, j;
9410
9411 for (i = 0; i < wpa_s->hw.num_modes; i++) {
9412 if (wpa_s->hw.modes[i].mode == HOSTAPD_MODE_IEEE80211A) {
9413 chnl = wpa_s->hw.modes[i].channels;
9414 for (j = 0; j < wpa_s->hw.modes[i].num_channels; j++) {
9415 if (only_enabled &&
9416 (chnl[j].flag & HOSTAPD_CHAN_DISABLED))
9417 continue;
9418 if (is_6ghz_freq(chnl[j].freq))
9419 return true;
9420 }
9421 }
9422 }
9423
9424 return false;
9425}