3 * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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.
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "eap_peer/eap.h"
24 #include "eap_server/eap_methods.h"
28 #include "l2_packet/l2_packet.h"
29 #include "wpa_supplicant_i.h"
31 #include "ctrl_iface.h"
32 #include "ctrl_iface_dbus.h"
33 #include "pcsc_funcs.h"
36 #include "pmksa_cache.h"
39 #include "ieee802_11_defs.h"
40 #include "blacklist.h"
41 #include "wpas_glue.h"
42 #include "wps_supplicant.h"
48 const char *wpa_supplicant_version =
49 "wpa_supplicant v" VERSION_STR "\n"
50 "Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
52 const char *wpa_supplicant_license =
53 "This program is free software. You can distribute it and/or modify it\n"
54 "under the terms of the GNU General Public License version 2.\n"
56 "Alternatively, this software may be distributed under the terms of the\n"
57 "BSD license. See README and COPYING for more details.\n"
58 #ifdef EAP_TLS_OPENSSL
59 "\nThis product includes software developed by the OpenSSL Project\n"
60 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
61 #endif /* EAP_TLS_OPENSSL */
64 #ifndef CONFIG_NO_STDOUT_DEBUG
65 /* Long text divided into parts in order to fit in C89 strings size limits. */
66 const char *wpa_supplicant_full_license1 =
67 "This program is free software; you can redistribute it and/or modify\n"
68 "it under the terms of the GNU General Public License version 2 as\n"
69 "published by the Free Software Foundation.\n"
71 "This program is distributed in the hope that it will be useful,\n"
72 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
73 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
74 "GNU General Public License for more details.\n"
76 const char *wpa_supplicant_full_license2 =
77 "You should have received a copy of the GNU General Public License\n"
78 "along with this program; if not, write to the Free Software\n"
79 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
81 "Alternatively, this software may be distributed under the terms of the\n"
84 "Redistribution and use in source and binary forms, with or without\n"
85 "modification, are permitted provided that the following conditions are\n"
88 const char *wpa_supplicant_full_license3 =
89 "1. Redistributions of source code must retain the above copyright\n"
90 " notice, this list of conditions and the following disclaimer.\n"
92 "2. Redistributions in binary form must reproduce the above copyright\n"
93 " notice, this list of conditions and the following disclaimer in the\n"
94 " documentation and/or other materials provided with the distribution.\n"
96 const char *wpa_supplicant_full_license4 =
97 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
98 " names of its contributors may be used to endorse or promote products\n"
99 " derived from this software without specific prior written permission.\n"
101 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
102 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
103 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
104 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
105 const char *wpa_supplicant_full_license5 =
106 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
107 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
108 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
109 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
110 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
111 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
112 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
114 #endif /* CONFIG_NO_STDOUT_DEBUG */
116 extern int wpa_debug_level;
117 extern int wpa_debug_show_keys;
118 extern int wpa_debug_timestamp;
119 extern struct wpa_driver_ops *wpa_drivers[];
121 /* Configure default/group WEP keys for static WEP */
122 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
123 struct wpa_ssid *ssid)
127 for (i = 0; i < NUM_WEP_KEYS; i++) {
128 if (ssid->wep_key_len[i] == 0)
132 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
133 (u8 *) "\xff\xff\xff\xff\xff\xff",
134 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
135 ssid->wep_key[i], ssid->wep_key_len[i]);
142 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
143 struct wpa_ssid *ssid)
150 /* IBSS/WPA-None uses only one key (Group) for both receiving and
151 * sending unicast and multicast packets. */
153 if (ssid->mode != IEEE80211_MODE_IBSS) {
154 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
155 "for WPA-None", ssid->mode);
159 if (!ssid->psk_set) {
160 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
164 switch (wpa_s->group_cipher) {
165 case WPA_CIPHER_CCMP:
166 os_memcpy(key, ssid->psk, 16);
170 case WPA_CIPHER_TKIP:
171 /* WPA-None uses the same Michael MIC key for both TX and RX */
172 os_memcpy(key, ssid->psk, 16 + 8);
173 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
178 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
179 "WPA-None", wpa_s->group_cipher);
183 /* TODO: should actually remember the previously used seq#, both for TX
184 * and RX from each STA.. */
186 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
187 0, 1, seq, 6, key, keylen);
191 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
193 struct wpa_supplicant *wpa_s = eloop_ctx;
194 const u8 *bssid = wpa_s->bssid;
195 if (is_zero_ether_addr(bssid))
196 bssid = wpa_s->pending_bssid;
197 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
199 wpa_blacklist_add(wpa_s, bssid);
200 wpa_sm_notify_disassoc(wpa_s->wpa);
201 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
202 wpa_s->reassociate = 1;
203 wpa_supplicant_req_scan(wpa_s, 0, 0);
208 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
209 * @wpa_s: Pointer to wpa_supplicant data
210 * @sec: Number of seconds after which to time out authentication
211 * @usec: Number of microseconds after which to time out authentication
213 * This function is used to schedule a timeout for the current authentication
216 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
219 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
220 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
223 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
224 "%d usec", sec, usec);
225 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
226 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
231 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
232 * @wpa_s: Pointer to wpa_supplicant data
234 * This function is used to cancel authentication timeout scheduled with
235 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
238 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
240 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
241 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
242 wpa_blacklist_del(wpa_s, wpa_s->bssid);
247 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
248 * @wpa_s: Pointer to wpa_supplicant data
250 * This function is used to configure EAPOL state machine based on the selected
251 * authentication mode.
253 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
255 #ifdef IEEE8021X_EAPOL
256 struct eapol_config eapol_conf;
257 struct wpa_ssid *ssid = wpa_s->current_ssid;
259 #ifdef CONFIG_IBSS_RSN
260 if (ssid->mode == IEEE80211_MODE_IBSS &&
261 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
262 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
264 * RSN IBSS authentication is per-STA and we can disable the
265 * per-BSSID EAPOL authentication.
267 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
268 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
269 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
272 #endif /* CONFIG_IBSS_RSN */
274 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
275 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
277 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
278 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
279 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
281 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
283 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
284 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
285 eapol_conf.accept_802_1x_keys = 1;
286 eapol_conf.required_keys = 0;
287 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
288 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
290 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
291 eapol_conf.required_keys |=
292 EAPOL_REQUIRE_KEY_BROADCAST;
295 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
296 eapol_conf.required_keys = 0;
299 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
300 eapol_conf.workaround = ssid->eap_workaround;
301 eapol_conf.eap_disabled =
302 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
303 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
304 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
305 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
306 #endif /* IEEE8021X_EAPOL */
311 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
312 * @wpa_s: Pointer to wpa_supplicant data
313 * @ssid: Configuration data for the network
315 * This function is used to configure WPA state machine and related parameters
316 * to a mode where WPA is not enabled. This is called as part of the
317 * authentication configuration when the selected network does not use WPA.
319 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
320 struct wpa_ssid *ssid)
324 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
325 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
326 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
327 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
329 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
330 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
331 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
332 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
333 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
334 wpa_s->group_cipher = WPA_CIPHER_NONE;
335 wpa_s->mgmt_group_cipher = 0;
337 for (i = 0; i < NUM_WEP_KEYS; i++) {
338 if (ssid->wep_key_len[i] > 5) {
339 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
340 wpa_s->group_cipher = WPA_CIPHER_WEP104;
342 } else if (ssid->wep_key_len[i] > 0) {
343 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
344 wpa_s->group_cipher = WPA_CIPHER_WEP40;
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
352 wpa_s->pairwise_cipher);
353 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
354 #ifdef CONFIG_IEEE80211W
355 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
356 wpa_s->mgmt_group_cipher);
357 #endif /* CONFIG_IEEE80211W */
359 pmksa_cache_clear_current(wpa_s->wpa);
363 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
365 scard_deinit(wpa_s->scard);
367 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
368 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
369 l2_packet_deinit(wpa_s->l2);
372 l2_packet_deinit(wpa_s->l2_br);
376 if (wpa_s->ctrl_iface) {
377 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
378 wpa_s->ctrl_iface = NULL;
380 if (wpa_s->conf != NULL) {
381 wpa_config_free(wpa_s->conf);
385 os_free(wpa_s->confname);
386 wpa_s->confname = NULL;
388 wpa_sm_set_eapol(wpa_s->wpa, NULL);
389 eapol_sm_deinit(wpa_s->eapol);
392 rsn_preauth_deinit(wpa_s->wpa);
394 pmksa_candidate_free(wpa_s->wpa);
395 wpa_sm_deinit(wpa_s->wpa);
397 wpa_blacklist_clear(wpa_s);
399 wpa_scan_results_free(wpa_s->scan_res);
400 wpa_s->scan_res = NULL;
402 wpa_supplicant_cancel_scan(wpa_s);
403 wpa_supplicant_cancel_auth_timeout(wpa_s);
405 ieee80211_sta_deinit(wpa_s);
407 wpas_wps_deinit(wpa_s);
409 wpabuf_free(wpa_s->pending_eapol_rx);
410 wpa_s->pending_eapol_rx = NULL;
412 #ifdef CONFIG_IBSS_RSN
413 ibss_rsn_deinit(wpa_s->ibss_rsn);
414 wpa_s->ibss_rsn = NULL;
415 #endif /* CONFIG_IBSS_RSN */
418 os_free(wpa_s->sme.ft_ies);
419 wpa_s->sme.ft_ies = NULL;
420 wpa_s->sme.ft_ies_len = 0;
421 #endif /* CONFIG_SME */
424 wpa_supplicant_ap_deinit(wpa_s);
425 #endif /* CONFIG_AP */
430 * wpa_clear_keys - Clear keys configured for the driver
431 * @wpa_s: Pointer to wpa_supplicant data
432 * @addr: Previously used BSSID or %NULL if not available
434 * This function clears the encryption keys that has been previously configured
437 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
439 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
441 if (wpa_s->keys_cleared) {
442 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
443 * timing issues with keys being cleared just before new keys
444 * are set or just after association or something similar. This
445 * shows up in group key handshake failing often because of the
446 * client not receiving the first encrypted packets correctly.
447 * Skipping some of the extra key clearing steps seems to help
448 * in completing group key handshake more reliably. */
449 wpa_printf(MSG_DEBUG, "No keys have been configured - "
450 "skip key clearing");
454 /* MLME-DELETEKEYS.request */
455 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
456 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
457 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
458 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
460 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
462 /* MLME-SETPROTECTION.request(None) */
463 wpa_drv_mlme_setprotection(
465 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
466 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
468 wpa_s->keys_cleared = 1;
473 * wpa_supplicant_state_txt - Get the connection state name as a text string
474 * @state: State (wpa_state; WPA_*)
475 * Returns: The state name as a printable text string
477 const char * wpa_supplicant_state_txt(int state)
480 case WPA_DISCONNECTED:
481 return "DISCONNECTED";
486 case WPA_AUTHENTICATING:
487 return "AUTHENTICATING";
488 case WPA_ASSOCIATING:
489 return "ASSOCIATING";
492 case WPA_4WAY_HANDSHAKE:
493 return "4WAY_HANDSHAKE";
494 case WPA_GROUP_HANDSHAKE:
495 return "GROUP_HANDSHAKE";
505 * wpa_supplicant_set_state - Set current connection state
506 * @wpa_s: Pointer to wpa_supplicant data
507 * @state: The new connection state
509 * This function is called whenever the connection state changes, e.g.,
510 * association is completed for WPA/WPA2 4-Way Handshake is started.
512 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
514 wpa_printf(MSG_DEBUG, "State: %s -> %s",
515 wpa_supplicant_state_txt(wpa_s->wpa_state),
516 wpa_supplicant_state_txt(state));
518 if (state != WPA_SCANNING)
519 wpa_supplicant_notify_scanning(wpa_s, 0);
521 wpas_notify_state_changed(wpa_s, state, wpa_s->wpa_state);
523 if (state == WPA_COMPLETED && wpa_s->new_connection) {
524 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
525 struct wpa_ssid *ssid = wpa_s->current_ssid;
526 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
527 MACSTR " completed %s [id=%d id_str=%s]",
528 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
529 "(reauth)" : "(auth)",
530 ssid ? ssid->id : -1,
531 ssid && ssid->id_str ? ssid->id_str : "");
532 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
533 wpa_s->new_connection = 0;
534 wpa_s->reassociated_connection = 1;
535 wpa_drv_set_operstate(wpa_s, 1);
536 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
537 state == WPA_ASSOCIATED) {
538 wpa_s->new_connection = 1;
539 wpa_drv_set_operstate(wpa_s, 0);
541 wpa_s->wpa_state = state;
545 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
548 struct wpa_global *global = eloop_ctx;
549 struct wpa_supplicant *wpa_s;
550 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
551 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
558 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
560 wpa_states old_state = wpa_s->wpa_state;
561 wpa_s->pairwise_cipher = 0;
562 wpa_s->group_cipher = 0;
563 wpa_s->mgmt_group_cipher = 0;
565 wpa_s->wpa_state = WPA_DISCONNECTED;
566 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
571 * wpa_supplicant_reload_configuration - Reload configuration data
572 * @wpa_s: Pointer to wpa_supplicant data
573 * Returns: 0 on success or -1 if configuration parsing failed
575 * This function can be used to request that the configuration data is reloaded
576 * (e.g., after configuration file change). This function is reloading
577 * configuration only for one interface, so this may need to be called multiple
578 * times if %wpa_supplicant is controlling multiple interfaces and all
579 * interfaces need reconfiguration.
581 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
583 struct wpa_config *conf;
584 struct wpa_ssid *old_ssid;
588 if (wpa_s->confname == NULL)
590 conf = wpa_config_read(wpa_s->confname);
592 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
593 "file '%s' - exiting", wpa_s->confname);
597 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
598 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
599 os_strcmp(conf->ctrl_interface,
600 wpa_s->conf->ctrl_interface) != 0);
602 if (reconf_ctrl && wpa_s->ctrl_iface) {
603 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
604 wpa_s->ctrl_iface = NULL;
607 eapol_sm_invalidate_cached_session(wpa_s->eapol);
608 old_ssid = wpa_s->current_ssid;
609 wpa_s->current_ssid = NULL;
610 if (old_ssid != wpa_s->current_ssid)
611 wpas_notify_network_changed(wpa_s);
614 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
615 * pkcs11_engine_path, pkcs11_module_path.
617 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
619 * Clear forced success to clear EAP state for next
622 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
624 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
625 wpa_sm_set_config(wpa_s->wpa, NULL);
626 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
627 rsn_preauth_deinit(wpa_s->wpa);
629 old_ap_scan = wpa_s->conf->ap_scan;
630 wpa_config_free(wpa_s->conf);
632 if (old_ap_scan != wpa_s->conf->ap_scan)
633 wpas_notify_ap_scan_changed(wpa_s);
636 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
638 wpa_supplicant_clear_status(wpa_s);
639 wpa_s->reassociate = 1;
640 wpa_supplicant_req_scan(wpa_s, 0, 0);
641 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
646 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
649 struct wpa_global *global = eloop_ctx;
650 struct wpa_supplicant *wpa_s;
651 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
652 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
653 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
660 static wpa_cipher cipher_suite2driver(int cipher)
663 case WPA_CIPHER_NONE:
665 case WPA_CIPHER_WEP40:
667 case WPA_CIPHER_WEP104:
668 return CIPHER_WEP104;
669 case WPA_CIPHER_CCMP:
671 case WPA_CIPHER_TKIP:
678 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
681 case WPA_KEY_MGMT_NONE:
682 return KEY_MGMT_NONE;
683 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
684 return KEY_MGMT_802_1X_NO_WPA;
685 case WPA_KEY_MGMT_IEEE8021X:
686 return KEY_MGMT_802_1X;
687 case WPA_KEY_MGMT_WPA_NONE:
688 return KEY_MGMT_WPA_NONE;
689 case WPA_KEY_MGMT_FT_IEEE8021X:
690 return KEY_MGMT_FT_802_1X;
691 case WPA_KEY_MGMT_FT_PSK:
692 return KEY_MGMT_FT_PSK;
693 case WPA_KEY_MGMT_IEEE8021X_SHA256:
694 return KEY_MGMT_802_1X_SHA256;
695 case WPA_KEY_MGMT_PSK_SHA256:
696 return KEY_MGMT_PSK_SHA256;
697 case WPA_KEY_MGMT_WPS:
699 case WPA_KEY_MGMT_PSK:
706 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
707 struct wpa_ssid *ssid,
708 struct wpa_ie_data *ie)
710 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
713 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
714 "from association info");
719 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
721 if (!(ie->group_cipher & ssid->group_cipher)) {
722 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
723 "cipher 0x%x (mask 0x%x) - reject",
724 ie->group_cipher, ssid->group_cipher);
727 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
728 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
729 "cipher 0x%x (mask 0x%x) - reject",
730 ie->pairwise_cipher, ssid->pairwise_cipher);
733 if (!(ie->key_mgmt & ssid->key_mgmt)) {
734 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
735 "management 0x%x (mask 0x%x) - reject",
736 ie->key_mgmt, ssid->key_mgmt);
740 #ifdef CONFIG_IEEE80211W
741 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
742 ssid->ieee80211w == IEEE80211W_REQUIRED) {
743 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
744 "that does not support management frame protection - "
748 #endif /* CONFIG_IEEE80211W */
755 * wpa_supplicant_set_suites - Set authentication and encryption parameters
756 * @wpa_s: Pointer to wpa_supplicant data
757 * @bss: Scan results for the selected BSS, or %NULL if not available
758 * @ssid: Configuration data for the selected network
759 * @wpa_ie: Buffer for the WPA/RSN IE
760 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
761 * used buffer length in case the functions returns success.
762 * Returns: 0 on success or -1 on failure
764 * This function is used to configure authentication and encryption parameters
765 * based on the network configuration and scan result for the selected BSS (if
768 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
769 struct wpa_scan_res *bss,
770 struct wpa_ssid *ssid,
771 u8 *wpa_ie, size_t *wpa_ie_len)
773 struct wpa_ie_data ie;
775 const u8 *bss_wpa, *bss_rsn;
778 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
779 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
781 bss_wpa = bss_rsn = NULL;
783 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
784 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
785 (ie.group_cipher & ssid->group_cipher) &&
786 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
787 (ie.key_mgmt & ssid->key_mgmt)) {
788 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
789 proto = WPA_PROTO_RSN;
790 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
791 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
792 (ie.group_cipher & ssid->group_cipher) &&
793 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
794 (ie.key_mgmt & ssid->key_mgmt)) {
795 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
796 proto = WPA_PROTO_WPA;
798 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
801 if (ssid->proto & WPA_PROTO_RSN)
802 proto = WPA_PROTO_RSN;
804 proto = WPA_PROTO_WPA;
805 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
806 os_memset(&ie, 0, sizeof(ie));
807 ie.group_cipher = ssid->group_cipher;
808 ie.pairwise_cipher = ssid->pairwise_cipher;
809 ie.key_mgmt = ssid->key_mgmt;
810 #ifdef CONFIG_IEEE80211W
811 ie.mgmt_group_cipher =
812 ssid->ieee80211w != NO_IEEE80211W ?
813 WPA_CIPHER_AES_128_CMAC : 0;
814 #endif /* CONFIG_IEEE80211W */
815 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
821 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
822 "pairwise %d key_mgmt %d proto %d",
823 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
824 #ifdef CONFIG_IEEE80211W
825 if (ssid->ieee80211w) {
826 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
827 ie.mgmt_group_cipher);
829 #endif /* CONFIG_IEEE80211W */
831 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
832 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
833 !!(ssid->proto & WPA_PROTO_RSN));
835 if (bss || !wpa_s->ap_ies_from_associnfo) {
836 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
837 bss_wpa ? 2 + bss_wpa[1] : 0) ||
838 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
839 bss_rsn ? 2 + bss_rsn[1] : 0))
843 sel = ie.group_cipher & ssid->group_cipher;
844 if (sel & WPA_CIPHER_CCMP) {
845 wpa_s->group_cipher = WPA_CIPHER_CCMP;
846 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
847 } else if (sel & WPA_CIPHER_TKIP) {
848 wpa_s->group_cipher = WPA_CIPHER_TKIP;
849 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
850 } else if (sel & WPA_CIPHER_WEP104) {
851 wpa_s->group_cipher = WPA_CIPHER_WEP104;
852 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
853 } else if (sel & WPA_CIPHER_WEP40) {
854 wpa_s->group_cipher = WPA_CIPHER_WEP40;
855 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
857 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
861 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
862 if (sel & WPA_CIPHER_CCMP) {
863 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
864 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
865 } else if (sel & WPA_CIPHER_TKIP) {
866 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
867 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
868 } else if (sel & WPA_CIPHER_NONE) {
869 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
870 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
872 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
877 sel = ie.key_mgmt & ssid->key_mgmt;
879 #ifdef CONFIG_IEEE80211R
880 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
881 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
882 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
883 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
884 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
885 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
886 #endif /* CONFIG_IEEE80211R */
887 #ifdef CONFIG_IEEE80211W
888 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
889 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
890 wpa_msg(wpa_s, MSG_DEBUG,
891 "WPA: using KEY_MGMT 802.1X with SHA256");
892 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
893 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
894 wpa_msg(wpa_s, MSG_DEBUG,
895 "WPA: using KEY_MGMT PSK with SHA256");
896 #endif /* CONFIG_IEEE80211W */
897 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
898 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
899 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
900 } else if (sel & WPA_KEY_MGMT_PSK) {
901 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
902 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
903 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
904 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
905 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
907 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
908 "key management type.");
912 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
913 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
914 wpa_s->pairwise_cipher);
915 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
917 #ifdef CONFIG_IEEE80211W
918 sel = ie.mgmt_group_cipher;
919 if (ssid->ieee80211w == NO_IEEE80211W ||
920 !(ie.capabilities & WPA_CAPABILITY_MFPC))
922 if (sel & WPA_CIPHER_AES_128_CMAC) {
923 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
924 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
927 wpa_s->mgmt_group_cipher = 0;
928 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
930 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
931 wpa_s->mgmt_group_cipher);
932 #endif /* CONFIG_IEEE80211W */
934 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
935 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
940 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
941 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
943 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
950 * wpa_supplicant_associate - Request association
951 * @wpa_s: Pointer to wpa_supplicant data
952 * @bss: Scan results for the selected BSS, or %NULL if not available
953 * @ssid: Configuration data for the selected network
955 * This function is used to request %wpa_supplicant to associate with a BSS.
957 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
958 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
962 int use_crypt, ret, i, bssid_changed;
963 int algs = AUTH_ALG_OPEN_SYSTEM;
964 wpa_cipher cipher_pairwise, cipher_group;
965 struct wpa_driver_associate_params params;
966 int wep_keys_set = 0;
967 struct wpa_driver_capa capa;
968 int assoc_failed = 0;
969 struct wpa_ssid *old_ssid;
971 if (ssid->mode == 2) {
973 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
974 wpa_printf(MSG_INFO, "Driver does not support AP "
978 wpa_supplicant_create_ap(wpa_s, ssid);
979 #else /* CONFIG_AP */
980 wpa_printf(MSG_ERROR, "AP mode support not included in the "
982 #endif /* CONFIG_AP */
986 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
987 sme_authenticate(wpa_s, bss, ssid);
991 wpa_s->reassociate = 0;
993 #ifdef CONFIG_IEEE80211R
995 #endif /* CONFIG_IEEE80211R */
996 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
997 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
998 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
999 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
1000 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1001 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1002 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1004 wpas_notify_bssid_changed(wpa_s);
1005 #ifdef CONFIG_IEEE80211R
1006 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1007 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1009 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
1011 /* Prepare for the next transition */
1012 wpa_ft_prepare_auth_request(wpa_s->wpa);
1014 #endif /* CONFIG_IEEE80211R */
1016 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1017 wpa_s->conf->ap_scan == 2 &&
1018 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1019 /* Use ap_scan==1 style network selection to find the network
1021 wpa_s->scan_req = 2;
1022 wpa_s->reassociate = 1;
1023 wpa_supplicant_req_scan(wpa_s, 0, 0);
1025 #endif /* CONFIG_WPS */
1027 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1028 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1029 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1031 wpa_supplicant_cancel_scan(wpa_s);
1033 /* Starting new association, so clear the possibly used WPA IE from the
1034 * previous association. */
1035 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1037 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
1038 wpa_printf(MSG_WARNING, "Failed to set operating mode");
1042 #ifdef IEEE8021X_EAPOL
1043 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1045 if (ssid->non_leap == 0)
1046 algs = AUTH_ALG_LEAP;
1048 algs |= AUTH_ALG_LEAP;
1051 #endif /* IEEE8021X_EAPOL */
1052 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1053 if (ssid->auth_alg) {
1055 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1056 algs |= AUTH_ALG_OPEN_SYSTEM;
1057 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1058 algs |= AUTH_ALG_SHARED_KEY;
1059 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1060 algs |= AUTH_ALG_LEAP;
1061 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1064 wpa_drv_set_auth_alg(wpa_s, algs);
1066 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1067 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1068 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1069 WPA_KEY_MGMT_FT_IEEE8021X |
1070 WPA_KEY_MGMT_FT_PSK |
1071 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1072 WPA_KEY_MGMT_PSK_SHA256))) {
1073 int try_opportunistic;
1074 try_opportunistic = ssid->proactive_key_caching &&
1075 (ssid->proto & WPA_PROTO_RSN);
1076 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1077 wpa_s->current_ssid,
1078 try_opportunistic) == 0)
1079 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1080 wpa_ie_len = sizeof(wpa_ie);
1081 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1082 wpa_ie, &wpa_ie_len)) {
1083 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1084 "management and encryption suites");
1087 } else if (ssid->key_mgmt &
1088 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1089 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1090 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1091 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1092 wpa_ie_len = sizeof(wpa_ie);
1093 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1094 wpa_ie, &wpa_ie_len)) {
1095 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1096 "management and encryption suites (no scan "
1101 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1102 struct wpabuf *wps_ie;
1103 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1104 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1105 wpa_ie_len = wpabuf_len(wps_ie);
1106 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1109 wpabuf_free(wps_ie);
1110 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1111 #endif /* CONFIG_WPS */
1113 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1117 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1119 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1120 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1121 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1122 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1123 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1125 if (wpa_set_wep_keys(wpa_s, ssid)) {
1130 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1133 #ifdef IEEE8021X_EAPOL
1134 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1135 if ((ssid->eapol_flags &
1136 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1137 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1141 /* Assume that dynamic WEP-104 keys will be used and
1142 * set cipher suites in order for drivers to expect
1144 cipher_pairwise = cipher_group = CIPHER_WEP104;
1147 #endif /* IEEE8021X_EAPOL */
1149 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1150 /* Set the key before (and later after) association */
1151 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1154 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1155 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1156 os_memset(¶ms, 0, sizeof(params));
1158 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1159 params.bssid = bss->bssid;
1160 params.ssid = ie ? ie + 2 : (u8 *) "";
1161 params.ssid_len = ie ? ie[1] : 0;
1162 params.freq = bss->freq;
1164 params.ssid = ssid->ssid;
1165 params.ssid_len = ssid->ssid_len;
1167 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1168 params.freq = ssid->frequency; /* Initial channel for IBSS */
1169 params.wpa_ie = wpa_ie;
1170 params.wpa_ie_len = wpa_ie_len;
1171 params.pairwise_suite = cipher_pairwise;
1172 params.group_suite = cipher_group;
1173 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1174 params.auth_alg = algs;
1175 params.mode = ssid->mode;
1176 for (i = 0; i < NUM_WEP_KEYS; i++) {
1177 if (ssid->wep_key_len[i])
1178 params.wep_key[i] = ssid->wep_key[i];
1179 params.wep_key_len[i] = ssid->wep_key_len[i];
1181 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1183 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1184 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1185 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1186 params.passphrase = ssid->passphrase;
1188 params.psk = ssid->psk;
1191 params.drop_unencrypted = use_crypt;
1193 #ifdef CONFIG_IEEE80211W
1194 switch (ssid->ieee80211w) {
1196 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1198 case IEEE80211W_OPTIONAL:
1199 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1201 case IEEE80211W_REQUIRED:
1202 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1205 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1206 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1207 struct wpa_ie_data ie;
1208 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1210 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1211 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1213 params.mgmt_frame_protection =
1214 MGMT_FRAME_PROTECTION_REQUIRED;
1217 #endif /* CONFIG_IEEE80211W */
1219 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1220 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1222 ret = wpa_drv_associate(wpa_s, ¶ms);
1224 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1226 /* try to continue anyway; new association will be tried again
1231 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1232 /* Set the key after the association just in case association
1233 * cleared the previously configured key. */
1234 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1235 /* No need to timeout authentication since there is no key
1237 wpa_supplicant_cancel_auth_timeout(wpa_s);
1238 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1239 #ifdef CONFIG_IBSS_RSN
1240 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1241 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1242 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1243 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1245 * RSN IBSS authentication is per-STA and we can disable the
1246 * per-BSSID authentication.
1248 wpa_supplicant_cancel_auth_timeout(wpa_s);
1249 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1250 #endif /* CONFIG_IBSS_RSN */
1252 /* Timeout for IEEE 802.11 authentication and association */
1256 /* give IBSS a bit more time */
1257 timeout = ssid->mode ? 10 : 5;
1258 } else if (wpa_s->conf->ap_scan == 1) {
1259 /* give IBSS a bit more time */
1260 timeout = ssid->mode ? 20 : 10;
1262 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1265 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1266 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1267 /* Set static WEP keys again */
1268 wpa_set_wep_keys(wpa_s, ssid);
1271 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1273 * Do not allow EAP session resumption between different
1274 * network configurations.
1276 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1278 old_ssid = wpa_s->current_ssid;
1279 wpa_s->current_ssid = ssid;
1280 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1281 wpa_supplicant_initiate_eapol(wpa_s);
1282 if (old_ssid != wpa_s->current_ssid)
1283 wpas_notify_network_changed(wpa_s);
1288 * wpa_supplicant_disassociate - Disassociate the current connection
1289 * @wpa_s: Pointer to wpa_supplicant data
1290 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1292 * This function is used to request %wpa_supplicant to disassociate with the
1295 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1298 struct wpa_ssid *old_ssid;
1301 if (!is_zero_ether_addr(wpa_s->bssid)) {
1302 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1303 ieee80211_sta_disassociate(wpa_s, reason_code);
1305 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1306 addr = wpa_s->bssid;
1308 wpa_clear_keys(wpa_s, addr);
1309 wpa_supplicant_mark_disassoc(wpa_s);
1310 old_ssid = wpa_s->current_ssid;
1311 wpa_s->current_ssid = NULL;
1312 wpa_sm_set_config(wpa_s->wpa, NULL);
1313 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1314 if (old_ssid != wpa_s->current_ssid)
1315 wpas_notify_network_changed(wpa_s);
1320 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1321 * @wpa_s: Pointer to wpa_supplicant data
1322 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1324 * This function is used to request %wpa_supplicant to deauthenticate from the
1327 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1330 struct wpa_ssid *old_ssid;
1333 if (!is_zero_ether_addr(wpa_s->bssid)) {
1334 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1335 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1337 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1339 addr = wpa_s->bssid;
1341 wpa_clear_keys(wpa_s, addr);
1342 wpa_supplicant_mark_disassoc(wpa_s);
1343 old_ssid = wpa_s->current_ssid;
1344 wpa_s->current_ssid = NULL;
1345 wpa_sm_set_config(wpa_s->wpa, NULL);
1346 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1347 if (old_ssid != wpa_s->current_ssid)
1348 wpas_notify_network_changed(wpa_s);
1352 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1354 #define SCAN_AP_LIMIT 128
1355 struct wpa_scan_result *results;
1357 struct wpa_scan_results *res;
1359 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1360 if (results == NULL) {
1361 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1366 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1367 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1369 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1373 if (num > SCAN_AP_LIMIT) {
1374 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1375 num, SCAN_AP_LIMIT);
1376 num = SCAN_AP_LIMIT;
1379 wpa_scan_results_free(wpa_s->scan_res);
1380 wpa_s->scan_res = NULL;
1382 /* Convert old scan result data structure to the new one */
1383 res = os_zalloc(sizeof(*res));
1388 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1389 if (res->res == NULL) {
1395 for (i = 0; i < num; i++) {
1396 struct wpa_scan_result *bss = &results[i];
1397 struct wpa_scan_res *r;
1401 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1404 if (bss->mdie_present)
1407 r = os_zalloc(sizeof(*r) + ie_len);
1411 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1412 r->freq = bss->freq;
1413 r->caps = bss->caps;
1414 r->qual = bss->qual;
1415 r->noise = bss->noise;
1416 r->level = bss->level;
1420 pos = (u8 *) (r + 1);
1423 *pos++ = WLAN_EID_SSID;
1424 *pos++ = bss->ssid_len;
1425 os_memcpy(pos, bss->ssid, bss->ssid_len);
1426 pos += bss->ssid_len;
1429 /* Fake Supported Rate IE to include max rate */
1430 *pos++ = WLAN_EID_SUPP_RATES;
1432 *pos++ = bss->maxrate;
1435 if (bss->rsn_ie_len) {
1436 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1437 pos += bss->rsn_ie_len;
1440 if (bss->mdie_present) {
1441 os_memcpy(pos, bss->mdie, 5);
1445 if (bss->wpa_ie_len) {
1446 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1447 pos += bss->wpa_ie_len;
1450 res->res[res->num++] = r;
1454 wpa_s->scan_res = res;
1461 * wpa_supplicant_get_scan_results - Get scan results
1462 * @wpa_s: Pointer to wpa_supplicant data
1463 * Returns: 0 on success, -1 on failure
1465 * This function is request the current scan results from the driver and stores
1466 * a local copy of the results in wpa_s->scan_res.
1468 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1472 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1473 wpa_scan_results_free(wpa_s->scan_res);
1474 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1475 if (wpa_s->scan_res == NULL) {
1476 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1480 } else if (wpa_s->driver->get_scan_results2 == NULL)
1481 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1483 wpa_scan_results_free(wpa_s->scan_res);
1484 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1485 if (wpa_s->scan_res == NULL) {
1486 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1492 if (wpa_s->scan_res)
1493 wpa_scan_sort_results(wpa_s->scan_res);
1500 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1501 * @wpa_s: Pointer to wpa_supplicant data
1502 * Returns: A pointer to the current network structure or %NULL on failure
1504 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1506 struct wpa_ssid *entry;
1507 u8 ssid[MAX_SSID_LEN];
1513 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1514 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1515 wpa_printf(MSG_WARNING, "Could not read SSID from "
1520 res = wpa_drv_get_ssid(wpa_s, ssid);
1522 wpa_printf(MSG_WARNING, "Could not read SSID from "
1529 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1530 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1531 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1532 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1536 wired = wpa_s->conf->ap_scan == 0 &&
1537 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1539 entry = wpa_s->conf->ssid;
1541 if (!entry->disabled &&
1542 ((ssid_len == entry->ssid_len &&
1543 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1544 (!entry->bssid_set ||
1545 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1548 if (!entry->disabled &&
1549 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1550 (entry->ssid == NULL || entry->ssid_len == 0) &&
1551 (!entry->bssid_set ||
1552 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1554 #endif /* CONFIG_WPS */
1555 entry = entry->next;
1562 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1572 if (wpa_drivers[0] == NULL) {
1573 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1579 /* default to first driver in the list */
1580 wpa_s->driver = wpa_drivers[0];
1584 pos = os_strchr(name, ',');
1588 len = os_strlen(name);
1589 for (i = 0; wpa_drivers[i]; i++) {
1590 if (os_strlen(wpa_drivers[i]->name) == len &&
1591 os_strncmp(name, wpa_drivers[i]->name, len) ==
1593 wpa_s->driver = wpa_drivers[i];
1598 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1603 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1604 const u8 *buf, size_t len)
1606 struct wpa_supplicant *wpa_s = ctx;
1608 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1609 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1611 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1613 * There is possible race condition between receiving the
1614 * association event and the EAPOL frame since they are coming
1615 * through different paths from the driver. In order to avoid
1616 * issues in trying to process the EAPOL frame before receiving
1617 * association information, lets queue it for processing until
1618 * the association event is received.
1620 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1621 "received EAPOL frame");
1622 wpabuf_free(wpa_s->pending_eapol_rx);
1623 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1624 if (wpa_s->pending_eapol_rx) {
1625 os_get_time(&wpa_s->pending_eapol_rx_time);
1626 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1633 if (wpa_s->ap_iface) {
1634 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1637 #endif /* CONFIG_AP */
1639 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1640 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1641 "no key management is configured");
1645 if (wpa_s->eapol_received == 0 &&
1646 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1647 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1648 wpa_s->wpa_state != WPA_COMPLETED)) {
1649 /* Timeout for completing IEEE 802.1X and WPA authentication */
1650 wpa_supplicant_req_auth_timeout(
1652 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1653 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1654 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1657 wpa_s->eapol_received++;
1659 if (wpa_s->countermeasures) {
1660 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1665 #ifdef CONFIG_IBSS_RSN
1666 if (wpa_s->current_ssid &&
1667 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1668 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1671 #endif /* CONFIG_IBSS_RSN */
1673 /* Source address of the incoming EAPOL frame could be compared to the
1674 * current BSSID. However, it is possible that a centralized
1675 * Authenticator could be using another MAC address than the BSSID of
1676 * an AP, so just allow any address to be used for now. The replies are
1677 * still sent to the current BSSID (if available), though. */
1679 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1680 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1681 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1683 wpa_drv_poll(wpa_s);
1684 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1685 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1686 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1688 * Set portValid = TRUE here since we are going to skip 4-way
1689 * handshake processing which would normally set portValid. We
1690 * need this to allow the EAPOL state machines to be completed
1691 * without going through EAPOL-Key handshake.
1693 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1698 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1699 struct ieee80211_rx_status *rx_status)
1701 struct wpa_supplicant *wpa_s = ctx;
1702 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1707 * wpa_supplicant_driver_init - Initialize driver interface parameters
1708 * @wpa_s: Pointer to wpa_supplicant data
1709 * Returns: 0 on success, -1 on failure
1711 * This function is called to initialize driver interface parameters.
1712 * wpa_drv_init() must have been called before this function to initialize the
1715 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1717 static int interface_count = 0;
1719 if (wpa_s->driver->send_eapol) {
1720 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1722 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1724 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1725 wpa_drv_get_mac_addr(wpa_s),
1727 wpa_supplicant_rx_eapol, wpa_s, 0);
1728 if (wpa_s->l2 == NULL)
1732 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1733 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1737 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1738 MAC2STR(wpa_s->own_addr));
1740 if (wpa_s->bridge_ifname[0]) {
1741 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1742 " '%s'", wpa_s->bridge_ifname);
1743 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1746 wpa_supplicant_rx_eapol, wpa_s,
1748 if (wpa_s->l2_br == NULL) {
1749 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1750 "connection for the bridge interface '%s'",
1751 wpa_s->bridge_ifname);
1756 /* Backwards compatibility call to set_wpa() handler. This is called
1757 * only just after init and just before deinit, so these handler can be
1758 * used to implement same functionality. */
1759 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1760 struct wpa_driver_capa capa;
1761 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1762 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1763 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1764 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1765 /* Continue to allow non-WPA modes to be used. */
1767 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1773 wpa_clear_keys(wpa_s, NULL);
1775 /* Make sure that TKIP countermeasures are not left enabled (could
1776 * happen if wpa_supplicant is killed during countermeasures. */
1777 wpa_drv_set_countermeasures(wpa_s, 0);
1779 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1781 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1782 wpa_drv_flush_pmkid(wpa_s);
1784 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1785 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1792 static int wpa_supplicant_daemon(const char *pid_file)
1794 wpa_printf(MSG_DEBUG, "Daemonize..");
1795 return os_daemonize(pid_file);
1799 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1801 struct wpa_supplicant *wpa_s;
1803 wpa_s = os_zalloc(sizeof(*wpa_s));
1806 wpa_s->scan_req = 1;
1812 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1813 struct wpa_interface *iface)
1815 const char *ifname, *driver;
1816 struct wpa_driver_capa capa;
1818 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1819 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1820 iface->confname ? iface->confname : "N/A",
1821 iface->driver ? iface->driver : "default",
1822 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1823 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1825 if (iface->confname) {
1826 #ifdef CONFIG_BACKEND_FILE
1827 wpa_s->confname = os_rel2abs_path(iface->confname);
1828 if (wpa_s->confname == NULL) {
1829 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1830 "for configuration file '%s'.",
1834 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1835 iface->confname, wpa_s->confname);
1836 #else /* CONFIG_BACKEND_FILE */
1837 wpa_s->confname = os_strdup(iface->confname);
1838 #endif /* CONFIG_BACKEND_FILE */
1839 wpa_s->conf = wpa_config_read(wpa_s->confname);
1840 if (wpa_s->conf == NULL) {
1841 wpa_printf(MSG_ERROR, "Failed to read or parse "
1842 "configuration '%s'.", wpa_s->confname);
1847 * Override ctrl_interface and driver_param if set on command
1850 if (iface->ctrl_interface) {
1851 os_free(wpa_s->conf->ctrl_interface);
1852 wpa_s->conf->ctrl_interface =
1853 os_strdup(iface->ctrl_interface);
1856 if (iface->driver_param) {
1857 os_free(wpa_s->conf->driver_param);
1858 wpa_s->conf->driver_param =
1859 os_strdup(iface->driver_param);
1862 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1863 iface->driver_param);
1865 if (wpa_s->conf == NULL) {
1866 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1870 if (iface->ifname == NULL) {
1871 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1874 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1875 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1879 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1881 if (iface->bridge_ifname) {
1882 if (os_strlen(iface->bridge_ifname) >=
1883 sizeof(wpa_s->bridge_ifname)) {
1884 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1885 "name '%s'.", iface->bridge_ifname);
1888 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1889 sizeof(wpa_s->bridge_ifname));
1892 /* RSNA Supplicant Key Management - INITIALIZE */
1893 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1894 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1896 /* Initialize driver interface and register driver event handler before
1897 * L2 receive handler so that association events are processed before
1898 * EAPOL-Key packets if both become available for the same select()
1900 driver = iface->driver;
1902 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
1905 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1906 if (wpa_s->drv_priv == NULL) {
1908 pos = os_strchr(driver, ',');
1910 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
1911 "interface - try next driver wrapper");
1915 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1918 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1919 wpa_printf(MSG_ERROR, "Driver interface rejected "
1920 "driver_param '%s'", wpa_s->conf->driver_param);
1924 ifname = wpa_drv_get_ifname(wpa_s);
1925 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1926 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1927 "name with '%s'", ifname);
1928 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1931 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1934 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1935 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1937 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1939 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1940 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1941 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1942 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1943 "dot11RSNAConfigPMKLifetime");
1947 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1948 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1949 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1950 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1951 "dot11RSNAConfigPMKReauthThreshold");
1955 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1956 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1957 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1958 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1959 "dot11RSNAConfigSATimeout");
1963 if (wpa_supplicant_driver_init(wpa_s) < 0)
1966 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
1967 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
1968 wpa_printf(MSG_DEBUG, "Failed to set country");
1972 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1974 if (wpas_wps_init(wpa_s))
1977 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1979 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1981 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1982 if (wpa_s->ctrl_iface == NULL) {
1983 wpa_printf(MSG_ERROR,
1984 "Failed to initialize control interface '%s'.\n"
1985 "You may have another wpa_supplicant process "
1986 "already running or the file was\n"
1987 "left by an unclean termination of wpa_supplicant "
1988 "in which case you will need\n"
1989 "to manually remove this file before starting "
1990 "wpa_supplicant again.\n",
1991 wpa_s->conf->ctrl_interface);
1995 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1996 wpa_s->drv_flags = capa.flags;
1997 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1998 if (ieee80211_sta_init(wpa_s))
2001 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2004 #ifdef CONFIG_IBSS_RSN
2005 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2006 if (!wpa_s->ibss_rsn) {
2007 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2010 #endif /* CONFIG_IBSS_RSN */
2016 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2018 if (wpa_s->drv_priv) {
2019 wpa_supplicant_deauthenticate(wpa_s,
2020 WLAN_REASON_DEAUTH_LEAVING);
2022 /* Backwards compatibility call to set_wpa() handler. This is
2023 * called only just after init and just before deinit, so these
2024 * handler can be used to implement same functionality. */
2025 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
2026 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
2030 wpa_drv_set_drop_unencrypted(wpa_s, 0);
2031 wpa_drv_set_countermeasures(wpa_s, 0);
2032 wpa_clear_keys(wpa_s, NULL);
2035 wpas_notify_unregister_interface(wpa_s);
2037 wpa_supplicant_cleanup(wpa_s);
2039 if (wpa_s->drv_priv)
2040 wpa_drv_deinit(wpa_s);
2045 * wpa_supplicant_add_iface - Add a new network interface
2046 * @global: Pointer to global data from wpa_supplicant_init()
2047 * @iface: Interface configuration options
2048 * Returns: Pointer to the created interface or %NULL on failure
2050 * This function is used to add new network interfaces for %wpa_supplicant.
2051 * This can be called before wpa_supplicant_run() to add interfaces before the
2052 * main event loop has been started. In addition, new interfaces can be added
2053 * dynamically while %wpa_supplicant is already running. This could happen,
2054 * e.g., when a hotplug network adapter is inserted.
2056 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2057 struct wpa_interface *iface)
2059 struct wpa_supplicant *wpa_s;
2061 if (global == NULL || iface == NULL)
2064 wpa_s = wpa_supplicant_alloc();
2068 if (wpa_supplicant_init_iface(wpa_s, iface)) {
2069 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2071 wpa_supplicant_deinit_iface(wpa_s);
2076 wpa_s->global = global;
2078 /* Register the interface with the dbus control interface */
2079 if (wpas_dbus_register_iface(wpa_s)) {
2080 wpa_supplicant_deinit_iface(wpa_s);
2085 wpa_s->next = global->ifaces;
2086 global->ifaces = wpa_s;
2088 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2095 * wpa_supplicant_remove_iface - Remove a network interface
2096 * @global: Pointer to global data from wpa_supplicant_init()
2097 * @wpa_s: Pointer to the network interface to be removed
2098 * Returns: 0 if interface was removed, -1 if interface was not found
2100 * This function can be used to dynamically remove network interfaces from
2101 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2102 * addition, this function is used to remove all remaining interfaces when
2103 * %wpa_supplicant is terminated.
2105 int wpa_supplicant_remove_iface(struct wpa_global *global,
2106 struct wpa_supplicant *wpa_s)
2108 struct wpa_supplicant *prev;
2110 /* Remove interface from the global list of interfaces */
2111 prev = global->ifaces;
2112 if (prev == wpa_s) {
2113 global->ifaces = wpa_s->next;
2115 while (prev && prev->next != wpa_s)
2119 prev->next = wpa_s->next;
2122 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2124 wpa_supplicant_deinit_iface(wpa_s);
2132 * wpa_supplicant_get_iface - Get a new network interface
2133 * @global: Pointer to global data from wpa_supplicant_init()
2134 * @ifname: Interface name
2135 * Returns: Pointer to the interface or %NULL if not found
2137 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2140 struct wpa_supplicant *wpa_s;
2142 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2143 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2151 * wpa_supplicant_init - Initialize %wpa_supplicant
2152 * @params: Parameters for %wpa_supplicant
2153 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2155 * This function is used to initialize %wpa_supplicant. After successful
2156 * initialization, the returned data pointer can be used to add and remove
2157 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2159 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2161 struct wpa_global *global;
2167 wpa_debug_open_file(params->wpa_debug_file_path);
2168 if (params->wpa_debug_syslog)
2169 wpa_debug_open_syslog();
2171 ret = eap_peer_register_methods();
2173 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2175 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2176 "the same EAP type.");
2181 ret = eap_server_register_methods();
2183 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2185 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2186 "the same EAP type.");
2189 #endif /* CONFIG_AP */
2191 global = os_zalloc(sizeof(*global));
2194 global->params.daemonize = params->daemonize;
2195 global->params.wait_for_monitor = params->wait_for_monitor;
2196 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2197 if (params->pid_file)
2198 global->params.pid_file = os_strdup(params->pid_file);
2199 if (params->ctrl_interface)
2200 global->params.ctrl_interface =
2201 os_strdup(params->ctrl_interface);
2202 wpa_debug_level = global->params.wpa_debug_level =
2203 params->wpa_debug_level;
2204 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2205 params->wpa_debug_show_keys;
2206 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2207 params->wpa_debug_timestamp;
2209 if (eloop_init(global)) {
2210 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2211 wpa_supplicant_deinit(global);
2215 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2216 if (global->ctrl_iface == NULL) {
2217 wpa_supplicant_deinit(global);
2221 if (global->params.dbus_ctrl_interface) {
2222 global->dbus_ctrl_iface =
2223 wpa_supplicant_dbus_ctrl_iface_init(global);
2224 if (global->dbus_ctrl_iface == NULL) {
2225 wpa_supplicant_deinit(global);
2230 for (i = 0; wpa_drivers[i]; i++)
2231 global->drv_count++;
2232 if (global->drv_count == 0) {
2233 wpa_printf(MSG_ERROR, "No drivers enabled");
2234 wpa_supplicant_deinit(global);
2237 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2238 if (global->drv_priv == NULL) {
2239 wpa_supplicant_deinit(global);
2242 for (i = 0; wpa_drivers[i]; i++) {
2243 if (!wpa_drivers[i]->global_init)
2245 global->drv_priv[i] = wpa_drivers[i]->global_init();
2246 if (global->drv_priv[i] == NULL) {
2247 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2248 "'%s'", wpa_drivers[i]->name);
2249 wpa_supplicant_deinit(global);
2259 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2260 * @global: Pointer to global data from wpa_supplicant_init()
2261 * Returns: 0 after successful event loop run, -1 on failure
2263 * This function starts the main event loop and continues running as long as
2264 * there are any remaining events. In most cases, this function is running as
2265 * long as the %wpa_supplicant process in still in use.
2267 int wpa_supplicant_run(struct wpa_global *global)
2269 struct wpa_supplicant *wpa_s;
2271 if (global->params.daemonize &&
2272 wpa_supplicant_daemon(global->params.pid_file))
2275 if (global->params.wait_for_monitor) {
2276 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2277 if (wpa_s->ctrl_iface)
2278 wpa_supplicant_ctrl_iface_wait(
2282 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2283 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2292 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2293 * @global: Pointer to global data from wpa_supplicant_init()
2295 * This function is called to deinitialize %wpa_supplicant and to free all
2296 * allocated resources. Remaining network interfaces will also be removed.
2298 void wpa_supplicant_deinit(struct wpa_global *global)
2305 while (global->ifaces)
2306 wpa_supplicant_remove_iface(global, global->ifaces);
2308 if (global->ctrl_iface)
2309 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2310 if (global->dbus_ctrl_iface)
2311 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2313 eap_peer_unregister_methods();
2315 eap_server_unregister_methods();
2316 #endif /* CONFIG_AP */
2318 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2319 if (!global->drv_priv[i])
2321 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2323 os_free(global->drv_priv);
2327 if (global->params.pid_file) {
2328 os_daemonize_terminate(global->params.pid_file);
2329 os_free(global->params.pid_file);
2331 os_free(global->params.ctrl_interface);
2334 wpa_debug_close_syslog();
2335 wpa_debug_close_file();