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