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