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"
49 const char *wpa_supplicant_version =
50 "wpa_supplicant v" VERSION_STR "\n"
51 "Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
53 const char *wpa_supplicant_license =
54 "This program is free software. You can distribute it and/or modify it\n"
55 "under the terms of the GNU General Public License version 2.\n"
57 "Alternatively, this software may be distributed under the terms of the\n"
58 "BSD license. See README and COPYING for more details.\n"
59 #ifdef EAP_TLS_OPENSSL
60 "\nThis product includes software developed by the OpenSSL Project\n"
61 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
62 #endif /* EAP_TLS_OPENSSL */
65 #ifndef CONFIG_NO_STDOUT_DEBUG
66 /* Long text divided into parts in order to fit in C89 strings size limits. */
67 const char *wpa_supplicant_full_license1 =
68 "This program is free software; you can redistribute it and/or modify\n"
69 "it under the terms of the GNU General Public License version 2 as\n"
70 "published by the Free Software Foundation.\n"
72 "This program is distributed in the hope that it will be useful,\n"
73 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
74 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
75 "GNU General Public License for more details.\n"
77 const char *wpa_supplicant_full_license2 =
78 "You should have received a copy of the GNU General Public License\n"
79 "along with this program; if not, write to the Free Software\n"
80 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
82 "Alternatively, this software may be distributed under the terms of the\n"
85 "Redistribution and use in source and binary forms, with or without\n"
86 "modification, are permitted provided that the following conditions are\n"
89 const char *wpa_supplicant_full_license3 =
90 "1. Redistributions of source code must retain the above copyright\n"
91 " notice, this list of conditions and the following disclaimer.\n"
93 "2. Redistributions in binary form must reproduce the above copyright\n"
94 " notice, this list of conditions and the following disclaimer in the\n"
95 " documentation and/or other materials provided with the distribution.\n"
97 const char *wpa_supplicant_full_license4 =
98 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
99 " names of its contributors may be used to endorse or promote products\n"
100 " derived from this software without specific prior written permission.\n"
102 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
103 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
104 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
105 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
106 const char *wpa_supplicant_full_license5 =
107 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
108 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
109 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
110 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
111 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
112 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
113 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
115 #endif /* CONFIG_NO_STDOUT_DEBUG */
117 extern int wpa_debug_level;
118 extern int wpa_debug_show_keys;
119 extern int wpa_debug_timestamp;
120 extern struct wpa_driver_ops *wpa_drivers[];
122 /* Configure default/group WEP keys for static WEP */
123 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
127 for (i = 0; i < NUM_WEP_KEYS; i++) {
128 if (ssid->wep_key_len[i] == 0)
132 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
133 (u8 *) "\xff\xff\xff\xff\xff\xff",
134 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
135 ssid->wep_key[i], ssid->wep_key_len[i]);
142 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
143 struct wpa_ssid *ssid)
150 /* IBSS/WPA-None uses only one key (Group) for both receiving and
151 * sending unicast and multicast packets. */
153 if (ssid->mode != IEEE80211_MODE_IBSS) {
154 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
155 "for WPA-None", ssid->mode);
159 if (!ssid->psk_set) {
160 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
164 switch (wpa_s->group_cipher) {
165 case WPA_CIPHER_CCMP:
166 os_memcpy(key, ssid->psk, 16);
170 case WPA_CIPHER_TKIP:
171 /* WPA-None uses the same Michael MIC key for both TX and RX */
172 os_memcpy(key, ssid->psk, 16 + 8);
173 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
178 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
179 "WPA-None", wpa_s->group_cipher);
183 /* TODO: should actually remember the previously used seq#, both for TX
184 * and RX from each STA.. */
186 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
187 0, 1, seq, 6, key, keylen);
191 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
193 struct wpa_supplicant *wpa_s = eloop_ctx;
194 const u8 *bssid = wpa_s->bssid;
195 if (is_zero_ether_addr(bssid))
196 bssid = wpa_s->pending_bssid;
197 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
199 wpa_blacklist_add(wpa_s, bssid);
200 wpa_sm_notify_disassoc(wpa_s->wpa);
201 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
202 wpa_s->reassociate = 1;
203 wpa_supplicant_req_scan(wpa_s, 0, 0);
208 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
209 * @wpa_s: Pointer to wpa_supplicant data
210 * @sec: Number of seconds after which to time out authentication
211 * @usec: Number of microseconds after which to time out authentication
213 * This function is used to schedule a timeout for the current authentication
216 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
219 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
220 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
223 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
224 "%d usec", sec, usec);
225 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
226 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
231 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
232 * @wpa_s: Pointer to wpa_supplicant data
234 * This function is used to cancel authentication timeout scheduled with
235 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
238 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
240 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
241 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
242 wpa_blacklist_del(wpa_s, wpa_s->bssid);
247 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
248 * @wpa_s: Pointer to wpa_supplicant data
250 * This function is used to configure EAPOL state machine based on the selected
251 * authentication mode.
253 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
255 #ifdef IEEE8021X_EAPOL
256 struct eapol_config eapol_conf;
257 struct wpa_ssid *ssid = wpa_s->current_ssid;
259 #ifdef CONFIG_IBSS_RSN
260 if (ssid->mode == IEEE80211_MODE_IBSS &&
261 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
262 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
264 * RSN IBSS authentication is per-STA and we can disable the
265 * per-BSSID EAPOL authentication.
267 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
268 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
269 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
272 #endif /* CONFIG_IBSS_RSN */
274 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
275 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
277 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
278 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
279 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
281 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
283 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
284 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
285 eapol_conf.accept_802_1x_keys = 1;
286 eapol_conf.required_keys = 0;
287 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
288 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
290 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
291 eapol_conf.required_keys |=
292 EAPOL_REQUIRE_KEY_BROADCAST;
295 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
296 eapol_conf.required_keys = 0;
299 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
300 eapol_conf.workaround = ssid->eap_workaround;
301 eapol_conf.eap_disabled =
302 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
303 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
304 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
305 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
306 #endif /* IEEE8021X_EAPOL */
311 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
312 * @wpa_s: Pointer to wpa_supplicant data
313 * @ssid: Configuration data for the network
315 * This function is used to configure WPA state machine and related parameters
316 * to a mode where WPA is not enabled. This is called as part of the
317 * authentication configuration when the selected network does not use WPA.
319 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
320 struct wpa_ssid *ssid)
324 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
325 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
326 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
327 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
329 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
330 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
331 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
332 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
333 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
334 wpa_s->group_cipher = WPA_CIPHER_NONE;
335 wpa_s->mgmt_group_cipher = 0;
337 for (i = 0; i < NUM_WEP_KEYS; i++) {
338 if (ssid->wep_key_len[i] > 5) {
339 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
340 wpa_s->group_cipher = WPA_CIPHER_WEP104;
342 } else if (ssid->wep_key_len[i] > 0) {
343 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
344 wpa_s->group_cipher = WPA_CIPHER_WEP40;
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
352 wpa_s->pairwise_cipher);
353 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
354 #ifdef CONFIG_IEEE80211W
355 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
356 wpa_s->mgmt_group_cipher);
357 #endif /* CONFIG_IEEE80211W */
359 pmksa_cache_clear_current(wpa_s->wpa);
363 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
365 bgscan_deinit(wpa_s);
366 scard_deinit(wpa_s->scard);
368 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
369 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
370 l2_packet_deinit(wpa_s->l2);
373 l2_packet_deinit(wpa_s->l2_br);
377 if (wpa_s->ctrl_iface) {
378 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
379 wpa_s->ctrl_iface = NULL;
381 if (wpa_s->conf != NULL) {
382 wpa_config_free(wpa_s->conf);
386 os_free(wpa_s->confname);
387 wpa_s->confname = NULL;
389 wpa_sm_set_eapol(wpa_s->wpa, NULL);
390 eapol_sm_deinit(wpa_s->eapol);
393 rsn_preauth_deinit(wpa_s->wpa);
395 pmksa_candidate_free(wpa_s->wpa);
396 wpa_sm_deinit(wpa_s->wpa);
398 wpa_blacklist_clear(wpa_s);
400 wpa_scan_results_free(wpa_s->scan_res);
401 wpa_s->scan_res = NULL;
403 wpa_supplicant_cancel_scan(wpa_s);
404 wpa_supplicant_cancel_auth_timeout(wpa_s);
406 ieee80211_sta_deinit(wpa_s);
408 wpas_wps_deinit(wpa_s);
410 wpabuf_free(wpa_s->pending_eapol_rx);
411 wpa_s->pending_eapol_rx = NULL;
413 #ifdef CONFIG_IBSS_RSN
414 ibss_rsn_deinit(wpa_s->ibss_rsn);
415 wpa_s->ibss_rsn = NULL;
416 #endif /* CONFIG_IBSS_RSN */
419 os_free(wpa_s->sme.ft_ies);
420 wpa_s->sme.ft_ies = NULL;
421 wpa_s->sme.ft_ies_len = 0;
422 #endif /* CONFIG_SME */
425 wpa_supplicant_ap_deinit(wpa_s);
426 #endif /* CONFIG_AP */
431 * wpa_clear_keys - Clear keys configured for the driver
432 * @wpa_s: Pointer to wpa_supplicant data
433 * @addr: Previously used BSSID or %NULL if not available
435 * This function clears the encryption keys that has been previously configured
438 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
440 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
442 if (wpa_s->keys_cleared) {
443 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
444 * timing issues with keys being cleared just before new keys
445 * are set or just after association or something similar. This
446 * shows up in group key handshake failing often because of the
447 * client not receiving the first encrypted packets correctly.
448 * Skipping some of the extra key clearing steps seems to help
449 * in completing group key handshake more reliably. */
450 wpa_printf(MSG_DEBUG, "No keys have been configured - "
451 "skip key clearing");
455 /* MLME-DELETEKEYS.request */
456 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
457 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
458 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
459 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
460 #ifdef CONFIG_IEEE80211W
461 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
462 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
463 #endif /* CONFIG_IEEE80211W */
465 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
467 /* MLME-SETPROTECTION.request(None) */
468 wpa_drv_mlme_setprotection(
470 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
471 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
473 wpa_s->keys_cleared = 1;
478 * wpa_supplicant_state_txt - Get the connection state name as a text string
479 * @state: State (wpa_state; WPA_*)
480 * Returns: The state name as a printable text string
482 const char * wpa_supplicant_state_txt(int state)
485 case WPA_DISCONNECTED:
486 return "DISCONNECTED";
491 case WPA_AUTHENTICATING:
492 return "AUTHENTICATING";
493 case WPA_ASSOCIATING:
494 return "ASSOCIATING";
497 case WPA_4WAY_HANDSHAKE:
498 return "4WAY_HANDSHAKE";
499 case WPA_GROUP_HANDSHAKE:
500 return "GROUP_HANDSHAKE";
510 * wpa_supplicant_set_state - Set current connection state
511 * @wpa_s: Pointer to wpa_supplicant data
512 * @state: The new connection state
514 * This function is called whenever the connection state changes, e.g.,
515 * association is completed for WPA/WPA2 4-Way Handshake is started.
517 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
519 wpa_printf(MSG_DEBUG, "State: %s -> %s",
520 wpa_supplicant_state_txt(wpa_s->wpa_state),
521 wpa_supplicant_state_txt(state));
523 if (state != WPA_SCANNING)
524 wpa_supplicant_notify_scanning(wpa_s, 0);
526 wpas_notify_state_changed(wpa_s, state, wpa_s->wpa_state);
528 if (state == WPA_COMPLETED && wpa_s->new_connection) {
529 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
530 struct wpa_ssid *ssid = wpa_s->current_ssid;
531 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
532 MACSTR " completed %s [id=%d id_str=%s]",
533 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
534 "(reauth)" : "(auth)",
535 ssid ? ssid->id : -1,
536 ssid && ssid->id_str ? ssid->id_str : "");
537 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
538 wpa_s->new_connection = 0;
539 wpa_s->reassociated_connection = 1;
540 wpa_drv_set_operstate(wpa_s, 1);
541 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
542 state == WPA_ASSOCIATED) {
543 wpa_s->new_connection = 1;
544 wpa_drv_set_operstate(wpa_s, 0);
546 wpa_s->wpa_state = state;
550 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
553 struct wpa_global *global = eloop_ctx;
554 struct wpa_supplicant *wpa_s;
555 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
556 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
563 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
565 wpa_states old_state = wpa_s->wpa_state;
566 wpa_s->pairwise_cipher = 0;
567 wpa_s->group_cipher = 0;
568 wpa_s->mgmt_group_cipher = 0;
570 wpa_s->wpa_state = WPA_DISCONNECTED;
571 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
576 * wpa_supplicant_reload_configuration - Reload configuration data
577 * @wpa_s: Pointer to wpa_supplicant data
578 * Returns: 0 on success or -1 if configuration parsing failed
580 * This function can be used to request that the configuration data is reloaded
581 * (e.g., after configuration file change). This function is reloading
582 * configuration only for one interface, so this may need to be called multiple
583 * times if %wpa_supplicant is controlling multiple interfaces and all
584 * interfaces need reconfiguration.
586 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
588 struct wpa_config *conf;
589 struct wpa_ssid *old_ssid;
593 if (wpa_s->confname == NULL)
595 conf = wpa_config_read(wpa_s->confname);
597 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
598 "file '%s' - exiting", wpa_s->confname);
602 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
603 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
604 os_strcmp(conf->ctrl_interface,
605 wpa_s->conf->ctrl_interface) != 0);
607 if (reconf_ctrl && wpa_s->ctrl_iface) {
608 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
609 wpa_s->ctrl_iface = NULL;
612 eapol_sm_invalidate_cached_session(wpa_s->eapol);
613 old_ssid = wpa_s->current_ssid;
614 wpa_s->current_ssid = NULL;
615 if (old_ssid != wpa_s->current_ssid)
616 wpas_notify_network_changed(wpa_s);
619 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
620 * pkcs11_engine_path, pkcs11_module_path.
622 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
624 * Clear forced success to clear EAP state for next
627 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
629 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
630 wpa_sm_set_config(wpa_s->wpa, NULL);
631 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
632 rsn_preauth_deinit(wpa_s->wpa);
634 old_ap_scan = wpa_s->conf->ap_scan;
635 wpa_config_free(wpa_s->conf);
637 if (old_ap_scan != wpa_s->conf->ap_scan)
638 wpas_notify_ap_scan_changed(wpa_s);
641 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
643 wpa_supplicant_clear_status(wpa_s);
644 wpa_s->reassociate = 1;
645 wpa_supplicant_req_scan(wpa_s, 0, 0);
646 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
651 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
654 struct wpa_global *global = eloop_ctx;
655 struct wpa_supplicant *wpa_s;
656 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
657 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
658 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
665 static wpa_cipher cipher_suite2driver(int cipher)
668 case WPA_CIPHER_NONE:
670 case WPA_CIPHER_WEP40:
672 case WPA_CIPHER_WEP104:
673 return CIPHER_WEP104;
674 case WPA_CIPHER_CCMP:
676 case WPA_CIPHER_TKIP:
683 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
686 case WPA_KEY_MGMT_NONE:
687 return KEY_MGMT_NONE;
688 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
689 return KEY_MGMT_802_1X_NO_WPA;
690 case WPA_KEY_MGMT_IEEE8021X:
691 return KEY_MGMT_802_1X;
692 case WPA_KEY_MGMT_WPA_NONE:
693 return KEY_MGMT_WPA_NONE;
694 case WPA_KEY_MGMT_FT_IEEE8021X:
695 return KEY_MGMT_FT_802_1X;
696 case WPA_KEY_MGMT_FT_PSK:
697 return KEY_MGMT_FT_PSK;
698 case WPA_KEY_MGMT_IEEE8021X_SHA256:
699 return KEY_MGMT_802_1X_SHA256;
700 case WPA_KEY_MGMT_PSK_SHA256:
701 return KEY_MGMT_PSK_SHA256;
702 case WPA_KEY_MGMT_WPS:
704 case WPA_KEY_MGMT_PSK:
711 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
712 struct wpa_ssid *ssid,
713 struct wpa_ie_data *ie)
715 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
718 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
719 "from association info");
724 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
726 if (!(ie->group_cipher & ssid->group_cipher)) {
727 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
728 "cipher 0x%x (mask 0x%x) - reject",
729 ie->group_cipher, ssid->group_cipher);
732 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
733 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
734 "cipher 0x%x (mask 0x%x) - reject",
735 ie->pairwise_cipher, ssid->pairwise_cipher);
738 if (!(ie->key_mgmt & ssid->key_mgmt)) {
739 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
740 "management 0x%x (mask 0x%x) - reject",
741 ie->key_mgmt, ssid->key_mgmt);
745 #ifdef CONFIG_IEEE80211W
746 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
747 ssid->ieee80211w == IEEE80211W_REQUIRED) {
748 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
749 "that does not support management frame protection - "
753 #endif /* CONFIG_IEEE80211W */
760 * wpa_supplicant_set_suites - Set authentication and encryption parameters
761 * @wpa_s: Pointer to wpa_supplicant data
762 * @bss: Scan results for the selected BSS, or %NULL if not available
763 * @ssid: Configuration data for the selected network
764 * @wpa_ie: Buffer for the WPA/RSN IE
765 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
766 * used buffer length in case the functions returns success.
767 * Returns: 0 on success or -1 on failure
769 * This function is used to configure authentication and encryption parameters
770 * based on the network configuration and scan result for the selected BSS (if
773 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
774 struct wpa_scan_res *bss,
775 struct wpa_ssid *ssid,
776 u8 *wpa_ie, size_t *wpa_ie_len)
778 struct wpa_ie_data ie;
780 const u8 *bss_wpa, *bss_rsn;
783 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
784 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
786 bss_wpa = bss_rsn = NULL;
788 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
789 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
790 (ie.group_cipher & ssid->group_cipher) &&
791 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
792 (ie.key_mgmt & ssid->key_mgmt)) {
793 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
794 proto = WPA_PROTO_RSN;
795 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
796 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
797 (ie.group_cipher & ssid->group_cipher) &&
798 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
799 (ie.key_mgmt & ssid->key_mgmt)) {
800 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
801 proto = WPA_PROTO_WPA;
803 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
806 if (ssid->proto & WPA_PROTO_RSN)
807 proto = WPA_PROTO_RSN;
809 proto = WPA_PROTO_WPA;
810 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
811 os_memset(&ie, 0, sizeof(ie));
812 ie.group_cipher = ssid->group_cipher;
813 ie.pairwise_cipher = ssid->pairwise_cipher;
814 ie.key_mgmt = ssid->key_mgmt;
815 #ifdef CONFIG_IEEE80211W
816 ie.mgmt_group_cipher =
817 ssid->ieee80211w != NO_IEEE80211W ?
818 WPA_CIPHER_AES_128_CMAC : 0;
819 #endif /* CONFIG_IEEE80211W */
820 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
826 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
827 "pairwise %d key_mgmt %d proto %d",
828 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
829 #ifdef CONFIG_IEEE80211W
830 if (ssid->ieee80211w) {
831 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
832 ie.mgmt_group_cipher);
834 #endif /* CONFIG_IEEE80211W */
836 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
837 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
838 !!(ssid->proto & WPA_PROTO_RSN));
840 if (bss || !wpa_s->ap_ies_from_associnfo) {
841 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
842 bss_wpa ? 2 + bss_wpa[1] : 0) ||
843 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
844 bss_rsn ? 2 + bss_rsn[1] : 0))
848 sel = ie.group_cipher & ssid->group_cipher;
849 if (sel & WPA_CIPHER_CCMP) {
850 wpa_s->group_cipher = WPA_CIPHER_CCMP;
851 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
852 } else if (sel & WPA_CIPHER_TKIP) {
853 wpa_s->group_cipher = WPA_CIPHER_TKIP;
854 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
855 } else if (sel & WPA_CIPHER_WEP104) {
856 wpa_s->group_cipher = WPA_CIPHER_WEP104;
857 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
858 } else if (sel & WPA_CIPHER_WEP40) {
859 wpa_s->group_cipher = WPA_CIPHER_WEP40;
860 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
862 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
866 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
867 if (sel & WPA_CIPHER_CCMP) {
868 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
869 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
870 } else if (sel & WPA_CIPHER_TKIP) {
871 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
872 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
873 } else if (sel & WPA_CIPHER_NONE) {
874 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
875 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
877 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
882 sel = ie.key_mgmt & ssid->key_mgmt;
884 #ifdef CONFIG_IEEE80211R
885 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
886 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
887 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
888 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
889 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
890 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
891 #endif /* CONFIG_IEEE80211R */
892 #ifdef CONFIG_IEEE80211W
893 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
894 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
895 wpa_msg(wpa_s, MSG_DEBUG,
896 "WPA: using KEY_MGMT 802.1X with SHA256");
897 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
898 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
899 wpa_msg(wpa_s, MSG_DEBUG,
900 "WPA: using KEY_MGMT PSK with SHA256");
901 #endif /* CONFIG_IEEE80211W */
902 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
903 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
904 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
905 } else if (sel & WPA_KEY_MGMT_PSK) {
906 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
907 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
908 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
909 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
910 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
912 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
913 "key management type.");
917 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
918 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
919 wpa_s->pairwise_cipher);
920 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
922 #ifdef CONFIG_IEEE80211W
923 sel = ie.mgmt_group_cipher;
924 if (ssid->ieee80211w == NO_IEEE80211W ||
925 !(ie.capabilities & WPA_CAPABILITY_MFPC))
927 if (sel & WPA_CIPHER_AES_128_CMAC) {
928 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
929 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
932 wpa_s->mgmt_group_cipher = 0;
933 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
935 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
936 wpa_s->mgmt_group_cipher);
937 #endif /* CONFIG_IEEE80211W */
939 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
940 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
945 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
946 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
948 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
955 * wpa_supplicant_associate - Request association
956 * @wpa_s: Pointer to wpa_supplicant data
957 * @bss: Scan results for the selected BSS, or %NULL if not available
958 * @ssid: Configuration data for the selected network
960 * This function is used to request %wpa_supplicant to associate with a BSS.
962 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
963 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
967 int use_crypt, ret, i, bssid_changed;
968 int algs = AUTH_ALG_OPEN_SYSTEM;
969 wpa_cipher cipher_pairwise, cipher_group;
970 struct wpa_driver_associate_params params;
971 int wep_keys_set = 0;
972 struct wpa_driver_capa capa;
973 int assoc_failed = 0;
974 struct wpa_ssid *old_ssid;
976 if (ssid->mode == 2) {
978 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
979 wpa_printf(MSG_INFO, "Driver does not support AP "
983 wpa_supplicant_create_ap(wpa_s, ssid);
984 #else /* CONFIG_AP */
985 wpa_printf(MSG_ERROR, "AP mode support not included in the "
987 #endif /* CONFIG_AP */
991 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
992 sme_authenticate(wpa_s, bss, ssid);
996 wpa_s->reassociate = 0;
998 #ifdef CONFIG_IEEE80211R
1000 #endif /* CONFIG_IEEE80211R */
1001 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1002 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1003 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1004 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
1005 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1006 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1007 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1009 wpas_notify_bssid_changed(wpa_s);
1010 #ifdef CONFIG_IEEE80211R
1011 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1012 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1014 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
1016 /* Prepare for the next transition */
1017 wpa_ft_prepare_auth_request(wpa_s->wpa);
1019 #endif /* CONFIG_IEEE80211R */
1021 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1022 wpa_s->conf->ap_scan == 2 &&
1023 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1024 /* Use ap_scan==1 style network selection to find the network
1026 wpa_s->scan_req = 2;
1027 wpa_s->reassociate = 1;
1028 wpa_supplicant_req_scan(wpa_s, 0, 0);
1030 #endif /* CONFIG_WPS */
1032 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1033 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1034 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1036 wpa_supplicant_cancel_scan(wpa_s);
1038 /* Starting new association, so clear the possibly used WPA IE from the
1039 * previous association. */
1040 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1042 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
1043 wpa_printf(MSG_WARNING, "Failed to set operating mode");
1047 #ifdef IEEE8021X_EAPOL
1048 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1050 if (ssid->non_leap == 0)
1051 algs = AUTH_ALG_LEAP;
1053 algs |= AUTH_ALG_LEAP;
1056 #endif /* IEEE8021X_EAPOL */
1057 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1058 if (ssid->auth_alg) {
1060 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1061 algs |= AUTH_ALG_OPEN_SYSTEM;
1062 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1063 algs |= AUTH_ALG_SHARED_KEY;
1064 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1065 algs |= AUTH_ALG_LEAP;
1066 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1069 wpa_drv_set_auth_alg(wpa_s, algs);
1071 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1072 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1073 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1074 WPA_KEY_MGMT_FT_IEEE8021X |
1075 WPA_KEY_MGMT_FT_PSK |
1076 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1077 WPA_KEY_MGMT_PSK_SHA256))) {
1078 int try_opportunistic;
1079 try_opportunistic = ssid->proactive_key_caching &&
1080 (ssid->proto & WPA_PROTO_RSN);
1081 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1082 wpa_s->current_ssid,
1083 try_opportunistic) == 0)
1084 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1085 wpa_ie_len = sizeof(wpa_ie);
1086 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1087 wpa_ie, &wpa_ie_len)) {
1088 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1089 "management and encryption suites");
1092 } else if (ssid->key_mgmt &
1093 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1094 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1095 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1096 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1097 wpa_ie_len = sizeof(wpa_ie);
1098 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1099 wpa_ie, &wpa_ie_len)) {
1100 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1101 "management and encryption suites (no scan "
1106 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1107 struct wpabuf *wps_ie;
1108 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1109 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1110 wpa_ie_len = wpabuf_len(wps_ie);
1111 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1114 wpabuf_free(wps_ie);
1115 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1116 #endif /* CONFIG_WPS */
1118 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1122 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1124 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1125 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1126 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1127 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1128 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1130 if (wpa_set_wep_keys(wpa_s, ssid)) {
1135 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1138 #ifdef IEEE8021X_EAPOL
1139 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1140 if ((ssid->eapol_flags &
1141 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1142 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1146 /* Assume that dynamic WEP-104 keys will be used and
1147 * set cipher suites in order for drivers to expect
1149 cipher_pairwise = cipher_group = CIPHER_WEP104;
1152 #endif /* IEEE8021X_EAPOL */
1154 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1155 /* Set the key before (and later after) association */
1156 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1159 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1160 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1161 os_memset(¶ms, 0, sizeof(params));
1163 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1164 params.bssid = bss->bssid;
1165 params.ssid = ie ? ie + 2 : (u8 *) "";
1166 params.ssid_len = ie ? ie[1] : 0;
1167 params.freq = bss->freq;
1169 params.ssid = ssid->ssid;
1170 params.ssid_len = ssid->ssid_len;
1172 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1173 params.freq = ssid->frequency; /* Initial channel for IBSS */
1174 params.wpa_ie = wpa_ie;
1175 params.wpa_ie_len = wpa_ie_len;
1176 params.pairwise_suite = cipher_pairwise;
1177 params.group_suite = cipher_group;
1178 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1179 params.auth_alg = algs;
1180 params.mode = ssid->mode;
1181 for (i = 0; i < NUM_WEP_KEYS; i++) {
1182 if (ssid->wep_key_len[i])
1183 params.wep_key[i] = ssid->wep_key[i];
1184 params.wep_key_len[i] = ssid->wep_key_len[i];
1186 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1188 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1189 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1190 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1191 params.passphrase = ssid->passphrase;
1193 params.psk = ssid->psk;
1196 params.drop_unencrypted = use_crypt;
1198 #ifdef CONFIG_IEEE80211W
1199 switch (ssid->ieee80211w) {
1201 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1203 case IEEE80211W_OPTIONAL:
1204 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1206 case IEEE80211W_REQUIRED:
1207 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1210 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1211 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1212 struct wpa_ie_data ie;
1213 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1215 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1216 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1218 params.mgmt_frame_protection =
1219 MGMT_FRAME_PROTECTION_REQUIRED;
1222 #endif /* CONFIG_IEEE80211W */
1224 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1225 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1227 ret = wpa_drv_associate(wpa_s, ¶ms);
1229 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1231 /* try to continue anyway; new association will be tried again
1236 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1237 /* Set the key after the association just in case association
1238 * cleared the previously configured key. */
1239 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1240 /* No need to timeout authentication since there is no key
1242 wpa_supplicant_cancel_auth_timeout(wpa_s);
1243 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1244 #ifdef CONFIG_IBSS_RSN
1245 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1246 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1247 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1248 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1250 * RSN IBSS authentication is per-STA and we can disable the
1251 * per-BSSID authentication.
1253 wpa_supplicant_cancel_auth_timeout(wpa_s);
1254 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1255 #endif /* CONFIG_IBSS_RSN */
1257 /* Timeout for IEEE 802.11 authentication and association */
1261 /* give IBSS a bit more time */
1262 timeout = ssid->mode ? 10 : 5;
1263 } else if (wpa_s->conf->ap_scan == 1) {
1264 /* give IBSS a bit more time */
1265 timeout = ssid->mode ? 20 : 10;
1267 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1270 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1271 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1272 /* Set static WEP keys again */
1273 wpa_set_wep_keys(wpa_s, ssid);
1276 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1278 * Do not allow EAP session resumption between different
1279 * network configurations.
1281 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1283 old_ssid = wpa_s->current_ssid;
1284 wpa_s->current_ssid = ssid;
1285 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1286 wpa_supplicant_initiate_eapol(wpa_s);
1287 if (old_ssid != wpa_s->current_ssid)
1288 wpas_notify_network_changed(wpa_s);
1293 * wpa_supplicant_disassociate - Disassociate the current connection
1294 * @wpa_s: Pointer to wpa_supplicant data
1295 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1297 * This function is used to request %wpa_supplicant to disassociate with the
1300 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1303 struct wpa_ssid *old_ssid;
1306 if (!is_zero_ether_addr(wpa_s->bssid)) {
1307 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1308 ieee80211_sta_disassociate(wpa_s, reason_code);
1310 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1311 addr = wpa_s->bssid;
1313 wpa_clear_keys(wpa_s, addr);
1314 wpa_supplicant_mark_disassoc(wpa_s);
1315 old_ssid = wpa_s->current_ssid;
1316 wpa_s->current_ssid = NULL;
1317 wpa_sm_set_config(wpa_s->wpa, NULL);
1318 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1319 if (old_ssid != wpa_s->current_ssid)
1320 wpas_notify_network_changed(wpa_s);
1325 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1326 * @wpa_s: Pointer to wpa_supplicant data
1327 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1329 * This function is used to request %wpa_supplicant to deauthenticate from the
1332 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1335 struct wpa_ssid *old_ssid;
1338 if (!is_zero_ether_addr(wpa_s->bssid)) {
1339 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1340 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1342 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1344 addr = wpa_s->bssid;
1346 wpa_clear_keys(wpa_s, addr);
1347 wpa_supplicant_mark_disassoc(wpa_s);
1348 old_ssid = wpa_s->current_ssid;
1349 wpa_s->current_ssid = NULL;
1350 wpa_sm_set_config(wpa_s->wpa, NULL);
1351 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1352 if (old_ssid != wpa_s->current_ssid)
1353 wpas_notify_network_changed(wpa_s);
1358 * wpa_supplicant_enable_network - Mark a configured network as enabled
1359 * @wpa_s: wpa_supplicant structure for a network interface
1360 * @ssid: wpa_ssid structure for a configured network or %NULL
1362 * Enables the specified network or all networks if no network specified.
1364 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1365 struct wpa_ssid *ssid)
1367 struct wpa_ssid *other_ssid;
1371 other_ssid = wpa_s->conf->ssid;
1372 while (other_ssid) {
1373 if (other_ssid == wpa_s->current_ssid &&
1374 other_ssid->disabled)
1375 wpa_s->reassociate = 1;
1377 was_disabled = other_ssid->disabled;
1379 other_ssid->disabled = 0;
1381 if (was_disabled != other_ssid->disabled)
1382 wpas_notify_network_enabled_changed(
1385 other_ssid = other_ssid->next;
1387 if (wpa_s->reassociate)
1388 wpa_supplicant_req_scan(wpa_s, 0, 0);
1389 } else if (wpa_s->current_ssid == NULL && ssid->disabled) {
1391 * Try to reassociate since there is no current configuration
1392 * and a new network was made available.
1394 wpa_s->reassociate = 1;
1395 wpa_supplicant_req_scan(wpa_s, 0, 0);
1397 was_disabled = ssid->disabled;
1401 if (was_disabled != ssid->disabled)
1402 wpas_notify_network_enabled_changed(wpa_s, ssid);
1408 * wpa_supplicant_disable_network - Mark a configured network as disabled
1409 * @wpa_s: wpa_supplicant structure for a network interface
1410 * @ssid: wpa_ssid structure for a configured network or %NULL
1412 * Disables the specified network or all networks if no network specified.
1414 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1415 struct wpa_ssid *ssid)
1417 struct wpa_ssid *other_ssid;
1421 other_ssid = wpa_s->conf->ssid;
1422 while (other_ssid) {
1423 was_disabled = other_ssid->disabled;
1425 other_ssid->disabled = 1;
1427 if (was_disabled != other_ssid->disabled)
1428 wpas_notify_network_enabled_changed(
1431 other_ssid = other_ssid->next;
1433 if (wpa_s->current_ssid)
1434 wpa_supplicant_disassociate(
1435 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1437 if (ssid == wpa_s->current_ssid)
1438 wpa_supplicant_disassociate(
1439 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1441 was_disabled = ssid->disabled;
1445 if (was_disabled != ssid->disabled)
1446 wpas_notify_network_enabled_changed(wpa_s, ssid);
1452 * wpa_supplicant_select_network - Attempt association with a network
1453 * @wpa_s: wpa_supplicant structure for a network interface
1454 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1456 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1457 struct wpa_ssid *ssid)
1460 struct wpa_ssid *other_ssid;
1462 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1463 wpa_supplicant_disassociate(
1464 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1467 * Mark all other networks disabled or mark all networks enabled if no
1468 * network specified.
1470 other_ssid = wpa_s->conf->ssid;
1471 while (other_ssid) {
1472 int was_disabled = other_ssid->disabled;
1474 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1476 if (was_disabled != other_ssid->disabled)
1477 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1479 other_ssid = other_ssid->next;
1481 wpa_s->disconnected = 0;
1482 wpa_s->reassociate = 1;
1483 wpa_supplicant_req_scan(wpa_s, 0, 0);
1485 wpas_notify_network_selected(wpa_s, ssid);
1490 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1491 * @wpa_s: wpa_supplicant structure for a network interface
1492 * @ap_scan: AP scan mode
1493 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1496 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1501 if (ap_scan < 0 || ap_scan > 2)
1504 old_ap_scan = wpa_s->conf->ap_scan;
1505 wpa_s->conf->ap_scan = ap_scan;
1507 if (old_ap_scan != wpa_s->conf->ap_scan)
1508 wpas_notify_ap_scan_changed(wpa_s);
1515 * wpa_supplicant_set_debug_params - Set global debug params
1516 * @global: wpa_global structure
1517 * @debug_level: debug level
1518 * @debug_timestamp: determines if show timestamp in debug data
1519 * @debug_show_keys: determines if show keys in debug data
1520 * Returns: 0 if succeed or -1 if debug_level has wrong value
1522 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1523 int debug_timestamp, int debug_show_keys)
1526 int old_level, old_timestamp, old_show_keys;
1528 /* check for allowed debuglevels */
1529 if (debug_level != MSG_MSGDUMP &&
1530 debug_level != MSG_DEBUG &&
1531 debug_level != MSG_INFO &&
1532 debug_level != MSG_WARNING &&
1533 debug_level != MSG_ERROR)
1536 old_level = wpa_debug_level;
1537 old_timestamp = wpa_debug_timestamp;
1538 old_show_keys = wpa_debug_show_keys;
1540 wpa_debug_level = debug_level;
1541 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1542 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1544 if (wpa_debug_level != old_level ||
1545 wpa_debug_timestamp != old_timestamp ||
1546 wpa_debug_show_keys != old_show_keys)
1547 wpas_notify_debug_params_changed(global);
1553 static struct wpa_scan_results * wpa_supplicant_get_scan_results_old(
1554 struct wpa_supplicant *wpa_s)
1556 #define SCAN_AP_LIMIT 128
1557 struct wpa_scan_result *results;
1559 struct wpa_scan_results *res;
1561 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1562 if (results == NULL) {
1563 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1568 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1569 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1571 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1575 if (num > SCAN_AP_LIMIT) {
1576 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1577 num, SCAN_AP_LIMIT);
1578 num = SCAN_AP_LIMIT;
1581 /* Convert old scan result data structure to the new one */
1582 res = os_zalloc(sizeof(*res));
1587 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1588 if (res->res == NULL) {
1594 for (i = 0; i < num; i++) {
1595 struct wpa_scan_result *bss = &results[i];
1596 struct wpa_scan_res *r;
1600 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1603 if (bss->mdie_present)
1606 r = os_zalloc(sizeof(*r) + ie_len);
1610 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1611 r->freq = bss->freq;
1612 r->caps = bss->caps;
1613 r->qual = bss->qual;
1614 r->noise = bss->noise;
1615 r->level = bss->level;
1619 pos = (u8 *) (r + 1);
1622 *pos++ = WLAN_EID_SSID;
1623 *pos++ = bss->ssid_len;
1624 os_memcpy(pos, bss->ssid, bss->ssid_len);
1625 pos += bss->ssid_len;
1628 /* Fake Supported Rate IE to include max rate */
1629 *pos++ = WLAN_EID_SUPP_RATES;
1631 *pos++ = bss->maxrate;
1634 if (bss->rsn_ie_len) {
1635 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1636 pos += bss->rsn_ie_len;
1639 if (bss->mdie_present) {
1640 os_memcpy(pos, bss->mdie, 5);
1644 if (bss->wpa_ie_len) {
1645 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1646 pos += bss->wpa_ie_len;
1649 res->res[res->num++] = r;
1659 * wpa_supplicant_get_scan_results - Get scan results
1660 * @wpa_s: Pointer to wpa_supplicant data
1661 * Returns: 0 on success, -1 on failure
1663 * This function is request the current scan results from the driver and stores
1664 * a local copy of the results in wpa_s->scan_res.
1666 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1670 wpa_scan_results_free(wpa_s->scan_res);
1671 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1672 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1673 else if (wpa_s->driver->get_scan_results2 == NULL)
1674 wpa_s->scan_res = wpa_supplicant_get_scan_results_old(wpa_s);
1676 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1677 if (wpa_s->scan_res == NULL) {
1678 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1682 wpa_scan_sort_results(wpa_s->scan_res);
1690 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1691 * @wpa_s: Pointer to wpa_supplicant data
1692 * Returns: A pointer to the current network structure or %NULL on failure
1694 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1696 struct wpa_ssid *entry;
1697 u8 ssid[MAX_SSID_LEN];
1703 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1704 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1705 wpa_printf(MSG_WARNING, "Could not read SSID from "
1710 res = wpa_drv_get_ssid(wpa_s, ssid);
1712 wpa_printf(MSG_WARNING, "Could not read SSID from "
1719 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1720 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1721 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1722 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1726 wired = wpa_s->conf->ap_scan == 0 &&
1727 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1729 entry = wpa_s->conf->ssid;
1731 if (!entry->disabled &&
1732 ((ssid_len == entry->ssid_len &&
1733 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1734 (!entry->bssid_set ||
1735 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1738 if (!entry->disabled &&
1739 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1740 (entry->ssid == NULL || entry->ssid_len == 0) &&
1741 (!entry->bssid_set ||
1742 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1744 #endif /* CONFIG_WPS */
1745 entry = entry->next;
1752 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1762 if (wpa_drivers[0] == NULL) {
1763 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1769 /* default to first driver in the list */
1770 wpa_s->driver = wpa_drivers[0];
1774 pos = os_strchr(name, ',');
1778 len = os_strlen(name);
1779 for (i = 0; wpa_drivers[i]; i++) {
1780 if (os_strlen(wpa_drivers[i]->name) == len &&
1781 os_strncmp(name, wpa_drivers[i]->name, len) ==
1783 wpa_s->driver = wpa_drivers[i];
1788 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1793 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1794 const u8 *buf, size_t len)
1796 struct wpa_supplicant *wpa_s = ctx;
1798 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1799 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1801 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1803 * There is possible race condition between receiving the
1804 * association event and the EAPOL frame since they are coming
1805 * through different paths from the driver. In order to avoid
1806 * issues in trying to process the EAPOL frame before receiving
1807 * association information, lets queue it for processing until
1808 * the association event is received.
1810 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1811 "received EAPOL frame");
1812 wpabuf_free(wpa_s->pending_eapol_rx);
1813 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1814 if (wpa_s->pending_eapol_rx) {
1815 os_get_time(&wpa_s->pending_eapol_rx_time);
1816 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1823 if (wpa_s->ap_iface) {
1824 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1827 #endif /* CONFIG_AP */
1829 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1830 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1831 "no key management is configured");
1835 if (wpa_s->eapol_received == 0 &&
1836 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1837 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1838 wpa_s->wpa_state != WPA_COMPLETED)) {
1839 /* Timeout for completing IEEE 802.1X and WPA authentication */
1840 wpa_supplicant_req_auth_timeout(
1842 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1843 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1844 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1847 wpa_s->eapol_received++;
1849 if (wpa_s->countermeasures) {
1850 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1855 #ifdef CONFIG_IBSS_RSN
1856 if (wpa_s->current_ssid &&
1857 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1858 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1861 #endif /* CONFIG_IBSS_RSN */
1863 /* Source address of the incoming EAPOL frame could be compared to the
1864 * current BSSID. However, it is possible that a centralized
1865 * Authenticator could be using another MAC address than the BSSID of
1866 * an AP, so just allow any address to be used for now. The replies are
1867 * still sent to the current BSSID (if available), though. */
1869 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1870 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1871 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1873 wpa_drv_poll(wpa_s);
1874 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1875 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1876 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1878 * Set portValid = TRUE here since we are going to skip 4-way
1879 * handshake processing which would normally set portValid. We
1880 * need this to allow the EAPOL state machines to be completed
1881 * without going through EAPOL-Key handshake.
1883 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1888 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1889 struct ieee80211_rx_status *rx_status)
1891 struct wpa_supplicant *wpa_s = ctx;
1892 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1897 * wpa_supplicant_driver_init - Initialize driver interface parameters
1898 * @wpa_s: Pointer to wpa_supplicant data
1899 * Returns: 0 on success, -1 on failure
1901 * This function is called to initialize driver interface parameters.
1902 * wpa_drv_init() must have been called before this function to initialize the
1905 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1907 static int interface_count = 0;
1909 if (wpa_s->driver->send_eapol) {
1910 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1912 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1914 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1915 wpa_drv_get_mac_addr(wpa_s),
1917 wpa_supplicant_rx_eapol, wpa_s, 0);
1918 if (wpa_s->l2 == NULL)
1922 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1923 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1927 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1928 MAC2STR(wpa_s->own_addr));
1930 if (wpa_s->bridge_ifname[0]) {
1931 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1932 " '%s'", wpa_s->bridge_ifname);
1933 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1936 wpa_supplicant_rx_eapol, wpa_s,
1938 if (wpa_s->l2_br == NULL) {
1939 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1940 "connection for the bridge interface '%s'",
1941 wpa_s->bridge_ifname);
1946 /* Backwards compatibility call to set_wpa() handler. This is called
1947 * only just after init and just before deinit, so these handler can be
1948 * used to implement same functionality. */
1949 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1950 struct wpa_driver_capa capa;
1951 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1952 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1953 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1954 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1955 /* Continue to allow non-WPA modes to be used. */
1957 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1963 wpa_clear_keys(wpa_s, NULL);
1965 /* Make sure that TKIP countermeasures are not left enabled (could
1966 * happen if wpa_supplicant is killed during countermeasures. */
1967 wpa_drv_set_countermeasures(wpa_s, 0);
1969 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1971 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1972 wpa_drv_flush_pmkid(wpa_s);
1974 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1975 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1982 static int wpa_supplicant_daemon(const char *pid_file)
1984 wpa_printf(MSG_DEBUG, "Daemonize..");
1985 return os_daemonize(pid_file);
1989 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1991 struct wpa_supplicant *wpa_s;
1993 wpa_s = os_zalloc(sizeof(*wpa_s));
1996 wpa_s->scan_req = 1;
2002 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2003 struct wpa_interface *iface)
2005 const char *ifname, *driver;
2006 struct wpa_driver_capa capa;
2008 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2009 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2010 iface->confname ? iface->confname : "N/A",
2011 iface->driver ? iface->driver : "default",
2012 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2013 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2015 if (iface->confname) {
2016 #ifdef CONFIG_BACKEND_FILE
2017 wpa_s->confname = os_rel2abs_path(iface->confname);
2018 if (wpa_s->confname == NULL) {
2019 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2020 "for configuration file '%s'.",
2024 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2025 iface->confname, wpa_s->confname);
2026 #else /* CONFIG_BACKEND_FILE */
2027 wpa_s->confname = os_strdup(iface->confname);
2028 #endif /* CONFIG_BACKEND_FILE */
2029 wpa_s->conf = wpa_config_read(wpa_s->confname);
2030 if (wpa_s->conf == NULL) {
2031 wpa_printf(MSG_ERROR, "Failed to read or parse "
2032 "configuration '%s'.", wpa_s->confname);
2037 * Override ctrl_interface and driver_param if set on command
2040 if (iface->ctrl_interface) {
2041 os_free(wpa_s->conf->ctrl_interface);
2042 wpa_s->conf->ctrl_interface =
2043 os_strdup(iface->ctrl_interface);
2046 if (iface->driver_param) {
2047 os_free(wpa_s->conf->driver_param);
2048 wpa_s->conf->driver_param =
2049 os_strdup(iface->driver_param);
2052 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2053 iface->driver_param);
2055 if (wpa_s->conf == NULL) {
2056 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2060 if (iface->ifname == NULL) {
2061 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2064 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2065 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2069 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2071 if (iface->bridge_ifname) {
2072 if (os_strlen(iface->bridge_ifname) >=
2073 sizeof(wpa_s->bridge_ifname)) {
2074 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2075 "name '%s'.", iface->bridge_ifname);
2078 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2079 sizeof(wpa_s->bridge_ifname));
2082 /* RSNA Supplicant Key Management - INITIALIZE */
2083 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2084 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2086 /* Initialize driver interface and register driver event handler before
2087 * L2 receive handler so that association events are processed before
2088 * EAPOL-Key packets if both become available for the same select()
2090 driver = iface->driver;
2092 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2095 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2096 if (wpa_s->drv_priv == NULL) {
2098 pos = os_strchr(driver, ',');
2100 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2101 "interface - try next driver wrapper");
2105 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2108 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2109 wpa_printf(MSG_ERROR, "Driver interface rejected "
2110 "driver_param '%s'", wpa_s->conf->driver_param);
2114 ifname = wpa_drv_get_ifname(wpa_s);
2115 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2116 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2117 "name with '%s'", ifname);
2118 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2121 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2124 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2125 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2127 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2129 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2130 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2131 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2132 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2133 "dot11RSNAConfigPMKLifetime");
2137 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2138 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2139 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2140 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2141 "dot11RSNAConfigPMKReauthThreshold");
2145 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2146 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2147 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2148 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2149 "dot11RSNAConfigSATimeout");
2153 if (wpa_supplicant_driver_init(wpa_s) < 0)
2156 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2157 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2158 wpa_printf(MSG_DEBUG, "Failed to set country");
2162 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2164 if (wpas_wps_init(wpa_s))
2167 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2169 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2171 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2172 if (wpa_s->ctrl_iface == NULL) {
2173 wpa_printf(MSG_ERROR,
2174 "Failed to initialize control interface '%s'.\n"
2175 "You may have another wpa_supplicant process "
2176 "already running or the file was\n"
2177 "left by an unclean termination of wpa_supplicant "
2178 "in which case you will need\n"
2179 "to manually remove this file before starting "
2180 "wpa_supplicant again.\n",
2181 wpa_s->conf->ctrl_interface);
2185 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2186 wpa_s->drv_flags = capa.flags;
2187 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2188 if (ieee80211_sta_init(wpa_s))
2191 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2194 #ifdef CONFIG_IBSS_RSN
2195 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2196 if (!wpa_s->ibss_rsn) {
2197 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2200 #endif /* CONFIG_IBSS_RSN */
2206 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2208 if (wpa_s->drv_priv) {
2209 wpa_supplicant_deauthenticate(wpa_s,
2210 WLAN_REASON_DEAUTH_LEAVING);
2212 /* Backwards compatibility call to set_wpa() handler. This is
2213 * called only just after init and just before deinit, so these
2214 * handler can be used to implement same functionality. */
2215 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
2216 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
2220 wpa_drv_set_drop_unencrypted(wpa_s, 0);
2221 wpa_drv_set_countermeasures(wpa_s, 0);
2222 wpa_clear_keys(wpa_s, NULL);
2225 wpas_notify_unregister_interface(wpa_s);
2227 wpa_supplicant_cleanup(wpa_s);
2229 if (wpa_s->drv_priv)
2230 wpa_drv_deinit(wpa_s);
2235 * wpa_supplicant_add_iface - Add a new network interface
2236 * @global: Pointer to global data from wpa_supplicant_init()
2237 * @iface: Interface configuration options
2238 * Returns: Pointer to the created interface or %NULL on failure
2240 * This function is used to add new network interfaces for %wpa_supplicant.
2241 * This can be called before wpa_supplicant_run() to add interfaces before the
2242 * main event loop has been started. In addition, new interfaces can be added
2243 * dynamically while %wpa_supplicant is already running. This could happen,
2244 * e.g., when a hotplug network adapter is inserted.
2246 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2247 struct wpa_interface *iface)
2249 struct wpa_supplicant *wpa_s;
2251 if (global == NULL || iface == NULL)
2254 wpa_s = wpa_supplicant_alloc();
2258 if (wpa_supplicant_init_iface(wpa_s, iface)) {
2259 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2261 wpa_supplicant_deinit_iface(wpa_s);
2266 wpa_s->global = global;
2268 /* Register the interface with the dbus control interface */
2269 if (wpas_dbus_register_iface(wpa_s)) {
2270 wpa_supplicant_deinit_iface(wpa_s);
2275 wpa_s->next = global->ifaces;
2276 global->ifaces = wpa_s;
2278 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2285 * wpa_supplicant_remove_iface - Remove a network interface
2286 * @global: Pointer to global data from wpa_supplicant_init()
2287 * @wpa_s: Pointer to the network interface to be removed
2288 * Returns: 0 if interface was removed, -1 if interface was not found
2290 * This function can be used to dynamically remove network interfaces from
2291 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2292 * addition, this function is used to remove all remaining interfaces when
2293 * %wpa_supplicant is terminated.
2295 int wpa_supplicant_remove_iface(struct wpa_global *global,
2296 struct wpa_supplicant *wpa_s)
2298 struct wpa_supplicant *prev;
2300 /* Remove interface from the global list of interfaces */
2301 prev = global->ifaces;
2302 if (prev == wpa_s) {
2303 global->ifaces = wpa_s->next;
2305 while (prev && prev->next != wpa_s)
2309 prev->next = wpa_s->next;
2312 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2314 wpa_supplicant_deinit_iface(wpa_s);
2322 * wpa_supplicant_get_iface - Get a new network interface
2323 * @global: Pointer to global data from wpa_supplicant_init()
2324 * @ifname: Interface name
2325 * Returns: Pointer to the interface or %NULL if not found
2327 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2330 struct wpa_supplicant *wpa_s;
2332 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2333 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2341 * wpa_supplicant_init - Initialize %wpa_supplicant
2342 * @params: Parameters for %wpa_supplicant
2343 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2345 * This function is used to initialize %wpa_supplicant. After successful
2346 * initialization, the returned data pointer can be used to add and remove
2347 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2349 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2351 struct wpa_global *global;
2357 wpa_debug_open_file(params->wpa_debug_file_path);
2358 if (params->wpa_debug_syslog)
2359 wpa_debug_open_syslog();
2361 ret = eap_peer_register_methods();
2363 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2365 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2366 "the same EAP type.");
2371 ret = eap_server_register_methods();
2373 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2375 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2376 "the same EAP type.");
2379 #endif /* CONFIG_AP */
2381 global = os_zalloc(sizeof(*global));
2384 global->params.daemonize = params->daemonize;
2385 global->params.wait_for_monitor = params->wait_for_monitor;
2386 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2387 if (params->pid_file)
2388 global->params.pid_file = os_strdup(params->pid_file);
2389 if (params->ctrl_interface)
2390 global->params.ctrl_interface =
2391 os_strdup(params->ctrl_interface);
2392 wpa_debug_level = global->params.wpa_debug_level =
2393 params->wpa_debug_level;
2394 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2395 params->wpa_debug_show_keys;
2396 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2397 params->wpa_debug_timestamp;
2399 if (eloop_init(global)) {
2400 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2401 wpa_supplicant_deinit(global);
2405 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2406 if (global->ctrl_iface == NULL) {
2407 wpa_supplicant_deinit(global);
2411 if (global->params.dbus_ctrl_interface) {
2412 global->dbus_ctrl_iface =
2413 wpa_supplicant_dbus_ctrl_iface_init(global);
2414 if (global->dbus_ctrl_iface == NULL) {
2415 wpa_supplicant_deinit(global);
2420 for (i = 0; wpa_drivers[i]; i++)
2421 global->drv_count++;
2422 if (global->drv_count == 0) {
2423 wpa_printf(MSG_ERROR, "No drivers enabled");
2424 wpa_supplicant_deinit(global);
2427 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2428 if (global->drv_priv == NULL) {
2429 wpa_supplicant_deinit(global);
2432 for (i = 0; wpa_drivers[i]; i++) {
2433 if (!wpa_drivers[i]->global_init)
2435 global->drv_priv[i] = wpa_drivers[i]->global_init();
2436 if (global->drv_priv[i] == NULL) {
2437 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2438 "'%s'", wpa_drivers[i]->name);
2439 wpa_supplicant_deinit(global);
2449 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2450 * @global: Pointer to global data from wpa_supplicant_init()
2451 * Returns: 0 after successful event loop run, -1 on failure
2453 * This function starts the main event loop and continues running as long as
2454 * there are any remaining events. In most cases, this function is running as
2455 * long as the %wpa_supplicant process in still in use.
2457 int wpa_supplicant_run(struct wpa_global *global)
2459 struct wpa_supplicant *wpa_s;
2461 if (global->params.daemonize &&
2462 wpa_supplicant_daemon(global->params.pid_file))
2465 if (global->params.wait_for_monitor) {
2466 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2467 if (wpa_s->ctrl_iface)
2468 wpa_supplicant_ctrl_iface_wait(
2472 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2473 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2482 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2483 * @global: Pointer to global data from wpa_supplicant_init()
2485 * This function is called to deinitialize %wpa_supplicant and to free all
2486 * allocated resources. Remaining network interfaces will also be removed.
2488 void wpa_supplicant_deinit(struct wpa_global *global)
2495 while (global->ifaces)
2496 wpa_supplicant_remove_iface(global, global->ifaces);
2498 if (global->ctrl_iface)
2499 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2500 if (global->dbus_ctrl_iface)
2501 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2503 eap_peer_unregister_methods();
2505 eap_server_unregister_methods();
2506 #endif /* CONFIG_AP */
2508 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2509 if (!global->drv_priv[i])
2511 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2513 os_free(global->drv_priv);
2517 if (global->params.pid_file) {
2518 os_daemonize_terminate(global->params.pid_file);
2519 os_free(global->params.pid_file);
2521 os_free(global->params.ctrl_interface);
2524 wpa_debug_close_syslog();
2525 wpa_debug_close_file();