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