2 * WPA Supplicant - Glue code to setup EAPOL and RSN modules
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.
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
22 #include "l2_packet/l2_packet.h"
23 #include "common/wpa_common.h"
24 #include "wpa_supplicant_i.h"
26 #include "rsn_supp/pmksa_cache.h"
29 #include "common/ieee802_11_defs.h"
30 #include "common/wpa_ctrl.h"
31 #include "wpas_glue.h"
32 #include "wps_supplicant.h"
37 #ifndef CONFIG_NO_CONFIG_BLOBS
38 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
39 static void wpa_supplicant_set_config_blob(void *ctx,
40 struct wpa_config_blob *blob)
42 struct wpa_supplicant *wpa_s = ctx;
43 wpa_config_set_blob(wpa_s->conf, blob);
44 if (wpa_s->conf->update_config) {
45 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
47 wpa_printf(MSG_DEBUG, "Failed to update config after "
54 static const struct wpa_config_blob *
55 wpa_supplicant_get_config_blob(void *ctx, const char *name)
57 struct wpa_supplicant *wpa_s = ctx;
58 return wpa_config_get_blob(wpa_s->conf, name);
60 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
61 #endif /* CONFIG_NO_CONFIG_BLOBS */
64 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
65 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
66 const void *data, u16 data_len,
67 size_t *msg_len, void **data_pos)
69 struct ieee802_1x_hdr *hdr;
71 *msg_len = sizeof(*hdr) + data_len;
72 hdr = os_malloc(*msg_len);
76 hdr->version = wpa_s->conf->eapol_version;
78 hdr->length = host_to_be16(data_len);
81 os_memcpy(hdr + 1, data, data_len);
83 os_memset(hdr + 1, 0, data_len);
93 * wpa_ether_send - Send Ethernet frame
94 * @wpa_s: Pointer to wpa_supplicant data
95 * @dest: Destination MAC address
96 * @proto: Ethertype in host byte order
97 * @buf: Frame payload starting from IEEE 802.1X header
98 * @len: Frame payload length
99 * Returns: >=0 on success, <0 on failure
101 static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
102 u16 proto, const u8 *buf, size_t len)
105 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
108 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
110 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
113 #ifdef IEEE8021X_EAPOL
116 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
117 * @ctx: Pointer to wpa_supplicant data (wpa_s)
118 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
119 * @buf: EAPOL payload (after IEEE 802.1X header)
120 * @len: EAPOL payload length
121 * Returns: >=0 on success, <0 on failure
123 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
124 * to the current Authenticator.
126 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
129 struct wpa_supplicant *wpa_s = ctx;
130 u8 *msg, *dst, bssid[ETH_ALEN];
134 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
137 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
138 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
139 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
140 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
142 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
143 "mode (type=%d len=%lu)", type,
144 (unsigned long) len);
148 if (pmksa_cache_get_current(wpa_s->wpa) &&
149 type == IEEE802_1X_TYPE_EAPOL_START) {
150 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
151 * since they will trigger full EAPOL authentication. */
152 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
157 if (is_zero_ether_addr(wpa_s->bssid)) {
158 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
160 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
161 !is_zero_ether_addr(bssid)) {
163 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
164 " from the driver as the EAPOL destination",
167 dst = wpa_s->last_eapol_src;
168 wpa_printf(MSG_DEBUG, "Using the source address of the"
169 " last received EAPOL frame " MACSTR " as "
170 "the EAPOL destination",
174 /* BSSID was already set (from (Re)Assoc event, so use it as
175 * the EAPOL destination. */
179 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
183 wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
184 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
185 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
192 * wpa_eapol_set_wep_key - set WEP key for the driver
193 * @ctx: Pointer to wpa_supplicant data (wpa_s)
194 * @unicast: 1 = individual unicast key, 0 = broadcast key
195 * @keyidx: WEP key index (0..3)
196 * @key: Pointer to key data
197 * @keylen: Key length in bytes
198 * Returns: 0 on success or < 0 on error.
200 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
201 const u8 *key, size_t keylen)
203 struct wpa_supplicant *wpa_s = ctx;
204 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
205 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
208 wpa_s->pairwise_cipher = cipher;
210 wpa_s->group_cipher = cipher;
212 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
213 unicast ? wpa_s->bssid : NULL,
214 keyidx, unicast, NULL, 0, key, keylen);
218 static void wpa_supplicant_aborted_cached(void *ctx)
220 struct wpa_supplicant *wpa_s = ctx;
221 wpa_sm_aborted_cached(wpa_s->wpa);
225 static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
228 struct wpa_supplicant *wpa_s = ctx;
232 wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
233 success ? "" : "un");
235 if (wpas_wps_eapol_cb(wpa_s) > 0)
240 * Make sure we do not get stuck here waiting for long EAPOL
241 * timeout if the AP does not disconnect in case of
242 * authentication failure.
244 wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
247 if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
250 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
253 wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
257 if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
258 #ifdef CONFIG_IEEE80211R
260 wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
261 "driver-based 4-way hs and FT");
262 res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
264 os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
265 os_memset(buf, 0, sizeof(buf));
267 #else /* CONFIG_IEEE80211R */
269 #endif /* CONFIG_IEEE80211R */
271 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
274 * EAP-LEAP is an exception from other EAP methods: it
275 * uses only 16-byte PMK.
277 res = eapol_sm_get_key(eapol, pmk, 16);
283 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
288 wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
289 "handshake", pmk, pmk_len);
291 if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
293 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
296 wpa_supplicant_cancel_scan(wpa_s);
297 wpa_supplicant_cancel_auth_timeout(wpa_s);
298 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
303 static void wpa_supplicant_notify_eapol_done(void *ctx)
305 struct wpa_supplicant *wpa_s = ctx;
306 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
307 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
308 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
310 wpa_supplicant_cancel_auth_timeout(wpa_s);
311 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
315 #endif /* IEEE8021X_EAPOL */
318 #ifndef CONFIG_NO_WPA
320 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
323 struct wpa_bss *curr = NULL, *bss;
324 struct wpa_ssid *ssid = wpa_s->current_ssid;
327 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
328 if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
331 ((bss->ssid_len == ssid->ssid_len &&
332 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
333 ssid->ssid_len == 0)) {
340 ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
341 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
344 ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
345 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
355 static int wpa_supplicant_get_beacon_ie(void *ctx)
357 struct wpa_supplicant *wpa_s = ctx;
358 if (wpa_get_beacon_ie(wpa_s) == 0) {
362 /* No WPA/RSN IE found in the cached scan results. Try to get updated
363 * scan results from the driver. */
364 if (wpa_supplicant_update_scan_results(wpa_s) < 0)
367 return wpa_get_beacon_ie(wpa_s);
371 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
372 const void *data, u16 data_len,
373 size_t *msg_len, void **data_pos)
375 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
379 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
380 const u8 *buf, size_t len)
382 return wpa_ether_send(wpa_s, dest, proto, buf, len);
386 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
388 wpa_supplicant_cancel_auth_timeout(wpa_s);
392 static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
394 wpa_supplicant_set_state(wpa_s, state);
399 * wpa_supplicant_get_state - Get the connection state
400 * @wpa_s: Pointer to wpa_supplicant data
401 * Returns: The current connection state (WPA_*)
403 static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
405 return wpa_s->wpa_state;
409 static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
411 return wpa_supplicant_get_state(wpa_s);
415 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
417 wpa_supplicant_disassociate(wpa_s, reason_code);
418 /* Schedule a scan to make sure we continue looking for networks */
419 wpa_supplicant_req_scan(wpa_s, 5, 0);
423 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
425 wpa_supplicant_deauthenticate(wpa_s, reason_code);
426 /* Schedule a scan to make sure we continue looking for networks */
427 wpa_supplicant_req_scan(wpa_s, 5, 0);
431 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
433 return wpa_supplicant_get_ssid(wpa_s);
437 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
439 struct wpa_supplicant *wpa_s = ctx;
440 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
441 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
444 return wpa_drv_get_bssid(wpa_s, bssid);
448 static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
449 const u8 *addr, int key_idx, int set_tx,
450 const u8 *seq, size_t seq_len,
451 const u8 *key, size_t key_len)
453 struct wpa_supplicant *wpa_s = _wpa_s;
454 if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
455 /* Clear the MIC error counter when setting a new PTK. */
456 wpa_s->mic_errors_seen = 0;
458 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
463 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
467 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
472 static int wpa_supplicant_add_pmkid(void *wpa_s,
473 const u8 *bssid, const u8 *pmkid)
475 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
479 static int wpa_supplicant_remove_pmkid(void *wpa_s,
480 const u8 *bssid, const u8 *pmkid)
482 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
486 #ifdef CONFIG_IEEE80211R
487 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
488 const u8 *ies, size_t ies_len)
490 struct wpa_supplicant *wpa_s = ctx;
491 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
492 return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
493 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
494 return sme_update_ft_ies(wpa_s, md, ies, ies_len);
495 return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
499 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
501 const u8 *ies, size_t ies_len)
503 struct wpa_supplicant *wpa_s = ctx;
504 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
505 return ieee80211_sta_send_ft_action(wpa_s, action, target_ap,
507 return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
511 static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
513 struct wpa_supplicant *wpa_s = ctx;
514 struct wpa_driver_auth_params params;
517 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
520 bss = wpa_bss_get_bssid(wpa_s, target_ap);
524 os_memset(¶ms, 0, sizeof(params));
525 params.bssid = target_ap;
526 params.freq = bss->freq;
527 params.ssid = bss->ssid;
528 params.ssid_len = bss->ssid_len;
529 params.auth_alg = WPA_AUTH_ALG_FT;
530 params.local_state_change = 1;
531 return wpa_drv_authenticate(wpa_s, ¶ms);
533 #endif /* CONFIG_IEEE80211R */
535 #endif /* CONFIG_NO_WPA */
538 #ifdef IEEE8021X_EAPOL
539 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
540 static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
543 struct wpa_supplicant *wpa_s = ctx;
544 struct wpa_ssid *ssid = wpa_s->current_ssid;
552 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
553 buf = os_malloc(buflen);
556 len = os_snprintf(buf, buflen,
557 WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
558 field, ssid->id, txt);
559 if (len < 0 || (size_t) len >= buflen) {
563 if (ssid->ssid && buflen > len + ssid->ssid_len) {
564 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
565 len += ssid->ssid_len;
568 buf[buflen - 1] = '\0';
569 wpa_msg(wpa_s, MSG_INFO, "%s", buf);
572 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
573 #define wpa_supplicant_eap_param_needed NULL
574 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
577 static void wpa_supplicant_port_cb(void *ctx, int authorized)
579 struct wpa_supplicant *wpa_s = ctx;
581 if (wpa_s->ap_iface) {
582 wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
584 authorized ? "Authorized" : "Unauthorized");
587 #endif /* CONFIG_AP */
588 wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
589 authorized ? "Authorized" : "Unauthorized");
590 wpa_drv_set_supp_port(wpa_s, authorized);
592 #endif /* IEEE8021X_EAPOL */
595 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
597 #ifdef IEEE8021X_EAPOL
598 struct eapol_ctx *ctx;
599 ctx = os_zalloc(sizeof(*ctx));
601 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
606 ctx->msg_ctx = wpa_s;
607 ctx->eapol_send_ctx = wpa_s;
609 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
610 ctx->eapol_send = wpa_supplicant_eapol_send;
611 ctx->set_wep_key = wpa_eapol_set_wep_key;
612 ctx->set_config_blob = wpa_supplicant_set_config_blob;
613 ctx->get_config_blob = wpa_supplicant_get_config_blob;
614 ctx->aborted_cached = wpa_supplicant_aborted_cached;
615 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
616 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
617 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
618 ctx->wps = wpa_s->wps;
619 ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
620 ctx->port_cb = wpa_supplicant_port_cb;
621 ctx->cb = wpa_supplicant_eapol_cb;
623 wpa_s->eapol = eapol_sm_init(ctx);
624 if (wpa_s->eapol == NULL) {
626 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
630 #endif /* IEEE8021X_EAPOL */
636 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
638 #ifndef CONFIG_NO_WPA
639 struct wpa_sm_ctx *ctx;
640 ctx = os_zalloc(sizeof(*ctx));
642 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
647 ctx->msg_ctx = wpa_s;
648 ctx->set_state = _wpa_supplicant_set_state;
649 ctx->get_state = _wpa_supplicant_get_state;
650 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
651 ctx->disassociate = _wpa_supplicant_disassociate;
652 ctx->set_key = wpa_supplicant_set_key;
653 ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
654 ctx->get_bssid = wpa_supplicant_get_bssid;
655 ctx->ether_send = _wpa_ether_send;
656 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
657 ctx->alloc_eapol = _wpa_alloc_eapol;
658 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
659 ctx->add_pmkid = wpa_supplicant_add_pmkid;
660 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
661 #ifndef CONFIG_NO_CONFIG_BLOBS
662 ctx->set_config_blob = wpa_supplicant_set_config_blob;
663 ctx->get_config_blob = wpa_supplicant_get_config_blob;
664 #endif /* CONFIG_NO_CONFIG_BLOBS */
665 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
666 #ifdef CONFIG_IEEE80211R
667 ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
668 ctx->send_ft_action = wpa_supplicant_send_ft_action;
669 ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
670 #endif /* CONFIG_IEEE80211R */
672 wpa_s->wpa = wpa_sm_init(ctx);
673 if (wpa_s->wpa == NULL) {
674 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
678 #endif /* CONFIG_NO_WPA */
684 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
685 struct wpa_ssid *ssid)
687 struct rsn_supp_config conf;
689 os_memset(&conf, 0, sizeof(conf));
690 conf.network_ctx = ssid;
691 conf.peerkey_enabled = ssid->peerkey;
692 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
693 #ifdef IEEE8021X_EAPOL
694 conf.eap_workaround = ssid->eap_workaround;
695 conf.eap_conf_ctx = &ssid->eap;
696 #endif /* IEEE8021X_EAPOL */
697 conf.ssid = ssid->ssid;
698 conf.ssid_len = ssid->ssid_len;
699 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
701 wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);