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_tx_key_one(struct hostapd_data *hapd,
109 struct sta_info *sta,
110 int idx, int broadcast,
111 u8 *key_data, size_t key_len)
114 struct ieee802_1x_hdr *hdr;
115 struct ieee802_1x_eapol_key *key;
116 size_t len, ekey_len;
117 struct eapol_state_machine *sm = sta->eapol_sm;
122 len = sizeof(*key) + key_len;
123 buf = os_zalloc(sizeof(*hdr) + len);
127 hdr = (struct ieee802_1x_hdr *) buf;
128 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
129 key->type = EAPOL_KEY_TYPE_RC4;
130 key->key_length = htons(key_len);
131 wpa_get_ntp_timestamp(key->replay_counter);
133 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
134 wpa_printf(MSG_ERROR, "Could not get random numbers");
139 key->key_index = idx | (broadcast ? 0 : BIT(7));
140 if (hapd->conf->eapol_key_index_workaround) {
141 /* According to some information, WinXP Supplicant seems to
142 * interpret bit7 as an indication whether the key is to be
143 * activated, so make it possible to enable workaround that
144 * sets this bit for all keys. */
145 key->key_index |= BIT(7);
148 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
149 * MSK[32..63] is used to sign the message. */
150 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
151 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
152 "and signing EAPOL-Key");
156 os_memcpy((u8 *) (key + 1), key_data, key_len);
157 ekey_len = sizeof(key->key_iv) + 32;
158 ekey = os_malloc(ekey_len);
160 wpa_printf(MSG_ERROR, "Could not encrypt key");
164 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
165 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
166 rc4((u8 *) (key + 1), key_len, ekey, ekey_len);
169 /* This header is needed here for HMAC-MD5, but it will be regenerated
170 * in ieee802_1x_send() */
171 hdr->version = hapd->conf->eapol_version;
172 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
173 hdr->length = host_to_be16(len);
174 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
177 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
178 " (%s index=%d)", MAC2STR(sm->addr),
179 broadcast ? "broadcast" : "unicast", idx);
180 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
182 sta->eapol_sm->dot1xAuthEapolFramesTx++;
187 static struct hostapd_wep_keys *
188 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
190 struct hostapd_wep_keys *key;
192 key = os_zalloc(sizeof(*key));
196 key->default_len = hapd->conf->default_wep_key_len;
198 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
199 key->idx < hapd->conf->broadcast_key_idx_min)
200 key->idx = hapd->conf->broadcast_key_idx_min;
204 if (!key->key[key->idx])
205 key->key[key->idx] = os_malloc(key->default_len);
206 if (key->key[key->idx] == NULL ||
207 os_get_random(key->key[key->idx], key->default_len)) {
208 printf("Could not generate random WEP key (dynamic VLAN).\n");
209 os_free(key->key[key->idx]);
210 key->key[key->idx] = NULL;
214 key->len[key->idx] = key->default_len;
216 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
218 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
219 key->key[key->idx], key->len[key->idx]);
221 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL, key->idx,
222 key->key[key->idx], key->len[key->idx], 1))
223 printf("Could not set dynamic VLAN WEP encryption key.\n");
225 hostapd_set_ieee8021x(ifname, hapd, 1);
231 static struct hostapd_wep_keys *
232 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
240 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
241 ssid->dyn_vlan_keys[vlan_id])
242 return ssid->dyn_vlan_keys[vlan_id];
244 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
245 "state machine for VLAN ID %lu",
246 (unsigned long) vlan_id);
248 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
249 if (ifname == NULL) {
250 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
251 "cannot create group key state machine",
252 (unsigned long) vlan_id);
256 if (ssid->dyn_vlan_keys == NULL) {
257 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
258 ssid->dyn_vlan_keys = os_zalloc(size);
259 if (ssid->dyn_vlan_keys == NULL)
261 ssid->max_dyn_vlan_keys = vlan_id;
264 if (ssid->max_dyn_vlan_keys < vlan_id) {
265 struct hostapd_wep_keys **na;
266 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
267 na = os_realloc(ssid->dyn_vlan_keys, size);
270 ssid->dyn_vlan_keys = na;
271 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
272 (vlan_id - ssid->max_dyn_vlan_keys) *
273 sizeof(ssid->dyn_vlan_keys[0]));
274 ssid->max_dyn_vlan_keys = vlan_id;
277 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
279 return ssid->dyn_vlan_keys[vlan_id];
283 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
285 struct hostapd_wep_keys *key = NULL;
286 struct eapol_state_machine *sm = sta->eapol_sm;
289 if (sm == NULL || !sm->eap_if->eapKeyData)
292 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
295 vlan_id = sta->vlan_id;
296 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
300 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
301 if (key && key->key[key->idx])
302 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
305 } else if (hapd->default_wep_key) {
306 ieee802_1x_tx_key_one(hapd, sta, hapd->default_wep_key_idx, 1,
307 hapd->default_wep_key,
308 hapd->conf->default_wep_key_len);
311 if (hapd->conf->individual_wep_key_len > 0) {
313 ikey = os_malloc(hapd->conf->individual_wep_key_len);
315 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
316 wpa_printf(MSG_ERROR, "Could not generate random "
317 "individual WEP key.");
322 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
323 ikey, hapd->conf->individual_wep_key_len);
325 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
326 hapd->conf->individual_wep_key_len);
328 /* TODO: set encryption in TX callback, i.e., only after STA
329 * has ACKed EAPOL-Key frame */
330 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP",
332 hapd->conf->individual_wep_key_len,
334 wpa_printf(MSG_ERROR, "Could not set individual WEP "
343 const char *radius_mode_txt(struct hostapd_data *hapd)
345 if (hapd->iface->current_mode == NULL)
348 switch (hapd->iface->current_mode->mode) {
349 case HOSTAPD_MODE_IEEE80211A:
351 case HOSTAPD_MODE_IEEE80211G:
353 case HOSTAPD_MODE_IEEE80211B:
360 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
365 for (i = 0; i < sta->supported_rates_len; i++)
366 if ((sta->supported_rates[i] & 0x7f) > rate)
367 rate = sta->supported_rates[i] & 0x7f;
373 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
374 struct eapol_state_machine *sm,
375 const u8 *eap, size_t len)
380 if (len <= sizeof(struct eap_hdr) ||
381 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
384 identity = eap_get_identity(sm->eap, &identity_len);
385 if (identity == NULL)
388 /* Save station identity for future RADIUS packets */
389 os_free(sm->identity);
390 sm->identity = os_malloc(identity_len + 1);
391 if (sm->identity == NULL) {
392 sm->identity_len = 0;
396 os_memcpy(sm->identity, identity, identity_len);
397 sm->identity_len = identity_len;
398 sm->identity[identity_len] = '\0';
399 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
400 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
401 sm->dot1xAuthEapolRespIdFramesRx++;
405 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
406 struct sta_info *sta,
407 const u8 *eap, size_t len)
409 struct radius_msg *msg;
411 struct eapol_state_machine *sm = sta->eapol_sm;
416 ieee802_1x_learn_identity(hapd, sm, eap, len);
418 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
421 sm->radius_identifier = radius_client_get_id(hapd->radius);
422 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
423 sm->radius_identifier);
425 printf("Could not create net RADIUS packet\n");
429 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
432 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
433 sm->identity, sm->identity_len)) {
434 printf("Could not add User-Name\n");
438 if (hapd->conf->own_ip_addr.af == AF_INET &&
439 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
440 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
441 printf("Could not add NAS-IP-Address\n");
446 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
447 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
448 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
449 printf("Could not add NAS-IPv6-Address\n");
452 #endif /* CONFIG_IPV6 */
454 if (hapd->conf->nas_identifier &&
455 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
456 (u8 *) hapd->conf->nas_identifier,
457 os_strlen(hapd->conf->nas_identifier))) {
458 printf("Could not add NAS-Identifier\n");
462 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
463 printf("Could not add NAS-Port\n");
467 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
468 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
469 buf[sizeof(buf) - 1] = '\0';
470 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
471 (u8 *) buf, os_strlen(buf))) {
472 printf("Could not add Called-Station-Id\n");
476 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
478 buf[sizeof(buf) - 1] = '\0';
479 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
480 (u8 *) buf, os_strlen(buf))) {
481 printf("Could not add Calling-Station-Id\n");
485 /* TODO: should probably check MTU from driver config; 2304 is max for
486 * IEEE 802.11, but use 1400 to avoid problems with too large packets
488 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
489 printf("Could not add Framed-MTU\n");
493 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
494 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
495 printf("Could not add NAS-Port-Type\n");
499 if (sta->flags & WLAN_STA_PREAUTH) {
500 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
503 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
504 radius_sta_rate(hapd, sta) / 2,
505 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
506 radius_mode_txt(hapd));
507 buf[sizeof(buf) - 1] = '\0';
509 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
510 (u8 *) buf, os_strlen(buf))) {
511 printf("Could not add Connect-Info\n");
515 if (eap && !radius_msg_add_eap(msg, eap, len)) {
516 printf("Could not add EAP-Message\n");
520 /* State attribute must be copied if and only if this packet is
521 * Access-Request reply to the previous Access-Challenge */
522 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
523 RADIUS_CODE_ACCESS_CHALLENGE) {
524 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
527 printf("Could not copy State attribute from previous "
528 "Access-Challenge\n");
532 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
536 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
540 radius_msg_free(msg);
545 char *eap_type_text(u8 type)
548 case EAP_TYPE_IDENTITY: return "Identity";
549 case EAP_TYPE_NOTIFICATION: return "Notification";
550 case EAP_TYPE_NAK: return "Nak";
551 case EAP_TYPE_MD5: return "MD5-Challenge";
552 case EAP_TYPE_OTP: return "One-Time Password";
553 case EAP_TYPE_GTC: return "Generic Token Card";
554 case EAP_TYPE_TLS: return "TLS";
555 case EAP_TYPE_TTLS: return "TTLS";
556 case EAP_TYPE_PEAP: return "PEAP";
557 case EAP_TYPE_SIM: return "SIM";
558 case EAP_TYPE_FAST: return "FAST";
559 case EAP_TYPE_SAKE: return "SAKE";
560 case EAP_TYPE_PSK: return "PSK";
561 case EAP_TYPE_PAX: return "PAX";
562 default: return "Unknown";
567 static void handle_eap_response(struct hostapd_data *hapd,
568 struct sta_info *sta, struct eap_hdr *eap,
572 struct eapol_state_machine *sm = sta->eapol_sm;
576 data = (u8 *) (eap + 1);
578 if (len < sizeof(*eap) + 1) {
579 printf("handle_eap_response: too short response data\n");
583 sm->eap_type_supp = type = data[0];
585 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
586 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
587 "id=%d len=%d) from STA: EAP Response-%s (%d)",
588 eap->code, eap->identifier, be_to_host16(eap->length),
589 eap_type_text(type), type);
591 sm->dot1xAuthEapolRespFramesRx++;
593 wpabuf_free(sm->eap_if->eapRespData);
594 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
599 /* Process incoming EAP packet from Supplicant */
600 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
606 if (len < sizeof(*eap)) {
607 printf(" too short EAP packet\n");
611 eap = (struct eap_hdr *) buf;
613 eap_len = be_to_host16(eap->length);
614 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
615 eap->code, eap->identifier, eap_len);
616 if (eap_len < sizeof(*eap)) {
617 wpa_printf(MSG_DEBUG, " Invalid EAP length");
619 } else if (eap_len > len) {
620 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
623 } else if (eap_len < len) {
624 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
625 "packet", (unsigned long) len - eap_len);
629 case EAP_CODE_REQUEST:
630 wpa_printf(MSG_DEBUG, " (request)");
632 case EAP_CODE_RESPONSE:
633 wpa_printf(MSG_DEBUG, " (response)");
634 handle_eap_response(hapd, sta, eap, eap_len);
636 case EAP_CODE_SUCCESS:
637 wpa_printf(MSG_DEBUG, " (success)");
639 case EAP_CODE_FAILURE:
640 wpa_printf(MSG_DEBUG, " (failure)");
643 wpa_printf(MSG_DEBUG, " (unknown code)");
649 /* Process the EAPOL frames from the Supplicant */
650 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
653 struct sta_info *sta;
654 struct ieee802_1x_hdr *hdr;
655 struct ieee802_1x_eapol_key *key;
657 struct rsn_pmksa_cache_entry *pmksa;
659 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
660 !hapd->conf->wps_state)
663 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
664 (unsigned long) len, MAC2STR(sa));
665 sta = ap_get_sta(hapd, sa);
667 printf(" no station information available\n");
671 if (len < sizeof(*hdr)) {
672 printf(" too short IEEE 802.1X packet\n");
676 hdr = (struct ieee802_1x_hdr *) buf;
677 datalen = be_to_host16(hdr->length);
678 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
679 hdr->version, hdr->type, datalen);
681 if (len - sizeof(*hdr) < datalen) {
682 printf(" frame too short for this IEEE 802.1X packet\n");
684 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
687 if (len - sizeof(*hdr) > datalen) {
688 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
689 "IEEE 802.1X packet",
690 (unsigned long) len - sizeof(*hdr) - datalen);
694 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
695 sta->eapol_sm->dot1xAuthEapolFramesRx++;
698 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
699 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
700 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
701 (key->type == EAPOL_KEY_TYPE_WPA ||
702 key->type == EAPOL_KEY_TYPE_RSN)) {
703 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
704 sizeof(*hdr) + datalen);
708 if ((!hapd->conf->ieee802_1x &&
709 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
710 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
713 if (!sta->eapol_sm) {
714 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
715 sta->flags & WLAN_STA_PREAUTH,
721 if (!hapd->conf->ieee802_1x &&
722 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
723 WLAN_STA_MAYBE_WPS)) {
725 * Delay EAPOL frame transmission until a possible WPS
726 * STA initiates the handshake with EAPOL-Start.
728 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
730 #endif /* CONFIG_WPS */
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 accounting_sta_stop(hapd, sta);
770 sta->eapol_sm->eapolLogoff = TRUE;
771 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
774 case IEEE802_1X_TYPE_EAPOL_KEY:
775 wpa_printf(MSG_DEBUG, " EAPOL-Key");
776 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
777 wpa_printf(MSG_DEBUG, " Dropped key data from "
778 "unauthorized Supplicant");
783 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
784 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
785 /* TODO: implement support for this; show data */
789 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
790 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
794 eapol_auth_step(sta->eapol_sm);
798 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
800 struct rsn_pmksa_cache_entry *pmksa;
805 if (hapd->conf->wps_state &&
806 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
808 * Need to enable IEEE 802.1X/EAPOL state machines for possible
809 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
810 * authentication in this BSS.
814 #endif /* CONFIG_WPS */
816 if ((!force_1x && !hapd->conf->ieee802_1x) ||
817 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
820 if (sta->eapol_sm == NULL) {
821 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
822 HOSTAPD_LEVEL_DEBUG, "start authentication");
823 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
824 sta->flags & WLAN_STA_PREAUTH,
826 if (sta->eapol_sm == NULL) {
827 hostapd_logger(hapd, sta->addr,
828 HOSTAPD_MODULE_IEEE8021X,
830 "failed to allocate state machine");
837 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
838 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
840 * Delay EAPOL frame transmission until a possible WPS
841 * initiates the handshake with EAPOL-Start.
843 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
845 #endif /* CONFIG_WPS */
847 sta->eapol_sm->eap_if->portEnabled = TRUE;
849 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
853 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
855 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
856 /* Setup EAPOL state machines to already authenticated state
857 * because of existing PMKSA information in the cache. */
858 sta->eapol_sm->keyRun = TRUE;
859 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
860 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
861 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
862 sta->eapol_sm->authSuccess = TRUE;
863 if (sta->eapol_sm->eap)
864 eap_sm_notify_cached(sta->eapol_sm->eap);
865 old_vlanid = sta->vlan_id;
866 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
867 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
869 ap_sta_bind_vlan(hapd, sta, old_vlanid);
873 * Force EAPOL state machines to start
874 * re-authentication without having to wait for the
875 * Supplicant to send EAPOL-Start.
877 sta->eapol_sm->reAuthenticate = TRUE;
879 eapol_auth_step(sta->eapol_sm);
884 void ieee802_1x_free_radius_class(struct radius_class_data *class)
889 for (i = 0; i < class->count; i++)
890 os_free(class->attr[i].data);
891 os_free(class->attr);
897 int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
898 const struct radius_class_data *src)
902 if (src->attr == NULL)
905 dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
906 if (dst->attr == NULL)
911 for (i = 0; i < src->count; i++) {
912 dst->attr[i].data = os_malloc(src->attr[i].len);
913 if (dst->attr[i].data == NULL)
916 os_memcpy(dst->attr[i].data, src->attr[i].data,
918 dst->attr[i].len = src->attr[i].len;
925 void ieee802_1x_free_station(struct sta_info *sta)
927 struct eapol_state_machine *sm = sta->eapol_sm;
932 sta->eapol_sm = NULL;
934 if (sm->last_recv_radius) {
935 radius_msg_free(sm->last_recv_radius);
936 os_free(sm->last_recv_radius);
939 os_free(sm->identity);
940 ieee802_1x_free_radius_class(&sm->radius_class);
945 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
946 struct sta_info *sta)
953 struct radius_msg *msg;
954 struct eapol_state_machine *sm = sta->eapol_sm;
956 if (sm == NULL || sm->last_recv_radius == NULL) {
958 sm->eap_if->aaaEapNoReq = TRUE;
962 msg = sm->last_recv_radius;
964 eap = radius_msg_get_eap(msg, &len);
966 /* RFC 3579, Chap. 2.6.3:
967 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
969 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
970 HOSTAPD_LEVEL_WARNING, "could not extract "
971 "EAP-Message from RADIUS message");
972 sm->eap_if->aaaEapNoReq = TRUE;
976 if (len < sizeof(*hdr)) {
977 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
978 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
979 "received from authentication server");
981 sm->eap_if->aaaEapNoReq = TRUE;
985 if (len > sizeof(*hdr))
986 eap_type = eap[sizeof(*hdr)];
988 hdr = (struct eap_hdr *) eap;
990 case EAP_CODE_REQUEST:
992 sm->eap_type_authsrv = eap_type;
993 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
994 eap_type >= 0 ? eap_type_text(eap_type) : "??",
997 case EAP_CODE_RESPONSE:
998 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
999 eap_type >= 0 ? eap_type_text(eap_type) : "??",
1002 case EAP_CODE_SUCCESS:
1003 os_strlcpy(buf, "EAP Success", sizeof(buf));
1005 case EAP_CODE_FAILURE:
1006 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1009 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1012 buf[sizeof(buf) - 1] = '\0';
1013 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1014 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1015 "id=%d len=%d) from RADIUS server: %s",
1016 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1018 sm->eap_if->aaaEapReq = TRUE;
1020 wpabuf_free(sm->eap_if->aaaEapReqData);
1021 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1025 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1026 struct sta_info *sta, struct radius_msg *msg,
1027 struct radius_msg *req,
1028 u8 *shared_secret, size_t shared_secret_len)
1030 struct radius_ms_mppe_keys *keys;
1031 struct eapol_state_machine *sm = sta->eapol_sm;
1035 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1038 if (keys && keys->send && keys->recv) {
1039 size_t len = keys->send_len + keys->recv_len;
1040 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1041 keys->send, keys->send_len);
1042 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1043 keys->recv, keys->recv_len);
1045 os_free(sm->eap_if->aaaEapKeyData);
1046 sm->eap_if->aaaEapKeyData = os_malloc(len);
1047 if (sm->eap_if->aaaEapKeyData) {
1048 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1050 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1051 keys->send, keys->send_len);
1052 sm->eap_if->aaaEapKeyDataLen = len;
1053 sm->eap_if->aaaEapKeyAvailable = TRUE;
1058 os_free(keys->send);
1059 os_free(keys->recv);
1065 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1066 struct sta_info *sta,
1067 struct radius_msg *msg)
1071 struct eapol_state_machine *sm = sta->eapol_sm;
1073 struct radius_attr_data *nclass;
1074 size_t nclass_count;
1076 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1080 ieee802_1x_free_radius_class(&sm->radius_class);
1081 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1085 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1092 for (i = 0; i < count; i++) {
1094 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1100 } while (class_len < 1);
1102 nclass[nclass_count].data = os_malloc(class_len);
1103 if (nclass[nclass_count].data == NULL)
1106 os_memcpy(nclass[nclass_count].data, class, class_len);
1107 nclass[nclass_count].len = class_len;
1111 sm->radius_class.attr = nclass;
1112 sm->radius_class.count = nclass_count;
1113 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1114 "attributes for " MACSTR,
1115 (unsigned long) sm->radius_class.count,
1116 MAC2STR(sta->addr));
1120 /* Update sta->identity based on User-Name attribute in Access-Accept */
1121 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1122 struct sta_info *sta,
1123 struct radius_msg *msg)
1127 struct eapol_state_machine *sm = sta->eapol_sm;
1132 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1136 identity = os_malloc(len + 1);
1137 if (identity == NULL)
1140 os_memcpy(identity, buf, len);
1141 identity[len] = '\0';
1143 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1144 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1145 "User-Name from Access-Accept '%s'",
1146 sm->identity ? (char *) sm->identity : "N/A",
1149 os_free(sm->identity);
1150 sm->identity = identity;
1151 sm->identity_len = len;
1155 struct sta_id_search {
1157 struct eapol_state_machine *sm;
1161 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1162 struct sta_info *sta,
1165 struct sta_id_search *id_search = ctx;
1166 struct eapol_state_machine *sm = sta->eapol_sm;
1168 if (sm && sm->radius_identifier >= 0 &&
1169 sm->radius_identifier == id_search->identifier) {
1177 static struct eapol_state_machine *
1178 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1180 struct sta_id_search id_search;
1181 id_search.identifier = identifier;
1182 id_search.sm = NULL;
1183 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1184 return id_search.sm;
1188 /* Process the RADIUS frames from Authentication Server */
1189 static RadiusRxResult
1190 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1191 u8 *shared_secret, size_t shared_secret_len,
1194 struct hostapd_data *hapd = data;
1195 struct sta_info *sta;
1196 u32 session_timeout = 0, termination_action, acct_interim_interval;
1197 int session_timeout_set, old_vlanid = 0;
1198 struct eapol_state_machine *sm;
1199 int override_eapReq = 0;
1201 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1203 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1204 "station for this RADIUS message");
1205 return RADIUS_RX_UNKNOWN;
1209 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1210 * present when packet contains an EAP-Message attribute */
1211 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1212 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1214 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1215 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1216 "Message-Authenticator since it does not include "
1218 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1220 printf("Incoming RADIUS packet did not have correct "
1221 "Message-Authenticator - dropped\n");
1222 return RADIUS_RX_INVALID_AUTHENTICATOR;
1225 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1226 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1227 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1228 printf("Unknown RADIUS message code\n");
1229 return RADIUS_RX_UNKNOWN;
1232 sm->radius_identifier = -1;
1233 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1234 MAC2STR(sta->addr));
1236 if (sm->last_recv_radius) {
1237 radius_msg_free(sm->last_recv_radius);
1238 os_free(sm->last_recv_radius);
1241 sm->last_recv_radius = msg;
1243 session_timeout_set =
1244 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1246 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1247 &termination_action))
1248 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1250 if (hapd->conf->radius->acct_interim_interval == 0 &&
1251 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1252 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1253 &acct_interim_interval) == 0) {
1254 if (acct_interim_interval < 60) {
1255 hostapd_logger(hapd, sta->addr,
1256 HOSTAPD_MODULE_IEEE8021X,
1258 "ignored too small "
1259 "Acct-Interim-Interval %d",
1260 acct_interim_interval);
1262 sta->acct_interim_interval = acct_interim_interval;
1266 switch (msg->hdr->code) {
1267 case RADIUS_CODE_ACCESS_ACCEPT:
1268 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1271 old_vlanid = sta->vlan_id;
1272 sta->vlan_id = radius_msg_get_vlanid(msg);
1274 if (sta->vlan_id > 0 &&
1275 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1277 hostapd_logger(hapd, sta->addr,
1278 HOSTAPD_MODULE_RADIUS,
1280 "VLAN ID %d", sta->vlan_id);
1281 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1282 sta->eapol_sm->authFail = TRUE;
1283 hostapd_logger(hapd, sta->addr,
1284 HOSTAPD_MODULE_IEEE8021X,
1285 HOSTAPD_LEVEL_INFO, "authentication "
1286 "server did not include required VLAN "
1287 "ID in Access-Accept");
1291 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1293 /* RFC 3580, Ch. 3.17 */
1294 if (session_timeout_set && termination_action ==
1295 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1296 sm->reAuthPeriod = session_timeout;
1297 } else if (session_timeout_set)
1298 ap_sta_session_timeout(hapd, sta, session_timeout);
1300 sm->eap_if->aaaSuccess = TRUE;
1301 override_eapReq = 1;
1302 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1304 ieee802_1x_store_radius_class(hapd, sta, msg);
1305 ieee802_1x_update_sta_identity(hapd, sta, msg);
1306 if (sm->eap_if->eapKeyAvailable &&
1307 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1308 session_timeout_set ?
1309 (int) session_timeout : -1, sm) == 0) {
1310 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1311 HOSTAPD_LEVEL_DEBUG,
1312 "Added PMKSA cache entry");
1315 case RADIUS_CODE_ACCESS_REJECT:
1316 sm->eap_if->aaaFail = TRUE;
1317 override_eapReq = 1;
1319 case RADIUS_CODE_ACCESS_CHALLENGE:
1320 sm->eap_if->aaaEapReq = TRUE;
1321 if (session_timeout_set) {
1322 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1323 sm->eap_if->aaaMethodTimeout = session_timeout;
1324 hostapd_logger(hapd, sm->addr,
1325 HOSTAPD_MODULE_IEEE8021X,
1326 HOSTAPD_LEVEL_DEBUG,
1327 "using EAP timeout of %d seconds (from "
1329 sm->eap_if->aaaMethodTimeout);
1332 * Use dynamic retransmission behavior per EAP
1335 sm->eap_if->aaaMethodTimeout = 0;
1340 ieee802_1x_decapsulate_radius(hapd, sta);
1341 if (override_eapReq)
1342 sm->eap_if->aaaEapReq = FALSE;
1344 eapol_auth_step(sm);
1346 return RADIUS_RX_QUEUED;
1350 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1352 struct eapol_state_machine *sm = sta->eapol_sm;
1356 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1357 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1359 if (sm->last_recv_radius) {
1360 radius_msg_free(sm->last_recv_radius);
1361 os_free(sm->last_recv_radius);
1362 sm->last_recv_radius = NULL;
1367 #ifdef HOSTAPD_DUMP_STATE
1368 static void fprint_char(FILE *f, char c)
1370 if (c >= 32 && c < 127)
1371 fprintf(f, "%c", c);
1373 fprintf(f, "<%02x>", c);
1377 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1379 struct eapol_state_machine *sm = sta->eapol_sm;
1383 fprintf(f, "%sIEEE 802.1X:\n", prefix);
1387 fprintf(f, "%sidentity=", prefix);
1388 for (i = 0; i < sm->identity_len; i++)
1389 fprint_char(f, sm->identity[i]);
1393 fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1394 "Supplicant: %d (%s)\n", prefix,
1395 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1396 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1398 fprintf(f, "%scached_packets=%s\n", prefix,
1399 sm->last_recv_radius ? "[RX RADIUS]" : "");
1401 eapol_auth_dump_state(f, prefix, sm);
1403 #endif /* HOSTAPD_DUMP_STATE */
1406 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1408 if (hapd->conf->default_wep_key_len < 1)
1411 os_free(hapd->default_wep_key);
1412 hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1413 if (hapd->default_wep_key == NULL ||
1414 os_get_random(hapd->default_wep_key,
1415 hapd->conf->default_wep_key_len)) {
1416 printf("Could not generate random WEP key.\n");
1417 os_free(hapd->default_wep_key);
1418 hapd->default_wep_key = NULL;
1422 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1423 hapd->default_wep_key,
1424 hapd->conf->default_wep_key_len);
1430 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1431 struct sta_info *sta, void *ctx)
1433 if (sta->eapol_sm) {
1434 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1435 eapol_auth_step(sta->eapol_sm);
1441 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1443 struct hostapd_data *hapd = eloop_ctx;
1445 if (hapd->default_wep_key_idx >= 3)
1446 hapd->default_wep_key_idx =
1447 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1449 hapd->default_wep_key_idx++;
1451 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1452 hapd->default_wep_key_idx);
1454 if (ieee802_1x_rekey_broadcast(hapd)) {
1455 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1456 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1457 "new broadcast key");
1458 os_free(hapd->default_wep_key);
1459 hapd->default_wep_key = NULL;
1463 /* TODO: Could setup key for RX here, but change default TX keyid only
1464 * after new broadcast key has been sent to all stations. */
1465 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1466 hapd->default_wep_key_idx,
1467 hapd->default_wep_key,
1468 hapd->conf->default_wep_key_len, 1)) {
1469 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1470 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1471 "new broadcast key");
1472 os_free(hapd->default_wep_key);
1473 hapd->default_wep_key = NULL;
1477 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1479 if (hapd->conf->wep_rekeying_period > 0) {
1480 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1481 ieee802_1x_rekey, hapd, NULL);
1486 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1487 const u8 *data, size_t datalen)
1489 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1493 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1494 const u8 *data, size_t datalen)
1496 struct hostapd_data *hapd = ctx;
1497 struct sta_info *sta = sta_ctx;
1499 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1503 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1506 struct hostapd_data *hapd = ctx;
1507 struct sta_info *sta = sta_ctx;
1509 rsn_preauth_finished(hapd, sta, success);
1511 ieee802_1x_finished(hapd, sta, success);
1515 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1516 size_t identity_len, int phase2,
1517 struct eap_user *user)
1519 struct hostapd_data *hapd = ctx;
1520 const struct hostapd_eap_user *eap_user;
1523 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1524 identity_len, phase2);
1525 if (eap_user == NULL)
1528 os_memset(user, 0, sizeof(*user));
1529 user->phase2 = phase2;
1530 count = EAP_USER_MAX_METHODS;
1531 if (count > EAP_MAX_METHODS)
1532 count = EAP_MAX_METHODS;
1533 for (i = 0; i < count; i++) {
1534 user->methods[i].vendor = eap_user->methods[i].vendor;
1535 user->methods[i].method = eap_user->methods[i].method;
1538 if (eap_user->password) {
1539 user->password = os_malloc(eap_user->password_len);
1540 if (user->password == NULL)
1542 os_memcpy(user->password, eap_user->password,
1543 eap_user->password_len);
1544 user->password_len = eap_user->password_len;
1546 user->force_version = eap_user->force_version;
1547 user->ttls_auth = eap_user->ttls_auth;
1553 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1555 struct hostapd_data *hapd = ctx;
1556 struct sta_info *sta;
1557 sta = ap_get_sta(hapd, addr);
1558 if (sta == NULL || sta->eapol_sm == NULL)
1564 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1565 eapol_logger_level level, const char *txt)
1567 struct hostapd_data *hapd = ctx;
1571 case EAPOL_LOGGER_WARNING:
1572 hlevel = HOSTAPD_LEVEL_WARNING;
1574 case EAPOL_LOGGER_INFO:
1575 hlevel = HOSTAPD_LEVEL_INFO;
1577 case EAPOL_LOGGER_DEBUG:
1579 hlevel = HOSTAPD_LEVEL_DEBUG;
1583 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1588 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1591 struct hostapd_data *hapd = ctx;
1592 struct sta_info *sta = sta_ctx;
1593 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1597 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1599 struct hostapd_data *hapd = ctx;
1600 struct sta_info *sta = sta_ctx;
1601 ieee802_1x_abort_auth(hapd, sta);
1605 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1607 struct hostapd_data *hapd = ctx;
1608 struct sta_info *sta = sta_ctx;
1609 ieee802_1x_tx_key(hapd, sta);
1613 int ieee802_1x_init(struct hostapd_data *hapd)
1616 struct eapol_auth_config conf;
1617 struct eapol_auth_cb cb;
1619 os_memset(&conf, 0, sizeof(conf));
1621 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1622 conf.wpa = hapd->conf->wpa;
1623 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1624 conf.eap_server = hapd->conf->eap_server;
1625 conf.ssl_ctx = hapd->ssl_ctx;
1626 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1627 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1628 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1629 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1630 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1631 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1632 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1633 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1634 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1635 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1636 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1637 conf.tnc = hapd->conf->tnc;
1638 conf.wps = hapd->wps;
1640 os_memset(&cb, 0, sizeof(cb));
1641 cb.eapol_send = ieee802_1x_eapol_send;
1642 cb.aaa_send = ieee802_1x_aaa_send;
1643 cb.finished = _ieee802_1x_finished;
1644 cb.get_eap_user = ieee802_1x_get_eap_user;
1645 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1646 cb.logger = ieee802_1x_logger;
1647 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1648 cb.abort_auth = _ieee802_1x_abort_auth;
1649 cb.tx_key = _ieee802_1x_tx_key;
1651 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1652 if (hapd->eapol_auth == NULL)
1655 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1656 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1659 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1660 ieee802_1x_receive_auth, hapd))
1663 if (hapd->conf->default_wep_key_len) {
1664 hostapd_set_privacy(hapd, 1);
1666 for (i = 0; i < 4; i++)
1667 hostapd_set_encryption(hapd->conf->iface, hapd,
1668 "none", NULL, i, NULL, 0, 0);
1670 ieee802_1x_rekey(hapd, NULL);
1672 if (hapd->default_wep_key == NULL)
1680 void ieee802_1x_deinit(struct hostapd_data *hapd)
1682 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1684 if (hapd->driver != NULL &&
1685 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1686 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1688 eapol_auth_deinit(hapd->eapol_auth);
1689 hapd->eapol_auth = NULL;
1693 int ieee802_1x_reconfig(struct hostapd_data *hapd,
1694 struct hostapd_config *oldconf,
1695 struct hostapd_bss_config *oldbss)
1697 ieee802_1x_deinit(hapd);
1698 return ieee802_1x_init(hapd);
1702 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1703 u8 *buf, size_t len, int ack)
1705 struct ieee80211_hdr *hdr;
1706 struct ieee802_1x_hdr *xhdr;
1707 struct ieee802_1x_eapol_key *key;
1709 const unsigned char rfc1042_hdr[ETH_ALEN] =
1710 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1714 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1717 hdr = (struct ieee80211_hdr *) buf;
1718 pos = (u8 *) (hdr + 1);
1719 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1721 pos += sizeof(rfc1042_hdr);
1722 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1726 xhdr = (struct ieee802_1x_hdr *) pos;
1727 pos += sizeof(*xhdr);
1729 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1730 "type=%d length=%d - ack=%d",
1731 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1732 be_to_host16(xhdr->length), ack);
1734 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1735 * or Authenticator state machines, but EAPOL-Key packets are not
1736 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1737 * packets couple of times because otherwise STA keys become
1738 * unsynchronized with AP. */
1739 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1740 pos + sizeof(*key) <= buf + len) {
1741 key = (struct ieee802_1x_eapol_key *) pos;
1742 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1743 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1744 "frame (%scast index=%d)",
1745 key->key_index & BIT(7) ? "uni" : "broad",
1746 key->key_index & ~BIT(7));
1747 /* TODO: re-send EAPOL-Key couple of times (with short delay
1748 * between them?). If all attempt fail, report error and
1749 * deauthenticate STA so that it will get new keys when
1750 * authenticating again (e.g., after returning in range).
1751 * Separate limit/transmit state needed both for unicast and
1752 * broadcast keys(?) */
1754 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1755 * to here and change the key only if the EAPOL-Key packet was Acked.
1762 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1764 if (sm == NULL || sm->identity == NULL)
1767 *len = sm->identity_len;
1768 return sm->identity;
1772 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1775 if (sm == NULL || sm->radius_class.attr == NULL ||
1776 idx >= (int) sm->radius_class.count)
1779 *len = sm->radius_class.attr[idx].len;
1780 return sm->radius_class.attr[idx].data;
1784 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1789 *len = sm->eap_if->eapKeyDataLen;
1790 return sm->eap_if->eapKeyData;
1794 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1799 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1800 eapol_auth_step(sm);
1804 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1809 sm->portValid = valid ? TRUE : FALSE;
1810 eapol_auth_step(sm);
1814 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1819 sm->flags |= EAPOL_SM_PREAUTH;
1821 sm->flags &= ~EAPOL_SM_PREAUTH;
1825 static const char * bool_txt(Boolean bool)
1827 return bool ? "TRUE" : "FALSE";
1831 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1838 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1839 char *buf, size_t buflen)
1842 struct eapol_state_machine *sm = sta->eapol_sm;
1847 ret = os_snprintf(buf + len, buflen - len,
1848 "dot1xPaePortNumber=%d\n"
1849 "dot1xPaePortProtocolVersion=%d\n"
1850 "dot1xPaePortCapabilities=1\n"
1851 "dot1xPaePortInitialize=%d\n"
1852 "dot1xPaePortReauthenticate=FALSE\n",
1856 if (ret < 0 || (size_t) ret >= buflen - len)
1860 /* dot1xAuthConfigTable */
1861 ret = os_snprintf(buf + len, buflen - len,
1862 "dot1xAuthPaeState=%d\n"
1863 "dot1xAuthBackendAuthState=%d\n"
1864 "dot1xAuthAdminControlledDirections=%d\n"
1865 "dot1xAuthOperControlledDirections=%d\n"
1866 "dot1xAuthAuthControlledPortStatus=%d\n"
1867 "dot1xAuthAuthControlledPortControl=%d\n"
1868 "dot1xAuthQuietPeriod=%u\n"
1869 "dot1xAuthServerTimeout=%u\n"
1870 "dot1xAuthReAuthPeriod=%u\n"
1871 "dot1xAuthReAuthEnabled=%s\n"
1872 "dot1xAuthKeyTxEnabled=%s\n",
1873 sm->auth_pae_state + 1,
1874 sm->be_auth_state + 1,
1875 sm->adminControlledDirections,
1876 sm->operControlledDirections,
1882 bool_txt(sm->reAuthEnabled),
1883 bool_txt(sm->keyTxEnabled));
1884 if (ret < 0 || (size_t) ret >= buflen - len)
1888 /* dot1xAuthStatsTable */
1889 ret = os_snprintf(buf + len, buflen - len,
1890 "dot1xAuthEapolFramesRx=%u\n"
1891 "dot1xAuthEapolFramesTx=%u\n"
1892 "dot1xAuthEapolStartFramesRx=%u\n"
1893 "dot1xAuthEapolLogoffFramesRx=%u\n"
1894 "dot1xAuthEapolRespIdFramesRx=%u\n"
1895 "dot1xAuthEapolRespFramesRx=%u\n"
1896 "dot1xAuthEapolReqIdFramesTx=%u\n"
1897 "dot1xAuthEapolReqFramesTx=%u\n"
1898 "dot1xAuthInvalidEapolFramesRx=%u\n"
1899 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1900 "dot1xAuthLastEapolFrameVersion=%u\n"
1901 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1902 sm->dot1xAuthEapolFramesRx,
1903 sm->dot1xAuthEapolFramesTx,
1904 sm->dot1xAuthEapolStartFramesRx,
1905 sm->dot1xAuthEapolLogoffFramesRx,
1906 sm->dot1xAuthEapolRespIdFramesRx,
1907 sm->dot1xAuthEapolRespFramesRx,
1908 sm->dot1xAuthEapolReqIdFramesTx,
1909 sm->dot1xAuthEapolReqFramesTx,
1910 sm->dot1xAuthInvalidEapolFramesRx,
1911 sm->dot1xAuthEapLengthErrorFramesRx,
1912 sm->dot1xAuthLastEapolFrameVersion,
1914 if (ret < 0 || (size_t) ret >= buflen - len)
1918 /* dot1xAuthDiagTable */
1919 ret = os_snprintf(buf + len, buflen - len,
1920 "dot1xAuthEntersConnecting=%u\n"
1921 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1922 "dot1xAuthEntersAuthenticating=%u\n"
1923 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1924 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1925 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1926 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1927 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1928 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1929 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1930 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1931 "dot1xAuthBackendResponses=%u\n"
1932 "dot1xAuthBackendAccessChallenges=%u\n"
1933 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1934 "dot1xAuthBackendAuthSuccesses=%u\n"
1935 "dot1xAuthBackendAuthFails=%u\n",
1936 sm->authEntersConnecting,
1937 sm->authEapLogoffsWhileConnecting,
1938 sm->authEntersAuthenticating,
1939 sm->authAuthSuccessesWhileAuthenticating,
1940 sm->authAuthTimeoutsWhileAuthenticating,
1941 sm->authAuthFailWhileAuthenticating,
1942 sm->authAuthEapStartsWhileAuthenticating,
1943 sm->authAuthEapLogoffWhileAuthenticating,
1944 sm->authAuthReauthsWhileAuthenticated,
1945 sm->authAuthEapStartsWhileAuthenticated,
1946 sm->authAuthEapLogoffWhileAuthenticated,
1947 sm->backendResponses,
1948 sm->backendAccessChallenges,
1949 sm->backendOtherRequestsToSupplicant,
1950 sm->backendAuthSuccesses,
1951 sm->backendAuthFails);
1952 if (ret < 0 || (size_t) ret >= buflen - len)
1956 /* dot1xAuthSessionStatsTable */
1957 ret = os_snprintf(buf + len, buflen - len,
1958 /* TODO: dot1xAuthSessionOctetsRx */
1959 /* TODO: dot1xAuthSessionOctetsTx */
1960 /* TODO: dot1xAuthSessionFramesRx */
1961 /* TODO: dot1xAuthSessionFramesTx */
1962 "dot1xAuthSessionId=%08X-%08X\n"
1963 "dot1xAuthSessionAuthenticMethod=%d\n"
1964 "dot1xAuthSessionTime=%u\n"
1965 "dot1xAuthSessionTerminateCause=999\n"
1966 "dot1xAuthSessionUserName=%s\n",
1967 sta->acct_session_id_hi, sta->acct_session_id_lo,
1968 (wpa_key_mgmt_wpa_ieee8021x(
1969 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1971 (unsigned int) (time(NULL) -
1972 sta->acct_session_start),
1974 if (ret < 0 || (size_t) ret >= buflen - len)
1982 static void ieee802_1x_finished(struct hostapd_data *hapd,
1983 struct sta_info *sta, int success)
1987 /* TODO: get PMKLifetime from WPA parameters */
1988 static const int dot11RSNAConfigPMKLifetime = 43200;
1990 key = ieee802_1x_get_key(sta->eapol_sm, &len);
1991 if (success && key && len >= PMK_LEN &&
1992 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
1993 sta->eapol_sm) == 0) {
1994 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1995 HOSTAPD_LEVEL_DEBUG,
1996 "Added PMKSA cache entry (IEEE 802.1X)");