3 * Copyright (c) 2003-2011, 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 "crypto/random.h"
23 #include "crypto/sha1.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "eap_peer/eap.h"
26 #include "eap_server/eap_methods.h"
27 #include "rsn_supp/wpa.h"
30 #include "l2_packet/l2_packet.h"
31 #include "wpa_supplicant_i.h"
33 #include "ctrl_iface.h"
34 #include "pcsc_funcs.h"
35 #include "common/version.h"
36 #include "rsn_supp/preauth.h"
37 #include "rsn_supp/pmksa_cache.h"
38 #include "common/wpa_ctrl.h"
40 #include "common/ieee802_11_defs.h"
42 #include "blacklist.h"
43 #include "wpas_glue.h"
44 #include "wps_supplicant.h"
47 #include "gas_query.h"
49 #include "p2p_supplicant.h"
54 #include "offchannel.h"
56 const char *wpa_supplicant_version =
57 "wpa_supplicant v" VERSION_STR "\n"
58 "Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
60 const char *wpa_supplicant_license =
61 "This program is free software. You can distribute it and/or modify it\n"
62 "under the terms of the GNU General Public License version 2.\n"
64 "Alternatively, this software may be distributed under the terms of the\n"
65 "BSD license. See README and COPYING for more details.\n"
66 #ifdef EAP_TLS_OPENSSL
67 "\nThis product includes software developed by the OpenSSL Project\n"
68 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
69 #endif /* EAP_TLS_OPENSSL */
72 #ifndef CONFIG_NO_STDOUT_DEBUG
73 /* Long text divided into parts in order to fit in C89 strings size limits. */
74 const char *wpa_supplicant_full_license1 =
75 "This program is free software; you can redistribute it and/or modify\n"
76 "it under the terms of the GNU General Public License version 2 as\n"
77 "published by the Free Software Foundation.\n"
79 "This program is distributed in the hope that it will be useful,\n"
80 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
81 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
82 "GNU General Public License for more details.\n"
84 const char *wpa_supplicant_full_license2 =
85 "You should have received a copy of the GNU General Public License\n"
86 "along with this program; if not, write to the Free Software\n"
87 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
89 "Alternatively, this software may be distributed under the terms of the\n"
92 "Redistribution and use in source and binary forms, with or without\n"
93 "modification, are permitted provided that the following conditions are\n"
96 const char *wpa_supplicant_full_license3 =
97 "1. Redistributions of source code must retain the above copyright\n"
98 " notice, this list of conditions and the following disclaimer.\n"
100 "2. Redistributions in binary form must reproduce the above copyright\n"
101 " notice, this list of conditions and the following disclaimer in the\n"
102 " documentation and/or other materials provided with the distribution.\n"
104 const char *wpa_supplicant_full_license4 =
105 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
106 " names of its contributors may be used to endorse or promote products\n"
107 " derived from this software without specific prior written permission.\n"
109 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
110 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
111 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
112 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
113 const char *wpa_supplicant_full_license5 =
114 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
115 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
116 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
117 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
118 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
119 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
120 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
122 #endif /* CONFIG_NO_STDOUT_DEBUG */
124 extern int wpa_debug_level;
125 extern int wpa_debug_show_keys;
126 extern int wpa_debug_timestamp;
127 extern struct wpa_driver_ops *wpa_drivers[];
129 /* Configure default/group WEP keys for static WEP */
130 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
134 for (i = 0; i < NUM_WEP_KEYS; i++) {
135 if (ssid->wep_key_len[i] == 0)
139 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
140 i, i == ssid->wep_tx_keyidx, NULL, 0,
141 ssid->wep_key[i], ssid->wep_key_len[i]);
148 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
149 struct wpa_ssid *ssid)
156 /* IBSS/WPA-None uses only one key (Group) for both receiving and
157 * sending unicast and multicast packets. */
159 if (ssid->mode != WPAS_MODE_IBSS) {
160 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
161 "IBSS/ad-hoc) for WPA-None", ssid->mode);
165 if (!ssid->psk_set) {
166 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
171 switch (wpa_s->group_cipher) {
172 case WPA_CIPHER_CCMP:
173 os_memcpy(key, ssid->psk, 16);
177 case WPA_CIPHER_TKIP:
178 /* WPA-None uses the same Michael MIC key for both TX and RX */
179 os_memcpy(key, ssid->psk, 16 + 8);
180 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
185 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
186 "WPA-None", wpa_s->group_cipher);
190 /* TODO: should actually remember the previously used seq#, both for TX
191 * and RX from each STA.. */
193 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
197 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
199 struct wpa_supplicant *wpa_s = eloop_ctx;
200 const u8 *bssid = wpa_s->bssid;
201 if (is_zero_ether_addr(bssid))
202 bssid = wpa_s->pending_bssid;
203 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
205 wpa_blacklist_add(wpa_s, bssid);
206 wpa_sm_notify_disassoc(wpa_s->wpa);
207 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
208 wpa_s->reassociate = 1;
211 * If we timed out, the AP or the local radio may be busy.
212 * So, wait a second until scanning again.
214 wpa_supplicant_req_scan(wpa_s, 1, 0);
219 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
220 * @wpa_s: Pointer to wpa_supplicant data
221 * @sec: Number of seconds after which to time out authentication
222 * @usec: Number of microseconds after which to time out authentication
224 * This function is used to schedule a timeout for the current authentication
227 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
230 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
231 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
234 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
235 "%d usec", sec, usec);
236 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
237 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
242 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
243 * @wpa_s: Pointer to wpa_supplicant data
245 * This function is used to cancel authentication timeout scheduled with
246 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
249 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
251 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
252 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
253 wpa_blacklist_del(wpa_s, wpa_s->bssid);
258 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
259 * @wpa_s: Pointer to wpa_supplicant data
261 * This function is used to configure EAPOL state machine based on the selected
262 * authentication mode.
264 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
266 #ifdef IEEE8021X_EAPOL
267 struct eapol_config eapol_conf;
268 struct wpa_ssid *ssid = wpa_s->current_ssid;
270 #ifdef CONFIG_IBSS_RSN
271 if (ssid->mode == WPAS_MODE_IBSS &&
272 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
273 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
275 * RSN IBSS authentication is per-STA and we can disable the
276 * per-BSSID EAPOL authentication.
278 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
279 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
280 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
283 #endif /* CONFIG_IBSS_RSN */
285 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
286 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
288 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
289 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
290 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
292 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
294 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
295 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
296 eapol_conf.accept_802_1x_keys = 1;
297 eapol_conf.required_keys = 0;
298 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
299 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
301 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
302 eapol_conf.required_keys |=
303 EAPOL_REQUIRE_KEY_BROADCAST;
306 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
307 eapol_conf.required_keys = 0;
310 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
311 eapol_conf.workaround = ssid->eap_workaround;
312 eapol_conf.eap_disabled =
313 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
314 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
315 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
316 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
317 #endif /* IEEE8021X_EAPOL */
322 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
323 * @wpa_s: Pointer to wpa_supplicant data
324 * @ssid: Configuration data for the network
326 * This function is used to configure WPA state machine and related parameters
327 * to a mode where WPA is not enabled. This is called as part of the
328 * authentication configuration when the selected network does not use WPA.
330 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
331 struct wpa_ssid *ssid)
335 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
336 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
337 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
338 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
340 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
341 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
342 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
343 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
344 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
345 wpa_s->group_cipher = WPA_CIPHER_NONE;
346 wpa_s->mgmt_group_cipher = 0;
348 for (i = 0; i < NUM_WEP_KEYS; i++) {
349 if (ssid->wep_key_len[i] > 5) {
350 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
351 wpa_s->group_cipher = WPA_CIPHER_WEP104;
353 } else if (ssid->wep_key_len[i] > 0) {
354 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
355 wpa_s->group_cipher = WPA_CIPHER_WEP40;
360 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
361 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
362 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
363 wpa_s->pairwise_cipher);
364 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
365 #ifdef CONFIG_IEEE80211W
366 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
367 wpa_s->mgmt_group_cipher);
368 #endif /* CONFIG_IEEE80211W */
370 pmksa_cache_clear_current(wpa_s->wpa);
374 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
376 bgscan_deinit(wpa_s);
377 scard_deinit(wpa_s->scard);
379 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
380 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
381 l2_packet_deinit(wpa_s->l2);
384 l2_packet_deinit(wpa_s->l2_br);
388 if (wpa_s->ctrl_iface) {
389 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
390 wpa_s->ctrl_iface = NULL;
392 if (wpa_s->conf != NULL) {
393 struct wpa_ssid *ssid;
394 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
395 wpas_notify_network_removed(wpa_s, ssid);
396 wpa_config_free(wpa_s->conf);
400 os_free(wpa_s->confname);
401 wpa_s->confname = NULL;
403 wpa_sm_set_eapol(wpa_s->wpa, NULL);
404 eapol_sm_deinit(wpa_s->eapol);
407 rsn_preauth_deinit(wpa_s->wpa);
410 wpa_tdls_deinit(wpa_s->wpa);
411 #endif /* CONFIG_TDLS */
413 pmksa_candidate_free(wpa_s->wpa);
414 wpa_sm_deinit(wpa_s->wpa);
416 wpa_blacklist_clear(wpa_s);
418 wpa_bss_deinit(wpa_s);
420 wpa_supplicant_cancel_scan(wpa_s);
421 wpa_supplicant_cancel_auth_timeout(wpa_s);
423 ieee80211_sta_deinit(wpa_s);
425 wpas_wps_deinit(wpa_s);
427 wpabuf_free(wpa_s->pending_eapol_rx);
428 wpa_s->pending_eapol_rx = NULL;
430 #ifdef CONFIG_IBSS_RSN
431 ibss_rsn_deinit(wpa_s->ibss_rsn);
432 wpa_s->ibss_rsn = NULL;
433 #endif /* CONFIG_IBSS_RSN */
438 wpa_supplicant_ap_deinit(wpa_s);
439 #endif /* CONFIG_AP */
442 wpas_p2p_deinit(wpa_s);
443 #endif /* CONFIG_P2P */
445 #ifdef CONFIG_OFFCHANNEL
446 offchannel_deinit(wpa_s);
447 #endif /* CONFIG_OFFCHANNEL */
449 os_free(wpa_s->next_scan_freqs);
450 wpa_s->next_scan_freqs = NULL;
452 gas_query_deinit(wpa_s->gas);
458 * wpa_clear_keys - Clear keys configured for the driver
459 * @wpa_s: Pointer to wpa_supplicant data
460 * @addr: Previously used BSSID or %NULL if not available
462 * This function clears the encryption keys that has been previously configured
465 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
467 if (wpa_s->keys_cleared) {
468 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
469 * timing issues with keys being cleared just before new keys
470 * are set or just after association or something similar. This
471 * shows up in group key handshake failing often because of the
472 * client not receiving the first encrypted packets correctly.
473 * Skipping some of the extra key clearing steps seems to help
474 * in completing group key handshake more reliably. */
475 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
476 "skip key clearing");
480 /* MLME-DELETEKEYS.request */
481 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
482 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
483 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
484 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
485 #ifdef CONFIG_IEEE80211W
486 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
487 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
488 #endif /* CONFIG_IEEE80211W */
490 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
492 /* MLME-SETPROTECTION.request(None) */
493 wpa_drv_mlme_setprotection(
495 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
496 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
498 wpa_s->keys_cleared = 1;
503 * wpa_supplicant_state_txt - Get the connection state name as a text string
504 * @state: State (wpa_state; WPA_*)
505 * Returns: The state name as a printable text string
507 const char * wpa_supplicant_state_txt(enum wpa_states state)
510 case WPA_DISCONNECTED:
511 return "DISCONNECTED";
514 case WPA_INTERFACE_DISABLED:
515 return "INTERFACE_DISABLED";
518 case WPA_AUTHENTICATING:
519 return "AUTHENTICATING";
520 case WPA_ASSOCIATING:
521 return "ASSOCIATING";
524 case WPA_4WAY_HANDSHAKE:
525 return "4WAY_HANDSHAKE";
526 case WPA_GROUP_HANDSHAKE:
527 return "GROUP_HANDSHAKE";
538 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
540 if (wpas_driver_bss_selection(wpa_s))
542 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
545 bgscan_deinit(wpa_s);
546 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
547 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
548 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
551 * Live without bgscan; it is only used as a roaming
552 * optimization, so the initial connection is not
556 wpa_s->bgscan_ssid = wpa_s->current_ssid;
558 wpa_s->bgscan_ssid = NULL;
562 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
564 if (wpa_s->bgscan_ssid != NULL) {
565 bgscan_deinit(wpa_s);
566 wpa_s->bgscan_ssid = NULL;
570 #endif /* CONFIG_BGSCAN */
574 * wpa_supplicant_set_state - Set current connection state
575 * @wpa_s: Pointer to wpa_supplicant data
576 * @state: The new connection state
578 * This function is called whenever the connection state changes, e.g.,
579 * association is completed for WPA/WPA2 4-Way Handshake is started.
581 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
582 enum wpa_states state)
584 enum wpa_states old_state = wpa_s->wpa_state;
586 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
587 wpa_supplicant_state_txt(wpa_s->wpa_state),
588 wpa_supplicant_state_txt(state));
590 if (state != WPA_SCANNING)
591 wpa_supplicant_notify_scanning(wpa_s, 0);
593 if (state == WPA_COMPLETED && wpa_s->new_connection) {
594 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
595 struct wpa_ssid *ssid = wpa_s->current_ssid;
596 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
597 MACSTR " completed %s [id=%d id_str=%s]",
598 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
599 "(reauth)" : "(auth)",
600 ssid ? ssid->id : -1,
601 ssid && ssid->id_str ? ssid->id_str : "");
602 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
603 wpa_s->new_connection = 0;
604 wpa_s->reassociated_connection = 1;
605 wpa_drv_set_operstate(wpa_s, 1);
606 #ifndef IEEE8021X_EAPOL
607 wpa_drv_set_supp_port(wpa_s, 1);
608 #endif /* IEEE8021X_EAPOL */
609 wpa_s->after_wps = 0;
611 wpas_p2p_completed(wpa_s);
612 #endif /* CONFIG_P2P */
613 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
614 state == WPA_ASSOCIATED) {
615 wpa_s->new_connection = 1;
616 wpa_drv_set_operstate(wpa_s, 0);
617 #ifndef IEEE8021X_EAPOL
618 wpa_drv_set_supp_port(wpa_s, 0);
619 #endif /* IEEE8021X_EAPOL */
621 wpa_s->wpa_state = state;
624 if (state == WPA_COMPLETED)
625 wpa_supplicant_start_bgscan(wpa_s);
627 wpa_supplicant_stop_bgscan(wpa_s);
628 #endif /* CONFIG_BGSCAN */
630 if (wpa_s->wpa_state != old_state) {
631 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
633 if (wpa_s->wpa_state == WPA_COMPLETED ||
634 old_state == WPA_COMPLETED)
635 wpas_notify_auth_changed(wpa_s);
640 void wpa_supplicant_terminate_proc(struct wpa_global *global)
644 struct wpa_supplicant *wpa_s = global->ifaces;
646 if (wpas_wps_terminate_pending(wpa_s) == 1)
650 #endif /* CONFIG_WPS */
657 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
659 struct wpa_global *global = signal_ctx;
660 struct wpa_supplicant *wpa_s;
661 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
662 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
665 wpa_supplicant_terminate_proc(global);
669 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
671 enum wpa_states old_state = wpa_s->wpa_state;
673 wpa_s->pairwise_cipher = 0;
674 wpa_s->group_cipher = 0;
675 wpa_s->mgmt_group_cipher = 0;
677 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
678 wpa_s->wpa_state = WPA_DISCONNECTED;
680 if (wpa_s->wpa_state != old_state)
681 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
686 * wpa_supplicant_reload_configuration - Reload configuration data
687 * @wpa_s: Pointer to wpa_supplicant data
688 * Returns: 0 on success or -1 if configuration parsing failed
690 * This function can be used to request that the configuration data is reloaded
691 * (e.g., after configuration file change). This function is reloading
692 * configuration only for one interface, so this may need to be called multiple
693 * times if %wpa_supplicant is controlling multiple interfaces and all
694 * interfaces need reconfiguration.
696 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
698 struct wpa_config *conf;
702 if (wpa_s->confname == NULL)
704 conf = wpa_config_read(wpa_s->confname);
706 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
707 "file '%s' - exiting", wpa_s->confname);
710 conf->changed_parameters = (unsigned int) -1;
712 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
713 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
714 os_strcmp(conf->ctrl_interface,
715 wpa_s->conf->ctrl_interface) != 0);
717 if (reconf_ctrl && wpa_s->ctrl_iface) {
718 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
719 wpa_s->ctrl_iface = NULL;
722 eapol_sm_invalidate_cached_session(wpa_s->eapol);
723 if (wpa_s->current_ssid) {
724 wpa_supplicant_deauthenticate(wpa_s,
725 WLAN_REASON_DEAUTH_LEAVING);
729 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
730 * pkcs11_engine_path, pkcs11_module_path.
732 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
734 * Clear forced success to clear EAP state for next
737 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
739 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
740 wpa_sm_set_config(wpa_s->wpa, NULL);
741 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
742 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
743 rsn_preauth_deinit(wpa_s->wpa);
745 old_ap_scan = wpa_s->conf->ap_scan;
746 wpa_config_free(wpa_s->conf);
748 if (old_ap_scan != wpa_s->conf->ap_scan)
749 wpas_notify_ap_scan_changed(wpa_s);
752 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
754 wpa_supplicant_update_config(wpa_s);
756 wpa_supplicant_clear_status(wpa_s);
757 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
758 wpa_s->reassociate = 1;
759 wpa_supplicant_req_scan(wpa_s, 0, 0);
761 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
766 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
768 struct wpa_global *global = signal_ctx;
769 struct wpa_supplicant *wpa_s;
770 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
771 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
773 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
774 wpa_supplicant_terminate_proc(global);
780 enum wpa_cipher cipher_suite2driver(int cipher)
783 case WPA_CIPHER_NONE:
785 case WPA_CIPHER_WEP40:
787 case WPA_CIPHER_WEP104:
788 return CIPHER_WEP104;
789 case WPA_CIPHER_CCMP:
791 case WPA_CIPHER_TKIP:
798 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
801 case WPA_KEY_MGMT_NONE:
802 return KEY_MGMT_NONE;
803 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
804 return KEY_MGMT_802_1X_NO_WPA;
805 case WPA_KEY_MGMT_IEEE8021X:
806 return KEY_MGMT_802_1X;
807 case WPA_KEY_MGMT_WPA_NONE:
808 return KEY_MGMT_WPA_NONE;
809 case WPA_KEY_MGMT_FT_IEEE8021X:
810 return KEY_MGMT_FT_802_1X;
811 case WPA_KEY_MGMT_FT_PSK:
812 return KEY_MGMT_FT_PSK;
813 case WPA_KEY_MGMT_IEEE8021X_SHA256:
814 return KEY_MGMT_802_1X_SHA256;
815 case WPA_KEY_MGMT_PSK_SHA256:
816 return KEY_MGMT_PSK_SHA256;
817 case WPA_KEY_MGMT_WPS:
819 case WPA_KEY_MGMT_PSK:
826 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
827 struct wpa_ssid *ssid,
828 struct wpa_ie_data *ie)
830 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
833 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
834 "from association info");
839 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
841 if (!(ie->group_cipher & ssid->group_cipher)) {
842 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
843 "cipher 0x%x (mask 0x%x) - reject",
844 ie->group_cipher, ssid->group_cipher);
847 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
848 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
849 "cipher 0x%x (mask 0x%x) - reject",
850 ie->pairwise_cipher, ssid->pairwise_cipher);
853 if (!(ie->key_mgmt & ssid->key_mgmt)) {
854 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
855 "management 0x%x (mask 0x%x) - reject",
856 ie->key_mgmt, ssid->key_mgmt);
860 #ifdef CONFIG_IEEE80211W
861 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
862 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
863 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
864 "that does not support management frame protection - "
868 #endif /* CONFIG_IEEE80211W */
875 * wpa_supplicant_set_suites - Set authentication and encryption parameters
876 * @wpa_s: Pointer to wpa_supplicant data
877 * @bss: Scan results for the selected BSS, or %NULL if not available
878 * @ssid: Configuration data for the selected network
879 * @wpa_ie: Buffer for the WPA/RSN IE
880 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
881 * used buffer length in case the functions returns success.
882 * Returns: 0 on success or -1 on failure
884 * This function is used to configure authentication and encryption parameters
885 * based on the network configuration and scan result for the selected BSS (if
888 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
889 struct wpa_bss *bss, struct wpa_ssid *ssid,
890 u8 *wpa_ie, size_t *wpa_ie_len)
892 struct wpa_ie_data ie;
894 const u8 *bss_wpa, *bss_rsn;
897 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
898 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
900 bss_wpa = bss_rsn = NULL;
902 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
903 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
904 (ie.group_cipher & ssid->group_cipher) &&
905 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
906 (ie.key_mgmt & ssid->key_mgmt)) {
907 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
908 proto = WPA_PROTO_RSN;
909 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
910 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
911 (ie.group_cipher & ssid->group_cipher) &&
912 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
913 (ie.key_mgmt & ssid->key_mgmt)) {
914 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
915 proto = WPA_PROTO_WPA;
917 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
920 if (ssid->proto & WPA_PROTO_RSN)
921 proto = WPA_PROTO_RSN;
923 proto = WPA_PROTO_WPA;
924 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
925 os_memset(&ie, 0, sizeof(ie));
926 ie.group_cipher = ssid->group_cipher;
927 ie.pairwise_cipher = ssid->pairwise_cipher;
928 ie.key_mgmt = ssid->key_mgmt;
929 #ifdef CONFIG_IEEE80211W
930 ie.mgmt_group_cipher =
931 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
932 WPA_CIPHER_AES_128_CMAC : 0;
933 #endif /* CONFIG_IEEE80211W */
934 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
935 "based on configuration");
940 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
941 "pairwise %d key_mgmt %d proto %d",
942 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
943 #ifdef CONFIG_IEEE80211W
944 if (ssid->ieee80211w) {
945 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
946 ie.mgmt_group_cipher);
948 #endif /* CONFIG_IEEE80211W */
950 wpa_s->wpa_proto = proto;
951 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
952 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
953 !!(ssid->proto & WPA_PROTO_RSN));
955 if (bss || !wpa_s->ap_ies_from_associnfo) {
956 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
957 bss_wpa ? 2 + bss_wpa[1] : 0) ||
958 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
959 bss_rsn ? 2 + bss_rsn[1] : 0))
963 sel = ie.group_cipher & ssid->group_cipher;
964 if (sel & WPA_CIPHER_CCMP) {
965 wpa_s->group_cipher = WPA_CIPHER_CCMP;
966 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
967 } else if (sel & WPA_CIPHER_TKIP) {
968 wpa_s->group_cipher = WPA_CIPHER_TKIP;
969 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
970 } else if (sel & WPA_CIPHER_WEP104) {
971 wpa_s->group_cipher = WPA_CIPHER_WEP104;
972 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
973 } else if (sel & WPA_CIPHER_WEP40) {
974 wpa_s->group_cipher = WPA_CIPHER_WEP40;
975 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
977 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
982 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
983 if (sel & WPA_CIPHER_CCMP) {
984 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
985 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
986 } else if (sel & WPA_CIPHER_TKIP) {
987 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
988 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
989 } else if (sel & WPA_CIPHER_NONE) {
990 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
991 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
993 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
998 sel = ie.key_mgmt & ssid->key_mgmt;
1000 #ifdef CONFIG_IEEE80211R
1001 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1002 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1003 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1004 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1005 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1006 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1007 #endif /* CONFIG_IEEE80211R */
1008 #ifdef CONFIG_IEEE80211W
1009 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1010 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1011 wpa_dbg(wpa_s, MSG_DEBUG,
1012 "WPA: using KEY_MGMT 802.1X with SHA256");
1013 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1014 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1015 wpa_dbg(wpa_s, MSG_DEBUG,
1016 "WPA: using KEY_MGMT PSK with SHA256");
1017 #endif /* CONFIG_IEEE80211W */
1018 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1019 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1020 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1021 } else if (sel & WPA_KEY_MGMT_PSK) {
1022 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1023 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1024 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1025 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1026 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1028 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1029 "authenticated key management type");
1033 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1034 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1035 wpa_s->pairwise_cipher);
1036 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1038 #ifdef CONFIG_IEEE80211W
1039 sel = ie.mgmt_group_cipher;
1040 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1041 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1043 if (sel & WPA_CIPHER_AES_128_CMAC) {
1044 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1045 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1048 wpa_s->mgmt_group_cipher = 0;
1049 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1051 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1052 wpa_s->mgmt_group_cipher);
1053 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1054 #endif /* CONFIG_IEEE80211W */
1056 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1057 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1061 if (ssid->key_mgmt &
1062 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
1064 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1065 #ifndef CONFIG_NO_PBKDF2
1066 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1069 pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
1070 bss->ssid_len, 4096, psk, PMK_LEN);
1071 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1073 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1075 #endif /* CONFIG_NO_PBKDF2 */
1077 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1084 * wpa_supplicant_associate - Request association
1085 * @wpa_s: Pointer to wpa_supplicant data
1086 * @bss: Scan results for the selected BSS, or %NULL if not available
1087 * @ssid: Configuration data for the selected network
1089 * This function is used to request %wpa_supplicant to associate with a BSS.
1091 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1092 struct wpa_bss *bss, struct wpa_ssid *ssid)
1096 int use_crypt, ret, i, bssid_changed;
1097 int algs = WPA_AUTH_ALG_OPEN;
1098 enum wpa_cipher cipher_pairwise, cipher_group;
1099 struct wpa_driver_associate_params params;
1100 int wep_keys_set = 0;
1101 struct wpa_driver_capa capa;
1102 int assoc_failed = 0;
1103 struct wpa_ssid *old_ssid;
1105 #ifdef CONFIG_IBSS_RSN
1106 ibss_rsn_deinit(wpa_s->ibss_rsn);
1107 wpa_s->ibss_rsn = NULL;
1108 #endif /* CONFIG_IBSS_RSN */
1110 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1111 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1113 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1114 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1118 wpa_supplicant_create_ap(wpa_s, ssid);
1119 wpa_s->current_bss = bss;
1120 #else /* CONFIG_AP */
1121 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1123 #endif /* CONFIG_AP */
1129 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1131 #endif /* CONFIG_TDLS */
1133 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1134 ssid->mode == IEEE80211_MODE_INFRA) {
1135 sme_authenticate(wpa_s, bss, ssid);
1139 os_memset(¶ms, 0, sizeof(params));
1140 wpa_s->reassociate = 0;
1141 if (bss && !wpas_driver_bss_selection(wpa_s)) {
1142 #ifdef CONFIG_IEEE80211R
1143 const u8 *ie, *md = NULL;
1144 #endif /* CONFIG_IEEE80211R */
1145 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1146 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1147 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1148 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1149 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1150 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1152 wpas_notify_bssid_changed(wpa_s);
1153 #ifdef CONFIG_IEEE80211R
1154 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1155 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1157 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1159 /* Prepare for the next transition */
1160 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1162 #endif /* CONFIG_IEEE80211R */
1164 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1165 wpa_s->conf->ap_scan == 2 &&
1166 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1167 /* Use ap_scan==1 style network selection to find the network
1169 wpa_s->scan_req = 2;
1170 wpa_s->reassociate = 1;
1171 wpa_supplicant_req_scan(wpa_s, 0, 0);
1173 #endif /* CONFIG_WPS */
1175 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1176 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1177 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1179 wpa_supplicant_cancel_scan(wpa_s);
1181 /* Starting new association, so clear the possibly used WPA IE from the
1182 * previous association. */
1183 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1185 #ifdef IEEE8021X_EAPOL
1186 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1188 if (ssid->non_leap == 0)
1189 algs = WPA_AUTH_ALG_LEAP;
1191 algs |= WPA_AUTH_ALG_LEAP;
1194 #endif /* IEEE8021X_EAPOL */
1195 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1196 if (ssid->auth_alg) {
1197 algs = ssid->auth_alg;
1198 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1202 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1203 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1204 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1205 WPA_KEY_MGMT_FT_IEEE8021X |
1206 WPA_KEY_MGMT_FT_PSK |
1207 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1208 WPA_KEY_MGMT_PSK_SHA256))) {
1209 int try_opportunistic;
1210 try_opportunistic = ssid->proactive_key_caching &&
1211 (ssid->proto & WPA_PROTO_RSN);
1212 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1213 wpa_s->current_ssid,
1214 try_opportunistic) == 0)
1215 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1216 wpa_ie_len = sizeof(wpa_ie);
1217 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1218 wpa_ie, &wpa_ie_len)) {
1219 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1220 "key management and encryption suites");
1223 } else if (ssid->key_mgmt &
1224 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1225 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1226 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1227 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1228 wpa_ie_len = sizeof(wpa_ie);
1229 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1230 wpa_ie, &wpa_ie_len)) {
1231 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1232 "key management and encryption suites (no "
1237 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1238 struct wpabuf *wps_ie;
1239 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1240 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1241 wpa_ie_len = wpabuf_len(wps_ie);
1242 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1245 wpabuf_free(wps_ie);
1246 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1247 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1248 params.wps = WPS_MODE_PRIVACY;
1250 params.wps = WPS_MODE_OPEN;
1251 wpa_s->wpa_proto = 0;
1252 #endif /* CONFIG_WPS */
1254 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1256 wpa_s->wpa_proto = 0;
1260 if (wpa_s->global->p2p) {
1265 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1266 pos = wpa_ie + wpa_ie_len;
1267 len = sizeof(wpa_ie) - wpa_ie_len;
1268 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1273 wpa_s->cross_connect_disallowed = 0;
1276 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1278 wpa_s->cross_connect_disallowed =
1279 p2p_get_cross_connect_disallowed(p2p);
1281 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1283 wpa_s->cross_connect_disallowed ?
1284 "disallows" : "allows");
1287 #endif /* CONFIG_P2P */
1289 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1291 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1292 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1293 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1294 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1295 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1297 if (wpa_set_wep_keys(wpa_s, ssid)) {
1302 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1305 #ifdef IEEE8021X_EAPOL
1306 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1307 if ((ssid->eapol_flags &
1308 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1309 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1313 /* Assume that dynamic WEP-104 keys will be used and
1314 * set cipher suites in order for drivers to expect
1316 cipher_pairwise = cipher_group = CIPHER_WEP104;
1319 #endif /* IEEE8021X_EAPOL */
1321 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1322 /* Set the key before (and later after) association */
1323 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1326 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1328 params.ssid = bss->ssid;
1329 params.ssid_len = bss->ssid_len;
1330 if (!wpas_driver_bss_selection(wpa_s)) {
1331 params.bssid = bss->bssid;
1332 params.freq = bss->freq;
1335 params.ssid = ssid->ssid;
1336 params.ssid_len = ssid->ssid_len;
1338 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1340 params.freq = ssid->frequency; /* Initial channel for IBSS */
1341 params.wpa_ie = wpa_ie;
1342 params.wpa_ie_len = wpa_ie_len;
1343 params.pairwise_suite = cipher_pairwise;
1344 params.group_suite = cipher_group;
1345 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1346 params.wpa_proto = wpa_s->wpa_proto;
1347 params.auth_alg = algs;
1348 params.mode = ssid->mode;
1349 for (i = 0; i < NUM_WEP_KEYS; i++) {
1350 if (ssid->wep_key_len[i])
1351 params.wep_key[i] = ssid->wep_key[i];
1352 params.wep_key_len[i] = ssid->wep_key_len[i];
1354 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1356 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1357 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1358 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1359 params.passphrase = ssid->passphrase;
1361 params.psk = ssid->psk;
1364 params.drop_unencrypted = use_crypt;
1366 #ifdef CONFIG_IEEE80211W
1367 params.mgmt_frame_protection = ssid->ieee80211w;
1368 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1369 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1370 struct wpa_ie_data ie;
1371 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1373 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1374 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1375 "MFP: require MFP");
1376 params.mgmt_frame_protection =
1377 MGMT_FRAME_PROTECTION_REQUIRED;
1380 #endif /* CONFIG_IEEE80211W */
1382 params.p2p = ssid->p2p_group;
1384 if (wpa_s->parent->set_sta_uapsd)
1385 params.uapsd = wpa_s->parent->sta_uapsd;
1389 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1390 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1392 ret = wpa_drv_associate(wpa_s, ¶ms);
1394 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1396 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1398 * The driver is known to mean what is saying, so we
1399 * can stop right here; the association will not
1402 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1403 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1406 /* try to continue anyway; new association will be tried again
1411 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1412 /* Set the key after the association just in case association
1413 * cleared the previously configured key. */
1414 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1415 /* No need to timeout authentication since there is no key
1417 wpa_supplicant_cancel_auth_timeout(wpa_s);
1418 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1419 #ifdef CONFIG_IBSS_RSN
1420 } else if (ssid->mode == WPAS_MODE_IBSS &&
1421 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1422 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1424 * RSN IBSS authentication is per-STA and we can disable the
1425 * per-BSSID authentication.
1427 wpa_supplicant_cancel_auth_timeout(wpa_s);
1428 #endif /* CONFIG_IBSS_RSN */
1430 /* Timeout for IEEE 802.11 authentication and association */
1434 /* give IBSS a bit more time */
1435 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1436 } else if (wpa_s->conf->ap_scan == 1) {
1437 /* give IBSS a bit more time */
1438 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1440 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1443 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1444 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1445 /* Set static WEP keys again */
1446 wpa_set_wep_keys(wpa_s, ssid);
1449 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1451 * Do not allow EAP session resumption between different
1452 * network configurations.
1454 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1456 old_ssid = wpa_s->current_ssid;
1457 wpa_s->current_ssid = ssid;
1458 wpa_s->current_bss = bss;
1459 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1460 wpa_supplicant_initiate_eapol(wpa_s);
1461 if (old_ssid != wpa_s->current_ssid)
1462 wpas_notify_network_changed(wpa_s);
1466 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1469 struct wpa_ssid *old_ssid;
1471 wpa_clear_keys(wpa_s, addr);
1472 wpa_supplicant_mark_disassoc(wpa_s);
1473 old_ssid = wpa_s->current_ssid;
1474 wpa_s->current_ssid = NULL;
1475 wpa_s->current_bss = NULL;
1476 wpa_sm_set_config(wpa_s->wpa, NULL);
1477 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1478 if (old_ssid != wpa_s->current_ssid)
1479 wpas_notify_network_changed(wpa_s);
1480 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1485 * wpa_supplicant_disassociate - Disassociate the current connection
1486 * @wpa_s: Pointer to wpa_supplicant data
1487 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1489 * This function is used to request %wpa_supplicant to disassociate with the
1492 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1497 if (!is_zero_ether_addr(wpa_s->bssid)) {
1498 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1499 ieee80211_sta_disassociate(wpa_s, reason_code);
1501 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1502 addr = wpa_s->bssid;
1505 wpa_supplicant_clear_connection(wpa_s, addr);
1510 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1511 * @wpa_s: Pointer to wpa_supplicant data
1512 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1514 * This function is used to request %wpa_supplicant to deauthenticate from the
1517 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1522 if (!is_zero_ether_addr(wpa_s->bssid)) {
1523 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1524 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1526 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1528 addr = wpa_s->bssid;
1531 wpa_supplicant_clear_connection(wpa_s, addr);
1536 * wpa_supplicant_enable_network - Mark a configured network as enabled
1537 * @wpa_s: wpa_supplicant structure for a network interface
1538 * @ssid: wpa_ssid structure for a configured network or %NULL
1540 * Enables the specified network or all networks if no network specified.
1542 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1543 struct wpa_ssid *ssid)
1545 struct wpa_ssid *other_ssid;
1549 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1550 other_ssid = other_ssid->next) {
1551 if (other_ssid->disabled == 2)
1552 continue; /* do not change persistent P2P group
1554 if (other_ssid == wpa_s->current_ssid &&
1555 other_ssid->disabled)
1556 wpa_s->reassociate = 1;
1558 was_disabled = other_ssid->disabled;
1560 other_ssid->disabled = 0;
1562 if (was_disabled != other_ssid->disabled)
1563 wpas_notify_network_enabled_changed(
1566 if (wpa_s->reassociate)
1567 wpa_supplicant_req_scan(wpa_s, 0, 0);
1568 } else if (ssid->disabled && ssid->disabled != 2) {
1569 if (wpa_s->current_ssid == NULL) {
1571 * Try to reassociate since there is no current
1572 * configuration and a new network was made available.
1574 wpa_s->reassociate = 1;
1575 wpa_supplicant_req_scan(wpa_s, 0, 0);
1578 was_disabled = ssid->disabled;
1582 if (was_disabled != ssid->disabled)
1583 wpas_notify_network_enabled_changed(wpa_s, ssid);
1589 * wpa_supplicant_disable_network - Mark a configured network as disabled
1590 * @wpa_s: wpa_supplicant structure for a network interface
1591 * @ssid: wpa_ssid structure for a configured network or %NULL
1593 * Disables the specified network or all networks if no network specified.
1595 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1596 struct wpa_ssid *ssid)
1598 struct wpa_ssid *other_ssid;
1602 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1603 other_ssid = other_ssid->next) {
1604 was_disabled = other_ssid->disabled;
1605 if (was_disabled == 2)
1606 continue; /* do not change persistent P2P group
1609 other_ssid->disabled = 1;
1611 if (was_disabled != other_ssid->disabled)
1612 wpas_notify_network_enabled_changed(
1615 if (wpa_s->current_ssid)
1616 wpa_supplicant_disassociate(
1617 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1618 } else if (ssid->disabled != 2) {
1619 if (ssid == wpa_s->current_ssid)
1620 wpa_supplicant_disassociate(
1621 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1623 was_disabled = ssid->disabled;
1627 if (was_disabled != ssid->disabled)
1628 wpas_notify_network_enabled_changed(wpa_s, ssid);
1634 * wpa_supplicant_select_network - Attempt association with a network
1635 * @wpa_s: wpa_supplicant structure for a network interface
1636 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1638 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1639 struct wpa_ssid *ssid)
1642 struct wpa_ssid *other_ssid;
1644 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1645 wpa_supplicant_disassociate(
1646 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1649 * Mark all other networks disabled or mark all networks enabled if no
1650 * network specified.
1652 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1653 other_ssid = other_ssid->next) {
1654 int was_disabled = other_ssid->disabled;
1655 if (was_disabled == 2)
1656 continue; /* do not change persistent P2P group data */
1658 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1660 if (was_disabled != other_ssid->disabled)
1661 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1664 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1665 /* We are already associated with the selected network */
1666 wpa_printf(MSG_DEBUG, "Already associated with the "
1667 "selected network - do nothing");
1671 wpa_s->connect_without_scan = NULL;
1672 wpa_s->disconnected = 0;
1673 wpa_s->reassociate = 1;
1674 wpa_supplicant_req_scan(wpa_s, 0, 0);
1677 wpas_notify_network_selected(wpa_s, ssid);
1682 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1683 * @wpa_s: wpa_supplicant structure for a network interface
1684 * @ap_scan: AP scan mode
1685 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1688 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1693 if (ap_scan < 0 || ap_scan > 2)
1696 old_ap_scan = wpa_s->conf->ap_scan;
1697 wpa_s->conf->ap_scan = ap_scan;
1699 if (old_ap_scan != wpa_s->conf->ap_scan)
1700 wpas_notify_ap_scan_changed(wpa_s);
1707 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1708 * @wpa_s: wpa_supplicant structure for a network interface
1709 * @expire_age: Expiration age in seconds
1710 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1713 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1714 unsigned int bss_expire_age)
1716 if (bss_expire_age < 10) {
1717 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1721 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1723 wpa_s->conf->bss_expiration_age = bss_expire_age;
1730 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1731 * @wpa_s: wpa_supplicant structure for a network interface
1732 * @expire_count: number of scans after which an unseen BSS is reclaimed
1733 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1736 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1737 unsigned int bss_expire_count)
1739 if (bss_expire_count < 1) {
1740 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1744 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1746 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1753 * wpa_supplicant_set_debug_params - Set global debug params
1754 * @global: wpa_global structure
1755 * @debug_level: debug level
1756 * @debug_timestamp: determines if show timestamp in debug data
1757 * @debug_show_keys: determines if show keys in debug data
1758 * Returns: 0 if succeed or -1 if debug_level has wrong value
1760 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1761 int debug_timestamp, int debug_show_keys)
1764 int old_level, old_timestamp, old_show_keys;
1766 /* check for allowed debuglevels */
1767 if (debug_level != MSG_EXCESSIVE &&
1768 debug_level != MSG_MSGDUMP &&
1769 debug_level != MSG_DEBUG &&
1770 debug_level != MSG_INFO &&
1771 debug_level != MSG_WARNING &&
1772 debug_level != MSG_ERROR)
1775 old_level = wpa_debug_level;
1776 old_timestamp = wpa_debug_timestamp;
1777 old_show_keys = wpa_debug_show_keys;
1779 wpa_debug_level = debug_level;
1780 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1781 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1783 if (wpa_debug_level != old_level)
1784 wpas_notify_debug_level_changed(global);
1785 if (wpa_debug_timestamp != old_timestamp)
1786 wpas_notify_debug_timestamp_changed(global);
1787 if (wpa_debug_show_keys != old_show_keys)
1788 wpas_notify_debug_show_keys_changed(global);
1795 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1796 * @wpa_s: Pointer to wpa_supplicant data
1797 * Returns: A pointer to the current network structure or %NULL on failure
1799 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1801 struct wpa_ssid *entry;
1802 u8 ssid[MAX_SSID_LEN];
1808 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1809 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1810 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1815 res = wpa_drv_get_ssid(wpa_s, ssid);
1817 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1824 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1825 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1826 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1827 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1832 wired = wpa_s->conf->ap_scan == 0 &&
1833 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1835 entry = wpa_s->conf->ssid;
1837 if (!entry->disabled &&
1838 ((ssid_len == entry->ssid_len &&
1839 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1840 (!entry->bssid_set ||
1841 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1844 if (!entry->disabled &&
1845 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1846 (entry->ssid == NULL || entry->ssid_len == 0) &&
1847 (!entry->bssid_set ||
1848 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1850 #endif /* CONFIG_WPS */
1852 if (!entry->disabled && entry->bssid_set &&
1853 entry->ssid_len == 0 &&
1854 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
1857 entry = entry->next;
1864 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1869 const char *pos, *driver = name;
1874 if (wpa_drivers[0] == NULL) {
1875 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1881 /* default to first driver in the list */
1882 wpa_s->driver = wpa_drivers[0];
1883 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1888 pos = os_strchr(driver, ',');
1892 len = os_strlen(driver);
1894 for (i = 0; wpa_drivers[i]; i++) {
1895 if (os_strlen(wpa_drivers[i]->name) == len &&
1896 os_strncmp(driver, wpa_drivers[i]->name, len) ==
1898 wpa_s->driver = wpa_drivers[i];
1899 wpa_s->global_drv_priv =
1900 wpa_s->global->drv_priv[i];
1908 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1914 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1915 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1916 * with struct wpa_driver_ops::init()
1917 * @src_addr: Source address of the EAPOL frame
1918 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1919 * @len: Length of the EAPOL data
1921 * This function is called for each received EAPOL frame. Most driver
1922 * interfaces rely on more generic OS mechanism for receiving frames through
1923 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1924 * take care of received EAPOL frames and deliver them to the core supplicant
1925 * code by calling this function.
1927 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1928 const u8 *buf, size_t len)
1930 struct wpa_supplicant *wpa_s = ctx;
1932 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1933 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1935 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1937 * There is possible race condition between receiving the
1938 * association event and the EAPOL frame since they are coming
1939 * through different paths from the driver. In order to avoid
1940 * issues in trying to process the EAPOL frame before receiving
1941 * association information, lets queue it for processing until
1942 * the association event is received.
1944 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1945 "of received EAPOL frame");
1946 wpabuf_free(wpa_s->pending_eapol_rx);
1947 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1948 if (wpa_s->pending_eapol_rx) {
1949 os_get_time(&wpa_s->pending_eapol_rx_time);
1950 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1957 if (wpa_s->ap_iface) {
1958 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1961 #endif /* CONFIG_AP */
1963 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1964 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
1965 "no key management is configured");
1969 if (wpa_s->eapol_received == 0 &&
1970 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1971 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1972 wpa_s->wpa_state != WPA_COMPLETED) &&
1973 (wpa_s->current_ssid == NULL ||
1974 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1975 /* Timeout for completing IEEE 802.1X and WPA authentication */
1976 wpa_supplicant_req_auth_timeout(
1978 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1979 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1980 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1983 wpa_s->eapol_received++;
1985 if (wpa_s->countermeasures) {
1986 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
1991 #ifdef CONFIG_IBSS_RSN
1992 if (wpa_s->current_ssid &&
1993 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1994 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1997 #endif /* CONFIG_IBSS_RSN */
1999 /* Source address of the incoming EAPOL frame could be compared to the
2000 * current BSSID. However, it is possible that a centralized
2001 * Authenticator could be using another MAC address than the BSSID of
2002 * an AP, so just allow any address to be used for now. The replies are
2003 * still sent to the current BSSID (if available), though. */
2005 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2006 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2007 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2009 wpa_drv_poll(wpa_s);
2010 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2011 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2012 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2014 * Set portValid = TRUE here since we are going to skip 4-way
2015 * handshake processing which would normally set portValid. We
2016 * need this to allow the EAPOL state machines to be completed
2017 * without going through EAPOL-Key handshake.
2019 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2025 * wpa_supplicant_driver_init - Initialize driver interface parameters
2026 * @wpa_s: Pointer to wpa_supplicant data
2027 * Returns: 0 on success, -1 on failure
2029 * This function is called to initialize driver interface parameters.
2030 * wpa_drv_init() must have been called before this function to initialize the
2033 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2035 static int interface_count = 0;
2037 if (wpa_s->driver->send_eapol) {
2038 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2040 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2041 } else if (!(wpa_s->drv_flags &
2042 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2043 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2044 wpa_drv_get_mac_addr(wpa_s),
2046 wpa_supplicant_rx_eapol, wpa_s, 0);
2047 if (wpa_s->l2 == NULL)
2050 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2052 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2055 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2056 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2060 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2061 MAC2STR(wpa_s->own_addr));
2063 if (wpa_s->bridge_ifname[0]) {
2064 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2065 "interface '%s'", wpa_s->bridge_ifname);
2066 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2069 wpa_supplicant_rx_eapol, wpa_s,
2071 if (wpa_s->l2_br == NULL) {
2072 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2073 "connection for the bridge interface '%s'",
2074 wpa_s->bridge_ifname);
2079 wpa_clear_keys(wpa_s, NULL);
2081 /* Make sure that TKIP countermeasures are not left enabled (could
2082 * happen if wpa_supplicant is killed during countermeasures. */
2083 wpa_drv_set_countermeasures(wpa_s, 0);
2085 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2086 wpa_drv_flush_pmkid(wpa_s);
2088 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2089 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2090 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
2093 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2099 static int wpa_supplicant_daemon(const char *pid_file)
2101 wpa_printf(MSG_DEBUG, "Daemonize..");
2102 return os_daemonize(pid_file);
2106 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2108 struct wpa_supplicant *wpa_s;
2110 wpa_s = os_zalloc(sizeof(*wpa_s));
2113 wpa_s->scan_req = 1;
2114 wpa_s->scan_interval = 5;
2115 wpa_s->new_connection = 1;
2116 wpa_s->parent = wpa_s;
2117 wpa_s->sched_scanning = 0;
2123 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2124 struct wpa_interface *iface)
2126 const char *ifname, *driver;
2127 struct wpa_driver_capa capa;
2129 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2130 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2131 iface->confname ? iface->confname : "N/A",
2132 iface->driver ? iface->driver : "default",
2133 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2134 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2136 if (iface->confname) {
2137 #ifdef CONFIG_BACKEND_FILE
2138 wpa_s->confname = os_rel2abs_path(iface->confname);
2139 if (wpa_s->confname == NULL) {
2140 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2141 "for configuration file '%s'.",
2145 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2146 iface->confname, wpa_s->confname);
2147 #else /* CONFIG_BACKEND_FILE */
2148 wpa_s->confname = os_strdup(iface->confname);
2149 #endif /* CONFIG_BACKEND_FILE */
2150 wpa_s->conf = wpa_config_read(wpa_s->confname);
2151 if (wpa_s->conf == NULL) {
2152 wpa_printf(MSG_ERROR, "Failed to read or parse "
2153 "configuration '%s'.", wpa_s->confname);
2158 * Override ctrl_interface and driver_param if set on command
2161 if (iface->ctrl_interface) {
2162 os_free(wpa_s->conf->ctrl_interface);
2163 wpa_s->conf->ctrl_interface =
2164 os_strdup(iface->ctrl_interface);
2167 if (iface->driver_param) {
2168 os_free(wpa_s->conf->driver_param);
2169 wpa_s->conf->driver_param =
2170 os_strdup(iface->driver_param);
2173 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2174 iface->driver_param);
2176 if (wpa_s->conf == NULL) {
2177 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2181 if (iface->ifname == NULL) {
2182 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2185 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2186 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2190 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2192 if (iface->bridge_ifname) {
2193 if (os_strlen(iface->bridge_ifname) >=
2194 sizeof(wpa_s->bridge_ifname)) {
2195 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2196 "name '%s'.", iface->bridge_ifname);
2199 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2200 sizeof(wpa_s->bridge_ifname));
2203 /* RSNA Supplicant Key Management - INITIALIZE */
2204 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2205 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2207 /* Initialize driver interface and register driver event handler before
2208 * L2 receive handler so that association events are processed before
2209 * EAPOL-Key packets if both become available for the same select()
2211 driver = iface->driver;
2213 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2216 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2217 if (wpa_s->drv_priv == NULL) {
2219 pos = driver ? os_strchr(driver, ',') : NULL;
2221 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2222 "driver interface - try next driver wrapper");
2226 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2230 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2231 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2232 "driver_param '%s'", wpa_s->conf->driver_param);
2236 ifname = wpa_drv_get_ifname(wpa_s);
2237 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2238 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2239 "interface name with '%s'", ifname);
2240 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2243 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2246 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2247 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2249 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2251 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2252 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2253 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2254 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2255 "dot11RSNAConfigPMKLifetime");
2259 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2260 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2261 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2262 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2263 "dot11RSNAConfigPMKReauthThreshold");
2267 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2268 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2269 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2270 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2271 "dot11RSNAConfigSATimeout");
2275 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2276 wpa_s->drv_flags = capa.flags;
2277 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2278 if (ieee80211_sta_init(wpa_s))
2281 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2282 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2283 wpa_s->sched_scan_supported = capa.sched_scan_supported;
2284 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2285 wpa_s->max_stations = capa.max_stations;
2287 if (wpa_s->max_remain_on_chan == 0)
2288 wpa_s->max_remain_on_chan = 1000;
2290 if (wpa_supplicant_driver_init(wpa_s) < 0)
2294 if (wpa_tdls_init(wpa_s->wpa))
2296 #endif /* CONFIG_TDLS */
2298 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2299 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2300 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2304 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2306 if (wpas_wps_init(wpa_s))
2309 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2311 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2313 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2314 if (wpa_s->ctrl_iface == NULL) {
2315 wpa_printf(MSG_ERROR,
2316 "Failed to initialize control interface '%s'.\n"
2317 "You may have another wpa_supplicant process "
2318 "already running or the file was\n"
2319 "left by an unclean termination of wpa_supplicant "
2320 "in which case you will need\n"
2321 "to manually remove this file before starting "
2322 "wpa_supplicant again.\n",
2323 wpa_s->conf->ctrl_interface);
2327 wpa_s->gas = gas_query_init(wpa_s);
2328 if (wpa_s->gas == NULL) {
2329 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2334 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2335 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2338 #endif /* CONFIG_P2P */
2340 if (wpa_bss_init(wpa_s) < 0)
2347 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2350 if (wpa_s->drv_priv) {
2351 wpa_supplicant_deauthenticate(wpa_s,
2352 WLAN_REASON_DEAUTH_LEAVING);
2354 wpa_drv_set_countermeasures(wpa_s, 0);
2355 wpa_clear_keys(wpa_s, NULL);
2358 wpa_supplicant_cleanup(wpa_s);
2361 wpas_notify_iface_removed(wpa_s);
2363 if (wpa_s->drv_priv)
2364 wpa_drv_deinit(wpa_s);
2369 * wpa_supplicant_add_iface - Add a new network interface
2370 * @global: Pointer to global data from wpa_supplicant_init()
2371 * @iface: Interface configuration options
2372 * Returns: Pointer to the created interface or %NULL on failure
2374 * This function is used to add new network interfaces for %wpa_supplicant.
2375 * This can be called before wpa_supplicant_run() to add interfaces before the
2376 * main event loop has been started. In addition, new interfaces can be added
2377 * dynamically while %wpa_supplicant is already running. This could happen,
2378 * e.g., when a hotplug network adapter is inserted.
2380 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2381 struct wpa_interface *iface)
2383 struct wpa_supplicant *wpa_s;
2384 struct wpa_interface t_iface;
2385 struct wpa_ssid *ssid;
2387 if (global == NULL || iface == NULL)
2390 wpa_s = wpa_supplicant_alloc();
2394 wpa_s->global = global;
2397 if (global->params.override_driver) {
2398 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2400 iface->driver, global->params.override_driver);
2401 t_iface.driver = global->params.override_driver;
2403 if (global->params.override_ctrl_interface) {
2404 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2405 "ctrl_interface ('%s' -> '%s')",
2406 iface->ctrl_interface,
2407 global->params.override_ctrl_interface);
2408 t_iface.ctrl_interface =
2409 global->params.override_ctrl_interface;
2411 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2412 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2414 wpa_supplicant_deinit_iface(wpa_s, 0);
2419 /* Notify the control interfaces about new iface */
2420 if (wpas_notify_iface_added(wpa_s)) {
2421 wpa_supplicant_deinit_iface(wpa_s, 1);
2426 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2427 wpas_notify_network_added(wpa_s, ssid);
2429 wpa_s->next = global->ifaces;
2430 global->ifaces = wpa_s;
2432 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2439 * wpa_supplicant_remove_iface - Remove a network interface
2440 * @global: Pointer to global data from wpa_supplicant_init()
2441 * @wpa_s: Pointer to the network interface to be removed
2442 * Returns: 0 if interface was removed, -1 if interface was not found
2444 * This function can be used to dynamically remove network interfaces from
2445 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2446 * addition, this function is used to remove all remaining interfaces when
2447 * %wpa_supplicant is terminated.
2449 int wpa_supplicant_remove_iface(struct wpa_global *global,
2450 struct wpa_supplicant *wpa_s)
2452 struct wpa_supplicant *prev;
2454 /* Remove interface from the global list of interfaces */
2455 prev = global->ifaces;
2456 if (prev == wpa_s) {
2457 global->ifaces = wpa_s->next;
2459 while (prev && prev->next != wpa_s)
2463 prev->next = wpa_s->next;
2466 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2468 if (global->p2p_group_formation == wpa_s)
2469 global->p2p_group_formation = NULL;
2470 wpa_supplicant_deinit_iface(wpa_s, 1);
2478 * wpa_supplicant_get_eap_mode - Get the current EAP mode
2479 * @wpa_s: Pointer to the network interface
2480 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2482 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2484 const char *eapol_method;
2486 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2487 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2491 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2492 if (eapol_method == NULL)
2493 return "UNKNOWN-EAP";
2495 return eapol_method;
2500 * wpa_supplicant_get_iface - Get a new network interface
2501 * @global: Pointer to global data from wpa_supplicant_init()
2502 * @ifname: Interface name
2503 * Returns: Pointer to the interface or %NULL if not found
2505 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2508 struct wpa_supplicant *wpa_s;
2510 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2511 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2518 #ifndef CONFIG_NO_WPA_MSG
2519 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2521 struct wpa_supplicant *wpa_s = ctx;
2524 return wpa_s->ifname;
2526 #endif /* CONFIG_NO_WPA_MSG */
2530 * wpa_supplicant_init - Initialize %wpa_supplicant
2531 * @params: Parameters for %wpa_supplicant
2532 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2534 * This function is used to initialize %wpa_supplicant. After successful
2535 * initialization, the returned data pointer can be used to add and remove
2536 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2538 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2540 struct wpa_global *global;
2546 #ifndef CONFIG_NO_WPA_MSG
2547 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2548 #endif /* CONFIG_NO_WPA_MSG */
2550 wpa_debug_open_file(params->wpa_debug_file_path);
2551 if (params->wpa_debug_syslog)
2552 wpa_debug_open_syslog();
2554 ret = eap_register_methods();
2556 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2558 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2559 "the same EAP type.");
2563 global = os_zalloc(sizeof(*global));
2566 dl_list_init(&global->p2p_srv_bonjour);
2567 dl_list_init(&global->p2p_srv_upnp);
2568 global->params.daemonize = params->daemonize;
2569 global->params.wait_for_monitor = params->wait_for_monitor;
2570 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2571 if (params->pid_file)
2572 global->params.pid_file = os_strdup(params->pid_file);
2573 if (params->ctrl_interface)
2574 global->params.ctrl_interface =
2575 os_strdup(params->ctrl_interface);
2576 if (params->override_driver)
2577 global->params.override_driver =
2578 os_strdup(params->override_driver);
2579 if (params->override_ctrl_interface)
2580 global->params.override_ctrl_interface =
2581 os_strdup(params->override_ctrl_interface);
2582 wpa_debug_level = global->params.wpa_debug_level =
2583 params->wpa_debug_level;
2584 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2585 params->wpa_debug_show_keys;
2586 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2587 params->wpa_debug_timestamp;
2589 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2592 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2593 wpa_supplicant_deinit(global);
2597 random_init(params->entropy_file);
2599 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2600 if (global->ctrl_iface == NULL) {
2601 wpa_supplicant_deinit(global);
2605 if (wpas_notify_supplicant_initialized(global)) {
2606 wpa_supplicant_deinit(global);
2610 for (i = 0; wpa_drivers[i]; i++)
2611 global->drv_count++;
2612 if (global->drv_count == 0) {
2613 wpa_printf(MSG_ERROR, "No drivers enabled");
2614 wpa_supplicant_deinit(global);
2617 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2618 if (global->drv_priv == NULL) {
2619 wpa_supplicant_deinit(global);
2622 for (i = 0; wpa_drivers[i]; i++) {
2623 if (!wpa_drivers[i]->global_init)
2625 global->drv_priv[i] = wpa_drivers[i]->global_init();
2626 if (global->drv_priv[i] == NULL) {
2627 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2628 "'%s'", wpa_drivers[i]->name);
2629 wpa_supplicant_deinit(global);
2639 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2640 * @global: Pointer to global data from wpa_supplicant_init()
2641 * Returns: 0 after successful event loop run, -1 on failure
2643 * This function starts the main event loop and continues running as long as
2644 * there are any remaining events. In most cases, this function is running as
2645 * long as the %wpa_supplicant process in still in use.
2647 int wpa_supplicant_run(struct wpa_global *global)
2649 struct wpa_supplicant *wpa_s;
2651 if (global->params.daemonize &&
2652 wpa_supplicant_daemon(global->params.pid_file))
2655 if (global->params.wait_for_monitor) {
2656 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2657 if (wpa_s->ctrl_iface)
2658 wpa_supplicant_ctrl_iface_wait(
2662 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2663 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2672 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2673 * @global: Pointer to global data from wpa_supplicant_init()
2675 * This function is called to deinitialize %wpa_supplicant and to free all
2676 * allocated resources. Remaining network interfaces will also be removed.
2678 void wpa_supplicant_deinit(struct wpa_global *global)
2686 wpas_p2p_deinit_global(global);
2687 #endif /* CONFIG_P2P */
2689 while (global->ifaces)
2690 wpa_supplicant_remove_iface(global, global->ifaces);
2692 if (global->ctrl_iface)
2693 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2695 wpas_notify_supplicant_deinitialized(global);
2697 eap_peer_unregister_methods();
2699 eap_server_unregister_methods();
2700 #endif /* CONFIG_AP */
2702 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2703 if (!global->drv_priv[i])
2705 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2707 os_free(global->drv_priv);
2713 if (global->params.pid_file) {
2714 os_daemonize_terminate(global->params.pid_file);
2715 os_free(global->params.pid_file);
2717 os_free(global->params.ctrl_interface);
2718 os_free(global->params.override_driver);
2719 os_free(global->params.override_ctrl_interface);
2722 wpa_debug_close_syslog();
2723 wpa_debug_close_file();
2727 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2729 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
2730 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2732 country[0] = wpa_s->conf->country[0];
2733 country[1] = wpa_s->conf->country[1];
2735 if (wpa_drv_set_country(wpa_s, country) < 0) {
2736 wpa_printf(MSG_ERROR, "Failed to set country code "
2742 wpas_wps_update_config(wpa_s);
2743 #endif /* CONFIG_WPS */
2746 wpas_p2p_update_config(wpa_s);
2747 #endif /* CONFIG_P2P */
2749 wpa_s->conf->changed_parameters = 0;
2753 void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
2754 size_t num_hw_features)
2758 if (hw_features == NULL)
2761 for (i = 0; i < num_hw_features; i++) {
2762 os_free(hw_features[i].channels);
2763 os_free(hw_features[i].rates);
2766 os_free(hw_features);
2770 static void add_freq(int *freqs, int *num_freqs, int freq)
2774 for (i = 0; i < *num_freqs; i++) {
2775 if (freqs[i] == freq)
2779 freqs[*num_freqs] = freq;
2784 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2786 struct wpa_bss *bss, *cbss;
2787 const int max_freqs = 10;
2791 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2795 cbss = wpa_s->current_bss;
2797 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2800 if (bss->ssid_len == cbss->ssid_len &&
2801 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2802 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2803 add_freq(freqs, &num_freqs, bss->freq);
2804 if (num_freqs == max_freqs)
2809 if (num_freqs == 0) {
2818 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2825 * Add the failed BSSID into the blacklist and speed up next scan
2826 * attempt if there could be other APs that could accept association.
2827 * The current blacklist count indicates how many times we have tried
2828 * connecting to this AP and multiple attempts mean that other APs are
2829 * either not available or has already been tried, so that we can start
2830 * increasing the delay here to avoid constant scanning.
2832 count = wpa_blacklist_add(wpa_s, bssid);
2833 if (count == 1 && wpa_s->current_bss) {
2835 * This BSS was not in the blacklist before. If there is
2836 * another BSS available for the same ESS, we should try that
2837 * next. Otherwise, we may as well try this one once more
2838 * before allowing other, likely worse, ESSes to be considered.
2840 freqs = get_bss_freqs_in_ess(wpa_s);
2842 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
2843 "has been seen; try it next");
2844 wpa_blacklist_add(wpa_s, bssid);
2846 * On the next scan, go through only the known channels
2847 * used in this ESS based on previous scans to speed up
2848 * common load balancing use case.
2850 os_free(wpa_s->next_scan_freqs);
2851 wpa_s->next_scan_freqs = freqs;
2870 * TODO: if more than one possible AP is available in scan results,
2871 * could try the other ones before requesting a new scan.
2873 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2874 1000 * (timeout % 1000));
2878 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
2880 return wpa_s->conf->ap_scan == 2 ||
2881 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);