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