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