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