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