blob: 5d4adf4f370457c27c265f51d817f78430336a05 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant
Hai Shalom74f70d42019-02-11 14:42:39 -08003 * Copyright (c) 2003-2019, 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"
20#include "crypto/random.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080021#include "crypto/sha1.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "eapol_supp/eapol_supp_sm.h"
23#include "eap_peer/eap.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070024#include "eap_peer/eap_proxy.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "eap_server/eap_methods.h"
26#include "rsn_supp/wpa.h"
27#include "eloop.h"
28#include "config.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070029#include "utils/ext_password.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "l2_packet/l2_packet.h"
31#include "wpa_supplicant_i.h"
32#include "driver_i.h"
33#include "ctrl_iface.h"
34#include "pcsc_funcs.h"
35#include "common/version.h"
36#include "rsn_supp/preauth.h"
37#include "rsn_supp/pmksa_cache.h"
38#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "common/ieee802_11_defs.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080040#include "common/hw_features_common.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070041#include "common/gas_server.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070042#include "common/dpp.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080044#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#include "blacklist.h"
46#include "wpas_glue.h"
47#include "wps_supplicant.h"
48#include "ibss_rsn.h"
49#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051#include "ap.h"
52#include "p2p_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070053#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054#include "notify.h"
55#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057#include "bss.h"
58#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080059#include "offchannel.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070060#include "hs20_supplicant.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070061#include "wnm_sta.h"
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070062#include "wpas_kay.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080063#include "mesh.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070064#include "dpp_supplicant.h"
65#ifdef CONFIG_MESH
66#include "ap/ap_config.h"
67#include "ap/hostapd.h"
68#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070070const char *const wpa_supplicant_version =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071"wpa_supplicant v" VERSION_STR "\n"
Hai Shalom74f70d42019-02-11 14:42:39 -080072"Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070074const char *const wpa_supplicant_license =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080075"This software may be distributed under the terms of the BSD license.\n"
76"See README for more details.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077#ifdef EAP_TLS_OPENSSL
78"\nThis product includes software developed by the OpenSSL Project\n"
79"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
80#endif /* EAP_TLS_OPENSSL */
81;
82
83#ifndef CONFIG_NO_STDOUT_DEBUG
84/* Long text divided into parts in order to fit in C89 strings size limits. */
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070085const char *const wpa_supplicant_full_license1 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080086"";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070087const char *const wpa_supplicant_full_license2 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080088"This software may be distributed under the terms of the BSD license.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089"\n"
90"Redistribution and use in source and binary forms, with or without\n"
91"modification, are permitted provided that the following conditions are\n"
92"met:\n"
93"\n";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070094const char *const wpa_supplicant_full_license3 =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095"1. Redistributions of source code must retain the above copyright\n"
96" notice, this list of conditions and the following disclaimer.\n"
97"\n"
98"2. Redistributions in binary form must reproduce the above copyright\n"
99" notice, this list of conditions and the following disclaimer in the\n"
100" documentation and/or other materials provided with the distribution.\n"
101"\n";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700102const char *const wpa_supplicant_full_license4 =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
104" names of its contributors may be used to endorse or promote products\n"
105" derived from this software without specific prior written permission.\n"
106"\n"
107"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
108"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
109"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
110"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700111const char *const wpa_supplicant_full_license5 =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
113"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
114"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
115"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
116"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
117"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
118"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
119"\n";
120#endif /* CONFIG_NO_STDOUT_DEBUG */
121
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700122
123static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx);
124#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
125static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s);
126#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
127
128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129/* Configure default/group WEP keys for static WEP */
130int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
131{
132 int i, set = 0;
133
134 for (i = 0; i < NUM_WEP_KEYS; i++) {
135 if (ssid->wep_key_len[i] == 0)
136 continue;
137
138 set = 1;
139 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
140 i, i == ssid->wep_tx_keyidx, NULL, 0,
141 ssid->wep_key[i], ssid->wep_key_len[i]);
142 }
143
144 return set;
145}
146
147
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700148int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
149 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150{
151 u8 key[32];
152 size_t keylen;
153 enum wpa_alg alg;
154 u8 seq[6] = { 0 };
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800155 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156
157 /* IBSS/WPA-None uses only one key (Group) for both receiving and
158 * sending unicast and multicast packets. */
159
160 if (ssid->mode != WPAS_MODE_IBSS) {
161 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
162 "IBSS/ad-hoc) for WPA-None", ssid->mode);
163 return -1;
164 }
165
166 if (!ssid->psk_set) {
167 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
168 "WPA-None");
169 return -1;
170 }
171
172 switch (wpa_s->group_cipher) {
173 case WPA_CIPHER_CCMP:
174 os_memcpy(key, ssid->psk, 16);
175 keylen = 16;
176 alg = WPA_ALG_CCMP;
177 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700178 case WPA_CIPHER_GCMP:
179 os_memcpy(key, ssid->psk, 16);
180 keylen = 16;
181 alg = WPA_ALG_GCMP;
182 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 case WPA_CIPHER_TKIP:
184 /* WPA-None uses the same Michael MIC key for both TX and RX */
185 os_memcpy(key, ssid->psk, 16 + 8);
186 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
187 keylen = 32;
188 alg = WPA_ALG_TKIP;
189 break;
190 default:
191 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
192 "WPA-None", wpa_s->group_cipher);
193 return -1;
194 }
195
196 /* TODO: should actually remember the previously used seq#, both for TX
197 * and RX from each STA.. */
198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800199 ret = wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
200 os_memset(key, 0, sizeof(key));
201 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202}
203
204
205static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
206{
207 struct wpa_supplicant *wpa_s = eloop_ctx;
208 const u8 *bssid = wpa_s->bssid;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700209 if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
210 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
211 wpa_s->wpa_state == WPA_ASSOCIATING))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 bssid = wpa_s->pending_bssid;
213 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
214 MAC2STR(bssid));
215 wpa_blacklist_add(wpa_s, bssid);
216 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800217 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 wpa_s->reassociate = 1;
Roshan Pius38e96762017-01-23 14:52:00 -0800219 wpas_notify_auth_timeout(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220
221 /*
222 * If we timed out, the AP or the local radio may be busy.
223 * So, wait a second until scanning again.
224 */
225 wpa_supplicant_req_scan(wpa_s, 1, 0);
226}
227
228
229/**
230 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
231 * @wpa_s: Pointer to wpa_supplicant data
232 * @sec: Number of seconds after which to time out authentication
233 * @usec: Number of microseconds after which to time out authentication
234 *
235 * This function is used to schedule a timeout for the current authentication
236 * attempt.
237 */
238void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
239 int sec, int usec)
240{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700241 if (wpa_s->conf->ap_scan == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
243 return;
244
245 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
246 "%d usec", sec, usec);
247 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700248 wpa_s->last_auth_timeout_sec = sec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
250}
251
252
Roshan Pius3a1667e2018-07-03 15:17:14 -0700253/*
254 * wpas_auth_timeout_restart - Restart and change timeout for authentication
255 * @wpa_s: Pointer to wpa_supplicant data
256 * @sec_diff: difference in seconds applied to original timeout value
257 */
258void wpas_auth_timeout_restart(struct wpa_supplicant *wpa_s, int sec_diff)
259{
260 int new_sec = wpa_s->last_auth_timeout_sec + sec_diff;
261
262 if (eloop_is_timeout_registered(wpa_supplicant_timeout, wpa_s, NULL)) {
263 wpa_dbg(wpa_s, MSG_DEBUG,
264 "Authentication timeout restart: %d sec", new_sec);
265 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
266 eloop_register_timeout(new_sec, 0, wpa_supplicant_timeout,
267 wpa_s, NULL);
268 }
269}
270
271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272/**
273 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
274 * @wpa_s: Pointer to wpa_supplicant data
275 *
276 * This function is used to cancel authentication timeout scheduled with
277 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
278 * been completed.
279 */
280void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
281{
282 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
283 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
284 wpa_blacklist_del(wpa_s, wpa_s->bssid);
Hai Shalomce48b4a2018-09-05 11:41:35 -0700285 os_free(wpa_s->last_con_fail_realm);
286 wpa_s->last_con_fail_realm = NULL;
287 wpa_s->last_con_fail_realm_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288}
289
290
291/**
292 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
293 * @wpa_s: Pointer to wpa_supplicant data
294 *
295 * This function is used to configure EAPOL state machine based on the selected
296 * authentication mode.
297 */
298void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
299{
300#ifdef IEEE8021X_EAPOL
301 struct eapol_config eapol_conf;
302 struct wpa_ssid *ssid = wpa_s->current_ssid;
303
304#ifdef CONFIG_IBSS_RSN
305 if (ssid->mode == WPAS_MODE_IBSS &&
306 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
307 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
308 /*
309 * RSN IBSS authentication is per-STA and we can disable the
310 * per-BSSID EAPOL authentication.
311 */
312 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
313 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
314 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
315 return;
316 }
317#endif /* CONFIG_IBSS_RSN */
318
319 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
320 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
321
322 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
323 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
324 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
325 else
326 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
327
328 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
329 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
330 eapol_conf.accept_802_1x_keys = 1;
331 eapol_conf.required_keys = 0;
332 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
333 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
334 }
335 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
336 eapol_conf.required_keys |=
337 EAPOL_REQUIRE_KEY_BROADCAST;
338 }
339
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700340 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 eapol_conf.required_keys = 0;
342 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700343 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 eapol_conf.workaround = ssid->eap_workaround;
345 eapol_conf.eap_disabled =
346 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
347 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
348 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700349 eapol_conf.external_sim = wpa_s->conf->external_sim;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800350
351#ifdef CONFIG_WPS
352 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
353 eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
354 if (wpa_s->current_bss) {
355 struct wpabuf *ie;
356 ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
357 WPS_IE_VENDOR_TYPE);
358 if (ie) {
359 if (wps_is_20(ie))
360 eapol_conf.wps |=
361 EAPOL_PEER_IS_WPS20_AP;
362 wpabuf_free(ie);
363 }
364 }
365 }
366#endif /* CONFIG_WPS */
367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700369
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800370#ifdef CONFIG_MACSEC
371 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE && ssid->mka_psk_set)
372 ieee802_1x_create_preshared_mka(wpa_s, ssid);
373 else
374 ieee802_1x_alloc_kay_sm(wpa_s, ssid);
375#endif /* CONFIG_MACSEC */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800376#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377}
378
379
380/**
381 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
382 * @wpa_s: Pointer to wpa_supplicant data
383 * @ssid: Configuration data for the network
384 *
385 * This function is used to configure WPA state machine and related parameters
386 * to a mode where WPA is not enabled. This is called as part of the
387 * authentication configuration when the selected network does not use WPA.
388 */
389void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
390 struct wpa_ssid *ssid)
391{
392 int i;
393
394 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
395 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
396 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
397 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
398 else
399 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
400 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
401 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
402 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
403 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
404 wpa_s->group_cipher = WPA_CIPHER_NONE;
405 wpa_s->mgmt_group_cipher = 0;
406
407 for (i = 0; i < NUM_WEP_KEYS; i++) {
408 if (ssid->wep_key_len[i] > 5) {
409 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
410 wpa_s->group_cipher = WPA_CIPHER_WEP104;
411 break;
412 } else if (ssid->wep_key_len[i] > 0) {
413 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
414 wpa_s->group_cipher = WPA_CIPHER_WEP40;
415 break;
416 }
417 }
418
419 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
420 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
421 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
422 wpa_s->pairwise_cipher);
423 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
424#ifdef CONFIG_IEEE80211W
425 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
426 wpa_s->mgmt_group_cipher);
427#endif /* CONFIG_IEEE80211W */
428
429 pmksa_cache_clear_current(wpa_s->wpa);
430}
431
432
Dmitry Shmidt04949592012-07-19 12:16:46 -0700433void free_hw_features(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434{
435 int i;
436 if (wpa_s->hw.modes == NULL)
437 return;
438
439 for (i = 0; i < wpa_s->hw.num_modes; i++) {
440 os_free(wpa_s->hw.modes[i].channels);
441 os_free(wpa_s->hw.modes[i].rates);
442 }
443
444 os_free(wpa_s->hw.modes);
445 wpa_s->hw.modes = NULL;
446}
447
448
Hai Shalom74f70d42019-02-11 14:42:39 -0800449void free_bss_tmp_disallowed(struct wpa_supplicant *wpa_s)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800450{
451 struct wpa_bss_tmp_disallowed *bss, *prev;
452
453 dl_list_for_each_safe(bss, prev, &wpa_s->bss_tmp_disallowed,
454 struct wpa_bss_tmp_disallowed, list) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700455 eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800456 dl_list_del(&bss->list);
457 os_free(bss);
458 }
459}
460
461
Paul Stewart092955c2017-02-06 09:13:09 -0800462void wpas_flush_fils_hlp_req(struct wpa_supplicant *wpa_s)
463{
464 struct fils_hlp_req *req;
465
466 while ((req = dl_list_first(&wpa_s->fils_hlp_req, struct fils_hlp_req,
467 list)) != NULL) {
468 dl_list_del(&req->list);
469 wpabuf_free(req->pkt);
470 os_free(req);
471 }
472}
473
474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
476{
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700477 int i;
478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479 bgscan_deinit(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700480 autoscan_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 scard_deinit(wpa_s->scard);
482 wpa_s->scard = NULL;
483 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
484 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
485 l2_packet_deinit(wpa_s->l2);
486 wpa_s->l2 = NULL;
487 if (wpa_s->l2_br) {
488 l2_packet_deinit(wpa_s->l2_br);
489 wpa_s->l2_br = NULL;
490 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800491#ifdef CONFIG_TESTING_OPTIONS
492 l2_packet_deinit(wpa_s->l2_test);
493 wpa_s->l2_test = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800494 os_free(wpa_s->get_pref_freq_list_override);
495 wpa_s->get_pref_freq_list_override = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700496 wpabuf_free(wpa_s->last_assoc_req_wpa_ie);
497 wpa_s->last_assoc_req_wpa_ie = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800498#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 if (wpa_s->conf != NULL) {
501 struct wpa_ssid *ssid;
502 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
503 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 }
505
506 os_free(wpa_s->confname);
507 wpa_s->confname = NULL;
508
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -0700509 os_free(wpa_s->confanother);
510 wpa_s->confanother = NULL;
511
Hai Shalomce48b4a2018-09-05 11:41:35 -0700512 os_free(wpa_s->last_con_fail_realm);
513 wpa_s->last_con_fail_realm = NULL;
514 wpa_s->last_con_fail_realm_len = 0;
515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 wpa_sm_set_eapol(wpa_s->wpa, NULL);
517 eapol_sm_deinit(wpa_s->eapol);
518 wpa_s->eapol = NULL;
519
520 rsn_preauth_deinit(wpa_s->wpa);
521
522#ifdef CONFIG_TDLS
523 wpa_tdls_deinit(wpa_s->wpa);
524#endif /* CONFIG_TDLS */
525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800526 wmm_ac_clear_saved_tspecs(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 pmksa_candidate_free(wpa_s->wpa);
528 wpa_sm_deinit(wpa_s->wpa);
529 wpa_s->wpa = NULL;
530 wpa_blacklist_clear(wpa_s);
531
532 wpa_bss_deinit(wpa_s);
533
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700534 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 wpa_supplicant_cancel_scan(wpa_s);
536 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800537 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
538#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
539 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
540 wpa_s, NULL);
541#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700543 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 wpas_wps_deinit(wpa_s);
546
547 wpabuf_free(wpa_s->pending_eapol_rx);
548 wpa_s->pending_eapol_rx = NULL;
549
550#ifdef CONFIG_IBSS_RSN
551 ibss_rsn_deinit(wpa_s->ibss_rsn);
552 wpa_s->ibss_rsn = NULL;
553#endif /* CONFIG_IBSS_RSN */
554
555 sme_deinit(wpa_s);
556
557#ifdef CONFIG_AP
558 wpa_supplicant_ap_deinit(wpa_s);
559#endif /* CONFIG_AP */
560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 wpas_p2p_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800563#ifdef CONFIG_OFFCHANNEL
564 offchannel_deinit(wpa_s);
565#endif /* CONFIG_OFFCHANNEL */
566
567 wpa_supplicant_cancel_sched_scan(wpa_s);
568
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 os_free(wpa_s->next_scan_freqs);
570 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800572 os_free(wpa_s->manual_scan_freqs);
573 wpa_s->manual_scan_freqs = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700574 os_free(wpa_s->select_network_scan_freqs);
575 wpa_s->select_network_scan_freqs = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800576
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700577 os_free(wpa_s->manual_sched_scan_freqs);
578 wpa_s->manual_sched_scan_freqs = NULL;
579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800580 wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
581
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700582 /*
583 * Need to remove any pending gas-query radio work before the
584 * gas_query_deinit() call because gas_query::work has not yet been set
585 * for works that have not been started. gas_query_free() will be unable
586 * to cancel such pending radio works and once the pending gas-query
587 * radio work eventually gets removed, the deinit notification call to
588 * gas_query_start_cb() would result in dereferencing freed memory.
589 */
590 if (wpa_s->radio)
591 radio_remove_works(wpa_s, "gas-query", 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800592 gas_query_deinit(wpa_s->gas);
593 wpa_s->gas = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700594 gas_server_deinit(wpa_s->gas_server);
595 wpa_s->gas_server = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800596
597 free_hw_features(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700598
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700599 ieee802_1x_dealloc_kay_sm(wpa_s);
600
Dmitry Shmidt04949592012-07-19 12:16:46 -0700601 os_free(wpa_s->bssid_filter);
602 wpa_s->bssid_filter = NULL;
603
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800604 os_free(wpa_s->disallow_aps_bssid);
605 wpa_s->disallow_aps_bssid = NULL;
606 os_free(wpa_s->disallow_aps_ssid);
607 wpa_s->disallow_aps_ssid = NULL;
608
Dmitry Shmidt04949592012-07-19 12:16:46 -0700609 wnm_bss_keep_alive_deinit(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700610#ifdef CONFIG_WNM
611 wnm_deallocate_memory(wpa_s);
612#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700613
614 ext_password_deinit(wpa_s->ext_pw);
615 wpa_s->ext_pw = NULL;
616
617 wpabuf_free(wpa_s->last_gas_resp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800618 wpa_s->last_gas_resp = NULL;
619 wpabuf_free(wpa_s->prev_gas_resp);
620 wpa_s->prev_gas_resp = NULL;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700621
622 os_free(wpa_s->last_scan_res);
623 wpa_s->last_scan_res = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800624
625#ifdef CONFIG_HS20
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700626 if (wpa_s->drv_priv)
627 wpa_drv_configure_frame_filters(wpa_s, 0);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700628 hs20_deinit(wpa_s);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800629#endif /* CONFIG_HS20 */
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700630
631 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
632 wpabuf_free(wpa_s->vendor_elem[i]);
633 wpa_s->vendor_elem[i] = NULL;
634 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800635
636 wmm_ac_notify_disassoc(wpa_s);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800637
638 wpa_s->sched_scan_plans_num = 0;
639 os_free(wpa_s->sched_scan_plans);
640 wpa_s->sched_scan_plans = NULL;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800641
642#ifdef CONFIG_MBO
643 wpa_s->non_pref_chan_num = 0;
644 os_free(wpa_s->non_pref_chan);
645 wpa_s->non_pref_chan = NULL;
646#endif /* CONFIG_MBO */
647
648 free_bss_tmp_disallowed(wpa_s);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700649
650 wpabuf_free(wpa_s->lci);
651 wpa_s->lci = NULL;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800652 wpas_clear_beacon_rep_data(wpa_s);
Paul Stewart092955c2017-02-06 09:13:09 -0800653
654#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
655#ifdef CONFIG_MESH
656 {
657 struct external_pmksa_cache *entry;
658
659 while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
660 struct external_pmksa_cache,
661 list)) != NULL) {
662 dl_list_del(&entry->list);
663 os_free(entry->pmksa_cache);
664 os_free(entry);
665 }
666 }
667#endif /* CONFIG_MESH */
668#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
669
670 wpas_flush_fils_hlp_req(wpa_s);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800671
672 wpabuf_free(wpa_s->ric_ies);
673 wpa_s->ric_ies = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700674
675#ifdef CONFIG_DPP
676 wpas_dpp_deinit(wpa_s);
Hai Shalom021b0b52019-04-10 11:17:58 -0700677 dpp_global_deinit(wpa_s->dpp);
678 wpa_s->dpp = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700679#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680}
681
682
683/**
684 * wpa_clear_keys - Clear keys configured for the driver
685 * @wpa_s: Pointer to wpa_supplicant data
686 * @addr: Previously used BSSID or %NULL if not available
687 *
688 * This function clears the encryption keys that has been previously configured
689 * for the driver.
690 */
691void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
692{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800693 int i, max;
694
695#ifdef CONFIG_IEEE80211W
696 max = 6;
697#else /* CONFIG_IEEE80211W */
698 max = 4;
699#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700
701 /* MLME-DELETEKEYS.request */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800702 for (i = 0; i < max; i++) {
703 if (wpa_s->keys_cleared & BIT(i))
704 continue;
705 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
706 NULL, 0);
707 }
708 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
709 !is_zero_ether_addr(addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
711 0);
712 /* MLME-SETPROTECTION.request(None) */
713 wpa_drv_mlme_setprotection(
714 wpa_s, addr,
715 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
716 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
717 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800718 wpa_s->keys_cleared = (u32) -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719}
720
721
722/**
723 * wpa_supplicant_state_txt - Get the connection state name as a text string
724 * @state: State (wpa_state; WPA_*)
725 * Returns: The state name as a printable text string
726 */
727const char * wpa_supplicant_state_txt(enum wpa_states state)
728{
729 switch (state) {
730 case WPA_DISCONNECTED:
731 return "DISCONNECTED";
732 case WPA_INACTIVE:
733 return "INACTIVE";
734 case WPA_INTERFACE_DISABLED:
735 return "INTERFACE_DISABLED";
736 case WPA_SCANNING:
737 return "SCANNING";
738 case WPA_AUTHENTICATING:
739 return "AUTHENTICATING";
740 case WPA_ASSOCIATING:
741 return "ASSOCIATING";
742 case WPA_ASSOCIATED:
743 return "ASSOCIATED";
744 case WPA_4WAY_HANDSHAKE:
745 return "4WAY_HANDSHAKE";
746 case WPA_GROUP_HANDSHAKE:
747 return "GROUP_HANDSHAKE";
748 case WPA_COMPLETED:
749 return "COMPLETED";
750 default:
751 return "UNKNOWN";
752 }
753}
754
755
756#ifdef CONFIG_BGSCAN
757
758static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
759{
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800760 const char *name;
761
762 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
763 name = wpa_s->current_ssid->bgscan;
764 else
765 name = wpa_s->conf->bgscan;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800766 if (name == NULL || name[0] == '\0')
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800767 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800768 if (wpas_driver_bss_selection(wpa_s))
769 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
771 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800772#ifdef CONFIG_P2P
773 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
774 return;
775#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776
777 bgscan_deinit(wpa_s);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800778 if (wpa_s->current_ssid) {
779 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
781 "bgscan");
782 /*
783 * Live without bgscan; it is only used as a roaming
784 * optimization, so the initial connection is not
785 * affected.
786 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700787 } else {
788 struct wpa_scan_results *scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 wpa_s->bgscan_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700790 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
791 0);
792 if (scan_res) {
793 bgscan_notify_scan(wpa_s, scan_res);
794 wpa_scan_results_free(scan_res);
795 }
796 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 } else
798 wpa_s->bgscan_ssid = NULL;
799}
800
801
802static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
803{
804 if (wpa_s->bgscan_ssid != NULL) {
805 bgscan_deinit(wpa_s);
806 wpa_s->bgscan_ssid = NULL;
807 }
808}
809
810#endif /* CONFIG_BGSCAN */
811
812
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
814{
815 if (autoscan_init(wpa_s, 0))
816 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
817}
818
819
820static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
821{
822 autoscan_deinit(wpa_s);
823}
824
825
826void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
827{
828 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
829 wpa_s->wpa_state == WPA_SCANNING) {
830 autoscan_deinit(wpa_s);
831 wpa_supplicant_start_autoscan(wpa_s);
832 }
833}
834
835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836/**
837 * wpa_supplicant_set_state - Set current connection state
838 * @wpa_s: Pointer to wpa_supplicant data
839 * @state: The new connection state
840 *
841 * This function is called whenever the connection state changes, e.g.,
842 * association is completed for WPA/WPA2 4-Way Handshake is started.
843 */
844void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
845 enum wpa_states state)
846{
847 enum wpa_states old_state = wpa_s->wpa_state;
848
849 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
850 wpa_supplicant_state_txt(wpa_s->wpa_state),
851 wpa_supplicant_state_txt(state));
852
Hai Shalom74f70d42019-02-11 14:42:39 -0800853 if (state == WPA_COMPLETED &&
854 os_reltime_initialized(&wpa_s->roam_start)) {
855 os_reltime_age(&wpa_s->roam_start, &wpa_s->roam_time);
856 wpa_s->roam_start.sec = 0;
857 wpa_s->roam_start.usec = 0;
858 wpas_notify_auth_changed(wpa_s);
859 wpas_notify_roam_time(wpa_s);
860 wpas_notify_roam_complete(wpa_s);
861 } else if (state == WPA_DISCONNECTED &&
862 os_reltime_initialized(&wpa_s->roam_start)) {
863 wpa_s->roam_start.sec = 0;
864 wpa_s->roam_start.usec = 0;
865 wpa_s->roam_time.sec = 0;
866 wpa_s->roam_time.usec = 0;
867 wpas_notify_roam_complete(wpa_s);
868 }
869
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -0800870 if (state == WPA_INTERFACE_DISABLED) {
871 /* Assure normal scan when interface is restored */
872 wpa_s->normal_scans = 0;
873 }
874
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700875 if (state == WPA_COMPLETED) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800876 wpas_connect_work_done(wpa_s);
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700877 /* Reinitialize normal_scan counter */
878 wpa_s->normal_scans = 0;
879 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800880
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700881#ifdef CONFIG_P2P
882 /*
883 * P2PS client has to reply to Probe Request frames received on the
884 * group operating channel. Enable Probe Request frame reporting for
885 * P2P connected client in case p2p_cli_probe configuration property is
886 * set to 1.
887 */
888 if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
889 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
890 wpa_s->current_ssid->p2p_group) {
891 if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
892 wpa_dbg(wpa_s, MSG_DEBUG,
893 "P2P: Enable CLI Probe Request RX reporting");
894 wpa_s->p2p_cli_probe =
895 wpa_drv_probe_req_report(wpa_s, 1) >= 0;
896 } else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
897 wpa_dbg(wpa_s, MSG_DEBUG,
898 "P2P: Disable CLI Probe Request RX reporting");
899 wpa_s->p2p_cli_probe = 0;
900 wpa_drv_probe_req_report(wpa_s, 0);
901 }
902 }
903#endif /* CONFIG_P2P */
904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 if (state != WPA_SCANNING)
906 wpa_supplicant_notify_scanning(wpa_s, 0);
907
908 if (state == WPA_COMPLETED && wpa_s->new_connection) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700910 int fils_hlp_sent = 0;
911
912#ifdef CONFIG_SME
913 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
914 wpa_auth_alg_fils(wpa_s->sme.auth_alg))
915 fils_hlp_sent = 1;
916#endif /* CONFIG_SME */
917 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
918 wpa_auth_alg_fils(wpa_s->auth_alg))
919 fils_hlp_sent = 1;
920
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700921#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700923 MACSTR " completed [id=%d id_str=%s%s]",
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800924 MAC2STR(wpa_s->bssid),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 ssid ? ssid->id : -1,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700926 ssid && ssid->id_str ? ssid->id_str : "",
927 fils_hlp_sent ? " FILS_HLP_SENT" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700929 wpas_clear_temp_disabled(wpa_s, ssid, 1);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -0700930 wpa_blacklist_clear(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800931 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 wpa_s->new_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 wpa_drv_set_operstate(wpa_s, 1);
934#ifndef IEEE8021X_EAPOL
935 wpa_drv_set_supp_port(wpa_s, 1);
936#endif /* IEEE8021X_EAPOL */
937 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700938 wpa_s->known_wps_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 wpas_p2p_completed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700940
941 sme_sched_obss_scan(wpa_s, 1);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700942
943#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
944 if (!fils_hlp_sent && ssid && ssid->eap.erp)
945 wpas_update_fils_connect_params(wpa_s);
946#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
948 state == WPA_ASSOCIATED) {
949 wpa_s->new_connection = 1;
950 wpa_drv_set_operstate(wpa_s, 0);
951#ifndef IEEE8021X_EAPOL
952 wpa_drv_set_supp_port(wpa_s, 0);
953#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700954 sme_sched_obss_scan(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 }
956 wpa_s->wpa_state = state;
957
958#ifdef CONFIG_BGSCAN
959 if (state == WPA_COMPLETED)
960 wpa_supplicant_start_bgscan(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800961 else if (state < WPA_ASSOCIATED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 wpa_supplicant_stop_bgscan(wpa_s);
963#endif /* CONFIG_BGSCAN */
964
Hai Shalom5f92bc92019-04-18 11:54:11 -0700965 if (state > WPA_SCANNING)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700966 wpa_supplicant_stop_autoscan(wpa_s);
967
968 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
969 wpa_supplicant_start_autoscan(wpa_s);
970
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800971 if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
972 wmm_ac_notify_disassoc(wpa_s);
973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 if (wpa_s->wpa_state != old_state) {
975 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
976
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700977 /*
978 * Notify the P2P Device interface about a state change in one
979 * of the interfaces.
980 */
981 wpas_p2p_indicate_state_change(wpa_s);
982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 if (wpa_s->wpa_state == WPA_COMPLETED ||
984 old_state == WPA_COMPLETED)
985 wpas_notify_auth_changed(wpa_s);
986 }
987}
988
989
990void wpa_supplicant_terminate_proc(struct wpa_global *global)
991{
992 int pending = 0;
993#ifdef CONFIG_WPS
994 struct wpa_supplicant *wpa_s = global->ifaces;
995 while (wpa_s) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800996 struct wpa_supplicant *next = wpa_s->next;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700997 if (wpas_wps_terminate_pending(wpa_s) == 1)
998 pending = 1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700999#ifdef CONFIG_P2P
1000 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
1001 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
1002 wpas_p2p_disconnect(wpa_s);
1003#endif /* CONFIG_P2P */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001004 wpa_s = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 }
1006#endif /* CONFIG_WPS */
1007 if (pending)
1008 return;
1009 eloop_terminate();
1010}
1011
1012
1013static void wpa_supplicant_terminate(int sig, void *signal_ctx)
1014{
1015 struct wpa_global *global = signal_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 wpa_supplicant_terminate_proc(global);
1017}
1018
1019
1020void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
1021{
1022 enum wpa_states old_state = wpa_s->wpa_state;
1023
1024 wpa_s->pairwise_cipher = 0;
1025 wpa_s->group_cipher = 0;
1026 wpa_s->mgmt_group_cipher = 0;
1027 wpa_s->key_mgmt = 0;
1028 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001029 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030
1031 if (wpa_s->wpa_state != old_state)
1032 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
1033}
1034
1035
1036/**
1037 * wpa_supplicant_reload_configuration - Reload configuration data
1038 * @wpa_s: Pointer to wpa_supplicant data
1039 * Returns: 0 on success or -1 if configuration parsing failed
1040 *
1041 * This function can be used to request that the configuration data is reloaded
1042 * (e.g., after configuration file change). This function is reloading
1043 * configuration only for one interface, so this may need to be called multiple
1044 * times if %wpa_supplicant is controlling multiple interfaces and all
1045 * interfaces need reconfiguration.
1046 */
1047int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
1048{
1049 struct wpa_config *conf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 int reconf_ctrl;
1051 int old_ap_scan;
1052
1053 if (wpa_s->confname == NULL)
1054 return -1;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07001055 conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 if (conf == NULL) {
1057 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
1058 "file '%s' - exiting", wpa_s->confname);
1059 return -1;
1060 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001061 if (wpa_s->confanother &&
1062 !wpa_config_read(wpa_s->confanother, conf)) {
1063 wpa_msg(wpa_s, MSG_ERROR,
1064 "Failed to parse the configuration file '%s' - exiting",
1065 wpa_s->confanother);
1066 return -1;
1067 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07001068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 conf->changed_parameters = (unsigned int) -1;
1070
1071 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
1072 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
1073 os_strcmp(conf->ctrl_interface,
1074 wpa_s->conf->ctrl_interface) != 0);
1075
1076 if (reconf_ctrl && wpa_s->ctrl_iface) {
1077 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
1078 wpa_s->ctrl_iface = NULL;
1079 }
1080
1081 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 if (wpa_s->current_ssid) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001083 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1084 wpa_s->own_disconnect_req = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 wpa_supplicant_deauthenticate(wpa_s,
1086 WLAN_REASON_DEAUTH_LEAVING);
1087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088
1089 /*
1090 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001091 * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001093 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1094 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
1095 wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 /*
1097 * Clear forced success to clear EAP state for next
1098 * authentication.
1099 */
1100 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1101 }
1102 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1103 wpa_sm_set_config(wpa_s->wpa, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001104 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1106 rsn_preauth_deinit(wpa_s->wpa);
1107
1108 old_ap_scan = wpa_s->conf->ap_scan;
1109 wpa_config_free(wpa_s->conf);
1110 wpa_s->conf = conf;
1111 if (old_ap_scan != wpa_s->conf->ap_scan)
1112 wpas_notify_ap_scan_changed(wpa_s);
1113
1114 if (reconf_ctrl)
1115 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1116
1117 wpa_supplicant_update_config(wpa_s);
1118
1119 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001120 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 wpa_s->reassociate = 1;
1122 wpa_supplicant_req_scan(wpa_s, 0, 0);
1123 }
1124 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
1125 return 0;
1126}
1127
1128
1129static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
1130{
1131 struct wpa_global *global = signal_ctx;
1132 struct wpa_supplicant *wpa_s;
1133 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1134 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
1135 sig);
1136 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1137 wpa_supplicant_terminate_proc(global);
1138 }
1139 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001140
1141 if (wpa_debug_reopen_file() < 0) {
1142 /* Ignore errors since we cannot really do much to fix this */
1143 wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
1144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145}
1146
1147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1149 struct wpa_ssid *ssid,
1150 struct wpa_ie_data *ie)
1151{
1152 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1153 if (ret) {
1154 if (ret == -2) {
1155 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1156 "from association info");
1157 }
1158 return -1;
1159 }
1160
1161 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
1162 "cipher suites");
1163 if (!(ie->group_cipher & ssid->group_cipher)) {
1164 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1165 "cipher 0x%x (mask 0x%x) - reject",
1166 ie->group_cipher, ssid->group_cipher);
1167 return -1;
1168 }
1169 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1170 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1171 "cipher 0x%x (mask 0x%x) - reject",
1172 ie->pairwise_cipher, ssid->pairwise_cipher);
1173 return -1;
1174 }
1175 if (!(ie->key_mgmt & ssid->key_mgmt)) {
1176 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1177 "management 0x%x (mask 0x%x) - reject",
1178 ie->key_mgmt, ssid->key_mgmt);
1179 return -1;
1180 }
1181
1182#ifdef CONFIG_IEEE80211W
1183 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001184 wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1186 "that does not support management frame protection - "
1187 "reject");
1188 return -1;
1189 }
1190#endif /* CONFIG_IEEE80211W */
1191
1192 return 0;
1193}
1194
1195
Hai Shalom021b0b52019-04-10 11:17:58 -07001196static int matching_ciphers(struct wpa_ssid *ssid, struct wpa_ie_data *ie,
1197 int freq)
1198{
1199 if (!ie->has_group)
1200 ie->group_cipher = wpa_default_rsn_cipher(freq);
1201 if (!ie->has_pairwise)
1202 ie->pairwise_cipher = wpa_default_rsn_cipher(freq);
1203 return (ie->group_cipher & ssid->group_cipher) &&
1204 (ie->pairwise_cipher & ssid->pairwise_cipher);
1205}
1206
1207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208/**
1209 * wpa_supplicant_set_suites - Set authentication and encryption parameters
1210 * @wpa_s: Pointer to wpa_supplicant data
1211 * @bss: Scan results for the selected BSS, or %NULL if not available
1212 * @ssid: Configuration data for the selected network
1213 * @wpa_ie: Buffer for the WPA/RSN IE
1214 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1215 * used buffer length in case the functions returns success.
1216 * Returns: 0 on success or -1 on failure
1217 *
1218 * This function is used to configure authentication and encryption parameters
1219 * based on the network configuration and scan result for the selected BSS (if
1220 * available).
1221 */
1222int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1223 struct wpa_bss *bss, struct wpa_ssid *ssid,
1224 u8 *wpa_ie, size_t *wpa_ie_len)
1225{
1226 struct wpa_ie_data ie;
1227 int sel, proto;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001228 const u8 *bss_wpa, *bss_rsn, *bss_osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229
1230 if (bss) {
1231 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1232 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001233 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 } else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001235 bss_wpa = bss_rsn = bss_osen = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236
1237 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
1238 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
Hai Shalom021b0b52019-04-10 11:17:58 -07001239 matching_ciphers(ssid, &ie, bss->freq) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 (ie.key_mgmt & ssid->key_mgmt)) {
1241 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1242 proto = WPA_PROTO_RSN;
1243 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001244 wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 (ie.group_cipher & ssid->group_cipher) &&
1246 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1247 (ie.key_mgmt & ssid->key_mgmt)) {
1248 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1249 proto = WPA_PROTO_WPA;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001250#ifdef CONFIG_HS20
Hai Shalom74f70d42019-02-11 14:42:39 -08001251 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN) &&
1252 wpa_parse_wpa_ie(bss_osen, 2 + bss_osen[1], &ie) == 0 &&
1253 (ie.group_cipher & ssid->group_cipher) &&
1254 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1255 (ie.key_mgmt & ssid->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001256 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001257 proto = WPA_PROTO_OSEN;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001258 } else if (bss_rsn && (ssid->proto & WPA_PROTO_OSEN) &&
1259 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1260 (ie.group_cipher & ssid->group_cipher) &&
1261 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1262 (ie.key_mgmt & ssid->key_mgmt)) {
1263 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using OSEN (within RSN)");
1264 proto = WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001265#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 } else if (bss) {
1267 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001268 wpa_dbg(wpa_s, MSG_DEBUG,
1269 "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1270 ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
1271 ssid->key_mgmt);
1272 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
1273 MAC2STR(bss->bssid),
1274 wpa_ssid_txt(bss->ssid, bss->ssid_len),
1275 bss_wpa ? " WPA" : "",
1276 bss_rsn ? " RSN" : "",
1277 bss_osen ? " OSEN" : "");
1278 if (bss_rsn) {
1279 wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
1280 if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
1281 wpa_dbg(wpa_s, MSG_DEBUG,
1282 "Could not parse RSN element");
1283 } else {
1284 wpa_dbg(wpa_s, MSG_DEBUG,
1285 "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1286 ie.pairwise_cipher, ie.group_cipher,
1287 ie.key_mgmt);
1288 }
1289 }
1290 if (bss_wpa) {
1291 wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
1292 if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
1293 wpa_dbg(wpa_s, MSG_DEBUG,
1294 "Could not parse WPA element");
1295 } else {
1296 wpa_dbg(wpa_s, MSG_DEBUG,
1297 "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1298 ie.pairwise_cipher, ie.group_cipher,
1299 ie.key_mgmt);
1300 }
1301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 return -1;
1303 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001304 if (ssid->proto & WPA_PROTO_OSEN)
1305 proto = WPA_PROTO_OSEN;
1306 else if (ssid->proto & WPA_PROTO_RSN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 proto = WPA_PROTO_RSN;
1308 else
1309 proto = WPA_PROTO_WPA;
1310 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1311 os_memset(&ie, 0, sizeof(ie));
1312 ie.group_cipher = ssid->group_cipher;
1313 ie.pairwise_cipher = ssid->pairwise_cipher;
1314 ie.key_mgmt = ssid->key_mgmt;
1315#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001316 ie.mgmt_group_cipher = 0;
1317 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1318 if (ssid->group_mgmt_cipher &
1319 WPA_CIPHER_BIP_GMAC_256)
1320 ie.mgmt_group_cipher =
1321 WPA_CIPHER_BIP_GMAC_256;
1322 else if (ssid->group_mgmt_cipher &
1323 WPA_CIPHER_BIP_CMAC_256)
1324 ie.mgmt_group_cipher =
1325 WPA_CIPHER_BIP_CMAC_256;
1326 else if (ssid->group_mgmt_cipher &
1327 WPA_CIPHER_BIP_GMAC_128)
1328 ie.mgmt_group_cipher =
1329 WPA_CIPHER_BIP_GMAC_128;
1330 else
1331 ie.mgmt_group_cipher =
1332 WPA_CIPHER_AES_128_CMAC;
1333 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334#endif /* CONFIG_IEEE80211W */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001335#ifdef CONFIG_OWE
1336 if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1337 !ssid->owe_only &&
1338 !bss_wpa && !bss_rsn && !bss_osen) {
1339 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1340 wpa_s->wpa_proto = 0;
1341 *wpa_ie_len = 0;
1342 return 0;
1343 }
1344#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1346 "based on configuration");
1347 } else
1348 proto = ie.proto;
1349 }
1350
1351 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1352 "pairwise %d key_mgmt %d proto %d",
1353 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1354#ifdef CONFIG_IEEE80211W
1355 if (ssid->ieee80211w) {
1356 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1357 ie.mgmt_group_cipher);
1358 }
1359#endif /* CONFIG_IEEE80211W */
1360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 wpa_s->wpa_proto = proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1363 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001364 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365
1366 if (bss || !wpa_s->ap_ies_from_associnfo) {
1367 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1368 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1369 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1370 bss_rsn ? 2 + bss_rsn[1] : 0))
1371 return -1;
1372 }
1373
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001374#ifdef CONFIG_NO_WPA
1375 wpa_s->group_cipher = WPA_CIPHER_NONE;
1376 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1377#else /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 sel = ie.group_cipher & ssid->group_cipher;
Hai Shalom021b0b52019-04-10 11:17:58 -07001379 wpa_dbg(wpa_s, MSG_DEBUG,
1380 "WPA: AP group 0x%x network profile group 0x%x; available group 0x%x",
1381 ie.group_cipher, ssid->group_cipher, sel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001382 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1383 if (wpa_s->group_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1385 "cipher");
1386 return -1;
1387 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001388 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1389 wpa_cipher_txt(wpa_s->group_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390
1391 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
Hai Shalom021b0b52019-04-10 11:17:58 -07001392 wpa_dbg(wpa_s, MSG_DEBUG,
1393 "WPA: AP pairwise 0x%x network profile pairwise 0x%x; available pairwise 0x%x",
1394 ie.pairwise_cipher, ssid->pairwise_cipher, sel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001395 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1396 if (wpa_s->pairwise_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1398 "cipher");
1399 return -1;
1400 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001401 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1402 wpa_cipher_txt(wpa_s->pairwise_cipher));
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001403#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404
1405 sel = ie.key_mgmt & ssid->key_mgmt;
Hai Shalom021b0b52019-04-10 11:17:58 -07001406 wpa_dbg(wpa_s, MSG_DEBUG,
1407 "WPA: AP key_mgmt 0x%x network profile key_mgmt 0x%x; available key_mgmt 0x%x",
1408 ie.key_mgmt, ssid->key_mgmt, sel);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001409#ifdef CONFIG_SAE
1410 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1411 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1412#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 if (0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001414#ifdef CONFIG_IEEE80211R
1415#ifdef CONFIG_SHA384
1416 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
1417 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
1418 wpa_dbg(wpa_s, MSG_DEBUG,
1419 "WPA: using KEY_MGMT FT/802.1X-SHA384");
1420 if (pmksa_cache_get_current(wpa_s->wpa)) {
1421 /* PMKSA caching with FT is not fully functional, so
1422 * disable the case for now. */
1423 wpa_dbg(wpa_s, MSG_DEBUG,
1424 "WPA: Disable PMKSA caching for FT/802.1X connection");
1425 pmksa_cache_clear_current(wpa_s->wpa);
1426 }
1427#endif /* CONFIG_SHA384 */
1428#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001429#ifdef CONFIG_SUITEB192
1430 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1431 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
1432 wpa_dbg(wpa_s, MSG_DEBUG,
1433 "WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
1434#endif /* CONFIG_SUITEB192 */
1435#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001436 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1437 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1438 wpa_dbg(wpa_s, MSG_DEBUG,
1439 "WPA: using KEY_MGMT 802.1X with Suite B");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001440#endif /* CONFIG_SUITEB */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001441#ifdef CONFIG_FILS
1442#ifdef CONFIG_IEEE80211R
1443 } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA384) {
1444 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
1445 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA384");
1446 } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA256) {
1447 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
1448 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA256");
1449#endif /* CONFIG_IEEE80211R */
1450 } else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
1451 wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
1452 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA384");
1453 } else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
1454 wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
1455 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA256");
1456#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457#ifdef CONFIG_IEEE80211R
1458 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1459 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1460 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
Roshan Pius3a1667e2018-07-03 15:17:14 -07001461 if (pmksa_cache_get_current(wpa_s->wpa)) {
1462 /* PMKSA caching with FT is not fully functional, so
1463 * disable the case for now. */
1464 wpa_dbg(wpa_s, MSG_DEBUG,
1465 "WPA: Disable PMKSA caching for FT/802.1X connection");
1466 pmksa_cache_clear_current(wpa_s->wpa);
1467 }
Hai Shalom021b0b52019-04-10 11:17:58 -07001468#endif /* CONFIG_IEEE80211R */
1469#ifdef CONFIG_DPP
1470 } else if (sel & WPA_KEY_MGMT_DPP) {
1471 wpa_s->key_mgmt = WPA_KEY_MGMT_DPP;
1472 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT DPP");
1473#endif /* CONFIG_DPP */
1474#ifdef CONFIG_SAE
1475 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1476 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1477 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1478 } else if (sel & WPA_KEY_MGMT_SAE) {
1479 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1480 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1481#endif /* CONFIG_SAE */
1482#ifdef CONFIG_IEEE80211R
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1484 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1485 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1486#endif /* CONFIG_IEEE80211R */
1487#ifdef CONFIG_IEEE80211W
1488 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1489 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1490 wpa_dbg(wpa_s, MSG_DEBUG,
1491 "WPA: using KEY_MGMT 802.1X with SHA256");
1492 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1493 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1494 wpa_dbg(wpa_s, MSG_DEBUG,
1495 "WPA: using KEY_MGMT PSK with SHA256");
1496#endif /* CONFIG_IEEE80211W */
1497 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1498 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1499 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1500 } else if (sel & WPA_KEY_MGMT_PSK) {
1501 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1502 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1503 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1504 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1505 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001506#ifdef CONFIG_HS20
1507 } else if (sel & WPA_KEY_MGMT_OSEN) {
1508 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1509 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1510#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001511#ifdef CONFIG_OWE
1512 } else if (sel & WPA_KEY_MGMT_OWE) {
1513 wpa_s->key_mgmt = WPA_KEY_MGMT_OWE;
1514 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT OWE");
1515#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 } else {
1517 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1518 "authenticated key management type");
1519 return -1;
1520 }
1521
1522 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1523 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1524 wpa_s->pairwise_cipher);
1525 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1526
1527#ifdef CONFIG_IEEE80211W
1528 sel = ie.mgmt_group_cipher;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001529 if (ssid->group_mgmt_cipher)
1530 sel &= ssid->group_mgmt_cipher;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001531 if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1533 sel = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001534 wpa_dbg(wpa_s, MSG_DEBUG,
1535 "WPA: AP mgmt_group_cipher 0x%x network profile mgmt_group_cipher 0x%x; available mgmt_group_cipher 0x%x",
1536 ie.mgmt_group_cipher, ssid->group_mgmt_cipher, sel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 if (sel & WPA_CIPHER_AES_128_CMAC) {
1538 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1539 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1540 "AES-128-CMAC");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001541 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1542 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1543 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1544 "BIP-GMAC-128");
1545 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1546 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1547 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1548 "BIP-GMAC-256");
1549 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1550 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1551 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1552 "BIP-CMAC-256");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 } else {
1554 wpa_s->mgmt_group_cipher = 0;
1555 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1556 }
1557 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1558 wpa_s->mgmt_group_cipher);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001559 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001560 wpas_get_ssid_pmf(wpa_s, ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561#endif /* CONFIG_IEEE80211W */
Hai Shalom74f70d42019-02-11 14:42:39 -08001562#ifdef CONFIG_OCV
1563 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV, ssid->ocv);
1564#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565
1566 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1567 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1568 return -1;
1569 }
1570
Hai Shalom021b0b52019-04-10 11:17:58 -07001571 if (0) {
1572#ifdef CONFIG_DPP
1573 } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_DPP) {
1574 /* Use PMK from DPP network introduction (PMKSA entry) */
1575 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1576#endif /* CONFIG_DPP */
1577 } else if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001578 int psk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001579 int sae_only;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001580
Roshan Pius3a1667e2018-07-03 15:17:14 -07001581 sae_only = (ssid->key_mgmt & (WPA_KEY_MGMT_PSK |
1582 WPA_KEY_MGMT_FT_PSK |
1583 WPA_KEY_MGMT_PSK_SHA256)) == 0;
1584
1585 if (ssid->psk_set && !sae_only) {
1586 wpa_hexdump_key(MSG_MSGDUMP, "PSK (set in config)",
1587 ssid->psk, PMK_LEN);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001588 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL,
1589 NULL);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001590 psk_set = 1;
1591 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001592
Roshan Pius3a1667e2018-07-03 15:17:14 -07001593 if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
1594 (ssid->sae_password || ssid->passphrase))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001595 psk_set = 1;
1596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597#ifndef CONFIG_NO_PBKDF2
1598 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07001599 ssid->passphrase && !sae_only) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001600 u8 psk[PMK_LEN];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001601 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1602 4096, psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001603 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1604 psk, PMK_LEN);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001605 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL, NULL);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001606 psk_set = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001607 os_memset(psk, 0, sizeof(psk));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 }
1609#endif /* CONFIG_NO_PBKDF2 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001610#ifdef CONFIG_EXT_PASSWORD
Roshan Pius3a1667e2018-07-03 15:17:14 -07001611 if (ssid->ext_psk && !sae_only) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001612 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1613 ssid->ext_psk);
1614 char pw_str[64 + 1];
1615 u8 psk[PMK_LEN];
1616
1617 if (pw == NULL) {
1618 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1619 "found from external storage");
1620 return -1;
1621 }
1622
1623 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1624 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1625 "PSK length %d in external storage",
1626 (int) wpabuf_len(pw));
1627 ext_password_free(pw);
1628 return -1;
1629 }
1630
1631 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1632 pw_str[wpabuf_len(pw)] = '\0';
1633
1634#ifndef CONFIG_NO_PBKDF2
1635 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1636 {
1637 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1638 4096, psk, PMK_LEN);
1639 os_memset(pw_str, 0, sizeof(pw_str));
1640 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1641 "external passphrase)",
1642 psk, PMK_LEN);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001643 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
1644 NULL);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001645 psk_set = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001646 os_memset(psk, 0, sizeof(psk));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001647 } else
1648#endif /* CONFIG_NO_PBKDF2 */
1649 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1650 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1651 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1652 "Invalid PSK hex string");
1653 os_memset(pw_str, 0, sizeof(pw_str));
1654 ext_password_free(pw);
1655 return -1;
1656 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001657 wpa_hexdump_key(MSG_MSGDUMP,
1658 "PSK (from external PSK)",
1659 psk, PMK_LEN);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001660 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
1661 NULL);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001662 psk_set = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001663 os_memset(psk, 0, sizeof(psk));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001664 } else {
1665 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1666 "PSK available");
1667 os_memset(pw_str, 0, sizeof(pw_str));
1668 ext_password_free(pw);
1669 return -1;
1670 }
1671
1672 os_memset(pw_str, 0, sizeof(pw_str));
1673 ext_password_free(pw);
1674 }
1675#endif /* CONFIG_EXT_PASSWORD */
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001676
1677 if (!psk_set) {
1678 wpa_msg(wpa_s, MSG_INFO,
1679 "No PSK available for association");
Roshan Pius3a1667e2018-07-03 15:17:14 -07001680 wpas_auth_failed(wpa_s, "NO_PSK_AVAILABLE");
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001681 return -1;
1682 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001683#ifdef CONFIG_OWE
1684 } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE) {
1685 /* OWE Diffie-Hellman exchange in (Re)Association
1686 * Request/Response frames set the PMK, so do not override it
1687 * here. */
1688#endif /* CONFIG_OWE */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1691
1692 return 0;
1693}
1694
1695
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001696static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
1697{
1698 *pos = 0x00;
1699
1700 switch (idx) {
1701 case 0: /* Bits 0-7 */
1702 break;
1703 case 1: /* Bits 8-15 */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001704 if (wpa_s->conf->coloc_intf_reporting) {
1705 /* Bit 13 - Collocated Interference Reporting */
1706 *pos |= 0x20;
1707 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001708 break;
1709 case 2: /* Bits 16-23 */
1710#ifdef CONFIG_WNM
1711 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1712 *pos |= 0x08; /* Bit 19 - BSS Transition */
1713#endif /* CONFIG_WNM */
1714 break;
1715 case 3: /* Bits 24-31 */
1716#ifdef CONFIG_WNM
1717 *pos |= 0x02; /* Bit 25 - SSID List */
1718#endif /* CONFIG_WNM */
1719#ifdef CONFIG_INTERWORKING
1720 if (wpa_s->conf->interworking)
1721 *pos |= 0x80; /* Bit 31 - Interworking */
1722#endif /* CONFIG_INTERWORKING */
1723 break;
1724 case 4: /* Bits 32-39 */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001725#ifdef CONFIG_INTERWORKING
Hai Shalomce48b4a2018-09-05 11:41:35 -07001726 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_QOS_MAPPING)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001727 *pos |= 0x01; /* Bit 32 - QoS Map */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001728#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001729 break;
1730 case 5: /* Bits 40-47 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001731#ifdef CONFIG_HS20
1732 if (wpa_s->conf->hs20)
1733 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1734#endif /* CONFIG_HS20 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001735#ifdef CONFIG_MBO
1736 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1737#endif /* CONFIG_MBO */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001738 break;
1739 case 6: /* Bits 48-55 */
1740 break;
Dmitry Shmidt7d175302016-09-06 13:11:34 -07001741 case 7: /* Bits 56-63 */
1742 break;
1743 case 8: /* Bits 64-71 */
1744 if (wpa_s->conf->ftm_responder)
1745 *pos |= 0x40; /* Bit 70 - FTM responder */
1746 if (wpa_s->conf->ftm_initiator)
1747 *pos |= 0x80; /* Bit 71 - FTM initiator */
1748 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001749 case 9: /* Bits 72-79 */
1750#ifdef CONFIG_FILS
Roshan Pius3a1667e2018-07-03 15:17:14 -07001751 if (!wpa_s->disable_fils)
1752 *pos |= 0x01;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001753#endif /* CONFIG_FILS */
1754 break;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001755 }
1756}
1757
1758
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001759int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001760{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001761 u8 *pos = buf;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001762 u8 len = 10, i;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001763
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001764 if (len < wpa_s->extended_capa_len)
1765 len = wpa_s->extended_capa_len;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001766 if (buflen < (size_t) len + 2) {
1767 wpa_printf(MSG_INFO,
1768 "Not enough room for building extended capabilities element");
1769 return -1;
1770 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001771
1772 *pos++ = WLAN_EID_EXT_CAPAB;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001773 *pos++ = len;
1774 for (i = 0; i < len; i++, pos++) {
1775 wpas_ext_capab_byte(wpa_s, pos, i);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001776
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001777 if (i < wpa_s->extended_capa_len) {
1778 *pos &= ~wpa_s->extended_capa_mask[i];
1779 *pos |= wpa_s->extended_capa[i];
1780 }
1781 }
1782
1783 while (len > 0 && buf[1 + len] == 0) {
1784 len--;
1785 buf[1] = len;
1786 }
1787 if (len == 0)
1788 return 0;
1789
1790 return 2 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001791}
1792
1793
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001794static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1795 struct wpa_bss *test_bss)
1796{
1797 struct wpa_bss *bss;
1798
1799 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1800 if (bss == test_bss)
1801 return 1;
1802 }
1803
1804 return 0;
1805}
1806
1807
1808static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1809 struct wpa_ssid *test_ssid)
1810{
1811 struct wpa_ssid *ssid;
1812
1813 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1814 if (ssid == test_ssid)
1815 return 1;
1816 }
1817
1818 return 0;
1819}
1820
1821
1822int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1823 struct wpa_ssid *test_ssid)
1824{
1825 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1826 return 0;
1827
1828 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1829}
1830
1831
1832void wpas_connect_work_free(struct wpa_connect_work *cwork)
1833{
1834 if (cwork == NULL)
1835 return;
1836 os_free(cwork);
1837}
1838
1839
1840void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1841{
1842 struct wpa_connect_work *cwork;
1843 struct wpa_radio_work *work = wpa_s->connect_work;
1844
1845 if (!work)
1846 return;
1847
1848 wpa_s->connect_work = NULL;
1849 cwork = work->ctx;
1850 work->ctx = NULL;
1851 wpas_connect_work_free(cwork);
1852 radio_work_done(work);
1853}
1854
1855
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001856int wpas_update_random_addr(struct wpa_supplicant *wpa_s, int style)
1857{
1858 struct os_reltime now;
1859 u8 addr[ETH_ALEN];
1860
1861 os_get_reltime(&now);
1862 if (wpa_s->last_mac_addr_style == style &&
1863 wpa_s->last_mac_addr_change.sec != 0 &&
1864 !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
1865 wpa_s->conf->rand_addr_lifetime)) {
1866 wpa_msg(wpa_s, MSG_DEBUG,
1867 "Previously selected random MAC address has not yet expired");
1868 return 0;
1869 }
1870
1871 switch (style) {
1872 case 1:
1873 if (random_mac_addr(addr) < 0)
1874 return -1;
1875 break;
1876 case 2:
1877 os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
1878 if (random_mac_addr_keep_oui(addr) < 0)
1879 return -1;
1880 break;
1881 default:
1882 return -1;
1883 }
1884
1885 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
1886 wpa_msg(wpa_s, MSG_INFO,
1887 "Failed to set random MAC address");
1888 return -1;
1889 }
1890
1891 os_get_reltime(&wpa_s->last_mac_addr_change);
1892 wpa_s->mac_addr_changed = 1;
1893 wpa_s->last_mac_addr_style = style;
1894
1895 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1896 wpa_msg(wpa_s, MSG_INFO,
1897 "Could not update MAC address information");
1898 return -1;
1899 }
1900
1901 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
1902 MAC2STR(addr));
1903
1904 return 0;
1905}
1906
1907
1908int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
1909{
1910 if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
1911 !wpa_s->conf->preassoc_mac_addr)
1912 return 0;
1913
1914 return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr);
1915}
1916
1917
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001918static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920/**
1921 * wpa_supplicant_associate - Request association
1922 * @wpa_s: Pointer to wpa_supplicant data
1923 * @bss: Scan results for the selected BSS, or %NULL if not available
1924 * @ssid: Configuration data for the selected network
1925 *
1926 * This function is used to request %wpa_supplicant to associate with a BSS.
1927 */
1928void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1929 struct wpa_bss *bss, struct wpa_ssid *ssid)
1930{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001931 struct wpa_connect_work *cwork;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001932 int rand_style;
1933
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001934 wpa_s->own_disconnect_req = 0;
1935
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001936 /*
1937 * If we are starting a new connection, any previously pending EAPOL
1938 * RX cannot be valid anymore.
1939 */
1940 wpabuf_free(wpa_s->pending_eapol_rx);
1941 wpa_s->pending_eapol_rx = NULL;
1942
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001943 if (ssid->mac_addr == -1)
1944 rand_style = wpa_s->conf->mac_addr;
1945 else
1946 rand_style = ssid->mac_addr;
1947
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001948 wmm_ac_clear_saved_tspecs(wpa_s);
1949 wpa_s->reassoc_same_bss = 0;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001950 wpa_s->reassoc_same_ess = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001951#ifdef CONFIG_TESTING_OPTIONS
1952 wpa_s->testing_resend_assoc = 0;
1953#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001954
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001955 if (wpa_s->last_ssid == ssid) {
1956 wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
Dmitry Shmidte4663042016-04-04 10:07:49 -07001957 wpa_s->reassoc_same_ess = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001958 if (wpa_s->current_bss && wpa_s->current_bss == bss) {
1959 wmm_ac_save_tspecs(wpa_s);
1960 wpa_s->reassoc_same_bss = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08001961 } else if (wpa_s->current_bss && wpa_s->current_bss != bss) {
1962 os_get_reltime(&wpa_s->roam_start);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001963 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001964 }
1965
1966 if (rand_style > 0 && !wpa_s->reassoc_same_ess) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001967 if (wpas_update_random_addr(wpa_s, rand_style) < 0)
1968 return;
1969 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001970 } else if (rand_style == 0 && wpa_s->mac_addr_changed) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001971 if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
1972 wpa_msg(wpa_s, MSG_INFO,
1973 "Could not restore permanent MAC address");
1974 return;
1975 }
1976 wpa_s->mac_addr_changed = 0;
1977 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1978 wpa_msg(wpa_s, MSG_INFO,
1979 "Could not update MAC address information");
1980 return;
1981 }
1982 wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
1983 }
1984 wpa_s->last_ssid = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985
1986#ifdef CONFIG_IBSS_RSN
1987 ibss_rsn_deinit(wpa_s->ibss_rsn);
1988 wpa_s->ibss_rsn = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001989#else /* CONFIG_IBSS_RSN */
1990 if (ssid->mode == WPAS_MODE_IBSS &&
1991 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
1992 wpa_msg(wpa_s, MSG_INFO,
1993 "IBSS RSN not supported in the build");
1994 return;
1995 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996#endif /* CONFIG_IBSS_RSN */
1997
1998 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1999 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
2000#ifdef CONFIG_AP
2001 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
2002 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
2003 "mode");
2004 return;
2005 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002006 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
2007 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002008 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
2009 wpas_p2p_ap_setup_failed(wpa_s);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002010 return;
2011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 wpa_s->current_bss = bss;
2013#else /* CONFIG_AP */
2014 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
2015 "the build");
2016#endif /* CONFIG_AP */
2017 return;
2018 }
2019
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002020 if (ssid->mode == WPAS_MODE_MESH) {
2021#ifdef CONFIG_MESH
2022 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
2023 wpa_msg(wpa_s, MSG_INFO,
2024 "Driver does not support mesh mode");
2025 return;
2026 }
2027 if (bss)
2028 ssid->frequency = bss->freq;
2029 if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
2030 wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
2031 return;
2032 }
2033 wpa_s->current_bss = bss;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002034 wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_STARTED "ssid=\"%s\" id=%d",
2035 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
2036 ssid->id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002037 wpas_notify_mesh_group_started(wpa_s, ssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002038#else /* CONFIG_MESH */
2039 wpa_msg(wpa_s, MSG_ERROR,
2040 "mesh mode support not included in the build");
2041#endif /* CONFIG_MESH */
2042 return;
2043 }
2044
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002045 /*
2046 * Set WPA state machine configuration to match the selected network now
2047 * so that the information is available before wpas_start_assoc_cb()
2048 * gets called. This is needed at least for RSN pre-authentication where
2049 * candidate APs are added to a list based on scan result processing
2050 * before completion of the first association.
2051 */
2052 wpa_supplicant_rsn_supp_set_config(wpa_s, ssid);
2053
2054#ifdef CONFIG_DPP
2055 if (wpas_dpp_check_connect(wpa_s, ssid, bss) != 0)
2056 return;
2057#endif /* CONFIG_DPP */
2058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059#ifdef CONFIG_TDLS
2060 if (bss)
2061 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
2062 bss->ie_len);
2063#endif /* CONFIG_TDLS */
2064
2065 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2066 ssid->mode == IEEE80211_MODE_INFRA) {
2067 sme_authenticate(wpa_s, bss, ssid);
2068 return;
2069 }
2070
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002071 if (wpa_s->connect_work) {
2072 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
2073 return;
2074 }
2075
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002076 if (radio_work_pending(wpa_s, "connect")) {
2077 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
2078 return;
2079 }
2080
Dmitry Shmidt29333592017-01-09 12:27:11 -08002081#ifdef CONFIG_SME
2082 if (ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) {
2083 /* Clear possibly set auth_alg, if any, from last attempt. */
2084 wpa_s->sme.auth_alg = WPA_AUTH_ALG_OPEN;
2085 }
2086#endif /* CONFIG_SME */
2087
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002088 wpas_abort_ongoing_scan(wpa_s);
2089
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002090 cwork = os_zalloc(sizeof(*cwork));
2091 if (cwork == NULL)
2092 return;
2093
2094 cwork->bss = bss;
2095 cwork->ssid = ssid;
2096
2097 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
2098 wpas_start_assoc_cb, cwork) < 0) {
2099 os_free(cwork);
2100 }
2101}
2102
2103
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002104static int bss_is_ibss(struct wpa_bss *bss)
2105{
2106 return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
2107 IEEE80211_CAP_IBSS;
2108}
2109
2110
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002111static int drv_supports_vht(struct wpa_supplicant *wpa_s,
2112 const struct wpa_ssid *ssid)
2113{
2114 enum hostapd_hw_mode hw_mode;
2115 struct hostapd_hw_modes *mode = NULL;
2116 u8 channel;
2117 int i;
2118
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002119 hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
2120 if (hw_mode == NUM_HOSTAPD_MODES)
2121 return 0;
2122 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
2123 if (wpa_s->hw.modes[i].mode == hw_mode) {
2124 mode = &wpa_s->hw.modes[i];
2125 break;
2126 }
2127 }
2128
2129 if (!mode)
2130 return 0;
2131
2132 return mode->vht_capab != 0;
2133}
2134
2135
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002136void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
2137 const struct wpa_ssid *ssid,
2138 struct hostapd_freq_params *freq)
2139{
2140 enum hostapd_hw_mode hw_mode;
2141 struct hostapd_hw_modes *mode = NULL;
2142 int ht40plus[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
2143 184, 192 };
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002144 int vht80[] = { 36, 52, 100, 116, 132, 149 };
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002145 struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
2146 u8 channel;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002147 int i, chan_idx, ht40 = -1, res, obss_scan = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002148 unsigned int j, k;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002149 struct hostapd_freq_params vht_freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002150 int chwidth, seg0, seg1;
2151 u32 vht_caps = 0;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002152
2153 freq->freq = ssid->frequency;
2154
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002155 for (j = 0; j < wpa_s->last_scan_res_used; j++) {
2156 struct wpa_bss *bss = wpa_s->last_scan_res[j];
2157
2158 if (ssid->mode != WPAS_MODE_IBSS)
2159 break;
2160
2161 /* Don't adjust control freq in case of fixed_freq */
2162 if (ssid->fixed_freq)
2163 break;
2164
2165 if (!bss_is_ibss(bss))
2166 continue;
2167
2168 if (ssid->ssid_len == bss->ssid_len &&
2169 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0) {
2170 wpa_printf(MSG_DEBUG,
2171 "IBSS already found in scan results, adjust control freq: %d",
2172 bss->freq);
2173 freq->freq = bss->freq;
2174 obss_scan = 0;
2175 break;
2176 }
2177 }
2178
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002179 /* For IBSS check HT_IBSS flag */
2180 if (ssid->mode == WPAS_MODE_IBSS &&
2181 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
2182 return;
2183
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002184 if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
2185 wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
2186 wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
2187 wpa_printf(MSG_DEBUG,
2188 "IBSS: WEP/TKIP detected, do not try to enable HT");
2189 return;
2190 }
2191
2192 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002193 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
2194 if (wpa_s->hw.modes[i].mode == hw_mode) {
2195 mode = &wpa_s->hw.modes[i];
2196 break;
2197 }
2198 }
2199
2200 if (!mode)
2201 return;
2202
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002203#ifdef CONFIG_HT_OVERRIDES
2204 if (ssid->disable_ht) {
2205 freq->ht_enabled = 0;
2206 return;
2207 }
2208#endif /* CONFIG_HT_OVERRIDES */
2209
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002210 freq->ht_enabled = ht_supported(mode);
2211 if (!freq->ht_enabled)
2212 return;
2213
2214 /* Setup higher BW only for 5 GHz */
2215 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
2216 return;
2217
2218 for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
2219 pri_chan = &mode->channels[chan_idx];
2220 if (pri_chan->chan == channel)
2221 break;
2222 pri_chan = NULL;
2223 }
2224 if (!pri_chan)
2225 return;
2226
2227 /* Check primary channel flags */
2228 if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2229 return;
2230
Hai Shalom74f70d42019-02-11 14:42:39 -08002231 freq->channel = pri_chan->chan;
2232
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002233#ifdef CONFIG_HT_OVERRIDES
Hai Shalom74f70d42019-02-11 14:42:39 -08002234 if (ssid->disable_ht40) {
2235 if (ssid->disable_vht)
2236 return;
2237 goto skip_ht40;
2238 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002239#endif /* CONFIG_HT_OVERRIDES */
2240
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002241 /* Check/setup HT40+/HT40- */
2242 for (j = 0; j < ARRAY_SIZE(ht40plus); j++) {
2243 if (ht40plus[j] == channel) {
2244 ht40 = 1;
2245 break;
2246 }
2247 }
2248
2249 /* Find secondary channel */
2250 for (i = 0; i < mode->num_channels; i++) {
2251 sec_chan = &mode->channels[i];
2252 if (sec_chan->chan == channel + ht40 * 4)
2253 break;
2254 sec_chan = NULL;
2255 }
2256 if (!sec_chan)
2257 return;
2258
2259 /* Check secondary channel flags */
2260 if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2261 return;
2262
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002263 if (ht40 == -1) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002264 if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
2265 return;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002266 } else {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002267 if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
2268 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002269 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002270 freq->sec_channel_offset = ht40;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002271
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07002272 if (obss_scan) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002273 struct wpa_scan_results *scan_res;
2274
2275 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2276 if (scan_res == NULL) {
2277 /* Back to HT20 */
2278 freq->sec_channel_offset = 0;
2279 return;
2280 }
2281
2282 res = check_40mhz_5g(mode, scan_res, pri_chan->chan,
2283 sec_chan->chan);
2284 switch (res) {
2285 case 0:
2286 /* Back to HT20 */
2287 freq->sec_channel_offset = 0;
2288 break;
2289 case 1:
2290 /* Configuration allowed */
2291 break;
2292 case 2:
2293 /* Switch pri/sec channels */
2294 freq->freq = hw_get_freq(mode, sec_chan->chan);
2295 freq->sec_channel_offset = -freq->sec_channel_offset;
2296 freq->channel = sec_chan->chan;
2297 break;
2298 default:
2299 freq->sec_channel_offset = 0;
2300 break;
2301 }
2302
2303 wpa_scan_results_free(scan_res);
2304 }
2305
Hai Shalom74f70d42019-02-11 14:42:39 -08002306#ifdef CONFIG_HT_OVERRIDES
2307skip_ht40:
2308#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002309 wpa_printf(MSG_DEBUG,
2310 "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
2311 freq->channel, freq->sec_channel_offset);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002312
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002313 if (!drv_supports_vht(wpa_s, ssid))
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002314 return;
2315
2316 /* For IBSS check VHT_IBSS flag */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002317 if (ssid->mode == WPAS_MODE_IBSS &&
2318 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002319 return;
2320
2321 vht_freq = *freq;
2322
Paul Stewart092955c2017-02-06 09:13:09 -08002323#ifdef CONFIG_VHT_OVERRIDES
2324 if (ssid->disable_vht) {
2325 freq->vht_enabled = 0;
2326 return;
2327 }
2328#endif /* CONFIG_VHT_OVERRIDES */
2329
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002330 vht_freq.vht_enabled = vht_supported(mode);
2331 if (!vht_freq.vht_enabled)
2332 return;
2333
2334 /* setup center_freq1, bandwidth */
2335 for (j = 0; j < ARRAY_SIZE(vht80); j++) {
2336 if (freq->channel >= vht80[j] &&
2337 freq->channel < vht80[j] + 16)
2338 break;
2339 }
2340
2341 if (j == ARRAY_SIZE(vht80))
2342 return;
2343
2344 for (i = vht80[j]; i < vht80[j] + 16; i += 4) {
2345 struct hostapd_channel_data *chan;
2346
2347 chan = hw_get_channel_chan(mode, i, NULL);
2348 if (!chan)
2349 return;
2350
2351 /* Back to HT configuration if channel not usable */
2352 if (chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2353 return;
2354 }
2355
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002356 chwidth = VHT_CHANWIDTH_80MHZ;
2357 seg0 = vht80[j] + 6;
2358 seg1 = 0;
2359
2360 if (ssid->max_oper_chwidth == VHT_CHANWIDTH_80P80MHZ) {
2361 /* setup center_freq2, bandwidth */
2362 for (k = 0; k < ARRAY_SIZE(vht80); k++) {
2363 /* Only accept 80 MHz segments separated by a gap */
2364 if (j == k || abs(vht80[j] - vht80[k]) == 16)
2365 continue;
2366 for (i = vht80[k]; i < vht80[k] + 16; i += 4) {
2367 struct hostapd_channel_data *chan;
2368
2369 chan = hw_get_channel_chan(mode, i, NULL);
2370 if (!chan)
2371 continue;
2372
2373 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
2374 HOSTAPD_CHAN_NO_IR |
2375 HOSTAPD_CHAN_RADAR))
2376 continue;
2377
2378 /* Found a suitable second segment for 80+80 */
2379 chwidth = VHT_CHANWIDTH_80P80MHZ;
2380 vht_caps |=
2381 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
2382 seg1 = vht80[k] + 6;
2383 }
2384
2385 if (chwidth == VHT_CHANWIDTH_80P80MHZ)
2386 break;
2387 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002388 } else if (ssid->max_oper_chwidth == VHT_CHANWIDTH_160MHZ) {
2389 if (freq->freq == 5180) {
2390 chwidth = VHT_CHANWIDTH_160MHZ;
2391 vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
2392 seg0 = 50;
2393 } else if (freq->freq == 5520) {
2394 chwidth = VHT_CHANWIDTH_160MHZ;
2395 vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
2396 seg0 = 114;
2397 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002398 } else if (ssid->max_oper_chwidth == VHT_CHANWIDTH_USE_HT) {
2399 chwidth = VHT_CHANWIDTH_USE_HT;
2400 seg0 = vht80[j] + 2;
2401#ifdef CONFIG_HT_OVERRIDES
2402 if (ssid->disable_ht40)
2403 seg0 = 0;
2404#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002405 }
2406
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002407 if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
2408 freq->channel, freq->ht_enabled,
2409 vht_freq.vht_enabled,
2410 freq->sec_channel_offset,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002411 chwidth, seg0, seg1, vht_caps) != 0)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002412 return;
2413
2414 *freq = vht_freq;
2415
2416 wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
2417 freq->center_freq1, freq->center_freq2, freq->bandwidth);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002418}
2419
2420
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002421#ifdef CONFIG_FILS
2422static size_t wpas_add_fils_hlp_req(struct wpa_supplicant *wpa_s, u8 *ie_buf,
2423 size_t ie_buf_len)
2424{
2425 struct fils_hlp_req *req;
2426 size_t rem_len, hdr_len, hlp_len, len, ie_len = 0;
2427 const u8 *pos;
2428 u8 *buf = ie_buf;
2429
2430 dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
2431 list) {
2432 rem_len = ie_buf_len - ie_len;
2433 pos = wpabuf_head(req->pkt);
2434 hdr_len = 1 + 2 * ETH_ALEN + 6;
2435 hlp_len = wpabuf_len(req->pkt);
2436
2437 if (rem_len < 2 + hdr_len + hlp_len) {
2438 wpa_printf(MSG_ERROR,
2439 "FILS: Cannot fit HLP - rem_len=%lu to_fill=%lu",
2440 (unsigned long) rem_len,
2441 (unsigned long) (2 + hdr_len + hlp_len));
2442 break;
2443 }
2444
2445 len = (hdr_len + hlp_len) > 255 ? 255 : hdr_len + hlp_len;
2446 /* Element ID */
2447 *buf++ = WLAN_EID_EXTENSION;
2448 /* Length */
2449 *buf++ = len;
2450 /* Element ID Extension */
2451 *buf++ = WLAN_EID_EXT_FILS_HLP_CONTAINER;
2452 /* Destination MAC address */
2453 os_memcpy(buf, req->dst, ETH_ALEN);
2454 buf += ETH_ALEN;
2455 /* Source MAC address */
2456 os_memcpy(buf, wpa_s->own_addr, ETH_ALEN);
2457 buf += ETH_ALEN;
2458 /* LLC/SNAP Header */
2459 os_memcpy(buf, "\xaa\xaa\x03\x00\x00\x00", 6);
2460 buf += 6;
2461 /* HLP Packet */
2462 os_memcpy(buf, pos, len - hdr_len);
2463 buf += len - hdr_len;
2464 pos += len - hdr_len;
2465
2466 hlp_len -= len - hdr_len;
2467 ie_len += 2 + len;
2468 rem_len -= 2 + len;
2469
2470 while (hlp_len) {
2471 len = (hlp_len > 255) ? 255 : hlp_len;
2472 if (rem_len < 2 + len)
2473 break;
2474 *buf++ = WLAN_EID_FRAGMENT;
2475 *buf++ = len;
2476 os_memcpy(buf, pos, len);
2477 buf += len;
2478 pos += len;
2479
2480 hlp_len -= len;
2481 ie_len += 2 + len;
2482 rem_len -= 2 + len;
2483 }
2484 }
2485
2486 return ie_len;
2487}
Roshan Pius3a1667e2018-07-03 15:17:14 -07002488
2489
2490int wpa_is_fils_supported(struct wpa_supplicant *wpa_s)
2491{
2492 return (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2493 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) ||
2494 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2495 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)));
2496}
2497
2498
2499int wpa_is_fils_sk_pfs_supported(struct wpa_supplicant *wpa_s)
2500{
2501#ifdef CONFIG_FILS_SK_PFS
2502 return (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2503 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS);
2504#else /* CONFIG_FILS_SK_PFS */
2505 return 0;
2506#endif /* CONFIG_FILS_SK_PFS */
2507}
2508
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002509#endif /* CONFIG_FILS */
2510
2511
2512static u8 * wpas_populate_assoc_ies(
2513 struct wpa_supplicant *wpa_s,
2514 struct wpa_bss *bss, struct wpa_ssid *ssid,
2515 struct wpa_driver_associate_params *params,
2516 enum wpa_drv_update_connect_params_mask *mask)
2517{
2518 u8 *wpa_ie;
2519 size_t max_wpa_ie_len = 500;
2520 size_t wpa_ie_len;
2521 int algs = WPA_AUTH_ALG_OPEN;
Hai Shalomce48b4a2018-09-05 11:41:35 -07002522#ifdef CONFIG_MBO
2523 const u8 *mbo_ie;
2524#endif
Hai Shalom5f92bc92019-04-18 11:54:11 -07002525#ifdef CONFIG_SAE
2526 int sae_pmksa_cached = 0;
2527#endif /* CONFIG_SAE */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002528#ifdef CONFIG_FILS
2529 const u8 *realm, *username, *rrk;
2530 size_t realm_len, username_len, rrk_len;
2531 u16 next_seq_num;
2532 struct fils_hlp_req *req;
2533
2534 dl_list_for_each(req, &wpa_s->fils_hlp_req, struct fils_hlp_req,
2535 list) {
2536 max_wpa_ie_len += 3 + 2 * ETH_ALEN + 6 + wpabuf_len(req->pkt) +
2537 2 + 2 * wpabuf_len(req->pkt) / 255;
2538 }
2539#endif /* CONFIG_FILS */
2540
2541 wpa_ie = os_malloc(max_wpa_ie_len);
2542 if (!wpa_ie) {
2543 wpa_printf(MSG_ERROR,
2544 "Failed to allocate connect IE buffer for %lu bytes",
2545 (unsigned long) max_wpa_ie_len);
2546 return NULL;
2547 }
2548
2549 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
2550 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
2551 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
2552 int try_opportunistic;
2553 const u8 *cache_id = NULL;
2554
2555 try_opportunistic = (ssid->proactive_key_caching < 0 ?
2556 wpa_s->conf->okc :
2557 ssid->proactive_key_caching) &&
2558 (ssid->proto & WPA_PROTO_RSN);
2559#ifdef CONFIG_FILS
2560 if (wpa_key_mgmt_fils(ssid->key_mgmt))
2561 cache_id = wpa_bss_get_fils_cache_id(bss);
2562#endif /* CONFIG_FILS */
2563 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
2564 ssid, try_opportunistic,
Hai Shalom5f92bc92019-04-18 11:54:11 -07002565 cache_id, 0) == 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002566 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Hai Shalom5f92bc92019-04-18 11:54:11 -07002567#ifdef CONFIG_SAE
2568 sae_pmksa_cached = 1;
2569#endif /* CONFIG_SAE */
2570 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002571 wpa_ie_len = max_wpa_ie_len;
2572 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
2573 wpa_ie, &wpa_ie_len)) {
2574 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
2575 "key management and encryption suites");
2576 os_free(wpa_ie);
2577 return NULL;
2578 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002579#ifdef CONFIG_HS20
2580 } else if (bss && wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE) &&
2581 (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)) {
2582 /* No PMKSA caching, but otherwise similar to RSN/WPA */
2583 wpa_ie_len = max_wpa_ie_len;
2584 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
2585 wpa_ie, &wpa_ie_len)) {
2586 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
2587 "key management and encryption suites");
2588 os_free(wpa_ie);
2589 return NULL;
2590 }
2591#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002592 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
2593 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
2594 /*
2595 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
2596 * use non-WPA since the scan results did not indicate that the
2597 * AP is using WPA or WPA2.
2598 */
2599 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
2600 wpa_ie_len = 0;
2601 wpa_s->wpa_proto = 0;
2602 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
2603 wpa_ie_len = max_wpa_ie_len;
2604 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
2605 wpa_ie, &wpa_ie_len)) {
2606 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
2607 "key management and encryption suites (no "
2608 "scan results)");
2609 os_free(wpa_ie);
2610 return NULL;
2611 }
2612#ifdef CONFIG_WPS
2613 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
2614 struct wpabuf *wps_ie;
2615 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
2616 if (wps_ie && wpabuf_len(wps_ie) <= max_wpa_ie_len) {
2617 wpa_ie_len = wpabuf_len(wps_ie);
2618 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
2619 } else
2620 wpa_ie_len = 0;
2621 wpabuf_free(wps_ie);
2622 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
2623 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
2624 params->wps = WPS_MODE_PRIVACY;
2625 else
2626 params->wps = WPS_MODE_OPEN;
2627 wpa_s->wpa_proto = 0;
2628#endif /* CONFIG_WPS */
2629 } else {
2630 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
2631 wpa_ie_len = 0;
2632 wpa_s->wpa_proto = 0;
2633 }
2634
2635#ifdef IEEE8021X_EAPOL
2636 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2637 if (ssid->leap) {
2638 if (ssid->non_leap == 0)
2639 algs = WPA_AUTH_ALG_LEAP;
2640 else
2641 algs |= WPA_AUTH_ALG_LEAP;
2642 }
2643 }
2644
2645#ifdef CONFIG_FILS
2646 /* Clear FILS association */
2647 wpa_sm_set_reset_fils_completed(wpa_s->wpa, 0);
2648
2649 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD) &&
2650 ssid->eap.erp && wpa_key_mgmt_fils(wpa_s->key_mgmt) &&
2651 eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap, &username,
2652 &username_len, &realm, &realm_len,
Hai Shalomce48b4a2018-09-05 11:41:35 -07002653 &next_seq_num, &rrk, &rrk_len) == 0 &&
2654 (!wpa_s->last_con_fail_realm ||
2655 wpa_s->last_con_fail_realm_len != realm_len ||
2656 os_memcmp(wpa_s->last_con_fail_realm, realm, realm_len) != 0)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002657 algs = WPA_AUTH_ALG_FILS;
2658 params->fils_erp_username = username;
2659 params->fils_erp_username_len = username_len;
2660 params->fils_erp_realm = realm;
2661 params->fils_erp_realm_len = realm_len;
2662 params->fils_erp_next_seq_num = next_seq_num;
2663 params->fils_erp_rrk = rrk;
2664 params->fils_erp_rrk_len = rrk_len;
2665
2666 if (mask)
2667 *mask |= WPA_DRV_UPDATE_FILS_ERP_INFO;
2668 }
2669#endif /* CONFIG_FILS */
2670#endif /* IEEE8021X_EAPOL */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002671#ifdef CONFIG_SAE
2672 if (wpa_s->key_mgmt & (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE))
2673 algs = WPA_AUTH_ALG_SAE;
2674#endif /* CONFIG_SAE */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002675
2676 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
2677 if (ssid->auth_alg) {
2678 algs = ssid->auth_alg;
2679 wpa_dbg(wpa_s, MSG_DEBUG,
2680 "Overriding auth_alg selection: 0x%x", algs);
2681 }
2682
Hai Shalom5f92bc92019-04-18 11:54:11 -07002683#ifdef CONFIG_SAE
2684 if (sae_pmksa_cached && algs == WPA_AUTH_ALG_SAE) {
2685 wpa_dbg(wpa_s, MSG_DEBUG,
2686 "SAE: Use WPA_AUTH_ALG_OPEN for PMKSA caching attempt");
2687 algs = WPA_AUTH_ALG_OPEN;
2688 }
2689#endif /* CONFIG_SAE */
2690
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002691#ifdef CONFIG_P2P
2692 if (wpa_s->global->p2p) {
2693 u8 *pos;
2694 size_t len;
2695 int res;
2696 pos = wpa_ie + wpa_ie_len;
2697 len = max_wpa_ie_len - wpa_ie_len;
2698 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
2699 ssid->p2p_group);
2700 if (res >= 0)
2701 wpa_ie_len += res;
2702 }
2703
2704 wpa_s->cross_connect_disallowed = 0;
2705 if (bss) {
2706 struct wpabuf *p2p;
2707 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
2708 if (p2p) {
2709 wpa_s->cross_connect_disallowed =
2710 p2p_get_cross_connect_disallowed(p2p);
2711 wpabuf_free(p2p);
2712 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
2713 "connection",
2714 wpa_s->cross_connect_disallowed ?
2715 "disallows" : "allows");
2716 }
2717 }
2718
2719 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
2720#endif /* CONFIG_P2P */
2721
2722 if (bss) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002723 wpa_ie_len += wpas_supp_op_class_ie(wpa_s, ssid, bss->freq,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002724 wpa_ie + wpa_ie_len,
2725 max_wpa_ie_len -
2726 wpa_ie_len);
2727 }
2728
2729 /*
2730 * Workaround: Add Extended Capabilities element only if the AP
2731 * included this element in Beacon/Probe Response frames. Some older
2732 * APs seem to have interoperability issues if this element is
2733 * included, so while the standard may require us to include the
2734 * element in all cases, it is justifiable to skip it to avoid
2735 * interoperability issues.
2736 */
2737 if (ssid->p2p_group)
2738 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
2739 else
2740 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
2741
2742 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
2743 u8 ext_capab[18];
2744 int ext_capab_len;
2745 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
2746 sizeof(ext_capab));
2747 if (ext_capab_len > 0 &&
2748 wpa_ie_len + ext_capab_len <= max_wpa_ie_len) {
2749 u8 *pos = wpa_ie;
2750 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
2751 pos += 2 + pos[1];
2752 os_memmove(pos + ext_capab_len, pos,
2753 wpa_ie_len - (pos - wpa_ie));
2754 wpa_ie_len += ext_capab_len;
2755 os_memcpy(pos, ext_capab, ext_capab_len);
2756 }
2757 }
2758
2759#ifdef CONFIG_HS20
2760 if (is_hs20_network(wpa_s, ssid, bss)) {
2761 struct wpabuf *hs20;
2762
Roshan Pius3a1667e2018-07-03 15:17:14 -07002763 hs20 = wpabuf_alloc(20 + MAX_ROAMING_CONS_OI_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002764 if (hs20) {
2765 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
2766 size_t len;
2767
Hai Shalom74f70d42019-02-11 14:42:39 -08002768 wpas_hs20_add_indication(hs20, pps_mo_id,
2769 get_hs20_version(bss));
Roshan Pius3a1667e2018-07-03 15:17:14 -07002770 wpas_hs20_add_roam_cons_sel(hs20, ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002771 len = max_wpa_ie_len - wpa_ie_len;
2772 if (wpabuf_len(hs20) <= len) {
2773 os_memcpy(wpa_ie + wpa_ie_len,
2774 wpabuf_head(hs20), wpabuf_len(hs20));
2775 wpa_ie_len += wpabuf_len(hs20);
2776 }
2777 wpabuf_free(hs20);
2778
2779 hs20_configure_frame_filters(wpa_s);
2780 }
2781 }
2782#endif /* CONFIG_HS20 */
2783
2784 if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
2785 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
2786 size_t len;
2787
2788 len = max_wpa_ie_len - wpa_ie_len;
2789 if (wpabuf_len(buf) <= len) {
2790 os_memcpy(wpa_ie + wpa_ie_len,
2791 wpabuf_head(buf), wpabuf_len(buf));
2792 wpa_ie_len += wpabuf_len(buf);
2793 }
2794 }
2795
2796#ifdef CONFIG_FST
2797 if (wpa_s->fst_ies) {
2798 int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
2799
2800 if (wpa_ie_len + fst_ies_len <= max_wpa_ie_len) {
2801 os_memcpy(wpa_ie + wpa_ie_len,
2802 wpabuf_head(wpa_s->fst_ies), fst_ies_len);
2803 wpa_ie_len += fst_ies_len;
2804 }
2805 }
2806#endif /* CONFIG_FST */
2807
2808#ifdef CONFIG_MBO
Hai Shalomce48b4a2018-09-05 11:41:35 -07002809 mbo_ie = bss ? wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE) : NULL;
2810 if (mbo_ie) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002811 int len;
2812
2813 len = wpas_mbo_ie(wpa_s, wpa_ie + wpa_ie_len,
Hai Shalomce48b4a2018-09-05 11:41:35 -07002814 max_wpa_ie_len - wpa_ie_len,
2815 !!mbo_attr_from_mbo_ie(mbo_ie,
2816 OCE_ATTR_ID_CAPA_IND));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002817 if (len >= 0)
2818 wpa_ie_len += len;
2819 }
2820#endif /* CONFIG_MBO */
2821
2822#ifdef CONFIG_FILS
2823 if (algs == WPA_AUTH_ALG_FILS) {
2824 size_t len;
2825
2826 len = wpas_add_fils_hlp_req(wpa_s, wpa_ie + wpa_ie_len,
2827 max_wpa_ie_len - wpa_ie_len);
2828 wpa_ie_len += len;
2829 }
2830#endif /* CONFIG_FILS */
2831
2832#ifdef CONFIG_OWE
Roshan Pius3a1667e2018-07-03 15:17:14 -07002833#ifdef CONFIG_TESTING_OPTIONS
2834 if (get_ie_ext(wpa_ie, wpa_ie_len, WLAN_EID_EXT_OWE_DH_PARAM)) {
2835 wpa_printf(MSG_INFO, "TESTING: Override OWE DH element");
2836 } else
2837#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002838 if (algs == WPA_AUTH_ALG_OPEN &&
2839 ssid->key_mgmt == WPA_KEY_MGMT_OWE) {
2840 struct wpabuf *owe_ie;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002841 u16 group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002842
Roshan Pius3a1667e2018-07-03 15:17:14 -07002843 if (ssid->owe_group) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002844 group = ssid->owe_group;
Hai Shalom74f70d42019-02-11 14:42:39 -08002845 } else if (wpa_s->assoc_status_code ==
2846 WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002847 if (wpa_s->last_owe_group == 19)
2848 group = 20;
2849 else if (wpa_s->last_owe_group == 20)
2850 group = 21;
2851 else
2852 group = OWE_DH_GROUP;
Hai Shalom74f70d42019-02-11 14:42:39 -08002853 } else {
2854 group = OWE_DH_GROUP;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002855 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002856
Roshan Pius3a1667e2018-07-03 15:17:14 -07002857 wpa_s->last_owe_group = group;
2858 wpa_printf(MSG_DEBUG, "OWE: Try to use group %u", group);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002859 owe_ie = owe_build_assoc_req(wpa_s->wpa, group);
2860 if (owe_ie &&
2861 wpabuf_len(owe_ie) <= max_wpa_ie_len - wpa_ie_len) {
2862 os_memcpy(wpa_ie + wpa_ie_len,
2863 wpabuf_head(owe_ie), wpabuf_len(owe_ie));
2864 wpa_ie_len += wpabuf_len(owe_ie);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002865 }
Hai Shalom021b0b52019-04-10 11:17:58 -07002866 wpabuf_free(owe_ie);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002867 }
2868#endif /* CONFIG_OWE */
2869
Hai Shalom021b0b52019-04-10 11:17:58 -07002870#ifdef CONFIG_DPP2
2871 if (wpa_sm_get_key_mgmt(wpa_s->wpa) == WPA_KEY_MGMT_DPP &&
2872 ssid->dpp_netaccesskey) {
2873 dpp_pfs_free(wpa_s->dpp_pfs);
2874 wpa_s->dpp_pfs = dpp_pfs_init(ssid->dpp_netaccesskey,
2875 ssid->dpp_netaccesskey_len);
2876 if (!wpa_s->dpp_pfs) {
2877 wpa_printf(MSG_DEBUG, "DPP: Could not initialize PFS");
2878 /* Try to continue without PFS */
2879 goto pfs_fail;
2880 }
2881 if (wpabuf_len(wpa_s->dpp_pfs->ie) <=
2882 max_wpa_ie_len - wpa_ie_len) {
2883 os_memcpy(wpa_ie + wpa_ie_len,
2884 wpabuf_head(wpa_s->dpp_pfs->ie),
2885 wpabuf_len(wpa_s->dpp_pfs->ie));
2886 wpa_ie_len += wpabuf_len(wpa_s->dpp_pfs->ie);
2887 }
2888 }
2889pfs_fail:
2890#endif /* CONFIG_DPP2 */
2891
Roshan Pius3a1667e2018-07-03 15:17:14 -07002892#ifdef CONFIG_IEEE80211R
2893 /*
2894 * Add MDIE under these conditions: the network profile allows FT,
2895 * the AP supports FT, and the mobility domain ID matches.
2896 */
2897 if (bss && wpa_key_mgmt_ft(wpa_sm_get_key_mgmt(wpa_s->wpa))) {
2898 const u8 *mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
2899
2900 if (mdie && mdie[1] >= MOBILITY_DOMAIN_ID_LEN) {
2901 size_t len = 0;
2902 const u8 *md = mdie + 2;
2903 const u8 *wpa_md = wpa_sm_get_ft_md(wpa_s->wpa);
2904
2905 if (os_memcmp(md, wpa_md,
2906 MOBILITY_DOMAIN_ID_LEN) == 0) {
2907 /* Add mobility domain IE */
2908 len = wpa_ft_add_mdie(
2909 wpa_s->wpa, wpa_ie + wpa_ie_len,
2910 max_wpa_ie_len - wpa_ie_len, mdie);
2911 wpa_ie_len += len;
2912 }
2913#ifdef CONFIG_SME
2914 if (len > 0 && wpa_s->sme.ft_used &&
2915 wpa_sm_has_ptk(wpa_s->wpa)) {
2916 wpa_dbg(wpa_s, MSG_DEBUG,
2917 "SME: Trying to use FT over-the-air");
2918 algs |= WPA_AUTH_ALG_FT;
2919 }
2920#endif /* CONFIG_SME */
2921 }
2922 }
2923#endif /* CONFIG_IEEE80211R */
2924
Hai Shalom74f70d42019-02-11 14:42:39 -08002925 if (ssid->multi_ap_backhaul_sta) {
2926 size_t multi_ap_ie_len;
2927
2928 multi_ap_ie_len = add_multi_ap_ie(wpa_ie + wpa_ie_len,
2929 max_wpa_ie_len - wpa_ie_len,
2930 MULTI_AP_BACKHAUL_STA);
2931 if (multi_ap_ie_len == 0) {
2932 wpa_printf(MSG_ERROR,
2933 "Multi-AP: Failed to build Multi-AP IE");
2934 os_free(wpa_ie);
2935 return NULL;
2936 }
2937 wpa_ie_len += multi_ap_ie_len;
2938 }
2939
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002940 params->wpa_ie = wpa_ie;
2941 params->wpa_ie_len = wpa_ie_len;
2942 params->auth_alg = algs;
2943 if (mask)
2944 *mask |= WPA_DRV_UPDATE_ASSOC_IES | WPA_DRV_UPDATE_AUTH_TYPE;
2945
2946 return wpa_ie;
2947}
2948
2949
2950#if defined(CONFIG_FILS) && defined(IEEE8021X_EAPOL)
2951static void wpas_update_fils_connect_params(struct wpa_supplicant *wpa_s)
2952{
2953 struct wpa_driver_associate_params params;
2954 enum wpa_drv_update_connect_params_mask mask = 0;
2955 u8 *wpa_ie;
2956
2957 if (wpa_s->auth_alg != WPA_AUTH_ALG_OPEN)
2958 return; /* nothing to do */
2959
2960 os_memset(&params, 0, sizeof(params));
2961 wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
2962 wpa_s->current_ssid, &params, &mask);
2963 if (!wpa_ie)
2964 return;
2965
2966 if (params.auth_alg != WPA_AUTH_ALG_FILS) {
2967 os_free(wpa_ie);
2968 return;
2969 }
2970
2971 wpa_s->auth_alg = params.auth_alg;
2972 wpa_drv_update_connect_params(wpa_s, &params, mask);
2973 os_free(wpa_ie);
2974}
2975#endif /* CONFIG_FILS && IEEE8021X_EAPOL */
2976
2977
Hai Shalom021b0b52019-04-10 11:17:58 -07002978#ifdef CONFIG_MBO
2979void wpas_update_mbo_connect_params(struct wpa_supplicant *wpa_s)
2980{
2981 struct wpa_driver_associate_params params;
2982 u8 *wpa_ie;
2983
2984 /*
2985 * Update MBO connect params only in case of change of MBO attributes
2986 * when connected, if the AP support MBO.
2987 */
2988
2989 if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_ssid ||
2990 !wpa_s->current_bss ||
2991 !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
2992 return;
2993
2994 os_memset(&params, 0, sizeof(params));
2995 wpa_ie = wpas_populate_assoc_ies(wpa_s, wpa_s->current_bss,
2996 wpa_s->current_ssid, &params, NULL);
2997 if (!wpa_ie)
2998 return;
2999
3000 wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_ASSOC_IES);
3001 os_free(wpa_ie);
3002}
3003#endif /* CONFIG_MBO */
3004
3005
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003006static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
3007{
3008 struct wpa_connect_work *cwork = work->ctx;
3009 struct wpa_bss *bss = cwork->bss;
3010 struct wpa_ssid *ssid = cwork->ssid;
3011 struct wpa_supplicant *wpa_s = work->wpa_s;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003012 u8 *wpa_ie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003013 int use_crypt, ret, i, bssid_changed;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003014 unsigned int cipher_pairwise, cipher_group, cipher_group_mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003015 struct wpa_driver_associate_params params;
3016 int wep_keys_set = 0;
3017 int assoc_failed = 0;
3018 struct wpa_ssid *old_ssid;
Dmitry Shmidte4663042016-04-04 10:07:49 -07003019 u8 prev_bssid[ETH_ALEN];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003020#ifdef CONFIG_HT_OVERRIDES
3021 struct ieee80211_ht_capabilities htcaps;
3022 struct ieee80211_ht_capabilities htcaps_mask;
3023#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003024#ifdef CONFIG_VHT_OVERRIDES
3025 struct ieee80211_vht_capabilities vhtcaps;
3026 struct ieee80211_vht_capabilities vhtcaps_mask;
3027#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003028
3029 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003030 if (work->started) {
3031 wpa_s->connect_work = NULL;
3032
3033 /* cancel possible auth. timeout */
3034 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
3035 NULL);
3036 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003037 wpas_connect_work_free(cwork);
3038 return;
3039 }
3040
3041 wpa_s->connect_work = work;
3042
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003043 if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
3044 wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003045 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
3046 wpas_connect_work_done(wpa_s);
3047 return;
3048 }
3049
Dmitry Shmidte4663042016-04-04 10:07:49 -07003050 os_memcpy(prev_bssid, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 os_memset(&params, 0, sizeof(params));
3052 wpa_s->reassociate = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003053 wpa_s->eap_expected_failure = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003054 if (bss &&
3055 (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056#ifdef CONFIG_IEEE80211R
3057 const u8 *ie, *md = NULL;
3058#endif /* CONFIG_IEEE80211R */
3059 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
3060 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
3061 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
3062 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
3063 os_memset(wpa_s->bssid, 0, ETH_ALEN);
3064 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
3065 if (bssid_changed)
3066 wpas_notify_bssid_changed(wpa_s);
3067#ifdef CONFIG_IEEE80211R
3068 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
3069 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
3070 md = ie + 2;
3071 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
3072 if (md) {
3073 /* Prepare for the next transition */
3074 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
3075 }
3076#endif /* CONFIG_IEEE80211R */
3077#ifdef CONFIG_WPS
3078 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
3079 wpa_s->conf->ap_scan == 2 &&
3080 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
3081 /* Use ap_scan==1 style network selection to find the network
3082 */
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08003083 wpas_connect_work_done(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003084 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085 wpa_s->reassociate = 1;
3086 wpa_supplicant_req_scan(wpa_s, 0, 0);
3087 return;
3088#endif /* CONFIG_WPS */
3089 } else {
3090 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
3091 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003092 if (bss)
3093 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
3094 else
3095 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003097 if (!wpa_s->pno)
3098 wpa_supplicant_cancel_sched_scan(wpa_s);
3099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 wpa_supplicant_cancel_scan(wpa_s);
3101
3102 /* Starting new association, so clear the possibly used WPA IE from the
3103 * previous association. */
3104 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
3105
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003106 wpa_ie = wpas_populate_assoc_ies(wpa_s, bss, ssid, &params, NULL);
3107 if (!wpa_ie) {
3108 wpas_connect_work_done(wpa_s);
3109 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
3113 use_crypt = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003114 cipher_pairwise = wpa_s->pairwise_cipher;
3115 cipher_group = wpa_s->group_cipher;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003116 cipher_group_mgmt = wpa_s->mgmt_group_cipher;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
3118 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3119 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
3120 use_crypt = 0;
3121 if (wpa_set_wep_keys(wpa_s, ssid)) {
3122 use_crypt = 1;
3123 wep_keys_set = 1;
3124 }
3125 }
3126 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
3127 use_crypt = 0;
3128
3129#ifdef IEEE8021X_EAPOL
3130 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3131 if ((ssid->eapol_flags &
3132 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
3133 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
3134 !wep_keys_set) {
3135 use_crypt = 0;
3136 } else {
3137 /* Assume that dynamic WEP-104 keys will be used and
3138 * set cipher suites in order for drivers to expect
3139 * encryption. */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003140 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 }
3142 }
3143#endif /* IEEE8021X_EAPOL */
3144
3145 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
3146 /* Set the key before (and later after) association */
3147 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
3148 }
3149
3150 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
3151 if (bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 params.ssid = bss->ssid;
3153 params.ssid_len = bss->ssid_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003154 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set ||
3155 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003156 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
3157 MACSTR " freq=%u MHz based on scan results "
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003158 "(bssid_set=%d wps=%d)",
Dmitry Shmidt04949592012-07-19 12:16:46 -07003159 MAC2STR(bss->bssid), bss->freq,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003160 ssid->bssid_set,
3161 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003162 params.bssid = bss->bssid;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003163 params.freq.freq = bss->freq;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003164 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003165 params.bssid_hint = bss->bssid;
3166 params.freq_hint = bss->freq;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003167 params.pbss = bss_is_pbss(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003169 if (ssid->bssid_hint_set)
3170 params.bssid_hint = ssid->bssid_hint;
3171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172 params.ssid = ssid->ssid;
3173 params.ssid_len = ssid->ssid_len;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003174 params.pbss = (ssid->pbss != 2) ? ssid->pbss : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003176
3177 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
3178 wpa_s->conf->ap_scan == 2) {
3179 params.bssid = ssid->bssid;
3180 params.fixed_bssid = 1;
3181 }
3182
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003183 /* Initial frequency for IBSS/mesh */
3184 if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003185 ssid->frequency > 0 && params.freq.freq == 0)
3186 ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003187
3188 if (ssid->mode == WPAS_MODE_IBSS) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003189 params.fixed_freq = ssid->fixed_freq;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003190 if (ssid->beacon_int)
3191 params.beacon_int = ssid->beacon_int;
3192 else
3193 params.beacon_int = wpa_s->conf->beacon_int;
3194 }
3195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 params.pairwise_suite = cipher_pairwise;
3197 params.group_suite = cipher_group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003198 params.mgmt_group_suite = cipher_group_mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003199 params.key_mgmt_suite = wpa_s->key_mgmt;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003200 params.wpa_proto = wpa_s->wpa_proto;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003201 wpa_s->auth_alg = params.auth_alg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003202 params.mode = ssid->mode;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003203 params.bg_scan_period = ssid->bg_scan_period;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204 for (i = 0; i < NUM_WEP_KEYS; i++) {
3205 if (ssid->wep_key_len[i])
3206 params.wep_key[i] = ssid->wep_key[i];
3207 params.wep_key_len[i] = ssid->wep_key_len[i];
3208 }
3209 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
3210
Hai Shalom74f70d42019-02-11 14:42:39 -08003211 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003212 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
3213 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 params.passphrase = ssid->passphrase;
3215 if (ssid->psk_set)
3216 params.psk = ssid->psk;
3217 }
3218
Hai Shalom74f70d42019-02-11 14:42:39 -08003219 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
3220 (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
3221 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
3222 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
3223 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192))
3224 params.req_key_mgmt_offload = 1;
3225
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003226 if (wpa_s->conf->key_mgmt_offload) {
3227 if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
3228 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003229 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
3230 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003231 params.req_key_mgmt_offload =
3232 ssid->proactive_key_caching < 0 ?
3233 wpa_s->conf->okc : ssid->proactive_key_caching;
3234 else
3235 params.req_key_mgmt_offload = 1;
3236
3237 if ((params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
3238 params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
3239 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
3240 ssid->psk_set)
3241 params.psk = ssid->psk;
3242 }
3243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 params.drop_unencrypted = use_crypt;
3245
3246#ifdef CONFIG_IEEE80211W
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003247 params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003248 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3250 struct wpa_ie_data ie;
3251 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
3252 ie.capabilities &
3253 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
3254 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
3255 "MFP: require MFP");
3256 params.mgmt_frame_protection =
3257 MGMT_FRAME_PROTECTION_REQUIRED;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003258#ifdef CONFIG_OWE
3259 } else if (!rsn && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
3260 !ssid->owe_only) {
3261 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
3262#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 }
3264 }
3265#endif /* CONFIG_IEEE80211W */
3266
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003267 params.p2p = ssid->p2p_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003269 if (wpa_s->p2pdev->set_sta_uapsd)
3270 params.uapsd = wpa_s->p2pdev->sta_uapsd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 else
3272 params.uapsd = -1;
3273
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003274#ifdef CONFIG_HT_OVERRIDES
3275 os_memset(&htcaps, 0, sizeof(htcaps));
3276 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
3277 params.htcaps = (u8 *) &htcaps;
3278 params.htcaps_mask = (u8 *) &htcaps_mask;
3279 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
3280#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003281#ifdef CONFIG_VHT_OVERRIDES
3282 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
3283 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
3284 params.vhtcaps = &vhtcaps;
3285 params.vhtcaps_mask = &vhtcaps_mask;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003286 wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003287#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003288
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003289#ifdef CONFIG_P2P
3290 /*
3291 * If multi-channel concurrency is not supported, check for any
3292 * frequency conflict. In case of any frequency conflict, remove the
3293 * least prioritized connection.
3294 */
3295 if (wpa_s->num_multichan_concurrent < 2) {
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003296 int freq, num;
3297 num = get_shared_radio_freqs(wpa_s, &freq, 1);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003298 if (num > 0 && freq > 0 && freq != params.freq.freq) {
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003299 wpa_printf(MSG_DEBUG,
3300 "Assoc conflicting freq found (%d != %d)",
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003301 freq, params.freq.freq);
3302 if (wpas_p2p_handle_frequency_conflicts(
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08003303 wpa_s, params.freq.freq, ssid) < 0) {
3304 wpas_connect_work_done(wpa_s);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003305 os_free(wpa_ie);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003306 return;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08003307 }
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003308 }
3309 }
3310#endif /* CONFIG_P2P */
3311
Dmitry Shmidte4663042016-04-04 10:07:49 -07003312 if (wpa_s->reassoc_same_ess && !is_zero_ether_addr(prev_bssid) &&
3313 wpa_s->current_ssid)
3314 params.prev_bssid = prev_bssid;
3315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003316 ret = wpa_drv_associate(wpa_s, &params);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003317 os_free(wpa_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 if (ret < 0) {
3319 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
3320 "failed");
3321 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
3322 /*
3323 * The driver is known to mean what is saying, so we
3324 * can stop right here; the association will not
3325 * succeed.
3326 */
3327 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003328 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
3330 return;
3331 }
3332 /* try to continue anyway; new association will be tried again
3333 * after timeout */
3334 assoc_failed = 1;
3335 }
3336
3337 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
3338 /* Set the key after the association just in case association
3339 * cleared the previously configured key. */
3340 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
3341 /* No need to timeout authentication since there is no key
3342 * management. */
3343 wpa_supplicant_cancel_auth_timeout(wpa_s);
3344 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3345#ifdef CONFIG_IBSS_RSN
3346 } else if (ssid->mode == WPAS_MODE_IBSS &&
3347 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
3348 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
3349 /*
3350 * RSN IBSS authentication is per-STA and we can disable the
3351 * per-BSSID authentication.
3352 */
3353 wpa_supplicant_cancel_auth_timeout(wpa_s);
3354#endif /* CONFIG_IBSS_RSN */
3355 } else {
3356 /* Timeout for IEEE 802.11 authentication and association */
3357 int timeout = 60;
3358
3359 if (assoc_failed) {
3360 /* give IBSS a bit more time */
3361 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
3362 } else if (wpa_s->conf->ap_scan == 1) {
3363 /* give IBSS a bit more time */
3364 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
3365 }
3366 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
3367 }
3368
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003369 if (wep_keys_set &&
3370 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371 /* Set static WEP keys again */
3372 wpa_set_wep_keys(wpa_s, ssid);
3373 }
3374
3375 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
3376 /*
3377 * Do not allow EAP session resumption between different
3378 * network configurations.
3379 */
3380 eapol_sm_invalidate_cached_session(wpa_s->eapol);
3381 }
3382 old_ssid = wpa_s->current_ssid;
3383 wpa_s->current_ssid = ssid;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003384
3385 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07003386 wpa_s->current_bss = bss;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003387#ifdef CONFIG_HS20
3388 hs20_configure_frame_filters(wpa_s);
3389#endif /* CONFIG_HS20 */
3390 }
3391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
3393 wpa_supplicant_initiate_eapol(wpa_s);
3394 if (old_ssid != wpa_s->current_ssid)
3395 wpas_notify_network_changed(wpa_s);
3396}
3397
3398
3399static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
3400 const u8 *addr)
3401{
3402 struct wpa_ssid *old_ssid;
3403
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003404 wpas_connect_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 wpa_clear_keys(wpa_s, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406 old_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003407 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 wpa_sm_set_config(wpa_s->wpa, NULL);
3409 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
3410 if (old_ssid != wpa_s->current_ssid)
3411 wpas_notify_network_changed(wpa_s);
3412 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
3413}
3414
3415
3416/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
3418 * @wpa_s: Pointer to wpa_supplicant data
3419 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
3420 *
3421 * This function is used to request %wpa_supplicant to deauthenticate from the
3422 * current AP.
3423 */
3424void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
3425 int reason_code)
3426{
3427 u8 *addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003428 union wpa_event_data event;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003429 int zero_addr = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003431 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
3432 " pending_bssid=" MACSTR " reason=%d state=%s",
3433 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
3434 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
3435
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003436 if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
3437 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
3438 wpa_s->wpa_state == WPA_ASSOCIATING))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003439 addr = wpa_s->pending_bssid;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003440 else if (!is_zero_ether_addr(wpa_s->bssid))
3441 addr = wpa_s->bssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003442 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
3443 /*
3444 * When using driver-based BSS selection, we may not know the
3445 * BSSID with which we are currently trying to associate. We
3446 * need to notify the driver of this disconnection even in such
3447 * a case, so use the all zeros address here.
3448 */
3449 addr = wpa_s->bssid;
3450 zero_addr = 1;
3451 }
3452
Hai Shalom74f70d42019-02-11 14:42:39 -08003453 if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
3454 wpa_s->enabled_4addr_mode = 0;
3455
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003456#ifdef CONFIG_TDLS
3457 wpa_tdls_teardown_peers(wpa_s->wpa);
3458#endif /* CONFIG_TDLS */
3459
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003460#ifdef CONFIG_MESH
3461 if (wpa_s->ifmsh) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003462 struct mesh_conf *mconf;
3463
3464 mconf = wpa_s->ifmsh->mconf;
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003465 wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
3466 wpa_s->ifname);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003467 wpas_notify_mesh_group_removed(wpa_s, mconf->meshid,
3468 mconf->meshid_len, reason_code);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003469 wpa_supplicant_leave_mesh(wpa_s);
3470 }
3471#endif /* CONFIG_MESH */
3472
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003473 if (addr) {
3474 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475 os_memset(&event, 0, sizeof(event));
3476 event.deauth_info.reason_code = (u16) reason_code;
3477 event.deauth_info.locally_generated = 1;
3478 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003479 if (zero_addr)
3480 addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003481 }
3482
3483 wpa_supplicant_clear_connection(wpa_s, addr);
3484}
3485
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003486static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
3487 struct wpa_ssid *ssid)
3488{
3489 if (!ssid || !ssid->disabled || ssid->disabled == 2)
3490 return;
3491
3492 ssid->disabled = 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003493 ssid->owe_transition_bss_select_count = 0;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003494 wpas_clear_temp_disabled(wpa_s, ssid, 1);
3495 wpas_notify_network_enabled_changed(wpa_s, ssid);
3496
3497 /*
3498 * Try to reassociate since there is no current configuration and a new
3499 * network was made available.
3500 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003501 if (!wpa_s->current_ssid && !wpa_s->disconnected)
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003502 wpa_s->reassociate = 1;
3503}
3504
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003505
Roshan Pius950bec92016-07-19 09:49:24 -07003506/**
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003507 * wpa_supplicant_add_network - Add a new network
Roshan Pius950bec92016-07-19 09:49:24 -07003508 * @wpa_s: wpa_supplicant structure for a network interface
3509 * Returns: The new network configuration or %NULL if operation failed
3510 *
3511 * This function performs the following operations:
3512 * 1. Adds a new network.
3513 * 2. Send network addition notification.
3514 * 3. Marks the network disabled.
3515 * 4. Set network default parameters.
3516 */
3517struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
3518{
3519 struct wpa_ssid *ssid;
3520
3521 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003522 if (!ssid)
Roshan Pius950bec92016-07-19 09:49:24 -07003523 return NULL;
Roshan Pius950bec92016-07-19 09:49:24 -07003524 wpas_notify_network_added(wpa_s, ssid);
3525 ssid->disabled = 1;
3526 wpa_config_set_network_defaults(ssid);
3527
3528 return ssid;
3529}
3530
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003531
Roshan Pius950bec92016-07-19 09:49:24 -07003532/**
3533 * wpa_supplicant_remove_network - Remove a configured network based on id
3534 * @wpa_s: wpa_supplicant structure for a network interface
3535 * @id: Unique network id to search for
3536 * Returns: 0 on success, or -1 if the network was not found, -2 if the network
3537 * could not be removed
3538 *
3539 * This function performs the following operations:
3540 * 1. Removes the network.
3541 * 2. Send network removal notification.
3542 * 3. Update internal state machines.
3543 * 4. Stop any running sched scans.
3544 */
3545int wpa_supplicant_remove_network(struct wpa_supplicant *wpa_s, int id)
3546{
3547 struct wpa_ssid *ssid;
3548 int was_disabled;
3549
3550 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003551 if (!ssid)
Roshan Pius950bec92016-07-19 09:49:24 -07003552 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003553 wpas_notify_network_removed(wpa_s, ssid);
Roshan Pius950bec92016-07-19 09:49:24 -07003554
3555 if (wpa_s->last_ssid == ssid)
3556 wpa_s->last_ssid = NULL;
3557
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003558 if (ssid == wpa_s->current_ssid || !wpa_s->current_ssid) {
Roshan Pius950bec92016-07-19 09:49:24 -07003559#ifdef CONFIG_SME
3560 wpa_s->sme.prev_bssid_set = 0;
3561#endif /* CONFIG_SME */
3562 /*
3563 * Invalidate the EAP session cache if the current or
3564 * previously used network is removed.
3565 */
3566 eapol_sm_invalidate_cached_session(wpa_s->eapol);
3567 }
3568
3569 if (ssid == wpa_s->current_ssid) {
3570 wpa_sm_set_config(wpa_s->wpa, NULL);
3571 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
3572
3573 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3574 wpa_s->own_disconnect_req = 1;
3575 wpa_supplicant_deauthenticate(wpa_s,
3576 WLAN_REASON_DEAUTH_LEAVING);
3577 }
3578
3579 was_disabled = ssid->disabled;
3580
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003581 if (wpa_config_remove_network(wpa_s->conf, id) < 0)
Roshan Pius950bec92016-07-19 09:49:24 -07003582 return -2;
Roshan Pius950bec92016-07-19 09:49:24 -07003583
3584 if (!was_disabled && wpa_s->sched_scanning) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003585 wpa_printf(MSG_DEBUG,
3586 "Stop ongoing sched_scan to remove network from filters");
Roshan Pius950bec92016-07-19 09:49:24 -07003587 wpa_supplicant_cancel_sched_scan(wpa_s);
3588 wpa_supplicant_req_scan(wpa_s, 0, 0);
3589 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003590
Roshan Pius950bec92016-07-19 09:49:24 -07003591 return 0;
3592}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595/**
3596 * wpa_supplicant_enable_network - Mark a configured network as enabled
3597 * @wpa_s: wpa_supplicant structure for a network interface
3598 * @ssid: wpa_ssid structure for a configured network or %NULL
3599 *
3600 * Enables the specified network or all networks if no network specified.
3601 */
3602void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
3603 struct wpa_ssid *ssid)
3604{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003606 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3607 wpa_supplicant_enable_one_network(wpa_s, ssid);
3608 } else
3609 wpa_supplicant_enable_one_network(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003611 if (wpa_s->reassociate && !wpa_s->disconnected &&
3612 (!wpa_s->current_ssid ||
3613 wpa_s->wpa_state == WPA_DISCONNECTED ||
3614 wpa_s->wpa_state == WPA_SCANNING)) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003615 if (wpa_s->sched_scanning) {
3616 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
3617 "new network to scan filters");
3618 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 }
3620
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003621 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
3622 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003623 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 }
3626}
3627
3628
3629/**
3630 * wpa_supplicant_disable_network - Mark a configured network as disabled
3631 * @wpa_s: wpa_supplicant structure for a network interface
3632 * @ssid: wpa_ssid structure for a configured network or %NULL
3633 *
3634 * Disables the specified network or all networks if no network specified.
3635 */
3636void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
3637 struct wpa_ssid *ssid)
3638{
3639 struct wpa_ssid *other_ssid;
3640 int was_disabled;
3641
3642 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003643 if (wpa_s->sched_scanning)
3644 wpa_supplicant_cancel_sched_scan(wpa_s);
3645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646 for (other_ssid = wpa_s->conf->ssid; other_ssid;
3647 other_ssid = other_ssid->next) {
3648 was_disabled = other_ssid->disabled;
3649 if (was_disabled == 2)
3650 continue; /* do not change persistent P2P group
3651 * data */
3652
3653 other_ssid->disabled = 1;
3654
3655 if (was_disabled != other_ssid->disabled)
3656 wpas_notify_network_enabled_changed(
3657 wpa_s, other_ssid);
3658 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003659 if (wpa_s->current_ssid) {
3660 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3661 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003662 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003664 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665 } else if (ssid->disabled != 2) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003666 if (ssid == wpa_s->current_ssid) {
3667 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3668 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003669 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003671 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672
3673 was_disabled = ssid->disabled;
3674
3675 ssid->disabled = 1;
3676
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003677 if (was_disabled != ssid->disabled) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003679 if (wpa_s->sched_scanning) {
3680 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
3681 "to remove network from filters");
3682 wpa_supplicant_cancel_sched_scan(wpa_s);
3683 wpa_supplicant_req_scan(wpa_s, 0, 0);
3684 }
3685 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 }
3687}
3688
3689
3690/**
3691 * wpa_supplicant_select_network - Attempt association with a network
3692 * @wpa_s: wpa_supplicant structure for a network interface
3693 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
3694 */
3695void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
3696 struct wpa_ssid *ssid)
3697{
3698
3699 struct wpa_ssid *other_ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003700 int disconnected = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003702 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003703 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3704 wpa_s->own_disconnect_req = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003705 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003706 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003707 disconnected = 1;
3708 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003710 if (ssid)
3711 wpas_clear_temp_disabled(wpa_s, ssid, 1);
3712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713 /*
3714 * Mark all other networks disabled or mark all networks enabled if no
3715 * network specified.
3716 */
3717 for (other_ssid = wpa_s->conf->ssid; other_ssid;
3718 other_ssid = other_ssid->next) {
3719 int was_disabled = other_ssid->disabled;
3720 if (was_disabled == 2)
3721 continue; /* do not change persistent P2P group data */
3722
3723 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724 if (was_disabled && !other_ssid->disabled)
3725 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726
3727 if (was_disabled != other_ssid->disabled)
3728 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
3729 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003730
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003731 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
3732 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003733 /* We are already associated with the selected network */
3734 wpa_printf(MSG_DEBUG, "Already associated with the "
3735 "selected network - do nothing");
3736 return;
3737 }
3738
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003739 if (ssid) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003740 wpa_s->current_ssid = ssid;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003741 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003742 wpa_s->connect_without_scan =
3743 (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003744
3745 /*
3746 * Don't optimize next scan freqs since a new ESS has been
3747 * selected.
3748 */
3749 os_free(wpa_s->next_scan_freqs);
3750 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003751 } else {
3752 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003753 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 wpa_s->disconnected = 0;
3756 wpa_s->reassociate = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003757 wpa_s->last_owe_group = 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003758 if (ssid)
3759 ssid->owe_transition_bss_select_count = 0;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003760
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003761 if (wpa_s->connect_without_scan ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003762 wpa_supplicant_fast_associate(wpa_s) != 1) {
3763 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003764 wpas_scan_reset_sched_scan(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003765 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767
3768 if (ssid)
3769 wpas_notify_network_selected(wpa_s, ssid);
3770}
3771
3772
3773/**
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003774 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
3775 * @wpa_s: wpa_supplicant structure for a network interface
3776 * @pkcs11_engine_path: PKCS #11 engine path or NULL
3777 * @pkcs11_module_path: PKCS #11 module path or NULL
3778 * Returns: 0 on success; -1 on failure
3779 *
3780 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
3781 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
3782 * module path fails the paths will be reset to the default value (NULL).
3783 */
3784int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
3785 const char *pkcs11_engine_path,
3786 const char *pkcs11_module_path)
3787{
3788 char *pkcs11_engine_path_copy = NULL;
3789 char *pkcs11_module_path_copy = NULL;
3790
3791 if (pkcs11_engine_path != NULL) {
3792 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
3793 if (pkcs11_engine_path_copy == NULL)
3794 return -1;
3795 }
3796 if (pkcs11_module_path != NULL) {
3797 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
Dmitry Shmidt97672262014-02-03 13:02:54 -08003798 if (pkcs11_module_path_copy == NULL) {
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003799 os_free(pkcs11_engine_path_copy);
3800 return -1;
3801 }
3802 }
3803
3804 os_free(wpa_s->conf->pkcs11_engine_path);
3805 os_free(wpa_s->conf->pkcs11_module_path);
3806 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
3807 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
3808
3809 wpa_sm_set_eapol(wpa_s->wpa, NULL);
3810 eapol_sm_deinit(wpa_s->eapol);
3811 wpa_s->eapol = NULL;
3812 if (wpa_supplicant_init_eapol(wpa_s)) {
3813 /* Error -> Reset paths to the default value (NULL) once. */
3814 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
3815 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
3816 NULL);
3817
3818 return -1;
3819 }
3820 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3821
3822 return 0;
3823}
3824
3825
3826/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
3828 * @wpa_s: wpa_supplicant structure for a network interface
3829 * @ap_scan: AP scan mode
3830 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
3831 *
3832 */
3833int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
3834{
3835
3836 int old_ap_scan;
3837
3838 if (ap_scan < 0 || ap_scan > 2)
3839 return -1;
3840
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003841 if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
3842 wpa_printf(MSG_INFO,
3843 "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
3844 }
3845
Dmitry Shmidt114c3862011-08-16 11:52:06 -07003846#ifdef ANDROID
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003847 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
3848 wpa_s->wpa_state >= WPA_ASSOCIATING &&
3849 wpa_s->wpa_state < WPA_COMPLETED) {
3850 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
3851 "associating", wpa_s->conf->ap_scan, ap_scan);
Dmitry Shmidt114c3862011-08-16 11:52:06 -07003852 return 0;
3853 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003854#endif /* ANDROID */
Dmitry Shmidt114c3862011-08-16 11:52:06 -07003855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 old_ap_scan = wpa_s->conf->ap_scan;
3857 wpa_s->conf->ap_scan = ap_scan;
3858
3859 if (old_ap_scan != wpa_s->conf->ap_scan)
3860 wpas_notify_ap_scan_changed(wpa_s);
3861
3862 return 0;
3863}
3864
3865
3866/**
3867 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
3868 * @wpa_s: wpa_supplicant structure for a network interface
3869 * @expire_age: Expiration age in seconds
3870 * Returns: 0 if succeed or -1 if expire_age has an invalid value
3871 *
3872 */
3873int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
3874 unsigned int bss_expire_age)
3875{
3876 if (bss_expire_age < 10) {
3877 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
3878 bss_expire_age);
3879 return -1;
3880 }
3881 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
3882 bss_expire_age);
3883 wpa_s->conf->bss_expiration_age = bss_expire_age;
3884
3885 return 0;
3886}
3887
3888
3889/**
3890 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
3891 * @wpa_s: wpa_supplicant structure for a network interface
3892 * @expire_count: number of scans after which an unseen BSS is reclaimed
3893 * Returns: 0 if succeed or -1 if expire_count has an invalid value
3894 *
3895 */
3896int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
3897 unsigned int bss_expire_count)
3898{
3899 if (bss_expire_count < 1) {
3900 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
3901 bss_expire_count);
3902 return -1;
3903 }
3904 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
3905 bss_expire_count);
3906 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
3907
3908 return 0;
3909}
3910
3911
3912/**
Dmitry Shmidt04949592012-07-19 12:16:46 -07003913 * wpa_supplicant_set_scan_interval - Set scan interval
3914 * @wpa_s: wpa_supplicant structure for a network interface
3915 * @scan_interval: scan interval in seconds
3916 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
3917 *
3918 */
3919int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
3920 int scan_interval)
3921{
3922 if (scan_interval < 0) {
3923 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
3924 scan_interval);
3925 return -1;
3926 }
3927 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
3928 scan_interval);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003929 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003930
3931 return 0;
3932}
3933
3934
3935/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 * wpa_supplicant_set_debug_params - Set global debug params
3937 * @global: wpa_global structure
3938 * @debug_level: debug level
3939 * @debug_timestamp: determines if show timestamp in debug data
3940 * @debug_show_keys: determines if show keys in debug data
3941 * Returns: 0 if succeed or -1 if debug_level has wrong value
3942 */
3943int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
3944 int debug_timestamp, int debug_show_keys)
3945{
3946
3947 int old_level, old_timestamp, old_show_keys;
3948
3949 /* check for allowed debuglevels */
3950 if (debug_level != MSG_EXCESSIVE &&
3951 debug_level != MSG_MSGDUMP &&
3952 debug_level != MSG_DEBUG &&
3953 debug_level != MSG_INFO &&
3954 debug_level != MSG_WARNING &&
3955 debug_level != MSG_ERROR)
3956 return -1;
3957
3958 old_level = wpa_debug_level;
3959 old_timestamp = wpa_debug_timestamp;
3960 old_show_keys = wpa_debug_show_keys;
3961
3962 wpa_debug_level = debug_level;
3963 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
3964 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
3965
3966 if (wpa_debug_level != old_level)
3967 wpas_notify_debug_level_changed(global);
3968 if (wpa_debug_timestamp != old_timestamp)
3969 wpas_notify_debug_timestamp_changed(global);
3970 if (wpa_debug_show_keys != old_show_keys)
3971 wpas_notify_debug_show_keys_changed(global);
3972
3973 return 0;
3974}
3975
3976
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003977#ifdef CONFIG_OWE
3978static int owe_trans_ssid_match(struct wpa_supplicant *wpa_s, const u8 *bssid,
3979 const u8 *entry_ssid, size_t entry_ssid_len)
3980{
3981 const u8 *owe, *pos, *end;
3982 u8 ssid_len;
3983 struct wpa_bss *bss;
3984
3985 /* Check network profile SSID aganst the SSID in the
3986 * OWE Transition Mode element. */
3987
3988 bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
3989 if (!bss)
3990 return 0;
3991
3992 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
3993 if (!owe)
3994 return 0;
3995
3996 pos = owe + 6;
3997 end = owe + 2 + owe[1];
3998
3999 if (end - pos < ETH_ALEN + 1)
4000 return 0;
4001 pos += ETH_ALEN;
4002 ssid_len = *pos++;
4003 if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
4004 return 0;
4005
4006 return entry_ssid_len == ssid_len &&
4007 os_memcmp(pos, entry_ssid, ssid_len) == 0;
4008}
4009#endif /* CONFIG_OWE */
4010
4011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012/**
4013 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
4014 * @wpa_s: Pointer to wpa_supplicant data
4015 * Returns: A pointer to the current network structure or %NULL on failure
4016 */
4017struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
4018{
4019 struct wpa_ssid *entry;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07004020 u8 ssid[SSID_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 int res;
4022 size_t ssid_len;
4023 u8 bssid[ETH_ALEN];
4024 int wired;
4025
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004026 res = wpa_drv_get_ssid(wpa_s, ssid);
4027 if (res < 0) {
4028 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
4029 "driver");
4030 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004032 ssid_len = res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004034 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
4036 "driver");
4037 return NULL;
4038 }
4039
4040 wired = wpa_s->conf->ap_scan == 0 &&
4041 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
4042
4043 entry = wpa_s->conf->ssid;
4044 while (entry) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004045 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004046 ((ssid_len == entry->ssid_len &&
Hai Shalom021b0b52019-04-10 11:17:58 -07004047 (!entry->ssid ||
4048 os_memcmp(ssid, entry->ssid, ssid_len) == 0)) ||
4049 wired) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050 (!entry->bssid_set ||
4051 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
4052 return entry;
4053#ifdef CONFIG_WPS
Dmitry Shmidt04949592012-07-19 12:16:46 -07004054 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
4056 (entry->ssid == NULL || entry->ssid_len == 0) &&
4057 (!entry->bssid_set ||
4058 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
4059 return entry;
4060#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004061
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004062#ifdef CONFIG_OWE
4063 if (!wpas_network_disabled(wpa_s, entry) &&
4064 owe_trans_ssid_match(wpa_s, bssid, entry->ssid,
4065 entry->ssid_len) &&
4066 (!entry->bssid_set ||
4067 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
4068 return entry;
4069#endif /* CONFIG_OWE */
4070
Dmitry Shmidt04949592012-07-19 12:16:46 -07004071 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004072 entry->ssid_len == 0 &&
4073 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
4074 return entry;
4075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 entry = entry->next;
4077 }
4078
4079 return NULL;
4080}
4081
4082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004083static int select_driver(struct wpa_supplicant *wpa_s, int i)
4084{
4085 struct wpa_global *global = wpa_s->global;
4086
4087 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07004088 global->drv_priv[i] = wpa_drivers[i]->global_init(global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004089 if (global->drv_priv[i] == NULL) {
4090 wpa_printf(MSG_ERROR, "Failed to initialize driver "
4091 "'%s'", wpa_drivers[i]->name);
4092 return -1;
4093 }
4094 }
4095
4096 wpa_s->driver = wpa_drivers[i];
4097 wpa_s->global_drv_priv = global->drv_priv[i];
4098
4099 return 0;
4100}
4101
4102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
4104 const char *name)
4105{
4106 int i;
4107 size_t len;
4108 const char *pos, *driver = name;
4109
4110 if (wpa_s == NULL)
4111 return -1;
4112
4113 if (wpa_drivers[0] == NULL) {
4114 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
4115 "wpa_supplicant");
4116 return -1;
4117 }
4118
4119 if (name == NULL) {
4120 /* default to first driver in the list */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004121 return select_driver(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 }
4123
4124 do {
4125 pos = os_strchr(driver, ',');
4126 if (pos)
4127 len = pos - driver;
4128 else
4129 len = os_strlen(driver);
4130
4131 for (i = 0; wpa_drivers[i]; i++) {
4132 if (os_strlen(wpa_drivers[i]->name) == len &&
4133 os_strncmp(driver, wpa_drivers[i]->name, len) ==
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004134 0) {
4135 /* First driver that succeeds wins */
4136 if (select_driver(wpa_s, i) == 0)
4137 return 0;
4138 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 }
4140
4141 driver = pos + 1;
4142 } while (pos);
4143
4144 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
4145 return -1;
4146}
4147
4148
4149/**
4150 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
4151 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
4152 * with struct wpa_driver_ops::init()
4153 * @src_addr: Source address of the EAPOL frame
4154 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
4155 * @len: Length of the EAPOL data
4156 *
4157 * This function is called for each received EAPOL frame. Most driver
4158 * interfaces rely on more generic OS mechanism for receiving frames through
4159 * l2_packet, but if such a mechanism is not available, the driver wrapper may
4160 * take care of received EAPOL frames and deliver them to the core supplicant
4161 * code by calling this function.
4162 */
4163void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
4164 const u8 *buf, size_t len)
4165{
4166 struct wpa_supplicant *wpa_s = ctx;
4167
4168 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
4169 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
4170
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004171#ifdef CONFIG_TESTING_OPTIONS
4172 if (wpa_s->ignore_auth_resp) {
4173 wpa_printf(MSG_INFO, "RX EAPOL - ignore_auth_resp active!");
4174 return;
4175 }
4176#endif /* CONFIG_TESTING_OPTIONS */
4177
Jouni Malinena05074c2012-12-21 21:35:35 +02004178 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4179 (wpa_s->last_eapol_matches_bssid &&
4180#ifdef CONFIG_AP
4181 !wpa_s->ap_iface &&
4182#endif /* CONFIG_AP */
4183 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184 /*
4185 * There is possible race condition between receiving the
4186 * association event and the EAPOL frame since they are coming
4187 * through different paths from the driver. In order to avoid
4188 * issues in trying to process the EAPOL frame before receiving
4189 * association information, lets queue it for processing until
Jouni Malinena05074c2012-12-21 21:35:35 +02004190 * the association event is received. This may also be needed in
4191 * driver-based roaming case, so also use src_addr != BSSID as a
4192 * trigger if we have previously confirmed that the
4193 * Authenticator uses BSSID as the src_addr (which is not the
4194 * case with wired IEEE 802.1X).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 */
4196 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
Jouni Malinena05074c2012-12-21 21:35:35 +02004197 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
4198 wpa_supplicant_state_txt(wpa_s->wpa_state),
4199 MAC2STR(wpa_s->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200 wpabuf_free(wpa_s->pending_eapol_rx);
4201 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
4202 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004203 os_get_reltime(&wpa_s->pending_eapol_rx_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
4205 ETH_ALEN);
4206 }
4207 return;
4208 }
4209
Jouni Malinena05074c2012-12-21 21:35:35 +02004210 wpa_s->last_eapol_matches_bssid =
4211 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
4212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213#ifdef CONFIG_AP
4214 if (wpa_s->ap_iface) {
4215 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
4216 return;
4217 }
4218#endif /* CONFIG_AP */
4219
4220 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
4221 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
4222 "no key management is configured");
4223 return;
4224 }
4225
4226 if (wpa_s->eapol_received == 0 &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004227 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
4229 wpa_s->wpa_state != WPA_COMPLETED) &&
4230 (wpa_s->current_ssid == NULL ||
4231 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
4232 /* Timeout for completing IEEE 802.1X and WPA authentication */
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004233 int timeout = 10;
4234
4235 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
4236 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
4237 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
4238 /* Use longer timeout for IEEE 802.1X/EAP */
4239 timeout = 70;
4240 }
4241
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07004242#ifdef CONFIG_WPS
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004243 if (wpa_s->current_ssid && wpa_s->current_bss &&
4244 (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
4245 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
4246 /*
4247 * Use shorter timeout if going through WPS AP iteration
4248 * for PIN config method with an AP that does not
4249 * advertise Selected Registrar.
4250 */
4251 struct wpabuf *wps_ie;
4252
4253 wps_ie = wpa_bss_get_vendor_ie_multi(
4254 wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
4255 if (wps_ie &&
4256 !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
4257 timeout = 10;
4258 wpabuf_free(wps_ie);
4259 }
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07004260#endif /* CONFIG_WPS */
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004261
4262 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 }
4264 wpa_s->eapol_received++;
4265
4266 if (wpa_s->countermeasures) {
4267 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
4268 "EAPOL packet");
4269 return;
4270 }
4271
4272#ifdef CONFIG_IBSS_RSN
4273 if (wpa_s->current_ssid &&
4274 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
4275 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
4276 return;
4277 }
4278#endif /* CONFIG_IBSS_RSN */
4279
4280 /* Source address of the incoming EAPOL frame could be compared to the
4281 * current BSSID. However, it is possible that a centralized
4282 * Authenticator could be using another MAC address than the BSSID of
4283 * an AP, so just allow any address to be used for now. The replies are
4284 * still sent to the current BSSID (if available), though. */
4285
4286 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
4287 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004288 wpa_s->key_mgmt != WPA_KEY_MGMT_OWE &&
4289 wpa_s->key_mgmt != WPA_KEY_MGMT_DPP &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004290 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
4291 return;
4292 wpa_drv_poll(wpa_s);
Hai Shalom74f70d42019-02-11 14:42:39 -08004293 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
4295 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
4296 /*
4297 * Set portValid = TRUE here since we are going to skip 4-way
4298 * handshake processing which would normally set portValid. We
4299 * need this to allow the EAPOL state machines to be completed
4300 * without going through EAPOL-Key handshake.
4301 */
4302 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
4303 }
4304}
4305
4306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004307int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004309 if ((!wpa_s->p2p_mgmt ||
4310 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
4311 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004312 l2_packet_deinit(wpa_s->l2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
4314 wpa_drv_get_mac_addr(wpa_s),
4315 ETH_P_EAPOL,
4316 wpa_supplicant_rx_eapol, wpa_s, 0);
4317 if (wpa_s->l2 == NULL)
4318 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004319
4320 if (l2_packet_set_packet_filter(wpa_s->l2,
4321 L2_PACKET_FILTER_PKTTYPE))
4322 wpa_dbg(wpa_s, MSG_DEBUG,
4323 "Failed to attach pkt_type filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 } else {
4325 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
4326 if (addr)
4327 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
4328 }
4329
4330 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
4331 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
4332 return -1;
4333 }
4334
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004335 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
Mikael Kanstrupb18a9582019-08-16 08:50:54 +02004336 wpas_wps_update_mac_addr(wpa_s);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004338 return 0;
4339}
4340
4341
Dmitry Shmidt04949592012-07-19 12:16:46 -07004342static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
4343 const u8 *buf, size_t len)
4344{
4345 struct wpa_supplicant *wpa_s = ctx;
4346 const struct l2_ethhdr *eth;
4347
4348 if (len < sizeof(*eth))
4349 return;
4350 eth = (const struct l2_ethhdr *) buf;
4351
4352 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
4353 !(eth->h_dest[0] & 0x01)) {
4354 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
4355 " (bridge - not for this interface - ignore)",
4356 MAC2STR(src_addr), MAC2STR(eth->h_dest));
4357 return;
4358 }
4359
4360 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
4361 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
4362 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
4363 len - sizeof(*eth));
4364}
4365
4366
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004367/**
4368 * wpa_supplicant_driver_init - Initialize driver interface parameters
4369 * @wpa_s: Pointer to wpa_supplicant data
4370 * Returns: 0 on success, -1 on failure
4371 *
4372 * This function is called to initialize driver interface parameters.
4373 * wpa_drv_init() must have been called before this function to initialize the
4374 * driver interface.
4375 */
4376int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
4377{
4378 static int interface_count = 0;
4379
4380 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
4381 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004383 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
4384 MAC2STR(wpa_s->own_addr));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004385 os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004386 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
4387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388 if (wpa_s->bridge_ifname[0]) {
4389 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
4390 "interface '%s'", wpa_s->bridge_ifname);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004391 wpa_s->l2_br = l2_packet_init_bridge(
4392 wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
4393 ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394 if (wpa_s->l2_br == NULL) {
4395 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
4396 "connection for the bridge interface '%s'",
4397 wpa_s->bridge_ifname);
4398 return -1;
4399 }
4400 }
4401
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004402 if (wpa_s->conf->ap_scan == 2 &&
4403 os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
4404 wpa_printf(MSG_INFO,
4405 "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
4406 }
4407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408 wpa_clear_keys(wpa_s, NULL);
4409
4410 /* Make sure that TKIP countermeasures are not left enabled (could
4411 * happen if wpa_supplicant is killed during countermeasures. */
4412 wpa_drv_set_countermeasures(wpa_s, 0);
4413
4414 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
4415 wpa_drv_flush_pmkid(wpa_s);
4416
4417 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004418 wpa_s->prev_scan_wildcard = 0;
4419
Dmitry Shmidt04949592012-07-19 12:16:46 -07004420 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004421 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4422 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4423 interface_count = 0;
4424 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004425#ifndef ANDROID
Dmitry Shmidta38abf92014-03-06 13:38:44 -08004426 if (!wpa_s->p2p_mgmt &&
Dmitry Shmidt98660862014-03-11 17:26:21 -07004427 wpa_supplicant_delayed_sched_scan(wpa_s,
4428 interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004429 100000))
Dmitry Shmidt98660862014-03-11 17:26:21 -07004430 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004431 100000);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004432#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 interface_count++;
4434 } else
4435 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
4436
4437 return 0;
4438}
4439
4440
4441static int wpa_supplicant_daemon(const char *pid_file)
4442{
4443 wpa_printf(MSG_DEBUG, "Daemonize..");
4444 return os_daemonize(pid_file);
4445}
4446
4447
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004448static struct wpa_supplicant *
4449wpa_supplicant_alloc(struct wpa_supplicant *parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
4451 struct wpa_supplicant *wpa_s;
4452
4453 wpa_s = os_zalloc(sizeof(*wpa_s));
4454 if (wpa_s == NULL)
4455 return NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004456 wpa_s->scan_req = INITIAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004457 wpa_s->scan_interval = 5;
4458 wpa_s->new_connection = 1;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004459 wpa_s->parent = parent ? parent : wpa_s;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004460 wpa_s->p2pdev = wpa_s->parent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004461 wpa_s->sched_scanning = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004463 dl_list_init(&wpa_s->bss_tmp_disallowed);
Paul Stewart092955c2017-02-06 09:13:09 -08004464 dl_list_init(&wpa_s->fils_hlp_req);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 return wpa_s;
4467}
4468
4469
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004470#ifdef CONFIG_HT_OVERRIDES
4471
4472static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
4473 struct ieee80211_ht_capabilities *htcaps,
4474 struct ieee80211_ht_capabilities *htcaps_mask,
4475 const char *ht_mcs)
4476{
4477 /* parse ht_mcs into hex array */
4478 int i;
4479 const char *tmp = ht_mcs;
4480 char *end = NULL;
4481
4482 /* If ht_mcs is null, do not set anything */
4483 if (!ht_mcs)
4484 return 0;
4485
4486 /* This is what we are setting in the kernel */
4487 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
4488
4489 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
4490
4491 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
Paul Stewart092955c2017-02-06 09:13:09 -08004492 long v;
4493
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004494 errno = 0;
Paul Stewart092955c2017-02-06 09:13:09 -08004495 v = strtol(tmp, &end, 16);
4496
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004497 if (errno == 0) {
4498 wpa_msg(wpa_s, MSG_DEBUG,
4499 "htcap value[%i]: %ld end: %p tmp: %p",
4500 i, v, end, tmp);
4501 if (end == tmp)
4502 break;
4503
4504 htcaps->supported_mcs_set[i] = v;
4505 tmp = end;
4506 } else {
4507 wpa_msg(wpa_s, MSG_ERROR,
4508 "Failed to parse ht-mcs: %s, error: %s\n",
4509 ht_mcs, strerror(errno));
4510 return -1;
4511 }
4512 }
4513
4514 /*
4515 * If we were able to parse any values, then set mask for the MCS set.
4516 */
4517 if (i) {
4518 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
4519 IEEE80211_HT_MCS_MASK_LEN - 1);
4520 /* skip the 3 reserved bits */
4521 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
4522 0x1f;
4523 }
4524
4525 return 0;
4526}
4527
4528
4529static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
4530 struct ieee80211_ht_capabilities *htcaps,
4531 struct ieee80211_ht_capabilities *htcaps_mask,
4532 int disabled)
4533{
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004534 le16 msk;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004535
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004536 if (disabled == -1)
4537 return 0;
4538
Hai Shalom74f70d42019-02-11 14:42:39 -08004539 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
4540
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004541 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
4542 htcaps_mask->ht_capabilities_info |= msk;
4543 if (disabled)
4544 htcaps->ht_capabilities_info &= msk;
4545 else
4546 htcaps->ht_capabilities_info |= msk;
4547
4548 return 0;
4549}
4550
4551
4552static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
4553 struct ieee80211_ht_capabilities *htcaps,
4554 struct ieee80211_ht_capabilities *htcaps_mask,
4555 int factor)
4556{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004557 if (factor == -1)
4558 return 0;
4559
Hai Shalom74f70d42019-02-11 14:42:39 -08004560 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
4561
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004562 if (factor < 0 || factor > 3) {
4563 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
4564 "Must be 0-3 or -1", factor);
4565 return -EINVAL;
4566 }
4567
4568 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
4569 htcaps->a_mpdu_params &= ~0x3;
4570 htcaps->a_mpdu_params |= factor & 0x3;
4571
4572 return 0;
4573}
4574
4575
4576static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
4577 struct ieee80211_ht_capabilities *htcaps,
4578 struct ieee80211_ht_capabilities *htcaps_mask,
4579 int density)
4580{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004581 if (density == -1)
4582 return 0;
4583
Hai Shalom74f70d42019-02-11 14:42:39 -08004584 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
4585
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004586 if (density < 0 || density > 7) {
4587 wpa_msg(wpa_s, MSG_ERROR,
4588 "ampdu_density: %d out of range. Must be 0-7 or -1.",
4589 density);
4590 return -EINVAL;
4591 }
4592
4593 htcaps_mask->a_mpdu_params |= 0x1C;
4594 htcaps->a_mpdu_params &= ~(0x1C);
4595 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
4596
4597 return 0;
4598}
4599
4600
4601static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
4602 struct ieee80211_ht_capabilities *htcaps,
4603 struct ieee80211_ht_capabilities *htcaps_mask,
4604 int disabled)
4605{
Hai Shalom74f70d42019-02-11 14:42:39 -08004606 if (disabled)
4607 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004608
Paul Stewart092955c2017-02-06 09:13:09 -08004609 set_disable_ht40(htcaps, disabled);
4610 set_disable_ht40(htcaps_mask, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004611
4612 return 0;
4613}
4614
4615
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004616static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
4617 struct ieee80211_ht_capabilities *htcaps,
4618 struct ieee80211_ht_capabilities *htcaps_mask,
4619 int disabled)
4620{
4621 /* Masking these out disables SGI */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004622 le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
4623 HT_CAP_INFO_SHORT_GI40MHZ);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004624
Hai Shalom74f70d42019-02-11 14:42:39 -08004625 if (disabled)
4626 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004627
4628 if (disabled)
4629 htcaps->ht_capabilities_info &= ~msk;
4630 else
4631 htcaps->ht_capabilities_info |= msk;
4632
4633 htcaps_mask->ht_capabilities_info |= msk;
4634
4635 return 0;
4636}
4637
4638
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004639static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
4640 struct ieee80211_ht_capabilities *htcaps,
4641 struct ieee80211_ht_capabilities *htcaps_mask,
4642 int disabled)
4643{
4644 /* Masking these out disables LDPC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004645 le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004646
Hai Shalom74f70d42019-02-11 14:42:39 -08004647 if (disabled)
4648 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004649
4650 if (disabled)
4651 htcaps->ht_capabilities_info &= ~msk;
4652 else
4653 htcaps->ht_capabilities_info |= msk;
4654
4655 htcaps_mask->ht_capabilities_info |= msk;
4656
4657 return 0;
4658}
4659
4660
Hai Shalom74f70d42019-02-11 14:42:39 -08004661static int wpa_set_tx_stbc(struct wpa_supplicant *wpa_s,
4662 struct ieee80211_ht_capabilities *htcaps,
4663 struct ieee80211_ht_capabilities *htcaps_mask,
4664 int tx_stbc)
4665{
4666 le16 msk = host_to_le16(HT_CAP_INFO_TX_STBC);
4667
4668 if (tx_stbc == -1)
4669 return 0;
4670
4671 wpa_msg(wpa_s, MSG_DEBUG, "set_tx_stbc: %d", tx_stbc);
4672
4673 if (tx_stbc < 0 || tx_stbc > 1) {
4674 wpa_msg(wpa_s, MSG_ERROR,
4675 "tx_stbc: %d out of range. Must be 0-1 or -1", tx_stbc);
4676 return -EINVAL;
4677 }
4678
4679 htcaps_mask->ht_capabilities_info |= msk;
4680 htcaps->ht_capabilities_info &= ~msk;
4681 htcaps->ht_capabilities_info |= (tx_stbc << 7) & msk;
4682
4683 return 0;
4684}
4685
4686
4687static int wpa_set_rx_stbc(struct wpa_supplicant *wpa_s,
4688 struct ieee80211_ht_capabilities *htcaps,
4689 struct ieee80211_ht_capabilities *htcaps_mask,
4690 int rx_stbc)
4691{
4692 le16 msk = host_to_le16(HT_CAP_INFO_RX_STBC_MASK);
4693
4694 if (rx_stbc == -1)
4695 return 0;
4696
4697 wpa_msg(wpa_s, MSG_DEBUG, "set_rx_stbc: %d", rx_stbc);
4698
4699 if (rx_stbc < 0 || rx_stbc > 3) {
4700 wpa_msg(wpa_s, MSG_ERROR,
4701 "rx_stbc: %d out of range. Must be 0-3 or -1", rx_stbc);
4702 return -EINVAL;
4703 }
4704
4705 htcaps_mask->ht_capabilities_info |= msk;
4706 htcaps->ht_capabilities_info &= ~msk;
4707 htcaps->ht_capabilities_info |= (rx_stbc << 8) & msk;
4708
4709 return 0;
4710}
4711
4712
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004713void wpa_supplicant_apply_ht_overrides(
4714 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
4715 struct wpa_driver_associate_params *params)
4716{
4717 struct ieee80211_ht_capabilities *htcaps;
4718 struct ieee80211_ht_capabilities *htcaps_mask;
4719
4720 if (!ssid)
4721 return;
4722
4723 params->disable_ht = ssid->disable_ht;
4724 if (!params->htcaps || !params->htcaps_mask)
4725 return;
4726
4727 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
4728 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
4729 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
4730 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
4731 ssid->disable_max_amsdu);
4732 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
4733 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
4734 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004735 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004736 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
Hai Shalom74f70d42019-02-11 14:42:39 -08004737 wpa_set_rx_stbc(wpa_s, htcaps, htcaps_mask, ssid->rx_stbc);
4738 wpa_set_tx_stbc(wpa_s, htcaps, htcaps_mask, ssid->tx_stbc);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07004739
4740 if (ssid->ht40_intolerant) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004741 le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07004742 htcaps->ht_capabilities_info |= bit;
4743 htcaps_mask->ht_capabilities_info |= bit;
4744 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004745}
4746
4747#endif /* CONFIG_HT_OVERRIDES */
4748
4749
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004750#ifdef CONFIG_VHT_OVERRIDES
4751void wpa_supplicant_apply_vht_overrides(
4752 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
4753 struct wpa_driver_associate_params *params)
4754{
4755 struct ieee80211_vht_capabilities *vhtcaps;
4756 struct ieee80211_vht_capabilities *vhtcaps_mask;
4757
4758 if (!ssid)
4759 return;
4760
4761 params->disable_vht = ssid->disable_vht;
4762
4763 vhtcaps = (void *) params->vhtcaps;
4764 vhtcaps_mask = (void *) params->vhtcaps_mask;
4765
4766 if (!vhtcaps || !vhtcaps_mask)
4767 return;
4768
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004769 vhtcaps->vht_capabilities_info = host_to_le32(ssid->vht_capa);
4770 vhtcaps_mask->vht_capabilities_info = host_to_le32(ssid->vht_capa_mask);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004771
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004772#ifdef CONFIG_HT_OVERRIDES
Hai Shalom74f70d42019-02-11 14:42:39 -08004773 if (ssid->disable_sgi) {
4774 vhtcaps_mask->vht_capabilities_info |= (VHT_CAP_SHORT_GI_80 |
4775 VHT_CAP_SHORT_GI_160);
4776 vhtcaps->vht_capabilities_info &= ~(VHT_CAP_SHORT_GI_80 |
4777 VHT_CAP_SHORT_GI_160);
4778 wpa_msg(wpa_s, MSG_DEBUG,
4779 "disable-sgi override specified, vht-caps: 0x%x",
4780 vhtcaps->vht_capabilities_info);
4781 }
4782
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004783 /* if max ampdu is <= 3, we have to make the HT cap the same */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004784 if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
4785 int max_ampdu;
4786
4787 max_ampdu = (ssid->vht_capa &
4788 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
4789 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004790
4791 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
4792 wpa_set_ampdu_factor(wpa_s,
4793 (void *) params->htcaps,
4794 (void *) params->htcaps_mask,
4795 max_ampdu);
4796 }
4797#endif /* CONFIG_HT_OVERRIDES */
4798
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004799#define OVERRIDE_MCS(i) \
4800 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
4801 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004802 host_to_le16(3 << 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004803 vhtcaps->vht_supported_mcs_set.tx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004804 host_to_le16(ssid->vht_tx_mcs_nss_ ##i << \
4805 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004806 } \
4807 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
4808 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004809 host_to_le16(3 << 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004810 vhtcaps->vht_supported_mcs_set.rx_map |= \
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004811 host_to_le16(ssid->vht_rx_mcs_nss_ ##i << \
4812 2 * (i - 1)); \
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004813 }
4814
4815 OVERRIDE_MCS(1);
4816 OVERRIDE_MCS(2);
4817 OVERRIDE_MCS(3);
4818 OVERRIDE_MCS(4);
4819 OVERRIDE_MCS(5);
4820 OVERRIDE_MCS(6);
4821 OVERRIDE_MCS(7);
4822 OVERRIDE_MCS(8);
4823}
4824#endif /* CONFIG_VHT_OVERRIDES */
4825
4826
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
4828{
4829#ifdef PCSC_FUNCS
4830 size_t len;
4831
4832 if (!wpa_s->conf->pcsc_reader)
4833 return 0;
4834
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004835 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 if (!wpa_s->scard)
4837 return 1;
4838
4839 if (wpa_s->conf->pcsc_pin &&
4840 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
4841 scard_deinit(wpa_s->scard);
4842 wpa_s->scard = NULL;
4843 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
4844 return -1;
4845 }
4846
4847 len = sizeof(wpa_s->imsi) - 1;
4848 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
4849 scard_deinit(wpa_s->scard);
4850 wpa_s->scard = NULL;
4851 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
4852 return -1;
4853 }
4854 wpa_s->imsi[len] = '\0';
4855
4856 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
4857
4858 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
4859 wpa_s->imsi, wpa_s->mnc_len);
4860
4861 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
4862 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
4863#endif /* PCSC_FUNCS */
4864
4865 return 0;
4866}
4867
4868
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004869int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
4870{
4871 char *val, *pos;
4872
4873 ext_password_deinit(wpa_s->ext_pw);
4874 wpa_s->ext_pw = NULL;
4875 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
4876
4877 if (!wpa_s->conf->ext_password_backend)
4878 return 0;
4879
4880 val = os_strdup(wpa_s->conf->ext_password_backend);
4881 if (val == NULL)
4882 return -1;
4883 pos = os_strchr(val, ':');
4884 if (pos)
4885 *pos++ = '\0';
4886
4887 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
4888
4889 wpa_s->ext_pw = ext_password_init(val, pos);
4890 os_free(val);
4891 if (wpa_s->ext_pw == NULL) {
4892 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
4893 return -1;
4894 }
4895 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
4896
4897 return 0;
4898}
4899
4900
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004901#ifdef CONFIG_FST
4902
4903static const u8 * wpas_fst_get_bssid_cb(void *ctx)
4904{
4905 struct wpa_supplicant *wpa_s = ctx;
4906
4907 return (is_zero_ether_addr(wpa_s->bssid) ||
4908 wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
4909}
4910
4911
4912static void wpas_fst_get_channel_info_cb(void *ctx,
4913 enum hostapd_hw_mode *hw_mode,
4914 u8 *channel)
4915{
4916 struct wpa_supplicant *wpa_s = ctx;
4917
4918 if (wpa_s->current_bss) {
4919 *hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
4920 channel);
4921 } else if (wpa_s->hw.num_modes) {
4922 *hw_mode = wpa_s->hw.modes[0].mode;
4923 } else {
4924 WPA_ASSERT(0);
4925 *hw_mode = 0;
4926 }
4927}
4928
4929
4930static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
4931{
4932 struct wpa_supplicant *wpa_s = ctx;
4933
4934 *modes = wpa_s->hw.modes;
4935 return wpa_s->hw.num_modes;
4936}
4937
4938
4939static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
4940{
4941 struct wpa_supplicant *wpa_s = ctx;
4942
4943 wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
4944 wpa_s->fst_ies = fst_ies;
4945}
4946
4947
4948static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
4949{
4950 struct wpa_supplicant *wpa_s = ctx;
4951
Paul Stewart092955c2017-02-06 09:13:09 -08004952 if (os_memcmp(wpa_s->bssid, da, ETH_ALEN) != 0) {
4953 wpa_printf(MSG_INFO, "FST:%s:bssid=" MACSTR " != da=" MACSTR,
4954 __func__, MAC2STR(wpa_s->bssid), MAC2STR(da));
4955 return -1;
4956 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004957 return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
Paul Stewart092955c2017-02-06 09:13:09 -08004958 wpa_s->own_addr, wpa_s->bssid,
4959 wpabuf_head(data), wpabuf_len(data),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004960 0);
4961}
4962
4963
4964static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
4965{
4966 struct wpa_supplicant *wpa_s = ctx;
4967
4968 WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
4969 return wpa_s->received_mb_ies;
4970}
4971
4972
4973static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
4974 const u8 *buf, size_t size)
4975{
4976 struct wpa_supplicant *wpa_s = ctx;
4977 struct mb_ies_info info;
4978
4979 WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
4980
4981 if (!mb_ies_info_by_ies(&info, buf, size)) {
4982 wpabuf_free(wpa_s->received_mb_ies);
4983 wpa_s->received_mb_ies = mb_ies_by_info(&info);
4984 }
4985}
4986
4987
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004988static const u8 * wpas_fst_get_peer_first(void *ctx,
4989 struct fst_get_peer_ctx **get_ctx,
4990 Boolean mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004991{
4992 struct wpa_supplicant *wpa_s = ctx;
4993
4994 *get_ctx = NULL;
4995 if (!is_zero_ether_addr(wpa_s->bssid))
4996 return (wpa_s->received_mb_ies || !mb_only) ?
4997 wpa_s->bssid : NULL;
4998 return NULL;
4999}
5000
5001
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07005002static const u8 * wpas_fst_get_peer_next(void *ctx,
5003 struct fst_get_peer_ctx **get_ctx,
5004 Boolean mb_only)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005005{
5006 return NULL;
5007}
5008
5009void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
5010 struct fst_wpa_obj *iface_obj)
5011{
5012 iface_obj->ctx = wpa_s;
5013 iface_obj->get_bssid = wpas_fst_get_bssid_cb;
5014 iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
5015 iface_obj->get_hw_modes = wpas_fst_get_hw_modes;
5016 iface_obj->set_ies = wpas_fst_set_ies_cb;
5017 iface_obj->send_action = wpas_fst_send_action_cb;
5018 iface_obj->get_mb_ie = wpas_fst_get_mb_ie_cb;
5019 iface_obj->update_mb_ie = wpas_fst_update_mb_ie_cb;
5020 iface_obj->get_peer_first = wpas_fst_get_peer_first;
5021 iface_obj->get_peer_next = wpas_fst_get_peer_next;
5022}
5023#endif /* CONFIG_FST */
5024
Dmitry Shmidtc2817022014-07-02 10:32:10 -07005025static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005026 const struct wpa_driver_capa *capa)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005027{
Dmitry Shmidt0207e232014-09-03 14:58:37 -07005028 struct wowlan_triggers *triggers;
5029 int ret = 0;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005030
5031 if (!wpa_s->conf->wowlan_triggers)
5032 return 0;
5033
Dmitry Shmidt0207e232014-09-03 14:58:37 -07005034 triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
5035 if (triggers) {
5036 ret = wpa_drv_wowlan(wpa_s, triggers);
5037 os_free(triggers);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005038 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005039 return ret;
5040}
5041
5042
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005043enum wpa_radio_work_band wpas_freq_to_band(int freq)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005044{
5045 if (freq < 3000)
5046 return BAND_2_4_GHZ;
5047 if (freq > 50000)
5048 return BAND_60_GHZ;
5049 return BAND_5_GHZ;
5050}
5051
5052
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005053unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s, const int *freqs)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005054{
5055 int i;
5056 unsigned int band = 0;
5057
5058 if (freqs) {
5059 /* freqs are specified for the radio work */
5060 for (i = 0; freqs[i]; i++)
5061 band |= wpas_freq_to_band(freqs[i]);
5062 } else {
5063 /*
5064 * freqs are not specified, implies all
5065 * the supported freqs by HW
5066 */
5067 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5068 if (wpa_s->hw.modes[i].num_channels != 0) {
5069 if (wpa_s->hw.modes[i].mode ==
5070 HOSTAPD_MODE_IEEE80211B ||
5071 wpa_s->hw.modes[i].mode ==
5072 HOSTAPD_MODE_IEEE80211G)
5073 band |= BAND_2_4_GHZ;
5074 else if (wpa_s->hw.modes[i].mode ==
5075 HOSTAPD_MODE_IEEE80211A)
5076 band |= BAND_5_GHZ;
5077 else if (wpa_s->hw.modes[i].mode ==
5078 HOSTAPD_MODE_IEEE80211AD)
5079 band |= BAND_60_GHZ;
5080 else if (wpa_s->hw.modes[i].mode ==
5081 HOSTAPD_MODE_IEEE80211ANY)
5082 band = BAND_2_4_GHZ | BAND_5_GHZ |
5083 BAND_60_GHZ;
5084 }
5085 }
5086 }
5087
5088 return band;
5089}
5090
5091
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005092static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
5093 const char *rn)
5094{
5095 struct wpa_supplicant *iface = wpa_s->global->ifaces;
5096 struct wpa_radio *radio;
5097
5098 while (rn && iface) {
5099 radio = iface->radio;
5100 if (radio && os_strcmp(rn, radio->name) == 0) {
5101 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
5102 wpa_s->ifname, rn);
5103 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
5104 return radio;
5105 }
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08005106
5107 iface = iface->next;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005108 }
5109
5110 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
5111 wpa_s->ifname, rn ? rn : "N/A");
5112 radio = os_zalloc(sizeof(*radio));
5113 if (radio == NULL)
5114 return NULL;
5115
5116 if (rn)
5117 os_strlcpy(radio->name, rn, sizeof(radio->name));
5118 dl_list_init(&radio->ifaces);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005119 dl_list_init(&radio->work);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005120 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
5121
5122 return radio;
5123}
5124
5125
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005126static void radio_work_free(struct wpa_radio_work *work)
5127{
5128 if (work->wpa_s->scan_work == work) {
5129 /* This should not really happen. */
5130 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
5131 work->type, work, work->started);
5132 work->wpa_s->scan_work = NULL;
5133 }
5134
5135#ifdef CONFIG_P2P
5136 if (work->wpa_s->p2p_scan_work == work) {
5137 /* This should not really happen. */
5138 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
5139 work->type, work, work->started);
5140 work->wpa_s->p2p_scan_work = NULL;
5141 }
5142#endif /* CONFIG_P2P */
5143
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005144 if (work->started) {
5145 work->wpa_s->radio->num_active_works--;
5146 wpa_dbg(work->wpa_s, MSG_DEBUG,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005147 "radio_work_free('%s'@%p): num_active_works --> %u",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005148 work->type, work,
5149 work->wpa_s->radio->num_active_works);
5150 }
5151
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005152 dl_list_del(&work->list);
5153 os_free(work);
5154}
5155
5156
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005157static int radio_work_is_connect(struct wpa_radio_work *work)
5158{
5159 return os_strcmp(work->type, "sme-connect") == 0 ||
5160 os_strcmp(work->type, "connect") == 0;
5161}
5162
5163
5164static int radio_work_is_scan(struct wpa_radio_work *work)
5165{
5166 return os_strcmp(work->type, "scan") == 0 ||
5167 os_strcmp(work->type, "p2p-scan") == 0;
5168}
5169
5170
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005171static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
5172{
5173 struct wpa_radio_work *active_work = NULL;
5174 struct wpa_radio_work *tmp;
5175
5176 /* Get the active work to know the type and band. */
5177 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
5178 if (tmp->started) {
5179 active_work = tmp;
5180 break;
5181 }
5182 }
5183
5184 if (!active_work) {
5185 /* No active work, start one */
5186 radio->num_active_works = 0;
5187 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
5188 list) {
5189 if (os_strcmp(tmp->type, "scan") == 0 &&
5190 radio->external_scan_running &&
5191 (((struct wpa_driver_scan_params *)
5192 tmp->ctx)->only_new_results ||
5193 tmp->wpa_s->clear_driver_scan_cache))
5194 continue;
5195 return tmp;
5196 }
5197 return NULL;
5198 }
5199
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005200 if (radio_work_is_connect(active_work)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005201 /*
5202 * If the active work is either connect or sme-connect,
5203 * do not parallelize them with other radio works.
5204 */
5205 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
5206 "Do not parallelize radio work with %s",
5207 active_work->type);
5208 return NULL;
5209 }
5210
5211 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
5212 if (tmp->started)
5213 continue;
5214
5215 /*
5216 * If connect or sme-connect are enqueued, parallelize only
5217 * those operations ahead of them in the queue.
5218 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005219 if (radio_work_is_connect(tmp))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005220 break;
5221
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005222 /* Serialize parallel scan and p2p_scan operations on the same
5223 * interface since the driver_nl80211 mechanism for tracking
5224 * scan cookies does not yet have support for this. */
5225 if (active_work->wpa_s == tmp->wpa_s &&
5226 radio_work_is_scan(active_work) &&
5227 radio_work_is_scan(tmp)) {
5228 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
5229 "Do not start work '%s' when another work '%s' is already scheduled",
5230 tmp->type, active_work->type);
5231 continue;
5232 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005233 /*
5234 * Check that the radio works are distinct and
5235 * on different bands.
5236 */
5237 if (os_strcmp(active_work->type, tmp->type) != 0 &&
5238 (active_work->bands != tmp->bands)) {
5239 /*
5240 * If a scan has to be scheduled through nl80211 scan
5241 * interface and if an external scan is already running,
5242 * do not schedule the scan since it is likely to get
5243 * rejected by kernel.
5244 */
5245 if (os_strcmp(tmp->type, "scan") == 0 &&
5246 radio->external_scan_running &&
5247 (((struct wpa_driver_scan_params *)
5248 tmp->ctx)->only_new_results ||
5249 tmp->wpa_s->clear_driver_scan_cache))
5250 continue;
5251
5252 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
5253 "active_work:%s new_work:%s",
5254 active_work->type, tmp->type);
5255 return tmp;
5256 }
5257 }
5258
5259 /* Did not find a radio work to schedule in parallel. */
5260 return NULL;
5261}
5262
5263
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005264static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
5265{
5266 struct wpa_radio *radio = eloop_ctx;
5267 struct wpa_radio_work *work;
5268 struct os_reltime now, diff;
5269 struct wpa_supplicant *wpa_s;
5270
5271 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005272 if (work == NULL) {
5273 radio->num_active_works = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005274 return;
5275 }
5276
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005277 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
5278 radio_list);
5279
5280 if (!(wpa_s &&
5281 wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
5282 if (work->started)
5283 return; /* already started and still in progress */
5284
5285 if (wpa_s && wpa_s->radio->external_scan_running) {
5286 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
5287 return;
5288 }
5289 } else {
5290 work = NULL;
5291 if (radio->num_active_works < MAX_ACTIVE_WORKS) {
5292 /* get the work to schedule next */
5293 work = radio_work_get_next_work(radio);
5294 }
5295 if (!work)
5296 return;
5297 }
5298
5299 wpa_s = work->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005300 os_get_reltime(&now);
5301 os_reltime_sub(&now, &work->time, &diff);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005302 wpa_dbg(wpa_s, MSG_DEBUG,
5303 "Starting radio work '%s'@%p after %ld.%06ld second wait",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005304 work->type, work, diff.sec, diff.usec);
5305 work->started = 1;
5306 work->time = now;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005307 radio->num_active_works++;
5308
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005309 work->cb(work, 0);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005310
5311 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
5312 radio->num_active_works < MAX_ACTIVE_WORKS)
5313 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005314}
5315
5316
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005317/*
5318 * This function removes both started and pending radio works running on
5319 * the provided interface's radio.
5320 * Prior to the removal of the radio work, its callback (cb) is called with
5321 * deinit set to be 1. Each work's callback is responsible for clearing its
5322 * internal data and restoring to a correct state.
5323 * @wpa_s: wpa_supplicant data
5324 * @type: type of works to be removed
5325 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
5326 * this interface's works.
5327 */
5328void radio_remove_works(struct wpa_supplicant *wpa_s,
5329 const char *type, int remove_all)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005330{
5331 struct wpa_radio_work *work, *tmp;
5332 struct wpa_radio *radio = wpa_s->radio;
5333
5334 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
5335 list) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005336 if (type && os_strcmp(type, work->type) != 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005337 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005338
5339 /* skip other ifaces' works */
5340 if (!remove_all && work->wpa_s != wpa_s)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005341 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005342
5343 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
5344 work->type, work, work->started ? " (started)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005345 work->cb(work, 1);
5346 radio_work_free(work);
5347 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08005348
5349 /* in case we removed the started work */
5350 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005351}
5352
5353
Roshan Pius3a1667e2018-07-03 15:17:14 -07005354void radio_remove_pending_work(struct wpa_supplicant *wpa_s, void *ctx)
5355{
5356 struct wpa_radio_work *work;
5357 struct wpa_radio *radio = wpa_s->radio;
5358
5359 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
5360 if (work->ctx != ctx)
5361 continue;
5362 wpa_dbg(wpa_s, MSG_DEBUG, "Free pending radio work '%s'@%p%s",
5363 work->type, work, work->started ? " (started)" : "");
5364 radio_work_free(work);
5365 break;
5366 }
5367}
5368
5369
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005370static void radio_remove_interface(struct wpa_supplicant *wpa_s)
5371{
5372 struct wpa_radio *radio = wpa_s->radio;
5373
5374 if (!radio)
5375 return;
5376
5377 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
5378 wpa_s->ifname, radio->name);
5379 dl_list_del(&wpa_s->radio_list);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005380 radio_remove_works(wpa_s, NULL, 0);
5381 wpa_s->radio = NULL;
5382 if (!dl_list_empty(&radio->ifaces))
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005383 return; /* Interfaces remain for this radio */
5384
5385 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005386 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005387 os_free(radio);
5388}
5389
5390
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005391void radio_work_check_next(struct wpa_supplicant *wpa_s)
5392{
5393 struct wpa_radio *radio = wpa_s->radio;
5394
5395 if (dl_list_empty(&radio->work))
5396 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005397 if (wpa_s->ext_work_in_progress) {
5398 wpa_printf(MSG_DEBUG,
5399 "External radio work in progress - delay start of pending item");
5400 return;
5401 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005402 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
5403 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
5404}
5405
5406
5407/**
5408 * radio_add_work - Add a radio work item
5409 * @wpa_s: Pointer to wpa_supplicant data
5410 * @freq: Frequency of the offchannel operation in MHz or 0
5411 * @type: Unique identifier for each type of work
5412 * @next: Force as the next work to be executed
5413 * @cb: Callback function for indicating when radio is available
5414 * @ctx: Context pointer for the work (work->ctx in cb())
5415 * Returns: 0 on success, -1 on failure
5416 *
5417 * This function is used to request time for an operation that requires
5418 * exclusive radio control. Once the radio is available, the registered callback
5419 * function will be called. radio_work_done() must be called once the exclusive
5420 * radio operation has been completed, so that the radio is freed for other
5421 * operations. The special case of deinit=1 is used to free the context data
5422 * during interface removal. That does not allow the callback function to start
5423 * the radio operation, i.e., it must free any resources allocated for the radio
5424 * work and return.
5425 *
5426 * The @freq parameter can be used to indicate a single channel on which the
5427 * offchannel operation will occur. This may allow multiple radio work
5428 * operations to be performed in parallel if they apply for the same channel.
5429 * Setting this to 0 indicates that the work item may use multiple channels or
5430 * requires exclusive control of the radio.
5431 */
5432int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
5433 const char *type, int next,
5434 void (*cb)(struct wpa_radio_work *work, int deinit),
5435 void *ctx)
5436{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005437 struct wpa_radio *radio = wpa_s->radio;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005438 struct wpa_radio_work *work;
5439 int was_empty;
5440
5441 work = os_zalloc(sizeof(*work));
5442 if (work == NULL)
5443 return -1;
5444 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
5445 os_get_reltime(&work->time);
5446 work->freq = freq;
5447 work->type = type;
5448 work->wpa_s = wpa_s;
5449 work->cb = cb;
5450 work->ctx = ctx;
5451
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005452 if (freq)
5453 work->bands = wpas_freq_to_band(freq);
5454 else if (os_strcmp(type, "scan") == 0 ||
5455 os_strcmp(type, "p2p-scan") == 0)
5456 work->bands = wpas_get_bands(wpa_s,
5457 ((struct wpa_driver_scan_params *)
5458 ctx)->freqs);
5459 else
5460 work->bands = wpas_get_bands(wpa_s, NULL);
5461
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005462 was_empty = dl_list_empty(&wpa_s->radio->work);
5463 if (next)
5464 dl_list_add(&wpa_s->radio->work, &work->list);
5465 else
5466 dl_list_add_tail(&wpa_s->radio->work, &work->list);
5467 if (was_empty) {
5468 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
5469 radio_work_check_next(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005470 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
5471 && radio->num_active_works < MAX_ACTIVE_WORKS) {
5472 wpa_dbg(wpa_s, MSG_DEBUG,
5473 "Try to schedule a radio work (num_active_works=%u)",
5474 radio->num_active_works);
5475 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005476 }
5477
5478 return 0;
5479}
5480
5481
5482/**
5483 * radio_work_done - Indicate that a radio work item has been completed
5484 * @work: Completed work
5485 *
5486 * This function is called once the callback function registered with
5487 * radio_add_work() has completed its work.
5488 */
5489void radio_work_done(struct wpa_radio_work *work)
5490{
5491 struct wpa_supplicant *wpa_s = work->wpa_s;
5492 struct os_reltime now, diff;
5493 unsigned int started = work->started;
5494
5495 os_get_reltime(&now);
5496 os_reltime_sub(&now, &work->time, &diff);
5497 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
5498 work->type, work, started ? "done" : "canceled",
5499 diff.sec, diff.usec);
5500 radio_work_free(work);
5501 if (started)
5502 radio_work_check_next(wpa_s);
5503}
5504
5505
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08005506struct wpa_radio_work *
5507radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005508{
5509 struct wpa_radio_work *work;
5510 struct wpa_radio *radio = wpa_s->radio;
5511
5512 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
5513 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08005514 return work;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005515 }
5516
Dmitry Shmidt2e425d62014-11-10 11:18:27 -08005517 return NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005518}
5519
5520
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005521static int wpas_init_driver(struct wpa_supplicant *wpa_s,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005522 const struct wpa_interface *iface)
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005523{
5524 const char *ifname, *driver, *rn;
5525
5526 driver = iface->driver;
5527next_driver:
5528 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
5529 return -1;
5530
5531 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
5532 if (wpa_s->drv_priv == NULL) {
5533 const char *pos;
5534 pos = driver ? os_strchr(driver, ',') : NULL;
5535 if (pos) {
5536 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
5537 "driver interface - try next driver wrapper");
5538 driver = pos + 1;
5539 goto next_driver;
5540 }
5541 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
5542 "interface");
5543 return -1;
5544 }
5545 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
5546 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
5547 "driver_param '%s'", wpa_s->conf->driver_param);
5548 return -1;
5549 }
5550
5551 ifname = wpa_drv_get_ifname(wpa_s);
5552 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
5553 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
5554 "interface name with '%s'", ifname);
5555 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
5556 }
5557
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005558 rn = wpa_driver_get_radio_name(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005559 if (rn && rn[0] == '\0')
5560 rn = NULL;
5561
5562 wpa_s->radio = radio_add_interface(wpa_s, rn);
5563 if (wpa_s->radio == NULL)
5564 return -1;
5565
5566 return 0;
5567}
5568
5569
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005570#ifdef CONFIG_GAS_SERVER
5571
5572static void wpas_gas_server_tx_status(struct wpa_supplicant *wpa_s,
5573 unsigned int freq, const u8 *dst,
5574 const u8 *src, const u8 *bssid,
5575 const u8 *data, size_t data_len,
5576 enum offchannel_send_action_result result)
5577{
5578 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
5579 " result=%s",
5580 freq, MAC2STR(dst),
5581 result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
5582 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
5583 "FAILED"));
5584 gas_server_tx_status(wpa_s->gas_server, dst, data, data_len,
5585 result == OFFCHANNEL_SEND_ACTION_SUCCESS);
5586}
5587
5588
5589static void wpas_gas_server_tx(void *ctx, int freq, const u8 *da,
5590 struct wpabuf *buf, unsigned int wait_time)
5591{
5592 struct wpa_supplicant *wpa_s = ctx;
5593 const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5594
5595 if (wait_time > wpa_s->max_remain_on_chan)
5596 wait_time = wpa_s->max_remain_on_chan;
5597
5598 offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, broadcast,
5599 wpabuf_head(buf), wpabuf_len(buf),
5600 wait_time, wpas_gas_server_tx_status, 0);
5601}
5602
5603#endif /* CONFIG_GAS_SERVER */
5604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005605static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005606 const struct wpa_interface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005608 struct wpa_driver_capa capa;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005609 int capa_res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005610 u8 dfs_domain;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611
5612 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
5613 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
5614 iface->confname ? iface->confname : "N/A",
5615 iface->driver ? iface->driver : "default",
5616 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
5617 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
5618
5619 if (iface->confname) {
5620#ifdef CONFIG_BACKEND_FILE
5621 wpa_s->confname = os_rel2abs_path(iface->confname);
5622 if (wpa_s->confname == NULL) {
5623 wpa_printf(MSG_ERROR, "Failed to get absolute path "
5624 "for configuration file '%s'.",
5625 iface->confname);
5626 return -1;
5627 }
5628 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
5629 iface->confname, wpa_s->confname);
5630#else /* CONFIG_BACKEND_FILE */
5631 wpa_s->confname = os_strdup(iface->confname);
5632#endif /* CONFIG_BACKEND_FILE */
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07005633 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 if (wpa_s->conf == NULL) {
5635 wpa_printf(MSG_ERROR, "Failed to read or parse "
5636 "configuration '%s'.", wpa_s->confname);
5637 return -1;
5638 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07005639 wpa_s->confanother = os_rel2abs_path(iface->confanother);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005640 if (wpa_s->confanother &&
5641 !wpa_config_read(wpa_s->confanother, wpa_s->conf)) {
5642 wpa_printf(MSG_ERROR,
5643 "Failed to read or parse configuration '%s'.",
5644 wpa_s->confanother);
5645 return -1;
5646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005647
5648 /*
5649 * Override ctrl_interface and driver_param if set on command
5650 * line.
5651 */
5652 if (iface->ctrl_interface) {
5653 os_free(wpa_s->conf->ctrl_interface);
5654 wpa_s->conf->ctrl_interface =
5655 os_strdup(iface->ctrl_interface);
5656 }
5657
5658 if (iface->driver_param) {
5659 os_free(wpa_s->conf->driver_param);
5660 wpa_s->conf->driver_param =
5661 os_strdup(iface->driver_param);
5662 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005663
5664 if (iface->p2p_mgmt && !iface->ctrl_interface) {
5665 os_free(wpa_s->conf->ctrl_interface);
5666 wpa_s->conf->ctrl_interface = NULL;
5667 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005668 } else
5669 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
5670 iface->driver_param);
5671
5672 if (wpa_s->conf == NULL) {
5673 wpa_printf(MSG_ERROR, "\nNo configuration found.");
5674 return -1;
5675 }
5676
5677 if (iface->ifname == NULL) {
5678 wpa_printf(MSG_ERROR, "\nInterface name is required.");
5679 return -1;
5680 }
5681 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
5682 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
5683 iface->ifname);
5684 return -1;
5685 }
5686 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
5687
5688 if (iface->bridge_ifname) {
5689 if (os_strlen(iface->bridge_ifname) >=
5690 sizeof(wpa_s->bridge_ifname)) {
5691 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
5692 "name '%s'.", iface->bridge_ifname);
5693 return -1;
5694 }
5695 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
5696 sizeof(wpa_s->bridge_ifname));
5697 }
5698
5699 /* RSNA Supplicant Key Management - INITIALIZE */
5700 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
5701 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
5702
5703 /* Initialize driver interface and register driver event handler before
5704 * L2 receive handler so that association events are processed before
5705 * EAPOL-Key packets if both become available for the same select()
5706 * call. */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08005707 if (wpas_init_driver(wpa_s, iface) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708 return -1;
5709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710 if (wpa_supplicant_init_wpa(wpa_s) < 0)
5711 return -1;
5712
5713 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
5714 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
5715 NULL);
5716 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
5717
5718 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
5719 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
5720 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
5721 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
5722 "dot11RSNAConfigPMKLifetime");
5723 return -1;
5724 }
5725
5726 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
5727 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
5728 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
5729 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
5730 "dot11RSNAConfigPMKReauthThreshold");
5731 return -1;
5732 }
5733
5734 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
5735 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
5736 wpa_s->conf->dot11RSNAConfigSATimeout)) {
5737 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
5738 "dot11RSNAConfigSATimeout");
5739 return -1;
5740 }
5741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005742 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
5743 &wpa_s->hw.num_modes,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005744 &wpa_s->hw.flags,
5745 &dfs_domain);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005746 if (wpa_s->hw.modes) {
5747 u16 i;
5748
5749 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5750 if (wpa_s->hw.modes[i].vht_capab) {
5751 wpa_s->hw_capab = CAPAB_VHT;
5752 break;
5753 }
5754
5755 if (wpa_s->hw.modes[i].ht_capab &
5756 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
5757 wpa_s->hw_capab = CAPAB_HT40;
5758 else if (wpa_s->hw.modes[i].ht_capab &&
5759 wpa_s->hw_capab == CAPAB_NO_HT_VHT)
5760 wpa_s->hw_capab = CAPAB_HT;
5761 }
5762 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005763
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005764 capa_res = wpa_drv_get_capa(wpa_s, &capa);
5765 if (capa_res == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005766 wpa_s->drv_capa_known = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005767 wpa_s->drv_flags = capa.flags;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005768 wpa_s->drv_enc = capa.enc;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005769 wpa_s->drv_smps_modes = capa.smps_modes;
5770 wpa_s->drv_rrm_flags = capa.rrm_flags;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005771 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772 wpa_s->max_scan_ssids = capa.max_scan_ssids;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005773 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005774 wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
5775 wpa_s->max_sched_scan_plan_interval =
5776 capa.max_sched_scan_plan_interval;
5777 wpa_s->max_sched_scan_plan_iterations =
5778 capa.max_sched_scan_plan_iterations;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005779 wpa_s->sched_scan_supported = capa.sched_scan_supported;
5780 wpa_s->max_match_sets = capa.max_match_sets;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
5782 wpa_s->max_stations = capa.max_stations;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005783 wpa_s->extended_capa = capa.extended_capa;
5784 wpa_s->extended_capa_mask = capa.extended_capa_mask;
5785 wpa_s->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005786 wpa_s->num_multichan_concurrent =
5787 capa.num_multichan_concurrent;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005788 wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
5789
5790 if (capa.mac_addr_rand_scan_supported)
5791 wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
5792 if (wpa_s->sched_scan_supported &&
5793 capa.mac_addr_rand_sched_scan_supported)
5794 wpa_s->mac_addr_rand_supported |=
5795 (MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
Hai Shalom74f70d42019-02-11 14:42:39 -08005796
5797 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
5798 if (wpa_s->extended_capa &&
5799 wpa_s->extended_capa_len >= 3 &&
5800 wpa_s->extended_capa[2] & 0x40)
5801 wpa_s->multi_bss_support = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802 }
5803 if (wpa_s->max_remain_on_chan == 0)
5804 wpa_s->max_remain_on_chan = 1000;
5805
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005806 /*
5807 * Only take p2p_mgmt parameters when P2P Device is supported.
5808 * Doing it here as it determines whether l2_packet_init() will be done
5809 * during wpa_supplicant_driver_init().
5810 */
5811 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
5812 wpa_s->p2p_mgmt = iface->p2p_mgmt;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005813
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005814 if (wpa_s->num_multichan_concurrent == 0)
5815 wpa_s->num_multichan_concurrent = 1;
5816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817 if (wpa_supplicant_driver_init(wpa_s) < 0)
5818 return -1;
5819
5820#ifdef CONFIG_TDLS
Roshan Pius3a1667e2018-07-03 15:17:14 -07005821 if (!iface->p2p_mgmt && wpa_tdls_init(wpa_s->wpa))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 return -1;
5823#endif /* CONFIG_TDLS */
5824
5825 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
5826 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
5827 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
5828 return -1;
5829 }
5830
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005831#ifdef CONFIG_FST
5832 if (wpa_s->conf->fst_group_id) {
5833 struct fst_iface_cfg cfg;
5834 struct fst_wpa_obj iface_obj;
5835
5836 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
5837 os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
5838 sizeof(cfg.group_id));
5839 cfg.priority = wpa_s->conf->fst_priority;
5840 cfg.llt = wpa_s->conf->fst_llt;
5841
5842 wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
5843 &iface_obj, &cfg);
5844 if (!wpa_s->fst) {
5845 wpa_msg(wpa_s, MSG_ERROR,
5846 "FST: Cannot attach iface %s to group %s",
5847 wpa_s->ifname, cfg.group_id);
5848 return -1;
5849 }
5850 }
5851#endif /* CONFIG_FST */
5852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 if (wpas_wps_init(wpa_s))
5854 return -1;
5855
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005856#ifdef CONFIG_GAS_SERVER
5857 wpa_s->gas_server = gas_server_init(wpa_s, wpas_gas_server_tx);
5858 if (!wpa_s->gas_server) {
5859 wpa_printf(MSG_ERROR, "Failed to initialize GAS server");
5860 return -1;
5861 }
5862#endif /* CONFIG_GAS_SERVER */
5863
5864#ifdef CONFIG_DPP
5865 if (wpas_dpp_init(wpa_s) < 0)
5866 return -1;
5867#endif /* CONFIG_DPP */
5868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869 if (wpa_supplicant_init_eapol(wpa_s) < 0)
5870 return -1;
5871 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
5872
5873 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
5874 if (wpa_s->ctrl_iface == NULL) {
5875 wpa_printf(MSG_ERROR,
5876 "Failed to initialize control interface '%s'.\n"
5877 "You may have another wpa_supplicant process "
5878 "already running or the file was\n"
5879 "left by an unclean termination of wpa_supplicant "
5880 "in which case you will need\n"
5881 "to manually remove this file before starting "
5882 "wpa_supplicant again.\n",
5883 wpa_s->conf->ctrl_interface);
5884 return -1;
5885 }
5886
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005887 wpa_s->gas = gas_query_init(wpa_s);
5888 if (wpa_s->gas == NULL) {
5889 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
5890 return -1;
5891 }
5892
Roshan Pius3a1667e2018-07-03 15:17:14 -07005893 if ((!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) ||
5894 wpa_s->p2p_mgmt) &&
5895 wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005896 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
5897 return -1;
5898 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899
5900 if (wpa_bss_init(wpa_s) < 0)
5901 return -1;
5902
Paul Stewart092955c2017-02-06 09:13:09 -08005903#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
5904#ifdef CONFIG_MESH
5905 dl_list_init(&wpa_s->mesh_external_pmksa_cache);
5906#endif /* CONFIG_MESH */
5907#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
5908
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005909 /*
5910 * Set Wake-on-WLAN triggers, if configured.
5911 * Note: We don't restore/remove the triggers on shutdown (it doesn't
5912 * have effect anyway when the interface is down).
5913 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005914 if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07005915 return -1;
5916
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005917#ifdef CONFIG_EAP_PROXY
5918{
5919 size_t len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005920 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
5921 wpa_s->imsi, &len);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005922 if (wpa_s->mnc_len > 0) {
5923 wpa_s->imsi[len] = '\0';
5924 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
5925 wpa_s->imsi, wpa_s->mnc_len);
5926 } else {
5927 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
5928 }
5929}
5930#endif /* CONFIG_EAP_PROXY */
5931
Dmitry Shmidt04949592012-07-19 12:16:46 -07005932 if (pcsc_reader_init(wpa_s) < 0)
5933 return -1;
5934
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005935 if (wpas_init_ext_pw(wpa_s) < 0)
5936 return -1;
5937
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005938 wpas_rrm_reset(wpa_s);
5939
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005940 wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
5941
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08005942#ifdef CONFIG_HS20
5943 hs20_init(wpa_s);
5944#endif /* CONFIG_HS20 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005945#ifdef CONFIG_MBO
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005946 if (wpa_s->conf->oce) {
5947 if ((wpa_s->conf->oce & OCE_STA) &&
5948 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
5949 wpa_s->enable_oce = OCE_STA;
5950 if ((wpa_s->conf->oce & OCE_STA_CFON) &&
5951 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
5952 /* TODO: Need to add STA-CFON support */
5953 wpa_printf(MSG_ERROR,
5954 "OCE STA-CFON feature is not yet supported");
5955 }
5956 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005957 wpas_mbo_update_non_pref_chan(wpa_s, wpa_s->conf->non_pref_chan);
5958#endif /* CONFIG_MBO */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08005959
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07005960 wpa_supplicant_set_default_scan_ies(wpa_s);
5961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005962 return 0;
5963}
5964
5965
5966static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07005967 int notify, int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005968{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005969 struct wpa_global *global = wpa_s->global;
5970 struct wpa_supplicant *iface, *prev;
5971
5972 if (wpa_s == wpa_s->parent)
5973 wpas_p2p_group_remove(wpa_s, "*");
5974
5975 iface = global->ifaces;
5976 while (iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005977 if (iface->p2pdev == wpa_s)
5978 iface->p2pdev = iface->parent;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005979 if (iface == wpa_s || iface->parent != wpa_s) {
5980 iface = iface->next;
5981 continue;
5982 }
5983 wpa_printf(MSG_DEBUG,
5984 "Remove remaining child interface %s from parent %s",
5985 iface->ifname, wpa_s->ifname);
5986 prev = iface;
5987 iface = iface->next;
5988 wpa_supplicant_remove_iface(global, prev, terminate);
5989 }
5990
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005991 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992 if (wpa_s->drv_priv) {
5993 wpa_supplicant_deauthenticate(wpa_s,
5994 WLAN_REASON_DEAUTH_LEAVING);
5995
5996 wpa_drv_set_countermeasures(wpa_s, 0);
5997 wpa_clear_keys(wpa_s, NULL);
5998 }
5999
6000 wpa_supplicant_cleanup(wpa_s);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006001 wpas_p2p_deinit_iface(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006002
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006003 wpas_ctrl_radio_work_flush(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006004 radio_remove_interface(wpa_s);
6005
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006006#ifdef CONFIG_FST
6007 if (wpa_s->fst) {
6008 fst_detach(wpa_s->fst);
6009 wpa_s->fst = NULL;
6010 }
6011 if (wpa_s->received_mb_ies) {
6012 wpabuf_free(wpa_s->received_mb_ies);
6013 wpa_s->received_mb_ies = NULL;
6014 }
6015#endif /* CONFIG_FST */
6016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006017 if (wpa_s->drv_priv)
6018 wpa_drv_deinit(wpa_s);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07006019
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006020 if (notify)
6021 wpas_notify_iface_removed(wpa_s);
6022
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006023 if (terminate)
6024 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07006025
6026 if (wpa_s->ctrl_iface) {
6027 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
6028 wpa_s->ctrl_iface = NULL;
6029 }
6030
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006031#ifdef CONFIG_MESH
6032 if (wpa_s->ifmsh) {
6033 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
6034 wpa_s->ifmsh = NULL;
6035 }
6036#endif /* CONFIG_MESH */
6037
Irfan Sheriff622b66d2011-08-03 09:11:49 -07006038 if (wpa_s->conf != NULL) {
Irfan Sheriff622b66d2011-08-03 09:11:49 -07006039 wpa_config_free(wpa_s->conf);
6040 wpa_s->conf = NULL;
6041 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006042
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07006043 os_free(wpa_s->ssids_from_scan_req);
6044
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006045 os_free(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046}
6047
6048
Dmitry Shmidte4663042016-04-04 10:07:49 -07006049#ifdef CONFIG_MATCH_IFACE
6050
6051/**
6052 * wpa_supplicant_match_iface - Match an interface description to a name
6053 * @global: Pointer to global data from wpa_supplicant_init()
6054 * @ifname: Name of the interface to match
6055 * Returns: Pointer to the created interface description or %NULL on failure
6056 */
6057struct wpa_interface * wpa_supplicant_match_iface(struct wpa_global *global,
6058 const char *ifname)
6059{
6060 int i;
6061 struct wpa_interface *iface, *miface;
6062
6063 for (i = 0; i < global->params.match_iface_count; i++) {
6064 miface = &global->params.match_ifaces[i];
6065 if (!miface->ifname ||
6066 fnmatch(miface->ifname, ifname, 0) == 0) {
6067 iface = os_zalloc(sizeof(*iface));
6068 if (!iface)
6069 return NULL;
6070 *iface = *miface;
6071 iface->ifname = ifname;
6072 return iface;
6073 }
6074 }
6075
6076 return NULL;
6077}
6078
6079
6080/**
6081 * wpa_supplicant_match_existing - Match existing interfaces
6082 * @global: Pointer to global data from wpa_supplicant_init()
6083 * Returns: 0 on success, -1 on failure
6084 */
6085static int wpa_supplicant_match_existing(struct wpa_global *global)
6086{
6087 struct if_nameindex *ifi, *ifp;
6088 struct wpa_supplicant *wpa_s;
6089 struct wpa_interface *iface;
6090
6091 ifp = if_nameindex();
6092 if (!ifp) {
6093 wpa_printf(MSG_ERROR, "if_nameindex: %s", strerror(errno));
6094 return -1;
6095 }
6096
6097 for (ifi = ifp; ifi->if_name; ifi++) {
6098 wpa_s = wpa_supplicant_get_iface(global, ifi->if_name);
6099 if (wpa_s)
6100 continue;
6101 iface = wpa_supplicant_match_iface(global, ifi->if_name);
6102 if (iface) {
6103 wpa_s = wpa_supplicant_add_iface(global, iface, NULL);
6104 os_free(iface);
6105 if (wpa_s)
6106 wpa_s->matched = 1;
6107 }
6108 }
6109
6110 if_freenameindex(ifp);
6111 return 0;
6112}
6113
6114#endif /* CONFIG_MATCH_IFACE */
6115
6116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117/**
6118 * wpa_supplicant_add_iface - Add a new network interface
6119 * @global: Pointer to global data from wpa_supplicant_init()
6120 * @iface: Interface configuration options
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006121 * @parent: Parent interface or %NULL to assign new interface as parent
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122 * Returns: Pointer to the created interface or %NULL on failure
6123 *
6124 * This function is used to add new network interfaces for %wpa_supplicant.
6125 * This can be called before wpa_supplicant_run() to add interfaces before the
6126 * main event loop has been started. In addition, new interfaces can be added
6127 * dynamically while %wpa_supplicant is already running. This could happen,
6128 * e.g., when a hotplug network adapter is inserted.
6129 */
6130struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006131 struct wpa_interface *iface,
6132 struct wpa_supplicant *parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133{
6134 struct wpa_supplicant *wpa_s;
6135 struct wpa_interface t_iface;
6136 struct wpa_ssid *ssid;
6137
6138 if (global == NULL || iface == NULL)
6139 return NULL;
6140
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006141 wpa_s = wpa_supplicant_alloc(parent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142 if (wpa_s == NULL)
6143 return NULL;
6144
6145 wpa_s->global = global;
6146
6147 t_iface = *iface;
6148 if (global->params.override_driver) {
6149 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
6150 "('%s' -> '%s')",
6151 iface->driver, global->params.override_driver);
6152 t_iface.driver = global->params.override_driver;
6153 }
6154 if (global->params.override_ctrl_interface) {
6155 wpa_printf(MSG_DEBUG, "Override interface parameter: "
6156 "ctrl_interface ('%s' -> '%s')",
6157 iface->ctrl_interface,
6158 global->params.override_ctrl_interface);
6159 t_iface.ctrl_interface =
6160 global->params.override_ctrl_interface;
6161 }
6162 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
6163 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
6164 iface->ifname);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006165 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006166 return NULL;
6167 }
6168
Roshan Piusd6d8b8d2016-11-08 14:45:26 -08006169 /* Notify the control interfaces about new iface */
6170 if (wpas_notify_iface_added(wpa_s)) {
6171 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
6172 return NULL;
6173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174
Roshan Piusd6d8b8d2016-11-08 14:45:26 -08006175 /* Notify the control interfaces about new networks for non p2p mgmt
6176 * ifaces. */
6177 if (iface->p2p_mgmt == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006178 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
6179 wpas_notify_network_added(wpa_s, ssid);
6180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181
6182 wpa_s->next = global->ifaces;
6183 global->ifaces = wpa_s;
6184
6185 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006186 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006188#ifdef CONFIG_P2P
6189 if (wpa_s->global->p2p == NULL &&
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006190 !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006191 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006192 wpas_p2p_add_p2pdev_interface(
6193 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006194 wpa_printf(MSG_INFO,
6195 "P2P: Failed to enable P2P Device interface");
6196 /* Try to continue without. P2P will be disabled. */
6197 }
6198#endif /* CONFIG_P2P */
6199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 return wpa_s;
6201}
6202
6203
6204/**
6205 * wpa_supplicant_remove_iface - Remove a network interface
6206 * @global: Pointer to global data from wpa_supplicant_init()
6207 * @wpa_s: Pointer to the network interface to be removed
6208 * Returns: 0 if interface was removed, -1 if interface was not found
6209 *
6210 * This function can be used to dynamically remove network interfaces from
6211 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
6212 * addition, this function is used to remove all remaining interfaces when
6213 * %wpa_supplicant is terminated.
6214 */
6215int wpa_supplicant_remove_iface(struct wpa_global *global,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006216 struct wpa_supplicant *wpa_s,
6217 int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218{
6219 struct wpa_supplicant *prev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006220#ifdef CONFIG_MESH
6221 unsigned int mesh_if_created = wpa_s->mesh_if_created;
6222 char *ifname = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006223 struct wpa_supplicant *parent = wpa_s->parent;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006224#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225
6226 /* Remove interface from the global list of interfaces */
6227 prev = global->ifaces;
6228 if (prev == wpa_s) {
6229 global->ifaces = wpa_s->next;
6230 } else {
6231 while (prev && prev->next != wpa_s)
6232 prev = prev->next;
6233 if (prev == NULL)
6234 return -1;
6235 prev->next = wpa_s->next;
6236 }
6237
6238 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
6239
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006240#ifdef CONFIG_MESH
6241 if (mesh_if_created) {
6242 ifname = os_strdup(wpa_s->ifname);
6243 if (ifname == NULL) {
6244 wpa_dbg(wpa_s, MSG_ERROR,
6245 "mesh: Failed to malloc ifname");
6246 return -1;
6247 }
6248 }
6249#endif /* CONFIG_MESH */
6250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 if (global->p2p_group_formation == wpa_s)
6252 global->p2p_group_formation = NULL;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006253 if (global->p2p_invite_group == wpa_s)
6254 global->p2p_invite_group = NULL;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006255 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006257#ifdef CONFIG_MESH
6258 if (mesh_if_created) {
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006259 wpa_drv_if_remove(parent, WPA_IF_MESH, ifname);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006260 os_free(ifname);
6261 }
6262#endif /* CONFIG_MESH */
6263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006264 return 0;
6265}
6266
6267
6268/**
6269 * wpa_supplicant_get_eap_mode - Get the current EAP mode
6270 * @wpa_s: Pointer to the network interface
6271 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
6272 */
6273const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
6274{
6275 const char *eapol_method;
6276
6277 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
6278 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
6279 return "NO-EAP";
6280 }
6281
6282 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
6283 if (eapol_method == NULL)
6284 return "UNKNOWN-EAP";
6285
6286 return eapol_method;
6287}
6288
6289
6290/**
6291 * wpa_supplicant_get_iface - Get a new network interface
6292 * @global: Pointer to global data from wpa_supplicant_init()
6293 * @ifname: Interface name
6294 * Returns: Pointer to the interface or %NULL if not found
6295 */
6296struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
6297 const char *ifname)
6298{
6299 struct wpa_supplicant *wpa_s;
6300
6301 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6302 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6303 return wpa_s;
6304 }
6305 return NULL;
6306}
6307
6308
6309#ifndef CONFIG_NO_WPA_MSG
6310static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
6311{
6312 struct wpa_supplicant *wpa_s = ctx;
6313 if (wpa_s == NULL)
6314 return NULL;
6315 return wpa_s->ifname;
6316}
6317#endif /* CONFIG_NO_WPA_MSG */
6318
6319
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006320#ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
6321#define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
6322#endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
6323
6324/* Periodic cleanup tasks */
6325static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
6326{
6327 struct wpa_global *global = eloop_ctx;
6328 struct wpa_supplicant *wpa_s;
6329
6330 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
6331 wpas_periodic, global, NULL);
6332
6333#ifdef CONFIG_P2P
6334 if (global->p2p)
6335 p2p_expire_peers(global->p2p);
6336#endif /* CONFIG_P2P */
6337
6338 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6339 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
6340#ifdef CONFIG_AP
6341 ap_periodic(wpa_s);
6342#endif /* CONFIG_AP */
6343 }
6344}
6345
6346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347/**
6348 * wpa_supplicant_init - Initialize %wpa_supplicant
6349 * @params: Parameters for %wpa_supplicant
6350 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
6351 *
6352 * This function is used to initialize %wpa_supplicant. After successful
6353 * initialization, the returned data pointer can be used to add and remove
6354 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
6355 */
6356struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
6357{
6358 struct wpa_global *global;
6359 int ret, i;
6360
6361 if (params == NULL)
6362 return NULL;
6363
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006364#ifdef CONFIG_DRIVER_NDIS
6365 {
6366 void driver_ndis_init_ops(void);
6367 driver_ndis_init_ops();
6368 }
6369#endif /* CONFIG_DRIVER_NDIS */
6370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006371#ifndef CONFIG_NO_WPA_MSG
6372 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
6373#endif /* CONFIG_NO_WPA_MSG */
6374
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006375 if (params->wpa_debug_file_path)
6376 wpa_debug_open_file(params->wpa_debug_file_path);
6377 else
6378 wpa_debug_setup_stdout();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379 if (params->wpa_debug_syslog)
6380 wpa_debug_open_syslog();
Dmitry Shmidt04949592012-07-19 12:16:46 -07006381 if (params->wpa_debug_tracing) {
6382 ret = wpa_debug_open_linux_tracing();
6383 if (ret) {
6384 wpa_printf(MSG_ERROR,
6385 "Failed to enable trace logging");
6386 return NULL;
6387 }
6388 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006389
6390 ret = eap_register_methods();
6391 if (ret) {
6392 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
6393 if (ret == -2)
6394 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
6395 "the same EAP type.");
6396 return NULL;
6397 }
6398
6399 global = os_zalloc(sizeof(*global));
6400 if (global == NULL)
6401 return NULL;
6402 dl_list_init(&global->p2p_srv_bonjour);
6403 dl_list_init(&global->p2p_srv_upnp);
6404 global->params.daemonize = params->daemonize;
6405 global->params.wait_for_monitor = params->wait_for_monitor;
6406 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
6407 if (params->pid_file)
6408 global->params.pid_file = os_strdup(params->pid_file);
6409 if (params->ctrl_interface)
6410 global->params.ctrl_interface =
6411 os_strdup(params->ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006412 if (params->ctrl_interface_group)
6413 global->params.ctrl_interface_group =
6414 os_strdup(params->ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006415 if (params->override_driver)
6416 global->params.override_driver =
6417 os_strdup(params->override_driver);
6418 if (params->override_ctrl_interface)
6419 global->params.override_ctrl_interface =
6420 os_strdup(params->override_ctrl_interface);
Dmitry Shmidte4663042016-04-04 10:07:49 -07006421#ifdef CONFIG_MATCH_IFACE
6422 global->params.match_iface_count = params->match_iface_count;
6423 if (params->match_iface_count) {
6424 global->params.match_ifaces =
6425 os_calloc(params->match_iface_count,
6426 sizeof(struct wpa_interface));
6427 os_memcpy(global->params.match_ifaces,
6428 params->match_ifaces,
6429 params->match_iface_count *
6430 sizeof(struct wpa_interface));
6431 }
6432#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006433#ifdef CONFIG_P2P
Sasha Levitskiydaa60e52015-08-05 13:02:59 -07006434 if (params->conf_p2p_dev)
6435 global->params.conf_p2p_dev =
6436 os_strdup(params->conf_p2p_dev);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006437#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006438 wpa_debug_level = global->params.wpa_debug_level =
6439 params->wpa_debug_level;
6440 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
6441 params->wpa_debug_show_keys;
6442 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
6443 params->wpa_debug_timestamp;
6444
6445 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
6446
6447 if (eloop_init()) {
6448 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
6449 wpa_supplicant_deinit(global);
6450 return NULL;
6451 }
6452
Jouni Malinen75ecf522011-06-27 15:19:46 -07006453 random_init(params->entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454
6455 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
6456 if (global->ctrl_iface == NULL) {
6457 wpa_supplicant_deinit(global);
6458 return NULL;
6459 }
6460
6461 if (wpas_notify_supplicant_initialized(global)) {
6462 wpa_supplicant_deinit(global);
6463 return NULL;
6464 }
6465
6466 for (i = 0; wpa_drivers[i]; i++)
6467 global->drv_count++;
6468 if (global->drv_count == 0) {
6469 wpa_printf(MSG_ERROR, "No drivers enabled");
6470 wpa_supplicant_deinit(global);
6471 return NULL;
6472 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006473 global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474 if (global->drv_priv == NULL) {
6475 wpa_supplicant_deinit(global);
6476 return NULL;
6477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006479#ifdef CONFIG_WIFI_DISPLAY
6480 if (wifi_display_init(global) < 0) {
6481 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
6482 wpa_supplicant_deinit(global);
6483 return NULL;
6484 }
6485#endif /* CONFIG_WIFI_DISPLAY */
6486
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006487 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
6488 wpas_periodic, global, NULL);
6489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006490 return global;
6491}
6492
6493
6494/**
6495 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
6496 * @global: Pointer to global data from wpa_supplicant_init()
6497 * Returns: 0 after successful event loop run, -1 on failure
6498 *
6499 * This function starts the main event loop and continues running as long as
6500 * there are any remaining events. In most cases, this function is running as
6501 * long as the %wpa_supplicant process in still in use.
6502 */
6503int wpa_supplicant_run(struct wpa_global *global)
6504{
6505 struct wpa_supplicant *wpa_s;
6506
6507 if (global->params.daemonize &&
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08006508 (wpa_supplicant_daemon(global->params.pid_file) ||
6509 eloop_sock_requeue()))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510 return -1;
6511
Dmitry Shmidte4663042016-04-04 10:07:49 -07006512#ifdef CONFIG_MATCH_IFACE
6513 if (wpa_supplicant_match_existing(global))
6514 return -1;
6515#endif
6516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 if (global->params.wait_for_monitor) {
6518 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08006519 if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520 wpa_supplicant_ctrl_iface_wait(
6521 wpa_s->ctrl_iface);
6522 }
6523
6524 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
6525 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
6526
6527 eloop_run();
6528
6529 return 0;
6530}
6531
6532
6533/**
6534 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
6535 * @global: Pointer to global data from wpa_supplicant_init()
6536 *
6537 * This function is called to deinitialize %wpa_supplicant and to free all
6538 * allocated resources. Remaining network interfaces will also be removed.
6539 */
6540void wpa_supplicant_deinit(struct wpa_global *global)
6541{
6542 int i;
6543
6544 if (global == NULL)
6545 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006546
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006547 eloop_cancel_timeout(wpas_periodic, global, NULL);
6548
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006549#ifdef CONFIG_WIFI_DISPLAY
6550 wifi_display_deinit(global);
6551#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006552
6553 while (global->ifaces)
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07006554 wpa_supplicant_remove_iface(global, global->ifaces, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555
6556 if (global->ctrl_iface)
6557 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
6558
6559 wpas_notify_supplicant_deinitialized(global);
6560
6561 eap_peer_unregister_methods();
6562#ifdef CONFIG_AP
6563 eap_server_unregister_methods();
6564#endif /* CONFIG_AP */
6565
6566 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
6567 if (!global->drv_priv[i])
6568 continue;
6569 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
6570 }
6571 os_free(global->drv_priv);
6572
6573 random_deinit();
6574
6575 eloop_destroy();
6576
6577 if (global->params.pid_file) {
6578 os_daemonize_terminate(global->params.pid_file);
6579 os_free(global->params.pid_file);
6580 }
6581 os_free(global->params.ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006582 os_free(global->params.ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583 os_free(global->params.override_driver);
6584 os_free(global->params.override_ctrl_interface);
Dmitry Shmidte4663042016-04-04 10:07:49 -07006585#ifdef CONFIG_MATCH_IFACE
6586 os_free(global->params.match_ifaces);
6587#endif /* CONFIG_MATCH_IFACE */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006588#ifdef CONFIG_P2P
Sasha Levitskiydaa60e52015-08-05 13:02:59 -07006589 os_free(global->params.conf_p2p_dev);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006590#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006591
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07006592 os_free(global->p2p_disallow_freq.range);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006593 os_free(global->p2p_go_avoid_freq.range);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006594 os_free(global->add_psk);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596 os_free(global);
6597 wpa_debug_close_syslog();
6598 wpa_debug_close_file();
Dmitry Shmidt04949592012-07-19 12:16:46 -07006599 wpa_debug_close_linux_tracing();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600}
6601
6602
6603void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
6604{
6605 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6606 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6607 char country[3];
6608 country[0] = wpa_s->conf->country[0];
6609 country[1] = wpa_s->conf->country[1];
6610 country[2] = '\0';
6611 if (wpa_drv_set_country(wpa_s, country) < 0) {
6612 wpa_printf(MSG_ERROR, "Failed to set country code "
6613 "'%s'", country);
6614 }
6615 }
6616
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006617 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
6618 wpas_init_ext_pw(wpa_s);
6619
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006620 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
6621 wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
6622
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006623 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WOWLAN_TRIGGERS) {
6624 struct wpa_driver_capa capa;
6625 int res = wpa_drv_get_capa(wpa_s, &capa);
6626
6627 if (res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
6628 wpa_printf(MSG_ERROR,
6629 "Failed to update wowlan_triggers to '%s'",
6630 wpa_s->conf->wowlan_triggers);
6631 }
6632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633#ifdef CONFIG_WPS
6634 wpas_wps_update_config(wpa_s);
6635#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006636 wpas_p2p_update_config(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006637 wpa_s->conf->changed_parameters = 0;
6638}
6639
6640
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006641void add_freq(int *freqs, int *num_freqs, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006642{
6643 int i;
6644
6645 for (i = 0; i < *num_freqs; i++) {
6646 if (freqs[i] == freq)
6647 return;
6648 }
6649
6650 freqs[*num_freqs] = freq;
6651 (*num_freqs)++;
6652}
6653
6654
6655static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
6656{
6657 struct wpa_bss *bss, *cbss;
6658 const int max_freqs = 10;
6659 int *freqs;
6660 int num_freqs = 0;
6661
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006662 freqs = os_calloc(max_freqs + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006663 if (freqs == NULL)
6664 return NULL;
6665
6666 cbss = wpa_s->current_bss;
6667
6668 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
6669 if (bss == cbss)
6670 continue;
6671 if (bss->ssid_len == cbss->ssid_len &&
6672 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
6673 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
6674 add_freq(freqs, &num_freqs, bss->freq);
6675 if (num_freqs == max_freqs)
6676 break;
6677 }
6678 }
6679
6680 if (num_freqs == 0) {
6681 os_free(freqs);
6682 freqs = NULL;
6683 }
6684
6685 return freqs;
6686}
6687
6688
6689void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
6690{
6691 int timeout;
6692 int count;
6693 int *freqs = NULL;
6694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006695 wpas_connect_work_done(wpa_s);
6696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697 /*
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006698 * Remove possible authentication timeout since the connection failed.
6699 */
6700 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
6701
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006702 /*
6703 * There is no point in blacklisting the AP if this event is
6704 * generated based on local request to disconnect.
6705 */
6706 if (wpa_s->own_disconnect_req) {
6707 wpa_s->own_disconnect_req = 0;
6708 wpa_dbg(wpa_s, MSG_DEBUG,
6709 "Ignore connection failure due to local request to disconnect");
6710 return;
6711 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006712 if (wpa_s->disconnected) {
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006713 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
6714 "indication since interface has been put into "
6715 "disconnected state");
6716 return;
6717 }
Roshan Piusb1ae0fe2019-02-15 08:05:38 -08006718 if (wpa_s->auto_reconnect_disabled) {
6719 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
6720 "indication since auto connect is disabled");
6721 return;
6722 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07006723
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006724 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725 * Add the failed BSSID into the blacklist and speed up next scan
6726 * attempt if there could be other APs that could accept association.
6727 * The current blacklist count indicates how many times we have tried
6728 * connecting to this AP and multiple attempts mean that other APs are
6729 * either not available or has already been tried, so that we can start
6730 * increasing the delay here to avoid constant scanning.
6731 */
6732 count = wpa_blacklist_add(wpa_s, bssid);
6733 if (count == 1 && wpa_s->current_bss) {
6734 /*
6735 * This BSS was not in the blacklist before. If there is
6736 * another BSS available for the same ESS, we should try that
6737 * next. Otherwise, we may as well try this one once more
6738 * before allowing other, likely worse, ESSes to be considered.
6739 */
6740 freqs = get_bss_freqs_in_ess(wpa_s);
6741 if (freqs) {
6742 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
6743 "has been seen; try it next");
6744 wpa_blacklist_add(wpa_s, bssid);
6745 /*
6746 * On the next scan, go through only the known channels
6747 * used in this ESS based on previous scans to speed up
6748 * common load balancing use case.
6749 */
6750 os_free(wpa_s->next_scan_freqs);
6751 wpa_s->next_scan_freqs = freqs;
6752 }
6753 }
6754
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006755 /*
6756 * Add previous failure count in case the temporary blacklist was
6757 * cleared due to no other BSSes being available.
6758 */
6759 count += wpa_s->extra_blacklist_count;
6760
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006761 if (count > 3 && wpa_s->current_ssid) {
6762 wpa_printf(MSG_DEBUG, "Continuous association failures - "
6763 "consider temporary network disabling");
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07006764 wpas_auth_failed(wpa_s, "CONN_FAILED");
Dmitry Shmidt4b060592013-04-29 16:42:49 -07006765 }
6766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 switch (count) {
6768 case 1:
6769 timeout = 100;
6770 break;
6771 case 2:
6772 timeout = 500;
6773 break;
6774 case 3:
6775 timeout = 1000;
6776 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006777 case 4:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006778 timeout = 5000;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006779 break;
6780 default:
6781 timeout = 10000;
6782 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006783 }
6784
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006785 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
6786 "ms", count, timeout);
6787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006788 /*
6789 * TODO: if more than one possible AP is available in scan results,
6790 * could try the other ones before requesting a new scan.
6791 */
Hai Shalom021b0b52019-04-10 11:17:58 -07006792
6793 /* speed up the connection attempt with normal scan */
6794 wpa_s->normal_scans = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
6796 1000 * (timeout % 1000));
6797}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006798
6799
Hai Shalomce48b4a2018-09-05 11:41:35 -07006800#ifdef CONFIG_FILS
6801void fils_connection_failure(struct wpa_supplicant *wpa_s)
6802{
6803 struct wpa_ssid *ssid = wpa_s->current_ssid;
6804 const u8 *realm, *username, *rrk;
6805 size_t realm_len, username_len, rrk_len;
6806 u16 next_seq_num;
6807
6808 if (!ssid || !ssid->eap.erp || !wpa_key_mgmt_fils(ssid->key_mgmt) ||
6809 eapol_sm_get_erp_info(wpa_s->eapol, &ssid->eap,
6810 &username, &username_len,
6811 &realm, &realm_len, &next_seq_num,
6812 &rrk, &rrk_len) != 0 ||
6813 !realm)
6814 return;
6815
6816 wpa_hexdump_ascii(MSG_DEBUG,
6817 "FILS: Store last connection failure realm",
6818 realm, realm_len);
6819 os_free(wpa_s->last_con_fail_realm);
6820 wpa_s->last_con_fail_realm = os_malloc(realm_len);
6821 if (wpa_s->last_con_fail_realm) {
6822 wpa_s->last_con_fail_realm_len = realm_len;
6823 os_memcpy(wpa_s->last_con_fail_realm, realm, realm_len);
6824 }
6825}
6826#endif /* CONFIG_FILS */
6827
6828
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006829int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
6830{
6831 return wpa_s->conf->ap_scan == 2 ||
6832 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
6833}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006834
Dmitry Shmidt04949592012-07-19 12:16:46 -07006835
Roshan Pius57ffbcf2016-09-27 09:12:46 -07006836#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW) || defined (CONFIG_CTRL_IFACE_HIDL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006837int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
6838 struct wpa_ssid *ssid,
6839 const char *field,
6840 const char *value)
6841{
6842#ifdef IEEE8021X_EAPOL
Roshan Pius71a6b8a2016-08-17 13:04:08 -07006843 enum wpa_ctrl_req_type rtype;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006844
6845 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
6846 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
6847 (const u8 *) value, os_strlen(value));
6848
Roshan Pius71a6b8a2016-08-17 13:04:08 -07006849 rtype = wpa_supplicant_ctrl_req_from_string(field);
pkanwareb9203e2017-10-26 16:00:35 -07006850 return wpa_supplicant_ctrl_rsp_handle(wpa_s, ssid, rtype, value, strlen(value));
Roshan Pius71a6b8a2016-08-17 13:04:08 -07006851#else /* IEEE8021X_EAPOL */
6852 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
6853 return -1;
6854#endif /* IEEE8021X_EAPOL */
6855}
6856
6857int wpa_supplicant_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
6858 struct wpa_ssid *ssid,
6859 enum wpa_ctrl_req_type rtype,
pkanwareb9203e2017-10-26 16:00:35 -07006860 const char *value, int value_len)
Roshan Pius71a6b8a2016-08-17 13:04:08 -07006861{
6862#ifdef IEEE8021X_EAPOL
6863 struct eap_peer_config *eap = &ssid->eap;
Ecco Park00a7b212018-01-26 13:44:44 -08006864 char *identity, *imsi_identity;
Roshan Pius71a6b8a2016-08-17 13:04:08 -07006865
6866 switch (rtype) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006867 case WPA_CTRL_REQ_EAP_IDENTITY:
6868 os_free(eap->identity);
Ecco Park00a7b212018-01-26 13:44:44 -08006869 os_free(eap->imsi_identity);
6870 if (value == NULL)
6871 return -1;
6872 identity = os_strchr(value, ':');
6873 if (identity == NULL) {
6874 /* plain identity */
6875 eap->identity = (u8 *)os_strdup(value);
6876 eap->identity_len = os_strlen(value);
6877 } else {
6878 /* have both plain identity and encrypted identity */
6879 imsi_identity = value;
6880 *identity++ = '\0';
6881 /* plain identity */
6882 eap->imsi_identity = (u8 *)dup_binstr(imsi_identity, strlen(imsi_identity));
6883 eap->imsi_identity_len = strlen(imsi_identity);
6884 /* encrypted identity */
6885 eap->identity = (u8 *)dup_binstr(identity,
6886 value_len - strlen(imsi_identity) - 1);
6887 eap->identity_len = value_len - strlen(imsi_identity) - 1;
6888 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006889 eap->pending_req_identity = 0;
6890 if (ssid == wpa_s->current_ssid)
6891 wpa_s->reassociate = 1;
6892 break;
6893 case WPA_CTRL_REQ_EAP_PASSWORD:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006894 bin_clear_free(eap->password, eap->password_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006895 eap->password = (u8 *) os_strdup(value);
pkanwareb9203e2017-10-26 16:00:35 -07006896 eap->password_len = value_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006897 eap->pending_req_password = 0;
6898 if (ssid == wpa_s->current_ssid)
6899 wpa_s->reassociate = 1;
6900 break;
6901 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006902 bin_clear_free(eap->new_password, eap->new_password_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006903 eap->new_password = (u8 *) os_strdup(value);
pkanwareb9203e2017-10-26 16:00:35 -07006904 eap->new_password_len = value_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006905 eap->pending_req_new_password = 0;
6906 if (ssid == wpa_s->current_ssid)
6907 wpa_s->reassociate = 1;
6908 break;
6909 case WPA_CTRL_REQ_EAP_PIN:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006910 str_clear_free(eap->pin);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006911 eap->pin = os_strdup(value);
6912 eap->pending_req_pin = 0;
6913 if (ssid == wpa_s->current_ssid)
6914 wpa_s->reassociate = 1;
6915 break;
6916 case WPA_CTRL_REQ_EAP_OTP:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006917 bin_clear_free(eap->otp, eap->otp_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006918 eap->otp = (u8 *) os_strdup(value);
pkanwareb9203e2017-10-26 16:00:35 -07006919 eap->otp_len = value_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006920 os_free(eap->pending_req_otp);
6921 eap->pending_req_otp = NULL;
6922 eap->pending_req_otp_len = 0;
6923 break;
6924 case WPA_CTRL_REQ_EAP_PASSPHRASE:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006925 str_clear_free(eap->private_key_passwd);
6926 eap->private_key_passwd = os_strdup(value);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006927 eap->pending_req_passphrase = 0;
6928 if (ssid == wpa_s->current_ssid)
6929 wpa_s->reassociate = 1;
6930 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006931 case WPA_CTRL_REQ_SIM:
Dmitry Shmidtc2817022014-07-02 10:32:10 -07006932 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt051af732013-10-22 13:52:46 -07006933 eap->external_sim_resp = os_strdup(value);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006934 eap->pending_req_sim = 0;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006935 break;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006936 case WPA_CTRL_REQ_PSK_PASSPHRASE:
6937 if (wpa_config_set(ssid, "psk", value, 0) < 0)
6938 return -1;
6939 ssid->mem_only_psk = 1;
6940 if (ssid->passphrase)
6941 wpa_config_update_psk(ssid);
6942 if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
6943 wpa_supplicant_req_scan(wpa_s, 0, 0);
6944 break;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08006945 case WPA_CTRL_REQ_EXT_CERT_CHECK:
6946 if (eap->pending_ext_cert_check != PENDING_CHECK)
6947 return -1;
6948 if (os_strcmp(value, "good") == 0)
6949 eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
6950 else if (os_strcmp(value, "bad") == 0)
6951 eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
6952 else
6953 return -1;
6954 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006955 default:
Roshan Pius71a6b8a2016-08-17 13:04:08 -07006956 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown type %d", rtype);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006957 return -1;
6958 }
6959
6960 return 0;
6961#else /* IEEE8021X_EAPOL */
6962 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
6963 return -1;
6964#endif /* IEEE8021X_EAPOL */
6965}
Roshan Pius57ffbcf2016-09-27 09:12:46 -07006966#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW || CONFIG_CTRL_IFACE_HIDL */
Dmitry Shmidt04949592012-07-19 12:16:46 -07006967
6968
6969int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
6970{
6971 int i;
6972 unsigned int drv_enc;
6973
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006974 if (wpa_s->p2p_mgmt)
6975 return 1; /* no normal network profiles on p2p_mgmt interface */
6976
Dmitry Shmidt04949592012-07-19 12:16:46 -07006977 if (ssid == NULL)
6978 return 1;
6979
6980 if (ssid->disabled)
6981 return 1;
6982
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006983 if (wpa_s->drv_capa_known)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006984 drv_enc = wpa_s->drv_enc;
6985 else
6986 drv_enc = (unsigned int) -1;
6987
6988 for (i = 0; i < NUM_WEP_KEYS; i++) {
6989 size_t len = ssid->wep_key_len[i];
6990 if (len == 0)
6991 continue;
6992 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
6993 continue;
6994 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
6995 continue;
6996 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
6997 continue;
6998 return 1; /* invalid WEP key */
6999 }
7000
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007001 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007002 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007003 !(wpa_key_mgmt_sae(ssid->key_mgmt) && ssid->sae_password) &&
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007004 !ssid->mem_only_psk)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007005 return 1;
7006
Dmitry Shmidt04949592012-07-19 12:16:46 -07007007 return 0;
7008}
7009
7010
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007011int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
7012{
7013#ifdef CONFIG_IEEE80211W
7014 if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
7015 if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
7016 !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
7017 /*
7018 * Driver does not support BIP -- ignore pmf=1 default
7019 * since the connection with PMF would fail and the
7020 * configuration does not require PMF to be enabled.
7021 */
7022 return NO_MGMT_FRAME_PROTECTION;
7023 }
7024
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007025 if (ssid &&
7026 (ssid->key_mgmt &
7027 ~(WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPS |
7028 WPA_KEY_MGMT_IEEE8021X_NO_WPA)) == 0) {
7029 /*
7030 * Do not use the default PMF value for non-RSN networks
7031 * since PMF is available only with RSN and pmf=2
7032 * configuration would otherwise prevent connections to
7033 * all open networks.
7034 */
7035 return NO_MGMT_FRAME_PROTECTION;
7036 }
7037
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007038 return wpa_s->conf->pmf;
7039 }
7040
7041 return ssid->ieee80211w;
7042#else /* CONFIG_IEEE80211W */
7043 return NO_MGMT_FRAME_PROTECTION;
7044#endif /* CONFIG_IEEE80211W */
7045}
7046
7047
Dmitry Shmidt687922c2012-03-26 14:02:32 -07007048int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007049{
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07007050 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007051 return 1;
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07007052 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
Dmitry Shmidt687922c2012-03-26 14:02:32 -07007053 return 0;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07007054 return -1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007055}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007056
7057
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007058void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007059{
7060 struct wpa_ssid *ssid = wpa_s->current_ssid;
7061 int dur;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007062 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007063
7064 if (ssid == NULL) {
7065 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
7066 "SSID block");
7067 return;
7068 }
7069
7070 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
7071 return;
7072
7073 ssid->auth_failures++;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07007074
7075#ifdef CONFIG_P2P
7076 if (ssid->p2p_group &&
7077 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
7078 /*
7079 * Skip the wait time since there is a short timeout on the
7080 * connection to a P2P group.
7081 */
7082 return;
7083 }
7084#endif /* CONFIG_P2P */
7085
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007086 if (ssid->auth_failures > 50)
7087 dur = 300;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007088 else if (ssid->auth_failures > 10)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007089 dur = 120;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007090 else if (ssid->auth_failures > 5)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007091 dur = 90;
7092 else if (ssid->auth_failures > 3)
7093 dur = 60;
7094 else if (ssid->auth_failures > 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007095 dur = 30;
7096 else if (ssid->auth_failures > 1)
7097 dur = 20;
7098 else
7099 dur = 10;
7100
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007101 if (ssid->auth_failures > 1 &&
7102 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
7103 dur += os_random() % (ssid->auth_failures * 10);
7104
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007105 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007106 if (now.sec + dur <= ssid->disabled_until.sec)
7107 return;
7108
7109 ssid->disabled_until.sec = now.sec + dur;
7110
7111 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007112 "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007113 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07007114 ssid->auth_failures, dur, reason);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007115}
7116
7117
7118void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
7119 struct wpa_ssid *ssid, int clear_failures)
7120{
7121 if (ssid == NULL)
7122 return;
7123
7124 if (ssid->disabled_until.sec) {
7125 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
7126 "id=%d ssid=\"%s\"",
7127 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
7128 }
7129 ssid->disabled_until.sec = 0;
7130 ssid->disabled_until.usec = 0;
7131 if (clear_failures)
7132 ssid->auth_failures = 0;
7133}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007134
7135
7136int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
7137{
7138 size_t i;
7139
7140 if (wpa_s->disallow_aps_bssid == NULL)
7141 return 0;
7142
7143 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
7144 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
7145 bssid, ETH_ALEN) == 0)
7146 return 1;
7147 }
7148
7149 return 0;
7150}
7151
7152
7153int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
7154 size_t ssid_len)
7155{
7156 size_t i;
7157
7158 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
7159 return 0;
7160
7161 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
7162 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
7163 if (ssid_len == s->ssid_len &&
7164 os_memcmp(ssid, s->ssid, ssid_len) == 0)
7165 return 1;
7166 }
7167
7168 return 0;
7169}
7170
7171
7172/**
7173 * wpas_request_connection - Request a new connection
7174 * @wpa_s: Pointer to the network interface
7175 *
7176 * This function is used to request a new connection to be found. It will mark
7177 * the interface to allow reassociation and request a new scan to find a
7178 * suitable network to connect to.
7179 */
7180void wpas_request_connection(struct wpa_supplicant *wpa_s)
7181{
7182 wpa_s->normal_scans = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007183 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007184 wpa_supplicant_reinit_autoscan(wpa_s);
7185 wpa_s->extra_blacklist_count = 0;
7186 wpa_s->disconnected = 0;
7187 wpa_s->reassociate = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007188 wpa_s->last_owe_group = 0;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08007189
7190 if (wpa_supplicant_fast_associate(wpa_s) != 1)
7191 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007192 else
7193 wpa_s->reattach = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007194}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007195
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007196
Roshan Pius02242d72016-08-09 15:31:48 -07007197/**
7198 * wpas_request_disconnection - Request disconnection
7199 * @wpa_s: Pointer to the network interface
7200 *
7201 * This function is used to request disconnection from the currently connected
7202 * network. This will stop any ongoing scans and initiate deauthentication.
7203 */
7204void wpas_request_disconnection(struct wpa_supplicant *wpa_s)
7205{
7206#ifdef CONFIG_SME
7207 wpa_s->sme.prev_bssid_set = 0;
7208#endif /* CONFIG_SME */
7209 wpa_s->reassociate = 0;
7210 wpa_s->disconnected = 1;
7211 wpa_supplicant_cancel_sched_scan(wpa_s);
7212 wpa_supplicant_cancel_scan(wpa_s);
7213 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
7214 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
Hai Shalom021b0b52019-04-10 11:17:58 -07007215 radio_remove_works(wpa_s, "connect", 0);
7216 radio_remove_works(wpa_s, "sme-connect", 0);
Roshan Pius02242d72016-08-09 15:31:48 -07007217}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007218
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07007219
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007220void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
7221 struct wpa_used_freq_data *freqs_data,
7222 unsigned int len)
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08007223{
7224 unsigned int i;
7225
7226 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
7227 len, title);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007228 for (i = 0; i < len; i++) {
7229 struct wpa_used_freq_data *cur = &freqs_data[i];
7230 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
7231 i, cur->freq, cur->flags);
7232 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08007233}
7234
7235
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007236/*
7237 * Find the operating frequencies of any of the virtual interfaces that
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007238 * are using the same radio as the current interface, and in addition, get
7239 * information about the interface types that are using the frequency.
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007240 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007241int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
7242 struct wpa_used_freq_data *freqs_data,
7243 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007244{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007245 struct wpa_supplicant *ifs;
7246 u8 bssid[ETH_ALEN];
7247 int freq;
7248 unsigned int idx = 0, i;
7249
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08007250 wpa_dbg(wpa_s, MSG_DEBUG,
7251 "Determining shared radio frequencies (max len %u)", len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007252 os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007253
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007254 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7255 radio_list) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007256 if (idx == len)
7257 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007258
7259 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
7260 continue;
7261
7262 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007263 ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
7264 ifs->current_ssid->mode == WPAS_MODE_MESH)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007265 freq = ifs->current_ssid->frequency;
7266 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
7267 freq = ifs->assoc_freq;
7268 else
7269 continue;
7270
7271 /* Hold only distinct freqs */
7272 for (i = 0; i < idx; i++)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007273 if (freqs_data[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007274 break;
7275
7276 if (i == idx)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007277 freqs_data[idx++].freq = freq;
7278
7279 if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007280 freqs_data[i].flags |= ifs->current_ssid->p2p_group ?
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007281 WPA_FREQ_USED_BY_P2P_CLIENT :
7282 WPA_FREQ_USED_BY_INFRA_STATION;
7283 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007284 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08007285
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007286 dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007287 return idx;
7288}
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007289
7290
7291/*
7292 * Find the operating frequencies of any of the virtual interfaces that
7293 * are using the same radio as the current interface.
7294 */
7295int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
7296 int *freq_array, unsigned int len)
7297{
7298 struct wpa_used_freq_data *freqs_data;
7299 int num, i;
7300
7301 os_memset(freq_array, 0, sizeof(int) * len);
7302
7303 freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
7304 if (!freqs_data)
7305 return -1;
7306
7307 num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
7308 for (i = 0; i < num; i++)
7309 freq_array[i] = freqs_data[i].freq;
7310
7311 os_free(freqs_data);
7312
7313 return num;
7314}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007315
7316
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08007317struct wpa_supplicant *
7318wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
7319{
7320 switch (frame) {
7321#ifdef CONFIG_P2P
7322 case VENDOR_ELEM_PROBE_REQ_P2P:
7323 case VENDOR_ELEM_PROBE_RESP_P2P:
7324 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
7325 case VENDOR_ELEM_BEACON_P2P_GO:
7326 case VENDOR_ELEM_P2P_PD_REQ:
7327 case VENDOR_ELEM_P2P_PD_RESP:
7328 case VENDOR_ELEM_P2P_GO_NEG_REQ:
7329 case VENDOR_ELEM_P2P_GO_NEG_RESP:
7330 case VENDOR_ELEM_P2P_GO_NEG_CONF:
7331 case VENDOR_ELEM_P2P_INV_REQ:
7332 case VENDOR_ELEM_P2P_INV_RESP:
7333 case VENDOR_ELEM_P2P_ASSOC_REQ:
7334 case VENDOR_ELEM_P2P_ASSOC_RESP:
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007335 return wpa_s->p2pdev;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08007336#endif /* CONFIG_P2P */
7337 default:
7338 return wpa_s;
7339 }
7340}
7341
7342
7343void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
7344{
7345 unsigned int i;
7346 char buf[30];
7347
7348 wpa_printf(MSG_DEBUG, "Update vendor elements");
7349
7350 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
7351 if (wpa_s->vendor_elem[i]) {
7352 int res;
7353
7354 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
7355 if (!os_snprintf_error(sizeof(buf), res)) {
7356 wpa_hexdump_buf(MSG_DEBUG, buf,
7357 wpa_s->vendor_elem[i]);
7358 }
7359 }
7360 }
7361
7362#ifdef CONFIG_P2P
7363 if (wpa_s->parent == wpa_s &&
7364 wpa_s->global->p2p &&
7365 !wpa_s->global->p2p_disabled)
7366 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
7367#endif /* CONFIG_P2P */
7368}
7369
7370
7371int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
7372 const u8 *elem, size_t len)
7373{
7374 u8 *ie, *end;
7375
7376 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
7377 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
7378
7379 for (; ie + 1 < end; ie += 2 + ie[1]) {
7380 if (ie + len > end)
7381 break;
7382 if (os_memcmp(ie, elem, len) != 0)
7383 continue;
7384
7385 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
7386 wpabuf_free(wpa_s->vendor_elem[frame]);
7387 wpa_s->vendor_elem[frame] = NULL;
7388 } else {
7389 os_memmove(ie, ie + len, end - (ie + len));
7390 wpa_s->vendor_elem[frame]->used -= len;
7391 }
7392 wpas_vendor_elem_update(wpa_s);
7393 return 0;
7394 }
7395
7396 return -1;
7397}
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007398
7399
7400struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
7401 u16 num_modes, enum hostapd_hw_mode mode)
7402{
7403 u16 i;
7404
7405 for (i = 0; i < num_modes; i++) {
7406 if (modes[i].mode == mode)
7407 return &modes[i];
7408 }
7409
7410 return NULL;
7411}
7412
7413
7414static struct
7415wpa_bss_tmp_disallowed * wpas_get_disallowed_bss(struct wpa_supplicant *wpa_s,
7416 const u8 *bssid)
7417{
7418 struct wpa_bss_tmp_disallowed *bss;
7419
7420 dl_list_for_each(bss, &wpa_s->bss_tmp_disallowed,
7421 struct wpa_bss_tmp_disallowed, list) {
7422 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) == 0)
7423 return bss;
7424 }
7425
7426 return NULL;
7427}
7428
7429
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007430static int wpa_set_driver_tmp_disallow_list(struct wpa_supplicant *wpa_s)
7431{
7432 struct wpa_bss_tmp_disallowed *tmp;
7433 unsigned int num_bssid = 0;
7434 u8 *bssids;
7435 int ret;
7436
7437 bssids = os_malloc(dl_list_len(&wpa_s->bss_tmp_disallowed) * ETH_ALEN);
7438 if (!bssids)
7439 return -1;
7440 dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
7441 struct wpa_bss_tmp_disallowed, list) {
7442 os_memcpy(&bssids[num_bssid * ETH_ALEN], tmp->bssid,
7443 ETH_ALEN);
7444 num_bssid++;
7445 }
7446 ret = wpa_drv_set_bssid_blacklist(wpa_s, num_bssid, bssids);
7447 os_free(bssids);
7448 return ret;
7449}
7450
7451
7452static void wpa_bss_tmp_disallow_timeout(void *eloop_ctx, void *timeout_ctx)
7453{
7454 struct wpa_supplicant *wpa_s = eloop_ctx;
7455 struct wpa_bss_tmp_disallowed *tmp, *bss = timeout_ctx;
7456
7457 /* Make sure the bss is not already freed */
7458 dl_list_for_each(tmp, &wpa_s->bss_tmp_disallowed,
7459 struct wpa_bss_tmp_disallowed, list) {
7460 if (bss == tmp) {
7461 dl_list_del(&tmp->list);
7462 os_free(tmp);
7463 wpa_set_driver_tmp_disallow_list(wpa_s);
7464 break;
7465 }
7466 }
7467}
7468
7469
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007470void wpa_bss_tmp_disallow(struct wpa_supplicant *wpa_s, const u8 *bssid,
Hai Shalom74f70d42019-02-11 14:42:39 -08007471 unsigned int sec, int rssi_threshold)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007472{
7473 struct wpa_bss_tmp_disallowed *bss;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007474
7475 bss = wpas_get_disallowed_bss(wpa_s, bssid);
7476 if (bss) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007477 eloop_cancel_timeout(wpa_bss_tmp_disallow_timeout, wpa_s, bss);
Hai Shalom74f70d42019-02-11 14:42:39 -08007478 goto finish;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007479 }
7480
7481 bss = os_malloc(sizeof(*bss));
7482 if (!bss) {
7483 wpa_printf(MSG_DEBUG,
7484 "Failed to allocate memory for temp disallow BSS");
7485 return;
7486 }
7487
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007488 os_memcpy(bss->bssid, bssid, ETH_ALEN);
7489 dl_list_add(&wpa_s->bss_tmp_disallowed, &bss->list);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007490 wpa_set_driver_tmp_disallow_list(wpa_s);
Hai Shalom74f70d42019-02-11 14:42:39 -08007491
7492finish:
7493 bss->rssi_threshold = rssi_threshold;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007494 eloop_register_timeout(sec, 0, wpa_bss_tmp_disallow_timeout,
7495 wpa_s, bss);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007496}
7497
7498
Hai Shalom74f70d42019-02-11 14:42:39 -08007499int wpa_is_bss_tmp_disallowed(struct wpa_supplicant *wpa_s,
7500 struct wpa_bss *bss)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007501{
Hai Shalom74f70d42019-02-11 14:42:39 -08007502 struct wpa_bss_tmp_disallowed *disallowed = NULL, *tmp, *prev;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007503
7504 dl_list_for_each_safe(tmp, prev, &wpa_s->bss_tmp_disallowed,
7505 struct wpa_bss_tmp_disallowed, list) {
Hai Shalom74f70d42019-02-11 14:42:39 -08007506 if (os_memcmp(bss->bssid, tmp->bssid, ETH_ALEN) == 0) {
7507 disallowed = tmp;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007508 break;
7509 }
7510 }
Hai Shalom74f70d42019-02-11 14:42:39 -08007511 if (!disallowed)
7512 return 0;
7513
7514 if (disallowed->rssi_threshold != 0 &&
7515 bss->level > disallowed->rssi_threshold)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007516 return 0;
7517
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007518 return 1;
7519}