3 * Copyright (c) 2003-2008, 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.
28 #include "wpa_supplicant.h"
30 #include "l2_packet.h"
31 #include "wpa_supplicant_i.h"
32 #include "ctrl_iface.h"
33 #include "ctrl_iface_dbus.h"
34 #include "pcsc_funcs.h"
37 #include "pmksa_cache.h"
41 const char *wpa_supplicant_version =
42 "wpa_supplicant v" VERSION_STR "\n"
43 "Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> and contributors";
45 const char *wpa_supplicant_license =
46 "This program is free software. You can distribute it and/or modify it\n"
47 "under the terms of the GNU General Public License version 2.\n"
49 "Alternatively, this software may be distributed under the terms of the\n"
50 "BSD license. See README and COPYING for more details.\n"
51 #ifdef EAP_TLS_OPENSSL
52 "\nThis product includes software developed by the OpenSSL Project\n"
53 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
54 #endif /* EAP_TLS_OPENSSL */
57 #ifndef CONFIG_NO_STDOUT_DEBUG
58 /* Long text divided into parts in order to fit in C89 strings size limits. */
59 const char *wpa_supplicant_full_license1 =
60 "This program is free software; you can redistribute it and/or modify\n"
61 "it under the terms of the GNU General Public License version 2 as\n"
62 "published by the Free Software Foundation.\n"
64 "This program is distributed in the hope that it will be useful,\n"
65 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
66 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
67 "GNU General Public License for more details.\n"
69 const char *wpa_supplicant_full_license2 =
70 "You should have received a copy of the GNU General Public License\n"
71 "along with this program; if not, write to the Free Software\n"
72 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
74 "Alternatively, this software may be distributed under the terms of the\n"
77 "Redistribution and use in source and binary forms, with or without\n"
78 "modification, are permitted provided that the following conditions are\n"
81 const char *wpa_supplicant_full_license3 =
82 "1. Redistributions of source code must retain the above copyright\n"
83 " notice, this list of conditions and the following disclaimer.\n"
85 "2. Redistributions in binary form must reproduce the above copyright\n"
86 " notice, this list of conditions and the following disclaimer in the\n"
87 " documentation and/or other materials provided with the distribution.\n"
89 const char *wpa_supplicant_full_license4 =
90 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
91 " names of its contributors may be used to endorse or promote products\n"
92 " derived from this software without specific prior written permission.\n"
94 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
95 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
96 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
97 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
98 const char *wpa_supplicant_full_license5 =
99 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
100 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
101 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
102 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
103 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
104 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
105 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
107 #endif /* CONFIG_NO_STDOUT_DEBUG */
109 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
111 extern int wpa_debug_level;
112 extern int wpa_debug_show_keys;
113 extern int wpa_debug_timestamp;
115 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx);
117 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
118 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
119 const void *data, u16 data_len,
120 size_t *msg_len, void **data_pos)
122 struct ieee802_1x_hdr *hdr;
124 *msg_len = sizeof(*hdr) + data_len;
125 hdr = os_malloc(*msg_len);
129 hdr->version = wpa_s->conf->eapol_version;
131 hdr->length = host_to_be16(data_len);
134 os_memcpy(hdr + 1, data, data_len);
136 os_memset(hdr + 1, 0, data_len);
146 * wpa_ether_send - Send Ethernet frame
147 * @wpa_s: Pointer to wpa_supplicant data
148 * @dest: Destination MAC address
149 * @proto: Ethertype in host byte order
150 * @buf: Frame payload starting from IEEE 802.1X header
151 * @len: Frame payload length
152 * Returns: >=0 on success, <0 on failure
154 static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
155 u16 proto, const u8 *buf, size_t len)
158 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
161 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
163 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
166 #ifdef IEEE8021X_EAPOL
168 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
169 * @ctx: Pointer to wpa_supplicant data (wpa_s)
170 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
171 * @buf: EAPOL payload (after IEEE 802.1X header)
172 * @len: EAPOL payload length
173 * Returns: >=0 on success, <0 on failure
175 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
176 * to the current Authenticator.
178 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
181 struct wpa_supplicant *wpa_s = ctx;
182 u8 *msg, *dst, bssid[ETH_ALEN];
186 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
189 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
190 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
191 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
192 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
194 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
195 "mode (type=%d len=%lu)", type,
196 (unsigned long) len);
200 if (pmksa_cache_get_current(wpa_s->wpa) &&
201 type == IEEE802_1X_TYPE_EAPOL_START) {
202 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
203 * since they will trigger full EAPOL authentication. */
204 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
209 if (os_memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)
211 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
213 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
214 os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) !=
217 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
218 " from the driver as the EAPOL destination",
221 dst = wpa_s->last_eapol_src;
222 wpa_printf(MSG_DEBUG, "Using the source address of the"
223 " last received EAPOL frame " MACSTR " as "
224 "the EAPOL destination",
228 /* BSSID was already set (from (Re)Assoc event, so use it as
229 * the EAPOL destination. */
233 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
237 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
238 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
245 * wpa_eapol_set_wep_key - set WEP key for the driver
246 * @ctx: Pointer to wpa_supplicant data (wpa_s)
247 * @unicast: 1 = individual unicast key, 0 = broadcast key
248 * @keyidx: WEP key index (0..3)
249 * @key: Pointer to key data
250 * @keylen: Key length in bytes
251 * Returns: 0 on success or < 0 on error.
253 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
254 const u8 *key, size_t keylen)
256 struct wpa_supplicant *wpa_s = ctx;
257 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
258 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
261 wpa_s->pairwise_cipher = cipher;
263 wpa_s->group_cipher = cipher;
265 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
266 unicast ? wpa_s->bssid :
267 (u8 *) "\xff\xff\xff\xff\xff\xff",
268 keyidx, unicast, (u8 *) "", 0, key, keylen);
272 static void wpa_supplicant_aborted_cached(void *ctx)
274 struct wpa_supplicant *wpa_s = ctx;
275 wpa_sm_aborted_cached(wpa_s->wpa);
278 #endif /* IEEE8021X_EAPOL */
281 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
282 static void wpa_supplicant_set_config_blob(void *ctx,
283 struct wpa_config_blob *blob)
285 struct wpa_supplicant *wpa_s = ctx;
286 wpa_config_set_blob(wpa_s->conf, blob);
290 static const struct wpa_config_blob *
291 wpa_supplicant_get_config_blob(void *ctx, const char *name)
293 struct wpa_supplicant *wpa_s = ctx;
294 return wpa_config_get_blob(wpa_s->conf, name);
296 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
299 /* Configure default/group WEP key for static WEP */
300 static int wpa_set_wep_key(void *ctx, int set_tx, int keyidx, const u8 *key,
303 struct wpa_supplicant *wpa_s = ctx;
304 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
305 (u8 *) "\xff\xff\xff\xff\xff\xff",
306 keyidx, set_tx, (u8 *) "", 0, key, keylen);
310 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
311 struct wpa_ssid *ssid)
318 /* IBSS/WPA-None uses only one key (Group) for both receiving and
319 * sending unicast and multicast packets. */
321 if (ssid->mode != IEEE80211_MODE_IBSS) {
322 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
323 "for WPA-None", ssid->mode);
327 if (!ssid->psk_set) {
328 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
332 switch (wpa_s->group_cipher) {
333 case WPA_CIPHER_CCMP:
334 os_memcpy(key, ssid->psk, 16);
338 case WPA_CIPHER_TKIP:
339 /* WPA-None uses the same Michael MIC key for both TX and RX */
340 os_memcpy(key, ssid->psk, 16 + 8);
341 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
346 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
347 "WPA-None", wpa_s->group_cipher);
351 /* TODO: should actually remember the previously used seq#, both for TX
352 * and RX from each STA.. */
354 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
355 0, 1, seq, 6, key, keylen);
359 #ifdef IEEE8021X_EAPOL
360 static void wpa_supplicant_notify_eapol_done(void *ctx)
362 struct wpa_supplicant *wpa_s = ctx;
363 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
364 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
365 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
367 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
368 wpa_supplicant_cancel_auth_timeout(wpa_s);
369 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
372 #endif /* IEEE8021X_EAPOL */
376 * wpa_blacklist_get - Get the blacklist entry for a BSSID
377 * @wpa_s: Pointer to wpa_supplicant data
379 * Returns: Matching blacklist entry for the BSSID or %NULL if not found
381 struct wpa_blacklist * wpa_blacklist_get(struct wpa_supplicant *wpa_s,
384 struct wpa_blacklist *e;
386 e = wpa_s->blacklist;
388 if (os_memcmp(e->bssid, bssid, ETH_ALEN) == 0)
398 * wpa_blacklist_add - Add an BSSID to the blacklist
399 * @wpa_s: Pointer to wpa_supplicant data
400 * @bssid: BSSID to be added to the blacklist
401 * Returns: 0 on success, -1 on failure
403 * This function adds the specified BSSID to the blacklist or increases the
404 * blacklist count if the BSSID was already listed. It should be called when
405 * an association attempt fails either due to the selected BSS rejecting
406 * association or due to timeout.
408 * This blacklist is used to force %wpa_supplicant to go through all available
409 * BSSes before retrying to associate with an BSS that rejected or timed out
410 * association. It does not prevent the listed BSS from being used; it only
411 * changes the order in which they are tried.
413 int wpa_blacklist_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
415 struct wpa_blacklist *e;
417 e = wpa_blacklist_get(wpa_s, bssid);
420 wpa_printf(MSG_DEBUG, "BSSID " MACSTR " blacklist count "
422 MAC2STR(bssid), e->count);
426 e = os_zalloc(sizeof(*e));
429 os_memcpy(e->bssid, bssid, ETH_ALEN);
431 e->next = wpa_s->blacklist;
432 wpa_s->blacklist = e;
433 wpa_printf(MSG_DEBUG, "Added BSSID " MACSTR " into blacklist",
440 static int wpa_blacklist_del(struct wpa_supplicant *wpa_s, const u8 *bssid)
442 struct wpa_blacklist *e, *prev = NULL;
444 e = wpa_s->blacklist;
446 if (os_memcmp(e->bssid, bssid, ETH_ALEN) == 0) {
448 wpa_s->blacklist = e->next;
450 prev->next = e->next;
452 wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
453 "blacklist", MAC2STR(bssid));
465 * wpa_blacklist_clear - Clear the blacklist of all entries
466 * @wpa_s: Pointer to wpa_supplicant data
468 void wpa_blacklist_clear(struct wpa_supplicant *wpa_s)
470 struct wpa_blacklist *e, *prev;
472 e = wpa_s->blacklist;
473 wpa_s->blacklist = NULL;
477 wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
478 "blacklist (clear)", MAC2STR(prev->bssid));
485 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
486 * @wpa_s: Pointer to wpa_supplicant data
487 * @sec: Number of seconds after which to scan
488 * @usec: Number of microseconds after which to scan
490 * This function is used to schedule a scan for neighboring access points after
491 * the specified time.
493 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
495 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
497 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
498 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
503 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
504 * @wpa_s: Pointer to wpa_supplicant data
506 * This function is used to cancel a scan request scheduled with
507 * wpa_supplicant_req_scan().
509 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
511 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
512 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
516 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
518 struct wpa_supplicant *wpa_s = eloop_ctx;
519 const u8 *bssid = wpa_s->bssid;
520 if (os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)
521 bssid = wpa_s->pending_bssid;
522 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
524 wpa_blacklist_add(wpa_s, bssid);
525 wpa_sm_notify_disassoc(wpa_s->wpa);
526 wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
527 wpa_s->reassociate = 1;
528 wpa_supplicant_req_scan(wpa_s, 0, 0);
533 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
534 * @wpa_s: Pointer to wpa_supplicant data
535 * @sec: Number of seconds after which to time out authentication
536 * @usec: Number of microseconds after which to time out authentication
538 * This function is used to schedule a timeout for the current authentication
541 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
544 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
545 wpa_s->driver && os_strcmp(wpa_s->driver->name, "wired") == 0)
548 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
549 "%d usec", sec, usec);
550 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
551 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
556 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
557 * @wpa_s: Pointer to wpa_supplicant data
559 * This function is used to cancel authentication timeout scheduled with
560 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
563 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
565 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
566 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
567 wpa_blacklist_del(wpa_s, wpa_s->bssid);
572 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
573 * @wpa_s: Pointer to wpa_supplicant data
575 * This function is used to configure EAPOL state machine based on the selected
576 * authentication mode.
578 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
580 #ifdef IEEE8021X_EAPOL
581 struct eapol_config eapol_conf;
582 struct wpa_ssid *ssid = wpa_s->current_ssid;
584 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
585 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
586 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
588 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
589 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
590 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
592 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
594 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
595 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
596 eapol_conf.accept_802_1x_keys = 1;
597 eapol_conf.required_keys = 0;
598 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
599 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
601 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
602 eapol_conf.required_keys |=
603 EAPOL_REQUIRE_KEY_BROADCAST;
606 if (wpa_s->conf && wpa_s->driver &&
607 os_strcmp(wpa_s->driver->name, "wired") == 0) {
608 eapol_conf.required_keys = 0;
612 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
613 eapol_conf.workaround = ssid->eap_workaround;
614 eapol_conf.eap_disabled = wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
615 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA;
616 eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
617 #endif /* IEEE8021X_EAPOL */
622 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
623 * @wpa_s: Pointer to wpa_supplicant data
624 * @ssid: Configuration data for the network
626 * This function is used to configure WPA state machine and related parameters
627 * to a mode where WPA is not enabled. This is called as part of the
628 * authentication configuration when the selected network does not use WPA.
630 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
631 struct wpa_ssid *ssid)
635 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
636 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
638 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
639 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
640 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
641 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
642 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
643 wpa_s->group_cipher = WPA_CIPHER_NONE;
644 wpa_s->mgmt_group_cipher = 0;
646 for (i = 0; i < NUM_WEP_KEYS; i++) {
647 if (ssid->wep_key_len[i] > 5) {
648 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
649 wpa_s->group_cipher = WPA_CIPHER_WEP104;
651 } else if (ssid->wep_key_len[i] > 0) {
652 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
653 wpa_s->group_cipher = WPA_CIPHER_WEP40;
658 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
659 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
660 wpa_s->pairwise_cipher);
661 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
662 #ifdef CONFIG_IEEE80211W
663 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
664 wpa_s->mgmt_group_cipher);
665 #endif /* CONFIG_IEEE80211W */
667 pmksa_cache_clear_current(wpa_s->wpa);
671 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
673 scard_deinit(wpa_s->scard);
675 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
676 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
677 l2_packet_deinit(wpa_s->l2);
680 l2_packet_deinit(wpa_s->l2_br);
684 if (wpa_s->ctrl_iface) {
685 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
686 wpa_s->ctrl_iface = NULL;
688 if (wpa_s->conf != NULL) {
689 wpa_config_free(wpa_s->conf);
693 os_free(wpa_s->confname);
694 wpa_s->confname = NULL;
696 wpa_sm_set_eapol(wpa_s->wpa, NULL);
697 eapol_sm_deinit(wpa_s->eapol);
700 rsn_preauth_deinit(wpa_s->wpa);
702 pmksa_candidate_free(wpa_s->wpa);
703 wpa_sm_deinit(wpa_s->wpa);
705 wpa_blacklist_clear(wpa_s);
707 os_free(wpa_s->scan_results);
708 wpa_s->scan_results = NULL;
709 wpa_s->num_scan_results = 0;
711 wpa_supplicant_cancel_scan(wpa_s);
712 wpa_supplicant_cancel_auth_timeout(wpa_s);
714 ieee80211_sta_deinit(wpa_s);
719 * wpa_clear_keys - Clear keys configured for the driver
720 * @wpa_s: Pointer to wpa_supplicant data
721 * @addr: Previously used BSSID or %NULL if not available
723 * This function clears the encryption keys that has been previously configured
726 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
728 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
730 if (wpa_s->keys_cleared) {
731 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
732 * timing issues with keys being cleared just before new keys
733 * are set or just after association or something similar. This
734 * shows up in group key handshake failing often because of the
735 * client not receiving the first encrypted packets correctly.
736 * Skipping some of the extra key clearing steps seems to help
737 * in completing group key handshake more reliably. */
738 wpa_printf(MSG_DEBUG, "No keys have been configured - "
739 "skip key clearing");
743 /* MLME-DELETEKEYS.request */
744 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
745 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
746 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
747 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
749 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
751 /* MLME-SETPROTECTION.request(None) */
752 wpa_drv_mlme_setprotection(
754 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
755 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
757 wpa_s->keys_cleared = 1;
762 * wpa_supplicant_state_txt - Get the connection state name as a text string
763 * @state: State (wpa_state; WPA_*)
764 * Returns: The state name as a printable text string
766 const char * wpa_supplicant_state_txt(int state)
769 case WPA_DISCONNECTED:
770 return "DISCONNECTED";
775 case WPA_ASSOCIATING:
776 return "ASSOCIATING";
779 case WPA_4WAY_HANDSHAKE:
780 return "4WAY_HANDSHAKE";
781 case WPA_GROUP_HANDSHAKE:
782 return "GROUP_HANDSHAKE";
792 * wpa_supplicant_set_state - Set current connection state
793 * @wpa_s: Pointer to wpa_supplicant data
794 * @state: The new connection state
796 * This function is called whenever the connection state changes, e.g.,
797 * association is completed for WPA/WPA2 4-Way Handshake is started.
799 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
801 wpa_printf(MSG_DEBUG, "State: %s -> %s",
802 wpa_supplicant_state_txt(wpa_s->wpa_state),
803 wpa_supplicant_state_txt(state));
805 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
808 if (state == WPA_COMPLETED && wpa_s->new_connection) {
809 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
810 struct wpa_ssid *ssid = wpa_s->current_ssid;
811 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
812 MACSTR " completed %s [id=%d id_str=%s]",
813 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
814 "(reauth)" : "(auth)",
815 ssid ? ssid->id : -1,
816 ssid && ssid->id_str ? ssid->id_str : "");
817 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
818 wpa_s->new_connection = 0;
819 wpa_s->reassociated_connection = 1;
820 wpa_drv_set_operstate(wpa_s, 1);
821 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
822 state == WPA_ASSOCIATED) {
823 wpa_s->new_connection = 1;
824 wpa_drv_set_operstate(wpa_s, 0);
826 wpa_s->wpa_state = state;
831 * wpa_supplicant_get_state - Get the connection state
832 * @wpa_s: Pointer to wpa_supplicant data
833 * Returns: The current connection state (WPA_*)
835 wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
837 return wpa_s->wpa_state;
841 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
844 struct wpa_global *global = eloop_ctx;
845 struct wpa_supplicant *wpa_s;
846 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
847 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
854 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
856 wpa_s->pairwise_cipher = 0;
857 wpa_s->group_cipher = 0;
858 wpa_s->mgmt_group_cipher = 0;
860 wpa_s->wpa_state = WPA_DISCONNECTED;
865 * wpa_supplicant_reload_configuration - Reload configuration data
866 * @wpa_s: Pointer to wpa_supplicant data
867 * Returns: 0 on success or -1 if configuration parsing failed
869 * This function can be used to request that the configuration data is reloaded
870 * (e.g., after configuration file change). This function is reloading
871 * configuration only for one interface, so this may need to be called multiple
872 * times if %wpa_supplicant is controlling multiple interfaces and all
873 * interfaces need reconfiguration.
875 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
877 struct wpa_config *conf;
879 if (wpa_s->confname == NULL)
881 conf = wpa_config_read(wpa_s->confname);
883 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
884 "file '%s' - exiting", wpa_s->confname);
888 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
889 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
890 os_strcmp(conf->ctrl_interface,
891 wpa_s->conf->ctrl_interface) != 0);
893 if (reconf_ctrl && wpa_s->ctrl_iface) {
894 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
895 wpa_s->ctrl_iface = NULL;
898 eapol_sm_invalidate_cached_session(wpa_s->eapol);
899 wpa_s->current_ssid = NULL;
901 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
902 * pkcs11_engine_path, pkcs11_module_path.
904 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
906 * Clear forced success to clear EAP state for next
909 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
911 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
912 wpa_sm_set_config(wpa_s->wpa, NULL);
913 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
914 rsn_preauth_deinit(wpa_s->wpa);
915 wpa_config_free(wpa_s->conf);
918 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
920 wpa_supplicant_clear_status(wpa_s);
921 wpa_s->reassociate = 1;
922 wpa_supplicant_req_scan(wpa_s, 0, 0);
923 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
928 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
931 struct wpa_global *global = eloop_ctx;
932 struct wpa_supplicant *wpa_s;
933 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
934 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
935 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
942 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
944 struct wpa_ssid *ssid;
945 union wpa_event_data data;
947 ssid = wpa_supplicant_get_ssid(wpa_s);
951 if (wpa_s->current_ssid == NULL)
952 wpa_s->current_ssid = ssid;
953 wpa_supplicant_initiate_eapol(wpa_s);
954 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
955 "generating associated event");
956 os_memset(&data, 0, sizeof(data));
957 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
961 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
963 struct wpa_supplicant *wpa_s = eloop_ctx;
964 struct wpa_ssid *ssid;
965 int enabled, scan_req = 0, ret;
967 if (wpa_s->disconnected && !wpa_s->scan_req)
971 ssid = wpa_s->conf->ssid;
973 if (!ssid->disabled) {
979 if (!enabled && !wpa_s->scan_req) {
980 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
981 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
984 scan_req = wpa_s->scan_req;
987 if (wpa_s->conf->ap_scan != 0 &&
988 wpa_s->driver && os_strcmp(wpa_s->driver->name, "wired") == 0) {
989 wpa_printf(MSG_DEBUG, "Using wired driver - overriding "
990 "ap_scan configuration");
991 wpa_s->conf->ap_scan = 0;
994 if (wpa_s->conf->ap_scan == 0) {
995 wpa_supplicant_gen_assoc_event(wpa_s);
999 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1000 wpa_s->wpa_state == WPA_INACTIVE)
1001 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1003 ssid = wpa_s->conf->ssid;
1004 if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
1006 if (ssid == wpa_s->prev_scan_ssid) {
1014 if (!ssid->disabled &&
1015 (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
1020 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
1022 * ap_scan=2 mode - try to associate with each SSID instead of
1023 * scanning for each scan_ssid=1 network.
1026 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
1027 "end of scan list - go back to beginning");
1028 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1029 wpa_supplicant_req_scan(wpa_s, 0, 0);
1033 /* Continue from the next SSID on the next attempt. */
1034 wpa_s->prev_scan_ssid = ssid;
1036 /* Start from the beginning of the SSID list. */
1037 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1039 wpa_supplicant_associate(wpa_s, NULL, ssid);
1043 wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
1044 ssid ? "specific": "broadcast");
1046 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1047 ssid->ssid, ssid->ssid_len);
1048 wpa_s->prev_scan_ssid = ssid;
1050 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1052 if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1) {
1053 wpa_s->scan_res_tried++;
1054 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
1055 "first without requesting a new scan to speed up "
1056 "initial association");
1057 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
1061 if (wpa_s->use_client_mlme) {
1062 ret = ieee80211_sta_req_scan(wpa_s, ssid ? ssid->ssid : NULL,
1063 ssid ? ssid->ssid_len : 0);
1065 ret = wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
1066 ssid ? ssid->ssid_len : 0);
1069 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
1070 wpa_supplicant_req_scan(wpa_s, 10, 0);
1075 static wpa_cipher cipher_suite2driver(int cipher)
1078 case WPA_CIPHER_NONE:
1080 case WPA_CIPHER_WEP40:
1081 return CIPHER_WEP40;
1082 case WPA_CIPHER_WEP104:
1083 return CIPHER_WEP104;
1084 case WPA_CIPHER_CCMP:
1086 case WPA_CIPHER_TKIP:
1093 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
1096 case WPA_KEY_MGMT_NONE:
1097 return KEY_MGMT_NONE;
1098 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
1099 return KEY_MGMT_802_1X_NO_WPA;
1100 case WPA_KEY_MGMT_IEEE8021X:
1101 return KEY_MGMT_802_1X;
1102 case WPA_KEY_MGMT_WPA_NONE:
1103 return KEY_MGMT_WPA_NONE;
1104 case WPA_KEY_MGMT_PSK:
1106 return KEY_MGMT_PSK;
1111 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1112 struct wpa_ssid *ssid,
1113 struct wpa_ie_data *ie)
1115 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1118 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1119 "from association info");
1124 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
1126 if (!(ie->group_cipher & ssid->group_cipher)) {
1127 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1128 "cipher 0x%x (mask 0x%x) - reject",
1129 ie->group_cipher, ssid->group_cipher);
1132 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1133 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1134 "cipher 0x%x (mask 0x%x) - reject",
1135 ie->pairwise_cipher, ssid->pairwise_cipher);
1138 if (!(ie->key_mgmt & ssid->key_mgmt)) {
1139 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1140 "management 0x%x (mask 0x%x) - reject",
1141 ie->key_mgmt, ssid->key_mgmt);
1145 #ifdef CONFIG_IEEE80211W
1146 if (!(ie->capabilities & WPA_CAPABILITY_MGMT_FRAME_PROTECTION) &&
1147 ssid->ieee80211w == IEEE80211W_REQUIRED) {
1148 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1149 "that does not support management frame protection - "
1153 #endif /* CONFIG_IEEE80211W */
1160 * wpa_supplicant_set_suites - Set authentication and encryption parameters
1161 * @wpa_s: Pointer to wpa_supplicant data
1162 * @bss: Scan results for the selected BSS, or %NULL if not available
1163 * @ssid: Configuration data for the selected network
1164 * @wpa_ie: Buffer for the WPA/RSN IE
1165 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1166 * used buffer length in case the functions returns success.
1167 * Returns: 0 on success or -1 on failure
1169 * This function is used to configure authentication and encryption parameters
1170 * based on the network configuration and scan result for the selected BSS (if
1173 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1174 struct wpa_scan_result *bss,
1175 struct wpa_ssid *ssid,
1176 u8 *wpa_ie, size_t *wpa_ie_len)
1178 struct wpa_ie_data ie;
1181 if (bss && bss->rsn_ie_len && (ssid->proto & WPA_PROTO_RSN) &&
1182 wpa_parse_wpa_ie(bss->rsn_ie, bss->rsn_ie_len, &ie) == 0 &&
1183 (ie.group_cipher & ssid->group_cipher) &&
1184 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1185 (ie.key_mgmt & ssid->key_mgmt)) {
1186 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1187 proto = WPA_PROTO_RSN;
1188 } else if (bss && bss->wpa_ie_len && (ssid->proto & WPA_PROTO_WPA) &&
1189 wpa_parse_wpa_ie(bss->wpa_ie, bss->wpa_ie_len, &ie) == 0 &&
1190 (ie.group_cipher & ssid->group_cipher) &&
1191 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1192 (ie.key_mgmt & ssid->key_mgmt)) {
1193 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1194 proto = WPA_PROTO_WPA;
1196 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1199 if (ssid->proto & WPA_PROTO_RSN)
1200 proto = WPA_PROTO_RSN;
1202 proto = WPA_PROTO_WPA;
1203 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1204 os_memset(&ie, 0, sizeof(ie));
1205 ie.group_cipher = ssid->group_cipher;
1206 ie.pairwise_cipher = ssid->pairwise_cipher;
1207 ie.key_mgmt = ssid->key_mgmt;
1208 #ifdef CONFIG_IEEE80211W
1209 ie.mgmt_group_cipher =
1210 ssid->ieee80211w != NO_IEEE80211W ?
1211 WPA_CIPHER_AES_128_CMAC : 0;
1212 #endif /* CONFIG_IEEE80211W */
1213 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
1214 "on configuration");
1219 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1220 "pairwise %d key_mgmt %d proto %d",
1221 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1222 #ifdef CONFIG_IEEE80211W
1223 if (ssid->ieee80211w) {
1224 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1225 ie.mgmt_group_cipher);
1227 #endif /* CONFIG_IEEE80211W */
1229 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1231 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss ? bss->wpa_ie : NULL,
1232 bss ? bss->wpa_ie_len : 0) ||
1233 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss ? bss->rsn_ie : NULL,
1234 bss ? bss->rsn_ie_len : 0))
1237 sel = ie.group_cipher & ssid->group_cipher;
1238 if (sel & WPA_CIPHER_CCMP) {
1239 wpa_s->group_cipher = WPA_CIPHER_CCMP;
1240 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
1241 } else if (sel & WPA_CIPHER_TKIP) {
1242 wpa_s->group_cipher = WPA_CIPHER_TKIP;
1243 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
1244 } else if (sel & WPA_CIPHER_WEP104) {
1245 wpa_s->group_cipher = WPA_CIPHER_WEP104;
1246 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
1247 } else if (sel & WPA_CIPHER_WEP40) {
1248 wpa_s->group_cipher = WPA_CIPHER_WEP40;
1249 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
1251 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
1255 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1256 if (sel & WPA_CIPHER_CCMP) {
1257 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
1258 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
1259 } else if (sel & WPA_CIPHER_TKIP) {
1260 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
1261 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
1262 } else if (sel & WPA_CIPHER_NONE) {
1263 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1264 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
1266 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
1271 sel = ie.key_mgmt & ssid->key_mgmt;
1272 if (sel & WPA_KEY_MGMT_IEEE8021X) {
1273 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1274 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1275 } else if (sel & WPA_KEY_MGMT_PSK) {
1276 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1277 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1278 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1279 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1280 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1282 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
1283 "key management type.");
1287 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1288 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1289 wpa_s->pairwise_cipher);
1290 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1292 #ifdef CONFIG_IEEE80211W
1293 sel = ie.mgmt_group_cipher;
1294 if (ssid->ieee80211w == NO_IEEE80211W ||
1295 !(ie.capabilities & WPA_CAPABILITY_MGMT_FRAME_PROTECTION))
1297 if (sel & WPA_CIPHER_AES_128_CMAC) {
1298 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1299 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1302 wpa_s->mgmt_group_cipher = 0;
1303 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1305 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1306 wpa_s->mgmt_group_cipher);
1307 #endif /* CONFIG_IEEE80211W */
1309 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1310 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
1314 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1315 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1317 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1324 * wpa_supplicant_associate - Request association
1325 * @wpa_s: Pointer to wpa_supplicant data
1326 * @bss: Scan results for the selected BSS, or %NULL if not available
1327 * @ssid: Configuration data for the selected network
1329 * This function is used to request %wpa_supplicant to associate with a BSS.
1331 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1332 struct wpa_scan_result *bss,
1333 struct wpa_ssid *ssid)
1337 int use_crypt, ret, i;
1338 int algs = AUTH_ALG_OPEN_SYSTEM;
1339 wpa_cipher cipher_pairwise, cipher_group;
1340 struct wpa_driver_associate_params params;
1341 int wep_keys_set = 0;
1342 struct wpa_driver_capa capa;
1343 int assoc_failed = 0;
1345 wpa_s->reassociate = 0;
1347 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1348 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1349 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1350 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1351 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1353 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1354 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1355 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1357 wpa_supplicant_cancel_scan(wpa_s);
1359 /* Starting new association, so clear the possibly used WPA IE from the
1360 * previous association. */
1361 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1363 #ifdef IEEE8021X_EAPOL
1364 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1366 if (ssid->non_leap == 0)
1367 algs = AUTH_ALG_LEAP;
1369 algs |= AUTH_ALG_LEAP;
1372 #endif /* IEEE8021X_EAPOL */
1373 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1374 if (ssid->auth_alg) {
1376 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1377 algs |= AUTH_ALG_OPEN_SYSTEM;
1378 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1379 algs |= AUTH_ALG_SHARED_KEY;
1380 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1381 algs |= AUTH_ALG_LEAP;
1382 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1385 wpa_drv_set_auth_alg(wpa_s, algs);
1387 if (bss && (bss->wpa_ie_len || bss->rsn_ie_len) &&
1388 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK))) {
1389 int try_opportunistic;
1390 try_opportunistic = ssid->proactive_key_caching &&
1391 (ssid->proto & WPA_PROTO_RSN);
1392 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1393 wpa_s->current_ssid,
1394 try_opportunistic) == 0)
1395 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1396 wpa_ie_len = sizeof(wpa_ie);
1397 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1398 wpa_ie, &wpa_ie_len)) {
1399 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1400 "management and encryption suites");
1403 } else if (ssid->key_mgmt &
1404 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1405 WPA_KEY_MGMT_WPA_NONE)) {
1406 wpa_ie_len = sizeof(wpa_ie);
1407 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1408 wpa_ie, &wpa_ie_len)) {
1409 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1410 "management and encryption suites (no scan "
1415 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1419 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1421 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1422 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1423 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1424 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1425 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1427 for (i = 0; i < NUM_WEP_KEYS; i++) {
1428 if (ssid->wep_key_len[i]) {
1431 wpa_set_wep_key(wpa_s,
1432 i == ssid->wep_tx_keyidx,
1433 i, ssid->wep_key[i],
1434 ssid->wep_key_len[i]);
1439 #ifdef IEEE8021X_EAPOL
1440 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1441 if ((ssid->eapol_flags &
1442 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1443 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1447 /* Assume that dynamic WEP-104 keys will be used and
1448 * set cipher suites in order for drivers to expect
1450 cipher_pairwise = cipher_group = CIPHER_WEP104;
1453 #endif /* IEEE8021X_EAPOL */
1455 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1456 /* Set the key before (and later after) association */
1457 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1460 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1461 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1462 os_memset(¶ms, 0, sizeof(params));
1464 params.bssid = bss->bssid;
1465 params.ssid = bss->ssid;
1466 params.ssid_len = bss->ssid_len;
1467 params.freq = bss->freq;
1469 params.ssid = ssid->ssid;
1470 params.ssid_len = ssid->ssid_len;
1472 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1473 params.freq = ssid->frequency; /* Initial channel for IBSS */
1474 params.wpa_ie = wpa_ie;
1475 params.wpa_ie_len = wpa_ie_len;
1476 params.pairwise_suite = cipher_pairwise;
1477 params.group_suite = cipher_group;
1478 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1479 params.auth_alg = algs;
1480 params.mode = ssid->mode;
1481 for (i = 0; i < NUM_WEP_KEYS; i++) {
1482 if (ssid->wep_key_len[i])
1483 params.wep_key[i] = ssid->wep_key[i];
1484 params.wep_key_len[i] = ssid->wep_key_len[i];
1486 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1488 #ifdef CONFIG_IEEE80211W
1489 switch (ssid->ieee80211w) {
1491 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1493 case IEEE80211W_OPTIONAL:
1494 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1496 case IEEE80211W_REQUIRED:
1497 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1500 #endif /* CONFIG_IEEE80211W */
1502 if (wpa_s->use_client_mlme)
1503 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1505 ret = wpa_drv_associate(wpa_s, ¶ms);
1507 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1509 /* try to continue anyway; new association will be tried again
1514 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1515 /* Set the key after the association just in case association
1516 * cleared the previously configured key. */
1517 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1518 /* No need to timeout authentication since there is no key
1520 wpa_supplicant_cancel_auth_timeout(wpa_s);
1521 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1523 /* Timeout for IEEE 802.11 authentication and association */
1527 else if (wpa_s->conf->ap_scan == 1)
1531 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1534 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1535 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1536 /* Set static WEP keys again */
1538 for (j = 0; j < NUM_WEP_KEYS; j++) {
1539 if (ssid->wep_key_len[j]) {
1540 wpa_set_wep_key(wpa_s,
1541 j == ssid->wep_tx_keyidx,
1542 j, ssid->wep_key[j],
1543 ssid->wep_key_len[j]);
1548 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1550 * Do not allow EAP session resumption between different
1551 * network configurations.
1553 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1555 wpa_s->current_ssid = ssid;
1556 wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);
1557 wpa_supplicant_initiate_eapol(wpa_s);
1562 * wpa_supplicant_disassociate - Disassociate the current connection
1563 * @wpa_s: Pointer to wpa_supplicant data
1564 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1566 * This function is used to request %wpa_supplicant to disassociate with the
1569 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1573 if (os_memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0)
1575 if (wpa_s->use_client_mlme)
1576 ieee80211_sta_disassociate(wpa_s, reason_code);
1578 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1579 addr = wpa_s->bssid;
1581 wpa_clear_keys(wpa_s, addr);
1582 wpa_supplicant_mark_disassoc(wpa_s);
1583 wpa_s->current_ssid = NULL;
1584 wpa_sm_set_config(wpa_s->wpa, NULL);
1585 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1590 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1591 * @wpa_s: Pointer to wpa_supplicant data
1592 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1594 * This function is used to request %wpa_supplicant to disassociate with the
1597 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1601 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1602 if (os_memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0)
1604 if (wpa_s->use_client_mlme)
1605 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1607 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1609 addr = wpa_s->bssid;
1611 wpa_clear_keys(wpa_s, addr);
1612 wpa_s->current_ssid = NULL;
1613 wpa_sm_set_config(wpa_s->wpa, NULL);
1614 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1615 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1616 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1621 * wpa_supplicant_get_scan_results - Get scan results
1622 * @wpa_s: Pointer to wpa_supplicant data
1623 * Returns: 0 on success, -1 on failure
1625 * This function is request the current scan results from the driver and stores
1626 * a local copy of the results in wpa_s->scan_results.
1628 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1630 #define SCAN_AP_LIMIT 128
1631 struct wpa_scan_result *results, *tmp;
1634 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1635 if (results == NULL) {
1636 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1641 if (wpa_s->use_client_mlme) {
1642 num = ieee80211_sta_get_scan_results(wpa_s, results,
1645 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1646 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1648 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1652 if (num > SCAN_AP_LIMIT) {
1653 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1654 num, SCAN_AP_LIMIT);
1655 num = SCAN_AP_LIMIT;
1658 /* Free unneeded memory for unused scan result entries */
1659 tmp = os_realloc(results, num * sizeof(struct wpa_scan_result));
1660 if (tmp || num == 0) {
1664 os_free(wpa_s->scan_results);
1665 wpa_s->scan_results = results;
1666 wpa_s->num_scan_results = num;
1672 #ifndef CONFIG_NO_WPA
1673 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
1676 struct wpa_scan_result *results, *curr = NULL;
1678 results = wpa_s->scan_results;
1679 if (results == NULL) {
1683 for (i = 0; i < wpa_s->num_scan_results; i++) {
1684 struct wpa_ssid *ssid = wpa_s->current_ssid;
1685 if (os_memcmp(results[i].bssid, wpa_s->bssid, ETH_ALEN) != 0)
1688 ((results[i].ssid_len == ssid->ssid_len &&
1689 os_memcmp(results[i].ssid, ssid->ssid, ssid->ssid_len)
1691 ssid->ssid_len == 0)) {
1698 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, curr->wpa_ie,
1699 curr->wpa_ie_len) ||
1700 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, curr->rsn_ie,
1711 static int wpa_supplicant_get_beacon_ie(void *ctx)
1713 struct wpa_supplicant *wpa_s = ctx;
1714 if (wpa_get_beacon_ie(wpa_s) == 0) {
1718 /* No WPA/RSN IE found in the cached scan results. Try to get updated
1719 * scan results from the driver. */
1720 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
1724 return wpa_get_beacon_ie(wpa_s);
1726 #endif /* CONFIG_NO_WPA */
1730 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1731 * @wpa_s: Pointer to wpa_supplicant data
1732 * Returns: A pointer to the current network structure or %NULL on failure
1734 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1736 struct wpa_ssid *entry;
1737 u8 ssid[MAX_SSID_LEN];
1743 if (wpa_s->use_client_mlme) {
1744 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1745 wpa_printf(MSG_WARNING, "Could not read SSID from "
1750 res = wpa_drv_get_ssid(wpa_s, ssid);
1752 wpa_printf(MSG_WARNING, "Could not read SSID from "
1759 if (wpa_s->use_client_mlme)
1760 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1761 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1762 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1766 wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
1767 os_strcmp(wpa_s->driver->name, "wired") == 0;
1769 entry = wpa_s->conf->ssid;
1771 if (!entry->disabled &&
1772 ((ssid_len == entry->ssid_len &&
1773 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1774 (!entry->bssid_set ||
1775 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1777 entry = entry->next;
1784 #ifndef CONFIG_NO_WPA
1785 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
1786 const void *data, u16 data_len,
1787 size_t *msg_len, void **data_pos)
1789 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
1793 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
1794 const u8 *buf, size_t len)
1796 return wpa_ether_send(wpa_s, dest, proto, buf, len);
1800 static void _wpa_supplicant_req_scan(void *wpa_s, int sec, int usec)
1802 wpa_supplicant_req_scan(wpa_s, sec, usec);
1806 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
1808 wpa_supplicant_cancel_auth_timeout(wpa_s);
1812 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
1814 wpa_supplicant_set_state(wpa_s, state);
1818 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
1820 return wpa_supplicant_get_state(wpa_s);
1824 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
1826 wpa_supplicant_disassociate(wpa_s, reason_code);
1830 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
1832 wpa_supplicant_deauthenticate(wpa_s, reason_code);
1836 static struct wpa_ssid * _wpa_supplicant_get_ssid(void *wpa_s)
1838 return wpa_supplicant_get_ssid(wpa_s);
1842 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
1844 struct wpa_supplicant *wpa_s = ctx;
1845 if (wpa_s->use_client_mlme) {
1846 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1849 return wpa_drv_get_bssid(wpa_s, bssid);
1853 static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,
1854 const u8 *addr, int key_idx, int set_tx,
1855 const u8 *seq, size_t seq_len,
1856 const u8 *key, size_t key_len)
1858 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
1863 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
1864 int protection_type,
1867 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
1872 static int wpa_supplicant_add_pmkid(void *wpa_s,
1873 const u8 *bssid, const u8 *pmkid)
1875 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
1879 static int wpa_supplicant_remove_pmkid(void *wpa_s,
1880 const u8 *bssid, const u8 *pmkid)
1882 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
1884 #endif /* CONFIG_NO_WPA */
1887 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1895 if (wpa_supplicant_drivers[0] == NULL) {
1896 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1902 /* default to first driver in the list */
1903 wpa_s->driver = wpa_supplicant_drivers[0];
1907 for (i = 0; wpa_supplicant_drivers[i]; i++) {
1908 if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1909 wpa_s->driver = wpa_supplicant_drivers[i];
1914 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
1919 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1920 const u8 *buf, size_t len)
1922 struct wpa_supplicant *wpa_s = ctx;
1924 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1925 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1927 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1928 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1929 "no key management is configured");
1933 if (wpa_s->eapol_received == 0) {
1934 /* Timeout for completing IEEE 802.1X and WPA authentication */
1935 wpa_supplicant_req_auth_timeout(
1937 (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
1938 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
1941 wpa_s->eapol_received++;
1943 if (wpa_s->countermeasures) {
1944 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1949 /* Source address of the incoming EAPOL frame could be compared to the
1950 * current BSSID. However, it is possible that a centralized
1951 * Authenticator could be using another MAC address than the BSSID of
1952 * an AP, so just allow any address to be used for now. The replies are
1953 * still sent to the current BSSID (if available), though. */
1955 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1956 if (wpa_s->key_mgmt != WPA_KEY_MGMT_PSK &&
1957 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1959 wpa_drv_poll(wpa_s);
1960 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1965 * wpa_supplicant_driver_init - Initialize driver interface parameters
1966 * @wpa_s: Pointer to wpa_supplicant data
1967 * @wait_for_interface: 0 = do not wait for the interface (reports a failure if
1968 * the interface is not present), 1 = wait until the interface is available
1969 * Returns: 0 on success, -1 on failure
1971 * This function is called to initialize driver interface parameters.
1972 * wpa_drv_init() must have been called before this function to initialize the
1975 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s,
1976 int wait_for_interface)
1978 static int interface_count = 0;
1981 if (wpa_s->driver->send_eapol) {
1982 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1984 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1987 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1988 wpa_drv_get_mac_addr(wpa_s),
1990 wpa_supplicant_rx_eapol, wpa_s, 0);
1993 else if (!wait_for_interface)
1995 wpa_printf(MSG_DEBUG, "Waiting for interface..");
1999 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2000 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
2004 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
2005 MAC2STR(wpa_s->own_addr));
2007 if (wpa_s->bridge_ifname[0]) {
2008 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
2009 " '%s'", wpa_s->bridge_ifname);
2010 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2013 wpa_supplicant_rx_eapol, wpa_s,
2015 if (wpa_s->l2_br == NULL) {
2016 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
2017 "connection for the bridge interface '%s'",
2018 wpa_s->bridge_ifname);
2023 /* Backwards compatibility call to set_wpa() handler. This is called
2024 * only just after init and just before deinit, so these handler can be
2025 * used to implement same functionality. */
2026 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
2027 struct wpa_driver_capa capa;
2028 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
2029 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2030 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
2031 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
2032 /* Continue to allow non-WPA modes to be used. */
2034 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
2040 wpa_clear_keys(wpa_s, NULL);
2042 /* Make sure that TKIP countermeasures are not left enabled (could
2043 * happen if wpa_supplicant is killed during countermeasures. */
2044 wpa_drv_set_countermeasures(wpa_s, 0);
2046 wpa_drv_set_drop_unencrypted(wpa_s, 1);
2048 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
2049 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
2056 static int wpa_supplicant_daemon(const char *pid_file)
2058 wpa_printf(MSG_DEBUG, "Daemonize..");
2059 return os_daemonize(pid_file);
2063 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2065 struct wpa_supplicant *wpa_s;
2067 wpa_s = os_zalloc(sizeof(*wpa_s));
2070 wpa_s->scan_req = 1;
2076 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2077 struct wpa_interface *iface)
2079 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2080 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2081 iface->confname ? iface->confname : "N/A",
2082 iface->driver ? iface->driver : "default",
2083 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2084 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2086 if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
2090 if (iface->confname) {
2091 #ifdef CONFIG_BACKEND_FILE
2092 wpa_s->confname = os_rel2abs_path(iface->confname);
2093 if (wpa_s->confname == NULL) {
2094 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2095 "for configuration file '%s'.",
2099 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2100 iface->confname, wpa_s->confname);
2101 #else /* CONFIG_BACKEND_FILE */
2102 wpa_s->confname = os_strdup(iface->confname);
2103 #endif /* CONFIG_BACKEND_FILE */
2104 wpa_s->conf = wpa_config_read(wpa_s->confname);
2105 if (wpa_s->conf == NULL) {
2106 wpa_printf(MSG_ERROR, "Failed to read or parse "
2107 "configuration '%s'.", wpa_s->confname);
2112 * Override ctrl_interface and driver_param if set on command
2115 if (iface->ctrl_interface) {
2116 os_free(wpa_s->conf->ctrl_interface);
2117 wpa_s->conf->ctrl_interface =
2118 os_strdup(iface->ctrl_interface);
2121 if (iface->driver_param) {
2122 os_free(wpa_s->conf->driver_param);
2123 wpa_s->conf->driver_param =
2124 os_strdup(iface->driver_param);
2127 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2128 iface->driver_param);
2130 if (wpa_s->conf == NULL) {
2131 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2135 if (iface->ifname == NULL) {
2136 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2139 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2140 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2144 os_strncpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2146 if (iface->bridge_ifname) {
2147 if (os_strlen(iface->bridge_ifname) >=
2148 sizeof(wpa_s->bridge_ifname)) {
2149 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2150 "name '%s'.", iface->bridge_ifname);
2153 os_strncpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2154 sizeof(wpa_s->bridge_ifname));
2161 static int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
2163 #ifdef IEEE8021X_EAPOL
2164 struct eapol_ctx *ctx;
2165 ctx = os_zalloc(sizeof(*ctx));
2167 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
2172 ctx->msg_ctx = wpa_s;
2173 ctx->eapol_send_ctx = wpa_s;
2175 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
2176 ctx->eapol_send = wpa_supplicant_eapol_send;
2177 ctx->set_wep_key = wpa_eapol_set_wep_key;
2178 ctx->set_config_blob = wpa_supplicant_set_config_blob;
2179 ctx->get_config_blob = wpa_supplicant_get_config_blob;
2180 ctx->aborted_cached = wpa_supplicant_aborted_cached;
2181 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
2182 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
2183 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
2184 wpa_s->eapol = eapol_sm_init(ctx);
2185 if (wpa_s->eapol == NULL) {
2187 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
2191 #endif /* IEEE8021X_EAPOL */
2197 static int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
2199 #ifndef CONFIG_NO_WPA
2200 struct wpa_sm_ctx *ctx;
2201 ctx = os_zalloc(sizeof(*ctx));
2203 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
2208 ctx->set_state = _wpa_supplicant_set_state;
2209 ctx->get_state = _wpa_supplicant_get_state;
2210 ctx->req_scan = _wpa_supplicant_req_scan;
2211 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
2212 ctx->disassociate = _wpa_supplicant_disassociate;
2213 ctx->set_key = wpa_supplicant_set_key;
2214 ctx->scan = wpa_supplicant_scan;
2215 ctx->get_ssid = _wpa_supplicant_get_ssid;
2216 ctx->get_bssid = wpa_supplicant_get_bssid;
2217 ctx->ether_send = _wpa_ether_send;
2218 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
2219 ctx->alloc_eapol = _wpa_alloc_eapol;
2220 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
2221 ctx->add_pmkid = wpa_supplicant_add_pmkid;
2222 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
2223 ctx->set_config_blob = wpa_supplicant_set_config_blob;
2224 ctx->get_config_blob = wpa_supplicant_get_config_blob;
2225 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
2227 wpa_s->wpa = wpa_sm_init(ctx);
2228 if (wpa_s->wpa == NULL) {
2229 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
2233 #endif /* CONFIG_NO_WPA */
2239 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s,
2240 int wait_for_interface)
2243 struct wpa_driver_capa capa;
2245 wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
2248 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2251 /* RSNA Supplicant Key Management - INITIALIZE */
2252 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2253 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2255 /* Initialize driver interface and register driver event handler before
2256 * L2 receive handler so that association events are processed before
2257 * EAPOL-Key packets if both become available for the same select()
2259 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2260 if (wpa_s->drv_priv == NULL) {
2261 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2264 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2265 wpa_printf(MSG_ERROR, "Driver interface rejected "
2266 "driver_param '%s'", wpa_s->conf->driver_param);
2270 ifname = wpa_drv_get_ifname(wpa_s);
2271 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2272 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2273 "name with '%s'", ifname);
2274 os_strncpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2277 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2280 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2281 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2283 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2284 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2286 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2287 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2288 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2289 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2290 "dot11RSNAConfigPMKLifetime");
2294 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2295 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2296 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2297 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2298 "dot11RSNAConfigPMKReauthThreshold");
2302 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2303 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2304 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2305 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2306 "dot11RSNAConfigSATimeout");
2310 if (wpa_supplicant_driver_init(wpa_s, wait_for_interface) < 0) {
2313 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2315 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2316 if (wpa_s->ctrl_iface == NULL) {
2317 wpa_printf(MSG_ERROR,
2318 "Failed to initialize control interface '%s'.\n"
2319 "You may have another wpa_supplicant process "
2320 "already running or the file was\n"
2321 "left by an unclean termination of wpa_supplicant "
2322 "in which case you will need\n"
2323 "to manually remove this file before starting "
2324 "wpa_supplicant again.\n",
2325 wpa_s->conf->ctrl_interface);
2329 if (wpa_drv_get_capa(wpa_s, &capa) == 0 &&
2330 capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2331 wpa_s->use_client_mlme = 1;
2332 if (ieee80211_sta_init(wpa_s))
2340 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2342 if (wpa_s->drv_priv) {
2343 wpa_supplicant_deauthenticate(wpa_s, REASON_DEAUTH_LEAVING);
2345 /* Backwards compatibility call to set_wpa() handler. This is
2346 * called only just after init and just before deinit, so these
2347 * handler can be used to implement same functionality. */
2348 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
2349 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
2353 wpa_drv_set_drop_unencrypted(wpa_s, 0);
2354 wpa_drv_set_countermeasures(wpa_s, 0);
2355 wpa_clear_keys(wpa_s, NULL);
2358 wpas_dbus_unregister_iface(wpa_s);
2360 wpa_supplicant_cleanup(wpa_s);
2362 if (wpa_s->drv_priv)
2363 wpa_drv_deinit(wpa_s);
2368 * wpa_supplicant_add_iface - Add a new network interface
2369 * @global: Pointer to global data from wpa_supplicant_init()
2370 * @iface: Interface configuration options
2371 * Returns: Pointer to the created interface or %NULL on failure
2373 * This function is used to add new network interfaces for %wpa_supplicant.
2374 * This can be called before wpa_supplicant_run() to add interfaces before the
2375 * main event loop has been started. In addition, new interfaces can be added
2376 * dynamically while %wpa_supplicant is already running. This could happen,
2377 * e.g., when a hotplug network adapter is inserted.
2379 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2380 struct wpa_interface *iface)
2382 struct wpa_supplicant *wpa_s;
2384 if (global == NULL || iface == NULL)
2387 wpa_s = wpa_supplicant_alloc();
2391 if (wpa_supplicant_init_iface(wpa_s, iface) ||
2392 wpa_supplicant_init_iface2(wpa_s,
2393 global->params.wait_for_interface)) {
2394 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2396 wpa_supplicant_deinit_iface(wpa_s);
2401 wpa_s->global = global;
2403 /* Register the interface with the dbus control interface */
2404 if (wpas_dbus_register_iface(wpa_s)) {
2405 wpa_supplicant_deinit_iface(wpa_s);
2410 wpa_s->next = global->ifaces;
2411 global->ifaces = wpa_s;
2413 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2420 * wpa_supplicant_remove_iface - Remove a network interface
2421 * @global: Pointer to global data from wpa_supplicant_init()
2422 * @wpa_s: Pointer to the network interface to be removed
2423 * Returns: 0 if interface was removed, -1 if interface was not found
2425 * This function can be used to dynamically remove network interfaces from
2426 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2427 * addition, this function is used to remove all remaining interfaces when
2428 * %wpa_supplicant is terminated.
2430 int wpa_supplicant_remove_iface(struct wpa_global *global,
2431 struct wpa_supplicant *wpa_s)
2433 struct wpa_supplicant *prev;
2435 /* Remove interface from the global list of interfaces */
2436 prev = global->ifaces;
2437 if (prev == wpa_s) {
2438 global->ifaces = wpa_s->next;
2440 while (prev && prev->next != wpa_s)
2444 prev->next = wpa_s->next;
2447 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2449 wpa_supplicant_deinit_iface(wpa_s);
2457 * wpa_supplicant_get_iface - Get a new network interface
2458 * @global: Pointer to global data from wpa_supplicant_init()
2459 * @ifname: Interface name
2460 * Returns: Pointer to the interface or %NULL if not found
2462 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2465 struct wpa_supplicant *wpa_s;
2467 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2468 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2476 * wpa_supplicant_init - Initialize %wpa_supplicant
2477 * @params: Parameters for %wpa_supplicant
2478 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2480 * This function is used to initialize %wpa_supplicant. After successful
2481 * initialization, the returned data pointer can be used to add and remove
2482 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2484 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2486 struct wpa_global *global;
2492 wpa_debug_open_file(params->wpa_debug_file_path);
2493 if (params->wpa_debug_syslog)
2494 wpa_debug_open_syslog();
2496 ret = eap_peer_register_methods();
2498 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2500 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2501 "the same EAP type.");
2505 global = os_zalloc(sizeof(*global));
2508 global->params.daemonize = params->daemonize;
2509 global->params.wait_for_interface = params->wait_for_interface;
2510 global->params.wait_for_monitor = params->wait_for_monitor;
2511 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2512 if (params->pid_file)
2513 global->params.pid_file = os_strdup(params->pid_file);
2514 if (params->ctrl_interface)
2515 global->params.ctrl_interface =
2516 os_strdup(params->ctrl_interface);
2517 wpa_debug_level = global->params.wpa_debug_level =
2518 params->wpa_debug_level;
2519 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2520 params->wpa_debug_show_keys;
2521 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2522 params->wpa_debug_timestamp;
2524 if (eloop_init(global)) {
2525 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2526 wpa_supplicant_deinit(global);
2530 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2531 if (global->ctrl_iface == NULL) {
2532 wpa_supplicant_deinit(global);
2536 if (global->params.dbus_ctrl_interface) {
2537 global->dbus_ctrl_iface =
2538 wpa_supplicant_dbus_ctrl_iface_init(global);
2539 if (global->dbus_ctrl_iface == NULL) {
2540 wpa_supplicant_deinit(global);
2545 if (global->params.wait_for_interface && global->params.daemonize &&
2546 wpa_supplicant_daemon(global->params.pid_file)) {
2547 wpa_supplicant_deinit(global);
2556 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2557 * @global: Pointer to global data from wpa_supplicant_init()
2558 * Returns: 0 after successful event loop run, -1 on failure
2560 * This function starts the main event loop and continues running as long as
2561 * there are any remaining events. In most cases, this function is running as
2562 * long as the %wpa_supplicant process in still in use.
2564 int wpa_supplicant_run(struct wpa_global *global)
2566 struct wpa_supplicant *wpa_s;
2568 if (!global->params.wait_for_interface && global->params.daemonize &&
2569 wpa_supplicant_daemon(global->params.pid_file))
2572 if (global->params.wait_for_monitor) {
2573 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2574 if (wpa_s->ctrl_iface)
2575 wpa_supplicant_ctrl_iface_wait(
2579 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2580 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2589 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2590 * @global: Pointer to global data from wpa_supplicant_init()
2592 * This function is called to deinitialize %wpa_supplicant and to free all
2593 * allocated resources. Remaining network interfaces will also be removed.
2595 void wpa_supplicant_deinit(struct wpa_global *global)
2600 while (global->ifaces)
2601 wpa_supplicant_remove_iface(global, global->ifaces);
2603 if (global->ctrl_iface)
2604 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2605 if (global->dbus_ctrl_iface)
2606 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2608 eap_peer_unregister_methods();
2612 if (global->params.pid_file) {
2613 os_daemonize_terminate(global->params.pid_file);
2614 os_free(global->params.pid_file);
2616 os_free(global->params.ctrl_interface);
2619 wpa_debug_close_syslog();
2620 wpa_debug_close_file();