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