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 #ifdef CONFIG_IBSS_RSN
409 ibss_rsn_deinit(wpa_s->ibss_rsn);
410 wpa_s->ibss_rsn = NULL;
411 #endif /* CONFIG_IBSS_RSN */
414 os_free(wpa_s->sme.ft_ies);
415 wpa_s->sme.ft_ies = NULL;
416 wpa_s->sme.ft_ies_len = 0;
417 #endif /* CONFIG_SME */
420 wpa_supplicant_ap_deinit(wpa_s);
421 #endif /* CONFIG_AP */
426 * wpa_clear_keys - Clear keys configured for the driver
427 * @wpa_s: Pointer to wpa_supplicant data
428 * @addr: Previously used BSSID or %NULL if not available
430 * This function clears the encryption keys that has been previously configured
433 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
435 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
437 if (wpa_s->keys_cleared) {
438 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
439 * timing issues with keys being cleared just before new keys
440 * are set or just after association or something similar. This
441 * shows up in group key handshake failing often because of the
442 * client not receiving the first encrypted packets correctly.
443 * Skipping some of the extra key clearing steps seems to help
444 * in completing group key handshake more reliably. */
445 wpa_printf(MSG_DEBUG, "No keys have been configured - "
446 "skip key clearing");
450 /* MLME-DELETEKEYS.request */
451 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
452 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
453 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
454 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
456 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
458 /* MLME-SETPROTECTION.request(None) */
459 wpa_drv_mlme_setprotection(
461 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
462 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
464 wpa_s->keys_cleared = 1;
469 * wpa_supplicant_state_txt - Get the connection state name as a text string
470 * @state: State (wpa_state; WPA_*)
471 * Returns: The state name as a printable text string
473 const char * wpa_supplicant_state_txt(int state)
476 case WPA_DISCONNECTED:
477 return "DISCONNECTED";
482 case WPA_AUTHENTICATING:
483 return "AUTHENTICATING";
484 case WPA_ASSOCIATING:
485 return "ASSOCIATING";
488 case WPA_4WAY_HANDSHAKE:
489 return "4WAY_HANDSHAKE";
490 case WPA_GROUP_HANDSHAKE:
491 return "GROUP_HANDSHAKE";
501 * wpa_supplicant_set_state - Set current connection state
502 * @wpa_s: Pointer to wpa_supplicant data
503 * @state: The new connection state
505 * This function is called whenever the connection state changes, e.g.,
506 * association is completed for WPA/WPA2 4-Way Handshake is started.
508 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
510 wpa_printf(MSG_DEBUG, "State: %s -> %s",
511 wpa_supplicant_state_txt(wpa_s->wpa_state),
512 wpa_supplicant_state_txt(state));
514 if (state != WPA_SCANNING)
515 wpa_supplicant_notify_scanning(wpa_s, 0);
517 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
520 if (state == WPA_COMPLETED && wpa_s->new_connection) {
521 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
522 struct wpa_ssid *ssid = wpa_s->current_ssid;
523 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
524 MACSTR " completed %s [id=%d id_str=%s]",
525 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
526 "(reauth)" : "(auth)",
527 ssid ? ssid->id : -1,
528 ssid && ssid->id_str ? ssid->id_str : "");
529 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
530 wpa_s->new_connection = 0;
531 wpa_s->reassociated_connection = 1;
532 wpa_drv_set_operstate(wpa_s, 1);
533 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
534 state == WPA_ASSOCIATED) {
535 wpa_s->new_connection = 1;
536 wpa_drv_set_operstate(wpa_s, 0);
538 wpa_s->wpa_state = state;
542 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
545 struct wpa_global *global = eloop_ctx;
546 struct wpa_supplicant *wpa_s;
547 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
548 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
555 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
557 wpa_s->pairwise_cipher = 0;
558 wpa_s->group_cipher = 0;
559 wpa_s->mgmt_group_cipher = 0;
561 wpa_s->wpa_state = WPA_DISCONNECTED;
566 * wpa_supplicant_reload_configuration - Reload configuration data
567 * @wpa_s: Pointer to wpa_supplicant data
568 * Returns: 0 on success or -1 if configuration parsing failed
570 * This function can be used to request that the configuration data is reloaded
571 * (e.g., after configuration file change). This function is reloading
572 * configuration only for one interface, so this may need to be called multiple
573 * times if %wpa_supplicant is controlling multiple interfaces and all
574 * interfaces need reconfiguration.
576 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
578 struct wpa_config *conf;
580 if (wpa_s->confname == NULL)
582 conf = wpa_config_read(wpa_s->confname);
584 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
585 "file '%s' - exiting", wpa_s->confname);
589 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
590 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
591 os_strcmp(conf->ctrl_interface,
592 wpa_s->conf->ctrl_interface) != 0);
594 if (reconf_ctrl && wpa_s->ctrl_iface) {
595 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
596 wpa_s->ctrl_iface = NULL;
599 eapol_sm_invalidate_cached_session(wpa_s->eapol);
600 wpa_s->current_ssid = NULL;
602 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
603 * pkcs11_engine_path, pkcs11_module_path.
605 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
607 * Clear forced success to clear EAP state for next
610 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
612 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
613 wpa_sm_set_config(wpa_s->wpa, NULL);
614 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
615 rsn_preauth_deinit(wpa_s->wpa);
616 wpa_config_free(wpa_s->conf);
619 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
621 wpa_supplicant_clear_status(wpa_s);
622 wpa_s->reassociate = 1;
623 wpa_supplicant_req_scan(wpa_s, 0, 0);
624 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
629 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
632 struct wpa_global *global = eloop_ctx;
633 struct wpa_supplicant *wpa_s;
634 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
635 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
636 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
643 static wpa_cipher cipher_suite2driver(int cipher)
646 case WPA_CIPHER_NONE:
648 case WPA_CIPHER_WEP40:
650 case WPA_CIPHER_WEP104:
651 return CIPHER_WEP104;
652 case WPA_CIPHER_CCMP:
654 case WPA_CIPHER_TKIP:
661 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
664 case WPA_KEY_MGMT_NONE:
665 return KEY_MGMT_NONE;
666 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
667 return KEY_MGMT_802_1X_NO_WPA;
668 case WPA_KEY_MGMT_IEEE8021X:
669 return KEY_MGMT_802_1X;
670 case WPA_KEY_MGMT_WPA_NONE:
671 return KEY_MGMT_WPA_NONE;
672 case WPA_KEY_MGMT_FT_IEEE8021X:
673 return KEY_MGMT_FT_802_1X;
674 case WPA_KEY_MGMT_FT_PSK:
675 return KEY_MGMT_FT_PSK;
676 case WPA_KEY_MGMT_IEEE8021X_SHA256:
677 return KEY_MGMT_802_1X_SHA256;
678 case WPA_KEY_MGMT_PSK_SHA256:
679 return KEY_MGMT_PSK_SHA256;
680 case WPA_KEY_MGMT_WPS:
682 case WPA_KEY_MGMT_PSK:
689 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
690 struct wpa_ssid *ssid,
691 struct wpa_ie_data *ie)
693 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
696 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
697 "from association info");
702 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
704 if (!(ie->group_cipher & ssid->group_cipher)) {
705 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
706 "cipher 0x%x (mask 0x%x) - reject",
707 ie->group_cipher, ssid->group_cipher);
710 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
711 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
712 "cipher 0x%x (mask 0x%x) - reject",
713 ie->pairwise_cipher, ssid->pairwise_cipher);
716 if (!(ie->key_mgmt & ssid->key_mgmt)) {
717 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
718 "management 0x%x (mask 0x%x) - reject",
719 ie->key_mgmt, ssid->key_mgmt);
723 #ifdef CONFIG_IEEE80211W
724 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
725 ssid->ieee80211w == IEEE80211W_REQUIRED) {
726 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
727 "that does not support management frame protection - "
731 #endif /* CONFIG_IEEE80211W */
738 * wpa_supplicant_set_suites - Set authentication and encryption parameters
739 * @wpa_s: Pointer to wpa_supplicant data
740 * @bss: Scan results for the selected BSS, or %NULL if not available
741 * @ssid: Configuration data for the selected network
742 * @wpa_ie: Buffer for the WPA/RSN IE
743 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
744 * used buffer length in case the functions returns success.
745 * Returns: 0 on success or -1 on failure
747 * This function is used to configure authentication and encryption parameters
748 * based on the network configuration and scan result for the selected BSS (if
751 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
752 struct wpa_scan_res *bss,
753 struct wpa_ssid *ssid,
754 u8 *wpa_ie, size_t *wpa_ie_len)
756 struct wpa_ie_data ie;
758 const u8 *bss_wpa, *bss_rsn;
761 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
762 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
764 bss_wpa = bss_rsn = NULL;
766 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
767 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
768 (ie.group_cipher & ssid->group_cipher) &&
769 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
770 (ie.key_mgmt & ssid->key_mgmt)) {
771 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
772 proto = WPA_PROTO_RSN;
773 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
774 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
775 (ie.group_cipher & ssid->group_cipher) &&
776 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
777 (ie.key_mgmt & ssid->key_mgmt)) {
778 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
779 proto = WPA_PROTO_WPA;
781 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
784 if (ssid->proto & WPA_PROTO_RSN)
785 proto = WPA_PROTO_RSN;
787 proto = WPA_PROTO_WPA;
788 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
789 os_memset(&ie, 0, sizeof(ie));
790 ie.group_cipher = ssid->group_cipher;
791 ie.pairwise_cipher = ssid->pairwise_cipher;
792 ie.key_mgmt = ssid->key_mgmt;
793 #ifdef CONFIG_IEEE80211W
794 ie.mgmt_group_cipher =
795 ssid->ieee80211w != NO_IEEE80211W ?
796 WPA_CIPHER_AES_128_CMAC : 0;
797 #endif /* CONFIG_IEEE80211W */
798 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
804 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
805 "pairwise %d key_mgmt %d proto %d",
806 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
807 #ifdef CONFIG_IEEE80211W
808 if (ssid->ieee80211w) {
809 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
810 ie.mgmt_group_cipher);
812 #endif /* CONFIG_IEEE80211W */
814 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
815 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
816 !!(ssid->proto & WPA_PROTO_RSN));
818 if (bss || !wpa_s->ap_ies_from_associnfo) {
819 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
820 bss_wpa ? 2 + bss_wpa[1] : 0) ||
821 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
822 bss_rsn ? 2 + bss_rsn[1] : 0))
826 sel = ie.group_cipher & ssid->group_cipher;
827 if (sel & WPA_CIPHER_CCMP) {
828 wpa_s->group_cipher = WPA_CIPHER_CCMP;
829 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
830 } else if (sel & WPA_CIPHER_TKIP) {
831 wpa_s->group_cipher = WPA_CIPHER_TKIP;
832 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
833 } else if (sel & WPA_CIPHER_WEP104) {
834 wpa_s->group_cipher = WPA_CIPHER_WEP104;
835 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
836 } else if (sel & WPA_CIPHER_WEP40) {
837 wpa_s->group_cipher = WPA_CIPHER_WEP40;
838 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
840 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
844 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
845 if (sel & WPA_CIPHER_CCMP) {
846 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
847 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
848 } else if (sel & WPA_CIPHER_TKIP) {
849 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
850 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
851 } else if (sel & WPA_CIPHER_NONE) {
852 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
853 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
855 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
860 sel = ie.key_mgmt & ssid->key_mgmt;
862 #ifdef CONFIG_IEEE80211R
863 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
864 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
865 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
866 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
867 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
868 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
869 #endif /* CONFIG_IEEE80211R */
870 #ifdef CONFIG_IEEE80211W
871 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
872 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
873 wpa_msg(wpa_s, MSG_DEBUG,
874 "WPA: using KEY_MGMT 802.1X with SHA256");
875 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
876 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
877 wpa_msg(wpa_s, MSG_DEBUG,
878 "WPA: using KEY_MGMT PSK with SHA256");
879 #endif /* CONFIG_IEEE80211W */
880 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
881 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
882 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
883 } else if (sel & WPA_KEY_MGMT_PSK) {
884 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
885 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
886 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
887 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
888 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
890 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
891 "key management type.");
895 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
896 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
897 wpa_s->pairwise_cipher);
898 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
900 #ifdef CONFIG_IEEE80211W
901 sel = ie.mgmt_group_cipher;
902 if (ssid->ieee80211w == NO_IEEE80211W ||
903 !(ie.capabilities & WPA_CAPABILITY_MFPC))
905 if (sel & WPA_CIPHER_AES_128_CMAC) {
906 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
907 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
910 wpa_s->mgmt_group_cipher = 0;
911 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
913 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
914 wpa_s->mgmt_group_cipher);
915 #endif /* CONFIG_IEEE80211W */
917 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
918 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
923 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
924 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
926 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
933 * wpa_supplicant_associate - Request association
934 * @wpa_s: Pointer to wpa_supplicant data
935 * @bss: Scan results for the selected BSS, or %NULL if not available
936 * @ssid: Configuration data for the selected network
938 * This function is used to request %wpa_supplicant to associate with a BSS.
940 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
941 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
945 int use_crypt, ret, i;
946 int algs = AUTH_ALG_OPEN_SYSTEM;
947 wpa_cipher cipher_pairwise, cipher_group;
948 struct wpa_driver_associate_params params;
949 int wep_keys_set = 0;
950 struct wpa_driver_capa capa;
951 int assoc_failed = 0;
953 if (ssid->mode == 2) {
955 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
956 wpa_printf(MSG_INFO, "Driver does not support AP "
960 wpa_supplicant_create_ap(wpa_s, ssid);
961 #else /* CONFIG_AP */
962 wpa_printf(MSG_ERROR, "AP mode support not included in the "
964 #endif /* CONFIG_AP */
968 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
969 sme_authenticate(wpa_s, bss, ssid);
973 wpa_s->reassociate = 0;
975 #ifdef CONFIG_IEEE80211R
977 #endif /* CONFIG_IEEE80211R */
978 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
979 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
980 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
981 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
982 os_memset(wpa_s->bssid, 0, ETH_ALEN);
983 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
984 #ifdef CONFIG_IEEE80211R
985 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
986 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
988 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
990 /* Prepare for the next transition */
991 wpa_ft_prepare_auth_request(wpa_s->wpa);
993 #endif /* CONFIG_IEEE80211R */
995 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
996 wpa_s->conf->ap_scan == 2 &&
997 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
998 /* Use ap_scan==1 style network selection to find the network
1000 wpa_s->scan_req = 2;
1001 wpa_s->reassociate = 1;
1002 wpa_supplicant_req_scan(wpa_s, 0, 0);
1004 #endif /* CONFIG_WPS */
1006 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1007 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1008 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1010 wpa_supplicant_cancel_scan(wpa_s);
1012 /* Starting new association, so clear the possibly used WPA IE from the
1013 * previous association. */
1014 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1016 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
1017 wpa_printf(MSG_WARNING, "Failed to set operating mode");
1021 #ifdef IEEE8021X_EAPOL
1022 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1024 if (ssid->non_leap == 0)
1025 algs = AUTH_ALG_LEAP;
1027 algs |= AUTH_ALG_LEAP;
1030 #endif /* IEEE8021X_EAPOL */
1031 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1032 if (ssid->auth_alg) {
1034 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1035 algs |= AUTH_ALG_OPEN_SYSTEM;
1036 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1037 algs |= AUTH_ALG_SHARED_KEY;
1038 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1039 algs |= AUTH_ALG_LEAP;
1040 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1043 wpa_drv_set_auth_alg(wpa_s, algs);
1045 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1046 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1047 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1048 WPA_KEY_MGMT_FT_IEEE8021X |
1049 WPA_KEY_MGMT_FT_PSK |
1050 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1051 WPA_KEY_MGMT_PSK_SHA256))) {
1052 int try_opportunistic;
1053 try_opportunistic = ssid->proactive_key_caching &&
1054 (ssid->proto & WPA_PROTO_RSN);
1055 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1056 wpa_s->current_ssid,
1057 try_opportunistic) == 0)
1058 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1059 wpa_ie_len = sizeof(wpa_ie);
1060 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1061 wpa_ie, &wpa_ie_len)) {
1062 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1063 "management and encryption suites");
1066 } else if (ssid->key_mgmt &
1067 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1068 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1069 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1070 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1071 wpa_ie_len = sizeof(wpa_ie);
1072 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1073 wpa_ie, &wpa_ie_len)) {
1074 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1075 "management and encryption suites (no scan "
1080 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1081 struct wpabuf *wps_ie;
1082 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1083 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1084 wpa_ie_len = wpabuf_len(wps_ie);
1085 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1088 wpabuf_free(wps_ie);
1089 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1090 #endif /* CONFIG_WPS */
1092 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1096 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1098 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1099 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1100 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1101 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1102 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1104 if (wpa_set_wep_keys(wpa_s, ssid)) {
1109 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1112 #ifdef IEEE8021X_EAPOL
1113 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1114 if ((ssid->eapol_flags &
1115 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1116 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1120 /* Assume that dynamic WEP-104 keys will be used and
1121 * set cipher suites in order for drivers to expect
1123 cipher_pairwise = cipher_group = CIPHER_WEP104;
1126 #endif /* IEEE8021X_EAPOL */
1128 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1129 /* Set the key before (and later after) association */
1130 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1133 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1134 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1135 os_memset(¶ms, 0, sizeof(params));
1137 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1138 params.bssid = bss->bssid;
1139 params.ssid = ie ? ie + 2 : (u8 *) "";
1140 params.ssid_len = ie ? ie[1] : 0;
1141 params.freq = bss->freq;
1143 params.ssid = ssid->ssid;
1144 params.ssid_len = ssid->ssid_len;
1146 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1147 params.freq = ssid->frequency; /* Initial channel for IBSS */
1148 params.wpa_ie = wpa_ie;
1149 params.wpa_ie_len = wpa_ie_len;
1150 params.pairwise_suite = cipher_pairwise;
1151 params.group_suite = cipher_group;
1152 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1153 params.auth_alg = algs;
1154 params.mode = ssid->mode;
1155 for (i = 0; i < NUM_WEP_KEYS; i++) {
1156 if (ssid->wep_key_len[i])
1157 params.wep_key[i] = ssid->wep_key[i];
1158 params.wep_key_len[i] = ssid->wep_key_len[i];
1160 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1162 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1163 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1164 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1165 params.passphrase = ssid->passphrase;
1167 params.psk = ssid->psk;
1170 params.drop_unencrypted = use_crypt;
1172 #ifdef CONFIG_IEEE80211W
1173 switch (ssid->ieee80211w) {
1175 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1177 case IEEE80211W_OPTIONAL:
1178 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1180 case IEEE80211W_REQUIRED:
1181 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1184 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1185 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1186 struct wpa_ie_data ie;
1187 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1189 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1190 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1192 params.mgmt_frame_protection =
1193 MGMT_FRAME_PROTECTION_REQUIRED;
1196 #endif /* CONFIG_IEEE80211W */
1198 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1199 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1201 ret = wpa_drv_associate(wpa_s, ¶ms);
1203 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1205 /* try to continue anyway; new association will be tried again
1210 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1211 /* Set the key after the association just in case association
1212 * cleared the previously configured key. */
1213 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1214 /* No need to timeout authentication since there is no key
1216 wpa_supplicant_cancel_auth_timeout(wpa_s);
1217 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1218 #ifdef CONFIG_IBSS_RSN
1219 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1220 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1221 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1222 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1224 * RSN IBSS authentication is per-STA and we can disable the
1225 * per-BSSID authentication.
1227 wpa_supplicant_cancel_auth_timeout(wpa_s);
1228 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1229 #endif /* CONFIG_IBSS_RSN */
1231 /* Timeout for IEEE 802.11 authentication and association */
1235 /* give IBSS a bit more time */
1236 timeout = ssid->mode ? 10 : 5;
1237 } else if (wpa_s->conf->ap_scan == 1) {
1238 /* give IBSS a bit more time */
1239 timeout = ssid->mode ? 20 : 10;
1241 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1244 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1245 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1246 /* Set static WEP keys again */
1247 wpa_set_wep_keys(wpa_s, ssid);
1250 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1252 * Do not allow EAP session resumption between different
1253 * network configurations.
1255 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1257 wpa_s->current_ssid = ssid;
1258 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1259 wpa_supplicant_initiate_eapol(wpa_s);
1264 * wpa_supplicant_disassociate - Disassociate the current connection
1265 * @wpa_s: Pointer to wpa_supplicant data
1266 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1268 * This function is used to request %wpa_supplicant to disassociate with the
1271 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1275 if (!is_zero_ether_addr(wpa_s->bssid)) {
1276 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1277 ieee80211_sta_disassociate(wpa_s, reason_code);
1279 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1280 addr = wpa_s->bssid;
1282 wpa_clear_keys(wpa_s, addr);
1283 wpa_supplicant_mark_disassoc(wpa_s);
1284 wpa_s->current_ssid = NULL;
1285 wpa_sm_set_config(wpa_s->wpa, NULL);
1286 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1291 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1292 * @wpa_s: Pointer to wpa_supplicant data
1293 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1295 * This function is used to request %wpa_supplicant to deauthenticate from the
1298 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1302 if (!is_zero_ether_addr(wpa_s->bssid)) {
1303 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1304 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1306 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1308 addr = wpa_s->bssid;
1310 wpa_clear_keys(wpa_s, addr);
1311 wpa_supplicant_mark_disassoc(wpa_s);
1312 wpa_s->current_ssid = NULL;
1313 wpa_sm_set_config(wpa_s->wpa, NULL);
1314 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1318 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1320 #define SCAN_AP_LIMIT 128
1321 struct wpa_scan_result *results;
1323 struct wpa_scan_results *res;
1325 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1326 if (results == NULL) {
1327 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1332 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1333 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1335 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1339 if (num > SCAN_AP_LIMIT) {
1340 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1341 num, SCAN_AP_LIMIT);
1342 num = SCAN_AP_LIMIT;
1345 wpa_scan_results_free(wpa_s->scan_res);
1346 wpa_s->scan_res = NULL;
1348 /* Convert old scan result data structure to the new one */
1349 res = os_zalloc(sizeof(*res));
1354 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1355 if (res->res == NULL) {
1361 for (i = 0; i < num; i++) {
1362 struct wpa_scan_result *bss = &results[i];
1363 struct wpa_scan_res *r;
1367 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1370 if (bss->mdie_present)
1373 r = os_zalloc(sizeof(*r) + ie_len);
1377 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1378 r->freq = bss->freq;
1379 r->caps = bss->caps;
1380 r->qual = bss->qual;
1381 r->noise = bss->noise;
1382 r->level = bss->level;
1386 pos = (u8 *) (r + 1);
1389 *pos++ = WLAN_EID_SSID;
1390 *pos++ = bss->ssid_len;
1391 os_memcpy(pos, bss->ssid, bss->ssid_len);
1392 pos += bss->ssid_len;
1395 /* Fake Supported Rate IE to include max rate */
1396 *pos++ = WLAN_EID_SUPP_RATES;
1398 *pos++ = bss->maxrate;
1401 if (bss->rsn_ie_len) {
1402 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1403 pos += bss->rsn_ie_len;
1406 if (bss->mdie_present) {
1407 os_memcpy(pos, bss->mdie, 5);
1411 if (bss->wpa_ie_len) {
1412 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1413 pos += bss->wpa_ie_len;
1416 res->res[res->num++] = r;
1420 wpa_s->scan_res = res;
1427 * wpa_supplicant_get_scan_results - Get scan results
1428 * @wpa_s: Pointer to wpa_supplicant data
1429 * Returns: 0 on success, -1 on failure
1431 * This function is request the current scan results from the driver and stores
1432 * a local copy of the results in wpa_s->scan_res.
1434 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1438 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1439 wpa_scan_results_free(wpa_s->scan_res);
1440 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1441 if (wpa_s->scan_res == NULL) {
1442 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1446 } else if (wpa_s->driver->get_scan_results2 == NULL)
1447 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1449 wpa_scan_results_free(wpa_s->scan_res);
1450 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1451 if (wpa_s->scan_res == NULL) {
1452 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1458 if (wpa_s->scan_res)
1459 wpa_scan_sort_results(wpa_s->scan_res);
1466 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1467 * @wpa_s: Pointer to wpa_supplicant data
1468 * Returns: A pointer to the current network structure or %NULL on failure
1470 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1472 struct wpa_ssid *entry;
1473 u8 ssid[MAX_SSID_LEN];
1479 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1480 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1481 wpa_printf(MSG_WARNING, "Could not read SSID from "
1486 res = wpa_drv_get_ssid(wpa_s, ssid);
1488 wpa_printf(MSG_WARNING, "Could not read SSID from "
1495 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1496 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1497 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1498 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1502 wired = wpa_s->conf->ap_scan == 0 &&
1503 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1505 entry = wpa_s->conf->ssid;
1507 if (!entry->disabled &&
1508 ((ssid_len == entry->ssid_len &&
1509 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1510 (!entry->bssid_set ||
1511 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1514 if (!entry->disabled &&
1515 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1516 (entry->ssid == NULL || entry->ssid_len == 0) &&
1517 (!entry->bssid_set ||
1518 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1520 #endif /* CONFIG_WPS */
1521 entry = entry->next;
1528 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1538 if (wpa_drivers[0] == NULL) {
1539 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1545 /* default to first driver in the list */
1546 wpa_s->driver = wpa_drivers[0];
1550 pos = os_strchr(name, ',');
1554 len = os_strlen(name);
1555 for (i = 0; wpa_drivers[i]; i++) {
1556 if (os_strlen(wpa_drivers[i]->name) == len &&
1557 os_strncmp(name, wpa_drivers[i]->name, len) ==
1559 wpa_s->driver = wpa_drivers[i];
1564 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1569 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1570 const u8 *buf, size_t len)
1572 struct wpa_supplicant *wpa_s = ctx;
1574 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1575 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1578 if (wpa_s->ap_iface) {
1579 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1582 #endif /* CONFIG_AP */
1584 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1585 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1586 "no key management is configured");
1590 if (wpa_s->eapol_received == 0 &&
1591 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1592 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1593 wpa_s->wpa_state != WPA_COMPLETED)) {
1594 /* Timeout for completing IEEE 802.1X and WPA authentication */
1595 wpa_supplicant_req_auth_timeout(
1597 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1598 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1599 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1602 wpa_s->eapol_received++;
1604 if (wpa_s->countermeasures) {
1605 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1610 #ifdef CONFIG_IBSS_RSN
1611 if (wpa_s->current_ssid &&
1612 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1613 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1616 #endif /* CONFIG_IBSS_RSN */
1618 /* Source address of the incoming EAPOL frame could be compared to the
1619 * current BSSID. However, it is possible that a centralized
1620 * Authenticator could be using another MAC address than the BSSID of
1621 * an AP, so just allow any address to be used for now. The replies are
1622 * still sent to the current BSSID (if available), though. */
1624 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1625 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1626 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1628 wpa_drv_poll(wpa_s);
1629 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1630 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1631 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1633 * Set portValid = TRUE here since we are going to skip 4-way
1634 * handshake processing which would normally set portValid. We
1635 * need this to allow the EAPOL state machines to be completed
1636 * without going through EAPOL-Key handshake.
1638 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1643 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1644 struct ieee80211_rx_status *rx_status)
1646 struct wpa_supplicant *wpa_s = ctx;
1647 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1652 * wpa_supplicant_driver_init - Initialize driver interface parameters
1653 * @wpa_s: Pointer to wpa_supplicant data
1654 * Returns: 0 on success, -1 on failure
1656 * This function is called to initialize driver interface parameters.
1657 * wpa_drv_init() must have been called before this function to initialize the
1660 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1662 static int interface_count = 0;
1664 if (wpa_s->driver->send_eapol) {
1665 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1667 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1669 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1670 wpa_drv_get_mac_addr(wpa_s),
1672 wpa_supplicant_rx_eapol, wpa_s, 0);
1673 if (wpa_s->l2 == NULL)
1677 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1678 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1682 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1683 MAC2STR(wpa_s->own_addr));
1685 if (wpa_s->bridge_ifname[0]) {
1686 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1687 " '%s'", wpa_s->bridge_ifname);
1688 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1691 wpa_supplicant_rx_eapol, wpa_s,
1693 if (wpa_s->l2_br == NULL) {
1694 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1695 "connection for the bridge interface '%s'",
1696 wpa_s->bridge_ifname);
1701 /* Backwards compatibility call to set_wpa() handler. This is called
1702 * only just after init and just before deinit, so these handler can be
1703 * used to implement same functionality. */
1704 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1705 struct wpa_driver_capa capa;
1706 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1707 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1708 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1709 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1710 /* Continue to allow non-WPA modes to be used. */
1712 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1718 wpa_clear_keys(wpa_s, NULL);
1720 /* Make sure that TKIP countermeasures are not left enabled (could
1721 * happen if wpa_supplicant is killed during countermeasures. */
1722 wpa_drv_set_countermeasures(wpa_s, 0);
1724 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1726 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1727 wpa_drv_flush_pmkid(wpa_s);
1729 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1730 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1737 static int wpa_supplicant_daemon(const char *pid_file)
1739 wpa_printf(MSG_DEBUG, "Daemonize..");
1740 return os_daemonize(pid_file);
1744 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1746 struct wpa_supplicant *wpa_s;
1748 wpa_s = os_zalloc(sizeof(*wpa_s));
1751 wpa_s->scan_req = 1;
1757 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1758 struct wpa_interface *iface)
1760 const char *ifname, *driver;
1761 struct wpa_driver_capa capa;
1763 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1764 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1765 iface->confname ? iface->confname : "N/A",
1766 iface->driver ? iface->driver : "default",
1767 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1768 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1770 if (iface->confname) {
1771 #ifdef CONFIG_BACKEND_FILE
1772 wpa_s->confname = os_rel2abs_path(iface->confname);
1773 if (wpa_s->confname == NULL) {
1774 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1775 "for configuration file '%s'.",
1779 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1780 iface->confname, wpa_s->confname);
1781 #else /* CONFIG_BACKEND_FILE */
1782 wpa_s->confname = os_strdup(iface->confname);
1783 #endif /* CONFIG_BACKEND_FILE */
1784 wpa_s->conf = wpa_config_read(wpa_s->confname);
1785 if (wpa_s->conf == NULL) {
1786 wpa_printf(MSG_ERROR, "Failed to read or parse "
1787 "configuration '%s'.", wpa_s->confname);
1792 * Override ctrl_interface and driver_param if set on command
1795 if (iface->ctrl_interface) {
1796 os_free(wpa_s->conf->ctrl_interface);
1797 wpa_s->conf->ctrl_interface =
1798 os_strdup(iface->ctrl_interface);
1801 if (iface->driver_param) {
1802 os_free(wpa_s->conf->driver_param);
1803 wpa_s->conf->driver_param =
1804 os_strdup(iface->driver_param);
1807 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1808 iface->driver_param);
1810 if (wpa_s->conf == NULL) {
1811 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1815 if (iface->ifname == NULL) {
1816 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1819 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1820 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1824 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1826 if (iface->bridge_ifname) {
1827 if (os_strlen(iface->bridge_ifname) >=
1828 sizeof(wpa_s->bridge_ifname)) {
1829 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1830 "name '%s'.", iface->bridge_ifname);
1833 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1834 sizeof(wpa_s->bridge_ifname));
1837 /* RSNA Supplicant Key Management - INITIALIZE */
1838 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1839 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1841 /* Initialize driver interface and register driver event handler before
1842 * L2 receive handler so that association events are processed before
1843 * EAPOL-Key packets if both become available for the same select()
1845 driver = iface->driver;
1847 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
1850 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1851 if (wpa_s->drv_priv == NULL) {
1853 pos = os_strchr(driver, ',');
1855 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
1856 "interface - try next driver wrapper");
1860 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1863 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1864 wpa_printf(MSG_ERROR, "Driver interface rejected "
1865 "driver_param '%s'", wpa_s->conf->driver_param);
1869 ifname = wpa_drv_get_ifname(wpa_s);
1870 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1871 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1872 "name with '%s'", ifname);
1873 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1876 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1879 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1880 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1882 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1884 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1885 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1886 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1887 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1888 "dot11RSNAConfigPMKLifetime");
1892 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1893 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1894 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1895 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1896 "dot11RSNAConfigPMKReauthThreshold");
1900 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1901 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1902 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1903 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1904 "dot11RSNAConfigSATimeout");
1908 if (wpa_supplicant_driver_init(wpa_s) < 0)
1911 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
1912 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
1913 wpa_printf(MSG_DEBUG, "Failed to set country");
1917 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1919 if (wpas_wps_init(wpa_s))
1922 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1924 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1926 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1927 if (wpa_s->ctrl_iface == NULL) {
1928 wpa_printf(MSG_ERROR,
1929 "Failed to initialize control interface '%s'.\n"
1930 "You may have another wpa_supplicant process "
1931 "already running or the file was\n"
1932 "left by an unclean termination of wpa_supplicant "
1933 "in which case you will need\n"
1934 "to manually remove this file before starting "
1935 "wpa_supplicant again.\n",
1936 wpa_s->conf->ctrl_interface);
1940 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1941 wpa_s->drv_flags = capa.flags;
1942 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1943 if (ieee80211_sta_init(wpa_s))
1946 wpa_s->max_scan_ssids = capa.max_scan_ssids;
1949 #ifdef CONFIG_IBSS_RSN
1950 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1951 if (!wpa_s->ibss_rsn) {
1952 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
1955 #endif /* CONFIG_IBSS_RSN */
1961 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1963 if (wpa_s->drv_priv) {
1964 wpa_supplicant_deauthenticate(wpa_s,
1965 WLAN_REASON_DEAUTH_LEAVING);
1967 /* Backwards compatibility call to set_wpa() handler. This is
1968 * called only just after init and just before deinit, so these
1969 * handler can be used to implement same functionality. */
1970 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1971 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
1975 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1976 wpa_drv_set_countermeasures(wpa_s, 0);
1977 wpa_clear_keys(wpa_s, NULL);
1980 wpas_dbus_unregister_iface(wpa_s);
1982 wpa_supplicant_cleanup(wpa_s);
1984 if (wpa_s->drv_priv)
1985 wpa_drv_deinit(wpa_s);
1990 * wpa_supplicant_add_iface - Add a new network interface
1991 * @global: Pointer to global data from wpa_supplicant_init()
1992 * @iface: Interface configuration options
1993 * Returns: Pointer to the created interface or %NULL on failure
1995 * This function is used to add new network interfaces for %wpa_supplicant.
1996 * This can be called before wpa_supplicant_run() to add interfaces before the
1997 * main event loop has been started. In addition, new interfaces can be added
1998 * dynamically while %wpa_supplicant is already running. This could happen,
1999 * e.g., when a hotplug network adapter is inserted.
2001 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2002 struct wpa_interface *iface)
2004 struct wpa_supplicant *wpa_s;
2006 if (global == NULL || iface == NULL)
2009 wpa_s = wpa_supplicant_alloc();
2013 if (wpa_supplicant_init_iface(wpa_s, iface)) {
2014 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2016 wpa_supplicant_deinit_iface(wpa_s);
2021 wpa_s->global = global;
2023 /* Register the interface with the dbus control interface */
2024 if (wpas_dbus_register_iface(wpa_s)) {
2025 wpa_supplicant_deinit_iface(wpa_s);
2030 wpa_s->next = global->ifaces;
2031 global->ifaces = wpa_s;
2033 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2040 * wpa_supplicant_remove_iface - Remove a network interface
2041 * @global: Pointer to global data from wpa_supplicant_init()
2042 * @wpa_s: Pointer to the network interface to be removed
2043 * Returns: 0 if interface was removed, -1 if interface was not found
2045 * This function can be used to dynamically remove network interfaces from
2046 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2047 * addition, this function is used to remove all remaining interfaces when
2048 * %wpa_supplicant is terminated.
2050 int wpa_supplicant_remove_iface(struct wpa_global *global,
2051 struct wpa_supplicant *wpa_s)
2053 struct wpa_supplicant *prev;
2055 /* Remove interface from the global list of interfaces */
2056 prev = global->ifaces;
2057 if (prev == wpa_s) {
2058 global->ifaces = wpa_s->next;
2060 while (prev && prev->next != wpa_s)
2064 prev->next = wpa_s->next;
2067 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2069 wpa_supplicant_deinit_iface(wpa_s);
2077 * wpa_supplicant_get_iface - Get a new network interface
2078 * @global: Pointer to global data from wpa_supplicant_init()
2079 * @ifname: Interface name
2080 * Returns: Pointer to the interface or %NULL if not found
2082 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2085 struct wpa_supplicant *wpa_s;
2087 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2088 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2096 * wpa_supplicant_init - Initialize %wpa_supplicant
2097 * @params: Parameters for %wpa_supplicant
2098 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2100 * This function is used to initialize %wpa_supplicant. After successful
2101 * initialization, the returned data pointer can be used to add and remove
2102 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2104 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2106 struct wpa_global *global;
2112 wpa_debug_open_file(params->wpa_debug_file_path);
2113 if (params->wpa_debug_syslog)
2114 wpa_debug_open_syslog();
2116 ret = eap_peer_register_methods();
2118 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2120 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2121 "the same EAP type.");
2126 ret = eap_server_register_methods();
2128 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2130 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2131 "the same EAP type.");
2134 #endif /* CONFIG_AP */
2136 global = os_zalloc(sizeof(*global));
2139 global->params.daemonize = params->daemonize;
2140 global->params.wait_for_monitor = params->wait_for_monitor;
2141 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2142 if (params->pid_file)
2143 global->params.pid_file = os_strdup(params->pid_file);
2144 if (params->ctrl_interface)
2145 global->params.ctrl_interface =
2146 os_strdup(params->ctrl_interface);
2147 wpa_debug_level = global->params.wpa_debug_level =
2148 params->wpa_debug_level;
2149 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2150 params->wpa_debug_show_keys;
2151 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2152 params->wpa_debug_timestamp;
2154 if (eloop_init(global)) {
2155 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2156 wpa_supplicant_deinit(global);
2160 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2161 if (global->ctrl_iface == NULL) {
2162 wpa_supplicant_deinit(global);
2166 if (global->params.dbus_ctrl_interface) {
2167 global->dbus_ctrl_iface =
2168 wpa_supplicant_dbus_ctrl_iface_init(global);
2169 if (global->dbus_ctrl_iface == NULL) {
2170 wpa_supplicant_deinit(global);
2175 for (i = 0; wpa_drivers[i]; i++)
2176 global->drv_count++;
2177 if (global->drv_count == 0) {
2178 wpa_printf(MSG_ERROR, "No drivers enabled");
2179 wpa_supplicant_deinit(global);
2182 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2183 if (global->drv_priv == NULL) {
2184 wpa_supplicant_deinit(global);
2187 for (i = 0; wpa_drivers[i]; i++) {
2188 if (!wpa_drivers[i]->global_init)
2190 global->drv_priv[i] = wpa_drivers[i]->global_init();
2191 if (global->drv_priv[i] == NULL) {
2192 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2193 "'%s'", wpa_drivers[i]->name);
2194 wpa_supplicant_deinit(global);
2204 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2205 * @global: Pointer to global data from wpa_supplicant_init()
2206 * Returns: 0 after successful event loop run, -1 on failure
2208 * This function starts the main event loop and continues running as long as
2209 * there are any remaining events. In most cases, this function is running as
2210 * long as the %wpa_supplicant process in still in use.
2212 int wpa_supplicant_run(struct wpa_global *global)
2214 struct wpa_supplicant *wpa_s;
2216 if (global->params.daemonize &&
2217 wpa_supplicant_daemon(global->params.pid_file))
2220 if (global->params.wait_for_monitor) {
2221 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2222 if (wpa_s->ctrl_iface)
2223 wpa_supplicant_ctrl_iface_wait(
2227 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2228 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2237 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2238 * @global: Pointer to global data from wpa_supplicant_init()
2240 * This function is called to deinitialize %wpa_supplicant and to free all
2241 * allocated resources. Remaining network interfaces will also be removed.
2243 void wpa_supplicant_deinit(struct wpa_global *global)
2250 while (global->ifaces)
2251 wpa_supplicant_remove_iface(global, global->ifaces);
2253 if (global->ctrl_iface)
2254 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2255 if (global->dbus_ctrl_iface)
2256 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2258 eap_peer_unregister_methods();
2260 eap_server_unregister_methods();
2261 #endif /* CONFIG_AP */
2263 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2264 if (!global->drv_priv[i])
2266 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2268 os_free(global->drv_priv);
2272 if (global->params.pid_file) {
2273 os_daemonize_terminate(global->params.pid_file);
2274 os_free(global->params.pid_file);
2276 os_free(global->params.ctrl_interface);
2279 wpa_debug_close_syslog();
2280 wpa_debug_close_file();