3 * Copyright (c) 2003-2010, 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"
25 #include "rsn_supp/wpa.h"
28 #include "l2_packet/l2_packet.h"
29 #include "wpa_supplicant_i.h"
31 #include "ctrl_iface.h"
32 #include "pcsc_funcs.h"
33 #include "common/version.h"
34 #include "rsn_supp/preauth.h"
35 #include "rsn_supp/pmksa_cache.h"
36 #include "common/wpa_ctrl.h"
38 #include "common/ieee802_11_defs.h"
39 #include "blacklist.h"
40 #include "wpas_glue.h"
41 #include "wps_supplicant.h"
45 #include "p2p_supplicant.h"
51 const char *wpa_supplicant_version =
52 "wpa_supplicant v" VERSION_STR "\n"
53 "Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> and contributors";
55 const char *wpa_supplicant_license =
56 "This program is free software. You can distribute it and/or modify it\n"
57 "under the terms of the GNU General Public License version 2.\n"
59 "Alternatively, this software may be distributed under the terms of the\n"
60 "BSD license. See README and COPYING for more details.\n"
61 #ifdef EAP_TLS_OPENSSL
62 "\nThis product includes software developed by the OpenSSL Project\n"
63 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
64 #endif /* EAP_TLS_OPENSSL */
67 #ifndef CONFIG_NO_STDOUT_DEBUG
68 /* Long text divided into parts in order to fit in C89 strings size limits. */
69 const char *wpa_supplicant_full_license1 =
70 "This program is free software; you can redistribute it and/or modify\n"
71 "it under the terms of the GNU General Public License version 2 as\n"
72 "published by the Free Software Foundation.\n"
74 "This program is distributed in the hope that it will be useful,\n"
75 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
76 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
77 "GNU General Public License for more details.\n"
79 const char *wpa_supplicant_full_license2 =
80 "You should have received a copy of the GNU General Public License\n"
81 "along with this program; if not, write to the Free Software\n"
82 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
84 "Alternatively, this software may be distributed under the terms of the\n"
87 "Redistribution and use in source and binary forms, with or without\n"
88 "modification, are permitted provided that the following conditions are\n"
91 const char *wpa_supplicant_full_license3 =
92 "1. Redistributions of source code must retain the above copyright\n"
93 " notice, this list of conditions and the following disclaimer.\n"
95 "2. Redistributions in binary form must reproduce the above copyright\n"
96 " notice, this list of conditions and the following disclaimer in the\n"
97 " documentation and/or other materials provided with the distribution.\n"
99 const char *wpa_supplicant_full_license4 =
100 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
101 " names of its contributors may be used to endorse or promote products\n"
102 " derived from this software without specific prior written permission.\n"
104 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
105 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
106 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
107 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
108 const char *wpa_supplicant_full_license5 =
109 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
110 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
111 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
112 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
113 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
114 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
115 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
117 #endif /* CONFIG_NO_STDOUT_DEBUG */
119 extern int wpa_debug_level;
120 extern int wpa_debug_show_keys;
121 extern int wpa_debug_timestamp;
122 extern struct wpa_driver_ops *wpa_drivers[];
124 /* Configure default/group WEP keys for static WEP */
125 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
129 for (i = 0; i < NUM_WEP_KEYS; i++) {
130 if (ssid->wep_key_len[i] == 0)
134 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
135 (u8 *) "\xff\xff\xff\xff\xff\xff",
136 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
137 ssid->wep_key[i], ssid->wep_key_len[i]);
144 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
145 struct wpa_ssid *ssid)
152 /* IBSS/WPA-None uses only one key (Group) for both receiving and
153 * sending unicast and multicast packets. */
155 if (ssid->mode != WPAS_MODE_IBSS) {
156 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
157 "for WPA-None", ssid->mode);
161 if (!ssid->psk_set) {
162 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
166 switch (wpa_s->group_cipher) {
167 case WPA_CIPHER_CCMP:
168 os_memcpy(key, ssid->psk, 16);
172 case WPA_CIPHER_TKIP:
173 /* WPA-None uses the same Michael MIC key for both TX and RX */
174 os_memcpy(key, ssid->psk, 16 + 8);
175 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
180 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
181 "WPA-None", wpa_s->group_cipher);
185 /* TODO: should actually remember the previously used seq#, both for TX
186 * and RX from each STA.. */
188 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
189 0, 1, seq, 6, key, keylen);
193 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
195 struct wpa_supplicant *wpa_s = eloop_ctx;
196 const u8 *bssid = wpa_s->bssid;
197 if (is_zero_ether_addr(bssid))
198 bssid = wpa_s->pending_bssid;
199 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
201 wpa_blacklist_add(wpa_s, bssid);
202 wpa_sm_notify_disassoc(wpa_s->wpa);
203 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
204 wpa_s->reassociate = 1;
205 wpa_supplicant_req_scan(wpa_s, 0, 0);
210 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
211 * @wpa_s: Pointer to wpa_supplicant data
212 * @sec: Number of seconds after which to time out authentication
213 * @usec: Number of microseconds after which to time out authentication
215 * This function is used to schedule a timeout for the current authentication
218 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
221 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
222 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
225 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
226 "%d usec", sec, usec);
227 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
228 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
233 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
234 * @wpa_s: Pointer to wpa_supplicant data
236 * This function is used to cancel authentication timeout scheduled with
237 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
240 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
242 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
243 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
244 wpa_blacklist_del(wpa_s, wpa_s->bssid);
249 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
250 * @wpa_s: Pointer to wpa_supplicant data
252 * This function is used to configure EAPOL state machine based on the selected
253 * authentication mode.
255 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
257 #ifdef IEEE8021X_EAPOL
258 struct eapol_config eapol_conf;
259 struct wpa_ssid *ssid = wpa_s->current_ssid;
261 #ifdef CONFIG_IBSS_RSN
262 if (ssid->mode == WPAS_MODE_IBSS &&
263 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
264 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
266 * RSN IBSS authentication is per-STA and we can disable the
267 * per-BSSID EAPOL authentication.
269 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
270 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
271 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
274 #endif /* CONFIG_IBSS_RSN */
276 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
277 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
279 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
280 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
281 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
283 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
285 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
286 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
287 eapol_conf.accept_802_1x_keys = 1;
288 eapol_conf.required_keys = 0;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
290 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
292 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
293 eapol_conf.required_keys |=
294 EAPOL_REQUIRE_KEY_BROADCAST;
297 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
298 eapol_conf.required_keys = 0;
301 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
302 eapol_conf.workaround = ssid->eap_workaround;
303 eapol_conf.eap_disabled =
304 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
305 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
306 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
307 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
308 #endif /* IEEE8021X_EAPOL */
313 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
314 * @wpa_s: Pointer to wpa_supplicant data
315 * @ssid: Configuration data for the network
317 * This function is used to configure WPA state machine and related parameters
318 * to a mode where WPA is not enabled. This is called as part of the
319 * authentication configuration when the selected network does not use WPA.
321 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
322 struct wpa_ssid *ssid)
326 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
327 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
328 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
329 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
331 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
332 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
333 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
334 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
335 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
336 wpa_s->group_cipher = WPA_CIPHER_NONE;
337 wpa_s->mgmt_group_cipher = 0;
339 for (i = 0; i < NUM_WEP_KEYS; i++) {
340 if (ssid->wep_key_len[i] > 5) {
341 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
342 wpa_s->group_cipher = WPA_CIPHER_WEP104;
344 } else if (ssid->wep_key_len[i] > 0) {
345 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
346 wpa_s->group_cipher = WPA_CIPHER_WEP40;
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
353 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
354 wpa_s->pairwise_cipher);
355 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
356 #ifdef CONFIG_IEEE80211W
357 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
358 wpa_s->mgmt_group_cipher);
359 #endif /* CONFIG_IEEE80211W */
361 pmksa_cache_clear_current(wpa_s->wpa);
365 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
367 bgscan_deinit(wpa_s);
368 scard_deinit(wpa_s->scard);
370 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
371 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
372 l2_packet_deinit(wpa_s->l2);
375 l2_packet_deinit(wpa_s->l2_br);
379 if (wpa_s->ctrl_iface) {
380 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
381 wpa_s->ctrl_iface = NULL;
383 if (wpa_s->conf != NULL) {
384 struct wpa_ssid *ssid;
385 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
386 wpas_notify_network_removed(wpa_s, ssid);
387 wpa_config_free(wpa_s->conf);
391 os_free(wpa_s->confname);
392 wpa_s->confname = NULL;
394 wpa_sm_set_eapol(wpa_s->wpa, NULL);
395 eapol_sm_deinit(wpa_s->eapol);
398 rsn_preauth_deinit(wpa_s->wpa);
400 pmksa_candidate_free(wpa_s->wpa);
401 wpa_sm_deinit(wpa_s->wpa);
403 wpa_blacklist_clear(wpa_s);
405 wpa_bss_deinit(wpa_s);
407 wpa_supplicant_cancel_scan(wpa_s);
408 wpa_supplicant_cancel_auth_timeout(wpa_s);
410 ieee80211_sta_deinit(wpa_s);
412 wpas_wps_deinit(wpa_s);
414 wpabuf_free(wpa_s->pending_eapol_rx);
415 wpa_s->pending_eapol_rx = NULL;
417 #ifdef CONFIG_IBSS_RSN
418 ibss_rsn_deinit(wpa_s->ibss_rsn);
419 wpa_s->ibss_rsn = NULL;
420 #endif /* CONFIG_IBSS_RSN */
423 os_free(wpa_s->sme.ft_ies);
424 wpa_s->sme.ft_ies = NULL;
425 wpa_s->sme.ft_ies_len = 0;
426 #endif /* CONFIG_SME */
429 wpa_supplicant_ap_deinit(wpa_s);
430 #endif /* CONFIG_AP */
433 wpas_p2p_deinit(wpa_s);
434 #endif /* CONFIG_P2P */
439 * wpa_clear_keys - Clear keys configured for the driver
440 * @wpa_s: Pointer to wpa_supplicant data
441 * @addr: Previously used BSSID or %NULL if not available
443 * This function clears the encryption keys that has been previously configured
446 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
448 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
450 if (wpa_s->keys_cleared) {
451 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
452 * timing issues with keys being cleared just before new keys
453 * are set or just after association or something similar. This
454 * shows up in group key handshake failing often because of the
455 * client not receiving the first encrypted packets correctly.
456 * Skipping some of the extra key clearing steps seems to help
457 * in completing group key handshake more reliably. */
458 wpa_printf(MSG_DEBUG, "No keys have been configured - "
459 "skip key clearing");
463 /* MLME-DELETEKEYS.request */
464 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
465 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
466 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
467 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
468 #ifdef CONFIG_IEEE80211W
469 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
470 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
471 #endif /* CONFIG_IEEE80211W */
473 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
475 /* MLME-SETPROTECTION.request(None) */
476 wpa_drv_mlme_setprotection(
478 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
479 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
481 wpa_s->keys_cleared = 1;
486 * wpa_supplicant_state_txt - Get the connection state name as a text string
487 * @state: State (wpa_state; WPA_*)
488 * Returns: The state name as a printable text string
490 const char * wpa_supplicant_state_txt(enum wpa_states state)
493 case WPA_DISCONNECTED:
494 return "DISCONNECTED";
497 case WPA_INTERFACE_DISABLED:
498 return "INTERFACE_DISABLED";
501 case WPA_AUTHENTICATING:
502 return "AUTHENTICATING";
503 case WPA_ASSOCIATING:
504 return "ASSOCIATING";
507 case WPA_4WAY_HANDSHAKE:
508 return "4WAY_HANDSHAKE";
509 case WPA_GROUP_HANDSHAKE:
510 return "GROUP_HANDSHAKE";
520 * wpa_supplicant_set_state - Set current connection state
521 * @wpa_s: Pointer to wpa_supplicant data
522 * @state: The new connection state
524 * This function is called whenever the connection state changes, e.g.,
525 * association is completed for WPA/WPA2 4-Way Handshake is started.
527 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
528 enum wpa_states state)
530 enum wpa_states old_state = wpa_s->wpa_state;
532 wpa_printf(MSG_DEBUG, "State: %s -> %s",
533 wpa_supplicant_state_txt(wpa_s->wpa_state),
534 wpa_supplicant_state_txt(state));
536 if (state != WPA_SCANNING)
537 wpa_supplicant_notify_scanning(wpa_s, 0);
539 if (state == WPA_COMPLETED && wpa_s->new_connection) {
540 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
541 struct wpa_ssid *ssid = wpa_s->current_ssid;
542 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
543 MACSTR " completed %s [id=%d id_str=%s]",
544 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
545 "(reauth)" : "(auth)",
546 ssid ? ssid->id : -1,
547 ssid && ssid->id_str ? ssid->id_str : "");
548 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
549 wpa_s->new_connection = 0;
550 wpa_s->reassociated_connection = 1;
551 wpa_drv_set_operstate(wpa_s, 1);
552 wpa_s->after_wps = 0;
554 wpas_p2p_completed(wpa_s);
555 #endif /* CONFIG_P2P */
556 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
557 state == WPA_ASSOCIATED) {
558 wpa_s->new_connection = 1;
559 wpa_drv_set_operstate(wpa_s, 0);
561 wpa_s->wpa_state = state;
563 if (wpa_s->wpa_state != old_state)
564 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
568 void wpa_supplicant_terminate_proc(struct wpa_global *global)
572 struct wpa_supplicant *wpa_s = global->ifaces;
574 if (wpas_wps_terminate_pending(wpa_s) == 1)
578 #endif /* CONFIG_WPS */
585 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
587 struct wpa_global *global = signal_ctx;
588 struct wpa_supplicant *wpa_s;
589 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
590 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
593 wpa_supplicant_terminate_proc(global);
597 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
599 enum wpa_states old_state = wpa_s->wpa_state;
601 wpa_s->pairwise_cipher = 0;
602 wpa_s->group_cipher = 0;
603 wpa_s->mgmt_group_cipher = 0;
605 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
606 wpa_s->wpa_state = WPA_DISCONNECTED;
608 if (wpa_s->wpa_state != old_state)
609 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
614 * wpa_supplicant_reload_configuration - Reload configuration data
615 * @wpa_s: Pointer to wpa_supplicant data
616 * Returns: 0 on success or -1 if configuration parsing failed
618 * This function can be used to request that the configuration data is reloaded
619 * (e.g., after configuration file change). This function is reloading
620 * configuration only for one interface, so this may need to be called multiple
621 * times if %wpa_supplicant is controlling multiple interfaces and all
622 * interfaces need reconfiguration.
624 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
626 struct wpa_config *conf;
627 struct wpa_ssid *old_ssid;
631 if (wpa_s->confname == NULL)
633 conf = wpa_config_read(wpa_s->confname);
635 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
636 "file '%s' - exiting", wpa_s->confname);
639 conf->changed_parameters = (unsigned int) -1;
641 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
642 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
643 os_strcmp(conf->ctrl_interface,
644 wpa_s->conf->ctrl_interface) != 0);
646 if (reconf_ctrl && wpa_s->ctrl_iface) {
647 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
648 wpa_s->ctrl_iface = NULL;
651 eapol_sm_invalidate_cached_session(wpa_s->eapol);
652 old_ssid = wpa_s->current_ssid;
653 wpa_s->current_ssid = NULL;
654 if (old_ssid != wpa_s->current_ssid)
655 wpas_notify_network_changed(wpa_s);
658 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
659 * pkcs11_engine_path, pkcs11_module_path.
661 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
663 * Clear forced success to clear EAP state for next
666 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
668 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
669 wpa_sm_set_config(wpa_s->wpa, NULL);
670 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
671 rsn_preauth_deinit(wpa_s->wpa);
673 old_ap_scan = wpa_s->conf->ap_scan;
674 wpa_config_free(wpa_s->conf);
676 if (old_ap_scan != wpa_s->conf->ap_scan)
677 wpas_notify_ap_scan_changed(wpa_s);
680 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
682 wpa_supplicant_update_config(wpa_s);
684 wpa_supplicant_clear_status(wpa_s);
685 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
686 wpa_s->reassociate = 1;
687 wpa_supplicant_req_scan(wpa_s, 0, 0);
689 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
694 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
696 struct wpa_global *global = signal_ctx;
697 struct wpa_supplicant *wpa_s;
698 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
699 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
700 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
701 wpa_supplicant_terminate_proc(global);
707 enum wpa_cipher cipher_suite2driver(int cipher)
710 case WPA_CIPHER_NONE:
712 case WPA_CIPHER_WEP40:
714 case WPA_CIPHER_WEP104:
715 return CIPHER_WEP104;
716 case WPA_CIPHER_CCMP:
718 case WPA_CIPHER_TKIP:
725 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
728 case WPA_KEY_MGMT_NONE:
729 return KEY_MGMT_NONE;
730 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
731 return KEY_MGMT_802_1X_NO_WPA;
732 case WPA_KEY_MGMT_IEEE8021X:
733 return KEY_MGMT_802_1X;
734 case WPA_KEY_MGMT_WPA_NONE:
735 return KEY_MGMT_WPA_NONE;
736 case WPA_KEY_MGMT_FT_IEEE8021X:
737 return KEY_MGMT_FT_802_1X;
738 case WPA_KEY_MGMT_FT_PSK:
739 return KEY_MGMT_FT_PSK;
740 case WPA_KEY_MGMT_IEEE8021X_SHA256:
741 return KEY_MGMT_802_1X_SHA256;
742 case WPA_KEY_MGMT_PSK_SHA256:
743 return KEY_MGMT_PSK_SHA256;
744 case WPA_KEY_MGMT_WPS:
746 case WPA_KEY_MGMT_PSK:
753 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
754 struct wpa_ssid *ssid,
755 struct wpa_ie_data *ie)
757 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
760 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
761 "from association info");
766 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
768 if (!(ie->group_cipher & ssid->group_cipher)) {
769 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
770 "cipher 0x%x (mask 0x%x) - reject",
771 ie->group_cipher, ssid->group_cipher);
774 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
775 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
776 "cipher 0x%x (mask 0x%x) - reject",
777 ie->pairwise_cipher, ssid->pairwise_cipher);
780 if (!(ie->key_mgmt & ssid->key_mgmt)) {
781 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
782 "management 0x%x (mask 0x%x) - reject",
783 ie->key_mgmt, ssid->key_mgmt);
787 #ifdef CONFIG_IEEE80211W
788 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
789 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
790 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
791 "that does not support management frame protection - "
795 #endif /* CONFIG_IEEE80211W */
802 * wpa_supplicant_set_suites - Set authentication and encryption parameters
803 * @wpa_s: Pointer to wpa_supplicant data
804 * @bss: Scan results for the selected BSS, or %NULL if not available
805 * @ssid: Configuration data for the selected network
806 * @wpa_ie: Buffer for the WPA/RSN IE
807 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
808 * used buffer length in case the functions returns success.
809 * Returns: 0 on success or -1 on failure
811 * This function is used to configure authentication and encryption parameters
812 * based on the network configuration and scan result for the selected BSS (if
815 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
816 struct wpa_bss *bss, struct wpa_ssid *ssid,
817 u8 *wpa_ie, size_t *wpa_ie_len)
819 struct wpa_ie_data ie;
821 const u8 *bss_wpa, *bss_rsn;
824 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
825 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
827 bss_wpa = bss_rsn = NULL;
829 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
830 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
831 (ie.group_cipher & ssid->group_cipher) &&
832 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
833 (ie.key_mgmt & ssid->key_mgmt)) {
834 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
835 proto = WPA_PROTO_RSN;
836 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
837 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
838 (ie.group_cipher & ssid->group_cipher) &&
839 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
840 (ie.key_mgmt & ssid->key_mgmt)) {
841 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
842 proto = WPA_PROTO_WPA;
844 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
847 if (ssid->proto & WPA_PROTO_RSN)
848 proto = WPA_PROTO_RSN;
850 proto = WPA_PROTO_WPA;
851 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
852 os_memset(&ie, 0, sizeof(ie));
853 ie.group_cipher = ssid->group_cipher;
854 ie.pairwise_cipher = ssid->pairwise_cipher;
855 ie.key_mgmt = ssid->key_mgmt;
856 #ifdef CONFIG_IEEE80211W
857 ie.mgmt_group_cipher =
858 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
859 WPA_CIPHER_AES_128_CMAC : 0;
860 #endif /* CONFIG_IEEE80211W */
861 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
867 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
868 "pairwise %d key_mgmt %d proto %d",
869 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
870 #ifdef CONFIG_IEEE80211W
871 if (ssid->ieee80211w) {
872 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
873 ie.mgmt_group_cipher);
875 #endif /* CONFIG_IEEE80211W */
877 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
878 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
879 !!(ssid->proto & WPA_PROTO_RSN));
881 if (bss || !wpa_s->ap_ies_from_associnfo) {
882 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
883 bss_wpa ? 2 + bss_wpa[1] : 0) ||
884 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
885 bss_rsn ? 2 + bss_rsn[1] : 0))
889 sel = ie.group_cipher & ssid->group_cipher;
890 if (sel & WPA_CIPHER_CCMP) {
891 wpa_s->group_cipher = WPA_CIPHER_CCMP;
892 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
893 } else if (sel & WPA_CIPHER_TKIP) {
894 wpa_s->group_cipher = WPA_CIPHER_TKIP;
895 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
896 } else if (sel & WPA_CIPHER_WEP104) {
897 wpa_s->group_cipher = WPA_CIPHER_WEP104;
898 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
899 } else if (sel & WPA_CIPHER_WEP40) {
900 wpa_s->group_cipher = WPA_CIPHER_WEP40;
901 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
903 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
907 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
908 if (sel & WPA_CIPHER_CCMP) {
909 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
910 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
911 } else if (sel & WPA_CIPHER_TKIP) {
912 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
913 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
914 } else if (sel & WPA_CIPHER_NONE) {
915 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
916 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
918 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
923 sel = ie.key_mgmt & ssid->key_mgmt;
925 #ifdef CONFIG_IEEE80211R
926 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
927 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
928 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
929 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
930 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
931 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
932 #endif /* CONFIG_IEEE80211R */
933 #ifdef CONFIG_IEEE80211W
934 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
935 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
936 wpa_msg(wpa_s, MSG_DEBUG,
937 "WPA: using KEY_MGMT 802.1X with SHA256");
938 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
939 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
940 wpa_msg(wpa_s, MSG_DEBUG,
941 "WPA: using KEY_MGMT PSK with SHA256");
942 #endif /* CONFIG_IEEE80211W */
943 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
944 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
945 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
946 } else if (sel & WPA_KEY_MGMT_PSK) {
947 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
948 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
949 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
950 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
951 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
953 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
954 "key management type.");
958 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
959 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
960 wpa_s->pairwise_cipher);
961 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
963 #ifdef CONFIG_IEEE80211W
964 sel = ie.mgmt_group_cipher;
965 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
966 !(ie.capabilities & WPA_CAPABILITY_MFPC))
968 if (sel & WPA_CIPHER_AES_128_CMAC) {
969 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
970 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
973 wpa_s->mgmt_group_cipher = 0;
974 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
976 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
977 wpa_s->mgmt_group_cipher);
978 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
979 #endif /* CONFIG_IEEE80211W */
981 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
982 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
987 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
988 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
990 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
997 * wpa_supplicant_associate - Request association
998 * @wpa_s: Pointer to wpa_supplicant data
999 * @bss: Scan results for the selected BSS, or %NULL if not available
1000 * @ssid: Configuration data for the selected network
1002 * This function is used to request %wpa_supplicant to associate with a BSS.
1004 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1005 struct wpa_bss *bss, struct wpa_ssid *ssid)
1009 int use_crypt, ret, i, bssid_changed;
1010 int algs = WPA_AUTH_ALG_OPEN;
1011 enum wpa_cipher cipher_pairwise, cipher_group;
1012 struct wpa_driver_associate_params params;
1013 int wep_keys_set = 0;
1014 struct wpa_driver_capa capa;
1015 int assoc_failed = 0;
1016 struct wpa_ssid *old_ssid;
1018 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1019 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1021 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1022 wpa_printf(MSG_INFO, "Driver does not support AP "
1026 wpa_supplicant_create_ap(wpa_s, ssid);
1027 wpa_s->current_bss = bss;
1028 #else /* CONFIG_AP */
1029 wpa_printf(MSG_ERROR, "AP mode support not included in the "
1031 #endif /* CONFIG_AP */
1035 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1036 ssid->mode == IEEE80211_MODE_INFRA) {
1037 sme_authenticate(wpa_s, bss, ssid);
1041 os_memset(¶ms, 0, sizeof(params));
1042 wpa_s->reassociate = 0;
1044 #ifdef CONFIG_IEEE80211R
1045 const u8 *ie, *md = NULL;
1046 #endif /* CONFIG_IEEE80211R */
1047 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1048 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1049 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1050 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1051 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1052 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1054 wpas_notify_bssid_changed(wpa_s);
1055 #ifdef CONFIG_IEEE80211R
1056 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1057 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1059 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1061 /* Prepare for the next transition */
1062 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1064 #endif /* CONFIG_IEEE80211R */
1066 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1067 wpa_s->conf->ap_scan == 2 &&
1068 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1069 /* Use ap_scan==1 style network selection to find the network
1071 wpa_s->scan_req = 2;
1072 wpa_s->reassociate = 1;
1073 wpa_supplicant_req_scan(wpa_s, 0, 0);
1075 #endif /* CONFIG_WPS */
1077 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1078 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1079 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1081 wpa_supplicant_cancel_scan(wpa_s);
1083 /* Starting new association, so clear the possibly used WPA IE from the
1084 * previous association. */
1085 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1087 #ifdef IEEE8021X_EAPOL
1088 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1090 if (ssid->non_leap == 0)
1091 algs = WPA_AUTH_ALG_LEAP;
1093 algs |= WPA_AUTH_ALG_LEAP;
1096 #endif /* IEEE8021X_EAPOL */
1097 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1098 if (ssid->auth_alg) {
1099 algs = ssid->auth_alg;
1100 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1104 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1105 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1106 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1107 WPA_KEY_MGMT_FT_IEEE8021X |
1108 WPA_KEY_MGMT_FT_PSK |
1109 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1110 WPA_KEY_MGMT_PSK_SHA256))) {
1111 int try_opportunistic;
1112 try_opportunistic = ssid->proactive_key_caching &&
1113 (ssid->proto & WPA_PROTO_RSN);
1114 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1115 wpa_s->current_ssid,
1116 try_opportunistic) == 0)
1117 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1118 wpa_ie_len = sizeof(wpa_ie);
1119 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1120 wpa_ie, &wpa_ie_len)) {
1121 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1122 "management and encryption suites");
1125 } else if (ssid->key_mgmt &
1126 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1127 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1128 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1129 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1130 wpa_ie_len = sizeof(wpa_ie);
1131 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1132 wpa_ie, &wpa_ie_len)) {
1133 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1134 "management and encryption suites (no scan "
1139 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1140 struct wpabuf *wps_ie;
1141 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1142 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1143 wpa_ie_len = wpabuf_len(wps_ie);
1144 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1147 wpabuf_free(wps_ie);
1148 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1149 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1150 params.wps = WPS_MODE_PRIVACY;
1152 params.wps = WPS_MODE_OPEN;
1153 #endif /* CONFIG_WPS */
1155 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1160 if (wpa_s->global->p2p) {
1165 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1166 pos = wpa_ie + wpa_ie_len;
1167 len = sizeof(wpa_ie) - wpa_ie_len;
1168 res = wpas_p2p_assoc_req_ie(wpa_s, bss->bssid, pos, len,
1173 #endif /* CONFIG_P2P */
1175 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1177 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1178 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1179 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1180 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1181 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1183 if (wpa_set_wep_keys(wpa_s, ssid)) {
1188 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1191 #ifdef IEEE8021X_EAPOL
1192 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1193 if ((ssid->eapol_flags &
1194 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1195 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1199 /* Assume that dynamic WEP-104 keys will be used and
1200 * set cipher suites in order for drivers to expect
1202 cipher_pairwise = cipher_group = CIPHER_WEP104;
1205 #endif /* IEEE8021X_EAPOL */
1207 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1208 /* Set the key before (and later after) association */
1209 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1212 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1214 params.bssid = bss->bssid;
1215 params.ssid = bss->ssid;
1216 params.ssid_len = bss->ssid_len;
1217 params.freq = bss->freq;
1219 params.ssid = ssid->ssid;
1220 params.ssid_len = ssid->ssid_len;
1222 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1224 params.freq = ssid->frequency; /* Initial channel for IBSS */
1225 params.wpa_ie = wpa_ie;
1226 params.wpa_ie_len = wpa_ie_len;
1227 params.pairwise_suite = cipher_pairwise;
1228 params.group_suite = cipher_group;
1229 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1230 params.auth_alg = algs;
1231 params.mode = ssid->mode;
1232 for (i = 0; i < NUM_WEP_KEYS; i++) {
1233 if (ssid->wep_key_len[i])
1234 params.wep_key[i] = ssid->wep_key[i];
1235 params.wep_key_len[i] = ssid->wep_key_len[i];
1237 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1239 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1240 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1241 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1242 params.passphrase = ssid->passphrase;
1244 params.psk = ssid->psk;
1247 params.drop_unencrypted = use_crypt;
1249 #ifdef CONFIG_IEEE80211W
1250 params.mgmt_frame_protection = ssid->ieee80211w;
1251 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1252 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1253 struct wpa_ie_data ie;
1254 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1256 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1257 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1259 params.mgmt_frame_protection =
1260 MGMT_FRAME_PROTECTION_REQUIRED;
1263 #endif /* CONFIG_IEEE80211W */
1266 if (wpa_s->global->p2p &&
1267 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1269 #endif /* CONFIG_P2P */
1271 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1272 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1274 ret = wpa_drv_associate(wpa_s, ¶ms);
1276 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1278 /* try to continue anyway; new association will be tried again
1283 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1284 /* Set the key after the association just in case association
1285 * cleared the previously configured key. */
1286 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1287 /* No need to timeout authentication since there is no key
1289 wpa_supplicant_cancel_auth_timeout(wpa_s);
1290 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1291 #ifdef CONFIG_IBSS_RSN
1292 } else if (ssid->mode == WPAS_MODE_IBSS &&
1293 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1294 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1295 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1297 * RSN IBSS authentication is per-STA and we can disable the
1298 * per-BSSID authentication.
1300 wpa_supplicant_cancel_auth_timeout(wpa_s);
1301 #endif /* CONFIG_IBSS_RSN */
1303 /* Timeout for IEEE 802.11 authentication and association */
1307 /* give IBSS a bit more time */
1308 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1309 } else if (wpa_s->conf->ap_scan == 1) {
1310 /* give IBSS a bit more time */
1311 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1313 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1316 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1317 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1318 /* Set static WEP keys again */
1319 wpa_set_wep_keys(wpa_s, ssid);
1322 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1324 * Do not allow EAP session resumption between different
1325 * network configurations.
1327 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1329 old_ssid = wpa_s->current_ssid;
1330 wpa_s->current_ssid = ssid;
1331 wpa_s->current_bss = bss;
1332 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1333 wpa_supplicant_initiate_eapol(wpa_s);
1334 if (old_ssid != wpa_s->current_ssid)
1335 wpas_notify_network_changed(wpa_s);
1339 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1342 struct wpa_ssid *old_ssid;
1344 wpa_clear_keys(wpa_s, addr);
1345 wpa_supplicant_mark_disassoc(wpa_s);
1346 old_ssid = wpa_s->current_ssid;
1347 wpa_s->current_ssid = NULL;
1348 wpa_s->current_bss = NULL;
1349 wpa_sm_set_config(wpa_s->wpa, NULL);
1350 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1351 if (old_ssid != wpa_s->current_ssid)
1352 wpas_notify_network_changed(wpa_s);
1353 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1358 * wpa_supplicant_disassociate - Disassociate the current connection
1359 * @wpa_s: Pointer to wpa_supplicant data
1360 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1362 * This function is used to request %wpa_supplicant to disassociate with the
1365 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1370 if (!is_zero_ether_addr(wpa_s->bssid)) {
1371 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1372 ieee80211_sta_disassociate(wpa_s, reason_code);
1374 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1375 addr = wpa_s->bssid;
1378 wpa_supplicant_clear_connection(wpa_s, addr);
1383 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1384 * @wpa_s: Pointer to wpa_supplicant data
1385 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1387 * This function is used to request %wpa_supplicant to deauthenticate from the
1390 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1395 if (!is_zero_ether_addr(wpa_s->bssid)) {
1396 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1397 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1399 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1401 addr = wpa_s->bssid;
1404 wpa_supplicant_clear_connection(wpa_s, addr);
1409 * wpa_supplicant_enable_network - Mark a configured network as enabled
1410 * @wpa_s: wpa_supplicant structure for a network interface
1411 * @ssid: wpa_ssid structure for a configured network or %NULL
1413 * Enables the specified network or all networks if no network specified.
1415 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1416 struct wpa_ssid *ssid)
1418 struct wpa_ssid *other_ssid;
1422 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1423 other_ssid = other_ssid->next) {
1424 if (other_ssid->disabled == 2)
1425 continue; /* do not change persistent P2P group
1427 if (other_ssid == wpa_s->current_ssid &&
1428 other_ssid->disabled)
1429 wpa_s->reassociate = 1;
1431 was_disabled = other_ssid->disabled;
1433 other_ssid->disabled = 0;
1435 if (was_disabled != other_ssid->disabled)
1436 wpas_notify_network_enabled_changed(
1439 if (wpa_s->reassociate)
1440 wpa_supplicant_req_scan(wpa_s, 0, 0);
1441 } else if (ssid->disabled && ssid->disabled != 2) {
1442 if (wpa_s->current_ssid == NULL) {
1444 * Try to reassociate since there is no current
1445 * configuration and a new network was made available.
1447 wpa_s->reassociate = 1;
1448 wpa_supplicant_req_scan(wpa_s, 0, 0);
1451 was_disabled = ssid->disabled;
1455 if (was_disabled != ssid->disabled)
1456 wpas_notify_network_enabled_changed(wpa_s, ssid);
1462 * wpa_supplicant_disable_network - Mark a configured network as disabled
1463 * @wpa_s: wpa_supplicant structure for a network interface
1464 * @ssid: wpa_ssid structure for a configured network or %NULL
1466 * Disables the specified network or all networks if no network specified.
1468 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1469 struct wpa_ssid *ssid)
1471 struct wpa_ssid *other_ssid;
1475 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1476 other_ssid = other_ssid->next) {
1477 was_disabled = other_ssid->disabled;
1478 if (was_disabled == 2)
1479 continue; /* do not change persistent P2P group
1482 other_ssid->disabled = 1;
1484 if (was_disabled != other_ssid->disabled)
1485 wpas_notify_network_enabled_changed(
1488 if (wpa_s->current_ssid)
1489 wpa_supplicant_disassociate(
1490 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1491 } else if (ssid->disabled != 2) {
1492 if (ssid == wpa_s->current_ssid)
1493 wpa_supplicant_disassociate(
1494 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1496 was_disabled = ssid->disabled;
1500 if (was_disabled != ssid->disabled)
1501 wpas_notify_network_enabled_changed(wpa_s, ssid);
1507 * wpa_supplicant_select_network - Attempt association with a network
1508 * @wpa_s: wpa_supplicant structure for a network interface
1509 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1511 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1512 struct wpa_ssid *ssid)
1515 struct wpa_ssid *other_ssid;
1517 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1518 wpa_supplicant_disassociate(
1519 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1522 * Mark all other networks disabled or mark all networks enabled if no
1523 * network specified.
1525 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1526 other_ssid = other_ssid->next) {
1527 int was_disabled = other_ssid->disabled;
1528 if (was_disabled == 2)
1529 continue; /* do not change persistent P2P group data */
1531 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1533 if (was_disabled != other_ssid->disabled)
1534 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1536 wpa_s->disconnected = 0;
1537 wpa_s->reassociate = 1;
1538 wpa_supplicant_req_scan(wpa_s, 0, 0);
1541 wpas_notify_network_selected(wpa_s, ssid);
1546 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1547 * @wpa_s: wpa_supplicant structure for a network interface
1548 * @ap_scan: AP scan mode
1549 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1552 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1557 if (ap_scan < 0 || ap_scan > 2)
1560 old_ap_scan = wpa_s->conf->ap_scan;
1561 wpa_s->conf->ap_scan = ap_scan;
1563 if (old_ap_scan != wpa_s->conf->ap_scan)
1564 wpas_notify_ap_scan_changed(wpa_s);
1571 * wpa_supplicant_set_debug_params - Set global debug params
1572 * @global: wpa_global structure
1573 * @debug_level: debug level
1574 * @debug_timestamp: determines if show timestamp in debug data
1575 * @debug_show_keys: determines if show keys in debug data
1576 * Returns: 0 if succeed or -1 if debug_level has wrong value
1578 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1579 int debug_timestamp, int debug_show_keys)
1582 int old_level, old_timestamp, old_show_keys;
1584 /* check for allowed debuglevels */
1585 if (debug_level != MSG_MSGDUMP &&
1586 debug_level != MSG_DEBUG &&
1587 debug_level != MSG_INFO &&
1588 debug_level != MSG_WARNING &&
1589 debug_level != MSG_ERROR)
1592 old_level = wpa_debug_level;
1593 old_timestamp = wpa_debug_timestamp;
1594 old_show_keys = wpa_debug_show_keys;
1596 wpa_debug_level = debug_level;
1597 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1598 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1600 if (wpa_debug_level != old_level)
1601 wpas_notify_debug_level_changed(global);
1602 if (wpa_debug_timestamp != old_timestamp)
1603 wpas_notify_debug_timestamp_changed(global);
1604 if (wpa_debug_show_keys != old_show_keys)
1605 wpas_notify_debug_show_keys_changed(global);
1612 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1613 * @wpa_s: Pointer to wpa_supplicant data
1614 * Returns: A pointer to the current network structure or %NULL on failure
1616 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1618 struct wpa_ssid *entry;
1619 u8 ssid[MAX_SSID_LEN];
1625 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1626 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1627 wpa_printf(MSG_WARNING, "Could not read SSID from "
1632 res = wpa_drv_get_ssid(wpa_s, ssid);
1634 wpa_printf(MSG_WARNING, "Could not read SSID from "
1641 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1642 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1643 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1644 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1648 wired = wpa_s->conf->ap_scan == 0 &&
1649 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1651 entry = wpa_s->conf->ssid;
1653 if (!entry->disabled &&
1654 ((ssid_len == entry->ssid_len &&
1655 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1656 (!entry->bssid_set ||
1657 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1660 if (!entry->disabled &&
1661 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1662 (entry->ssid == NULL || entry->ssid_len == 0) &&
1663 (!entry->bssid_set ||
1664 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1666 #endif /* CONFIG_WPS */
1667 entry = entry->next;
1674 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1684 if (wpa_drivers[0] == NULL) {
1685 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1691 /* default to first driver in the list */
1692 wpa_s->driver = wpa_drivers[0];
1693 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1697 pos = os_strchr(name, ',');
1701 len = os_strlen(name);
1702 for (i = 0; wpa_drivers[i]; i++) {
1703 if (os_strlen(wpa_drivers[i]->name) == len &&
1704 os_strncmp(name, wpa_drivers[i]->name, len) ==
1706 wpa_s->driver = wpa_drivers[i];
1707 wpa_s->global_drv_priv = wpa_s->global->drv_priv[i];
1712 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1718 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1719 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1720 * with struct wpa_driver_ops::init()
1721 * @src_addr: Source address of the EAPOL frame
1722 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1723 * @len: Length of the EAPOL data
1725 * This function is called for each received EAPOL frame. Most driver
1726 * interfaces rely on more generic OS mechanism for receiving frames through
1727 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1728 * take care of received EAPOL frames and deliver them to the core supplicant
1729 * code by calling this function.
1731 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1732 const u8 *buf, size_t len)
1734 struct wpa_supplicant *wpa_s = ctx;
1736 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1737 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1739 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1741 * There is possible race condition between receiving the
1742 * association event and the EAPOL frame since they are coming
1743 * through different paths from the driver. In order to avoid
1744 * issues in trying to process the EAPOL frame before receiving
1745 * association information, lets queue it for processing until
1746 * the association event is received.
1748 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1749 "received EAPOL frame");
1750 wpabuf_free(wpa_s->pending_eapol_rx);
1751 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1752 if (wpa_s->pending_eapol_rx) {
1753 os_get_time(&wpa_s->pending_eapol_rx_time);
1754 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1761 if (wpa_s->ap_iface) {
1762 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1765 #endif /* CONFIG_AP */
1767 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1768 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1769 "no key management is configured");
1773 if (wpa_s->eapol_received == 0 &&
1774 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1775 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1776 wpa_s->wpa_state != WPA_COMPLETED) &&
1777 (wpa_s->current_ssid == NULL ||
1778 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1779 /* Timeout for completing IEEE 802.1X and WPA authentication */
1780 wpa_supplicant_req_auth_timeout(
1782 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1783 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1784 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1787 wpa_s->eapol_received++;
1789 if (wpa_s->countermeasures) {
1790 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1795 #ifdef CONFIG_IBSS_RSN
1796 if (wpa_s->current_ssid &&
1797 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1798 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1801 #endif /* CONFIG_IBSS_RSN */
1803 /* Source address of the incoming EAPOL frame could be compared to the
1804 * current BSSID. However, it is possible that a centralized
1805 * Authenticator could be using another MAC address than the BSSID of
1806 * an AP, so just allow any address to be used for now. The replies are
1807 * still sent to the current BSSID (if available), though. */
1809 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1810 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1811 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1813 wpa_drv_poll(wpa_s);
1814 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1815 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1816 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1818 * Set portValid = TRUE here since we are going to skip 4-way
1819 * handshake processing which would normally set portValid. We
1820 * need this to allow the EAPOL state machines to be completed
1821 * without going through EAPOL-Key handshake.
1823 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1829 * wpa_supplicant_driver_init - Initialize driver interface parameters
1830 * @wpa_s: Pointer to wpa_supplicant data
1831 * Returns: 0 on success, -1 on failure
1833 * This function is called to initialize driver interface parameters.
1834 * wpa_drv_init() must have been called before this function to initialize the
1837 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1839 static int interface_count = 0;
1841 if (wpa_s->driver->send_eapol) {
1842 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1844 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1845 } else if (!(wpa_s->drv_flags &
1846 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
1847 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1848 wpa_drv_get_mac_addr(wpa_s),
1850 wpa_supplicant_rx_eapol, wpa_s, 0);
1851 if (wpa_s->l2 == NULL)
1854 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1856 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1859 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1860 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1864 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1865 MAC2STR(wpa_s->own_addr));
1867 if (wpa_s->bridge_ifname[0]) {
1868 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1869 " '%s'", wpa_s->bridge_ifname);
1870 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1873 wpa_supplicant_rx_eapol, wpa_s,
1875 if (wpa_s->l2_br == NULL) {
1876 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1877 "connection for the bridge interface '%s'",
1878 wpa_s->bridge_ifname);
1883 wpa_clear_keys(wpa_s, NULL);
1885 /* Make sure that TKIP countermeasures are not left enabled (could
1886 * happen if wpa_supplicant is killed during countermeasures. */
1887 wpa_drv_set_countermeasures(wpa_s, 0);
1889 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1890 wpa_drv_flush_pmkid(wpa_s);
1892 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1893 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
1894 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1897 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1903 static int wpa_supplicant_daemon(const char *pid_file)
1905 wpa_printf(MSG_DEBUG, "Daemonize..");
1906 return os_daemonize(pid_file);
1910 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1912 struct wpa_supplicant *wpa_s;
1914 wpa_s = os_zalloc(sizeof(*wpa_s));
1917 wpa_s->scan_req = 1;
1918 wpa_s->new_connection = 1;
1919 wpa_s->parent = wpa_s;
1925 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1926 struct wpa_interface *iface)
1928 const char *ifname, *driver;
1929 struct wpa_driver_capa capa;
1931 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1932 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1933 iface->confname ? iface->confname : "N/A",
1934 iface->driver ? iface->driver : "default",
1935 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1936 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1938 if (iface->confname) {
1939 #ifdef CONFIG_BACKEND_FILE
1940 wpa_s->confname = os_rel2abs_path(iface->confname);
1941 if (wpa_s->confname == NULL) {
1942 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1943 "for configuration file '%s'.",
1947 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1948 iface->confname, wpa_s->confname);
1949 #else /* CONFIG_BACKEND_FILE */
1950 wpa_s->confname = os_strdup(iface->confname);
1951 #endif /* CONFIG_BACKEND_FILE */
1952 wpa_s->conf = wpa_config_read(wpa_s->confname);
1953 if (wpa_s->conf == NULL) {
1954 wpa_printf(MSG_ERROR, "Failed to read or parse "
1955 "configuration '%s'.", wpa_s->confname);
1960 * Override ctrl_interface and driver_param if set on command
1963 if (iface->ctrl_interface) {
1964 os_free(wpa_s->conf->ctrl_interface);
1965 wpa_s->conf->ctrl_interface =
1966 os_strdup(iface->ctrl_interface);
1969 if (iface->driver_param) {
1970 os_free(wpa_s->conf->driver_param);
1971 wpa_s->conf->driver_param =
1972 os_strdup(iface->driver_param);
1975 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1976 iface->driver_param);
1978 if (wpa_s->conf == NULL) {
1979 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1983 if (iface->ifname == NULL) {
1984 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1987 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1988 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1992 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1994 if (iface->bridge_ifname) {
1995 if (os_strlen(iface->bridge_ifname) >=
1996 sizeof(wpa_s->bridge_ifname)) {
1997 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1998 "name '%s'.", iface->bridge_ifname);
2001 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2002 sizeof(wpa_s->bridge_ifname));
2005 /* RSNA Supplicant Key Management - INITIALIZE */
2006 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2007 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2009 /* Initialize driver interface and register driver event handler before
2010 * L2 receive handler so that association events are processed before
2011 * EAPOL-Key packets if both become available for the same select()
2013 driver = iface->driver;
2015 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2018 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2019 if (wpa_s->drv_priv == NULL) {
2021 pos = driver ? os_strchr(driver, ',') : NULL;
2023 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2024 "interface - try next driver wrapper");
2028 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2031 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2032 wpa_printf(MSG_ERROR, "Driver interface rejected "
2033 "driver_param '%s'", wpa_s->conf->driver_param);
2037 ifname = wpa_drv_get_ifname(wpa_s);
2038 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2039 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2040 "name with '%s'", ifname);
2041 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2044 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2047 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2048 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2050 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2052 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2053 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2054 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2055 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2056 "dot11RSNAConfigPMKLifetime");
2060 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2061 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2062 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2063 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2064 "dot11RSNAConfigPMKReauthThreshold");
2068 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2069 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2070 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2071 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2072 "dot11RSNAConfigSATimeout");
2076 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2077 wpa_s->drv_flags = capa.flags;
2078 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2079 if (ieee80211_sta_init(wpa_s))
2082 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2083 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2085 if (wpa_s->max_remain_on_chan == 0)
2086 wpa_s->max_remain_on_chan = 1000;
2088 if (wpa_supplicant_driver_init(wpa_s) < 0)
2091 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2092 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2093 wpa_printf(MSG_DEBUG, "Failed to set country");
2097 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2099 if (wpas_wps_init(wpa_s))
2102 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2104 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2106 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2107 if (wpa_s->ctrl_iface == NULL) {
2108 wpa_printf(MSG_ERROR,
2109 "Failed to initialize control interface '%s'.\n"
2110 "You may have another wpa_supplicant process "
2111 "already running or the file was\n"
2112 "left by an unclean termination of wpa_supplicant "
2113 "in which case you will need\n"
2114 "to manually remove this file before starting "
2115 "wpa_supplicant again.\n",
2116 wpa_s->conf->ctrl_interface);
2120 #ifdef CONFIG_IBSS_RSN
2121 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2122 if (!wpa_s->ibss_rsn) {
2123 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2126 #endif /* CONFIG_IBSS_RSN */
2129 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2130 wpa_printf(MSG_ERROR, "Failed to init P2P");
2133 #endif /* CONFIG_P2P */
2135 if (wpa_bss_init(wpa_s) < 0)
2142 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2145 if (wpa_s->drv_priv) {
2146 wpa_supplicant_deauthenticate(wpa_s,
2147 WLAN_REASON_DEAUTH_LEAVING);
2149 wpa_drv_set_countermeasures(wpa_s, 0);
2150 wpa_clear_keys(wpa_s, NULL);
2153 wpa_supplicant_cleanup(wpa_s);
2156 wpas_notify_iface_removed(wpa_s);
2158 if (wpa_s->drv_priv)
2159 wpa_drv_deinit(wpa_s);
2164 * wpa_supplicant_add_iface - Add a new network interface
2165 * @global: Pointer to global data from wpa_supplicant_init()
2166 * @iface: Interface configuration options
2167 * Returns: Pointer to the created interface or %NULL on failure
2169 * This function is used to add new network interfaces for %wpa_supplicant.
2170 * This can be called before wpa_supplicant_run() to add interfaces before the
2171 * main event loop has been started. In addition, new interfaces can be added
2172 * dynamically while %wpa_supplicant is already running. This could happen,
2173 * e.g., when a hotplug network adapter is inserted.
2175 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2176 struct wpa_interface *iface)
2178 struct wpa_supplicant *wpa_s;
2179 struct wpa_interface t_iface;
2180 struct wpa_ssid *ssid;
2182 if (global == NULL || iface == NULL)
2185 wpa_s = wpa_supplicant_alloc();
2189 wpa_s->global = global;
2192 if (global->params.override_driver) {
2193 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2195 iface->driver, global->params.override_driver);
2196 t_iface.driver = global->params.override_driver;
2198 if (global->params.override_ctrl_interface) {
2199 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2200 "ctrl_interface ('%s' -> '%s')",
2201 iface->ctrl_interface,
2202 global->params.override_ctrl_interface);
2203 t_iface.ctrl_interface =
2204 global->params.override_ctrl_interface;
2206 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2207 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2209 wpa_supplicant_deinit_iface(wpa_s, 0);
2214 /* Notify the control interfaces about new iface */
2215 if (wpas_notify_iface_added(wpa_s)) {
2216 wpa_supplicant_deinit_iface(wpa_s, 1);
2221 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2222 wpas_notify_network_added(wpa_s, ssid);
2224 wpa_s->next = global->ifaces;
2225 global->ifaces = wpa_s;
2227 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2234 * wpa_supplicant_remove_iface - Remove a network interface
2235 * @global: Pointer to global data from wpa_supplicant_init()
2236 * @wpa_s: Pointer to the network interface to be removed
2237 * Returns: 0 if interface was removed, -1 if interface was not found
2239 * This function can be used to dynamically remove network interfaces from
2240 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2241 * addition, this function is used to remove all remaining interfaces when
2242 * %wpa_supplicant is terminated.
2244 int wpa_supplicant_remove_iface(struct wpa_global *global,
2245 struct wpa_supplicant *wpa_s)
2247 struct wpa_supplicant *prev;
2249 /* Remove interface from the global list of interfaces */
2250 prev = global->ifaces;
2251 if (prev == wpa_s) {
2252 global->ifaces = wpa_s->next;
2254 while (prev && prev->next != wpa_s)
2258 prev->next = wpa_s->next;
2261 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2263 if (global->p2p_group_formation == wpa_s)
2264 global->p2p_group_formation = NULL;
2265 wpa_supplicant_deinit_iface(wpa_s, 1);
2273 * wpa_supplicant_get_iface - Get a new network interface
2274 * @global: Pointer to global data from wpa_supplicant_init()
2275 * @ifname: Interface name
2276 * Returns: Pointer to the interface or %NULL if not found
2278 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2281 struct wpa_supplicant *wpa_s;
2283 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2284 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2292 * wpa_supplicant_init - Initialize %wpa_supplicant
2293 * @params: Parameters for %wpa_supplicant
2294 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2296 * This function is used to initialize %wpa_supplicant. After successful
2297 * initialization, the returned data pointer can be used to add and remove
2298 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2300 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2302 struct wpa_global *global;
2308 wpa_debug_open_file(params->wpa_debug_file_path);
2309 if (params->wpa_debug_syslog)
2310 wpa_debug_open_syslog();
2312 ret = eap_register_methods();
2314 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2316 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2317 "the same EAP type.");
2321 global = os_zalloc(sizeof(*global));
2324 dl_list_init(&global->p2p_srv_bonjour);
2325 dl_list_init(&global->p2p_srv_upnp);
2326 global->params.daemonize = params->daemonize;
2327 global->params.wait_for_monitor = params->wait_for_monitor;
2328 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2329 if (params->pid_file)
2330 global->params.pid_file = os_strdup(params->pid_file);
2331 if (params->ctrl_interface)
2332 global->params.ctrl_interface =
2333 os_strdup(params->ctrl_interface);
2334 if (params->override_driver)
2335 global->params.override_driver =
2336 os_strdup(params->override_driver);
2337 if (params->override_ctrl_interface)
2338 global->params.override_ctrl_interface =
2339 os_strdup(params->override_ctrl_interface);
2340 wpa_debug_level = global->params.wpa_debug_level =
2341 params->wpa_debug_level;
2342 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2343 params->wpa_debug_show_keys;
2344 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2345 params->wpa_debug_timestamp;
2348 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2349 wpa_supplicant_deinit(global);
2353 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2354 if (global->ctrl_iface == NULL) {
2355 wpa_supplicant_deinit(global);
2359 if (wpas_notify_supplicant_initialized(global)) {
2360 wpa_supplicant_deinit(global);
2364 for (i = 0; wpa_drivers[i]; i++)
2365 global->drv_count++;
2366 if (global->drv_count == 0) {
2367 wpa_printf(MSG_ERROR, "No drivers enabled");
2368 wpa_supplicant_deinit(global);
2371 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2372 if (global->drv_priv == NULL) {
2373 wpa_supplicant_deinit(global);
2376 for (i = 0; wpa_drivers[i]; i++) {
2377 if (!wpa_drivers[i]->global_init)
2379 global->drv_priv[i] = wpa_drivers[i]->global_init();
2380 if (global->drv_priv[i] == NULL) {
2381 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2382 "'%s'", wpa_drivers[i]->name);
2383 wpa_supplicant_deinit(global);
2393 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2394 * @global: Pointer to global data from wpa_supplicant_init()
2395 * Returns: 0 after successful event loop run, -1 on failure
2397 * This function starts the main event loop and continues running as long as
2398 * there are any remaining events. In most cases, this function is running as
2399 * long as the %wpa_supplicant process in still in use.
2401 int wpa_supplicant_run(struct wpa_global *global)
2403 struct wpa_supplicant *wpa_s;
2405 if (global->params.daemonize &&
2406 wpa_supplicant_daemon(global->params.pid_file))
2409 if (global->params.wait_for_monitor) {
2410 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2411 if (wpa_s->ctrl_iface)
2412 wpa_supplicant_ctrl_iface_wait(
2416 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2417 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2426 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2427 * @global: Pointer to global data from wpa_supplicant_init()
2429 * This function is called to deinitialize %wpa_supplicant and to free all
2430 * allocated resources. Remaining network interfaces will also be removed.
2432 void wpa_supplicant_deinit(struct wpa_global *global)
2440 wpas_p2p_deinit_global(global);
2441 #endif /* CONFIG_P2P */
2443 while (global->ifaces)
2444 wpa_supplicant_remove_iface(global, global->ifaces);
2446 if (global->ctrl_iface)
2447 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2449 wpas_notify_supplicant_deinitialized(global);
2451 eap_peer_unregister_methods();
2453 eap_server_unregister_methods();
2454 #endif /* CONFIG_AP */
2456 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2457 if (!global->drv_priv[i])
2459 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2461 os_free(global->drv_priv);
2465 if (global->params.pid_file) {
2466 os_daemonize_terminate(global->params.pid_file);
2467 os_free(global->params.pid_file);
2469 os_free(global->params.ctrl_interface);
2470 os_free(global->params.override_driver);
2471 os_free(global->params.override_ctrl_interface);
2474 wpa_debug_close_syslog();
2475 wpa_debug_close_file();
2479 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2482 wpas_wps_update_config(wpa_s);
2483 #endif /* CONFIG_WPS */
2486 wpas_p2p_update_config(wpa_s);
2487 #endif /* CONFIG_P2P */
2489 wpa_s->conf->changed_parameters = 0;