3 * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * This file implements functions for registering and unregistering
15 * %wpa_supplicant interfaces. In addition, this file contains number of
16 * functions for managing network connections.
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "eap_peer/eap.h"
24 #include "eap_server/eap_methods.h"
25 #include "rsn_supp/wpa.h"
28 #include "l2_packet/l2_packet.h"
29 #include "wpa_supplicant_i.h"
31 #include "ctrl_iface.h"
32 #include "pcsc_funcs.h"
33 #include "common/version.h"
34 #include "rsn_supp/preauth.h"
35 #include "rsn_supp/pmksa_cache.h"
36 #include "common/wpa_ctrl.h"
38 #include "common/ieee802_11_defs.h"
39 #include "blacklist.h"
40 #include "wpas_glue.h"
41 #include "wps_supplicant.h"
48 const char *wpa_supplicant_version =
49 "wpa_supplicant v" VERSION_STR "\n"
50 "Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi> and contributors";
52 const char *wpa_supplicant_license =
53 "This program is free software. You can distribute it and/or modify it\n"
54 "under the terms of the GNU General Public License version 2.\n"
56 "Alternatively, this software may be distributed under the terms of the\n"
57 "BSD license. See README and COPYING for more details.\n"
58 #ifdef EAP_TLS_OPENSSL
59 "\nThis product includes software developed by the OpenSSL Project\n"
60 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
61 #endif /* EAP_TLS_OPENSSL */
64 #ifndef CONFIG_NO_STDOUT_DEBUG
65 /* Long text divided into parts in order to fit in C89 strings size limits. */
66 const char *wpa_supplicant_full_license1 =
67 "This program is free software; you can redistribute it and/or modify\n"
68 "it under the terms of the GNU General Public License version 2 as\n"
69 "published by the Free Software Foundation.\n"
71 "This program is distributed in the hope that it will be useful,\n"
72 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
73 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
74 "GNU General Public License for more details.\n"
76 const char *wpa_supplicant_full_license2 =
77 "You should have received a copy of the GNU General Public License\n"
78 "along with this program; if not, write to the Free Software\n"
79 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
81 "Alternatively, this software may be distributed under the terms of the\n"
84 "Redistribution and use in source and binary forms, with or without\n"
85 "modification, are permitted provided that the following conditions are\n"
88 const char *wpa_supplicant_full_license3 =
89 "1. Redistributions of source code must retain the above copyright\n"
90 " notice, this list of conditions and the following disclaimer.\n"
92 "2. Redistributions in binary form must reproduce the above copyright\n"
93 " notice, this list of conditions and the following disclaimer in the\n"
94 " documentation and/or other materials provided with the distribution.\n"
96 const char *wpa_supplicant_full_license4 =
97 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
98 " names of its contributors may be used to endorse or promote products\n"
99 " derived from this software without specific prior written permission.\n"
101 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
102 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
103 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
104 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
105 const char *wpa_supplicant_full_license5 =
106 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
107 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
108 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
109 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
110 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
111 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
112 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
114 #endif /* CONFIG_NO_STDOUT_DEBUG */
116 extern int wpa_debug_level;
117 extern int wpa_debug_show_keys;
118 extern int wpa_debug_timestamp;
119 extern struct wpa_driver_ops *wpa_drivers[];
121 /* Configure default/group WEP keys for static WEP */
122 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
126 for (i = 0; i < NUM_WEP_KEYS; i++) {
127 if (ssid->wep_key_len[i] == 0)
131 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
132 (u8 *) "\xff\xff\xff\xff\xff\xff",
133 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
134 ssid->wep_key[i], ssid->wep_key_len[i]);
141 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
142 struct wpa_ssid *ssid)
149 /* IBSS/WPA-None uses only one key (Group) for both receiving and
150 * sending unicast and multicast packets. */
152 if (ssid->mode != IEEE80211_MODE_IBSS) {
153 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
154 "for WPA-None", ssid->mode);
158 if (!ssid->psk_set) {
159 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
163 switch (wpa_s->group_cipher) {
164 case WPA_CIPHER_CCMP:
165 os_memcpy(key, ssid->psk, 16);
169 case WPA_CIPHER_TKIP:
170 /* WPA-None uses the same Michael MIC key for both TX and RX */
171 os_memcpy(key, ssid->psk, 16 + 8);
172 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
177 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
178 "WPA-None", wpa_s->group_cipher);
182 /* TODO: should actually remember the previously used seq#, both for TX
183 * and RX from each STA.. */
185 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
186 0, 1, seq, 6, key, keylen);
190 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
192 struct wpa_supplicant *wpa_s = eloop_ctx;
193 const u8 *bssid = wpa_s->bssid;
194 if (is_zero_ether_addr(bssid))
195 bssid = wpa_s->pending_bssid;
196 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
198 wpa_blacklist_add(wpa_s, bssid);
199 wpa_sm_notify_disassoc(wpa_s->wpa);
200 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
201 wpa_s->reassociate = 1;
202 wpa_supplicant_req_scan(wpa_s, 0, 0);
207 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
208 * @wpa_s: Pointer to wpa_supplicant data
209 * @sec: Number of seconds after which to time out authentication
210 * @usec: Number of microseconds after which to time out authentication
212 * This function is used to schedule a timeout for the current authentication
215 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
218 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
219 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
222 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
223 "%d usec", sec, usec);
224 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
225 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
230 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
231 * @wpa_s: Pointer to wpa_supplicant data
233 * This function is used to cancel authentication timeout scheduled with
234 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
237 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
239 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
240 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
241 wpa_blacklist_del(wpa_s, wpa_s->bssid);
246 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
247 * @wpa_s: Pointer to wpa_supplicant data
249 * This function is used to configure EAPOL state machine based on the selected
250 * authentication mode.
252 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
254 #ifdef IEEE8021X_EAPOL
255 struct eapol_config eapol_conf;
256 struct wpa_ssid *ssid = wpa_s->current_ssid;
258 #ifdef CONFIG_IBSS_RSN
259 if (ssid->mode == IEEE80211_MODE_IBSS &&
260 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
261 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
263 * RSN IBSS authentication is per-STA and we can disable the
264 * per-BSSID EAPOL authentication.
266 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
267 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
268 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
271 #endif /* CONFIG_IBSS_RSN */
273 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
274 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
276 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
277 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
278 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
280 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
282 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
283 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
284 eapol_conf.accept_802_1x_keys = 1;
285 eapol_conf.required_keys = 0;
286 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
287 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
290 eapol_conf.required_keys |=
291 EAPOL_REQUIRE_KEY_BROADCAST;
294 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
295 eapol_conf.required_keys = 0;
298 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
299 eapol_conf.workaround = ssid->eap_workaround;
300 eapol_conf.eap_disabled =
301 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
302 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
303 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
304 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
305 #endif /* IEEE8021X_EAPOL */
310 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
311 * @wpa_s: Pointer to wpa_supplicant data
312 * @ssid: Configuration data for the network
314 * This function is used to configure WPA state machine and related parameters
315 * to a mode where WPA is not enabled. This is called as part of the
316 * authentication configuration when the selected network does not use WPA.
318 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
319 struct wpa_ssid *ssid)
323 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
324 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
325 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
326 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
328 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
329 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
330 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
331 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
332 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
333 wpa_s->group_cipher = WPA_CIPHER_NONE;
334 wpa_s->mgmt_group_cipher = 0;
336 for (i = 0; i < NUM_WEP_KEYS; i++) {
337 if (ssid->wep_key_len[i] > 5) {
338 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
339 wpa_s->group_cipher = WPA_CIPHER_WEP104;
341 } else if (ssid->wep_key_len[i] > 0) {
342 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
343 wpa_s->group_cipher = WPA_CIPHER_WEP40;
348 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
351 wpa_s->pairwise_cipher);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
353 #ifdef CONFIG_IEEE80211W
354 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
355 wpa_s->mgmt_group_cipher);
356 #endif /* CONFIG_IEEE80211W */
358 pmksa_cache_clear_current(wpa_s->wpa);
362 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
364 bgscan_deinit(wpa_s);
365 scard_deinit(wpa_s->scard);
367 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
368 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
369 l2_packet_deinit(wpa_s->l2);
372 l2_packet_deinit(wpa_s->l2_br);
376 if (wpa_s->ctrl_iface) {
377 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
378 wpa_s->ctrl_iface = NULL;
380 if (wpa_s->conf != NULL) {
381 wpa_config_free(wpa_s->conf);
385 os_free(wpa_s->confname);
386 wpa_s->confname = NULL;
388 wpa_sm_set_eapol(wpa_s->wpa, NULL);
389 eapol_sm_deinit(wpa_s->eapol);
392 rsn_preauth_deinit(wpa_s->wpa);
394 pmksa_candidate_free(wpa_s->wpa);
395 wpa_sm_deinit(wpa_s->wpa);
397 wpa_blacklist_clear(wpa_s);
399 wpa_scan_results_free(wpa_s->scan_res);
400 wpa_s->scan_res = NULL;
402 wpa_supplicant_cancel_scan(wpa_s);
403 wpa_supplicant_cancel_auth_timeout(wpa_s);
405 ieee80211_sta_deinit(wpa_s);
407 wpas_wps_deinit(wpa_s);
409 wpabuf_free(wpa_s->pending_eapol_rx);
410 wpa_s->pending_eapol_rx = NULL;
412 #ifdef CONFIG_IBSS_RSN
413 ibss_rsn_deinit(wpa_s->ibss_rsn);
414 wpa_s->ibss_rsn = NULL;
415 #endif /* CONFIG_IBSS_RSN */
418 os_free(wpa_s->sme.ft_ies);
419 wpa_s->sme.ft_ies = NULL;
420 wpa_s->sme.ft_ies_len = 0;
421 #endif /* CONFIG_SME */
424 wpa_supplicant_ap_deinit(wpa_s);
425 #endif /* CONFIG_AP */
430 * wpa_clear_keys - Clear keys configured for the driver
431 * @wpa_s: Pointer to wpa_supplicant data
432 * @addr: Previously used BSSID or %NULL if not available
434 * This function clears the encryption keys that has been previously configured
437 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
439 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
441 if (wpa_s->keys_cleared) {
442 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
443 * timing issues with keys being cleared just before new keys
444 * are set or just after association or something similar. This
445 * shows up in group key handshake failing often because of the
446 * client not receiving the first encrypted packets correctly.
447 * Skipping some of the extra key clearing steps seems to help
448 * in completing group key handshake more reliably. */
449 wpa_printf(MSG_DEBUG, "No keys have been configured - "
450 "skip key clearing");
454 /* MLME-DELETEKEYS.request */
455 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
456 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
457 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
458 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
459 #ifdef CONFIG_IEEE80211W
460 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
461 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
462 #endif /* CONFIG_IEEE80211W */
464 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
466 /* MLME-SETPROTECTION.request(None) */
467 wpa_drv_mlme_setprotection(
469 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
470 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
472 wpa_s->keys_cleared = 1;
477 * wpa_supplicant_state_txt - Get the connection state name as a text string
478 * @state: State (wpa_state; WPA_*)
479 * Returns: The state name as a printable text string
481 const char * wpa_supplicant_state_txt(int state)
484 case WPA_DISCONNECTED:
485 return "DISCONNECTED";
490 case WPA_AUTHENTICATING:
491 return "AUTHENTICATING";
492 case WPA_ASSOCIATING:
493 return "ASSOCIATING";
496 case WPA_4WAY_HANDSHAKE:
497 return "4WAY_HANDSHAKE";
498 case WPA_GROUP_HANDSHAKE:
499 return "GROUP_HANDSHAKE";
509 * wpa_supplicant_set_state - Set current connection state
510 * @wpa_s: Pointer to wpa_supplicant data
511 * @state: The new connection state
513 * This function is called whenever the connection state changes, e.g.,
514 * association is completed for WPA/WPA2 4-Way Handshake is started.
516 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
518 wpa_printf(MSG_DEBUG, "State: %s -> %s",
519 wpa_supplicant_state_txt(wpa_s->wpa_state),
520 wpa_supplicant_state_txt(state));
522 if (state != WPA_SCANNING)
523 wpa_supplicant_notify_scanning(wpa_s, 0);
525 wpas_notify_state_changed(wpa_s, state, wpa_s->wpa_state);
527 if (state == WPA_COMPLETED && wpa_s->new_connection) {
528 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
529 struct wpa_ssid *ssid = wpa_s->current_ssid;
530 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
531 MACSTR " completed %s [id=%d id_str=%s]",
532 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
533 "(reauth)" : "(auth)",
534 ssid ? ssid->id : -1,
535 ssid && ssid->id_str ? ssid->id_str : "");
536 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
537 wpa_s->new_connection = 0;
538 wpa_s->reassociated_connection = 1;
539 wpa_drv_set_operstate(wpa_s, 1);
540 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
541 state == WPA_ASSOCIATED) {
542 wpa_s->new_connection = 1;
543 wpa_drv_set_operstate(wpa_s, 0);
545 wpa_s->wpa_state = state;
549 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
551 struct wpa_global *global = signal_ctx;
552 struct wpa_supplicant *wpa_s;
553 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
554 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
561 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
563 wpa_states old_state = wpa_s->wpa_state;
564 wpa_s->pairwise_cipher = 0;
565 wpa_s->group_cipher = 0;
566 wpa_s->mgmt_group_cipher = 0;
568 wpa_s->wpa_state = WPA_DISCONNECTED;
569 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
574 * wpa_supplicant_reload_configuration - Reload configuration data
575 * @wpa_s: Pointer to wpa_supplicant data
576 * Returns: 0 on success or -1 if configuration parsing failed
578 * This function can be used to request that the configuration data is reloaded
579 * (e.g., after configuration file change). This function is reloading
580 * configuration only for one interface, so this may need to be called multiple
581 * times if %wpa_supplicant is controlling multiple interfaces and all
582 * interfaces need reconfiguration.
584 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
586 struct wpa_config *conf;
587 struct wpa_ssid *old_ssid;
591 if (wpa_s->confname == NULL)
593 conf = wpa_config_read(wpa_s->confname);
595 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
596 "file '%s' - exiting", wpa_s->confname);
600 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
601 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
602 os_strcmp(conf->ctrl_interface,
603 wpa_s->conf->ctrl_interface) != 0);
605 if (reconf_ctrl && wpa_s->ctrl_iface) {
606 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
607 wpa_s->ctrl_iface = NULL;
610 eapol_sm_invalidate_cached_session(wpa_s->eapol);
611 old_ssid = wpa_s->current_ssid;
612 wpa_s->current_ssid = NULL;
613 if (old_ssid != wpa_s->current_ssid)
614 wpas_notify_network_changed(wpa_s);
617 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
618 * pkcs11_engine_path, pkcs11_module_path.
620 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
622 * Clear forced success to clear EAP state for next
625 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
627 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
628 wpa_sm_set_config(wpa_s->wpa, NULL);
629 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
630 rsn_preauth_deinit(wpa_s->wpa);
632 old_ap_scan = wpa_s->conf->ap_scan;
633 wpa_config_free(wpa_s->conf);
635 if (old_ap_scan != wpa_s->conf->ap_scan)
636 wpas_notify_ap_scan_changed(wpa_s);
639 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
641 wpa_supplicant_clear_status(wpa_s);
642 wpa_s->reassociate = 1;
643 wpa_supplicant_req_scan(wpa_s, 0, 0);
644 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
649 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
651 struct wpa_global *global = signal_ctx;
652 struct wpa_supplicant *wpa_s;
653 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
654 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
655 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
662 static wpa_cipher cipher_suite2driver(int cipher)
665 case WPA_CIPHER_NONE:
667 case WPA_CIPHER_WEP40:
669 case WPA_CIPHER_WEP104:
670 return CIPHER_WEP104;
671 case WPA_CIPHER_CCMP:
673 case WPA_CIPHER_TKIP:
680 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
683 case WPA_KEY_MGMT_NONE:
684 return KEY_MGMT_NONE;
685 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
686 return KEY_MGMT_802_1X_NO_WPA;
687 case WPA_KEY_MGMT_IEEE8021X:
688 return KEY_MGMT_802_1X;
689 case WPA_KEY_MGMT_WPA_NONE:
690 return KEY_MGMT_WPA_NONE;
691 case WPA_KEY_MGMT_FT_IEEE8021X:
692 return KEY_MGMT_FT_802_1X;
693 case WPA_KEY_MGMT_FT_PSK:
694 return KEY_MGMT_FT_PSK;
695 case WPA_KEY_MGMT_IEEE8021X_SHA256:
696 return KEY_MGMT_802_1X_SHA256;
697 case WPA_KEY_MGMT_PSK_SHA256:
698 return KEY_MGMT_PSK_SHA256;
699 case WPA_KEY_MGMT_WPS:
701 case WPA_KEY_MGMT_PSK:
708 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
709 struct wpa_ssid *ssid,
710 struct wpa_ie_data *ie)
712 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
715 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
716 "from association info");
721 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
723 if (!(ie->group_cipher & ssid->group_cipher)) {
724 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
725 "cipher 0x%x (mask 0x%x) - reject",
726 ie->group_cipher, ssid->group_cipher);
729 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
730 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
731 "cipher 0x%x (mask 0x%x) - reject",
732 ie->pairwise_cipher, ssid->pairwise_cipher);
735 if (!(ie->key_mgmt & ssid->key_mgmt)) {
736 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
737 "management 0x%x (mask 0x%x) - reject",
738 ie->key_mgmt, ssid->key_mgmt);
742 #ifdef CONFIG_IEEE80211W
743 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
744 ssid->ieee80211w == IEEE80211W_REQUIRED) {
745 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
746 "that does not support management frame protection - "
750 #endif /* CONFIG_IEEE80211W */
757 * wpa_supplicant_set_suites - Set authentication and encryption parameters
758 * @wpa_s: Pointer to wpa_supplicant data
759 * @bss: Scan results for the selected BSS, or %NULL if not available
760 * @ssid: Configuration data for the selected network
761 * @wpa_ie: Buffer for the WPA/RSN IE
762 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
763 * used buffer length in case the functions returns success.
764 * Returns: 0 on success or -1 on failure
766 * This function is used to configure authentication and encryption parameters
767 * based on the network configuration and scan result for the selected BSS (if
770 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
771 struct wpa_scan_res *bss,
772 struct wpa_ssid *ssid,
773 u8 *wpa_ie, size_t *wpa_ie_len)
775 struct wpa_ie_data ie;
777 const u8 *bss_wpa, *bss_rsn;
780 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
781 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
783 bss_wpa = bss_rsn = NULL;
785 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
786 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
787 (ie.group_cipher & ssid->group_cipher) &&
788 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
789 (ie.key_mgmt & ssid->key_mgmt)) {
790 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
791 proto = WPA_PROTO_RSN;
792 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
793 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
794 (ie.group_cipher & ssid->group_cipher) &&
795 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
796 (ie.key_mgmt & ssid->key_mgmt)) {
797 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
798 proto = WPA_PROTO_WPA;
800 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
803 if (ssid->proto & WPA_PROTO_RSN)
804 proto = WPA_PROTO_RSN;
806 proto = WPA_PROTO_WPA;
807 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
808 os_memset(&ie, 0, sizeof(ie));
809 ie.group_cipher = ssid->group_cipher;
810 ie.pairwise_cipher = ssid->pairwise_cipher;
811 ie.key_mgmt = ssid->key_mgmt;
812 #ifdef CONFIG_IEEE80211W
813 ie.mgmt_group_cipher =
814 ssid->ieee80211w != NO_IEEE80211W ?
815 WPA_CIPHER_AES_128_CMAC : 0;
816 #endif /* CONFIG_IEEE80211W */
817 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
823 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
824 "pairwise %d key_mgmt %d proto %d",
825 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
826 #ifdef CONFIG_IEEE80211W
827 if (ssid->ieee80211w) {
828 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
829 ie.mgmt_group_cipher);
831 #endif /* CONFIG_IEEE80211W */
833 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
834 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
835 !!(ssid->proto & WPA_PROTO_RSN));
837 if (bss || !wpa_s->ap_ies_from_associnfo) {
838 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
839 bss_wpa ? 2 + bss_wpa[1] : 0) ||
840 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
841 bss_rsn ? 2 + bss_rsn[1] : 0))
845 sel = ie.group_cipher & ssid->group_cipher;
846 if (sel & WPA_CIPHER_CCMP) {
847 wpa_s->group_cipher = WPA_CIPHER_CCMP;
848 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
849 } else if (sel & WPA_CIPHER_TKIP) {
850 wpa_s->group_cipher = WPA_CIPHER_TKIP;
851 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
852 } else if (sel & WPA_CIPHER_WEP104) {
853 wpa_s->group_cipher = WPA_CIPHER_WEP104;
854 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
855 } else if (sel & WPA_CIPHER_WEP40) {
856 wpa_s->group_cipher = WPA_CIPHER_WEP40;
857 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
859 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
863 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
864 if (sel & WPA_CIPHER_CCMP) {
865 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
866 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
867 } else if (sel & WPA_CIPHER_TKIP) {
868 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
869 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
870 } else if (sel & WPA_CIPHER_NONE) {
871 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
872 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
874 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
879 sel = ie.key_mgmt & ssid->key_mgmt;
881 #ifdef CONFIG_IEEE80211R
882 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
883 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
884 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
885 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
886 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
887 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
888 #endif /* CONFIG_IEEE80211R */
889 #ifdef CONFIG_IEEE80211W
890 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
891 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
892 wpa_msg(wpa_s, MSG_DEBUG,
893 "WPA: using KEY_MGMT 802.1X with SHA256");
894 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
895 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
896 wpa_msg(wpa_s, MSG_DEBUG,
897 "WPA: using KEY_MGMT PSK with SHA256");
898 #endif /* CONFIG_IEEE80211W */
899 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
900 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
901 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
902 } else if (sel & WPA_KEY_MGMT_PSK) {
903 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
904 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
905 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
906 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
907 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
909 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
910 "key management type.");
914 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
915 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
916 wpa_s->pairwise_cipher);
917 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
919 #ifdef CONFIG_IEEE80211W
920 sel = ie.mgmt_group_cipher;
921 if (ssid->ieee80211w == NO_IEEE80211W ||
922 !(ie.capabilities & WPA_CAPABILITY_MFPC))
924 if (sel & WPA_CIPHER_AES_128_CMAC) {
925 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
926 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
929 wpa_s->mgmt_group_cipher = 0;
930 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
932 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
933 wpa_s->mgmt_group_cipher);
934 #endif /* CONFIG_IEEE80211W */
936 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
937 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
942 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
943 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
945 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
952 * wpa_supplicant_associate - Request association
953 * @wpa_s: Pointer to wpa_supplicant data
954 * @bss: Scan results for the selected BSS, or %NULL if not available
955 * @ssid: Configuration data for the selected network
957 * This function is used to request %wpa_supplicant to associate with a BSS.
959 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
960 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
964 int use_crypt, ret, i, bssid_changed;
965 int algs = AUTH_ALG_OPEN_SYSTEM;
966 wpa_cipher cipher_pairwise, cipher_group;
967 struct wpa_driver_associate_params params;
968 int wep_keys_set = 0;
969 struct wpa_driver_capa capa;
970 int assoc_failed = 0;
971 struct wpa_ssid *old_ssid;
973 if (ssid->mode == IEEE80211_MODE_AP) {
975 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
976 wpa_printf(MSG_INFO, "Driver does not support AP "
980 wpa_supplicant_create_ap(wpa_s, ssid);
981 #else /* CONFIG_AP */
982 wpa_printf(MSG_ERROR, "AP mode support not included in the "
984 #endif /* CONFIG_AP */
988 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
989 ssid->mode == IEEE80211_MODE_INFRA) {
990 sme_authenticate(wpa_s, bss, ssid);
994 wpa_s->reassociate = 0;
996 #ifdef CONFIG_IEEE80211R
998 #endif /* CONFIG_IEEE80211R */
999 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1000 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1001 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1002 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
1003 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1004 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1005 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1007 wpas_notify_bssid_changed(wpa_s);
1008 #ifdef CONFIG_IEEE80211R
1009 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1010 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1012 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
1014 /* Prepare for the next transition */
1015 wpa_ft_prepare_auth_request(wpa_s->wpa);
1017 #endif /* CONFIG_IEEE80211R */
1019 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1020 wpa_s->conf->ap_scan == 2 &&
1021 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1022 /* Use ap_scan==1 style network selection to find the network
1024 wpa_s->scan_req = 2;
1025 wpa_s->reassociate = 1;
1026 wpa_supplicant_req_scan(wpa_s, 0, 0);
1028 #endif /* CONFIG_WPS */
1030 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1031 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1032 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1034 wpa_supplicant_cancel_scan(wpa_s);
1036 /* Starting new association, so clear the possibly used WPA IE from the
1037 * previous association. */
1038 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1040 #ifdef IEEE8021X_EAPOL
1041 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1043 if (ssid->non_leap == 0)
1044 algs = AUTH_ALG_LEAP;
1046 algs |= AUTH_ALG_LEAP;
1049 #endif /* IEEE8021X_EAPOL */
1050 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1051 if (ssid->auth_alg) {
1053 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1054 algs |= AUTH_ALG_OPEN_SYSTEM;
1055 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1056 algs |= AUTH_ALG_SHARED_KEY;
1057 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1058 algs |= AUTH_ALG_LEAP;
1059 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1063 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1064 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
1065 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1066 WPA_KEY_MGMT_FT_IEEE8021X |
1067 WPA_KEY_MGMT_FT_PSK |
1068 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1069 WPA_KEY_MGMT_PSK_SHA256))) {
1070 int try_opportunistic;
1071 try_opportunistic = ssid->proactive_key_caching &&
1072 (ssid->proto & WPA_PROTO_RSN);
1073 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1074 wpa_s->current_ssid,
1075 try_opportunistic) == 0)
1076 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1077 wpa_ie_len = sizeof(wpa_ie);
1078 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1079 wpa_ie, &wpa_ie_len)) {
1080 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1081 "management and encryption suites");
1084 } else if (ssid->key_mgmt &
1085 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1086 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1087 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1088 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1089 wpa_ie_len = sizeof(wpa_ie);
1090 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1091 wpa_ie, &wpa_ie_len)) {
1092 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1093 "management and encryption suites (no scan "
1098 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1099 struct wpabuf *wps_ie;
1100 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1101 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1102 wpa_ie_len = wpabuf_len(wps_ie);
1103 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1106 wpabuf_free(wps_ie);
1107 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1108 #endif /* CONFIG_WPS */
1110 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1114 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1116 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1117 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1118 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1119 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1120 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1122 if (wpa_set_wep_keys(wpa_s, ssid)) {
1127 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1130 #ifdef IEEE8021X_EAPOL
1131 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1132 if ((ssid->eapol_flags &
1133 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1134 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1138 /* Assume that dynamic WEP-104 keys will be used and
1139 * set cipher suites in order for drivers to expect
1141 cipher_pairwise = cipher_group = CIPHER_WEP104;
1144 #endif /* IEEE8021X_EAPOL */
1146 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1147 /* Set the key before (and later after) association */
1148 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1151 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1152 os_memset(¶ms, 0, sizeof(params));
1154 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1155 params.bssid = bss->bssid;
1156 params.ssid = ie ? ie + 2 : (u8 *) "";
1157 params.ssid_len = ie ? ie[1] : 0;
1158 params.freq = bss->freq;
1160 params.ssid = ssid->ssid;
1161 params.ssid_len = ssid->ssid_len;
1163 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1164 params.freq = ssid->frequency; /* Initial channel for IBSS */
1165 params.wpa_ie = wpa_ie;
1166 params.wpa_ie_len = wpa_ie_len;
1167 params.pairwise_suite = cipher_pairwise;
1168 params.group_suite = cipher_group;
1169 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1170 params.auth_alg = algs;
1171 params.mode = ssid->mode;
1172 for (i = 0; i < NUM_WEP_KEYS; i++) {
1173 if (ssid->wep_key_len[i])
1174 params.wep_key[i] = ssid->wep_key[i];
1175 params.wep_key_len[i] = ssid->wep_key_len[i];
1177 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1179 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1180 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1181 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1182 params.passphrase = ssid->passphrase;
1184 params.psk = ssid->psk;
1187 params.drop_unencrypted = use_crypt;
1189 #ifdef CONFIG_IEEE80211W
1190 switch (ssid->ieee80211w) {
1192 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1194 case IEEE80211W_OPTIONAL:
1195 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1197 case IEEE80211W_REQUIRED:
1198 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1201 if (ssid->ieee80211w != NO_IEEE80211W && bss) {
1202 const u8 *rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
1203 struct wpa_ie_data ie;
1204 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1206 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1207 wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
1209 params.mgmt_frame_protection =
1210 MGMT_FRAME_PROTECTION_REQUIRED;
1213 #endif /* CONFIG_IEEE80211W */
1215 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1216 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1218 ret = wpa_drv_associate(wpa_s, ¶ms);
1220 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1222 /* try to continue anyway; new association will be tried again
1227 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1228 /* Set the key after the association just in case association
1229 * cleared the previously configured key. */
1230 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1231 /* No need to timeout authentication since there is no key
1233 wpa_supplicant_cancel_auth_timeout(wpa_s);
1234 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1235 #ifdef CONFIG_IBSS_RSN
1236 } else if (ssid->mode == IEEE80211_MODE_IBSS &&
1237 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1238 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1239 ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
1241 * RSN IBSS authentication is per-STA and we can disable the
1242 * per-BSSID authentication.
1244 wpa_supplicant_cancel_auth_timeout(wpa_s);
1245 #endif /* CONFIG_IBSS_RSN */
1247 /* Timeout for IEEE 802.11 authentication and association */
1251 /* give IBSS a bit more time */
1252 timeout = ssid->mode ? 10 : 5;
1253 } else if (wpa_s->conf->ap_scan == 1) {
1254 /* give IBSS a bit more time */
1255 timeout = ssid->mode ? 20 : 10;
1257 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1260 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1261 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1262 /* Set static WEP keys again */
1263 wpa_set_wep_keys(wpa_s, ssid);
1266 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1268 * Do not allow EAP session resumption between different
1269 * network configurations.
1271 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1273 old_ssid = wpa_s->current_ssid;
1274 wpa_s->current_ssid = ssid;
1275 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1276 wpa_supplicant_initiate_eapol(wpa_s);
1277 if (old_ssid != wpa_s->current_ssid)
1278 wpas_notify_network_changed(wpa_s);
1283 * wpa_supplicant_disassociate - Disassociate the current connection
1284 * @wpa_s: Pointer to wpa_supplicant data
1285 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1287 * This function is used to request %wpa_supplicant to disassociate with the
1290 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1293 struct wpa_ssid *old_ssid;
1296 if (!is_zero_ether_addr(wpa_s->bssid)) {
1297 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1298 ieee80211_sta_disassociate(wpa_s, reason_code);
1300 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1301 addr = wpa_s->bssid;
1303 wpa_clear_keys(wpa_s, addr);
1304 wpa_supplicant_mark_disassoc(wpa_s);
1305 old_ssid = wpa_s->current_ssid;
1306 wpa_s->current_ssid = NULL;
1307 wpa_sm_set_config(wpa_s->wpa, NULL);
1308 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1309 if (old_ssid != wpa_s->current_ssid)
1310 wpas_notify_network_changed(wpa_s);
1315 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1316 * @wpa_s: Pointer to wpa_supplicant data
1317 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1319 * This function is used to request %wpa_supplicant to deauthenticate from the
1322 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1325 struct wpa_ssid *old_ssid;
1328 if (!is_zero_ether_addr(wpa_s->bssid)) {
1329 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1330 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1332 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1334 addr = wpa_s->bssid;
1336 wpa_clear_keys(wpa_s, addr);
1337 wpa_supplicant_mark_disassoc(wpa_s);
1338 old_ssid = wpa_s->current_ssid;
1339 wpa_s->current_ssid = NULL;
1340 wpa_sm_set_config(wpa_s->wpa, NULL);
1341 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1342 if (old_ssid != wpa_s->current_ssid)
1343 wpas_notify_network_changed(wpa_s);
1348 * wpa_supplicant_enable_network - Mark a configured network as enabled
1349 * @wpa_s: wpa_supplicant structure for a network interface
1350 * @ssid: wpa_ssid structure for a configured network or %NULL
1352 * Enables the specified network or all networks if no network specified.
1354 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1355 struct wpa_ssid *ssid)
1357 struct wpa_ssid *other_ssid;
1361 other_ssid = wpa_s->conf->ssid;
1362 while (other_ssid) {
1363 if (other_ssid == wpa_s->current_ssid &&
1364 other_ssid->disabled)
1365 wpa_s->reassociate = 1;
1367 was_disabled = other_ssid->disabled;
1369 other_ssid->disabled = 0;
1371 if (was_disabled != other_ssid->disabled)
1372 wpas_notify_network_enabled_changed(
1375 other_ssid = other_ssid->next;
1377 if (wpa_s->reassociate)
1378 wpa_supplicant_req_scan(wpa_s, 0, 0);
1379 } else if (wpa_s->current_ssid == NULL && ssid->disabled) {
1381 * Try to reassociate since there is no current configuration
1382 * and a new network was made available.
1384 wpa_s->reassociate = 1;
1385 wpa_supplicant_req_scan(wpa_s, 0, 0);
1387 was_disabled = ssid->disabled;
1391 if (was_disabled != ssid->disabled)
1392 wpas_notify_network_enabled_changed(wpa_s, ssid);
1398 * wpa_supplicant_disable_network - Mark a configured network as disabled
1399 * @wpa_s: wpa_supplicant structure for a network interface
1400 * @ssid: wpa_ssid structure for a configured network or %NULL
1402 * Disables the specified network or all networks if no network specified.
1404 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1405 struct wpa_ssid *ssid)
1407 struct wpa_ssid *other_ssid;
1411 other_ssid = wpa_s->conf->ssid;
1412 while (other_ssid) {
1413 was_disabled = other_ssid->disabled;
1415 other_ssid->disabled = 1;
1417 if (was_disabled != other_ssid->disabled)
1418 wpas_notify_network_enabled_changed(
1421 other_ssid = other_ssid->next;
1423 if (wpa_s->current_ssid)
1424 wpa_supplicant_disassociate(
1425 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1427 if (ssid == wpa_s->current_ssid)
1428 wpa_supplicant_disassociate(
1429 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1431 was_disabled = ssid->disabled;
1435 if (was_disabled != ssid->disabled)
1436 wpas_notify_network_enabled_changed(wpa_s, ssid);
1442 * wpa_supplicant_select_network - Attempt association with a network
1443 * @wpa_s: wpa_supplicant structure for a network interface
1444 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1446 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1447 struct wpa_ssid *ssid)
1450 struct wpa_ssid *other_ssid;
1452 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1453 wpa_supplicant_disassociate(
1454 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1457 * Mark all other networks disabled or mark all networks enabled if no
1458 * network specified.
1460 other_ssid = wpa_s->conf->ssid;
1461 while (other_ssid) {
1462 int was_disabled = other_ssid->disabled;
1464 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1466 if (was_disabled != other_ssid->disabled)
1467 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1469 other_ssid = other_ssid->next;
1471 wpa_s->disconnected = 0;
1472 wpa_s->reassociate = 1;
1473 wpa_supplicant_req_scan(wpa_s, 0, 0);
1476 wpas_notify_network_selected(wpa_s, ssid);
1481 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1482 * @wpa_s: wpa_supplicant structure for a network interface
1483 * @ap_scan: AP scan mode
1484 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1487 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1492 if (ap_scan < 0 || ap_scan > 2)
1495 old_ap_scan = wpa_s->conf->ap_scan;
1496 wpa_s->conf->ap_scan = ap_scan;
1498 if (old_ap_scan != wpa_s->conf->ap_scan)
1499 wpas_notify_ap_scan_changed(wpa_s);
1506 * wpa_supplicant_set_debug_params - Set global debug params
1507 * @global: wpa_global structure
1508 * @debug_level: debug level
1509 * @debug_timestamp: determines if show timestamp in debug data
1510 * @debug_show_keys: determines if show keys in debug data
1511 * Returns: 0 if succeed or -1 if debug_level has wrong value
1513 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1514 int debug_timestamp, int debug_show_keys)
1517 int old_level, old_timestamp, old_show_keys;
1519 /* check for allowed debuglevels */
1520 if (debug_level != MSG_MSGDUMP &&
1521 debug_level != MSG_DEBUG &&
1522 debug_level != MSG_INFO &&
1523 debug_level != MSG_WARNING &&
1524 debug_level != MSG_ERROR)
1527 old_level = wpa_debug_level;
1528 old_timestamp = wpa_debug_timestamp;
1529 old_show_keys = wpa_debug_show_keys;
1531 wpa_debug_level = debug_level;
1532 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1533 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1535 if (wpa_debug_level != old_level ||
1536 wpa_debug_timestamp != old_timestamp ||
1537 wpa_debug_show_keys != old_show_keys)
1538 wpas_notify_debug_params_changed(global);
1544 static void notify_bss_changes(struct wpa_supplicant *wpa_s,
1545 u8 (*prev_bssids)[ETH_ALEN], int prev_num,
1546 struct wpa_scan_results *new)
1550 new_num = new != NULL ? new->num : 0;
1551 if (prev_bssids == NULL)
1554 for (i = 0; i < prev_num; i++) {
1555 for (j = 0; j < new_num; j++) {
1556 if (!os_memcmp(prev_bssids[i], new->res[j]->bssid,
1561 wpas_notify_bss_removed(wpa_s, prev_bssids[i]);
1563 for (i = 0; i < new_num; i++) {
1564 for (j = 0; j < prev_num; j++) {
1565 if (!os_memcmp(new->res[i]->bssid, prev_bssids[j],
1570 wpas_notify_bss_added(wpa_s, new->res[i]->bssid);
1576 * wpa_supplicant_get_scan_results - Get scan results
1577 * @wpa_s: Pointer to wpa_supplicant data
1578 * Returns: 0 on success, -1 on failure
1580 * This function is request the current scan results from the driver and stores
1581 * a local copy of the results in wpa_s->scan_res.
1583 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1585 int ret, i, prev_scan_res_num;
1586 u8 (*prev_scan_bssids)[ETH_ALEN];
1588 prev_scan_res_num = wpa_s->scan_res ? wpa_s->scan_res->num : 0;
1589 prev_scan_bssids = os_malloc(prev_scan_res_num * ETH_ALEN);
1591 if (prev_scan_bssids) {
1592 for (i = 0; i < prev_scan_res_num; i++) {
1593 os_memcpy(prev_scan_bssids[i],
1594 wpa_s->scan_res->res[i]->bssid, ETH_ALEN);
1597 wpa_printf(MSG_WARNING, "Not enough memory for old scan "
1599 prev_scan_res_num = 0;
1602 wpa_scan_results_free(wpa_s->scan_res);
1603 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1604 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1606 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1607 if (wpa_s->scan_res == NULL) {
1608 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1612 wpa_scan_sort_results(wpa_s->scan_res);
1615 notify_bss_changes(wpa_s, prev_scan_bssids, prev_scan_res_num,
1617 os_free(prev_scan_bssids);
1624 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1625 * @wpa_s: Pointer to wpa_supplicant data
1626 * Returns: A pointer to the current network structure or %NULL on failure
1628 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1630 struct wpa_ssid *entry;
1631 u8 ssid[MAX_SSID_LEN];
1637 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1638 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1639 wpa_printf(MSG_WARNING, "Could not read SSID from "
1644 res = wpa_drv_get_ssid(wpa_s, ssid);
1646 wpa_printf(MSG_WARNING, "Could not read SSID from "
1653 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1654 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1655 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1656 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1660 wired = wpa_s->conf->ap_scan == 0 &&
1661 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1663 entry = wpa_s->conf->ssid;
1665 if (!entry->disabled &&
1666 ((ssid_len == entry->ssid_len &&
1667 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1668 (!entry->bssid_set ||
1669 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1672 if (!entry->disabled &&
1673 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1674 (entry->ssid == NULL || entry->ssid_len == 0) &&
1675 (!entry->bssid_set ||
1676 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1678 #endif /* CONFIG_WPS */
1679 entry = entry->next;
1686 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1696 if (wpa_drivers[0] == NULL) {
1697 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1703 /* default to first driver in the list */
1704 wpa_s->driver = wpa_drivers[0];
1705 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1709 pos = os_strchr(name, ',');
1713 len = os_strlen(name);
1714 for (i = 0; wpa_drivers[i]; i++) {
1715 if (os_strlen(wpa_drivers[i]->name) == len &&
1716 os_strncmp(name, wpa_drivers[i]->name, len) ==
1718 wpa_s->driver = wpa_drivers[i];
1719 wpa_s->global_drv_priv = wpa_s->global->drv_priv[i];
1724 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
1729 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1730 const u8 *buf, size_t len)
1732 struct wpa_supplicant *wpa_s = ctx;
1734 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1735 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1737 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1739 * There is possible race condition between receiving the
1740 * association event and the EAPOL frame since they are coming
1741 * through different paths from the driver. In order to avoid
1742 * issues in trying to process the EAPOL frame before receiving
1743 * association information, lets queue it for processing until
1744 * the association event is received.
1746 wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
1747 "received EAPOL frame");
1748 wpabuf_free(wpa_s->pending_eapol_rx);
1749 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1750 if (wpa_s->pending_eapol_rx) {
1751 os_get_time(&wpa_s->pending_eapol_rx_time);
1752 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1759 if (wpa_s->ap_iface) {
1760 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1763 #endif /* CONFIG_AP */
1765 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1766 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1767 "no key management is configured");
1771 if (wpa_s->eapol_received == 0 &&
1772 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1773 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1774 wpa_s->wpa_state != WPA_COMPLETED) &&
1775 (wpa_s->current_ssid == NULL ||
1776 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1777 /* Timeout for completing IEEE 802.1X and WPA authentication */
1778 wpa_supplicant_req_auth_timeout(
1780 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1781 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1782 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1785 wpa_s->eapol_received++;
1787 if (wpa_s->countermeasures) {
1788 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1793 #ifdef CONFIG_IBSS_RSN
1794 if (wpa_s->current_ssid &&
1795 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS) {
1796 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1799 #endif /* CONFIG_IBSS_RSN */
1801 /* Source address of the incoming EAPOL frame could be compared to the
1802 * current BSSID. However, it is possible that a centralized
1803 * Authenticator could be using another MAC address than the BSSID of
1804 * an AP, so just allow any address to be used for now. The replies are
1805 * still sent to the current BSSID (if available), though. */
1807 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1808 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1809 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1811 wpa_drv_poll(wpa_s);
1812 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1813 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1814 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1816 * Set portValid = TRUE here since we are going to skip 4-way
1817 * handshake processing which would normally set portValid. We
1818 * need this to allow the EAPOL state machines to be completed
1819 * without going through EAPOL-Key handshake.
1821 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1826 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1827 struct ieee80211_rx_status *rx_status)
1829 struct wpa_supplicant *wpa_s = ctx;
1830 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1835 * wpa_supplicant_driver_init - Initialize driver interface parameters
1836 * @wpa_s: Pointer to wpa_supplicant data
1837 * Returns: 0 on success, -1 on failure
1839 * This function is called to initialize driver interface parameters.
1840 * wpa_drv_init() must have been called before this function to initialize the
1843 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1845 static int interface_count = 0;
1847 if (wpa_s->driver->send_eapol) {
1848 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1850 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1852 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1853 wpa_drv_get_mac_addr(wpa_s),
1855 wpa_supplicant_rx_eapol, wpa_s, 0);
1856 if (wpa_s->l2 == NULL)
1860 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1861 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1865 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1866 MAC2STR(wpa_s->own_addr));
1868 if (wpa_s->bridge_ifname[0]) {
1869 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1870 " '%s'", wpa_s->bridge_ifname);
1871 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1874 wpa_supplicant_rx_eapol, wpa_s,
1876 if (wpa_s->l2_br == NULL) {
1877 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1878 "connection for the bridge interface '%s'",
1879 wpa_s->bridge_ifname);
1884 wpa_clear_keys(wpa_s, NULL);
1886 /* Make sure that TKIP countermeasures are not left enabled (could
1887 * happen if wpa_supplicant is killed during countermeasures. */
1888 wpa_drv_set_countermeasures(wpa_s, 0);
1890 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1891 wpa_drv_flush_pmkid(wpa_s);
1893 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
1894 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1901 static int wpa_supplicant_daemon(const char *pid_file)
1903 wpa_printf(MSG_DEBUG, "Daemonize..");
1904 return os_daemonize(pid_file);
1908 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1910 struct wpa_supplicant *wpa_s;
1912 wpa_s = os_zalloc(sizeof(*wpa_s));
1915 wpa_s->scan_req = 1;
1916 wpa_s->new_connection = 1;
1922 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1923 struct wpa_interface *iface)
1925 const char *ifname, *driver;
1926 struct wpa_driver_capa capa;
1928 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1929 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1930 iface->confname ? iface->confname : "N/A",
1931 iface->driver ? iface->driver : "default",
1932 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1933 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1935 if (iface->confname) {
1936 #ifdef CONFIG_BACKEND_FILE
1937 wpa_s->confname = os_rel2abs_path(iface->confname);
1938 if (wpa_s->confname == NULL) {
1939 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1940 "for configuration file '%s'.",
1944 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1945 iface->confname, wpa_s->confname);
1946 #else /* CONFIG_BACKEND_FILE */
1947 wpa_s->confname = os_strdup(iface->confname);
1948 #endif /* CONFIG_BACKEND_FILE */
1949 wpa_s->conf = wpa_config_read(wpa_s->confname);
1950 if (wpa_s->conf == NULL) {
1951 wpa_printf(MSG_ERROR, "Failed to read or parse "
1952 "configuration '%s'.", wpa_s->confname);
1957 * Override ctrl_interface and driver_param if set on command
1960 if (iface->ctrl_interface) {
1961 os_free(wpa_s->conf->ctrl_interface);
1962 wpa_s->conf->ctrl_interface =
1963 os_strdup(iface->ctrl_interface);
1966 if (iface->driver_param) {
1967 os_free(wpa_s->conf->driver_param);
1968 wpa_s->conf->driver_param =
1969 os_strdup(iface->driver_param);
1972 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1973 iface->driver_param);
1975 if (wpa_s->conf == NULL) {
1976 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1980 if (iface->ifname == NULL) {
1981 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1984 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1985 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1989 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1991 if (iface->bridge_ifname) {
1992 if (os_strlen(iface->bridge_ifname) >=
1993 sizeof(wpa_s->bridge_ifname)) {
1994 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1995 "name '%s'.", iface->bridge_ifname);
1998 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1999 sizeof(wpa_s->bridge_ifname));
2002 /* RSNA Supplicant Key Management - INITIALIZE */
2003 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2004 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2006 /* Initialize driver interface and register driver event handler before
2007 * L2 receive handler so that association events are processed before
2008 * EAPOL-Key packets if both become available for the same select()
2010 driver = iface->driver;
2012 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2015 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2016 if (wpa_s->drv_priv == NULL) {
2018 pos = os_strchr(driver, ',');
2020 wpa_printf(MSG_DEBUG, "Failed to initialize driver "
2021 "interface - try next driver wrapper");
2025 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2028 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2029 wpa_printf(MSG_ERROR, "Driver interface rejected "
2030 "driver_param '%s'", wpa_s->conf->driver_param);
2034 ifname = wpa_drv_get_ifname(wpa_s);
2035 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2036 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2037 "name with '%s'", ifname);
2038 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2041 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2044 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2045 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2047 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2049 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2050 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2051 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2052 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2053 "dot11RSNAConfigPMKLifetime");
2057 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2058 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2059 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2060 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2061 "dot11RSNAConfigPMKReauthThreshold");
2065 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2066 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2067 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2068 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2069 "dot11RSNAConfigSATimeout");
2073 if (wpa_supplicant_driver_init(wpa_s) < 0)
2076 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2077 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2078 wpa_printf(MSG_DEBUG, "Failed to set country");
2082 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2084 if (wpas_wps_init(wpa_s))
2087 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2089 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2091 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2092 if (wpa_s->ctrl_iface == NULL) {
2093 wpa_printf(MSG_ERROR,
2094 "Failed to initialize control interface '%s'.\n"
2095 "You may have another wpa_supplicant process "
2096 "already running or the file was\n"
2097 "left by an unclean termination of wpa_supplicant "
2098 "in which case you will need\n"
2099 "to manually remove this file before starting "
2100 "wpa_supplicant again.\n",
2101 wpa_s->conf->ctrl_interface);
2105 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2106 wpa_s->drv_flags = capa.flags;
2107 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2108 if (ieee80211_sta_init(wpa_s))
2111 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2114 #ifdef CONFIG_IBSS_RSN
2115 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2116 if (!wpa_s->ibss_rsn) {
2117 wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
2120 #endif /* CONFIG_IBSS_RSN */
2126 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2128 if (wpa_s->drv_priv) {
2129 wpa_supplicant_deauthenticate(wpa_s,
2130 WLAN_REASON_DEAUTH_LEAVING);
2132 wpa_drv_set_countermeasures(wpa_s, 0);
2133 wpa_clear_keys(wpa_s, NULL);
2136 wpas_notify_iface_removed(wpa_s);
2138 wpa_supplicant_cleanup(wpa_s);
2140 if (wpa_s->drv_priv)
2141 wpa_drv_deinit(wpa_s);
2146 * wpa_supplicant_add_iface - Add a new network interface
2147 * @global: Pointer to global data from wpa_supplicant_init()
2148 * @iface: Interface configuration options
2149 * Returns: Pointer to the created interface or %NULL on failure
2151 * This function is used to add new network interfaces for %wpa_supplicant.
2152 * This can be called before wpa_supplicant_run() to add interfaces before the
2153 * main event loop has been started. In addition, new interfaces can be added
2154 * dynamically while %wpa_supplicant is already running. This could happen,
2155 * e.g., when a hotplug network adapter is inserted.
2157 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2158 struct wpa_interface *iface)
2160 struct wpa_supplicant *wpa_s;
2161 struct wpa_interface t_iface;
2163 if (global == NULL || iface == NULL)
2166 wpa_s = wpa_supplicant_alloc();
2170 wpa_s->global = global;
2173 if (global->params.override_driver) {
2174 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2176 iface->driver, global->params.override_driver);
2177 t_iface.driver = global->params.override_driver;
2179 if (global->params.override_ctrl_interface) {
2180 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2181 "ctrl_interface ('%s' -> '%s')",
2182 iface->ctrl_interface,
2183 global->params.override_ctrl_interface);
2184 t_iface.ctrl_interface =
2185 global->params.override_ctrl_interface;
2187 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2188 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2190 wpa_supplicant_deinit_iface(wpa_s);
2195 /* Notify the control interfaces about new iface */
2196 if (wpas_notify_iface_added(wpa_s)) {
2197 wpa_supplicant_deinit_iface(wpa_s);
2202 wpa_s->next = global->ifaces;
2203 global->ifaces = wpa_s;
2205 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2212 * wpa_supplicant_remove_iface - Remove a network interface
2213 * @global: Pointer to global data from wpa_supplicant_init()
2214 * @wpa_s: Pointer to the network interface to be removed
2215 * Returns: 0 if interface was removed, -1 if interface was not found
2217 * This function can be used to dynamically remove network interfaces from
2218 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2219 * addition, this function is used to remove all remaining interfaces when
2220 * %wpa_supplicant is terminated.
2222 int wpa_supplicant_remove_iface(struct wpa_global *global,
2223 struct wpa_supplicant *wpa_s)
2225 struct wpa_supplicant *prev;
2227 /* Remove interface from the global list of interfaces */
2228 prev = global->ifaces;
2229 if (prev == wpa_s) {
2230 global->ifaces = wpa_s->next;
2232 while (prev && prev->next != wpa_s)
2236 prev->next = wpa_s->next;
2239 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2241 wpa_supplicant_deinit_iface(wpa_s);
2249 * wpa_supplicant_get_iface - Get a new network interface
2250 * @global: Pointer to global data from wpa_supplicant_init()
2251 * @ifname: Interface name
2252 * Returns: Pointer to the interface or %NULL if not found
2254 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2257 struct wpa_supplicant *wpa_s;
2259 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2260 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2268 * wpa_supplicant_init - Initialize %wpa_supplicant
2269 * @params: Parameters for %wpa_supplicant
2270 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2272 * This function is used to initialize %wpa_supplicant. After successful
2273 * initialization, the returned data pointer can be used to add and remove
2274 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2276 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2278 struct wpa_global *global;
2284 wpa_debug_open_file(params->wpa_debug_file_path);
2285 if (params->wpa_debug_syslog)
2286 wpa_debug_open_syslog();
2288 ret = eap_register_methods();
2290 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2292 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2293 "the same EAP type.");
2297 global = os_zalloc(sizeof(*global));
2300 global->params.daemonize = params->daemonize;
2301 global->params.wait_for_monitor = params->wait_for_monitor;
2302 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2303 if (params->pid_file)
2304 global->params.pid_file = os_strdup(params->pid_file);
2305 if (params->ctrl_interface)
2306 global->params.ctrl_interface =
2307 os_strdup(params->ctrl_interface);
2308 if (params->override_driver)
2309 global->params.override_driver =
2310 os_strdup(params->override_driver);
2311 if (params->override_ctrl_interface)
2312 global->params.override_ctrl_interface =
2313 os_strdup(params->override_ctrl_interface);
2314 wpa_debug_level = global->params.wpa_debug_level =
2315 params->wpa_debug_level;
2316 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2317 params->wpa_debug_show_keys;
2318 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2319 params->wpa_debug_timestamp;
2322 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2323 wpa_supplicant_deinit(global);
2327 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2328 if (global->ctrl_iface == NULL) {
2329 wpa_supplicant_deinit(global);
2333 if (wpas_notify_supplicant_initialized(global)) {
2334 wpa_supplicant_deinit(global);
2338 for (i = 0; wpa_drivers[i]; i++)
2339 global->drv_count++;
2340 if (global->drv_count == 0) {
2341 wpa_printf(MSG_ERROR, "No drivers enabled");
2342 wpa_supplicant_deinit(global);
2345 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2346 if (global->drv_priv == NULL) {
2347 wpa_supplicant_deinit(global);
2350 for (i = 0; wpa_drivers[i]; i++) {
2351 if (!wpa_drivers[i]->global_init)
2353 global->drv_priv[i] = wpa_drivers[i]->global_init();
2354 if (global->drv_priv[i] == NULL) {
2355 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2356 "'%s'", wpa_drivers[i]->name);
2357 wpa_supplicant_deinit(global);
2367 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2368 * @global: Pointer to global data from wpa_supplicant_init()
2369 * Returns: 0 after successful event loop run, -1 on failure
2371 * This function starts the main event loop and continues running as long as
2372 * there are any remaining events. In most cases, this function is running as
2373 * long as the %wpa_supplicant process in still in use.
2375 int wpa_supplicant_run(struct wpa_global *global)
2377 struct wpa_supplicant *wpa_s;
2379 if (global->params.daemonize &&
2380 wpa_supplicant_daemon(global->params.pid_file))
2383 if (global->params.wait_for_monitor) {
2384 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2385 if (wpa_s->ctrl_iface)
2386 wpa_supplicant_ctrl_iface_wait(
2390 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2391 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2400 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2401 * @global: Pointer to global data from wpa_supplicant_init()
2403 * This function is called to deinitialize %wpa_supplicant and to free all
2404 * allocated resources. Remaining network interfaces will also be removed.
2406 void wpa_supplicant_deinit(struct wpa_global *global)
2413 while (global->ifaces)
2414 wpa_supplicant_remove_iface(global, global->ifaces);
2416 if (global->ctrl_iface)
2417 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2419 wpas_notify_supplicant_deinitialized(global);
2421 eap_peer_unregister_methods();
2423 eap_server_unregister_methods();
2424 #endif /* CONFIG_AP */
2426 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2427 if (!global->drv_priv[i])
2429 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2431 os_free(global->drv_priv);
2435 if (global->params.pid_file) {
2436 os_daemonize_terminate(global->params.pid_file);
2437 os_free(global->params.pid_file);
2439 os_free(global->params.ctrl_interface);
2440 os_free(global->params.override_driver);
2441 os_free(global->params.override_ctrl_interface);
2444 wpa_debug_close_syslog();
2445 wpa_debug_close_file();