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 "pcsc_funcs.h"
35 #include "pmksa_cache.h"
38 #include "ieee802_11_defs.h"
39 #include "blacklist.h"
40 #include "wpas_glue.h"
41 #include "wps_supplicant.h"
48 const char *wpa_supplicant_version =
49 "wpa_supplicant v" VERSION_STR "\n"
50 "Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
52 const char *wpa_supplicant_license =
53 "This program is free software. You can distribute it and/or modify it\n"
54 "under the terms of the GNU General Public License version 2.\n"
56 "Alternatively, this software may be distributed under the terms of the\n"
57 "BSD license. See README and COPYING for more details.\n"
58 #ifdef EAP_TLS_OPENSSL
59 "\nThis product includes software developed by the OpenSSL Project\n"
60 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
61 #endif /* EAP_TLS_OPENSSL */
64 #ifndef CONFIG_NO_STDOUT_DEBUG
65 /* Long text divided into parts in order to fit in C89 strings size limits. */
66 const char *wpa_supplicant_full_license1 =
67 "This program is free software; you can redistribute it and/or modify\n"
68 "it under the terms of the GNU General Public License version 2 as\n"
69 "published by the Free Software Foundation.\n"
71 "This program is distributed in the hope that it will be useful,\n"
72 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
73 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
74 "GNU General Public License for more details.\n"
76 const char *wpa_supplicant_full_license2 =
77 "You should have received a copy of the GNU General Public License\n"
78 "along with this program; if not, write to the Free Software\n"
79 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
81 "Alternatively, this software may be distributed under the terms of the\n"
84 "Redistribution and use in source and binary forms, with or without\n"
85 "modification, are permitted provided that the following conditions are\n"
88 const char *wpa_supplicant_full_license3 =
89 "1. Redistributions of source code must retain the above copyright\n"
90 " notice, this list of conditions and the following disclaimer.\n"
92 "2. Redistributions in binary form must reproduce the above copyright\n"
93 " notice, this list of conditions and the following disclaimer in the\n"
94 " documentation and/or other materials provided with the distribution.\n"
96 const char *wpa_supplicant_full_license4 =
97 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
98 " names of its contributors may be used to endorse or promote products\n"
99 " derived from this software without specific prior written permission.\n"
101 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
102 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
103 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
104 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
105 const char *wpa_supplicant_full_license5 =
106 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
107 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
108 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
109 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
110 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
111 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
112 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
114 #endif /* CONFIG_NO_STDOUT_DEBUG */
116 extern int wpa_debug_level;
117 extern int wpa_debug_show_keys;
118 extern int wpa_debug_timestamp;
119 extern struct wpa_driver_ops *wpa_drivers[];
121 /* Configure default/group WEP keys for static WEP */
122 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
126 for (i = 0; i < NUM_WEP_KEYS; i++) {
127 if (ssid->wep_key_len[i] == 0)
131 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
132 (u8 *) "\xff\xff\xff\xff\xff\xff",
133 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
134 ssid->wep_key[i], ssid->wep_key_len[i]);
141 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
142 struct wpa_ssid *ssid)
149 /* IBSS/WPA-None uses only one key (Group) for both receiving and
150 * sending unicast and multicast packets. */
152 if (ssid->mode != IEEE80211_MODE_IBSS) {
153 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
154 "for WPA-None", ssid->mode);
158 if (!ssid->psk_set) {
159 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
163 switch (wpa_s->group_cipher) {
164 case WPA_CIPHER_CCMP:
165 os_memcpy(key, ssid->psk, 16);
169 case WPA_CIPHER_TKIP:
170 /* WPA-None uses the same Michael MIC key for both TX and RX */
171 os_memcpy(key, ssid->psk, 16 + 8);
172 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
177 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
178 "WPA-None", wpa_s->group_cipher);
182 /* TODO: should actually remember the previously used seq#, both for TX
183 * and RX from each STA.. */
185 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
186 0, 1, seq, 6, key, keylen);
190 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
192 struct wpa_supplicant *wpa_s = eloop_ctx;
193 const u8 *bssid = wpa_s->bssid;
194 if (is_zero_ether_addr(bssid))
195 bssid = wpa_s->pending_bssid;
196 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
198 wpa_blacklist_add(wpa_s, bssid);
199 wpa_sm_notify_disassoc(wpa_s->wpa);
200 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
201 wpa_s->reassociate = 1;
202 wpa_supplicant_req_scan(wpa_s, 0, 0);
207 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
208 * @wpa_s: Pointer to wpa_supplicant data
209 * @sec: Number of seconds after which to time out authentication
210 * @usec: Number of microseconds after which to time out authentication
212 * This function is used to schedule a timeout for the current authentication
215 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
218 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
219 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
222 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
223 "%d usec", sec, usec);
224 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
225 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
230 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
231 * @wpa_s: Pointer to wpa_supplicant data
233 * This function is used to cancel authentication timeout scheduled with
234 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
237 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
239 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
240 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
241 wpa_blacklist_del(wpa_s, wpa_s->bssid);
246 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
247 * @wpa_s: Pointer to wpa_supplicant data
249 * This function is used to configure EAPOL state machine based on the selected
250 * authentication mode.
252 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
254 #ifdef IEEE8021X_EAPOL
255 struct eapol_config eapol_conf;
256 struct wpa_ssid *ssid = wpa_s->current_ssid;
258 #ifdef CONFIG_IBSS_RSN
259 if (ssid->mode == IEEE80211_MODE_IBSS &&
260 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
261 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
263 * RSN IBSS authentication is per-STA and we can disable the
264 * per-BSSID EAPOL authentication.
266 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
267 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
268 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
271 #endif /* CONFIG_IBSS_RSN */
273 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
274 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
276 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
277 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
278 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
280 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
282 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
283 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
284 eapol_conf.accept_802_1x_keys = 1;
285 eapol_conf.required_keys = 0;
286 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
287 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
290 eapol_conf.required_keys |=
291 EAPOL_REQUIRE_KEY_BROADCAST;
294 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
295 eapol_conf.required_keys = 0;
298 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
299 eapol_conf.workaround = ssid->eap_workaround;
300 eapol_conf.eap_disabled =
301 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
302 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
303 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
304 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
305 #endif /* IEEE8021X_EAPOL */
310 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
311 * @wpa_s: Pointer to wpa_supplicant data
312 * @ssid: Configuration data for the network
314 * This function is used to configure WPA state machine and related parameters
315 * to a mode where WPA is not enabled. This is called as part of the
316 * authentication configuration when the selected network does not use WPA.
318 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
319 struct wpa_ssid *ssid)
323 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
324 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
325 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
326 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
328 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
329 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
330 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
331 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
332 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
333 wpa_s->group_cipher = WPA_CIPHER_NONE;
334 wpa_s->mgmt_group_cipher = 0;
336 for (i = 0; i < NUM_WEP_KEYS; i++) {
337 if (ssid->wep_key_len[i] > 5) {
338 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
339 wpa_s->group_cipher = WPA_CIPHER_WEP104;
341 } else if (ssid->wep_key_len[i] > 0) {
342 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
343 wpa_s->group_cipher = WPA_CIPHER_WEP40;
348 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
351 wpa_s->pairwise_cipher);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
353 #ifdef CONFIG_IEEE80211W
354 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
355 wpa_s->mgmt_group_cipher);
356 #endif /* CONFIG_IEEE80211W */
358 pmksa_cache_clear_current(wpa_s->wpa);
362 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
364 bgscan_deinit(wpa_s);
365 scard_deinit(wpa_s->scard);
367 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
368 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
369 l2_packet_deinit(wpa_s->l2);
372 l2_packet_deinit(wpa_s->l2_br);
376 if (wpa_s->ctrl_iface) {
377 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
378 wpa_s->ctrl_iface = NULL;
380 if (wpa_s->conf != NULL) {
381 wpa_config_free(wpa_s->conf);
385 os_free(wpa_s->confname);
386 wpa_s->confname = NULL;
388 wpa_sm_set_eapol(wpa_s->wpa, NULL);
389 eapol_sm_deinit(wpa_s->eapol);
392 rsn_preauth_deinit(wpa_s->wpa);
394 pmksa_candidate_free(wpa_s->wpa);
395 wpa_sm_deinit(wpa_s->wpa);
397 wpa_blacklist_clear(wpa_s);
399 wpa_scan_results_free(wpa_s->scan_res);
400 wpa_s->scan_res = NULL;
402 wpa_supplicant_cancel_scan(wpa_s);
403 wpa_supplicant_cancel_auth_timeout(wpa_s);
405 ieee80211_sta_deinit(wpa_s);
407 wpas_wps_deinit(wpa_s);
409 wpabuf_free(wpa_s->pending_eapol_rx);
410 wpa_s->pending_eapol_rx = NULL;
412 #ifdef CONFIG_IBSS_RSN
413 ibss_rsn_deinit(wpa_s->ibss_rsn);
414 wpa_s->ibss_rsn = NULL;
415 #endif /* CONFIG_IBSS_RSN */
418 os_free(wpa_s->sme.ft_ies);
419 wpa_s->sme.ft_ies = NULL;
420 wpa_s->sme.ft_ies_len = 0;
421 #endif /* CONFIG_SME */
424 wpa_supplicant_ap_deinit(wpa_s);
425 #endif /* CONFIG_AP */
430 * wpa_clear_keys - Clear keys configured for the driver
431 * @wpa_s: Pointer to wpa_supplicant data
432 * @addr: Previously used BSSID or %NULL if not available
434 * This function clears the encryption keys that has been previously configured
437 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
439 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
441 if (wpa_s->keys_cleared) {
442 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
443 * timing issues with keys being cleared just before new keys
444 * are set or just after association or something similar. This
445 * shows up in group key handshake failing often because of the
446 * client not receiving the first encrypted packets correctly.
447 * Skipping some of the extra key clearing steps seems to help
448 * in completing group key handshake more reliably. */
449 wpa_printf(MSG_DEBUG, "No keys have been configured - "
450 "skip key clearing");
454 /* MLME-DELETEKEYS.request */
455 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
456 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
457 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
458 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
459 #ifdef CONFIG_IEEE80211W
460 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
461 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
462 #endif /* CONFIG_IEEE80211W */
464 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
466 /* MLME-SETPROTECTION.request(None) */
467 wpa_drv_mlme_setprotection(
469 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
470 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
472 wpa_s->keys_cleared = 1;
477 * wpa_supplicant_state_txt - Get the connection state name as a text string
478 * @state: State (wpa_state; WPA_*)
479 * Returns: The state name as a printable text string
481 const char * wpa_supplicant_state_txt(int state)
484 case WPA_DISCONNECTED:
485 return "DISCONNECTED";
490 case WPA_AUTHENTICATING:
491 return "AUTHENTICATING";
492 case WPA_ASSOCIATING:
493 return "ASSOCIATING";
496 case WPA_4WAY_HANDSHAKE:
497 return "4WAY_HANDSHAKE";
498 case WPA_GROUP_HANDSHAKE:
499 return "GROUP_HANDSHAKE";
509 * wpa_supplicant_set_state - Set current connection state
510 * @wpa_s: Pointer to wpa_supplicant data
511 * @state: The new connection state
513 * This function is called whenever the connection state changes, e.g.,
514 * association is completed for WPA/WPA2 4-Way Handshake is started.
516 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
518 wpa_printf(MSG_DEBUG, "State: %s -> %s",
519 wpa_supplicant_state_txt(wpa_s->wpa_state),
520 wpa_supplicant_state_txt(state));
522 if (state != WPA_SCANNING)
523 wpa_supplicant_notify_scanning(wpa_s, 0);
525 wpas_notify_state_changed(wpa_s, state, wpa_s->wpa_state);
527 if (state == WPA_COMPLETED && wpa_s->new_connection) {
528 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
529 struct wpa_ssid *ssid = wpa_s->current_ssid;
530 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
531 MACSTR " completed %s [id=%d id_str=%s]",
532 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
533 "(reauth)" : "(auth)",
534 ssid ? ssid->id : -1,
535 ssid && ssid->id_str ? ssid->id_str : "");
536 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
537 wpa_s->new_connection = 0;
538 wpa_s->reassociated_connection = 1;
539 wpa_drv_set_operstate(wpa_s, 1);
540 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
541 state == WPA_ASSOCIATED) {
542 wpa_s->new_connection = 1;
543 wpa_drv_set_operstate(wpa_s, 0);
545 wpa_s->wpa_state = state;
549 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
552 struct wpa_global *global = eloop_ctx;
553 struct wpa_supplicant *wpa_s;
554 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
555 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
562 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
564 wpa_states old_state = wpa_s->wpa_state;
565 wpa_s->pairwise_cipher = 0;
566 wpa_s->group_cipher = 0;
567 wpa_s->mgmt_group_cipher = 0;
569 wpa_s->wpa_state = WPA_DISCONNECTED;
570 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
575 * wpa_supplicant_reload_configuration - Reload configuration data
576 * @wpa_s: Pointer to wpa_supplicant data
577 * Returns: 0 on success or -1 if configuration parsing failed
579 * This function can be used to request that the configuration data is reloaded
580 * (e.g., after configuration file change). This function is reloading
581 * configuration only for one interface, so this may need to be called multiple
582 * times if %wpa_supplicant is controlling multiple interfaces and all
583 * interfaces need reconfiguration.
585 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
587 struct wpa_config *conf;
588 struct wpa_ssid *old_ssid;
592 if (wpa_s->confname == NULL)
594 conf = wpa_config_read(wpa_s->confname);
596 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
597 "file '%s' - exiting", wpa_s->confname);
601 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
602 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
603 os_strcmp(conf->ctrl_interface,
604 wpa_s->conf->ctrl_interface) != 0);
606 if (reconf_ctrl && wpa_s->ctrl_iface) {
607 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
608 wpa_s->ctrl_iface = NULL;
611 eapol_sm_invalidate_cached_session(wpa_s->eapol);
612 old_ssid = wpa_s->current_ssid;
613 wpa_s->current_ssid = NULL;
614 if (old_ssid != wpa_s->current_ssid)
615 wpas_notify_network_changed(wpa_s);
618 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
619 * pkcs11_engine_path, pkcs11_module_path.
621 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
623 * Clear forced success to clear EAP state for next
626 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
628 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
629 wpa_sm_set_config(wpa_s->wpa, NULL);
630 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
631 rsn_preauth_deinit(wpa_s->wpa);
633 old_ap_scan = wpa_s->conf->ap_scan;
634 wpa_config_free(wpa_s->conf);
636 if (old_ap_scan != wpa_s->conf->ap_scan)
637 wpas_notify_ap_scan_changed(wpa_s);
640 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
642 wpa_supplicant_clear_status(wpa_s);
643 wpa_s->reassociate = 1;
644 wpa_supplicant_req_scan(wpa_s, 0, 0);
645 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
650 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
653 struct wpa_global *global = eloop_ctx;
654 struct wpa_supplicant *wpa_s;
655 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
656 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
657 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
664 static wpa_cipher cipher_suite2driver(int cipher)
667 case WPA_CIPHER_NONE:
669 case WPA_CIPHER_WEP40:
671 case WPA_CIPHER_WEP104:
672 return CIPHER_WEP104;
673 case WPA_CIPHER_CCMP:
675 case WPA_CIPHER_TKIP:
682 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
685 case WPA_KEY_MGMT_NONE:
686 return KEY_MGMT_NONE;
687 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
688 return KEY_MGMT_802_1X_NO_WPA;
689 case WPA_KEY_MGMT_IEEE8021X:
690 return KEY_MGMT_802_1X;
691 case WPA_KEY_MGMT_WPA_NONE:
692 return KEY_MGMT_WPA_NONE;
693 case WPA_KEY_MGMT_FT_IEEE8021X:
694 return KEY_MGMT_FT_802_1X;
695 case WPA_KEY_MGMT_FT_PSK:
696 return KEY_MGMT_FT_PSK;
697 case WPA_KEY_MGMT_IEEE8021X_SHA256:
698 return KEY_MGMT_802_1X_SHA256;
699 case WPA_KEY_MGMT_PSK_SHA256:
700 return KEY_MGMT_PSK_SHA256;
701 case WPA_KEY_MGMT_WPS:
703 case WPA_KEY_MGMT_PSK:
710 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
711 struct wpa_ssid *ssid,
712 struct wpa_ie_data *ie)
714 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
717 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
718 "from association info");
723 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
725 if (!(ie->group_cipher & ssid->group_cipher)) {
726 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
727 "cipher 0x%x (mask 0x%x) - reject",
728 ie->group_cipher, ssid->group_cipher);
731 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
732 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
733 "cipher 0x%x (mask 0x%x) - reject",
734 ie->pairwise_cipher, ssid->pairwise_cipher);
737 if (!(ie->key_mgmt & ssid->key_mgmt)) {
738 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
739 "management 0x%x (mask 0x%x) - reject",
740 ie->key_mgmt, ssid->key_mgmt);
744 #ifdef CONFIG_IEEE80211W
745 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
746 ssid->ieee80211w == IEEE80211W_REQUIRED) {
747 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
748 "that does not support management frame protection - "
752 #endif /* CONFIG_IEEE80211W */
759 * wpa_supplicant_set_suites - Set authentication and encryption parameters
760 * @wpa_s: Pointer to wpa_supplicant data
761 * @bss: Scan results for the selected BSS, or %NULL if not available
762 * @ssid: Configuration data for the selected network
763 * @wpa_ie: Buffer for the WPA/RSN IE
764 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
765 * used buffer length in case the functions returns success.
766 * Returns: 0 on success or -1 on failure
768 * This function is used to configure authentication and encryption parameters
769 * based on the network configuration and scan result for the selected BSS (if
772 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
773 struct wpa_scan_res *bss,
774 struct wpa_ssid *ssid,
775 u8 *wpa_ie, size_t *wpa_ie_len)
777 struct wpa_ie_data ie;
779 const u8 *bss_wpa, *bss_rsn;
782 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
783 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
785 bss_wpa = bss_rsn = NULL;
787 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
788 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
789 (ie.group_cipher & ssid->group_cipher) &&
790 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
791 (ie.key_mgmt & ssid->key_mgmt)) {
792 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
793 proto = WPA_PROTO_RSN;
794 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
795 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
796 (ie.group_cipher & ssid->group_cipher) &&
797 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
798 (ie.key_mgmt & ssid->key_mgmt)) {
799 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
800 proto = WPA_PROTO_WPA;
802 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
805 if (ssid->proto & WPA_PROTO_RSN)
806 proto = WPA_PROTO_RSN;
808 proto = WPA_PROTO_WPA;
809 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
810 os_memset(&ie, 0, sizeof(ie));
811 ie.group_cipher = ssid->group_cipher;
812 ie.pairwise_cipher = ssid->pairwise_cipher;
813 ie.key_mgmt = ssid->key_mgmt;
814 #ifdef CONFIG_IEEE80211W
815 ie.mgmt_group_cipher =
816 ssid->ieee80211w != NO_IEEE80211W ?
817 WPA_CIPHER_AES_128_CMAC : 0;
818 #endif /* CONFIG_IEEE80211W */
819 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
825 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
826 "pairwise %d key_mgmt %d proto %d",
827 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
828 #ifdef CONFIG_IEEE80211W
829 if (ssid->ieee80211w) {
830 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
831 ie.mgmt_group_cipher);
833 #endif /* CONFIG_IEEE80211W */
835 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
836 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
837 !!(ssid->proto & WPA_PROTO_RSN));
839 if (bss || !wpa_s->ap_ies_from_associnfo) {
840 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
841 bss_wpa ? 2 + bss_wpa[1] : 0) ||
842 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
843 bss_rsn ? 2 + bss_rsn[1] : 0))
847 sel = ie.group_cipher & ssid->group_cipher;
848 if (sel & WPA_CIPHER_CCMP) {
849 wpa_s->group_cipher = WPA_CIPHER_CCMP;
850 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
851 } else if (sel & WPA_CIPHER_TKIP) {
852 wpa_s->group_cipher = WPA_CIPHER_TKIP;
853 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
854 } else if (sel & WPA_CIPHER_WEP104) {
855 wpa_s->group_cipher = WPA_CIPHER_WEP104;
856 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
857 } else if (sel & WPA_CIPHER_WEP40) {
858 wpa_s->group_cipher = WPA_CIPHER_WEP40;
859 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
861 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
865 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
866 if (sel & WPA_CIPHER_CCMP) {
867 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
868 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
869 } else if (sel & WPA_CIPHER_TKIP) {
870 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
871 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
872 } else if (sel & WPA_CIPHER_NONE) {
873 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
874 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
876 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
881 sel = ie.key_mgmt & ssid->key_mgmt;
883 #ifdef CONFIG_IEEE80211R
884 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
885 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
886 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
887 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
888 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
889 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
890 #endif /* CONFIG_IEEE80211R */
891 #ifdef CONFIG_IEEE80211W
892 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
893 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
894 wpa_msg(wpa_s, MSG_DEBUG,
895 "WPA: using KEY_MGMT 802.1X with SHA256");
896 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
897 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
898 wpa_msg(wpa_s, MSG_DEBUG,
899 "WPA: using KEY_MGMT PSK with SHA256");
900 #endif /* CONFIG_IEEE80211W */
901 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
902 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
903 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
904 } else if (sel & WPA_KEY_MGMT_PSK) {
905 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
906 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
907 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
908 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
909 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
911 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
912 "key management type.");
916 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
917 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
918 wpa_s->pairwise_cipher);
919 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
921 #ifdef CONFIG_IEEE80211W
922 sel = ie.mgmt_group_cipher;
923 if (ssid->ieee80211w == NO_IEEE80211W ||
924 !(ie.capabilities & WPA_CAPABILITY_MFPC))
926 if (sel & WPA_CIPHER_AES_128_CMAC) {
927 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
928 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
931 wpa_s->mgmt_group_cipher = 0;
932 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
934 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
935 wpa_s->mgmt_group_cipher);
936 #endif /* CONFIG_IEEE80211W */
938 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
939 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
944 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
945 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
947 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
954 * wpa_supplicant_associate - Request association
955 * @wpa_s: Pointer to wpa_supplicant data
956 * @bss: Scan results for the selected BSS, or %NULL if not available
957 * @ssid: Configuration data for the selected network
959 * This function is used to request %wpa_supplicant to associate with a BSS.
961 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
962 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
966 int use_crypt, ret, i, bssid_changed;
967 int algs = AUTH_ALG_OPEN_SYSTEM;
968 wpa_cipher cipher_pairwise, cipher_group;
969 struct wpa_driver_associate_params params;
970 int wep_keys_set = 0;
971 struct wpa_driver_capa capa;
972 int assoc_failed = 0;
973 struct wpa_ssid *old_ssid;
975 if (ssid->mode == 2) {
977 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
978 wpa_printf(MSG_INFO, "Driver does not support AP "
982 wpa_supplicant_create_ap(wpa_s, ssid);
983 #else /* CONFIG_AP */
984 wpa_printf(MSG_ERROR, "AP mode support not included in the "
986 #endif /* CONFIG_AP */
990 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
991 sme_authenticate(wpa_s, bss, ssid);
995 wpa_s->reassociate = 0;
997 #ifdef CONFIG_IEEE80211R
999 #endif /* CONFIG_IEEE80211R */
1000 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1001 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1002 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1003 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
1004 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1005 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1006 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1008 wpas_notify_bssid_changed(wpa_s);
1009 #ifdef CONFIG_IEEE80211R
1010 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1011 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1013 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
1015 /* Prepare for the next transition */
1016 wpa_ft_prepare_auth_request(wpa_s->wpa);
1018 #endif /* CONFIG_IEEE80211R */
1020 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1021 wpa_s->conf->ap_scan == 2 &&
1022 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1023 /* Use ap_scan==1 style network selection to find the network
1025 wpa_s->scan_req = 2;
1026 wpa_s->reassociate = 1;
1027 wpa_supplicant_req_scan(wpa_s, 0, 0);
1029 #endif /* CONFIG_WPS */
1031 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1032 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1033 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1035 wpa_supplicant_cancel_scan(wpa_s);
1037 /* Starting new association, so clear the possibly used WPA IE from the
1038 * previous association. */
1039 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1041 #ifdef IEEE8021X_EAPOL
1042 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1044 if (ssid->non_leap == 0)
1045 algs = AUTH_ALG_LEAP;
1047 algs |= AUTH_ALG_LEAP;
1050 #endif /* IEEE8021X_EAPOL */
1051 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1052 if (ssid->auth_alg) {
1054 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1055 algs |= AUTH_ALG_OPEN_SYSTEM;
1056 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1057 algs |= AUTH_ALG_SHARED_KEY;
1058 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1059 algs |= AUTH_ALG_LEAP;
1060 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1064 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1065 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1066 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1067 WPA_KEY_MGMT_FT_IEEE8021X |
1068 WPA_KEY_MGMT_FT_PSK |
1069 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1070 WPA_KEY_MGMT_PSK_SHA256))) {
1071 int try_opportunistic;
1072 try_opportunistic = ssid->proactive_key_caching &&
1073 (ssid->proto & WPA_PROTO_RSN);
1074 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1075 wpa_s->current_ssid,
1076 try_opportunistic) == 0)
1077 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1078 wpa_ie_len = sizeof(wpa_ie);
1079 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1080 wpa_ie, &wpa_ie_len)) {
1081 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1082 "management and encryption suites");
1085 } else if (ssid->key_mgmt &
1086 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1087 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1088 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1089 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1090 wpa_ie_len = sizeof(wpa_ie);
1091 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1092 wpa_ie, &wpa_ie_len)) {
1093 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1094 "management and encryption suites (no scan "
1099 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1100 struct wpabuf *wps_ie;
1101 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1102 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1103 wpa_ie_len = wpabuf_len(wps_ie);
1104 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1107 wpabuf_free(wps_ie);
1108 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1109 #endif /* CONFIG_WPS */
1111 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1115 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1117 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1118 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1119 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1120 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1121 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1123 if (wpa_set_wep_keys(wpa_s, ssid)) {
1128 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1131 #ifdef IEEE8021X_EAPOL
1132 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1133 if ((ssid->eapol_flags &
1134 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1135 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1139 /* Assume that dynamic WEP-104 keys will be used and
1140 * set cipher suites in order for drivers to expect
1142 cipher_pairwise = cipher_group = CIPHER_WEP104;
1145 #endif /* IEEE8021X_EAPOL */
1147 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1148 /* Set the key before (and later after) association */
1149 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1152 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1153 os_memset(¶ms, 0, sizeof(params));
1155 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1156 params.bssid = bss->bssid;
1157 params.ssid = ie ? ie + 2 : (u8 *) "";
1158 params.ssid_len = ie ? ie[1] : 0;
1159 params.freq = bss->freq;
1161 params.ssid = ssid->ssid;
1162 params.ssid_len = ssid->ssid_len;
1164 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1165 params.freq = ssid->frequency; /* Initial channel for IBSS */
1166 params.wpa_ie = wpa_ie;
1167 params.wpa_ie_len = wpa_ie_len;
1168 params.pairwise_suite = cipher_pairwise;
1169 params.group_suite = cipher_group;
1170 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1171 params.auth_alg = algs;
1172 params.mode = ssid->mode;
1173 for (i = 0; i < NUM_WEP_KEYS; i++) {
1174 if (ssid->wep_key_len[i])
1175 params.wep_key[i] = ssid->wep_key[i];
1176 params.wep_key_len[i] = ssid->wep_key_len[i];
1178 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1180 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1181 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1182 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1183 params.passphrase = ssid->passphrase;
1185 params.psk = ssid->psk;
1188 params.drop_unencrypted = use_crypt;
1190 #ifdef CONFIG_IEEE80211W
1191 switch (ssid->ieee80211w) {
1193 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1195 case IEEE80211W_OPTIONAL:
1196 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1198 case IEEE80211W_REQUIRED:
1199 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1202 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1203 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1204 struct wpa_ie_data ie;
1205 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1207 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1208 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1210 params.mgmt_frame_protection =
1211 MGMT_FRAME_PROTECTION_REQUIRED;
1214 #endif /* CONFIG_IEEE80211W */
1216 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1217 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1219 ret = wpa_drv_associate(wpa_s, ¶ms);
1221 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1223 /* try to continue anyway; new association will be tried again
1228 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1229 /* Set the key after the association just in case association
1230 * cleared the previously configured key. */
1231 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1232 /* No need to timeout authentication since there is no key
1234 wpa_supplicant_cancel_auth_timeout(wpa_s);
1235 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1236 #ifdef CONFIG_IBSS_RSN
1237 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1238 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1239 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1240 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1242 * RSN IBSS authentication is per-STA and we can disable the
1243 * per-BSSID authentication.
1245 wpa_supplicant_cancel_auth_timeout(wpa_s);
1246 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1247 #endif /* CONFIG_IBSS_RSN */
1249 /* Timeout for IEEE 802.11 authentication and association */
1253 /* give IBSS a bit more time */
1254 timeout = ssid->mode ? 10 : 5;
1255 } else if (wpa_s->conf->ap_scan == 1) {
1256 /* give IBSS a bit more time */
1257 timeout = ssid->mode ? 20 : 10;
1259 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1262 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1263 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1264 /* Set static WEP keys again */
1265 wpa_set_wep_keys(wpa_s, ssid);
1268 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1270 * Do not allow EAP session resumption between different
1271 * network configurations.
1273 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1275 old_ssid = wpa_s->current_ssid;
1276 wpa_s->current_ssid = ssid;
1277 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1278 wpa_supplicant_initiate_eapol(wpa_s);
1279 if (old_ssid != wpa_s->current_ssid)
1280 wpas_notify_network_changed(wpa_s);
1285 * wpa_supplicant_disassociate - Disassociate the current connection
1286 * @wpa_s: Pointer to wpa_supplicant data
1287 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1289 * This function is used to request %wpa_supplicant to disassociate with the
1292 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1295 struct wpa_ssid *old_ssid;
1298 if (!is_zero_ether_addr(wpa_s->bssid)) {
1299 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1300 ieee80211_sta_disassociate(wpa_s, reason_code);
1302 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1303 addr = wpa_s->bssid;
1305 wpa_clear_keys(wpa_s, addr);
1306 wpa_supplicant_mark_disassoc(wpa_s);
1307 old_ssid = wpa_s->current_ssid;
1308 wpa_s->current_ssid = NULL;
1309 wpa_sm_set_config(wpa_s->wpa, NULL);
1310 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1311 if (old_ssid != wpa_s->current_ssid)
1312 wpas_notify_network_changed(wpa_s);
1317 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1318 * @wpa_s: Pointer to wpa_supplicant data
1319 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1321 * This function is used to request %wpa_supplicant to deauthenticate from the
1324 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1327 struct wpa_ssid *old_ssid;
1330 if (!is_zero_ether_addr(wpa_s->bssid)) {
1331 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1332 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1334 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1336 addr = wpa_s->bssid;
1338 wpa_clear_keys(wpa_s, addr);
1339 wpa_supplicant_mark_disassoc(wpa_s);
1340 old_ssid = wpa_s->current_ssid;
1341 wpa_s->current_ssid = NULL;
1342 wpa_sm_set_config(wpa_s->wpa, NULL);
1343 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1344 if (old_ssid != wpa_s->current_ssid)
1345 wpas_notify_network_changed(wpa_s);
1350 * wpa_supplicant_enable_network - Mark a configured network as enabled
1351 * @wpa_s: wpa_supplicant structure for a network interface
1352 * @ssid: wpa_ssid structure for a configured network or %NULL
1354 * Enables the specified network or all networks if no network specified.
1356 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1357 struct wpa_ssid *ssid)
1359 struct wpa_ssid *other_ssid;
1363 other_ssid = wpa_s->conf->ssid;
1364 while (other_ssid) {
1365 if (other_ssid == wpa_s->current_ssid &&
1366 other_ssid->disabled)
1367 wpa_s->reassociate = 1;
1369 was_disabled = other_ssid->disabled;
1371 other_ssid->disabled = 0;
1373 if (was_disabled != other_ssid->disabled)
1374 wpas_notify_network_enabled_changed(
1377 other_ssid = other_ssid->next;
1379 if (wpa_s->reassociate)
1380 wpa_supplicant_req_scan(wpa_s, 0, 0);
1381 } else if (wpa_s->current_ssid == NULL && ssid->disabled) {
1383 * Try to reassociate since there is no current configuration
1384 * and a new network was made available.
1386 wpa_s->reassociate = 1;
1387 wpa_supplicant_req_scan(wpa_s, 0, 0);
1389 was_disabled = ssid->disabled;
1393 if (was_disabled != ssid->disabled)
1394 wpas_notify_network_enabled_changed(wpa_s, ssid);
1400 * wpa_supplicant_disable_network - Mark a configured network as disabled
1401 * @wpa_s: wpa_supplicant structure for a network interface
1402 * @ssid: wpa_ssid structure for a configured network or %NULL
1404 * Disables the specified network or all networks if no network specified.
1406 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1407 struct wpa_ssid *ssid)
1409 struct wpa_ssid *other_ssid;
1413 other_ssid = wpa_s->conf->ssid;
1414 while (other_ssid) {
1415 was_disabled = other_ssid->disabled;
1417 other_ssid->disabled = 1;
1419 if (was_disabled != other_ssid->disabled)
1420 wpas_notify_network_enabled_changed(
1423 other_ssid = other_ssid->next;
1425 if (wpa_s->current_ssid)
1426 wpa_supplicant_disassociate(
1427 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1429 if (ssid == wpa_s->current_ssid)
1430 wpa_supplicant_disassociate(
1431 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1433 was_disabled = ssid->disabled;
1437 if (was_disabled != ssid->disabled)
1438 wpas_notify_network_enabled_changed(wpa_s, ssid);
1444 * wpa_supplicant_select_network - Attempt association with a network
1445 * @wpa_s: wpa_supplicant structure for a network interface
1446 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1448 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1449 struct wpa_ssid *ssid)
1452 struct wpa_ssid *other_ssid;
1454 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1455 wpa_supplicant_disassociate(
1456 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1459 * Mark all other networks disabled or mark all networks enabled if no
1460 * network specified.
1462 other_ssid = wpa_s->conf->ssid;
1463 while (other_ssid) {
1464 int was_disabled = other_ssid->disabled;
1466 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1468 if (was_disabled != other_ssid->disabled)
1469 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1471 other_ssid = other_ssid->next;
1473 wpa_s->disconnected = 0;
1474 wpa_s->reassociate = 1;
1475 wpa_supplicant_req_scan(wpa_s, 0, 0);
1478 wpas_notify_network_selected(wpa_s, ssid);
1483 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1484 * @wpa_s: wpa_supplicant structure for a network interface
1485 * @ap_scan: AP scan mode
1486 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1489 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1494 if (ap_scan < 0 || ap_scan > 2)
1497 old_ap_scan = wpa_s->conf->ap_scan;
1498 wpa_s->conf->ap_scan = ap_scan;
1500 if (old_ap_scan != wpa_s->conf->ap_scan)
1501 wpas_notify_ap_scan_changed(wpa_s);
1508 * wpa_supplicant_set_debug_params - Set global debug params
1509 * @global: wpa_global structure
1510 * @debug_level: debug level
1511 * @debug_timestamp: determines if show timestamp in debug data
1512 * @debug_show_keys: determines if show keys in debug data
1513 * Returns: 0 if succeed or -1 if debug_level has wrong value
1515 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1516 int debug_timestamp, int debug_show_keys)
1519 int old_level, old_timestamp, old_show_keys;
1521 /* check for allowed debuglevels */
1522 if (debug_level != MSG_MSGDUMP &&
1523 debug_level != MSG_DEBUG &&
1524 debug_level != MSG_INFO &&
1525 debug_level != MSG_WARNING &&
1526 debug_level != MSG_ERROR)
1529 old_level = wpa_debug_level;
1530 old_timestamp = wpa_debug_timestamp;
1531 old_show_keys = wpa_debug_show_keys;
1533 wpa_debug_level = debug_level;
1534 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1535 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1537 if (wpa_debug_level != old_level ||
1538 wpa_debug_timestamp != old_timestamp ||
1539 wpa_debug_show_keys != old_show_keys)
1540 wpas_notify_debug_params_changed(global);
1546 static void notify_bss_changes(struct wpa_supplicant *wpa_s,
1547 u8 (*prev_bssids)[ETH_ALEN], int prev_num,
1548 struct wpa_scan_results *new)
1552 new_num = new != NULL ? new->num : 0;
1553 if (prev_bssids == NULL)
1556 for (i = 0; i < prev_num; i++) {
1557 for (j = 0; j < new_num; j++) {
1558 if (!os_memcmp(prev_bssids[i], new->res[j]->bssid,
1563 wpas_notify_bss_removed(wpa_s, prev_bssids[i]);
1565 for (i = 0; i < new_num; i++) {
1566 for (j = 0; j < prev_num; j++) {
1567 if (!os_memcmp(new->res[i]->bssid, prev_bssids[j],
1572 wpas_notify_bss_added(wpa_s, new->res[i]->bssid);
1577 static struct wpa_scan_results * wpa_supplicant_get_scan_results_old(
1578 struct wpa_supplicant *wpa_s)
1580 #define SCAN_AP_LIMIT 128
1581 struct wpa_scan_result *results;
1583 struct wpa_scan_results *res;
1585 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1586 if (results == NULL) {
1587 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1592 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1593 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1595 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1599 if (num > SCAN_AP_LIMIT) {
1600 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1601 num, SCAN_AP_LIMIT);
1602 num = SCAN_AP_LIMIT;
1605 /* Convert old scan result data structure to the new one */
1606 res = os_zalloc(sizeof(*res));
1611 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1612 if (res->res == NULL) {
1618 for (i = 0; i < num; i++) {
1619 struct wpa_scan_result *bss = &results[i];
1620 struct wpa_scan_res *r;
1624 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1627 if (bss->mdie_present)
1630 r = os_zalloc(sizeof(*r) + ie_len);
1634 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1635 r->freq = bss->freq;
1636 r->caps = bss->caps;
1637 r->qual = bss->qual;
1638 r->noise = bss->noise;
1639 r->level = bss->level;
1643 pos = (u8 *) (r + 1);
1646 *pos++ = WLAN_EID_SSID;
1647 *pos++ = bss->ssid_len;
1648 os_memcpy(pos, bss->ssid, bss->ssid_len);
1649 pos += bss->ssid_len;
1652 /* Fake Supported Rate IE to include max rate */
1653 *pos++ = WLAN_EID_SUPP_RATES;
1655 *pos++ = bss->maxrate;
1658 if (bss->rsn_ie_len) {
1659 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1660 pos += bss->rsn_ie_len;
1663 if (bss->mdie_present) {
1664 os_memcpy(pos, bss->mdie, 5);
1668 if (bss->wpa_ie_len) {
1669 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1670 pos += bss->wpa_ie_len;
1673 res->res[res->num++] = r;
1683 * wpa_supplicant_get_scan_results - Get scan results
1684 * @wpa_s: Pointer to wpa_supplicant data
1685 * Returns: 0 on success, -1 on failure
1687 * This function is request the current scan results from the driver and stores
1688 * a local copy of the results in wpa_s->scan_res.
1690 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1692 int ret, i, prev_scan_res_num;
1693 u8 (*prev_scan_bssids)[ETH_ALEN];
1695 prev_scan_res_num = wpa_s->scan_res ? wpa_s->scan_res->num : 0;
1696 prev_scan_bssids = os_malloc(prev_scan_res_num * ETH_ALEN);
1698 if (prev_scan_bssids) {
1699 for (i = 0; i < prev_scan_res_num; i++) {
1700 os_memcpy(prev_scan_bssids[i],
1701 wpa_s->scan_res->res[i]->bssid, ETH_ALEN);
1704 wpa_printf(MSG_WARNING, "Not enough memory for old scan "
1706 prev_scan_res_num = 0;
1709 wpa_scan_results_free(wpa_s->scan_res);
1710 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1711 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1712 else if (wpa_s->driver->get_scan_results2 == NULL)
1713 wpa_s->scan_res = wpa_supplicant_get_scan_results_old(wpa_s);
1715 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1716 if (wpa_s->scan_res == NULL) {
1717 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1721 wpa_scan_sort_results(wpa_s->scan_res);
1724 notify_bss_changes(wpa_s, prev_scan_bssids, prev_scan_res_num,
1726 os_free(prev_scan_bssids);
1733 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1734 * @wpa_s: Pointer to wpa_supplicant data
1735 * Returns: A pointer to the current network structure or %NULL on failure
1737 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1739 struct wpa_ssid *entry;
1740 u8 ssid[MAX_SSID_LEN];
1746 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1747 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1748 wpa_printf(MSG_WARNING, "Could not read SSID from "
1753 res = wpa_drv_get_ssid(wpa_s, ssid);
1755 wpa_printf(MSG_WARNING, "Could not read SSID from "
1762 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1763 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1764 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1765 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1769 wired = wpa_s->conf->ap_scan == 0 &&
1770 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1772 entry = wpa_s->conf->ssid;
1774 if (!entry->disabled &&
1775 ((ssid_len == entry->ssid_len &&
1776 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1777 (!entry->bssid_set ||
1778 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1781 if (!entry->disabled &&
1782 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1783 (entry->ssid == NULL || entry->ssid_len == 0) &&
1784 (!entry->bssid_set ||
1785 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1787 #endif /* CONFIG_WPS */
1788 entry = entry->next;
1795 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1805 if (wpa_drivers[0] == NULL) {
1806 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1812 /* default to first driver in the list */
1813 wpa_s->driver = wpa_drivers[0];
1817 pos = os_strchr(name, ',');
1821 len = os_strlen(name);
1822 for (i = 0; wpa_drivers[i]; i++) {
1823 if (os_strlen(wpa_drivers[i]->name) == len &&
1824 os_strncmp(name, wpa_drivers[i]->name, len) ==
1826 wpa_s->driver = wpa_drivers[i];
1831 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1836 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1837 const u8 *buf, size_t len)
1839 struct wpa_supplicant *wpa_s = ctx;
1841 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1842 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1844 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1846 * There is possible race condition between receiving the
1847 * association event and the EAPOL frame since they are coming
1848 * through different paths from the driver. In order to avoid
1849 * issues in trying to process the EAPOL frame before receiving
1850 * association information, lets queue it for processing until
1851 * the association event is received.
1853 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1854 "received EAPOL frame");
1855 wpabuf_free(wpa_s->pending_eapol_rx);
1856 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1857 if (wpa_s->pending_eapol_rx) {
1858 os_get_time(&wpa_s->pending_eapol_rx_time);
1859 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1866 if (wpa_s->ap_iface) {
1867 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1870 #endif /* CONFIG_AP */
1872 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1873 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1874 "no key management is configured");
1878 if (wpa_s->eapol_received == 0 &&
1879 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1880 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1881 wpa_s->wpa_state != WPA_COMPLETED)) {
1882 /* Timeout for completing IEEE 802.1X and WPA authentication */
1883 wpa_supplicant_req_auth_timeout(
1885 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1886 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1887 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1890 wpa_s->eapol_received++;
1892 if (wpa_s->countermeasures) {
1893 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1898 #ifdef CONFIG_IBSS_RSN
1899 if (wpa_s->current_ssid &&
1900 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1901 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1904 #endif /* CONFIG_IBSS_RSN */
1906 /* Source address of the incoming EAPOL frame could be compared to the
1907 * current BSSID. However, it is possible that a centralized
1908 * Authenticator could be using another MAC address than the BSSID of
1909 * an AP, so just allow any address to be used for now. The replies are
1910 * still sent to the current BSSID (if available), though. */
1912 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1913 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1914 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1916 wpa_drv_poll(wpa_s);
1917 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1918 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1919 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1921 * Set portValid = TRUE here since we are going to skip 4-way
1922 * handshake processing which would normally set portValid. We
1923 * need this to allow the EAPOL state machines to be completed
1924 * without going through EAPOL-Key handshake.
1926 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1931 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1932 struct ieee80211_rx_status *rx_status)
1934 struct wpa_supplicant *wpa_s = ctx;
1935 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1940 * wpa_supplicant_driver_init - Initialize driver interface parameters
1941 * @wpa_s: Pointer to wpa_supplicant data
1942 * Returns: 0 on success, -1 on failure
1944 * This function is called to initialize driver interface parameters.
1945 * wpa_drv_init() must have been called before this function to initialize the
1948 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1950 static int interface_count = 0;
1952 if (wpa_s->driver->send_eapol) {
1953 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1955 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1957 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1958 wpa_drv_get_mac_addr(wpa_s),
1960 wpa_supplicant_rx_eapol, wpa_s, 0);
1961 if (wpa_s->l2 == NULL)
1965 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1966 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1970 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1971 MAC2STR(wpa_s->own_addr));
1973 if (wpa_s->bridge_ifname[0]) {
1974 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1975 " '%s'", wpa_s->bridge_ifname);
1976 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1979 wpa_supplicant_rx_eapol, wpa_s,
1981 if (wpa_s->l2_br == NULL) {
1982 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1983 "connection for the bridge interface '%s'",
1984 wpa_s->bridge_ifname);
1989 wpa_clear_keys(wpa_s, NULL);
1991 /* Make sure that TKIP countermeasures are not left enabled (could
1992 * happen if wpa_supplicant is killed during countermeasures. */
1993 wpa_drv_set_countermeasures(wpa_s, 0);
1995 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1996 wpa_drv_flush_pmkid(wpa_s);
1998 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1999 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
2006 static int wpa_supplicant_daemon(const char *pid_file)
2008 wpa_printf(MSG_DEBUG, "Daemonize..");
2009 return os_daemonize(pid_file);
2013 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2015 struct wpa_supplicant *wpa_s;
2017 wpa_s = os_zalloc(sizeof(*wpa_s));
2020 wpa_s->scan_req = 1;
2026 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2027 struct wpa_interface *iface)
2029 const char *ifname, *driver;
2030 struct wpa_driver_capa capa;
2032 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2033 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2034 iface->confname ? iface->confname : "N/A",
2035 iface->driver ? iface->driver : "default",
2036 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2037 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2039 if (iface->confname) {
2040 #ifdef CONFIG_BACKEND_FILE
2041 wpa_s->confname = os_rel2abs_path(iface->confname);
2042 if (wpa_s->confname == NULL) {
2043 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2044 "for configuration file '%s'.",
2048 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2049 iface->confname, wpa_s->confname);
2050 #else /* CONFIG_BACKEND_FILE */
2051 wpa_s->confname = os_strdup(iface->confname);
2052 #endif /* CONFIG_BACKEND_FILE */
2053 wpa_s->conf = wpa_config_read(wpa_s->confname);
2054 if (wpa_s->conf == NULL) {
2055 wpa_printf(MSG_ERROR, "Failed to read or parse "
2056 "configuration '%s'.", wpa_s->confname);
2061 * Override ctrl_interface and driver_param if set on command
2064 if (iface->ctrl_interface) {
2065 os_free(wpa_s->conf->ctrl_interface);
2066 wpa_s->conf->ctrl_interface =
2067 os_strdup(iface->ctrl_interface);
2070 if (iface->driver_param) {
2071 os_free(wpa_s->conf->driver_param);
2072 wpa_s->conf->driver_param =
2073 os_strdup(iface->driver_param);
2076 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2077 iface->driver_param);
2079 if (wpa_s->conf == NULL) {
2080 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2084 if (iface->ifname == NULL) {
2085 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2088 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2089 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2093 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2095 if (iface->bridge_ifname) {
2096 if (os_strlen(iface->bridge_ifname) >=
2097 sizeof(wpa_s->bridge_ifname)) {
2098 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2099 "name '%s'.", iface->bridge_ifname);
2102 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2103 sizeof(wpa_s->bridge_ifname));
2106 /* RSNA Supplicant Key Management - INITIALIZE */
2107 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2108 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2110 /* Initialize driver interface and register driver event handler before
2111 * L2 receive handler so that association events are processed before
2112 * EAPOL-Key packets if both become available for the same select()
2114 driver = iface->driver;
2116 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2119 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2120 if (wpa_s->drv_priv == NULL) {
2122 pos = os_strchr(driver, ',');
2124 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2125 "interface - try next driver wrapper");
2129 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2132 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2133 wpa_printf(MSG_ERROR, "Driver interface rejected "
2134 "driver_param '%s'", wpa_s->conf->driver_param);
2138 ifname = wpa_drv_get_ifname(wpa_s);
2139 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2140 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2141 "name with '%s'", ifname);
2142 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2145 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2148 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2149 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2151 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2153 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2154 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2155 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2156 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2157 "dot11RSNAConfigPMKLifetime");
2161 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2162 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2163 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2164 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2165 "dot11RSNAConfigPMKReauthThreshold");
2169 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2170 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2171 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2172 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2173 "dot11RSNAConfigSATimeout");
2177 if (wpa_supplicant_driver_init(wpa_s) < 0)
2180 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2181 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2182 wpa_printf(MSG_DEBUG, "Failed to set country");
2186 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2188 if (wpas_wps_init(wpa_s))
2191 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2193 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2195 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2196 if (wpa_s->ctrl_iface == NULL) {
2197 wpa_printf(MSG_ERROR,
2198 "Failed to initialize control interface '%s'.\n"
2199 "You may have another wpa_supplicant process "
2200 "already running or the file was\n"
2201 "left by an unclean termination of wpa_supplicant "
2202 "in which case you will need\n"
2203 "to manually remove this file before starting "
2204 "wpa_supplicant again.\n",
2205 wpa_s->conf->ctrl_interface);
2209 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2210 wpa_s->drv_flags = capa.flags;
2211 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2212 if (ieee80211_sta_init(wpa_s))
2215 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2218 #ifdef CONFIG_IBSS_RSN
2219 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2220 if (!wpa_s->ibss_rsn) {
2221 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2224 #endif /* CONFIG_IBSS_RSN */
2230 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2232 if (wpa_s->drv_priv) {
2233 wpa_supplicant_deauthenticate(wpa_s,
2234 WLAN_REASON_DEAUTH_LEAVING);
2236 wpa_drv_set_countermeasures(wpa_s, 0);
2237 wpa_clear_keys(wpa_s, NULL);
2240 wpas_notify_iface_removed(wpa_s);
2242 wpa_supplicant_cleanup(wpa_s);
2244 if (wpa_s->drv_priv)
2245 wpa_drv_deinit(wpa_s);
2250 * wpa_supplicant_add_iface - Add a new network interface
2251 * @global: Pointer to global data from wpa_supplicant_init()
2252 * @iface: Interface configuration options
2253 * Returns: Pointer to the created interface or %NULL on failure
2255 * This function is used to add new network interfaces for %wpa_supplicant.
2256 * This can be called before wpa_supplicant_run() to add interfaces before the
2257 * main event loop has been started. In addition, new interfaces can be added
2258 * dynamically while %wpa_supplicant is already running. This could happen,
2259 * e.g., when a hotplug network adapter is inserted.
2261 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2262 struct wpa_interface *iface)
2264 struct wpa_supplicant *wpa_s;
2265 struct wpa_interface t_iface;
2267 if (global == NULL || iface == NULL)
2270 wpa_s = wpa_supplicant_alloc();
2275 if (global->params.override_driver) {
2276 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2278 iface->driver, global->params.override_driver);
2279 t_iface.driver = global->params.override_driver;
2281 if (global->params.override_ctrl_interface) {
2282 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2283 "ctrl_interface ('%s' -> '%s')",
2284 iface->ctrl_interface,
2285 global->params.override_ctrl_interface);
2286 t_iface.ctrl_interface =
2287 global->params.override_ctrl_interface;
2289 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2290 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2292 wpa_supplicant_deinit_iface(wpa_s);
2297 wpa_s->global = global;
2299 /* Notify the control interfaces about new iface */
2300 if (wpas_notify_iface_added(wpa_s)) {
2301 wpa_supplicant_deinit_iface(wpa_s);
2306 wpa_s->next = global->ifaces;
2307 global->ifaces = wpa_s;
2309 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2316 * wpa_supplicant_remove_iface - Remove a network interface
2317 * @global: Pointer to global data from wpa_supplicant_init()
2318 * @wpa_s: Pointer to the network interface to be removed
2319 * Returns: 0 if interface was removed, -1 if interface was not found
2321 * This function can be used to dynamically remove network interfaces from
2322 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2323 * addition, this function is used to remove all remaining interfaces when
2324 * %wpa_supplicant is terminated.
2326 int wpa_supplicant_remove_iface(struct wpa_global *global,
2327 struct wpa_supplicant *wpa_s)
2329 struct wpa_supplicant *prev;
2331 /* Remove interface from the global list of interfaces */
2332 prev = global->ifaces;
2333 if (prev == wpa_s) {
2334 global->ifaces = wpa_s->next;
2336 while (prev && prev->next != wpa_s)
2340 prev->next = wpa_s->next;
2343 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2345 wpa_supplicant_deinit_iface(wpa_s);
2353 * wpa_supplicant_get_iface - Get a new network interface
2354 * @global: Pointer to global data from wpa_supplicant_init()
2355 * @ifname: Interface name
2356 * Returns: Pointer to the interface or %NULL if not found
2358 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2361 struct wpa_supplicant *wpa_s;
2363 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2364 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2372 * wpa_supplicant_init - Initialize %wpa_supplicant
2373 * @params: Parameters for %wpa_supplicant
2374 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2376 * This function is used to initialize %wpa_supplicant. After successful
2377 * initialization, the returned data pointer can be used to add and remove
2378 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2380 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2382 struct wpa_global *global;
2388 wpa_debug_open_file(params->wpa_debug_file_path);
2389 if (params->wpa_debug_syslog)
2390 wpa_debug_open_syslog();
2392 ret = eap_peer_register_methods();
2394 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2396 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2397 "the same EAP type.");
2402 ret = eap_server_register_methods();
2404 wpa_printf(MSG_ERROR, "Failed to register EAP server methods");
2406 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2407 "the same EAP type.");
2410 #endif /* CONFIG_AP */
2412 global = os_zalloc(sizeof(*global));
2415 global->params.daemonize = params->daemonize;
2416 global->params.wait_for_monitor = params->wait_for_monitor;
2417 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2418 if (params->pid_file)
2419 global->params.pid_file = os_strdup(params->pid_file);
2420 if (params->ctrl_interface)
2421 global->params.ctrl_interface =
2422 os_strdup(params->ctrl_interface);
2423 if (params->override_driver)
2424 global->params.override_driver =
2425 os_strdup(params->override_driver);
2426 if (params->override_ctrl_interface)
2427 global->params.override_ctrl_interface =
2428 os_strdup(params->override_ctrl_interface);
2429 wpa_debug_level = global->params.wpa_debug_level =
2430 params->wpa_debug_level;
2431 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2432 params->wpa_debug_show_keys;
2433 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2434 params->wpa_debug_timestamp;
2436 if (eloop_init(global)) {
2437 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2438 wpa_supplicant_deinit(global);
2442 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2443 if (global->ctrl_iface == NULL) {
2444 wpa_supplicant_deinit(global);
2448 if (wpas_notify_supplicant_initialized(global)) {
2449 wpa_supplicant_deinit(global);
2453 for (i = 0; wpa_drivers[i]; i++)
2454 global->drv_count++;
2455 if (global->drv_count == 0) {
2456 wpa_printf(MSG_ERROR, "No drivers enabled");
2457 wpa_supplicant_deinit(global);
2460 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2461 if (global->drv_priv == NULL) {
2462 wpa_supplicant_deinit(global);
2465 for (i = 0; wpa_drivers[i]; i++) {
2466 if (!wpa_drivers[i]->global_init)
2468 global->drv_priv[i] = wpa_drivers[i]->global_init();
2469 if (global->drv_priv[i] == NULL) {
2470 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2471 "'%s'", wpa_drivers[i]->name);
2472 wpa_supplicant_deinit(global);
2482 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2483 * @global: Pointer to global data from wpa_supplicant_init()
2484 * Returns: 0 after successful event loop run, -1 on failure
2486 * This function starts the main event loop and continues running as long as
2487 * there are any remaining events. In most cases, this function is running as
2488 * long as the %wpa_supplicant process in still in use.
2490 int wpa_supplicant_run(struct wpa_global *global)
2492 struct wpa_supplicant *wpa_s;
2494 if (global->params.daemonize &&
2495 wpa_supplicant_daemon(global->params.pid_file))
2498 if (global->params.wait_for_monitor) {
2499 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2500 if (wpa_s->ctrl_iface)
2501 wpa_supplicant_ctrl_iface_wait(
2505 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2506 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2515 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2516 * @global: Pointer to global data from wpa_supplicant_init()
2518 * This function is called to deinitialize %wpa_supplicant and to free all
2519 * allocated resources. Remaining network interfaces will also be removed.
2521 void wpa_supplicant_deinit(struct wpa_global *global)
2528 while (global->ifaces)
2529 wpa_supplicant_remove_iface(global, global->ifaces);
2531 if (global->ctrl_iface)
2532 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2534 wpas_notify_supplicant_deinitialized(global);
2536 eap_peer_unregister_methods();
2538 eap_server_unregister_methods();
2539 #endif /* CONFIG_AP */
2541 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2542 if (!global->drv_priv[i])
2544 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2546 os_free(global->drv_priv);
2550 if (global->params.pid_file) {
2551 os_daemonize_terminate(global->params.pid_file);
2552 os_free(global->params.pid_file);
2554 os_free(global->params.ctrl_interface);
2555 os_free(global->params.override_driver);
2556 os_free(global->params.override_ctrl_interface);
2559 wpa_debug_close_syslog();
2560 wpa_debug_close_file();