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