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.
19 #include "crypto/md5.h"
20 #include "crypto/crypto.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/wpa_ctrl.h"
23 #include "radius/radius.h"
24 #include "radius/radius_client.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "eapol_auth/eapol_auth_sm_i.h"
28 #include "ieee802_1x.h"
29 #include "accounting.h"
33 #include "pmksa_cache.h"
35 #include "hw_features.h"
36 #include "eap_server/eap.h"
39 static void ieee802_1x_finished(struct hostapd_data *hapd,
40 struct sta_info *sta, int success);
43 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
44 u8 type, const u8 *data, size_t datalen)
47 struct ieee802_1x_hdr *xhdr;
51 len = sizeof(*xhdr) + datalen;
54 wpa_printf(MSG_ERROR, "malloc() failed for "
55 "ieee802_1x_send(len=%lu)",
60 xhdr = (struct ieee802_1x_hdr *) buf;
61 xhdr->version = hapd->conf->eapol_version;
63 xhdr->length = host_to_be16(datalen);
65 if (datalen > 0 && data != NULL)
66 os_memcpy(xhdr + 1, data, datalen);
68 if (wpa_auth_pairwise_set(sta->wpa_sm))
70 if (sta->flags & WLAN_STA_PREAUTH) {
71 rsn_preauth_send(hapd, sta, buf, len);
73 hostapd_send_eapol(hapd, sta->addr, buf, len, encrypt);
80 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
81 struct sta_info *sta, int authorized)
85 if (sta->flags & WLAN_STA_PREAUTH)
89 if (!(sta->flags & WLAN_STA_AUTHORIZED))
90 wpa_msg(hapd->msg_ctx, MSG_INFO,
91 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
92 sta->flags |= WLAN_STA_AUTHORIZED;
93 res = hostapd_sta_set_flags(hapd, sta->addr,
94 hostapd_sta_flags_to_drv(
96 WPA_STA_AUTHORIZED, ~0);
97 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
98 HOSTAPD_LEVEL_DEBUG, "authorizing port");
100 if ((sta->flags & (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC)) ==
101 (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC))
102 wpa_msg(hapd->msg_ctx, MSG_INFO,
103 AP_STA_DISCONNECTED MACSTR,
105 sta->flags &= ~WLAN_STA_AUTHORIZED;
106 res = hostapd_sta_set_flags(hapd, sta->addr,
107 hostapd_sta_flags_to_drv(
109 0, ~WPA_STA_AUTHORIZED);
110 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
111 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
114 if (res && errno != ENOENT) {
115 printf("Could not set station " MACSTR " flags for kernel "
116 "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
120 accounting_sta_start(hapd, sta);
124 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
125 struct sta_info *sta,
126 int idx, int broadcast,
127 u8 *key_data, size_t key_len)
130 struct ieee802_1x_hdr *hdr;
131 struct ieee802_1x_eapol_key *key;
132 size_t len, ekey_len;
133 struct eapol_state_machine *sm = sta->eapol_sm;
138 len = sizeof(*key) + key_len;
139 buf = os_zalloc(sizeof(*hdr) + len);
143 hdr = (struct ieee802_1x_hdr *) buf;
144 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
145 key->type = EAPOL_KEY_TYPE_RC4;
146 key->key_length = htons(key_len);
147 wpa_get_ntp_timestamp(key->replay_counter);
149 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
150 wpa_printf(MSG_ERROR, "Could not get random numbers");
155 key->key_index = idx | (broadcast ? 0 : BIT(7));
156 if (hapd->conf->eapol_key_index_workaround) {
157 /* According to some information, WinXP Supplicant seems to
158 * interpret bit7 as an indication whether the key is to be
159 * activated, so make it possible to enable workaround that
160 * sets this bit for all keys. */
161 key->key_index |= BIT(7);
164 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
165 * MSK[32..63] is used to sign the message. */
166 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
167 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
168 "and signing EAPOL-Key");
172 os_memcpy((u8 *) (key + 1), key_data, key_len);
173 ekey_len = sizeof(key->key_iv) + 32;
174 ekey = os_malloc(ekey_len);
176 wpa_printf(MSG_ERROR, "Could not encrypt key");
180 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
181 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
182 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
185 /* This header is needed here for HMAC-MD5, but it will be regenerated
186 * in ieee802_1x_send() */
187 hdr->version = hapd->conf->eapol_version;
188 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
189 hdr->length = host_to_be16(len);
190 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
193 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
194 " (%s index=%d)", MAC2STR(sm->addr),
195 broadcast ? "broadcast" : "unicast", idx);
196 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
198 sta->eapol_sm->dot1xAuthEapolFramesTx++;
203 #ifndef CONFIG_NO_VLAN
204 static struct hostapd_wep_keys *
205 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
207 struct hostapd_wep_keys *key;
209 key = os_zalloc(sizeof(*key));
213 key->default_len = hapd->conf->default_wep_key_len;
215 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
216 key->idx < hapd->conf->broadcast_key_idx_min)
217 key->idx = hapd->conf->broadcast_key_idx_min;
221 if (!key->key[key->idx])
222 key->key[key->idx] = os_malloc(key->default_len);
223 if (key->key[key->idx] == NULL ||
224 os_get_random(key->key[key->idx], key->default_len)) {
225 printf("Could not generate random WEP key (dynamic VLAN).\n");
226 os_free(key->key[key->idx]);
227 key->key[key->idx] = NULL;
231 key->len[key->idx] = key->default_len;
233 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
235 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
236 key->key[key->idx], key->len[key->idx]);
238 if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL, key->idx, 1,
239 NULL, 0, key->key[key->idx], key->len[key->idx]))
240 printf("Could not set dynamic VLAN WEP encryption key.\n");
242 hostapd_set_drv_ieee8021x(hapd, ifname, 1);
248 static struct hostapd_wep_keys *
249 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
257 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
258 ssid->dyn_vlan_keys[vlan_id])
259 return ssid->dyn_vlan_keys[vlan_id];
261 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
262 "state machine for VLAN ID %lu",
263 (unsigned long) vlan_id);
265 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
266 if (ifname == NULL) {
267 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
268 "cannot create group key state machine",
269 (unsigned long) vlan_id);
273 if (ssid->dyn_vlan_keys == NULL) {
274 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
275 ssid->dyn_vlan_keys = os_zalloc(size);
276 if (ssid->dyn_vlan_keys == NULL)
278 ssid->max_dyn_vlan_keys = vlan_id;
281 if (ssid->max_dyn_vlan_keys < vlan_id) {
282 struct hostapd_wep_keys **na;
283 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
284 na = os_realloc(ssid->dyn_vlan_keys, size);
287 ssid->dyn_vlan_keys = na;
288 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
289 (vlan_id - ssid->max_dyn_vlan_keys) *
290 sizeof(ssid->dyn_vlan_keys[0]));
291 ssid->max_dyn_vlan_keys = vlan_id;
294 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
296 return ssid->dyn_vlan_keys[vlan_id];
298 #endif /* CONFIG_NO_VLAN */
301 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
303 struct eapol_authenticator *eapol = hapd->eapol_auth;
304 struct eapol_state_machine *sm = sta->eapol_sm;
305 #ifndef CONFIG_NO_VLAN
306 struct hostapd_wep_keys *key = NULL;
308 #endif /* CONFIG_NO_VLAN */
310 if (sm == NULL || !sm->eap_if->eapKeyData)
313 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
316 #ifndef CONFIG_NO_VLAN
317 vlan_id = sta->vlan_id;
318 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
322 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
323 if (key && key->key[key->idx])
324 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
328 #endif /* CONFIG_NO_VLAN */
329 if (eapol->default_wep_key) {
330 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
331 eapol->default_wep_key,
332 hapd->conf->default_wep_key_len);
335 if (hapd->conf->individual_wep_key_len > 0) {
337 ikey = os_malloc(hapd->conf->individual_wep_key_len);
339 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
340 wpa_printf(MSG_ERROR, "Could not generate random "
341 "individual WEP key.");
346 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
347 ikey, hapd->conf->individual_wep_key_len);
349 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
350 hapd->conf->individual_wep_key_len);
352 /* TODO: set encryption in TX callback, i.e., only after STA
353 * has ACKed EAPOL-Key frame */
354 if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
355 sta->addr, 0, 1, NULL, 0, ikey,
356 hapd->conf->individual_wep_key_len)) {
357 wpa_printf(MSG_ERROR, "Could not set individual WEP "
366 const char *radius_mode_txt(struct hostapd_data *hapd)
368 if (hapd->iface->current_mode == NULL)
371 switch (hapd->iface->current_mode->mode) {
372 case HOSTAPD_MODE_IEEE80211A:
374 case HOSTAPD_MODE_IEEE80211G:
376 case HOSTAPD_MODE_IEEE80211B:
383 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
388 for (i = 0; i < sta->supported_rates_len; i++)
389 if ((sta->supported_rates[i] & 0x7f) > rate)
390 rate = sta->supported_rates[i] & 0x7f;
396 #ifndef CONFIG_NO_RADIUS
397 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
398 struct eapol_state_machine *sm,
399 const u8 *eap, size_t len)
404 if (len <= sizeof(struct eap_hdr) ||
405 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
408 identity = eap_get_identity(sm->eap, &identity_len);
409 if (identity == NULL)
412 /* Save station identity for future RADIUS packets */
413 os_free(sm->identity);
414 sm->identity = os_malloc(identity_len + 1);
415 if (sm->identity == NULL) {
416 sm->identity_len = 0;
420 os_memcpy(sm->identity, identity, identity_len);
421 sm->identity_len = identity_len;
422 sm->identity[identity_len] = '\0';
423 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
424 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
425 sm->dot1xAuthEapolRespIdFramesRx++;
429 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
430 struct sta_info *sta,
431 const u8 *eap, size_t len)
433 struct radius_msg *msg;
435 struct eapol_state_machine *sm = sta->eapol_sm;
440 ieee802_1x_learn_identity(hapd, sm, eap, len);
442 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
445 sm->radius_identifier = radius_client_get_id(hapd->radius);
446 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
447 sm->radius_identifier);
449 printf("Could not create net RADIUS packet\n");
453 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
456 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
457 sm->identity, sm->identity_len)) {
458 printf("Could not add User-Name\n");
462 if (hapd->conf->own_ip_addr.af == AF_INET &&
463 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
464 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
465 printf("Could not add NAS-IP-Address\n");
470 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
471 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
472 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
473 printf("Could not add NAS-IPv6-Address\n");
476 #endif /* CONFIG_IPV6 */
478 if (hapd->conf->nas_identifier &&
479 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
480 (u8 *) hapd->conf->nas_identifier,
481 os_strlen(hapd->conf->nas_identifier))) {
482 printf("Could not add NAS-Identifier\n");
486 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
487 printf("Could not add NAS-Port\n");
491 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
492 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
493 buf[sizeof(buf) - 1] = '\0';
494 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
495 (u8 *) buf, os_strlen(buf))) {
496 printf("Could not add Called-Station-Id\n");
500 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
502 buf[sizeof(buf) - 1] = '\0';
503 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
504 (u8 *) buf, os_strlen(buf))) {
505 printf("Could not add Calling-Station-Id\n");
509 /* TODO: should probably check MTU from driver config; 2304 is max for
510 * IEEE 802.11, but use 1400 to avoid problems with too large packets
512 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
513 printf("Could not add Framed-MTU\n");
517 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
518 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
519 printf("Could not add NAS-Port-Type\n");
523 if (sta->flags & WLAN_STA_PREAUTH) {
524 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
527 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
528 radius_sta_rate(hapd, sta) / 2,
529 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
530 radius_mode_txt(hapd));
531 buf[sizeof(buf) - 1] = '\0';
533 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
534 (u8 *) buf, os_strlen(buf))) {
535 printf("Could not add Connect-Info\n");
539 if (eap && !radius_msg_add_eap(msg, eap, len)) {
540 printf("Could not add EAP-Message\n");
544 /* State attribute must be copied if and only if this packet is
545 * Access-Request reply to the previous Access-Challenge */
546 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
547 RADIUS_CODE_ACCESS_CHALLENGE) {
548 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
551 printf("Could not copy State attribute from previous "
552 "Access-Challenge\n");
556 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
560 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
564 radius_msg_free(msg);
567 #endif /* CONFIG_NO_RADIUS */
570 static void handle_eap_response(struct hostapd_data *hapd,
571 struct sta_info *sta, struct eap_hdr *eap,
575 struct eapol_state_machine *sm = sta->eapol_sm;
579 data = (u8 *) (eap + 1);
581 if (len < sizeof(*eap) + 1) {
582 printf("handle_eap_response: too short response data\n");
586 sm->eap_type_supp = type = data[0];
588 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
589 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
590 "id=%d len=%d) from STA: EAP Response-%s (%d)",
591 eap->code, eap->identifier, be_to_host16(eap->length),
592 eap_server_get_name(0, type), type);
594 sm->dot1xAuthEapolRespFramesRx++;
596 wpabuf_free(sm->eap_if->eapRespData);
597 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
602 /* Process incoming EAP packet from Supplicant */
603 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
609 if (len < sizeof(*eap)) {
610 printf(" too short EAP packet\n");
614 eap = (struct eap_hdr *) buf;
616 eap_len = be_to_host16(eap->length);
617 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
618 eap->code, eap->identifier, eap_len);
619 if (eap_len < sizeof(*eap)) {
620 wpa_printf(MSG_DEBUG, " Invalid EAP length");
622 } else if (eap_len > len) {
623 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
626 } else if (eap_len < len) {
627 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
628 "packet", (unsigned long) len - eap_len);
632 case EAP_CODE_REQUEST:
633 wpa_printf(MSG_DEBUG, " (request)");
635 case EAP_CODE_RESPONSE:
636 wpa_printf(MSG_DEBUG, " (response)");
637 handle_eap_response(hapd, sta, eap, eap_len);
639 case EAP_CODE_SUCCESS:
640 wpa_printf(MSG_DEBUG, " (success)");
642 case EAP_CODE_FAILURE:
643 wpa_printf(MSG_DEBUG, " (failure)");
646 wpa_printf(MSG_DEBUG, " (unknown code)");
652 static struct eapol_state_machine *
653 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
656 if (sta->flags & WLAN_STA_PREAUTH)
657 flags |= EAPOL_SM_PREAUTH;
659 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
660 flags |= EAPOL_SM_USES_WPA;
661 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
662 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
664 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
670 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
671 * @hapd: hostapd BSS data
672 * @sa: Source address (sender of the EAPOL frame)
674 * @len: Length of buf in octets
676 * This function is called for each incoming EAPOL frame from the interface
678 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
681 struct sta_info *sta;
682 struct ieee802_1x_hdr *hdr;
683 struct ieee802_1x_eapol_key *key;
685 struct rsn_pmksa_cache_entry *pmksa;
687 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
688 !hapd->conf->wps_state)
691 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
692 (unsigned long) len, MAC2STR(sa));
693 sta = ap_get_sta(hapd, sa);
694 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
695 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
700 if (len < sizeof(*hdr)) {
701 printf(" too short IEEE 802.1X packet\n");
705 hdr = (struct ieee802_1x_hdr *) buf;
706 datalen = be_to_host16(hdr->length);
707 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
708 hdr->version, hdr->type, datalen);
710 if (len - sizeof(*hdr) < datalen) {
711 printf(" frame too short for this IEEE 802.1X packet\n");
713 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
716 if (len - sizeof(*hdr) > datalen) {
717 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
718 "IEEE 802.1X packet",
719 (unsigned long) len - sizeof(*hdr) - datalen);
723 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
724 sta->eapol_sm->dot1xAuthEapolFramesRx++;
727 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
728 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
729 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
730 (key->type == EAPOL_KEY_TYPE_WPA ||
731 key->type == EAPOL_KEY_TYPE_RSN)) {
732 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
733 sizeof(*hdr) + datalen);
737 if ((!hapd->conf->ieee802_1x &&
738 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
739 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
742 if (!sta->eapol_sm) {
743 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
748 if (!hapd->conf->ieee802_1x &&
749 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
750 WLAN_STA_MAYBE_WPS)) {
752 * Delay EAPOL frame transmission until a possible WPS
753 * STA initiates the handshake with EAPOL-Start.
755 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
757 #endif /* CONFIG_WPS */
759 sta->eapol_sm->eap_if->portEnabled = TRUE;
762 /* since we support version 1, we can ignore version field and proceed
763 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
764 /* TODO: actually, we are not version 1 anymore.. However, Version 2
765 * does not change frame contents, so should be ok to process frames
766 * more or less identically. Some changes might be needed for
767 * verification of fields. */
770 case IEEE802_1X_TYPE_EAP_PACKET:
771 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
774 case IEEE802_1X_TYPE_EAPOL_START:
775 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
776 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
778 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
779 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
781 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
782 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
783 "available - ignore it since "
784 "STA sent EAPOL-Start");
785 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
787 sta->eapol_sm->eapolStart = TRUE;
788 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
789 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
792 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
793 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
794 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
796 sta->acct_terminate_cause =
797 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
798 accounting_sta_stop(hapd, sta);
799 sta->eapol_sm->eapolLogoff = TRUE;
800 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
803 case IEEE802_1X_TYPE_EAPOL_KEY:
804 wpa_printf(MSG_DEBUG, " EAPOL-Key");
805 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
806 wpa_printf(MSG_DEBUG, " Dropped key data from "
807 "unauthorized Supplicant");
812 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
813 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
814 /* TODO: implement support for this; show data */
818 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
819 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
823 eapol_auth_step(sta->eapol_sm);
828 * ieee802_1x_new_station - Start IEEE 802.1X authentication
829 * @hapd: hostapd BSS data
832 * This function is called to start IEEE 802.1X authentication when a new
833 * station completes IEEE 802.11 association.
835 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
837 struct rsn_pmksa_cache_entry *pmksa;
842 if (hapd->conf->wps_state && hapd->conf->wpa &&
843 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
845 * Need to enable IEEE 802.1X/EAPOL state machines for possible
846 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
847 * authentication in this BSS.
851 #endif /* CONFIG_WPS */
853 if ((!force_1x && !hapd->conf->ieee802_1x) ||
854 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
857 if (sta->eapol_sm == NULL) {
858 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
859 HOSTAPD_LEVEL_DEBUG, "start authentication");
860 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
861 if (sta->eapol_sm == NULL) {
862 hostapd_logger(hapd, sta->addr,
863 HOSTAPD_MODULE_IEEE8021X,
865 "failed to allocate state machine");
872 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
873 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
875 * Delay EAPOL frame transmission until a possible WPS
876 * initiates the handshake with EAPOL-Start.
878 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
880 #endif /* CONFIG_WPS */
882 sta->eapol_sm->eap_if->portEnabled = TRUE;
884 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
888 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
890 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
891 /* Setup EAPOL state machines to already authenticated state
892 * because of existing PMKSA information in the cache. */
893 sta->eapol_sm->keyRun = TRUE;
894 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
895 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
896 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
897 sta->eapol_sm->authSuccess = TRUE;
898 if (sta->eapol_sm->eap)
899 eap_sm_notify_cached(sta->eapol_sm->eap);
900 old_vlanid = sta->vlan_id;
901 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
902 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
904 ap_sta_bind_vlan(hapd, sta, old_vlanid);
908 * Force EAPOL state machines to start
909 * re-authentication without having to wait for the
910 * Supplicant to send EAPOL-Start.
912 sta->eapol_sm->reAuthenticate = TRUE;
914 eapol_auth_step(sta->eapol_sm);
919 void ieee802_1x_free_station(struct sta_info *sta)
921 struct eapol_state_machine *sm = sta->eapol_sm;
926 sta->eapol_sm = NULL;
928 #ifndef CONFIG_NO_RADIUS
929 if (sm->last_recv_radius) {
930 radius_msg_free(sm->last_recv_radius);
931 os_free(sm->last_recv_radius);
933 radius_free_class(&sm->radius_class);
934 #endif /* CONFIG_NO_RADIUS */
936 os_free(sm->identity);
941 #ifndef CONFIG_NO_RADIUS
942 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
943 struct sta_info *sta)
950 struct radius_msg *msg;
951 struct eapol_state_machine *sm = sta->eapol_sm;
953 if (sm == NULL || sm->last_recv_radius == NULL) {
955 sm->eap_if->aaaEapNoReq = TRUE;
959 msg = sm->last_recv_radius;
961 eap = radius_msg_get_eap(msg, &len);
963 /* RFC 3579, Chap. 2.6.3:
964 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
966 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
967 HOSTAPD_LEVEL_WARNING, "could not extract "
968 "EAP-Message from RADIUS message");
969 sm->eap_if->aaaEapNoReq = TRUE;
973 if (len < sizeof(*hdr)) {
974 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
975 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
976 "received from authentication server");
978 sm->eap_if->aaaEapNoReq = TRUE;
982 if (len > sizeof(*hdr))
983 eap_type = eap[sizeof(*hdr)];
985 hdr = (struct eap_hdr *) eap;
987 case EAP_CODE_REQUEST:
989 sm->eap_type_authsrv = eap_type;
990 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
991 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
995 case EAP_CODE_RESPONSE:
996 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
997 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
1001 case EAP_CODE_SUCCESS:
1002 os_strlcpy(buf, "EAP Success", sizeof(buf));
1004 case EAP_CODE_FAILURE:
1005 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1008 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1011 buf[sizeof(buf) - 1] = '\0';
1012 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1013 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1014 "id=%d len=%d) from RADIUS server: %s",
1015 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1017 sm->eap_if->aaaEapReq = TRUE;
1019 wpabuf_free(sm->eap_if->aaaEapReqData);
1020 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1024 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1025 struct sta_info *sta, struct radius_msg *msg,
1026 struct radius_msg *req,
1027 const u8 *shared_secret,
1028 size_t shared_secret_len)
1030 struct radius_ms_mppe_keys *keys;
1031 struct eapol_state_machine *sm = sta->eapol_sm;
1035 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1038 if (keys && keys->send && keys->recv) {
1039 size_t len = keys->send_len + keys->recv_len;
1040 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1041 keys->send, keys->send_len);
1042 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1043 keys->recv, keys->recv_len);
1045 os_free(sm->eap_if->aaaEapKeyData);
1046 sm->eap_if->aaaEapKeyData = os_malloc(len);
1047 if (sm->eap_if->aaaEapKeyData) {
1048 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1050 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1051 keys->send, keys->send_len);
1052 sm->eap_if->aaaEapKeyDataLen = len;
1053 sm->eap_if->aaaEapKeyAvailable = TRUE;
1058 os_free(keys->send);
1059 os_free(keys->recv);
1065 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1066 struct sta_info *sta,
1067 struct radius_msg *msg)
1071 struct eapol_state_machine *sm = sta->eapol_sm;
1073 struct radius_attr_data *nclass;
1074 size_t nclass_count;
1076 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1080 radius_free_class(&sm->radius_class);
1081 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1085 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1092 for (i = 0; i < count; i++) {
1094 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1100 } while (class_len < 1);
1102 nclass[nclass_count].data = os_malloc(class_len);
1103 if (nclass[nclass_count].data == NULL)
1106 os_memcpy(nclass[nclass_count].data, class, class_len);
1107 nclass[nclass_count].len = class_len;
1111 sm->radius_class.attr = nclass;
1112 sm->radius_class.count = nclass_count;
1113 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1114 "attributes for " MACSTR,
1115 (unsigned long) sm->radius_class.count,
1116 MAC2STR(sta->addr));
1120 /* Update sta->identity based on User-Name attribute in Access-Accept */
1121 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1122 struct sta_info *sta,
1123 struct radius_msg *msg)
1127 struct eapol_state_machine *sm = sta->eapol_sm;
1132 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1136 identity = os_malloc(len + 1);
1137 if (identity == NULL)
1140 os_memcpy(identity, buf, len);
1141 identity[len] = '\0';
1143 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1144 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1145 "User-Name from Access-Accept '%s'",
1146 sm->identity ? (char *) sm->identity : "N/A",
1149 os_free(sm->identity);
1150 sm->identity = identity;
1151 sm->identity_len = len;
1155 struct sta_id_search {
1157 struct eapol_state_machine *sm;
1161 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1162 struct sta_info *sta,
1165 struct sta_id_search *id_search = ctx;
1166 struct eapol_state_machine *sm = sta->eapol_sm;
1168 if (sm && sm->radius_identifier >= 0 &&
1169 sm->radius_identifier == id_search->identifier) {
1177 static struct eapol_state_machine *
1178 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1180 struct sta_id_search id_search;
1181 id_search.identifier = identifier;
1182 id_search.sm = NULL;
1183 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1184 return id_search.sm;
1189 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1190 * @msg: RADIUS response message
1191 * @req: RADIUS request message
1192 * @shared_secret: RADIUS shared secret
1193 * @shared_secret_len: Length of shared_secret in octets
1194 * @data: Context data (struct hostapd_data *)
1195 * Returns: Processing status
1197 static RadiusRxResult
1198 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1199 const u8 *shared_secret, size_t shared_secret_len,
1202 struct hostapd_data *hapd = data;
1203 struct sta_info *sta;
1204 u32 session_timeout = 0, termination_action, acct_interim_interval;
1205 int session_timeout_set, old_vlanid = 0;
1206 struct eapol_state_machine *sm;
1207 int override_eapReq = 0;
1209 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1211 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1212 "station for this RADIUS message");
1213 return RADIUS_RX_UNKNOWN;
1217 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1218 * present when packet contains an EAP-Message attribute */
1219 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1220 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1222 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1223 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1224 "Message-Authenticator since it does not include "
1226 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1228 printf("Incoming RADIUS packet did not have correct "
1229 "Message-Authenticator - dropped\n");
1230 return RADIUS_RX_INVALID_AUTHENTICATOR;
1233 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1234 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1235 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1236 printf("Unknown RADIUS message code\n");
1237 return RADIUS_RX_UNKNOWN;
1240 sm->radius_identifier = -1;
1241 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1242 MAC2STR(sta->addr));
1244 if (sm->last_recv_radius) {
1245 radius_msg_free(sm->last_recv_radius);
1246 os_free(sm->last_recv_radius);
1249 sm->last_recv_radius = msg;
1251 session_timeout_set =
1252 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1254 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1255 &termination_action))
1256 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1258 if (hapd->conf->acct_interim_interval == 0 &&
1259 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1260 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1261 &acct_interim_interval) == 0) {
1262 if (acct_interim_interval < 60) {
1263 hostapd_logger(hapd, sta->addr,
1264 HOSTAPD_MODULE_IEEE8021X,
1266 "ignored too small "
1267 "Acct-Interim-Interval %d",
1268 acct_interim_interval);
1270 sta->acct_interim_interval = acct_interim_interval;
1274 switch (msg->hdr->code) {
1275 case RADIUS_CODE_ACCESS_ACCEPT:
1276 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1278 #ifndef CONFIG_NO_VLAN
1280 old_vlanid = sta->vlan_id;
1281 sta->vlan_id = radius_msg_get_vlanid(msg);
1283 if (sta->vlan_id > 0 &&
1284 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1286 hostapd_logger(hapd, sta->addr,
1287 HOSTAPD_MODULE_RADIUS,
1289 "VLAN ID %d", sta->vlan_id);
1290 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1291 sta->eapol_sm->authFail = TRUE;
1292 hostapd_logger(hapd, sta->addr,
1293 HOSTAPD_MODULE_IEEE8021X,
1294 HOSTAPD_LEVEL_INFO, "authentication "
1295 "server did not include required VLAN "
1296 "ID in Access-Accept");
1299 #endif /* CONFIG_NO_VLAN */
1301 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1303 /* RFC 3580, Ch. 3.17 */
1304 if (session_timeout_set && termination_action ==
1305 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1306 sm->reAuthPeriod = session_timeout;
1307 } else if (session_timeout_set)
1308 ap_sta_session_timeout(hapd, sta, session_timeout);
1310 sm->eap_if->aaaSuccess = TRUE;
1311 override_eapReq = 1;
1312 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1314 ieee802_1x_store_radius_class(hapd, sta, msg);
1315 ieee802_1x_update_sta_identity(hapd, sta, msg);
1316 if (sm->eap_if->eapKeyAvailable &&
1317 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1318 session_timeout_set ?
1319 (int) session_timeout : -1, sm) == 0) {
1320 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1321 HOSTAPD_LEVEL_DEBUG,
1322 "Added PMKSA cache entry");
1325 case RADIUS_CODE_ACCESS_REJECT:
1326 sm->eap_if->aaaFail = TRUE;
1327 override_eapReq = 1;
1329 case RADIUS_CODE_ACCESS_CHALLENGE:
1330 sm->eap_if->aaaEapReq = TRUE;
1331 if (session_timeout_set) {
1332 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1333 sm->eap_if->aaaMethodTimeout = session_timeout;
1334 hostapd_logger(hapd, sm->addr,
1335 HOSTAPD_MODULE_IEEE8021X,
1336 HOSTAPD_LEVEL_DEBUG,
1337 "using EAP timeout of %d seconds (from "
1339 sm->eap_if->aaaMethodTimeout);
1342 * Use dynamic retransmission behavior per EAP
1345 sm->eap_if->aaaMethodTimeout = 0;
1350 ieee802_1x_decapsulate_radius(hapd, sta);
1351 if (override_eapReq)
1352 sm->eap_if->aaaEapReq = FALSE;
1354 eapol_auth_step(sm);
1356 return RADIUS_RX_QUEUED;
1358 #endif /* CONFIG_NO_RADIUS */
1361 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1363 struct eapol_state_machine *sm = sta->eapol_sm;
1367 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1368 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1370 #ifndef CONFIG_NO_RADIUS
1371 if (sm->last_recv_radius) {
1372 radius_msg_free(sm->last_recv_radius);
1373 os_free(sm->last_recv_radius);
1374 sm->last_recv_radius = NULL;
1376 #endif /* CONFIG_NO_RADIUS */
1378 if (sm->eap_if->eapTimeout) {
1380 * Disconnect the STA since it did not reply to the last EAP
1381 * request and we cannot continue EAP processing (EAP-Failure
1382 * could only be sent if the EAP peer actually replied).
1384 sm->eap_if->portEnabled = FALSE;
1385 hostapd_sta_deauth(hapd, sta->addr,
1386 WLAN_REASON_PREV_AUTH_NOT_VALID);
1387 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1388 WLAN_STA_AUTHORIZED);
1389 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1390 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1391 sta->timeout_next = STA_REMOVE;
1396 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1398 struct eapol_authenticator *eapol = hapd->eapol_auth;
1400 if (hapd->conf->default_wep_key_len < 1)
1403 os_free(eapol->default_wep_key);
1404 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1405 if (eapol->default_wep_key == NULL ||
1406 os_get_random(eapol->default_wep_key,
1407 hapd->conf->default_wep_key_len)) {
1408 printf("Could not generate random WEP key.\n");
1409 os_free(eapol->default_wep_key);
1410 eapol->default_wep_key = NULL;
1414 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1415 eapol->default_wep_key,
1416 hapd->conf->default_wep_key_len);
1422 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1423 struct sta_info *sta, void *ctx)
1425 if (sta->eapol_sm) {
1426 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1427 eapol_auth_step(sta->eapol_sm);
1433 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1435 struct hostapd_data *hapd = eloop_ctx;
1436 struct eapol_authenticator *eapol = hapd->eapol_auth;
1438 if (eapol->default_wep_key_idx >= 3)
1439 eapol->default_wep_key_idx =
1440 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1442 eapol->default_wep_key_idx++;
1444 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1445 eapol->default_wep_key_idx);
1447 if (ieee802_1x_rekey_broadcast(hapd)) {
1448 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1449 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1450 "new broadcast key");
1451 os_free(eapol->default_wep_key);
1452 eapol->default_wep_key = NULL;
1456 /* TODO: Could setup key for RX here, but change default TX keyid only
1457 * after new broadcast key has been sent to all stations. */
1458 if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
1459 eapol->default_wep_key_idx, 1, NULL, 0,
1460 eapol->default_wep_key,
1461 hapd->conf->default_wep_key_len)) {
1462 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1463 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1464 "new broadcast key");
1465 os_free(eapol->default_wep_key);
1466 eapol->default_wep_key = NULL;
1470 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1472 if (hapd->conf->wep_rekeying_period > 0) {
1473 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1474 ieee802_1x_rekey, hapd, NULL);
1479 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1480 const u8 *data, size_t datalen)
1482 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1486 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1487 const u8 *data, size_t datalen)
1489 #ifndef CONFIG_NO_RADIUS
1490 struct hostapd_data *hapd = ctx;
1491 struct sta_info *sta = sta_ctx;
1493 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1494 #endif /* CONFIG_NO_RADIUS */
1498 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1501 struct hostapd_data *hapd = ctx;
1502 struct sta_info *sta = sta_ctx;
1504 rsn_preauth_finished(hapd, sta, success);
1506 ieee802_1x_finished(hapd, sta, success);
1510 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1511 size_t identity_len, int phase2,
1512 struct eap_user *user)
1514 struct hostapd_data *hapd = ctx;
1515 const struct hostapd_eap_user *eap_user;
1518 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1519 identity_len, phase2);
1520 if (eap_user == NULL)
1523 os_memset(user, 0, sizeof(*user));
1524 user->phase2 = phase2;
1525 count = EAP_USER_MAX_METHODS;
1526 if (count > EAP_MAX_METHODS)
1527 count = EAP_MAX_METHODS;
1528 for (i = 0; i < count; i++) {
1529 user->methods[i].vendor = eap_user->methods[i].vendor;
1530 user->methods[i].method = eap_user->methods[i].method;
1533 if (eap_user->password) {
1534 user->password = os_malloc(eap_user->password_len);
1535 if (user->password == NULL)
1537 os_memcpy(user->password, eap_user->password,
1538 eap_user->password_len);
1539 user->password_len = eap_user->password_len;
1541 user->force_version = eap_user->force_version;
1542 user->ttls_auth = eap_user->ttls_auth;
1548 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1550 struct hostapd_data *hapd = ctx;
1551 struct sta_info *sta;
1552 sta = ap_get_sta(hapd, addr);
1553 if (sta == NULL || sta->eapol_sm == NULL)
1559 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1560 eapol_logger_level level, const char *txt)
1562 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1563 struct hostapd_data *hapd = ctx;
1567 case EAPOL_LOGGER_WARNING:
1568 hlevel = HOSTAPD_LEVEL_WARNING;
1570 case EAPOL_LOGGER_INFO:
1571 hlevel = HOSTAPD_LEVEL_INFO;
1573 case EAPOL_LOGGER_DEBUG:
1575 hlevel = HOSTAPD_LEVEL_DEBUG;
1579 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1581 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1585 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1588 struct hostapd_data *hapd = ctx;
1589 struct sta_info *sta = sta_ctx;
1590 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1594 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1596 struct hostapd_data *hapd = ctx;
1597 struct sta_info *sta = sta_ctx;
1598 ieee802_1x_abort_auth(hapd, sta);
1602 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1604 struct hostapd_data *hapd = ctx;
1605 struct sta_info *sta = sta_ctx;
1606 ieee802_1x_tx_key(hapd, sta);
1610 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1611 enum eapol_event type)
1613 /* struct hostapd_data *hapd = ctx; */
1614 struct sta_info *sta = sta_ctx;
1616 case EAPOL_AUTH_SM_CHANGE:
1617 wpa_auth_sm_notify(sta->wpa_sm);
1619 case EAPOL_AUTH_REAUTHENTICATE:
1620 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1626 int ieee802_1x_init(struct hostapd_data *hapd)
1629 struct eapol_auth_config conf;
1630 struct eapol_auth_cb cb;
1632 os_memset(&conf, 0, sizeof(conf));
1634 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1635 conf.wpa = hapd->conf->wpa;
1636 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1637 conf.eap_server = hapd->conf->eap_server;
1638 conf.ssl_ctx = hapd->ssl_ctx;
1639 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1640 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1641 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1642 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1643 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1644 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1645 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1646 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1647 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1648 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1649 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1650 conf.tnc = hapd->conf->tnc;
1651 conf.wps = hapd->wps;
1653 os_memset(&cb, 0, sizeof(cb));
1654 cb.eapol_send = ieee802_1x_eapol_send;
1655 cb.aaa_send = ieee802_1x_aaa_send;
1656 cb.finished = _ieee802_1x_finished;
1657 cb.get_eap_user = ieee802_1x_get_eap_user;
1658 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1659 cb.logger = ieee802_1x_logger;
1660 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1661 cb.abort_auth = _ieee802_1x_abort_auth;
1662 cb.tx_key = _ieee802_1x_tx_key;
1663 cb.eapol_event = ieee802_1x_eapol_event;
1665 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1666 if (hapd->eapol_auth == NULL)
1669 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1670 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
1673 #ifndef CONFIG_NO_RADIUS
1674 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1675 ieee802_1x_receive_auth, hapd))
1677 #endif /* CONFIG_NO_RADIUS */
1679 if (hapd->conf->default_wep_key_len) {
1680 hostapd_set_privacy(hapd, 1);
1682 for (i = 0; i < 4; i++)
1683 hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
1684 NULL, i, 0, NULL, 0, NULL, 0);
1686 ieee802_1x_rekey(hapd, NULL);
1688 if (hapd->eapol_auth->default_wep_key == NULL)
1696 void ieee802_1x_deinit(struct hostapd_data *hapd)
1698 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1700 if (hapd->driver != NULL &&
1701 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1702 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
1704 eapol_auth_deinit(hapd->eapol_auth);
1705 hapd->eapol_auth = NULL;
1709 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1710 const u8 *buf, size_t len, int ack)
1712 struct ieee80211_hdr *hdr;
1713 struct ieee802_1x_hdr *xhdr;
1714 struct ieee802_1x_eapol_key *key;
1716 const unsigned char rfc1042_hdr[ETH_ALEN] =
1717 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1721 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1724 hdr = (struct ieee80211_hdr *) buf;
1725 pos = (u8 *) (hdr + 1);
1726 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1728 pos += sizeof(rfc1042_hdr);
1729 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1733 xhdr = (struct ieee802_1x_hdr *) pos;
1734 pos += sizeof(*xhdr);
1736 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1737 "type=%d length=%d - ack=%d",
1738 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1739 be_to_host16(xhdr->length), ack);
1741 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1742 * or Authenticator state machines, but EAPOL-Key packets are not
1743 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1744 * packets couple of times because otherwise STA keys become
1745 * unsynchronized with AP. */
1746 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1747 pos + sizeof(*key) <= buf + len) {
1748 key = (struct ieee802_1x_eapol_key *) pos;
1749 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1750 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1751 "frame (%scast index=%d)",
1752 key->key_index & BIT(7) ? "uni" : "broad",
1753 key->key_index & ~BIT(7));
1754 /* TODO: re-send EAPOL-Key couple of times (with short delay
1755 * between them?). If all attempt fail, report error and
1756 * deauthenticate STA so that it will get new keys when
1757 * authenticating again (e.g., after returning in range).
1758 * Separate limit/transmit state needed both for unicast and
1759 * broadcast keys(?) */
1761 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1762 * to here and change the key only if the EAPOL-Key packet was Acked.
1769 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1771 if (sm == NULL || sm->identity == NULL)
1774 *len = sm->identity_len;
1775 return sm->identity;
1779 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1782 if (sm == NULL || sm->radius_class.attr == NULL ||
1783 idx >= (int) sm->radius_class.count)
1786 *len = sm->radius_class.attr[idx].len;
1787 return sm->radius_class.attr[idx].data;
1791 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1796 *len = sm->eap_if->eapKeyDataLen;
1797 return sm->eap_if->eapKeyData;
1801 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1806 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1807 eapol_auth_step(sm);
1811 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1816 sm->portValid = valid ? TRUE : FALSE;
1817 eapol_auth_step(sm);
1821 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1826 sm->flags |= EAPOL_SM_PREAUTH;
1828 sm->flags &= ~EAPOL_SM_PREAUTH;
1832 static const char * bool_txt(Boolean bool)
1834 return bool ? "TRUE" : "FALSE";
1838 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1845 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1846 char *buf, size_t buflen)
1849 struct eapol_state_machine *sm = sta->eapol_sm;
1854 ret = os_snprintf(buf + len, buflen - len,
1855 "dot1xPaePortNumber=%d\n"
1856 "dot1xPaePortProtocolVersion=%d\n"
1857 "dot1xPaePortCapabilities=1\n"
1858 "dot1xPaePortInitialize=%d\n"
1859 "dot1xPaePortReauthenticate=FALSE\n",
1863 if (ret < 0 || (size_t) ret >= buflen - len)
1867 /* dot1xAuthConfigTable */
1868 ret = os_snprintf(buf + len, buflen - len,
1869 "dot1xAuthPaeState=%d\n"
1870 "dot1xAuthBackendAuthState=%d\n"
1871 "dot1xAuthAdminControlledDirections=%d\n"
1872 "dot1xAuthOperControlledDirections=%d\n"
1873 "dot1xAuthAuthControlledPortStatus=%d\n"
1874 "dot1xAuthAuthControlledPortControl=%d\n"
1875 "dot1xAuthQuietPeriod=%u\n"
1876 "dot1xAuthServerTimeout=%u\n"
1877 "dot1xAuthReAuthPeriod=%u\n"
1878 "dot1xAuthReAuthEnabled=%s\n"
1879 "dot1xAuthKeyTxEnabled=%s\n",
1880 sm->auth_pae_state + 1,
1881 sm->be_auth_state + 1,
1882 sm->adminControlledDirections,
1883 sm->operControlledDirections,
1889 bool_txt(sm->reAuthEnabled),
1890 bool_txt(sm->keyTxEnabled));
1891 if (ret < 0 || (size_t) ret >= buflen - len)
1895 /* dot1xAuthStatsTable */
1896 ret = os_snprintf(buf + len, buflen - len,
1897 "dot1xAuthEapolFramesRx=%u\n"
1898 "dot1xAuthEapolFramesTx=%u\n"
1899 "dot1xAuthEapolStartFramesRx=%u\n"
1900 "dot1xAuthEapolLogoffFramesRx=%u\n"
1901 "dot1xAuthEapolRespIdFramesRx=%u\n"
1902 "dot1xAuthEapolRespFramesRx=%u\n"
1903 "dot1xAuthEapolReqIdFramesTx=%u\n"
1904 "dot1xAuthEapolReqFramesTx=%u\n"
1905 "dot1xAuthInvalidEapolFramesRx=%u\n"
1906 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1907 "dot1xAuthLastEapolFrameVersion=%u\n"
1908 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1909 sm->dot1xAuthEapolFramesRx,
1910 sm->dot1xAuthEapolFramesTx,
1911 sm->dot1xAuthEapolStartFramesRx,
1912 sm->dot1xAuthEapolLogoffFramesRx,
1913 sm->dot1xAuthEapolRespIdFramesRx,
1914 sm->dot1xAuthEapolRespFramesRx,
1915 sm->dot1xAuthEapolReqIdFramesTx,
1916 sm->dot1xAuthEapolReqFramesTx,
1917 sm->dot1xAuthInvalidEapolFramesRx,
1918 sm->dot1xAuthEapLengthErrorFramesRx,
1919 sm->dot1xAuthLastEapolFrameVersion,
1921 if (ret < 0 || (size_t) ret >= buflen - len)
1925 /* dot1xAuthDiagTable */
1926 ret = os_snprintf(buf + len, buflen - len,
1927 "dot1xAuthEntersConnecting=%u\n"
1928 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1929 "dot1xAuthEntersAuthenticating=%u\n"
1930 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1931 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1932 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1933 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1934 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1935 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1936 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1937 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1938 "dot1xAuthBackendResponses=%u\n"
1939 "dot1xAuthBackendAccessChallenges=%u\n"
1940 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1941 "dot1xAuthBackendAuthSuccesses=%u\n"
1942 "dot1xAuthBackendAuthFails=%u\n",
1943 sm->authEntersConnecting,
1944 sm->authEapLogoffsWhileConnecting,
1945 sm->authEntersAuthenticating,
1946 sm->authAuthSuccessesWhileAuthenticating,
1947 sm->authAuthTimeoutsWhileAuthenticating,
1948 sm->authAuthFailWhileAuthenticating,
1949 sm->authAuthEapStartsWhileAuthenticating,
1950 sm->authAuthEapLogoffWhileAuthenticating,
1951 sm->authAuthReauthsWhileAuthenticated,
1952 sm->authAuthEapStartsWhileAuthenticated,
1953 sm->authAuthEapLogoffWhileAuthenticated,
1954 sm->backendResponses,
1955 sm->backendAccessChallenges,
1956 sm->backendOtherRequestsToSupplicant,
1957 sm->backendAuthSuccesses,
1958 sm->backendAuthFails);
1959 if (ret < 0 || (size_t) ret >= buflen - len)
1963 /* dot1xAuthSessionStatsTable */
1964 ret = os_snprintf(buf + len, buflen - len,
1965 /* TODO: dot1xAuthSessionOctetsRx */
1966 /* TODO: dot1xAuthSessionOctetsTx */
1967 /* TODO: dot1xAuthSessionFramesRx */
1968 /* TODO: dot1xAuthSessionFramesTx */
1969 "dot1xAuthSessionId=%08X-%08X\n"
1970 "dot1xAuthSessionAuthenticMethod=%d\n"
1971 "dot1xAuthSessionTime=%u\n"
1972 "dot1xAuthSessionTerminateCause=999\n"
1973 "dot1xAuthSessionUserName=%s\n",
1974 sta->acct_session_id_hi, sta->acct_session_id_lo,
1975 (wpa_key_mgmt_wpa_ieee8021x(
1976 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1978 (unsigned int) (time(NULL) -
1979 sta->acct_session_start),
1981 if (ret < 0 || (size_t) ret >= buflen - len)
1989 static void ieee802_1x_finished(struct hostapd_data *hapd,
1990 struct sta_info *sta, int success)
1994 /* TODO: get PMKLifetime from WPA parameters */
1995 static const int dot11RSNAConfigPMKLifetime = 43200;
1997 key = ieee802_1x_get_key(sta->eapol_sm, &len);
1998 if (success && key && len >= PMK_LEN &&
1999 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2000 sta->eapol_sm) == 0) {
2001 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2002 HOSTAPD_LEVEL_DEBUG,
2003 "Added PMKSA cache entry (IEEE 802.1X)");