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"
47 const char *wpa_supplicant_version =
48 "wpa_supplicant v" VERSION_STR "\n"
49 "Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
51 const char *wpa_supplicant_license =
52 "This program is free software. You can distribute it and/or modify it\n"
53 "under the terms of the GNU General Public License version 2.\n"
55 "Alternatively, this software may be distributed under the terms of the\n"
56 "BSD license. See README and COPYING for more details.\n"
57 #ifdef EAP_TLS_OPENSSL
58 "\nThis product includes software developed by the OpenSSL Project\n"
59 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
60 #endif /* EAP_TLS_OPENSSL */
63 #ifndef CONFIG_NO_STDOUT_DEBUG
64 /* Long text divided into parts in order to fit in C89 strings size limits. */
65 const char *wpa_supplicant_full_license1 =
66 "This program is free software; you can redistribute it and/or modify\n"
67 "it under the terms of the GNU General Public License version 2 as\n"
68 "published by the Free Software Foundation.\n"
70 "This program is distributed in the hope that it will be useful,\n"
71 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
72 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
73 "GNU General Public License for more details.\n"
75 const char *wpa_supplicant_full_license2 =
76 "You should have received a copy of the GNU General Public License\n"
77 "along with this program; if not, write to the Free Software\n"
78 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
80 "Alternatively, this software may be distributed under the terms of the\n"
83 "Redistribution and use in source and binary forms, with or without\n"
84 "modification, are permitted provided that the following conditions are\n"
87 const char *wpa_supplicant_full_license3 =
88 "1. Redistributions of source code must retain the above copyright\n"
89 " notice, this list of conditions and the following disclaimer.\n"
91 "2. Redistributions in binary form must reproduce the above copyright\n"
92 " notice, this list of conditions and the following disclaimer in the\n"
93 " documentation and/or other materials provided with the distribution.\n"
95 const char *wpa_supplicant_full_license4 =
96 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
97 " names of its contributors may be used to endorse or promote products\n"
98 " derived from this software without specific prior written permission.\n"
100 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
101 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
102 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
103 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
104 const char *wpa_supplicant_full_license5 =
105 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
106 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
107 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
108 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
109 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
110 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
111 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
113 #endif /* CONFIG_NO_STDOUT_DEBUG */
115 extern int wpa_debug_level;
116 extern int wpa_debug_show_keys;
117 extern int wpa_debug_timestamp;
118 extern struct wpa_driver_ops *wpa_drivers[];
120 /* Configure default/group WEP keys for static WEP */
121 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
122 struct wpa_ssid *ssid)
126 for (i = 0; i < NUM_WEP_KEYS; i++) {
127 if (ssid->wep_key_len[i] == 0)
131 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
132 (u8 *) "\xff\xff\xff\xff\xff\xff",
133 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
134 ssid->wep_key[i], ssid->wep_key_len[i]);
141 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
142 struct wpa_ssid *ssid)
149 /* IBSS/WPA-None uses only one key (Group) for both receiving and
150 * sending unicast and multicast packets. */
152 if (ssid->mode != IEEE80211_MODE_IBSS) {
153 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
154 "for WPA-None", ssid->mode);
158 if (!ssid->psk_set) {
159 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
163 switch (wpa_s->group_cipher) {
164 case WPA_CIPHER_CCMP:
165 os_memcpy(key, ssid->psk, 16);
169 case WPA_CIPHER_TKIP:
170 /* WPA-None uses the same Michael MIC key for both TX and RX */
171 os_memcpy(key, ssid->psk, 16 + 8);
172 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
177 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
178 "WPA-None", wpa_s->group_cipher);
182 /* TODO: should actually remember the previously used seq#, both for TX
183 * and RX from each STA.. */
185 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
186 0, 1, seq, 6, key, keylen);
190 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
192 struct wpa_supplicant *wpa_s = eloop_ctx;
193 const u8 *bssid = wpa_s->bssid;
194 if (is_zero_ether_addr(bssid))
195 bssid = wpa_s->pending_bssid;
196 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
198 wpa_blacklist_add(wpa_s, bssid);
199 wpa_sm_notify_disassoc(wpa_s->wpa);
200 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
201 wpa_s->reassociate = 1;
202 wpa_supplicant_req_scan(wpa_s, 0, 0);
207 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
208 * @wpa_s: Pointer to wpa_supplicant data
209 * @sec: Number of seconds after which to time out authentication
210 * @usec: Number of microseconds after which to time out authentication
212 * This function is used to schedule a timeout for the current authentication
215 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
218 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
219 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
222 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
223 "%d usec", sec, usec);
224 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
225 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
230 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
231 * @wpa_s: Pointer to wpa_supplicant data
233 * This function is used to cancel authentication timeout scheduled with
234 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
237 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
239 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
240 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
241 wpa_blacklist_del(wpa_s, wpa_s->bssid);
246 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
247 * @wpa_s: Pointer to wpa_supplicant data
249 * This function is used to configure EAPOL state machine based on the selected
250 * authentication mode.
252 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
254 #ifdef IEEE8021X_EAPOL
255 struct eapol_config eapol_conf;
256 struct wpa_ssid *ssid = wpa_s->current_ssid;
258 #ifdef CONFIG_IBSS_RSN
259 if (ssid->mode == IEEE80211_MODE_IBSS &&
260 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
261 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
263 * RSN IBSS authentication is per-STA and we can disable the
264 * per-BSSID EAPOL authentication.
266 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
267 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
268 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
271 #endif /* CONFIG_IBSS_RSN */
273 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
274 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
276 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
277 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
278 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
280 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
282 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
283 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
284 eapol_conf.accept_802_1x_keys = 1;
285 eapol_conf.required_keys = 0;
286 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
287 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
290 eapol_conf.required_keys |=
291 EAPOL_REQUIRE_KEY_BROADCAST;
294 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
295 eapol_conf.required_keys = 0;
298 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
299 eapol_conf.workaround = ssid->eap_workaround;
300 eapol_conf.eap_disabled =
301 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
302 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
303 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
304 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
305 #endif /* IEEE8021X_EAPOL */
310 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
311 * @wpa_s: Pointer to wpa_supplicant data
312 * @ssid: Configuration data for the network
314 * This function is used to configure WPA state machine and related parameters
315 * to a mode where WPA is not enabled. This is called as part of the
316 * authentication configuration when the selected network does not use WPA.
318 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
319 struct wpa_ssid *ssid)
323 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
324 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
325 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
326 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
328 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
329 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
330 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
331 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
332 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
333 wpa_s->group_cipher = WPA_CIPHER_NONE;
334 wpa_s->mgmt_group_cipher = 0;
336 for (i = 0; i < NUM_WEP_KEYS; i++) {
337 if (ssid->wep_key_len[i] > 5) {
338 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
339 wpa_s->group_cipher = WPA_CIPHER_WEP104;
341 } else if (ssid->wep_key_len[i] > 0) {
342 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
343 wpa_s->group_cipher = WPA_CIPHER_WEP40;
348 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
351 wpa_s->pairwise_cipher);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
353 #ifdef CONFIG_IEEE80211W
354 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
355 wpa_s->mgmt_group_cipher);
356 #endif /* CONFIG_IEEE80211W */
358 pmksa_cache_clear_current(wpa_s->wpa);
362 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
364 scard_deinit(wpa_s->scard);
366 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
367 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
368 l2_packet_deinit(wpa_s->l2);
371 l2_packet_deinit(wpa_s->l2_br);
375 if (wpa_s->ctrl_iface) {
376 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
377 wpa_s->ctrl_iface = NULL;
379 if (wpa_s->conf != NULL) {
380 wpa_config_free(wpa_s->conf);
384 os_free(wpa_s->confname);
385 wpa_s->confname = NULL;
387 wpa_sm_set_eapol(wpa_s->wpa, NULL);
388 eapol_sm_deinit(wpa_s->eapol);
391 rsn_preauth_deinit(wpa_s->wpa);
393 pmksa_candidate_free(wpa_s->wpa);
394 wpa_sm_deinit(wpa_s->wpa);
396 wpa_blacklist_clear(wpa_s);
398 wpa_scan_results_free(wpa_s->scan_res);
399 wpa_s->scan_res = NULL;
401 wpa_supplicant_cancel_scan(wpa_s);
402 wpa_supplicant_cancel_auth_timeout(wpa_s);
404 ieee80211_sta_deinit(wpa_s);
406 wpas_wps_deinit(wpa_s);
408 wpabuf_free(wpa_s->pending_eapol_rx);
409 wpa_s->pending_eapol_rx = NULL;
411 #ifdef CONFIG_IBSS_RSN
412 ibss_rsn_deinit(wpa_s->ibss_rsn);
413 wpa_s->ibss_rsn = NULL;
414 #endif /* CONFIG_IBSS_RSN */
417 os_free(wpa_s->sme.ft_ies);
418 wpa_s->sme.ft_ies = NULL;
419 wpa_s->sme.ft_ies_len = 0;
420 #endif /* CONFIG_SME */
423 wpa_supplicant_ap_deinit(wpa_s);
424 #endif /* CONFIG_AP */
429 * wpa_clear_keys - Clear keys configured for the driver
430 * @wpa_s: Pointer to wpa_supplicant data
431 * @addr: Previously used BSSID or %NULL if not available
433 * This function clears the encryption keys that has been previously configured
436 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
438 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
440 if (wpa_s->keys_cleared) {
441 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
442 * timing issues with keys being cleared just before new keys
443 * are set or just after association or something similar. This
444 * shows up in group key handshake failing often because of the
445 * client not receiving the first encrypted packets correctly.
446 * Skipping some of the extra key clearing steps seems to help
447 * in completing group key handshake more reliably. */
448 wpa_printf(MSG_DEBUG, "No keys have been configured - "
449 "skip key clearing");
453 /* MLME-DELETEKEYS.request */
454 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
455 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
456 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
457 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
459 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
461 /* MLME-SETPROTECTION.request(None) */
462 wpa_drv_mlme_setprotection(
464 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
465 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
467 wpa_s->keys_cleared = 1;
472 * wpa_supplicant_state_txt - Get the connection state name as a text string
473 * @state: State (wpa_state; WPA_*)
474 * Returns: The state name as a printable text string
476 const char * wpa_supplicant_state_txt(int state)
479 case WPA_DISCONNECTED:
480 return "DISCONNECTED";
485 case WPA_AUTHENTICATING:
486 return "AUTHENTICATING";
487 case WPA_ASSOCIATING:
488 return "ASSOCIATING";
491 case WPA_4WAY_HANDSHAKE:
492 return "4WAY_HANDSHAKE";
493 case WPA_GROUP_HANDSHAKE:
494 return "GROUP_HANDSHAKE";
504 * wpa_supplicant_set_state - Set current connection state
505 * @wpa_s: Pointer to wpa_supplicant data
506 * @state: The new connection state
508 * This function is called whenever the connection state changes, e.g.,
509 * association is completed for WPA/WPA2 4-Way Handshake is started.
511 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
513 wpa_printf(MSG_DEBUG, "State: %s -> %s",
514 wpa_supplicant_state_txt(wpa_s->wpa_state),
515 wpa_supplicant_state_txt(state));
517 if (state != WPA_SCANNING)
518 wpa_supplicant_notify_scanning(wpa_s, 0);
520 wpa_supplicant_dbus_notify_state_change(wpa_s, 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_s->pairwise_cipher = 0;
561 wpa_s->group_cipher = 0;
562 wpa_s->mgmt_group_cipher = 0;
564 wpa_s->wpa_state = WPA_DISCONNECTED;
569 * wpa_supplicant_reload_configuration - Reload configuration data
570 * @wpa_s: Pointer to wpa_supplicant data
571 * Returns: 0 on success or -1 if configuration parsing failed
573 * This function can be used to request that the configuration data is reloaded
574 * (e.g., after configuration file change). This function is reloading
575 * configuration only for one interface, so this may need to be called multiple
576 * times if %wpa_supplicant is controlling multiple interfaces and all
577 * interfaces need reconfiguration.
579 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
581 struct wpa_config *conf;
583 if (wpa_s->confname == NULL)
585 conf = wpa_config_read(wpa_s->confname);
587 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
588 "file '%s' - exiting", wpa_s->confname);
592 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
593 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
594 os_strcmp(conf->ctrl_interface,
595 wpa_s->conf->ctrl_interface) != 0);
597 if (reconf_ctrl && wpa_s->ctrl_iface) {
598 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
599 wpa_s->ctrl_iface = NULL;
602 eapol_sm_invalidate_cached_session(wpa_s->eapol);
603 wpa_s->current_ssid = NULL;
605 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
606 * pkcs11_engine_path, pkcs11_module_path.
608 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
610 * Clear forced success to clear EAP state for next
613 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
615 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
616 wpa_sm_set_config(wpa_s->wpa, NULL);
617 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
618 rsn_preauth_deinit(wpa_s->wpa);
619 wpa_config_free(wpa_s->conf);
622 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
624 wpa_supplicant_clear_status(wpa_s);
625 wpa_s->reassociate = 1;
626 wpa_supplicant_req_scan(wpa_s, 0, 0);
627 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
632 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
635 struct wpa_global *global = eloop_ctx;
636 struct wpa_supplicant *wpa_s;
637 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
638 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
639 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
646 static wpa_cipher cipher_suite2driver(int cipher)
649 case WPA_CIPHER_NONE:
651 case WPA_CIPHER_WEP40:
653 case WPA_CIPHER_WEP104:
654 return CIPHER_WEP104;
655 case WPA_CIPHER_CCMP:
657 case WPA_CIPHER_TKIP:
664 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
667 case WPA_KEY_MGMT_NONE:
668 return KEY_MGMT_NONE;
669 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
670 return KEY_MGMT_802_1X_NO_WPA;
671 case WPA_KEY_MGMT_IEEE8021X:
672 return KEY_MGMT_802_1X;
673 case WPA_KEY_MGMT_WPA_NONE:
674 return KEY_MGMT_WPA_NONE;
675 case WPA_KEY_MGMT_FT_IEEE8021X:
676 return KEY_MGMT_FT_802_1X;
677 case WPA_KEY_MGMT_FT_PSK:
678 return KEY_MGMT_FT_PSK;
679 case WPA_KEY_MGMT_IEEE8021X_SHA256:
680 return KEY_MGMT_802_1X_SHA256;
681 case WPA_KEY_MGMT_PSK_SHA256:
682 return KEY_MGMT_PSK_SHA256;
683 case WPA_KEY_MGMT_WPS:
685 case WPA_KEY_MGMT_PSK:
692 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
693 struct wpa_ssid *ssid,
694 struct wpa_ie_data *ie)
696 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
699 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
700 "from association info");
705 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
707 if (!(ie->group_cipher & ssid->group_cipher)) {
708 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
709 "cipher 0x%x (mask 0x%x) - reject",
710 ie->group_cipher, ssid->group_cipher);
713 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
714 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
715 "cipher 0x%x (mask 0x%x) - reject",
716 ie->pairwise_cipher, ssid->pairwise_cipher);
719 if (!(ie->key_mgmt & ssid->key_mgmt)) {
720 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
721 "management 0x%x (mask 0x%x) - reject",
722 ie->key_mgmt, ssid->key_mgmt);
726 #ifdef CONFIG_IEEE80211W
727 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
728 ssid->ieee80211w == IEEE80211W_REQUIRED) {
729 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
730 "that does not support management frame protection - "
734 #endif /* CONFIG_IEEE80211W */
741 * wpa_supplicant_set_suites - Set authentication and encryption parameters
742 * @wpa_s: Pointer to wpa_supplicant data
743 * @bss: Scan results for the selected BSS, or %NULL if not available
744 * @ssid: Configuration data for the selected network
745 * @wpa_ie: Buffer for the WPA/RSN IE
746 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
747 * used buffer length in case the functions returns success.
748 * Returns: 0 on success or -1 on failure
750 * This function is used to configure authentication and encryption parameters
751 * based on the network configuration and scan result for the selected BSS (if
754 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
755 struct wpa_scan_res *bss,
756 struct wpa_ssid *ssid,
757 u8 *wpa_ie, size_t *wpa_ie_len)
759 struct wpa_ie_data ie;
761 const u8 *bss_wpa, *bss_rsn;
764 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
765 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
767 bss_wpa = bss_rsn = NULL;
769 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
770 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
771 (ie.group_cipher & ssid->group_cipher) &&
772 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
773 (ie.key_mgmt & ssid->key_mgmt)) {
774 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
775 proto = WPA_PROTO_RSN;
776 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
777 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
778 (ie.group_cipher & ssid->group_cipher) &&
779 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
780 (ie.key_mgmt & ssid->key_mgmt)) {
781 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
782 proto = WPA_PROTO_WPA;
784 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
787 if (ssid->proto & WPA_PROTO_RSN)
788 proto = WPA_PROTO_RSN;
790 proto = WPA_PROTO_WPA;
791 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
792 os_memset(&ie, 0, sizeof(ie));
793 ie.group_cipher = ssid->group_cipher;
794 ie.pairwise_cipher = ssid->pairwise_cipher;
795 ie.key_mgmt = ssid->key_mgmt;
796 #ifdef CONFIG_IEEE80211W
797 ie.mgmt_group_cipher =
798 ssid->ieee80211w != NO_IEEE80211W ?
799 WPA_CIPHER_AES_128_CMAC : 0;
800 #endif /* CONFIG_IEEE80211W */
801 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
807 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
808 "pairwise %d key_mgmt %d proto %d",
809 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
810 #ifdef CONFIG_IEEE80211W
811 if (ssid->ieee80211w) {
812 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
813 ie.mgmt_group_cipher);
815 #endif /* CONFIG_IEEE80211W */
817 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
818 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
819 !!(ssid->proto & WPA_PROTO_RSN));
821 if (bss || !wpa_s->ap_ies_from_associnfo) {
822 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
823 bss_wpa ? 2 + bss_wpa[1] : 0) ||
824 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
825 bss_rsn ? 2 + bss_rsn[1] : 0))
829 sel = ie.group_cipher & ssid->group_cipher;
830 if (sel & WPA_CIPHER_CCMP) {
831 wpa_s->group_cipher = WPA_CIPHER_CCMP;
832 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
833 } else if (sel & WPA_CIPHER_TKIP) {
834 wpa_s->group_cipher = WPA_CIPHER_TKIP;
835 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
836 } else if (sel & WPA_CIPHER_WEP104) {
837 wpa_s->group_cipher = WPA_CIPHER_WEP104;
838 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
839 } else if (sel & WPA_CIPHER_WEP40) {
840 wpa_s->group_cipher = WPA_CIPHER_WEP40;
841 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
843 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
847 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
848 if (sel & WPA_CIPHER_CCMP) {
849 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
850 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
851 } else if (sel & WPA_CIPHER_TKIP) {
852 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
853 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
854 } else if (sel & WPA_CIPHER_NONE) {
855 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
856 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
858 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
863 sel = ie.key_mgmt & ssid->key_mgmt;
865 #ifdef CONFIG_IEEE80211R
866 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
867 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
868 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
869 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
870 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
871 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
872 #endif /* CONFIG_IEEE80211R */
873 #ifdef CONFIG_IEEE80211W
874 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
875 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
876 wpa_msg(wpa_s, MSG_DEBUG,
877 "WPA: using KEY_MGMT 802.1X with SHA256");
878 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
879 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
880 wpa_msg(wpa_s, MSG_DEBUG,
881 "WPA: using KEY_MGMT PSK with SHA256");
882 #endif /* CONFIG_IEEE80211W */
883 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
884 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
885 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
886 } else if (sel & WPA_KEY_MGMT_PSK) {
887 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
888 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
889 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
890 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
891 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
893 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
894 "key management type.");
898 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
899 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
900 wpa_s->pairwise_cipher);
901 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
903 #ifdef CONFIG_IEEE80211W
904 sel = ie.mgmt_group_cipher;
905 if (ssid->ieee80211w == NO_IEEE80211W ||
906 !(ie.capabilities & WPA_CAPABILITY_MFPC))
908 if (sel & WPA_CIPHER_AES_128_CMAC) {
909 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
910 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
913 wpa_s->mgmt_group_cipher = 0;
914 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
916 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
917 wpa_s->mgmt_group_cipher);
918 #endif /* CONFIG_IEEE80211W */
920 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
921 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
926 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
927 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
929 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
936 * wpa_supplicant_associate - Request association
937 * @wpa_s: Pointer to wpa_supplicant data
938 * @bss: Scan results for the selected BSS, or %NULL if not available
939 * @ssid: Configuration data for the selected network
941 * This function is used to request %wpa_supplicant to associate with a BSS.
943 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
944 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
948 int use_crypt, ret, i;
949 int algs = AUTH_ALG_OPEN_SYSTEM;
950 wpa_cipher cipher_pairwise, cipher_group;
951 struct wpa_driver_associate_params params;
952 int wep_keys_set = 0;
953 struct wpa_driver_capa capa;
954 int assoc_failed = 0;
956 if (ssid->mode == 2) {
958 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
959 wpa_printf(MSG_INFO, "Driver does not support AP "
963 wpa_supplicant_create_ap(wpa_s, ssid);
964 #else /* CONFIG_AP */
965 wpa_printf(MSG_ERROR, "AP mode support not included in the "
967 #endif /* CONFIG_AP */
971 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
972 sme_authenticate(wpa_s, bss, ssid);
976 wpa_s->reassociate = 0;
978 #ifdef CONFIG_IEEE80211R
980 #endif /* CONFIG_IEEE80211R */
981 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
982 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
983 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
984 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
985 os_memset(wpa_s->bssid, 0, ETH_ALEN);
986 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
987 #ifdef CONFIG_IEEE80211R
988 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
989 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
991 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
993 /* Prepare for the next transition */
994 wpa_ft_prepare_auth_request(wpa_s->wpa);
996 #endif /* CONFIG_IEEE80211R */
998 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
999 wpa_s->conf->ap_scan == 2 &&
1000 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1001 /* Use ap_scan==1 style network selection to find the network
1003 wpa_s->scan_req = 2;
1004 wpa_s->reassociate = 1;
1005 wpa_supplicant_req_scan(wpa_s, 0, 0);
1007 #endif /* CONFIG_WPS */
1009 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1010 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1011 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1013 wpa_supplicant_cancel_scan(wpa_s);
1015 /* Starting new association, so clear the possibly used WPA IE from the
1016 * previous association. */
1017 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1019 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
1020 wpa_printf(MSG_WARNING, "Failed to set operating mode");
1024 #ifdef IEEE8021X_EAPOL
1025 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1027 if (ssid->non_leap == 0)
1028 algs = AUTH_ALG_LEAP;
1030 algs |= AUTH_ALG_LEAP;
1033 #endif /* IEEE8021X_EAPOL */
1034 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1035 if (ssid->auth_alg) {
1037 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1038 algs |= AUTH_ALG_OPEN_SYSTEM;
1039 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1040 algs |= AUTH_ALG_SHARED_KEY;
1041 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1042 algs |= AUTH_ALG_LEAP;
1043 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1046 wpa_drv_set_auth_alg(wpa_s, algs);
1048 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1049 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1050 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1051 WPA_KEY_MGMT_FT_IEEE8021X |
1052 WPA_KEY_MGMT_FT_PSK |
1053 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1054 WPA_KEY_MGMT_PSK_SHA256))) {
1055 int try_opportunistic;
1056 try_opportunistic = ssid->proactive_key_caching &&
1057 (ssid->proto & WPA_PROTO_RSN);
1058 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1059 wpa_s->current_ssid,
1060 try_opportunistic) == 0)
1061 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1062 wpa_ie_len = sizeof(wpa_ie);
1063 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1064 wpa_ie, &wpa_ie_len)) {
1065 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1066 "management and encryption suites");
1069 } else if (ssid->key_mgmt &
1070 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1071 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1072 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1073 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1074 wpa_ie_len = sizeof(wpa_ie);
1075 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1076 wpa_ie, &wpa_ie_len)) {
1077 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1078 "management and encryption suites (no scan "
1083 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1084 struct wpabuf *wps_ie;
1085 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1086 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1087 wpa_ie_len = wpabuf_len(wps_ie);
1088 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1091 wpabuf_free(wps_ie);
1092 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1093 #endif /* CONFIG_WPS */
1095 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1099 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1101 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1102 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1103 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1104 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1105 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1107 if (wpa_set_wep_keys(wpa_s, ssid)) {
1112 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1115 #ifdef IEEE8021X_EAPOL
1116 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1117 if ((ssid->eapol_flags &
1118 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1119 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1123 /* Assume that dynamic WEP-104 keys will be used and
1124 * set cipher suites in order for drivers to expect
1126 cipher_pairwise = cipher_group = CIPHER_WEP104;
1129 #endif /* IEEE8021X_EAPOL */
1131 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1132 /* Set the key before (and later after) association */
1133 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1136 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1137 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1138 os_memset(¶ms, 0, sizeof(params));
1140 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1141 params.bssid = bss->bssid;
1142 params.ssid = ie ? ie + 2 : (u8 *) "";
1143 params.ssid_len = ie ? ie[1] : 0;
1144 params.freq = bss->freq;
1146 params.ssid = ssid->ssid;
1147 params.ssid_len = ssid->ssid_len;
1149 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1150 params.freq = ssid->frequency; /* Initial channel for IBSS */
1151 params.wpa_ie = wpa_ie;
1152 params.wpa_ie_len = wpa_ie_len;
1153 params.pairwise_suite = cipher_pairwise;
1154 params.group_suite = cipher_group;
1155 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1156 params.auth_alg = algs;
1157 params.mode = ssid->mode;
1158 for (i = 0; i < NUM_WEP_KEYS; i++) {
1159 if (ssid->wep_key_len[i])
1160 params.wep_key[i] = ssid->wep_key[i];
1161 params.wep_key_len[i] = ssid->wep_key_len[i];
1163 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1165 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1166 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1167 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1168 params.passphrase = ssid->passphrase;
1170 params.psk = ssid->psk;
1173 params.drop_unencrypted = use_crypt;
1175 #ifdef CONFIG_IEEE80211W
1176 switch (ssid->ieee80211w) {
1178 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1180 case IEEE80211W_OPTIONAL:
1181 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1183 case IEEE80211W_REQUIRED:
1184 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1187 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1188 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1189 struct wpa_ie_data ie;
1190 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1192 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1193 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1195 params.mgmt_frame_protection =
1196 MGMT_FRAME_PROTECTION_REQUIRED;
1199 #endif /* CONFIG_IEEE80211W */
1201 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1202 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1204 ret = wpa_drv_associate(wpa_s, ¶ms);
1206 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1208 /* try to continue anyway; new association will be tried again
1213 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1214 /* Set the key after the association just in case association
1215 * cleared the previously configured key. */
1216 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1217 /* No need to timeout authentication since there is no key
1219 wpa_supplicant_cancel_auth_timeout(wpa_s);
1220 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1221 #ifdef CONFIG_IBSS_RSN
1222 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1223 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1224 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1225 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1227 * RSN IBSS authentication is per-STA and we can disable the
1228 * per-BSSID authentication.
1230 wpa_supplicant_cancel_auth_timeout(wpa_s);
1231 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1232 #endif /* CONFIG_IBSS_RSN */
1234 /* Timeout for IEEE 802.11 authentication and association */
1238 /* give IBSS a bit more time */
1239 timeout = ssid->mode ? 10 : 5;
1240 } else if (wpa_s->conf->ap_scan == 1) {
1241 /* give IBSS a bit more time */
1242 timeout = ssid->mode ? 20 : 10;
1244 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1247 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1248 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1249 /* Set static WEP keys again */
1250 wpa_set_wep_keys(wpa_s, ssid);
1253 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1255 * Do not allow EAP session resumption between different
1256 * network configurations.
1258 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1260 wpa_s->current_ssid = ssid;
1261 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1262 wpa_supplicant_initiate_eapol(wpa_s);
1267 * wpa_supplicant_disassociate - Disassociate the current connection
1268 * @wpa_s: Pointer to wpa_supplicant data
1269 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1271 * This function is used to request %wpa_supplicant to disassociate with the
1274 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1278 if (!is_zero_ether_addr(wpa_s->bssid)) {
1279 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1280 ieee80211_sta_disassociate(wpa_s, reason_code);
1282 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1283 addr = wpa_s->bssid;
1285 wpa_clear_keys(wpa_s, addr);
1286 wpa_supplicant_mark_disassoc(wpa_s);
1287 wpa_s->current_ssid = NULL;
1288 wpa_sm_set_config(wpa_s->wpa, NULL);
1289 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1294 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1295 * @wpa_s: Pointer to wpa_supplicant data
1296 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1298 * This function is used to request %wpa_supplicant to deauthenticate from the
1301 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1305 if (!is_zero_ether_addr(wpa_s->bssid)) {
1306 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1307 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1309 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1311 addr = wpa_s->bssid;
1313 wpa_clear_keys(wpa_s, addr);
1314 wpa_supplicant_mark_disassoc(wpa_s);
1315 wpa_s->current_ssid = NULL;
1316 wpa_sm_set_config(wpa_s->wpa, NULL);
1317 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1321 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1323 #define SCAN_AP_LIMIT 128
1324 struct wpa_scan_result *results;
1326 struct wpa_scan_results *res;
1328 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1329 if (results == NULL) {
1330 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1335 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1336 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1338 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1342 if (num > SCAN_AP_LIMIT) {
1343 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1344 num, SCAN_AP_LIMIT);
1345 num = SCAN_AP_LIMIT;
1348 wpa_scan_results_free(wpa_s->scan_res);
1349 wpa_s->scan_res = NULL;
1351 /* Convert old scan result data structure to the new one */
1352 res = os_zalloc(sizeof(*res));
1357 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1358 if (res->res == NULL) {
1364 for (i = 0; i < num; i++) {
1365 struct wpa_scan_result *bss = &results[i];
1366 struct wpa_scan_res *r;
1370 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1373 if (bss->mdie_present)
1376 r = os_zalloc(sizeof(*r) + ie_len);
1380 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1381 r->freq = bss->freq;
1382 r->caps = bss->caps;
1383 r->qual = bss->qual;
1384 r->noise = bss->noise;
1385 r->level = bss->level;
1389 pos = (u8 *) (r + 1);
1392 *pos++ = WLAN_EID_SSID;
1393 *pos++ = bss->ssid_len;
1394 os_memcpy(pos, bss->ssid, bss->ssid_len);
1395 pos += bss->ssid_len;
1398 /* Fake Supported Rate IE to include max rate */
1399 *pos++ = WLAN_EID_SUPP_RATES;
1401 *pos++ = bss->maxrate;
1404 if (bss->rsn_ie_len) {
1405 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1406 pos += bss->rsn_ie_len;
1409 if (bss->mdie_present) {
1410 os_memcpy(pos, bss->mdie, 5);
1414 if (bss->wpa_ie_len) {
1415 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1416 pos += bss->wpa_ie_len;
1419 res->res[res->num++] = r;
1423 wpa_s->scan_res = res;
1430 * wpa_supplicant_get_scan_results - Get scan results
1431 * @wpa_s: Pointer to wpa_supplicant data
1432 * Returns: 0 on success, -1 on failure
1434 * This function is request the current scan results from the driver and stores
1435 * a local copy of the results in wpa_s->scan_res.
1437 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1441 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1442 wpa_scan_results_free(wpa_s->scan_res);
1443 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1444 if (wpa_s->scan_res == NULL) {
1445 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1449 } else if (wpa_s->driver->get_scan_results2 == NULL)
1450 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1452 wpa_scan_results_free(wpa_s->scan_res);
1453 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1454 if (wpa_s->scan_res == NULL) {
1455 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1461 if (wpa_s->scan_res)
1462 wpa_scan_sort_results(wpa_s->scan_res);
1469 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1470 * @wpa_s: Pointer to wpa_supplicant data
1471 * Returns: A pointer to the current network structure or %NULL on failure
1473 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1475 struct wpa_ssid *entry;
1476 u8 ssid[MAX_SSID_LEN];
1482 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1483 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1484 wpa_printf(MSG_WARNING, "Could not read SSID from "
1489 res = wpa_drv_get_ssid(wpa_s, ssid);
1491 wpa_printf(MSG_WARNING, "Could not read SSID from "
1498 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1499 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1500 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1501 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1505 wired = wpa_s->conf->ap_scan == 0 &&
1506 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1508 entry = wpa_s->conf->ssid;
1510 if (!entry->disabled &&
1511 ((ssid_len == entry->ssid_len &&
1512 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1513 (!entry->bssid_set ||
1514 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1517 if (!entry->disabled &&
1518 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1519 (entry->ssid == NULL || entry->ssid_len == 0) &&
1520 (!entry->bssid_set ||
1521 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1523 #endif /* CONFIG_WPS */
1524 entry = entry->next;
1531 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1541 if (wpa_drivers[0] == NULL) {
1542 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1548 /* default to first driver in the list */
1549 wpa_s->driver = wpa_drivers[0];
1553 pos = os_strchr(name, ',');
1557 len = os_strlen(name);
1558 for (i = 0; wpa_drivers[i]; i++) {
1559 if (os_strlen(wpa_drivers[i]->name) == len &&
1560 os_strncmp(name, wpa_drivers[i]->name, len) ==
1562 wpa_s->driver = wpa_drivers[i];
1567 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1572 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1573 const u8 *buf, size_t len)
1575 struct wpa_supplicant *wpa_s = ctx;
1577 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1578 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1580 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1582 * There is possible race condition between receiving the
1583 * association event and the EAPOL frame since they are coming
1584 * through different paths from the driver. In order to avoid
1585 * issues in trying to process the EAPOL frame before receiving
1586 * association information, lets queue it for processing until
1587 * the association event is received.
1589 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1590 "received EAPOL frame");
1591 wpabuf_free(wpa_s->pending_eapol_rx);
1592 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1593 if (wpa_s->pending_eapol_rx) {
1594 os_get_time(&wpa_s->pending_eapol_rx_time);
1595 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1602 if (wpa_s->ap_iface) {
1603 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1606 #endif /* CONFIG_AP */
1608 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1609 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1610 "no key management is configured");
1614 if (wpa_s->eapol_received == 0 &&
1615 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1616 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1617 wpa_s->wpa_state != WPA_COMPLETED)) {
1618 /* Timeout for completing IEEE 802.1X and WPA authentication */
1619 wpa_supplicant_req_auth_timeout(
1621 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1622 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1623 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1626 wpa_s->eapol_received++;
1628 if (wpa_s->countermeasures) {
1629 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1634 #ifdef CONFIG_IBSS_RSN
1635 if (wpa_s->current_ssid &&
1636 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1637 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1640 #endif /* CONFIG_IBSS_RSN */
1642 /* Source address of the incoming EAPOL frame could be compared to the
1643 * current BSSID. However, it is possible that a centralized
1644 * Authenticator could be using another MAC address than the BSSID of
1645 * an AP, so just allow any address to be used for now. The replies are
1646 * still sent to the current BSSID (if available), though. */
1648 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1649 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1650 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1652 wpa_drv_poll(wpa_s);
1653 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1654 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1655 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1657 * Set portValid = TRUE here since we are going to skip 4-way
1658 * handshake processing which would normally set portValid. We
1659 * need this to allow the EAPOL state machines to be completed
1660 * without going through EAPOL-Key handshake.
1662 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1667 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1668 struct ieee80211_rx_status *rx_status)
1670 struct wpa_supplicant *wpa_s = ctx;
1671 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1676 * wpa_supplicant_driver_init - Initialize driver interface parameters
1677 * @wpa_s: Pointer to wpa_supplicant data
1678 * Returns: 0 on success, -1 on failure
1680 * This function is called to initialize driver interface parameters.
1681 * wpa_drv_init() must have been called before this function to initialize the
1684 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1686 static int interface_count = 0;
1688 if (wpa_s->driver->send_eapol) {
1689 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1691 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1693 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1694 wpa_drv_get_mac_addr(wpa_s),
1696 wpa_supplicant_rx_eapol, wpa_s, 0);
1697 if (wpa_s->l2 == NULL)
1701 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1702 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1706 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1707 MAC2STR(wpa_s->own_addr));
1709 if (wpa_s->bridge_ifname[0]) {
1710 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1711 " '%s'", wpa_s->bridge_ifname);
1712 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1715 wpa_supplicant_rx_eapol, wpa_s,
1717 if (wpa_s->l2_br == NULL) {
1718 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1719 "connection for the bridge interface '%s'",
1720 wpa_s->bridge_ifname);
1725 /* Backwards compatibility call to set_wpa() handler. This is called
1726 * only just after init and just before deinit, so these handler can be
1727 * used to implement same functionality. */
1728 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1729 struct wpa_driver_capa capa;
1730 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1731 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1732 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1733 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1734 /* Continue to allow non-WPA modes to be used. */
1736 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1742 wpa_clear_keys(wpa_s, NULL);
1744 /* Make sure that TKIP countermeasures are not left enabled (could
1745 * happen if wpa_supplicant is killed during countermeasures. */
1746 wpa_drv_set_countermeasures(wpa_s, 0);
1748 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1750 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1751 wpa_drv_flush_pmkid(wpa_s);
1753 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1754 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1761 static int wpa_supplicant_daemon(const char *pid_file)
1763 wpa_printf(MSG_DEBUG, "Daemonize..");
1764 return os_daemonize(pid_file);
1768 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1770 struct wpa_supplicant *wpa_s;
1772 wpa_s = os_zalloc(sizeof(*wpa_s));
1775 wpa_s->scan_req = 1;
1781 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1782 struct wpa_interface *iface)
1784 const char *ifname, *driver;
1785 struct wpa_driver_capa capa;
1787 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1788 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1789 iface->confname ? iface->confname : "N/A",
1790 iface->driver ? iface->driver : "default",
1791 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1792 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1794 if (iface->confname) {
1795 #ifdef CONFIG_BACKEND_FILE
1796 wpa_s->confname = os_rel2abs_path(iface->confname);
1797 if (wpa_s->confname == NULL) {
1798 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1799 "for configuration file '%s'.",
1803 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1804 iface->confname, wpa_s->confname);
1805 #else /* CONFIG_BACKEND_FILE */
1806 wpa_s->confname = os_strdup(iface->confname);
1807 #endif /* CONFIG_BACKEND_FILE */
1808 wpa_s->conf = wpa_config_read(wpa_s->confname);
1809 if (wpa_s->conf == NULL) {
1810 wpa_printf(MSG_ERROR, "Failed to read or parse "
1811 "configuration '%s'.", wpa_s->confname);
1816 * Override ctrl_interface and driver_param if set on command
1819 if (iface->ctrl_interface) {
1820 os_free(wpa_s->conf->ctrl_interface);
1821 wpa_s->conf->ctrl_interface =
1822 os_strdup(iface->ctrl_interface);
1825 if (iface->driver_param) {
1826 os_free(wpa_s->conf->driver_param);
1827 wpa_s->conf->driver_param =
1828 os_strdup(iface->driver_param);
1831 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1832 iface->driver_param);
1834 if (wpa_s->conf == NULL) {
1835 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1839 if (iface->ifname == NULL) {
1840 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1843 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1844 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1848 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1850 if (iface->bridge_ifname) {
1851 if (os_strlen(iface->bridge_ifname) >=
1852 sizeof(wpa_s->bridge_ifname)) {
1853 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1854 "name '%s'.", iface->bridge_ifname);
1857 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1858 sizeof(wpa_s->bridge_ifname));
1861 /* RSNA Supplicant Key Management - INITIALIZE */
1862 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1863 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1865 /* Initialize driver interface and register driver event handler before
1866 * L2 receive handler so that association events are processed before
1867 * EAPOL-Key packets if both become available for the same select()
1869 driver = iface->driver;
1871 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
1874 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1875 if (wpa_s->drv_priv == NULL) {
1877 pos = os_strchr(driver, ',');
1879 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
1880 "interface - try next driver wrapper");
1884 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1887 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1888 wpa_printf(MSG_ERROR, "Driver interface rejected "
1889 "driver_param '%s'", wpa_s->conf->driver_param);
1893 ifname = wpa_drv_get_ifname(wpa_s);
1894 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1895 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1896 "name with '%s'", ifname);
1897 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1900 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1903 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1904 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1906 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1908 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1909 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1910 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1911 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1912 "dot11RSNAConfigPMKLifetime");
1916 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1917 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1918 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1919 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1920 "dot11RSNAConfigPMKReauthThreshold");
1924 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1925 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1926 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1927 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1928 "dot11RSNAConfigSATimeout");
1932 if (wpa_supplicant_driver_init(wpa_s) < 0)
1935 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
1936 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
1937 wpa_printf(MSG_DEBUG, "Failed to set country");
1941 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1943 if (wpas_wps_init(wpa_s))
1946 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1948 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1950 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1951 if (wpa_s->ctrl_iface == NULL) {
1952 wpa_printf(MSG_ERROR,
1953 "Failed to initialize control interface '%s'.\n"
1954 "You may have another wpa_supplicant process "
1955 "already running or the file was\n"
1956 "left by an unclean termination of wpa_supplicant "
1957 "in which case you will need\n"
1958 "to manually remove this file before starting "
1959 "wpa_supplicant again.\n",
1960 wpa_s->conf->ctrl_interface);
1964 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1965 wpa_s->drv_flags = capa.flags;
1966 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1967 if (ieee80211_sta_init(wpa_s))
1970 wpa_s->max_scan_ssids = capa.max_scan_ssids;
1973 #ifdef CONFIG_IBSS_RSN
1974 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1975 if (!wpa_s->ibss_rsn) {
1976 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
1979 #endif /* CONFIG_IBSS_RSN */
1985 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1987 if (wpa_s->drv_priv) {
1988 wpa_supplicant_deauthenticate(wpa_s,
1989 WLAN_REASON_DEAUTH_LEAVING);
1991 /* Backwards compatibility call to set_wpa() handler. This is
1992 * called only just after init and just before deinit, so these
1993 * handler can be used to implement same functionality. */
1994 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1995 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
1999 wpa_drv_set_drop_unencrypted(wpa_s, 0);
2000 wpa_drv_set_countermeasures(wpa_s, 0);
2001 wpa_clear_keys(wpa_s, NULL);
2004 wpas_dbus_unregister_iface(wpa_s);
2006 wpa_supplicant_cleanup(wpa_s);
2008 if (wpa_s->drv_priv)
2009 wpa_drv_deinit(wpa_s);
2014 * wpa_supplicant_add_iface - Add a new network interface
2015 * @global: Pointer to global data from wpa_supplicant_init()
2016 * @iface: Interface configuration options
2017 * Returns: Pointer to the created interface or %NULL on failure
2019 * This function is used to add new network interfaces for %wpa_supplicant.
2020 * This can be called before wpa_supplicant_run() to add interfaces before the
2021 * main event loop has been started. In addition, new interfaces can be added
2022 * dynamically while %wpa_supplicant is already running. This could happen,
2023 * e.g., when a hotplug network adapter is inserted.
2025 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2026 struct wpa_interface *iface)
2028 struct wpa_supplicant *wpa_s;
2030 if (global == NULL || iface == NULL)
2033 wpa_s = wpa_supplicant_alloc();
2037 if (wpa_supplicant_init_iface(wpa_s, iface)) {
2038 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2040 wpa_supplicant_deinit_iface(wpa_s);
2045 wpa_s->global = global;
2047 /* Register the interface with the dbus control interface */
2048 if (wpas_dbus_register_iface(wpa_s)) {
2049 wpa_supplicant_deinit_iface(wpa_s);
2054 wpa_s->next = global->ifaces;
2055 global->ifaces = wpa_s;
2057 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2064 * wpa_supplicant_remove_iface - Remove a network interface
2065 * @global: Pointer to global data from wpa_supplicant_init()
2066 * @wpa_s: Pointer to the network interface to be removed
2067 * Returns: 0 if interface was removed, -1 if interface was not found
2069 * This function can be used to dynamically remove network interfaces from
2070 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2071 * addition, this function is used to remove all remaining interfaces when
2072 * %wpa_supplicant is terminated.
2074 int wpa_supplicant_remove_iface(struct wpa_global *global,
2075 struct wpa_supplicant *wpa_s)
2077 struct wpa_supplicant *prev;
2079 /* Remove interface from the global list of interfaces */
2080 prev = global->ifaces;
2081 if (prev == wpa_s) {
2082 global->ifaces = wpa_s->next;
2084 while (prev && prev->next != wpa_s)
2088 prev->next = wpa_s->next;
2091 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2093 wpa_supplicant_deinit_iface(wpa_s);
2101 * wpa_supplicant_get_iface - Get a new network interface
2102 * @global: Pointer to global data from wpa_supplicant_init()
2103 * @ifname: Interface name
2104 * Returns: Pointer to the interface or %NULL if not found
2106 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2109 struct wpa_supplicant *wpa_s;
2111 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2112 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2120 * wpa_supplicant_init - Initialize %wpa_supplicant
2121 * @params: Parameters for %wpa_supplicant
2122 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2124 * This function is used to initialize %wpa_supplicant. After successful
2125 * initialization, the returned data pointer can be used to add and remove
2126 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2128 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2130 struct wpa_global *global;
2136 wpa_debug_open_file(params->wpa_debug_file_path);
2137 if (params->wpa_debug_syslog)
2138 wpa_debug_open_syslog();
2140 ret = eap_peer_register_methods();
2142 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2144 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2145 "the same EAP type.");
2150 ret = eap_server_register_methods();
2152 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2154 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2155 "the same EAP type.");
2158 #endif /* CONFIG_AP */
2160 global = os_zalloc(sizeof(*global));
2163 global->params.daemonize = params->daemonize;
2164 global->params.wait_for_monitor = params->wait_for_monitor;
2165 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2166 if (params->pid_file)
2167 global->params.pid_file = os_strdup(params->pid_file);
2168 if (params->ctrl_interface)
2169 global->params.ctrl_interface =
2170 os_strdup(params->ctrl_interface);
2171 wpa_debug_level = global->params.wpa_debug_level =
2172 params->wpa_debug_level;
2173 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2174 params->wpa_debug_show_keys;
2175 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2176 params->wpa_debug_timestamp;
2178 if (eloop_init(global)) {
2179 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2180 wpa_supplicant_deinit(global);
2184 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2185 if (global->ctrl_iface == NULL) {
2186 wpa_supplicant_deinit(global);
2190 if (global->params.dbus_ctrl_interface) {
2191 global->dbus_ctrl_iface =
2192 wpa_supplicant_dbus_ctrl_iface_init(global);
2193 if (global->dbus_ctrl_iface == NULL) {
2194 wpa_supplicant_deinit(global);
2199 for (i = 0; wpa_drivers[i]; i++)
2200 global->drv_count++;
2201 if (global->drv_count == 0) {
2202 wpa_printf(MSG_ERROR, "No drivers enabled");
2203 wpa_supplicant_deinit(global);
2206 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2207 if (global->drv_priv == NULL) {
2208 wpa_supplicant_deinit(global);
2211 for (i = 0; wpa_drivers[i]; i++) {
2212 if (!wpa_drivers[i]->global_init)
2214 global->drv_priv[i] = wpa_drivers[i]->global_init();
2215 if (global->drv_priv[i] == NULL) {
2216 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2217 "'%s'", wpa_drivers[i]->name);
2218 wpa_supplicant_deinit(global);
2228 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2229 * @global: Pointer to global data from wpa_supplicant_init()
2230 * Returns: 0 after successful event loop run, -1 on failure
2232 * This function starts the main event loop and continues running as long as
2233 * there are any remaining events. In most cases, this function is running as
2234 * long as the %wpa_supplicant process in still in use.
2236 int wpa_supplicant_run(struct wpa_global *global)
2238 struct wpa_supplicant *wpa_s;
2240 if (global->params.daemonize &&
2241 wpa_supplicant_daemon(global->params.pid_file))
2244 if (global->params.wait_for_monitor) {
2245 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2246 if (wpa_s->ctrl_iface)
2247 wpa_supplicant_ctrl_iface_wait(
2251 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2252 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2261 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2262 * @global: Pointer to global data from wpa_supplicant_init()
2264 * This function is called to deinitialize %wpa_supplicant and to free all
2265 * allocated resources. Remaining network interfaces will also be removed.
2267 void wpa_supplicant_deinit(struct wpa_global *global)
2274 while (global->ifaces)
2275 wpa_supplicant_remove_iface(global, global->ifaces);
2277 if (global->ctrl_iface)
2278 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2279 if (global->dbus_ctrl_iface)
2280 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2282 eap_peer_unregister_methods();
2284 eap_server_unregister_methods();
2285 #endif /* CONFIG_AP */
2287 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2288 if (!global->drv_priv[i])
2290 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2292 os_free(global->drv_priv);
2296 if (global->params.pid_file) {
2297 os_daemonize_terminate(global->params.pid_file);
2298 os_free(global->params.pid_file);
2300 os_free(global->params.ctrl_interface);
2303 wpa_debug_close_syslog();
2304 wpa_debug_close_file();