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