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"
39 #include "common/ieee802_11_defs.h"
41 #include "blacklist.h"
42 #include "wpas_glue.h"
43 #include "wps_supplicant.h"
46 #include "gas_query.h"
48 #include "p2p_supplicant.h"
53 #include "offchannel.h"
55 const char *wpa_supplicant_version =
56 "wpa_supplicant v" VERSION_STR "\n"
57 "Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
59 const char *wpa_supplicant_license =
60 "This program is free software. You can distribute it and/or modify it\n"
61 "under the terms of the GNU General Public License version 2.\n"
63 "Alternatively, this software may be distributed under the terms of the\n"
64 "BSD license. See README and COPYING for more details.\n"
65 #ifdef EAP_TLS_OPENSSL
66 "\nThis product includes software developed by the OpenSSL Project\n"
67 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
68 #endif /* EAP_TLS_OPENSSL */
71 #ifndef CONFIG_NO_STDOUT_DEBUG
72 /* Long text divided into parts in order to fit in C89 strings size limits. */
73 const char *wpa_supplicant_full_license1 =
74 "This program is free software; you can redistribute it and/or modify\n"
75 "it under the terms of the GNU General Public License version 2 as\n"
76 "published by the Free Software Foundation.\n"
78 "This program is distributed in the hope that it will be useful,\n"
79 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
80 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
81 "GNU General Public License for more details.\n"
83 const char *wpa_supplicant_full_license2 =
84 "You should have received a copy of the GNU General Public License\n"
85 "along with this program; if not, write to the Free Software\n"
86 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
88 "Alternatively, this software may be distributed under the terms of the\n"
91 "Redistribution and use in source and binary forms, with or without\n"
92 "modification, are permitted provided that the following conditions are\n"
95 const char *wpa_supplicant_full_license3 =
96 "1. Redistributions of source code must retain the above copyright\n"
97 " notice, this list of conditions and the following disclaimer.\n"
99 "2. Redistributions in binary form must reproduce the above copyright\n"
100 " notice, this list of conditions and the following disclaimer in the\n"
101 " documentation and/or other materials provided with the distribution.\n"
103 const char *wpa_supplicant_full_license4 =
104 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
105 " names of its contributors may be used to endorse or promote products\n"
106 " derived from this software without specific prior written permission.\n"
108 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
109 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
110 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
111 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
112 const char *wpa_supplicant_full_license5 =
113 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
114 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
115 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
116 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
117 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
118 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
119 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
121 #endif /* CONFIG_NO_STDOUT_DEBUG */
123 extern int wpa_debug_level;
124 extern int wpa_debug_show_keys;
125 extern int wpa_debug_timestamp;
126 extern struct wpa_driver_ops *wpa_drivers[];
128 /* Configure default/group WEP keys for static WEP */
129 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
133 for (i = 0; i < NUM_WEP_KEYS; i++) {
134 if (ssid->wep_key_len[i] == 0)
138 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
139 i, i == ssid->wep_tx_keyidx, NULL, 0,
140 ssid->wep_key[i], ssid->wep_key_len[i]);
147 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
148 struct wpa_ssid *ssid)
155 /* IBSS/WPA-None uses only one key (Group) for both receiving and
156 * sending unicast and multicast packets. */
158 if (ssid->mode != WPAS_MODE_IBSS) {
159 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
160 "IBSS/ad-hoc) for WPA-None", ssid->mode);
164 if (!ssid->psk_set) {
165 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
170 switch (wpa_s->group_cipher) {
171 case WPA_CIPHER_CCMP:
172 os_memcpy(key, ssid->psk, 16);
176 case WPA_CIPHER_TKIP:
177 /* WPA-None uses the same Michael MIC key for both TX and RX */
178 os_memcpy(key, ssid->psk, 16 + 8);
179 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
184 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
185 "WPA-None", wpa_s->group_cipher);
189 /* TODO: should actually remember the previously used seq#, both for TX
190 * and RX from each STA.. */
192 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
196 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
198 struct wpa_supplicant *wpa_s = eloop_ctx;
199 const u8 *bssid = wpa_s->bssid;
200 if (is_zero_ether_addr(bssid))
201 bssid = wpa_s->pending_bssid;
202 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
204 wpa_blacklist_add(wpa_s, bssid);
205 wpa_sm_notify_disassoc(wpa_s->wpa);
206 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
207 wpa_s->reassociate = 1;
210 * If we timed out, the AP or the local radio may be busy.
211 * So, wait a second until scanning again.
213 wpa_supplicant_req_scan(wpa_s, 1, 0);
218 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
219 * @wpa_s: Pointer to wpa_supplicant data
220 * @sec: Number of seconds after which to time out authentication
221 * @usec: Number of microseconds after which to time out authentication
223 * This function is used to schedule a timeout for the current authentication
226 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
229 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
230 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
233 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
234 "%d usec", sec, usec);
235 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
236 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
241 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
242 * @wpa_s: Pointer to wpa_supplicant data
244 * This function is used to cancel authentication timeout scheduled with
245 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
248 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
250 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
251 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
252 wpa_blacklist_del(wpa_s, wpa_s->bssid);
257 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
258 * @wpa_s: Pointer to wpa_supplicant data
260 * This function is used to configure EAPOL state machine based on the selected
261 * authentication mode.
263 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
265 #ifdef IEEE8021X_EAPOL
266 struct eapol_config eapol_conf;
267 struct wpa_ssid *ssid = wpa_s->current_ssid;
269 #ifdef CONFIG_IBSS_RSN
270 if (ssid->mode == WPAS_MODE_IBSS &&
271 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
272 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
274 * RSN IBSS authentication is per-STA and we can disable the
275 * per-BSSID EAPOL authentication.
277 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
278 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
279 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
282 #endif /* CONFIG_IBSS_RSN */
284 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
285 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
287 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
288 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
289 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
291 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
293 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
294 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
295 eapol_conf.accept_802_1x_keys = 1;
296 eapol_conf.required_keys = 0;
297 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
298 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
300 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
301 eapol_conf.required_keys |=
302 EAPOL_REQUIRE_KEY_BROADCAST;
305 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
306 eapol_conf.required_keys = 0;
309 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
310 eapol_conf.workaround = ssid->eap_workaround;
311 eapol_conf.eap_disabled =
312 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
313 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
314 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
315 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
316 #endif /* IEEE8021X_EAPOL */
321 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
322 * @wpa_s: Pointer to wpa_supplicant data
323 * @ssid: Configuration data for the network
325 * This function is used to configure WPA state machine and related parameters
326 * to a mode where WPA is not enabled. This is called as part of the
327 * authentication configuration when the selected network does not use WPA.
329 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
330 struct wpa_ssid *ssid)
334 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
335 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
336 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
337 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
339 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
340 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
341 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
342 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
343 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
344 wpa_s->group_cipher = WPA_CIPHER_NONE;
345 wpa_s->mgmt_group_cipher = 0;
347 for (i = 0; i < NUM_WEP_KEYS; i++) {
348 if (ssid->wep_key_len[i] > 5) {
349 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
350 wpa_s->group_cipher = WPA_CIPHER_WEP104;
352 } else if (ssid->wep_key_len[i] > 0) {
353 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
354 wpa_s->group_cipher = WPA_CIPHER_WEP40;
359 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
360 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
361 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
362 wpa_s->pairwise_cipher);
363 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
364 #ifdef CONFIG_IEEE80211W
365 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
366 wpa_s->mgmt_group_cipher);
367 #endif /* CONFIG_IEEE80211W */
369 pmksa_cache_clear_current(wpa_s->wpa);
373 static void free_hw_features(struct wpa_supplicant *wpa_s)
376 if (wpa_s->hw.modes == NULL)
379 for (i = 0; i < wpa_s->hw.num_modes; i++) {
380 os_free(wpa_s->hw.modes[i].channels);
381 os_free(wpa_s->hw.modes[i].rates);
384 os_free(wpa_s->hw.modes);
385 wpa_s->hw.modes = NULL;
389 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
391 bgscan_deinit(wpa_s);
392 scard_deinit(wpa_s->scard);
394 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
395 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
396 l2_packet_deinit(wpa_s->l2);
399 l2_packet_deinit(wpa_s->l2_br);
403 if (wpa_s->conf != NULL) {
404 struct wpa_ssid *ssid;
405 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
406 wpas_notify_network_removed(wpa_s, ssid);
409 os_free(wpa_s->confname);
410 wpa_s->confname = NULL;
412 wpa_sm_set_eapol(wpa_s->wpa, NULL);
413 eapol_sm_deinit(wpa_s->eapol);
416 rsn_preauth_deinit(wpa_s->wpa);
419 wpa_tdls_deinit(wpa_s->wpa);
420 #endif /* CONFIG_TDLS */
422 pmksa_candidate_free(wpa_s->wpa);
423 wpa_sm_deinit(wpa_s->wpa);
425 wpa_blacklist_clear(wpa_s);
427 wpa_bss_deinit(wpa_s);
429 wpa_supplicant_cancel_scan(wpa_s);
430 wpa_supplicant_cancel_auth_timeout(wpa_s);
431 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
432 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
433 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
435 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
437 wpas_wps_deinit(wpa_s);
439 wpabuf_free(wpa_s->pending_eapol_rx);
440 wpa_s->pending_eapol_rx = NULL;
442 #ifdef CONFIG_IBSS_RSN
443 ibss_rsn_deinit(wpa_s->ibss_rsn);
444 wpa_s->ibss_rsn = NULL;
445 #endif /* CONFIG_IBSS_RSN */
450 wpa_supplicant_ap_deinit(wpa_s);
451 #endif /* CONFIG_AP */
454 wpas_p2p_deinit(wpa_s);
455 #endif /* CONFIG_P2P */
457 #ifdef CONFIG_OFFCHANNEL
458 offchannel_deinit(wpa_s);
459 #endif /* CONFIG_OFFCHANNEL */
461 wpa_supplicant_cancel_sched_scan(wpa_s);
463 os_free(wpa_s->next_scan_freqs);
464 wpa_s->next_scan_freqs = NULL;
466 gas_query_deinit(wpa_s->gas);
469 free_hw_features(wpa_s);
474 * wpa_clear_keys - Clear keys configured for the driver
475 * @wpa_s: Pointer to wpa_supplicant data
476 * @addr: Previously used BSSID or %NULL if not available
478 * This function clears the encryption keys that has been previously configured
481 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
483 if (wpa_s->keys_cleared) {
484 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
485 * timing issues with keys being cleared just before new keys
486 * are set or just after association or something similar. This
487 * shows up in group key handshake failing often because of the
488 * client not receiving the first encrypted packets correctly.
489 * Skipping some of the extra key clearing steps seems to help
490 * in completing group key handshake more reliably. */
491 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
492 "skip key clearing");
496 /* MLME-DELETEKEYS.request */
497 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
498 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
499 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
500 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
501 #ifdef CONFIG_IEEE80211W
502 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
503 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
504 #endif /* CONFIG_IEEE80211W */
506 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
508 /* MLME-SETPROTECTION.request(None) */
509 wpa_drv_mlme_setprotection(
511 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
512 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
514 wpa_s->keys_cleared = 1;
519 * wpa_supplicant_state_txt - Get the connection state name as a text string
520 * @state: State (wpa_state; WPA_*)
521 * Returns: The state name as a printable text string
523 const char * wpa_supplicant_state_txt(enum wpa_states state)
526 case WPA_DISCONNECTED:
527 return "DISCONNECTED";
530 case WPA_INTERFACE_DISABLED:
531 return "INTERFACE_DISABLED";
534 case WPA_AUTHENTICATING:
535 return "AUTHENTICATING";
536 case WPA_ASSOCIATING:
537 return "ASSOCIATING";
540 case WPA_4WAY_HANDSHAKE:
541 return "4WAY_HANDSHAKE";
542 case WPA_GROUP_HANDSHAKE:
543 return "GROUP_HANDSHAKE";
554 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
556 if (wpas_driver_bss_selection(wpa_s))
558 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
561 bgscan_deinit(wpa_s);
562 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
563 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
564 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
567 * Live without bgscan; it is only used as a roaming
568 * optimization, so the initial connection is not
572 wpa_s->bgscan_ssid = wpa_s->current_ssid;
574 wpa_s->bgscan_ssid = NULL;
578 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
580 if (wpa_s->bgscan_ssid != NULL) {
581 bgscan_deinit(wpa_s);
582 wpa_s->bgscan_ssid = NULL;
586 #endif /* CONFIG_BGSCAN */
590 * wpa_supplicant_set_state - Set current connection state
591 * @wpa_s: Pointer to wpa_supplicant data
592 * @state: The new connection state
594 * This function is called whenever the connection state changes, e.g.,
595 * association is completed for WPA/WPA2 4-Way Handshake is started.
597 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
598 enum wpa_states state)
600 enum wpa_states old_state = wpa_s->wpa_state;
602 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
603 wpa_supplicant_state_txt(wpa_s->wpa_state),
604 wpa_supplicant_state_txt(state));
606 if (state != WPA_SCANNING)
607 wpa_supplicant_notify_scanning(wpa_s, 0);
609 if (state == WPA_COMPLETED && wpa_s->new_connection) {
610 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
611 struct wpa_ssid *ssid = wpa_s->current_ssid;
612 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
613 MACSTR " completed %s [id=%d id_str=%s]",
614 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
615 "(reauth)" : "(auth)",
616 ssid ? ssid->id : -1,
617 ssid && ssid->id_str ? ssid->id_str : "");
618 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
619 wpa_s->new_connection = 0;
620 wpa_s->reassociated_connection = 1;
621 wpa_drv_set_operstate(wpa_s, 1);
622 #ifndef IEEE8021X_EAPOL
623 wpa_drv_set_supp_port(wpa_s, 1);
624 #endif /* IEEE8021X_EAPOL */
625 wpa_s->after_wps = 0;
627 wpas_p2p_completed(wpa_s);
628 #endif /* CONFIG_P2P */
629 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
630 state == WPA_ASSOCIATED) {
631 wpa_s->new_connection = 1;
632 wpa_drv_set_operstate(wpa_s, 0);
633 #ifndef IEEE8021X_EAPOL
634 wpa_drv_set_supp_port(wpa_s, 0);
635 #endif /* IEEE8021X_EAPOL */
637 wpa_s->wpa_state = state;
640 if (state == WPA_COMPLETED)
641 wpa_supplicant_start_bgscan(wpa_s);
643 wpa_supplicant_stop_bgscan(wpa_s);
644 #endif /* CONFIG_BGSCAN */
646 if (wpa_s->wpa_state != old_state) {
647 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
649 if (wpa_s->wpa_state == WPA_COMPLETED ||
650 old_state == WPA_COMPLETED)
651 wpas_notify_auth_changed(wpa_s);
656 void wpa_supplicant_terminate_proc(struct wpa_global *global)
660 struct wpa_supplicant *wpa_s = global->ifaces;
662 if (wpas_wps_terminate_pending(wpa_s) == 1)
666 #endif /* CONFIG_WPS */
673 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
675 struct wpa_global *global = signal_ctx;
676 struct wpa_supplicant *wpa_s;
677 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
678 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
681 wpa_supplicant_terminate_proc(global);
685 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
687 enum wpa_states old_state = wpa_s->wpa_state;
689 wpa_s->pairwise_cipher = 0;
690 wpa_s->group_cipher = 0;
691 wpa_s->mgmt_group_cipher = 0;
693 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
694 wpa_s->wpa_state = WPA_DISCONNECTED;
696 if (wpa_s->wpa_state != old_state)
697 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
702 * wpa_supplicant_reload_configuration - Reload configuration data
703 * @wpa_s: Pointer to wpa_supplicant data
704 * Returns: 0 on success or -1 if configuration parsing failed
706 * This function can be used to request that the configuration data is reloaded
707 * (e.g., after configuration file change). This function is reloading
708 * configuration only for one interface, so this may need to be called multiple
709 * times if %wpa_supplicant is controlling multiple interfaces and all
710 * interfaces need reconfiguration.
712 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
714 struct wpa_config *conf;
718 if (wpa_s->confname == NULL)
720 conf = wpa_config_read(wpa_s->confname);
722 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
723 "file '%s' - exiting", wpa_s->confname);
726 conf->changed_parameters = (unsigned int) -1;
728 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
729 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
730 os_strcmp(conf->ctrl_interface,
731 wpa_s->conf->ctrl_interface) != 0);
733 if (reconf_ctrl && wpa_s->ctrl_iface) {
734 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
735 wpa_s->ctrl_iface = NULL;
738 eapol_sm_invalidate_cached_session(wpa_s->eapol);
739 if (wpa_s->current_ssid) {
740 wpa_supplicant_deauthenticate(wpa_s,
741 WLAN_REASON_DEAUTH_LEAVING);
745 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
746 * pkcs11_engine_path, pkcs11_module_path.
748 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
750 * Clear forced success to clear EAP state for next
753 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
755 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
756 wpa_sm_set_config(wpa_s->wpa, NULL);
757 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
758 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
759 rsn_preauth_deinit(wpa_s->wpa);
761 old_ap_scan = wpa_s->conf->ap_scan;
762 wpa_config_free(wpa_s->conf);
764 if (old_ap_scan != wpa_s->conf->ap_scan)
765 wpas_notify_ap_scan_changed(wpa_s);
768 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
770 wpa_supplicant_update_config(wpa_s);
772 wpa_supplicant_clear_status(wpa_s);
773 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
774 wpa_s->reassociate = 1;
775 wpa_supplicant_req_scan(wpa_s, 0, 0);
777 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
782 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
784 struct wpa_global *global = signal_ctx;
785 struct wpa_supplicant *wpa_s;
786 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
787 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
789 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
790 wpa_supplicant_terminate_proc(global);
796 enum wpa_cipher cipher_suite2driver(int cipher)
799 case WPA_CIPHER_NONE:
801 case WPA_CIPHER_WEP40:
803 case WPA_CIPHER_WEP104:
804 return CIPHER_WEP104;
805 case WPA_CIPHER_CCMP:
807 case WPA_CIPHER_TKIP:
814 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
817 case WPA_KEY_MGMT_NONE:
818 return KEY_MGMT_NONE;
819 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
820 return KEY_MGMT_802_1X_NO_WPA;
821 case WPA_KEY_MGMT_IEEE8021X:
822 return KEY_MGMT_802_1X;
823 case WPA_KEY_MGMT_WPA_NONE:
824 return KEY_MGMT_WPA_NONE;
825 case WPA_KEY_MGMT_FT_IEEE8021X:
826 return KEY_MGMT_FT_802_1X;
827 case WPA_KEY_MGMT_FT_PSK:
828 return KEY_MGMT_FT_PSK;
829 case WPA_KEY_MGMT_IEEE8021X_SHA256:
830 return KEY_MGMT_802_1X_SHA256;
831 case WPA_KEY_MGMT_PSK_SHA256:
832 return KEY_MGMT_PSK_SHA256;
833 case WPA_KEY_MGMT_WPS:
835 case WPA_KEY_MGMT_PSK:
842 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
843 struct wpa_ssid *ssid,
844 struct wpa_ie_data *ie)
846 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
849 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
850 "from association info");
855 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
857 if (!(ie->group_cipher & ssid->group_cipher)) {
858 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
859 "cipher 0x%x (mask 0x%x) - reject",
860 ie->group_cipher, ssid->group_cipher);
863 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
864 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
865 "cipher 0x%x (mask 0x%x) - reject",
866 ie->pairwise_cipher, ssid->pairwise_cipher);
869 if (!(ie->key_mgmt & ssid->key_mgmt)) {
870 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
871 "management 0x%x (mask 0x%x) - reject",
872 ie->key_mgmt, ssid->key_mgmt);
876 #ifdef CONFIG_IEEE80211W
877 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
878 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
879 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
880 "that does not support management frame protection - "
884 #endif /* CONFIG_IEEE80211W */
891 * wpa_supplicant_set_suites - Set authentication and encryption parameters
892 * @wpa_s: Pointer to wpa_supplicant data
893 * @bss: Scan results for the selected BSS, or %NULL if not available
894 * @ssid: Configuration data for the selected network
895 * @wpa_ie: Buffer for the WPA/RSN IE
896 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
897 * used buffer length in case the functions returns success.
898 * Returns: 0 on success or -1 on failure
900 * This function is used to configure authentication and encryption parameters
901 * based on the network configuration and scan result for the selected BSS (if
904 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
905 struct wpa_bss *bss, struct wpa_ssid *ssid,
906 u8 *wpa_ie, size_t *wpa_ie_len)
908 struct wpa_ie_data ie;
910 const u8 *bss_wpa, *bss_rsn;
913 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
914 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
916 bss_wpa = bss_rsn = NULL;
918 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
919 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
920 (ie.group_cipher & ssid->group_cipher) &&
921 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
922 (ie.key_mgmt & ssid->key_mgmt)) {
923 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
924 proto = WPA_PROTO_RSN;
925 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
926 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
927 (ie.group_cipher & ssid->group_cipher) &&
928 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
929 (ie.key_mgmt & ssid->key_mgmt)) {
930 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
931 proto = WPA_PROTO_WPA;
933 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
936 if (ssid->proto & WPA_PROTO_RSN)
937 proto = WPA_PROTO_RSN;
939 proto = WPA_PROTO_WPA;
940 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
941 os_memset(&ie, 0, sizeof(ie));
942 ie.group_cipher = ssid->group_cipher;
943 ie.pairwise_cipher = ssid->pairwise_cipher;
944 ie.key_mgmt = ssid->key_mgmt;
945 #ifdef CONFIG_IEEE80211W
946 ie.mgmt_group_cipher =
947 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
948 WPA_CIPHER_AES_128_CMAC : 0;
949 #endif /* CONFIG_IEEE80211W */
950 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
951 "based on configuration");
956 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
957 "pairwise %d key_mgmt %d proto %d",
958 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
959 #ifdef CONFIG_IEEE80211W
960 if (ssid->ieee80211w) {
961 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
962 ie.mgmt_group_cipher);
964 #endif /* CONFIG_IEEE80211W */
966 wpa_s->wpa_proto = proto;
967 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
968 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
969 !!(ssid->proto & WPA_PROTO_RSN));
971 if (bss || !wpa_s->ap_ies_from_associnfo) {
972 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
973 bss_wpa ? 2 + bss_wpa[1] : 0) ||
974 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
975 bss_rsn ? 2 + bss_rsn[1] : 0))
979 sel = ie.group_cipher & ssid->group_cipher;
980 if (sel & WPA_CIPHER_CCMP) {
981 wpa_s->group_cipher = WPA_CIPHER_CCMP;
982 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
983 } else if (sel & WPA_CIPHER_TKIP) {
984 wpa_s->group_cipher = WPA_CIPHER_TKIP;
985 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
986 } else if (sel & WPA_CIPHER_WEP104) {
987 wpa_s->group_cipher = WPA_CIPHER_WEP104;
988 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
989 } else if (sel & WPA_CIPHER_WEP40) {
990 wpa_s->group_cipher = WPA_CIPHER_WEP40;
991 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
993 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
998 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
999 if (sel & WPA_CIPHER_CCMP) {
1000 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
1001 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
1002 } else if (sel & WPA_CIPHER_TKIP) {
1003 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
1004 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
1005 } else if (sel & WPA_CIPHER_NONE) {
1006 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1007 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
1009 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1014 sel = ie.key_mgmt & ssid->key_mgmt;
1016 #ifdef CONFIG_IEEE80211R
1017 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1018 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1019 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1020 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1021 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1022 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1023 #endif /* CONFIG_IEEE80211R */
1024 #ifdef CONFIG_IEEE80211W
1025 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1026 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1027 wpa_dbg(wpa_s, MSG_DEBUG,
1028 "WPA: using KEY_MGMT 802.1X with SHA256");
1029 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1030 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1031 wpa_dbg(wpa_s, MSG_DEBUG,
1032 "WPA: using KEY_MGMT PSK with SHA256");
1033 #endif /* CONFIG_IEEE80211W */
1034 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1035 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1036 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1037 } else if (sel & WPA_KEY_MGMT_PSK) {
1038 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1039 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1040 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1041 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1042 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1044 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1045 "authenticated key management type");
1049 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1050 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1051 wpa_s->pairwise_cipher);
1052 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1054 #ifdef CONFIG_IEEE80211W
1055 sel = ie.mgmt_group_cipher;
1056 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1057 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1059 if (sel & WPA_CIPHER_AES_128_CMAC) {
1060 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1061 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1064 wpa_s->mgmt_group_cipher = 0;
1065 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1067 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1068 wpa_s->mgmt_group_cipher);
1069 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1070 #endif /* CONFIG_IEEE80211W */
1072 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1073 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1077 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
1078 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1079 #ifndef CONFIG_NO_PBKDF2
1080 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1083 pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
1084 bss->ssid_len, 4096, psk, PMK_LEN);
1085 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1087 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1089 #endif /* CONFIG_NO_PBKDF2 */
1091 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1098 * wpa_supplicant_associate - Request association
1099 * @wpa_s: Pointer to wpa_supplicant data
1100 * @bss: Scan results for the selected BSS, or %NULL if not available
1101 * @ssid: Configuration data for the selected network
1103 * This function is used to request %wpa_supplicant to associate with a BSS.
1105 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1106 struct wpa_bss *bss, struct wpa_ssid *ssid)
1110 int use_crypt, ret, i, bssid_changed;
1111 int algs = WPA_AUTH_ALG_OPEN;
1112 enum wpa_cipher cipher_pairwise, cipher_group;
1113 struct wpa_driver_associate_params params;
1114 int wep_keys_set = 0;
1115 struct wpa_driver_capa capa;
1116 int assoc_failed = 0;
1117 struct wpa_ssid *old_ssid;
1122 #ifdef CONFIG_IBSS_RSN
1123 ibss_rsn_deinit(wpa_s->ibss_rsn);
1124 wpa_s->ibss_rsn = NULL;
1125 #endif /* CONFIG_IBSS_RSN */
1127 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1128 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1130 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1131 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1135 wpa_supplicant_create_ap(wpa_s, ssid);
1136 wpa_s->current_bss = bss;
1137 #else /* CONFIG_AP */
1138 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1140 #endif /* CONFIG_AP */
1146 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1148 #endif /* CONFIG_TDLS */
1150 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1151 ssid->mode == IEEE80211_MODE_INFRA) {
1152 sme_authenticate(wpa_s, bss, ssid);
1156 os_memset(¶ms, 0, sizeof(params));
1157 wpa_s->reassociate = 0;
1158 if (bss && !wpas_driver_bss_selection(wpa_s)) {
1159 #ifdef CONFIG_IEEE80211R
1160 const u8 *ie, *md = NULL;
1161 #endif /* CONFIG_IEEE80211R */
1162 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1163 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1164 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1165 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1166 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1167 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1169 wpas_notify_bssid_changed(wpa_s);
1170 #ifdef CONFIG_IEEE80211R
1171 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1172 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1174 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1176 /* Prepare for the next transition */
1177 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1179 #endif /* CONFIG_IEEE80211R */
1181 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1182 wpa_s->conf->ap_scan == 2 &&
1183 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1184 /* Use ap_scan==1 style network selection to find the network
1186 wpa_s->scan_req = 2;
1187 wpa_s->reassociate = 1;
1188 wpa_supplicant_req_scan(wpa_s, 0, 0);
1190 #endif /* CONFIG_WPS */
1192 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1193 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1194 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1196 wpa_supplicant_cancel_sched_scan(wpa_s);
1197 wpa_supplicant_cancel_scan(wpa_s);
1199 /* Starting new association, so clear the possibly used WPA IE from the
1200 * previous association. */
1201 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1203 #ifdef IEEE8021X_EAPOL
1204 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1206 if (ssid->non_leap == 0)
1207 algs = WPA_AUTH_ALG_LEAP;
1209 algs |= WPA_AUTH_ALG_LEAP;
1212 #endif /* IEEE8021X_EAPOL */
1213 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1214 if (ssid->auth_alg) {
1215 algs = ssid->auth_alg;
1216 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1220 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1221 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1222 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
1223 int try_opportunistic;
1224 try_opportunistic = ssid->proactive_key_caching &&
1225 (ssid->proto & WPA_PROTO_RSN);
1226 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1227 wpa_s->current_ssid,
1228 try_opportunistic) == 0)
1229 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1230 wpa_ie_len = sizeof(wpa_ie);
1231 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1232 wpa_ie, &wpa_ie_len)) {
1233 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1234 "key management and encryption suites");
1237 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
1238 wpa_ie_len = sizeof(wpa_ie);
1239 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1240 wpa_ie, &wpa_ie_len)) {
1241 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1242 "key management and encryption suites (no "
1247 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1248 struct wpabuf *wps_ie;
1249 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1250 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1251 wpa_ie_len = wpabuf_len(wps_ie);
1252 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1255 wpabuf_free(wps_ie);
1256 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1257 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1258 params.wps = WPS_MODE_PRIVACY;
1260 params.wps = WPS_MODE_OPEN;
1261 wpa_s->wpa_proto = 0;
1262 #endif /* CONFIG_WPS */
1264 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1266 wpa_s->wpa_proto = 0;
1270 if (wpa_s->global->p2p) {
1275 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1276 pos = wpa_ie + wpa_ie_len;
1277 len = sizeof(wpa_ie) - wpa_ie_len;
1278 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1283 wpa_s->cross_connect_disallowed = 0;
1286 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1288 wpa_s->cross_connect_disallowed =
1289 p2p_get_cross_connect_disallowed(p2p);
1291 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1293 wpa_s->cross_connect_disallowed ?
1294 "disallows" : "allows");
1297 #endif /* CONFIG_P2P */
1299 #ifdef CONFIG_INTERWORKING
1300 if (wpa_s->conf->interworking) {
1302 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1304 os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
1306 *pos++ = WLAN_EID_EXT_CAPAB;
1311 *pos++ = 0x80; /* Bit 31 - Interworking */
1313 #endif /* CONFIG_INTERWORKING */
1315 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1317 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1318 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1319 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1320 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1321 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1323 if (wpa_set_wep_keys(wpa_s, ssid)) {
1328 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1331 #ifdef IEEE8021X_EAPOL
1332 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1333 if ((ssid->eapol_flags &
1334 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1335 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1339 /* Assume that dynamic WEP-104 keys will be used and
1340 * set cipher suites in order for drivers to expect
1342 cipher_pairwise = cipher_group = CIPHER_WEP104;
1345 #endif /* IEEE8021X_EAPOL */
1347 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1348 /* Set the key before (and later after) association */
1349 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1352 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1354 params.ssid = bss->ssid;
1355 params.ssid_len = bss->ssid_len;
1356 if (!wpas_driver_bss_selection(wpa_s)) {
1357 params.bssid = bss->bssid;
1358 params.freq = bss->freq;
1361 params.ssid = ssid->ssid;
1362 params.ssid_len = ssid->ssid_len;
1364 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1366 params.freq = ssid->frequency; /* Initial channel for IBSS */
1367 params.wpa_ie = wpa_ie;
1368 params.wpa_ie_len = wpa_ie_len;
1369 params.pairwise_suite = cipher_pairwise;
1370 params.group_suite = cipher_group;
1371 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1372 params.wpa_proto = wpa_s->wpa_proto;
1373 params.auth_alg = algs;
1374 params.mode = ssid->mode;
1375 for (i = 0; i < NUM_WEP_KEYS; i++) {
1376 if (ssid->wep_key_len[i])
1377 params.wep_key[i] = ssid->wep_key[i];
1378 params.wep_key_len[i] = ssid->wep_key_len[i];
1380 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1382 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1383 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1384 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1385 params.passphrase = ssid->passphrase;
1387 params.psk = ssid->psk;
1390 params.drop_unencrypted = use_crypt;
1392 #ifdef CONFIG_IEEE80211W
1393 params.mgmt_frame_protection = ssid->ieee80211w;
1394 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1395 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1396 struct wpa_ie_data ie;
1397 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1399 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1400 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1401 "MFP: require MFP");
1402 params.mgmt_frame_protection =
1403 MGMT_FRAME_PROTECTION_REQUIRED;
1406 #endif /* CONFIG_IEEE80211W */
1408 params.p2p = ssid->p2p_group;
1410 if (wpa_s->parent->set_sta_uapsd)
1411 params.uapsd = wpa_s->parent->sta_uapsd;
1416 /* If multichannel concurrency is not supported, check for any frequency
1417 * conflict and take appropriate action.
1419 if (((freq = wpa_drv_shared_freq(wpa_s)) > 0) && (freq != params.freq) &&
1420 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
1421 wpa_printf(MSG_ERROR, "Shared interface with conflicting frequency found (%d != %d)"
1422 , freq, params.freq);
1423 wpas_p2p_handle_frequency_conflicts(wpa_s, params.freq);
1426 ret = wpa_drv_associate(wpa_s, ¶ms);
1428 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1430 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1432 * The driver is known to mean what is saying, so we
1433 * can stop right here; the association will not
1436 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1437 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1440 /* try to continue anyway; new association will be tried again
1445 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1446 /* Set the key after the association just in case association
1447 * cleared the previously configured key. */
1448 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1449 /* No need to timeout authentication since there is no key
1451 wpa_supplicant_cancel_auth_timeout(wpa_s);
1452 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1453 #ifdef CONFIG_IBSS_RSN
1454 } else if (ssid->mode == WPAS_MODE_IBSS &&
1455 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1456 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1458 * RSN IBSS authentication is per-STA and we can disable the
1459 * per-BSSID authentication.
1461 wpa_supplicant_cancel_auth_timeout(wpa_s);
1462 #endif /* CONFIG_IBSS_RSN */
1464 /* Timeout for IEEE 802.11 authentication and association */
1468 /* give IBSS a bit more time */
1469 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1470 } else if (wpa_s->conf->ap_scan == 1) {
1471 /* give IBSS a bit more time */
1472 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1474 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1477 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1478 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1479 /* Set static WEP keys again */
1480 wpa_set_wep_keys(wpa_s, ssid);
1483 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1485 * Do not allow EAP session resumption between different
1486 * network configurations.
1488 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1490 old_ssid = wpa_s->current_ssid;
1491 wpa_s->current_ssid = ssid;
1492 wpa_s->current_bss = bss;
1493 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1494 wpa_supplicant_initiate_eapol(wpa_s);
1495 if (old_ssid != wpa_s->current_ssid)
1496 wpas_notify_network_changed(wpa_s);
1500 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1503 struct wpa_ssid *old_ssid;
1505 wpa_clear_keys(wpa_s, addr);
1506 wpa_supplicant_mark_disassoc(wpa_s);
1507 old_ssid = wpa_s->current_ssid;
1508 wpa_s->current_ssid = NULL;
1509 wpa_s->current_bss = NULL;
1510 wpa_sm_set_config(wpa_s->wpa, NULL);
1511 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1512 if (old_ssid != wpa_s->current_ssid)
1513 wpas_notify_network_changed(wpa_s);
1514 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1519 * wpa_supplicant_disassociate - Disassociate the current connection
1520 * @wpa_s: Pointer to wpa_supplicant data
1521 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1523 * This function is used to request %wpa_supplicant to disassociate with the
1526 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1531 if (!is_zero_ether_addr(wpa_s->bssid)) {
1532 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1533 addr = wpa_s->bssid;
1536 wpa_supplicant_clear_connection(wpa_s, addr);
1541 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1542 * @wpa_s: Pointer to wpa_supplicant data
1543 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1545 * This function is used to request %wpa_supplicant to deauthenticate from the
1548 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1553 if (!is_zero_ether_addr(wpa_s->bssid)) {
1554 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
1555 addr = wpa_s->bssid;
1558 wpa_supplicant_clear_connection(wpa_s, addr);
1563 * wpa_supplicant_enable_network - Mark a configured network as enabled
1564 * @wpa_s: wpa_supplicant structure for a network interface
1565 * @ssid: wpa_ssid structure for a configured network or %NULL
1567 * Enables the specified network or all networks if no network specified.
1569 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1570 struct wpa_ssid *ssid)
1572 struct wpa_ssid *other_ssid;
1576 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1577 other_ssid = other_ssid->next) {
1578 if (other_ssid->disabled == 2)
1579 continue; /* do not change persistent P2P group
1581 if (other_ssid == wpa_s->current_ssid &&
1582 other_ssid->disabled)
1583 wpa_s->reassociate = 1;
1585 was_disabled = other_ssid->disabled;
1587 other_ssid->disabled = 0;
1589 if (was_disabled != other_ssid->disabled)
1590 wpas_notify_network_enabled_changed(
1593 if (wpa_s->reassociate)
1594 wpa_supplicant_req_scan(wpa_s, 0, 0);
1595 } else if (ssid->disabled && ssid->disabled != 2) {
1596 if (wpa_s->current_ssid == NULL) {
1598 * Try to reassociate since there is no current
1599 * configuration and a new network was made available.
1601 wpa_s->reassociate = 1;
1602 wpa_supplicant_req_scan(wpa_s, 0, 0);
1605 was_disabled = ssid->disabled;
1609 if (was_disabled != ssid->disabled)
1610 wpas_notify_network_enabled_changed(wpa_s, ssid);
1616 * wpa_supplicant_disable_network - Mark a configured network as disabled
1617 * @wpa_s: wpa_supplicant structure for a network interface
1618 * @ssid: wpa_ssid structure for a configured network or %NULL
1620 * Disables the specified network or all networks if no network specified.
1622 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1623 struct wpa_ssid *ssid)
1625 struct wpa_ssid *other_ssid;
1629 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1630 other_ssid = other_ssid->next) {
1631 was_disabled = other_ssid->disabled;
1632 if (was_disabled == 2)
1633 continue; /* do not change persistent P2P group
1636 other_ssid->disabled = 1;
1638 if (was_disabled != other_ssid->disabled)
1639 wpas_notify_network_enabled_changed(
1642 if (wpa_s->current_ssid)
1643 wpa_supplicant_disassociate(
1644 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1645 } else if (ssid->disabled != 2) {
1646 if (ssid == wpa_s->current_ssid)
1647 wpa_supplicant_disassociate(
1648 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1650 was_disabled = ssid->disabled;
1654 if (was_disabled != ssid->disabled)
1655 wpas_notify_network_enabled_changed(wpa_s, ssid);
1661 * wpa_supplicant_select_network - Attempt association with a network
1662 * @wpa_s: wpa_supplicant structure for a network interface
1663 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1665 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1666 struct wpa_ssid *ssid)
1669 struct wpa_ssid *other_ssid;
1671 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1672 wpa_supplicant_disassociate(
1673 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1676 * Mark all other networks disabled or mark all networks enabled if no
1677 * network specified.
1679 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1680 other_ssid = other_ssid->next) {
1681 int was_disabled = other_ssid->disabled;
1682 if (was_disabled == 2)
1683 continue; /* do not change persistent P2P group data */
1685 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1687 if (was_disabled != other_ssid->disabled)
1688 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1691 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1692 /* We are already associated with the selected network */
1693 wpa_printf(MSG_DEBUG, "Already associated with the "
1694 "selected network - do nothing");
1698 wpa_s->connect_without_scan = NULL;
1699 wpa_s->disconnected = 0;
1700 wpa_s->reassociate = 1;
1701 wpa_supplicant_req_scan(wpa_s, 0, 0);
1704 wpas_notify_network_selected(wpa_s, ssid);
1709 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1710 * @wpa_s: wpa_supplicant structure for a network interface
1711 * @ap_scan: AP scan mode
1712 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1715 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1720 if (ap_scan < 0 || ap_scan > 2)
1724 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
1725 wpa_s->wpa_state >= WPA_ASSOCIATING &&
1726 wpa_s->wpa_state < WPA_COMPLETED) {
1727 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
1728 "associating", wpa_s->conf->ap_scan, ap_scan);
1731 #endif /* ANDROID */
1733 old_ap_scan = wpa_s->conf->ap_scan;
1734 wpa_s->conf->ap_scan = ap_scan;
1736 if (old_ap_scan != wpa_s->conf->ap_scan)
1737 wpas_notify_ap_scan_changed(wpa_s);
1744 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1745 * @wpa_s: wpa_supplicant structure for a network interface
1746 * @expire_age: Expiration age in seconds
1747 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1750 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1751 unsigned int bss_expire_age)
1753 if (bss_expire_age < 10) {
1754 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1758 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1760 wpa_s->conf->bss_expiration_age = bss_expire_age;
1767 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1768 * @wpa_s: wpa_supplicant structure for a network interface
1769 * @expire_count: number of scans after which an unseen BSS is reclaimed
1770 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1773 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1774 unsigned int bss_expire_count)
1776 if (bss_expire_count < 1) {
1777 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1781 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1783 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1790 * wpa_supplicant_set_debug_params - Set global debug params
1791 * @global: wpa_global structure
1792 * @debug_level: debug level
1793 * @debug_timestamp: determines if show timestamp in debug data
1794 * @debug_show_keys: determines if show keys in debug data
1795 * Returns: 0 if succeed or -1 if debug_level has wrong value
1797 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1798 int debug_timestamp, int debug_show_keys)
1801 int old_level, old_timestamp, old_show_keys;
1803 /* check for allowed debuglevels */
1804 if (debug_level != MSG_EXCESSIVE &&
1805 debug_level != MSG_MSGDUMP &&
1806 debug_level != MSG_DEBUG &&
1807 debug_level != MSG_INFO &&
1808 debug_level != MSG_WARNING &&
1809 debug_level != MSG_ERROR)
1812 old_level = wpa_debug_level;
1813 old_timestamp = wpa_debug_timestamp;
1814 old_show_keys = wpa_debug_show_keys;
1816 wpa_debug_level = debug_level;
1817 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1818 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1820 if (wpa_debug_level != old_level)
1821 wpas_notify_debug_level_changed(global);
1822 if (wpa_debug_timestamp != old_timestamp)
1823 wpas_notify_debug_timestamp_changed(global);
1824 if (wpa_debug_show_keys != old_show_keys)
1825 wpas_notify_debug_show_keys_changed(global);
1832 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1833 * @wpa_s: Pointer to wpa_supplicant data
1834 * Returns: A pointer to the current network structure or %NULL on failure
1836 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1838 struct wpa_ssid *entry;
1839 u8 ssid[MAX_SSID_LEN];
1845 res = wpa_drv_get_ssid(wpa_s, ssid);
1847 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1853 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1854 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1859 wired = wpa_s->conf->ap_scan == 0 &&
1860 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1862 entry = wpa_s->conf->ssid;
1864 if (!entry->disabled &&
1865 ((ssid_len == entry->ssid_len &&
1866 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1867 (!entry->bssid_set ||
1868 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1871 if (!entry->disabled &&
1872 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1873 (entry->ssid == NULL || entry->ssid_len == 0) &&
1874 (!entry->bssid_set ||
1875 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1877 #endif /* CONFIG_WPS */
1879 if (!entry->disabled && entry->bssid_set &&
1880 entry->ssid_len == 0 &&
1881 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
1884 entry = entry->next;
1891 static int select_driver(struct wpa_supplicant *wpa_s, int i)
1893 struct wpa_global *global = wpa_s->global;
1895 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
1896 global->drv_priv[i] = wpa_drivers[i]->global_init();
1897 if (global->drv_priv[i] == NULL) {
1898 wpa_printf(MSG_ERROR, "Failed to initialize driver "
1899 "'%s'", wpa_drivers[i]->name);
1904 wpa_s->driver = wpa_drivers[i];
1905 wpa_s->global_drv_priv = global->drv_priv[i];
1911 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1916 const char *pos, *driver = name;
1921 if (wpa_drivers[0] == NULL) {
1922 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1928 /* default to first driver in the list */
1929 return select_driver(wpa_s, 0);
1933 pos = os_strchr(driver, ',');
1937 len = os_strlen(driver);
1939 for (i = 0; wpa_drivers[i]; i++) {
1940 if (os_strlen(wpa_drivers[i]->name) == len &&
1941 os_strncmp(driver, wpa_drivers[i]->name, len) ==
1943 return select_driver(wpa_s, i);
1949 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1955 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1956 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1957 * with struct wpa_driver_ops::init()
1958 * @src_addr: Source address of the EAPOL frame
1959 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1960 * @len: Length of the EAPOL data
1962 * This function is called for each received EAPOL frame. Most driver
1963 * interfaces rely on more generic OS mechanism for receiving frames through
1964 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1965 * take care of received EAPOL frames and deliver them to the core supplicant
1966 * code by calling this function.
1968 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1969 const u8 *buf, size_t len)
1971 struct wpa_supplicant *wpa_s = ctx;
1973 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1974 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1976 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1978 * There is possible race condition between receiving the
1979 * association event and the EAPOL frame since they are coming
1980 * through different paths from the driver. In order to avoid
1981 * issues in trying to process the EAPOL frame before receiving
1982 * association information, lets queue it for processing until
1983 * the association event is received.
1985 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1986 "of received EAPOL frame");
1987 wpabuf_free(wpa_s->pending_eapol_rx);
1988 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1989 if (wpa_s->pending_eapol_rx) {
1990 os_get_time(&wpa_s->pending_eapol_rx_time);
1991 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1998 if (wpa_s->ap_iface) {
1999 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2002 #endif /* CONFIG_AP */
2004 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2005 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2006 "no key management is configured");
2010 if (wpa_s->eapol_received == 0 &&
2011 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2012 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2013 wpa_s->wpa_state != WPA_COMPLETED) &&
2014 (wpa_s->current_ssid == NULL ||
2015 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2016 /* Timeout for completing IEEE 802.1X and WPA authentication */
2017 wpa_supplicant_req_auth_timeout(
2019 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2020 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2021 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2024 wpa_s->eapol_received++;
2026 if (wpa_s->countermeasures) {
2027 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2032 #ifdef CONFIG_IBSS_RSN
2033 if (wpa_s->current_ssid &&
2034 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2035 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2038 #endif /* CONFIG_IBSS_RSN */
2040 /* Source address of the incoming EAPOL frame could be compared to the
2041 * current BSSID. However, it is possible that a centralized
2042 * Authenticator could be using another MAC address than the BSSID of
2043 * an AP, so just allow any address to be used for now. The replies are
2044 * still sent to the current BSSID (if available), though. */
2046 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2047 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2048 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2050 wpa_drv_poll(wpa_s);
2051 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2052 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2053 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2055 * Set portValid = TRUE here since we are going to skip 4-way
2056 * handshake processing which would normally set portValid. We
2057 * need this to allow the EAPOL state machines to be completed
2058 * without going through EAPOL-Key handshake.
2060 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2065 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
2067 if (wpa_s->driver->send_eapol) {
2068 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2070 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2071 } else if (!(wpa_s->drv_flags &
2072 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2073 l2_packet_deinit(wpa_s->l2);
2074 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2075 wpa_drv_get_mac_addr(wpa_s),
2077 wpa_supplicant_rx_eapol, wpa_s, 0);
2078 if (wpa_s->l2 == NULL)
2081 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2083 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2086 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2087 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2091 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2092 MAC2STR(wpa_s->own_addr));
2093 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2100 * wpa_supplicant_driver_init - Initialize driver interface parameters
2101 * @wpa_s: Pointer to wpa_supplicant data
2102 * Returns: 0 on success, -1 on failure
2104 * This function is called to initialize driver interface parameters.
2105 * wpa_drv_init() must have been called before this function to initialize the
2108 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2110 static int interface_count = 0;
2112 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2115 if (wpa_s->bridge_ifname[0]) {
2116 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2117 "interface '%s'", wpa_s->bridge_ifname);
2118 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2121 wpa_supplicant_rx_eapol, wpa_s,
2123 if (wpa_s->l2_br == NULL) {
2124 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2125 "connection for the bridge interface '%s'",
2126 wpa_s->bridge_ifname);
2131 wpa_clear_keys(wpa_s, NULL);
2133 /* Make sure that TKIP countermeasures are not left enabled (could
2134 * happen if wpa_supplicant is killed during countermeasures. */
2135 wpa_drv_set_countermeasures(wpa_s, 0);
2137 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2138 wpa_drv_flush_pmkid(wpa_s);
2140 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2141 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2142 if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
2144 wpa_supplicant_req_scan(wpa_s, interface_count,
2148 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2154 static int wpa_supplicant_daemon(const char *pid_file)
2156 wpa_printf(MSG_DEBUG, "Daemonize..");
2157 return os_daemonize(pid_file);
2161 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2163 struct wpa_supplicant *wpa_s;
2165 wpa_s = os_zalloc(sizeof(*wpa_s));
2168 wpa_s->scan_req = 1;
2169 wpa_s->scan_interval = 5;
2170 wpa_s->new_connection = 1;
2171 wpa_s->parent = wpa_s;
2172 wpa_s->sched_scanning = 0;
2178 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2179 struct wpa_interface *iface)
2181 const char *ifname, *driver;
2182 struct wpa_driver_capa capa;
2184 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2185 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2186 iface->confname ? iface->confname : "N/A",
2187 iface->driver ? iface->driver : "default",
2188 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2189 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2191 if (iface->confname) {
2192 #ifdef CONFIG_BACKEND_FILE
2193 wpa_s->confname = os_rel2abs_path(iface->confname);
2194 if (wpa_s->confname == NULL) {
2195 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2196 "for configuration file '%s'.",
2200 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2201 iface->confname, wpa_s->confname);
2202 #else /* CONFIG_BACKEND_FILE */
2203 wpa_s->confname = os_strdup(iface->confname);
2204 #endif /* CONFIG_BACKEND_FILE */
2205 wpa_s->conf = wpa_config_read(wpa_s->confname);
2206 if (wpa_s->conf == NULL) {
2207 wpa_printf(MSG_ERROR, "Failed to read or parse "
2208 "configuration '%s'.", wpa_s->confname);
2213 * Override ctrl_interface and driver_param if set on command
2216 if (iface->ctrl_interface) {
2217 os_free(wpa_s->conf->ctrl_interface);
2218 wpa_s->conf->ctrl_interface =
2219 os_strdup(iface->ctrl_interface);
2222 if (iface->driver_param) {
2223 os_free(wpa_s->conf->driver_param);
2224 wpa_s->conf->driver_param =
2225 os_strdup(iface->driver_param);
2228 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2229 iface->driver_param);
2231 if (wpa_s->conf == NULL) {
2232 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2236 if (iface->ifname == NULL) {
2237 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2240 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2241 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2245 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2247 if (iface->bridge_ifname) {
2248 if (os_strlen(iface->bridge_ifname) >=
2249 sizeof(wpa_s->bridge_ifname)) {
2250 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2251 "name '%s'.", iface->bridge_ifname);
2254 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2255 sizeof(wpa_s->bridge_ifname));
2258 /* RSNA Supplicant Key Management - INITIALIZE */
2259 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2260 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2262 /* Initialize driver interface and register driver event handler before
2263 * L2 receive handler so that association events are processed before
2264 * EAPOL-Key packets if both become available for the same select()
2266 driver = iface->driver;
2268 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2271 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2272 if (wpa_s->drv_priv == NULL) {
2274 pos = driver ? os_strchr(driver, ',') : NULL;
2276 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2277 "driver interface - try next driver wrapper");
2281 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2285 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2286 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2287 "driver_param '%s'", wpa_s->conf->driver_param);
2291 ifname = wpa_drv_get_ifname(wpa_s);
2292 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2293 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2294 "interface name with '%s'", ifname);
2295 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2298 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2301 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2302 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2304 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2306 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2307 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2308 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2309 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2310 "dot11RSNAConfigPMKLifetime");
2314 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2315 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2316 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2317 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2318 "dot11RSNAConfigPMKReauthThreshold");
2322 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2323 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2324 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2325 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2326 "dot11RSNAConfigSATimeout");
2330 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
2331 &wpa_s->hw.num_modes,
2334 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2335 wpa_s->drv_capa_known = 1;
2336 wpa_s->drv_flags = capa.flags;
2337 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
2338 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2339 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2340 wpa_s->sched_scan_supported = capa.sched_scan_supported;
2341 wpa_s->max_match_sets = capa.max_match_sets;
2342 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2343 wpa_s->max_stations = capa.max_stations;
2345 if (wpa_s->max_remain_on_chan == 0)
2346 wpa_s->max_remain_on_chan = 1000;
2348 if (wpa_supplicant_driver_init(wpa_s) < 0)
2352 if (wpa_tdls_init(wpa_s->wpa))
2354 #endif /* CONFIG_TDLS */
2356 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2357 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2358 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2362 if (wpas_wps_init(wpa_s))
2365 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2367 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2369 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2370 if (wpa_s->ctrl_iface == NULL) {
2371 wpa_printf(MSG_ERROR,
2372 "Failed to initialize control interface '%s'.\n"
2373 "You may have another wpa_supplicant process "
2374 "already running or the file was\n"
2375 "left by an unclean termination of wpa_supplicant "
2376 "in which case you will need\n"
2377 "to manually remove this file before starting "
2378 "wpa_supplicant again.\n",
2379 wpa_s->conf->ctrl_interface);
2383 wpa_s->gas = gas_query_init(wpa_s);
2384 if (wpa_s->gas == NULL) {
2385 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2390 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2391 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2394 #endif /* CONFIG_P2P */
2396 if (wpa_bss_init(wpa_s) < 0)
2403 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2404 int notify, int terminate)
2406 if (wpa_s->drv_priv) {
2407 wpa_supplicant_deauthenticate(wpa_s,
2408 WLAN_REASON_DEAUTH_LEAVING);
2410 wpa_drv_set_countermeasures(wpa_s, 0);
2411 wpa_clear_keys(wpa_s, NULL);
2414 wpa_supplicant_cleanup(wpa_s);
2416 if (wpa_s->drv_priv)
2417 wpa_drv_deinit(wpa_s);
2420 wpas_notify_iface_removed(wpa_s);
2423 * The wpa_drv_deinit call after sending TERMINATING to the framework causes
2424 * race condition with the start of hostapd.
2425 * This has been moved out of wpa_supplicant_cleanup(). Send the control
2426 * message and free config after the deinit.
2429 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
2431 if (wpa_s->ctrl_iface) {
2432 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
2433 wpa_s->ctrl_iface = NULL;
2436 if (wpa_s->conf != NULL) {
2437 wpa_config_free(wpa_s->conf);
2444 * wpa_supplicant_add_iface - Add a new network interface
2445 * @global: Pointer to global data from wpa_supplicant_init()
2446 * @iface: Interface configuration options
2447 * Returns: Pointer to the created interface or %NULL on failure
2449 * This function is used to add new network interfaces for %wpa_supplicant.
2450 * This can be called before wpa_supplicant_run() to add interfaces before the
2451 * main event loop has been started. In addition, new interfaces can be added
2452 * dynamically while %wpa_supplicant is already running. This could happen,
2453 * e.g., when a hotplug network adapter is inserted.
2455 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2456 struct wpa_interface *iface)
2458 struct wpa_supplicant *wpa_s;
2459 struct wpa_interface t_iface;
2460 struct wpa_ssid *ssid;
2462 if (global == NULL || iface == NULL)
2465 wpa_s = wpa_supplicant_alloc();
2469 wpa_s->global = global;
2472 if (global->params.override_driver) {
2473 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2475 iface->driver, global->params.override_driver);
2476 t_iface.driver = global->params.override_driver;
2478 if (global->params.override_ctrl_interface) {
2479 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2480 "ctrl_interface ('%s' -> '%s')",
2481 iface->ctrl_interface,
2482 global->params.override_ctrl_interface);
2483 t_iface.ctrl_interface =
2484 global->params.override_ctrl_interface;
2486 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2487 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2489 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
2494 /* Notify the control interfaces about new iface */
2495 if (wpas_notify_iface_added(wpa_s)) {
2496 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
2501 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2502 wpas_notify_network_added(wpa_s, ssid);
2504 wpa_s->next = global->ifaces;
2505 global->ifaces = wpa_s;
2507 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2514 * wpa_supplicant_remove_iface - Remove a network interface
2515 * @global: Pointer to global data from wpa_supplicant_init()
2516 * @wpa_s: Pointer to the network interface to be removed
2517 * Returns: 0 if interface was removed, -1 if interface was not found
2519 * This function can be used to dynamically remove network interfaces from
2520 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2521 * addition, this function is used to remove all remaining interfaces when
2522 * %wpa_supplicant is terminated.
2524 int wpa_supplicant_remove_iface(struct wpa_global *global,
2525 struct wpa_supplicant *wpa_s,
2528 struct wpa_supplicant *prev;
2530 /* Remove interface from the global list of interfaces */
2531 prev = global->ifaces;
2532 if (prev == wpa_s) {
2533 global->ifaces = wpa_s->next;
2535 while (prev && prev->next != wpa_s)
2539 prev->next = wpa_s->next;
2542 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2544 if (global->p2p_group_formation == wpa_s)
2545 global->p2p_group_formation = NULL;
2546 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
2554 * wpa_supplicant_get_eap_mode - Get the current EAP mode
2555 * @wpa_s: Pointer to the network interface
2556 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2558 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2560 const char *eapol_method;
2562 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2563 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2567 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2568 if (eapol_method == NULL)
2569 return "UNKNOWN-EAP";
2571 return eapol_method;
2576 * wpa_supplicant_get_iface - Get a new network interface
2577 * @global: Pointer to global data from wpa_supplicant_init()
2578 * @ifname: Interface name
2579 * Returns: Pointer to the interface or %NULL if not found
2581 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2584 struct wpa_supplicant *wpa_s;
2586 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2587 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2594 #ifndef CONFIG_NO_WPA_MSG
2595 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2597 struct wpa_supplicant *wpa_s = ctx;
2600 return wpa_s->ifname;
2602 #endif /* CONFIG_NO_WPA_MSG */
2606 * wpa_supplicant_init - Initialize %wpa_supplicant
2607 * @params: Parameters for %wpa_supplicant
2608 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2610 * This function is used to initialize %wpa_supplicant. After successful
2611 * initialization, the returned data pointer can be used to add and remove
2612 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2614 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2616 struct wpa_global *global;
2622 #ifdef CONFIG_DRIVER_NDIS
2624 void driver_ndis_init_ops(void);
2625 driver_ndis_init_ops();
2627 #endif /* CONFIG_DRIVER_NDIS */
2629 #ifndef CONFIG_NO_WPA_MSG
2630 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2631 #endif /* CONFIG_NO_WPA_MSG */
2633 wpa_debug_open_file(params->wpa_debug_file_path);
2634 if (params->wpa_debug_syslog)
2635 wpa_debug_open_syslog();
2637 ret = eap_register_methods();
2639 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2641 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2642 "the same EAP type.");
2646 global = os_zalloc(sizeof(*global));
2649 dl_list_init(&global->p2p_srv_bonjour);
2650 dl_list_init(&global->p2p_srv_upnp);
2651 global->params.daemonize = params->daemonize;
2652 global->params.wait_for_monitor = params->wait_for_monitor;
2653 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2654 if (params->pid_file)
2655 global->params.pid_file = os_strdup(params->pid_file);
2656 if (params->ctrl_interface)
2657 global->params.ctrl_interface =
2658 os_strdup(params->ctrl_interface);
2659 if (params->override_driver)
2660 global->params.override_driver =
2661 os_strdup(params->override_driver);
2662 if (params->override_ctrl_interface)
2663 global->params.override_ctrl_interface =
2664 os_strdup(params->override_ctrl_interface);
2665 wpa_debug_level = global->params.wpa_debug_level =
2666 params->wpa_debug_level;
2667 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2668 params->wpa_debug_show_keys;
2669 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2670 params->wpa_debug_timestamp;
2672 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2675 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2676 wpa_supplicant_deinit(global);
2680 random_init(params->entropy_file);
2682 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2683 if (global->ctrl_iface == NULL) {
2684 wpa_supplicant_deinit(global);
2688 if (wpas_notify_supplicant_initialized(global)) {
2689 wpa_supplicant_deinit(global);
2693 for (i = 0; wpa_drivers[i]; i++)
2694 global->drv_count++;
2695 if (global->drv_count == 0) {
2696 wpa_printf(MSG_ERROR, "No drivers enabled");
2697 wpa_supplicant_deinit(global);
2700 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2701 if (global->drv_priv == NULL) {
2702 wpa_supplicant_deinit(global);
2711 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2712 * @global: Pointer to global data from wpa_supplicant_init()
2713 * Returns: 0 after successful event loop run, -1 on failure
2715 * This function starts the main event loop and continues running as long as
2716 * there are any remaining events. In most cases, this function is running as
2717 * long as the %wpa_supplicant process in still in use.
2719 int wpa_supplicant_run(struct wpa_global *global)
2721 struct wpa_supplicant *wpa_s;
2723 if (global->params.daemonize &&
2724 wpa_supplicant_daemon(global->params.pid_file))
2727 if (global->params.wait_for_monitor) {
2728 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2729 if (wpa_s->ctrl_iface)
2730 wpa_supplicant_ctrl_iface_wait(
2734 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2735 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2744 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2745 * @global: Pointer to global data from wpa_supplicant_init()
2747 * This function is called to deinitialize %wpa_supplicant and to free all
2748 * allocated resources. Remaining network interfaces will also be removed.
2750 void wpa_supplicant_deinit(struct wpa_global *global)
2758 wpas_p2p_deinit_global(global);
2759 #endif /* CONFIG_P2P */
2761 while (global->ifaces)
2762 wpa_supplicant_remove_iface(global, global->ifaces, 1);
2764 if (global->ctrl_iface)
2765 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2767 wpas_notify_supplicant_deinitialized(global);
2769 eap_peer_unregister_methods();
2771 eap_server_unregister_methods();
2772 #endif /* CONFIG_AP */
2774 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2775 if (!global->drv_priv[i])
2777 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2779 os_free(global->drv_priv);
2785 if (global->params.pid_file) {
2786 os_daemonize_terminate(global->params.pid_file);
2787 os_free(global->params.pid_file);
2789 os_free(global->params.ctrl_interface);
2790 os_free(global->params.override_driver);
2791 os_free(global->params.override_ctrl_interface);
2794 wpa_debug_close_syslog();
2795 wpa_debug_close_file();
2799 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2801 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
2802 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2804 country[0] = wpa_s->conf->country[0];
2805 country[1] = wpa_s->conf->country[1];
2807 if (wpa_drv_set_country(wpa_s, country) < 0) {
2808 wpa_printf(MSG_ERROR, "Failed to set country code "
2814 wpas_wps_update_config(wpa_s);
2815 #endif /* CONFIG_WPS */
2818 wpas_p2p_update_config(wpa_s);
2819 #endif /* CONFIG_P2P */
2821 wpa_s->conf->changed_parameters = 0;
2825 static void add_freq(int *freqs, int *num_freqs, int freq)
2829 for (i = 0; i < *num_freqs; i++) {
2830 if (freqs[i] == freq)
2834 freqs[*num_freqs] = freq;
2839 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2841 struct wpa_bss *bss, *cbss;
2842 const int max_freqs = 10;
2846 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2850 cbss = wpa_s->current_bss;
2852 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2855 if (bss->ssid_len == cbss->ssid_len &&
2856 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2857 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2858 add_freq(freqs, &num_freqs, bss->freq);
2859 if (num_freqs == max_freqs)
2864 if (num_freqs == 0) {
2873 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2880 * Add the failed BSSID into the blacklist and speed up next scan
2881 * attempt if there could be other APs that could accept association.
2882 * The current blacklist count indicates how many times we have tried
2883 * connecting to this AP and multiple attempts mean that other APs are
2884 * either not available or has already been tried, so that we can start
2885 * increasing the delay here to avoid constant scanning.
2887 count = wpa_blacklist_add(wpa_s, bssid);
2888 if (count == 1 && wpa_s->current_bss) {
2890 * This BSS was not in the blacklist before. If there is
2891 * another BSS available for the same ESS, we should try that
2892 * next. Otherwise, we may as well try this one once more
2893 * before allowing other, likely worse, ESSes to be considered.
2895 freqs = get_bss_freqs_in_ess(wpa_s);
2897 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
2898 "has been seen; try it next");
2899 wpa_blacklist_add(wpa_s, bssid);
2901 * On the next scan, go through only the known channels
2902 * used in this ESS based on previous scans to speed up
2903 * common load balancing use case.
2905 os_free(wpa_s->next_scan_freqs);
2906 wpa_s->next_scan_freqs = freqs;
2925 * TODO: if more than one possible AP is available in scan results,
2926 * could try the other ones before requesting a new scan.
2928 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2929 1000 * (timeout % 1000));
2933 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
2935 return wpa_s->conf->ap_scan == 2 ||
2936 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);