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