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