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