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