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