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 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
124 struct wpa_ssid *ssid)
128 for (i = 0; i < NUM_WEP_KEYS; i++) {
129 if (ssid->wep_key_len[i] == 0)
133 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
134 (u8 *) "\xff\xff\xff\xff\xff\xff",
135 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
136 ssid->wep_key[i], ssid->wep_key_len[i]);
143 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
144 struct wpa_ssid *ssid)
151 /* IBSS/WPA-None uses only one key (Group) for both receiving and
152 * sending unicast and multicast packets. */
154 if (ssid->mode != IEEE80211_MODE_IBSS) {
155 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
156 "for WPA-None", ssid->mode);
160 if (!ssid->psk_set) {
161 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
165 switch (wpa_s->group_cipher) {
166 case WPA_CIPHER_CCMP:
167 os_memcpy(key, ssid->psk, 16);
171 case WPA_CIPHER_TKIP:
172 /* WPA-None uses the same Michael MIC key for both TX and RX */
173 os_memcpy(key, ssid->psk, 16 + 8);
174 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
179 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
180 "WPA-None", wpa_s->group_cipher);
184 /* TODO: should actually remember the previously used seq#, both for TX
185 * and RX from each STA.. */
187 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
188 0, 1, seq, 6, key, keylen);
192 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
194 struct wpa_supplicant *wpa_s = eloop_ctx;
195 const u8 *bssid = wpa_s->bssid;
196 if (is_zero_ether_addr(bssid))
197 bssid = wpa_s->pending_bssid;
198 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
200 wpa_blacklist_add(wpa_s, bssid);
201 wpa_sm_notify_disassoc(wpa_s->wpa);
202 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
203 wpa_s->reassociate = 1;
204 wpa_supplicant_req_scan(wpa_s, 0, 0);
209 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
210 * @wpa_s: Pointer to wpa_supplicant data
211 * @sec: Number of seconds after which to time out authentication
212 * @usec: Number of microseconds after which to time out authentication
214 * This function is used to schedule a timeout for the current authentication
217 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
220 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
221 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
224 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
225 "%d usec", sec, usec);
226 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
227 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
232 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
233 * @wpa_s: Pointer to wpa_supplicant data
235 * This function is used to cancel authentication timeout scheduled with
236 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
239 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
241 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
242 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
243 wpa_blacklist_del(wpa_s, wpa_s->bssid);
248 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
249 * @wpa_s: Pointer to wpa_supplicant data
251 * This function is used to configure EAPOL state machine based on the selected
252 * authentication mode.
254 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
256 #ifdef IEEE8021X_EAPOL
257 struct eapol_config eapol_conf;
258 struct wpa_ssid *ssid = wpa_s->current_ssid;
260 #ifdef CONFIG_IBSS_RSN
261 if (ssid->mode == IEEE80211_MODE_IBSS &&
262 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
263 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
265 * RSN IBSS authentication is per-STA and we can disable the
266 * per-BSSID EAPOL authentication.
268 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
269 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
270 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
273 #endif /* CONFIG_IBSS_RSN */
275 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
276 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
278 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
279 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
280 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
282 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
284 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
285 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
286 eapol_conf.accept_802_1x_keys = 1;
287 eapol_conf.required_keys = 0;
288 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
289 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
291 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
292 eapol_conf.required_keys |=
293 EAPOL_REQUIRE_KEY_BROADCAST;
296 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
297 eapol_conf.required_keys = 0;
300 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
301 eapol_conf.workaround = ssid->eap_workaround;
302 eapol_conf.eap_disabled =
303 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
304 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
305 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
306 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
307 #endif /* IEEE8021X_EAPOL */
312 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
313 * @wpa_s: Pointer to wpa_supplicant data
314 * @ssid: Configuration data for the network
316 * This function is used to configure WPA state machine and related parameters
317 * to a mode where WPA is not enabled. This is called as part of the
318 * authentication configuration when the selected network does not use WPA.
320 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
321 struct wpa_ssid *ssid)
325 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
326 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
327 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
328 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
330 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
331 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
332 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
333 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
334 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
335 wpa_s->group_cipher = WPA_CIPHER_NONE;
336 wpa_s->mgmt_group_cipher = 0;
338 for (i = 0; i < NUM_WEP_KEYS; i++) {
339 if (ssid->wep_key_len[i] > 5) {
340 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
341 wpa_s->group_cipher = WPA_CIPHER_WEP104;
343 } else if (ssid->wep_key_len[i] > 0) {
344 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
345 wpa_s->group_cipher = WPA_CIPHER_WEP40;
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
353 wpa_s->pairwise_cipher);
354 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
355 #ifdef CONFIG_IEEE80211W
356 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
357 wpa_s->mgmt_group_cipher);
358 #endif /* CONFIG_IEEE80211W */
360 pmksa_cache_clear_current(wpa_s->wpa);
364 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
366 bgscan_deinit(wpa_s);
367 scard_deinit(wpa_s->scard);
369 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
370 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
371 l2_packet_deinit(wpa_s->l2);
374 l2_packet_deinit(wpa_s->l2_br);
378 if (wpa_s->ctrl_iface) {
379 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
380 wpa_s->ctrl_iface = NULL;
382 if (wpa_s->conf != NULL) {
383 wpa_config_free(wpa_s->conf);
387 os_free(wpa_s->confname);
388 wpa_s->confname = NULL;
390 wpa_sm_set_eapol(wpa_s->wpa, NULL);
391 eapol_sm_deinit(wpa_s->eapol);
394 rsn_preauth_deinit(wpa_s->wpa);
396 pmksa_candidate_free(wpa_s->wpa);
397 wpa_sm_deinit(wpa_s->wpa);
399 wpa_blacklist_clear(wpa_s);
401 wpa_scan_results_free(wpa_s->scan_res);
402 wpa_s->scan_res = NULL;
404 wpa_supplicant_cancel_scan(wpa_s);
405 wpa_supplicant_cancel_auth_timeout(wpa_s);
407 ieee80211_sta_deinit(wpa_s);
409 wpas_wps_deinit(wpa_s);
411 wpabuf_free(wpa_s->pending_eapol_rx);
412 wpa_s->pending_eapol_rx = NULL;
414 #ifdef CONFIG_IBSS_RSN
415 ibss_rsn_deinit(wpa_s->ibss_rsn);
416 wpa_s->ibss_rsn = NULL;
417 #endif /* CONFIG_IBSS_RSN */
420 os_free(wpa_s->sme.ft_ies);
421 wpa_s->sme.ft_ies = NULL;
422 wpa_s->sme.ft_ies_len = 0;
423 #endif /* CONFIG_SME */
426 wpa_supplicant_ap_deinit(wpa_s);
427 #endif /* CONFIG_AP */
432 * wpa_clear_keys - Clear keys configured for the driver
433 * @wpa_s: Pointer to wpa_supplicant data
434 * @addr: Previously used BSSID or %NULL if not available
436 * This function clears the encryption keys that has been previously configured
439 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
441 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
443 if (wpa_s->keys_cleared) {
444 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
445 * timing issues with keys being cleared just before new keys
446 * are set or just after association or something similar. This
447 * shows up in group key handshake failing often because of the
448 * client not receiving the first encrypted packets correctly.
449 * Skipping some of the extra key clearing steps seems to help
450 * in completing group key handshake more reliably. */
451 wpa_printf(MSG_DEBUG, "No keys have been configured - "
452 "skip key clearing");
456 /* MLME-DELETEKEYS.request */
457 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
458 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
459 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
460 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
462 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
464 /* MLME-SETPROTECTION.request(None) */
465 wpa_drv_mlme_setprotection(
467 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
468 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
470 wpa_s->keys_cleared = 1;
475 * wpa_supplicant_state_txt - Get the connection state name as a text string
476 * @state: State (wpa_state; WPA_*)
477 * Returns: The state name as a printable text string
479 const char * wpa_supplicant_state_txt(int state)
482 case WPA_DISCONNECTED:
483 return "DISCONNECTED";
488 case WPA_AUTHENTICATING:
489 return "AUTHENTICATING";
490 case WPA_ASSOCIATING:
491 return "ASSOCIATING";
494 case WPA_4WAY_HANDSHAKE:
495 return "4WAY_HANDSHAKE";
496 case WPA_GROUP_HANDSHAKE:
497 return "GROUP_HANDSHAKE";
507 * wpa_supplicant_set_state - Set current connection state
508 * @wpa_s: Pointer to wpa_supplicant data
509 * @state: The new connection state
511 * This function is called whenever the connection state changes, e.g.,
512 * association is completed for WPA/WPA2 4-Way Handshake is started.
514 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
516 wpa_printf(MSG_DEBUG, "State: %s -> %s",
517 wpa_supplicant_state_txt(wpa_s->wpa_state),
518 wpa_supplicant_state_txt(state));
520 if (state != WPA_SCANNING)
521 wpa_supplicant_notify_scanning(wpa_s, 0);
523 wpas_notify_state_changed(wpa_s, state, wpa_s->wpa_state);
525 if (state == WPA_COMPLETED && wpa_s->new_connection) {
526 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
527 struct wpa_ssid *ssid = wpa_s->current_ssid;
528 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
529 MACSTR " completed %s [id=%d id_str=%s]",
530 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
531 "(reauth)" : "(auth)",
532 ssid ? ssid->id : -1,
533 ssid && ssid->id_str ? ssid->id_str : "");
534 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
535 wpa_s->new_connection = 0;
536 wpa_s->reassociated_connection = 1;
537 wpa_drv_set_operstate(wpa_s, 1);
538 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
539 state == WPA_ASSOCIATED) {
540 wpa_s->new_connection = 1;
541 wpa_drv_set_operstate(wpa_s, 0);
543 wpa_s->wpa_state = state;
547 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
550 struct wpa_global *global = eloop_ctx;
551 struct wpa_supplicant *wpa_s;
552 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
553 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
560 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
562 wpa_states old_state = wpa_s->wpa_state;
563 wpa_s->pairwise_cipher = 0;
564 wpa_s->group_cipher = 0;
565 wpa_s->mgmt_group_cipher = 0;
567 wpa_s->wpa_state = WPA_DISCONNECTED;
568 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
573 * wpa_supplicant_reload_configuration - Reload configuration data
574 * @wpa_s: Pointer to wpa_supplicant data
575 * Returns: 0 on success or -1 if configuration parsing failed
577 * This function can be used to request that the configuration data is reloaded
578 * (e.g., after configuration file change). This function is reloading
579 * configuration only for one interface, so this may need to be called multiple
580 * times if %wpa_supplicant is controlling multiple interfaces and all
581 * interfaces need reconfiguration.
583 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
585 struct wpa_config *conf;
586 struct wpa_ssid *old_ssid;
590 if (wpa_s->confname == NULL)
592 conf = wpa_config_read(wpa_s->confname);
594 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
595 "file '%s' - exiting", wpa_s->confname);
599 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
600 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
601 os_strcmp(conf->ctrl_interface,
602 wpa_s->conf->ctrl_interface) != 0);
604 if (reconf_ctrl && wpa_s->ctrl_iface) {
605 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
606 wpa_s->ctrl_iface = NULL;
609 eapol_sm_invalidate_cached_session(wpa_s->eapol);
610 old_ssid = wpa_s->current_ssid;
611 wpa_s->current_ssid = NULL;
612 if (old_ssid != wpa_s->current_ssid)
613 wpas_notify_network_changed(wpa_s);
616 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
617 * pkcs11_engine_path, pkcs11_module_path.
619 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
621 * Clear forced success to clear EAP state for next
624 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
626 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
627 wpa_sm_set_config(wpa_s->wpa, NULL);
628 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
629 rsn_preauth_deinit(wpa_s->wpa);
631 old_ap_scan = wpa_s->conf->ap_scan;
632 wpa_config_free(wpa_s->conf);
634 if (old_ap_scan != wpa_s->conf->ap_scan)
635 wpas_notify_ap_scan_changed(wpa_s);
638 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
640 wpa_supplicant_clear_status(wpa_s);
641 wpa_s->reassociate = 1;
642 wpa_supplicant_req_scan(wpa_s, 0, 0);
643 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
648 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
651 struct wpa_global *global = eloop_ctx;
652 struct wpa_supplicant *wpa_s;
653 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
654 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
655 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
662 static wpa_cipher cipher_suite2driver(int cipher)
665 case WPA_CIPHER_NONE:
667 case WPA_CIPHER_WEP40:
669 case WPA_CIPHER_WEP104:
670 return CIPHER_WEP104;
671 case WPA_CIPHER_CCMP:
673 case WPA_CIPHER_TKIP:
680 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
683 case WPA_KEY_MGMT_NONE:
684 return KEY_MGMT_NONE;
685 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
686 return KEY_MGMT_802_1X_NO_WPA;
687 case WPA_KEY_MGMT_IEEE8021X:
688 return KEY_MGMT_802_1X;
689 case WPA_KEY_MGMT_WPA_NONE:
690 return KEY_MGMT_WPA_NONE;
691 case WPA_KEY_MGMT_FT_IEEE8021X:
692 return KEY_MGMT_FT_802_1X;
693 case WPA_KEY_MGMT_FT_PSK:
694 return KEY_MGMT_FT_PSK;
695 case WPA_KEY_MGMT_IEEE8021X_SHA256:
696 return KEY_MGMT_802_1X_SHA256;
697 case WPA_KEY_MGMT_PSK_SHA256:
698 return KEY_MGMT_PSK_SHA256;
699 case WPA_KEY_MGMT_WPS:
701 case WPA_KEY_MGMT_PSK:
708 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
709 struct wpa_ssid *ssid,
710 struct wpa_ie_data *ie)
712 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
715 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
716 "from association info");
721 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
723 if (!(ie->group_cipher & ssid->group_cipher)) {
724 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
725 "cipher 0x%x (mask 0x%x) - reject",
726 ie->group_cipher, ssid->group_cipher);
729 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
730 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
731 "cipher 0x%x (mask 0x%x) - reject",
732 ie->pairwise_cipher, ssid->pairwise_cipher);
735 if (!(ie->key_mgmt & ssid->key_mgmt)) {
736 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
737 "management 0x%x (mask 0x%x) - reject",
738 ie->key_mgmt, ssid->key_mgmt);
742 #ifdef CONFIG_IEEE80211W
743 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
744 ssid->ieee80211w == IEEE80211W_REQUIRED) {
745 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
746 "that does not support management frame protection - "
750 #endif /* CONFIG_IEEE80211W */
757 * wpa_supplicant_set_suites - Set authentication and encryption parameters
758 * @wpa_s: Pointer to wpa_supplicant data
759 * @bss: Scan results for the selected BSS, or %NULL if not available
760 * @ssid: Configuration data for the selected network
761 * @wpa_ie: Buffer for the WPA/RSN IE
762 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
763 * used buffer length in case the functions returns success.
764 * Returns: 0 on success or -1 on failure
766 * This function is used to configure authentication and encryption parameters
767 * based on the network configuration and scan result for the selected BSS (if
770 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
771 struct wpa_scan_res *bss,
772 struct wpa_ssid *ssid,
773 u8 *wpa_ie, size_t *wpa_ie_len)
775 struct wpa_ie_data ie;
777 const u8 *bss_wpa, *bss_rsn;
780 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
781 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
783 bss_wpa = bss_rsn = NULL;
785 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
786 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
787 (ie.group_cipher & ssid->group_cipher) &&
788 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
789 (ie.key_mgmt & ssid->key_mgmt)) {
790 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
791 proto = WPA_PROTO_RSN;
792 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
793 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
794 (ie.group_cipher & ssid->group_cipher) &&
795 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
796 (ie.key_mgmt & ssid->key_mgmt)) {
797 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
798 proto = WPA_PROTO_WPA;
800 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
803 if (ssid->proto & WPA_PROTO_RSN)
804 proto = WPA_PROTO_RSN;
806 proto = WPA_PROTO_WPA;
807 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
808 os_memset(&ie, 0, sizeof(ie));
809 ie.group_cipher = ssid->group_cipher;
810 ie.pairwise_cipher = ssid->pairwise_cipher;
811 ie.key_mgmt = ssid->key_mgmt;
812 #ifdef CONFIG_IEEE80211W
813 ie.mgmt_group_cipher =
814 ssid->ieee80211w != NO_IEEE80211W ?
815 WPA_CIPHER_AES_128_CMAC : 0;
816 #endif /* CONFIG_IEEE80211W */
817 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
823 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
824 "pairwise %d key_mgmt %d proto %d",
825 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
826 #ifdef CONFIG_IEEE80211W
827 if (ssid->ieee80211w) {
828 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
829 ie.mgmt_group_cipher);
831 #endif /* CONFIG_IEEE80211W */
833 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
834 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
835 !!(ssid->proto & WPA_PROTO_RSN));
837 if (bss || !wpa_s->ap_ies_from_associnfo) {
838 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
839 bss_wpa ? 2 + bss_wpa[1] : 0) ||
840 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
841 bss_rsn ? 2 + bss_rsn[1] : 0))
845 sel = ie.group_cipher & ssid->group_cipher;
846 if (sel & WPA_CIPHER_CCMP) {
847 wpa_s->group_cipher = WPA_CIPHER_CCMP;
848 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
849 } else if (sel & WPA_CIPHER_TKIP) {
850 wpa_s->group_cipher = WPA_CIPHER_TKIP;
851 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
852 } else if (sel & WPA_CIPHER_WEP104) {
853 wpa_s->group_cipher = WPA_CIPHER_WEP104;
854 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
855 } else if (sel & WPA_CIPHER_WEP40) {
856 wpa_s->group_cipher = WPA_CIPHER_WEP40;
857 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
859 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
863 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
864 if (sel & WPA_CIPHER_CCMP) {
865 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
866 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
867 } else if (sel & WPA_CIPHER_TKIP) {
868 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
869 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
870 } else if (sel & WPA_CIPHER_NONE) {
871 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
872 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
874 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
879 sel = ie.key_mgmt & ssid->key_mgmt;
881 #ifdef CONFIG_IEEE80211R
882 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
883 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
884 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
885 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
886 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
887 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
888 #endif /* CONFIG_IEEE80211R */
889 #ifdef CONFIG_IEEE80211W
890 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
891 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
892 wpa_msg(wpa_s, MSG_DEBUG,
893 "WPA: using KEY_MGMT 802.1X with SHA256");
894 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
895 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
896 wpa_msg(wpa_s, MSG_DEBUG,
897 "WPA: using KEY_MGMT PSK with SHA256");
898 #endif /* CONFIG_IEEE80211W */
899 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
900 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
901 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
902 } else if (sel & WPA_KEY_MGMT_PSK) {
903 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
904 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
905 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
906 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
907 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
909 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
910 "key management type.");
914 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
915 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
916 wpa_s->pairwise_cipher);
917 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
919 #ifdef CONFIG_IEEE80211W
920 sel = ie.mgmt_group_cipher;
921 if (ssid->ieee80211w == NO_IEEE80211W ||
922 !(ie.capabilities & WPA_CAPABILITY_MFPC))
924 if (sel & WPA_CIPHER_AES_128_CMAC) {
925 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
926 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
929 wpa_s->mgmt_group_cipher = 0;
930 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
932 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
933 wpa_s->mgmt_group_cipher);
934 #endif /* CONFIG_IEEE80211W */
936 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
937 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
942 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
943 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
945 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
952 * wpa_supplicant_associate - Request association
953 * @wpa_s: Pointer to wpa_supplicant data
954 * @bss: Scan results for the selected BSS, or %NULL if not available
955 * @ssid: Configuration data for the selected network
957 * This function is used to request %wpa_supplicant to associate with a BSS.
959 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
960 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
964 int use_crypt, ret, i, bssid_changed;
965 int algs = AUTH_ALG_OPEN_SYSTEM;
966 wpa_cipher cipher_pairwise, cipher_group;
967 struct wpa_driver_associate_params params;
968 int wep_keys_set = 0;
969 struct wpa_driver_capa capa;
970 int assoc_failed = 0;
971 struct wpa_ssid *old_ssid;
973 if (ssid->mode == 2) {
975 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
976 wpa_printf(MSG_INFO, "Driver does not support AP "
980 wpa_supplicant_create_ap(wpa_s, ssid);
981 #else /* CONFIG_AP */
982 wpa_printf(MSG_ERROR, "AP mode support not included in the "
984 #endif /* CONFIG_AP */
988 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
989 sme_authenticate(wpa_s, bss, ssid);
993 wpa_s->reassociate = 0;
995 #ifdef CONFIG_IEEE80211R
997 #endif /* CONFIG_IEEE80211R */
998 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
999 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1000 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1001 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
1002 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1003 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1004 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1006 wpas_notify_bssid_changed(wpa_s);
1007 #ifdef CONFIG_IEEE80211R
1008 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1009 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1011 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
1013 /* Prepare for the next transition */
1014 wpa_ft_prepare_auth_request(wpa_s->wpa);
1016 #endif /* CONFIG_IEEE80211R */
1018 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1019 wpa_s->conf->ap_scan == 2 &&
1020 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1021 /* Use ap_scan==1 style network selection to find the network
1023 wpa_s->scan_req = 2;
1024 wpa_s->reassociate = 1;
1025 wpa_supplicant_req_scan(wpa_s, 0, 0);
1027 #endif /* CONFIG_WPS */
1029 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1030 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1031 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1033 wpa_supplicant_cancel_scan(wpa_s);
1035 /* Starting new association, so clear the possibly used WPA IE from the
1036 * previous association. */
1037 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1039 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
1040 wpa_printf(MSG_WARNING, "Failed to set operating mode");
1044 #ifdef IEEE8021X_EAPOL
1045 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1047 if (ssid->non_leap == 0)
1048 algs = AUTH_ALG_LEAP;
1050 algs |= AUTH_ALG_LEAP;
1053 #endif /* IEEE8021X_EAPOL */
1054 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1055 if (ssid->auth_alg) {
1057 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1058 algs |= AUTH_ALG_OPEN_SYSTEM;
1059 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1060 algs |= AUTH_ALG_SHARED_KEY;
1061 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1062 algs |= AUTH_ALG_LEAP;
1063 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1066 wpa_drv_set_auth_alg(wpa_s, algs);
1068 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1069 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1070 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1071 WPA_KEY_MGMT_FT_IEEE8021X |
1072 WPA_KEY_MGMT_FT_PSK |
1073 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1074 WPA_KEY_MGMT_PSK_SHA256))) {
1075 int try_opportunistic;
1076 try_opportunistic = ssid->proactive_key_caching &&
1077 (ssid->proto & WPA_PROTO_RSN);
1078 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1079 wpa_s->current_ssid,
1080 try_opportunistic) == 0)
1081 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1082 wpa_ie_len = sizeof(wpa_ie);
1083 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1084 wpa_ie, &wpa_ie_len)) {
1085 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1086 "management and encryption suites");
1089 } else if (ssid->key_mgmt &
1090 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1091 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1092 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1093 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1094 wpa_ie_len = sizeof(wpa_ie);
1095 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1096 wpa_ie, &wpa_ie_len)) {
1097 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1098 "management and encryption suites (no scan "
1103 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1104 struct wpabuf *wps_ie;
1105 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1106 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1107 wpa_ie_len = wpabuf_len(wps_ie);
1108 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1111 wpabuf_free(wps_ie);
1112 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1113 #endif /* CONFIG_WPS */
1115 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1119 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1121 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1122 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1123 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1124 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1125 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1127 if (wpa_set_wep_keys(wpa_s, ssid)) {
1132 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1135 #ifdef IEEE8021X_EAPOL
1136 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1137 if ((ssid->eapol_flags &
1138 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1139 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1143 /* Assume that dynamic WEP-104 keys will be used and
1144 * set cipher suites in order for drivers to expect
1146 cipher_pairwise = cipher_group = CIPHER_WEP104;
1149 #endif /* IEEE8021X_EAPOL */
1151 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1152 /* Set the key before (and later after) association */
1153 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1156 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1157 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1158 os_memset(¶ms, 0, sizeof(params));
1160 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1161 params.bssid = bss->bssid;
1162 params.ssid = ie ? ie + 2 : (u8 *) "";
1163 params.ssid_len = ie ? ie[1] : 0;
1164 params.freq = bss->freq;
1166 params.ssid = ssid->ssid;
1167 params.ssid_len = ssid->ssid_len;
1169 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1170 params.freq = ssid->frequency; /* Initial channel for IBSS */
1171 params.wpa_ie = wpa_ie;
1172 params.wpa_ie_len = wpa_ie_len;
1173 params.pairwise_suite = cipher_pairwise;
1174 params.group_suite = cipher_group;
1175 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1176 params.auth_alg = algs;
1177 params.mode = ssid->mode;
1178 for (i = 0; i < NUM_WEP_KEYS; i++) {
1179 if (ssid->wep_key_len[i])
1180 params.wep_key[i] = ssid->wep_key[i];
1181 params.wep_key_len[i] = ssid->wep_key_len[i];
1183 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1185 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1186 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1187 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1188 params.passphrase = ssid->passphrase;
1190 params.psk = ssid->psk;
1193 params.drop_unencrypted = use_crypt;
1195 #ifdef CONFIG_IEEE80211W
1196 switch (ssid->ieee80211w) {
1198 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1200 case IEEE80211W_OPTIONAL:
1201 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1203 case IEEE80211W_REQUIRED:
1204 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1207 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1208 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1209 struct wpa_ie_data ie;
1210 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1212 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1213 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1215 params.mgmt_frame_protection =
1216 MGMT_FRAME_PROTECTION_REQUIRED;
1219 #endif /* CONFIG_IEEE80211W */
1221 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1222 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1224 ret = wpa_drv_associate(wpa_s, ¶ms);
1226 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1228 /* try to continue anyway; new association will be tried again
1233 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1234 /* Set the key after the association just in case association
1235 * cleared the previously configured key. */
1236 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1237 /* No need to timeout authentication since there is no key
1239 wpa_supplicant_cancel_auth_timeout(wpa_s);
1240 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1241 #ifdef CONFIG_IBSS_RSN
1242 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1243 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1244 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1245 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1247 * RSN IBSS authentication is per-STA and we can disable the
1248 * per-BSSID authentication.
1250 wpa_supplicant_cancel_auth_timeout(wpa_s);
1251 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1252 #endif /* CONFIG_IBSS_RSN */
1254 /* Timeout for IEEE 802.11 authentication and association */
1258 /* give IBSS a bit more time */
1259 timeout = ssid->mode ? 10 : 5;
1260 } else if (wpa_s->conf->ap_scan == 1) {
1261 /* give IBSS a bit more time */
1262 timeout = ssid->mode ? 20 : 10;
1264 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1267 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1268 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1269 /* Set static WEP keys again */
1270 wpa_set_wep_keys(wpa_s, ssid);
1273 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1275 * Do not allow EAP session resumption between different
1276 * network configurations.
1278 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1280 old_ssid = wpa_s->current_ssid;
1281 wpa_s->current_ssid = ssid;
1282 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1283 wpa_supplicant_initiate_eapol(wpa_s);
1284 if (old_ssid != wpa_s->current_ssid)
1285 wpas_notify_network_changed(wpa_s);
1290 * wpa_supplicant_disassociate - Disassociate the current connection
1291 * @wpa_s: Pointer to wpa_supplicant data
1292 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1294 * This function is used to request %wpa_supplicant to disassociate with the
1297 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1300 struct wpa_ssid *old_ssid;
1303 if (!is_zero_ether_addr(wpa_s->bssid)) {
1304 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1305 ieee80211_sta_disassociate(wpa_s, reason_code);
1307 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1308 addr = wpa_s->bssid;
1310 wpa_clear_keys(wpa_s, addr);
1311 wpa_supplicant_mark_disassoc(wpa_s);
1312 old_ssid = wpa_s->current_ssid;
1313 wpa_s->current_ssid = NULL;
1314 wpa_sm_set_config(wpa_s->wpa, NULL);
1315 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1316 if (old_ssid != wpa_s->current_ssid)
1317 wpas_notify_network_changed(wpa_s);
1322 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1323 * @wpa_s: Pointer to wpa_supplicant data
1324 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1326 * This function is used to request %wpa_supplicant to deauthenticate from the
1329 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1332 struct wpa_ssid *old_ssid;
1335 if (!is_zero_ether_addr(wpa_s->bssid)) {
1336 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1337 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1339 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1341 addr = wpa_s->bssid;
1343 wpa_clear_keys(wpa_s, addr);
1344 wpa_supplicant_mark_disassoc(wpa_s);
1345 old_ssid = wpa_s->current_ssid;
1346 wpa_s->current_ssid = NULL;
1347 wpa_sm_set_config(wpa_s->wpa, NULL);
1348 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1349 if (old_ssid != wpa_s->current_ssid)
1350 wpas_notify_network_changed(wpa_s);
1355 * wpa_supplicant_enable_network - Mark a configured network as enabled
1356 * @wpa_s: wpa_supplicant structure for a network interface
1357 * @ssid: wpa_ssid structure for a configured network or %NULL
1359 * Enables the specified network or all networks if no network specified.
1361 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1362 struct wpa_ssid *ssid)
1364 struct wpa_ssid *other_ssid;
1368 other_ssid = wpa_s->conf->ssid;
1369 while (other_ssid) {
1370 if (other_ssid == wpa_s->current_ssid &&
1371 other_ssid->disabled)
1372 wpa_s->reassociate = 1;
1374 was_disabled = other_ssid->disabled;
1376 other_ssid->disabled = 0;
1378 if (was_disabled != other_ssid->disabled)
1379 wpas_notify_network_enabled_changed(
1382 other_ssid = other_ssid->next;
1384 if (wpa_s->reassociate)
1385 wpa_supplicant_req_scan(wpa_s, 0, 0);
1386 } else if (wpa_s->current_ssid == NULL && ssid->disabled) {
1388 * Try to reassociate since there is no current configuration
1389 * and a new network was made available.
1391 wpa_s->reassociate = 1;
1392 wpa_supplicant_req_scan(wpa_s, 0, 0);
1394 was_disabled = ssid->disabled;
1398 if (was_disabled != ssid->disabled)
1399 wpas_notify_network_enabled_changed(wpa_s, ssid);
1405 * wpa_supplicant_disable_network - Mark a configured network as disabled
1406 * @wpa_s: wpa_supplicant structure for a network interface
1407 * @ssid: wpa_ssid structure for a configured network or %NULL
1409 * Disables the specified network or all networks if no network specified.
1411 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1412 struct wpa_ssid *ssid)
1414 struct wpa_ssid *other_ssid;
1418 other_ssid = wpa_s->conf->ssid;
1419 while (other_ssid) {
1420 was_disabled = other_ssid->disabled;
1422 other_ssid->disabled = 1;
1424 if (was_disabled != other_ssid->disabled)
1425 wpas_notify_network_enabled_changed(
1428 other_ssid = other_ssid->next;
1430 if (wpa_s->current_ssid)
1431 wpa_supplicant_disassociate(
1432 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1434 if (ssid == wpa_s->current_ssid)
1435 wpa_supplicant_disassociate(
1436 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1438 was_disabled = ssid->disabled;
1442 if (was_disabled != ssid->disabled)
1443 wpas_notify_network_enabled_changed(wpa_s, ssid);
1449 * wpa_supplicant_select_network - Attempt association with a network
1450 * @wpa_s: wpa_supplicant structure for a network interface
1451 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1453 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1454 struct wpa_ssid *ssid)
1457 struct wpa_ssid *other_ssid;
1459 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1460 wpa_supplicant_disassociate(
1461 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1464 * Mark all other networks disabled or mark all networks enabled if no
1465 * network specified.
1467 other_ssid = wpa_s->conf->ssid;
1468 while (other_ssid) {
1469 int was_disabled = other_ssid->disabled;
1471 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1473 if (was_disabled != other_ssid->disabled)
1474 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1476 other_ssid = other_ssid->next;
1478 wpa_s->disconnected = 0;
1479 wpa_s->reassociate = 1;
1480 wpa_supplicant_req_scan(wpa_s, 0, 0);
1482 wpas_notify_network_selected(wpa_s, ssid);
1487 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1488 * @wpa_s: wpa_supplicant structure for a network interface
1489 * @ap_scan: AP scan mode
1490 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1493 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1498 if (ap_scan < 0 || ap_scan > 2)
1501 old_ap_scan = wpa_s->conf->ap_scan;
1502 wpa_s->conf->ap_scan = ap_scan;
1504 if (old_ap_scan != wpa_s->conf->ap_scan)
1505 wpas_notify_ap_scan_changed(wpa_s);
1512 * wpa_supplicant_set_debug_params - Set global debug params
1513 * @global: wpa_global structure
1514 * @debug_level: debug level
1515 * @debug_timestamp: determines if show timestamp in debug data
1516 * @debug_show_keys: determines if show keys in debug data
1517 * Returns: 0 if succeed or -1 if debug_level has wrong value
1519 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1520 int debug_timestamp, int debug_show_keys)
1523 int old_level, old_timestamp, old_show_keys;
1525 /* check for allowed debuglevels */
1526 if (debug_level != MSG_MSGDUMP &&
1527 debug_level != MSG_DEBUG &&
1528 debug_level != MSG_INFO &&
1529 debug_level != MSG_WARNING &&
1530 debug_level != MSG_ERROR)
1533 old_level = wpa_debug_level;
1534 old_timestamp = wpa_debug_timestamp;
1535 old_show_keys = wpa_debug_show_keys;
1537 wpa_debug_level = debug_level;
1538 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1539 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1541 if (wpa_debug_level != old_level ||
1542 wpa_debug_timestamp != old_timestamp ||
1543 wpa_debug_show_keys != old_show_keys)
1544 wpas_notify_debug_params_changed(global);
1550 static struct wpa_scan_results * wpa_supplicant_get_scan_results_old(
1551 struct wpa_supplicant *wpa_s)
1553 #define SCAN_AP_LIMIT 128
1554 struct wpa_scan_result *results;
1556 struct wpa_scan_results *res;
1558 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1559 if (results == NULL) {
1560 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1565 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1566 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1568 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1572 if (num > SCAN_AP_LIMIT) {
1573 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1574 num, SCAN_AP_LIMIT);
1575 num = SCAN_AP_LIMIT;
1578 /* Convert old scan result data structure to the new one */
1579 res = os_zalloc(sizeof(*res));
1584 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1585 if (res->res == NULL) {
1591 for (i = 0; i < num; i++) {
1592 struct wpa_scan_result *bss = &results[i];
1593 struct wpa_scan_res *r;
1597 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1600 if (bss->mdie_present)
1603 r = os_zalloc(sizeof(*r) + ie_len);
1607 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1608 r->freq = bss->freq;
1609 r->caps = bss->caps;
1610 r->qual = bss->qual;
1611 r->noise = bss->noise;
1612 r->level = bss->level;
1616 pos = (u8 *) (r + 1);
1619 *pos++ = WLAN_EID_SSID;
1620 *pos++ = bss->ssid_len;
1621 os_memcpy(pos, bss->ssid, bss->ssid_len);
1622 pos += bss->ssid_len;
1625 /* Fake Supported Rate IE to include max rate */
1626 *pos++ = WLAN_EID_SUPP_RATES;
1628 *pos++ = bss->maxrate;
1631 if (bss->rsn_ie_len) {
1632 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1633 pos += bss->rsn_ie_len;
1636 if (bss->mdie_present) {
1637 os_memcpy(pos, bss->mdie, 5);
1641 if (bss->wpa_ie_len) {
1642 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1643 pos += bss->wpa_ie_len;
1646 res->res[res->num++] = r;
1656 * wpa_supplicant_get_scan_results - Get scan results
1657 * @wpa_s: Pointer to wpa_supplicant data
1658 * Returns: 0 on success, -1 on failure
1660 * This function is request the current scan results from the driver and stores
1661 * a local copy of the results in wpa_s->scan_res.
1663 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1667 wpa_scan_results_free(wpa_s->scan_res);
1668 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1669 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1670 else if (wpa_s->driver->get_scan_results2 == NULL)
1671 wpa_s->scan_res = wpa_supplicant_get_scan_results_old(wpa_s);
1673 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1674 if (wpa_s->scan_res == NULL) {
1675 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1679 wpa_scan_sort_results(wpa_s->scan_res);
1687 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1688 * @wpa_s: Pointer to wpa_supplicant data
1689 * Returns: A pointer to the current network structure or %NULL on failure
1691 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1693 struct wpa_ssid *entry;
1694 u8 ssid[MAX_SSID_LEN];
1700 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1701 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1702 wpa_printf(MSG_WARNING, "Could not read SSID from "
1707 res = wpa_drv_get_ssid(wpa_s, ssid);
1709 wpa_printf(MSG_WARNING, "Could not read SSID from "
1716 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1717 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1718 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1719 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1723 wired = wpa_s->conf->ap_scan == 0 &&
1724 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1726 entry = wpa_s->conf->ssid;
1728 if (!entry->disabled &&
1729 ((ssid_len == entry->ssid_len &&
1730 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1731 (!entry->bssid_set ||
1732 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1735 if (!entry->disabled &&
1736 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1737 (entry->ssid == NULL || entry->ssid_len == 0) &&
1738 (!entry->bssid_set ||
1739 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1741 #endif /* CONFIG_WPS */
1742 entry = entry->next;
1749 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1759 if (wpa_drivers[0] == NULL) {
1760 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1766 /* default to first driver in the list */
1767 wpa_s->driver = wpa_drivers[0];
1771 pos = os_strchr(name, ',');
1775 len = os_strlen(name);
1776 for (i = 0; wpa_drivers[i]; i++) {
1777 if (os_strlen(wpa_drivers[i]->name) == len &&
1778 os_strncmp(name, wpa_drivers[i]->name, len) ==
1780 wpa_s->driver = wpa_drivers[i];
1785 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1790 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1791 const u8 *buf, size_t len)
1793 struct wpa_supplicant *wpa_s = ctx;
1795 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1796 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1798 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1800 * There is possible race condition between receiving the
1801 * association event and the EAPOL frame since they are coming
1802 * through different paths from the driver. In order to avoid
1803 * issues in trying to process the EAPOL frame before receiving
1804 * association information, lets queue it for processing until
1805 * the association event is received.
1807 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1808 "received EAPOL frame");
1809 wpabuf_free(wpa_s->pending_eapol_rx);
1810 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1811 if (wpa_s->pending_eapol_rx) {
1812 os_get_time(&wpa_s->pending_eapol_rx_time);
1813 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1820 if (wpa_s->ap_iface) {
1821 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1824 #endif /* CONFIG_AP */
1826 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1827 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1828 "no key management is configured");
1832 if (wpa_s->eapol_received == 0 &&
1833 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1834 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1835 wpa_s->wpa_state != WPA_COMPLETED)) {
1836 /* Timeout for completing IEEE 802.1X and WPA authentication */
1837 wpa_supplicant_req_auth_timeout(
1839 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1840 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1841 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1844 wpa_s->eapol_received++;
1846 if (wpa_s->countermeasures) {
1847 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1852 #ifdef CONFIG_IBSS_RSN
1853 if (wpa_s->current_ssid &&
1854 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1855 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1858 #endif /* CONFIG_IBSS_RSN */
1860 /* Source address of the incoming EAPOL frame could be compared to the
1861 * current BSSID. However, it is possible that a centralized
1862 * Authenticator could be using another MAC address than the BSSID of
1863 * an AP, so just allow any address to be used for now. The replies are
1864 * still sent to the current BSSID (if available), though. */
1866 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1867 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1868 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1870 wpa_drv_poll(wpa_s);
1871 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1872 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1873 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1875 * Set portValid = TRUE here since we are going to skip 4-way
1876 * handshake processing which would normally set portValid. We
1877 * need this to allow the EAPOL state machines to be completed
1878 * without going through EAPOL-Key handshake.
1880 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1885 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1886 struct ieee80211_rx_status *rx_status)
1888 struct wpa_supplicant *wpa_s = ctx;
1889 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1894 * wpa_supplicant_driver_init - Initialize driver interface parameters
1895 * @wpa_s: Pointer to wpa_supplicant data
1896 * Returns: 0 on success, -1 on failure
1898 * This function is called to initialize driver interface parameters.
1899 * wpa_drv_init() must have been called before this function to initialize the
1902 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1904 static int interface_count = 0;
1906 if (wpa_s->driver->send_eapol) {
1907 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1909 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1911 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1912 wpa_drv_get_mac_addr(wpa_s),
1914 wpa_supplicant_rx_eapol, wpa_s, 0);
1915 if (wpa_s->l2 == NULL)
1919 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1920 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1924 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1925 MAC2STR(wpa_s->own_addr));
1927 if (wpa_s->bridge_ifname[0]) {
1928 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1929 " '%s'", wpa_s->bridge_ifname);
1930 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1933 wpa_supplicant_rx_eapol, wpa_s,
1935 if (wpa_s->l2_br == NULL) {
1936 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1937 "connection for the bridge interface '%s'",
1938 wpa_s->bridge_ifname);
1943 /* Backwards compatibility call to set_wpa() handler. This is called
1944 * only just after init and just before deinit, so these handler can be
1945 * used to implement same functionality. */
1946 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1947 struct wpa_driver_capa capa;
1948 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1949 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1950 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1951 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1952 /* Continue to allow non-WPA modes to be used. */
1954 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1960 wpa_clear_keys(wpa_s, NULL);
1962 /* Make sure that TKIP countermeasures are not left enabled (could
1963 * happen if wpa_supplicant is killed during countermeasures. */
1964 wpa_drv_set_countermeasures(wpa_s, 0);
1966 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1968 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1969 wpa_drv_flush_pmkid(wpa_s);
1971 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1972 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1979 static int wpa_supplicant_daemon(const char *pid_file)
1981 wpa_printf(MSG_DEBUG, "Daemonize..");
1982 return os_daemonize(pid_file);
1986 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1988 struct wpa_supplicant *wpa_s;
1990 wpa_s = os_zalloc(sizeof(*wpa_s));
1993 wpa_s->scan_req = 1;
1999 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2000 struct wpa_interface *iface)
2002 const char *ifname, *driver;
2003 struct wpa_driver_capa capa;
2005 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2006 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2007 iface->confname ? iface->confname : "N/A",
2008 iface->driver ? iface->driver : "default",
2009 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2010 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2012 if (iface->confname) {
2013 #ifdef CONFIG_BACKEND_FILE
2014 wpa_s->confname = os_rel2abs_path(iface->confname);
2015 if (wpa_s->confname == NULL) {
2016 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2017 "for configuration file '%s'.",
2021 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2022 iface->confname, wpa_s->confname);
2023 #else /* CONFIG_BACKEND_FILE */
2024 wpa_s->confname = os_strdup(iface->confname);
2025 #endif /* CONFIG_BACKEND_FILE */
2026 wpa_s->conf = wpa_config_read(wpa_s->confname);
2027 if (wpa_s->conf == NULL) {
2028 wpa_printf(MSG_ERROR, "Failed to read or parse "
2029 "configuration '%s'.", wpa_s->confname);
2034 * Override ctrl_interface and driver_param if set on command
2037 if (iface->ctrl_interface) {
2038 os_free(wpa_s->conf->ctrl_interface);
2039 wpa_s->conf->ctrl_interface =
2040 os_strdup(iface->ctrl_interface);
2043 if (iface->driver_param) {
2044 os_free(wpa_s->conf->driver_param);
2045 wpa_s->conf->driver_param =
2046 os_strdup(iface->driver_param);
2049 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2050 iface->driver_param);
2052 if (wpa_s->conf == NULL) {
2053 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2057 if (iface->ifname == NULL) {
2058 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2061 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2062 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2066 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2068 if (iface->bridge_ifname) {
2069 if (os_strlen(iface->bridge_ifname) >=
2070 sizeof(wpa_s->bridge_ifname)) {
2071 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2072 "name '%s'.", iface->bridge_ifname);
2075 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2076 sizeof(wpa_s->bridge_ifname));
2079 /* RSNA Supplicant Key Management - INITIALIZE */
2080 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2081 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2083 /* Initialize driver interface and register driver event handler before
2084 * L2 receive handler so that association events are processed before
2085 * EAPOL-Key packets if both become available for the same select()
2087 driver = iface->driver;
2089 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2092 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2093 if (wpa_s->drv_priv == NULL) {
2095 pos = os_strchr(driver, ',');
2097 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2098 "interface - try next driver wrapper");
2102 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2105 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2106 wpa_printf(MSG_ERROR, "Driver interface rejected "
2107 "driver_param '%s'", wpa_s->conf->driver_param);
2111 ifname = wpa_drv_get_ifname(wpa_s);
2112 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2113 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2114 "name with '%s'", ifname);
2115 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2118 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2121 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2122 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2124 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2126 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2127 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2128 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2129 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2130 "dot11RSNAConfigPMKLifetime");
2134 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2135 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2136 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2137 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2138 "dot11RSNAConfigPMKReauthThreshold");
2142 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2143 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2144 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2145 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2146 "dot11RSNAConfigSATimeout");
2150 if (wpa_supplicant_driver_init(wpa_s) < 0)
2153 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2154 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2155 wpa_printf(MSG_DEBUG, "Failed to set country");
2159 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2161 if (wpas_wps_init(wpa_s))
2164 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2166 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2168 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2169 if (wpa_s->ctrl_iface == NULL) {
2170 wpa_printf(MSG_ERROR,
2171 "Failed to initialize control interface '%s'.\n"
2172 "You may have another wpa_supplicant process "
2173 "already running or the file was\n"
2174 "left by an unclean termination of wpa_supplicant "
2175 "in which case you will need\n"
2176 "to manually remove this file before starting "
2177 "wpa_supplicant again.\n",
2178 wpa_s->conf->ctrl_interface);
2182 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2183 wpa_s->drv_flags = capa.flags;
2184 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2185 if (ieee80211_sta_init(wpa_s))
2188 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2191 #ifdef CONFIG_IBSS_RSN
2192 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2193 if (!wpa_s->ibss_rsn) {
2194 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2197 #endif /* CONFIG_IBSS_RSN */
2203 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2205 if (wpa_s->drv_priv) {
2206 wpa_supplicant_deauthenticate(wpa_s,
2207 WLAN_REASON_DEAUTH_LEAVING);
2209 /* Backwards compatibility call to set_wpa() handler. This is
2210 * called only just after init and just before deinit, so these
2211 * handler can be used to implement same functionality. */
2212 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
2213 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
2217 wpa_drv_set_drop_unencrypted(wpa_s, 0);
2218 wpa_drv_set_countermeasures(wpa_s, 0);
2219 wpa_clear_keys(wpa_s, NULL);
2222 wpas_notify_unregister_interface(wpa_s);
2224 wpa_supplicant_cleanup(wpa_s);
2226 if (wpa_s->drv_priv)
2227 wpa_drv_deinit(wpa_s);
2232 * wpa_supplicant_add_iface - Add a new network interface
2233 * @global: Pointer to global data from wpa_supplicant_init()
2234 * @iface: Interface configuration options
2235 * Returns: Pointer to the created interface or %NULL on failure
2237 * This function is used to add new network interfaces for %wpa_supplicant.
2238 * This can be called before wpa_supplicant_run() to add interfaces before the
2239 * main event loop has been started. In addition, new interfaces can be added
2240 * dynamically while %wpa_supplicant is already running. This could happen,
2241 * e.g., when a hotplug network adapter is inserted.
2243 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2244 struct wpa_interface *iface)
2246 struct wpa_supplicant *wpa_s;
2248 if (global == NULL || iface == NULL)
2251 wpa_s = wpa_supplicant_alloc();
2255 if (wpa_supplicant_init_iface(wpa_s, iface)) {
2256 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2258 wpa_supplicant_deinit_iface(wpa_s);
2263 wpa_s->global = global;
2265 /* Register the interface with the dbus control interface */
2266 if (wpas_dbus_register_iface(wpa_s)) {
2267 wpa_supplicant_deinit_iface(wpa_s);
2272 wpa_s->next = global->ifaces;
2273 global->ifaces = wpa_s;
2275 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2282 * wpa_supplicant_remove_iface - Remove a network interface
2283 * @global: Pointer to global data from wpa_supplicant_init()
2284 * @wpa_s: Pointer to the network interface to be removed
2285 * Returns: 0 if interface was removed, -1 if interface was not found
2287 * This function can be used to dynamically remove network interfaces from
2288 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2289 * addition, this function is used to remove all remaining interfaces when
2290 * %wpa_supplicant is terminated.
2292 int wpa_supplicant_remove_iface(struct wpa_global *global,
2293 struct wpa_supplicant *wpa_s)
2295 struct wpa_supplicant *prev;
2297 /* Remove interface from the global list of interfaces */
2298 prev = global->ifaces;
2299 if (prev == wpa_s) {
2300 global->ifaces = wpa_s->next;
2302 while (prev && prev->next != wpa_s)
2306 prev->next = wpa_s->next;
2309 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2311 wpa_supplicant_deinit_iface(wpa_s);
2319 * wpa_supplicant_get_iface - Get a new network interface
2320 * @global: Pointer to global data from wpa_supplicant_init()
2321 * @ifname: Interface name
2322 * Returns: Pointer to the interface or %NULL if not found
2324 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2327 struct wpa_supplicant *wpa_s;
2329 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2330 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2338 * wpa_supplicant_init - Initialize %wpa_supplicant
2339 * @params: Parameters for %wpa_supplicant
2340 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2342 * This function is used to initialize %wpa_supplicant. After successful
2343 * initialization, the returned data pointer can be used to add and remove
2344 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2346 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2348 struct wpa_global *global;
2354 wpa_debug_open_file(params->wpa_debug_file_path);
2355 if (params->wpa_debug_syslog)
2356 wpa_debug_open_syslog();
2358 ret = eap_peer_register_methods();
2360 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2362 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2363 "the same EAP type.");
2368 ret = eap_server_register_methods();
2370 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2372 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2373 "the same EAP type.");
2376 #endif /* CONFIG_AP */
2378 global = os_zalloc(sizeof(*global));
2381 global->params.daemonize = params->daemonize;
2382 global->params.wait_for_monitor = params->wait_for_monitor;
2383 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2384 if (params->pid_file)
2385 global->params.pid_file = os_strdup(params->pid_file);
2386 if (params->ctrl_interface)
2387 global->params.ctrl_interface =
2388 os_strdup(params->ctrl_interface);
2389 wpa_debug_level = global->params.wpa_debug_level =
2390 params->wpa_debug_level;
2391 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2392 params->wpa_debug_show_keys;
2393 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2394 params->wpa_debug_timestamp;
2396 if (eloop_init(global)) {
2397 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2398 wpa_supplicant_deinit(global);
2402 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2403 if (global->ctrl_iface == NULL) {
2404 wpa_supplicant_deinit(global);
2408 if (global->params.dbus_ctrl_interface) {
2409 global->dbus_ctrl_iface =
2410 wpa_supplicant_dbus_ctrl_iface_init(global);
2411 if (global->dbus_ctrl_iface == NULL) {
2412 wpa_supplicant_deinit(global);
2417 for (i = 0; wpa_drivers[i]; i++)
2418 global->drv_count++;
2419 if (global->drv_count == 0) {
2420 wpa_printf(MSG_ERROR, "No drivers enabled");
2421 wpa_supplicant_deinit(global);
2424 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2425 if (global->drv_priv == NULL) {
2426 wpa_supplicant_deinit(global);
2429 for (i = 0; wpa_drivers[i]; i++) {
2430 if (!wpa_drivers[i]->global_init)
2432 global->drv_priv[i] = wpa_drivers[i]->global_init();
2433 if (global->drv_priv[i] == NULL) {
2434 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2435 "'%s'", wpa_drivers[i]->name);
2436 wpa_supplicant_deinit(global);
2446 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2447 * @global: Pointer to global data from wpa_supplicant_init()
2448 * Returns: 0 after successful event loop run, -1 on failure
2450 * This function starts the main event loop and continues running as long as
2451 * there are any remaining events. In most cases, this function is running as
2452 * long as the %wpa_supplicant process in still in use.
2454 int wpa_supplicant_run(struct wpa_global *global)
2456 struct wpa_supplicant *wpa_s;
2458 if (global->params.daemonize &&
2459 wpa_supplicant_daemon(global->params.pid_file))
2462 if (global->params.wait_for_monitor) {
2463 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2464 if (wpa_s->ctrl_iface)
2465 wpa_supplicant_ctrl_iface_wait(
2469 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2470 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2479 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2480 * @global: Pointer to global data from wpa_supplicant_init()
2482 * This function is called to deinitialize %wpa_supplicant and to free all
2483 * allocated resources. Remaining network interfaces will also be removed.
2485 void wpa_supplicant_deinit(struct wpa_global *global)
2492 while (global->ifaces)
2493 wpa_supplicant_remove_iface(global, global->ifaces);
2495 if (global->ctrl_iface)
2496 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2497 if (global->dbus_ctrl_iface)
2498 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2500 eap_peer_unregister_methods();
2502 eap_server_unregister_methods();
2503 #endif /* CONFIG_AP */
2505 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2506 if (!global->drv_priv[i])
2508 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2510 os_free(global->drv_priv);
2514 if (global->params.pid_file) {
2515 os_daemonize_terminate(global->params.pid_file);
2516 os_free(global->params.pid_file);
2518 os_free(global->params.ctrl_interface);
2521 wpa_debug_close_syslog();
2522 wpa_debug_close_file();