]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wpa_supplicant.c
nl80211: Debug print HT/VHT capability override information
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant
b1ae396f 3 * Copyright (c) 2003-2014, 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"
72044390 36#include "p2p/p2p.h"
6fc6879b
JM
37#include "blacklist.h"
38#include "wpas_glue.h"
116654ce 39#include "wps_supplicant.h"
11ef8d35 40#include "ibss_rsn.h"
c2a04078 41#include "sme.h"
04ea7b79 42#include "gas_query.h"
1f1b62a0 43#include "ap.h"
b22128ef 44#include "p2p_supplicant.h"
9675ce35 45#include "wifi_display.h"
8bac466b 46#include "notify.h"
60b94c98 47#include "bgscan.h"
7c865c68 48#include "autoscan.h"
83922c2d 49#include "bss.h"
9ba9fa07 50#include "scan.h"
24f6497c 51#include "offchannel.h"
cb418324 52#include "hs20_supplicant.h"
e27d20bb 53#include "wnm_sta.h"
6fc6879b
JM
54
55const char *wpa_supplicant_version =
56"wpa_supplicant v" VERSION_STR "\n"
b1ae396f 57"Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> and contributors";
6fc6879b
JM
58
59const char *wpa_supplicant_license =
331f89ff
JM
60"This software may be distributed under the terms of the BSD license.\n"
61"See README for more details.\n"
6fc6879b
JM
62#ifdef EAP_TLS_OPENSSL
63"\nThis product includes software developed by the OpenSSL Project\n"
64"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
65#endif /* EAP_TLS_OPENSSL */
66;
67
68#ifndef CONFIG_NO_STDOUT_DEBUG
69/* Long text divided into parts in order to fit in C89 strings size limits. */
70const char *wpa_supplicant_full_license1 =
331f89ff 71"";
6fc6879b 72const char *wpa_supplicant_full_license2 =
331f89ff 73"This software may be distributed under the terms of the BSD license.\n"
6fc6879b
JM
74"\n"
75"Redistribution and use in source and binary forms, with or without\n"
76"modification, are permitted provided that the following conditions are\n"
77"met:\n"
78"\n";
79const char *wpa_supplicant_full_license3 =
80"1. Redistributions of source code must retain the above copyright\n"
81" notice, this list of conditions and the following disclaimer.\n"
82"\n"
83"2. Redistributions in binary form must reproduce the above copyright\n"
84" notice, this list of conditions and the following disclaimer in the\n"
85" documentation and/or other materials provided with the distribution.\n"
86"\n";
87const char *wpa_supplicant_full_license4 =
88"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
89" names of its contributors may be used to endorse or promote products\n"
90" derived from this software without specific prior written permission.\n"
91"\n"
92"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
93"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
94"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
95"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
96const char *wpa_supplicant_full_license5 =
97"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
98"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
99"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
100"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
101"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
102"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
103"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
104"\n";
105#endif /* CONFIG_NO_STDOUT_DEBUG */
106
6fc6879b 107/* Configure default/group WEP keys for static WEP */
0194fedb 108int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
6fc6879b
JM
109{
110 int i, set = 0;
111
112 for (i = 0; i < NUM_WEP_KEYS; i++) {
113 if (ssid->wep_key_len[i] == 0)
114 continue;
115
116 set = 1;
0382097e 117 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
da64c266 118 i, i == ssid->wep_tx_keyidx, NULL, 0,
6fc6879b
JM
119 ssid->wep_key[i], ssid->wep_key_len[i]);
120 }
121
122 return set;
123}
124
125
6ea1f413
JM
126int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
127 struct wpa_ssid *ssid)
6fc6879b
JM
128{
129 u8 key[32];
130 size_t keylen;
71934751 131 enum wpa_alg alg;
6fc6879b
JM
132 u8 seq[6] = { 0 };
133
134 /* IBSS/WPA-None uses only one key (Group) for both receiving and
135 * sending unicast and multicast packets. */
136
d7dcba70 137 if (ssid->mode != WPAS_MODE_IBSS) {
f049052b
BG
138 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
139 "IBSS/ad-hoc) for WPA-None", ssid->mode);
6fc6879b
JM
140 return -1;
141 }
142
143 if (!ssid->psk_set) {
f049052b
BG
144 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
145 "WPA-None");
6fc6879b
JM
146 return -1;
147 }
148
149 switch (wpa_s->group_cipher) {
150 case WPA_CIPHER_CCMP:
151 os_memcpy(key, ssid->psk, 16);
152 keylen = 16;
153 alg = WPA_ALG_CCMP;
154 break;
eb7719ff
JM
155 case WPA_CIPHER_GCMP:
156 os_memcpy(key, ssid->psk, 16);
157 keylen = 16;
158 alg = WPA_ALG_GCMP;
159 break;
6fc6879b
JM
160 case WPA_CIPHER_TKIP:
161 /* WPA-None uses the same Michael MIC key for both TX and RX */
162 os_memcpy(key, ssid->psk, 16 + 8);
163 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
164 keylen = 32;
165 alg = WPA_ALG_TKIP;
166 break;
167 default:
f049052b
BG
168 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
169 "WPA-None", wpa_s->group_cipher);
6fc6879b
JM
170 return -1;
171 }
172
173 /* TODO: should actually remember the previously used seq#, both for TX
174 * and RX from each STA.. */
175
0382097e 176 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
6fc6879b
JM
177}
178
179
180static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
181{
182 struct wpa_supplicant *wpa_s = eloop_ctx;
183 const u8 *bssid = wpa_s->bssid;
a8e16edc 184 if (is_zero_ether_addr(bssid))
6fc6879b
JM
185 bssid = wpa_s->pending_bssid;
186 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
187 MAC2STR(bssid));
188 wpa_blacklist_add(wpa_s, bssid);
189 wpa_sm_notify_disassoc(wpa_s->wpa);
07783eaa 190 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
6fc6879b 191 wpa_s->reassociate = 1;
48b84f18
BG
192
193 /*
194 * If we timed out, the AP or the local radio may be busy.
195 * So, wait a second until scanning again.
196 */
197 wpa_supplicant_req_scan(wpa_s, 1, 0);
6fc6879b
JM
198}
199
200
201/**
202 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
203 * @wpa_s: Pointer to wpa_supplicant data
204 * @sec: Number of seconds after which to time out authentication
205 * @usec: Number of microseconds after which to time out authentication
206 *
207 * This function is used to schedule a timeout for the current authentication
208 * attempt.
209 */
210void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
211 int sec, int usec)
212{
a2a535f8 213 if (wpa_s->conf->ap_scan == 0 &&
c2a04078 214 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
6fc6879b
JM
215 return;
216
f049052b 217 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
6fc6879b
JM
218 "%d usec", sec, usec);
219 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
220 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
221}
222
223
224/**
225 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
226 * @wpa_s: Pointer to wpa_supplicant data
227 *
228 * This function is used to cancel authentication timeout scheduled with
229 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
230 * been completed.
231 */
232void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
233{
f049052b 234 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
6fc6879b
JM
235 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
236 wpa_blacklist_del(wpa_s, wpa_s->bssid);
237}
238
239
240/**
241 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
242 * @wpa_s: Pointer to wpa_supplicant data
243 *
244 * This function is used to configure EAPOL state machine based on the selected
245 * authentication mode.
246 */
247void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
248{
249#ifdef IEEE8021X_EAPOL
250 struct eapol_config eapol_conf;
251 struct wpa_ssid *ssid = wpa_s->current_ssid;
252
53895c3b 253#ifdef CONFIG_IBSS_RSN
d7dcba70 254 if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
255 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
256 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
257 /*
258 * RSN IBSS authentication is per-STA and we can disable the
259 * per-BSSID EAPOL authentication.
260 */
261 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
262 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
263 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
264 return;
265 }
266#endif /* CONFIG_IBSS_RSN */
267
0a40ec6a
JM
268 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
269 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
270
6fc6879b
JM
271 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
272 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
273 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
274 else
275 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
276
277 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
278 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
279 eapol_conf.accept_802_1x_keys = 1;
280 eapol_conf.required_keys = 0;
281 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
282 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
283 }
284 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
285 eapol_conf.required_keys |=
286 EAPOL_REQUIRE_KEY_BROADCAST;
287 }
288
a2a535f8 289 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
6fc6879b 290 eapol_conf.required_keys = 0;
6fc6879b 291 }
a2a535f8 292 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
6fc6879b 293 eapol_conf.workaround = ssid->eap_workaround;
56586197
JM
294 eapol_conf.eap_disabled =
295 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
ad08c363
JM
296 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
297 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
a5d44ac0 298 eapol_conf.external_sim = wpa_s->conf->external_sim;
6fc6879b
JM
299 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
300#endif /* IEEE8021X_EAPOL */
301}
302
303
304/**
305 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
306 * @wpa_s: Pointer to wpa_supplicant data
307 * @ssid: Configuration data for the network
308 *
309 * This function is used to configure WPA state machine and related parameters
310 * to a mode where WPA is not enabled. This is called as part of the
311 * authentication configuration when the selected network does not use WPA.
312 */
313void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
314 struct wpa_ssid *ssid)
315{
316 int i;
317
ad08c363
JM
318 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
319 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
320 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
6fc6879b
JM
321 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
322 else
323 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
324 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
325 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
326 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
327 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
328 wpa_s->group_cipher = WPA_CIPHER_NONE;
329 wpa_s->mgmt_group_cipher = 0;
330
331 for (i = 0; i < NUM_WEP_KEYS; i++) {
332 if (ssid->wep_key_len[i] > 5) {
333 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
334 wpa_s->group_cipher = WPA_CIPHER_WEP104;
335 break;
336 } else if (ssid->wep_key_len[i] > 0) {
337 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
338 wpa_s->group_cipher = WPA_CIPHER_WEP40;
339 break;
340 }
341 }
342
343 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
344 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
345 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
346 wpa_s->pairwise_cipher);
347 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
348#ifdef CONFIG_IEEE80211W
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
350 wpa_s->mgmt_group_cipher);
351#endif /* CONFIG_IEEE80211W */
352
353 pmksa_cache_clear_current(wpa_s->wpa);
354}
355
356
6979582c 357void free_hw_features(struct wpa_supplicant *wpa_s)
6bf731e8
CL
358{
359 int i;
360 if (wpa_s->hw.modes == NULL)
361 return;
362
363 for (i = 0; i < wpa_s->hw.num_modes; i++) {
364 os_free(wpa_s->hw.modes[i].channels);
365 os_free(wpa_s->hw.modes[i].rates);
366 }
367
368 os_free(wpa_s->hw.modes);
369 wpa_s->hw.modes = NULL;
370}
371
372
6fc6879b
JM
373static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
374{
60b94c98 375 bgscan_deinit(wpa_s);
7c865c68 376 autoscan_deinit(wpa_s);
6fc6879b
JM
377 scard_deinit(wpa_s->scard);
378 wpa_s->scard = NULL;
379 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
380 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
381 l2_packet_deinit(wpa_s->l2);
382 wpa_s->l2 = NULL;
383 if (wpa_s->l2_br) {
384 l2_packet_deinit(wpa_s->l2_br);
385 wpa_s->l2_br = NULL;
386 }
387
6fc6879b 388 if (wpa_s->conf != NULL) {
8e56d189
JM
389 struct wpa_ssid *ssid;
390 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
391 wpas_notify_network_removed(wpa_s, ssid);
6fc6879b
JM
392 }
393
394 os_free(wpa_s->confname);
395 wpa_s->confname = NULL;
396
e6304cad
DS
397 os_free(wpa_s->confanother);
398 wpa_s->confanother = NULL;
399
c16a7590
IP
400#ifdef CONFIG_P2P
401 os_free(wpa_s->conf_p2p_dev);
402 wpa_s->conf_p2p_dev = NULL;
403#endif /* CONFIG_P2P */
404
6fc6879b
JM
405 wpa_sm_set_eapol(wpa_s->wpa, NULL);
406 eapol_sm_deinit(wpa_s->eapol);
407 wpa_s->eapol = NULL;
408
409 rsn_preauth_deinit(wpa_s->wpa);
410
281ff0aa
GP
411#ifdef CONFIG_TDLS
412 wpa_tdls_deinit(wpa_s->wpa);
413#endif /* CONFIG_TDLS */
414
6fc6879b
JM
415 pmksa_candidate_free(wpa_s->wpa);
416 wpa_sm_deinit(wpa_s->wpa);
417 wpa_s->wpa = NULL;
418 wpa_blacklist_clear(wpa_s);
419
83922c2d 420 wpa_bss_deinit(wpa_s);
6fc6879b 421
831770bf 422 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
6fc6879b
JM
423 wpa_supplicant_cancel_scan(wpa_s);
424 wpa_supplicant_cancel_auth_timeout(wpa_s);
01a17491
JM
425 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
426#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
427 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
428 wpa_s, NULL);
429#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b 430
116654ce 431 wpas_wps_deinit(wpa_s);
11ef8d35 432
1ff73338
JM
433 wpabuf_free(wpa_s->pending_eapol_rx);
434 wpa_s->pending_eapol_rx = NULL;
435
11ef8d35
JM
436#ifdef CONFIG_IBSS_RSN
437 ibss_rsn_deinit(wpa_s->ibss_rsn);
438 wpa_s->ibss_rsn = NULL;
439#endif /* CONFIG_IBSS_RSN */
c2a04078 440
e29853bb 441 sme_deinit(wpa_s);
2d5b792d
JM
442
443#ifdef CONFIG_AP
444 wpa_supplicant_ap_deinit(wpa_s);
445#endif /* CONFIG_AP */
b22128ef
JM
446
447#ifdef CONFIG_P2P
448 wpas_p2p_deinit(wpa_s);
449#endif /* CONFIG_P2P */
f47d639d 450
24f6497c
JM
451#ifdef CONFIG_OFFCHANNEL
452 offchannel_deinit(wpa_s);
453#endif /* CONFIG_OFFCHANNEL */
454
a4cba8f1
LC
455 wpa_supplicant_cancel_sched_scan(wpa_s);
456
f47d639d
JM
457 os_free(wpa_s->next_scan_freqs);
458 wpa_s->next_scan_freqs = NULL;
fee52342
JM
459
460 os_free(wpa_s->manual_scan_freqs);
461 wpa_s->manual_scan_freqs = NULL;
04ea7b79 462
d3c9c35f
DS
463 os_free(wpa_s->manual_sched_scan_freqs);
464 wpa_s->manual_sched_scan_freqs = NULL;
465
04ea7b79
JM
466 gas_query_deinit(wpa_s->gas);
467 wpa_s->gas = NULL;
6bf731e8
CL
468
469 free_hw_features(wpa_s);
d445a5cd
JM
470
471 os_free(wpa_s->bssid_filter);
472 wpa_s->bssid_filter = NULL;
b6668734 473
6407f413
JM
474 os_free(wpa_s->disallow_aps_bssid);
475 wpa_s->disallow_aps_bssid = NULL;
476 os_free(wpa_s->disallow_aps_ssid);
477 wpa_s->disallow_aps_ssid = NULL;
478
b6668734 479 wnm_bss_keep_alive_deinit(wpa_s);
e27d20bb
VK
480#ifdef CONFIG_WNM
481 wnm_deallocate_memory(wpa_s);
482#endif /* CONFIG_WNM */
306ae225
JM
483
484 ext_password_deinit(wpa_s->ext_pw);
485 wpa_s->ext_pw = NULL;
b1f12296
JM
486
487 wpabuf_free(wpa_s->last_gas_resp);
b6a9590b
JM
488 wpa_s->last_gas_resp = NULL;
489 wpabuf_free(wpa_s->prev_gas_resp);
490 wpa_s->prev_gas_resp = NULL;
a297201d
JM
491
492 os_free(wpa_s->last_scan_res);
493 wpa_s->last_scan_res = NULL;
b572df86
JM
494
495#ifdef CONFIG_HS20
496 hs20_free_osu_prov(wpa_s);
497#endif /* CONFIG_HS20 */
6fc6879b
JM
498}
499
500
501/**
502 * wpa_clear_keys - Clear keys configured for the driver
503 * @wpa_s: Pointer to wpa_supplicant data
504 * @addr: Previously used BSSID or %NULL if not available
505 *
506 * This function clears the encryption keys that has been previously configured
507 * for the driver.
508 */
509void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
510{
2f30cac3 511 int i, max;
6fc6879b 512
0e27f655 513#ifdef CONFIG_IEEE80211W
2f30cac3
JM
514 max = 6;
515#else /* CONFIG_IEEE80211W */
516 max = 4;
0e27f655 517#endif /* CONFIG_IEEE80211W */
2f30cac3
JM
518
519 /* MLME-DELETEKEYS.request */
520 for (i = 0; i < max; i++) {
521 if (wpa_s->keys_cleared & BIT(i))
522 continue;
523 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
524 NULL, 0);
525 }
526 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
527 !is_zero_ether_addr(addr)) {
6fc6879b
JM
528 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
529 0);
530 /* MLME-SETPROTECTION.request(None) */
531 wpa_drv_mlme_setprotection(
532 wpa_s, addr,
533 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
534 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
535 }
2f30cac3 536 wpa_s->keys_cleared = (u32) -1;
6fc6879b
JM
537}
538
539
540/**
541 * wpa_supplicant_state_txt - Get the connection state name as a text string
542 * @state: State (wpa_state; WPA_*)
543 * Returns: The state name as a printable text string
544 */
71934751 545const char * wpa_supplicant_state_txt(enum wpa_states state)
6fc6879b
JM
546{
547 switch (state) {
548 case WPA_DISCONNECTED:
549 return "DISCONNECTED";
550 case WPA_INACTIVE:
551 return "INACTIVE";
8401a6b0
JM
552 case WPA_INTERFACE_DISABLED:
553 return "INTERFACE_DISABLED";
6fc6879b
JM
554 case WPA_SCANNING:
555 return "SCANNING";
c2a04078
JM
556 case WPA_AUTHENTICATING:
557 return "AUTHENTICATING";
6fc6879b
JM
558 case WPA_ASSOCIATING:
559 return "ASSOCIATING";
560 case WPA_ASSOCIATED:
561 return "ASSOCIATED";
562 case WPA_4WAY_HANDSHAKE:
563 return "4WAY_HANDSHAKE";
564 case WPA_GROUP_HANDSHAKE:
565 return "GROUP_HANDSHAKE";
566 case WPA_COMPLETED:
567 return "COMPLETED";
568 default:
569 return "UNKNOWN";
570 }
571}
572
573
cfe53c9a
PS
574#ifdef CONFIG_BGSCAN
575
576static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
577{
31392709
HD
578 const char *name;
579
580 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
581 name = wpa_s->current_ssid->bgscan;
582 else
583 name = wpa_s->conf->bgscan;
268043d5 584 if (name == NULL || name[0] == '\0')
31392709 585 return;
0096c427
JM
586 if (wpas_driver_bss_selection(wpa_s))
587 return;
cfe53c9a
PS
588 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
589 return;
aa109830
DS
590#ifdef CONFIG_P2P
591 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
592 return;
593#endif /* CONFIG_P2P */
cfe53c9a
PS
594
595 bgscan_deinit(wpa_s);
31392709
HD
596 if (wpa_s->current_ssid) {
597 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
cfe53c9a
PS
598 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
599 "bgscan");
600 /*
601 * Live without bgscan; it is only used as a roaming
602 * optimization, so the initial connection is not
603 * affected.
604 */
6409b7a7
YD
605 } else {
606 struct wpa_scan_results *scan_res;
cfe53c9a 607 wpa_s->bgscan_ssid = wpa_s->current_ssid;
6409b7a7
YD
608 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
609 0);
610 if (scan_res) {
611 bgscan_notify_scan(wpa_s, scan_res);
612 wpa_scan_results_free(scan_res);
613 }
614 }
cfe53c9a
PS
615 } else
616 wpa_s->bgscan_ssid = NULL;
617}
618
619
620static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
621{
622 if (wpa_s->bgscan_ssid != NULL) {
623 bgscan_deinit(wpa_s);
624 wpa_s->bgscan_ssid = NULL;
625 }
626}
627
628#endif /* CONFIG_BGSCAN */
629
630
7c865c68
TB
631static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
632{
99218999 633 if (autoscan_init(wpa_s, 0))
7c865c68
TB
634 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
635}
636
637
638static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
639{
640 autoscan_deinit(wpa_s);
641}
642
643
c3d12238
JM
644void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
645{
646 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
647 wpa_s->wpa_state == WPA_SCANNING) {
648 autoscan_deinit(wpa_s);
649 wpa_supplicant_start_autoscan(wpa_s);
650 }
651}
652
653
6fc6879b
JM
654/**
655 * wpa_supplicant_set_state - Set current connection state
656 * @wpa_s: Pointer to wpa_supplicant data
657 * @state: The new connection state
658 *
659 * This function is called whenever the connection state changes, e.g.,
660 * association is completed for WPA/WPA2 4-Way Handshake is started.
661 */
71934751
JM
662void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
663 enum wpa_states state)
6fc6879b 664{
27f43d8d
MH
665 enum wpa_states old_state = wpa_s->wpa_state;
666
f049052b
BG
667 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
668 wpa_supplicant_state_txt(wpa_s->wpa_state),
669 wpa_supplicant_state_txt(state));
6fc6879b 670
5ddd07cb
AS
671 if (state == WPA_INTERFACE_DISABLED) {
672 /* Assure normal scan when interface is restored */
673 wpa_s->normal_scans = 0;
674 }
675
6ac4b15e
JM
676 if (state == WPA_COMPLETED)
677 wpas_connect_work_done(wpa_s);
678
cb8564b1
DW
679 if (state != WPA_SCANNING)
680 wpa_supplicant_notify_scanning(wpa_s, 0);
681
6fc6879b 682 if (state == WPA_COMPLETED && wpa_s->new_connection) {
6fc6879b 683 struct wpa_ssid *ssid = wpa_s->current_ssid;
7d37a357 684#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
6fc6879b 685 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
1cfc6787
JM
686 MACSTR " completed [id=%d id_str=%s]",
687 MAC2STR(wpa_s->bssid),
6fc6879b
JM
688 ssid ? ssid->id : -1,
689 ssid && ssid->id_str ? ssid->id_str : "");
690#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
00e5e3d5 691 wpas_clear_temp_disabled(wpa_s, ssid, 1);
f1a52633 692 wpa_s->extra_blacklist_count = 0;
6fc6879b 693 wpa_s->new_connection = 0;
6fc6879b 694 wpa_drv_set_operstate(wpa_s, 1);
99ac2913
FF
695#ifndef IEEE8021X_EAPOL
696 wpa_drv_set_supp_port(wpa_s, 1);
697#endif /* IEEE8021X_EAPOL */
17a4734d 698 wpa_s->after_wps = 0;
4d9fb08d 699 wpa_s->known_wps_freq = 0;
b22128ef
JM
700#ifdef CONFIG_P2P
701 wpas_p2p_completed(wpa_s);
702#endif /* CONFIG_P2P */
c3701c66
RM
703
704 sme_sched_obss_scan(wpa_s, 1);
6fc6879b
JM
705 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
706 state == WPA_ASSOCIATED) {
707 wpa_s->new_connection = 1;
708 wpa_drv_set_operstate(wpa_s, 0);
99ac2913
FF
709#ifndef IEEE8021X_EAPOL
710 wpa_drv_set_supp_port(wpa_s, 0);
711#endif /* IEEE8021X_EAPOL */
c3701c66 712 sme_sched_obss_scan(wpa_s, 0);
6fc6879b
JM
713 }
714 wpa_s->wpa_state = state;
27f43d8d 715
cfe53c9a
PS
716#ifdef CONFIG_BGSCAN
717 if (state == WPA_COMPLETED)
718 wpa_supplicant_start_bgscan(wpa_s);
37271232 719 else if (state < WPA_ASSOCIATED)
cfe53c9a
PS
720 wpa_supplicant_stop_bgscan(wpa_s);
721#endif /* CONFIG_BGSCAN */
722
7c865c68
TB
723 if (state == WPA_AUTHENTICATING)
724 wpa_supplicant_stop_autoscan(wpa_s);
725
726 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
727 wpa_supplicant_start_autoscan(wpa_s);
728
5bbf9f10 729 if (wpa_s->wpa_state != old_state) {
27f43d8d 730 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
5bbf9f10
PS
731
732 if (wpa_s->wpa_state == WPA_COMPLETED ||
733 old_state == WPA_COMPLETED)
734 wpas_notify_auth_changed(wpa_s);
735 }
6fc6879b
JM
736}
737
738
1a1bf008
JM
739void wpa_supplicant_terminate_proc(struct wpa_global *global)
740{
741 int pending = 0;
742#ifdef CONFIG_WPS
743 struct wpa_supplicant *wpa_s = global->ifaces;
744 while (wpa_s) {
ab41595f 745 struct wpa_supplicant *next = wpa_s->next;
20625e97
JM
746#ifdef CONFIG_P2P
747 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
748 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
749 wpas_p2p_disconnect(wpa_s);
750#endif /* CONFIG_P2P */
1a1bf008
JM
751 if (wpas_wps_terminate_pending(wpa_s) == 1)
752 pending = 1;
ab41595f 753 wpa_s = next;
1a1bf008
JM
754 }
755#endif /* CONFIG_WPS */
756 if (pending)
757 return;
758 eloop_terminate();
759}
760
761
0456ea16 762static void wpa_supplicant_terminate(int sig, void *signal_ctx)
6fc6879b 763{
0456ea16 764 struct wpa_global *global = signal_ctx;
1a1bf008 765 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
766}
767
768
b22128ef 769void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
6fc6879b 770{
71934751 771 enum wpa_states old_state = wpa_s->wpa_state;
27f43d8d 772
6fc6879b
JM
773 wpa_s->pairwise_cipher = 0;
774 wpa_s->group_cipher = 0;
775 wpa_s->mgmt_group_cipher = 0;
776 wpa_s->key_mgmt = 0;
8401a6b0 777 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
99218999 778 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
27f43d8d
MH
779
780 if (wpa_s->wpa_state != old_state)
781 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
6fc6879b
JM
782}
783
784
785/**
786 * wpa_supplicant_reload_configuration - Reload configuration data
787 * @wpa_s: Pointer to wpa_supplicant data
788 * Returns: 0 on success or -1 if configuration parsing failed
789 *
790 * This function can be used to request that the configuration data is reloaded
791 * (e.g., after configuration file change). This function is reloading
792 * configuration only for one interface, so this may need to be called multiple
793 * times if %wpa_supplicant is controlling multiple interfaces and all
794 * interfaces need reconfiguration.
795 */
796int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
797{
798 struct wpa_config *conf;
799 int reconf_ctrl;
8bac466b
JM
800 int old_ap_scan;
801
6fc6879b
JM
802 if (wpa_s->confname == NULL)
803 return -1;
e6304cad 804 conf = wpa_config_read(wpa_s->confname, NULL);
6fc6879b
JM
805 if (conf == NULL) {
806 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
807 "file '%s' - exiting", wpa_s->confname);
808 return -1;
809 }
e6304cad
DS
810 wpa_config_read(wpa_s->confanother, conf);
811
611aea7d 812 conf->changed_parameters = (unsigned int) -1;
6fc6879b
JM
813
814 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
815 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
816 os_strcmp(conf->ctrl_interface,
817 wpa_s->conf->ctrl_interface) != 0);
818
819 if (reconf_ctrl && wpa_s->ctrl_iface) {
820 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
821 wpa_s->ctrl_iface = NULL;
822 }
823
824 eapol_sm_invalidate_cached_session(wpa_s->eapol);
7b7ce8aa
JM
825 if (wpa_s->current_ssid) {
826 wpa_supplicant_deauthenticate(wpa_s,
827 WLAN_REASON_DEAUTH_LEAVING);
828 }
8bac466b 829
6fc6879b
JM
830 /*
831 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
832 * pkcs11_engine_path, pkcs11_module_path.
833 */
56586197 834 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
835 /*
836 * Clear forced success to clear EAP state for next
837 * authentication.
838 */
839 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
840 }
841 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
842 wpa_sm_set_config(wpa_s->wpa, NULL);
d8a790b9 843 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6fc6879b
JM
844 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
845 rsn_preauth_deinit(wpa_s->wpa);
8bac466b
JM
846
847 old_ap_scan = wpa_s->conf->ap_scan;
6fc6879b
JM
848 wpa_config_free(wpa_s->conf);
849 wpa_s->conf = conf;
8bac466b
JM
850 if (old_ap_scan != wpa_s->conf->ap_scan)
851 wpas_notify_ap_scan_changed(wpa_s);
852
6fc6879b
JM
853 if (reconf_ctrl)
854 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
855
611aea7d
JM
856 wpa_supplicant_update_config(wpa_s);
857
6fc6879b 858 wpa_supplicant_clear_status(wpa_s);
349493bd 859 if (wpa_supplicant_enabled_networks(wpa_s)) {
43a38635
JM
860 wpa_s->reassociate = 1;
861 wpa_supplicant_req_scan(wpa_s, 0, 0);
862 }
f049052b 863 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
6fc6879b
JM
864 return 0;
865}
866
867
0456ea16 868static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
6fc6879b 869{
0456ea16 870 struct wpa_global *global = signal_ctx;
6fc6879b 871 struct wpa_supplicant *wpa_s;
6fc6879b 872 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
f049052b
BG
873 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
874 sig);
6fc6879b 875 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1a1bf008 876 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
877 }
878 }
879}
880
881
6fc6879b
JM
882static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
883 struct wpa_ssid *ssid,
884 struct wpa_ie_data *ie)
885{
886 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
887 if (ret) {
888 if (ret == -2) {
889 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
890 "from association info");
891 }
892 return -1;
893 }
894
f049052b
BG
895 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
896 "cipher suites");
6fc6879b
JM
897 if (!(ie->group_cipher & ssid->group_cipher)) {
898 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
899 "cipher 0x%x (mask 0x%x) - reject",
900 ie->group_cipher, ssid->group_cipher);
901 return -1;
902 }
903 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
904 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
905 "cipher 0x%x (mask 0x%x) - reject",
906 ie->pairwise_cipher, ssid->pairwise_cipher);
907 return -1;
908 }
909 if (!(ie->key_mgmt & ssid->key_mgmt)) {
910 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
911 "management 0x%x (mask 0x%x) - reject",
912 ie->key_mgmt, ssid->key_mgmt);
913 return -1;
914 }
915
916#ifdef CONFIG_IEEE80211W
0b60b0aa 917 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
62d49803
JM
918 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
919 wpa_s->conf->pmf : ssid->ieee80211w) ==
920 MGMT_FRAME_PROTECTION_REQUIRED) {
6fc6879b
JM
921 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
922 "that does not support management frame protection - "
923 "reject");
924 return -1;
925 }
926#endif /* CONFIG_IEEE80211W */
927
928 return 0;
929}
930
931
932/**
933 * wpa_supplicant_set_suites - Set authentication and encryption parameters
934 * @wpa_s: Pointer to wpa_supplicant data
935 * @bss: Scan results for the selected BSS, or %NULL if not available
936 * @ssid: Configuration data for the selected network
937 * @wpa_ie: Buffer for the WPA/RSN IE
938 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
939 * used buffer length in case the functions returns success.
940 * Returns: 0 on success or -1 on failure
941 *
942 * This function is used to configure authentication and encryption parameters
943 * based on the network configuration and scan result for the selected BSS (if
944 * available).
945 */
946int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
6fa81a3b 947 struct wpa_bss *bss, struct wpa_ssid *ssid,
6fc6879b
JM
948 u8 *wpa_ie, size_t *wpa_ie_len)
949{
950 struct wpa_ie_data ie;
951 int sel, proto;
df0f01d9 952 const u8 *bss_wpa, *bss_rsn, *bss_osen;
6fc6879b
JM
953
954 if (bss) {
6fa81a3b
JM
955 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
956 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
df0f01d9 957 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
6fc6879b 958 } else
df0f01d9 959 bss_wpa = bss_rsn = bss_osen = NULL;
6fc6879b
JM
960
961 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
962 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
963 (ie.group_cipher & ssid->group_cipher) &&
964 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
965 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 966 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
6fc6879b
JM
967 proto = WPA_PROTO_RSN;
968 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
969 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
970 (ie.group_cipher & ssid->group_cipher) &&
971 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
972 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 973 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
6fc6879b 974 proto = WPA_PROTO_WPA;
df0f01d9
JM
975#ifdef CONFIG_HS20
976 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
977 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
978 /* TODO: parse OSEN element */
979 ie.group_cipher = WPA_CIPHER_CCMP;
980 ie.pairwise_cipher = WPA_CIPHER_CCMP;
981 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
982 proto = WPA_PROTO_OSEN;
983#endif /* CONFIG_HS20 */
6fc6879b
JM
984 } else if (bss) {
985 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
986 return -1;
987 } else {
df0f01d9
JM
988 if (ssid->proto & WPA_PROTO_OSEN)
989 proto = WPA_PROTO_OSEN;
990 else if (ssid->proto & WPA_PROTO_RSN)
6fc6879b
JM
991 proto = WPA_PROTO_RSN;
992 else
993 proto = WPA_PROTO_WPA;
994 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
995 os_memset(&ie, 0, sizeof(ie));
996 ie.group_cipher = ssid->group_cipher;
997 ie.pairwise_cipher = ssid->pairwise_cipher;
998 ie.key_mgmt = ssid->key_mgmt;
999#ifdef CONFIG_IEEE80211W
1000 ie.mgmt_group_cipher =
70f8cc8e 1001 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
6fc6879b
JM
1002 WPA_CIPHER_AES_128_CMAC : 0;
1003#endif /* CONFIG_IEEE80211W */
f049052b
BG
1004 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1005 "based on configuration");
6fc6879b
JM
1006 } else
1007 proto = ie.proto;
1008 }
1009
f049052b
BG
1010 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1011 "pairwise %d key_mgmt %d proto %d",
1012 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
6fc6879b
JM
1013#ifdef CONFIG_IEEE80211W
1014 if (ssid->ieee80211w) {
f049052b
BG
1015 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1016 ie.mgmt_group_cipher);
6fc6879b
JM
1017 }
1018#endif /* CONFIG_IEEE80211W */
1019
64fa840a 1020 wpa_s->wpa_proto = proto;
6fc6879b
JM
1021 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1022 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
df0f01d9 1023 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
6fc6879b
JM
1024
1025 if (bss || !wpa_s->ap_ies_from_associnfo) {
1026 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1027 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1028 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1029 bss_rsn ? 2 + bss_rsn[1] : 0))
1030 return -1;
1031 }
1032
1033 sel = ie.group_cipher & ssid->group_cipher;
edbd2a19
JM
1034 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1035 if (wpa_s->group_cipher < 0) {
f049052b
BG
1036 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1037 "cipher");
6fc6879b
JM
1038 return -1;
1039 }
edbd2a19
JM
1040 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1041 wpa_cipher_txt(wpa_s->group_cipher));
6fc6879b
JM
1042
1043 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
edbd2a19
JM
1044 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1045 if (wpa_s->pairwise_cipher < 0) {
f049052b
BG
1046 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1047 "cipher");
6fc6879b
JM
1048 return -1;
1049 }
edbd2a19
JM
1050 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1051 wpa_cipher_txt(wpa_s->pairwise_cipher));
6fc6879b
JM
1052
1053 sel = ie.key_mgmt & ssid->key_mgmt;
c10347f2
JM
1054#ifdef CONFIG_SAE
1055 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1056 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1057#endif /* CONFIG_SAE */
6fc6879b
JM
1058 if (0) {
1059#ifdef CONFIG_IEEE80211R
1060 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1061 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
f049052b 1062 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
6fc6879b
JM
1063 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1064 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
f049052b 1065 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
6fc6879b 1066#endif /* CONFIG_IEEE80211R */
c10347f2
JM
1067#ifdef CONFIG_SAE
1068 } else if (sel & WPA_KEY_MGMT_SAE) {
1069 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1070 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1071 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1072 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1073 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1074#endif /* CONFIG_SAE */
56586197
JM
1075#ifdef CONFIG_IEEE80211W
1076 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1077 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
f049052b 1078 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1079 "WPA: using KEY_MGMT 802.1X with SHA256");
1080 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1081 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
f049052b 1082 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1083 "WPA: using KEY_MGMT PSK with SHA256");
1084#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
1085 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1086 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
f049052b 1087 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
6fc6879b
JM
1088 } else if (sel & WPA_KEY_MGMT_PSK) {
1089 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
f049052b 1090 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
6fc6879b
JM
1091 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1092 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
f049052b 1093 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
df0f01d9
JM
1094#ifdef CONFIG_HS20
1095 } else if (sel & WPA_KEY_MGMT_OSEN) {
1096 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1097 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1098#endif /* CONFIG_HS20 */
6fc6879b 1099 } else {
f049052b
BG
1100 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1101 "authenticated key management type");
6fc6879b
JM
1102 return -1;
1103 }
1104
1105 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1106 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1107 wpa_s->pairwise_cipher);
1108 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1109
1110#ifdef CONFIG_IEEE80211W
1111 sel = ie.mgmt_group_cipher;
62d49803
JM
1112 if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1113 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
0b60b0aa 1114 !(ie.capabilities & WPA_CAPABILITY_MFPC))
6fc6879b
JM
1115 sel = 0;
1116 if (sel & WPA_CIPHER_AES_128_CMAC) {
1117 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
f049052b 1118 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
6fc6879b 1119 "AES-128-CMAC");
8dd9f9cd
JM
1120 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1121 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1122 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1123 "BIP-GMAC-128");
1124 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1125 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1126 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1127 "BIP-GMAC-256");
1128 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1129 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1130 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1131 "BIP-CMAC-256");
6fc6879b
JM
1132 } else {
1133 wpa_s->mgmt_group_cipher = 0;
f049052b 1134 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
6fc6879b
JM
1135 }
1136 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1137 wpa_s->mgmt_group_cipher);
62d49803
JM
1138 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1139 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1140 wpa_s->conf->pmf : ssid->ieee80211w));
6fc6879b
JM
1141#endif /* CONFIG_IEEE80211W */
1142
1143 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
f049052b 1144 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
6fc6879b
JM
1145 return -1;
1146 }
1147
0bf927a0 1148 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
6fc6879b 1149 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
7d232e23
ZC
1150#ifndef CONFIG_NO_PBKDF2
1151 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1152 ssid->passphrase) {
1153 u8 psk[PMK_LEN];
986de33d
JM
1154 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1155 4096, psk, PMK_LEN);
7d232e23
ZC
1156 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1157 psk, PMK_LEN);
1158 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1159 }
1160#endif /* CONFIG_NO_PBKDF2 */
9173b16f
JM
1161#ifdef CONFIG_EXT_PASSWORD
1162 if (ssid->ext_psk) {
1163 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1164 ssid->ext_psk);
1165 char pw_str[64 + 1];
1166 u8 psk[PMK_LEN];
1167
1168 if (pw == NULL) {
1169 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1170 "found from external storage");
1171 return -1;
1172 }
1173
1174 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1175 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1176 "PSK length %d in external storage",
1177 (int) wpabuf_len(pw));
1178 ext_password_free(pw);
1179 return -1;
1180 }
1181
1182 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1183 pw_str[wpabuf_len(pw)] = '\0';
1184
1185#ifndef CONFIG_NO_PBKDF2
1186 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1187 {
986de33d
JM
1188 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1189 4096, psk, PMK_LEN);
9173b16f
JM
1190 os_memset(pw_str, 0, sizeof(pw_str));
1191 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1192 "external passphrase)",
1193 psk, PMK_LEN);
1194 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1195 } else
1196#endif /* CONFIG_NO_PBKDF2 */
1197 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1198 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1199 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1200 "Invalid PSK hex string");
1201 os_memset(pw_str, 0, sizeof(pw_str));
1202 ext_password_free(pw);
1203 return -1;
1204 }
1205 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1206 } else {
1207 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1208 "PSK available");
1209 os_memset(pw_str, 0, sizeof(pw_str));
1210 ext_password_free(pw);
1211 return -1;
1212 }
1213
1214 os_memset(pw_str, 0, sizeof(pw_str));
1215 ext_password_free(pw);
1216 }
1217#endif /* CONFIG_EXT_PASSWORD */
7d232e23 1218 } else
6fc6879b
JM
1219 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1220
1221 return 0;
1222}
1223
1224
8cd6b7bc 1225static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
03e47c9c 1226{
8cd6b7bc 1227 *pos = 0x00;
03e47c9c 1228
8cd6b7bc
JB
1229 switch (idx) {
1230 case 0: /* Bits 0-7 */
1231 break;
1232 case 1: /* Bits 8-15 */
1233 break;
1234 case 2: /* Bits 16-23 */
1235#ifdef CONFIG_WNM
1236 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1237 *pos |= 0x08; /* Bit 19 - BSS Transition */
1238#endif /* CONFIG_WNM */
1239 break;
1240 case 3: /* Bits 24-31 */
1241#ifdef CONFIG_WNM
1242 *pos |= 0x02; /* Bit 25 - SSID List */
1243#endif /* CONFIG_WNM */
03e47c9c 1244#ifdef CONFIG_INTERWORKING
8cd6b7bc
JB
1245 if (wpa_s->conf->interworking)
1246 *pos |= 0x80; /* Bit 31 - Interworking */
03e47c9c 1247#endif /* CONFIG_INTERWORKING */
8cd6b7bc
JB
1248 break;
1249 case 4: /* Bits 32-39 */
56f5af48 1250#ifdef CONFIG_INTERWORKING
429dd9af
JM
1251 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1252 *pos |= 0x01; /* Bit 32 - QoS Map */
56f5af48 1253#endif /* CONFIG_INTERWORKING */
8cd6b7bc
JB
1254 break;
1255 case 5: /* Bits 40-47 */
95a3ea94
JM
1256#ifdef CONFIG_HS20
1257 if (wpa_s->conf->hs20)
1258 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1259#endif /* CONFIG_HS20 */
8cd6b7bc
JB
1260 break;
1261 case 6: /* Bits 48-55 */
1262 break;
1263 }
1264}
03e47c9c 1265
03e47c9c 1266
8cd6b7bc
JB
1267int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf)
1268{
1269 u8 *pos = buf;
95a3ea94 1270 u8 len = 6, i;
8cd6b7bc
JB
1271
1272 if (len < wpa_s->extended_capa_len)
1273 len = wpa_s->extended_capa_len;
03e47c9c
JM
1274
1275 *pos++ = WLAN_EID_EXT_CAPAB;
8cd6b7bc
JB
1276 *pos++ = len;
1277 for (i = 0; i < len; i++, pos++) {
1278 wpas_ext_capab_byte(wpa_s, pos, i);
1279
1280 if (i < wpa_s->extended_capa_len) {
1281 *pos &= ~wpa_s->extended_capa_mask[i];
1282 *pos |= wpa_s->extended_capa[i];
1283 }
1284 }
03e47c9c 1285
3db5439a
JM
1286 while (len > 0 && buf[1 + len] == 0) {
1287 len--;
1288 buf[1] = len;
1289 }
1290 if (len == 0)
1291 return 0;
1292
1293 return 2 + len;
03e47c9c
JM
1294}
1295
1296
6ac4b15e
JM
1297static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1298 struct wpa_bss *test_bss)
1299{
1300 struct wpa_bss *bss;
1301
1302 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1303 if (bss == test_bss)
1304 return 1;
1305 }
1306
1307 return 0;
1308}
1309
1310
1311static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1312 struct wpa_ssid *test_ssid)
1313{
1314 struct wpa_ssid *ssid;
1315
1316 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1317 if (ssid == test_ssid)
1318 return 1;
1319 }
1320
1321 return 0;
1322}
1323
1324
1325int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1326 struct wpa_ssid *test_ssid)
1327{
1328 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1329 return 0;
1330
1331 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1332}
1333
1334
1335void wpas_connect_work_free(struct wpa_connect_work *cwork)
1336{
1337 if (cwork == NULL)
1338 return;
1339 os_free(cwork);
1340}
1341
1342
1343void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1344{
1345 struct wpa_connect_work *cwork;
1346 struct wpa_radio_work *work = wpa_s->connect_work;
1347
1348 if (!work)
1349 return;
1350
1351 wpa_s->connect_work = NULL;
1352 cwork = work->ctx;
1353 work->ctx = NULL;
1354 wpas_connect_work_free(cwork);
1355 radio_work_done(work);
1356}
1357
1358
1359static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1360
6fc6879b
JM
1361/**
1362 * wpa_supplicant_associate - Request association
1363 * @wpa_s: Pointer to wpa_supplicant data
1364 * @bss: Scan results for the selected BSS, or %NULL if not available
1365 * @ssid: Configuration data for the selected network
1366 *
1367 * This function is used to request %wpa_supplicant to associate with a BSS.
1368 */
1369void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
6fa81a3b 1370 struct wpa_bss *bss, struct wpa_ssid *ssid)
6fc6879b 1371{
6ac4b15e 1372 struct wpa_connect_work *cwork;
6fc6879b 1373
78177a00
JM
1374#ifdef CONFIG_IBSS_RSN
1375 ibss_rsn_deinit(wpa_s->ibss_rsn);
1376 wpa_s->ibss_rsn = NULL;
1377#endif /* CONFIG_IBSS_RSN */
1378
2c5d725c
JM
1379 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1380 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1581b38b
JM
1381#ifdef CONFIG_AP
1382 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
f049052b
BG
1383 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1384 "mode");
1581b38b
JM
1385 return;
1386 }
8c981d17
DW
1387 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1388 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
b2b688d1
VKE
1389 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1390 wpas_p2p_ap_setup_failed(wpa_s);
8c981d17
DW
1391 return;
1392 }
8f770587 1393 wpa_s->current_bss = bss;
1581b38b 1394#else /* CONFIG_AP */
f049052b
BG
1395 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1396 "the build");
1581b38b
JM
1397#endif /* CONFIG_AP */
1398 return;
1399 }
1400
52c9e6f3 1401#ifdef CONFIG_TDLS
95cb2d88
JM
1402 if (bss)
1403 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1404 bss->ie_len);
52c9e6f3
JM
1405#endif /* CONFIG_TDLS */
1406
5cc4d64b
JM
1407 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1408 ssid->mode == IEEE80211_MODE_INFRA) {
c2a04078
JM
1409 sme_authenticate(wpa_s, bss, ssid);
1410 return;
1411 }
1412
6ac4b15e
JM
1413 if (wpa_s->connect_work) {
1414 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1415 return;
1416 }
1417
f0e30c84
JM
1418 if (radio_work_pending(wpa_s, "connect")) {
1419 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1420 return;
1421 }
1422
6ac4b15e
JM
1423 cwork = os_zalloc(sizeof(*cwork));
1424 if (cwork == NULL)
1425 return;
1426
1427 cwork->bss = bss;
1428 cwork->ssid = ssid;
1429
1430 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1431 wpas_start_assoc_cb, cwork) < 0) {
1432 os_free(cwork);
1433 }
1434}
1435
1436
1437static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
1438{
1439 struct wpa_connect_work *cwork = work->ctx;
1440 struct wpa_bss *bss = cwork->bss;
1441 struct wpa_ssid *ssid = cwork->ssid;
1442 struct wpa_supplicant *wpa_s = work->wpa_s;
1443 u8 wpa_ie[200];
1444 size_t wpa_ie_len;
1445 int use_crypt, ret, i, bssid_changed;
1446 int algs = WPA_AUTH_ALG_OPEN;
1447 unsigned int cipher_pairwise, cipher_group;
1448 struct wpa_driver_associate_params params;
1449 int wep_keys_set = 0;
1450 int assoc_failed = 0;
1451 struct wpa_ssid *old_ssid;
1452#ifdef CONFIG_HT_OVERRIDES
1453 struct ieee80211_ht_capabilities htcaps;
1454 struct ieee80211_ht_capabilities htcaps_mask;
1455#endif /* CONFIG_HT_OVERRIDES */
6aa1cd4e
PS
1456#ifdef CONFIG_VHT_OVERRIDES
1457 struct ieee80211_vht_capabilities vhtcaps;
1458 struct ieee80211_vht_capabilities vhtcaps_mask;
1459#endif /* CONFIG_VHT_OVERRIDES */
6ac4b15e
JM
1460
1461 if (deinit) {
b3253ebb
AO
1462 if (work->started) {
1463 wpa_s->connect_work = NULL;
1464
1465 /* cancel possible auth. timeout */
1466 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
1467 NULL);
1468 }
6ac4b15e
JM
1469 wpas_connect_work_free(cwork);
1470 return;
1471 }
1472
1473 wpa_s->connect_work = work;
1474
1475 if (!wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
1476 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
1477 wpas_connect_work_done(wpa_s);
1478 return;
1479 }
1480
0c80427d 1481 os_memset(&params, 0, sizeof(params));
6fc6879b 1482 wpa_s->reassociate = 0;
c60ba9f7 1483 wpa_s->eap_expected_failure = 0;
22628eca 1484 if (bss && !wpas_driver_bss_selection(wpa_s)) {
6fc6879b 1485#ifdef CONFIG_IEEE80211R
6fa81a3b 1486 const u8 *ie, *md = NULL;
6fc6879b 1487#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
1488 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1489 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
6fa81a3b 1490 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
8bac466b 1491 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
1492 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1493 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
8bac466b
JM
1494 if (bssid_changed)
1495 wpas_notify_bssid_changed(wpa_s);
6fc6879b 1496#ifdef CONFIG_IEEE80211R
6fa81a3b 1497 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
6fc6879b
JM
1498 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1499 md = ie + 2;
e7846b68 1500 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
91a05482
JM
1501 if (md) {
1502 /* Prepare for the next transition */
76b7981d 1503 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
91a05482 1504 }
6fc6879b 1505#endif /* CONFIG_IEEE80211R */
24c23d1b
JM
1506#ifdef CONFIG_WPS
1507 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1508 wpa_s->conf->ap_scan == 2 &&
1509 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1510 /* Use ap_scan==1 style network selection to find the network
1511 */
4115303b 1512 wpa_s->scan_req = MANUAL_SCAN_REQ;
24c23d1b
JM
1513 wpa_s->reassociate = 1;
1514 wpa_supplicant_req_scan(wpa_s, 0, 0);
1515 return;
1516#endif /* CONFIG_WPS */
6fc6879b
JM
1517 } else {
1518 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1519 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1520 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1521 }
a4cba8f1 1522 wpa_supplicant_cancel_sched_scan(wpa_s);
6fc6879b
JM
1523 wpa_supplicant_cancel_scan(wpa_s);
1524
1525 /* Starting new association, so clear the possibly used WPA IE from the
1526 * previous association. */
1527 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1528
1529#ifdef IEEE8021X_EAPOL
1530 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1531 if (ssid->leap) {
1532 if (ssid->non_leap == 0)
abd9fafa 1533 algs = WPA_AUTH_ALG_LEAP;
6fc6879b 1534 else
abd9fafa 1535 algs |= WPA_AUTH_ALG_LEAP;
6fc6879b
JM
1536 }
1537 }
1538#endif /* IEEE8021X_EAPOL */
f049052b 1539 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
6fc6879b 1540 if (ssid->auth_alg) {
abd9fafa 1541 algs = ssid->auth_alg;
f049052b
BG
1542 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1543 "0x%x", algs);
6fc6879b 1544 }
6fc6879b 1545
6fa81a3b
JM
1546 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1547 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
0bf927a0 1548 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
6fc6879b 1549 int try_opportunistic;
6e202021
JM
1550 try_opportunistic = (ssid->proactive_key_caching < 0 ?
1551 wpa_s->conf->okc :
1552 ssid->proactive_key_caching) &&
6fc6879b
JM
1553 (ssid->proto & WPA_PROTO_RSN);
1554 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
b2a12c4f 1555 ssid, try_opportunistic) == 0)
6fc6879b
JM
1556 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1557 wpa_ie_len = sizeof(wpa_ie);
1558 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1559 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1560 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1561 "key management and encryption suites");
6fc6879b
JM
1562 return;
1563 }
a3f7e518
JM
1564 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1565 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1566 /*
1567 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1568 * use non-WPA since the scan results did not indicate that the
1569 * AP is using WPA or WPA2.
1570 */
1571 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1572 wpa_ie_len = 0;
1573 wpa_s->wpa_proto = 0;
0bf927a0 1574 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
6fc6879b
JM
1575 wpa_ie_len = sizeof(wpa_ie);
1576 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1577 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1578 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1579 "key management and encryption suites (no "
1580 "scan results)");
6fc6879b
JM
1581 return;
1582 }
ad08c363
JM
1583#ifdef CONFIG_WPS
1584 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
b01c18a8
JM
1585 struct wpabuf *wps_ie;
1586 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
ad08c363
JM
1587 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1588 wpa_ie_len = wpabuf_len(wps_ie);
1589 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
24386985
JM
1590 } else
1591 wpa_ie_len = 0;
ad08c363
JM
1592 wpabuf_free(wps_ie);
1593 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
0c80427d
JM
1594 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1595 params.wps = WPS_MODE_PRIVACY;
1596 else
1597 params.wps = WPS_MODE_OPEN;
cf546f1a 1598 wpa_s->wpa_proto = 0;
ad08c363 1599#endif /* CONFIG_WPS */
6fc6879b
JM
1600 } else {
1601 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1602 wpa_ie_len = 0;
cf546f1a 1603 wpa_s->wpa_proto = 0;
6fc6879b
JM
1604 }
1605
5f3a6aa0
JM
1606#ifdef CONFIG_P2P
1607 if (wpa_s->global->p2p) {
1608 u8 *pos;
1609 size_t len;
1610 int res;
5f3a6aa0
JM
1611 pos = wpa_ie + wpa_ie_len;
1612 len = sizeof(wpa_ie) - wpa_ie_len;
b8a8d677
JM
1613 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1614 ssid->p2p_group);
5f3a6aa0
JM
1615 if (res >= 0)
1616 wpa_ie_len += res;
1617 }
72044390
JM
1618
1619 wpa_s->cross_connect_disallowed = 0;
1620 if (bss) {
1621 struct wpabuf *p2p;
1622 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1623 if (p2p) {
1624 wpa_s->cross_connect_disallowed =
1625 p2p_get_cross_connect_disallowed(p2p);
1626 wpabuf_free(p2p);
f049052b
BG
1627 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1628 "connection",
1629 wpa_s->cross_connect_disallowed ?
1630 "disallows" : "allows");
72044390
JM
1631 }
1632 }
25ef8529
JM
1633
1634 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
5f3a6aa0
JM
1635#endif /* CONFIG_P2P */
1636
cb418324 1637#ifdef CONFIG_HS20
55a2df43 1638 if (is_hs20_network(wpa_s, ssid, bss)) {
cb418324
JM
1639 struct wpabuf *hs20;
1640 hs20 = wpabuf_alloc(20);
1641 if (hs20) {
f9cd147d
JM
1642 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
1643 wpas_hs20_add_indication(hs20, pps_mo_id);
cb418324
JM
1644 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
1645 wpabuf_len(hs20));
1646 wpa_ie_len += wpabuf_len(hs20);
1647 wpabuf_free(hs20);
1648 }
1649 }
1650#endif /* CONFIG_HS20 */
1651
8b3b803a
AH
1652 /*
1653 * Workaround: Add Extended Capabilities element only if the AP
1654 * included this element in Beacon/Probe Response frames. Some older
1655 * APs seem to have interoperability issues if this element is
1656 * included, so while the standard may require us to include the
1657 * element in all cases, it is justifiable to skip it to avoid
1658 * interoperability issues.
1659 */
1660 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
1661 u8 ext_capab[10];
1662 int ext_capab_len;
1663 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab);
1664 if (ext_capab_len > 0) {
1665 u8 *pos = wpa_ie;
1666 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1667 pos += 2 + pos[1];
1668 os_memmove(pos + ext_capab_len, pos,
1669 wpa_ie_len - (pos - wpa_ie));
1670 wpa_ie_len += ext_capab_len;
1671 os_memcpy(pos, ext_capab, ext_capab_len);
1672 }
92cbcf91 1673 }
92cbcf91 1674
6fc6879b
JM
1675 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1676 use_crypt = 1;
4848a38d
JM
1677 cipher_pairwise = wpa_s->pairwise_cipher;
1678 cipher_group = wpa_s->group_cipher;
6fc6879b
JM
1679 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1680 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1681 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1682 use_crypt = 0;
1683 if (wpa_set_wep_keys(wpa_s, ssid)) {
1684 use_crypt = 1;
1685 wep_keys_set = 1;
1686 }
1687 }
ad08c363
JM
1688 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1689 use_crypt = 0;
6fc6879b
JM
1690
1691#ifdef IEEE8021X_EAPOL
1692 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1693 if ((ssid->eapol_flags &
1694 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1695 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1696 !wep_keys_set) {
1697 use_crypt = 0;
1698 } else {
1699 /* Assume that dynamic WEP-104 keys will be used and
1700 * set cipher suites in order for drivers to expect
1701 * encryption. */
4848a38d 1702 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
6fc6879b
JM
1703 }
1704 }
1705#endif /* IEEE8021X_EAPOL */
1706
1707 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1708 /* Set the key before (and later after) association */
1709 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1710 }
1711
6fc6879b 1712 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
6fc6879b 1713 if (bss) {
6fa81a3b
JM
1714 params.ssid = bss->ssid;
1715 params.ssid_len = bss->ssid_len;
f15854d1
JM
1716 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1717 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1718 MACSTR " freq=%u MHz based on scan results "
1719 "(bssid_set=%d)",
1720 MAC2STR(bss->bssid), bss->freq,
1721 ssid->bssid_set);
22628eca
JM
1722 params.bssid = bss->bssid;
1723 params.freq = bss->freq;
1724 }
7ac7fd43
DS
1725 params.bssid_hint = bss->bssid;
1726 params.freq_hint = bss->freq;
6fc6879b
JM
1727 } else {
1728 params.ssid = ssid->ssid;
1729 params.ssid_len = ssid->ssid_len;
1730 }
9e2af29f
NC
1731
1732 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1733 wpa_s->conf->ap_scan == 2) {
1734 params.bssid = ssid->bssid;
1735 params.fixed_bssid = 1;
1736 }
1737
d7dcba70
JM
1738 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1739 params.freq == 0)
6fc6879b 1740 params.freq = ssid->frequency; /* Initial channel for IBSS */
8f05577d
JM
1741
1742 if (ssid->mode == WPAS_MODE_IBSS) {
1743 if (ssid->beacon_int)
1744 params.beacon_int = ssid->beacon_int;
1745 else
1746 params.beacon_int = wpa_s->conf->beacon_int;
1747 }
1748
6fc6879b
JM
1749 params.wpa_ie = wpa_ie;
1750 params.wpa_ie_len = wpa_ie_len;
1751 params.pairwise_suite = cipher_pairwise;
1752 params.group_suite = cipher_group;
4848a38d 1753 params.key_mgmt_suite = wpa_s->key_mgmt;
64fa840a 1754 params.wpa_proto = wpa_s->wpa_proto;
6fc6879b
JM
1755 params.auth_alg = algs;
1756 params.mode = ssid->mode;
1f6c0ab8 1757 params.bg_scan_period = ssid->bg_scan_period;
6fc6879b
JM
1758 for (i = 0; i < NUM_WEP_KEYS; i++) {
1759 if (ssid->wep_key_len[i])
1760 params.wep_key[i] = ssid->wep_key[i];
1761 params.wep_key_len[i] = ssid->wep_key_len[i];
1762 }
1763 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1764
c2a04078 1765 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
4848a38d
JM
1766 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
1767 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
6fc6879b
JM
1768 params.passphrase = ssid->passphrase;
1769 if (ssid->psk_set)
1770 params.psk = ssid->psk;
1771 }
1772
36b15723
JM
1773 params.drop_unencrypted = use_crypt;
1774
6fc6879b 1775#ifdef CONFIG_IEEE80211W
62d49803
JM
1776 params.mgmt_frame_protection =
1777 ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1778 wpa_s->conf->pmf : ssid->ieee80211w;
1779 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
6fa81a3b 1780 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
97d3497e
JM
1781 struct wpa_ie_data ie;
1782 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1783 ie.capabilities &
1784 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
f049052b
BG
1785 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1786 "MFP: require MFP");
97d3497e
JM
1787 params.mgmt_frame_protection =
1788 MGMT_FRAME_PROTECTION_REQUIRED;
1789 }
1790 }
6fc6879b
JM
1791#endif /* CONFIG_IEEE80211W */
1792
ffad8858 1793 params.p2p = ssid->p2p_group;
6e3f4b89 1794
eea2fd9e
JM
1795 if (wpa_s->parent->set_sta_uapsd)
1796 params.uapsd = wpa_s->parent->sta_uapsd;
1797 else
1798 params.uapsd = -1;
1799
80e8a5ee
BG
1800#ifdef CONFIG_HT_OVERRIDES
1801 os_memset(&htcaps, 0, sizeof(htcaps));
1802 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1803 params.htcaps = (u8 *) &htcaps;
1804 params.htcaps_mask = (u8 *) &htcaps_mask;
1805 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1806#endif /* CONFIG_HT_OVERRIDES */
6aa1cd4e
PS
1807#ifdef CONFIG_VHT_OVERRIDES
1808 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
1809 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
1810 params.vhtcaps = &vhtcaps;
1811 params.vhtcaps_mask = &vhtcaps_mask;
1812 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
1813#endif /* CONFIG_VHT_OVERRIDES */
80e8a5ee 1814
8567866d
JJ
1815#ifdef CONFIG_P2P
1816 /*
1817 * If multi-channel concurrency is not supported, check for any
1818 * frequency conflict. In case of any frequency conflict, remove the
1819 * least prioritized connection.
1820 */
1821 if (wpa_s->num_multichan_concurrent < 2) {
1822 int freq = wpa_drv_shared_freq(wpa_s);
1823 if (freq > 0 && freq != params.freq) {
1824 wpa_printf(MSG_DEBUG, "Shared interface with conflicting frequency found (%d != %d)",
1825 freq, params.freq);
1826 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
1827 params.freq,
1828 ssid) < 0)
1829 return;
1830 }
1831 }
1832#endif /* CONFIG_P2P */
1833
17fbb751 1834 ret = wpa_drv_associate(wpa_s, &params);
6fc6879b
JM
1835 if (ret < 0) {
1836 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1837 "failed");
871f4dd0
JM
1838 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1839 /*
1840 * The driver is known to mean what is saying, so we
1841 * can stop right here; the association will not
1842 * succeed.
1843 */
1844 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
c1c02342 1845 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
871f4dd0
JM
1846 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1847 return;
1848 }
6fc6879b
JM
1849 /* try to continue anyway; new association will be tried again
1850 * after timeout */
1851 assoc_failed = 1;
1852 }
1853
1854 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1855 /* Set the key after the association just in case association
1856 * cleared the previously configured key. */
1857 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1858 /* No need to timeout authentication since there is no key
1859 * management. */
1860 wpa_supplicant_cancel_auth_timeout(wpa_s);
1861 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
53895c3b 1862#ifdef CONFIG_IBSS_RSN
d7dcba70 1863 } else if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
1864 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1865 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1866 /*
1867 * RSN IBSS authentication is per-STA and we can disable the
1868 * per-BSSID authentication.
1869 */
1870 wpa_supplicant_cancel_auth_timeout(wpa_s);
53895c3b 1871#endif /* CONFIG_IBSS_RSN */
6fc6879b
JM
1872 } else {
1873 /* Timeout for IEEE 802.11 authentication and association */
1d3c75b3
DW
1874 int timeout = 60;
1875
1876 if (assoc_failed) {
1877 /* give IBSS a bit more time */
d7dcba70 1878 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1d3c75b3
DW
1879 } else if (wpa_s->conf->ap_scan == 1) {
1880 /* give IBSS a bit more time */
d7dcba70 1881 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1d3c75b3 1882 }
6fc6879b
JM
1883 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1884 }
1885
66562e9c
JM
1886 if (wep_keys_set &&
1887 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
6fc6879b
JM
1888 /* Set static WEP keys again */
1889 wpa_set_wep_keys(wpa_s, ssid);
1890 }
1891
1892 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1893 /*
1894 * Do not allow EAP session resumption between different
1895 * network configurations.
1896 */
1897 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1898 }
8bac466b 1899 old_ssid = wpa_s->current_ssid;
6fc6879b 1900 wpa_s->current_ssid = ssid;
8f770587 1901 wpa_s->current_bss = bss;
6fc6879b
JM
1902 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1903 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
1904 if (old_ssid != wpa_s->current_ssid)
1905 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
1906}
1907
1908
09f58c09
JM
1909static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1910 const u8 *addr)
1911{
1912 struct wpa_ssid *old_ssid;
1913
1914 wpa_clear_keys(wpa_s, addr);
09f58c09 1915 old_ssid = wpa_s->current_ssid;
0d30cc24 1916 wpa_supplicant_mark_disassoc(wpa_s);
09f58c09
JM
1917 wpa_sm_set_config(wpa_s->wpa, NULL);
1918 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1919 if (old_ssid != wpa_s->current_ssid)
1920 wpas_notify_network_changed(wpa_s);
1921 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1922}
1923
1924
6fc6879b
JM
1925/**
1926 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1927 * @wpa_s: Pointer to wpa_supplicant data
1928 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1929 *
073ab58f 1930 * This function is used to request %wpa_supplicant to deauthenticate from the
6fc6879b
JM
1931 * current AP.
1932 */
1933void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1934 int reason_code)
1935{
1936 u8 *addr = NULL;
ef48ff94 1937 union wpa_event_data event;
42d23547 1938 int zero_addr = 0;
8bac466b 1939
42d23547
JM
1940 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
1941 " pending_bssid=" MACSTR " reason=%d state=%s",
1942 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1943 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
1944
1945 if (!is_zero_ether_addr(wpa_s->bssid))
1946 addr = wpa_s->bssid;
1947 else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1948 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
1949 wpa_s->wpa_state == WPA_ASSOCIATING))
1950 addr = wpa_s->pending_bssid;
1951 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1952 /*
1953 * When using driver-based BSS selection, we may not know the
1954 * BSSID with which we are currently trying to associate. We
1955 * need to notify the driver of this disconnection even in such
1956 * a case, so use the all zeros address here.
1957 */
6fc6879b 1958 addr = wpa_s->bssid;
42d23547
JM
1959 zero_addr = 1;
1960 }
1961
7b44ff2c
SD
1962#ifdef CONFIG_TDLS
1963 wpa_tdls_teardown_peers(wpa_s->wpa);
1964#endif /* CONFIG_TDLS */
1965
42d23547
JM
1966 if (addr) {
1967 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
ef48ff94
JM
1968 os_memset(&event, 0, sizeof(event));
1969 event.deauth_info.reason_code = (u16) reason_code;
1970 event.deauth_info.locally_generated = 1;
1971 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
42d23547
JM
1972 if (zero_addr)
1973 addr = NULL;
6fc6879b 1974 }
09f58c09
JM
1975
1976 wpa_supplicant_clear_connection(wpa_s, addr);
6fc6879b
JM
1977}
1978
dca1a511
DS
1979static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
1980 struct wpa_ssid *ssid)
1981{
1982 if (!ssid || !ssid->disabled || ssid->disabled == 2)
1983 return;
1984
1985 ssid->disabled = 0;
1986 wpas_clear_temp_disabled(wpa_s, ssid, 1);
1987 wpas_notify_network_enabled_changed(wpa_s, ssid);
1988
1989 /*
1990 * Try to reassociate since there is no current configuration and a new
1991 * network was made available.
1992 */
d2592497 1993 if (!wpa_s->current_ssid && !wpa_s->disconnected)
dca1a511
DS
1994 wpa_s->reassociate = 1;
1995}
1996
6fc6879b 1997
86b89452
WS
1998/**
1999 * wpa_supplicant_enable_network - Mark a configured network as enabled
2000 * @wpa_s: wpa_supplicant structure for a network interface
2001 * @ssid: wpa_ssid structure for a configured network or %NULL
2002 *
2003 * Enables the specified network or all networks if no network specified.
2004 */
2005void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
2006 struct wpa_ssid *ssid)
2007{
86b89452 2008 if (ssid == NULL) {
14f79078
JM
2009 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2010 wpa_supplicant_enable_one_network(wpa_s, ssid);
dca1a511
DS
2011 } else
2012 wpa_supplicant_enable_one_network(wpa_s, ssid);
86b89452 2013
d2592497 2014 if (wpa_s->reassociate && !wpa_s->disconnected) {
dca1a511
DS
2015 if (wpa_s->sched_scanning) {
2016 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
2017 "new network to scan filters");
2018 wpa_supplicant_cancel_sched_scan(wpa_s);
2019 }
86b89452 2020
dad153d1
JM
2021 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2022 wpa_supplicant_req_scan(wpa_s, 0, 0);
86b89452
WS
2023 }
2024}
2025
2026
2027/**
2028 * wpa_supplicant_disable_network - Mark a configured network as disabled
2029 * @wpa_s: wpa_supplicant structure for a network interface
2030 * @ssid: wpa_ssid structure for a configured network or %NULL
2031 *
2032 * Disables the specified network or all networks if no network specified.
2033 */
2034void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2035 struct wpa_ssid *ssid)
2036{
2037 struct wpa_ssid *other_ssid;
2038 int was_disabled;
2039
2040 if (ssid == NULL) {
725fc39e
DS
2041 if (wpa_s->sched_scanning)
2042 wpa_supplicant_cancel_sched_scan(wpa_s);
2043
4dac0245
JM
2044 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2045 other_ssid = other_ssid->next) {
86b89452 2046 was_disabled = other_ssid->disabled;
4dac0245
JM
2047 if (was_disabled == 2)
2048 continue; /* do not change persistent P2P group
2049 * data */
86b89452
WS
2050
2051 other_ssid->disabled = 1;
2052
2053 if (was_disabled != other_ssid->disabled)
2054 wpas_notify_network_enabled_changed(
2055 wpa_s, other_ssid);
86b89452
WS
2056 }
2057 if (wpa_s->current_ssid)
07783eaa 2058 wpa_supplicant_deauthenticate(
86b89452 2059 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4dac0245 2060 } else if (ssid->disabled != 2) {
86b89452 2061 if (ssid == wpa_s->current_ssid)
07783eaa 2062 wpa_supplicant_deauthenticate(
86b89452
WS
2063 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2064
2065 was_disabled = ssid->disabled;
2066
2067 ssid->disabled = 1;
2068
725fc39e 2069 if (was_disabled != ssid->disabled) {
86b89452 2070 wpas_notify_network_enabled_changed(wpa_s, ssid);
725fc39e
DS
2071 if (wpa_s->sched_scanning) {
2072 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
2073 "to remove network from filters");
2074 wpa_supplicant_cancel_sched_scan(wpa_s);
2075 wpa_supplicant_req_scan(wpa_s, 0, 0);
2076 }
2077 }
86b89452
WS
2078 }
2079}
2080
2081
2082/**
2083 * wpa_supplicant_select_network - Attempt association with a network
2084 * @wpa_s: wpa_supplicant structure for a network interface
2085 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
2086 */
2087void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
2088 struct wpa_ssid *ssid)
2089{
2090
2091 struct wpa_ssid *other_ssid;
d93dfbd5 2092 int disconnected = 0;
86b89452 2093
d93dfbd5 2094 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
07783eaa 2095 wpa_supplicant_deauthenticate(
86b89452 2096 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
d93dfbd5
JM
2097 disconnected = 1;
2098 }
86b89452 2099
00e5e3d5
JM
2100 if (ssid)
2101 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2102
86b89452
WS
2103 /*
2104 * Mark all other networks disabled or mark all networks enabled if no
2105 * network specified.
2106 */
4dac0245
JM
2107 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2108 other_ssid = other_ssid->next) {
86b89452 2109 int was_disabled = other_ssid->disabled;
4dac0245
JM
2110 if (was_disabled == 2)
2111 continue; /* do not change persistent P2P group data */
86b89452
WS
2112
2113 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
00e5e3d5
JM
2114 if (was_disabled && !other_ssid->disabled)
2115 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
86b89452
WS
2116
2117 if (was_disabled != other_ssid->disabled)
2118 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
86b89452 2119 }
2a6f78fb
JJ
2120
2121 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
2122 /* We are already associated with the selected network */
2123 wpa_printf(MSG_DEBUG, "Already associated with the "
2124 "selected network - do nothing");
2125 return;
2126 }
2127
25a8f9e3 2128 if (ssid) {
96efeeb6 2129 wpa_s->current_ssid = ssid;
25a8f9e3
JM
2130 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2131 }
7dcdcfd6 2132 wpa_s->connect_without_scan = NULL;
86b89452
WS
2133 wpa_s->disconnected = 0;
2134 wpa_s->reassociate = 1;
cecdddc1
PS
2135
2136 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2137 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
86b89452 2138
a1641d26
JM
2139 if (ssid)
2140 wpas_notify_network_selected(wpa_s, ssid);
86b89452
WS
2141}
2142
2143
bdec7ee5
MS
2144/**
2145 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
2146 * @wpa_s: wpa_supplicant structure for a network interface
2147 * @pkcs11_engine_path: PKCS #11 engine path or NULL
2148 * @pkcs11_module_path: PKCS #11 module path or NULL
2149 * Returns: 0 on success; -1 on failure
2150 *
2151 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
2152 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
2153 * module path fails the paths will be reset to the default value (NULL).
2154 */
2155int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
2156 const char *pkcs11_engine_path,
2157 const char *pkcs11_module_path)
2158{
2159 char *pkcs11_engine_path_copy = NULL;
2160 char *pkcs11_module_path_copy = NULL;
2161
2162 if (pkcs11_engine_path != NULL) {
2163 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
2164 if (pkcs11_engine_path_copy == NULL)
2165 return -1;
2166 }
2167 if (pkcs11_module_path != NULL) {
2168 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
04c366cb 2169 if (pkcs11_module_path_copy == NULL) {
bdec7ee5
MS
2170 os_free(pkcs11_engine_path_copy);
2171 return -1;
2172 }
2173 }
2174
2175 os_free(wpa_s->conf->pkcs11_engine_path);
2176 os_free(wpa_s->conf->pkcs11_module_path);
2177 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
2178 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
2179
2180 wpa_sm_set_eapol(wpa_s->wpa, NULL);
2181 eapol_sm_deinit(wpa_s->eapol);
2182 wpa_s->eapol = NULL;
2183 if (wpa_supplicant_init_eapol(wpa_s)) {
2184 /* Error -> Reset paths to the default value (NULL) once. */
2185 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
2186 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
2187 NULL);
2188
2189 return -1;
2190 }
2191 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2192
2193 return 0;
2194}
2195
2196
86b89452
WS
2197/**
2198 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
2199 * @wpa_s: wpa_supplicant structure for a network interface
2200 * @ap_scan: AP scan mode
2201 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
2202 *
2203 */
2204int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
2205{
2206
2207 int old_ap_scan;
2208
2209 if (ap_scan < 0 || ap_scan > 2)
2210 return -1;
2211
48f8e036
DS
2212#ifdef ANDROID
2213 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
2214 wpa_s->wpa_state >= WPA_ASSOCIATING &&
2215 wpa_s->wpa_state < WPA_COMPLETED) {
2216 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
2217 "associating", wpa_s->conf->ap_scan, ap_scan);
2218 return 0;
2219 }
2220#endif /* ANDROID */
2221
86b89452
WS
2222 old_ap_scan = wpa_s->conf->ap_scan;
2223 wpa_s->conf->ap_scan = ap_scan;
2224
2225 if (old_ap_scan != wpa_s->conf->ap_scan)
2226 wpas_notify_ap_scan_changed(wpa_s);
2227
2228 return 0;
2229}
2230
2231
78633c37
SL
2232/**
2233 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
2234 * @wpa_s: wpa_supplicant structure for a network interface
2235 * @expire_age: Expiration age in seconds
2236 * Returns: 0 if succeed or -1 if expire_age has an invalid value
2237 *
2238 */
2239int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
2240 unsigned int bss_expire_age)
2241{
2242 if (bss_expire_age < 10) {
2243 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
2244 bss_expire_age);
2245 return -1;
2246 }
2247 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
2248 bss_expire_age);
2249 wpa_s->conf->bss_expiration_age = bss_expire_age;
2250
2251 return 0;
2252}
2253
2254
2255/**
2256 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
2257 * @wpa_s: wpa_supplicant structure for a network interface
2258 * @expire_count: number of scans after which an unseen BSS is reclaimed
2259 * Returns: 0 if succeed or -1 if expire_count has an invalid value
2260 *
2261 */
2262int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
2263 unsigned int bss_expire_count)
2264{
2265 if (bss_expire_count < 1) {
2266 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
2267 bss_expire_count);
2268 return -1;
2269 }
2270 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
2271 bss_expire_count);
2272 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
2273
2274 return 0;
2275}
2276
2277
c6e86b63
MA
2278/**
2279 * wpa_supplicant_set_scan_interval - Set scan interval
2280 * @wpa_s: wpa_supplicant structure for a network interface
2281 * @scan_interval: scan interval in seconds
2282 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
2283 *
2284 */
2285int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
2286 int scan_interval)
2287{
2288 if (scan_interval < 0) {
2289 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
2290 scan_interval);
2291 return -1;
2292 }
2293 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
2294 scan_interval);
9e737f08 2295 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
c6e86b63
MA
2296
2297 return 0;
2298}
2299
2300
86b89452
WS
2301/**
2302 * wpa_supplicant_set_debug_params - Set global debug params
2303 * @global: wpa_global structure
2304 * @debug_level: debug level
2305 * @debug_timestamp: determines if show timestamp in debug data
2306 * @debug_show_keys: determines if show keys in debug data
2307 * Returns: 0 if succeed or -1 if debug_level has wrong value
2308 */
2309int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
2310 int debug_timestamp, int debug_show_keys)
2311{
2312
2313 int old_level, old_timestamp, old_show_keys;
2314
2315 /* check for allowed debuglevels */
14dc0011
PS
2316 if (debug_level != MSG_EXCESSIVE &&
2317 debug_level != MSG_MSGDUMP &&
86b89452
WS
2318 debug_level != MSG_DEBUG &&
2319 debug_level != MSG_INFO &&
2320 debug_level != MSG_WARNING &&
2321 debug_level != MSG_ERROR)
2322 return -1;
2323
2324 old_level = wpa_debug_level;
2325 old_timestamp = wpa_debug_timestamp;
2326 old_show_keys = wpa_debug_show_keys;
2327
2328 wpa_debug_level = debug_level;
2329 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
2330 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
2331
db9133ac
WS
2332 if (wpa_debug_level != old_level)
2333 wpas_notify_debug_level_changed(global);
2334 if (wpa_debug_timestamp != old_timestamp)
2335 wpas_notify_debug_timestamp_changed(global);
2336 if (wpa_debug_show_keys != old_show_keys)
2337 wpas_notify_debug_show_keys_changed(global);
86b89452
WS
2338
2339 return 0;
2340}
2341
2342
6fc6879b
JM
2343/**
2344 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2345 * @wpa_s: Pointer to wpa_supplicant data
2346 * Returns: A pointer to the current network structure or %NULL on failure
2347 */
2348struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2349{
2350 struct wpa_ssid *entry;
2351 u8 ssid[MAX_SSID_LEN];
2352 int res;
2353 size_t ssid_len;
2354 u8 bssid[ETH_ALEN];
2355 int wired;
2356
17fbb751
JM
2357 res = wpa_drv_get_ssid(wpa_s, ssid);
2358 if (res < 0) {
2359 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2360 "driver");
2361 return NULL;
6fc6879b 2362 }
17fbb751 2363 ssid_len = res;
6fc6879b 2364
17fbb751 2365 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
f049052b
BG
2366 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2367 "driver");
6fc6879b
JM
2368 return NULL;
2369 }
2370
c2a04078
JM
2371 wired = wpa_s->conf->ap_scan == 0 &&
2372 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
6fc6879b
JM
2373
2374 entry = wpa_s->conf->ssid;
2375 while (entry) {
349493bd 2376 if (!wpas_network_disabled(wpa_s, entry) &&
6fc6879b
JM
2377 ((ssid_len == entry->ssid_len &&
2378 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2379 (!entry->bssid_set ||
2380 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2381 return entry;
24c23d1b 2382#ifdef CONFIG_WPS
349493bd 2383 if (!wpas_network_disabled(wpa_s, entry) &&
24c23d1b
JM
2384 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2385 (entry->ssid == NULL || entry->ssid_len == 0) &&
2386 (!entry->bssid_set ||
2387 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2388 return entry;
2389#endif /* CONFIG_WPS */
7d232e23 2390
349493bd 2391 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
7d232e23
ZC
2392 entry->ssid_len == 0 &&
2393 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2394 return entry;
2395
6fc6879b
JM
2396 entry = entry->next;
2397 }
2398
2399 return NULL;
2400}
2401
2402
7756114f
JM
2403static int select_driver(struct wpa_supplicant *wpa_s, int i)
2404{
2405 struct wpa_global *global = wpa_s->global;
2406
2407 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2408 global->drv_priv[i] = wpa_drivers[i]->global_init();
2409 if (global->drv_priv[i] == NULL) {
2410 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2411 "'%s'", wpa_drivers[i]->name);
2412 return -1;
2413 }
2414 }
2415
2416 wpa_s->driver = wpa_drivers[i];
2417 wpa_s->global_drv_priv = global->drv_priv[i];
2418
2419 return 0;
2420}
2421
2422
6fc6879b
JM
2423static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2424 const char *name)
2425{
2426 int i;
362f781e 2427 size_t len;
74b1c84a 2428 const char *pos, *driver = name;
6fc6879b
JM
2429
2430 if (wpa_s == NULL)
2431 return -1;
2432
c5121837 2433 if (wpa_drivers[0] == NULL) {
f049052b
BG
2434 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2435 "wpa_supplicant");
6fc6879b
JM
2436 return -1;
2437 }
2438
2439 if (name == NULL) {
2440 /* default to first driver in the list */
7756114f 2441 return select_driver(wpa_s, 0);
6fc6879b
JM
2442 }
2443
74b1c84a
SO
2444 do {
2445 pos = os_strchr(driver, ',');
2446 if (pos)
2447 len = pos - driver;
2448 else
2449 len = os_strlen(driver);
2450
2451 for (i = 0; wpa_drivers[i]; i++) {
2452 if (os_strlen(wpa_drivers[i]->name) == len &&
2453 os_strncmp(driver, wpa_drivers[i]->name, len) ==
0f4668ce
DW
2454 0) {
2455 /* First driver that succeeds wins */
2456 if (select_driver(wpa_s, i) == 0)
2457 return 0;
2458 }
6fc6879b 2459 }
74b1c84a
SO
2460
2461 driver = pos + 1;
2462 } while (pos);
6fc6879b 2463
f049052b 2464 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
6fc6879b
JM
2465 return -1;
2466}
2467
2468
a8e0505b
JM
2469/**
2470 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2471 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2472 * with struct wpa_driver_ops::init()
2473 * @src_addr: Source address of the EAPOL frame
2474 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2475 * @len: Length of the EAPOL data
2476 *
2477 * This function is called for each received EAPOL frame. Most driver
2478 * interfaces rely on more generic OS mechanism for receiving frames through
2479 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2480 * take care of received EAPOL frames and deliver them to the core supplicant
2481 * code by calling this function.
2482 */
6fc6879b
JM
2483void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2484 const u8 *buf, size_t len)
2485{
2486 struct wpa_supplicant *wpa_s = ctx;
2487
f049052b 2488 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
6fc6879b
JM
2489 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2490
db76aa64
JM
2491#ifdef CONFIG_PEERKEY
2492 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
2493 wpa_s->current_ssid->peerkey &&
2494 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2495 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
2496 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
2497 return;
2498 }
2499#endif /* CONFIG_PEERKEY */
2500
3ab35a66
JM
2501 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2502 (wpa_s->last_eapol_matches_bssid &&
2503#ifdef CONFIG_AP
2504 !wpa_s->ap_iface &&
2505#endif /* CONFIG_AP */
2506 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
1ff73338
JM
2507 /*
2508 * There is possible race condition between receiving the
2509 * association event and the EAPOL frame since they are coming
2510 * through different paths from the driver. In order to avoid
2511 * issues in trying to process the EAPOL frame before receiving
2512 * association information, lets queue it for processing until
3ab35a66
JM
2513 * the association event is received. This may also be needed in
2514 * driver-based roaming case, so also use src_addr != BSSID as a
2515 * trigger if we have previously confirmed that the
2516 * Authenticator uses BSSID as the src_addr (which is not the
2517 * case with wired IEEE 802.1X).
1ff73338 2518 */
f049052b 2519 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
3ab35a66
JM
2520 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
2521 wpa_supplicant_state_txt(wpa_s->wpa_state),
2522 MAC2STR(wpa_s->bssid));
1ff73338
JM
2523 wpabuf_free(wpa_s->pending_eapol_rx);
2524 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2525 if (wpa_s->pending_eapol_rx) {
c2be937c 2526 os_get_reltime(&wpa_s->pending_eapol_rx_time);
1ff73338
JM
2527 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2528 ETH_ALEN);
2529 }
2530 return;
2531 }
2532
3ab35a66
JM
2533 wpa_s->last_eapol_matches_bssid =
2534 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
2535
db149ac9
JM
2536#ifdef CONFIG_AP
2537 if (wpa_s->ap_iface) {
2538 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2539 return;
2540 }
2541#endif /* CONFIG_AP */
2542
6fc6879b 2543 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
f049052b
BG
2544 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2545 "no key management is configured");
6fc6879b
JM
2546 return;
2547 }
2548
2549 if (wpa_s->eapol_received == 0 &&
c2a04078 2550 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
56586197 2551 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
9c972abb
JM
2552 wpa_s->wpa_state != WPA_COMPLETED) &&
2553 (wpa_s->current_ssid == NULL ||
2554 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
6fc6879b
JM
2555 /* Timeout for completing IEEE 802.1X and WPA authentication */
2556 wpa_supplicant_req_auth_timeout(
2557 wpa_s,
56586197 2558 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
a6f06dab
AT
2559 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2560 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
6fc6879b
JM
2561 70 : 10, 0);
2562 }
2563 wpa_s->eapol_received++;
2564
2565 if (wpa_s->countermeasures) {
f049052b
BG
2566 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2567 "EAPOL packet");
6fc6879b
JM
2568 return;
2569 }
2570
8be18440
JM
2571#ifdef CONFIG_IBSS_RSN
2572 if (wpa_s->current_ssid &&
d7dcba70 2573 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
8be18440
JM
2574 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2575 return;
2576 }
2577#endif /* CONFIG_IBSS_RSN */
2578
6fc6879b
JM
2579 /* Source address of the incoming EAPOL frame could be compared to the
2580 * current BSSID. However, it is possible that a centralized
2581 * Authenticator could be using another MAC address than the BSSID of
2582 * an AP, so just allow any address to be used for now. The replies are
2583 * still sent to the current BSSID (if available), though. */
2584
2585 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
56586197 2586 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
6fc6879b
JM
2587 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2588 return;
2589 wpa_drv_poll(wpa_s);
c2a04078 2590 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
6fc6879b 2591 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
56586197 2592 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6fc6879b
JM
2593 /*
2594 * Set portValid = TRUE here since we are going to skip 4-way
2595 * handshake processing which would normally set portValid. We
2596 * need this to allow the EAPOL state machines to be completed
2597 * without going through EAPOL-Key handshake.
2598 */
2599 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2600 }
2601}
2602
2603
bfba8deb 2604int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
6fc6879b 2605{
6fc6879b
JM
2606 if (wpa_s->driver->send_eapol) {
2607 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2608 if (addr)
2609 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1c42b42f
JM
2610 } else if ((!wpa_s->p2p_mgmt ||
2611 !(wpa_s->drv_flags &
2612 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
c68f6200 2613 !(wpa_s->drv_flags &
fdadd5fe 2614 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
bfba8deb 2615 l2_packet_deinit(wpa_s->l2);
6fc6879b
JM
2616 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2617 wpa_drv_get_mac_addr(wpa_s),
2618 ETH_P_EAPOL,
2619 wpa_supplicant_rx_eapol, wpa_s, 0);
2620 if (wpa_s->l2 == NULL)
2621 return -1;
fdadd5fe
JM
2622 } else {
2623 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2624 if (addr)
2625 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
6fc6879b
JM
2626 }
2627
2628 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
f049052b 2629 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
6fc6879b
JM
2630 return -1;
2631 }
2632
bfba8deb
JM
2633 return 0;
2634}
2635
2636
25f839c6
JM
2637static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2638 const u8 *buf, size_t len)
2639{
2640 struct wpa_supplicant *wpa_s = ctx;
2641 const struct l2_ethhdr *eth;
2642
2643 if (len < sizeof(*eth))
2644 return;
2645 eth = (const struct l2_ethhdr *) buf;
2646
2647 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2648 !(eth->h_dest[0] & 0x01)) {
2649 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2650 " (bridge - not for this interface - ignore)",
2651 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2652 return;
2653 }
2654
2655 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2656 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2657 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2658 len - sizeof(*eth));
2659}
2660
2661
bfba8deb
JM
2662/**
2663 * wpa_supplicant_driver_init - Initialize driver interface parameters
2664 * @wpa_s: Pointer to wpa_supplicant data
2665 * Returns: 0 on success, -1 on failure
2666 *
2667 * This function is called to initialize driver interface parameters.
2668 * wpa_drv_init() must have been called before this function to initialize the
2669 * driver interface.
2670 */
2671int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2672{
2673 static int interface_count = 0;
2674
2675 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2676 return -1;
2677
c68f6200
AS
2678 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2679 MAC2STR(wpa_s->own_addr));
2680 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2681
6fc6879b 2682 if (wpa_s->bridge_ifname[0]) {
f049052b
BG
2683 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2684 "interface '%s'", wpa_s->bridge_ifname);
6fc6879b
JM
2685 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2686 wpa_s->own_addr,
2687 ETH_P_EAPOL,
25f839c6
JM
2688 wpa_supplicant_rx_eapol_bridge,
2689 wpa_s, 1);
6fc6879b 2690 if (wpa_s->l2_br == NULL) {
f049052b
BG
2691 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2692 "connection for the bridge interface '%s'",
2693 wpa_s->bridge_ifname);
6fc6879b
JM
2694 return -1;
2695 }
2696 }
2697
6fc6879b
JM
2698 wpa_clear_keys(wpa_s, NULL);
2699
2700 /* Make sure that TKIP countermeasures are not left enabled (could
2701 * happen if wpa_supplicant is killed during countermeasures. */
2702 wpa_drv_set_countermeasures(wpa_s, 0);
2703
f049052b 2704 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
6fc6879b
JM
2705 wpa_drv_flush_pmkid(wpa_s);
2706
ba2a573c 2707 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
b3aa456b
ES
2708 wpa_s->prev_scan_wildcard = 0;
2709
349493bd 2710 if (wpa_supplicant_enabled_networks(wpa_s)) {
a0e9d892
AS
2711 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2712 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2713 interface_count = 0;
2714 }
3a94adbf 2715 if (!wpa_s->p2p_mgmt &&
5d0d72a3
BG
2716 wpa_supplicant_delayed_sched_scan(wpa_s,
2717 interface_count % 3,
6a90053c 2718 100000))
5d0d72a3 2719 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
a4cba8f1 2720 100000);
74e259ec
JM
2721 interface_count++;
2722 } else
2723 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
6fc6879b
JM
2724
2725 return 0;
2726}
2727
2728
2729static int wpa_supplicant_daemon(const char *pid_file)
2730{
2731 wpa_printf(MSG_DEBUG, "Daemonize..");
2732 return os_daemonize(pid_file);
2733}
2734
2735
2736static struct wpa_supplicant * wpa_supplicant_alloc(void)
2737{
2738 struct wpa_supplicant *wpa_s;
2739
2740 wpa_s = os_zalloc(sizeof(*wpa_s));
2741 if (wpa_s == NULL)
2742 return NULL;
4115303b 2743 wpa_s->scan_req = INITIAL_SCAN_REQ;
67b9bd08 2744 wpa_s->scan_interval = 5;
c302f207 2745 wpa_s->new_connection = 1;
b22128ef 2746 wpa_s->parent = wpa_s;
cbdf3507 2747 wpa_s->sched_scanning = 0;
6fc6879b
JM
2748
2749 return wpa_s;
2750}
2751
2752
80e8a5ee
BG
2753#ifdef CONFIG_HT_OVERRIDES
2754
2755static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2756 struct ieee80211_ht_capabilities *htcaps,
2757 struct ieee80211_ht_capabilities *htcaps_mask,
2758 const char *ht_mcs)
2759{
2760 /* parse ht_mcs into hex array */
2761 int i;
2762 const char *tmp = ht_mcs;
2763 char *end = NULL;
2764
2765 /* If ht_mcs is null, do not set anything */
2766 if (!ht_mcs)
2767 return 0;
2768
2769 /* This is what we are setting in the kernel */
2770 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2771
2772 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2773
2774 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2775 errno = 0;
2776 long v = strtol(tmp, &end, 16);
2777 if (errno == 0) {
2778 wpa_msg(wpa_s, MSG_DEBUG,
2779 "htcap value[%i]: %ld end: %p tmp: %p",
2780 i, v, end, tmp);
2781 if (end == tmp)
2782 break;
2783
2784 htcaps->supported_mcs_set[i] = v;
2785 tmp = end;
2786 } else {
2787 wpa_msg(wpa_s, MSG_ERROR,
2788 "Failed to parse ht-mcs: %s, error: %s\n",
2789 ht_mcs, strerror(errno));
2790 return -1;
2791 }
2792 }
2793
2794 /*
2795 * If we were able to parse any values, then set mask for the MCS set.
2796 */
2797 if (i) {
2798 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2799 IEEE80211_HT_MCS_MASK_LEN - 1);
2800 /* skip the 3 reserved bits */
2801 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2802 0x1f;
2803 }
2804
2805 return 0;
2806}
2807
2808
2809static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2810 struct ieee80211_ht_capabilities *htcaps,
2811 struct ieee80211_ht_capabilities *htcaps_mask,
2812 int disabled)
2813{
2814 u16 msk;
2815
2816 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2817
2818 if (disabled == -1)
2819 return 0;
2820
2821 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2822 htcaps_mask->ht_capabilities_info |= msk;
2823 if (disabled)
2824 htcaps->ht_capabilities_info &= msk;
2825 else
2826 htcaps->ht_capabilities_info |= msk;
2827
2828 return 0;
2829}
2830
2831
2832static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2833 struct ieee80211_ht_capabilities *htcaps,
2834 struct ieee80211_ht_capabilities *htcaps_mask,
2835 int factor)
2836{
2837 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2838
2839 if (factor == -1)
2840 return 0;
2841
2842 if (factor < 0 || factor > 3) {
2843 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2844 "Must be 0-3 or -1", factor);
2845 return -EINVAL;
2846 }
2847
2848 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2849 htcaps->a_mpdu_params &= ~0x3;
2850 htcaps->a_mpdu_params |= factor & 0x3;
2851
2852 return 0;
2853}
2854
2855
2856static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2857 struct ieee80211_ht_capabilities *htcaps,
2858 struct ieee80211_ht_capabilities *htcaps_mask,
2859 int density)
2860{
2861 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2862
2863 if (density == -1)
2864 return 0;
2865
2866 if (density < 0 || density > 7) {
2867 wpa_msg(wpa_s, MSG_ERROR,
2868 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2869 density);
2870 return -EINVAL;
2871 }
2872
2873 htcaps_mask->a_mpdu_params |= 0x1C;
2874 htcaps->a_mpdu_params &= ~(0x1C);
2875 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2876
2877 return 0;
2878}
2879
2880
2881static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2882 struct ieee80211_ht_capabilities *htcaps,
2883 struct ieee80211_ht_capabilities *htcaps_mask,
2884 int disabled)
2885{
2886 /* Masking these out disables HT40 */
2887 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2888 HT_CAP_INFO_SHORT_GI40MHZ);
2889
2890 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2891
2892 if (disabled)
2893 htcaps->ht_capabilities_info &= ~msk;
2894 else
2895 htcaps->ht_capabilities_info |= msk;
2896
2897 htcaps_mask->ht_capabilities_info |= msk;
2898
2899 return 0;
2900}
2901
2902
a90497f8
BG
2903static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
2904 struct ieee80211_ht_capabilities *htcaps,
2905 struct ieee80211_ht_capabilities *htcaps_mask,
2906 int disabled)
2907{
2908 /* Masking these out disables SGI */
2909 u16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
2910 HT_CAP_INFO_SHORT_GI40MHZ);
2911
2912 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
2913
2914 if (disabled)
2915 htcaps->ht_capabilities_info &= ~msk;
2916 else
2917 htcaps->ht_capabilities_info |= msk;
2918
2919 htcaps_mask->ht_capabilities_info |= msk;
2920
2921 return 0;
2922}
2923
2924
39a5800f
PK
2925static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
2926 struct ieee80211_ht_capabilities *htcaps,
2927 struct ieee80211_ht_capabilities *htcaps_mask,
2928 int disabled)
2929{
2930 /* Masking these out disables LDPC */
2931 u16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
2932
2933 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
2934
2935 if (disabled)
2936 htcaps->ht_capabilities_info &= ~msk;
2937 else
2938 htcaps->ht_capabilities_info |= msk;
2939
2940 htcaps_mask->ht_capabilities_info |= msk;
2941
2942 return 0;
2943}
2944
2945
80e8a5ee
BG
2946void wpa_supplicant_apply_ht_overrides(
2947 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2948 struct wpa_driver_associate_params *params)
2949{
2950 struct ieee80211_ht_capabilities *htcaps;
2951 struct ieee80211_ht_capabilities *htcaps_mask;
2952
2953 if (!ssid)
2954 return;
2955
2956 params->disable_ht = ssid->disable_ht;
2957 if (!params->htcaps || !params->htcaps_mask)
2958 return;
2959
2960 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2961 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2962 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2963 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2964 ssid->disable_max_amsdu);
2965 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2966 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2967 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
a90497f8 2968 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
39a5800f 2969 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
80e8a5ee
BG
2970}
2971
2972#endif /* CONFIG_HT_OVERRIDES */
2973
2974
e9ee8dc3
JB
2975#ifdef CONFIG_VHT_OVERRIDES
2976void wpa_supplicant_apply_vht_overrides(
2977 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2978 struct wpa_driver_associate_params *params)
2979{
2980 struct ieee80211_vht_capabilities *vhtcaps;
2981 struct ieee80211_vht_capabilities *vhtcaps_mask;
4f560cde
EP
2982#ifdef CONFIG_HT_OVERRIDES
2983 int max_ampdu;
2984 const u32 max_ampdu_mask = VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX;
2985#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
2986
2987 if (!ssid)
2988 return;
2989
2990 params->disable_vht = ssid->disable_vht;
2991
2992 vhtcaps = (void *) params->vhtcaps;
2993 vhtcaps_mask = (void *) params->vhtcaps_mask;
2994
2995 if (!vhtcaps || !vhtcaps_mask)
2996 return;
2997
2998 vhtcaps->vht_capabilities_info = ssid->vht_capa;
2999 vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
3000
4f560cde
EP
3001#ifdef CONFIG_HT_OVERRIDES
3002 /* if max ampdu is <= 3, we have to make the HT cap the same */
3003 if (ssid->vht_capa_mask & max_ampdu_mask) {
3004 max_ampdu = (ssid->vht_capa & max_ampdu_mask) >>
3005 find_first_bit(max_ampdu_mask);
3006
3007 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
3008 wpa_set_ampdu_factor(wpa_s,
3009 (void *) params->htcaps,
3010 (void *) params->htcaps_mask,
3011 max_ampdu);
3012 }
3013#endif /* CONFIG_HT_OVERRIDES */
3014
e9ee8dc3
JB
3015#define OVERRIDE_MCS(i) \
3016 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
3017 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
3018 3 << 2 * (i - 1); \
3019 vhtcaps->vht_supported_mcs_set.tx_map |= \
3020 ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
3021 } \
3022 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
3023 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
3024 3 << 2 * (i - 1); \
3025 vhtcaps->vht_supported_mcs_set.rx_map |= \
3026 ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
3027 }
3028
3029 OVERRIDE_MCS(1);
3030 OVERRIDE_MCS(2);
3031 OVERRIDE_MCS(3);
3032 OVERRIDE_MCS(4);
3033 OVERRIDE_MCS(5);
3034 OVERRIDE_MCS(6);
3035 OVERRIDE_MCS(7);
3036 OVERRIDE_MCS(8);
3037}
3038#endif /* CONFIG_VHT_OVERRIDES */
3039
3040
f64adcd7
JM
3041static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
3042{
3043#ifdef PCSC_FUNCS
3044 size_t len;
3045
3046 if (!wpa_s->conf->pcsc_reader)
3047 return 0;
3048
22cf7d73 3049 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
f64adcd7
JM
3050 if (!wpa_s->scard)
3051 return 1;
3052
3053 if (wpa_s->conf->pcsc_pin &&
3054 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
3055 scard_deinit(wpa_s->scard);
3056 wpa_s->scard = NULL;
3057 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
3058 return -1;
3059 }
3060
3061 len = sizeof(wpa_s->imsi) - 1;
3062 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
3063 scard_deinit(wpa_s->scard);
3064 wpa_s->scard = NULL;
3065 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
3066 return -1;
3067 }
3068 wpa_s->imsi[len] = '\0';
3069
3070 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
3071
3072 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
3073 wpa_s->imsi, wpa_s->mnc_len);
3074
3075 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
3076 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
3077#endif /* PCSC_FUNCS */
3078
3079 return 0;
3080}
3081
3082
306ae225
JM
3083int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
3084{
3085 char *val, *pos;
3086
3087 ext_password_deinit(wpa_s->ext_pw);
3088 wpa_s->ext_pw = NULL;
3089 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
3090
3091 if (!wpa_s->conf->ext_password_backend)
3092 return 0;
3093
3094 val = os_strdup(wpa_s->conf->ext_password_backend);
3095 if (val == NULL)
3096 return -1;
3097 pos = os_strchr(val, ':');
3098 if (pos)
3099 *pos++ = '\0';
3100
3101 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
3102
3103 wpa_s->ext_pw = ext_password_init(val, pos);
3104 os_free(val);
3105 if (wpa_s->ext_pw == NULL) {
3106 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
3107 return -1;
3108 }
3109 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
3110
3111 return 0;
3112}
3113
3114
202dec2a
JM
3115static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3116 const char *rn)
3117{
3118 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3119 struct wpa_radio *radio;
3120
3121 while (rn && iface) {
3122 radio = iface->radio;
3123 if (radio && os_strcmp(rn, radio->name) == 0) {
3124 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3125 wpa_s->ifname, rn);
3126 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3127 return radio;
3128 }
b154a24e
TB
3129
3130 iface = iface->next;
202dec2a
JM
3131 }
3132
3133 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3134 wpa_s->ifname, rn ? rn : "N/A");
3135 radio = os_zalloc(sizeof(*radio));
3136 if (radio == NULL)
3137 return NULL;
3138
3139 if (rn)
3140 os_strlcpy(radio->name, rn, sizeof(radio->name));
3141 dl_list_init(&radio->ifaces);
b1ae396f 3142 dl_list_init(&radio->work);
202dec2a
JM
3143 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3144
3145 return radio;
3146}
3147
3148
b1ae396f
JM
3149static void radio_work_free(struct wpa_radio_work *work)
3150{
d12a51b5
JM
3151 if (work->wpa_s->scan_work == work) {
3152 /* This should not really happen. */
3153 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3154 work->type, work, work->started);
3155 work->wpa_s->scan_work = NULL;
3156 }
3157
1b5d4714
JM
3158#ifdef CONFIG_P2P
3159 if (work->wpa_s->p2p_scan_work == work) {
3160 /* This should not really happen. */
3161 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3162 work->type, work, work->started);
3163 work->wpa_s->p2p_scan_work = NULL;
3164 }
3165#endif /* CONFIG_P2P */
3166
b1ae396f
JM
3167 dl_list_del(&work->list);
3168 os_free(work);
3169}
3170
3171
3172static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3173{
3174 struct wpa_radio *radio = eloop_ctx;
3175 struct wpa_radio_work *work;
3176 struct os_reltime now, diff;
6428d0a7 3177 struct wpa_supplicant *wpa_s;
b1ae396f
JM
3178
3179 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3180 if (work == NULL)
3181 return;
3182
3183 if (work->started)
3184 return; /* already started and still in progress */
3185
6428d0a7
JM
3186 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3187 radio_list);
3188 if (wpa_s && wpa_s->external_scan_running) {
3189 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3190 return;
3191 }
3192
b1ae396f
JM
3193 os_get_reltime(&now);
3194 os_reltime_sub(&now, &work->time, &diff);
3195 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3196 work->type, work, diff.sec, diff.usec);
3197 work->started = 1;
3198 work->time = now;
3199 work->cb(work, 0);
3200}
3201
3202
b3253ebb
AO
3203/*
3204 * This function removes both started and pending radio works running on
3205 * the provided interface's radio.
3206 * Prior to the removal of the radio work, its callback (cb) is called with
3207 * deinit set to be 1. Each work's callback is responsible for clearing its
3208 * internal data and restoring to a correct state.
3209 * @wpa_s: wpa_supplicant data
3210 * @type: type of works to be removed
3211 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3212 * this interface's works.
3213 */
3214void radio_remove_works(struct wpa_supplicant *wpa_s,
3215 const char *type, int remove_all)
b1ae396f
JM
3216{
3217 struct wpa_radio_work *work, *tmp;
3218 struct wpa_radio *radio = wpa_s->radio;
3219
3220 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3221 list) {
b3253ebb 3222 if (type && os_strcmp(type, work->type) != 0)
b1ae396f 3223 continue;
b3253ebb
AO
3224
3225 /* skip other ifaces' works */
3226 if (!remove_all && work->wpa_s != wpa_s)
b1ae396f 3227 continue;
b3253ebb
AO
3228
3229 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3230 work->type, work, work->started ? " (started)" : "");
b1ae396f
JM
3231 work->cb(work, 1);
3232 radio_work_free(work);
3233 }
b3253ebb
AO
3234
3235 /* in case we removed the started work */
3236 radio_work_check_next(wpa_s);
b1ae396f
JM
3237}
3238
3239
202dec2a
JM
3240static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3241{
3242 struct wpa_radio *radio = wpa_s->radio;
3243
3244 if (!radio)
3245 return;
3246
3247 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3248 wpa_s->ifname, radio->name);
3249 dl_list_del(&wpa_s->radio_list);
c46235aa
AO
3250 radio_remove_works(wpa_s, NULL, 0);
3251 wpa_s->radio = NULL;
3252 if (!dl_list_empty(&radio->ifaces))
202dec2a
JM
3253 return; /* Interfaces remain for this radio */
3254
3255 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
b1ae396f 3256 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
202dec2a
JM
3257 os_free(radio);
3258}
3259
3260
6428d0a7 3261void radio_work_check_next(struct wpa_supplicant *wpa_s)
b1ae396f
JM
3262{
3263 struct wpa_radio *radio = wpa_s->radio;
3264
3265 if (dl_list_empty(&radio->work))
3266 return;
3267 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3268 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3269}
3270
3271
3272/**
3273 * radio_add_work - Add a radio work item
3274 * @wpa_s: Pointer to wpa_supplicant data
3275 * @freq: Frequency of the offchannel operation in MHz or 0
3276 * @type: Unique identifier for each type of work
3277 * @next: Force as the next work to be executed
3278 * @cb: Callback function for indicating when radio is available
3279 * @ctx: Context pointer for the work (work->ctx in cb())
3280 * Returns: 0 on success, -1 on failure
3281 *
3282 * This function is used to request time for an operation that requires
3283 * exclusive radio control. Once the radio is available, the registered callback
3284 * function will be called. radio_work_done() must be called once the exclusive
3285 * radio operation has been completed, so that the radio is freed for other
3286 * operations. The special case of deinit=1 is used to free the context data
3287 * during interface removal. That does not allow the callback function to start
3288 * the radio operation, i.e., it must free any resources allocated for the radio
3289 * work and return.
3290 *
3291 * The @freq parameter can be used to indicate a single channel on which the
3292 * offchannel operation will occur. This may allow multiple radio work
3293 * operations to be performed in parallel if they apply for the same channel.
3294 * Setting this to 0 indicates that the work item may use multiple channels or
3295 * requires exclusive control of the radio.
3296 */
3297int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3298 const char *type, int next,
3299 void (*cb)(struct wpa_radio_work *work, int deinit),
3300 void *ctx)
3301{
3302 struct wpa_radio_work *work;
3303 int was_empty;
3304
3305 work = os_zalloc(sizeof(*work));
3306 if (work == NULL)
3307 return -1;
3308 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3309 os_get_reltime(&work->time);
3310 work->freq = freq;
3311 work->type = type;
3312 work->wpa_s = wpa_s;
3313 work->cb = cb;
3314 work->ctx = ctx;
3315
3316 was_empty = dl_list_empty(&wpa_s->radio->work);
3317 if (next)
3318 dl_list_add(&wpa_s->radio->work, &work->list);
3319 else
3320 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3321 if (was_empty) {
3322 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3323 radio_work_check_next(wpa_s);
3324 }
3325
3326 return 0;
3327}
3328
3329
3330/**
3331 * radio_work_done - Indicate that a radio work item has been completed
3332 * @work: Completed work
3333 *
3334 * This function is called once the callback function registered with
3335 * radio_add_work() has completed its work.
3336 */
3337void radio_work_done(struct wpa_radio_work *work)
3338{
3339 struct wpa_supplicant *wpa_s = work->wpa_s;
3340 struct os_reltime now, diff;
1f965e62 3341 unsigned int started = work->started;
b1ae396f
JM
3342
3343 os_get_reltime(&now);
3344 os_reltime_sub(&now, &work->time, &diff);
1f965e62
JM
3345 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3346 work->type, work, started ? "done" : "canceled",
3347 diff.sec, diff.usec);
b1ae396f 3348 radio_work_free(work);
1f965e62
JM
3349 if (started)
3350 radio_work_check_next(wpa_s);
b1ae396f
JM
3351}
3352
3353
f0e30c84
JM
3354int radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3355{
3356 struct wpa_radio_work *work;
3357 struct wpa_radio *radio = wpa_s->radio;
3358
3359 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3360 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3361 return 1;
3362 }
3363
3364 return 0;
3365}
3366
3367
73c00fd7
JM
3368static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3369 struct wpa_interface *iface)
3370{
202dec2a 3371 const char *ifname, *driver, *rn;
73c00fd7
JM
3372
3373 driver = iface->driver;
3374next_driver:
3375 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3376 return -1;
3377
3378 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3379 if (wpa_s->drv_priv == NULL) {
3380 const char *pos;
3381 pos = driver ? os_strchr(driver, ',') : NULL;
3382 if (pos) {
3383 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3384 "driver interface - try next driver wrapper");
3385 driver = pos + 1;
3386 goto next_driver;
3387 }
3388 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3389 "interface");
3390 return -1;
3391 }
3392 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3393 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3394 "driver_param '%s'", wpa_s->conf->driver_param);
3395 return -1;
3396 }
3397
3398 ifname = wpa_drv_get_ifname(wpa_s);
3399 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3400 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3401 "interface name with '%s'", ifname);
3402 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3403 }
3404
95bf699f 3405 rn = wpa_driver_get_radio_name(wpa_s);
202dec2a
JM
3406 if (rn && rn[0] == '\0')
3407 rn = NULL;
3408
3409 wpa_s->radio = radio_add_interface(wpa_s, rn);
3410 if (wpa_s->radio == NULL)
3411 return -1;
3412
73c00fd7
JM
3413 return 0;
3414}
3415
3416
6fc6879b
JM
3417static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3418 struct wpa_interface *iface)
3419{
362f781e
JM
3420 struct wpa_driver_capa capa;
3421
6fc6879b
JM
3422 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3423 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3424 iface->confname ? iface->confname : "N/A",
3425 iface->driver ? iface->driver : "default",
3426 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3427 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3428
6fc6879b
JM
3429 if (iface->confname) {
3430#ifdef CONFIG_BACKEND_FILE
3431 wpa_s->confname = os_rel2abs_path(iface->confname);
3432 if (wpa_s->confname == NULL) {
3433 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3434 "for configuration file '%s'.",
3435 iface->confname);
3436 return -1;
3437 }
3438 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3439 iface->confname, wpa_s->confname);
3440#else /* CONFIG_BACKEND_FILE */
3441 wpa_s->confname = os_strdup(iface->confname);
3442#endif /* CONFIG_BACKEND_FILE */
e6304cad 3443 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
6fc6879b
JM
3444 if (wpa_s->conf == NULL) {
3445 wpa_printf(MSG_ERROR, "Failed to read or parse "
3446 "configuration '%s'.", wpa_s->confname);
3447 return -1;
3448 }
e6304cad
DS
3449 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3450 wpa_config_read(wpa_s->confanother, wpa_s->conf);
6fc6879b 3451
c16a7590
IP
3452#ifdef CONFIG_P2P
3453 wpa_s->conf_p2p_dev = os_rel2abs_path(iface->conf_p2p_dev);
3454 wpa_config_read(wpa_s->conf_p2p_dev, wpa_s->conf);
3455#endif /* CONFIG_P2P */
3456
6fc6879b
JM
3457 /*
3458 * Override ctrl_interface and driver_param if set on command
3459 * line.
3460 */
3461 if (iface->ctrl_interface) {
3462 os_free(wpa_s->conf->ctrl_interface);
3463 wpa_s->conf->ctrl_interface =
3464 os_strdup(iface->ctrl_interface);
3465 }
3466
3467 if (iface->driver_param) {
3468 os_free(wpa_s->conf->driver_param);
3469 wpa_s->conf->driver_param =
3470 os_strdup(iface->driver_param);
3471 }
78f79fe5
JM
3472
3473 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3474 os_free(wpa_s->conf->ctrl_interface);
3475 wpa_s->conf->ctrl_interface = NULL;
3476 }
6fc6879b
JM
3477 } else
3478 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3479 iface->driver_param);
3480
3481 if (wpa_s->conf == NULL) {
3482 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3483 return -1;
3484 }
3485
3486 if (iface->ifname == NULL) {
3487 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3488 return -1;
3489 }
3490 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3491 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3492 iface->ifname);
3493 return -1;
3494 }
3495 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3496
3497 if (iface->bridge_ifname) {
3498 if (os_strlen(iface->bridge_ifname) >=
3499 sizeof(wpa_s->bridge_ifname)) {
3500 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3501 "name '%s'.", iface->bridge_ifname);
3502 return -1;
3503 }
3504 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3505 sizeof(wpa_s->bridge_ifname));
3506 }
3507
6fc6879b
JM
3508 /* RSNA Supplicant Key Management - INITIALIZE */
3509 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3510 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3511
3512 /* Initialize driver interface and register driver event handler before
3513 * L2 receive handler so that association events are processed before
3514 * EAPOL-Key packets if both become available for the same select()
3515 * call. */
73c00fd7 3516 if (wpas_init_driver(wpa_s, iface) < 0)
362f781e
JM
3517 return -1;
3518
6fc6879b
JM
3519 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3520 return -1;
3521
3522 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3523 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3524 NULL);
3525 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3526
3527 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3528 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3529 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
f049052b
BG
3530 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3531 "dot11RSNAConfigPMKLifetime");
6fc6879b
JM
3532 return -1;
3533 }
3534
3535 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3536 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3537 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
f049052b 3538 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
6fc6879b
JM
3539 "dot11RSNAConfigPMKReauthThreshold");
3540 return -1;
3541 }
3542
3543 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3544 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3545 wpa_s->conf->dot11RSNAConfigSATimeout)) {
f049052b
BG
3546 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3547 "dot11RSNAConfigSATimeout");
6fc6879b
JM
3548 return -1;
3549 }
3550
6bf731e8
CL
3551 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3552 &wpa_s->hw.num_modes,
3553 &wpa_s->hw.flags);
3554
814782b9 3555 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
c58ab8f2 3556 wpa_s->drv_capa_known = 1;
814782b9 3557 wpa_s->drv_flags = capa.flags;
349493bd 3558 wpa_s->drv_enc = capa.enc;
4f73d88a 3559 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
814782b9 3560 wpa_s->max_scan_ssids = capa.max_scan_ssids;
cbdf3507
LC
3561 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3562 wpa_s->sched_scan_supported = capa.sched_scan_supported;
b59e6f26 3563 wpa_s->max_match_sets = capa.max_match_sets;
814782b9 3564 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
c4ea4c5c 3565 wpa_s->max_stations = capa.max_stations;
8cd6b7bc
JB
3566 wpa_s->extended_capa = capa.extended_capa;
3567 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3568 wpa_s->extended_capa_len = capa.extended_capa_len;
4752147d
IP
3569 wpa_s->num_multichan_concurrent =
3570 capa.num_multichan_concurrent;
814782b9
JM
3571 }
3572 if (wpa_s->max_remain_on_chan == 0)
3573 wpa_s->max_remain_on_chan = 1000;
3574
c68f6200
AS
3575 /*
3576 * Only take p2p_mgmt parameters when P2P Device is supported.
3577 * Doing it here as it determines whether l2_packet_init() will be done
3578 * during wpa_supplicant_driver_init().
3579 */
3580 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3581 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3582 else
3583 iface->p2p_mgmt = 1;
3584
4752147d
IP
3585 if (wpa_s->num_multichan_concurrent == 0)
3586 wpa_s->num_multichan_concurrent = 1;
3587
6fc6879b
JM
3588 if (wpa_supplicant_driver_init(wpa_s) < 0)
3589 return -1;
3590
281ff0aa 3591#ifdef CONFIG_TDLS
1c42b42f
JM
3592 if ((!iface->p2p_mgmt ||
3593 !(wpa_s->drv_flags &
3594 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3595 wpa_tdls_init(wpa_s->wpa))
281ff0aa
GP
3596 return -1;
3597#endif /* CONFIG_TDLS */
3598
315ce40a
JM
3599 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3600 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
f049052b 3601 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
6d158490
LR
3602 return -1;
3603 }
3604
116654ce
JM
3605 if (wpas_wps_init(wpa_s))
3606 return -1;
3607
6fc6879b
JM
3608 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3609 return -1;
3610 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3611
3612 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3613 if (wpa_s->ctrl_iface == NULL) {
3614 wpa_printf(MSG_ERROR,
3615 "Failed to initialize control interface '%s'.\n"
3616 "You may have another wpa_supplicant process "
3617 "already running or the file was\n"
3618 "left by an unclean termination of wpa_supplicant "
3619 "in which case you will need\n"
3620 "to manually remove this file before starting "
3621 "wpa_supplicant again.\n",
3622 wpa_s->conf->ctrl_interface);
3623 return -1;
3624 }
3625
04ea7b79
JM
3626 wpa_s->gas = gas_query_init(wpa_s);
3627 if (wpa_s->gas == NULL) {
3628 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3629 return -1;
3630 }
3631
b22128ef 3632#ifdef CONFIG_P2P
c68f6200 3633 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
f049052b 3634 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
b22128ef
JM
3635 return -1;
3636 }
3637#endif /* CONFIG_P2P */
3638
83922c2d
JM
3639 if (wpa_bss_init(wpa_s) < 0)
3640 return -1;
83922c2d 3641
ec7b97ab
JM
3642#ifdef CONFIG_EAP_PROXY
3643{
3644 size_t len;
07041c6f
NJ
3645 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3646 &len);
ec7b97ab
JM
3647 if (wpa_s->mnc_len > 0) {
3648 wpa_s->imsi[len] = '\0';
3649 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3650 wpa_s->imsi, wpa_s->mnc_len);
3651 } else {
3652 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3653 }
3654}
3655#endif /* CONFIG_EAP_PROXY */
3656
f64adcd7
JM
3657 if (pcsc_reader_init(wpa_s) < 0)
3658 return -1;
3659
306ae225
JM
3660 if (wpas_init_ext_pw(wpa_s) < 0)
3661 return -1;
3662
6fc6879b
JM
3663 return 0;
3664}
3665
3666
2ee055b3 3667static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
df509539 3668 int notify, int terminate)
6fc6879b 3669{
e679f140 3670 wpa_s->disconnected = 1;
6fc6879b
JM
3671 if (wpa_s->drv_priv) {
3672 wpa_supplicant_deauthenticate(wpa_s,
3673 WLAN_REASON_DEAUTH_LEAVING);
3674
6fc6879b
JM
3675 wpa_drv_set_countermeasures(wpa_s, 0);
3676 wpa_clear_keys(wpa_s, NULL);
3677 }
3678
8e56d189
JM
3679 wpa_supplicant_cleanup(wpa_s);
3680
ab28911d 3681#ifdef CONFIG_P2P
e83e15ee
JM
3682 if (wpa_s == wpa_s->parent)
3683 wpas_p2p_group_remove(wpa_s, "*");
ab28911d
JM
3684 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
3685 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
3686 "the management interface is being removed");
3687 wpas_p2p_deinit_global(wpa_s->global);
3688 }
3689#endif /* CONFIG_P2P */
3690
1f965e62 3691 wpas_ctrl_radio_work_flush(wpa_s);
202dec2a
JM
3692 radio_remove_interface(wpa_s);
3693
6fc6879b
JM
3694 if (wpa_s->drv_priv)
3695 wpa_drv_deinit(wpa_s);
2523ff6e
DS
3696
3697 if (notify)
3698 wpas_notify_iface_removed(wpa_s);
f0811516
DS
3699
3700 if (terminate)
3701 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
3702
3703 if (wpa_s->ctrl_iface) {
3704 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
3705 wpa_s->ctrl_iface = NULL;
3706 }
3707
3708 if (wpa_s->conf != NULL) {
3709 wpa_config_free(wpa_s->conf);
3710 wpa_s->conf = NULL;
3711 }
18e00b5e
JM
3712
3713 os_free(wpa_s);
6fc6879b
JM
3714}
3715
3716
3717/**
3718 * wpa_supplicant_add_iface - Add a new network interface
3719 * @global: Pointer to global data from wpa_supplicant_init()
3720 * @iface: Interface configuration options
3721 * Returns: Pointer to the created interface or %NULL on failure
3722 *
3723 * This function is used to add new network interfaces for %wpa_supplicant.
3724 * This can be called before wpa_supplicant_run() to add interfaces before the
3725 * main event loop has been started. In addition, new interfaces can be added
3726 * dynamically while %wpa_supplicant is already running. This could happen,
3727 * e.g., when a hotplug network adapter is inserted.
3728 */
3729struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
3730 struct wpa_interface *iface)
3731{
3732 struct wpa_supplicant *wpa_s;
d27df100 3733 struct wpa_interface t_iface;
8e56d189 3734 struct wpa_ssid *ssid;
6fc6879b
JM
3735
3736 if (global == NULL || iface == NULL)
3737 return NULL;
3738
3739 wpa_s = wpa_supplicant_alloc();
3740 if (wpa_s == NULL)
3741 return NULL;
3742
d8222ae3
JM
3743 wpa_s->global = global;
3744
d27df100
JM
3745 t_iface = *iface;
3746 if (global->params.override_driver) {
3747 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
3748 "('%s' -> '%s')",
3749 iface->driver, global->params.override_driver);
3750 t_iface.driver = global->params.override_driver;
3751 }
3752 if (global->params.override_ctrl_interface) {
3753 wpa_printf(MSG_DEBUG, "Override interface parameter: "
3754 "ctrl_interface ('%s' -> '%s')",
3755 iface->ctrl_interface,
3756 global->params.override_ctrl_interface);
3757 t_iface.ctrl_interface =
3758 global->params.override_ctrl_interface;
3759 }
3760 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
6fc6879b
JM
3761 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
3762 iface->ifname);
df509539 3763 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
6fc6879b
JM
3764 return NULL;
3765 }
3766
dc461de4
WS
3767 /* Notify the control interfaces about new iface */
3768 if (wpas_notify_iface_added(wpa_s)) {
df509539 3769 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
6fc6879b
JM
3770 return NULL;
3771 }
1bd3f426 3772
8e56d189
JM
3773 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3774 wpas_notify_network_added(wpa_s, ssid);
3775
6fc6879b
JM
3776 wpa_s->next = global->ifaces;
3777 global->ifaces = wpa_s;
3778
f049052b 3779 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
99218999 3780 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b
JM
3781
3782 return wpa_s;
3783}
3784
3785
3786/**
3787 * wpa_supplicant_remove_iface - Remove a network interface
3788 * @global: Pointer to global data from wpa_supplicant_init()
3789 * @wpa_s: Pointer to the network interface to be removed
3790 * Returns: 0 if interface was removed, -1 if interface was not found
3791 *
3792 * This function can be used to dynamically remove network interfaces from
3793 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
3794 * addition, this function is used to remove all remaining interfaces when
3795 * %wpa_supplicant is terminated.
3796 */
3797int wpa_supplicant_remove_iface(struct wpa_global *global,
df509539
DS
3798 struct wpa_supplicant *wpa_s,
3799 int terminate)
6fc6879b
JM
3800{
3801 struct wpa_supplicant *prev;
3802
3803 /* Remove interface from the global list of interfaces */
3804 prev = global->ifaces;
3805 if (prev == wpa_s) {
3806 global->ifaces = wpa_s->next;
3807 } else {
3808 while (prev && prev->next != wpa_s)
3809 prev = prev->next;
3810 if (prev == NULL)
3811 return -1;
3812 prev->next = wpa_s->next;
3813 }
3814
f049052b 3815 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
6fc6879b 3816
b22128ef
JM
3817 if (global->p2p_group_formation == wpa_s)
3818 global->p2p_group_formation = NULL;
dbca75f8
JM
3819 if (global->p2p_invite_group == wpa_s)
3820 global->p2p_invite_group = NULL;
df509539 3821 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
6fc6879b
JM
3822
3823 return 0;
3824}
3825
3826
cf83fb0b
PS
3827/**
3828 * wpa_supplicant_get_eap_mode - Get the current EAP mode
3829 * @wpa_s: Pointer to the network interface
3830 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
3831 */
3832const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
3833{
3834 const char *eapol_method;
3835
3836 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
3837 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3838 return "NO-EAP";
3839 }
3840
3841 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
3842 if (eapol_method == NULL)
3843 return "UNKNOWN-EAP";
3844
3845 return eapol_method;
3846}
3847
3848
6fc6879b
JM
3849/**
3850 * wpa_supplicant_get_iface - Get a new network interface
3851 * @global: Pointer to global data from wpa_supplicant_init()
3852 * @ifname: Interface name
3853 * Returns: Pointer to the interface or %NULL if not found
3854 */
3855struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
3856 const char *ifname)
3857{
3858 struct wpa_supplicant *wpa_s;
3859
3860 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3861 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3862 return wpa_s;
3863 }
3864 return NULL;
3865}
3866
3867
50b16da1 3868#ifndef CONFIG_NO_WPA_MSG
4f1495ae
BG
3869static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
3870{
3871 struct wpa_supplicant *wpa_s = ctx;
3872 if (wpa_s == NULL)
3873 return NULL;
3874 return wpa_s->ifname;
3875}
50b16da1 3876#endif /* CONFIG_NO_WPA_MSG */
4f1495ae
BG
3877
3878
6fc6879b
JM
3879/**
3880 * wpa_supplicant_init - Initialize %wpa_supplicant
3881 * @params: Parameters for %wpa_supplicant
3882 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
3883 *
3884 * This function is used to initialize %wpa_supplicant. After successful
3885 * initialization, the returned data pointer can be used to add and remove
3886 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
3887 */
3888struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
3889{
3890 struct wpa_global *global;
ac305589 3891 int ret, i;
6fc6879b
JM
3892
3893 if (params == NULL)
3894 return NULL;
3895
39e7d718
JM
3896#ifdef CONFIG_DRIVER_NDIS
3897 {
3898 void driver_ndis_init_ops(void);
3899 driver_ndis_init_ops();
3900 }
3901#endif /* CONFIG_DRIVER_NDIS */
3902
50b16da1 3903#ifndef CONFIG_NO_WPA_MSG
4f1495ae 3904 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
50b16da1 3905#endif /* CONFIG_NO_WPA_MSG */
4f1495ae 3906
6fc6879b 3907 wpa_debug_open_file(params->wpa_debug_file_path);
daa70d49
SL
3908 if (params->wpa_debug_syslog)
3909 wpa_debug_open_syslog();
4f68895e
JB
3910 if (params->wpa_debug_tracing) {
3911 ret = wpa_debug_open_linux_tracing();
3912 if (ret) {
3913 wpa_printf(MSG_ERROR,
3914 "Failed to enable trace logging");
3915 return NULL;
3916 }
3917 }
6fc6879b 3918
12760815 3919 ret = eap_register_methods();
6fc6879b
JM
3920 if (ret) {
3921 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
3922 if (ret == -2)
3923 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
3924 "the same EAP type.");
3925 return NULL;
3926 }
3927
3928 global = os_zalloc(sizeof(*global));
3929 if (global == NULL)
3930 return NULL;
b22128ef
JM
3931 dl_list_init(&global->p2p_srv_bonjour);
3932 dl_list_init(&global->p2p_srv_upnp);
6fc6879b
JM
3933 global->params.daemonize = params->daemonize;
3934 global->params.wait_for_monitor = params->wait_for_monitor;
3935 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
3936 if (params->pid_file)
3937 global->params.pid_file = os_strdup(params->pid_file);
3938 if (params->ctrl_interface)
3939 global->params.ctrl_interface =
3940 os_strdup(params->ctrl_interface);
29257565
JM
3941 if (params->ctrl_interface_group)
3942 global->params.ctrl_interface_group =
3943 os_strdup(params->ctrl_interface_group);
d27df100
JM
3944 if (params->override_driver)
3945 global->params.override_driver =
3946 os_strdup(params->override_driver);
3947 if (params->override_ctrl_interface)
3948 global->params.override_ctrl_interface =
3949 os_strdup(params->override_ctrl_interface);
6fc6879b
JM
3950 wpa_debug_level = global->params.wpa_debug_level =
3951 params->wpa_debug_level;
3952 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
3953 params->wpa_debug_show_keys;
3954 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
3955 params->wpa_debug_timestamp;
3956
f19858f5
JM
3957 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
3958
0456ea16 3959 if (eloop_init()) {
6fc6879b
JM
3960 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
3961 wpa_supplicant_deinit(global);
3962 return NULL;
3963 }
3964
38e24575 3965 random_init(params->entropy_file);
d47fa330 3966
6fc6879b
JM
3967 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
3968 if (global->ctrl_iface == NULL) {
3969 wpa_supplicant_deinit(global);
3970 return NULL;
3971 }
3972
dc461de4
WS
3973 if (wpas_notify_supplicant_initialized(global)) {
3974 wpa_supplicant_deinit(global);
3975 return NULL;
6fc6879b
JM
3976 }
3977
c5121837 3978 for (i = 0; wpa_drivers[i]; i++)
ac305589
JM
3979 global->drv_count++;
3980 if (global->drv_count == 0) {
3981 wpa_printf(MSG_ERROR, "No drivers enabled");
3982 wpa_supplicant_deinit(global);
3983 return NULL;
3984 }
3985 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
3986 if (global->drv_priv == NULL) {
3987 wpa_supplicant_deinit(global);
3988 return NULL;
3989 }
ac305589 3990
9675ce35
JM
3991#ifdef CONFIG_WIFI_DISPLAY
3992 if (wifi_display_init(global) < 0) {
3993 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
3994 wpa_supplicant_deinit(global);
3995 return NULL;
3996 }
3997#endif /* CONFIG_WIFI_DISPLAY */
3998
6fc6879b
JM
3999 return global;
4000}
4001
4002
4003/**
4004 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
4005 * @global: Pointer to global data from wpa_supplicant_init()
4006 * Returns: 0 after successful event loop run, -1 on failure
4007 *
4008 * This function starts the main event loop and continues running as long as
4009 * there are any remaining events. In most cases, this function is running as
4010 * long as the %wpa_supplicant process in still in use.
4011 */
4012int wpa_supplicant_run(struct wpa_global *global)
4013{
4014 struct wpa_supplicant *wpa_s;
4015
4016 if (global->params.daemonize &&
4017 wpa_supplicant_daemon(global->params.pid_file))
4018 return -1;
4019
4020 if (global->params.wait_for_monitor) {
4021 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
4022 if (wpa_s->ctrl_iface)
4023 wpa_supplicant_ctrl_iface_wait(
4024 wpa_s->ctrl_iface);
4025 }
4026
0456ea16
JM
4027 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
4028 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
6fc6879b
JM
4029
4030 eloop_run();
4031
4032 return 0;
4033}
4034
4035
4036/**
4037 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
4038 * @global: Pointer to global data from wpa_supplicant_init()
4039 *
4040 * This function is called to deinitialize %wpa_supplicant and to free all
4041 * allocated resources. Remaining network interfaces will also be removed.
4042 */
4043void wpa_supplicant_deinit(struct wpa_global *global)
4044{
ac305589
JM
4045 int i;
4046
6fc6879b
JM
4047 if (global == NULL)
4048 return;
4049
9675ce35
JM
4050#ifdef CONFIG_WIFI_DISPLAY
4051 wifi_display_deinit(global);
4052#endif /* CONFIG_WIFI_DISPLAY */
b22128ef 4053
6fc6879b 4054 while (global->ifaces)
df509539 4055 wpa_supplicant_remove_iface(global, global->ifaces, 1);
6fc6879b
JM
4056
4057 if (global->ctrl_iface)
4058 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
dc461de4
WS
4059
4060 wpas_notify_supplicant_deinitialized(global);
6fc6879b
JM
4061
4062 eap_peer_unregister_methods();
3ec97afe
JM
4063#ifdef CONFIG_AP
4064 eap_server_unregister_methods();
4065#endif /* CONFIG_AP */
6fc6879b 4066
c5121837 4067 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
ac305589
JM
4068 if (!global->drv_priv[i])
4069 continue;
c5121837 4070 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
ac305589
JM
4071 }
4072 os_free(global->drv_priv);
4073
d47fa330
JM
4074 random_deinit();
4075
6fc6879b
JM
4076 eloop_destroy();
4077
4078 if (global->params.pid_file) {
4079 os_daemonize_terminate(global->params.pid_file);
4080 os_free(global->params.pid_file);
4081 }
4082 os_free(global->params.ctrl_interface);
29257565 4083 os_free(global->params.ctrl_interface_group);
d27df100
JM
4084 os_free(global->params.override_driver);
4085 os_free(global->params.override_ctrl_interface);
6fc6879b 4086
af8a827b 4087 os_free(global->p2p_disallow_freq.range);
253f2e37 4088 os_free(global->p2p_go_avoid_freq.range);
01a57fe4 4089 os_free(global->add_psk);
6f3bc72b 4090
6fc6879b 4091 os_free(global);
daa70d49 4092 wpa_debug_close_syslog();
6fc6879b 4093 wpa_debug_close_file();
4f68895e 4094 wpa_debug_close_linux_tracing();
6fc6879b 4095}
611aea7d
JM
4096
4097
4098void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4099{
849b5dc7
JM
4100 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4101 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4102 char country[3];
4103 country[0] = wpa_s->conf->country[0];
4104 country[1] = wpa_s->conf->country[1];
4105 country[2] = '\0';
4106 if (wpa_drv_set_country(wpa_s, country) < 0) {
4107 wpa_printf(MSG_ERROR, "Failed to set country code "
4108 "'%s'", country);
4109 }
4110 }
4111
306ae225
JM
4112 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4113 wpas_init_ext_pw(wpa_s);
4114
611aea7d
JM
4115#ifdef CONFIG_WPS
4116 wpas_wps_update_config(wpa_s);
4117#endif /* CONFIG_WPS */
4118
b22128ef
JM
4119#ifdef CONFIG_P2P
4120 wpas_p2p_update_config(wpa_s);
4121#endif /* CONFIG_P2P */
4122
611aea7d
JM
4123 wpa_s->conf->changed_parameters = 0;
4124}
2f9c6aa6
JM
4125
4126
0fb337c1
JM
4127static void add_freq(int *freqs, int *num_freqs, int freq)
4128{
4129 int i;
4130
4131 for (i = 0; i < *num_freqs; i++) {
4132 if (freqs[i] == freq)
4133 return;
4134 }
4135
4136 freqs[*num_freqs] = freq;
4137 (*num_freqs)++;
4138}
4139
4140
4141static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4142{
4143 struct wpa_bss *bss, *cbss;
4144 const int max_freqs = 10;
4145 int *freqs;
4146 int num_freqs = 0;
4147
4148 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
4149 if (freqs == NULL)
4150 return NULL;
4151
4152 cbss = wpa_s->current_bss;
4153
4154 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4155 if (bss == cbss)
4156 continue;
4157 if (bss->ssid_len == cbss->ssid_len &&
4158 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4159 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4160 add_freq(freqs, &num_freqs, bss->freq);
4161 if (num_freqs == max_freqs)
4162 break;
4163 }
4164 }
4165
4166 if (num_freqs == 0) {
4167 os_free(freqs);
4168 freqs = NULL;
4169 }
4170
4171 return freqs;
4172}
4173
4174
4175void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4176{
4177 int timeout;
4178 int count;
4179 int *freqs = NULL;
4180
6ac4b15e
JM
4181 wpas_connect_work_done(wpa_s);
4182
5fd9fb27
JM
4183 /*
4184 * Remove possible authentication timeout since the connection failed.
4185 */
4186 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4187
0cdb93fe
JM
4188 if (wpa_s->disconnected) {
4189 /*
4190 * There is no point in blacklisting the AP if this event is
4191 * generated based on local request to disconnect.
4192 */
4193 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4194 "indication since interface has been put into "
4195 "disconnected state");
4196 return;
4197 }
4198
0fb337c1
JM
4199 /*
4200 * Add the failed BSSID into the blacklist and speed up next scan
4201 * attempt if there could be other APs that could accept association.
4202 * The current blacklist count indicates how many times we have tried
4203 * connecting to this AP and multiple attempts mean that other APs are
4204 * either not available or has already been tried, so that we can start
4205 * increasing the delay here to avoid constant scanning.
4206 */
4207 count = wpa_blacklist_add(wpa_s, bssid);
4208 if (count == 1 && wpa_s->current_bss) {
4209 /*
4210 * This BSS was not in the blacklist before. If there is
4211 * another BSS available for the same ESS, we should try that
4212 * next. Otherwise, we may as well try this one once more
4213 * before allowing other, likely worse, ESSes to be considered.
4214 */
4215 freqs = get_bss_freqs_in_ess(wpa_s);
4216 if (freqs) {
f049052b
BG
4217 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4218 "has been seen; try it next");
0fb337c1
JM
4219 wpa_blacklist_add(wpa_s, bssid);
4220 /*
4221 * On the next scan, go through only the known channels
4222 * used in this ESS based on previous scans to speed up
4223 * common load balancing use case.
4224 */
4225 os_free(wpa_s->next_scan_freqs);
4226 wpa_s->next_scan_freqs = freqs;
4227 }
4228 }
4229
f1a52633
JM
4230 /*
4231 * Add previous failure count in case the temporary blacklist was
4232 * cleared due to no other BSSes being available.
4233 */
4234 count += wpa_s->extra_blacklist_count;
4235
dd579704
JM
4236 if (count > 3 && wpa_s->current_ssid) {
4237 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4238 "consider temporary network disabling");
4239 wpas_auth_failed(wpa_s);
4240 }
4241
0fb337c1
JM
4242 switch (count) {
4243 case 1:
4244 timeout = 100;
4245 break;
4246 case 2:
4247 timeout = 500;
4248 break;
4249 case 3:
4250 timeout = 1000;
4251 break;
f1a52633 4252 case 4:
0fb337c1 4253 timeout = 5000;
f1a52633
JM
4254 break;
4255 default:
4256 timeout = 10000;
4257 break;
0fb337c1
JM
4258 }
4259
f1a52633
JM
4260 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4261 "ms", count, timeout);
4262
0fb337c1
JM
4263 /*
4264 * TODO: if more than one possible AP is available in scan results,
4265 * could try the other ones before requesting a new scan.
4266 */
4267 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4268 1000 * (timeout % 1000));
4269}
22628eca
JM
4270
4271
4272int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4273{
4274 return wpa_s->conf->ap_scan == 2 ||
4275 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4276}
d2118814
JM
4277
4278
4279#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4280int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4281 struct wpa_ssid *ssid,
4282 const char *field,
4283 const char *value)
4284{
4285#ifdef IEEE8021X_EAPOL
4286 struct eap_peer_config *eap = &ssid->eap;
4287
4288 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4289 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4290 (const u8 *) value, os_strlen(value));
4291
4292 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4293 case WPA_CTRL_REQ_EAP_IDENTITY:
4294 os_free(eap->identity);
4295 eap->identity = (u8 *) os_strdup(value);
4296 eap->identity_len = os_strlen(value);
4297 eap->pending_req_identity = 0;
4298 if (ssid == wpa_s->current_ssid)
4299 wpa_s->reassociate = 1;
4300 break;
4301 case WPA_CTRL_REQ_EAP_PASSWORD:
4302 os_free(eap->password);
4303 eap->password = (u8 *) os_strdup(value);
4304 eap->password_len = os_strlen(value);
4305 eap->pending_req_password = 0;
4306 if (ssid == wpa_s->current_ssid)
4307 wpa_s->reassociate = 1;
4308 break;
4309 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
4310 os_free(eap->new_password);
4311 eap->new_password = (u8 *) os_strdup(value);
4312 eap->new_password_len = os_strlen(value);
4313 eap->pending_req_new_password = 0;
4314 if (ssid == wpa_s->current_ssid)
4315 wpa_s->reassociate = 1;
4316 break;
4317 case WPA_CTRL_REQ_EAP_PIN:
4318 os_free(eap->pin);
4319 eap->pin = os_strdup(value);
4320 eap->pending_req_pin = 0;
4321 if (ssid == wpa_s->current_ssid)
4322 wpa_s->reassociate = 1;
4323 break;
4324 case WPA_CTRL_REQ_EAP_OTP:
4325 os_free(eap->otp);
4326 eap->otp = (u8 *) os_strdup(value);
4327 eap->otp_len = os_strlen(value);
4328 os_free(eap->pending_req_otp);
4329 eap->pending_req_otp = NULL;
4330 eap->pending_req_otp_len = 0;
4331 break;
4332 case WPA_CTRL_REQ_EAP_PASSPHRASE:
4333 os_free(eap->private_key_passwd);
4334 eap->private_key_passwd = (u8 *) os_strdup(value);
4335 eap->pending_req_passphrase = 0;
4336 if (ssid == wpa_s->current_ssid)
4337 wpa_s->reassociate = 1;
4338 break;
a5d44ac0
JM
4339 case WPA_CTRL_REQ_SIM:
4340 os_free(eap->external_sim_resp);
4341 eap->external_sim_resp = os_strdup(value);
4342 break;
d2118814
JM
4343 default:
4344 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4345 return -1;
4346 }
4347
4348 return 0;
4349#else /* IEEE8021X_EAPOL */
4350 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4351 return -1;
4352#endif /* IEEE8021X_EAPOL */
4353}
4354#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
349493bd
JM
4355
4356
4357int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4358{
4359 int i;
4360 unsigned int drv_enc;
4361
4362 if (ssid == NULL)
4363 return 1;
4364
4365 if (ssid->disabled)
4366 return 1;
4367
4368 if (wpa_s && wpa_s->drv_capa_known)
4369 drv_enc = wpa_s->drv_enc;
4370 else
4371 drv_enc = (unsigned int) -1;
4372
4373 for (i = 0; i < NUM_WEP_KEYS; i++) {
4374 size_t len = ssid->wep_key_len[i];
4375 if (len == 0)
4376 continue;
4377 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4378 continue;
4379 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4380 continue;
4381 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4382 continue;
4383 return 1; /* invalid WEP key */
4384 }
4385
9173b16f 4386 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
759ff2f0 4387 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
2518aad3
JM
4388 return 1;
4389
349493bd
JM
4390 return 0;
4391}
b9cfc09a
JJ
4392
4393
4394int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
4395{
4396 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
4397 return 1;
4398 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
4399 return 0;
4400 return -1;
4401}
00e5e3d5
JM
4402
4403
4404void wpas_auth_failed(struct wpa_supplicant *wpa_s)
4405{
4406 struct wpa_ssid *ssid = wpa_s->current_ssid;
4407 int dur;
4e1eae1d 4408 struct os_reltime now;
00e5e3d5
JM
4409
4410 if (ssid == NULL) {
4411 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4412 "SSID block");
4413 return;
4414 }
4415
4416 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4417 return;
4418
4419 ssid->auth_failures++;
cbf41ca7
SL
4420
4421#ifdef CONFIG_P2P
4422 if (ssid->p2p_group &&
4423 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4424 /*
4425 * Skip the wait time since there is a short timeout on the
4426 * connection to a P2P group.
4427 */
4428 return;
4429 }
4430#endif /* CONFIG_P2P */
4431
00e5e3d5
JM
4432 if (ssid->auth_failures > 50)
4433 dur = 300;
00e5e3d5 4434 else if (ssid->auth_failures > 10)
8a77f1be 4435 dur = 120;
00e5e3d5 4436 else if (ssid->auth_failures > 5)
8a77f1be
JM
4437 dur = 90;
4438 else if (ssid->auth_failures > 3)
4439 dur = 60;
4440 else if (ssid->auth_failures > 2)
00e5e3d5
JM
4441 dur = 30;
4442 else if (ssid->auth_failures > 1)
4443 dur = 20;
4444 else
4445 dur = 10;
4446
8a77f1be
JM
4447 if (ssid->auth_failures > 1 &&
4448 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4449 dur += os_random() % (ssid->auth_failures * 10);
4450
4e1eae1d 4451 os_get_reltime(&now);
00e5e3d5
JM
4452 if (now.sec + dur <= ssid->disabled_until.sec)
4453 return;
4454
4455 ssid->disabled_until.sec = now.sec + dur;
4456
4457 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
4458 "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
4459 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
4460 ssid->auth_failures, dur);
4461}
4462
4463
4464void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4465 struct wpa_ssid *ssid, int clear_failures)
4466{
4467 if (ssid == NULL)
4468 return;
4469
4470 if (ssid->disabled_until.sec) {
4471 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4472 "id=%d ssid=\"%s\"",
4473 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4474 }
4475 ssid->disabled_until.sec = 0;
4476 ssid->disabled_until.usec = 0;
4477 if (clear_failures)
4478 ssid->auth_failures = 0;
4479}
6407f413
JM
4480
4481
4482int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4483{
4484 size_t i;
4485
4486 if (wpa_s->disallow_aps_bssid == NULL)
4487 return 0;
4488
4489 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4490 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4491 bssid, ETH_ALEN) == 0)
4492 return 1;
4493 }
4494
4495 return 0;
4496}
4497
4498
4499int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4500 size_t ssid_len)
4501{
4502 size_t i;
4503
4504 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4505 return 0;
4506
4507 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4508 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4509 if (ssid_len == s->ssid_len &&
4510 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4511 return 1;
4512 }
4513
4514 return 0;
4515}
9796a86c
JM
4516
4517
4518/**
4519 * wpas_request_connection - Request a new connection
4520 * @wpa_s: Pointer to the network interface
4521 *
4522 * This function is used to request a new connection to be found. It will mark
4523 * the interface to allow reassociation and request a new scan to find a
4524 * suitable network to connect to.
4525 */
4526void wpas_request_connection(struct wpa_supplicant *wpa_s)
4527{
4528 wpa_s->normal_scans = 0;
4529 wpa_supplicant_reinit_autoscan(wpa_s);
4530 wpa_s->extra_blacklist_count = 0;
4531 wpa_s->disconnected = 0;
4532 wpa_s->reassociate = 1;
5e24beae
MH
4533
4534 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4535 wpa_supplicant_req_scan(wpa_s, 0, 0);
9796a86c 4536}
36b9883d
DG
4537
4538
217cf499
JM
4539void dump_freq_array(struct wpa_supplicant *wpa_s, const char *title,
4540 int *freq_array, unsigned int len)
4541{
4542 unsigned int i;
4543
4544 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4545 len, title);
4546 for (i = 0; i < len; i++)
4547 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d", i, freq_array[i]);
4548}
4549
4550
53c5dfc2
IP
4551/*
4552 * Find the operating frequencies of any of the virtual interfaces that
4553 * are using the same radio as the current interface.
4554 */
4555int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4556 int *freq_array, unsigned int len)
4557{
53c5dfc2
IP
4558 struct wpa_supplicant *ifs;
4559 u8 bssid[ETH_ALEN];
4560 int freq;
4561 unsigned int idx = 0, i;
4562
217cf499
JM
4563 wpa_dbg(wpa_s, MSG_DEBUG,
4564 "Determining shared radio frequencies (max len %u)", len);
53c5dfc2
IP
4565 os_memset(freq_array, 0, sizeof(int) * len);
4566
4567 /* First add the frequency of the local interface */
4568 if (wpa_s->current_ssid != NULL && wpa_s->assoc_freq != 0) {
4569 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4570 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)
4571 freq_array[idx++] = wpa_s->current_ssid->frequency;
4572 else if (wpa_drv_get_bssid(wpa_s, bssid) == 0)
4573 freq_array[idx++] = wpa_s->assoc_freq;
4574 }
4575
4576 /* If get_radio_name is not supported, use only the local freq */
95bf699f 4577 if (!wpa_driver_get_radio_name(wpa_s)) {
a21816a9
JM
4578 freq = wpa_drv_shared_freq(wpa_s);
4579 if (freq > 0 && idx < len &&
4580 (idx == 0 || freq_array[0] != freq))
4581 freq_array[idx++] = freq;
217cf499 4582 dump_freq_array(wpa_s, "No get_radio_name", freq_array, idx);
53c5dfc2 4583 return idx;
a21816a9 4584 }
53c5dfc2 4585
0ad3b9c4
JM
4586 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4587 radio_list) {
4588 if (wpa_s == ifs)
53c5dfc2
IP
4589 continue;
4590
4591 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4592 continue;
4593
4594 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4595 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4596 freq = ifs->current_ssid->frequency;
4597 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4598 freq = ifs->assoc_freq;
4599 else
4600 continue;
4601
4602 /* Hold only distinct freqs */
4603 for (i = 0; i < idx; i++)
4604 if (freq_array[i] == freq)
4605 break;
4606
4607 if (i == idx)
4608 freq_array[idx++] = freq;
4609 }
217cf499
JM
4610
4611 dump_freq_array(wpa_s, "completed iteration", freq_array, idx);
53c5dfc2
IP
4612 return idx;
4613}