2 * hostapd / IEEE 802.1X-2004 Authenticator
3 * Copyright (c) 2002-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 "ieee802_1x.h"
19 #include "accounting.h"
20 #include "radius/radius.h"
21 #include "radius/radius_client.h"
29 #include "pmksa_cache.h"
31 #include "hw_features.h"
32 #include "eap_server/eap.h"
35 static void ieee802_1x_finished(struct hostapd_data *hapd,
36 struct sta_info *sta, int success);
39 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
40 u8 type, const u8 *data, size_t datalen)
43 struct ieee802_1x_hdr *xhdr;
47 len = sizeof(*xhdr) + datalen;
50 wpa_printf(MSG_ERROR, "malloc() failed for "
51 "ieee802_1x_send(len=%lu)",
56 xhdr = (struct ieee802_1x_hdr *) buf;
57 xhdr->version = hapd->conf->eapol_version;
59 xhdr->length = host_to_be16(datalen);
61 if (datalen > 0 && data != NULL)
62 os_memcpy(xhdr + 1, data, datalen);
64 if (wpa_auth_pairwise_set(sta->wpa_sm))
66 if (sta->flags & WLAN_STA_PREAUTH) {
67 rsn_preauth_send(hapd, sta, buf, len);
69 hostapd_send_eapol(hapd, sta->addr, buf, len, encrypt);
76 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
77 struct sta_info *sta, int authorized)
81 if (sta->flags & WLAN_STA_PREAUTH)
85 sta->flags |= WLAN_STA_AUTHORIZED;
86 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
87 WLAN_STA_AUTHORIZED, ~0);
88 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
89 HOSTAPD_LEVEL_DEBUG, "authorizing port");
91 sta->flags &= ~WLAN_STA_AUTHORIZED;
92 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
93 0, ~WLAN_STA_AUTHORIZED);
94 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
95 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
98 if (res && errno != ENOENT) {
99 printf("Could not set station " MACSTR " flags for kernel "
100 "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
104 accounting_sta_start(hapd, sta);
108 static void ieee802_1x_eap_timeout(void *eloop_ctx, void *timeout_ctx)
110 struct sta_info *sta = eloop_ctx;
111 struct eapol_state_machine *sm = sta->eapol_sm;
114 hostapd_logger(sm->hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
115 HOSTAPD_LEVEL_DEBUG, "EAP timeout");
116 sm->eap_if->eapTimeout = TRUE;
121 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
122 struct sta_info *sta,
123 int idx, int broadcast,
124 u8 *key_data, size_t key_len)
127 struct ieee802_1x_hdr *hdr;
128 struct ieee802_1x_eapol_key *key;
129 size_t len, ekey_len;
130 struct eapol_state_machine *sm = sta->eapol_sm;
135 len = sizeof(*key) + key_len;
136 buf = os_zalloc(sizeof(*hdr) + len);
140 hdr = (struct ieee802_1x_hdr *) buf;
141 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
142 key->type = EAPOL_KEY_TYPE_RC4;
143 key->key_length = htons(key_len);
144 wpa_get_ntp_timestamp(key->replay_counter);
146 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
147 wpa_printf(MSG_ERROR, "Could not get random numbers");
152 key->key_index = idx | (broadcast ? 0 : BIT(7));
153 if (hapd->conf->eapol_key_index_workaround) {
154 /* According to some information, WinXP Supplicant seems to
155 * interpret bit7 as an indication whether the key is to be
156 * activated, so make it possible to enable workaround that
157 * sets this bit for all keys. */
158 key->key_index |= BIT(7);
161 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
162 * MSK[32..63] is used to sign the message. */
163 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
164 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
165 "and signing EAPOL-Key");
169 os_memcpy((u8 *) (key + 1), key_data, key_len);
170 ekey_len = sizeof(key->key_iv) + 32;
171 ekey = os_malloc(ekey_len);
173 wpa_printf(MSG_ERROR, "Could not encrypt key");
177 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
178 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
179 rc4((u8 *) (key + 1), key_len, ekey, ekey_len);
182 /* This header is needed here for HMAC-MD5, but it will be regenerated
183 * in ieee802_1x_send() */
184 hdr->version = hapd->conf->eapol_version;
185 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
186 hdr->length = host_to_be16(len);
187 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
190 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
191 " (%s index=%d)", MAC2STR(sm->addr),
192 broadcast ? "broadcast" : "unicast", idx);
193 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
195 sta->eapol_sm->dot1xAuthEapolFramesTx++;
200 static struct hostapd_wep_keys *
201 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
203 struct hostapd_wep_keys *key;
205 key = os_zalloc(sizeof(*key));
209 key->default_len = hapd->conf->default_wep_key_len;
211 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
212 key->idx < hapd->conf->broadcast_key_idx_min)
213 key->idx = hapd->conf->broadcast_key_idx_min;
217 if (!key->key[key->idx])
218 key->key[key->idx] = os_malloc(key->default_len);
219 if (key->key[key->idx] == NULL ||
220 os_get_random(key->key[key->idx], key->default_len)) {
221 printf("Could not generate random WEP key (dynamic VLAN).\n");
222 os_free(key->key[key->idx]);
223 key->key[key->idx] = NULL;
227 key->len[key->idx] = key->default_len;
229 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
231 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
232 key->key[key->idx], key->len[key->idx]);
234 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL, key->idx,
235 key->key[key->idx], key->len[key->idx], 1))
236 printf("Could not set dynamic VLAN WEP encryption key.\n");
238 hostapd_set_ieee8021x(ifname, hapd, 1);
244 static struct hostapd_wep_keys *
245 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
253 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
254 ssid->dyn_vlan_keys[vlan_id])
255 return ssid->dyn_vlan_keys[vlan_id];
257 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
258 "state machine for VLAN ID %lu",
259 (unsigned long) vlan_id);
261 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
262 if (ifname == NULL) {
263 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
264 "cannot create group key state machine",
265 (unsigned long) vlan_id);
269 if (ssid->dyn_vlan_keys == NULL) {
270 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
271 ssid->dyn_vlan_keys = os_zalloc(size);
272 if (ssid->dyn_vlan_keys == NULL)
274 ssid->max_dyn_vlan_keys = vlan_id;
277 if (ssid->max_dyn_vlan_keys < vlan_id) {
278 struct hostapd_wep_keys **na;
279 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
280 na = os_realloc(ssid->dyn_vlan_keys, size);
283 ssid->dyn_vlan_keys = na;
284 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
285 (vlan_id - ssid->max_dyn_vlan_keys) *
286 sizeof(ssid->dyn_vlan_keys[0]));
287 ssid->max_dyn_vlan_keys = vlan_id;
290 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
292 return ssid->dyn_vlan_keys[vlan_id];
296 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
298 struct hostapd_wep_keys *key = NULL;
299 struct eapol_state_machine *sm = sta->eapol_sm;
302 if (sm == NULL || !sm->eap_if->eapKeyData)
305 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
308 vlan_id = sta->vlan_id;
309 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
313 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
314 if (key && key->key[key->idx])
315 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
318 } else if (hapd->default_wep_key) {
319 ieee802_1x_tx_key_one(hapd, sta, hapd->default_wep_key_idx, 1,
320 hapd->default_wep_key,
321 hapd->conf->default_wep_key_len);
324 if (hapd->conf->individual_wep_key_len > 0) {
326 ikey = os_malloc(hapd->conf->individual_wep_key_len);
328 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
329 wpa_printf(MSG_ERROR, "Could not generate random "
330 "individual WEP key.");
335 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
336 ikey, hapd->conf->individual_wep_key_len);
338 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
339 hapd->conf->individual_wep_key_len);
341 /* TODO: set encryption in TX callback, i.e., only after STA
342 * has ACKed EAPOL-Key frame */
343 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP",
345 hapd->conf->individual_wep_key_len,
347 wpa_printf(MSG_ERROR, "Could not set individual WEP "
356 const char *radius_mode_txt(struct hostapd_data *hapd)
358 if (hapd->iface->current_mode == NULL)
361 switch (hapd->iface->current_mode->mode) {
362 case HOSTAPD_MODE_IEEE80211A:
364 case HOSTAPD_MODE_IEEE80211G:
366 case HOSTAPD_MODE_IEEE80211B:
373 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
378 for (i = 0; i < sta->supported_rates_len; i++)
379 if ((sta->supported_rates[i] & 0x7f) > rate)
380 rate = sta->supported_rates[i] & 0x7f;
386 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
387 struct eapol_state_machine *sm,
388 const u8 *eap, size_t len)
393 if (len <= sizeof(struct eap_hdr) ||
394 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
397 identity = eap_get_identity(sm->eap, &identity_len);
398 if (identity == NULL)
401 /* Save station identity for future RADIUS packets */
402 os_free(sm->identity);
403 sm->identity = os_malloc(identity_len + 1);
404 if (sm->identity == NULL) {
405 sm->identity_len = 0;
409 os_memcpy(sm->identity, identity, identity_len);
410 sm->identity_len = identity_len;
411 sm->identity[identity_len] = '\0';
412 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
413 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
414 sm->dot1xAuthEapolRespIdFramesRx++;
418 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
419 struct sta_info *sta,
420 const u8 *eap, size_t len)
422 struct radius_msg *msg;
424 struct eapol_state_machine *sm = sta->eapol_sm;
429 ieee802_1x_learn_identity(hapd, sm, eap, len);
431 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
434 sm->radius_identifier = radius_client_get_id(hapd->radius);
435 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
436 sm->radius_identifier);
438 printf("Could not create net RADIUS packet\n");
442 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
445 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
446 sm->identity, sm->identity_len)) {
447 printf("Could not add User-Name\n");
451 if (hapd->conf->own_ip_addr.af == AF_INET &&
452 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
453 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
454 printf("Could not add NAS-IP-Address\n");
459 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
460 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
461 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
462 printf("Could not add NAS-IPv6-Address\n");
465 #endif /* CONFIG_IPV6 */
467 if (hapd->conf->nas_identifier &&
468 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
469 (u8 *) hapd->conf->nas_identifier,
470 os_strlen(hapd->conf->nas_identifier))) {
471 printf("Could not add NAS-Identifier\n");
475 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
476 printf("Could not add NAS-Port\n");
480 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
481 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
482 buf[sizeof(buf) - 1] = '\0';
483 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
484 (u8 *) buf, os_strlen(buf))) {
485 printf("Could not add Called-Station-Id\n");
489 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
491 buf[sizeof(buf) - 1] = '\0';
492 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
493 (u8 *) buf, os_strlen(buf))) {
494 printf("Could not add Calling-Station-Id\n");
498 /* TODO: should probably check MTU from driver config; 2304 is max for
499 * IEEE 802.11, but use 1400 to avoid problems with too large packets
501 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
502 printf("Could not add Framed-MTU\n");
506 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
507 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
508 printf("Could not add NAS-Port-Type\n");
512 if (sta->flags & WLAN_STA_PREAUTH) {
513 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
516 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
517 radius_sta_rate(hapd, sta) / 2,
518 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
519 radius_mode_txt(hapd));
520 buf[sizeof(buf) - 1] = '\0';
522 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
523 (u8 *) buf, os_strlen(buf))) {
524 printf("Could not add Connect-Info\n");
528 if (eap && !radius_msg_add_eap(msg, eap, len)) {
529 printf("Could not add EAP-Message\n");
533 /* State attribute must be copied if and only if this packet is
534 * Access-Request reply to the previous Access-Challenge */
535 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
536 RADIUS_CODE_ACCESS_CHALLENGE) {
537 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
540 printf("Could not copy State attribute from previous "
541 "Access-Challenge\n");
545 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
549 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
553 radius_msg_free(msg);
558 char *eap_type_text(u8 type)
561 case EAP_TYPE_IDENTITY: return "Identity";
562 case EAP_TYPE_NOTIFICATION: return "Notification";
563 case EAP_TYPE_NAK: return "Nak";
564 case EAP_TYPE_MD5: return "MD5-Challenge";
565 case EAP_TYPE_OTP: return "One-Time Password";
566 case EAP_TYPE_GTC: return "Generic Token Card";
567 case EAP_TYPE_TLS: return "TLS";
568 case EAP_TYPE_TTLS: return "TTLS";
569 case EAP_TYPE_PEAP: return "PEAP";
570 case EAP_TYPE_SIM: return "SIM";
571 case EAP_TYPE_FAST: return "FAST";
572 case EAP_TYPE_SAKE: return "SAKE";
573 case EAP_TYPE_PSK: return "PSK";
574 case EAP_TYPE_PAX: return "PAX";
575 default: return "Unknown";
580 static void handle_eap_response(struct hostapd_data *hapd,
581 struct sta_info *sta, struct eap_hdr *eap,
585 struct eapol_state_machine *sm = sta->eapol_sm;
589 data = (u8 *) (eap + 1);
591 if (len < sizeof(*eap) + 1) {
592 printf("handle_eap_response: too short response data\n");
596 sm->eap_type_supp = type = data[0];
597 eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
599 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
600 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
601 "id=%d len=%d) from STA: EAP Response-%s (%d)",
602 eap->code, eap->identifier, be_to_host16(eap->length),
603 eap_type_text(type), type);
605 sm->dot1xAuthEapolRespFramesRx++;
607 wpabuf_free(sm->eap_if->eapRespData);
608 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
613 /* Process incoming EAP packet from Supplicant */
614 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
620 if (len < sizeof(*eap)) {
621 printf(" too short EAP packet\n");
625 eap = (struct eap_hdr *) buf;
627 eap_len = be_to_host16(eap->length);
628 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
629 eap->code, eap->identifier, eap_len);
630 if (eap_len < sizeof(*eap)) {
631 wpa_printf(MSG_DEBUG, " Invalid EAP length");
633 } else if (eap_len > len) {
634 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
637 } else if (eap_len < len) {
638 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
639 "packet", (unsigned long) len - eap_len);
643 case EAP_CODE_REQUEST:
644 wpa_printf(MSG_DEBUG, " (request)");
646 case EAP_CODE_RESPONSE:
647 wpa_printf(MSG_DEBUG, " (response)");
648 handle_eap_response(hapd, sta, eap, eap_len);
650 case EAP_CODE_SUCCESS:
651 wpa_printf(MSG_DEBUG, " (success)");
653 case EAP_CODE_FAILURE:
654 wpa_printf(MSG_DEBUG, " (failure)");
657 wpa_printf(MSG_DEBUG, " (unknown code)");
663 /* Process the EAPOL frames from the Supplicant */
664 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
667 struct sta_info *sta;
668 struct ieee802_1x_hdr *hdr;
669 struct ieee802_1x_eapol_key *key;
671 struct rsn_pmksa_cache_entry *pmksa;
673 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
676 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
677 (unsigned long) len, MAC2STR(sa));
678 sta = ap_get_sta(hapd, sa);
680 printf(" no station information available\n");
684 if (len < sizeof(*hdr)) {
685 printf(" too short IEEE 802.1X packet\n");
689 hdr = (struct ieee802_1x_hdr *) buf;
690 datalen = be_to_host16(hdr->length);
691 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
692 hdr->version, hdr->type, datalen);
694 if (len - sizeof(*hdr) < datalen) {
695 printf(" frame too short for this IEEE 802.1X packet\n");
697 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
700 if (len - sizeof(*hdr) > datalen) {
701 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
702 "IEEE 802.1X packet",
703 (unsigned long) len - sizeof(*hdr) - datalen);
707 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
708 sta->eapol_sm->dot1xAuthEapolFramesRx++;
711 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
712 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
713 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
714 (key->type == EAPOL_KEY_TYPE_WPA ||
715 key->type == EAPOL_KEY_TYPE_RSN)) {
716 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
717 sizeof(*hdr) + datalen);
721 if (!hapd->conf->ieee802_1x ||
722 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
725 if (!sta->eapol_sm) {
726 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
727 sta->flags & WLAN_STA_PREAUTH,
733 /* since we support version 1, we can ignore version field and proceed
734 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
735 /* TODO: actually, we are not version 1 anymore.. However, Version 2
736 * does not change frame contents, so should be ok to process frames
737 * more or less identically. Some changes might be needed for
738 * verification of fields. */
741 case IEEE802_1X_TYPE_EAP_PACKET:
742 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
745 case IEEE802_1X_TYPE_EAPOL_START:
746 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
747 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
749 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
750 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
752 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
753 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
754 "available - ignore it since "
755 "STA sent EAPOL-Start");
756 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
758 sta->eapol_sm->eapolStart = TRUE;
759 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
760 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
763 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
764 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
765 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
767 sta->acct_terminate_cause =
768 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
769 sta->eapol_sm->eapolLogoff = TRUE;
770 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
773 case IEEE802_1X_TYPE_EAPOL_KEY:
774 wpa_printf(MSG_DEBUG, " EAPOL-Key");
775 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
776 wpa_printf(MSG_DEBUG, " Dropped key data from "
777 "unauthorized Supplicant");
782 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
783 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
784 /* TODO: implement support for this; show data */
788 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
789 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
793 eapol_auth_step(sta->eapol_sm);
797 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
799 struct rsn_pmksa_cache_entry *pmksa;
803 if ((!force_1x && !hapd->conf->ieee802_1x) ||
804 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
807 if (sta->eapol_sm == NULL) {
808 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
809 HOSTAPD_LEVEL_DEBUG, "start authentication");
810 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
811 sta->flags & WLAN_STA_PREAUTH,
813 if (sta->eapol_sm == NULL) {
814 hostapd_logger(hapd, sta->addr,
815 HOSTAPD_MODULE_IEEE8021X,
817 "failed to allocate state machine");
823 sta->eapol_sm->eap_if->portEnabled = TRUE;
825 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
829 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
831 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
832 /* Setup EAPOL state machines to already authenticated state
833 * because of existing PMKSA information in the cache. */
834 sta->eapol_sm->keyRun = TRUE;
835 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
836 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
837 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
838 sta->eapol_sm->authSuccess = TRUE;
839 if (sta->eapol_sm->eap)
840 eap_sm_notify_cached(sta->eapol_sm->eap);
841 old_vlanid = sta->vlan_id;
842 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
843 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
845 ap_sta_bind_vlan(hapd, sta, old_vlanid);
849 * Force EAPOL state machines to start
850 * re-authentication without having to wait for the
851 * Supplicant to send EAPOL-Start.
853 sta->eapol_sm->reAuthenticate = TRUE;
855 eapol_auth_step(sta->eapol_sm);
860 void ieee802_1x_free_radius_class(struct radius_class_data *class)
865 for (i = 0; i < class->count; i++)
866 os_free(class->attr[i].data);
867 os_free(class->attr);
873 int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
874 const struct radius_class_data *src)
878 if (src->attr == NULL)
881 dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
882 if (dst->attr == NULL)
887 for (i = 0; i < src->count; i++) {
888 dst->attr[i].data = os_malloc(src->attr[i].len);
889 if (dst->attr[i].data == NULL)
892 os_memcpy(dst->attr[i].data, src->attr[i].data,
894 dst->attr[i].len = src->attr[i].len;
901 void ieee802_1x_free_station(struct sta_info *sta)
903 struct eapol_state_machine *sm = sta->eapol_sm;
905 eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
910 sta->eapol_sm = NULL;
912 if (sm->last_recv_radius) {
913 radius_msg_free(sm->last_recv_radius);
914 os_free(sm->last_recv_radius);
917 os_free(sm->identity);
918 ieee802_1x_free_radius_class(&sm->radius_class);
923 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
924 struct sta_info *sta)
931 struct radius_msg *msg;
932 struct eapol_state_machine *sm = sta->eapol_sm;
934 if (sm == NULL || sm->last_recv_radius == NULL) {
936 sm->eap_if->aaaEapNoReq = TRUE;
940 msg = sm->last_recv_radius;
942 eap = radius_msg_get_eap(msg, &len);
944 /* RFC 3579, Chap. 2.6.3:
945 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
947 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
948 HOSTAPD_LEVEL_WARNING, "could not extract "
949 "EAP-Message from RADIUS message");
950 sm->eap_if->aaaEapNoReq = TRUE;
954 if (len < sizeof(*hdr)) {
955 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
956 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
957 "received from authentication server");
959 sm->eap_if->aaaEapNoReq = TRUE;
963 if (len > sizeof(*hdr))
964 eap_type = eap[sizeof(*hdr)];
966 hdr = (struct eap_hdr *) eap;
968 case EAP_CODE_REQUEST:
970 sm->eap_type_authsrv = eap_type;
971 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
972 eap_type >= 0 ? eap_type_text(eap_type) : "??",
975 case EAP_CODE_RESPONSE:
976 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
977 eap_type >= 0 ? eap_type_text(eap_type) : "??",
980 case EAP_CODE_SUCCESS:
981 os_strlcpy(buf, "EAP Success", sizeof(buf));
983 case EAP_CODE_FAILURE:
984 os_strlcpy(buf, "EAP Failure", sizeof(buf));
987 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
990 buf[sizeof(buf) - 1] = '\0';
991 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
992 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
993 "id=%d len=%d) from RADIUS server: %s",
994 hdr->code, hdr->identifier, be_to_host16(hdr->length),
996 sm->eap_if->aaaEapReq = TRUE;
998 wpabuf_free(sm->eap_if->aaaEapReqData);
999 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1003 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1004 struct sta_info *sta, struct radius_msg *msg,
1005 struct radius_msg *req,
1006 u8 *shared_secret, size_t shared_secret_len)
1008 struct radius_ms_mppe_keys *keys;
1009 struct eapol_state_machine *sm = sta->eapol_sm;
1013 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1016 if (keys && keys->send && keys->recv) {
1017 size_t len = keys->send_len + keys->recv_len;
1018 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1019 keys->send, keys->send_len);
1020 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1021 keys->recv, keys->recv_len);
1023 os_free(sm->eap_if->aaaEapKeyData);
1024 sm->eap_if->aaaEapKeyData = os_malloc(len);
1025 if (sm->eap_if->aaaEapKeyData) {
1026 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1028 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1029 keys->send, keys->send_len);
1030 sm->eap_if->aaaEapKeyDataLen = len;
1031 sm->eap_if->aaaEapKeyAvailable = TRUE;
1036 os_free(keys->send);
1037 os_free(keys->recv);
1043 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1044 struct sta_info *sta,
1045 struct radius_msg *msg)
1049 struct eapol_state_machine *sm = sta->eapol_sm;
1051 struct radius_attr_data *nclass;
1052 size_t nclass_count;
1054 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1058 ieee802_1x_free_radius_class(&sm->radius_class);
1059 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1063 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1070 for (i = 0; i < count; i++) {
1072 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1078 } while (class_len < 1);
1080 nclass[nclass_count].data = os_malloc(class_len);
1081 if (nclass[nclass_count].data == NULL)
1084 os_memcpy(nclass[nclass_count].data, class, class_len);
1085 nclass[nclass_count].len = class_len;
1089 sm->radius_class.attr = nclass;
1090 sm->radius_class.count = nclass_count;
1091 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1092 "attributes for " MACSTR,
1093 (unsigned long) sm->radius_class.count,
1094 MAC2STR(sta->addr));
1098 /* Update sta->identity based on User-Name attribute in Access-Accept */
1099 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1100 struct sta_info *sta,
1101 struct radius_msg *msg)
1105 struct eapol_state_machine *sm = sta->eapol_sm;
1110 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1114 identity = os_malloc(len + 1);
1115 if (identity == NULL)
1118 os_memcpy(identity, buf, len);
1119 identity[len] = '\0';
1121 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1122 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1123 "User-Name from Access-Accept '%s'",
1124 sm->identity ? (char *) sm->identity : "N/A",
1127 os_free(sm->identity);
1128 sm->identity = identity;
1129 sm->identity_len = len;
1133 struct sta_id_search {
1135 struct eapol_state_machine *sm;
1139 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1140 struct sta_info *sta,
1143 struct sta_id_search *id_search = ctx;
1144 struct eapol_state_machine *sm = sta->eapol_sm;
1146 if (sm && sm->radius_identifier >= 0 &&
1147 sm->radius_identifier == id_search->identifier) {
1155 static struct eapol_state_machine *
1156 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1158 struct sta_id_search id_search;
1159 id_search.identifier = identifier;
1160 id_search.sm = NULL;
1161 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1162 return id_search.sm;
1166 /* Process the RADIUS frames from Authentication Server */
1167 static RadiusRxResult
1168 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1169 u8 *shared_secret, size_t shared_secret_len,
1172 struct hostapd_data *hapd = data;
1173 struct sta_info *sta;
1174 u32 session_timeout = 0, termination_action, acct_interim_interval;
1175 int session_timeout_set, old_vlanid = 0;
1177 struct eapol_state_machine *sm;
1178 int override_eapReq = 0;
1180 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1182 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1183 "station for this RADIUS message");
1184 return RADIUS_RX_UNKNOWN;
1188 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1189 * present when packet contains an EAP-Message attribute */
1190 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1191 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1193 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1194 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1195 "Message-Authenticator since it does not include "
1197 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1199 printf("Incoming RADIUS packet did not have correct "
1200 "Message-Authenticator - dropped\n");
1201 return RADIUS_RX_INVALID_AUTHENTICATOR;
1204 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1205 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1206 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1207 printf("Unknown RADIUS message code\n");
1208 return RADIUS_RX_UNKNOWN;
1211 sm->radius_identifier = -1;
1212 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1213 MAC2STR(sta->addr));
1215 if (sm->last_recv_radius) {
1216 radius_msg_free(sm->last_recv_radius);
1217 os_free(sm->last_recv_radius);
1220 sm->last_recv_radius = msg;
1222 session_timeout_set =
1223 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1225 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1226 &termination_action))
1227 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1229 if (hapd->conf->radius->acct_interim_interval == 0 &&
1230 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1231 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1232 &acct_interim_interval) == 0) {
1233 if (acct_interim_interval < 60) {
1234 hostapd_logger(hapd, sta->addr,
1235 HOSTAPD_MODULE_IEEE8021X,
1237 "ignored too small "
1238 "Acct-Interim-Interval %d",
1239 acct_interim_interval);
1241 sta->acct_interim_interval = acct_interim_interval;
1245 switch (msg->hdr->code) {
1246 case RADIUS_CODE_ACCESS_ACCEPT:
1247 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1250 old_vlanid = sta->vlan_id;
1251 sta->vlan_id = radius_msg_get_vlanid(msg);
1253 if (sta->vlan_id > 0 &&
1254 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1256 hostapd_logger(hapd, sta->addr,
1257 HOSTAPD_MODULE_RADIUS,
1259 "VLAN ID %d", sta->vlan_id);
1260 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1261 sta->eapol_sm->authFail = TRUE;
1262 hostapd_logger(hapd, sta->addr,
1263 HOSTAPD_MODULE_IEEE8021X,
1264 HOSTAPD_LEVEL_INFO, "authentication "
1265 "server did not include required VLAN "
1266 "ID in Access-Accept");
1270 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1272 /* RFC 3580, Ch. 3.17 */
1273 if (session_timeout_set && termination_action ==
1274 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1275 sm->reAuthPeriod = session_timeout;
1276 } else if (session_timeout_set)
1277 ap_sta_session_timeout(hapd, sta, session_timeout);
1279 sm->eap_if->aaaSuccess = TRUE;
1280 override_eapReq = 1;
1281 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1283 ieee802_1x_store_radius_class(hapd, sta, msg);
1284 ieee802_1x_update_sta_identity(hapd, sta, msg);
1285 if (sm->eap_if->eapKeyAvailable &&
1286 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1287 session_timeout_set ?
1288 (int) session_timeout : -1, sm) == 0) {
1289 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1290 HOSTAPD_LEVEL_DEBUG,
1291 "Added PMKSA cache entry");
1294 case RADIUS_CODE_ACCESS_REJECT:
1295 sm->eap_if->aaaFail = TRUE;
1296 override_eapReq = 1;
1298 case RADIUS_CODE_ACCESS_CHALLENGE:
1299 sm->eap_if->aaaEapReq = TRUE;
1300 if (session_timeout_set) {
1301 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1302 eap_timeout = session_timeout;
1305 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
1306 HOSTAPD_LEVEL_DEBUG,
1307 "using EAP timeout of %d seconds%s",
1309 session_timeout_set ? " (from RADIUS)" : "");
1310 eloop_cancel_timeout(ieee802_1x_eap_timeout, sta, NULL);
1311 eloop_register_timeout(eap_timeout, 0, ieee802_1x_eap_timeout,
1313 sm->eap_if->eapTimeout = FALSE;
1317 ieee802_1x_decapsulate_radius(hapd, sta);
1318 if (override_eapReq)
1319 sm->eap_if->aaaEapReq = FALSE;
1321 eapol_auth_step(sm);
1323 return RADIUS_RX_QUEUED;
1327 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1329 struct eapol_state_machine *sm = sta->eapol_sm;
1333 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1334 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1336 if (sm->last_recv_radius) {
1337 radius_msg_free(sm->last_recv_radius);
1338 os_free(sm->last_recv_radius);
1339 sm->last_recv_radius = NULL;
1344 #ifdef HOSTAPD_DUMP_STATE
1345 static void fprint_char(FILE *f, char c)
1347 if (c >= 32 && c < 127)
1348 fprintf(f, "%c", c);
1350 fprintf(f, "<%02x>", c);
1354 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1356 struct eapol_state_machine *sm = sta->eapol_sm;
1360 fprintf(f, "%sIEEE 802.1X:\n", prefix);
1364 fprintf(f, "%sidentity=", prefix);
1365 for (i = 0; i < sm->identity_len; i++)
1366 fprint_char(f, sm->identity[i]);
1370 fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1371 "Supplicant: %d (%s)\n", prefix,
1372 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1373 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1375 fprintf(f, "%scached_packets=%s\n", prefix,
1376 sm->last_recv_radius ? "[RX RADIUS]" : "");
1378 eapol_auth_dump_state(f, prefix, sm);
1380 #endif /* HOSTAPD_DUMP_STATE */
1383 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1385 if (hapd->conf->default_wep_key_len < 1)
1388 os_free(hapd->default_wep_key);
1389 hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1390 if (hapd->default_wep_key == NULL ||
1391 os_get_random(hapd->default_wep_key,
1392 hapd->conf->default_wep_key_len)) {
1393 printf("Could not generate random WEP key.\n");
1394 os_free(hapd->default_wep_key);
1395 hapd->default_wep_key = NULL;
1399 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1400 hapd->default_wep_key,
1401 hapd->conf->default_wep_key_len);
1407 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1408 struct sta_info *sta, void *ctx)
1410 if (sta->eapol_sm) {
1411 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1412 eapol_auth_step(sta->eapol_sm);
1418 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1420 struct hostapd_data *hapd = eloop_ctx;
1422 if (hapd->default_wep_key_idx >= 3)
1423 hapd->default_wep_key_idx =
1424 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1426 hapd->default_wep_key_idx++;
1428 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1429 hapd->default_wep_key_idx);
1431 if (ieee802_1x_rekey_broadcast(hapd)) {
1432 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1433 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1434 "new broadcast key");
1435 os_free(hapd->default_wep_key);
1436 hapd->default_wep_key = NULL;
1440 /* TODO: Could setup key for RX here, but change default TX keyid only
1441 * after new broadcast key has been sent to all stations. */
1442 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1443 hapd->default_wep_key_idx,
1444 hapd->default_wep_key,
1445 hapd->conf->default_wep_key_len, 1)) {
1446 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1447 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1448 "new broadcast key");
1449 os_free(hapd->default_wep_key);
1450 hapd->default_wep_key = NULL;
1454 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1456 if (hapd->conf->wep_rekeying_period > 0) {
1457 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1458 ieee802_1x_rekey, hapd, NULL);
1463 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1464 const u8 *data, size_t datalen)
1466 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1470 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1471 const u8 *data, size_t datalen)
1473 struct hostapd_data *hapd = ctx;
1474 struct sta_info *sta = sta_ctx;
1476 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1480 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1483 struct hostapd_data *hapd = ctx;
1484 struct sta_info *sta = sta_ctx;
1486 rsn_preauth_finished(hapd, sta, success);
1488 ieee802_1x_finished(hapd, sta, success);
1492 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1493 size_t identity_len, int phase2,
1494 struct eap_user *user)
1496 struct hostapd_data *hapd = ctx;
1497 const struct hostapd_eap_user *eap_user;
1500 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1501 identity_len, phase2);
1502 if (eap_user == NULL)
1505 os_memset(user, 0, sizeof(*user));
1506 user->phase2 = phase2;
1507 count = EAP_USER_MAX_METHODS;
1508 if (count > EAP_MAX_METHODS)
1509 count = EAP_MAX_METHODS;
1510 for (i = 0; i < count; i++) {
1511 user->methods[i].vendor = eap_user->methods[i].vendor;
1512 user->methods[i].method = eap_user->methods[i].method;
1515 if (eap_user->password) {
1516 user->password = os_malloc(eap_user->password_len);
1517 if (user->password == NULL)
1519 os_memcpy(user->password, eap_user->password,
1520 eap_user->password_len);
1521 user->password_len = eap_user->password_len;
1523 user->force_version = eap_user->force_version;
1524 user->ttls_auth = eap_user->ttls_auth;
1530 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1532 struct hostapd_data *hapd = ctx;
1533 struct sta_info *sta;
1534 sta = ap_get_sta(hapd, addr);
1535 if (sta == NULL || sta->eapol_sm == NULL)
1541 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1542 eapol_logger_level level, const char *txt)
1544 struct hostapd_data *hapd = ctx;
1548 case EAPOL_LOGGER_WARNING:
1549 hlevel = HOSTAPD_LEVEL_WARNING;
1551 case EAPOL_LOGGER_INFO:
1552 hlevel = HOSTAPD_LEVEL_INFO;
1554 case EAPOL_LOGGER_DEBUG:
1556 hlevel = HOSTAPD_LEVEL_DEBUG;
1560 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1565 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1568 struct hostapd_data *hapd = ctx;
1569 struct sta_info *sta = sta_ctx;
1570 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1574 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1576 struct hostapd_data *hapd = ctx;
1577 struct sta_info *sta = sta_ctx;
1578 ieee802_1x_abort_auth(hapd, sta);
1582 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1584 struct hostapd_data *hapd = ctx;
1585 struct sta_info *sta = sta_ctx;
1586 ieee802_1x_tx_key(hapd, sta);
1590 int ieee802_1x_init(struct hostapd_data *hapd)
1593 struct eapol_auth_config conf;
1594 struct eapol_auth_cb cb;
1596 os_memset(&conf, 0, sizeof(conf));
1598 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1599 conf.wpa = hapd->conf->wpa;
1600 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1601 conf.eap_server = hapd->conf->eap_server;
1602 conf.ssl_ctx = hapd->ssl_ctx;
1603 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1604 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1605 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1606 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1607 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1608 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1609 conf.tnc = hapd->conf->tnc;
1611 os_memset(&cb, 0, sizeof(cb));
1612 cb.eapol_send = ieee802_1x_eapol_send;
1613 cb.aaa_send = ieee802_1x_aaa_send;
1614 cb.finished = _ieee802_1x_finished;
1615 cb.get_eap_user = ieee802_1x_get_eap_user;
1616 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1617 cb.logger = ieee802_1x_logger;
1618 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1619 cb.abort_auth = _ieee802_1x_abort_auth;
1620 cb.tx_key = _ieee802_1x_tx_key;
1622 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1623 if (hapd->eapol_auth == NULL)
1626 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1627 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1630 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1631 ieee802_1x_receive_auth, hapd))
1634 if (hapd->conf->default_wep_key_len) {
1635 hostapd_set_privacy(hapd, 1);
1637 for (i = 0; i < 4; i++)
1638 hostapd_set_encryption(hapd->conf->iface, hapd,
1639 "none", NULL, i, NULL, 0, 0);
1641 ieee802_1x_rekey(hapd, NULL);
1643 if (hapd->default_wep_key == NULL)
1651 void ieee802_1x_deinit(struct hostapd_data *hapd)
1653 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1655 if (hapd->driver != NULL &&
1656 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1657 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1659 eapol_auth_deinit(hapd->eapol_auth);
1660 hapd->eapol_auth = NULL;
1664 int ieee802_1x_reconfig(struct hostapd_data *hapd,
1665 struct hostapd_config *oldconf,
1666 struct hostapd_bss_config *oldbss)
1668 ieee802_1x_deinit(hapd);
1669 return ieee802_1x_init(hapd);
1673 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1674 u8 *buf, size_t len, int ack)
1676 struct ieee80211_hdr *hdr;
1677 struct ieee802_1x_hdr *xhdr;
1678 struct ieee802_1x_eapol_key *key;
1680 const unsigned char rfc1042_hdr[ETH_ALEN] =
1681 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1685 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1688 hdr = (struct ieee80211_hdr *) buf;
1689 pos = (u8 *) (hdr + 1);
1690 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1692 pos += sizeof(rfc1042_hdr);
1693 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1697 xhdr = (struct ieee802_1x_hdr *) pos;
1698 pos += sizeof(*xhdr);
1700 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1701 "type=%d length=%d - ack=%d",
1702 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1703 be_to_host16(xhdr->length), ack);
1705 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1706 * or Authenticator state machines, but EAPOL-Key packets are not
1707 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1708 * packets couple of times because otherwise STA keys become
1709 * unsynchronized with AP. */
1710 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1711 pos + sizeof(*key) <= buf + len) {
1712 key = (struct ieee802_1x_eapol_key *) pos;
1713 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1714 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1715 "frame (%scast index=%d)",
1716 key->key_index & BIT(7) ? "uni" : "broad",
1717 key->key_index & ~BIT(7));
1718 /* TODO: re-send EAPOL-Key couple of times (with short delay
1719 * between them?). If all attempt fail, report error and
1720 * deauthenticate STA so that it will get new keys when
1721 * authenticating again (e.g., after returning in range).
1722 * Separate limit/transmit state needed both for unicast and
1723 * broadcast keys(?) */
1725 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1726 * to here and change the key only if the EAPOL-Key packet was Acked.
1733 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1735 if (sm == NULL || sm->identity == NULL)
1738 *len = sm->identity_len;
1739 return sm->identity;
1743 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1746 if (sm == NULL || sm->radius_class.attr == NULL ||
1747 idx >= (int) sm->radius_class.count)
1750 *len = sm->radius_class.attr[idx].len;
1751 return sm->radius_class.attr[idx].data;
1755 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1760 *len = sm->eap_if->eapKeyDataLen;
1761 return sm->eap_if->eapKeyData;
1765 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1770 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1771 eapol_auth_step(sm);
1775 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1780 sm->portValid = valid ? TRUE : FALSE;
1781 eapol_auth_step(sm);
1785 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1790 sm->flags |= EAPOL_SM_PREAUTH;
1792 sm->flags &= ~EAPOL_SM_PREAUTH;
1796 static const char * bool_txt(Boolean bool)
1798 return bool ? "TRUE" : "FALSE";
1802 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1809 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1810 char *buf, size_t buflen)
1813 struct eapol_state_machine *sm = sta->eapol_sm;
1818 ret = os_snprintf(buf + len, buflen - len,
1819 "dot1xPaePortNumber=%d\n"
1820 "dot1xPaePortProtocolVersion=%d\n"
1821 "dot1xPaePortCapabilities=1\n"
1822 "dot1xPaePortInitialize=%d\n"
1823 "dot1xPaePortReauthenticate=FALSE\n",
1827 if (ret < 0 || (size_t) ret >= buflen - len)
1831 /* dot1xAuthConfigTable */
1832 ret = os_snprintf(buf + len, buflen - len,
1833 "dot1xAuthPaeState=%d\n"
1834 "dot1xAuthBackendAuthState=%d\n"
1835 "dot1xAuthAdminControlledDirections=%d\n"
1836 "dot1xAuthOperControlledDirections=%d\n"
1837 "dot1xAuthAuthControlledPortStatus=%d\n"
1838 "dot1xAuthAuthControlledPortControl=%d\n"
1839 "dot1xAuthQuietPeriod=%u\n"
1840 "dot1xAuthServerTimeout=%u\n"
1841 "dot1xAuthReAuthPeriod=%u\n"
1842 "dot1xAuthReAuthEnabled=%s\n"
1843 "dot1xAuthKeyTxEnabled=%s\n",
1844 sm->auth_pae_state + 1,
1845 sm->be_auth_state + 1,
1846 sm->adminControlledDirections,
1847 sm->operControlledDirections,
1853 bool_txt(sm->reAuthEnabled),
1854 bool_txt(sm->keyTxEnabled));
1855 if (ret < 0 || (size_t) ret >= buflen - len)
1859 /* dot1xAuthStatsTable */
1860 ret = os_snprintf(buf + len, buflen - len,
1861 "dot1xAuthEapolFramesRx=%u\n"
1862 "dot1xAuthEapolFramesTx=%u\n"
1863 "dot1xAuthEapolStartFramesRx=%u\n"
1864 "dot1xAuthEapolLogoffFramesRx=%u\n"
1865 "dot1xAuthEapolRespIdFramesRx=%u\n"
1866 "dot1xAuthEapolRespFramesRx=%u\n"
1867 "dot1xAuthEapolReqIdFramesTx=%u\n"
1868 "dot1xAuthEapolReqFramesTx=%u\n"
1869 "dot1xAuthInvalidEapolFramesRx=%u\n"
1870 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1871 "dot1xAuthLastEapolFrameVersion=%u\n"
1872 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1873 sm->dot1xAuthEapolFramesRx,
1874 sm->dot1xAuthEapolFramesTx,
1875 sm->dot1xAuthEapolStartFramesRx,
1876 sm->dot1xAuthEapolLogoffFramesRx,
1877 sm->dot1xAuthEapolRespIdFramesRx,
1878 sm->dot1xAuthEapolRespFramesRx,
1879 sm->dot1xAuthEapolReqIdFramesTx,
1880 sm->dot1xAuthEapolReqFramesTx,
1881 sm->dot1xAuthInvalidEapolFramesRx,
1882 sm->dot1xAuthEapLengthErrorFramesRx,
1883 sm->dot1xAuthLastEapolFrameVersion,
1885 if (ret < 0 || (size_t) ret >= buflen - len)
1889 /* dot1xAuthDiagTable */
1890 ret = os_snprintf(buf + len, buflen - len,
1891 "dot1xAuthEntersConnecting=%u\n"
1892 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1893 "dot1xAuthEntersAuthenticating=%u\n"
1894 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1895 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1896 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1897 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1898 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1899 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1900 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1901 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1902 "dot1xAuthBackendResponses=%u\n"
1903 "dot1xAuthBackendAccessChallenges=%u\n"
1904 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1905 "dot1xAuthBackendAuthSuccesses=%u\n"
1906 "dot1xAuthBackendAuthFails=%u\n",
1907 sm->authEntersConnecting,
1908 sm->authEapLogoffsWhileConnecting,
1909 sm->authEntersAuthenticating,
1910 sm->authAuthSuccessesWhileAuthenticating,
1911 sm->authAuthTimeoutsWhileAuthenticating,
1912 sm->authAuthFailWhileAuthenticating,
1913 sm->authAuthEapStartsWhileAuthenticating,
1914 sm->authAuthEapLogoffWhileAuthenticating,
1915 sm->authAuthReauthsWhileAuthenticated,
1916 sm->authAuthEapStartsWhileAuthenticated,
1917 sm->authAuthEapLogoffWhileAuthenticated,
1918 sm->backendResponses,
1919 sm->backendAccessChallenges,
1920 sm->backendOtherRequestsToSupplicant,
1921 sm->backendAuthSuccesses,
1922 sm->backendAuthFails);
1923 if (ret < 0 || (size_t) ret >= buflen - len)
1927 /* dot1xAuthSessionStatsTable */
1928 ret = os_snprintf(buf + len, buflen - len,
1929 /* TODO: dot1xAuthSessionOctetsRx */
1930 /* TODO: dot1xAuthSessionOctetsTx */
1931 /* TODO: dot1xAuthSessionFramesRx */
1932 /* TODO: dot1xAuthSessionFramesTx */
1933 "dot1xAuthSessionId=%08X-%08X\n"
1934 "dot1xAuthSessionAuthenticMethod=%d\n"
1935 "dot1xAuthSessionTime=%u\n"
1936 "dot1xAuthSessionTerminateCause=999\n"
1937 "dot1xAuthSessionUserName=%s\n",
1938 sta->acct_session_id_hi, sta->acct_session_id_lo,
1939 (wpa_key_mgmt_wpa_ieee8021x(
1940 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1942 (unsigned int) (time(NULL) -
1943 sta->acct_session_start),
1945 if (ret < 0 || (size_t) ret >= buflen - len)
1953 static void ieee802_1x_finished(struct hostapd_data *hapd,
1954 struct sta_info *sta, int success)
1958 /* TODO: get PMKLifetime from WPA parameters */
1959 static const int dot11RSNAConfigPMKLifetime = 43200;
1961 key = ieee802_1x_get_key(sta->eapol_sm, &len);
1962 if (success && key && len >= PMK_LEN &&
1963 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
1964 sta->eapol_sm) == 0) {
1965 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1966 HOSTAPD_LEVEL_DEBUG,
1967 "Added PMKSA cache entry (IEEE 802.1X)");