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 "ieee802_1x.h"
20 #include "accounting.h"
21 #include "radius/radius.h"
22 #include "radius/radius_client.h"
23 #include "eapol_auth/eapol_auth_sm.h"
27 #include "sta_flags.h"
31 #include "pmksa_cache.h"
33 #include "hw_features.h"
34 #include "eap_server/eap.h"
35 #include "common/ieee802_11_defs.h"
36 #include "common/wpa_ctrl.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, sta->flags,
94 WLAN_STA_AUTHORIZED, ~0);
95 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
96 HOSTAPD_LEVEL_DEBUG, "authorizing port");
98 if ((sta->flags & (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC)) ==
99 (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC))
100 wpa_msg(hapd->msg_ctx, MSG_INFO,
101 AP_STA_DISCONNECTED MACSTR,
103 sta->flags &= ~WLAN_STA_AUTHORIZED;
104 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
105 0, ~WLAN_STA_AUTHORIZED);
106 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
107 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
110 if (res && errno != ENOENT) {
111 printf("Could not set station " MACSTR " flags for kernel "
112 "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
116 accounting_sta_start(hapd, sta);
120 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
121 struct sta_info *sta,
122 int idx, int broadcast,
123 u8 *key_data, size_t key_len)
126 struct ieee802_1x_hdr *hdr;
127 struct ieee802_1x_eapol_key *key;
128 size_t len, ekey_len;
129 struct eapol_state_machine *sm = sta->eapol_sm;
134 len = sizeof(*key) + key_len;
135 buf = os_zalloc(sizeof(*hdr) + len);
139 hdr = (struct ieee802_1x_hdr *) buf;
140 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
141 key->type = EAPOL_KEY_TYPE_RC4;
142 key->key_length = htons(key_len);
143 wpa_get_ntp_timestamp(key->replay_counter);
145 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
146 wpa_printf(MSG_ERROR, "Could not get random numbers");
151 key->key_index = idx | (broadcast ? 0 : BIT(7));
152 if (hapd->conf->eapol_key_index_workaround) {
153 /* According to some information, WinXP Supplicant seems to
154 * interpret bit7 as an indication whether the key is to be
155 * activated, so make it possible to enable workaround that
156 * sets this bit for all keys. */
157 key->key_index |= BIT(7);
160 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
161 * MSK[32..63] is used to sign the message. */
162 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
163 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
164 "and signing EAPOL-Key");
168 os_memcpy((u8 *) (key + 1), key_data, key_len);
169 ekey_len = sizeof(key->key_iv) + 32;
170 ekey = os_malloc(ekey_len);
172 wpa_printf(MSG_ERROR, "Could not encrypt key");
176 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
177 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
178 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
181 /* This header is needed here for HMAC-MD5, but it will be regenerated
182 * in ieee802_1x_send() */
183 hdr->version = hapd->conf->eapol_version;
184 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
185 hdr->length = host_to_be16(len);
186 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
189 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
190 " (%s index=%d)", MAC2STR(sm->addr),
191 broadcast ? "broadcast" : "unicast", idx);
192 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
194 sta->eapol_sm->dot1xAuthEapolFramesTx++;
199 #ifndef CONFIG_NO_VLAN
200 static struct hostapd_wep_keys *
201 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
203 struct hostapd_wep_keys *key;
205 key = os_zalloc(sizeof(*key));
209 key->default_len = hapd->conf->default_wep_key_len;
211 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
212 key->idx < hapd->conf->broadcast_key_idx_min)
213 key->idx = hapd->conf->broadcast_key_idx_min;
217 if (!key->key[key->idx])
218 key->key[key->idx] = os_malloc(key->default_len);
219 if (key->key[key->idx] == NULL ||
220 os_get_random(key->key[key->idx], key->default_len)) {
221 printf("Could not generate random WEP key (dynamic VLAN).\n");
222 os_free(key->key[key->idx]);
223 key->key[key->idx] = NULL;
227 key->len[key->idx] = key->default_len;
229 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
231 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
232 key->key[key->idx], key->len[key->idx]);
234 if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL, key->idx, 1,
235 NULL, 0, key->key[key->idx], key->len[key->idx]))
236 printf("Could not set dynamic VLAN WEP encryption key.\n");
238 hostapd_set_ieee8021x(ifname, hapd, 1);
244 static struct hostapd_wep_keys *
245 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
253 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
254 ssid->dyn_vlan_keys[vlan_id])
255 return ssid->dyn_vlan_keys[vlan_id];
257 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
258 "state machine for VLAN ID %lu",
259 (unsigned long) vlan_id);
261 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
262 if (ifname == NULL) {
263 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
264 "cannot create group key state machine",
265 (unsigned long) vlan_id);
269 if (ssid->dyn_vlan_keys == NULL) {
270 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
271 ssid->dyn_vlan_keys = os_zalloc(size);
272 if (ssid->dyn_vlan_keys == NULL)
274 ssid->max_dyn_vlan_keys = vlan_id;
277 if (ssid->max_dyn_vlan_keys < vlan_id) {
278 struct hostapd_wep_keys **na;
279 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
280 na = os_realloc(ssid->dyn_vlan_keys, size);
283 ssid->dyn_vlan_keys = na;
284 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
285 (vlan_id - ssid->max_dyn_vlan_keys) *
286 sizeof(ssid->dyn_vlan_keys[0]));
287 ssid->max_dyn_vlan_keys = vlan_id;
290 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
292 return ssid->dyn_vlan_keys[vlan_id];
294 #endif /* CONFIG_NO_VLAN */
297 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
299 struct eapol_authenticator *eapol = hapd->eapol_auth;
300 struct eapol_state_machine *sm = sta->eapol_sm;
301 #ifndef CONFIG_NO_VLAN
302 struct hostapd_wep_keys *key = NULL;
304 #endif /* CONFIG_NO_VLAN */
306 if (sm == NULL || !sm->eap_if->eapKeyData)
309 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
312 #ifndef CONFIG_NO_VLAN
313 vlan_id = sta->vlan_id;
314 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
318 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
319 if (key && key->key[key->idx])
320 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
324 #endif /* CONFIG_NO_VLAN */
325 if (eapol->default_wep_key) {
326 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
327 eapol->default_wep_key,
328 hapd->conf->default_wep_key_len);
331 if (hapd->conf->individual_wep_key_len > 0) {
333 ikey = os_malloc(hapd->conf->individual_wep_key_len);
335 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
336 wpa_printf(MSG_ERROR, "Could not generate random "
337 "individual WEP key.");
342 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
343 ikey, hapd->conf->individual_wep_key_len);
345 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
346 hapd->conf->individual_wep_key_len);
348 /* TODO: set encryption in TX callback, i.e., only after STA
349 * has ACKed EAPOL-Key frame */
350 if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
351 sta->addr, 0, 1, NULL, 0, ikey,
352 hapd->conf->individual_wep_key_len)) {
353 wpa_printf(MSG_ERROR, "Could not set individual WEP "
362 const char *radius_mode_txt(struct hostapd_data *hapd)
364 if (hapd->iface->current_mode == NULL)
367 switch (hapd->iface->current_mode->mode) {
368 case HOSTAPD_MODE_IEEE80211A:
370 case HOSTAPD_MODE_IEEE80211G:
372 case HOSTAPD_MODE_IEEE80211B:
379 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
384 for (i = 0; i < sta->supported_rates_len; i++)
385 if ((sta->supported_rates[i] & 0x7f) > rate)
386 rate = sta->supported_rates[i] & 0x7f;
392 #ifndef CONFIG_NO_RADIUS
393 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
394 struct eapol_state_machine *sm,
395 const u8 *eap, size_t len)
400 if (len <= sizeof(struct eap_hdr) ||
401 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
404 identity = eap_get_identity(sm->eap, &identity_len);
405 if (identity == NULL)
408 /* Save station identity for future RADIUS packets */
409 os_free(sm->identity);
410 sm->identity = os_malloc(identity_len + 1);
411 if (sm->identity == NULL) {
412 sm->identity_len = 0;
416 os_memcpy(sm->identity, identity, identity_len);
417 sm->identity_len = identity_len;
418 sm->identity[identity_len] = '\0';
419 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
420 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
421 sm->dot1xAuthEapolRespIdFramesRx++;
425 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
426 struct sta_info *sta,
427 const u8 *eap, size_t len)
429 struct radius_msg *msg;
431 struct eapol_state_machine *sm = sta->eapol_sm;
436 ieee802_1x_learn_identity(hapd, sm, eap, len);
438 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
441 sm->radius_identifier = radius_client_get_id(hapd->radius);
442 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
443 sm->radius_identifier);
445 printf("Could not create net RADIUS packet\n");
449 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
452 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
453 sm->identity, sm->identity_len)) {
454 printf("Could not add User-Name\n");
458 if (hapd->conf->own_ip_addr.af == AF_INET &&
459 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
460 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
461 printf("Could not add NAS-IP-Address\n");
466 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
467 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
468 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
469 printf("Could not add NAS-IPv6-Address\n");
472 #endif /* CONFIG_IPV6 */
474 if (hapd->conf->nas_identifier &&
475 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
476 (u8 *) hapd->conf->nas_identifier,
477 os_strlen(hapd->conf->nas_identifier))) {
478 printf("Could not add NAS-Identifier\n");
482 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
483 printf("Could not add NAS-Port\n");
487 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
488 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
489 buf[sizeof(buf) - 1] = '\0';
490 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
491 (u8 *) buf, os_strlen(buf))) {
492 printf("Could not add Called-Station-Id\n");
496 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
498 buf[sizeof(buf) - 1] = '\0';
499 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
500 (u8 *) buf, os_strlen(buf))) {
501 printf("Could not add Calling-Station-Id\n");
505 /* TODO: should probably check MTU from driver config; 2304 is max for
506 * IEEE 802.11, but use 1400 to avoid problems with too large packets
508 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
509 printf("Could not add Framed-MTU\n");
513 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
514 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
515 printf("Could not add NAS-Port-Type\n");
519 if (sta->flags & WLAN_STA_PREAUTH) {
520 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
523 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
524 radius_sta_rate(hapd, sta) / 2,
525 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
526 radius_mode_txt(hapd));
527 buf[sizeof(buf) - 1] = '\0';
529 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
530 (u8 *) buf, os_strlen(buf))) {
531 printf("Could not add Connect-Info\n");
535 if (eap && !radius_msg_add_eap(msg, eap, len)) {
536 printf("Could not add EAP-Message\n");
540 /* State attribute must be copied if and only if this packet is
541 * Access-Request reply to the previous Access-Challenge */
542 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
543 RADIUS_CODE_ACCESS_CHALLENGE) {
544 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
547 printf("Could not copy State attribute from previous "
548 "Access-Challenge\n");
552 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
556 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
560 radius_msg_free(msg);
563 #endif /* CONFIG_NO_RADIUS */
566 static void handle_eap_response(struct hostapd_data *hapd,
567 struct sta_info *sta, struct eap_hdr *eap,
571 struct eapol_state_machine *sm = sta->eapol_sm;
575 data = (u8 *) (eap + 1);
577 if (len < sizeof(*eap) + 1) {
578 printf("handle_eap_response: too short response data\n");
582 sm->eap_type_supp = type = data[0];
584 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
585 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
586 "id=%d len=%d) from STA: EAP Response-%s (%d)",
587 eap->code, eap->identifier, be_to_host16(eap->length),
588 eap_server_get_name(0, type), type);
590 sm->dot1xAuthEapolRespFramesRx++;
592 wpabuf_free(sm->eap_if->eapRespData);
593 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
598 /* Process incoming EAP packet from Supplicant */
599 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
605 if (len < sizeof(*eap)) {
606 printf(" too short EAP packet\n");
610 eap = (struct eap_hdr *) buf;
612 eap_len = be_to_host16(eap->length);
613 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
614 eap->code, eap->identifier, eap_len);
615 if (eap_len < sizeof(*eap)) {
616 wpa_printf(MSG_DEBUG, " Invalid EAP length");
618 } else if (eap_len > len) {
619 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
622 } else if (eap_len < len) {
623 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
624 "packet", (unsigned long) len - eap_len);
628 case EAP_CODE_REQUEST:
629 wpa_printf(MSG_DEBUG, " (request)");
631 case EAP_CODE_RESPONSE:
632 wpa_printf(MSG_DEBUG, " (response)");
633 handle_eap_response(hapd, sta, eap, eap_len);
635 case EAP_CODE_SUCCESS:
636 wpa_printf(MSG_DEBUG, " (success)");
638 case EAP_CODE_FAILURE:
639 wpa_printf(MSG_DEBUG, " (failure)");
642 wpa_printf(MSG_DEBUG, " (unknown code)");
648 static struct eapol_state_machine *
649 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
652 if (sta->flags & WLAN_STA_PREAUTH)
653 flags |= EAPOL_SM_PREAUTH;
655 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
656 flags |= EAPOL_SM_USES_WPA;
657 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
658 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
660 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
666 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
667 * @hapd: hostapd BSS data
668 * @sa: Source address (sender of the EAPOL frame)
670 * @len: Length of buf in octets
672 * This function is called for each incoming EAPOL frame from the interface
674 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
677 struct sta_info *sta;
678 struct ieee802_1x_hdr *hdr;
679 struct ieee802_1x_eapol_key *key;
681 struct rsn_pmksa_cache_entry *pmksa;
683 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
684 !hapd->conf->wps_state)
687 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
688 (unsigned long) len, MAC2STR(sa));
689 sta = ap_get_sta(hapd, sa);
690 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
691 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
696 if (len < sizeof(*hdr)) {
697 printf(" too short IEEE 802.1X packet\n");
701 hdr = (struct ieee802_1x_hdr *) buf;
702 datalen = be_to_host16(hdr->length);
703 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
704 hdr->version, hdr->type, datalen);
706 if (len - sizeof(*hdr) < datalen) {
707 printf(" frame too short for this IEEE 802.1X packet\n");
709 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
712 if (len - sizeof(*hdr) > datalen) {
713 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
714 "IEEE 802.1X packet",
715 (unsigned long) len - sizeof(*hdr) - datalen);
719 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
720 sta->eapol_sm->dot1xAuthEapolFramesRx++;
723 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
724 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
725 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
726 (key->type == EAPOL_KEY_TYPE_WPA ||
727 key->type == EAPOL_KEY_TYPE_RSN)) {
728 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
729 sizeof(*hdr) + datalen);
733 if ((!hapd->conf->ieee802_1x &&
734 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
735 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
738 if (!sta->eapol_sm) {
739 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
744 if (!hapd->conf->ieee802_1x &&
745 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
746 WLAN_STA_MAYBE_WPS)) {
748 * Delay EAPOL frame transmission until a possible WPS
749 * STA initiates the handshake with EAPOL-Start.
751 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
753 #endif /* CONFIG_WPS */
755 sta->eapol_sm->eap_if->portEnabled = TRUE;
758 /* since we support version 1, we can ignore version field and proceed
759 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
760 /* TODO: actually, we are not version 1 anymore.. However, Version 2
761 * does not change frame contents, so should be ok to process frames
762 * more or less identically. Some changes might be needed for
763 * verification of fields. */
766 case IEEE802_1X_TYPE_EAP_PACKET:
767 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
770 case IEEE802_1X_TYPE_EAPOL_START:
771 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
772 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
774 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
775 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
777 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
778 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
779 "available - ignore it since "
780 "STA sent EAPOL-Start");
781 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
783 sta->eapol_sm->eapolStart = TRUE;
784 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
785 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
788 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
789 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
790 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
792 sta->acct_terminate_cause =
793 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
794 accounting_sta_stop(hapd, sta);
795 sta->eapol_sm->eapolLogoff = TRUE;
796 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
799 case IEEE802_1X_TYPE_EAPOL_KEY:
800 wpa_printf(MSG_DEBUG, " EAPOL-Key");
801 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
802 wpa_printf(MSG_DEBUG, " Dropped key data from "
803 "unauthorized Supplicant");
808 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
809 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
810 /* TODO: implement support for this; show data */
814 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
815 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
819 eapol_auth_step(sta->eapol_sm);
824 * ieee802_1x_new_station - Start IEEE 802.1X authentication
825 * @hapd: hostapd BSS data
828 * This function is called to start IEEE 802.1X authentication when a new
829 * station completes IEEE 802.11 association.
831 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
833 struct rsn_pmksa_cache_entry *pmksa;
838 if (hapd->conf->wps_state && hapd->conf->wpa &&
839 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
841 * Need to enable IEEE 802.1X/EAPOL state machines for possible
842 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
843 * authentication in this BSS.
847 #endif /* CONFIG_WPS */
849 if ((!force_1x && !hapd->conf->ieee802_1x) ||
850 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
853 if (sta->eapol_sm == NULL) {
854 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
855 HOSTAPD_LEVEL_DEBUG, "start authentication");
856 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
857 if (sta->eapol_sm == NULL) {
858 hostapd_logger(hapd, sta->addr,
859 HOSTAPD_MODULE_IEEE8021X,
861 "failed to allocate state machine");
868 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
869 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
871 * Delay EAPOL frame transmission until a possible WPS
872 * initiates the handshake with EAPOL-Start.
874 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
876 #endif /* CONFIG_WPS */
878 sta->eapol_sm->eap_if->portEnabled = TRUE;
880 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
884 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
886 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
887 /* Setup EAPOL state machines to already authenticated state
888 * because of existing PMKSA information in the cache. */
889 sta->eapol_sm->keyRun = TRUE;
890 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
891 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
892 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
893 sta->eapol_sm->authSuccess = TRUE;
894 if (sta->eapol_sm->eap)
895 eap_sm_notify_cached(sta->eapol_sm->eap);
896 old_vlanid = sta->vlan_id;
897 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
898 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
900 ap_sta_bind_vlan(hapd, sta, old_vlanid);
904 * Force EAPOL state machines to start
905 * re-authentication without having to wait for the
906 * Supplicant to send EAPOL-Start.
908 sta->eapol_sm->reAuthenticate = TRUE;
910 eapol_auth_step(sta->eapol_sm);
915 void ieee802_1x_free_station(struct sta_info *sta)
917 struct eapol_state_machine *sm = sta->eapol_sm;
922 sta->eapol_sm = NULL;
924 #ifndef CONFIG_NO_RADIUS
925 if (sm->last_recv_radius) {
926 radius_msg_free(sm->last_recv_radius);
927 os_free(sm->last_recv_radius);
929 radius_free_class(&sm->radius_class);
930 #endif /* CONFIG_NO_RADIUS */
932 os_free(sm->identity);
937 #ifndef CONFIG_NO_RADIUS
938 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
939 struct sta_info *sta)
946 struct radius_msg *msg;
947 struct eapol_state_machine *sm = sta->eapol_sm;
949 if (sm == NULL || sm->last_recv_radius == NULL) {
951 sm->eap_if->aaaEapNoReq = TRUE;
955 msg = sm->last_recv_radius;
957 eap = radius_msg_get_eap(msg, &len);
959 /* RFC 3579, Chap. 2.6.3:
960 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
962 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
963 HOSTAPD_LEVEL_WARNING, "could not extract "
964 "EAP-Message from RADIUS message");
965 sm->eap_if->aaaEapNoReq = TRUE;
969 if (len < sizeof(*hdr)) {
970 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
971 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
972 "received from authentication server");
974 sm->eap_if->aaaEapNoReq = TRUE;
978 if (len > sizeof(*hdr))
979 eap_type = eap[sizeof(*hdr)];
981 hdr = (struct eap_hdr *) eap;
983 case EAP_CODE_REQUEST:
985 sm->eap_type_authsrv = eap_type;
986 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
987 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
991 case EAP_CODE_RESPONSE:
992 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
993 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
997 case EAP_CODE_SUCCESS:
998 os_strlcpy(buf, "EAP Success", sizeof(buf));
1000 case EAP_CODE_FAILURE:
1001 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1004 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1007 buf[sizeof(buf) - 1] = '\0';
1008 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1009 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1010 "id=%d len=%d) from RADIUS server: %s",
1011 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1013 sm->eap_if->aaaEapReq = TRUE;
1015 wpabuf_free(sm->eap_if->aaaEapReqData);
1016 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1020 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1021 struct sta_info *sta, struct radius_msg *msg,
1022 struct radius_msg *req,
1023 const u8 *shared_secret,
1024 size_t shared_secret_len)
1026 struct radius_ms_mppe_keys *keys;
1027 struct eapol_state_machine *sm = sta->eapol_sm;
1031 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1034 if (keys && keys->send && keys->recv) {
1035 size_t len = keys->send_len + keys->recv_len;
1036 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1037 keys->send, keys->send_len);
1038 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1039 keys->recv, keys->recv_len);
1041 os_free(sm->eap_if->aaaEapKeyData);
1042 sm->eap_if->aaaEapKeyData = os_malloc(len);
1043 if (sm->eap_if->aaaEapKeyData) {
1044 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1046 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1047 keys->send, keys->send_len);
1048 sm->eap_if->aaaEapKeyDataLen = len;
1049 sm->eap_if->aaaEapKeyAvailable = TRUE;
1054 os_free(keys->send);
1055 os_free(keys->recv);
1061 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1062 struct sta_info *sta,
1063 struct radius_msg *msg)
1067 struct eapol_state_machine *sm = sta->eapol_sm;
1069 struct radius_attr_data *nclass;
1070 size_t nclass_count;
1072 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1076 radius_free_class(&sm->radius_class);
1077 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1081 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1088 for (i = 0; i < count; i++) {
1090 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1096 } while (class_len < 1);
1098 nclass[nclass_count].data = os_malloc(class_len);
1099 if (nclass[nclass_count].data == NULL)
1102 os_memcpy(nclass[nclass_count].data, class, class_len);
1103 nclass[nclass_count].len = class_len;
1107 sm->radius_class.attr = nclass;
1108 sm->radius_class.count = nclass_count;
1109 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1110 "attributes for " MACSTR,
1111 (unsigned long) sm->radius_class.count,
1112 MAC2STR(sta->addr));
1116 /* Update sta->identity based on User-Name attribute in Access-Accept */
1117 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1118 struct sta_info *sta,
1119 struct radius_msg *msg)
1123 struct eapol_state_machine *sm = sta->eapol_sm;
1128 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1132 identity = os_malloc(len + 1);
1133 if (identity == NULL)
1136 os_memcpy(identity, buf, len);
1137 identity[len] = '\0';
1139 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1140 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1141 "User-Name from Access-Accept '%s'",
1142 sm->identity ? (char *) sm->identity : "N/A",
1145 os_free(sm->identity);
1146 sm->identity = identity;
1147 sm->identity_len = len;
1151 struct sta_id_search {
1153 struct eapol_state_machine *sm;
1157 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1158 struct sta_info *sta,
1161 struct sta_id_search *id_search = ctx;
1162 struct eapol_state_machine *sm = sta->eapol_sm;
1164 if (sm && sm->radius_identifier >= 0 &&
1165 sm->radius_identifier == id_search->identifier) {
1173 static struct eapol_state_machine *
1174 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1176 struct sta_id_search id_search;
1177 id_search.identifier = identifier;
1178 id_search.sm = NULL;
1179 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1180 return id_search.sm;
1185 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1186 * @msg: RADIUS response message
1187 * @req: RADIUS request message
1188 * @shared_secret: RADIUS shared secret
1189 * @shared_secret_len: Length of shared_secret in octets
1190 * @data: Context data (struct hostapd_data *)
1191 * Returns: Processing status
1193 static RadiusRxResult
1194 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1195 const u8 *shared_secret, size_t shared_secret_len,
1198 struct hostapd_data *hapd = data;
1199 struct sta_info *sta;
1200 u32 session_timeout = 0, termination_action, acct_interim_interval;
1201 int session_timeout_set, old_vlanid = 0;
1202 struct eapol_state_machine *sm;
1203 int override_eapReq = 0;
1205 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1207 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1208 "station for this RADIUS message");
1209 return RADIUS_RX_UNKNOWN;
1213 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1214 * present when packet contains an EAP-Message attribute */
1215 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1216 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1218 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1219 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1220 "Message-Authenticator since it does not include "
1222 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1224 printf("Incoming RADIUS packet did not have correct "
1225 "Message-Authenticator - dropped\n");
1226 return RADIUS_RX_INVALID_AUTHENTICATOR;
1229 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1230 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1231 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1232 printf("Unknown RADIUS message code\n");
1233 return RADIUS_RX_UNKNOWN;
1236 sm->radius_identifier = -1;
1237 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1238 MAC2STR(sta->addr));
1240 if (sm->last_recv_radius) {
1241 radius_msg_free(sm->last_recv_radius);
1242 os_free(sm->last_recv_radius);
1245 sm->last_recv_radius = msg;
1247 session_timeout_set =
1248 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1250 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1251 &termination_action))
1252 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1254 if (hapd->conf->acct_interim_interval == 0 &&
1255 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1256 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1257 &acct_interim_interval) == 0) {
1258 if (acct_interim_interval < 60) {
1259 hostapd_logger(hapd, sta->addr,
1260 HOSTAPD_MODULE_IEEE8021X,
1262 "ignored too small "
1263 "Acct-Interim-Interval %d",
1264 acct_interim_interval);
1266 sta->acct_interim_interval = acct_interim_interval;
1270 switch (msg->hdr->code) {
1271 case RADIUS_CODE_ACCESS_ACCEPT:
1272 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1274 #ifndef CONFIG_NO_VLAN
1276 old_vlanid = sta->vlan_id;
1277 sta->vlan_id = radius_msg_get_vlanid(msg);
1279 if (sta->vlan_id > 0 &&
1280 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1282 hostapd_logger(hapd, sta->addr,
1283 HOSTAPD_MODULE_RADIUS,
1285 "VLAN ID %d", sta->vlan_id);
1286 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1287 sta->eapol_sm->authFail = TRUE;
1288 hostapd_logger(hapd, sta->addr,
1289 HOSTAPD_MODULE_IEEE8021X,
1290 HOSTAPD_LEVEL_INFO, "authentication "
1291 "server did not include required VLAN "
1292 "ID in Access-Accept");
1295 #endif /* CONFIG_NO_VLAN */
1297 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1299 /* RFC 3580, Ch. 3.17 */
1300 if (session_timeout_set && termination_action ==
1301 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1302 sm->reAuthPeriod = session_timeout;
1303 } else if (session_timeout_set)
1304 ap_sta_session_timeout(hapd, sta, session_timeout);
1306 sm->eap_if->aaaSuccess = TRUE;
1307 override_eapReq = 1;
1308 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1310 ieee802_1x_store_radius_class(hapd, sta, msg);
1311 ieee802_1x_update_sta_identity(hapd, sta, msg);
1312 if (sm->eap_if->eapKeyAvailable &&
1313 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1314 session_timeout_set ?
1315 (int) session_timeout : -1, sm) == 0) {
1316 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1317 HOSTAPD_LEVEL_DEBUG,
1318 "Added PMKSA cache entry");
1321 case RADIUS_CODE_ACCESS_REJECT:
1322 sm->eap_if->aaaFail = TRUE;
1323 override_eapReq = 1;
1325 case RADIUS_CODE_ACCESS_CHALLENGE:
1326 sm->eap_if->aaaEapReq = TRUE;
1327 if (session_timeout_set) {
1328 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1329 sm->eap_if->aaaMethodTimeout = session_timeout;
1330 hostapd_logger(hapd, sm->addr,
1331 HOSTAPD_MODULE_IEEE8021X,
1332 HOSTAPD_LEVEL_DEBUG,
1333 "using EAP timeout of %d seconds (from "
1335 sm->eap_if->aaaMethodTimeout);
1338 * Use dynamic retransmission behavior per EAP
1341 sm->eap_if->aaaMethodTimeout = 0;
1346 ieee802_1x_decapsulate_radius(hapd, sta);
1347 if (override_eapReq)
1348 sm->eap_if->aaaEapReq = FALSE;
1350 eapol_auth_step(sm);
1352 return RADIUS_RX_QUEUED;
1354 #endif /* CONFIG_NO_RADIUS */
1357 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1359 struct eapol_state_machine *sm = sta->eapol_sm;
1363 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1364 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1366 #ifndef CONFIG_NO_RADIUS
1367 if (sm->last_recv_radius) {
1368 radius_msg_free(sm->last_recv_radius);
1369 os_free(sm->last_recv_radius);
1370 sm->last_recv_radius = NULL;
1372 #endif /* CONFIG_NO_RADIUS */
1374 if (sm->eap_if->eapTimeout) {
1376 * Disconnect the STA since it did not reply to the last EAP
1377 * request and we cannot continue EAP processing (EAP-Failure
1378 * could only be sent if the EAP peer actually replied).
1380 sm->eap_if->portEnabled = FALSE;
1381 hostapd_sta_deauth(hapd, sta->addr,
1382 WLAN_REASON_PREV_AUTH_NOT_VALID);
1383 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1384 WLAN_STA_AUTHORIZED);
1385 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1386 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1387 sta->timeout_next = STA_REMOVE;
1392 #ifdef HOSTAPD_DUMP_STATE
1393 static void fprint_char(FILE *f, char c)
1395 if (c >= 32 && c < 127)
1396 fprintf(f, "%c", c);
1398 fprintf(f, "<%02x>", c);
1402 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1404 struct eapol_state_machine *sm = sta->eapol_sm;
1408 fprintf(f, "%sIEEE 802.1X:\n", prefix);
1412 fprintf(f, "%sidentity=", prefix);
1413 for (i = 0; i < sm->identity_len; i++)
1414 fprint_char(f, sm->identity[i]);
1418 fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1419 "Supplicant: %d (%s)\n", prefix,
1420 sm->eap_type_authsrv,
1421 eap_server_get_name(0, sm->eap_type_authsrv),
1422 sm->eap_type_supp, eap_server_get_name(0, sm->eap_type_supp));
1424 fprintf(f, "%scached_packets=%s\n", prefix,
1425 sm->last_recv_radius ? "[RX RADIUS]" : "");
1427 eapol_auth_dump_state(f, prefix, sm);
1429 #endif /* HOSTAPD_DUMP_STATE */
1432 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1434 struct eapol_authenticator *eapol = hapd->eapol_auth;
1436 if (hapd->conf->default_wep_key_len < 1)
1439 os_free(eapol->default_wep_key);
1440 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1441 if (eapol->default_wep_key == NULL ||
1442 os_get_random(eapol->default_wep_key,
1443 hapd->conf->default_wep_key_len)) {
1444 printf("Could not generate random WEP key.\n");
1445 os_free(eapol->default_wep_key);
1446 eapol->default_wep_key = NULL;
1450 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1451 eapol->default_wep_key,
1452 hapd->conf->default_wep_key_len);
1458 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1459 struct sta_info *sta, void *ctx)
1461 if (sta->eapol_sm) {
1462 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1463 eapol_auth_step(sta->eapol_sm);
1469 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1471 struct hostapd_data *hapd = eloop_ctx;
1472 struct eapol_authenticator *eapol = hapd->eapol_auth;
1474 if (eapol->default_wep_key_idx >= 3)
1475 eapol->default_wep_key_idx =
1476 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1478 eapol->default_wep_key_idx++;
1480 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1481 eapol->default_wep_key_idx);
1483 if (ieee802_1x_rekey_broadcast(hapd)) {
1484 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1485 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1486 "new broadcast key");
1487 os_free(eapol->default_wep_key);
1488 eapol->default_wep_key = NULL;
1492 /* TODO: Could setup key for RX here, but change default TX keyid only
1493 * after new broadcast key has been sent to all stations. */
1494 if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
1495 eapol->default_wep_key_idx, 1, NULL, 0,
1496 eapol->default_wep_key,
1497 hapd->conf->default_wep_key_len)) {
1498 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1499 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1500 "new broadcast key");
1501 os_free(eapol->default_wep_key);
1502 eapol->default_wep_key = NULL;
1506 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1508 if (hapd->conf->wep_rekeying_period > 0) {
1509 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1510 ieee802_1x_rekey, hapd, NULL);
1515 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1516 const u8 *data, size_t datalen)
1518 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1522 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1523 const u8 *data, size_t datalen)
1525 #ifndef CONFIG_NO_RADIUS
1526 struct hostapd_data *hapd = ctx;
1527 struct sta_info *sta = sta_ctx;
1529 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1530 #endif /* CONFIG_NO_RADIUS */
1534 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1537 struct hostapd_data *hapd = ctx;
1538 struct sta_info *sta = sta_ctx;
1540 rsn_preauth_finished(hapd, sta, success);
1542 ieee802_1x_finished(hapd, sta, success);
1546 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1547 size_t identity_len, int phase2,
1548 struct eap_user *user)
1550 struct hostapd_data *hapd = ctx;
1551 const struct hostapd_eap_user *eap_user;
1554 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1555 identity_len, phase2);
1556 if (eap_user == NULL)
1559 os_memset(user, 0, sizeof(*user));
1560 user->phase2 = phase2;
1561 count = EAP_USER_MAX_METHODS;
1562 if (count > EAP_MAX_METHODS)
1563 count = EAP_MAX_METHODS;
1564 for (i = 0; i < count; i++) {
1565 user->methods[i].vendor = eap_user->methods[i].vendor;
1566 user->methods[i].method = eap_user->methods[i].method;
1569 if (eap_user->password) {
1570 user->password = os_malloc(eap_user->password_len);
1571 if (user->password == NULL)
1573 os_memcpy(user->password, eap_user->password,
1574 eap_user->password_len);
1575 user->password_len = eap_user->password_len;
1577 user->force_version = eap_user->force_version;
1578 user->ttls_auth = eap_user->ttls_auth;
1584 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1586 struct hostapd_data *hapd = ctx;
1587 struct sta_info *sta;
1588 sta = ap_get_sta(hapd, addr);
1589 if (sta == NULL || sta->eapol_sm == NULL)
1595 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1596 eapol_logger_level level, const char *txt)
1598 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1599 struct hostapd_data *hapd = ctx;
1603 case EAPOL_LOGGER_WARNING:
1604 hlevel = HOSTAPD_LEVEL_WARNING;
1606 case EAPOL_LOGGER_INFO:
1607 hlevel = HOSTAPD_LEVEL_INFO;
1609 case EAPOL_LOGGER_DEBUG:
1611 hlevel = HOSTAPD_LEVEL_DEBUG;
1615 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1617 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1621 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1624 struct hostapd_data *hapd = ctx;
1625 struct sta_info *sta = sta_ctx;
1626 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1630 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1632 struct hostapd_data *hapd = ctx;
1633 struct sta_info *sta = sta_ctx;
1634 ieee802_1x_abort_auth(hapd, sta);
1638 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1640 struct hostapd_data *hapd = ctx;
1641 struct sta_info *sta = sta_ctx;
1642 ieee802_1x_tx_key(hapd, sta);
1646 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1647 enum eapol_event type)
1649 /* struct hostapd_data *hapd = ctx; */
1650 struct sta_info *sta = sta_ctx;
1652 case EAPOL_AUTH_SM_CHANGE:
1653 wpa_auth_sm_notify(sta->wpa_sm);
1655 case EAPOL_AUTH_REAUTHENTICATE:
1656 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1662 int ieee802_1x_init(struct hostapd_data *hapd)
1665 struct eapol_auth_config conf;
1666 struct eapol_auth_cb cb;
1668 os_memset(&conf, 0, sizeof(conf));
1670 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1671 conf.wpa = hapd->conf->wpa;
1672 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1673 conf.eap_server = hapd->conf->eap_server;
1674 conf.ssl_ctx = hapd->ssl_ctx;
1675 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1676 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1677 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1678 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1679 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1680 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1681 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1682 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1683 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1684 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1685 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1686 conf.tnc = hapd->conf->tnc;
1687 conf.wps = hapd->wps;
1689 os_memset(&cb, 0, sizeof(cb));
1690 cb.eapol_send = ieee802_1x_eapol_send;
1691 cb.aaa_send = ieee802_1x_aaa_send;
1692 cb.finished = _ieee802_1x_finished;
1693 cb.get_eap_user = ieee802_1x_get_eap_user;
1694 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1695 cb.logger = ieee802_1x_logger;
1696 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1697 cb.abort_auth = _ieee802_1x_abort_auth;
1698 cb.tx_key = _ieee802_1x_tx_key;
1699 cb.eapol_event = ieee802_1x_eapol_event;
1701 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1702 if (hapd->eapol_auth == NULL)
1705 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1706 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1709 #ifndef CONFIG_NO_RADIUS
1710 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1711 ieee802_1x_receive_auth, hapd))
1713 #endif /* CONFIG_NO_RADIUS */
1715 if (hapd->conf->default_wep_key_len) {
1716 hostapd_set_privacy(hapd, 1);
1718 for (i = 0; i < 4; i++)
1719 hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
1720 NULL, i, 0, NULL, 0, NULL, 0);
1722 ieee802_1x_rekey(hapd, NULL);
1724 if (hapd->eapol_auth->default_wep_key == NULL)
1732 void ieee802_1x_deinit(struct hostapd_data *hapd)
1734 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1736 if (hapd->driver != NULL &&
1737 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1738 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1740 eapol_auth_deinit(hapd->eapol_auth);
1741 hapd->eapol_auth = NULL;
1745 int ieee802_1x_reconfig(struct hostapd_data *hapd,
1746 struct hostapd_config *oldconf,
1747 struct hostapd_bss_config *oldbss)
1749 ieee802_1x_deinit(hapd);
1750 return ieee802_1x_init(hapd);
1754 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1755 const u8 *buf, size_t len, int ack)
1757 struct ieee80211_hdr *hdr;
1758 struct ieee802_1x_hdr *xhdr;
1759 struct ieee802_1x_eapol_key *key;
1761 const unsigned char rfc1042_hdr[ETH_ALEN] =
1762 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1766 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1769 hdr = (struct ieee80211_hdr *) buf;
1770 pos = (u8 *) (hdr + 1);
1771 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1773 pos += sizeof(rfc1042_hdr);
1774 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1778 xhdr = (struct ieee802_1x_hdr *) pos;
1779 pos += sizeof(*xhdr);
1781 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1782 "type=%d length=%d - ack=%d",
1783 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1784 be_to_host16(xhdr->length), ack);
1786 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1787 * or Authenticator state machines, but EAPOL-Key packets are not
1788 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1789 * packets couple of times because otherwise STA keys become
1790 * unsynchronized with AP. */
1791 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1792 pos + sizeof(*key) <= buf + len) {
1793 key = (struct ieee802_1x_eapol_key *) pos;
1794 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1795 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1796 "frame (%scast index=%d)",
1797 key->key_index & BIT(7) ? "uni" : "broad",
1798 key->key_index & ~BIT(7));
1799 /* TODO: re-send EAPOL-Key couple of times (with short delay
1800 * between them?). If all attempt fail, report error and
1801 * deauthenticate STA so that it will get new keys when
1802 * authenticating again (e.g., after returning in range).
1803 * Separate limit/transmit state needed both for unicast and
1804 * broadcast keys(?) */
1806 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1807 * to here and change the key only if the EAPOL-Key packet was Acked.
1814 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1816 if (sm == NULL || sm->identity == NULL)
1819 *len = sm->identity_len;
1820 return sm->identity;
1824 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1827 if (sm == NULL || sm->radius_class.attr == NULL ||
1828 idx >= (int) sm->radius_class.count)
1831 *len = sm->radius_class.attr[idx].len;
1832 return sm->radius_class.attr[idx].data;
1836 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1841 *len = sm->eap_if->eapKeyDataLen;
1842 return sm->eap_if->eapKeyData;
1846 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1851 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1852 eapol_auth_step(sm);
1856 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1861 sm->portValid = valid ? TRUE : FALSE;
1862 eapol_auth_step(sm);
1866 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1871 sm->flags |= EAPOL_SM_PREAUTH;
1873 sm->flags &= ~EAPOL_SM_PREAUTH;
1877 static const char * bool_txt(Boolean bool)
1879 return bool ? "TRUE" : "FALSE";
1883 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1890 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1891 char *buf, size_t buflen)
1894 struct eapol_state_machine *sm = sta->eapol_sm;
1899 ret = os_snprintf(buf + len, buflen - len,
1900 "dot1xPaePortNumber=%d\n"
1901 "dot1xPaePortProtocolVersion=%d\n"
1902 "dot1xPaePortCapabilities=1\n"
1903 "dot1xPaePortInitialize=%d\n"
1904 "dot1xPaePortReauthenticate=FALSE\n",
1908 if (ret < 0 || (size_t) ret >= buflen - len)
1912 /* dot1xAuthConfigTable */
1913 ret = os_snprintf(buf + len, buflen - len,
1914 "dot1xAuthPaeState=%d\n"
1915 "dot1xAuthBackendAuthState=%d\n"
1916 "dot1xAuthAdminControlledDirections=%d\n"
1917 "dot1xAuthOperControlledDirections=%d\n"
1918 "dot1xAuthAuthControlledPortStatus=%d\n"
1919 "dot1xAuthAuthControlledPortControl=%d\n"
1920 "dot1xAuthQuietPeriod=%u\n"
1921 "dot1xAuthServerTimeout=%u\n"
1922 "dot1xAuthReAuthPeriod=%u\n"
1923 "dot1xAuthReAuthEnabled=%s\n"
1924 "dot1xAuthKeyTxEnabled=%s\n",
1925 sm->auth_pae_state + 1,
1926 sm->be_auth_state + 1,
1927 sm->adminControlledDirections,
1928 sm->operControlledDirections,
1934 bool_txt(sm->reAuthEnabled),
1935 bool_txt(sm->keyTxEnabled));
1936 if (ret < 0 || (size_t) ret >= buflen - len)
1940 /* dot1xAuthStatsTable */
1941 ret = os_snprintf(buf + len, buflen - len,
1942 "dot1xAuthEapolFramesRx=%u\n"
1943 "dot1xAuthEapolFramesTx=%u\n"
1944 "dot1xAuthEapolStartFramesRx=%u\n"
1945 "dot1xAuthEapolLogoffFramesRx=%u\n"
1946 "dot1xAuthEapolRespIdFramesRx=%u\n"
1947 "dot1xAuthEapolRespFramesRx=%u\n"
1948 "dot1xAuthEapolReqIdFramesTx=%u\n"
1949 "dot1xAuthEapolReqFramesTx=%u\n"
1950 "dot1xAuthInvalidEapolFramesRx=%u\n"
1951 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1952 "dot1xAuthLastEapolFrameVersion=%u\n"
1953 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1954 sm->dot1xAuthEapolFramesRx,
1955 sm->dot1xAuthEapolFramesTx,
1956 sm->dot1xAuthEapolStartFramesRx,
1957 sm->dot1xAuthEapolLogoffFramesRx,
1958 sm->dot1xAuthEapolRespIdFramesRx,
1959 sm->dot1xAuthEapolRespFramesRx,
1960 sm->dot1xAuthEapolReqIdFramesTx,
1961 sm->dot1xAuthEapolReqFramesTx,
1962 sm->dot1xAuthInvalidEapolFramesRx,
1963 sm->dot1xAuthEapLengthErrorFramesRx,
1964 sm->dot1xAuthLastEapolFrameVersion,
1966 if (ret < 0 || (size_t) ret >= buflen - len)
1970 /* dot1xAuthDiagTable */
1971 ret = os_snprintf(buf + len, buflen - len,
1972 "dot1xAuthEntersConnecting=%u\n"
1973 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1974 "dot1xAuthEntersAuthenticating=%u\n"
1975 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1976 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1977 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1978 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1979 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1980 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1981 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1982 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1983 "dot1xAuthBackendResponses=%u\n"
1984 "dot1xAuthBackendAccessChallenges=%u\n"
1985 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1986 "dot1xAuthBackendAuthSuccesses=%u\n"
1987 "dot1xAuthBackendAuthFails=%u\n",
1988 sm->authEntersConnecting,
1989 sm->authEapLogoffsWhileConnecting,
1990 sm->authEntersAuthenticating,
1991 sm->authAuthSuccessesWhileAuthenticating,
1992 sm->authAuthTimeoutsWhileAuthenticating,
1993 sm->authAuthFailWhileAuthenticating,
1994 sm->authAuthEapStartsWhileAuthenticating,
1995 sm->authAuthEapLogoffWhileAuthenticating,
1996 sm->authAuthReauthsWhileAuthenticated,
1997 sm->authAuthEapStartsWhileAuthenticated,
1998 sm->authAuthEapLogoffWhileAuthenticated,
1999 sm->backendResponses,
2000 sm->backendAccessChallenges,
2001 sm->backendOtherRequestsToSupplicant,
2002 sm->backendAuthSuccesses,
2003 sm->backendAuthFails);
2004 if (ret < 0 || (size_t) ret >= buflen - len)
2008 /* dot1xAuthSessionStatsTable */
2009 ret = os_snprintf(buf + len, buflen - len,
2010 /* TODO: dot1xAuthSessionOctetsRx */
2011 /* TODO: dot1xAuthSessionOctetsTx */
2012 /* TODO: dot1xAuthSessionFramesRx */
2013 /* TODO: dot1xAuthSessionFramesTx */
2014 "dot1xAuthSessionId=%08X-%08X\n"
2015 "dot1xAuthSessionAuthenticMethod=%d\n"
2016 "dot1xAuthSessionTime=%u\n"
2017 "dot1xAuthSessionTerminateCause=999\n"
2018 "dot1xAuthSessionUserName=%s\n",
2019 sta->acct_session_id_hi, sta->acct_session_id_lo,
2020 (wpa_key_mgmt_wpa_ieee8021x(
2021 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2023 (unsigned int) (time(NULL) -
2024 sta->acct_session_start),
2026 if (ret < 0 || (size_t) ret >= buflen - len)
2034 static void ieee802_1x_finished(struct hostapd_data *hapd,
2035 struct sta_info *sta, int success)
2039 /* TODO: get PMKLifetime from WPA parameters */
2040 static const int dot11RSNAConfigPMKLifetime = 43200;
2042 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2043 if (success && key && len >= PMK_LEN &&
2044 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2045 sta->eapol_sm) == 0) {
2046 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2047 HOSTAPD_LEVEL_DEBUG,
2048 "Added PMKSA cache entry (IEEE 802.1X)");