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 #ifndef CONFIG_NO_VLAN
189 static struct hostapd_wep_keys *
190 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
192 struct hostapd_wep_keys *key;
194 key = os_zalloc(sizeof(*key));
198 key->default_len = hapd->conf->default_wep_key_len;
200 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
201 key->idx < hapd->conf->broadcast_key_idx_min)
202 key->idx = hapd->conf->broadcast_key_idx_min;
206 if (!key->key[key->idx])
207 key->key[key->idx] = os_malloc(key->default_len);
208 if (key->key[key->idx] == NULL ||
209 os_get_random(key->key[key->idx], key->default_len)) {
210 printf("Could not generate random WEP key (dynamic VLAN).\n");
211 os_free(key->key[key->idx]);
212 key->key[key->idx] = NULL;
216 key->len[key->idx] = key->default_len;
218 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
220 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
221 key->key[key->idx], key->len[key->idx]);
223 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL, key->idx,
224 key->key[key->idx], key->len[key->idx], 1))
225 printf("Could not set dynamic VLAN WEP encryption key.\n");
227 hostapd_set_ieee8021x(ifname, hapd, 1);
233 static struct hostapd_wep_keys *
234 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
242 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
243 ssid->dyn_vlan_keys[vlan_id])
244 return ssid->dyn_vlan_keys[vlan_id];
246 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
247 "state machine for VLAN ID %lu",
248 (unsigned long) vlan_id);
250 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
251 if (ifname == NULL) {
252 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
253 "cannot create group key state machine",
254 (unsigned long) vlan_id);
258 if (ssid->dyn_vlan_keys == NULL) {
259 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
260 ssid->dyn_vlan_keys = os_zalloc(size);
261 if (ssid->dyn_vlan_keys == NULL)
263 ssid->max_dyn_vlan_keys = vlan_id;
266 if (ssid->max_dyn_vlan_keys < vlan_id) {
267 struct hostapd_wep_keys **na;
268 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
269 na = os_realloc(ssid->dyn_vlan_keys, size);
272 ssid->dyn_vlan_keys = na;
273 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
274 (vlan_id - ssid->max_dyn_vlan_keys) *
275 sizeof(ssid->dyn_vlan_keys[0]));
276 ssid->max_dyn_vlan_keys = vlan_id;
279 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
281 return ssid->dyn_vlan_keys[vlan_id];
283 #endif /* CONFIG_NO_VLAN */
286 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
288 struct eapol_state_machine *sm = sta->eapol_sm;
289 #ifndef CONFIG_NO_VLAN
290 struct hostapd_wep_keys *key = NULL;
292 #endif /* CONFIG_NO_VLAN */
294 if (sm == NULL || !sm->eap_if->eapKeyData)
297 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
300 #ifndef CONFIG_NO_VLAN
301 vlan_id = sta->vlan_id;
302 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
306 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
307 if (key && key->key[key->idx])
308 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
312 #endif /* CONFIG_NO_VLAN */
313 if (hapd->default_wep_key) {
314 ieee802_1x_tx_key_one(hapd, sta, hapd->default_wep_key_idx, 1,
315 hapd->default_wep_key,
316 hapd->conf->default_wep_key_len);
319 if (hapd->conf->individual_wep_key_len > 0) {
321 ikey = os_malloc(hapd->conf->individual_wep_key_len);
323 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
324 wpa_printf(MSG_ERROR, "Could not generate random "
325 "individual WEP key.");
330 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
331 ikey, hapd->conf->individual_wep_key_len);
333 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
334 hapd->conf->individual_wep_key_len);
336 /* TODO: set encryption in TX callback, i.e., only after STA
337 * has ACKed EAPOL-Key frame */
338 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP",
340 hapd->conf->individual_wep_key_len,
342 wpa_printf(MSG_ERROR, "Could not set individual WEP "
351 const char *radius_mode_txt(struct hostapd_data *hapd)
353 if (hapd->iface->current_mode == NULL)
356 switch (hapd->iface->current_mode->mode) {
357 case HOSTAPD_MODE_IEEE80211A:
359 case HOSTAPD_MODE_IEEE80211G:
361 case HOSTAPD_MODE_IEEE80211B:
368 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
373 for (i = 0; i < sta->supported_rates_len; i++)
374 if ((sta->supported_rates[i] & 0x7f) > rate)
375 rate = sta->supported_rates[i] & 0x7f;
381 #ifndef CONFIG_NO_RADIUS
382 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
383 struct eapol_state_machine *sm,
384 const u8 *eap, size_t len)
389 if (len <= sizeof(struct eap_hdr) ||
390 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
393 identity = eap_get_identity(sm->eap, &identity_len);
394 if (identity == NULL)
397 /* Save station identity for future RADIUS packets */
398 os_free(sm->identity);
399 sm->identity = os_malloc(identity_len + 1);
400 if (sm->identity == NULL) {
401 sm->identity_len = 0;
405 os_memcpy(sm->identity, identity, identity_len);
406 sm->identity_len = identity_len;
407 sm->identity[identity_len] = '\0';
408 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
409 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
410 sm->dot1xAuthEapolRespIdFramesRx++;
414 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
415 struct sta_info *sta,
416 const u8 *eap, size_t len)
418 struct radius_msg *msg;
420 struct eapol_state_machine *sm = sta->eapol_sm;
425 ieee802_1x_learn_identity(hapd, sm, eap, len);
427 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
430 sm->radius_identifier = radius_client_get_id(hapd->radius);
431 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
432 sm->radius_identifier);
434 printf("Could not create net RADIUS packet\n");
438 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
441 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
442 sm->identity, sm->identity_len)) {
443 printf("Could not add User-Name\n");
447 if (hapd->conf->own_ip_addr.af == AF_INET &&
448 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
449 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
450 printf("Could not add NAS-IP-Address\n");
455 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
456 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
457 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
458 printf("Could not add NAS-IPv6-Address\n");
461 #endif /* CONFIG_IPV6 */
463 if (hapd->conf->nas_identifier &&
464 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
465 (u8 *) hapd->conf->nas_identifier,
466 os_strlen(hapd->conf->nas_identifier))) {
467 printf("Could not add NAS-Identifier\n");
471 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
472 printf("Could not add NAS-Port\n");
476 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
477 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
478 buf[sizeof(buf) - 1] = '\0';
479 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
480 (u8 *) buf, os_strlen(buf))) {
481 printf("Could not add Called-Station-Id\n");
485 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
487 buf[sizeof(buf) - 1] = '\0';
488 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
489 (u8 *) buf, os_strlen(buf))) {
490 printf("Could not add Calling-Station-Id\n");
494 /* TODO: should probably check MTU from driver config; 2304 is max for
495 * IEEE 802.11, but use 1400 to avoid problems with too large packets
497 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
498 printf("Could not add Framed-MTU\n");
502 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
503 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
504 printf("Could not add NAS-Port-Type\n");
508 if (sta->flags & WLAN_STA_PREAUTH) {
509 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
512 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
513 radius_sta_rate(hapd, sta) / 2,
514 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
515 radius_mode_txt(hapd));
516 buf[sizeof(buf) - 1] = '\0';
518 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
519 (u8 *) buf, os_strlen(buf))) {
520 printf("Could not add Connect-Info\n");
524 if (eap && !radius_msg_add_eap(msg, eap, len)) {
525 printf("Could not add EAP-Message\n");
529 /* State attribute must be copied if and only if this packet is
530 * Access-Request reply to the previous Access-Challenge */
531 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
532 RADIUS_CODE_ACCESS_CHALLENGE) {
533 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
536 printf("Could not copy State attribute from previous "
537 "Access-Challenge\n");
541 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
545 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
549 radius_msg_free(msg);
552 #endif /* CONFIG_NO_RADIUS */
555 char *eap_type_text(u8 type)
558 case EAP_TYPE_IDENTITY: return "Identity";
559 case EAP_TYPE_NOTIFICATION: return "Notification";
560 case EAP_TYPE_NAK: return "Nak";
561 case EAP_TYPE_MD5: return "MD5-Challenge";
562 case EAP_TYPE_OTP: return "One-Time Password";
563 case EAP_TYPE_GTC: return "Generic Token Card";
564 case EAP_TYPE_TLS: return "TLS";
565 case EAP_TYPE_TTLS: return "TTLS";
566 case EAP_TYPE_PEAP: return "PEAP";
567 case EAP_TYPE_SIM: return "SIM";
568 case EAP_TYPE_FAST: return "FAST";
569 case EAP_TYPE_SAKE: return "SAKE";
570 case EAP_TYPE_PSK: return "PSK";
571 case EAP_TYPE_PAX: return "PAX";
572 default: return "Unknown";
577 static void handle_eap_response(struct hostapd_data *hapd,
578 struct sta_info *sta, struct eap_hdr *eap,
582 struct eapol_state_machine *sm = sta->eapol_sm;
586 data = (u8 *) (eap + 1);
588 if (len < sizeof(*eap) + 1) {
589 printf("handle_eap_response: too short response data\n");
593 sm->eap_type_supp = type = data[0];
595 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
596 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
597 "id=%d len=%d) from STA: EAP Response-%s (%d)",
598 eap->code, eap->identifier, be_to_host16(eap->length),
599 eap_type_text(type), type);
601 sm->dot1xAuthEapolRespFramesRx++;
603 wpabuf_free(sm->eap_if->eapRespData);
604 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
609 /* Process incoming EAP packet from Supplicant */
610 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
616 if (len < sizeof(*eap)) {
617 printf(" too short EAP packet\n");
621 eap = (struct eap_hdr *) buf;
623 eap_len = be_to_host16(eap->length);
624 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
625 eap->code, eap->identifier, eap_len);
626 if (eap_len < sizeof(*eap)) {
627 wpa_printf(MSG_DEBUG, " Invalid EAP length");
629 } else if (eap_len > len) {
630 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
633 } else if (eap_len < len) {
634 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
635 "packet", (unsigned long) len - eap_len);
639 case EAP_CODE_REQUEST:
640 wpa_printf(MSG_DEBUG, " (request)");
642 case EAP_CODE_RESPONSE:
643 wpa_printf(MSG_DEBUG, " (response)");
644 handle_eap_response(hapd, sta, eap, eap_len);
646 case EAP_CODE_SUCCESS:
647 wpa_printf(MSG_DEBUG, " (success)");
649 case EAP_CODE_FAILURE:
650 wpa_printf(MSG_DEBUG, " (failure)");
653 wpa_printf(MSG_DEBUG, " (unknown code)");
660 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
661 * @hapd: hostapd BSS data
662 * @sa: Source address (sender of the EAPOL frame)
664 * @len: Length of buf in octets
666 * This function is called for each incoming EAPOL frame from the interface
668 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
671 struct sta_info *sta;
672 struct ieee802_1x_hdr *hdr;
673 struct ieee802_1x_eapol_key *key;
675 struct rsn_pmksa_cache_entry *pmksa;
677 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
678 !hapd->conf->wps_state)
681 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
682 (unsigned long) len, MAC2STR(sa));
683 sta = ap_get_sta(hapd, sa);
684 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
685 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
690 if (len < sizeof(*hdr)) {
691 printf(" too short IEEE 802.1X packet\n");
695 hdr = (struct ieee802_1x_hdr *) buf;
696 datalen = be_to_host16(hdr->length);
697 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
698 hdr->version, hdr->type, datalen);
700 if (len - sizeof(*hdr) < datalen) {
701 printf(" frame too short for this IEEE 802.1X packet\n");
703 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
706 if (len - sizeof(*hdr) > datalen) {
707 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
708 "IEEE 802.1X packet",
709 (unsigned long) len - sizeof(*hdr) - datalen);
713 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
714 sta->eapol_sm->dot1xAuthEapolFramesRx++;
717 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
718 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
719 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
720 (key->type == EAPOL_KEY_TYPE_WPA ||
721 key->type == EAPOL_KEY_TYPE_RSN)) {
722 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
723 sizeof(*hdr) + datalen);
727 if ((!hapd->conf->ieee802_1x &&
728 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
729 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
732 if (!sta->eapol_sm) {
733 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
734 sta->flags & WLAN_STA_PREAUTH,
740 if (!hapd->conf->ieee802_1x &&
741 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
742 WLAN_STA_MAYBE_WPS)) {
744 * Delay EAPOL frame transmission until a possible WPS
745 * STA initiates the handshake with EAPOL-Start.
747 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
749 #endif /* CONFIG_WPS */
752 /* since we support version 1, we can ignore version field and proceed
753 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
754 /* TODO: actually, we are not version 1 anymore.. However, Version 2
755 * does not change frame contents, so should be ok to process frames
756 * more or less identically. Some changes might be needed for
757 * verification of fields. */
760 case IEEE802_1X_TYPE_EAP_PACKET:
761 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
764 case IEEE802_1X_TYPE_EAPOL_START:
765 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
766 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
768 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
769 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
771 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
772 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
773 "available - ignore it since "
774 "STA sent EAPOL-Start");
775 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
777 sta->eapol_sm->eapolStart = TRUE;
778 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
779 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
782 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
783 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
784 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
786 sta->acct_terminate_cause =
787 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
788 accounting_sta_stop(hapd, sta);
789 sta->eapol_sm->eapolLogoff = TRUE;
790 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
793 case IEEE802_1X_TYPE_EAPOL_KEY:
794 wpa_printf(MSG_DEBUG, " EAPOL-Key");
795 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
796 wpa_printf(MSG_DEBUG, " Dropped key data from "
797 "unauthorized Supplicant");
802 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
803 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
804 /* TODO: implement support for this; show data */
808 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
809 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
813 eapol_auth_step(sta->eapol_sm);
818 * ieee802_1x_new_station - Start IEEE 802.1X authentication
819 * @hapd: hostapd BSS data
822 * This function is called to start IEEE 802.1X authentication when a new
823 * station completes IEEE 802.11 association.
825 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
827 struct rsn_pmksa_cache_entry *pmksa;
832 if (hapd->conf->wps_state &&
833 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
835 * Need to enable IEEE 802.1X/EAPOL state machines for possible
836 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
837 * authentication in this BSS.
841 #endif /* CONFIG_WPS */
843 if ((!force_1x && !hapd->conf->ieee802_1x) ||
844 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
847 if (sta->eapol_sm == NULL) {
848 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
849 HOSTAPD_LEVEL_DEBUG, "start authentication");
850 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
851 sta->flags & WLAN_STA_PREAUTH,
853 if (sta->eapol_sm == NULL) {
854 hostapd_logger(hapd, sta->addr,
855 HOSTAPD_MODULE_IEEE8021X,
857 "failed to allocate state machine");
864 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
865 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
867 * Delay EAPOL frame transmission until a possible WPS
868 * initiates the handshake with EAPOL-Start.
870 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
872 #endif /* CONFIG_WPS */
874 sta->eapol_sm->eap_if->portEnabled = TRUE;
876 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
880 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
882 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
883 /* Setup EAPOL state machines to already authenticated state
884 * because of existing PMKSA information in the cache. */
885 sta->eapol_sm->keyRun = TRUE;
886 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
887 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
888 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
889 sta->eapol_sm->authSuccess = TRUE;
890 if (sta->eapol_sm->eap)
891 eap_sm_notify_cached(sta->eapol_sm->eap);
892 old_vlanid = sta->vlan_id;
893 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
894 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
896 ap_sta_bind_vlan(hapd, sta, old_vlanid);
900 * Force EAPOL state machines to start
901 * re-authentication without having to wait for the
902 * Supplicant to send EAPOL-Start.
904 sta->eapol_sm->reAuthenticate = TRUE;
906 eapol_auth_step(sta->eapol_sm);
911 void ieee802_1x_free_station(struct sta_info *sta)
913 struct eapol_state_machine *sm = sta->eapol_sm;
918 sta->eapol_sm = NULL;
920 #ifndef CONFIG_NO_RADIUS
921 if (sm->last_recv_radius) {
922 radius_msg_free(sm->last_recv_radius);
923 os_free(sm->last_recv_radius);
925 radius_free_class(&sm->radius_class);
926 #endif /* CONFIG_NO_RADIUS */
928 os_free(sm->identity);
933 #ifndef CONFIG_NO_RADIUS
934 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
935 struct sta_info *sta)
942 struct radius_msg *msg;
943 struct eapol_state_machine *sm = sta->eapol_sm;
945 if (sm == NULL || sm->last_recv_radius == NULL) {
947 sm->eap_if->aaaEapNoReq = TRUE;
951 msg = sm->last_recv_radius;
953 eap = radius_msg_get_eap(msg, &len);
955 /* RFC 3579, Chap. 2.6.3:
956 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
958 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
959 HOSTAPD_LEVEL_WARNING, "could not extract "
960 "EAP-Message from RADIUS message");
961 sm->eap_if->aaaEapNoReq = TRUE;
965 if (len < sizeof(*hdr)) {
966 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
967 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
968 "received from authentication server");
970 sm->eap_if->aaaEapNoReq = TRUE;
974 if (len > sizeof(*hdr))
975 eap_type = eap[sizeof(*hdr)];
977 hdr = (struct eap_hdr *) eap;
979 case EAP_CODE_REQUEST:
981 sm->eap_type_authsrv = eap_type;
982 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
983 eap_type >= 0 ? eap_type_text(eap_type) : "??",
986 case EAP_CODE_RESPONSE:
987 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
988 eap_type >= 0 ? eap_type_text(eap_type) : "??",
991 case EAP_CODE_SUCCESS:
992 os_strlcpy(buf, "EAP Success", sizeof(buf));
994 case EAP_CODE_FAILURE:
995 os_strlcpy(buf, "EAP Failure", sizeof(buf));
998 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1001 buf[sizeof(buf) - 1] = '\0';
1002 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1003 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1004 "id=%d len=%d) from RADIUS server: %s",
1005 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1007 sm->eap_if->aaaEapReq = TRUE;
1009 wpabuf_free(sm->eap_if->aaaEapReqData);
1010 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1014 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1015 struct sta_info *sta, struct radius_msg *msg,
1016 struct radius_msg *req,
1017 const u8 *shared_secret,
1018 size_t shared_secret_len)
1020 struct radius_ms_mppe_keys *keys;
1021 struct eapol_state_machine *sm = sta->eapol_sm;
1025 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1028 if (keys && keys->send && keys->recv) {
1029 size_t len = keys->send_len + keys->recv_len;
1030 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1031 keys->send, keys->send_len);
1032 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1033 keys->recv, keys->recv_len);
1035 os_free(sm->eap_if->aaaEapKeyData);
1036 sm->eap_if->aaaEapKeyData = os_malloc(len);
1037 if (sm->eap_if->aaaEapKeyData) {
1038 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1040 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1041 keys->send, keys->send_len);
1042 sm->eap_if->aaaEapKeyDataLen = len;
1043 sm->eap_if->aaaEapKeyAvailable = TRUE;
1048 os_free(keys->send);
1049 os_free(keys->recv);
1055 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1056 struct sta_info *sta,
1057 struct radius_msg *msg)
1061 struct eapol_state_machine *sm = sta->eapol_sm;
1063 struct radius_attr_data *nclass;
1064 size_t nclass_count;
1066 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1070 radius_free_class(&sm->radius_class);
1071 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1075 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1082 for (i = 0; i < count; i++) {
1084 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1090 } while (class_len < 1);
1092 nclass[nclass_count].data = os_malloc(class_len);
1093 if (nclass[nclass_count].data == NULL)
1096 os_memcpy(nclass[nclass_count].data, class, class_len);
1097 nclass[nclass_count].len = class_len;
1101 sm->radius_class.attr = nclass;
1102 sm->radius_class.count = nclass_count;
1103 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1104 "attributes for " MACSTR,
1105 (unsigned long) sm->radius_class.count,
1106 MAC2STR(sta->addr));
1110 /* Update sta->identity based on User-Name attribute in Access-Accept */
1111 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1112 struct sta_info *sta,
1113 struct radius_msg *msg)
1117 struct eapol_state_machine *sm = sta->eapol_sm;
1122 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1126 identity = os_malloc(len + 1);
1127 if (identity == NULL)
1130 os_memcpy(identity, buf, len);
1131 identity[len] = '\0';
1133 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1134 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1135 "User-Name from Access-Accept '%s'",
1136 sm->identity ? (char *) sm->identity : "N/A",
1139 os_free(sm->identity);
1140 sm->identity = identity;
1141 sm->identity_len = len;
1145 struct sta_id_search {
1147 struct eapol_state_machine *sm;
1151 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1152 struct sta_info *sta,
1155 struct sta_id_search *id_search = ctx;
1156 struct eapol_state_machine *sm = sta->eapol_sm;
1158 if (sm && sm->radius_identifier >= 0 &&
1159 sm->radius_identifier == id_search->identifier) {
1167 static struct eapol_state_machine *
1168 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1170 struct sta_id_search id_search;
1171 id_search.identifier = identifier;
1172 id_search.sm = NULL;
1173 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1174 return id_search.sm;
1179 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1180 * @msg: RADIUS response message
1181 * @req: RADIUS request message
1182 * @shared_secret: RADIUS shared secret
1183 * @shared_secret_len: Length of shared_secret in octets
1184 * @data: Context data (struct hostapd_data *)
1185 * Returns: Processing status
1187 static RadiusRxResult
1188 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1189 const u8 *shared_secret, size_t shared_secret_len,
1192 struct hostapd_data *hapd = data;
1193 struct sta_info *sta;
1194 u32 session_timeout = 0, termination_action, acct_interim_interval;
1195 int session_timeout_set, old_vlanid = 0;
1196 struct eapol_state_machine *sm;
1197 int override_eapReq = 0;
1199 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1201 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1202 "station for this RADIUS message");
1203 return RADIUS_RX_UNKNOWN;
1207 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1208 * present when packet contains an EAP-Message attribute */
1209 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1210 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1212 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1213 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1214 "Message-Authenticator since it does not include "
1216 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1218 printf("Incoming RADIUS packet did not have correct "
1219 "Message-Authenticator - dropped\n");
1220 return RADIUS_RX_INVALID_AUTHENTICATOR;
1223 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1224 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1225 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1226 printf("Unknown RADIUS message code\n");
1227 return RADIUS_RX_UNKNOWN;
1230 sm->radius_identifier = -1;
1231 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1232 MAC2STR(sta->addr));
1234 if (sm->last_recv_radius) {
1235 radius_msg_free(sm->last_recv_radius);
1236 os_free(sm->last_recv_radius);
1239 sm->last_recv_radius = msg;
1241 session_timeout_set =
1242 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1244 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1245 &termination_action))
1246 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1248 if (hapd->conf->radius->acct_interim_interval == 0 &&
1249 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1250 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1251 &acct_interim_interval) == 0) {
1252 if (acct_interim_interval < 60) {
1253 hostapd_logger(hapd, sta->addr,
1254 HOSTAPD_MODULE_IEEE8021X,
1256 "ignored too small "
1257 "Acct-Interim-Interval %d",
1258 acct_interim_interval);
1260 sta->acct_interim_interval = acct_interim_interval;
1264 switch (msg->hdr->code) {
1265 case RADIUS_CODE_ACCESS_ACCEPT:
1266 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1268 #ifndef CONFIG_NO_VLAN
1270 old_vlanid = sta->vlan_id;
1271 sta->vlan_id = radius_msg_get_vlanid(msg);
1273 if (sta->vlan_id > 0 &&
1274 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1276 hostapd_logger(hapd, sta->addr,
1277 HOSTAPD_MODULE_RADIUS,
1279 "VLAN ID %d", sta->vlan_id);
1280 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1281 sta->eapol_sm->authFail = TRUE;
1282 hostapd_logger(hapd, sta->addr,
1283 HOSTAPD_MODULE_IEEE8021X,
1284 HOSTAPD_LEVEL_INFO, "authentication "
1285 "server did not include required VLAN "
1286 "ID in Access-Accept");
1289 #endif /* CONFIG_NO_VLAN */
1291 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1293 /* RFC 3580, Ch. 3.17 */
1294 if (session_timeout_set && termination_action ==
1295 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1296 sm->reAuthPeriod = session_timeout;
1297 } else if (session_timeout_set)
1298 ap_sta_session_timeout(hapd, sta, session_timeout);
1300 sm->eap_if->aaaSuccess = TRUE;
1301 override_eapReq = 1;
1302 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1304 ieee802_1x_store_radius_class(hapd, sta, msg);
1305 ieee802_1x_update_sta_identity(hapd, sta, msg);
1306 if (sm->eap_if->eapKeyAvailable &&
1307 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1308 session_timeout_set ?
1309 (int) session_timeout : -1, sm) == 0) {
1310 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1311 HOSTAPD_LEVEL_DEBUG,
1312 "Added PMKSA cache entry");
1315 case RADIUS_CODE_ACCESS_REJECT:
1316 sm->eap_if->aaaFail = TRUE;
1317 override_eapReq = 1;
1319 case RADIUS_CODE_ACCESS_CHALLENGE:
1320 sm->eap_if->aaaEapReq = TRUE;
1321 if (session_timeout_set) {
1322 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1323 sm->eap_if->aaaMethodTimeout = session_timeout;
1324 hostapd_logger(hapd, sm->addr,
1325 HOSTAPD_MODULE_IEEE8021X,
1326 HOSTAPD_LEVEL_DEBUG,
1327 "using EAP timeout of %d seconds (from "
1329 sm->eap_if->aaaMethodTimeout);
1332 * Use dynamic retransmission behavior per EAP
1335 sm->eap_if->aaaMethodTimeout = 0;
1340 ieee802_1x_decapsulate_radius(hapd, sta);
1341 if (override_eapReq)
1342 sm->eap_if->aaaEapReq = FALSE;
1344 eapol_auth_step(sm);
1346 return RADIUS_RX_QUEUED;
1348 #endif /* CONFIG_NO_RADIUS */
1351 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1353 struct eapol_state_machine *sm = sta->eapol_sm;
1357 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1358 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1360 #ifndef CONFIG_NO_RADIUS
1361 if (sm->last_recv_radius) {
1362 radius_msg_free(sm->last_recv_radius);
1363 os_free(sm->last_recv_radius);
1364 sm->last_recv_radius = NULL;
1366 #endif /* CONFIG_NO_RADIUS */
1368 if (sm->eap_if->eapTimeout) {
1370 * Disconnect the STA since it did not reply to the last EAP
1371 * request and we cannot continue EAP processing (EAP-Failure
1372 * could only be sent if the EAP peer actually replied).
1374 sm->eap_if->portEnabled = FALSE;
1375 hostapd_sta_deauth(hapd, sta->addr,
1376 WLAN_REASON_PREV_AUTH_NOT_VALID);
1377 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1378 WLAN_STA_AUTHORIZED);
1379 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1380 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1381 sta->timeout_next = STA_REMOVE;
1386 #ifdef HOSTAPD_DUMP_STATE
1387 static void fprint_char(FILE *f, char c)
1389 if (c >= 32 && c < 127)
1390 fprintf(f, "%c", c);
1392 fprintf(f, "<%02x>", c);
1396 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1398 struct eapol_state_machine *sm = sta->eapol_sm;
1402 fprintf(f, "%sIEEE 802.1X:\n", prefix);
1406 fprintf(f, "%sidentity=", prefix);
1407 for (i = 0; i < sm->identity_len; i++)
1408 fprint_char(f, sm->identity[i]);
1412 fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1413 "Supplicant: %d (%s)\n", prefix,
1414 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1415 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1417 fprintf(f, "%scached_packets=%s\n", prefix,
1418 sm->last_recv_radius ? "[RX RADIUS]" : "");
1420 eapol_auth_dump_state(f, prefix, sm);
1422 #endif /* HOSTAPD_DUMP_STATE */
1425 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1427 if (hapd->conf->default_wep_key_len < 1)
1430 os_free(hapd->default_wep_key);
1431 hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1432 if (hapd->default_wep_key == NULL ||
1433 os_get_random(hapd->default_wep_key,
1434 hapd->conf->default_wep_key_len)) {
1435 printf("Could not generate random WEP key.\n");
1436 os_free(hapd->default_wep_key);
1437 hapd->default_wep_key = NULL;
1441 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1442 hapd->default_wep_key,
1443 hapd->conf->default_wep_key_len);
1449 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1450 struct sta_info *sta, void *ctx)
1452 if (sta->eapol_sm) {
1453 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1454 eapol_auth_step(sta->eapol_sm);
1460 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1462 struct hostapd_data *hapd = eloop_ctx;
1464 if (hapd->default_wep_key_idx >= 3)
1465 hapd->default_wep_key_idx =
1466 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1468 hapd->default_wep_key_idx++;
1470 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1471 hapd->default_wep_key_idx);
1473 if (ieee802_1x_rekey_broadcast(hapd)) {
1474 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1475 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1476 "new broadcast key");
1477 os_free(hapd->default_wep_key);
1478 hapd->default_wep_key = NULL;
1482 /* TODO: Could setup key for RX here, but change default TX keyid only
1483 * after new broadcast key has been sent to all stations. */
1484 if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1485 hapd->default_wep_key_idx,
1486 hapd->default_wep_key,
1487 hapd->conf->default_wep_key_len, 1)) {
1488 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1489 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1490 "new broadcast key");
1491 os_free(hapd->default_wep_key);
1492 hapd->default_wep_key = NULL;
1496 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1498 if (hapd->conf->wep_rekeying_period > 0) {
1499 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1500 ieee802_1x_rekey, hapd, NULL);
1505 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1506 const u8 *data, size_t datalen)
1508 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1512 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1513 const u8 *data, size_t datalen)
1515 #ifndef CONFIG_NO_RADIUS
1516 struct hostapd_data *hapd = ctx;
1517 struct sta_info *sta = sta_ctx;
1519 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1520 #endif /* CONFIG_NO_RADIUS */
1524 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1527 struct hostapd_data *hapd = ctx;
1528 struct sta_info *sta = sta_ctx;
1530 rsn_preauth_finished(hapd, sta, success);
1532 ieee802_1x_finished(hapd, sta, success);
1536 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1537 size_t identity_len, int phase2,
1538 struct eap_user *user)
1540 struct hostapd_data *hapd = ctx;
1541 const struct hostapd_eap_user *eap_user;
1544 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1545 identity_len, phase2);
1546 if (eap_user == NULL)
1549 os_memset(user, 0, sizeof(*user));
1550 user->phase2 = phase2;
1551 count = EAP_USER_MAX_METHODS;
1552 if (count > EAP_MAX_METHODS)
1553 count = EAP_MAX_METHODS;
1554 for (i = 0; i < count; i++) {
1555 user->methods[i].vendor = eap_user->methods[i].vendor;
1556 user->methods[i].method = eap_user->methods[i].method;
1559 if (eap_user->password) {
1560 user->password = os_malloc(eap_user->password_len);
1561 if (user->password == NULL)
1563 os_memcpy(user->password, eap_user->password,
1564 eap_user->password_len);
1565 user->password_len = eap_user->password_len;
1567 user->force_version = eap_user->force_version;
1568 user->ttls_auth = eap_user->ttls_auth;
1574 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1576 struct hostapd_data *hapd = ctx;
1577 struct sta_info *sta;
1578 sta = ap_get_sta(hapd, addr);
1579 if (sta == NULL || sta->eapol_sm == NULL)
1585 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1586 eapol_logger_level level, const char *txt)
1588 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1589 struct hostapd_data *hapd = ctx;
1593 case EAPOL_LOGGER_WARNING:
1594 hlevel = HOSTAPD_LEVEL_WARNING;
1596 case EAPOL_LOGGER_INFO:
1597 hlevel = HOSTAPD_LEVEL_INFO;
1599 case EAPOL_LOGGER_DEBUG:
1601 hlevel = HOSTAPD_LEVEL_DEBUG;
1605 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1607 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1611 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1614 struct hostapd_data *hapd = ctx;
1615 struct sta_info *sta = sta_ctx;
1616 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1620 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1622 struct hostapd_data *hapd = ctx;
1623 struct sta_info *sta = sta_ctx;
1624 ieee802_1x_abort_auth(hapd, sta);
1628 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1630 struct hostapd_data *hapd = ctx;
1631 struct sta_info *sta = sta_ctx;
1632 ieee802_1x_tx_key(hapd, sta);
1636 int ieee802_1x_init(struct hostapd_data *hapd)
1639 struct eapol_auth_config conf;
1640 struct eapol_auth_cb cb;
1642 os_memset(&conf, 0, sizeof(conf));
1644 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1645 conf.wpa = hapd->conf->wpa;
1646 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1647 conf.eap_server = hapd->conf->eap_server;
1648 conf.ssl_ctx = hapd->ssl_ctx;
1649 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1650 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1651 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1652 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1653 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1654 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1655 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1656 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1657 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1658 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1659 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1660 conf.tnc = hapd->conf->tnc;
1661 conf.wps = hapd->wps;
1663 os_memset(&cb, 0, sizeof(cb));
1664 cb.eapol_send = ieee802_1x_eapol_send;
1665 cb.aaa_send = ieee802_1x_aaa_send;
1666 cb.finished = _ieee802_1x_finished;
1667 cb.get_eap_user = ieee802_1x_get_eap_user;
1668 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1669 cb.logger = ieee802_1x_logger;
1670 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1671 cb.abort_auth = _ieee802_1x_abort_auth;
1672 cb.tx_key = _ieee802_1x_tx_key;
1674 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1675 if (hapd->eapol_auth == NULL)
1678 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1679 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1682 #ifndef CONFIG_NO_RADIUS
1683 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1684 ieee802_1x_receive_auth, hapd))
1686 #endif /* CONFIG_NO_RADIUS */
1688 if (hapd->conf->default_wep_key_len) {
1689 hostapd_set_privacy(hapd, 1);
1691 for (i = 0; i < 4; i++)
1692 hostapd_set_encryption(hapd->conf->iface, hapd,
1693 "none", NULL, i, NULL, 0, 0);
1695 ieee802_1x_rekey(hapd, NULL);
1697 if (hapd->default_wep_key == NULL)
1705 void ieee802_1x_deinit(struct hostapd_data *hapd)
1707 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1709 if (hapd->driver != NULL &&
1710 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1711 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1713 eapol_auth_deinit(hapd->eapol_auth);
1714 hapd->eapol_auth = NULL;
1718 int ieee802_1x_reconfig(struct hostapd_data *hapd,
1719 struct hostapd_config *oldconf,
1720 struct hostapd_bss_config *oldbss)
1722 ieee802_1x_deinit(hapd);
1723 return ieee802_1x_init(hapd);
1727 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1728 const u8 *buf, size_t len, int ack)
1730 struct ieee80211_hdr *hdr;
1731 struct ieee802_1x_hdr *xhdr;
1732 struct ieee802_1x_eapol_key *key;
1734 const unsigned char rfc1042_hdr[ETH_ALEN] =
1735 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1739 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1742 hdr = (struct ieee80211_hdr *) buf;
1743 pos = (u8 *) (hdr + 1);
1744 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1746 pos += sizeof(rfc1042_hdr);
1747 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1751 xhdr = (struct ieee802_1x_hdr *) pos;
1752 pos += sizeof(*xhdr);
1754 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1755 "type=%d length=%d - ack=%d",
1756 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1757 be_to_host16(xhdr->length), ack);
1759 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1760 * or Authenticator state machines, but EAPOL-Key packets are not
1761 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1762 * packets couple of times because otherwise STA keys become
1763 * unsynchronized with AP. */
1764 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1765 pos + sizeof(*key) <= buf + len) {
1766 key = (struct ieee802_1x_eapol_key *) pos;
1767 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1768 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1769 "frame (%scast index=%d)",
1770 key->key_index & BIT(7) ? "uni" : "broad",
1771 key->key_index & ~BIT(7));
1772 /* TODO: re-send EAPOL-Key couple of times (with short delay
1773 * between them?). If all attempt fail, report error and
1774 * deauthenticate STA so that it will get new keys when
1775 * authenticating again (e.g., after returning in range).
1776 * Separate limit/transmit state needed both for unicast and
1777 * broadcast keys(?) */
1779 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1780 * to here and change the key only if the EAPOL-Key packet was Acked.
1787 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1789 if (sm == NULL || sm->identity == NULL)
1792 *len = sm->identity_len;
1793 return sm->identity;
1797 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1800 if (sm == NULL || sm->radius_class.attr == NULL ||
1801 idx >= (int) sm->radius_class.count)
1804 *len = sm->radius_class.attr[idx].len;
1805 return sm->radius_class.attr[idx].data;
1809 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1814 *len = sm->eap_if->eapKeyDataLen;
1815 return sm->eap_if->eapKeyData;
1819 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1824 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1825 eapol_auth_step(sm);
1829 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1834 sm->portValid = valid ? TRUE : FALSE;
1835 eapol_auth_step(sm);
1839 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1844 sm->flags |= EAPOL_SM_PREAUTH;
1846 sm->flags &= ~EAPOL_SM_PREAUTH;
1850 static const char * bool_txt(Boolean bool)
1852 return bool ? "TRUE" : "FALSE";
1856 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1863 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1864 char *buf, size_t buflen)
1867 struct eapol_state_machine *sm = sta->eapol_sm;
1872 ret = os_snprintf(buf + len, buflen - len,
1873 "dot1xPaePortNumber=%d\n"
1874 "dot1xPaePortProtocolVersion=%d\n"
1875 "dot1xPaePortCapabilities=1\n"
1876 "dot1xPaePortInitialize=%d\n"
1877 "dot1xPaePortReauthenticate=FALSE\n",
1881 if (ret < 0 || (size_t) ret >= buflen - len)
1885 /* dot1xAuthConfigTable */
1886 ret = os_snprintf(buf + len, buflen - len,
1887 "dot1xAuthPaeState=%d\n"
1888 "dot1xAuthBackendAuthState=%d\n"
1889 "dot1xAuthAdminControlledDirections=%d\n"
1890 "dot1xAuthOperControlledDirections=%d\n"
1891 "dot1xAuthAuthControlledPortStatus=%d\n"
1892 "dot1xAuthAuthControlledPortControl=%d\n"
1893 "dot1xAuthQuietPeriod=%u\n"
1894 "dot1xAuthServerTimeout=%u\n"
1895 "dot1xAuthReAuthPeriod=%u\n"
1896 "dot1xAuthReAuthEnabled=%s\n"
1897 "dot1xAuthKeyTxEnabled=%s\n",
1898 sm->auth_pae_state + 1,
1899 sm->be_auth_state + 1,
1900 sm->adminControlledDirections,
1901 sm->operControlledDirections,
1907 bool_txt(sm->reAuthEnabled),
1908 bool_txt(sm->keyTxEnabled));
1909 if (ret < 0 || (size_t) ret >= buflen - len)
1913 /* dot1xAuthStatsTable */
1914 ret = os_snprintf(buf + len, buflen - len,
1915 "dot1xAuthEapolFramesRx=%u\n"
1916 "dot1xAuthEapolFramesTx=%u\n"
1917 "dot1xAuthEapolStartFramesRx=%u\n"
1918 "dot1xAuthEapolLogoffFramesRx=%u\n"
1919 "dot1xAuthEapolRespIdFramesRx=%u\n"
1920 "dot1xAuthEapolRespFramesRx=%u\n"
1921 "dot1xAuthEapolReqIdFramesTx=%u\n"
1922 "dot1xAuthEapolReqFramesTx=%u\n"
1923 "dot1xAuthInvalidEapolFramesRx=%u\n"
1924 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1925 "dot1xAuthLastEapolFrameVersion=%u\n"
1926 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1927 sm->dot1xAuthEapolFramesRx,
1928 sm->dot1xAuthEapolFramesTx,
1929 sm->dot1xAuthEapolStartFramesRx,
1930 sm->dot1xAuthEapolLogoffFramesRx,
1931 sm->dot1xAuthEapolRespIdFramesRx,
1932 sm->dot1xAuthEapolRespFramesRx,
1933 sm->dot1xAuthEapolReqIdFramesTx,
1934 sm->dot1xAuthEapolReqFramesTx,
1935 sm->dot1xAuthInvalidEapolFramesRx,
1936 sm->dot1xAuthEapLengthErrorFramesRx,
1937 sm->dot1xAuthLastEapolFrameVersion,
1939 if (ret < 0 || (size_t) ret >= buflen - len)
1943 /* dot1xAuthDiagTable */
1944 ret = os_snprintf(buf + len, buflen - len,
1945 "dot1xAuthEntersConnecting=%u\n"
1946 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1947 "dot1xAuthEntersAuthenticating=%u\n"
1948 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1949 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1950 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1951 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1952 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1953 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1954 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1955 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1956 "dot1xAuthBackendResponses=%u\n"
1957 "dot1xAuthBackendAccessChallenges=%u\n"
1958 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1959 "dot1xAuthBackendAuthSuccesses=%u\n"
1960 "dot1xAuthBackendAuthFails=%u\n",
1961 sm->authEntersConnecting,
1962 sm->authEapLogoffsWhileConnecting,
1963 sm->authEntersAuthenticating,
1964 sm->authAuthSuccessesWhileAuthenticating,
1965 sm->authAuthTimeoutsWhileAuthenticating,
1966 sm->authAuthFailWhileAuthenticating,
1967 sm->authAuthEapStartsWhileAuthenticating,
1968 sm->authAuthEapLogoffWhileAuthenticating,
1969 sm->authAuthReauthsWhileAuthenticated,
1970 sm->authAuthEapStartsWhileAuthenticated,
1971 sm->authAuthEapLogoffWhileAuthenticated,
1972 sm->backendResponses,
1973 sm->backendAccessChallenges,
1974 sm->backendOtherRequestsToSupplicant,
1975 sm->backendAuthSuccesses,
1976 sm->backendAuthFails);
1977 if (ret < 0 || (size_t) ret >= buflen - len)
1981 /* dot1xAuthSessionStatsTable */
1982 ret = os_snprintf(buf + len, buflen - len,
1983 /* TODO: dot1xAuthSessionOctetsRx */
1984 /* TODO: dot1xAuthSessionOctetsTx */
1985 /* TODO: dot1xAuthSessionFramesRx */
1986 /* TODO: dot1xAuthSessionFramesTx */
1987 "dot1xAuthSessionId=%08X-%08X\n"
1988 "dot1xAuthSessionAuthenticMethod=%d\n"
1989 "dot1xAuthSessionTime=%u\n"
1990 "dot1xAuthSessionTerminateCause=999\n"
1991 "dot1xAuthSessionUserName=%s\n",
1992 sta->acct_session_id_hi, sta->acct_session_id_lo,
1993 (wpa_key_mgmt_wpa_ieee8021x(
1994 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1996 (unsigned int) (time(NULL) -
1997 sta->acct_session_start),
1999 if (ret < 0 || (size_t) ret >= buflen - len)
2007 static void ieee802_1x_finished(struct hostapd_data *hapd,
2008 struct sta_info *sta, int success)
2012 /* TODO: get PMKLifetime from WPA parameters */
2013 static const int dot11RSNAConfigPMKLifetime = 43200;
2015 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2016 if (success && key && len >= PMK_LEN &&
2017 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2018 sta->eapol_sm) == 0) {
2019 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2020 HOSTAPD_LEVEL_DEBUG,
2021 "Added PMKSA cache entry (IEEE 802.1X)");