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 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
375 struct eapol_state_machine *sm,
376 const u8 *eap, size_t len)
381 if (len <= sizeof(struct eap_hdr) ||
382 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
385 identity = eap_get_identity(sm->eap, &identity_len);
386 if (identity == NULL)
389 /* Save station identity for future RADIUS packets */
390 os_free(sm->identity);
391 sm->identity = os_malloc(identity_len + 1);
392 if (sm->identity == NULL) {
393 sm->identity_len = 0;
397 os_memcpy(sm->identity, identity, identity_len);
398 sm->identity_len = identity_len;
399 sm->identity[identity_len] = '\0';
400 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
401 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
402 sm->dot1xAuthEapolRespIdFramesRx++;
406 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
407 struct sta_info *sta,
408 const u8 *eap, size_t len)
410 struct radius_msg *msg;
412 struct eapol_state_machine *sm = sta->eapol_sm;
417 ieee802_1x_learn_identity(hapd, sm, eap, len);
419 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
422 sm->radius_identifier = radius_client_get_id(hapd->radius);
423 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
424 sm->radius_identifier);
426 printf("Could not create net RADIUS packet\n");
430 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
433 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
434 sm->identity, sm->identity_len)) {
435 printf("Could not add User-Name\n");
439 if (hapd->conf->own_ip_addr.af == AF_INET &&
440 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
441 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
442 printf("Could not add NAS-IP-Address\n");
447 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
448 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
449 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
450 printf("Could not add NAS-IPv6-Address\n");
453 #endif /* CONFIG_IPV6 */
455 if (hapd->conf->nas_identifier &&
456 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
457 (u8 *) hapd->conf->nas_identifier,
458 os_strlen(hapd->conf->nas_identifier))) {
459 printf("Could not add NAS-Identifier\n");
463 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
464 printf("Could not add NAS-Port\n");
468 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
469 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
470 buf[sizeof(buf) - 1] = '\0';
471 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
472 (u8 *) buf, os_strlen(buf))) {
473 printf("Could not add Called-Station-Id\n");
477 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
479 buf[sizeof(buf) - 1] = '\0';
480 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
481 (u8 *) buf, os_strlen(buf))) {
482 printf("Could not add Calling-Station-Id\n");
486 /* TODO: should probably check MTU from driver config; 2304 is max for
487 * IEEE 802.11, but use 1400 to avoid problems with too large packets
489 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
490 printf("Could not add Framed-MTU\n");
494 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
495 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
496 printf("Could not add NAS-Port-Type\n");
500 if (sta->flags & WLAN_STA_PREAUTH) {
501 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
504 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
505 radius_sta_rate(hapd, sta) / 2,
506 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
507 radius_mode_txt(hapd));
508 buf[sizeof(buf) - 1] = '\0';
510 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
511 (u8 *) buf, os_strlen(buf))) {
512 printf("Could not add Connect-Info\n");
516 if (eap && !radius_msg_add_eap(msg, eap, len)) {
517 printf("Could not add EAP-Message\n");
521 /* State attribute must be copied if and only if this packet is
522 * Access-Request reply to the previous Access-Challenge */
523 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
524 RADIUS_CODE_ACCESS_CHALLENGE) {
525 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
528 printf("Could not copy State attribute from previous "
529 "Access-Challenge\n");
533 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
537 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
541 radius_msg_free(msg);
546 char *eap_type_text(u8 type)
549 case EAP_TYPE_IDENTITY: return "Identity";
550 case EAP_TYPE_NOTIFICATION: return "Notification";
551 case EAP_TYPE_NAK: return "Nak";
552 case EAP_TYPE_MD5: return "MD5-Challenge";
553 case EAP_TYPE_OTP: return "One-Time Password";
554 case EAP_TYPE_GTC: return "Generic Token Card";
555 case EAP_TYPE_TLS: return "TLS";
556 case EAP_TYPE_TTLS: return "TTLS";
557 case EAP_TYPE_PEAP: return "PEAP";
558 case EAP_TYPE_SIM: return "SIM";
559 case EAP_TYPE_FAST: return "FAST";
560 case EAP_TYPE_SAKE: return "SAKE";
561 case EAP_TYPE_PSK: return "PSK";
562 case EAP_TYPE_PAX: return "PAX";
563 default: return "Unknown";
568 static void handle_eap_response(struct hostapd_data *hapd,
569 struct sta_info *sta, struct eap_hdr *eap,
573 struct eapol_state_machine *sm = sta->eapol_sm;
577 data = (u8 *) (eap + 1);
579 if (len < sizeof(*eap) + 1) {
580 printf("handle_eap_response: too short response data\n");
584 sm->eap_type_supp = type = data[0];
586 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
587 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
588 "id=%d len=%d) from STA: EAP Response-%s (%d)",
589 eap->code, eap->identifier, be_to_host16(eap->length),
590 eap_type_text(type), type);
592 sm->dot1xAuthEapolRespFramesRx++;
594 wpabuf_free(sm->eap_if->eapRespData);
595 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
600 /* Process incoming EAP packet from Supplicant */
601 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
607 if (len < sizeof(*eap)) {
608 printf(" too short EAP packet\n");
612 eap = (struct eap_hdr *) buf;
614 eap_len = be_to_host16(eap->length);
615 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
616 eap->code, eap->identifier, eap_len);
617 if (eap_len < sizeof(*eap)) {
618 wpa_printf(MSG_DEBUG, " Invalid EAP length");
620 } else if (eap_len > len) {
621 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
624 } else if (eap_len < len) {
625 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
626 "packet", (unsigned long) len - eap_len);
630 case EAP_CODE_REQUEST:
631 wpa_printf(MSG_DEBUG, " (request)");
633 case EAP_CODE_RESPONSE:
634 wpa_printf(MSG_DEBUG, " (response)");
635 handle_eap_response(hapd, sta, eap, eap_len);
637 case EAP_CODE_SUCCESS:
638 wpa_printf(MSG_DEBUG, " (success)");
640 case EAP_CODE_FAILURE:
641 wpa_printf(MSG_DEBUG, " (failure)");
644 wpa_printf(MSG_DEBUG, " (unknown code)");
651 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
652 * @hapd: hostapd BSS data
653 * @sa: Source address (sender of the EAPOL frame)
655 * @len: Length of buf in octets
657 * This function is called for each incoming EAPOL frame from the interface
659 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
662 struct sta_info *sta;
663 struct ieee802_1x_hdr *hdr;
664 struct ieee802_1x_eapol_key *key;
666 struct rsn_pmksa_cache_entry *pmksa;
668 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
669 !hapd->conf->wps_state)
672 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
673 (unsigned long) len, MAC2STR(sa));
674 sta = ap_get_sta(hapd, sa);
676 printf(" no station information available\n");
680 if (len < sizeof(*hdr)) {
681 printf(" too short IEEE 802.1X packet\n");
685 hdr = (struct ieee802_1x_hdr *) buf;
686 datalen = be_to_host16(hdr->length);
687 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
688 hdr->version, hdr->type, datalen);
690 if (len - sizeof(*hdr) < datalen) {
691 printf(" frame too short for this IEEE 802.1X packet\n");
693 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
696 if (len - sizeof(*hdr) > datalen) {
697 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
698 "IEEE 802.1X packet",
699 (unsigned long) len - sizeof(*hdr) - datalen);
703 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
704 sta->eapol_sm->dot1xAuthEapolFramesRx++;
707 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
708 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
709 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
710 (key->type == EAPOL_KEY_TYPE_WPA ||
711 key->type == EAPOL_KEY_TYPE_RSN)) {
712 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
713 sizeof(*hdr) + datalen);
717 if ((!hapd->conf->ieee802_1x &&
718 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
719 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
722 if (!sta->eapol_sm) {
723 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
724 sta->flags & WLAN_STA_PREAUTH,
730 if (!hapd->conf->ieee802_1x &&
731 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
732 WLAN_STA_MAYBE_WPS)) {
734 * Delay EAPOL frame transmission until a possible WPS
735 * STA initiates the handshake with EAPOL-Start.
737 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
739 #endif /* CONFIG_WPS */
742 /* since we support version 1, we can ignore version field and proceed
743 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
744 /* TODO: actually, we are not version 1 anymore.. However, Version 2
745 * does not change frame contents, so should be ok to process frames
746 * more or less identically. Some changes might be needed for
747 * verification of fields. */
750 case IEEE802_1X_TYPE_EAP_PACKET:
751 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
754 case IEEE802_1X_TYPE_EAPOL_START:
755 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
756 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
758 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
759 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
761 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
762 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
763 "available - ignore it since "
764 "STA sent EAPOL-Start");
765 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
767 sta->eapol_sm->eapolStart = TRUE;
768 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
769 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
772 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
773 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
774 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
776 sta->acct_terminate_cause =
777 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
778 accounting_sta_stop(hapd, sta);
779 sta->eapol_sm->eapolLogoff = TRUE;
780 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
783 case IEEE802_1X_TYPE_EAPOL_KEY:
784 wpa_printf(MSG_DEBUG, " EAPOL-Key");
785 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
786 wpa_printf(MSG_DEBUG, " Dropped key data from "
787 "unauthorized Supplicant");
792 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
793 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
794 /* TODO: implement support for this; show data */
798 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
799 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
803 eapol_auth_step(sta->eapol_sm);
808 * ieee802_1x_new_station - Start IEEE 802.1X authentication
809 * @hapd: hostapd BSS data
812 * This function is called to start IEEE 802.1X authentication when a new
813 * station completes IEEE 802.11 association.
815 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
817 struct rsn_pmksa_cache_entry *pmksa;
822 if (hapd->conf->wps_state &&
823 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
825 * Need to enable IEEE 802.1X/EAPOL state machines for possible
826 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
827 * authentication in this BSS.
831 #endif /* CONFIG_WPS */
833 if ((!force_1x && !hapd->conf->ieee802_1x) ||
834 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
837 if (sta->eapol_sm == NULL) {
838 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
839 HOSTAPD_LEVEL_DEBUG, "start authentication");
840 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
841 sta->flags & WLAN_STA_PREAUTH,
843 if (sta->eapol_sm == NULL) {
844 hostapd_logger(hapd, sta->addr,
845 HOSTAPD_MODULE_IEEE8021X,
847 "failed to allocate state machine");
854 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
855 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
857 * Delay EAPOL frame transmission until a possible WPS
858 * initiates the handshake with EAPOL-Start.
860 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
862 #endif /* CONFIG_WPS */
864 sta->eapol_sm->eap_if->portEnabled = TRUE;
866 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
870 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
872 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
873 /* Setup EAPOL state machines to already authenticated state
874 * because of existing PMKSA information in the cache. */
875 sta->eapol_sm->keyRun = TRUE;
876 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
877 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
878 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
879 sta->eapol_sm->authSuccess = TRUE;
880 if (sta->eapol_sm->eap)
881 eap_sm_notify_cached(sta->eapol_sm->eap);
882 old_vlanid = sta->vlan_id;
883 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
884 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
886 ap_sta_bind_vlan(hapd, sta, old_vlanid);
890 * Force EAPOL state machines to start
891 * re-authentication without having to wait for the
892 * Supplicant to send EAPOL-Start.
894 sta->eapol_sm->reAuthenticate = TRUE;
896 eapol_auth_step(sta->eapol_sm);
901 void ieee802_1x_free_radius_class(struct radius_class_data *class)
906 for (i = 0; i < class->count; i++)
907 os_free(class->attr[i].data);
908 os_free(class->attr);
914 int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
915 const struct radius_class_data *src)
919 if (src->attr == NULL)
922 dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
923 if (dst->attr == NULL)
928 for (i = 0; i < src->count; i++) {
929 dst->attr[i].data = os_malloc(src->attr[i].len);
930 if (dst->attr[i].data == NULL)
933 os_memcpy(dst->attr[i].data, src->attr[i].data,
935 dst->attr[i].len = src->attr[i].len;
942 void ieee802_1x_free_station(struct sta_info *sta)
944 struct eapol_state_machine *sm = sta->eapol_sm;
949 sta->eapol_sm = NULL;
951 if (sm->last_recv_radius) {
952 radius_msg_free(sm->last_recv_radius);
953 os_free(sm->last_recv_radius);
956 os_free(sm->identity);
957 ieee802_1x_free_radius_class(&sm->radius_class);
962 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
963 struct sta_info *sta)
970 struct radius_msg *msg;
971 struct eapol_state_machine *sm = sta->eapol_sm;
973 if (sm == NULL || sm->last_recv_radius == NULL) {
975 sm->eap_if->aaaEapNoReq = TRUE;
979 msg = sm->last_recv_radius;
981 eap = radius_msg_get_eap(msg, &len);
983 /* RFC 3579, Chap. 2.6.3:
984 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
986 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
987 HOSTAPD_LEVEL_WARNING, "could not extract "
988 "EAP-Message from RADIUS message");
989 sm->eap_if->aaaEapNoReq = TRUE;
993 if (len < sizeof(*hdr)) {
994 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
995 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
996 "received from authentication server");
998 sm->eap_if->aaaEapNoReq = TRUE;
1002 if (len > sizeof(*hdr))
1003 eap_type = eap[sizeof(*hdr)];
1005 hdr = (struct eap_hdr *) eap;
1006 switch (hdr->code) {
1007 case EAP_CODE_REQUEST:
1009 sm->eap_type_authsrv = eap_type;
1010 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1011 eap_type >= 0 ? eap_type_text(eap_type) : "??",
1014 case EAP_CODE_RESPONSE:
1015 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1016 eap_type >= 0 ? eap_type_text(eap_type) : "??",
1019 case EAP_CODE_SUCCESS:
1020 os_strlcpy(buf, "EAP Success", sizeof(buf));
1022 case EAP_CODE_FAILURE:
1023 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1026 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1029 buf[sizeof(buf) - 1] = '\0';
1030 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1031 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1032 "id=%d len=%d) from RADIUS server: %s",
1033 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1035 sm->eap_if->aaaEapReq = TRUE;
1037 wpabuf_free(sm->eap_if->aaaEapReqData);
1038 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1042 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1043 struct sta_info *sta, struct radius_msg *msg,
1044 struct radius_msg *req,
1045 const u8 *shared_secret,
1046 size_t shared_secret_len)
1048 struct radius_ms_mppe_keys *keys;
1049 struct eapol_state_machine *sm = sta->eapol_sm;
1053 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1056 if (keys && keys->send && keys->recv) {
1057 size_t len = keys->send_len + keys->recv_len;
1058 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1059 keys->send, keys->send_len);
1060 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1061 keys->recv, keys->recv_len);
1063 os_free(sm->eap_if->aaaEapKeyData);
1064 sm->eap_if->aaaEapKeyData = os_malloc(len);
1065 if (sm->eap_if->aaaEapKeyData) {
1066 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1068 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1069 keys->send, keys->send_len);
1070 sm->eap_if->aaaEapKeyDataLen = len;
1071 sm->eap_if->aaaEapKeyAvailable = TRUE;
1076 os_free(keys->send);
1077 os_free(keys->recv);
1083 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1084 struct sta_info *sta,
1085 struct radius_msg *msg)
1089 struct eapol_state_machine *sm = sta->eapol_sm;
1091 struct radius_attr_data *nclass;
1092 size_t nclass_count;
1094 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1098 ieee802_1x_free_radius_class(&sm->radius_class);
1099 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1103 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1110 for (i = 0; i < count; i++) {
1112 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1118 } while (class_len < 1);
1120 nclass[nclass_count].data = os_malloc(class_len);
1121 if (nclass[nclass_count].data == NULL)
1124 os_memcpy(nclass[nclass_count].data, class, class_len);
1125 nclass[nclass_count].len = class_len;
1129 sm->radius_class.attr = nclass;
1130 sm->radius_class.count = nclass_count;
1131 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1132 "attributes for " MACSTR,
1133 (unsigned long) sm->radius_class.count,
1134 MAC2STR(sta->addr));
1138 /* Update sta->identity based on User-Name attribute in Access-Accept */
1139 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1140 struct sta_info *sta,
1141 struct radius_msg *msg)
1145 struct eapol_state_machine *sm = sta->eapol_sm;
1150 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1154 identity = os_malloc(len + 1);
1155 if (identity == NULL)
1158 os_memcpy(identity, buf, len);
1159 identity[len] = '\0';
1161 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1162 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1163 "User-Name from Access-Accept '%s'",
1164 sm->identity ? (char *) sm->identity : "N/A",
1167 os_free(sm->identity);
1168 sm->identity = identity;
1169 sm->identity_len = len;
1173 struct sta_id_search {
1175 struct eapol_state_machine *sm;
1179 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1180 struct sta_info *sta,
1183 struct sta_id_search *id_search = ctx;
1184 struct eapol_state_machine *sm = sta->eapol_sm;
1186 if (sm && sm->radius_identifier >= 0 &&
1187 sm->radius_identifier == id_search->identifier) {
1195 static struct eapol_state_machine *
1196 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1198 struct sta_id_search id_search;
1199 id_search.identifier = identifier;
1200 id_search.sm = NULL;
1201 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1202 return id_search.sm;
1207 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1208 * @msg: RADIUS response message
1209 * @req: RADIUS request message
1210 * @shared_secret: RADIUS shared secret
1211 * @shared_secret_len: Length of shared_secret in octets
1212 * @data: Context data (struct hostapd_data *)
1213 * Returns: Processing status
1215 static RadiusRxResult
1216 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1217 const u8 *shared_secret, size_t shared_secret_len,
1220 struct hostapd_data *hapd = data;
1221 struct sta_info *sta;
1222 u32 session_timeout = 0, termination_action, acct_interim_interval;
1223 int session_timeout_set, old_vlanid = 0;
1224 struct eapol_state_machine *sm;
1225 int override_eapReq = 0;
1227 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1229 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1230 "station for this RADIUS message");
1231 return RADIUS_RX_UNKNOWN;
1235 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1236 * present when packet contains an EAP-Message attribute */
1237 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1238 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1240 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1241 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1242 "Message-Authenticator since it does not include "
1244 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1246 printf("Incoming RADIUS packet did not have correct "
1247 "Message-Authenticator - dropped\n");
1248 return RADIUS_RX_INVALID_AUTHENTICATOR;
1251 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1252 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1253 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1254 printf("Unknown RADIUS message code\n");
1255 return RADIUS_RX_UNKNOWN;
1258 sm->radius_identifier = -1;
1259 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1260 MAC2STR(sta->addr));
1262 if (sm->last_recv_radius) {
1263 radius_msg_free(sm->last_recv_radius);
1264 os_free(sm->last_recv_radius);
1267 sm->last_recv_radius = msg;
1269 session_timeout_set =
1270 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1272 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1273 &termination_action))
1274 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1276 if (hapd->conf->radius->acct_interim_interval == 0 &&
1277 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1278 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1279 &acct_interim_interval) == 0) {
1280 if (acct_interim_interval < 60) {
1281 hostapd_logger(hapd, sta->addr,
1282 HOSTAPD_MODULE_IEEE8021X,
1284 "ignored too small "
1285 "Acct-Interim-Interval %d",
1286 acct_interim_interval);
1288 sta->acct_interim_interval = acct_interim_interval;
1292 switch (msg->hdr->code) {
1293 case RADIUS_CODE_ACCESS_ACCEPT:
1294 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1297 old_vlanid = sta->vlan_id;
1298 sta->vlan_id = radius_msg_get_vlanid(msg);
1300 if (sta->vlan_id > 0 &&
1301 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1303 hostapd_logger(hapd, sta->addr,
1304 HOSTAPD_MODULE_RADIUS,
1306 "VLAN ID %d", sta->vlan_id);
1307 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1308 sta->eapol_sm->authFail = TRUE;
1309 hostapd_logger(hapd, sta->addr,
1310 HOSTAPD_MODULE_IEEE8021X,
1311 HOSTAPD_LEVEL_INFO, "authentication "
1312 "server did not include required VLAN "
1313 "ID in Access-Accept");
1317 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1319 /* RFC 3580, Ch. 3.17 */
1320 if (session_timeout_set && termination_action ==
1321 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1322 sm->reAuthPeriod = session_timeout;
1323 } else if (session_timeout_set)
1324 ap_sta_session_timeout(hapd, sta, session_timeout);
1326 sm->eap_if->aaaSuccess = TRUE;
1327 override_eapReq = 1;
1328 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1330 ieee802_1x_store_radius_class(hapd, sta, msg);
1331 ieee802_1x_update_sta_identity(hapd, sta, msg);
1332 if (sm->eap_if->eapKeyAvailable &&
1333 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1334 session_timeout_set ?
1335 (int) session_timeout : -1, sm) == 0) {
1336 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1337 HOSTAPD_LEVEL_DEBUG,
1338 "Added PMKSA cache entry");
1341 case RADIUS_CODE_ACCESS_REJECT:
1342 sm->eap_if->aaaFail = TRUE;
1343 override_eapReq = 1;
1345 case RADIUS_CODE_ACCESS_CHALLENGE:
1346 sm->eap_if->aaaEapReq = TRUE;
1347 if (session_timeout_set) {
1348 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1349 sm->eap_if->aaaMethodTimeout = session_timeout;
1350 hostapd_logger(hapd, sm->addr,
1351 HOSTAPD_MODULE_IEEE8021X,
1352 HOSTAPD_LEVEL_DEBUG,
1353 "using EAP timeout of %d seconds (from "
1355 sm->eap_if->aaaMethodTimeout);
1358 * Use dynamic retransmission behavior per EAP
1361 sm->eap_if->aaaMethodTimeout = 0;
1366 ieee802_1x_decapsulate_radius(hapd, sta);
1367 if (override_eapReq)
1368 sm->eap_if->aaaEapReq = FALSE;
1370 eapol_auth_step(sm);
1372 return RADIUS_RX_QUEUED;
1376 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1378 struct eapol_state_machine *sm = sta->eapol_sm;
1382 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1383 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1385 if (sm->last_recv_radius) {
1386 radius_msg_free(sm->last_recv_radius);
1387 os_free(sm->last_recv_radius);
1388 sm->last_recv_radius = NULL;
1391 if (sm->eap_if->eapTimeout) {
1393 * Disconnect the STA since it did not reply to the last EAP
1394 * request and we cannot continue EAP processing (EAP-Failure
1395 * could only be sent if the EAP peer actually replied).
1397 sm->eap_if->portEnabled = FALSE;
1398 hostapd_sta_deauth(hapd, sta->addr,
1399 WLAN_REASON_PREV_AUTH_NOT_VALID);
1400 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1401 WLAN_STA_AUTHORIZED);
1402 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1403 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1404 sta->timeout_next = STA_REMOVE;
1409 #ifdef HOSTAPD_DUMP_STATE
1410 static void fprint_char(FILE *f, char c)
1412 if (c >= 32 && c < 127)
1413 fprintf(f, "%c", c);
1415 fprintf(f, "<%02x>", c);
1419 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1421 struct eapol_state_machine *sm = sta->eapol_sm;
1425 fprintf(f, "%sIEEE 802.1X:\n", prefix);
1429 fprintf(f, "%sidentity=", prefix);
1430 for (i = 0; i < sm->identity_len; i++)
1431 fprint_char(f, sm->identity[i]);
1435 fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1436 "Supplicant: %d (%s)\n", prefix,
1437 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1438 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1440 fprintf(f, "%scached_packets=%s\n", prefix,
1441 sm->last_recv_radius ? "[RX RADIUS]" : "");
1443 eapol_auth_dump_state(f, prefix, sm);
1445 #endif /* HOSTAPD_DUMP_STATE */
1448 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1450 if (hapd->conf->default_wep_key_len < 1)
1453 os_free(hapd->default_wep_key);
1454 hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1455 if (hapd->default_wep_key == NULL ||
1456 os_get_random(hapd->default_wep_key,
1457 hapd->conf->default_wep_key_len)) {
1458 printf("Could not generate random WEP key.\n");
1459 os_free(hapd->default_wep_key);
1460 hapd->default_wep_key = NULL;
1464 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1465 hapd->default_wep_key,
1466 hapd->conf->default_wep_key_len);
1472 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1473 struct sta_info *sta, void *ctx)
1475 if (sta->eapol_sm) {
1476 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1477 eapol_auth_step(sta->eapol_sm);
1483 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1485 struct hostapd_data *hapd = eloop_ctx;
1487 if (hapd->default_wep_key_idx >= 3)
1488 hapd->default_wep_key_idx =
1489 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1491 hapd->default_wep_key_idx++;
1493 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1494 hapd->default_wep_key_idx);
1496 if (ieee802_1x_rekey_broadcast(hapd)) {
1497 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1498 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1499 "new broadcast key");
1500 os_free(hapd->default_wep_key);
1501 hapd->default_wep_key = NULL;
1505 /* TODO: Could setup key for RX here, but change default TX keyid only
1506 * after new broadcast key has been sent to all stations. */
1507 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1508 hapd->default_wep_key_idx,
1509 hapd->default_wep_key,
1510 hapd->conf->default_wep_key_len, 1)) {
1511 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1512 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1513 "new broadcast key");
1514 os_free(hapd->default_wep_key);
1515 hapd->default_wep_key = NULL;
1519 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1521 if (hapd->conf->wep_rekeying_period > 0) {
1522 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1523 ieee802_1x_rekey, hapd, NULL);
1528 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1529 const u8 *data, size_t datalen)
1531 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1535 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1536 const u8 *data, size_t datalen)
1538 struct hostapd_data *hapd = ctx;
1539 struct sta_info *sta = sta_ctx;
1541 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1545 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1548 struct hostapd_data *hapd = ctx;
1549 struct sta_info *sta = sta_ctx;
1551 rsn_preauth_finished(hapd, sta, success);
1553 ieee802_1x_finished(hapd, sta, success);
1557 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1558 size_t identity_len, int phase2,
1559 struct eap_user *user)
1561 struct hostapd_data *hapd = ctx;
1562 const struct hostapd_eap_user *eap_user;
1565 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1566 identity_len, phase2);
1567 if (eap_user == NULL)
1570 os_memset(user, 0, sizeof(*user));
1571 user->phase2 = phase2;
1572 count = EAP_USER_MAX_METHODS;
1573 if (count > EAP_MAX_METHODS)
1574 count = EAP_MAX_METHODS;
1575 for (i = 0; i < count; i++) {
1576 user->methods[i].vendor = eap_user->methods[i].vendor;
1577 user->methods[i].method = eap_user->methods[i].method;
1580 if (eap_user->password) {
1581 user->password = os_malloc(eap_user->password_len);
1582 if (user->password == NULL)
1584 os_memcpy(user->password, eap_user->password,
1585 eap_user->password_len);
1586 user->password_len = eap_user->password_len;
1588 user->force_version = eap_user->force_version;
1589 user->ttls_auth = eap_user->ttls_auth;
1595 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1597 struct hostapd_data *hapd = ctx;
1598 struct sta_info *sta;
1599 sta = ap_get_sta(hapd, addr);
1600 if (sta == NULL || sta->eapol_sm == NULL)
1606 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1607 eapol_logger_level level, const char *txt)
1609 struct hostapd_data *hapd = ctx;
1613 case EAPOL_LOGGER_WARNING:
1614 hlevel = HOSTAPD_LEVEL_WARNING;
1616 case EAPOL_LOGGER_INFO:
1617 hlevel = HOSTAPD_LEVEL_INFO;
1619 case EAPOL_LOGGER_DEBUG:
1621 hlevel = HOSTAPD_LEVEL_DEBUG;
1625 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1630 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1633 struct hostapd_data *hapd = ctx;
1634 struct sta_info *sta = sta_ctx;
1635 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1639 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1641 struct hostapd_data *hapd = ctx;
1642 struct sta_info *sta = sta_ctx;
1643 ieee802_1x_abort_auth(hapd, sta);
1647 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1649 struct hostapd_data *hapd = ctx;
1650 struct sta_info *sta = sta_ctx;
1651 ieee802_1x_tx_key(hapd, sta);
1655 int ieee802_1x_init(struct hostapd_data *hapd)
1658 struct eapol_auth_config conf;
1659 struct eapol_auth_cb cb;
1661 os_memset(&conf, 0, sizeof(conf));
1663 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1664 conf.wpa = hapd->conf->wpa;
1665 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1666 conf.eap_server = hapd->conf->eap_server;
1667 conf.ssl_ctx = hapd->ssl_ctx;
1668 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1669 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1670 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1671 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1672 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1673 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1674 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1675 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1676 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1677 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1678 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1679 conf.tnc = hapd->conf->tnc;
1680 conf.wps = hapd->wps;
1682 os_memset(&cb, 0, sizeof(cb));
1683 cb.eapol_send = ieee802_1x_eapol_send;
1684 cb.aaa_send = ieee802_1x_aaa_send;
1685 cb.finished = _ieee802_1x_finished;
1686 cb.get_eap_user = ieee802_1x_get_eap_user;
1687 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1688 cb.logger = ieee802_1x_logger;
1689 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1690 cb.abort_auth = _ieee802_1x_abort_auth;
1691 cb.tx_key = _ieee802_1x_tx_key;
1693 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1694 if (hapd->eapol_auth == NULL)
1697 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1698 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1701 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1702 ieee802_1x_receive_auth, hapd))
1705 if (hapd->conf->default_wep_key_len) {
1706 hostapd_set_privacy(hapd, 1);
1708 for (i = 0; i < 4; i++)
1709 hostapd_set_encryption(hapd->conf->iface, hapd,
1710 "none", NULL, i, NULL, 0, 0);
1712 ieee802_1x_rekey(hapd, NULL);
1714 if (hapd->default_wep_key == NULL)
1722 void ieee802_1x_deinit(struct hostapd_data *hapd)
1724 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1726 if (hapd->driver != NULL &&
1727 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1728 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1730 eapol_auth_deinit(hapd->eapol_auth);
1731 hapd->eapol_auth = NULL;
1735 int ieee802_1x_reconfig(struct hostapd_data *hapd,
1736 struct hostapd_config *oldconf,
1737 struct hostapd_bss_config *oldbss)
1739 ieee802_1x_deinit(hapd);
1740 return ieee802_1x_init(hapd);
1744 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1745 u8 *buf, size_t len, int ack)
1747 struct ieee80211_hdr *hdr;
1748 struct ieee802_1x_hdr *xhdr;
1749 struct ieee802_1x_eapol_key *key;
1751 const unsigned char rfc1042_hdr[ETH_ALEN] =
1752 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1756 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1759 hdr = (struct ieee80211_hdr *) buf;
1760 pos = (u8 *) (hdr + 1);
1761 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1763 pos += sizeof(rfc1042_hdr);
1764 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1768 xhdr = (struct ieee802_1x_hdr *) pos;
1769 pos += sizeof(*xhdr);
1771 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1772 "type=%d length=%d - ack=%d",
1773 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1774 be_to_host16(xhdr->length), ack);
1776 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1777 * or Authenticator state machines, but EAPOL-Key packets are not
1778 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1779 * packets couple of times because otherwise STA keys become
1780 * unsynchronized with AP. */
1781 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1782 pos + sizeof(*key) <= buf + len) {
1783 key = (struct ieee802_1x_eapol_key *) pos;
1784 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1785 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1786 "frame (%scast index=%d)",
1787 key->key_index & BIT(7) ? "uni" : "broad",
1788 key->key_index & ~BIT(7));
1789 /* TODO: re-send EAPOL-Key couple of times (with short delay
1790 * between them?). If all attempt fail, report error and
1791 * deauthenticate STA so that it will get new keys when
1792 * authenticating again (e.g., after returning in range).
1793 * Separate limit/transmit state needed both for unicast and
1794 * broadcast keys(?) */
1796 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1797 * to here and change the key only if the EAPOL-Key packet was Acked.
1804 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1806 if (sm == NULL || sm->identity == NULL)
1809 *len = sm->identity_len;
1810 return sm->identity;
1814 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1817 if (sm == NULL || sm->radius_class.attr == NULL ||
1818 idx >= (int) sm->radius_class.count)
1821 *len = sm->radius_class.attr[idx].len;
1822 return sm->radius_class.attr[idx].data;
1826 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1831 *len = sm->eap_if->eapKeyDataLen;
1832 return sm->eap_if->eapKeyData;
1836 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1841 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1842 eapol_auth_step(sm);
1846 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1851 sm->portValid = valid ? TRUE : FALSE;
1852 eapol_auth_step(sm);
1856 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1861 sm->flags |= EAPOL_SM_PREAUTH;
1863 sm->flags &= ~EAPOL_SM_PREAUTH;
1867 static const char * bool_txt(Boolean bool)
1869 return bool ? "TRUE" : "FALSE";
1873 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1880 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1881 char *buf, size_t buflen)
1884 struct eapol_state_machine *sm = sta->eapol_sm;
1889 ret = os_snprintf(buf + len, buflen - len,
1890 "dot1xPaePortNumber=%d\n"
1891 "dot1xPaePortProtocolVersion=%d\n"
1892 "dot1xPaePortCapabilities=1\n"
1893 "dot1xPaePortInitialize=%d\n"
1894 "dot1xPaePortReauthenticate=FALSE\n",
1898 if (ret < 0 || (size_t) ret >= buflen - len)
1902 /* dot1xAuthConfigTable */
1903 ret = os_snprintf(buf + len, buflen - len,
1904 "dot1xAuthPaeState=%d\n"
1905 "dot1xAuthBackendAuthState=%d\n"
1906 "dot1xAuthAdminControlledDirections=%d\n"
1907 "dot1xAuthOperControlledDirections=%d\n"
1908 "dot1xAuthAuthControlledPortStatus=%d\n"
1909 "dot1xAuthAuthControlledPortControl=%d\n"
1910 "dot1xAuthQuietPeriod=%u\n"
1911 "dot1xAuthServerTimeout=%u\n"
1912 "dot1xAuthReAuthPeriod=%u\n"
1913 "dot1xAuthReAuthEnabled=%s\n"
1914 "dot1xAuthKeyTxEnabled=%s\n",
1915 sm->auth_pae_state + 1,
1916 sm->be_auth_state + 1,
1917 sm->adminControlledDirections,
1918 sm->operControlledDirections,
1924 bool_txt(sm->reAuthEnabled),
1925 bool_txt(sm->keyTxEnabled));
1926 if (ret < 0 || (size_t) ret >= buflen - len)
1930 /* dot1xAuthStatsTable */
1931 ret = os_snprintf(buf + len, buflen - len,
1932 "dot1xAuthEapolFramesRx=%u\n"
1933 "dot1xAuthEapolFramesTx=%u\n"
1934 "dot1xAuthEapolStartFramesRx=%u\n"
1935 "dot1xAuthEapolLogoffFramesRx=%u\n"
1936 "dot1xAuthEapolRespIdFramesRx=%u\n"
1937 "dot1xAuthEapolRespFramesRx=%u\n"
1938 "dot1xAuthEapolReqIdFramesTx=%u\n"
1939 "dot1xAuthEapolReqFramesTx=%u\n"
1940 "dot1xAuthInvalidEapolFramesRx=%u\n"
1941 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1942 "dot1xAuthLastEapolFrameVersion=%u\n"
1943 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1944 sm->dot1xAuthEapolFramesRx,
1945 sm->dot1xAuthEapolFramesTx,
1946 sm->dot1xAuthEapolStartFramesRx,
1947 sm->dot1xAuthEapolLogoffFramesRx,
1948 sm->dot1xAuthEapolRespIdFramesRx,
1949 sm->dot1xAuthEapolRespFramesRx,
1950 sm->dot1xAuthEapolReqIdFramesTx,
1951 sm->dot1xAuthEapolReqFramesTx,
1952 sm->dot1xAuthInvalidEapolFramesRx,
1953 sm->dot1xAuthEapLengthErrorFramesRx,
1954 sm->dot1xAuthLastEapolFrameVersion,
1956 if (ret < 0 || (size_t) ret >= buflen - len)
1960 /* dot1xAuthDiagTable */
1961 ret = os_snprintf(buf + len, buflen - len,
1962 "dot1xAuthEntersConnecting=%u\n"
1963 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1964 "dot1xAuthEntersAuthenticating=%u\n"
1965 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1966 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1967 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1968 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1969 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1970 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1971 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1972 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1973 "dot1xAuthBackendResponses=%u\n"
1974 "dot1xAuthBackendAccessChallenges=%u\n"
1975 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1976 "dot1xAuthBackendAuthSuccesses=%u\n"
1977 "dot1xAuthBackendAuthFails=%u\n",
1978 sm->authEntersConnecting,
1979 sm->authEapLogoffsWhileConnecting,
1980 sm->authEntersAuthenticating,
1981 sm->authAuthSuccessesWhileAuthenticating,
1982 sm->authAuthTimeoutsWhileAuthenticating,
1983 sm->authAuthFailWhileAuthenticating,
1984 sm->authAuthEapStartsWhileAuthenticating,
1985 sm->authAuthEapLogoffWhileAuthenticating,
1986 sm->authAuthReauthsWhileAuthenticated,
1987 sm->authAuthEapStartsWhileAuthenticated,
1988 sm->authAuthEapLogoffWhileAuthenticated,
1989 sm->backendResponses,
1990 sm->backendAccessChallenges,
1991 sm->backendOtherRequestsToSupplicant,
1992 sm->backendAuthSuccesses,
1993 sm->backendAuthFails);
1994 if (ret < 0 || (size_t) ret >= buflen - len)
1998 /* dot1xAuthSessionStatsTable */
1999 ret = os_snprintf(buf + len, buflen - len,
2000 /* TODO: dot1xAuthSessionOctetsRx */
2001 /* TODO: dot1xAuthSessionOctetsTx */
2002 /* TODO: dot1xAuthSessionFramesRx */
2003 /* TODO: dot1xAuthSessionFramesTx */
2004 "dot1xAuthSessionId=%08X-%08X\n"
2005 "dot1xAuthSessionAuthenticMethod=%d\n"
2006 "dot1xAuthSessionTime=%u\n"
2007 "dot1xAuthSessionTerminateCause=999\n"
2008 "dot1xAuthSessionUserName=%s\n",
2009 sta->acct_session_id_hi, sta->acct_session_id_lo,
2010 (wpa_key_mgmt_wpa_ieee8021x(
2011 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2013 (unsigned int) (time(NULL) -
2014 sta->acct_session_start),
2016 if (ret < 0 || (size_t) ret >= buflen - len)
2024 static void ieee802_1x_finished(struct hostapd_data *hapd,
2025 struct sta_info *sta, int success)
2029 /* TODO: get PMKLifetime from WPA parameters */
2030 static const int dot11RSNAConfigPMKLifetime = 43200;
2032 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2033 if (success && key && len >= PMK_LEN &&
2034 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2035 sta->eapol_sm) == 0) {
2036 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2037 HOSTAPD_LEVEL_DEBUG,
2038 "Added PMKSA cache entry (IEEE 802.1X)");