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