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"
33 #include "ieee802_11_defs.h"
36 static void ieee802_1x_finished(struct hostapd_data *hapd,
37 struct sta_info *sta, int success);
40 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
41 u8 type, const u8 *data, size_t datalen)
44 struct ieee802_1x_hdr *xhdr;
48 len = sizeof(*xhdr) + datalen;
51 wpa_printf(MSG_ERROR, "malloc() failed for "
52 "ieee802_1x_send(len=%lu)",
57 xhdr = (struct ieee802_1x_hdr *) buf;
58 xhdr->version = hapd->conf->eapol_version;
60 xhdr->length = host_to_be16(datalen);
62 if (datalen > 0 && data != NULL)
63 os_memcpy(xhdr + 1, data, datalen);
65 if (wpa_auth_pairwise_set(sta->wpa_sm))
67 if (sta->flags & WLAN_STA_PREAUTH) {
68 rsn_preauth_send(hapd, sta, buf, len);
70 hostapd_send_eapol(hapd, sta->addr, buf, len, encrypt);
77 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
78 struct sta_info *sta, int authorized)
82 if (sta->flags & WLAN_STA_PREAUTH)
86 sta->flags |= WLAN_STA_AUTHORIZED;
87 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
88 WLAN_STA_AUTHORIZED, ~0);
89 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
90 HOSTAPD_LEVEL_DEBUG, "authorizing port");
92 sta->flags &= ~WLAN_STA_AUTHORIZED;
93 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
94 0, ~WLAN_STA_AUTHORIZED);
95 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
96 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
99 if (res && errno != ENOENT) {
100 printf("Could not set station " MACSTR " flags for kernel "
101 "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
105 accounting_sta_start(hapd, sta);
109 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
110 struct sta_info *sta,
111 int idx, int broadcast,
112 u8 *key_data, size_t key_len)
115 struct ieee802_1x_hdr *hdr;
116 struct ieee802_1x_eapol_key *key;
117 size_t len, ekey_len;
118 struct eapol_state_machine *sm = sta->eapol_sm;
123 len = sizeof(*key) + key_len;
124 buf = os_zalloc(sizeof(*hdr) + len);
128 hdr = (struct ieee802_1x_hdr *) buf;
129 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
130 key->type = EAPOL_KEY_TYPE_RC4;
131 key->key_length = htons(key_len);
132 wpa_get_ntp_timestamp(key->replay_counter);
134 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
135 wpa_printf(MSG_ERROR, "Could not get random numbers");
140 key->key_index = idx | (broadcast ? 0 : BIT(7));
141 if (hapd->conf->eapol_key_index_workaround) {
142 /* According to some information, WinXP Supplicant seems to
143 * interpret bit7 as an indication whether the key is to be
144 * activated, so make it possible to enable workaround that
145 * sets this bit for all keys. */
146 key->key_index |= BIT(7);
149 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
150 * MSK[32..63] is used to sign the message. */
151 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
152 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
153 "and signing EAPOL-Key");
157 os_memcpy((u8 *) (key + 1), key_data, key_len);
158 ekey_len = sizeof(key->key_iv) + 32;
159 ekey = os_malloc(ekey_len);
161 wpa_printf(MSG_ERROR, "Could not encrypt key");
165 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
166 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
167 rc4((u8 *) (key + 1), key_len, ekey, ekey_len);
170 /* This header is needed here for HMAC-MD5, but it will be regenerated
171 * in ieee802_1x_send() */
172 hdr->version = hapd->conf->eapol_version;
173 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
174 hdr->length = host_to_be16(len);
175 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
178 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
179 " (%s index=%d)", MAC2STR(sm->addr),
180 broadcast ? "broadcast" : "unicast", idx);
181 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
183 sta->eapol_sm->dot1xAuthEapolFramesTx++;
188 static struct hostapd_wep_keys *
189 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
191 struct hostapd_wep_keys *key;
193 key = os_zalloc(sizeof(*key));
197 key->default_len = hapd->conf->default_wep_key_len;
199 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
200 key->idx < hapd->conf->broadcast_key_idx_min)
201 key->idx = hapd->conf->broadcast_key_idx_min;
205 if (!key->key[key->idx])
206 key->key[key->idx] = os_malloc(key->default_len);
207 if (key->key[key->idx] == NULL ||
208 os_get_random(key->key[key->idx], key->default_len)) {
209 printf("Could not generate random WEP key (dynamic VLAN).\n");
210 os_free(key->key[key->idx]);
211 key->key[key->idx] = NULL;
215 key->len[key->idx] = key->default_len;
217 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
219 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
220 key->key[key->idx], key->len[key->idx]);
222 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL, key->idx,
223 key->key[key->idx], key->len[key->idx], 1))
224 printf("Could not set dynamic VLAN WEP encryption key.\n");
226 hostapd_set_ieee8021x(ifname, hapd, 1);
232 static struct hostapd_wep_keys *
233 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
241 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
242 ssid->dyn_vlan_keys[vlan_id])
243 return ssid->dyn_vlan_keys[vlan_id];
245 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
246 "state machine for VLAN ID %lu",
247 (unsigned long) vlan_id);
249 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
250 if (ifname == NULL) {
251 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
252 "cannot create group key state machine",
253 (unsigned long) vlan_id);
257 if (ssid->dyn_vlan_keys == NULL) {
258 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
259 ssid->dyn_vlan_keys = os_zalloc(size);
260 if (ssid->dyn_vlan_keys == NULL)
262 ssid->max_dyn_vlan_keys = vlan_id;
265 if (ssid->max_dyn_vlan_keys < vlan_id) {
266 struct hostapd_wep_keys **na;
267 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
268 na = os_realloc(ssid->dyn_vlan_keys, size);
271 ssid->dyn_vlan_keys = na;
272 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
273 (vlan_id - ssid->max_dyn_vlan_keys) *
274 sizeof(ssid->dyn_vlan_keys[0]));
275 ssid->max_dyn_vlan_keys = vlan_id;
278 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
280 return ssid->dyn_vlan_keys[vlan_id];
284 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
286 struct hostapd_wep_keys *key = NULL;
287 struct eapol_state_machine *sm = sta->eapol_sm;
290 if (sm == NULL || !sm->eap_if->eapKeyData)
293 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
296 vlan_id = sta->vlan_id;
297 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
301 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
302 if (key && key->key[key->idx])
303 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
306 } else if (hapd->default_wep_key) {
307 ieee802_1x_tx_key_one(hapd, sta, hapd->default_wep_key_idx, 1,
308 hapd->default_wep_key,
309 hapd->conf->default_wep_key_len);
312 if (hapd->conf->individual_wep_key_len > 0) {
314 ikey = os_malloc(hapd->conf->individual_wep_key_len);
316 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
317 wpa_printf(MSG_ERROR, "Could not generate random "
318 "individual WEP key.");
323 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
324 ikey, hapd->conf->individual_wep_key_len);
326 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
327 hapd->conf->individual_wep_key_len);
329 /* TODO: set encryption in TX callback, i.e., only after STA
330 * has ACKed EAPOL-Key frame */
331 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP",
333 hapd->conf->individual_wep_key_len,
335 wpa_printf(MSG_ERROR, "Could not set individual WEP "
344 const char *radius_mode_txt(struct hostapd_data *hapd)
346 if (hapd->iface->current_mode == NULL)
349 switch (hapd->iface->current_mode->mode) {
350 case HOSTAPD_MODE_IEEE80211A:
352 case HOSTAPD_MODE_IEEE80211G:
354 case HOSTAPD_MODE_IEEE80211B:
361 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
366 for (i = 0; i < sta->supported_rates_len; i++)
367 if ((sta->supported_rates[i] & 0x7f) > rate)
368 rate = sta->supported_rates[i] & 0x7f;
374 #ifndef CONFIG_NO_RADIUS
375 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
376 struct eapol_state_machine *sm,
377 const u8 *eap, size_t len)
382 if (len <= sizeof(struct eap_hdr) ||
383 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
386 identity = eap_get_identity(sm->eap, &identity_len);
387 if (identity == NULL)
390 /* Save station identity for future RADIUS packets */
391 os_free(sm->identity);
392 sm->identity = os_malloc(identity_len + 1);
393 if (sm->identity == NULL) {
394 sm->identity_len = 0;
398 os_memcpy(sm->identity, identity, identity_len);
399 sm->identity_len = identity_len;
400 sm->identity[identity_len] = '\0';
401 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
402 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
403 sm->dot1xAuthEapolRespIdFramesRx++;
407 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
408 struct sta_info *sta,
409 const u8 *eap, size_t len)
411 struct radius_msg *msg;
413 struct eapol_state_machine *sm = sta->eapol_sm;
418 ieee802_1x_learn_identity(hapd, sm, eap, len);
420 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
423 sm->radius_identifier = radius_client_get_id(hapd->radius);
424 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
425 sm->radius_identifier);
427 printf("Could not create net RADIUS packet\n");
431 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
434 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
435 sm->identity, sm->identity_len)) {
436 printf("Could not add User-Name\n");
440 if (hapd->conf->own_ip_addr.af == AF_INET &&
441 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
442 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
443 printf("Could not add NAS-IP-Address\n");
448 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
449 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
450 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
451 printf("Could not add NAS-IPv6-Address\n");
454 #endif /* CONFIG_IPV6 */
456 if (hapd->conf->nas_identifier &&
457 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
458 (u8 *) hapd->conf->nas_identifier,
459 os_strlen(hapd->conf->nas_identifier))) {
460 printf("Could not add NAS-Identifier\n");
464 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
465 printf("Could not add NAS-Port\n");
469 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
470 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
471 buf[sizeof(buf) - 1] = '\0';
472 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
473 (u8 *) buf, os_strlen(buf))) {
474 printf("Could not add Called-Station-Id\n");
478 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
480 buf[sizeof(buf) - 1] = '\0';
481 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
482 (u8 *) buf, os_strlen(buf))) {
483 printf("Could not add Calling-Station-Id\n");
487 /* TODO: should probably check MTU from driver config; 2304 is max for
488 * IEEE 802.11, but use 1400 to avoid problems with too large packets
490 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
491 printf("Could not add Framed-MTU\n");
495 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
496 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
497 printf("Could not add NAS-Port-Type\n");
501 if (sta->flags & WLAN_STA_PREAUTH) {
502 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
505 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
506 radius_sta_rate(hapd, sta) / 2,
507 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
508 radius_mode_txt(hapd));
509 buf[sizeof(buf) - 1] = '\0';
511 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
512 (u8 *) buf, os_strlen(buf))) {
513 printf("Could not add Connect-Info\n");
517 if (eap && !radius_msg_add_eap(msg, eap, len)) {
518 printf("Could not add EAP-Message\n");
522 /* State attribute must be copied if and only if this packet is
523 * Access-Request reply to the previous Access-Challenge */
524 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
525 RADIUS_CODE_ACCESS_CHALLENGE) {
526 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
529 printf("Could not copy State attribute from previous "
530 "Access-Challenge\n");
534 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
538 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
542 radius_msg_free(msg);
545 #endif /* CONFIG_NO_RADIUS */
548 char *eap_type_text(u8 type)
551 case EAP_TYPE_IDENTITY: return "Identity";
552 case EAP_TYPE_NOTIFICATION: return "Notification";
553 case EAP_TYPE_NAK: return "Nak";
554 case EAP_TYPE_MD5: return "MD5-Challenge";
555 case EAP_TYPE_OTP: return "One-Time Password";
556 case EAP_TYPE_GTC: return "Generic Token Card";
557 case EAP_TYPE_TLS: return "TLS";
558 case EAP_TYPE_TTLS: return "TTLS";
559 case EAP_TYPE_PEAP: return "PEAP";
560 case EAP_TYPE_SIM: return "SIM";
561 case EAP_TYPE_FAST: return "FAST";
562 case EAP_TYPE_SAKE: return "SAKE";
563 case EAP_TYPE_PSK: return "PSK";
564 case EAP_TYPE_PAX: return "PAX";
565 default: return "Unknown";
570 static void handle_eap_response(struct hostapd_data *hapd,
571 struct sta_info *sta, struct eap_hdr *eap,
575 struct eapol_state_machine *sm = sta->eapol_sm;
579 data = (u8 *) (eap + 1);
581 if (len < sizeof(*eap) + 1) {
582 printf("handle_eap_response: too short response data\n");
586 sm->eap_type_supp = type = data[0];
588 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
589 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
590 "id=%d len=%d) from STA: EAP Response-%s (%d)",
591 eap->code, eap->identifier, be_to_host16(eap->length),
592 eap_type_text(type), type);
594 sm->dot1xAuthEapolRespFramesRx++;
596 wpabuf_free(sm->eap_if->eapRespData);
597 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
602 /* Process incoming EAP packet from Supplicant */
603 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
609 if (len < sizeof(*eap)) {
610 printf(" too short EAP packet\n");
614 eap = (struct eap_hdr *) buf;
616 eap_len = be_to_host16(eap->length);
617 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
618 eap->code, eap->identifier, eap_len);
619 if (eap_len < sizeof(*eap)) {
620 wpa_printf(MSG_DEBUG, " Invalid EAP length");
622 } else if (eap_len > len) {
623 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
626 } else if (eap_len < len) {
627 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
628 "packet", (unsigned long) len - eap_len);
632 case EAP_CODE_REQUEST:
633 wpa_printf(MSG_DEBUG, " (request)");
635 case EAP_CODE_RESPONSE:
636 wpa_printf(MSG_DEBUG, " (response)");
637 handle_eap_response(hapd, sta, eap, eap_len);
639 case EAP_CODE_SUCCESS:
640 wpa_printf(MSG_DEBUG, " (success)");
642 case EAP_CODE_FAILURE:
643 wpa_printf(MSG_DEBUG, " (failure)");
646 wpa_printf(MSG_DEBUG, " (unknown code)");
653 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
654 * @hapd: hostapd BSS data
655 * @sa: Source address (sender of the EAPOL frame)
657 * @len: Length of buf in octets
659 * This function is called for each incoming EAPOL frame from the interface
661 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
664 struct sta_info *sta;
665 struct ieee802_1x_hdr *hdr;
666 struct ieee802_1x_eapol_key *key;
668 struct rsn_pmksa_cache_entry *pmksa;
670 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
671 !hapd->conf->wps_state)
674 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
675 (unsigned long) len, MAC2STR(sa));
676 sta = ap_get_sta(hapd, sa);
677 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
678 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
683 if (len < sizeof(*hdr)) {
684 printf(" too short IEEE 802.1X packet\n");
688 hdr = (struct ieee802_1x_hdr *) buf;
689 datalen = be_to_host16(hdr->length);
690 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
691 hdr->version, hdr->type, datalen);
693 if (len - sizeof(*hdr) < datalen) {
694 printf(" frame too short for this IEEE 802.1X packet\n");
696 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
699 if (len - sizeof(*hdr) > datalen) {
700 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
701 "IEEE 802.1X packet",
702 (unsigned long) len - sizeof(*hdr) - datalen);
706 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
707 sta->eapol_sm->dot1xAuthEapolFramesRx++;
710 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
711 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
712 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
713 (key->type == EAPOL_KEY_TYPE_WPA ||
714 key->type == EAPOL_KEY_TYPE_RSN)) {
715 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
716 sizeof(*hdr) + datalen);
720 if ((!hapd->conf->ieee802_1x &&
721 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
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 if (!hapd->conf->ieee802_1x &&
734 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
735 WLAN_STA_MAYBE_WPS)) {
737 * Delay EAPOL frame transmission until a possible WPS
738 * STA initiates the handshake with EAPOL-Start.
740 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
742 #endif /* CONFIG_WPS */
745 /* since we support version 1, we can ignore version field and proceed
746 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
747 /* TODO: actually, we are not version 1 anymore.. However, Version 2
748 * does not change frame contents, so should be ok to process frames
749 * more or less identically. Some changes might be needed for
750 * verification of fields. */
753 case IEEE802_1X_TYPE_EAP_PACKET:
754 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
757 case IEEE802_1X_TYPE_EAPOL_START:
758 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
759 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
761 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
762 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
764 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
765 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
766 "available - ignore it since "
767 "STA sent EAPOL-Start");
768 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
770 sta->eapol_sm->eapolStart = TRUE;
771 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
772 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
775 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
776 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
777 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
779 sta->acct_terminate_cause =
780 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
781 accounting_sta_stop(hapd, sta);
782 sta->eapol_sm->eapolLogoff = TRUE;
783 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
786 case IEEE802_1X_TYPE_EAPOL_KEY:
787 wpa_printf(MSG_DEBUG, " EAPOL-Key");
788 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
789 wpa_printf(MSG_DEBUG, " Dropped key data from "
790 "unauthorized Supplicant");
795 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
796 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
797 /* TODO: implement support for this; show data */
801 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
802 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
806 eapol_auth_step(sta->eapol_sm);
811 * ieee802_1x_new_station - Start IEEE 802.1X authentication
812 * @hapd: hostapd BSS data
815 * This function is called to start IEEE 802.1X authentication when a new
816 * station completes IEEE 802.11 association.
818 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
820 struct rsn_pmksa_cache_entry *pmksa;
825 if (hapd->conf->wps_state &&
826 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
828 * Need to enable IEEE 802.1X/EAPOL state machines for possible
829 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
830 * authentication in this BSS.
834 #endif /* CONFIG_WPS */
836 if ((!force_1x && !hapd->conf->ieee802_1x) ||
837 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
840 if (sta->eapol_sm == NULL) {
841 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
842 HOSTAPD_LEVEL_DEBUG, "start authentication");
843 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
844 sta->flags & WLAN_STA_PREAUTH,
846 if (sta->eapol_sm == NULL) {
847 hostapd_logger(hapd, sta->addr,
848 HOSTAPD_MODULE_IEEE8021X,
850 "failed to allocate state machine");
857 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
858 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
860 * Delay EAPOL frame transmission until a possible WPS
861 * initiates the handshake with EAPOL-Start.
863 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
865 #endif /* CONFIG_WPS */
867 sta->eapol_sm->eap_if->portEnabled = TRUE;
869 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
873 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
875 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
876 /* Setup EAPOL state machines to already authenticated state
877 * because of existing PMKSA information in the cache. */
878 sta->eapol_sm->keyRun = TRUE;
879 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
880 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
881 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
882 sta->eapol_sm->authSuccess = TRUE;
883 if (sta->eapol_sm->eap)
884 eap_sm_notify_cached(sta->eapol_sm->eap);
885 old_vlanid = sta->vlan_id;
886 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
887 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
889 ap_sta_bind_vlan(hapd, sta, old_vlanid);
893 * Force EAPOL state machines to start
894 * re-authentication without having to wait for the
895 * Supplicant to send EAPOL-Start.
897 sta->eapol_sm->reAuthenticate = TRUE;
899 eapol_auth_step(sta->eapol_sm);
904 void ieee802_1x_free_radius_class(struct radius_class_data *class)
909 for (i = 0; i < class->count; i++)
910 os_free(class->attr[i].data);
911 os_free(class->attr);
917 int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
918 const struct radius_class_data *src)
922 if (src->attr == NULL)
925 dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
926 if (dst->attr == NULL)
931 for (i = 0; i < src->count; i++) {
932 dst->attr[i].data = os_malloc(src->attr[i].len);
933 if (dst->attr[i].data == NULL)
936 os_memcpy(dst->attr[i].data, src->attr[i].data,
938 dst->attr[i].len = src->attr[i].len;
945 void ieee802_1x_free_station(struct sta_info *sta)
947 struct eapol_state_machine *sm = sta->eapol_sm;
952 sta->eapol_sm = NULL;
954 #ifndef CONFIG_NO_RADIUS
955 if (sm->last_recv_radius) {
956 radius_msg_free(sm->last_recv_radius);
957 os_free(sm->last_recv_radius);
959 #endif /* CONFIG_NO_RADIUS */
961 os_free(sm->identity);
962 ieee802_1x_free_radius_class(&sm->radius_class);
967 #ifndef CONFIG_NO_RADIUS
968 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
969 struct sta_info *sta)
976 struct radius_msg *msg;
977 struct eapol_state_machine *sm = sta->eapol_sm;
979 if (sm == NULL || sm->last_recv_radius == NULL) {
981 sm->eap_if->aaaEapNoReq = TRUE;
985 msg = sm->last_recv_radius;
987 eap = radius_msg_get_eap(msg, &len);
989 /* RFC 3579, Chap. 2.6.3:
990 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
992 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
993 HOSTAPD_LEVEL_WARNING, "could not extract "
994 "EAP-Message from RADIUS message");
995 sm->eap_if->aaaEapNoReq = TRUE;
999 if (len < sizeof(*hdr)) {
1000 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1001 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1002 "received from authentication server");
1004 sm->eap_if->aaaEapNoReq = TRUE;
1008 if (len > sizeof(*hdr))
1009 eap_type = eap[sizeof(*hdr)];
1011 hdr = (struct eap_hdr *) eap;
1012 switch (hdr->code) {
1013 case EAP_CODE_REQUEST:
1015 sm->eap_type_authsrv = eap_type;
1016 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1017 eap_type >= 0 ? eap_type_text(eap_type) : "??",
1020 case EAP_CODE_RESPONSE:
1021 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1022 eap_type >= 0 ? eap_type_text(eap_type) : "??",
1025 case EAP_CODE_SUCCESS:
1026 os_strlcpy(buf, "EAP Success", sizeof(buf));
1028 case EAP_CODE_FAILURE:
1029 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1032 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1035 buf[sizeof(buf) - 1] = '\0';
1036 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1037 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1038 "id=%d len=%d) from RADIUS server: %s",
1039 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1041 sm->eap_if->aaaEapReq = TRUE;
1043 wpabuf_free(sm->eap_if->aaaEapReqData);
1044 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1048 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1049 struct sta_info *sta, struct radius_msg *msg,
1050 struct radius_msg *req,
1051 const u8 *shared_secret,
1052 size_t shared_secret_len)
1054 struct radius_ms_mppe_keys *keys;
1055 struct eapol_state_machine *sm = sta->eapol_sm;
1059 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1062 if (keys && keys->send && keys->recv) {
1063 size_t len = keys->send_len + keys->recv_len;
1064 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1065 keys->send, keys->send_len);
1066 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1067 keys->recv, keys->recv_len);
1069 os_free(sm->eap_if->aaaEapKeyData);
1070 sm->eap_if->aaaEapKeyData = os_malloc(len);
1071 if (sm->eap_if->aaaEapKeyData) {
1072 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1074 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1075 keys->send, keys->send_len);
1076 sm->eap_if->aaaEapKeyDataLen = len;
1077 sm->eap_if->aaaEapKeyAvailable = TRUE;
1082 os_free(keys->send);
1083 os_free(keys->recv);
1089 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1090 struct sta_info *sta,
1091 struct radius_msg *msg)
1095 struct eapol_state_machine *sm = sta->eapol_sm;
1097 struct radius_attr_data *nclass;
1098 size_t nclass_count;
1100 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1104 ieee802_1x_free_radius_class(&sm->radius_class);
1105 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1109 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1116 for (i = 0; i < count; i++) {
1118 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1124 } while (class_len < 1);
1126 nclass[nclass_count].data = os_malloc(class_len);
1127 if (nclass[nclass_count].data == NULL)
1130 os_memcpy(nclass[nclass_count].data, class, class_len);
1131 nclass[nclass_count].len = class_len;
1135 sm->radius_class.attr = nclass;
1136 sm->radius_class.count = nclass_count;
1137 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1138 "attributes for " MACSTR,
1139 (unsigned long) sm->radius_class.count,
1140 MAC2STR(sta->addr));
1144 /* Update sta->identity based on User-Name attribute in Access-Accept */
1145 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1146 struct sta_info *sta,
1147 struct radius_msg *msg)
1151 struct eapol_state_machine *sm = sta->eapol_sm;
1156 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1160 identity = os_malloc(len + 1);
1161 if (identity == NULL)
1164 os_memcpy(identity, buf, len);
1165 identity[len] = '\0';
1167 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1168 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1169 "User-Name from Access-Accept '%s'",
1170 sm->identity ? (char *) sm->identity : "N/A",
1173 os_free(sm->identity);
1174 sm->identity = identity;
1175 sm->identity_len = len;
1179 struct sta_id_search {
1181 struct eapol_state_machine *sm;
1185 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1186 struct sta_info *sta,
1189 struct sta_id_search *id_search = ctx;
1190 struct eapol_state_machine *sm = sta->eapol_sm;
1192 if (sm && sm->radius_identifier >= 0 &&
1193 sm->radius_identifier == id_search->identifier) {
1201 static struct eapol_state_machine *
1202 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1204 struct sta_id_search id_search;
1205 id_search.identifier = identifier;
1206 id_search.sm = NULL;
1207 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1208 return id_search.sm;
1213 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1214 * @msg: RADIUS response message
1215 * @req: RADIUS request message
1216 * @shared_secret: RADIUS shared secret
1217 * @shared_secret_len: Length of shared_secret in octets
1218 * @data: Context data (struct hostapd_data *)
1219 * Returns: Processing status
1221 static RadiusRxResult
1222 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1223 const u8 *shared_secret, size_t shared_secret_len,
1226 struct hostapd_data *hapd = data;
1227 struct sta_info *sta;
1228 u32 session_timeout = 0, termination_action, acct_interim_interval;
1229 int session_timeout_set, old_vlanid = 0;
1230 struct eapol_state_machine *sm;
1231 int override_eapReq = 0;
1233 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1235 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1236 "station for this RADIUS message");
1237 return RADIUS_RX_UNKNOWN;
1241 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1242 * present when packet contains an EAP-Message attribute */
1243 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1244 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1246 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1247 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1248 "Message-Authenticator since it does not include "
1250 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1252 printf("Incoming RADIUS packet did not have correct "
1253 "Message-Authenticator - dropped\n");
1254 return RADIUS_RX_INVALID_AUTHENTICATOR;
1257 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1258 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1259 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1260 printf("Unknown RADIUS message code\n");
1261 return RADIUS_RX_UNKNOWN;
1264 sm->radius_identifier = -1;
1265 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1266 MAC2STR(sta->addr));
1268 if (sm->last_recv_radius) {
1269 radius_msg_free(sm->last_recv_radius);
1270 os_free(sm->last_recv_radius);
1273 sm->last_recv_radius = msg;
1275 session_timeout_set =
1276 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1278 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1279 &termination_action))
1280 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1282 if (hapd->conf->radius->acct_interim_interval == 0 &&
1283 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1284 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1285 &acct_interim_interval) == 0) {
1286 if (acct_interim_interval < 60) {
1287 hostapd_logger(hapd, sta->addr,
1288 HOSTAPD_MODULE_IEEE8021X,
1290 "ignored too small "
1291 "Acct-Interim-Interval %d",
1292 acct_interim_interval);
1294 sta->acct_interim_interval = acct_interim_interval;
1298 switch (msg->hdr->code) {
1299 case RADIUS_CODE_ACCESS_ACCEPT:
1300 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1303 old_vlanid = sta->vlan_id;
1304 sta->vlan_id = radius_msg_get_vlanid(msg);
1306 if (sta->vlan_id > 0 &&
1307 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1309 hostapd_logger(hapd, sta->addr,
1310 HOSTAPD_MODULE_RADIUS,
1312 "VLAN ID %d", sta->vlan_id);
1313 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1314 sta->eapol_sm->authFail = TRUE;
1315 hostapd_logger(hapd, sta->addr,
1316 HOSTAPD_MODULE_IEEE8021X,
1317 HOSTAPD_LEVEL_INFO, "authentication "
1318 "server did not include required VLAN "
1319 "ID in Access-Accept");
1323 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1325 /* RFC 3580, Ch. 3.17 */
1326 if (session_timeout_set && termination_action ==
1327 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1328 sm->reAuthPeriod = session_timeout;
1329 } else if (session_timeout_set)
1330 ap_sta_session_timeout(hapd, sta, session_timeout);
1332 sm->eap_if->aaaSuccess = TRUE;
1333 override_eapReq = 1;
1334 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1336 ieee802_1x_store_radius_class(hapd, sta, msg);
1337 ieee802_1x_update_sta_identity(hapd, sta, msg);
1338 if (sm->eap_if->eapKeyAvailable &&
1339 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1340 session_timeout_set ?
1341 (int) session_timeout : -1, sm) == 0) {
1342 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1343 HOSTAPD_LEVEL_DEBUG,
1344 "Added PMKSA cache entry");
1347 case RADIUS_CODE_ACCESS_REJECT:
1348 sm->eap_if->aaaFail = TRUE;
1349 override_eapReq = 1;
1351 case RADIUS_CODE_ACCESS_CHALLENGE:
1352 sm->eap_if->aaaEapReq = TRUE;
1353 if (session_timeout_set) {
1354 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1355 sm->eap_if->aaaMethodTimeout = session_timeout;
1356 hostapd_logger(hapd, sm->addr,
1357 HOSTAPD_MODULE_IEEE8021X,
1358 HOSTAPD_LEVEL_DEBUG,
1359 "using EAP timeout of %d seconds (from "
1361 sm->eap_if->aaaMethodTimeout);
1364 * Use dynamic retransmission behavior per EAP
1367 sm->eap_if->aaaMethodTimeout = 0;
1372 ieee802_1x_decapsulate_radius(hapd, sta);
1373 if (override_eapReq)
1374 sm->eap_if->aaaEapReq = FALSE;
1376 eapol_auth_step(sm);
1378 return RADIUS_RX_QUEUED;
1380 #endif /* CONFIG_NO_RADIUS */
1383 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1385 struct eapol_state_machine *sm = sta->eapol_sm;
1389 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1390 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1392 #ifndef CONFIG_NO_RADIUS
1393 if (sm->last_recv_radius) {
1394 radius_msg_free(sm->last_recv_radius);
1395 os_free(sm->last_recv_radius);
1396 sm->last_recv_radius = NULL;
1398 #endif /* CONFIG_NO_RADIUS */
1400 if (sm->eap_if->eapTimeout) {
1402 * Disconnect the STA since it did not reply to the last EAP
1403 * request and we cannot continue EAP processing (EAP-Failure
1404 * could only be sent if the EAP peer actually replied).
1406 sm->eap_if->portEnabled = FALSE;
1407 hostapd_sta_deauth(hapd, sta->addr,
1408 WLAN_REASON_PREV_AUTH_NOT_VALID);
1409 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1410 WLAN_STA_AUTHORIZED);
1411 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1412 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1413 sta->timeout_next = STA_REMOVE;
1418 #ifdef HOSTAPD_DUMP_STATE
1419 static void fprint_char(FILE *f, char c)
1421 if (c >= 32 && c < 127)
1422 fprintf(f, "%c", c);
1424 fprintf(f, "<%02x>", c);
1428 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1430 struct eapol_state_machine *sm = sta->eapol_sm;
1434 fprintf(f, "%sIEEE 802.1X:\n", prefix);
1438 fprintf(f, "%sidentity=", prefix);
1439 for (i = 0; i < sm->identity_len; i++)
1440 fprint_char(f, sm->identity[i]);
1444 fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1445 "Supplicant: %d (%s)\n", prefix,
1446 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1447 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1449 fprintf(f, "%scached_packets=%s\n", prefix,
1450 sm->last_recv_radius ? "[RX RADIUS]" : "");
1452 eapol_auth_dump_state(f, prefix, sm);
1454 #endif /* HOSTAPD_DUMP_STATE */
1457 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1459 if (hapd->conf->default_wep_key_len < 1)
1462 os_free(hapd->default_wep_key);
1463 hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1464 if (hapd->default_wep_key == NULL ||
1465 os_get_random(hapd->default_wep_key,
1466 hapd->conf->default_wep_key_len)) {
1467 printf("Could not generate random WEP key.\n");
1468 os_free(hapd->default_wep_key);
1469 hapd->default_wep_key = NULL;
1473 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1474 hapd->default_wep_key,
1475 hapd->conf->default_wep_key_len);
1481 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1482 struct sta_info *sta, void *ctx)
1484 if (sta->eapol_sm) {
1485 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1486 eapol_auth_step(sta->eapol_sm);
1492 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1494 struct hostapd_data *hapd = eloop_ctx;
1496 if (hapd->default_wep_key_idx >= 3)
1497 hapd->default_wep_key_idx =
1498 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1500 hapd->default_wep_key_idx++;
1502 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1503 hapd->default_wep_key_idx);
1505 if (ieee802_1x_rekey_broadcast(hapd)) {
1506 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1507 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1508 "new broadcast key");
1509 os_free(hapd->default_wep_key);
1510 hapd->default_wep_key = NULL;
1514 /* TODO: Could setup key for RX here, but change default TX keyid only
1515 * after new broadcast key has been sent to all stations. */
1516 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1517 hapd->default_wep_key_idx,
1518 hapd->default_wep_key,
1519 hapd->conf->default_wep_key_len, 1)) {
1520 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1521 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1522 "new broadcast key");
1523 os_free(hapd->default_wep_key);
1524 hapd->default_wep_key = NULL;
1528 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1530 if (hapd->conf->wep_rekeying_period > 0) {
1531 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1532 ieee802_1x_rekey, hapd, NULL);
1537 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1538 const u8 *data, size_t datalen)
1540 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1544 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1545 const u8 *data, size_t datalen)
1547 #ifndef CONFIG_NO_RADIUS
1548 struct hostapd_data *hapd = ctx;
1549 struct sta_info *sta = sta_ctx;
1551 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1552 #endif /* CONFIG_NO_RADIUS */
1556 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1559 struct hostapd_data *hapd = ctx;
1560 struct sta_info *sta = sta_ctx;
1562 rsn_preauth_finished(hapd, sta, success);
1564 ieee802_1x_finished(hapd, sta, success);
1568 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1569 size_t identity_len, int phase2,
1570 struct eap_user *user)
1572 struct hostapd_data *hapd = ctx;
1573 const struct hostapd_eap_user *eap_user;
1576 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1577 identity_len, phase2);
1578 if (eap_user == NULL)
1581 os_memset(user, 0, sizeof(*user));
1582 user->phase2 = phase2;
1583 count = EAP_USER_MAX_METHODS;
1584 if (count > EAP_MAX_METHODS)
1585 count = EAP_MAX_METHODS;
1586 for (i = 0; i < count; i++) {
1587 user->methods[i].vendor = eap_user->methods[i].vendor;
1588 user->methods[i].method = eap_user->methods[i].method;
1591 if (eap_user->password) {
1592 user->password = os_malloc(eap_user->password_len);
1593 if (user->password == NULL)
1595 os_memcpy(user->password, eap_user->password,
1596 eap_user->password_len);
1597 user->password_len = eap_user->password_len;
1599 user->force_version = eap_user->force_version;
1600 user->ttls_auth = eap_user->ttls_auth;
1606 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1608 struct hostapd_data *hapd = ctx;
1609 struct sta_info *sta;
1610 sta = ap_get_sta(hapd, addr);
1611 if (sta == NULL || sta->eapol_sm == NULL)
1617 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1618 eapol_logger_level level, const char *txt)
1620 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1621 struct hostapd_data *hapd = ctx;
1625 case EAPOL_LOGGER_WARNING:
1626 hlevel = HOSTAPD_LEVEL_WARNING;
1628 case EAPOL_LOGGER_INFO:
1629 hlevel = HOSTAPD_LEVEL_INFO;
1631 case EAPOL_LOGGER_DEBUG:
1633 hlevel = HOSTAPD_LEVEL_DEBUG;
1637 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1639 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1643 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1646 struct hostapd_data *hapd = ctx;
1647 struct sta_info *sta = sta_ctx;
1648 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1652 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1654 struct hostapd_data *hapd = ctx;
1655 struct sta_info *sta = sta_ctx;
1656 ieee802_1x_abort_auth(hapd, sta);
1660 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1662 struct hostapd_data *hapd = ctx;
1663 struct sta_info *sta = sta_ctx;
1664 ieee802_1x_tx_key(hapd, sta);
1668 int ieee802_1x_init(struct hostapd_data *hapd)
1671 struct eapol_auth_config conf;
1672 struct eapol_auth_cb cb;
1674 os_memset(&conf, 0, sizeof(conf));
1676 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1677 conf.wpa = hapd->conf->wpa;
1678 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1679 conf.eap_server = hapd->conf->eap_server;
1680 conf.ssl_ctx = hapd->ssl_ctx;
1681 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1682 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1683 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1684 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1685 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1686 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1687 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1688 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1689 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1690 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1691 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1692 conf.tnc = hapd->conf->tnc;
1693 conf.wps = hapd->wps;
1695 os_memset(&cb, 0, sizeof(cb));
1696 cb.eapol_send = ieee802_1x_eapol_send;
1697 cb.aaa_send = ieee802_1x_aaa_send;
1698 cb.finished = _ieee802_1x_finished;
1699 cb.get_eap_user = ieee802_1x_get_eap_user;
1700 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1701 cb.logger = ieee802_1x_logger;
1702 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1703 cb.abort_auth = _ieee802_1x_abort_auth;
1704 cb.tx_key = _ieee802_1x_tx_key;
1706 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1707 if (hapd->eapol_auth == NULL)
1710 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1711 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1714 #ifndef CONFIG_NO_RADIUS
1715 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1716 ieee802_1x_receive_auth, hapd))
1718 #endif /* CONFIG_NO_RADIUS */
1720 if (hapd->conf->default_wep_key_len) {
1721 hostapd_set_privacy(hapd, 1);
1723 for (i = 0; i < 4; i++)
1724 hostapd_set_encryption(hapd->conf->iface, hapd,
1725 "none", NULL, i, NULL, 0, 0);
1727 ieee802_1x_rekey(hapd, NULL);
1729 if (hapd->default_wep_key == NULL)
1737 void ieee802_1x_deinit(struct hostapd_data *hapd)
1739 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1741 if (hapd->driver != NULL &&
1742 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1743 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1745 eapol_auth_deinit(hapd->eapol_auth);
1746 hapd->eapol_auth = NULL;
1750 int ieee802_1x_reconfig(struct hostapd_data *hapd,
1751 struct hostapd_config *oldconf,
1752 struct hostapd_bss_config *oldbss)
1754 ieee802_1x_deinit(hapd);
1755 return ieee802_1x_init(hapd);
1759 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1760 const u8 *buf, size_t len, int ack)
1762 struct ieee80211_hdr *hdr;
1763 struct ieee802_1x_hdr *xhdr;
1764 struct ieee802_1x_eapol_key *key;
1766 const unsigned char rfc1042_hdr[ETH_ALEN] =
1767 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1771 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1774 hdr = (struct ieee80211_hdr *) buf;
1775 pos = (u8 *) (hdr + 1);
1776 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1778 pos += sizeof(rfc1042_hdr);
1779 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1783 xhdr = (struct ieee802_1x_hdr *) pos;
1784 pos += sizeof(*xhdr);
1786 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1787 "type=%d length=%d - ack=%d",
1788 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1789 be_to_host16(xhdr->length), ack);
1791 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1792 * or Authenticator state machines, but EAPOL-Key packets are not
1793 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1794 * packets couple of times because otherwise STA keys become
1795 * unsynchronized with AP. */
1796 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1797 pos + sizeof(*key) <= buf + len) {
1798 key = (struct ieee802_1x_eapol_key *) pos;
1799 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1800 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1801 "frame (%scast index=%d)",
1802 key->key_index & BIT(7) ? "uni" : "broad",
1803 key->key_index & ~BIT(7));
1804 /* TODO: re-send EAPOL-Key couple of times (with short delay
1805 * between them?). If all attempt fail, report error and
1806 * deauthenticate STA so that it will get new keys when
1807 * authenticating again (e.g., after returning in range).
1808 * Separate limit/transmit state needed both for unicast and
1809 * broadcast keys(?) */
1811 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1812 * to here and change the key only if the EAPOL-Key packet was Acked.
1819 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1821 if (sm == NULL || sm->identity == NULL)
1824 *len = sm->identity_len;
1825 return sm->identity;
1829 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1832 if (sm == NULL || sm->radius_class.attr == NULL ||
1833 idx >= (int) sm->radius_class.count)
1836 *len = sm->radius_class.attr[idx].len;
1837 return sm->radius_class.attr[idx].data;
1841 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1846 *len = sm->eap_if->eapKeyDataLen;
1847 return sm->eap_if->eapKeyData;
1851 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1856 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1857 eapol_auth_step(sm);
1861 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1866 sm->portValid = valid ? TRUE : FALSE;
1867 eapol_auth_step(sm);
1871 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1876 sm->flags |= EAPOL_SM_PREAUTH;
1878 sm->flags &= ~EAPOL_SM_PREAUTH;
1882 static const char * bool_txt(Boolean bool)
1884 return bool ? "TRUE" : "FALSE";
1888 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1895 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1896 char *buf, size_t buflen)
1899 struct eapol_state_machine *sm = sta->eapol_sm;
1904 ret = os_snprintf(buf + len, buflen - len,
1905 "dot1xPaePortNumber=%d\n"
1906 "dot1xPaePortProtocolVersion=%d\n"
1907 "dot1xPaePortCapabilities=1\n"
1908 "dot1xPaePortInitialize=%d\n"
1909 "dot1xPaePortReauthenticate=FALSE\n",
1913 if (ret < 0 || (size_t) ret >= buflen - len)
1917 /* dot1xAuthConfigTable */
1918 ret = os_snprintf(buf + len, buflen - len,
1919 "dot1xAuthPaeState=%d\n"
1920 "dot1xAuthBackendAuthState=%d\n"
1921 "dot1xAuthAdminControlledDirections=%d\n"
1922 "dot1xAuthOperControlledDirections=%d\n"
1923 "dot1xAuthAuthControlledPortStatus=%d\n"
1924 "dot1xAuthAuthControlledPortControl=%d\n"
1925 "dot1xAuthQuietPeriod=%u\n"
1926 "dot1xAuthServerTimeout=%u\n"
1927 "dot1xAuthReAuthPeriod=%u\n"
1928 "dot1xAuthReAuthEnabled=%s\n"
1929 "dot1xAuthKeyTxEnabled=%s\n",
1930 sm->auth_pae_state + 1,
1931 sm->be_auth_state + 1,
1932 sm->adminControlledDirections,
1933 sm->operControlledDirections,
1939 bool_txt(sm->reAuthEnabled),
1940 bool_txt(sm->keyTxEnabled));
1941 if (ret < 0 || (size_t) ret >= buflen - len)
1945 /* dot1xAuthStatsTable */
1946 ret = os_snprintf(buf + len, buflen - len,
1947 "dot1xAuthEapolFramesRx=%u\n"
1948 "dot1xAuthEapolFramesTx=%u\n"
1949 "dot1xAuthEapolStartFramesRx=%u\n"
1950 "dot1xAuthEapolLogoffFramesRx=%u\n"
1951 "dot1xAuthEapolRespIdFramesRx=%u\n"
1952 "dot1xAuthEapolRespFramesRx=%u\n"
1953 "dot1xAuthEapolReqIdFramesTx=%u\n"
1954 "dot1xAuthEapolReqFramesTx=%u\n"
1955 "dot1xAuthInvalidEapolFramesRx=%u\n"
1956 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1957 "dot1xAuthLastEapolFrameVersion=%u\n"
1958 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1959 sm->dot1xAuthEapolFramesRx,
1960 sm->dot1xAuthEapolFramesTx,
1961 sm->dot1xAuthEapolStartFramesRx,
1962 sm->dot1xAuthEapolLogoffFramesRx,
1963 sm->dot1xAuthEapolRespIdFramesRx,
1964 sm->dot1xAuthEapolRespFramesRx,
1965 sm->dot1xAuthEapolReqIdFramesTx,
1966 sm->dot1xAuthEapolReqFramesTx,
1967 sm->dot1xAuthInvalidEapolFramesRx,
1968 sm->dot1xAuthEapLengthErrorFramesRx,
1969 sm->dot1xAuthLastEapolFrameVersion,
1971 if (ret < 0 || (size_t) ret >= buflen - len)
1975 /* dot1xAuthDiagTable */
1976 ret = os_snprintf(buf + len, buflen - len,
1977 "dot1xAuthEntersConnecting=%u\n"
1978 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1979 "dot1xAuthEntersAuthenticating=%u\n"
1980 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1981 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1982 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1983 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1984 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1985 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1986 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1987 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1988 "dot1xAuthBackendResponses=%u\n"
1989 "dot1xAuthBackendAccessChallenges=%u\n"
1990 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1991 "dot1xAuthBackendAuthSuccesses=%u\n"
1992 "dot1xAuthBackendAuthFails=%u\n",
1993 sm->authEntersConnecting,
1994 sm->authEapLogoffsWhileConnecting,
1995 sm->authEntersAuthenticating,
1996 sm->authAuthSuccessesWhileAuthenticating,
1997 sm->authAuthTimeoutsWhileAuthenticating,
1998 sm->authAuthFailWhileAuthenticating,
1999 sm->authAuthEapStartsWhileAuthenticating,
2000 sm->authAuthEapLogoffWhileAuthenticating,
2001 sm->authAuthReauthsWhileAuthenticated,
2002 sm->authAuthEapStartsWhileAuthenticated,
2003 sm->authAuthEapLogoffWhileAuthenticated,
2004 sm->backendResponses,
2005 sm->backendAccessChallenges,
2006 sm->backendOtherRequestsToSupplicant,
2007 sm->backendAuthSuccesses,
2008 sm->backendAuthFails);
2009 if (ret < 0 || (size_t) ret >= buflen - len)
2013 /* dot1xAuthSessionStatsTable */
2014 ret = os_snprintf(buf + len, buflen - len,
2015 /* TODO: dot1xAuthSessionOctetsRx */
2016 /* TODO: dot1xAuthSessionOctetsTx */
2017 /* TODO: dot1xAuthSessionFramesRx */
2018 /* TODO: dot1xAuthSessionFramesTx */
2019 "dot1xAuthSessionId=%08X-%08X\n"
2020 "dot1xAuthSessionAuthenticMethod=%d\n"
2021 "dot1xAuthSessionTime=%u\n"
2022 "dot1xAuthSessionTerminateCause=999\n"
2023 "dot1xAuthSessionUserName=%s\n",
2024 sta->acct_session_id_hi, sta->acct_session_id_lo,
2025 (wpa_key_mgmt_wpa_ieee8021x(
2026 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2028 (unsigned int) (time(NULL) -
2029 sta->acct_session_start),
2031 if (ret < 0 || (size_t) ret >= buflen - len)
2039 static void ieee802_1x_finished(struct hostapd_data *hapd,
2040 struct sta_info *sta, int success)
2044 /* TODO: get PMKLifetime from WPA parameters */
2045 static const int dot11RSNAConfigPMKLifetime = 43200;
2047 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2048 if (success && key && len >= PMK_LEN &&
2049 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2050 sta->eapol_sm) == 0) {
2051 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2052 HOSTAPD_LEVEL_DEBUG,
2053 "Added PMKSA cache entry (IEEE 802.1X)");