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