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"
27 #include "ieee802_11_defs.h"
29 #include "wpas_glue.h"
32 #ifndef CONFIG_NO_CONFIG_BLOBS
33 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
34 static void wpa_supplicant_set_config_blob(void *ctx,
35 struct wpa_config_blob *blob)
37 struct wpa_supplicant *wpa_s = ctx;
38 wpa_config_set_blob(wpa_s->conf, blob);
39 if (wpa_s->conf->update_config) {
40 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
42 wpa_printf(MSG_DEBUG, "Failed to update config after "
49 static const struct wpa_config_blob *
50 wpa_supplicant_get_config_blob(void *ctx, const char *name)
52 struct wpa_supplicant *wpa_s = ctx;
53 return wpa_config_get_blob(wpa_s->conf, name);
55 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
56 #endif /* CONFIG_NO_CONFIG_BLOBS */
59 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
60 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
61 const void *data, u16 data_len,
62 size_t *msg_len, void **data_pos)
64 struct ieee802_1x_hdr *hdr;
66 *msg_len = sizeof(*hdr) + data_len;
67 hdr = os_malloc(*msg_len);
71 hdr->version = wpa_s->conf->eapol_version;
73 hdr->length = host_to_be16(data_len);
76 os_memcpy(hdr + 1, data, data_len);
78 os_memset(hdr + 1, 0, data_len);
88 * wpa_ether_send - Send Ethernet frame
89 * @wpa_s: Pointer to wpa_supplicant data
90 * @dest: Destination MAC address
91 * @proto: Ethertype in host byte order
92 * @buf: Frame payload starting from IEEE 802.1X header
93 * @len: Frame payload length
94 * Returns: >=0 on success, <0 on failure
96 static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
97 u16 proto, const u8 *buf, size_t len)
100 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
103 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
105 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
108 #ifdef IEEE8021X_EAPOL
111 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
112 * @ctx: Pointer to wpa_supplicant data (wpa_s)
113 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
114 * @buf: EAPOL payload (after IEEE 802.1X header)
115 * @len: EAPOL payload length
116 * Returns: >=0 on success, <0 on failure
118 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
119 * to the current Authenticator.
121 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
124 struct wpa_supplicant *wpa_s = ctx;
125 u8 *msg, *dst, bssid[ETH_ALEN];
129 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
132 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
133 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
134 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
135 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
137 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
138 "mode (type=%d len=%lu)", type,
139 (unsigned long) len);
143 if (pmksa_cache_get_current(wpa_s->wpa) &&
144 type == IEEE802_1X_TYPE_EAPOL_START) {
145 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
146 * since they will trigger full EAPOL authentication. */
147 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
152 if (is_zero_ether_addr(wpa_s->bssid)) {
153 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
155 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
156 !is_zero_ether_addr(bssid)) {
158 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
159 " from the driver as the EAPOL destination",
162 dst = wpa_s->last_eapol_src;
163 wpa_printf(MSG_DEBUG, "Using the source address of the"
164 " last received EAPOL frame " MACSTR " as "
165 "the EAPOL destination",
169 /* BSSID was already set (from (Re)Assoc event, so use it as
170 * the EAPOL destination. */
174 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
178 wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
179 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
180 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
187 * wpa_eapol_set_wep_key - set WEP key for the driver
188 * @ctx: Pointer to wpa_supplicant data (wpa_s)
189 * @unicast: 1 = individual unicast key, 0 = broadcast key
190 * @keyidx: WEP key index (0..3)
191 * @key: Pointer to key data
192 * @keylen: Key length in bytes
193 * Returns: 0 on success or < 0 on error.
195 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
196 const u8 *key, size_t keylen)
198 struct wpa_supplicant *wpa_s = ctx;
199 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
200 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
203 wpa_s->pairwise_cipher = cipher;
205 wpa_s->group_cipher = cipher;
207 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
208 unicast ? wpa_s->bssid :
209 (u8 *) "\xff\xff\xff\xff\xff\xff",
210 keyidx, unicast, (u8 *) "", 0, key, keylen);
214 static void wpa_supplicant_aborted_cached(void *ctx)
216 struct wpa_supplicant *wpa_s = ctx;
217 wpa_sm_aborted_cached(wpa_s->wpa);
221 static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
224 struct wpa_supplicant *wpa_s = ctx;
228 wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
229 success ? "" : "un");
231 if (!success || !wpa_s->driver_4way_handshake)
234 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
237 wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
241 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
244 * EAP-LEAP is an exception from other EAP methods: it
245 * uses only 16-byte PMK.
247 res = eapol_sm_get_key(eapol, pmk, 16);
252 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
257 if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
259 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
262 wpa_supplicant_cancel_scan(wpa_s);
263 wpa_supplicant_cancel_auth_timeout(wpa_s);
264 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
269 static void wpa_supplicant_notify_eapol_done(void *ctx)
271 struct wpa_supplicant *wpa_s = ctx;
272 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
273 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
274 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
276 wpa_supplicant_cancel_auth_timeout(wpa_s);
277 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
281 #endif /* IEEE8021X_EAPOL */
284 #ifndef CONFIG_NO_WPA
286 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
290 struct wpa_scan_res *curr = NULL;
291 struct wpa_ssid *ssid = wpa_s->current_ssid;
294 if (wpa_s->scan_res == NULL)
297 for (i = 0; i < wpa_s->scan_res->num; i++) {
298 struct wpa_scan_res *r = wpa_s->scan_res->res[i];
299 if (os_memcmp(r->bssid, wpa_s->bssid, ETH_ALEN) != 0)
301 ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
303 ((ie && ie[1] == ssid->ssid_len &&
304 os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) == 0) ||
305 ssid->ssid_len == 0)) {
312 ie = wpa_scan_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
313 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
316 ie = wpa_scan_get_ie(curr, WLAN_EID_RSN);
317 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
327 static int wpa_supplicant_get_beacon_ie(void *ctx)
329 struct wpa_supplicant *wpa_s = ctx;
330 if (wpa_get_beacon_ie(wpa_s) == 0) {
334 /* No WPA/RSN IE found in the cached scan results. Try to get updated
335 * scan results from the driver. */
336 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
340 return wpa_get_beacon_ie(wpa_s);
344 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
345 const void *data, u16 data_len,
346 size_t *msg_len, void **data_pos)
348 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
352 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
353 const u8 *buf, size_t len)
355 return wpa_ether_send(wpa_s, dest, proto, buf, len);
359 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
361 wpa_supplicant_cancel_auth_timeout(wpa_s);
365 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
367 wpa_supplicant_set_state(wpa_s, state);
372 * wpa_supplicant_get_state - Get the connection state
373 * @wpa_s: Pointer to wpa_supplicant data
374 * Returns: The current connection state (WPA_*)
376 static wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
378 return wpa_s->wpa_state;
382 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
384 return wpa_supplicant_get_state(wpa_s);
388 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
390 wpa_supplicant_disassociate(wpa_s, reason_code);
391 /* Schedule a scan to make sure we continue looking for networks */
392 wpa_supplicant_req_scan(wpa_s, 0, 0);
396 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
398 wpa_supplicant_deauthenticate(wpa_s, reason_code);
399 /* Schedule a scan to make sure we continue looking for networks */
400 wpa_supplicant_req_scan(wpa_s, 0, 0);
404 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
406 return wpa_supplicant_get_ssid(wpa_s);
410 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
412 struct wpa_supplicant *wpa_s = ctx;
413 if (wpa_s->use_client_mlme) {
414 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
417 return wpa_drv_get_bssid(wpa_s, bssid);
421 static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,
422 const u8 *addr, int key_idx, int set_tx,
423 const u8 *seq, size_t seq_len,
424 const u8 *key, size_t key_len)
426 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
431 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
435 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
440 static int wpa_supplicant_add_pmkid(void *wpa_s,
441 const u8 *bssid, const u8 *pmkid)
443 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
447 static int wpa_supplicant_remove_pmkid(void *wpa_s,
448 const u8 *bssid, const u8 *pmkid)
450 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
454 #ifdef CONFIG_IEEE80211R
455 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
456 const u8 *ies, size_t ies_len)
458 struct wpa_supplicant *wpa_s = ctx;
459 if (wpa_s->use_client_mlme)
460 return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
461 return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
465 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
467 const u8 *ies, size_t ies_len)
469 struct wpa_supplicant *wpa_s = ctx;
470 if (wpa_s->use_client_mlme)
471 return ieee80211_sta_send_ft_action(wpa_s, action, target_ap,
473 return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
475 #endif /* CONFIG_IEEE80211R */
477 #endif /* CONFIG_NO_WPA */
480 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
481 static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
484 struct wpa_supplicant *wpa_s = ctx;
485 struct wpa_ssid *ssid = wpa_s->current_ssid;
493 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
494 buf = os_malloc(buflen);
497 len = os_snprintf(buf, buflen,
498 WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
499 field, ssid->id, txt);
500 if (len < 0 || (size_t) len >= buflen) {
504 if (ssid->ssid && buflen > len + ssid->ssid_len) {
505 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
506 len += ssid->ssid_len;
509 buf[buflen - 1] = '\0';
510 wpa_msg(wpa_s, MSG_INFO, "%s", buf);
513 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
514 #define wpa_supplicant_eap_param_needed NULL
515 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
518 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
520 #ifdef IEEE8021X_EAPOL
521 struct eapol_ctx *ctx;
522 ctx = os_zalloc(sizeof(*ctx));
524 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
529 ctx->msg_ctx = wpa_s;
530 ctx->eapol_send_ctx = wpa_s;
532 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
533 ctx->eapol_send = wpa_supplicant_eapol_send;
534 ctx->set_wep_key = wpa_eapol_set_wep_key;
535 ctx->set_config_blob = wpa_supplicant_set_config_blob;
536 ctx->get_config_blob = wpa_supplicant_get_config_blob;
537 ctx->aborted_cached = wpa_supplicant_aborted_cached;
538 #ifdef EAP_TLS_OPENSSL
539 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
540 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
541 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
542 #endif /* EAP_TLS_OPENSSL */
543 ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
544 ctx->cb = wpa_supplicant_eapol_cb;
546 wpa_s->eapol = eapol_sm_init(ctx);
547 if (wpa_s->eapol == NULL) {
549 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
553 #endif /* IEEE8021X_EAPOL */
559 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
561 #ifndef CONFIG_NO_WPA
562 struct wpa_sm_ctx *ctx;
563 ctx = os_zalloc(sizeof(*ctx));
565 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
570 ctx->set_state = _wpa_supplicant_set_state;
571 ctx->get_state = _wpa_supplicant_get_state;
572 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
573 ctx->disassociate = _wpa_supplicant_disassociate;
574 ctx->set_key = wpa_supplicant_set_key;
575 ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
576 ctx->get_bssid = wpa_supplicant_get_bssid;
577 ctx->ether_send = _wpa_ether_send;
578 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
579 ctx->alloc_eapol = _wpa_alloc_eapol;
580 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
581 ctx->add_pmkid = wpa_supplicant_add_pmkid;
582 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
583 #ifndef CONFIG_NO_CONFIG_BLOBS
584 ctx->set_config_blob = wpa_supplicant_set_config_blob;
585 ctx->get_config_blob = wpa_supplicant_get_config_blob;
586 #endif /* CONFIG_NO_CONFIG_BLOBS */
587 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
588 #ifdef CONFIG_IEEE80211R
589 ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
590 ctx->send_ft_action = wpa_supplicant_send_ft_action;
591 #endif /* CONFIG_IEEE80211R */
593 wpa_s->wpa = wpa_sm_init(ctx);
594 if (wpa_s->wpa == NULL) {
595 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
599 #endif /* CONFIG_NO_WPA */
605 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
606 struct wpa_ssid *ssid)
608 struct rsn_supp_config conf;
610 os_memset(&conf, 0, sizeof(conf));
611 conf.network_ctx = ssid;
612 conf.peerkey_enabled = ssid->peerkey;
613 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
614 #ifdef IEEE8021X_EAPOL
615 conf.eap_workaround = ssid->eap_workaround;
616 conf.eap_conf_ctx = &ssid->eap;
617 #endif /* IEEE8021X_EAPOL */
618 conf.ssid = ssid->ssid;
619 conf.ssid_len = ssid->ssid_len;
621 wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);