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