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