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