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"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_common.h"
24 #include "wpa_supplicant_i.h"
25 #include "pmksa_cache.h"
28 #include "ieee802_11_defs.h"
30 #include "wpas_glue.h"
31 #include "wps_supplicant.h"
34 #ifndef CONFIG_NO_CONFIG_BLOBS
35 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
36 static void wpa_supplicant_set_config_blob(void *ctx,
37 struct wpa_config_blob *blob)
39 struct wpa_supplicant *wpa_s = ctx;
40 wpa_config_set_blob(wpa_s->conf, blob);
41 if (wpa_s->conf->update_config) {
42 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
44 wpa_printf(MSG_DEBUG, "Failed to update config after "
51 static const struct wpa_config_blob *
52 wpa_supplicant_get_config_blob(void *ctx, const char *name)
54 struct wpa_supplicant *wpa_s = ctx;
55 return wpa_config_get_blob(wpa_s->conf, name);
57 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
58 #endif /* CONFIG_NO_CONFIG_BLOBS */
61 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
62 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
63 const void *data, u16 data_len,
64 size_t *msg_len, void **data_pos)
66 struct ieee802_1x_hdr *hdr;
68 *msg_len = sizeof(*hdr) + data_len;
69 hdr = os_malloc(*msg_len);
73 hdr->version = wpa_s->conf->eapol_version;
75 hdr->length = host_to_be16(data_len);
78 os_memcpy(hdr + 1, data, data_len);
80 os_memset(hdr + 1, 0, data_len);
90 * wpa_ether_send - Send Ethernet frame
91 * @wpa_s: Pointer to wpa_supplicant data
92 * @dest: Destination MAC address
93 * @proto: Ethertype in host byte order
94 * @buf: Frame payload starting from IEEE 802.1X header
95 * @len: Frame payload length
96 * Returns: >=0 on success, <0 on failure
98 static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
99 u16 proto, const u8 *buf, size_t len)
102 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
105 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
107 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
110 #ifdef IEEE8021X_EAPOL
113 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
114 * @ctx: Pointer to wpa_supplicant data (wpa_s)
115 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
116 * @buf: EAPOL payload (after IEEE 802.1X header)
117 * @len: EAPOL payload length
118 * Returns: >=0 on success, <0 on failure
120 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
121 * to the current Authenticator.
123 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
126 struct wpa_supplicant *wpa_s = ctx;
127 u8 *msg, *dst, bssid[ETH_ALEN];
131 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
134 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
135 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
136 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
137 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
139 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
140 "mode (type=%d len=%lu)", type,
141 (unsigned long) len);
145 if (pmksa_cache_get_current(wpa_s->wpa) &&
146 type == IEEE802_1X_TYPE_EAPOL_START) {
147 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
148 * since they will trigger full EAPOL authentication. */
149 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
154 if (is_zero_ether_addr(wpa_s->bssid)) {
155 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
157 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
158 !is_zero_ether_addr(bssid)) {
160 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
161 " from the driver as the EAPOL destination",
164 dst = wpa_s->last_eapol_src;
165 wpa_printf(MSG_DEBUG, "Using the source address of the"
166 " last received EAPOL frame " MACSTR " as "
167 "the EAPOL destination",
171 /* BSSID was already set (from (Re)Assoc event, so use it as
172 * the EAPOL destination. */
176 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
180 wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
181 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
182 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
189 * wpa_eapol_set_wep_key - set WEP key for the driver
190 * @ctx: Pointer to wpa_supplicant data (wpa_s)
191 * @unicast: 1 = individual unicast key, 0 = broadcast key
192 * @keyidx: WEP key index (0..3)
193 * @key: Pointer to key data
194 * @keylen: Key length in bytes
195 * Returns: 0 on success or < 0 on error.
197 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
198 const u8 *key, size_t keylen)
200 struct wpa_supplicant *wpa_s = ctx;
201 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
202 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
205 wpa_s->pairwise_cipher = cipher;
207 wpa_s->group_cipher = cipher;
209 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
210 unicast ? wpa_s->bssid :
211 (u8 *) "\xff\xff\xff\xff\xff\xff",
212 keyidx, unicast, (u8 *) "", 0, key, keylen);
216 static void wpa_supplicant_aborted_cached(void *ctx)
218 struct wpa_supplicant *wpa_s = ctx;
219 wpa_sm_aborted_cached(wpa_s->wpa);
223 static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
226 struct wpa_supplicant *wpa_s = ctx;
230 wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
231 success ? "" : "un");
233 if (wpas_wps_eapol_cb(wpa_s) > 0)
238 * Make sure we do not get stuck here waiting for long EAPOL
239 * timeout if the AP does not disconnect in case of
240 * authentication failure.
242 wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
245 if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
248 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
251 wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
255 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
258 * EAP-LEAP is an exception from other EAP methods: it
259 * uses only 16-byte PMK.
261 res = eapol_sm_get_key(eapol, pmk, 16);
266 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
271 if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
273 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
276 wpa_supplicant_cancel_scan(wpa_s);
277 wpa_supplicant_cancel_auth_timeout(wpa_s);
278 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
283 static void wpa_supplicant_notify_eapol_done(void *ctx)
285 struct wpa_supplicant *wpa_s = ctx;
286 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
287 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
288 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
290 wpa_supplicant_cancel_auth_timeout(wpa_s);
291 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
295 #endif /* IEEE8021X_EAPOL */
298 #ifndef CONFIG_NO_WPA
300 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
304 struct wpa_scan_res *curr = NULL;
305 struct wpa_ssid *ssid = wpa_s->current_ssid;
308 if (wpa_s->scan_res == NULL)
311 for (i = 0; i < wpa_s->scan_res->num; i++) {
312 struct wpa_scan_res *r = wpa_s->scan_res->res[i];
313 if (os_memcmp(r->bssid, wpa_s->bssid, ETH_ALEN) != 0)
315 ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
317 ((ie && ie[1] == ssid->ssid_len &&
318 os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) == 0) ||
319 ssid->ssid_len == 0)) {
326 ie = wpa_scan_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
327 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
330 ie = wpa_scan_get_ie(curr, WLAN_EID_RSN);
331 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
341 static int wpa_supplicant_get_beacon_ie(void *ctx)
343 struct wpa_supplicant *wpa_s = ctx;
344 if (wpa_get_beacon_ie(wpa_s) == 0) {
348 /* No WPA/RSN IE found in the cached scan results. Try to get updated
349 * scan results from the driver. */
350 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
354 return wpa_get_beacon_ie(wpa_s);
358 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
359 const void *data, u16 data_len,
360 size_t *msg_len, void **data_pos)
362 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
366 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
367 const u8 *buf, size_t len)
369 return wpa_ether_send(wpa_s, dest, proto, buf, len);
373 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
375 wpa_supplicant_cancel_auth_timeout(wpa_s);
379 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
381 wpa_supplicant_set_state(wpa_s, state);
386 * wpa_supplicant_get_state - Get the connection state
387 * @wpa_s: Pointer to wpa_supplicant data
388 * Returns: The current connection state (WPA_*)
390 static wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
392 return wpa_s->wpa_state;
396 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
398 return wpa_supplicant_get_state(wpa_s);
402 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
404 wpa_supplicant_disassociate(wpa_s, reason_code);
405 /* Schedule a scan to make sure we continue looking for networks */
406 wpa_supplicant_req_scan(wpa_s, 0, 0);
410 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
412 wpa_supplicant_deauthenticate(wpa_s, reason_code);
413 /* Schedule a scan to make sure we continue looking for networks */
414 wpa_supplicant_req_scan(wpa_s, 0, 0);
418 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
420 return wpa_supplicant_get_ssid(wpa_s);
424 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
426 struct wpa_supplicant *wpa_s = ctx;
427 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
428 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
431 return wpa_drv_get_bssid(wpa_s, bssid);
435 static int wpa_supplicant_set_key(void *_wpa_s, wpa_alg alg,
436 const u8 *addr, int key_idx, int set_tx,
437 const u8 *seq, size_t seq_len,
438 const u8 *key, size_t key_len)
440 struct wpa_supplicant *wpa_s = _wpa_s;
441 if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
442 /* Clear the MIC error counter when setting a new PTK. */
443 wpa_s->mic_errors_seen = 0;
445 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
450 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
454 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
459 static int wpa_supplicant_add_pmkid(void *wpa_s,
460 const u8 *bssid, const u8 *pmkid)
462 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
466 static int wpa_supplicant_remove_pmkid(void *wpa_s,
467 const u8 *bssid, const u8 *pmkid)
469 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
473 #ifdef CONFIG_IEEE80211R
474 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
475 const u8 *ies, size_t ies_len)
477 struct wpa_supplicant *wpa_s = ctx;
478 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
479 return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
480 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
481 return sme_update_ft_ies(wpa_s, md, ies, ies_len);
482 return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
486 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
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_send_ft_action(wpa_s, action, target_ap,
494 return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
496 #endif /* CONFIG_IEEE80211R */
498 #endif /* CONFIG_NO_WPA */
501 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
502 static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
505 struct wpa_supplicant *wpa_s = ctx;
506 struct wpa_ssid *ssid = wpa_s->current_ssid;
514 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
515 buf = os_malloc(buflen);
518 len = os_snprintf(buf, buflen,
519 WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
520 field, ssid->id, txt);
521 if (len < 0 || (size_t) len >= buflen) {
525 if (ssid->ssid && buflen > len + ssid->ssid_len) {
526 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
527 len += ssid->ssid_len;
530 buf[buflen - 1] = '\0';
531 wpa_msg(wpa_s, MSG_INFO, "%s", buf);
534 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
535 #define wpa_supplicant_eap_param_needed NULL
536 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
539 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
541 #ifdef IEEE8021X_EAPOL
542 struct eapol_ctx *ctx;
543 ctx = os_zalloc(sizeof(*ctx));
545 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
550 ctx->msg_ctx = wpa_s;
551 ctx->eapol_send_ctx = wpa_s;
553 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
554 ctx->eapol_send = wpa_supplicant_eapol_send;
555 ctx->set_wep_key = wpa_eapol_set_wep_key;
556 ctx->set_config_blob = wpa_supplicant_set_config_blob;
557 ctx->get_config_blob = wpa_supplicant_get_config_blob;
558 ctx->aborted_cached = wpa_supplicant_aborted_cached;
559 #ifdef EAP_TLS_OPENSSL
560 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
561 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
562 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
563 #endif /* EAP_TLS_OPENSSL */
564 ctx->wps = wpa_s->wps;
565 ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
566 ctx->cb = wpa_supplicant_eapol_cb;
568 wpa_s->eapol = eapol_sm_init(ctx);
569 if (wpa_s->eapol == NULL) {
571 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
575 #endif /* IEEE8021X_EAPOL */
581 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
583 #ifndef CONFIG_NO_WPA
584 struct wpa_sm_ctx *ctx;
585 ctx = os_zalloc(sizeof(*ctx));
587 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
592 ctx->msg_ctx = wpa_s;
593 ctx->set_state = _wpa_supplicant_set_state;
594 ctx->get_state = _wpa_supplicant_get_state;
595 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
596 ctx->disassociate = _wpa_supplicant_disassociate;
597 ctx->set_key = wpa_supplicant_set_key;
598 ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
599 ctx->get_bssid = wpa_supplicant_get_bssid;
600 ctx->ether_send = _wpa_ether_send;
601 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
602 ctx->alloc_eapol = _wpa_alloc_eapol;
603 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
604 ctx->add_pmkid = wpa_supplicant_add_pmkid;
605 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
606 #ifndef CONFIG_NO_CONFIG_BLOBS
607 ctx->set_config_blob = wpa_supplicant_set_config_blob;
608 ctx->get_config_blob = wpa_supplicant_get_config_blob;
609 #endif /* CONFIG_NO_CONFIG_BLOBS */
610 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
611 #ifdef CONFIG_IEEE80211R
612 ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
613 ctx->send_ft_action = wpa_supplicant_send_ft_action;
614 #endif /* CONFIG_IEEE80211R */
616 wpa_s->wpa = wpa_sm_init(ctx);
617 if (wpa_s->wpa == NULL) {
618 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
622 #endif /* CONFIG_NO_WPA */
628 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
629 struct wpa_ssid *ssid)
631 struct rsn_supp_config conf;
633 os_memset(&conf, 0, sizeof(conf));
634 conf.network_ctx = ssid;
635 conf.peerkey_enabled = ssid->peerkey;
636 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
637 #ifdef IEEE8021X_EAPOL
638 conf.eap_workaround = ssid->eap_workaround;
639 conf.eap_conf_ctx = &ssid->eap;
640 #endif /* IEEE8021X_EAPOL */
641 conf.ssid = ssid->ssid;
642 conf.ssid_len = ssid->ssid_len;
643 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
645 wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);