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 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
517 if (state == WPA_COMPLETED && wpa_s->new_connection) {
518 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
519 struct wpa_ssid *ssid = wpa_s->current_ssid;
520 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
521 MACSTR " completed %s [id=%d id_str=%s]",
522 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
523 "(reauth)" : "(auth)",
524 ssid ? ssid->id : -1,
525 ssid && ssid->id_str ? ssid->id_str : "");
526 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
527 wpa_s->new_connection = 0;
528 wpa_s->reassociated_connection = 1;
529 wpa_drv_set_operstate(wpa_s, 1);
530 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
531 state == WPA_ASSOCIATED) {
532 wpa_s->new_connection = 1;
533 wpa_drv_set_operstate(wpa_s, 0);
535 wpa_s->wpa_state = state;
539 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
542 struct wpa_global *global = eloop_ctx;
543 struct wpa_supplicant *wpa_s;
544 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
545 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
552 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
554 wpa_s->pairwise_cipher = 0;
555 wpa_s->group_cipher = 0;
556 wpa_s->mgmt_group_cipher = 0;
558 wpa_s->wpa_state = WPA_DISCONNECTED;
563 * wpa_supplicant_reload_configuration - Reload configuration data
564 * @wpa_s: Pointer to wpa_supplicant data
565 * Returns: 0 on success or -1 if configuration parsing failed
567 * This function can be used to request that the configuration data is reloaded
568 * (e.g., after configuration file change). This function is reloading
569 * configuration only for one interface, so this may need to be called multiple
570 * times if %wpa_supplicant is controlling multiple interfaces and all
571 * interfaces need reconfiguration.
573 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
575 struct wpa_config *conf;
577 if (wpa_s->confname == NULL)
579 conf = wpa_config_read(wpa_s->confname);
581 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
582 "file '%s' - exiting", wpa_s->confname);
586 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
587 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
588 os_strcmp(conf->ctrl_interface,
589 wpa_s->conf->ctrl_interface) != 0);
591 if (reconf_ctrl && wpa_s->ctrl_iface) {
592 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
593 wpa_s->ctrl_iface = NULL;
596 eapol_sm_invalidate_cached_session(wpa_s->eapol);
597 wpa_s->current_ssid = NULL;
599 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
600 * pkcs11_engine_path, pkcs11_module_path.
602 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
604 * Clear forced success to clear EAP state for next
607 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
609 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
610 wpa_sm_set_config(wpa_s->wpa, NULL);
611 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
612 rsn_preauth_deinit(wpa_s->wpa);
613 wpa_config_free(wpa_s->conf);
616 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
618 wpa_supplicant_clear_status(wpa_s);
619 wpa_s->reassociate = 1;
620 wpa_supplicant_req_scan(wpa_s, 0, 0);
621 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
626 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
629 struct wpa_global *global = eloop_ctx;
630 struct wpa_supplicant *wpa_s;
631 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
632 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
633 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
640 static wpa_cipher cipher_suite2driver(int cipher)
643 case WPA_CIPHER_NONE:
645 case WPA_CIPHER_WEP40:
647 case WPA_CIPHER_WEP104:
648 return CIPHER_WEP104;
649 case WPA_CIPHER_CCMP:
651 case WPA_CIPHER_TKIP:
658 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
661 case WPA_KEY_MGMT_NONE:
662 return KEY_MGMT_NONE;
663 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
664 return KEY_MGMT_802_1X_NO_WPA;
665 case WPA_KEY_MGMT_IEEE8021X:
666 return KEY_MGMT_802_1X;
667 case WPA_KEY_MGMT_WPA_NONE:
668 return KEY_MGMT_WPA_NONE;
669 case WPA_KEY_MGMT_FT_IEEE8021X:
670 return KEY_MGMT_FT_802_1X;
671 case WPA_KEY_MGMT_FT_PSK:
672 return KEY_MGMT_FT_PSK;
673 case WPA_KEY_MGMT_IEEE8021X_SHA256:
674 return KEY_MGMT_802_1X_SHA256;
675 case WPA_KEY_MGMT_PSK_SHA256:
676 return KEY_MGMT_PSK_SHA256;
677 case WPA_KEY_MGMT_WPS:
679 case WPA_KEY_MGMT_PSK:
686 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
687 struct wpa_ssid *ssid,
688 struct wpa_ie_data *ie)
690 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
693 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
694 "from association info");
699 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
701 if (!(ie->group_cipher & ssid->group_cipher)) {
702 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
703 "cipher 0x%x (mask 0x%x) - reject",
704 ie->group_cipher, ssid->group_cipher);
707 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
708 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
709 "cipher 0x%x (mask 0x%x) - reject",
710 ie->pairwise_cipher, ssid->pairwise_cipher);
713 if (!(ie->key_mgmt & ssid->key_mgmt)) {
714 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
715 "management 0x%x (mask 0x%x) - reject",
716 ie->key_mgmt, ssid->key_mgmt);
720 #ifdef CONFIG_IEEE80211W
721 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
722 ssid->ieee80211w == IEEE80211W_REQUIRED) {
723 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
724 "that does not support management frame protection - "
728 #endif /* CONFIG_IEEE80211W */
735 * wpa_supplicant_set_suites - Set authentication and encryption parameters
736 * @wpa_s: Pointer to wpa_supplicant data
737 * @bss: Scan results for the selected BSS, or %NULL if not available
738 * @ssid: Configuration data for the selected network
739 * @wpa_ie: Buffer for the WPA/RSN IE
740 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
741 * used buffer length in case the functions returns success.
742 * Returns: 0 on success or -1 on failure
744 * This function is used to configure authentication and encryption parameters
745 * based on the network configuration and scan result for the selected BSS (if
748 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
749 struct wpa_scan_res *bss,
750 struct wpa_ssid *ssid,
751 u8 *wpa_ie, size_t *wpa_ie_len)
753 struct wpa_ie_data ie;
755 const u8 *bss_wpa, *bss_rsn;
758 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
759 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
761 bss_wpa = bss_rsn = NULL;
763 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
764 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
765 (ie.group_cipher & ssid->group_cipher) &&
766 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
767 (ie.key_mgmt & ssid->key_mgmt)) {
768 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
769 proto = WPA_PROTO_RSN;
770 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
771 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
772 (ie.group_cipher & ssid->group_cipher) &&
773 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
774 (ie.key_mgmt & ssid->key_mgmt)) {
775 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
776 proto = WPA_PROTO_WPA;
778 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
781 if (ssid->proto & WPA_PROTO_RSN)
782 proto = WPA_PROTO_RSN;
784 proto = WPA_PROTO_WPA;
785 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
786 os_memset(&ie, 0, sizeof(ie));
787 ie.group_cipher = ssid->group_cipher;
788 ie.pairwise_cipher = ssid->pairwise_cipher;
789 ie.key_mgmt = ssid->key_mgmt;
790 #ifdef CONFIG_IEEE80211W
791 ie.mgmt_group_cipher =
792 ssid->ieee80211w != NO_IEEE80211W ?
793 WPA_CIPHER_AES_128_CMAC : 0;
794 #endif /* CONFIG_IEEE80211W */
795 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
801 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
802 "pairwise %d key_mgmt %d proto %d",
803 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
804 #ifdef CONFIG_IEEE80211W
805 if (ssid->ieee80211w) {
806 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
807 ie.mgmt_group_cipher);
809 #endif /* CONFIG_IEEE80211W */
811 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
812 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
813 !!(ssid->proto & WPA_PROTO_RSN));
815 if (bss || !wpa_s->ap_ies_from_associnfo) {
816 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
817 bss_wpa ? 2 + bss_wpa[1] : 0) ||
818 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
819 bss_rsn ? 2 + bss_rsn[1] : 0))
823 sel = ie.group_cipher & ssid->group_cipher;
824 if (sel & WPA_CIPHER_CCMP) {
825 wpa_s->group_cipher = WPA_CIPHER_CCMP;
826 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
827 } else if (sel & WPA_CIPHER_TKIP) {
828 wpa_s->group_cipher = WPA_CIPHER_TKIP;
829 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
830 } else if (sel & WPA_CIPHER_WEP104) {
831 wpa_s->group_cipher = WPA_CIPHER_WEP104;
832 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
833 } else if (sel & WPA_CIPHER_WEP40) {
834 wpa_s->group_cipher = WPA_CIPHER_WEP40;
835 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
837 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
841 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
842 if (sel & WPA_CIPHER_CCMP) {
843 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
844 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
845 } else if (sel & WPA_CIPHER_TKIP) {
846 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
847 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
848 } else if (sel & WPA_CIPHER_NONE) {
849 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
850 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
852 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
857 sel = ie.key_mgmt & ssid->key_mgmt;
859 #ifdef CONFIG_IEEE80211R
860 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
861 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
862 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
863 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
864 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
865 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
866 #endif /* CONFIG_IEEE80211R */
867 #ifdef CONFIG_IEEE80211W
868 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
869 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
870 wpa_msg(wpa_s, MSG_DEBUG,
871 "WPA: using KEY_MGMT 802.1X with SHA256");
872 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
873 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
874 wpa_msg(wpa_s, MSG_DEBUG,
875 "WPA: using KEY_MGMT PSK with SHA256");
876 #endif /* CONFIG_IEEE80211W */
877 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
878 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
879 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
880 } else if (sel & WPA_KEY_MGMT_PSK) {
881 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
882 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
883 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
884 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
885 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
887 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
888 "key management type.");
892 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
893 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
894 wpa_s->pairwise_cipher);
895 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
897 #ifdef CONFIG_IEEE80211W
898 sel = ie.mgmt_group_cipher;
899 if (ssid->ieee80211w == NO_IEEE80211W ||
900 !(ie.capabilities & WPA_CAPABILITY_MFPC))
902 if (sel & WPA_CIPHER_AES_128_CMAC) {
903 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
904 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
907 wpa_s->mgmt_group_cipher = 0;
908 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
910 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
911 wpa_s->mgmt_group_cipher);
912 #endif /* CONFIG_IEEE80211W */
914 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
915 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
920 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
921 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
923 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
930 * wpa_supplicant_associate - Request association
931 * @wpa_s: Pointer to wpa_supplicant data
932 * @bss: Scan results for the selected BSS, or %NULL if not available
933 * @ssid: Configuration data for the selected network
935 * This function is used to request %wpa_supplicant to associate with a BSS.
937 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
938 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
942 int use_crypt, ret, i;
943 int algs = AUTH_ALG_OPEN_SYSTEM;
944 wpa_cipher cipher_pairwise, cipher_group;
945 struct wpa_driver_associate_params params;
946 int wep_keys_set = 0;
947 struct wpa_driver_capa capa;
948 int assoc_failed = 0;
950 if (ssid->mode == 2) {
952 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
953 wpa_printf(MSG_INFO, "Driver does not support AP "
957 wpa_supplicant_create_ap(wpa_s, ssid);
958 #else /* CONFIG_AP */
959 wpa_printf(MSG_ERROR, "AP mode support not included in the "
961 #endif /* CONFIG_AP */
965 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
966 sme_authenticate(wpa_s, bss, ssid);
970 wpa_s->reassociate = 0;
972 #ifdef CONFIG_IEEE80211R
974 #endif /* CONFIG_IEEE80211R */
975 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
976 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
977 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
978 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
979 os_memset(wpa_s->bssid, 0, ETH_ALEN);
980 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
981 #ifdef CONFIG_IEEE80211R
982 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
983 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
985 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
987 /* Prepare for the next transition */
988 wpa_ft_prepare_auth_request(wpa_s->wpa);
990 #endif /* CONFIG_IEEE80211R */
992 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
993 wpa_s->conf->ap_scan == 2 &&
994 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
995 /* Use ap_scan==1 style network selection to find the network
998 wpa_s->reassociate = 1;
999 wpa_supplicant_req_scan(wpa_s, 0, 0);
1001 #endif /* CONFIG_WPS */
1003 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1004 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1005 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1007 wpa_supplicant_cancel_scan(wpa_s);
1009 /* Starting new association, so clear the possibly used WPA IE from the
1010 * previous association. */
1011 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1013 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
1014 wpa_printf(MSG_WARNING, "Failed to set operating mode");
1018 #ifdef IEEE8021X_EAPOL
1019 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1021 if (ssid->non_leap == 0)
1022 algs = AUTH_ALG_LEAP;
1024 algs |= AUTH_ALG_LEAP;
1027 #endif /* IEEE8021X_EAPOL */
1028 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1029 if (ssid->auth_alg) {
1031 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1032 algs |= AUTH_ALG_OPEN_SYSTEM;
1033 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1034 algs |= AUTH_ALG_SHARED_KEY;
1035 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1036 algs |= AUTH_ALG_LEAP;
1037 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1040 wpa_drv_set_auth_alg(wpa_s, algs);
1042 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1043 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1044 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1045 WPA_KEY_MGMT_FT_IEEE8021X |
1046 WPA_KEY_MGMT_FT_PSK |
1047 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1048 WPA_KEY_MGMT_PSK_SHA256))) {
1049 int try_opportunistic;
1050 try_opportunistic = ssid->proactive_key_caching &&
1051 (ssid->proto & WPA_PROTO_RSN);
1052 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1053 wpa_s->current_ssid,
1054 try_opportunistic) == 0)
1055 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1056 wpa_ie_len = sizeof(wpa_ie);
1057 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1058 wpa_ie, &wpa_ie_len)) {
1059 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1060 "management and encryption suites");
1063 } else if (ssid->key_mgmt &
1064 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1065 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1066 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1067 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1068 wpa_ie_len = sizeof(wpa_ie);
1069 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1070 wpa_ie, &wpa_ie_len)) {
1071 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1072 "management and encryption suites (no scan "
1077 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1078 struct wpabuf *wps_ie;
1079 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1080 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1081 wpa_ie_len = wpabuf_len(wps_ie);
1082 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1085 wpabuf_free(wps_ie);
1086 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1087 #endif /* CONFIG_WPS */
1089 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1093 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1095 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1096 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1097 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1098 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1099 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1101 if (wpa_set_wep_keys(wpa_s, ssid)) {
1106 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1109 #ifdef IEEE8021X_EAPOL
1110 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1111 if ((ssid->eapol_flags &
1112 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1113 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1117 /* Assume that dynamic WEP-104 keys will be used and
1118 * set cipher suites in order for drivers to expect
1120 cipher_pairwise = cipher_group = CIPHER_WEP104;
1123 #endif /* IEEE8021X_EAPOL */
1125 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1126 /* Set the key before (and later after) association */
1127 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1130 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1131 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1132 os_memset(¶ms, 0, sizeof(params));
1134 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1135 params.bssid = bss->bssid;
1136 params.ssid = ie ? ie + 2 : (u8 *) "";
1137 params.ssid_len = ie ? ie[1] : 0;
1138 params.freq = bss->freq;
1140 params.ssid = ssid->ssid;
1141 params.ssid_len = ssid->ssid_len;
1143 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1144 params.freq = ssid->frequency; /* Initial channel for IBSS */
1145 params.wpa_ie = wpa_ie;
1146 params.wpa_ie_len = wpa_ie_len;
1147 params.pairwise_suite = cipher_pairwise;
1148 params.group_suite = cipher_group;
1149 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1150 params.auth_alg = algs;
1151 params.mode = ssid->mode;
1152 for (i = 0; i < NUM_WEP_KEYS; i++) {
1153 if (ssid->wep_key_len[i])
1154 params.wep_key[i] = ssid->wep_key[i];
1155 params.wep_key_len[i] = ssid->wep_key_len[i];
1157 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1159 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1160 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1161 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1162 params.passphrase = ssid->passphrase;
1164 params.psk = ssid->psk;
1167 params.drop_unencrypted = use_crypt;
1169 #ifdef CONFIG_IEEE80211W
1170 switch (ssid->ieee80211w) {
1172 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1174 case IEEE80211W_OPTIONAL:
1175 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1177 case IEEE80211W_REQUIRED:
1178 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1181 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1182 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1183 struct wpa_ie_data ie;
1184 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1186 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1187 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1189 params.mgmt_frame_protection =
1190 MGMT_FRAME_PROTECTION_REQUIRED;
1193 #endif /* CONFIG_IEEE80211W */
1195 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1196 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1198 ret = wpa_drv_associate(wpa_s, ¶ms);
1200 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1202 /* try to continue anyway; new association will be tried again
1207 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1208 /* Set the key after the association just in case association
1209 * cleared the previously configured key. */
1210 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1211 /* No need to timeout authentication since there is no key
1213 wpa_supplicant_cancel_auth_timeout(wpa_s);
1214 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1215 #ifdef CONFIG_IBSS_RSN
1216 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1217 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1218 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1219 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1221 * RSN IBSS authentication is per-STA and we can disable the
1222 * per-BSSID authentication.
1224 wpa_supplicant_cancel_auth_timeout(wpa_s);
1225 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1226 #endif /* CONFIG_IBSS_RSN */
1228 /* Timeout for IEEE 802.11 authentication and association */
1232 /* give IBSS a bit more time */
1233 timeout = ssid->mode ? 10 : 5;
1234 } else if (wpa_s->conf->ap_scan == 1) {
1235 /* give IBSS a bit more time */
1236 timeout = ssid->mode ? 20 : 10;
1238 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1241 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1242 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1243 /* Set static WEP keys again */
1244 wpa_set_wep_keys(wpa_s, ssid);
1247 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1249 * Do not allow EAP session resumption between different
1250 * network configurations.
1252 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1254 wpa_s->current_ssid = ssid;
1255 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1256 wpa_supplicant_initiate_eapol(wpa_s);
1261 * wpa_supplicant_disassociate - Disassociate the current connection
1262 * @wpa_s: Pointer to wpa_supplicant data
1263 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1265 * This function is used to request %wpa_supplicant to disassociate with the
1268 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1272 if (!is_zero_ether_addr(wpa_s->bssid)) {
1273 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1274 ieee80211_sta_disassociate(wpa_s, reason_code);
1276 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1277 addr = wpa_s->bssid;
1279 wpa_clear_keys(wpa_s, addr);
1280 wpa_supplicant_mark_disassoc(wpa_s);
1281 wpa_s->current_ssid = NULL;
1282 wpa_sm_set_config(wpa_s->wpa, NULL);
1283 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1288 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1289 * @wpa_s: Pointer to wpa_supplicant data
1290 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1292 * This function is used to request %wpa_supplicant to deauthenticate from the
1295 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1299 if (!is_zero_ether_addr(wpa_s->bssid)) {
1300 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1301 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1303 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1305 addr = wpa_s->bssid;
1307 wpa_clear_keys(wpa_s, addr);
1308 wpa_supplicant_mark_disassoc(wpa_s);
1309 wpa_s->current_ssid = NULL;
1310 wpa_sm_set_config(wpa_s->wpa, NULL);
1311 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1315 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1317 #define SCAN_AP_LIMIT 128
1318 struct wpa_scan_result *results;
1320 struct wpa_scan_results *res;
1322 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1323 if (results == NULL) {
1324 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1329 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1330 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1332 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1336 if (num > SCAN_AP_LIMIT) {
1337 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1338 num, SCAN_AP_LIMIT);
1339 num = SCAN_AP_LIMIT;
1342 wpa_scan_results_free(wpa_s->scan_res);
1343 wpa_s->scan_res = NULL;
1345 /* Convert old scan result data structure to the new one */
1346 res = os_zalloc(sizeof(*res));
1351 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1352 if (res->res == NULL) {
1358 for (i = 0; i < num; i++) {
1359 struct wpa_scan_result *bss = &results[i];
1360 struct wpa_scan_res *r;
1364 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1367 if (bss->mdie_present)
1370 r = os_zalloc(sizeof(*r) + ie_len);
1374 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1375 r->freq = bss->freq;
1376 r->caps = bss->caps;
1377 r->qual = bss->qual;
1378 r->noise = bss->noise;
1379 r->level = bss->level;
1383 pos = (u8 *) (r + 1);
1386 *pos++ = WLAN_EID_SSID;
1387 *pos++ = bss->ssid_len;
1388 os_memcpy(pos, bss->ssid, bss->ssid_len);
1389 pos += bss->ssid_len;
1392 /* Fake Supported Rate IE to include max rate */
1393 *pos++ = WLAN_EID_SUPP_RATES;
1395 *pos++ = bss->maxrate;
1398 if (bss->rsn_ie_len) {
1399 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1400 pos += bss->rsn_ie_len;
1403 if (bss->mdie_present) {
1404 os_memcpy(pos, bss->mdie, 5);
1408 if (bss->wpa_ie_len) {
1409 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1410 pos += bss->wpa_ie_len;
1413 res->res[res->num++] = r;
1417 wpa_s->scan_res = res;
1424 * wpa_supplicant_get_scan_results - Get scan results
1425 * @wpa_s: Pointer to wpa_supplicant data
1426 * Returns: 0 on success, -1 on failure
1428 * This function is request the current scan results from the driver and stores
1429 * a local copy of the results in wpa_s->scan_res.
1431 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1435 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1436 wpa_scan_results_free(wpa_s->scan_res);
1437 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1438 if (wpa_s->scan_res == NULL) {
1439 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1443 } else if (wpa_s->driver->get_scan_results2 == NULL)
1444 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1446 wpa_scan_results_free(wpa_s->scan_res);
1447 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1448 if (wpa_s->scan_res == NULL) {
1449 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1455 if (wpa_s->scan_res)
1456 wpa_scan_sort_results(wpa_s->scan_res);
1463 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1464 * @wpa_s: Pointer to wpa_supplicant data
1465 * Returns: A pointer to the current network structure or %NULL on failure
1467 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1469 struct wpa_ssid *entry;
1470 u8 ssid[MAX_SSID_LEN];
1476 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1477 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1478 wpa_printf(MSG_WARNING, "Could not read SSID from "
1483 res = wpa_drv_get_ssid(wpa_s, ssid);
1485 wpa_printf(MSG_WARNING, "Could not read SSID from "
1492 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1493 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1494 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1495 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1499 wired = wpa_s->conf->ap_scan == 0 &&
1500 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1502 entry = wpa_s->conf->ssid;
1504 if (!entry->disabled &&
1505 ((ssid_len == entry->ssid_len &&
1506 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1507 (!entry->bssid_set ||
1508 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1511 if (!entry->disabled &&
1512 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1513 (entry->ssid == NULL || entry->ssid_len == 0) &&
1514 (!entry->bssid_set ||
1515 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1517 #endif /* CONFIG_WPS */
1518 entry = entry->next;
1525 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1535 if (wpa_drivers[0] == NULL) {
1536 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1542 /* default to first driver in the list */
1543 wpa_s->driver = wpa_drivers[0];
1547 pos = os_strchr(name, ',');
1551 len = os_strlen(name);
1552 for (i = 0; wpa_drivers[i]; i++) {
1553 if (os_strlen(wpa_drivers[i]->name) == len &&
1554 os_strncmp(name, wpa_drivers[i]->name, len) ==
1556 wpa_s->driver = wpa_drivers[i];
1561 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1566 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1567 const u8 *buf, size_t len)
1569 struct wpa_supplicant *wpa_s = ctx;
1571 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1572 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1575 if (wpa_s->ap_iface) {
1576 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1579 #endif /* CONFIG_AP */
1581 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1582 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1583 "no key management is configured");
1587 if (wpa_s->eapol_received == 0 &&
1588 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1589 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1590 wpa_s->wpa_state != WPA_COMPLETED)) {
1591 /* Timeout for completing IEEE 802.1X and WPA authentication */
1592 wpa_supplicant_req_auth_timeout(
1594 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1595 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1596 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1599 wpa_s->eapol_received++;
1601 if (wpa_s->countermeasures) {
1602 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1607 #ifdef CONFIG_IBSS_RSN
1608 if (wpa_s->current_ssid &&
1609 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1610 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1613 #endif /* CONFIG_IBSS_RSN */
1615 /* Source address of the incoming EAPOL frame could be compared to the
1616 * current BSSID. However, it is possible that a centralized
1617 * Authenticator could be using another MAC address than the BSSID of
1618 * an AP, so just allow any address to be used for now. The replies are
1619 * still sent to the current BSSID (if available), though. */
1621 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1622 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1623 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1625 wpa_drv_poll(wpa_s);
1626 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1627 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1628 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1630 * Set portValid = TRUE here since we are going to skip 4-way
1631 * handshake processing which would normally set portValid. We
1632 * need this to allow the EAPOL state machines to be completed
1633 * without going through EAPOL-Key handshake.
1635 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1640 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1641 struct ieee80211_rx_status *rx_status)
1643 struct wpa_supplicant *wpa_s = ctx;
1644 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1649 * wpa_supplicant_driver_init - Initialize driver interface parameters
1650 * @wpa_s: Pointer to wpa_supplicant data
1651 * Returns: 0 on success, -1 on failure
1653 * This function is called to initialize driver interface parameters.
1654 * wpa_drv_init() must have been called before this function to initialize the
1657 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1659 static int interface_count = 0;
1661 if (wpa_s->driver->send_eapol) {
1662 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1664 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1666 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1667 wpa_drv_get_mac_addr(wpa_s),
1669 wpa_supplicant_rx_eapol, wpa_s, 0);
1670 if (wpa_s->l2 == NULL)
1674 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1675 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1679 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1680 MAC2STR(wpa_s->own_addr));
1682 if (wpa_s->bridge_ifname[0]) {
1683 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1684 " '%s'", wpa_s->bridge_ifname);
1685 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1688 wpa_supplicant_rx_eapol, wpa_s,
1690 if (wpa_s->l2_br == NULL) {
1691 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1692 "connection for the bridge interface '%s'",
1693 wpa_s->bridge_ifname);
1698 /* Backwards compatibility call to set_wpa() handler. This is called
1699 * only just after init and just before deinit, so these handler can be
1700 * used to implement same functionality. */
1701 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1702 struct wpa_driver_capa capa;
1703 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1704 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1705 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1706 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1707 /* Continue to allow non-WPA modes to be used. */
1709 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1715 wpa_clear_keys(wpa_s, NULL);
1717 /* Make sure that TKIP countermeasures are not left enabled (could
1718 * happen if wpa_supplicant is killed during countermeasures. */
1719 wpa_drv_set_countermeasures(wpa_s, 0);
1721 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1723 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1724 wpa_drv_flush_pmkid(wpa_s);
1726 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1727 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1734 static int wpa_supplicant_daemon(const char *pid_file)
1736 wpa_printf(MSG_DEBUG, "Daemonize..");
1737 return os_daemonize(pid_file);
1741 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1743 struct wpa_supplicant *wpa_s;
1745 wpa_s = os_zalloc(sizeof(*wpa_s));
1748 wpa_s->scan_req = 1;
1754 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1755 struct wpa_interface *iface)
1757 const char *ifname, *driver;
1758 struct wpa_driver_capa capa;
1760 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1761 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1762 iface->confname ? iface->confname : "N/A",
1763 iface->driver ? iface->driver : "default",
1764 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1765 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1767 if (iface->confname) {
1768 #ifdef CONFIG_BACKEND_FILE
1769 wpa_s->confname = os_rel2abs_path(iface->confname);
1770 if (wpa_s->confname == NULL) {
1771 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1772 "for configuration file '%s'.",
1776 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1777 iface->confname, wpa_s->confname);
1778 #else /* CONFIG_BACKEND_FILE */
1779 wpa_s->confname = os_strdup(iface->confname);
1780 #endif /* CONFIG_BACKEND_FILE */
1781 wpa_s->conf = wpa_config_read(wpa_s->confname);
1782 if (wpa_s->conf == NULL) {
1783 wpa_printf(MSG_ERROR, "Failed to read or parse "
1784 "configuration '%s'.", wpa_s->confname);
1789 * Override ctrl_interface and driver_param if set on command
1792 if (iface->ctrl_interface) {
1793 os_free(wpa_s->conf->ctrl_interface);
1794 wpa_s->conf->ctrl_interface =
1795 os_strdup(iface->ctrl_interface);
1798 if (iface->driver_param) {
1799 os_free(wpa_s->conf->driver_param);
1800 wpa_s->conf->driver_param =
1801 os_strdup(iface->driver_param);
1804 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1805 iface->driver_param);
1807 if (wpa_s->conf == NULL) {
1808 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1812 if (iface->ifname == NULL) {
1813 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1816 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1817 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1821 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1823 if (iface->bridge_ifname) {
1824 if (os_strlen(iface->bridge_ifname) >=
1825 sizeof(wpa_s->bridge_ifname)) {
1826 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1827 "name '%s'.", iface->bridge_ifname);
1830 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1831 sizeof(wpa_s->bridge_ifname));
1834 /* RSNA Supplicant Key Management - INITIALIZE */
1835 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1836 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1838 /* Initialize driver interface and register driver event handler before
1839 * L2 receive handler so that association events are processed before
1840 * EAPOL-Key packets if both become available for the same select()
1842 driver = iface->driver;
1844 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
1847 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1848 if (wpa_s->drv_priv == NULL) {
1850 pos = os_strchr(driver, ',');
1852 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
1853 "interface - try next driver wrapper");
1857 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1860 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1861 wpa_printf(MSG_ERROR, "Driver interface rejected "
1862 "driver_param '%s'", wpa_s->conf->driver_param);
1866 ifname = wpa_drv_get_ifname(wpa_s);
1867 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1868 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1869 "name with '%s'", ifname);
1870 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1873 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1876 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1877 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1879 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1881 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1882 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1883 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1884 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1885 "dot11RSNAConfigPMKLifetime");
1889 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1890 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1891 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1892 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1893 "dot11RSNAConfigPMKReauthThreshold");
1897 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1898 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1899 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1900 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1901 "dot11RSNAConfigSATimeout");
1905 if (wpa_supplicant_driver_init(wpa_s) < 0)
1908 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
1909 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
1910 wpa_printf(MSG_DEBUG, "Failed to set country");
1914 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1916 if (wpas_wps_init(wpa_s))
1919 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1921 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1923 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1924 if (wpa_s->ctrl_iface == NULL) {
1925 wpa_printf(MSG_ERROR,
1926 "Failed to initialize control interface '%s'.\n"
1927 "You may have another wpa_supplicant process "
1928 "already running or the file was\n"
1929 "left by an unclean termination of wpa_supplicant "
1930 "in which case you will need\n"
1931 "to manually remove this file before starting "
1932 "wpa_supplicant again.\n",
1933 wpa_s->conf->ctrl_interface);
1937 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1938 wpa_s->drv_flags = capa.flags;
1939 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1940 if (ieee80211_sta_init(wpa_s))
1943 wpa_s->max_scan_ssids = capa.max_scan_ssids;
1946 #ifdef CONFIG_IBSS_RSN
1947 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1948 if (!wpa_s->ibss_rsn) {
1949 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
1952 #endif /* CONFIG_IBSS_RSN */
1958 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1960 if (wpa_s->drv_priv) {
1961 wpa_supplicant_deauthenticate(wpa_s,
1962 WLAN_REASON_DEAUTH_LEAVING);
1964 /* Backwards compatibility call to set_wpa() handler. This is
1965 * called only just after init and just before deinit, so these
1966 * handler can be used to implement same functionality. */
1967 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1968 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
1972 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1973 wpa_drv_set_countermeasures(wpa_s, 0);
1974 wpa_clear_keys(wpa_s, NULL);
1977 wpas_dbus_unregister_iface(wpa_s);
1979 wpa_supplicant_cleanup(wpa_s);
1981 if (wpa_s->drv_priv)
1982 wpa_drv_deinit(wpa_s);
1987 * wpa_supplicant_add_iface - Add a new network interface
1988 * @global: Pointer to global data from wpa_supplicant_init()
1989 * @iface: Interface configuration options
1990 * Returns: Pointer to the created interface or %NULL on failure
1992 * This function is used to add new network interfaces for %wpa_supplicant.
1993 * This can be called before wpa_supplicant_run() to add interfaces before the
1994 * main event loop has been started. In addition, new interfaces can be added
1995 * dynamically while %wpa_supplicant is already running. This could happen,
1996 * e.g., when a hotplug network adapter is inserted.
1998 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
1999 struct wpa_interface *iface)
2001 struct wpa_supplicant *wpa_s;
2003 if (global == NULL || iface == NULL)
2006 wpa_s = wpa_supplicant_alloc();
2010 if (wpa_supplicant_init_iface(wpa_s, iface)) {
2011 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2013 wpa_supplicant_deinit_iface(wpa_s);
2018 wpa_s->global = global;
2020 /* Register the interface with the dbus control interface */
2021 if (wpas_dbus_register_iface(wpa_s)) {
2022 wpa_supplicant_deinit_iface(wpa_s);
2027 wpa_s->next = global->ifaces;
2028 global->ifaces = wpa_s;
2030 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2037 * wpa_supplicant_remove_iface - Remove a network interface
2038 * @global: Pointer to global data from wpa_supplicant_init()
2039 * @wpa_s: Pointer to the network interface to be removed
2040 * Returns: 0 if interface was removed, -1 if interface was not found
2042 * This function can be used to dynamically remove network interfaces from
2043 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2044 * addition, this function is used to remove all remaining interfaces when
2045 * %wpa_supplicant is terminated.
2047 int wpa_supplicant_remove_iface(struct wpa_global *global,
2048 struct wpa_supplicant *wpa_s)
2050 struct wpa_supplicant *prev;
2052 /* Remove interface from the global list of interfaces */
2053 prev = global->ifaces;
2054 if (prev == wpa_s) {
2055 global->ifaces = wpa_s->next;
2057 while (prev && prev->next != wpa_s)
2061 prev->next = wpa_s->next;
2064 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2066 wpa_supplicant_deinit_iface(wpa_s);
2074 * wpa_supplicant_get_iface - Get a new network interface
2075 * @global: Pointer to global data from wpa_supplicant_init()
2076 * @ifname: Interface name
2077 * Returns: Pointer to the interface or %NULL if not found
2079 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2082 struct wpa_supplicant *wpa_s;
2084 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2085 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2093 * wpa_supplicant_init - Initialize %wpa_supplicant
2094 * @params: Parameters for %wpa_supplicant
2095 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2097 * This function is used to initialize %wpa_supplicant. After successful
2098 * initialization, the returned data pointer can be used to add and remove
2099 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2101 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2103 struct wpa_global *global;
2109 wpa_debug_open_file(params->wpa_debug_file_path);
2110 if (params->wpa_debug_syslog)
2111 wpa_debug_open_syslog();
2113 ret = eap_peer_register_methods();
2115 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2117 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2118 "the same EAP type.");
2123 ret = eap_server_register_methods();
2125 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2127 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2128 "the same EAP type.");
2131 #endif /* CONFIG_AP */
2133 global = os_zalloc(sizeof(*global));
2136 global->params.daemonize = params->daemonize;
2137 global->params.wait_for_monitor = params->wait_for_monitor;
2138 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2139 if (params->pid_file)
2140 global->params.pid_file = os_strdup(params->pid_file);
2141 if (params->ctrl_interface)
2142 global->params.ctrl_interface =
2143 os_strdup(params->ctrl_interface);
2144 wpa_debug_level = global->params.wpa_debug_level =
2145 params->wpa_debug_level;
2146 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2147 params->wpa_debug_show_keys;
2148 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2149 params->wpa_debug_timestamp;
2151 if (eloop_init(global)) {
2152 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2153 wpa_supplicant_deinit(global);
2157 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2158 if (global->ctrl_iface == NULL) {
2159 wpa_supplicant_deinit(global);
2163 if (global->params.dbus_ctrl_interface) {
2164 global->dbus_ctrl_iface =
2165 wpa_supplicant_dbus_ctrl_iface_init(global);
2166 if (global->dbus_ctrl_iface == NULL) {
2167 wpa_supplicant_deinit(global);
2172 for (i = 0; wpa_drivers[i]; i++)
2173 global->drv_count++;
2174 if (global->drv_count == 0) {
2175 wpa_printf(MSG_ERROR, "No drivers enabled");
2176 wpa_supplicant_deinit(global);
2179 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2180 if (global->drv_priv == NULL) {
2181 wpa_supplicant_deinit(global);
2184 for (i = 0; wpa_drivers[i]; i++) {
2185 if (!wpa_drivers[i]->global_init)
2187 global->drv_priv[i] = wpa_drivers[i]->global_init();
2188 if (global->drv_priv[i] == NULL) {
2189 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2190 "'%s'", wpa_drivers[i]->name);
2191 wpa_supplicant_deinit(global);
2201 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2202 * @global: Pointer to global data from wpa_supplicant_init()
2203 * Returns: 0 after successful event loop run, -1 on failure
2205 * This function starts the main event loop and continues running as long as
2206 * there are any remaining events. In most cases, this function is running as
2207 * long as the %wpa_supplicant process in still in use.
2209 int wpa_supplicant_run(struct wpa_global *global)
2211 struct wpa_supplicant *wpa_s;
2213 if (global->params.daemonize &&
2214 wpa_supplicant_daemon(global->params.pid_file))
2217 if (global->params.wait_for_monitor) {
2218 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2219 if (wpa_s->ctrl_iface)
2220 wpa_supplicant_ctrl_iface_wait(
2224 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2225 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2234 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2235 * @global: Pointer to global data from wpa_supplicant_init()
2237 * This function is called to deinitialize %wpa_supplicant and to free all
2238 * allocated resources. Remaining network interfaces will also be removed.
2240 void wpa_supplicant_deinit(struct wpa_global *global)
2247 while (global->ifaces)
2248 wpa_supplicant_remove_iface(global, global->ifaces);
2250 if (global->ctrl_iface)
2251 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2252 if (global->dbus_ctrl_iface)
2253 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2255 eap_peer_unregister_methods();
2257 eap_server_unregister_methods();
2258 #endif /* CONFIG_AP */
2260 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2261 if (!global->drv_priv[i])
2263 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2265 os_free(global->drv_priv);
2269 if (global->params.pid_file) {
2270 os_daemonize_terminate(global->params.pid_file);
2271 os_free(global->params.pid_file);
2273 os_free(global->params.ctrl_interface);
2276 wpa_debug_close_syslog();
2277 wpa_debug_close_file();