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