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