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