]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wpa_supplicant.c
P2P: Improve handling of p2p_connect-auto fallback
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant
d93dfbd5 3 * Copyright (c) 2003-2012, 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"
3ec97afe 20#include "eap_server/eap_methods.h"
3acb5005 21#include "rsn_supp/wpa.h"
6fc6879b 22#include "eloop.h"
6fc6879b
JM
23#include "config.h"
24#include "l2_packet/l2_packet.h"
25#include "wpa_supplicant_i.h"
2d5b792d 26#include "driver_i.h"
6fc6879b 27#include "ctrl_iface.h"
6fc6879b 28#include "pcsc_funcs.h"
90973fb2 29#include "common/version.h"
3acb5005
JM
30#include "rsn_supp/preauth.h"
31#include "rsn_supp/pmksa_cache.h"
90973fb2 32#include "common/wpa_ctrl.h"
90973fb2 33#include "common/ieee802_11_defs.h"
72044390 34#include "p2p/p2p.h"
6fc6879b
JM
35#include "blacklist.h"
36#include "wpas_glue.h"
116654ce 37#include "wps_supplicant.h"
11ef8d35 38#include "ibss_rsn.h"
c2a04078 39#include "sme.h"
04ea7b79 40#include "gas_query.h"
1f1b62a0 41#include "ap.h"
b22128ef 42#include "p2p_supplicant.h"
8bac466b 43#include "notify.h"
60b94c98 44#include "bgscan.h"
83922c2d 45#include "bss.h"
9ba9fa07 46#include "scan.h"
24f6497c 47#include "offchannel.h"
cb418324 48#include "hs20_supplicant.h"
6fc6879b
JM
49
50const char *wpa_supplicant_version =
51"wpa_supplicant v" VERSION_STR "\n"
57d38ddf 52"Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
6fc6879b
JM
53
54const char *wpa_supplicant_license =
331f89ff
JM
55"This software may be distributed under the terms of the BSD license.\n"
56"See README for more details.\n"
6fc6879b
JM
57#ifdef EAP_TLS_OPENSSL
58"\nThis product includes software developed by the OpenSSL Project\n"
59"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
60#endif /* EAP_TLS_OPENSSL */
61;
62
63#ifndef CONFIG_NO_STDOUT_DEBUG
64/* Long text divided into parts in order to fit in C89 strings size limits. */
65const char *wpa_supplicant_full_license1 =
331f89ff 66"";
6fc6879b 67const char *wpa_supplicant_full_license2 =
331f89ff 68"This software may be distributed under the terms of the BSD license.\n"
6fc6879b
JM
69"\n"
70"Redistribution and use in source and binary forms, with or without\n"
71"modification, are permitted provided that the following conditions are\n"
72"met:\n"
73"\n";
74const char *wpa_supplicant_full_license3 =
75"1. Redistributions of source code must retain the above copyright\n"
76" notice, this list of conditions and the following disclaimer.\n"
77"\n"
78"2. Redistributions in binary form must reproduce the above copyright\n"
79" notice, this list of conditions and the following disclaimer in the\n"
80" documentation and/or other materials provided with the distribution.\n"
81"\n";
82const char *wpa_supplicant_full_license4 =
83"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
84" names of its contributors may be used to endorse or promote products\n"
85" derived from this software without specific prior written permission.\n"
86"\n"
87"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
88"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
89"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
90"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
91const char *wpa_supplicant_full_license5 =
92"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
93"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
94"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
95"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
96"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
97"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
98"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
99"\n";
100#endif /* CONFIG_NO_STDOUT_DEBUG */
101
6fc6879b
JM
102extern int wpa_debug_level;
103extern int wpa_debug_show_keys;
104extern int wpa_debug_timestamp;
c5121837 105extern struct wpa_driver_ops *wpa_drivers[];
6fc6879b
JM
106
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
126static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
127 struct wpa_ssid *ssid)
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;
155 case WPA_CIPHER_TKIP:
156 /* WPA-None uses the same Michael MIC key for both TX and RX */
157 os_memcpy(key, ssid->psk, 16 + 8);
158 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
159 keylen = 32;
160 alg = WPA_ALG_TKIP;
161 break;
162 default:
f049052b
BG
163 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
164 "WPA-None", wpa_s->group_cipher);
6fc6879b
JM
165 return -1;
166 }
167
168 /* TODO: should actually remember the previously used seq#, both for TX
169 * and RX from each STA.. */
170
0382097e 171 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
6fc6879b
JM
172}
173
174
175static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
176{
177 struct wpa_supplicant *wpa_s = eloop_ctx;
178 const u8 *bssid = wpa_s->bssid;
a8e16edc 179 if (is_zero_ether_addr(bssid))
6fc6879b
JM
180 bssid = wpa_s->pending_bssid;
181 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
182 MAC2STR(bssid));
183 wpa_blacklist_add(wpa_s, bssid);
184 wpa_sm_notify_disassoc(wpa_s->wpa);
185 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
186 wpa_s->reassociate = 1;
48b84f18
BG
187
188 /*
189 * If we timed out, the AP or the local radio may be busy.
190 * So, wait a second until scanning again.
191 */
192 wpa_supplicant_req_scan(wpa_s, 1, 0);
6fc6879b
JM
193}
194
195
196/**
197 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
198 * @wpa_s: Pointer to wpa_supplicant data
199 * @sec: Number of seconds after which to time out authentication
200 * @usec: Number of microseconds after which to time out authentication
201 *
202 * This function is used to schedule a timeout for the current authentication
203 * attempt.
204 */
205void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
206 int sec, int usec)
207{
c2a04078
JM
208 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
209 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
6fc6879b
JM
210 return;
211
f049052b 212 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
6fc6879b
JM
213 "%d usec", sec, usec);
214 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
215 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
216}
217
218
219/**
220 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
221 * @wpa_s: Pointer to wpa_supplicant data
222 *
223 * This function is used to cancel authentication timeout scheduled with
224 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
225 * been completed.
226 */
227void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
228{
f049052b 229 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
6fc6879b
JM
230 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
231 wpa_blacklist_del(wpa_s, wpa_s->bssid);
232}
233
234
235/**
236 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
237 * @wpa_s: Pointer to wpa_supplicant data
238 *
239 * This function is used to configure EAPOL state machine based on the selected
240 * authentication mode.
241 */
242void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
243{
244#ifdef IEEE8021X_EAPOL
245 struct eapol_config eapol_conf;
246 struct wpa_ssid *ssid = wpa_s->current_ssid;
247
53895c3b 248#ifdef CONFIG_IBSS_RSN
d7dcba70 249 if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
250 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
251 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
252 /*
253 * RSN IBSS authentication is per-STA and we can disable the
254 * per-BSSID EAPOL authentication.
255 */
256 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
257 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
258 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
259 return;
260 }
261#endif /* CONFIG_IBSS_RSN */
262
0a40ec6a
JM
263 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
264 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
265
6fc6879b
JM
266 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
267 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
268 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
269 else
270 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
271
272 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
273 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
274 eapol_conf.accept_802_1x_keys = 1;
275 eapol_conf.required_keys = 0;
276 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
277 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
278 }
279 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
280 eapol_conf.required_keys |=
281 EAPOL_REQUIRE_KEY_BROADCAST;
282 }
283
c2a04078 284 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
6fc6879b 285 eapol_conf.required_keys = 0;
6fc6879b
JM
286 }
287 if (wpa_s->conf)
288 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
289 eapol_conf.workaround = ssid->eap_workaround;
56586197
JM
290 eapol_conf.eap_disabled =
291 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
ad08c363
JM
292 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
293 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
6fc6879b
JM
294 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
295#endif /* IEEE8021X_EAPOL */
296}
297
298
299/**
300 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
301 * @wpa_s: Pointer to wpa_supplicant data
302 * @ssid: Configuration data for the network
303 *
304 * This function is used to configure WPA state machine and related parameters
305 * to a mode where WPA is not enabled. This is called as part of the
306 * authentication configuration when the selected network does not use WPA.
307 */
308void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
309 struct wpa_ssid *ssid)
310{
311 int i;
312
ad08c363
JM
313 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
314 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
315 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
6fc6879b
JM
316 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
317 else
318 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
319 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
320 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
321 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
322 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
323 wpa_s->group_cipher = WPA_CIPHER_NONE;
324 wpa_s->mgmt_group_cipher = 0;
325
326 for (i = 0; i < NUM_WEP_KEYS; i++) {
327 if (ssid->wep_key_len[i] > 5) {
328 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
329 wpa_s->group_cipher = WPA_CIPHER_WEP104;
330 break;
331 } else if (ssid->wep_key_len[i] > 0) {
332 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
333 wpa_s->group_cipher = WPA_CIPHER_WEP40;
334 break;
335 }
336 }
337
338 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
339 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
340 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
341 wpa_s->pairwise_cipher);
342 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
343#ifdef CONFIG_IEEE80211W
344 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
345 wpa_s->mgmt_group_cipher);
346#endif /* CONFIG_IEEE80211W */
347
348 pmksa_cache_clear_current(wpa_s->wpa);
349}
350
351
6979582c 352void free_hw_features(struct wpa_supplicant *wpa_s)
6bf731e8
CL
353{
354 int i;
355 if (wpa_s->hw.modes == NULL)
356 return;
357
358 for (i = 0; i < wpa_s->hw.num_modes; i++) {
359 os_free(wpa_s->hw.modes[i].channels);
360 os_free(wpa_s->hw.modes[i].rates);
361 }
362
363 os_free(wpa_s->hw.modes);
364 wpa_s->hw.modes = NULL;
365}
366
367
6fc6879b
JM
368static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
369{
60b94c98 370 bgscan_deinit(wpa_s);
6fc6879b
JM
371 scard_deinit(wpa_s->scard);
372 wpa_s->scard = NULL;
373 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
374 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
375 l2_packet_deinit(wpa_s->l2);
376 wpa_s->l2 = NULL;
377 if (wpa_s->l2_br) {
378 l2_packet_deinit(wpa_s->l2_br);
379 wpa_s->l2_br = NULL;
380 }
381
6fc6879b 382 if (wpa_s->conf != NULL) {
8e56d189
JM
383 struct wpa_ssid *ssid;
384 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
385 wpas_notify_network_removed(wpa_s, ssid);
6fc6879b
JM
386 }
387
388 os_free(wpa_s->confname);
389 wpa_s->confname = NULL;
390
391 wpa_sm_set_eapol(wpa_s->wpa, NULL);
392 eapol_sm_deinit(wpa_s->eapol);
393 wpa_s->eapol = NULL;
394
395 rsn_preauth_deinit(wpa_s->wpa);
396
281ff0aa
GP
397#ifdef CONFIG_TDLS
398 wpa_tdls_deinit(wpa_s->wpa);
399#endif /* CONFIG_TDLS */
400
6fc6879b
JM
401 pmksa_candidate_free(wpa_s->wpa);
402 wpa_sm_deinit(wpa_s->wpa);
403 wpa_s->wpa = NULL;
404 wpa_blacklist_clear(wpa_s);
405
83922c2d 406 wpa_bss_deinit(wpa_s);
6fc6879b
JM
407
408 wpa_supplicant_cancel_scan(wpa_s);
409 wpa_supplicant_cancel_auth_timeout(wpa_s);
01a17491
JM
410 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
411#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
412 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
413 wpa_s, NULL);
414#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b 415
116654ce 416 wpas_wps_deinit(wpa_s);
11ef8d35 417
1ff73338
JM
418 wpabuf_free(wpa_s->pending_eapol_rx);
419 wpa_s->pending_eapol_rx = NULL;
420
11ef8d35
JM
421#ifdef CONFIG_IBSS_RSN
422 ibss_rsn_deinit(wpa_s->ibss_rsn);
423 wpa_s->ibss_rsn = NULL;
424#endif /* CONFIG_IBSS_RSN */
c2a04078 425
e29853bb 426 sme_deinit(wpa_s);
2d5b792d
JM
427
428#ifdef CONFIG_AP
429 wpa_supplicant_ap_deinit(wpa_s);
430#endif /* CONFIG_AP */
b22128ef
JM
431
432#ifdef CONFIG_P2P
433 wpas_p2p_deinit(wpa_s);
434#endif /* CONFIG_P2P */
f47d639d 435
24f6497c
JM
436#ifdef CONFIG_OFFCHANNEL
437 offchannel_deinit(wpa_s);
438#endif /* CONFIG_OFFCHANNEL */
439
a4cba8f1
LC
440 wpa_supplicant_cancel_sched_scan(wpa_s);
441
f47d639d
JM
442 os_free(wpa_s->next_scan_freqs);
443 wpa_s->next_scan_freqs = NULL;
04ea7b79
JM
444
445 gas_query_deinit(wpa_s->gas);
446 wpa_s->gas = NULL;
6bf731e8
CL
447
448 free_hw_features(wpa_s);
d445a5cd
JM
449
450 os_free(wpa_s->bssid_filter);
451 wpa_s->bssid_filter = NULL;
6fc6879b
JM
452}
453
454
455/**
456 * wpa_clear_keys - Clear keys configured for the driver
457 * @wpa_s: Pointer to wpa_supplicant data
458 * @addr: Previously used BSSID or %NULL if not available
459 *
460 * This function clears the encryption keys that has been previously configured
461 * for the driver.
462 */
463void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
464{
6fc6879b
JM
465 if (wpa_s->keys_cleared) {
466 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
467 * timing issues with keys being cleared just before new keys
468 * are set or just after association or something similar. This
469 * shows up in group key handshake failing often because of the
470 * client not receiving the first encrypted packets correctly.
471 * Skipping some of the extra key clearing steps seems to help
472 * in completing group key handshake more reliably. */
f049052b
BG
473 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
474 "skip key clearing");
6fc6879b
JM
475 return;
476 }
477
478 /* MLME-DELETEKEYS.request */
0382097e
JM
479 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
480 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
481 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
482 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
0e27f655 483#ifdef CONFIG_IEEE80211W
0382097e
JM
484 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
485 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
0e27f655 486#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
487 if (addr) {
488 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
489 0);
490 /* MLME-SETPROTECTION.request(None) */
491 wpa_drv_mlme_setprotection(
492 wpa_s, addr,
493 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
494 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
495 }
496 wpa_s->keys_cleared = 1;
497}
498
499
500/**
501 * wpa_supplicant_state_txt - Get the connection state name as a text string
502 * @state: State (wpa_state; WPA_*)
503 * Returns: The state name as a printable text string
504 */
71934751 505const char * wpa_supplicant_state_txt(enum wpa_states state)
6fc6879b
JM
506{
507 switch (state) {
508 case WPA_DISCONNECTED:
509 return "DISCONNECTED";
510 case WPA_INACTIVE:
511 return "INACTIVE";
8401a6b0
JM
512 case WPA_INTERFACE_DISABLED:
513 return "INTERFACE_DISABLED";
6fc6879b
JM
514 case WPA_SCANNING:
515 return "SCANNING";
c2a04078
JM
516 case WPA_AUTHENTICATING:
517 return "AUTHENTICATING";
6fc6879b
JM
518 case WPA_ASSOCIATING:
519 return "ASSOCIATING";
520 case WPA_ASSOCIATED:
521 return "ASSOCIATED";
522 case WPA_4WAY_HANDSHAKE:
523 return "4WAY_HANDSHAKE";
524 case WPA_GROUP_HANDSHAKE:
525 return "GROUP_HANDSHAKE";
526 case WPA_COMPLETED:
527 return "COMPLETED";
528 default:
529 return "UNKNOWN";
530 }
531}
532
533
cfe53c9a
PS
534#ifdef CONFIG_BGSCAN
535
536static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
537{
0096c427
JM
538 if (wpas_driver_bss_selection(wpa_s))
539 return;
cfe53c9a
PS
540 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
541 return;
542
543 bgscan_deinit(wpa_s);
544 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
545 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
546 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
547 "bgscan");
548 /*
549 * Live without bgscan; it is only used as a roaming
550 * optimization, so the initial connection is not
551 * affected.
552 */
553 } else
554 wpa_s->bgscan_ssid = wpa_s->current_ssid;
555 } else
556 wpa_s->bgscan_ssid = NULL;
557}
558
559
560static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
561{
562 if (wpa_s->bgscan_ssid != NULL) {
563 bgscan_deinit(wpa_s);
564 wpa_s->bgscan_ssid = NULL;
565 }
566}
567
568#endif /* CONFIG_BGSCAN */
569
570
6fc6879b
JM
571/**
572 * wpa_supplicant_set_state - Set current connection state
573 * @wpa_s: Pointer to wpa_supplicant data
574 * @state: The new connection state
575 *
576 * This function is called whenever the connection state changes, e.g.,
577 * association is completed for WPA/WPA2 4-Way Handshake is started.
578 */
71934751
JM
579void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
580 enum wpa_states state)
6fc6879b 581{
27f43d8d
MH
582 enum wpa_states old_state = wpa_s->wpa_state;
583
f049052b
BG
584 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
585 wpa_supplicant_state_txt(wpa_s->wpa_state),
586 wpa_supplicant_state_txt(state));
6fc6879b 587
cb8564b1
DW
588 if (state != WPA_SCANNING)
589 wpa_supplicant_notify_scanning(wpa_s, 0);
590
6fc6879b
JM
591 if (state == WPA_COMPLETED && wpa_s->new_connection) {
592#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
593 struct wpa_ssid *ssid = wpa_s->current_ssid;
594 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
595 MACSTR " completed %s [id=%d id_str=%s]",
596 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
597 "(reauth)" : "(auth)",
598 ssid ? ssid->id : -1,
599 ssid && ssid->id_str ? ssid->id_str : "");
600#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
601 wpa_s->new_connection = 0;
602 wpa_s->reassociated_connection = 1;
603 wpa_drv_set_operstate(wpa_s, 1);
99ac2913
FF
604#ifndef IEEE8021X_EAPOL
605 wpa_drv_set_supp_port(wpa_s, 1);
606#endif /* IEEE8021X_EAPOL */
17a4734d 607 wpa_s->after_wps = 0;
b22128ef
JM
608#ifdef CONFIG_P2P
609 wpas_p2p_completed(wpa_s);
610#endif /* CONFIG_P2P */
c3701c66
RM
611
612 sme_sched_obss_scan(wpa_s, 1);
6fc6879b
JM
613 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
614 state == WPA_ASSOCIATED) {
615 wpa_s->new_connection = 1;
616 wpa_drv_set_operstate(wpa_s, 0);
99ac2913
FF
617#ifndef IEEE8021X_EAPOL
618 wpa_drv_set_supp_port(wpa_s, 0);
619#endif /* IEEE8021X_EAPOL */
c3701c66 620 sme_sched_obss_scan(wpa_s, 0);
6fc6879b
JM
621 }
622 wpa_s->wpa_state = state;
27f43d8d 623
cfe53c9a
PS
624#ifdef CONFIG_BGSCAN
625 if (state == WPA_COMPLETED)
626 wpa_supplicant_start_bgscan(wpa_s);
627 else
628 wpa_supplicant_stop_bgscan(wpa_s);
629#endif /* CONFIG_BGSCAN */
630
5bbf9f10 631 if (wpa_s->wpa_state != old_state) {
27f43d8d 632 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
5bbf9f10
PS
633
634 if (wpa_s->wpa_state == WPA_COMPLETED ||
635 old_state == WPA_COMPLETED)
636 wpas_notify_auth_changed(wpa_s);
637 }
6fc6879b
JM
638}
639
640
1a1bf008
JM
641void wpa_supplicant_terminate_proc(struct wpa_global *global)
642{
643 int pending = 0;
644#ifdef CONFIG_WPS
645 struct wpa_supplicant *wpa_s = global->ifaces;
646 while (wpa_s) {
647 if (wpas_wps_terminate_pending(wpa_s) == 1)
648 pending = 1;
649 wpa_s = wpa_s->next;
650 }
651#endif /* CONFIG_WPS */
652 if (pending)
653 return;
654 eloop_terminate();
655}
656
657
0456ea16 658static void wpa_supplicant_terminate(int sig, void *signal_ctx)
6fc6879b 659{
0456ea16 660 struct wpa_global *global = signal_ctx;
1a1bf008 661 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
662}
663
664
b22128ef 665void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
6fc6879b 666{
71934751 667 enum wpa_states old_state = wpa_s->wpa_state;
27f43d8d 668
6fc6879b
JM
669 wpa_s->pairwise_cipher = 0;
670 wpa_s->group_cipher = 0;
671 wpa_s->mgmt_group_cipher = 0;
672 wpa_s->key_mgmt = 0;
8401a6b0
JM
673 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
674 wpa_s->wpa_state = WPA_DISCONNECTED;
27f43d8d
MH
675
676 if (wpa_s->wpa_state != old_state)
677 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
6fc6879b
JM
678}
679
680
681/**
682 * wpa_supplicant_reload_configuration - Reload configuration data
683 * @wpa_s: Pointer to wpa_supplicant data
684 * Returns: 0 on success or -1 if configuration parsing failed
685 *
686 * This function can be used to request that the configuration data is reloaded
687 * (e.g., after configuration file change). This function is reloading
688 * configuration only for one interface, so this may need to be called multiple
689 * times if %wpa_supplicant is controlling multiple interfaces and all
690 * interfaces need reconfiguration.
691 */
692int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
693{
694 struct wpa_config *conf;
695 int reconf_ctrl;
8bac466b
JM
696 int old_ap_scan;
697
6fc6879b
JM
698 if (wpa_s->confname == NULL)
699 return -1;
700 conf = wpa_config_read(wpa_s->confname);
701 if (conf == NULL) {
702 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
703 "file '%s' - exiting", wpa_s->confname);
704 return -1;
705 }
611aea7d 706 conf->changed_parameters = (unsigned int) -1;
6fc6879b
JM
707
708 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
709 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
710 os_strcmp(conf->ctrl_interface,
711 wpa_s->conf->ctrl_interface) != 0);
712
713 if (reconf_ctrl && wpa_s->ctrl_iface) {
714 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
715 wpa_s->ctrl_iface = NULL;
716 }
717
718 eapol_sm_invalidate_cached_session(wpa_s->eapol);
7b7ce8aa
JM
719 if (wpa_s->current_ssid) {
720 wpa_supplicant_deauthenticate(wpa_s,
721 WLAN_REASON_DEAUTH_LEAVING);
722 }
8bac466b 723
6fc6879b
JM
724 /*
725 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
726 * pkcs11_engine_path, pkcs11_module_path.
727 */
56586197 728 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
729 /*
730 * Clear forced success to clear EAP state for next
731 * authentication.
732 */
733 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
734 }
735 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
736 wpa_sm_set_config(wpa_s->wpa, NULL);
d8a790b9 737 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6fc6879b
JM
738 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
739 rsn_preauth_deinit(wpa_s->wpa);
8bac466b
JM
740
741 old_ap_scan = wpa_s->conf->ap_scan;
6fc6879b
JM
742 wpa_config_free(wpa_s->conf);
743 wpa_s->conf = conf;
8bac466b
JM
744 if (old_ap_scan != wpa_s->conf->ap_scan)
745 wpas_notify_ap_scan_changed(wpa_s);
746
6fc6879b
JM
747 if (reconf_ctrl)
748 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
749
611aea7d
JM
750 wpa_supplicant_update_config(wpa_s);
751
6fc6879b 752 wpa_supplicant_clear_status(wpa_s);
349493bd 753 if (wpa_supplicant_enabled_networks(wpa_s)) {
43a38635
JM
754 wpa_s->reassociate = 1;
755 wpa_supplicant_req_scan(wpa_s, 0, 0);
756 }
f049052b 757 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
6fc6879b
JM
758 return 0;
759}
760
761
0456ea16 762static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
6fc6879b 763{
0456ea16 764 struct wpa_global *global = signal_ctx;
6fc6879b 765 struct wpa_supplicant *wpa_s;
6fc6879b 766 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
f049052b
BG
767 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
768 sig);
6fc6879b 769 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1a1bf008 770 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
771 }
772 }
773}
774
775
508545f3 776enum wpa_cipher cipher_suite2driver(int cipher)
6fc6879b
JM
777{
778 switch (cipher) {
779 case WPA_CIPHER_NONE:
780 return CIPHER_NONE;
781 case WPA_CIPHER_WEP40:
782 return CIPHER_WEP40;
783 case WPA_CIPHER_WEP104:
784 return CIPHER_WEP104;
785 case WPA_CIPHER_CCMP:
786 return CIPHER_CCMP;
787 case WPA_CIPHER_TKIP:
788 default:
789 return CIPHER_TKIP;
790 }
791}
792
793
508545f3 794enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
6fc6879b
JM
795{
796 switch (key_mgmt) {
797 case WPA_KEY_MGMT_NONE:
798 return KEY_MGMT_NONE;
799 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
800 return KEY_MGMT_802_1X_NO_WPA;
801 case WPA_KEY_MGMT_IEEE8021X:
802 return KEY_MGMT_802_1X;
803 case WPA_KEY_MGMT_WPA_NONE:
804 return KEY_MGMT_WPA_NONE;
805 case WPA_KEY_MGMT_FT_IEEE8021X:
806 return KEY_MGMT_FT_802_1X;
807 case WPA_KEY_MGMT_FT_PSK:
808 return KEY_MGMT_FT_PSK;
56586197
JM
809 case WPA_KEY_MGMT_IEEE8021X_SHA256:
810 return KEY_MGMT_802_1X_SHA256;
811 case WPA_KEY_MGMT_PSK_SHA256:
812 return KEY_MGMT_PSK_SHA256;
ad08c363
JM
813 case WPA_KEY_MGMT_WPS:
814 return KEY_MGMT_WPS;
6fc6879b
JM
815 case WPA_KEY_MGMT_PSK:
816 default:
817 return KEY_MGMT_PSK;
818 }
819}
820
821
822static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
823 struct wpa_ssid *ssid,
824 struct wpa_ie_data *ie)
825{
826 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
827 if (ret) {
828 if (ret == -2) {
829 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
830 "from association info");
831 }
832 return -1;
833 }
834
f049052b
BG
835 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
836 "cipher suites");
6fc6879b
JM
837 if (!(ie->group_cipher & ssid->group_cipher)) {
838 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
839 "cipher 0x%x (mask 0x%x) - reject",
840 ie->group_cipher, ssid->group_cipher);
841 return -1;
842 }
843 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
844 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
845 "cipher 0x%x (mask 0x%x) - reject",
846 ie->pairwise_cipher, ssid->pairwise_cipher);
847 return -1;
848 }
849 if (!(ie->key_mgmt & ssid->key_mgmt)) {
850 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
851 "management 0x%x (mask 0x%x) - reject",
852 ie->key_mgmt, ssid->key_mgmt);
853 return -1;
854 }
855
856#ifdef CONFIG_IEEE80211W
0b60b0aa 857 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
70f8cc8e 858 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
6fc6879b
JM
859 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
860 "that does not support management frame protection - "
861 "reject");
862 return -1;
863 }
864#endif /* CONFIG_IEEE80211W */
865
866 return 0;
867}
868
869
870/**
871 * wpa_supplicant_set_suites - Set authentication and encryption parameters
872 * @wpa_s: Pointer to wpa_supplicant data
873 * @bss: Scan results for the selected BSS, or %NULL if not available
874 * @ssid: Configuration data for the selected network
875 * @wpa_ie: Buffer for the WPA/RSN IE
876 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
877 * used buffer length in case the functions returns success.
878 * Returns: 0 on success or -1 on failure
879 *
880 * This function is used to configure authentication and encryption parameters
881 * based on the network configuration and scan result for the selected BSS (if
882 * available).
883 */
884int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
6fa81a3b 885 struct wpa_bss *bss, struct wpa_ssid *ssid,
6fc6879b
JM
886 u8 *wpa_ie, size_t *wpa_ie_len)
887{
888 struct wpa_ie_data ie;
889 int sel, proto;
890 const u8 *bss_wpa, *bss_rsn;
891
892 if (bss) {
6fa81a3b
JM
893 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
894 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
6fc6879b
JM
895 } else
896 bss_wpa = bss_rsn = NULL;
897
898 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
899 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
900 (ie.group_cipher & ssid->group_cipher) &&
901 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
902 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 903 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
6fc6879b
JM
904 proto = WPA_PROTO_RSN;
905 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
906 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
907 (ie.group_cipher & ssid->group_cipher) &&
908 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
909 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 910 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
6fc6879b
JM
911 proto = WPA_PROTO_WPA;
912 } else if (bss) {
913 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
914 return -1;
915 } else {
916 if (ssid->proto & WPA_PROTO_RSN)
917 proto = WPA_PROTO_RSN;
918 else
919 proto = WPA_PROTO_WPA;
920 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
921 os_memset(&ie, 0, sizeof(ie));
922 ie.group_cipher = ssid->group_cipher;
923 ie.pairwise_cipher = ssid->pairwise_cipher;
924 ie.key_mgmt = ssid->key_mgmt;
925#ifdef CONFIG_IEEE80211W
926 ie.mgmt_group_cipher =
70f8cc8e 927 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
6fc6879b
JM
928 WPA_CIPHER_AES_128_CMAC : 0;
929#endif /* CONFIG_IEEE80211W */
f049052b
BG
930 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
931 "based on configuration");
6fc6879b
JM
932 } else
933 proto = ie.proto;
934 }
935
f049052b
BG
936 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
937 "pairwise %d key_mgmt %d proto %d",
938 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
6fc6879b
JM
939#ifdef CONFIG_IEEE80211W
940 if (ssid->ieee80211w) {
f049052b
BG
941 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
942 ie.mgmt_group_cipher);
6fc6879b
JM
943 }
944#endif /* CONFIG_IEEE80211W */
945
64fa840a 946 wpa_s->wpa_proto = proto;
6fc6879b
JM
947 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
948 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
949 !!(ssid->proto & WPA_PROTO_RSN));
950
951 if (bss || !wpa_s->ap_ies_from_associnfo) {
952 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
953 bss_wpa ? 2 + bss_wpa[1] : 0) ||
954 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
955 bss_rsn ? 2 + bss_rsn[1] : 0))
956 return -1;
957 }
958
959 sel = ie.group_cipher & ssid->group_cipher;
960 if (sel & WPA_CIPHER_CCMP) {
961 wpa_s->group_cipher = WPA_CIPHER_CCMP;
f049052b 962 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
6fc6879b
JM
963 } else if (sel & WPA_CIPHER_TKIP) {
964 wpa_s->group_cipher = WPA_CIPHER_TKIP;
f049052b 965 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
6fc6879b
JM
966 } else if (sel & WPA_CIPHER_WEP104) {
967 wpa_s->group_cipher = WPA_CIPHER_WEP104;
f049052b 968 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
6fc6879b
JM
969 } else if (sel & WPA_CIPHER_WEP40) {
970 wpa_s->group_cipher = WPA_CIPHER_WEP40;
f049052b 971 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
6fc6879b 972 } else {
f049052b
BG
973 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
974 "cipher");
6fc6879b
JM
975 return -1;
976 }
977
978 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
979 if (sel & WPA_CIPHER_CCMP) {
980 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
f049052b 981 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
6fc6879b
JM
982 } else if (sel & WPA_CIPHER_TKIP) {
983 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
f049052b 984 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
6fc6879b
JM
985 } else if (sel & WPA_CIPHER_NONE) {
986 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
f049052b 987 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
6fc6879b 988 } else {
f049052b
BG
989 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
990 "cipher");
6fc6879b
JM
991 return -1;
992 }
993
994 sel = ie.key_mgmt & ssid->key_mgmt;
995 if (0) {
996#ifdef CONFIG_IEEE80211R
997 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
998 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
f049052b 999 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
6fc6879b
JM
1000 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1001 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
f049052b 1002 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
6fc6879b 1003#endif /* CONFIG_IEEE80211R */
56586197
JM
1004#ifdef CONFIG_IEEE80211W
1005 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1006 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
f049052b 1007 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1008 "WPA: using KEY_MGMT 802.1X with SHA256");
1009 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1010 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
f049052b 1011 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1012 "WPA: using KEY_MGMT PSK with SHA256");
1013#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
1014 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1015 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
f049052b 1016 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
6fc6879b
JM
1017 } else if (sel & WPA_KEY_MGMT_PSK) {
1018 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
f049052b 1019 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
6fc6879b
JM
1020 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1021 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
f049052b 1022 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
6fc6879b 1023 } else {
f049052b
BG
1024 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1025 "authenticated key management type");
6fc6879b
JM
1026 return -1;
1027 }
1028
1029 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1030 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1031 wpa_s->pairwise_cipher);
1032 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1033
1034#ifdef CONFIG_IEEE80211W
1035 sel = ie.mgmt_group_cipher;
70f8cc8e 1036 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
0b60b0aa 1037 !(ie.capabilities & WPA_CAPABILITY_MFPC))
6fc6879b
JM
1038 sel = 0;
1039 if (sel & WPA_CIPHER_AES_128_CMAC) {
1040 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
f049052b 1041 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
6fc6879b
JM
1042 "AES-128-CMAC");
1043 } else {
1044 wpa_s->mgmt_group_cipher = 0;
f049052b 1045 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
6fc6879b
JM
1046 }
1047 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1048 wpa_s->mgmt_group_cipher);
e820cf95 1049 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
6fc6879b
JM
1050#endif /* CONFIG_IEEE80211W */
1051
1052 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
f049052b 1053 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
6fc6879b
JM
1054 return -1;
1055 }
1056
0bf927a0 1057 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
6fc6879b 1058 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
7d232e23
ZC
1059#ifndef CONFIG_NO_PBKDF2
1060 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1061 ssid->passphrase) {
1062 u8 psk[PMK_LEN];
1063 pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
1064 bss->ssid_len, 4096, psk, PMK_LEN);
1065 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1066 psk, PMK_LEN);
1067 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1068 }
1069#endif /* CONFIG_NO_PBKDF2 */
1070 } else
6fc6879b
JM
1071 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1072
1073 return 0;
1074}
1075
1076
1077/**
1078 * wpa_supplicant_associate - Request association
1079 * @wpa_s: Pointer to wpa_supplicant data
1080 * @bss: Scan results for the selected BSS, or %NULL if not available
1081 * @ssid: Configuration data for the selected network
1082 *
1083 * This function is used to request %wpa_supplicant to associate with a BSS.
1084 */
1085void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
6fa81a3b 1086 struct wpa_bss *bss, struct wpa_ssid *ssid)
6fc6879b 1087{
5f3a6aa0 1088 u8 wpa_ie[200];
6fc6879b 1089 size_t wpa_ie_len;
8bac466b 1090 int use_crypt, ret, i, bssid_changed;
abd9fafa 1091 int algs = WPA_AUTH_ALG_OPEN;
71934751 1092 enum wpa_cipher cipher_pairwise, cipher_group;
6fc6879b
JM
1093 struct wpa_driver_associate_params params;
1094 int wep_keys_set = 0;
1095 struct wpa_driver_capa capa;
1096 int assoc_failed = 0;
8bac466b 1097 struct wpa_ssid *old_ssid;
80e8a5ee
BG
1098#ifdef CONFIG_HT_OVERRIDES
1099 struct ieee80211_ht_capabilities htcaps;
1100 struct ieee80211_ht_capabilities htcaps_mask;
1101#endif /* CONFIG_HT_OVERRIDES */
6fc6879b 1102
78177a00
JM
1103#ifdef CONFIG_IBSS_RSN
1104 ibss_rsn_deinit(wpa_s->ibss_rsn);
1105 wpa_s->ibss_rsn = NULL;
1106#endif /* CONFIG_IBSS_RSN */
1107
2c5d725c
JM
1108 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1109 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1581b38b
JM
1110#ifdef CONFIG_AP
1111 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
f049052b
BG
1112 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1113 "mode");
1581b38b
JM
1114 return;
1115 }
1116 wpa_supplicant_create_ap(wpa_s, ssid);
8f770587 1117 wpa_s->current_bss = bss;
1581b38b 1118#else /* CONFIG_AP */
f049052b
BG
1119 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1120 "the build");
1581b38b
JM
1121#endif /* CONFIG_AP */
1122 return;
1123 }
1124
52c9e6f3 1125#ifdef CONFIG_TDLS
95cb2d88
JM
1126 if (bss)
1127 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1128 bss->ie_len);
52c9e6f3
JM
1129#endif /* CONFIG_TDLS */
1130
5cc4d64b
JM
1131 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1132 ssid->mode == IEEE80211_MODE_INFRA) {
c2a04078
JM
1133 sme_authenticate(wpa_s, bss, ssid);
1134 return;
1135 }
1136
0c80427d 1137 os_memset(&params, 0, sizeof(params));
6fc6879b 1138 wpa_s->reassociate = 0;
22628eca 1139 if (bss && !wpas_driver_bss_selection(wpa_s)) {
6fc6879b 1140#ifdef CONFIG_IEEE80211R
6fa81a3b 1141 const u8 *ie, *md = NULL;
6fc6879b 1142#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
1143 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1144 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
6fa81a3b 1145 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
8bac466b 1146 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
1147 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1148 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
8bac466b
JM
1149 if (bssid_changed)
1150 wpas_notify_bssid_changed(wpa_s);
6fc6879b 1151#ifdef CONFIG_IEEE80211R
6fa81a3b 1152 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
6fc6879b
JM
1153 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1154 md = ie + 2;
e7846b68 1155 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
91a05482
JM
1156 if (md) {
1157 /* Prepare for the next transition */
76b7981d 1158 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
91a05482 1159 }
6fc6879b 1160#endif /* CONFIG_IEEE80211R */
24c23d1b
JM
1161#ifdef CONFIG_WPS
1162 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1163 wpa_s->conf->ap_scan == 2 &&
1164 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1165 /* Use ap_scan==1 style network selection to find the network
1166 */
1167 wpa_s->scan_req = 2;
1168 wpa_s->reassociate = 1;
1169 wpa_supplicant_req_scan(wpa_s, 0, 0);
1170 return;
1171#endif /* CONFIG_WPS */
6fc6879b
JM
1172 } else {
1173 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1174 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1175 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1176 }
a4cba8f1 1177 wpa_supplicant_cancel_sched_scan(wpa_s);
6fc6879b
JM
1178 wpa_supplicant_cancel_scan(wpa_s);
1179
1180 /* Starting new association, so clear the possibly used WPA IE from the
1181 * previous association. */
1182 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1183
1184#ifdef IEEE8021X_EAPOL
1185 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1186 if (ssid->leap) {
1187 if (ssid->non_leap == 0)
abd9fafa 1188 algs = WPA_AUTH_ALG_LEAP;
6fc6879b 1189 else
abd9fafa 1190 algs |= WPA_AUTH_ALG_LEAP;
6fc6879b
JM
1191 }
1192 }
1193#endif /* IEEE8021X_EAPOL */
f049052b 1194 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
6fc6879b 1195 if (ssid->auth_alg) {
abd9fafa 1196 algs = ssid->auth_alg;
f049052b
BG
1197 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1198 "0x%x", algs);
6fc6879b 1199 }
6fc6879b 1200
6fa81a3b
JM
1201 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1202 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
0bf927a0 1203 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
6fc6879b
JM
1204 int try_opportunistic;
1205 try_opportunistic = ssid->proactive_key_caching &&
1206 (ssid->proto & WPA_PROTO_RSN);
1207 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1208 wpa_s->current_ssid,
1209 try_opportunistic) == 0)
1210 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1211 wpa_ie_len = sizeof(wpa_ie);
1212 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1213 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1214 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1215 "key management and encryption suites");
6fc6879b
JM
1216 return;
1217 }
0bf927a0 1218 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
6fc6879b
JM
1219 wpa_ie_len = sizeof(wpa_ie);
1220 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1221 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1222 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1223 "key management and encryption suites (no "
1224 "scan results)");
6fc6879b
JM
1225 return;
1226 }
ad08c363
JM
1227#ifdef CONFIG_WPS
1228 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
b01c18a8
JM
1229 struct wpabuf *wps_ie;
1230 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
ad08c363
JM
1231 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1232 wpa_ie_len = wpabuf_len(wps_ie);
1233 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
24386985
JM
1234 } else
1235 wpa_ie_len = 0;
ad08c363
JM
1236 wpabuf_free(wps_ie);
1237 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
0c80427d
JM
1238 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1239 params.wps = WPS_MODE_PRIVACY;
1240 else
1241 params.wps = WPS_MODE_OPEN;
cf546f1a 1242 wpa_s->wpa_proto = 0;
ad08c363 1243#endif /* CONFIG_WPS */
6fc6879b
JM
1244 } else {
1245 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1246 wpa_ie_len = 0;
cf546f1a 1247 wpa_s->wpa_proto = 0;
6fc6879b
JM
1248 }
1249
5f3a6aa0
JM
1250#ifdef CONFIG_P2P
1251 if (wpa_s->global->p2p) {
1252 u8 *pos;
1253 size_t len;
1254 int res;
5f3a6aa0
JM
1255 pos = wpa_ie + wpa_ie_len;
1256 len = sizeof(wpa_ie) - wpa_ie_len;
b8a8d677
JM
1257 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1258 ssid->p2p_group);
5f3a6aa0
JM
1259 if (res >= 0)
1260 wpa_ie_len += res;
1261 }
72044390
JM
1262
1263 wpa_s->cross_connect_disallowed = 0;
1264 if (bss) {
1265 struct wpabuf *p2p;
1266 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1267 if (p2p) {
1268 wpa_s->cross_connect_disallowed =
1269 p2p_get_cross_connect_disallowed(p2p);
1270 wpabuf_free(p2p);
f049052b
BG
1271 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1272 "connection",
1273 wpa_s->cross_connect_disallowed ?
1274 "disallows" : "allows");
72044390
JM
1275 }
1276 }
5f3a6aa0
JM
1277#endif /* CONFIG_P2P */
1278
cb418324
JM
1279#ifdef CONFIG_HS20
1280 if (wpa_s->conf->hs20) {
1281 struct wpabuf *hs20;
1282 hs20 = wpabuf_alloc(20);
1283 if (hs20) {
1284 wpas_hs20_add_indication(hs20);
1285 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
1286 wpabuf_len(hs20));
1287 wpa_ie_len += wpabuf_len(hs20);
1288 wpabuf_free(hs20);
1289 }
1290 }
1291#endif /* CONFIG_HS20 */
1292
92cbcf91
JM
1293#ifdef CONFIG_INTERWORKING
1294 if (wpa_s->conf->interworking) {
1295 u8 *pos = wpa_ie;
1296 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1297 pos += 2 + pos[1];
1298 os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
1299 wpa_ie_len += 6;
1300 *pos++ = WLAN_EID_EXT_CAPAB;
1301 *pos++ = 4;
1302 *pos++ = 0x00;
1303 *pos++ = 0x00;
1304 *pos++ = 0x00;
1305 *pos++ = 0x80; /* Bit 31 - Interworking */
1306 }
1307#endif /* CONFIG_INTERWORKING */
1308
6fc6879b
JM
1309 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1310 use_crypt = 1;
1311 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1312 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1313 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1314 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1315 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1316 use_crypt = 0;
1317 if (wpa_set_wep_keys(wpa_s, ssid)) {
1318 use_crypt = 1;
1319 wep_keys_set = 1;
1320 }
1321 }
ad08c363
JM
1322 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1323 use_crypt = 0;
6fc6879b
JM
1324
1325#ifdef IEEE8021X_EAPOL
1326 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1327 if ((ssid->eapol_flags &
1328 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1329 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1330 !wep_keys_set) {
1331 use_crypt = 0;
1332 } else {
1333 /* Assume that dynamic WEP-104 keys will be used and
1334 * set cipher suites in order for drivers to expect
1335 * encryption. */
1336 cipher_pairwise = cipher_group = CIPHER_WEP104;
1337 }
1338 }
1339#endif /* IEEE8021X_EAPOL */
1340
1341 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1342 /* Set the key before (and later after) association */
1343 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1344 }
1345
6fc6879b 1346 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
6fc6879b 1347 if (bss) {
6fa81a3b
JM
1348 params.ssid = bss->ssid;
1349 params.ssid_len = bss->ssid_len;
f15854d1
JM
1350 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1351 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1352 MACSTR " freq=%u MHz based on scan results "
1353 "(bssid_set=%d)",
1354 MAC2STR(bss->bssid), bss->freq,
1355 ssid->bssid_set);
22628eca
JM
1356 params.bssid = bss->bssid;
1357 params.freq = bss->freq;
1358 }
6fc6879b
JM
1359 } else {
1360 params.ssid = ssid->ssid;
1361 params.ssid_len = ssid->ssid_len;
1362 }
9e2af29f
NC
1363
1364 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1365 wpa_s->conf->ap_scan == 2) {
1366 params.bssid = ssid->bssid;
1367 params.fixed_bssid = 1;
1368 }
1369
d7dcba70
JM
1370 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1371 params.freq == 0)
6fc6879b
JM
1372 params.freq = ssid->frequency; /* Initial channel for IBSS */
1373 params.wpa_ie = wpa_ie;
1374 params.wpa_ie_len = wpa_ie_len;
1375 params.pairwise_suite = cipher_pairwise;
1376 params.group_suite = cipher_group;
1377 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
64fa840a 1378 params.wpa_proto = wpa_s->wpa_proto;
6fc6879b
JM
1379 params.auth_alg = algs;
1380 params.mode = ssid->mode;
1f6c0ab8 1381 params.bg_scan_period = ssid->bg_scan_period;
6fc6879b
JM
1382 for (i = 0; i < NUM_WEP_KEYS; i++) {
1383 if (ssid->wep_key_len[i])
1384 params.wep_key[i] = ssid->wep_key[i];
1385 params.wep_key_len[i] = ssid->wep_key_len[i];
1386 }
1387 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1388
c2a04078 1389 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
6fc6879b
JM
1390 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1391 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1392 params.passphrase = ssid->passphrase;
1393 if (ssid->psk_set)
1394 params.psk = ssid->psk;
1395 }
1396
36b15723
JM
1397 params.drop_unencrypted = use_crypt;
1398
6fc6879b 1399#ifdef CONFIG_IEEE80211W
70f8cc8e
JM
1400 params.mgmt_frame_protection = ssid->ieee80211w;
1401 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
6fa81a3b 1402 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
97d3497e
JM
1403 struct wpa_ie_data ie;
1404 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1405 ie.capabilities &
1406 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
f049052b
BG
1407 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1408 "MFP: require MFP");
97d3497e
JM
1409 params.mgmt_frame_protection =
1410 MGMT_FRAME_PROTECTION_REQUIRED;
1411 }
1412 }
6fc6879b
JM
1413#endif /* CONFIG_IEEE80211W */
1414
ffad8858 1415 params.p2p = ssid->p2p_group;
6e3f4b89 1416
eea2fd9e
JM
1417 if (wpa_s->parent->set_sta_uapsd)
1418 params.uapsd = wpa_s->parent->sta_uapsd;
1419 else
1420 params.uapsd = -1;
1421
80e8a5ee
BG
1422#ifdef CONFIG_HT_OVERRIDES
1423 os_memset(&htcaps, 0, sizeof(htcaps));
1424 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1425 params.htcaps = (u8 *) &htcaps;
1426 params.htcaps_mask = (u8 *) &htcaps_mask;
1427 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1428#endif /* CONFIG_HT_OVERRIDES */
1429
17fbb751 1430 ret = wpa_drv_associate(wpa_s, &params);
6fc6879b
JM
1431 if (ret < 0) {
1432 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1433 "failed");
871f4dd0
JM
1434 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1435 /*
1436 * The driver is known to mean what is saying, so we
1437 * can stop right here; the association will not
1438 * succeed.
1439 */
1440 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1441 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1442 return;
1443 }
6fc6879b
JM
1444 /* try to continue anyway; new association will be tried again
1445 * after timeout */
1446 assoc_failed = 1;
1447 }
1448
1449 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1450 /* Set the key after the association just in case association
1451 * cleared the previously configured key. */
1452 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1453 /* No need to timeout authentication since there is no key
1454 * management. */
1455 wpa_supplicant_cancel_auth_timeout(wpa_s);
1456 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
53895c3b 1457#ifdef CONFIG_IBSS_RSN
d7dcba70 1458 } else if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
1459 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1460 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1461 /*
1462 * RSN IBSS authentication is per-STA and we can disable the
1463 * per-BSSID authentication.
1464 */
1465 wpa_supplicant_cancel_auth_timeout(wpa_s);
53895c3b 1466#endif /* CONFIG_IBSS_RSN */
6fc6879b
JM
1467 } else {
1468 /* Timeout for IEEE 802.11 authentication and association */
1d3c75b3
DW
1469 int timeout = 60;
1470
1471 if (assoc_failed) {
1472 /* give IBSS a bit more time */
d7dcba70 1473 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1d3c75b3
DW
1474 } else if (wpa_s->conf->ap_scan == 1) {
1475 /* give IBSS a bit more time */
d7dcba70 1476 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1d3c75b3 1477 }
6fc6879b
JM
1478 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1479 }
1480
1481 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1482 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1483 /* Set static WEP keys again */
1484 wpa_set_wep_keys(wpa_s, ssid);
1485 }
1486
1487 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1488 /*
1489 * Do not allow EAP session resumption between different
1490 * network configurations.
1491 */
1492 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1493 }
8bac466b 1494 old_ssid = wpa_s->current_ssid;
6fc6879b 1495 wpa_s->current_ssid = ssid;
8f770587 1496 wpa_s->current_bss = bss;
6fc6879b
JM
1497 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1498 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
1499 if (old_ssid != wpa_s->current_ssid)
1500 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
1501}
1502
1503
09f58c09
JM
1504static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1505 const u8 *addr)
1506{
1507 struct wpa_ssid *old_ssid;
1508
1509 wpa_clear_keys(wpa_s, addr);
1510 wpa_supplicant_mark_disassoc(wpa_s);
1511 old_ssid = wpa_s->current_ssid;
1512 wpa_s->current_ssid = NULL;
1513 wpa_s->current_bss = NULL;
1514 wpa_sm_set_config(wpa_s->wpa, NULL);
1515 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1516 if (old_ssid != wpa_s->current_ssid)
1517 wpas_notify_network_changed(wpa_s);
1518 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1519}
1520
1521
6fc6879b
JM
1522/**
1523 * wpa_supplicant_disassociate - Disassociate the current connection
1524 * @wpa_s: Pointer to wpa_supplicant data
1525 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1526 *
1527 * This function is used to request %wpa_supplicant to disassociate with the
1528 * current AP.
1529 */
1530void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1531 int reason_code)
1532{
1533 u8 *addr = NULL;
ef48ff94 1534 union wpa_event_data event;
8bac466b 1535
a8e16edc 1536 if (!is_zero_ether_addr(wpa_s->bssid)) {
17fbb751 1537 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
6fc6879b 1538 addr = wpa_s->bssid;
ef48ff94
JM
1539 os_memset(&event, 0, sizeof(event));
1540 event.disassoc_info.reason_code = (u16) reason_code;
1541 event.disassoc_info.locally_generated = 1;
1542 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
6fc6879b 1543 }
09f58c09
JM
1544
1545 wpa_supplicant_clear_connection(wpa_s, addr);
6fc6879b
JM
1546}
1547
1548
1549/**
1550 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1551 * @wpa_s: Pointer to wpa_supplicant data
1552 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1553 *
073ab58f 1554 * This function is used to request %wpa_supplicant to deauthenticate from the
6fc6879b
JM
1555 * current AP.
1556 */
1557void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1558 int reason_code)
1559{
1560 u8 *addr = NULL;
ef48ff94 1561 union wpa_event_data event;
8bac466b 1562
a8e16edc 1563 if (!is_zero_ether_addr(wpa_s->bssid)) {
17fbb751 1564 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
6fc6879b 1565 addr = wpa_s->bssid;
ef48ff94
JM
1566 os_memset(&event, 0, sizeof(event));
1567 event.deauth_info.reason_code = (u16) reason_code;
1568 event.deauth_info.locally_generated = 1;
1569 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
6fc6879b 1570 }
09f58c09
JM
1571
1572 wpa_supplicant_clear_connection(wpa_s, addr);
6fc6879b
JM
1573}
1574
1575
86b89452
WS
1576/**
1577 * wpa_supplicant_enable_network - Mark a configured network as enabled
1578 * @wpa_s: wpa_supplicant structure for a network interface
1579 * @ssid: wpa_ssid structure for a configured network or %NULL
1580 *
1581 * Enables the specified network or all networks if no network specified.
1582 */
1583void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1584 struct wpa_ssid *ssid)
1585{
1586 struct wpa_ssid *other_ssid;
1587 int was_disabled;
1588
1589 if (ssid == NULL) {
4dac0245
JM
1590 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1591 other_ssid = other_ssid->next) {
1592 if (other_ssid->disabled == 2)
1593 continue; /* do not change persistent P2P group
1594 * data */
86b89452
WS
1595 if (other_ssid == wpa_s->current_ssid &&
1596 other_ssid->disabled)
1597 wpa_s->reassociate = 1;
1598
1599 was_disabled = other_ssid->disabled;
1600
1601 other_ssid->disabled = 0;
1602
1603 if (was_disabled != other_ssid->disabled)
1604 wpas_notify_network_enabled_changed(
1605 wpa_s, other_ssid);
86b89452
WS
1606 }
1607 if (wpa_s->reassociate)
1608 wpa_supplicant_req_scan(wpa_s, 0, 0);
4dac0245 1609 } else if (ssid->disabled && ssid->disabled != 2) {
adc8d4a7
KM
1610 if (wpa_s->current_ssid == NULL) {
1611 /*
1612 * Try to reassociate since there is no current
1613 * configuration and a new network was made available.
1614 */
1615 wpa_s->reassociate = 1;
1616 wpa_supplicant_req_scan(wpa_s, 0, 0);
1617 }
86b89452
WS
1618
1619 was_disabled = ssid->disabled;
1620
1621 ssid->disabled = 0;
1622
1623 if (was_disabled != ssid->disabled)
1624 wpas_notify_network_enabled_changed(wpa_s, ssid);
1625 }
1626}
1627
1628
1629/**
1630 * wpa_supplicant_disable_network - Mark a configured network as disabled
1631 * @wpa_s: wpa_supplicant structure for a network interface
1632 * @ssid: wpa_ssid structure for a configured network or %NULL
1633 *
1634 * Disables the specified network or all networks if no network specified.
1635 */
1636void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1637 struct wpa_ssid *ssid)
1638{
1639 struct wpa_ssid *other_ssid;
1640 int was_disabled;
1641
1642 if (ssid == NULL) {
4dac0245
JM
1643 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1644 other_ssid = other_ssid->next) {
86b89452 1645 was_disabled = other_ssid->disabled;
4dac0245
JM
1646 if (was_disabled == 2)
1647 continue; /* do not change persistent P2P group
1648 * data */
86b89452
WS
1649
1650 other_ssid->disabled = 1;
1651
1652 if (was_disabled != other_ssid->disabled)
1653 wpas_notify_network_enabled_changed(
1654 wpa_s, other_ssid);
86b89452
WS
1655 }
1656 if (wpa_s->current_ssid)
1657 wpa_supplicant_disassociate(
1658 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4dac0245 1659 } else if (ssid->disabled != 2) {
86b89452
WS
1660 if (ssid == wpa_s->current_ssid)
1661 wpa_supplicant_disassociate(
1662 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1663
1664 was_disabled = ssid->disabled;
1665
1666 ssid->disabled = 1;
1667
1668 if (was_disabled != ssid->disabled)
1669 wpas_notify_network_enabled_changed(wpa_s, ssid);
1670 }
1671}
1672
1673
1674/**
1675 * wpa_supplicant_select_network - Attempt association with a network
1676 * @wpa_s: wpa_supplicant structure for a network interface
1677 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1678 */
1679void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1680 struct wpa_ssid *ssid)
1681{
1682
1683 struct wpa_ssid *other_ssid;
d93dfbd5 1684 int disconnected = 0;
86b89452 1685
d93dfbd5 1686 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
86b89452
WS
1687 wpa_supplicant_disassociate(
1688 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
d93dfbd5
JM
1689 disconnected = 1;
1690 }
86b89452
WS
1691
1692 /*
1693 * Mark all other networks disabled or mark all networks enabled if no
1694 * network specified.
1695 */
4dac0245
JM
1696 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1697 other_ssid = other_ssid->next) {
86b89452 1698 int was_disabled = other_ssid->disabled;
4dac0245
JM
1699 if (was_disabled == 2)
1700 continue; /* do not change persistent P2P group data */
86b89452
WS
1701
1702 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1703
1704 if (was_disabled != other_ssid->disabled)
1705 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
86b89452 1706 }
2a6f78fb
JJ
1707
1708 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1709 /* We are already associated with the selected network */
1710 wpa_printf(MSG_DEBUG, "Already associated with the "
1711 "selected network - do nothing");
1712 return;
1713 }
1714
96efeeb6
JM
1715 if (ssid)
1716 wpa_s->current_ssid = ssid;
7dcdcfd6 1717 wpa_s->connect_without_scan = NULL;
86b89452
WS
1718 wpa_s->disconnected = 0;
1719 wpa_s->reassociate = 1;
d93dfbd5 1720 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
86b89452 1721
a1641d26
JM
1722 if (ssid)
1723 wpas_notify_network_selected(wpa_s, ssid);
86b89452
WS
1724}
1725
1726
1727/**
1728 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1729 * @wpa_s: wpa_supplicant structure for a network interface
1730 * @ap_scan: AP scan mode
1731 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1732 *
1733 */
1734int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1735{
1736
1737 int old_ap_scan;
1738
1739 if (ap_scan < 0 || ap_scan > 2)
1740 return -1;
1741
48f8e036
DS
1742#ifdef ANDROID
1743 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
1744 wpa_s->wpa_state >= WPA_ASSOCIATING &&
1745 wpa_s->wpa_state < WPA_COMPLETED) {
1746 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
1747 "associating", wpa_s->conf->ap_scan, ap_scan);
1748 return 0;
1749 }
1750#endif /* ANDROID */
1751
86b89452
WS
1752 old_ap_scan = wpa_s->conf->ap_scan;
1753 wpa_s->conf->ap_scan = ap_scan;
1754
1755 if (old_ap_scan != wpa_s->conf->ap_scan)
1756 wpas_notify_ap_scan_changed(wpa_s);
1757
1758 return 0;
1759}
1760
1761
78633c37
SL
1762/**
1763 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1764 * @wpa_s: wpa_supplicant structure for a network interface
1765 * @expire_age: Expiration age in seconds
1766 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1767 *
1768 */
1769int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1770 unsigned int bss_expire_age)
1771{
1772 if (bss_expire_age < 10) {
1773 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1774 bss_expire_age);
1775 return -1;
1776 }
1777 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1778 bss_expire_age);
1779 wpa_s->conf->bss_expiration_age = bss_expire_age;
1780
1781 return 0;
1782}
1783
1784
1785/**
1786 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1787 * @wpa_s: wpa_supplicant structure for a network interface
1788 * @expire_count: number of scans after which an unseen BSS is reclaimed
1789 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1790 *
1791 */
1792int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1793 unsigned int bss_expire_count)
1794{
1795 if (bss_expire_count < 1) {
1796 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1797 bss_expire_count);
1798 return -1;
1799 }
1800 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1801 bss_expire_count);
1802 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1803
1804 return 0;
1805}
1806
1807
c6e86b63
MA
1808/**
1809 * wpa_supplicant_set_scan_interval - Set scan interval
1810 * @wpa_s: wpa_supplicant structure for a network interface
1811 * @scan_interval: scan interval in seconds
1812 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
1813 *
1814 */
1815int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
1816 int scan_interval)
1817{
1818 if (scan_interval < 0) {
1819 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
1820 scan_interval);
1821 return -1;
1822 }
1823 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
1824 scan_interval);
1825 wpa_s->scan_interval = scan_interval;
1826
1827 return 0;
1828}
1829
1830
86b89452
WS
1831/**
1832 * wpa_supplicant_set_debug_params - Set global debug params
1833 * @global: wpa_global structure
1834 * @debug_level: debug level
1835 * @debug_timestamp: determines if show timestamp in debug data
1836 * @debug_show_keys: determines if show keys in debug data
1837 * Returns: 0 if succeed or -1 if debug_level has wrong value
1838 */
1839int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1840 int debug_timestamp, int debug_show_keys)
1841{
1842
1843 int old_level, old_timestamp, old_show_keys;
1844
1845 /* check for allowed debuglevels */
14dc0011
PS
1846 if (debug_level != MSG_EXCESSIVE &&
1847 debug_level != MSG_MSGDUMP &&
86b89452
WS
1848 debug_level != MSG_DEBUG &&
1849 debug_level != MSG_INFO &&
1850 debug_level != MSG_WARNING &&
1851 debug_level != MSG_ERROR)
1852 return -1;
1853
1854 old_level = wpa_debug_level;
1855 old_timestamp = wpa_debug_timestamp;
1856 old_show_keys = wpa_debug_show_keys;
1857
1858 wpa_debug_level = debug_level;
1859 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1860 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1861
db9133ac
WS
1862 if (wpa_debug_level != old_level)
1863 wpas_notify_debug_level_changed(global);
1864 if (wpa_debug_timestamp != old_timestamp)
1865 wpas_notify_debug_timestamp_changed(global);
1866 if (wpa_debug_show_keys != old_show_keys)
1867 wpas_notify_debug_show_keys_changed(global);
86b89452
WS
1868
1869 return 0;
1870}
1871
1872
6fc6879b
JM
1873/**
1874 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1875 * @wpa_s: Pointer to wpa_supplicant data
1876 * Returns: A pointer to the current network structure or %NULL on failure
1877 */
1878struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1879{
1880 struct wpa_ssid *entry;
1881 u8 ssid[MAX_SSID_LEN];
1882 int res;
1883 size_t ssid_len;
1884 u8 bssid[ETH_ALEN];
1885 int wired;
1886
17fbb751
JM
1887 res = wpa_drv_get_ssid(wpa_s, ssid);
1888 if (res < 0) {
1889 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1890 "driver");
1891 return NULL;
6fc6879b 1892 }
17fbb751 1893 ssid_len = res;
6fc6879b 1894
17fbb751 1895 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
f049052b
BG
1896 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1897 "driver");
6fc6879b
JM
1898 return NULL;
1899 }
1900
c2a04078
JM
1901 wired = wpa_s->conf->ap_scan == 0 &&
1902 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
6fc6879b
JM
1903
1904 entry = wpa_s->conf->ssid;
1905 while (entry) {
349493bd 1906 if (!wpas_network_disabled(wpa_s, entry) &&
6fc6879b
JM
1907 ((ssid_len == entry->ssid_len &&
1908 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1909 (!entry->bssid_set ||
1910 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1911 return entry;
24c23d1b 1912#ifdef CONFIG_WPS
349493bd 1913 if (!wpas_network_disabled(wpa_s, entry) &&
24c23d1b
JM
1914 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1915 (entry->ssid == NULL || entry->ssid_len == 0) &&
1916 (!entry->bssid_set ||
1917 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1918 return entry;
1919#endif /* CONFIG_WPS */
7d232e23 1920
349493bd 1921 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
7d232e23
ZC
1922 entry->ssid_len == 0 &&
1923 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
1924 return entry;
1925
6fc6879b
JM
1926 entry = entry->next;
1927 }
1928
1929 return NULL;
1930}
1931
1932
7756114f
JM
1933static int select_driver(struct wpa_supplicant *wpa_s, int i)
1934{
1935 struct wpa_global *global = wpa_s->global;
1936
1937 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
1938 global->drv_priv[i] = wpa_drivers[i]->global_init();
1939 if (global->drv_priv[i] == NULL) {
1940 wpa_printf(MSG_ERROR, "Failed to initialize driver "
1941 "'%s'", wpa_drivers[i]->name);
1942 return -1;
1943 }
1944 }
1945
1946 wpa_s->driver = wpa_drivers[i];
1947 wpa_s->global_drv_priv = global->drv_priv[i];
1948
1949 return 0;
1950}
1951
1952
6fc6879b
JM
1953static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1954 const char *name)
1955{
1956 int i;
362f781e 1957 size_t len;
74b1c84a 1958 const char *pos, *driver = name;
6fc6879b
JM
1959
1960 if (wpa_s == NULL)
1961 return -1;
1962
c5121837 1963 if (wpa_drivers[0] == NULL) {
f049052b
BG
1964 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1965 "wpa_supplicant");
6fc6879b
JM
1966 return -1;
1967 }
1968
1969 if (name == NULL) {
1970 /* default to first driver in the list */
7756114f 1971 return select_driver(wpa_s, 0);
6fc6879b
JM
1972 }
1973
74b1c84a
SO
1974 do {
1975 pos = os_strchr(driver, ',');
1976 if (pos)
1977 len = pos - driver;
1978 else
1979 len = os_strlen(driver);
1980
1981 for (i = 0; wpa_drivers[i]; i++) {
1982 if (os_strlen(wpa_drivers[i]->name) == len &&
1983 os_strncmp(driver, wpa_drivers[i]->name, len) ==
0f4668ce
DW
1984 0) {
1985 /* First driver that succeeds wins */
1986 if (select_driver(wpa_s, i) == 0)
1987 return 0;
1988 }
6fc6879b 1989 }
74b1c84a
SO
1990
1991 driver = pos + 1;
1992 } while (pos);
6fc6879b 1993
f049052b 1994 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
6fc6879b
JM
1995 return -1;
1996}
1997
1998
a8e0505b
JM
1999/**
2000 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2001 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2002 * with struct wpa_driver_ops::init()
2003 * @src_addr: Source address of the EAPOL frame
2004 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2005 * @len: Length of the EAPOL data
2006 *
2007 * This function is called for each received EAPOL frame. Most driver
2008 * interfaces rely on more generic OS mechanism for receiving frames through
2009 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2010 * take care of received EAPOL frames and deliver them to the core supplicant
2011 * code by calling this function.
2012 */
6fc6879b
JM
2013void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2014 const u8 *buf, size_t len)
2015{
2016 struct wpa_supplicant *wpa_s = ctx;
2017
f049052b 2018 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
6fc6879b
JM
2019 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2020
1ff73338
JM
2021 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
2022 /*
2023 * There is possible race condition between receiving the
2024 * association event and the EAPOL frame since they are coming
2025 * through different paths from the driver. In order to avoid
2026 * issues in trying to process the EAPOL frame before receiving
2027 * association information, lets queue it for processing until
2028 * the association event is received.
2029 */
f049052b
BG
2030 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
2031 "of received EAPOL frame");
1ff73338
JM
2032 wpabuf_free(wpa_s->pending_eapol_rx);
2033 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2034 if (wpa_s->pending_eapol_rx) {
2035 os_get_time(&wpa_s->pending_eapol_rx_time);
2036 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2037 ETH_ALEN);
2038 }
2039 return;
2040 }
2041
db149ac9
JM
2042#ifdef CONFIG_AP
2043 if (wpa_s->ap_iface) {
2044 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2045 return;
2046 }
2047#endif /* CONFIG_AP */
2048
6fc6879b 2049 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
f049052b
BG
2050 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2051 "no key management is configured");
6fc6879b
JM
2052 return;
2053 }
2054
2055 if (wpa_s->eapol_received == 0 &&
c2a04078 2056 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
56586197 2057 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
9c972abb
JM
2058 wpa_s->wpa_state != WPA_COMPLETED) &&
2059 (wpa_s->current_ssid == NULL ||
2060 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
6fc6879b
JM
2061 /* Timeout for completing IEEE 802.1X and WPA authentication */
2062 wpa_supplicant_req_auth_timeout(
2063 wpa_s,
56586197 2064 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
a6f06dab
AT
2065 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2066 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
6fc6879b
JM
2067 70 : 10, 0);
2068 }
2069 wpa_s->eapol_received++;
2070
2071 if (wpa_s->countermeasures) {
f049052b
BG
2072 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2073 "EAPOL packet");
6fc6879b
JM
2074 return;
2075 }
2076
8be18440
JM
2077#ifdef CONFIG_IBSS_RSN
2078 if (wpa_s->current_ssid &&
d7dcba70 2079 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
8be18440
JM
2080 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2081 return;
2082 }
2083#endif /* CONFIG_IBSS_RSN */
2084
6fc6879b
JM
2085 /* Source address of the incoming EAPOL frame could be compared to the
2086 * current BSSID. However, it is possible that a centralized
2087 * Authenticator could be using another MAC address than the BSSID of
2088 * an AP, so just allow any address to be used for now. The replies are
2089 * still sent to the current BSSID (if available), though. */
2090
2091 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
56586197 2092 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
6fc6879b
JM
2093 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2094 return;
2095 wpa_drv_poll(wpa_s);
c2a04078 2096 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
6fc6879b 2097 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
56586197 2098 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6fc6879b
JM
2099 /*
2100 * Set portValid = TRUE here since we are going to skip 4-way
2101 * handshake processing which would normally set portValid. We
2102 * need this to allow the EAPOL state machines to be completed
2103 * without going through EAPOL-Key handshake.
2104 */
2105 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2106 }
2107}
2108
2109
bfba8deb 2110int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
6fc6879b 2111{
6fc6879b
JM
2112 if (wpa_s->driver->send_eapol) {
2113 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2114 if (addr)
2115 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
fdadd5fe
JM
2116 } else if (!(wpa_s->drv_flags &
2117 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
bfba8deb 2118 l2_packet_deinit(wpa_s->l2);
6fc6879b
JM
2119 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2120 wpa_drv_get_mac_addr(wpa_s),
2121 ETH_P_EAPOL,
2122 wpa_supplicant_rx_eapol, wpa_s, 0);
2123 if (wpa_s->l2 == NULL)
2124 return -1;
fdadd5fe
JM
2125 } else {
2126 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2127 if (addr)
2128 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
6fc6879b
JM
2129 }
2130
2131 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
f049052b 2132 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
6fc6879b
JM
2133 return -1;
2134 }
2135
f049052b
BG
2136 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2137 MAC2STR(wpa_s->own_addr));
f98eb880 2138 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
6fc6879b 2139
bfba8deb
JM
2140 return 0;
2141}
2142
2143
25f839c6
JM
2144static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2145 const u8 *buf, size_t len)
2146{
2147 struct wpa_supplicant *wpa_s = ctx;
2148 const struct l2_ethhdr *eth;
2149
2150 if (len < sizeof(*eth))
2151 return;
2152 eth = (const struct l2_ethhdr *) buf;
2153
2154 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2155 !(eth->h_dest[0] & 0x01)) {
2156 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2157 " (bridge - not for this interface - ignore)",
2158 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2159 return;
2160 }
2161
2162 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2163 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2164 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2165 len - sizeof(*eth));
2166}
2167
2168
bfba8deb
JM
2169/**
2170 * wpa_supplicant_driver_init - Initialize driver interface parameters
2171 * @wpa_s: Pointer to wpa_supplicant data
2172 * Returns: 0 on success, -1 on failure
2173 *
2174 * This function is called to initialize driver interface parameters.
2175 * wpa_drv_init() must have been called before this function to initialize the
2176 * driver interface.
2177 */
2178int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2179{
2180 static int interface_count = 0;
2181
2182 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2183 return -1;
2184
6fc6879b 2185 if (wpa_s->bridge_ifname[0]) {
f049052b
BG
2186 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2187 "interface '%s'", wpa_s->bridge_ifname);
6fc6879b
JM
2188 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2189 wpa_s->own_addr,
2190 ETH_P_EAPOL,
25f839c6
JM
2191 wpa_supplicant_rx_eapol_bridge,
2192 wpa_s, 1);
6fc6879b 2193 if (wpa_s->l2_br == NULL) {
f049052b
BG
2194 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2195 "connection for the bridge interface '%s'",
2196 wpa_s->bridge_ifname);
6fc6879b
JM
2197 return -1;
2198 }
2199 }
2200
6fc6879b
JM
2201 wpa_clear_keys(wpa_s, NULL);
2202
2203 /* Make sure that TKIP countermeasures are not left enabled (could
2204 * happen if wpa_supplicant is killed during countermeasures. */
2205 wpa_drv_set_countermeasures(wpa_s, 0);
2206
f049052b 2207 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
6fc6879b
JM
2208 wpa_drv_flush_pmkid(wpa_s);
2209
ba2a573c 2210 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
b3aa456b
ES
2211 wpa_s->prev_scan_wildcard = 0;
2212
349493bd 2213 if (wpa_supplicant_enabled_networks(wpa_s)) {
6a90053c
LC
2214 if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
2215 100000))
a4cba8f1
LC
2216 wpa_supplicant_req_scan(wpa_s, interface_count,
2217 100000);
74e259ec
JM
2218 interface_count++;
2219 } else
2220 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
6fc6879b
JM
2221
2222 return 0;
2223}
2224
2225
2226static int wpa_supplicant_daemon(const char *pid_file)
2227{
2228 wpa_printf(MSG_DEBUG, "Daemonize..");
2229 return os_daemonize(pid_file);
2230}
2231
2232
2233static struct wpa_supplicant * wpa_supplicant_alloc(void)
2234{
2235 struct wpa_supplicant *wpa_s;
2236
2237 wpa_s = os_zalloc(sizeof(*wpa_s));
2238 if (wpa_s == NULL)
2239 return NULL;
2240 wpa_s->scan_req = 1;
67b9bd08 2241 wpa_s->scan_interval = 5;
c302f207 2242 wpa_s->new_connection = 1;
b22128ef 2243 wpa_s->parent = wpa_s;
cbdf3507 2244 wpa_s->sched_scanning = 0;
6fc6879b
JM
2245
2246 return wpa_s;
2247}
2248
2249
80e8a5ee
BG
2250#ifdef CONFIG_HT_OVERRIDES
2251
2252static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2253 struct ieee80211_ht_capabilities *htcaps,
2254 struct ieee80211_ht_capabilities *htcaps_mask,
2255 const char *ht_mcs)
2256{
2257 /* parse ht_mcs into hex array */
2258 int i;
2259 const char *tmp = ht_mcs;
2260 char *end = NULL;
2261
2262 /* If ht_mcs is null, do not set anything */
2263 if (!ht_mcs)
2264 return 0;
2265
2266 /* This is what we are setting in the kernel */
2267 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2268
2269 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2270
2271 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2272 errno = 0;
2273 long v = strtol(tmp, &end, 16);
2274 if (errno == 0) {
2275 wpa_msg(wpa_s, MSG_DEBUG,
2276 "htcap value[%i]: %ld end: %p tmp: %p",
2277 i, v, end, tmp);
2278 if (end == tmp)
2279 break;
2280
2281 htcaps->supported_mcs_set[i] = v;
2282 tmp = end;
2283 } else {
2284 wpa_msg(wpa_s, MSG_ERROR,
2285 "Failed to parse ht-mcs: %s, error: %s\n",
2286 ht_mcs, strerror(errno));
2287 return -1;
2288 }
2289 }
2290
2291 /*
2292 * If we were able to parse any values, then set mask for the MCS set.
2293 */
2294 if (i) {
2295 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2296 IEEE80211_HT_MCS_MASK_LEN - 1);
2297 /* skip the 3 reserved bits */
2298 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2299 0x1f;
2300 }
2301
2302 return 0;
2303}
2304
2305
2306static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2307 struct ieee80211_ht_capabilities *htcaps,
2308 struct ieee80211_ht_capabilities *htcaps_mask,
2309 int disabled)
2310{
2311 u16 msk;
2312
2313 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2314
2315 if (disabled == -1)
2316 return 0;
2317
2318 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2319 htcaps_mask->ht_capabilities_info |= msk;
2320 if (disabled)
2321 htcaps->ht_capabilities_info &= msk;
2322 else
2323 htcaps->ht_capabilities_info |= msk;
2324
2325 return 0;
2326}
2327
2328
2329static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2330 struct ieee80211_ht_capabilities *htcaps,
2331 struct ieee80211_ht_capabilities *htcaps_mask,
2332 int factor)
2333{
2334 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2335
2336 if (factor == -1)
2337 return 0;
2338
2339 if (factor < 0 || factor > 3) {
2340 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2341 "Must be 0-3 or -1", factor);
2342 return -EINVAL;
2343 }
2344
2345 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2346 htcaps->a_mpdu_params &= ~0x3;
2347 htcaps->a_mpdu_params |= factor & 0x3;
2348
2349 return 0;
2350}
2351
2352
2353static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2354 struct ieee80211_ht_capabilities *htcaps,
2355 struct ieee80211_ht_capabilities *htcaps_mask,
2356 int density)
2357{
2358 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2359
2360 if (density == -1)
2361 return 0;
2362
2363 if (density < 0 || density > 7) {
2364 wpa_msg(wpa_s, MSG_ERROR,
2365 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2366 density);
2367 return -EINVAL;
2368 }
2369
2370 htcaps_mask->a_mpdu_params |= 0x1C;
2371 htcaps->a_mpdu_params &= ~(0x1C);
2372 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2373
2374 return 0;
2375}
2376
2377
2378static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2379 struct ieee80211_ht_capabilities *htcaps,
2380 struct ieee80211_ht_capabilities *htcaps_mask,
2381 int disabled)
2382{
2383 /* Masking these out disables HT40 */
2384 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2385 HT_CAP_INFO_SHORT_GI40MHZ);
2386
2387 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2388
2389 if (disabled)
2390 htcaps->ht_capabilities_info &= ~msk;
2391 else
2392 htcaps->ht_capabilities_info |= msk;
2393
2394 htcaps_mask->ht_capabilities_info |= msk;
2395
2396 return 0;
2397}
2398
2399
2400void wpa_supplicant_apply_ht_overrides(
2401 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2402 struct wpa_driver_associate_params *params)
2403{
2404 struct ieee80211_ht_capabilities *htcaps;
2405 struct ieee80211_ht_capabilities *htcaps_mask;
2406
2407 if (!ssid)
2408 return;
2409
2410 params->disable_ht = ssid->disable_ht;
2411 if (!params->htcaps || !params->htcaps_mask)
2412 return;
2413
2414 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2415 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2416 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2417 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2418 ssid->disable_max_amsdu);
2419 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2420 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2421 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
2422}
2423
2424#endif /* CONFIG_HT_OVERRIDES */
2425
2426
f64adcd7
JM
2427static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
2428{
2429#ifdef PCSC_FUNCS
2430 size_t len;
2431
2432 if (!wpa_s->conf->pcsc_reader)
2433 return 0;
2434
2435 wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
2436 if (!wpa_s->scard)
2437 return 1;
2438
2439 if (wpa_s->conf->pcsc_pin &&
2440 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
2441 scard_deinit(wpa_s->scard);
2442 wpa_s->scard = NULL;
2443 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
2444 return -1;
2445 }
2446
2447 len = sizeof(wpa_s->imsi) - 1;
2448 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
2449 scard_deinit(wpa_s->scard);
2450 wpa_s->scard = NULL;
2451 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
2452 return -1;
2453 }
2454 wpa_s->imsi[len] = '\0';
2455
2456 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
2457
2458 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
2459 wpa_s->imsi, wpa_s->mnc_len);
2460
2461 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
2462 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
2463#endif /* PCSC_FUNCS */
2464
2465 return 0;
2466}
2467
2468
6fc6879b
JM
2469static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2470 struct wpa_interface *iface)
2471{
362f781e
JM
2472 const char *ifname, *driver;
2473 struct wpa_driver_capa capa;
2474
6fc6879b
JM
2475 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2476 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2477 iface->confname ? iface->confname : "N/A",
2478 iface->driver ? iface->driver : "default",
2479 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2480 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2481
6fc6879b
JM
2482 if (iface->confname) {
2483#ifdef CONFIG_BACKEND_FILE
2484 wpa_s->confname = os_rel2abs_path(iface->confname);
2485 if (wpa_s->confname == NULL) {
2486 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2487 "for configuration file '%s'.",
2488 iface->confname);
2489 return -1;
2490 }
2491 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2492 iface->confname, wpa_s->confname);
2493#else /* CONFIG_BACKEND_FILE */
2494 wpa_s->confname = os_strdup(iface->confname);
2495#endif /* CONFIG_BACKEND_FILE */
2496 wpa_s->conf = wpa_config_read(wpa_s->confname);
2497 if (wpa_s->conf == NULL) {
2498 wpa_printf(MSG_ERROR, "Failed to read or parse "
2499 "configuration '%s'.", wpa_s->confname);
2500 return -1;
2501 }
2502
2503 /*
2504 * Override ctrl_interface and driver_param if set on command
2505 * line.
2506 */
2507 if (iface->ctrl_interface) {
2508 os_free(wpa_s->conf->ctrl_interface);
2509 wpa_s->conf->ctrl_interface =
2510 os_strdup(iface->ctrl_interface);
2511 }
2512
2513 if (iface->driver_param) {
2514 os_free(wpa_s->conf->driver_param);
2515 wpa_s->conf->driver_param =
2516 os_strdup(iface->driver_param);
2517 }
2518 } else
2519 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2520 iface->driver_param);
2521
2522 if (wpa_s->conf == NULL) {
2523 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2524 return -1;
2525 }
2526
2527 if (iface->ifname == NULL) {
2528 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2529 return -1;
2530 }
2531 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2532 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2533 iface->ifname);
2534 return -1;
2535 }
2536 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2537
2538 if (iface->bridge_ifname) {
2539 if (os_strlen(iface->bridge_ifname) >=
2540 sizeof(wpa_s->bridge_ifname)) {
2541 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2542 "name '%s'.", iface->bridge_ifname);
2543 return -1;
2544 }
2545 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2546 sizeof(wpa_s->bridge_ifname));
2547 }
2548
6fc6879b
JM
2549 /* RSNA Supplicant Key Management - INITIALIZE */
2550 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2551 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2552
2553 /* Initialize driver interface and register driver event handler before
2554 * L2 receive handler so that association events are processed before
2555 * EAPOL-Key packets if both become available for the same select()
2556 * call. */
362f781e
JM
2557 driver = iface->driver;
2558next_driver:
2559 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2560 return -1;
2561
6fc6879b
JM
2562 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2563 if (wpa_s->drv_priv == NULL) {
362f781e 2564 const char *pos;
a5b9337f 2565 pos = driver ? os_strchr(driver, ',') : NULL;
362f781e 2566 if (pos) {
f049052b
BG
2567 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2568 "driver interface - try next driver wrapper");
362f781e
JM
2569 driver = pos + 1;
2570 goto next_driver;
2571 }
f049052b
BG
2572 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2573 "interface");
6fc6879b
JM
2574 return -1;
2575 }
2576 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
f049052b
BG
2577 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2578 "driver_param '%s'", wpa_s->conf->driver_param);
6fc6879b
JM
2579 return -1;
2580 }
2581
2582 ifname = wpa_drv_get_ifname(wpa_s);
2583 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
f049052b
BG
2584 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2585 "interface name with '%s'", ifname);
6fc6879b
JM
2586 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2587 }
2588
2589 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2590 return -1;
2591
2592 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2593 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2594 NULL);
2595 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2596
2597 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2598 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2599 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
f049052b
BG
2600 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2601 "dot11RSNAConfigPMKLifetime");
6fc6879b
JM
2602 return -1;
2603 }
2604
2605 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2606 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2607 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
f049052b 2608 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
6fc6879b
JM
2609 "dot11RSNAConfigPMKReauthThreshold");
2610 return -1;
2611 }
2612
2613 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2614 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2615 wpa_s->conf->dot11RSNAConfigSATimeout)) {
f049052b
BG
2616 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2617 "dot11RSNAConfigSATimeout");
6fc6879b
JM
2618 return -1;
2619 }
2620
6bf731e8
CL
2621 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
2622 &wpa_s->hw.num_modes,
2623 &wpa_s->hw.flags);
2624
814782b9 2625 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
c58ab8f2 2626 wpa_s->drv_capa_known = 1;
814782b9 2627 wpa_s->drv_flags = capa.flags;
349493bd 2628 wpa_s->drv_enc = capa.enc;
4f73d88a 2629 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
814782b9 2630 wpa_s->max_scan_ssids = capa.max_scan_ssids;
cbdf3507
LC
2631 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2632 wpa_s->sched_scan_supported = capa.sched_scan_supported;
b59e6f26 2633 wpa_s->max_match_sets = capa.max_match_sets;
814782b9 2634 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
c4ea4c5c 2635 wpa_s->max_stations = capa.max_stations;
814782b9
JM
2636 }
2637 if (wpa_s->max_remain_on_chan == 0)
2638 wpa_s->max_remain_on_chan = 1000;
2639
6fc6879b
JM
2640 if (wpa_supplicant_driver_init(wpa_s) < 0)
2641 return -1;
2642
281ff0aa
GP
2643#ifdef CONFIG_TDLS
2644 if (wpa_tdls_init(wpa_s->wpa))
2645 return -1;
2646#endif /* CONFIG_TDLS */
2647
315ce40a
JM
2648 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2649 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
f049052b 2650 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
6d158490
LR
2651 return -1;
2652 }
2653
116654ce
JM
2654 if (wpas_wps_init(wpa_s))
2655 return -1;
2656
6fc6879b
JM
2657 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2658 return -1;
2659 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2660
2661 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2662 if (wpa_s->ctrl_iface == NULL) {
2663 wpa_printf(MSG_ERROR,
2664 "Failed to initialize control interface '%s'.\n"
2665 "You may have another wpa_supplicant process "
2666 "already running or the file was\n"
2667 "left by an unclean termination of wpa_supplicant "
2668 "in which case you will need\n"
2669 "to manually remove this file before starting "
2670 "wpa_supplicant again.\n",
2671 wpa_s->conf->ctrl_interface);
2672 return -1;
2673 }
2674
04ea7b79
JM
2675 wpa_s->gas = gas_query_init(wpa_s);
2676 if (wpa_s->gas == NULL) {
2677 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2678 return -1;
2679 }
2680
b22128ef
JM
2681#ifdef CONFIG_P2P
2682 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
f049052b 2683 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
b22128ef
JM
2684 return -1;
2685 }
2686#endif /* CONFIG_P2P */
2687
83922c2d
JM
2688 if (wpa_bss_init(wpa_s) < 0)
2689 return -1;
83922c2d 2690
f64adcd7
JM
2691 if (pcsc_reader_init(wpa_s) < 0)
2692 return -1;
2693
6fc6879b
JM
2694 return 0;
2695}
2696
2697
2ee055b3 2698static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
df509539 2699 int notify, int terminate)
6fc6879b
JM
2700{
2701 if (wpa_s->drv_priv) {
2702 wpa_supplicant_deauthenticate(wpa_s,
2703 WLAN_REASON_DEAUTH_LEAVING);
2704
6fc6879b
JM
2705 wpa_drv_set_countermeasures(wpa_s, 0);
2706 wpa_clear_keys(wpa_s, NULL);
2707 }
2708
8e56d189
JM
2709 wpa_supplicant_cleanup(wpa_s);
2710
ab28911d
JM
2711#ifdef CONFIG_P2P
2712 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
2713 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
2714 "the management interface is being removed");
2715 wpas_p2p_deinit_global(wpa_s->global);
2716 }
2717#endif /* CONFIG_P2P */
2718
6fc6879b
JM
2719 if (wpa_s->drv_priv)
2720 wpa_drv_deinit(wpa_s);
2523ff6e
DS
2721
2722 if (notify)
2723 wpas_notify_iface_removed(wpa_s);
f0811516
DS
2724
2725 if (terminate)
2726 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
2727
2728 if (wpa_s->ctrl_iface) {
2729 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
2730 wpa_s->ctrl_iface = NULL;
2731 }
2732
2733 if (wpa_s->conf != NULL) {
2734 wpa_config_free(wpa_s->conf);
2735 wpa_s->conf = NULL;
2736 }
6fc6879b
JM
2737}
2738
2739
2740/**
2741 * wpa_supplicant_add_iface - Add a new network interface
2742 * @global: Pointer to global data from wpa_supplicant_init()
2743 * @iface: Interface configuration options
2744 * Returns: Pointer to the created interface or %NULL on failure
2745 *
2746 * This function is used to add new network interfaces for %wpa_supplicant.
2747 * This can be called before wpa_supplicant_run() to add interfaces before the
2748 * main event loop has been started. In addition, new interfaces can be added
2749 * dynamically while %wpa_supplicant is already running. This could happen,
2750 * e.g., when a hotplug network adapter is inserted.
2751 */
2752struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2753 struct wpa_interface *iface)
2754{
2755 struct wpa_supplicant *wpa_s;
d27df100 2756 struct wpa_interface t_iface;
8e56d189 2757 struct wpa_ssid *ssid;
6fc6879b
JM
2758
2759 if (global == NULL || iface == NULL)
2760 return NULL;
2761
2762 wpa_s = wpa_supplicant_alloc();
2763 if (wpa_s == NULL)
2764 return NULL;
2765
d8222ae3
JM
2766 wpa_s->global = global;
2767
d27df100
JM
2768 t_iface = *iface;
2769 if (global->params.override_driver) {
2770 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2771 "('%s' -> '%s')",
2772 iface->driver, global->params.override_driver);
2773 t_iface.driver = global->params.override_driver;
2774 }
2775 if (global->params.override_ctrl_interface) {
2776 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2777 "ctrl_interface ('%s' -> '%s')",
2778 iface->ctrl_interface,
2779 global->params.override_ctrl_interface);
2780 t_iface.ctrl_interface =
2781 global->params.override_ctrl_interface;
2782 }
2783 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
6fc6879b
JM
2784 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2785 iface->ifname);
df509539 2786 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
6fc6879b
JM
2787 os_free(wpa_s);
2788 return NULL;
2789 }
2790
dc461de4
WS
2791 /* Notify the control interfaces about new iface */
2792 if (wpas_notify_iface_added(wpa_s)) {
df509539 2793 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
6fc6879b
JM
2794 os_free(wpa_s);
2795 return NULL;
2796 }
1bd3f426 2797
8e56d189
JM
2798 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2799 wpas_notify_network_added(wpa_s, ssid);
2800
6fc6879b
JM
2801 wpa_s->next = global->ifaces;
2802 global->ifaces = wpa_s;
2803
f049052b 2804 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
6fc6879b
JM
2805
2806 return wpa_s;
2807}
2808
2809
2810/**
2811 * wpa_supplicant_remove_iface - Remove a network interface
2812 * @global: Pointer to global data from wpa_supplicant_init()
2813 * @wpa_s: Pointer to the network interface to be removed
2814 * Returns: 0 if interface was removed, -1 if interface was not found
2815 *
2816 * This function can be used to dynamically remove network interfaces from
2817 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2818 * addition, this function is used to remove all remaining interfaces when
2819 * %wpa_supplicant is terminated.
2820 */
2821int wpa_supplicant_remove_iface(struct wpa_global *global,
df509539
DS
2822 struct wpa_supplicant *wpa_s,
2823 int terminate)
6fc6879b
JM
2824{
2825 struct wpa_supplicant *prev;
2826
2827 /* Remove interface from the global list of interfaces */
2828 prev = global->ifaces;
2829 if (prev == wpa_s) {
2830 global->ifaces = wpa_s->next;
2831 } else {
2832 while (prev && prev->next != wpa_s)
2833 prev = prev->next;
2834 if (prev == NULL)
2835 return -1;
2836 prev->next = wpa_s->next;
2837 }
2838
f049052b 2839 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
6fc6879b 2840
b22128ef
JM
2841 if (global->p2p_group_formation == wpa_s)
2842 global->p2p_group_formation = NULL;
df509539 2843 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
6fc6879b
JM
2844 os_free(wpa_s);
2845
2846 return 0;
2847}
2848
2849
cf83fb0b
PS
2850/**
2851 * wpa_supplicant_get_eap_mode - Get the current EAP mode
2852 * @wpa_s: Pointer to the network interface
2853 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2854 */
2855const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2856{
2857 const char *eapol_method;
2858
2859 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2860 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2861 return "NO-EAP";
2862 }
2863
2864 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2865 if (eapol_method == NULL)
2866 return "UNKNOWN-EAP";
2867
2868 return eapol_method;
2869}
2870
2871
6fc6879b
JM
2872/**
2873 * wpa_supplicant_get_iface - Get a new network interface
2874 * @global: Pointer to global data from wpa_supplicant_init()
2875 * @ifname: Interface name
2876 * Returns: Pointer to the interface or %NULL if not found
2877 */
2878struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2879 const char *ifname)
2880{
2881 struct wpa_supplicant *wpa_s;
2882
2883 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2884 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2885 return wpa_s;
2886 }
2887 return NULL;
2888}
2889
2890
50b16da1 2891#ifndef CONFIG_NO_WPA_MSG
4f1495ae
BG
2892static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2893{
2894 struct wpa_supplicant *wpa_s = ctx;
2895 if (wpa_s == NULL)
2896 return NULL;
2897 return wpa_s->ifname;
2898}
50b16da1 2899#endif /* CONFIG_NO_WPA_MSG */
4f1495ae
BG
2900
2901
6fc6879b
JM
2902/**
2903 * wpa_supplicant_init - Initialize %wpa_supplicant
2904 * @params: Parameters for %wpa_supplicant
2905 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2906 *
2907 * This function is used to initialize %wpa_supplicant. After successful
2908 * initialization, the returned data pointer can be used to add and remove
2909 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2910 */
2911struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2912{
2913 struct wpa_global *global;
ac305589 2914 int ret, i;
6fc6879b
JM
2915
2916 if (params == NULL)
2917 return NULL;
2918
39e7d718
JM
2919#ifdef CONFIG_DRIVER_NDIS
2920 {
2921 void driver_ndis_init_ops(void);
2922 driver_ndis_init_ops();
2923 }
2924#endif /* CONFIG_DRIVER_NDIS */
2925
50b16da1 2926#ifndef CONFIG_NO_WPA_MSG
4f1495ae 2927 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
50b16da1 2928#endif /* CONFIG_NO_WPA_MSG */
4f1495ae 2929
6fc6879b 2930 wpa_debug_open_file(params->wpa_debug_file_path);
daa70d49
SL
2931 if (params->wpa_debug_syslog)
2932 wpa_debug_open_syslog();
4f68895e
JB
2933 if (params->wpa_debug_tracing) {
2934 ret = wpa_debug_open_linux_tracing();
2935 if (ret) {
2936 wpa_printf(MSG_ERROR,
2937 "Failed to enable trace logging");
2938 return NULL;
2939 }
2940 }
6fc6879b 2941
12760815 2942 ret = eap_register_methods();
6fc6879b
JM
2943 if (ret) {
2944 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2945 if (ret == -2)
2946 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2947 "the same EAP type.");
2948 return NULL;
2949 }
2950
2951 global = os_zalloc(sizeof(*global));
2952 if (global == NULL)
2953 return NULL;
b22128ef
JM
2954 dl_list_init(&global->p2p_srv_bonjour);
2955 dl_list_init(&global->p2p_srv_upnp);
6fc6879b
JM
2956 global->params.daemonize = params->daemonize;
2957 global->params.wait_for_monitor = params->wait_for_monitor;
2958 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2959 if (params->pid_file)
2960 global->params.pid_file = os_strdup(params->pid_file);
2961 if (params->ctrl_interface)
2962 global->params.ctrl_interface =
2963 os_strdup(params->ctrl_interface);
d27df100
JM
2964 if (params->override_driver)
2965 global->params.override_driver =
2966 os_strdup(params->override_driver);
2967 if (params->override_ctrl_interface)
2968 global->params.override_ctrl_interface =
2969 os_strdup(params->override_ctrl_interface);
6fc6879b
JM
2970 wpa_debug_level = global->params.wpa_debug_level =
2971 params->wpa_debug_level;
2972 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2973 params->wpa_debug_show_keys;
2974 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2975 params->wpa_debug_timestamp;
2976
f19858f5
JM
2977 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2978
0456ea16 2979 if (eloop_init()) {
6fc6879b
JM
2980 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2981 wpa_supplicant_deinit(global);
2982 return NULL;
2983 }
2984
38e24575 2985 random_init(params->entropy_file);
d47fa330 2986
6fc6879b
JM
2987 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2988 if (global->ctrl_iface == NULL) {
2989 wpa_supplicant_deinit(global);
2990 return NULL;
2991 }
2992
dc461de4
WS
2993 if (wpas_notify_supplicant_initialized(global)) {
2994 wpa_supplicant_deinit(global);
2995 return NULL;
6fc6879b
JM
2996 }
2997
c5121837 2998 for (i = 0; wpa_drivers[i]; i++)
ac305589
JM
2999 global->drv_count++;
3000 if (global->drv_count == 0) {
3001 wpa_printf(MSG_ERROR, "No drivers enabled");
3002 wpa_supplicant_deinit(global);
3003 return NULL;
3004 }
3005 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
3006 if (global->drv_priv == NULL) {
3007 wpa_supplicant_deinit(global);
3008 return NULL;
3009 }
ac305589 3010
6fc6879b
JM
3011 return global;
3012}
3013
3014
3015/**
3016 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
3017 * @global: Pointer to global data from wpa_supplicant_init()
3018 * Returns: 0 after successful event loop run, -1 on failure
3019 *
3020 * This function starts the main event loop and continues running as long as
3021 * there are any remaining events. In most cases, this function is running as
3022 * long as the %wpa_supplicant process in still in use.
3023 */
3024int wpa_supplicant_run(struct wpa_global *global)
3025{
3026 struct wpa_supplicant *wpa_s;
3027
3028 if (global->params.daemonize &&
3029 wpa_supplicant_daemon(global->params.pid_file))
3030 return -1;
3031
3032 if (global->params.wait_for_monitor) {
3033 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
3034 if (wpa_s->ctrl_iface)
3035 wpa_supplicant_ctrl_iface_wait(
3036 wpa_s->ctrl_iface);
3037 }
3038
0456ea16
JM
3039 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
3040 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
6fc6879b
JM
3041
3042 eloop_run();
3043
3044 return 0;
3045}
3046
3047
3048/**
3049 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
3050 * @global: Pointer to global data from wpa_supplicant_init()
3051 *
3052 * This function is called to deinitialize %wpa_supplicant and to free all
3053 * allocated resources. Remaining network interfaces will also be removed.
3054 */
3055void wpa_supplicant_deinit(struct wpa_global *global)
3056{
ac305589
JM
3057 int i;
3058
6fc6879b
JM
3059 if (global == NULL)
3060 return;
3061
b22128ef
JM
3062#ifdef CONFIG_P2P
3063 wpas_p2p_deinit_global(global);
3064#endif /* CONFIG_P2P */
3065
6fc6879b 3066 while (global->ifaces)
df509539 3067 wpa_supplicant_remove_iface(global, global->ifaces, 1);
6fc6879b
JM
3068
3069 if (global->ctrl_iface)
3070 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
dc461de4
WS
3071
3072 wpas_notify_supplicant_deinitialized(global);
6fc6879b
JM
3073
3074 eap_peer_unregister_methods();
3ec97afe
JM
3075#ifdef CONFIG_AP
3076 eap_server_unregister_methods();
3077#endif /* CONFIG_AP */
6fc6879b 3078
c5121837 3079 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
ac305589
JM
3080 if (!global->drv_priv[i])
3081 continue;
c5121837 3082 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
ac305589
JM
3083 }
3084 os_free(global->drv_priv);
3085
d47fa330
JM
3086 random_deinit();
3087
6fc6879b
JM
3088 eloop_destroy();
3089
3090 if (global->params.pid_file) {
3091 os_daemonize_terminate(global->params.pid_file);
3092 os_free(global->params.pid_file);
3093 }
3094 os_free(global->params.ctrl_interface);
d27df100
JM
3095 os_free(global->params.override_driver);
3096 os_free(global->params.override_ctrl_interface);
6fc6879b 3097
6f3bc72b
JM
3098 os_free(global->p2p_disallow_freq);
3099
6fc6879b 3100 os_free(global);
daa70d49 3101 wpa_debug_close_syslog();
6fc6879b 3102 wpa_debug_close_file();
4f68895e 3103 wpa_debug_close_linux_tracing();
6fc6879b 3104}
611aea7d
JM
3105
3106
3107void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
3108{
849b5dc7
JM
3109 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3110 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3111 char country[3];
3112 country[0] = wpa_s->conf->country[0];
3113 country[1] = wpa_s->conf->country[1];
3114 country[2] = '\0';
3115 if (wpa_drv_set_country(wpa_s, country) < 0) {
3116 wpa_printf(MSG_ERROR, "Failed to set country code "
3117 "'%s'", country);
3118 }
3119 }
3120
611aea7d
JM
3121#ifdef CONFIG_WPS
3122 wpas_wps_update_config(wpa_s);
3123#endif /* CONFIG_WPS */
3124
b22128ef
JM
3125#ifdef CONFIG_P2P
3126 wpas_p2p_update_config(wpa_s);
3127#endif /* CONFIG_P2P */
3128
611aea7d
JM
3129 wpa_s->conf->changed_parameters = 0;
3130}
2f9c6aa6
JM
3131
3132
0fb337c1
JM
3133static void add_freq(int *freqs, int *num_freqs, int freq)
3134{
3135 int i;
3136
3137 for (i = 0; i < *num_freqs; i++) {
3138 if (freqs[i] == freq)
3139 return;
3140 }
3141
3142 freqs[*num_freqs] = freq;
3143 (*num_freqs)++;
3144}
3145
3146
3147static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
3148{
3149 struct wpa_bss *bss, *cbss;
3150 const int max_freqs = 10;
3151 int *freqs;
3152 int num_freqs = 0;
3153
3154 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
3155 if (freqs == NULL)
3156 return NULL;
3157
3158 cbss = wpa_s->current_bss;
3159
3160 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
3161 if (bss == cbss)
3162 continue;
3163 if (bss->ssid_len == cbss->ssid_len &&
3164 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
3165 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
3166 add_freq(freqs, &num_freqs, bss->freq);
3167 if (num_freqs == max_freqs)
3168 break;
3169 }
3170 }
3171
3172 if (num_freqs == 0) {
3173 os_free(freqs);
3174 freqs = NULL;
3175 }
3176
3177 return freqs;
3178}
3179
3180
3181void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
3182{
3183 int timeout;
3184 int count;
3185 int *freqs = NULL;
3186
5fd9fb27
JM
3187 /*
3188 * Remove possible authentication timeout since the connection failed.
3189 */
3190 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
3191
0fb337c1
JM
3192 /*
3193 * Add the failed BSSID into the blacklist and speed up next scan
3194 * attempt if there could be other APs that could accept association.
3195 * The current blacklist count indicates how many times we have tried
3196 * connecting to this AP and multiple attempts mean that other APs are
3197 * either not available or has already been tried, so that we can start
3198 * increasing the delay here to avoid constant scanning.
3199 */
3200 count = wpa_blacklist_add(wpa_s, bssid);
3201 if (count == 1 && wpa_s->current_bss) {
3202 /*
3203 * This BSS was not in the blacklist before. If there is
3204 * another BSS available for the same ESS, we should try that
3205 * next. Otherwise, we may as well try this one once more
3206 * before allowing other, likely worse, ESSes to be considered.
3207 */
3208 freqs = get_bss_freqs_in_ess(wpa_s);
3209 if (freqs) {
f049052b
BG
3210 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
3211 "has been seen; try it next");
0fb337c1
JM
3212 wpa_blacklist_add(wpa_s, bssid);
3213 /*
3214 * On the next scan, go through only the known channels
3215 * used in this ESS based on previous scans to speed up
3216 * common load balancing use case.
3217 */
3218 os_free(wpa_s->next_scan_freqs);
3219 wpa_s->next_scan_freqs = freqs;
3220 }
3221 }
3222
3223 switch (count) {
3224 case 1:
3225 timeout = 100;
3226 break;
3227 case 2:
3228 timeout = 500;
3229 break;
3230 case 3:
3231 timeout = 1000;
3232 break;
3233 default:
3234 timeout = 5000;
3235 }
3236
3237 /*
3238 * TODO: if more than one possible AP is available in scan results,
3239 * could try the other ones before requesting a new scan.
3240 */
3241 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
3242 1000 * (timeout % 1000));
3243}
22628eca
JM
3244
3245
3246int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
3247{
3248 return wpa_s->conf->ap_scan == 2 ||
3249 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
3250}
d2118814
JM
3251
3252
3253#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
3254int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
3255 struct wpa_ssid *ssid,
3256 const char *field,
3257 const char *value)
3258{
3259#ifdef IEEE8021X_EAPOL
3260 struct eap_peer_config *eap = &ssid->eap;
3261
3262 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
3263 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
3264 (const u8 *) value, os_strlen(value));
3265
3266 switch (wpa_supplicant_ctrl_req_from_string(field)) {
3267 case WPA_CTRL_REQ_EAP_IDENTITY:
3268 os_free(eap->identity);
3269 eap->identity = (u8 *) os_strdup(value);
3270 eap->identity_len = os_strlen(value);
3271 eap->pending_req_identity = 0;
3272 if (ssid == wpa_s->current_ssid)
3273 wpa_s->reassociate = 1;
3274 break;
3275 case WPA_CTRL_REQ_EAP_PASSWORD:
3276 os_free(eap->password);
3277 eap->password = (u8 *) os_strdup(value);
3278 eap->password_len = os_strlen(value);
3279 eap->pending_req_password = 0;
3280 if (ssid == wpa_s->current_ssid)
3281 wpa_s->reassociate = 1;
3282 break;
3283 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
3284 os_free(eap->new_password);
3285 eap->new_password = (u8 *) os_strdup(value);
3286 eap->new_password_len = os_strlen(value);
3287 eap->pending_req_new_password = 0;
3288 if (ssid == wpa_s->current_ssid)
3289 wpa_s->reassociate = 1;
3290 break;
3291 case WPA_CTRL_REQ_EAP_PIN:
3292 os_free(eap->pin);
3293 eap->pin = os_strdup(value);
3294 eap->pending_req_pin = 0;
3295 if (ssid == wpa_s->current_ssid)
3296 wpa_s->reassociate = 1;
3297 break;
3298 case WPA_CTRL_REQ_EAP_OTP:
3299 os_free(eap->otp);
3300 eap->otp = (u8 *) os_strdup(value);
3301 eap->otp_len = os_strlen(value);
3302 os_free(eap->pending_req_otp);
3303 eap->pending_req_otp = NULL;
3304 eap->pending_req_otp_len = 0;
3305 break;
3306 case WPA_CTRL_REQ_EAP_PASSPHRASE:
3307 os_free(eap->private_key_passwd);
3308 eap->private_key_passwd = (u8 *) os_strdup(value);
3309 eap->pending_req_passphrase = 0;
3310 if (ssid == wpa_s->current_ssid)
3311 wpa_s->reassociate = 1;
3312 break;
3313 default:
3314 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
3315 return -1;
3316 }
3317
3318 return 0;
3319#else /* IEEE8021X_EAPOL */
3320 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
3321 return -1;
3322#endif /* IEEE8021X_EAPOL */
3323}
3324#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
349493bd
JM
3325
3326
3327int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
3328{
3329 int i;
3330 unsigned int drv_enc;
3331
3332 if (ssid == NULL)
3333 return 1;
3334
3335 if (ssid->disabled)
3336 return 1;
3337
3338 if (wpa_s && wpa_s->drv_capa_known)
3339 drv_enc = wpa_s->drv_enc;
3340 else
3341 drv_enc = (unsigned int) -1;
3342
3343 for (i = 0; i < NUM_WEP_KEYS; i++) {
3344 size_t len = ssid->wep_key_len[i];
3345 if (len == 0)
3346 continue;
3347 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
3348 continue;
3349 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
3350 continue;
3351 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
3352 continue;
3353 return 1; /* invalid WEP key */
3354 }
3355
3356 return 0;
3357}