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"
26 #include "drivers/driver.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"
35 #include "pmksa_cache.h"
38 #include "ieee802_11_defs.h"
39 #include "blacklist.h"
40 #include "wpas_glue.h"
41 #include "wps_supplicant.h"
43 const char *wpa_supplicant_version =
44 "wpa_supplicant v" VERSION_STR "\n"
45 "Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
47 const char *wpa_supplicant_license =
48 "This program is free software. You can distribute it and/or modify it\n"
49 "under the terms of the GNU General Public License version 2.\n"
51 "Alternatively, this software may be distributed under the terms of the\n"
52 "BSD license. See README and COPYING for more details.\n"
53 #ifdef EAP_TLS_OPENSSL
54 "\nThis product includes software developed by the OpenSSL Project\n"
55 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
56 #endif /* EAP_TLS_OPENSSL */
59 #ifndef CONFIG_NO_STDOUT_DEBUG
60 /* Long text divided into parts in order to fit in C89 strings size limits. */
61 const char *wpa_supplicant_full_license1 =
62 "This program is free software; you can redistribute it and/or modify\n"
63 "it under the terms of the GNU General Public License version 2 as\n"
64 "published by the Free Software Foundation.\n"
66 "This program is distributed in the hope that it will be useful,\n"
67 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
68 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
69 "GNU General Public License for more details.\n"
71 const char *wpa_supplicant_full_license2 =
72 "You should have received a copy of the GNU General Public License\n"
73 "along with this program; if not, write to the Free Software\n"
74 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
76 "Alternatively, this software may be distributed under the terms of the\n"
79 "Redistribution and use in source and binary forms, with or without\n"
80 "modification, are permitted provided that the following conditions are\n"
83 const char *wpa_supplicant_full_license3 =
84 "1. Redistributions of source code must retain the above copyright\n"
85 " notice, this list of conditions and the following disclaimer.\n"
87 "2. Redistributions in binary form must reproduce the above copyright\n"
88 " notice, this list of conditions and the following disclaimer in the\n"
89 " documentation and/or other materials provided with the distribution.\n"
91 const char *wpa_supplicant_full_license4 =
92 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
93 " names of its contributors may be used to endorse or promote products\n"
94 " derived from this software without specific prior written permission.\n"
96 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
97 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
98 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
99 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
100 const char *wpa_supplicant_full_license5 =
101 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
102 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
103 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
104 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
105 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
106 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
107 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
109 #endif /* CONFIG_NO_STDOUT_DEBUG */
111 extern int wpa_debug_level;
112 extern int wpa_debug_show_keys;
113 extern int wpa_debug_timestamp;
115 /* Configure default/group WEP keys for static WEP */
116 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
117 struct wpa_ssid *ssid)
121 for (i = 0; i < NUM_WEP_KEYS; i++) {
122 if (ssid->wep_key_len[i] == 0)
126 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
127 (u8 *) "\xff\xff\xff\xff\xff\xff",
128 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
129 ssid->wep_key[i], ssid->wep_key_len[i]);
136 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
137 struct wpa_ssid *ssid)
144 /* IBSS/WPA-None uses only one key (Group) for both receiving and
145 * sending unicast and multicast packets. */
147 if (ssid->mode != IEEE80211_MODE_IBSS) {
148 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
149 "for WPA-None", ssid->mode);
153 if (!ssid->psk_set) {
154 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
158 switch (wpa_s->group_cipher) {
159 case WPA_CIPHER_CCMP:
160 os_memcpy(key, ssid->psk, 16);
164 case WPA_CIPHER_TKIP:
165 /* WPA-None uses the same Michael MIC key for both TX and RX */
166 os_memcpy(key, ssid->psk, 16 + 8);
167 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
172 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
173 "WPA-None", wpa_s->group_cipher);
177 /* TODO: should actually remember the previously used seq#, both for TX
178 * and RX from each STA.. */
180 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
181 0, 1, seq, 6, key, keylen);
185 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
187 struct wpa_supplicant *wpa_s = eloop_ctx;
188 const u8 *bssid = wpa_s->bssid;
189 if (is_zero_ether_addr(bssid))
190 bssid = wpa_s->pending_bssid;
191 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
193 wpa_blacklist_add(wpa_s, bssid);
194 wpa_sm_notify_disassoc(wpa_s->wpa);
195 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
196 wpa_s->reassociate = 1;
197 wpa_supplicant_req_scan(wpa_s, 0, 0);
202 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
203 * @wpa_s: Pointer to wpa_supplicant data
204 * @sec: Number of seconds after which to time out authentication
205 * @usec: Number of microseconds after which to time out authentication
207 * This function is used to schedule a timeout for the current authentication
210 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
213 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
214 wpa_s->driver && IS_WIRED(wpa_s->driver))
217 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
218 "%d usec", sec, usec);
219 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
220 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
225 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
226 * @wpa_s: Pointer to wpa_supplicant data
228 * This function is used to cancel authentication timeout scheduled with
229 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
232 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
234 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
235 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
236 wpa_blacklist_del(wpa_s, wpa_s->bssid);
241 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
242 * @wpa_s: Pointer to wpa_supplicant data
244 * This function is used to configure EAPOL state machine based on the selected
245 * authentication mode.
247 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
249 #ifdef IEEE8021X_EAPOL
250 struct eapol_config eapol_conf;
251 struct wpa_ssid *ssid = wpa_s->current_ssid;
253 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
254 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
256 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
257 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
258 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
260 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
262 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
263 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
264 eapol_conf.accept_802_1x_keys = 1;
265 eapol_conf.required_keys = 0;
266 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
267 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
269 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
270 eapol_conf.required_keys |=
271 EAPOL_REQUIRE_KEY_BROADCAST;
274 if (wpa_s->conf && wpa_s->driver && IS_WIRED(wpa_s->driver)) {
275 eapol_conf.required_keys = 0;
279 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
280 eapol_conf.workaround = ssid->eap_workaround;
281 eapol_conf.eap_disabled =
282 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
283 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
284 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
285 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
286 #endif /* IEEE8021X_EAPOL */
291 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
292 * @wpa_s: Pointer to wpa_supplicant data
293 * @ssid: Configuration data for the network
295 * This function is used to configure WPA state machine and related parameters
296 * to a mode where WPA is not enabled. This is called as part of the
297 * authentication configuration when the selected network does not use WPA.
299 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
300 struct wpa_ssid *ssid)
304 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
305 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
306 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
307 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
309 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
310 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
311 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
312 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
313 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
314 wpa_s->group_cipher = WPA_CIPHER_NONE;
315 wpa_s->mgmt_group_cipher = 0;
317 for (i = 0; i < NUM_WEP_KEYS; i++) {
318 if (ssid->wep_key_len[i] > 5) {
319 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
320 wpa_s->group_cipher = WPA_CIPHER_WEP104;
322 } else if (ssid->wep_key_len[i] > 0) {
323 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
324 wpa_s->group_cipher = WPA_CIPHER_WEP40;
329 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
330 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
331 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
332 wpa_s->pairwise_cipher);
333 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
334 #ifdef CONFIG_IEEE80211W
335 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
336 wpa_s->mgmt_group_cipher);
337 #endif /* CONFIG_IEEE80211W */
339 pmksa_cache_clear_current(wpa_s->wpa);
343 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
345 scard_deinit(wpa_s->scard);
347 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
348 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
349 l2_packet_deinit(wpa_s->l2);
352 l2_packet_deinit(wpa_s->l2_br);
356 if (wpa_s->ctrl_iface) {
357 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
358 wpa_s->ctrl_iface = NULL;
360 if (wpa_s->conf != NULL) {
361 wpa_config_free(wpa_s->conf);
365 os_free(wpa_s->confname);
366 wpa_s->confname = NULL;
368 wpa_sm_set_eapol(wpa_s->wpa, NULL);
369 eapol_sm_deinit(wpa_s->eapol);
372 rsn_preauth_deinit(wpa_s->wpa);
374 pmksa_candidate_free(wpa_s->wpa);
375 wpa_sm_deinit(wpa_s->wpa);
377 wpa_blacklist_clear(wpa_s);
379 wpa_scan_results_free(wpa_s->scan_res);
380 wpa_s->scan_res = NULL;
382 wpa_supplicant_cancel_scan(wpa_s);
383 wpa_supplicant_cancel_auth_timeout(wpa_s);
385 ieee80211_sta_deinit(wpa_s);
387 wpas_wps_deinit(wpa_s);
392 * wpa_clear_keys - Clear keys configured for the driver
393 * @wpa_s: Pointer to wpa_supplicant data
394 * @addr: Previously used BSSID or %NULL if not available
396 * This function clears the encryption keys that has been previously configured
399 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
401 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
403 if (wpa_s->keys_cleared) {
404 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
405 * timing issues with keys being cleared just before new keys
406 * are set or just after association or something similar. This
407 * shows up in group key handshake failing often because of the
408 * client not receiving the first encrypted packets correctly.
409 * Skipping some of the extra key clearing steps seems to help
410 * in completing group key handshake more reliably. */
411 wpa_printf(MSG_DEBUG, "No keys have been configured - "
412 "skip key clearing");
416 /* MLME-DELETEKEYS.request */
417 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
418 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
419 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
420 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
422 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
424 /* MLME-SETPROTECTION.request(None) */
425 wpa_drv_mlme_setprotection(
427 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
428 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
430 wpa_s->keys_cleared = 1;
435 * wpa_supplicant_state_txt - Get the connection state name as a text string
436 * @state: State (wpa_state; WPA_*)
437 * Returns: The state name as a printable text string
439 const char * wpa_supplicant_state_txt(int state)
442 case WPA_DISCONNECTED:
443 return "DISCONNECTED";
448 case WPA_ASSOCIATING:
449 return "ASSOCIATING";
452 case WPA_4WAY_HANDSHAKE:
453 return "4WAY_HANDSHAKE";
454 case WPA_GROUP_HANDSHAKE:
455 return "GROUP_HANDSHAKE";
465 * wpa_supplicant_set_state - Set current connection state
466 * @wpa_s: Pointer to wpa_supplicant data
467 * @state: The new connection state
469 * This function is called whenever the connection state changes, e.g.,
470 * association is completed for WPA/WPA2 4-Way Handshake is started.
472 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
474 wpa_printf(MSG_DEBUG, "State: %s -> %s",
475 wpa_supplicant_state_txt(wpa_s->wpa_state),
476 wpa_supplicant_state_txt(state));
478 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
481 if (state == WPA_COMPLETED && wpa_s->new_connection) {
482 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
483 struct wpa_ssid *ssid = wpa_s->current_ssid;
484 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
485 MACSTR " completed %s [id=%d id_str=%s]",
486 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
487 "(reauth)" : "(auth)",
488 ssid ? ssid->id : -1,
489 ssid && ssid->id_str ? ssid->id_str : "");
490 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
491 wpa_s->new_connection = 0;
492 wpa_s->reassociated_connection = 1;
493 wpa_drv_set_operstate(wpa_s, 1);
494 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
495 state == WPA_ASSOCIATED) {
496 wpa_s->new_connection = 1;
497 wpa_drv_set_operstate(wpa_s, 0);
499 wpa_s->wpa_state = state;
503 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
506 struct wpa_global *global = eloop_ctx;
507 struct wpa_supplicant *wpa_s;
508 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
509 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
516 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
518 wpa_s->pairwise_cipher = 0;
519 wpa_s->group_cipher = 0;
520 wpa_s->mgmt_group_cipher = 0;
522 wpa_s->wpa_state = WPA_DISCONNECTED;
527 * wpa_supplicant_reload_configuration - Reload configuration data
528 * @wpa_s: Pointer to wpa_supplicant data
529 * Returns: 0 on success or -1 if configuration parsing failed
531 * This function can be used to request that the configuration data is reloaded
532 * (e.g., after configuration file change). This function is reloading
533 * configuration only for one interface, so this may need to be called multiple
534 * times if %wpa_supplicant is controlling multiple interfaces and all
535 * interfaces need reconfiguration.
537 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
539 struct wpa_config *conf;
541 if (wpa_s->confname == NULL)
543 conf = wpa_config_read(wpa_s->confname);
545 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
546 "file '%s' - exiting", wpa_s->confname);
550 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
551 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
552 os_strcmp(conf->ctrl_interface,
553 wpa_s->conf->ctrl_interface) != 0);
555 if (reconf_ctrl && wpa_s->ctrl_iface) {
556 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
557 wpa_s->ctrl_iface = NULL;
560 eapol_sm_invalidate_cached_session(wpa_s->eapol);
561 wpa_s->current_ssid = NULL;
563 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
564 * pkcs11_engine_path, pkcs11_module_path.
566 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
568 * Clear forced success to clear EAP state for next
571 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
573 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
574 wpa_sm_set_config(wpa_s->wpa, NULL);
575 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
576 rsn_preauth_deinit(wpa_s->wpa);
577 wpa_config_free(wpa_s->conf);
580 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
582 wpa_supplicant_clear_status(wpa_s);
583 wpa_s->reassociate = 1;
584 wpa_supplicant_req_scan(wpa_s, 0, 0);
585 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
590 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
593 struct wpa_global *global = eloop_ctx;
594 struct wpa_supplicant *wpa_s;
595 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
596 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
597 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
604 static wpa_cipher cipher_suite2driver(int cipher)
607 case WPA_CIPHER_NONE:
609 case WPA_CIPHER_WEP40:
611 case WPA_CIPHER_WEP104:
612 return CIPHER_WEP104;
613 case WPA_CIPHER_CCMP:
615 case WPA_CIPHER_TKIP:
622 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
625 case WPA_KEY_MGMT_NONE:
626 return KEY_MGMT_NONE;
627 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
628 return KEY_MGMT_802_1X_NO_WPA;
629 case WPA_KEY_MGMT_IEEE8021X:
630 return KEY_MGMT_802_1X;
631 case WPA_KEY_MGMT_WPA_NONE:
632 return KEY_MGMT_WPA_NONE;
633 case WPA_KEY_MGMT_FT_IEEE8021X:
634 return KEY_MGMT_FT_802_1X;
635 case WPA_KEY_MGMT_FT_PSK:
636 return KEY_MGMT_FT_PSK;
637 case WPA_KEY_MGMT_IEEE8021X_SHA256:
638 return KEY_MGMT_802_1X_SHA256;
639 case WPA_KEY_MGMT_PSK_SHA256:
640 return KEY_MGMT_PSK_SHA256;
641 case WPA_KEY_MGMT_WPS:
643 case WPA_KEY_MGMT_PSK:
650 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
651 struct wpa_ssid *ssid,
652 struct wpa_ie_data *ie)
654 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
657 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
658 "from association info");
663 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
665 if (!(ie->group_cipher & ssid->group_cipher)) {
666 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
667 "cipher 0x%x (mask 0x%x) - reject",
668 ie->group_cipher, ssid->group_cipher);
671 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
672 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
673 "cipher 0x%x (mask 0x%x) - reject",
674 ie->pairwise_cipher, ssid->pairwise_cipher);
677 if (!(ie->key_mgmt & ssid->key_mgmt)) {
678 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
679 "management 0x%x (mask 0x%x) - reject",
680 ie->key_mgmt, ssid->key_mgmt);
684 #ifdef CONFIG_IEEE80211W
685 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
686 ssid->ieee80211w == IEEE80211W_REQUIRED) {
687 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
688 "that does not support management frame protection - "
692 #endif /* CONFIG_IEEE80211W */
699 * wpa_supplicant_set_suites - Set authentication and encryption parameters
700 * @wpa_s: Pointer to wpa_supplicant data
701 * @bss: Scan results for the selected BSS, or %NULL if not available
702 * @ssid: Configuration data for the selected network
703 * @wpa_ie: Buffer for the WPA/RSN IE
704 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
705 * used buffer length in case the functions returns success.
706 * Returns: 0 on success or -1 on failure
708 * This function is used to configure authentication and encryption parameters
709 * based on the network configuration and scan result for the selected BSS (if
712 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
713 struct wpa_scan_res *bss,
714 struct wpa_ssid *ssid,
715 u8 *wpa_ie, size_t *wpa_ie_len)
717 struct wpa_ie_data ie;
719 const u8 *bss_wpa, *bss_rsn;
722 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
723 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
725 bss_wpa = bss_rsn = NULL;
727 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
728 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
729 (ie.group_cipher & ssid->group_cipher) &&
730 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
731 (ie.key_mgmt & ssid->key_mgmt)) {
732 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
733 proto = WPA_PROTO_RSN;
734 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
735 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
736 (ie.group_cipher & ssid->group_cipher) &&
737 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
738 (ie.key_mgmt & ssid->key_mgmt)) {
739 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
740 proto = WPA_PROTO_WPA;
742 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
745 if (ssid->proto & WPA_PROTO_RSN)
746 proto = WPA_PROTO_RSN;
748 proto = WPA_PROTO_WPA;
749 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
750 os_memset(&ie, 0, sizeof(ie));
751 ie.group_cipher = ssid->group_cipher;
752 ie.pairwise_cipher = ssid->pairwise_cipher;
753 ie.key_mgmt = ssid->key_mgmt;
754 #ifdef CONFIG_IEEE80211W
755 ie.mgmt_group_cipher =
756 ssid->ieee80211w != NO_IEEE80211W ?
757 WPA_CIPHER_AES_128_CMAC : 0;
758 #endif /* CONFIG_IEEE80211W */
759 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
765 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
766 "pairwise %d key_mgmt %d proto %d",
767 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
768 #ifdef CONFIG_IEEE80211W
769 if (ssid->ieee80211w) {
770 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
771 ie.mgmt_group_cipher);
773 #endif /* CONFIG_IEEE80211W */
775 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
776 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
777 !!(ssid->proto & WPA_PROTO_RSN));
779 if (bss || !wpa_s->ap_ies_from_associnfo) {
780 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
781 bss_wpa ? 2 + bss_wpa[1] : 0) ||
782 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
783 bss_rsn ? 2 + bss_rsn[1] : 0))
787 sel = ie.group_cipher & ssid->group_cipher;
788 if (sel & WPA_CIPHER_CCMP) {
789 wpa_s->group_cipher = WPA_CIPHER_CCMP;
790 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
791 } else if (sel & WPA_CIPHER_TKIP) {
792 wpa_s->group_cipher = WPA_CIPHER_TKIP;
793 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
794 } else if (sel & WPA_CIPHER_WEP104) {
795 wpa_s->group_cipher = WPA_CIPHER_WEP104;
796 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
797 } else if (sel & WPA_CIPHER_WEP40) {
798 wpa_s->group_cipher = WPA_CIPHER_WEP40;
799 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
801 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
805 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
806 if (sel & WPA_CIPHER_CCMP) {
807 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
808 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
809 } else if (sel & WPA_CIPHER_TKIP) {
810 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
811 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
812 } else if (sel & WPA_CIPHER_NONE) {
813 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
814 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
816 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
821 sel = ie.key_mgmt & ssid->key_mgmt;
823 #ifdef CONFIG_IEEE80211R
824 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
825 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
826 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
827 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
828 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
829 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
830 #endif /* CONFIG_IEEE80211R */
831 #ifdef CONFIG_IEEE80211W
832 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
833 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
834 wpa_msg(wpa_s, MSG_DEBUG,
835 "WPA: using KEY_MGMT 802.1X with SHA256");
836 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
837 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
838 wpa_msg(wpa_s, MSG_DEBUG,
839 "WPA: using KEY_MGMT PSK with SHA256");
840 #endif /* CONFIG_IEEE80211W */
841 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
842 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
843 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
844 } else if (sel & WPA_KEY_MGMT_PSK) {
845 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
846 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
847 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
848 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
849 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
851 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
852 "key management type.");
856 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
857 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
858 wpa_s->pairwise_cipher);
859 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
861 #ifdef CONFIG_IEEE80211W
862 sel = ie.mgmt_group_cipher;
863 if (ssid->ieee80211w == NO_IEEE80211W ||
864 !(ie.capabilities & WPA_CAPABILITY_MFPC))
866 if (sel & WPA_CIPHER_AES_128_CMAC) {
867 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
868 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
871 wpa_s->mgmt_group_cipher = 0;
872 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
874 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
875 wpa_s->mgmt_group_cipher);
876 #endif /* CONFIG_IEEE80211W */
878 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
879 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
884 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
885 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
887 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
894 * wpa_supplicant_associate - Request association
895 * @wpa_s: Pointer to wpa_supplicant data
896 * @bss: Scan results for the selected BSS, or %NULL if not available
897 * @ssid: Configuration data for the selected network
899 * This function is used to request %wpa_supplicant to associate with a BSS.
901 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
902 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
906 int use_crypt, ret, i;
907 int algs = AUTH_ALG_OPEN_SYSTEM;
908 wpa_cipher cipher_pairwise, cipher_group;
909 struct wpa_driver_associate_params params;
910 int wep_keys_set = 0;
911 struct wpa_driver_capa capa;
912 int assoc_failed = 0;
914 wpa_s->reassociate = 0;
916 #ifdef CONFIG_IEEE80211R
918 #endif /* CONFIG_IEEE80211R */
919 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
920 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
921 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
922 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
923 os_memset(wpa_s->bssid, 0, ETH_ALEN);
924 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
925 #ifdef CONFIG_IEEE80211R
926 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
927 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
929 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
931 /* Prepare for the next transition */
932 wpa_ft_prepare_auth_request(wpa_s->wpa);
934 #endif /* CONFIG_IEEE80211R */
936 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
937 wpa_s->conf->ap_scan == 2 &&
938 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
939 /* Use ap_scan==1 style network selection to find the network
942 wpa_s->reassociate = 1;
943 wpa_supplicant_req_scan(wpa_s, 0, 0);
945 #endif /* CONFIG_WPS */
947 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
948 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
949 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
951 wpa_supplicant_cancel_scan(wpa_s);
953 /* Starting new association, so clear the possibly used WPA IE from the
954 * previous association. */
955 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
957 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
958 wpa_printf(MSG_WARNING, "Failed to set operating mode");
962 #ifdef IEEE8021X_EAPOL
963 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
965 if (ssid->non_leap == 0)
966 algs = AUTH_ALG_LEAP;
968 algs |= AUTH_ALG_LEAP;
971 #endif /* IEEE8021X_EAPOL */
972 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
973 if (ssid->auth_alg) {
975 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
976 algs |= AUTH_ALG_OPEN_SYSTEM;
977 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
978 algs |= AUTH_ALG_SHARED_KEY;
979 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
980 algs |= AUTH_ALG_LEAP;
981 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
984 wpa_drv_set_auth_alg(wpa_s, algs);
986 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
987 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
988 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
989 WPA_KEY_MGMT_FT_IEEE8021X |
990 WPA_KEY_MGMT_FT_PSK |
991 WPA_KEY_MGMT_IEEE8021X_SHA256 |
992 WPA_KEY_MGMT_PSK_SHA256))) {
993 int try_opportunistic;
994 try_opportunistic = ssid->proactive_key_caching &&
995 (ssid->proto & WPA_PROTO_RSN);
996 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
998 try_opportunistic) == 0)
999 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1000 wpa_ie_len = sizeof(wpa_ie);
1001 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1002 wpa_ie, &wpa_ie_len)) {
1003 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1004 "management and encryption suites");
1007 } else if (ssid->key_mgmt &
1008 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1009 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1010 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1011 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1012 wpa_ie_len = sizeof(wpa_ie);
1013 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1014 wpa_ie, &wpa_ie_len)) {
1015 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1016 "management and encryption suites (no scan "
1021 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1022 struct wpabuf *wps_ie;
1023 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1024 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1025 wpa_ie_len = wpabuf_len(wps_ie);
1026 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1029 wpabuf_free(wps_ie);
1030 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1031 #endif /* CONFIG_WPS */
1033 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1037 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1039 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1040 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1041 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1042 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1043 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1045 if (wpa_set_wep_keys(wpa_s, ssid)) {
1050 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1053 #ifdef IEEE8021X_EAPOL
1054 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1055 if ((ssid->eapol_flags &
1056 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1057 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1061 /* Assume that dynamic WEP-104 keys will be used and
1062 * set cipher suites in order for drivers to expect
1064 cipher_pairwise = cipher_group = CIPHER_WEP104;
1067 #endif /* IEEE8021X_EAPOL */
1069 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1070 /* Set the key before (and later after) association */
1071 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1074 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1075 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1076 os_memset(¶ms, 0, sizeof(params));
1078 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1079 params.bssid = bss->bssid;
1080 params.ssid = ie ? ie + 2 : (u8 *) "";
1081 params.ssid_len = ie ? ie[1] : 0;
1082 params.freq = bss->freq;
1084 params.ssid = ssid->ssid;
1085 params.ssid_len = ssid->ssid_len;
1087 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1088 params.freq = ssid->frequency; /* Initial channel for IBSS */
1089 params.wpa_ie = wpa_ie;
1090 params.wpa_ie_len = wpa_ie_len;
1091 params.pairwise_suite = cipher_pairwise;
1092 params.group_suite = cipher_group;
1093 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1094 params.auth_alg = algs;
1095 params.mode = ssid->mode;
1096 for (i = 0; i < NUM_WEP_KEYS; i++) {
1097 if (ssid->wep_key_len[i])
1098 params.wep_key[i] = ssid->wep_key[i];
1099 params.wep_key_len[i] = ssid->wep_key_len[i];
1101 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1103 if (wpa_s->driver_4way_handshake &&
1104 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1105 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1106 params.passphrase = ssid->passphrase;
1108 params.psk = ssid->psk;
1111 #ifdef CONFIG_IEEE80211W
1112 switch (ssid->ieee80211w) {
1114 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1116 case IEEE80211W_OPTIONAL:
1117 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1119 case IEEE80211W_REQUIRED:
1120 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1123 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1124 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1125 struct wpa_ie_data ie;
1126 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1128 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1129 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1131 params.mgmt_frame_protection =
1132 MGMT_FRAME_PROTECTION_REQUIRED;
1135 #endif /* CONFIG_IEEE80211W */
1137 if (wpa_s->use_client_mlme)
1138 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1140 ret = wpa_drv_associate(wpa_s, ¶ms);
1142 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1144 /* try to continue anyway; new association will be tried again
1149 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1150 /* Set the key after the association just in case association
1151 * cleared the previously configured key. */
1152 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1153 /* No need to timeout authentication since there is no key
1155 wpa_supplicant_cancel_auth_timeout(wpa_s);
1156 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1158 /* Timeout for IEEE 802.11 authentication and association */
1162 /* give IBSS a bit more time */
1163 timeout = ssid->mode ? 10 : 5;
1164 } else if (wpa_s->conf->ap_scan == 1) {
1165 /* give IBSS a bit more time */
1166 timeout = ssid->mode ? 20 : 10;
1168 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1171 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1172 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1173 /* Set static WEP keys again */
1174 wpa_set_wep_keys(wpa_s, ssid);
1177 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1179 * Do not allow EAP session resumption between different
1180 * network configurations.
1182 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1184 wpa_s->current_ssid = ssid;
1185 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1186 wpa_supplicant_initiate_eapol(wpa_s);
1191 * wpa_supplicant_disassociate - Disassociate the current connection
1192 * @wpa_s: Pointer to wpa_supplicant data
1193 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1195 * This function is used to request %wpa_supplicant to disassociate with the
1198 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1202 if (!is_zero_ether_addr(wpa_s->bssid)) {
1203 if (wpa_s->use_client_mlme)
1204 ieee80211_sta_disassociate(wpa_s, reason_code);
1206 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1207 addr = wpa_s->bssid;
1209 wpa_clear_keys(wpa_s, addr);
1210 wpa_supplicant_mark_disassoc(wpa_s);
1211 wpa_s->current_ssid = NULL;
1212 wpa_sm_set_config(wpa_s->wpa, NULL);
1213 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1218 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1219 * @wpa_s: Pointer to wpa_supplicant data
1220 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1222 * This function is used to request %wpa_supplicant to disassociate with the
1225 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1229 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1230 if (!is_zero_ether_addr(wpa_s->bssid)) {
1231 if (wpa_s->use_client_mlme)
1232 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1234 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1236 addr = wpa_s->bssid;
1238 wpa_clear_keys(wpa_s, addr);
1239 wpa_s->current_ssid = NULL;
1240 wpa_sm_set_config(wpa_s->wpa, NULL);
1241 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1242 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1243 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1247 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1249 #define SCAN_AP_LIMIT 128
1250 struct wpa_scan_result *results;
1252 struct wpa_scan_results *res;
1254 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1255 if (results == NULL) {
1256 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1261 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1262 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1264 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1268 if (num > SCAN_AP_LIMIT) {
1269 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1270 num, SCAN_AP_LIMIT);
1271 num = SCAN_AP_LIMIT;
1274 wpa_scan_results_free(wpa_s->scan_res);
1275 wpa_s->scan_res = NULL;
1277 /* Convert old scan result data structure to the new one */
1278 res = os_zalloc(sizeof(*res));
1283 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1284 if (res->res == NULL) {
1290 for (i = 0; i < num; i++) {
1291 struct wpa_scan_result *bss = &results[i];
1292 struct wpa_scan_res *r;
1296 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1299 if (bss->mdie_present)
1302 r = os_zalloc(sizeof(*r) + ie_len);
1306 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1307 r->freq = bss->freq;
1308 r->caps = bss->caps;
1309 r->qual = bss->qual;
1310 r->noise = bss->noise;
1311 r->level = bss->level;
1315 pos = (u8 *) (r + 1);
1318 *pos++ = WLAN_EID_SSID;
1319 *pos++ = bss->ssid_len;
1320 os_memcpy(pos, bss->ssid, bss->ssid_len);
1321 pos += bss->ssid_len;
1324 /* Fake Supported Rate IE to include max rate */
1325 *pos++ = WLAN_EID_SUPP_RATES;
1327 *pos++ = bss->maxrate;
1330 if (bss->rsn_ie_len) {
1331 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1332 pos += bss->rsn_ie_len;
1335 if (bss->mdie_present) {
1336 os_memcpy(pos, bss->mdie, 5);
1340 if (bss->wpa_ie_len) {
1341 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1342 pos += bss->wpa_ie_len;
1345 res->res[res->num++] = r;
1349 wpa_s->scan_res = res;
1356 * wpa_supplicant_get_scan_results - Get scan results
1357 * @wpa_s: Pointer to wpa_supplicant data
1358 * Returns: 0 on success, -1 on failure
1360 * This function is request the current scan results from the driver and stores
1361 * a local copy of the results in wpa_s->scan_res.
1363 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1367 if (wpa_s->use_client_mlme) {
1368 wpa_scan_results_free(wpa_s->scan_res);
1369 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1370 if (wpa_s->scan_res == NULL) {
1371 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1375 } else if (wpa_s->driver->get_scan_results2 == NULL)
1376 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1378 wpa_scan_results_free(wpa_s->scan_res);
1379 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1380 if (wpa_s->scan_res == NULL) {
1381 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1387 if (wpa_s->scan_res)
1388 wpa_scan_sort_results(wpa_s->scan_res);
1395 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1396 * @wpa_s: Pointer to wpa_supplicant data
1397 * Returns: A pointer to the current network structure or %NULL on failure
1399 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1401 struct wpa_ssid *entry;
1402 u8 ssid[MAX_SSID_LEN];
1408 if (wpa_s->use_client_mlme) {
1409 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1410 wpa_printf(MSG_WARNING, "Could not read SSID from "
1415 res = wpa_drv_get_ssid(wpa_s, ssid);
1417 wpa_printf(MSG_WARNING, "Could not read SSID from "
1424 if (wpa_s->use_client_mlme)
1425 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1426 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1427 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1431 wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
1432 IS_WIRED(wpa_s->driver);
1434 entry = wpa_s->conf->ssid;
1436 if (!entry->disabled &&
1437 ((ssid_len == entry->ssid_len &&
1438 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1439 (!entry->bssid_set ||
1440 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1443 if (!entry->disabled &&
1444 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1445 (entry->ssid == NULL || entry->ssid_len == 0) &&
1446 (!entry->bssid_set ||
1447 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1449 #endif /* CONFIG_WPS */
1450 entry = entry->next;
1457 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1465 if (wpa_supplicant_drivers[0] == NULL) {
1466 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1472 /* default to first driver in the list */
1473 wpa_s->driver = wpa_supplicant_drivers[0];
1477 for (i = 0; wpa_supplicant_drivers[i]; i++) {
1478 if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1479 wpa_s->driver = wpa_supplicant_drivers[i];
1484 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
1489 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1490 const u8 *buf, size_t len)
1492 struct wpa_supplicant *wpa_s = ctx;
1494 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1495 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1497 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1498 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1499 "no key management is configured");
1503 if (wpa_s->eapol_received == 0 &&
1504 (!wpa_s->driver_4way_handshake ||
1505 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1506 wpa_s->wpa_state != WPA_COMPLETED)) {
1507 /* Timeout for completing IEEE 802.1X and WPA authentication */
1508 wpa_supplicant_req_auth_timeout(
1510 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1511 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1512 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1515 wpa_s->eapol_received++;
1517 if (wpa_s->countermeasures) {
1518 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1523 /* Source address of the incoming EAPOL frame could be compared to the
1524 * current BSSID. However, it is possible that a centralized
1525 * Authenticator could be using another MAC address than the BSSID of
1526 * an AP, so just allow any address to be used for now. The replies are
1527 * still sent to the current BSSID (if available), though. */
1529 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1530 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1531 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1533 wpa_drv_poll(wpa_s);
1534 if (!wpa_s->driver_4way_handshake)
1535 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1536 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1538 * Set portValid = TRUE here since we are going to skip 4-way
1539 * handshake processing which would normally set portValid. We
1540 * need this to allow the EAPOL state machines to be completed
1541 * without going through EAPOL-Key handshake.
1543 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1548 void wpa_supplicant_sta_free_hw_features(struct wpa_hw_modes *hw_features,
1549 size_t num_hw_features)
1551 ieee80211_sta_free_hw_features(hw_features, num_hw_features);
1555 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1556 struct ieee80211_rx_status *rx_status)
1558 struct wpa_supplicant *wpa_s = ctx;
1559 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1564 * wpa_supplicant_driver_init - Initialize driver interface parameters
1565 * @wpa_s: Pointer to wpa_supplicant data
1566 * Returns: 0 on success, -1 on failure
1568 * This function is called to initialize driver interface parameters.
1569 * wpa_drv_init() must have been called before this function to initialize the
1572 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1574 static int interface_count = 0;
1576 if (wpa_s->driver->send_eapol) {
1577 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1579 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1581 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1582 wpa_drv_get_mac_addr(wpa_s),
1584 wpa_supplicant_rx_eapol, wpa_s, 0);
1585 if (wpa_s->l2 == NULL)
1589 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1590 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1594 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1595 MAC2STR(wpa_s->own_addr));
1597 if (wpa_s->bridge_ifname[0]) {
1598 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1599 " '%s'", wpa_s->bridge_ifname);
1600 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1603 wpa_supplicant_rx_eapol, wpa_s,
1605 if (wpa_s->l2_br == NULL) {
1606 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1607 "connection for the bridge interface '%s'",
1608 wpa_s->bridge_ifname);
1613 /* Backwards compatibility call to set_wpa() handler. This is called
1614 * only just after init and just before deinit, so these handler can be
1615 * used to implement same functionality. */
1616 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1617 struct wpa_driver_capa capa;
1618 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1619 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1620 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1621 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1622 /* Continue to allow non-WPA modes to be used. */
1624 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1630 wpa_clear_keys(wpa_s, NULL);
1632 /* Make sure that TKIP countermeasures are not left enabled (could
1633 * happen if wpa_supplicant is killed during countermeasures. */
1634 wpa_drv_set_countermeasures(wpa_s, 0);
1636 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1638 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1639 wpa_drv_flush_pmkid(wpa_s);
1641 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1642 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1649 static int wpa_supplicant_daemon(const char *pid_file)
1651 wpa_printf(MSG_DEBUG, "Daemonize..");
1652 return os_daemonize(pid_file);
1656 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1658 struct wpa_supplicant *wpa_s;
1660 wpa_s = os_zalloc(sizeof(*wpa_s));
1663 wpa_s->scan_req = 1;
1669 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1670 struct wpa_interface *iface)
1672 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1673 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1674 iface->confname ? iface->confname : "N/A",
1675 iface->driver ? iface->driver : "default",
1676 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1677 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1679 if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
1683 if (iface->confname) {
1684 #ifdef CONFIG_BACKEND_FILE
1685 wpa_s->confname = os_rel2abs_path(iface->confname);
1686 if (wpa_s->confname == NULL) {
1687 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1688 "for configuration file '%s'.",
1692 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1693 iface->confname, wpa_s->confname);
1694 #else /* CONFIG_BACKEND_FILE */
1695 wpa_s->confname = os_strdup(iface->confname);
1696 #endif /* CONFIG_BACKEND_FILE */
1697 wpa_s->conf = wpa_config_read(wpa_s->confname);
1698 if (wpa_s->conf == NULL) {
1699 wpa_printf(MSG_ERROR, "Failed to read or parse "
1700 "configuration '%s'.", wpa_s->confname);
1705 * Override ctrl_interface and driver_param if set on command
1708 if (iface->ctrl_interface) {
1709 os_free(wpa_s->conf->ctrl_interface);
1710 wpa_s->conf->ctrl_interface =
1711 os_strdup(iface->ctrl_interface);
1714 if (iface->driver_param) {
1715 os_free(wpa_s->conf->driver_param);
1716 wpa_s->conf->driver_param =
1717 os_strdup(iface->driver_param);
1720 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1721 iface->driver_param);
1723 if (wpa_s->conf == NULL) {
1724 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1728 if (iface->ifname == NULL) {
1729 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1732 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1733 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1737 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1739 if (iface->bridge_ifname) {
1740 if (os_strlen(iface->bridge_ifname) >=
1741 sizeof(wpa_s->bridge_ifname)) {
1742 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1743 "name '%s'.", iface->bridge_ifname);
1746 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1747 sizeof(wpa_s->bridge_ifname));
1754 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s)
1757 struct wpa_driver_capa capa;
1759 wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
1762 /* RSNA Supplicant Key Management - INITIALIZE */
1763 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1764 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1766 /* Initialize driver interface and register driver event handler before
1767 * L2 receive handler so that association events are processed before
1768 * EAPOL-Key packets if both become available for the same select()
1770 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1771 if (wpa_s->drv_priv == NULL) {
1772 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1775 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1776 wpa_printf(MSG_ERROR, "Driver interface rejected "
1777 "driver_param '%s'", wpa_s->conf->driver_param);
1781 ifname = wpa_drv_get_ifname(wpa_s);
1782 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1783 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1784 "name with '%s'", ifname);
1785 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1788 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1791 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1792 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1794 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1796 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1797 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1798 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1799 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1800 "dot11RSNAConfigPMKLifetime");
1804 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1805 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1806 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1807 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1808 "dot11RSNAConfigPMKReauthThreshold");
1812 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1813 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1814 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1815 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1816 "dot11RSNAConfigSATimeout");
1820 if (wpa_supplicant_driver_init(wpa_s) < 0)
1823 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
1824 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
1825 wpa_printf(MSG_DEBUG, "Failed to set country");
1829 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1831 if (wpas_wps_init(wpa_s))
1834 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1836 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1838 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1839 if (wpa_s->ctrl_iface == NULL) {
1840 wpa_printf(MSG_ERROR,
1841 "Failed to initialize control interface '%s'.\n"
1842 "You may have another wpa_supplicant process "
1843 "already running or the file was\n"
1844 "left by an unclean termination of wpa_supplicant "
1845 "in which case you will need\n"
1846 "to manually remove this file before starting "
1847 "wpa_supplicant again.\n",
1848 wpa_s->conf->ctrl_interface);
1852 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1853 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1854 wpa_s->use_client_mlme = 1;
1855 if (ieee80211_sta_init(wpa_s))
1858 if (capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)
1859 wpa_s->driver_4way_handshake = 1;
1866 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1868 if (wpa_s->drv_priv) {
1869 wpa_supplicant_deauthenticate(wpa_s,
1870 WLAN_REASON_DEAUTH_LEAVING);
1872 /* Backwards compatibility call to set_wpa() handler. This is
1873 * called only just after init and just before deinit, so these
1874 * handler can be used to implement same functionality. */
1875 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1876 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
1880 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1881 wpa_drv_set_countermeasures(wpa_s, 0);
1882 wpa_clear_keys(wpa_s, NULL);
1885 wpas_dbus_unregister_iface(wpa_s);
1887 wpa_supplicant_cleanup(wpa_s);
1889 if (wpa_s->drv_priv)
1890 wpa_drv_deinit(wpa_s);
1895 * wpa_supplicant_add_iface - Add a new network interface
1896 * @global: Pointer to global data from wpa_supplicant_init()
1897 * @iface: Interface configuration options
1898 * Returns: Pointer to the created interface or %NULL on failure
1900 * This function is used to add new network interfaces for %wpa_supplicant.
1901 * This can be called before wpa_supplicant_run() to add interfaces before the
1902 * main event loop has been started. In addition, new interfaces can be added
1903 * dynamically while %wpa_supplicant is already running. This could happen,
1904 * e.g., when a hotplug network adapter is inserted.
1906 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
1907 struct wpa_interface *iface)
1909 struct wpa_supplicant *wpa_s;
1911 if (global == NULL || iface == NULL)
1914 wpa_s = wpa_supplicant_alloc();
1918 if (wpa_supplicant_init_iface(wpa_s, iface) ||
1919 wpa_supplicant_init_iface2(wpa_s)) {
1920 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
1922 wpa_supplicant_deinit_iface(wpa_s);
1927 wpa_s->global = global;
1929 /* Register the interface with the dbus control interface */
1930 if (wpas_dbus_register_iface(wpa_s)) {
1931 wpa_supplicant_deinit_iface(wpa_s);
1936 wpa_s->next = global->ifaces;
1937 global->ifaces = wpa_s;
1939 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
1946 * wpa_supplicant_remove_iface - Remove a network interface
1947 * @global: Pointer to global data from wpa_supplicant_init()
1948 * @wpa_s: Pointer to the network interface to be removed
1949 * Returns: 0 if interface was removed, -1 if interface was not found
1951 * This function can be used to dynamically remove network interfaces from
1952 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
1953 * addition, this function is used to remove all remaining interfaces when
1954 * %wpa_supplicant is terminated.
1956 int wpa_supplicant_remove_iface(struct wpa_global *global,
1957 struct wpa_supplicant *wpa_s)
1959 struct wpa_supplicant *prev;
1961 /* Remove interface from the global list of interfaces */
1962 prev = global->ifaces;
1963 if (prev == wpa_s) {
1964 global->ifaces = wpa_s->next;
1966 while (prev && prev->next != wpa_s)
1970 prev->next = wpa_s->next;
1973 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
1975 wpa_supplicant_deinit_iface(wpa_s);
1983 * wpa_supplicant_get_iface - Get a new network interface
1984 * @global: Pointer to global data from wpa_supplicant_init()
1985 * @ifname: Interface name
1986 * Returns: Pointer to the interface or %NULL if not found
1988 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
1991 struct wpa_supplicant *wpa_s;
1993 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1994 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2002 * wpa_supplicant_init - Initialize %wpa_supplicant
2003 * @params: Parameters for %wpa_supplicant
2004 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2006 * This function is used to initialize %wpa_supplicant. After successful
2007 * initialization, the returned data pointer can be used to add and remove
2008 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2010 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2012 struct wpa_global *global;
2018 wpa_debug_open_file(params->wpa_debug_file_path);
2020 ret = eap_peer_register_methods();
2022 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2024 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2025 "the same EAP type.");
2029 global = os_zalloc(sizeof(*global));
2032 global->params.daemonize = params->daemonize;
2033 global->params.wait_for_monitor = params->wait_for_monitor;
2034 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2035 if (params->pid_file)
2036 global->params.pid_file = os_strdup(params->pid_file);
2037 if (params->ctrl_interface)
2038 global->params.ctrl_interface =
2039 os_strdup(params->ctrl_interface);
2040 wpa_debug_level = global->params.wpa_debug_level =
2041 params->wpa_debug_level;
2042 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2043 params->wpa_debug_show_keys;
2044 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2045 params->wpa_debug_timestamp;
2047 if (eloop_init(global)) {
2048 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2049 wpa_supplicant_deinit(global);
2053 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2054 if (global->ctrl_iface == NULL) {
2055 wpa_supplicant_deinit(global);
2059 if (global->params.dbus_ctrl_interface) {
2060 global->dbus_ctrl_iface =
2061 wpa_supplicant_dbus_ctrl_iface_init(global);
2062 if (global->dbus_ctrl_iface == NULL) {
2063 wpa_supplicant_deinit(global);
2068 for (i = 0; wpa_supplicant_drivers[i]; i++)
2069 global->drv_count++;
2070 if (global->drv_count == 0) {
2071 wpa_printf(MSG_ERROR, "No drivers enabled");
2072 wpa_supplicant_deinit(global);
2075 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2076 if (global->drv_priv == NULL) {
2077 wpa_supplicant_deinit(global);
2080 for (i = 0; wpa_supplicant_drivers[i]; i++) {
2081 if (!wpa_supplicant_drivers[i]->global_init)
2083 global->drv_priv[i] = wpa_supplicant_drivers[i]->global_init();
2084 if (global->drv_priv[i] == NULL) {
2085 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2086 "'%s'", wpa_supplicant_drivers[i]->name);
2087 wpa_supplicant_deinit(global);
2097 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2098 * @global: Pointer to global data from wpa_supplicant_init()
2099 * Returns: 0 after successful event loop run, -1 on failure
2101 * This function starts the main event loop and continues running as long as
2102 * there are any remaining events. In most cases, this function is running as
2103 * long as the %wpa_supplicant process in still in use.
2105 int wpa_supplicant_run(struct wpa_global *global)
2107 struct wpa_supplicant *wpa_s;
2109 if (global->params.daemonize &&
2110 wpa_supplicant_daemon(global->params.pid_file))
2113 if (global->params.wait_for_monitor) {
2114 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2115 if (wpa_s->ctrl_iface)
2116 wpa_supplicant_ctrl_iface_wait(
2120 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2121 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2130 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2131 * @global: Pointer to global data from wpa_supplicant_init()
2133 * This function is called to deinitialize %wpa_supplicant and to free all
2134 * allocated resources. Remaining network interfaces will also be removed.
2136 void wpa_supplicant_deinit(struct wpa_global *global)
2143 while (global->ifaces)
2144 wpa_supplicant_remove_iface(global, global->ifaces);
2146 if (global->ctrl_iface)
2147 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2148 if (global->dbus_ctrl_iface)
2149 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2151 eap_peer_unregister_methods();
2153 for (i = 0; wpa_supplicant_drivers[i]; i++) {
2154 if (!global->drv_priv[i])
2156 wpa_supplicant_drivers[i]->global_deinit(global->drv_priv[i]);
2158 os_free(global->drv_priv);
2162 if (global->params.pid_file) {
2163 os_daemonize_terminate(global->params.pid_file);
2164 os_free(global->params.pid_file);
2166 os_free(global->params.ctrl_interface);
2169 wpa_debug_close_file();