2 * hostapd / IEEE 802.1X-2004 Authenticator
3 * Copyright (c) 2002-2009, 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 hapd->drv.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 = hapd->drv.set_authorized(hapd, sta, 1);
94 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
95 HOSTAPD_LEVEL_DEBUG, "authorizing port");
97 if ((sta->flags & (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC)) ==
98 (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC))
99 wpa_msg(hapd->msg_ctx, MSG_INFO,
100 AP_STA_DISCONNECTED MACSTR,
102 sta->flags &= ~WLAN_STA_AUTHORIZED;
103 res = hapd->drv.set_authorized(hapd, sta, 0);
104 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
105 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
108 if (res && errno != ENOENT) {
109 printf("Could not set station " MACSTR " flags for kernel "
110 "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
114 accounting_sta_start(hapd, sta);
118 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
119 struct sta_info *sta,
120 int idx, int broadcast,
121 u8 *key_data, size_t key_len)
124 struct ieee802_1x_hdr *hdr;
125 struct ieee802_1x_eapol_key *key;
126 size_t len, ekey_len;
127 struct eapol_state_machine *sm = sta->eapol_sm;
132 len = sizeof(*key) + key_len;
133 buf = os_zalloc(sizeof(*hdr) + len);
137 hdr = (struct ieee802_1x_hdr *) buf;
138 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
139 key->type = EAPOL_KEY_TYPE_RC4;
140 key->key_length = htons(key_len);
141 wpa_get_ntp_timestamp(key->replay_counter);
143 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
144 wpa_printf(MSG_ERROR, "Could not get random numbers");
149 key->key_index = idx | (broadcast ? 0 : BIT(7));
150 if (hapd->conf->eapol_key_index_workaround) {
151 /* According to some information, WinXP Supplicant seems to
152 * interpret bit7 as an indication whether the key is to be
153 * activated, so make it possible to enable workaround that
154 * sets this bit for all keys. */
155 key->key_index |= BIT(7);
158 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
159 * MSK[32..63] is used to sign the message. */
160 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
161 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
162 "and signing EAPOL-Key");
166 os_memcpy((u8 *) (key + 1), key_data, key_len);
167 ekey_len = sizeof(key->key_iv) + 32;
168 ekey = os_malloc(ekey_len);
170 wpa_printf(MSG_ERROR, "Could not encrypt key");
174 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
175 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
176 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
179 /* This header is needed here for HMAC-MD5, but it will be regenerated
180 * in ieee802_1x_send() */
181 hdr->version = hapd->conf->eapol_version;
182 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
183 hdr->length = host_to_be16(len);
184 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
187 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
188 " (%s index=%d)", MAC2STR(sm->addr),
189 broadcast ? "broadcast" : "unicast", idx);
190 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
192 sta->eapol_sm->dot1xAuthEapolFramesTx++;
197 #ifndef CONFIG_NO_VLAN
198 static struct hostapd_wep_keys *
199 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
201 struct hostapd_wep_keys *key;
203 key = os_zalloc(sizeof(*key));
207 key->default_len = hapd->conf->default_wep_key_len;
209 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
210 key->idx < hapd->conf->broadcast_key_idx_min)
211 key->idx = hapd->conf->broadcast_key_idx_min;
215 if (!key->key[key->idx])
216 key->key[key->idx] = os_malloc(key->default_len);
217 if (key->key[key->idx] == NULL ||
218 os_get_random(key->key[key->idx], key->default_len)) {
219 printf("Could not generate random WEP key (dynamic VLAN).\n");
220 os_free(key->key[key->idx]);
221 key->key[key->idx] = NULL;
225 key->len[key->idx] = key->default_len;
227 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
229 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
230 key->key[key->idx], key->len[key->idx]);
232 if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL, key->idx, 1,
233 NULL, 0, key->key[key->idx], key->len[key->idx]))
234 printf("Could not set dynamic VLAN WEP encryption key.\n");
236 hapd->drv.set_drv_ieee8021x(hapd, ifname, 1);
242 static struct hostapd_wep_keys *
243 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
251 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
252 ssid->dyn_vlan_keys[vlan_id])
253 return ssid->dyn_vlan_keys[vlan_id];
255 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
256 "state machine for VLAN ID %lu",
257 (unsigned long) vlan_id);
259 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
260 if (ifname == NULL) {
261 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
262 "cannot create group key state machine",
263 (unsigned long) vlan_id);
267 if (ssid->dyn_vlan_keys == NULL) {
268 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
269 ssid->dyn_vlan_keys = os_zalloc(size);
270 if (ssid->dyn_vlan_keys == NULL)
272 ssid->max_dyn_vlan_keys = vlan_id;
275 if (ssid->max_dyn_vlan_keys < vlan_id) {
276 struct hostapd_wep_keys **na;
277 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
278 na = os_realloc(ssid->dyn_vlan_keys, size);
281 ssid->dyn_vlan_keys = na;
282 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
283 (vlan_id - ssid->max_dyn_vlan_keys) *
284 sizeof(ssid->dyn_vlan_keys[0]));
285 ssid->max_dyn_vlan_keys = vlan_id;
288 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
290 return ssid->dyn_vlan_keys[vlan_id];
292 #endif /* CONFIG_NO_VLAN */
295 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
297 struct eapol_authenticator *eapol = hapd->eapol_auth;
298 struct eapol_state_machine *sm = sta->eapol_sm;
299 #ifndef CONFIG_NO_VLAN
300 struct hostapd_wep_keys *key = NULL;
302 #endif /* CONFIG_NO_VLAN */
304 if (sm == NULL || !sm->eap_if->eapKeyData)
307 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
310 #ifndef CONFIG_NO_VLAN
311 vlan_id = sta->vlan_id;
312 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
316 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
317 if (key && key->key[key->idx])
318 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
322 #endif /* CONFIG_NO_VLAN */
323 if (eapol->default_wep_key) {
324 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
325 eapol->default_wep_key,
326 hapd->conf->default_wep_key_len);
329 if (hapd->conf->individual_wep_key_len > 0) {
331 ikey = os_malloc(hapd->conf->individual_wep_key_len);
333 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
334 wpa_printf(MSG_ERROR, "Could not generate random "
335 "individual WEP key.");
340 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
341 ikey, hapd->conf->individual_wep_key_len);
343 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
344 hapd->conf->individual_wep_key_len);
346 /* TODO: set encryption in TX callback, i.e., only after STA
347 * has ACKed EAPOL-Key frame */
348 if (hapd->drv.set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
349 sta->addr, 0, 1, NULL, 0, ikey,
350 hapd->conf->individual_wep_key_len)) {
351 wpa_printf(MSG_ERROR, "Could not set individual WEP "
360 const char *radius_mode_txt(struct hostapd_data *hapd)
362 switch (hapd->iface->conf->hw_mode) {
363 case HOSTAPD_MODE_IEEE80211A:
365 case HOSTAPD_MODE_IEEE80211G:
367 case HOSTAPD_MODE_IEEE80211B:
374 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
379 for (i = 0; i < sta->supported_rates_len; i++)
380 if ((sta->supported_rates[i] & 0x7f) > rate)
381 rate = sta->supported_rates[i] & 0x7f;
387 #ifndef CONFIG_NO_RADIUS
388 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
389 struct eapol_state_machine *sm,
390 const u8 *eap, size_t len)
395 if (len <= sizeof(struct eap_hdr) ||
396 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
399 identity = eap_get_identity(sm->eap, &identity_len);
400 if (identity == NULL)
403 /* Save station identity for future RADIUS packets */
404 os_free(sm->identity);
405 sm->identity = os_malloc(identity_len + 1);
406 if (sm->identity == NULL) {
407 sm->identity_len = 0;
411 os_memcpy(sm->identity, identity, identity_len);
412 sm->identity_len = identity_len;
413 sm->identity[identity_len] = '\0';
414 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
415 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
416 sm->dot1xAuthEapolRespIdFramesRx++;
420 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
421 struct sta_info *sta,
422 const u8 *eap, size_t len)
424 struct radius_msg *msg;
426 struct eapol_state_machine *sm = sta->eapol_sm;
431 ieee802_1x_learn_identity(hapd, sm, eap, len);
433 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
436 sm->radius_identifier = radius_client_get_id(hapd->radius);
437 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
438 sm->radius_identifier);
440 printf("Could not create net RADIUS packet\n");
444 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
447 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
448 sm->identity, sm->identity_len)) {
449 printf("Could not add User-Name\n");
453 if (hapd->conf->own_ip_addr.af == AF_INET &&
454 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
455 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
456 printf("Could not add NAS-IP-Address\n");
461 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
462 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
463 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
464 printf("Could not add NAS-IPv6-Address\n");
467 #endif /* CONFIG_IPV6 */
469 if (hapd->conf->nas_identifier &&
470 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
471 (u8 *) hapd->conf->nas_identifier,
472 os_strlen(hapd->conf->nas_identifier))) {
473 printf("Could not add NAS-Identifier\n");
477 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
478 printf("Could not add NAS-Port\n");
482 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
483 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
484 buf[sizeof(buf) - 1] = '\0';
485 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
486 (u8 *) buf, os_strlen(buf))) {
487 printf("Could not add Called-Station-Id\n");
491 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
493 buf[sizeof(buf) - 1] = '\0';
494 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
495 (u8 *) buf, os_strlen(buf))) {
496 printf("Could not add Calling-Station-Id\n");
500 /* TODO: should probably check MTU from driver config; 2304 is max for
501 * IEEE 802.11, but use 1400 to avoid problems with too large packets
503 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
504 printf("Could not add Framed-MTU\n");
508 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
509 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
510 printf("Could not add NAS-Port-Type\n");
514 if (sta->flags & WLAN_STA_PREAUTH) {
515 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
518 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
519 radius_sta_rate(hapd, sta) / 2,
520 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
521 radius_mode_txt(hapd));
522 buf[sizeof(buf) - 1] = '\0';
524 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
525 (u8 *) buf, os_strlen(buf))) {
526 printf("Could not add Connect-Info\n");
530 if (eap && !radius_msg_add_eap(msg, eap, len)) {
531 printf("Could not add EAP-Message\n");
535 /* State attribute must be copied if and only if this packet is
536 * Access-Request reply to the previous Access-Challenge */
537 if (sm->last_recv_radius &&
538 radius_msg_get_hdr(sm->last_recv_radius)->code ==
539 RADIUS_CODE_ACCESS_CHALLENGE) {
540 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
543 printf("Could not copy State attribute from previous "
544 "Access-Challenge\n");
548 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
552 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
556 radius_msg_free(msg);
558 #endif /* CONFIG_NO_RADIUS */
561 static void handle_eap_response(struct hostapd_data *hapd,
562 struct sta_info *sta, struct eap_hdr *eap,
566 struct eapol_state_machine *sm = sta->eapol_sm;
570 data = (u8 *) (eap + 1);
572 if (len < sizeof(*eap) + 1) {
573 printf("handle_eap_response: too short response data\n");
577 sm->eap_type_supp = type = data[0];
579 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
580 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
581 "id=%d len=%d) from STA: EAP Response-%s (%d)",
582 eap->code, eap->identifier, be_to_host16(eap->length),
583 eap_server_get_name(0, type), type);
585 sm->dot1xAuthEapolRespFramesRx++;
587 wpabuf_free(sm->eap_if->eapRespData);
588 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
593 /* Process incoming EAP packet from Supplicant */
594 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
600 if (len < sizeof(*eap)) {
601 printf(" too short EAP packet\n");
605 eap = (struct eap_hdr *) buf;
607 eap_len = be_to_host16(eap->length);
608 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
609 eap->code, eap->identifier, eap_len);
610 if (eap_len < sizeof(*eap)) {
611 wpa_printf(MSG_DEBUG, " Invalid EAP length");
613 } else if (eap_len > len) {
614 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
617 } else if (eap_len < len) {
618 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
619 "packet", (unsigned long) len - eap_len);
623 case EAP_CODE_REQUEST:
624 wpa_printf(MSG_DEBUG, " (request)");
626 case EAP_CODE_RESPONSE:
627 wpa_printf(MSG_DEBUG, " (response)");
628 handle_eap_response(hapd, sta, eap, eap_len);
630 case EAP_CODE_SUCCESS:
631 wpa_printf(MSG_DEBUG, " (success)");
633 case EAP_CODE_FAILURE:
634 wpa_printf(MSG_DEBUG, " (failure)");
637 wpa_printf(MSG_DEBUG, " (unknown code)");
643 static struct eapol_state_machine *
644 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
647 if (sta->flags & WLAN_STA_PREAUTH)
648 flags |= EAPOL_SM_PREAUTH;
650 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
651 flags |= EAPOL_SM_USES_WPA;
652 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
653 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
655 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
661 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
662 * @hapd: hostapd BSS data
663 * @sa: Source address (sender of the EAPOL frame)
665 * @len: Length of buf in octets
667 * This function is called for each incoming EAPOL frame from the interface
669 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
672 struct sta_info *sta;
673 struct ieee802_1x_hdr *hdr;
674 struct ieee802_1x_eapol_key *key;
676 struct rsn_pmksa_cache_entry *pmksa;
678 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
679 !hapd->conf->wps_state)
682 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
683 (unsigned long) len, MAC2STR(sa));
684 sta = ap_get_sta(hapd, sa);
685 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
686 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
691 if (len < sizeof(*hdr)) {
692 printf(" too short IEEE 802.1X packet\n");
696 hdr = (struct ieee802_1x_hdr *) buf;
697 datalen = be_to_host16(hdr->length);
698 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
699 hdr->version, hdr->type, datalen);
701 if (len - sizeof(*hdr) < datalen) {
702 printf(" frame too short for this IEEE 802.1X packet\n");
704 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
707 if (len - sizeof(*hdr) > datalen) {
708 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
709 "IEEE 802.1X packet",
710 (unsigned long) len - sizeof(*hdr) - datalen);
714 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
715 sta->eapol_sm->dot1xAuthEapolFramesRx++;
718 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
719 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
720 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
721 (key->type == EAPOL_KEY_TYPE_WPA ||
722 key->type == EAPOL_KEY_TYPE_RSN)) {
723 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
724 sizeof(*hdr) + datalen);
728 if ((!hapd->conf->ieee802_1x &&
729 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
730 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
733 if (!sta->eapol_sm) {
734 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
739 if (!hapd->conf->ieee802_1x &&
740 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
741 WLAN_STA_MAYBE_WPS)) {
743 * Delay EAPOL frame transmission until a possible WPS
744 * STA initiates the handshake with EAPOL-Start.
746 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
748 #endif /* CONFIG_WPS */
750 sta->eapol_sm->eap_if->portEnabled = TRUE;
753 /* since we support version 1, we can ignore version field and proceed
754 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
755 /* TODO: actually, we are not version 1 anymore.. However, Version 2
756 * does not change frame contents, so should be ok to process frames
757 * more or less identically. Some changes might be needed for
758 * verification of fields. */
761 case IEEE802_1X_TYPE_EAP_PACKET:
762 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
765 case IEEE802_1X_TYPE_EAPOL_START:
766 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
767 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
769 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
770 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
772 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
773 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
774 "available - ignore it since "
775 "STA sent EAPOL-Start");
776 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
778 sta->eapol_sm->eapolStart = TRUE;
779 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
780 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
783 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
784 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
785 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
787 sta->acct_terminate_cause =
788 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
789 accounting_sta_stop(hapd, sta);
790 sta->eapol_sm->eapolLogoff = TRUE;
791 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
794 case IEEE802_1X_TYPE_EAPOL_KEY:
795 wpa_printf(MSG_DEBUG, " EAPOL-Key");
796 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
797 wpa_printf(MSG_DEBUG, " Dropped key data from "
798 "unauthorized Supplicant");
803 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
804 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
805 /* TODO: implement support for this; show data */
809 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
810 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
814 eapol_auth_step(sta->eapol_sm);
819 * ieee802_1x_new_station - Start IEEE 802.1X authentication
820 * @hapd: hostapd BSS data
823 * This function is called to start IEEE 802.1X authentication when a new
824 * station completes IEEE 802.11 association.
826 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
828 struct rsn_pmksa_cache_entry *pmksa;
833 if (hapd->conf->wps_state && hapd->conf->wpa &&
834 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
836 * Need to enable IEEE 802.1X/EAPOL state machines for possible
837 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
838 * authentication in this BSS.
842 #endif /* CONFIG_WPS */
844 if ((!force_1x && !hapd->conf->ieee802_1x) ||
845 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
848 if (sta->eapol_sm == NULL) {
849 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
850 HOSTAPD_LEVEL_DEBUG, "start authentication");
851 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
852 if (sta->eapol_sm == NULL) {
853 hostapd_logger(hapd, sta->addr,
854 HOSTAPD_MODULE_IEEE8021X,
856 "failed to allocate state machine");
863 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
864 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
866 * Delay EAPOL frame transmission until a possible WPS
867 * initiates the handshake with EAPOL-Start.
869 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
871 #endif /* CONFIG_WPS */
873 sta->eapol_sm->eap_if->portEnabled = TRUE;
875 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
879 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
881 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
882 /* Setup EAPOL state machines to already authenticated state
883 * because of existing PMKSA information in the cache. */
884 sta->eapol_sm->keyRun = TRUE;
885 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
886 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
887 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
888 sta->eapol_sm->authSuccess = TRUE;
889 if (sta->eapol_sm->eap)
890 eap_sm_notify_cached(sta->eapol_sm->eap);
891 old_vlanid = sta->vlan_id;
892 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
893 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
895 ap_sta_bind_vlan(hapd, sta, old_vlanid);
899 * Force EAPOL state machines to start
900 * re-authentication without having to wait for the
901 * Supplicant to send EAPOL-Start.
903 sta->eapol_sm->reAuthenticate = TRUE;
905 eapol_auth_step(sta->eapol_sm);
910 void ieee802_1x_free_station(struct sta_info *sta)
912 struct eapol_state_machine *sm = sta->eapol_sm;
917 sta->eapol_sm = NULL;
919 #ifndef CONFIG_NO_RADIUS
920 radius_msg_free(sm->last_recv_radius);
921 radius_free_class(&sm->radius_class);
922 #endif /* CONFIG_NO_RADIUS */
924 os_free(sm->identity);
929 #ifndef CONFIG_NO_RADIUS
930 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
931 struct sta_info *sta)
938 struct radius_msg *msg;
939 struct eapol_state_machine *sm = sta->eapol_sm;
941 if (sm == NULL || sm->last_recv_radius == NULL) {
943 sm->eap_if->aaaEapNoReq = TRUE;
947 msg = sm->last_recv_radius;
949 eap = radius_msg_get_eap(msg, &len);
951 /* RFC 3579, Chap. 2.6.3:
952 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
954 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
955 HOSTAPD_LEVEL_WARNING, "could not extract "
956 "EAP-Message from RADIUS message");
957 sm->eap_if->aaaEapNoReq = TRUE;
961 if (len < sizeof(*hdr)) {
962 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
963 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
964 "received from authentication server");
966 sm->eap_if->aaaEapNoReq = TRUE;
970 if (len > sizeof(*hdr))
971 eap_type = eap[sizeof(*hdr)];
973 hdr = (struct eap_hdr *) eap;
975 case EAP_CODE_REQUEST:
977 sm->eap_type_authsrv = eap_type;
978 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
979 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
983 case EAP_CODE_RESPONSE:
984 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
985 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
989 case EAP_CODE_SUCCESS:
990 os_strlcpy(buf, "EAP Success", sizeof(buf));
992 case EAP_CODE_FAILURE:
993 os_strlcpy(buf, "EAP Failure", sizeof(buf));
996 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
999 buf[sizeof(buf) - 1] = '\0';
1000 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1001 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1002 "id=%d len=%d) from RADIUS server: %s",
1003 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1005 sm->eap_if->aaaEapReq = TRUE;
1007 wpabuf_free(sm->eap_if->aaaEapReqData);
1008 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1012 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1013 struct sta_info *sta, struct radius_msg *msg,
1014 struct radius_msg *req,
1015 const u8 *shared_secret,
1016 size_t shared_secret_len)
1018 struct radius_ms_mppe_keys *keys;
1019 struct eapol_state_machine *sm = sta->eapol_sm;
1023 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1026 if (keys && keys->send && keys->recv) {
1027 size_t len = keys->send_len + keys->recv_len;
1028 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1029 keys->send, keys->send_len);
1030 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1031 keys->recv, keys->recv_len);
1033 os_free(sm->eap_if->aaaEapKeyData);
1034 sm->eap_if->aaaEapKeyData = os_malloc(len);
1035 if (sm->eap_if->aaaEapKeyData) {
1036 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1038 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1039 keys->send, keys->send_len);
1040 sm->eap_if->aaaEapKeyDataLen = len;
1041 sm->eap_if->aaaEapKeyAvailable = TRUE;
1046 os_free(keys->send);
1047 os_free(keys->recv);
1053 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1054 struct sta_info *sta,
1055 struct radius_msg *msg)
1059 struct eapol_state_machine *sm = sta->eapol_sm;
1061 struct radius_attr_data *nclass;
1062 size_t nclass_count;
1064 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1068 radius_free_class(&sm->radius_class);
1069 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1073 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1080 for (i = 0; i < count; i++) {
1082 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1088 } while (class_len < 1);
1090 nclass[nclass_count].data = os_malloc(class_len);
1091 if (nclass[nclass_count].data == NULL)
1094 os_memcpy(nclass[nclass_count].data, class, class_len);
1095 nclass[nclass_count].len = class_len;
1099 sm->radius_class.attr = nclass;
1100 sm->radius_class.count = nclass_count;
1101 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1102 "attributes for " MACSTR,
1103 (unsigned long) sm->radius_class.count,
1104 MAC2STR(sta->addr));
1108 /* Update sta->identity based on User-Name attribute in Access-Accept */
1109 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1110 struct sta_info *sta,
1111 struct radius_msg *msg)
1115 struct eapol_state_machine *sm = sta->eapol_sm;
1120 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1124 identity = os_malloc(len + 1);
1125 if (identity == NULL)
1128 os_memcpy(identity, buf, len);
1129 identity[len] = '\0';
1131 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1132 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1133 "User-Name from Access-Accept '%s'",
1134 sm->identity ? (char *) sm->identity : "N/A",
1137 os_free(sm->identity);
1138 sm->identity = identity;
1139 sm->identity_len = len;
1143 struct sta_id_search {
1145 struct eapol_state_machine *sm;
1149 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1150 struct sta_info *sta,
1153 struct sta_id_search *id_search = ctx;
1154 struct eapol_state_machine *sm = sta->eapol_sm;
1156 if (sm && sm->radius_identifier >= 0 &&
1157 sm->radius_identifier == id_search->identifier) {
1165 static struct eapol_state_machine *
1166 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1168 struct sta_id_search id_search;
1169 id_search.identifier = identifier;
1170 id_search.sm = NULL;
1171 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1172 return id_search.sm;
1177 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1178 * @msg: RADIUS response message
1179 * @req: RADIUS request message
1180 * @shared_secret: RADIUS shared secret
1181 * @shared_secret_len: Length of shared_secret in octets
1182 * @data: Context data (struct hostapd_data *)
1183 * Returns: Processing status
1185 static RadiusRxResult
1186 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1187 const u8 *shared_secret, size_t shared_secret_len,
1190 struct hostapd_data *hapd = data;
1191 struct sta_info *sta;
1192 u32 session_timeout = 0, termination_action, acct_interim_interval;
1193 int session_timeout_set, old_vlanid = 0;
1194 struct eapol_state_machine *sm;
1195 int override_eapReq = 0;
1196 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1198 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1200 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1201 "station for this RADIUS message");
1202 return RADIUS_RX_UNKNOWN;
1206 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1207 * present when packet contains an EAP-Message attribute */
1208 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1209 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1211 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1212 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1213 "Message-Authenticator since it does not include "
1215 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1217 printf("Incoming RADIUS packet did not have correct "
1218 "Message-Authenticator - dropped\n");
1219 return RADIUS_RX_INVALID_AUTHENTICATOR;
1222 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1223 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1224 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1225 printf("Unknown RADIUS message code\n");
1226 return RADIUS_RX_UNKNOWN;
1229 sm->radius_identifier = -1;
1230 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1231 MAC2STR(sta->addr));
1233 radius_msg_free(sm->last_recv_radius);
1234 sm->last_recv_radius = msg;
1236 session_timeout_set =
1237 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1239 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1240 &termination_action))
1241 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1243 if (hapd->conf->acct_interim_interval == 0 &&
1244 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1245 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1246 &acct_interim_interval) == 0) {
1247 if (acct_interim_interval < 60) {
1248 hostapd_logger(hapd, sta->addr,
1249 HOSTAPD_MODULE_IEEE8021X,
1251 "ignored too small "
1252 "Acct-Interim-Interval %d",
1253 acct_interim_interval);
1255 sta->acct_interim_interval = acct_interim_interval;
1259 switch (hdr->code) {
1260 case RADIUS_CODE_ACCESS_ACCEPT:
1261 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1263 #ifndef CONFIG_NO_VLAN
1265 old_vlanid = sta->vlan_id;
1266 sta->vlan_id = radius_msg_get_vlanid(msg);
1268 if (sta->vlan_id > 0 &&
1269 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1271 hostapd_logger(hapd, sta->addr,
1272 HOSTAPD_MODULE_RADIUS,
1274 "VLAN ID %d", sta->vlan_id);
1275 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1276 sta->eapol_sm->authFail = TRUE;
1277 hostapd_logger(hapd, sta->addr,
1278 HOSTAPD_MODULE_IEEE8021X,
1279 HOSTAPD_LEVEL_INFO, "authentication "
1280 "server did not include required VLAN "
1281 "ID in Access-Accept");
1284 #endif /* CONFIG_NO_VLAN */
1286 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1288 /* RFC 3580, Ch. 3.17 */
1289 if (session_timeout_set && termination_action ==
1290 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1291 sm->reAuthPeriod = session_timeout;
1292 } else if (session_timeout_set)
1293 ap_sta_session_timeout(hapd, sta, session_timeout);
1295 sm->eap_if->aaaSuccess = TRUE;
1296 override_eapReq = 1;
1297 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1299 ieee802_1x_store_radius_class(hapd, sta, msg);
1300 ieee802_1x_update_sta_identity(hapd, sta, msg);
1301 if (sm->eap_if->eapKeyAvailable &&
1302 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1303 session_timeout_set ?
1304 (int) session_timeout : -1, sm) == 0) {
1305 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1306 HOSTAPD_LEVEL_DEBUG,
1307 "Added PMKSA cache entry");
1310 case RADIUS_CODE_ACCESS_REJECT:
1311 sm->eap_if->aaaFail = TRUE;
1312 override_eapReq = 1;
1314 case RADIUS_CODE_ACCESS_CHALLENGE:
1315 sm->eap_if->aaaEapReq = TRUE;
1316 if (session_timeout_set) {
1317 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1318 sm->eap_if->aaaMethodTimeout = session_timeout;
1319 hostapd_logger(hapd, sm->addr,
1320 HOSTAPD_MODULE_IEEE8021X,
1321 HOSTAPD_LEVEL_DEBUG,
1322 "using EAP timeout of %d seconds (from "
1324 sm->eap_if->aaaMethodTimeout);
1327 * Use dynamic retransmission behavior per EAP
1330 sm->eap_if->aaaMethodTimeout = 0;
1335 ieee802_1x_decapsulate_radius(hapd, sta);
1336 if (override_eapReq)
1337 sm->eap_if->aaaEapReq = FALSE;
1339 eapol_auth_step(sm);
1341 return RADIUS_RX_QUEUED;
1343 #endif /* CONFIG_NO_RADIUS */
1346 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1348 struct eapol_state_machine *sm = sta->eapol_sm;
1352 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1353 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1355 #ifndef CONFIG_NO_RADIUS
1356 radius_msg_free(sm->last_recv_radius);
1357 sm->last_recv_radius = NULL;
1358 #endif /* CONFIG_NO_RADIUS */
1360 if (sm->eap_if->eapTimeout) {
1362 * Disconnect the STA since it did not reply to the last EAP
1363 * request and we cannot continue EAP processing (EAP-Failure
1364 * could only be sent if the EAP peer actually replied).
1366 sm->eap_if->portEnabled = FALSE;
1367 ap_sta_disconnect(hapd, sta, sta->addr,
1368 WLAN_REASON_PREV_AUTH_NOT_VALID);
1373 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1375 struct eapol_authenticator *eapol = hapd->eapol_auth;
1377 if (hapd->conf->default_wep_key_len < 1)
1380 os_free(eapol->default_wep_key);
1381 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1382 if (eapol->default_wep_key == NULL ||
1383 os_get_random(eapol->default_wep_key,
1384 hapd->conf->default_wep_key_len)) {
1385 printf("Could not generate random WEP key.\n");
1386 os_free(eapol->default_wep_key);
1387 eapol->default_wep_key = NULL;
1391 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1392 eapol->default_wep_key,
1393 hapd->conf->default_wep_key_len);
1399 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1400 struct sta_info *sta, void *ctx)
1402 if (sta->eapol_sm) {
1403 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1404 eapol_auth_step(sta->eapol_sm);
1410 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1412 struct hostapd_data *hapd = eloop_ctx;
1413 struct eapol_authenticator *eapol = hapd->eapol_auth;
1415 if (eapol->default_wep_key_idx >= 3)
1416 eapol->default_wep_key_idx =
1417 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1419 eapol->default_wep_key_idx++;
1421 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1422 eapol->default_wep_key_idx);
1424 if (ieee802_1x_rekey_broadcast(hapd)) {
1425 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1426 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1427 "new broadcast key");
1428 os_free(eapol->default_wep_key);
1429 eapol->default_wep_key = NULL;
1433 /* TODO: Could setup key for RX here, but change default TX keyid only
1434 * after new broadcast key has been sent to all stations. */
1435 if (hapd->drv.set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
1436 eapol->default_wep_key_idx, 1, NULL, 0,
1437 eapol->default_wep_key,
1438 hapd->conf->default_wep_key_len)) {
1439 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1440 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1441 "new broadcast key");
1442 os_free(eapol->default_wep_key);
1443 eapol->default_wep_key = NULL;
1447 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1449 if (hapd->conf->wep_rekeying_period > 0) {
1450 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1451 ieee802_1x_rekey, hapd, NULL);
1456 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1457 const u8 *data, size_t datalen)
1459 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1463 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1464 const u8 *data, size_t datalen)
1466 #ifndef CONFIG_NO_RADIUS
1467 struct hostapd_data *hapd = ctx;
1468 struct sta_info *sta = sta_ctx;
1470 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1471 #endif /* CONFIG_NO_RADIUS */
1475 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1478 struct hostapd_data *hapd = ctx;
1479 struct sta_info *sta = sta_ctx;
1481 rsn_preauth_finished(hapd, sta, success);
1483 ieee802_1x_finished(hapd, sta, success);
1487 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1488 size_t identity_len, int phase2,
1489 struct eap_user *user)
1491 struct hostapd_data *hapd = ctx;
1492 const struct hostapd_eap_user *eap_user;
1495 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1496 identity_len, phase2);
1497 if (eap_user == NULL)
1500 os_memset(user, 0, sizeof(*user));
1501 user->phase2 = phase2;
1502 count = EAP_USER_MAX_METHODS;
1503 if (count > EAP_MAX_METHODS)
1504 count = EAP_MAX_METHODS;
1505 for (i = 0; i < count; i++) {
1506 user->methods[i].vendor = eap_user->methods[i].vendor;
1507 user->methods[i].method = eap_user->methods[i].method;
1510 if (eap_user->password) {
1511 user->password = os_malloc(eap_user->password_len);
1512 if (user->password == NULL)
1514 os_memcpy(user->password, eap_user->password,
1515 eap_user->password_len);
1516 user->password_len = eap_user->password_len;
1518 user->force_version = eap_user->force_version;
1519 user->ttls_auth = eap_user->ttls_auth;
1525 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1527 struct hostapd_data *hapd = ctx;
1528 struct sta_info *sta;
1529 sta = ap_get_sta(hapd, addr);
1530 if (sta == NULL || sta->eapol_sm == NULL)
1536 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1537 eapol_logger_level level, const char *txt)
1539 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1540 struct hostapd_data *hapd = ctx;
1544 case EAPOL_LOGGER_WARNING:
1545 hlevel = HOSTAPD_LEVEL_WARNING;
1547 case EAPOL_LOGGER_INFO:
1548 hlevel = HOSTAPD_LEVEL_INFO;
1550 case EAPOL_LOGGER_DEBUG:
1552 hlevel = HOSTAPD_LEVEL_DEBUG;
1556 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1558 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1562 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1565 struct hostapd_data *hapd = ctx;
1566 struct sta_info *sta = sta_ctx;
1567 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1571 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1573 struct hostapd_data *hapd = ctx;
1574 struct sta_info *sta = sta_ctx;
1575 ieee802_1x_abort_auth(hapd, sta);
1579 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1581 struct hostapd_data *hapd = ctx;
1582 struct sta_info *sta = sta_ctx;
1583 ieee802_1x_tx_key(hapd, sta);
1587 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1588 enum eapol_event type)
1590 /* struct hostapd_data *hapd = ctx; */
1591 struct sta_info *sta = sta_ctx;
1593 case EAPOL_AUTH_SM_CHANGE:
1594 wpa_auth_sm_notify(sta->wpa_sm);
1596 case EAPOL_AUTH_REAUTHENTICATE:
1597 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1603 int ieee802_1x_init(struct hostapd_data *hapd)
1606 struct eapol_auth_config conf;
1607 struct eapol_auth_cb cb;
1609 os_memset(&conf, 0, sizeof(conf));
1611 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1612 conf.wpa = hapd->conf->wpa;
1613 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1614 conf.eap_server = hapd->conf->eap_server;
1615 conf.ssl_ctx = hapd->ssl_ctx;
1616 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1617 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1618 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1619 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1620 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1621 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1622 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1623 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1624 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1625 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1626 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1627 conf.tnc = hapd->conf->tnc;
1628 conf.wps = hapd->wps;
1630 os_memset(&cb, 0, sizeof(cb));
1631 cb.eapol_send = ieee802_1x_eapol_send;
1632 cb.aaa_send = ieee802_1x_aaa_send;
1633 cb.finished = _ieee802_1x_finished;
1634 cb.get_eap_user = ieee802_1x_get_eap_user;
1635 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1636 cb.logger = ieee802_1x_logger;
1637 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1638 cb.abort_auth = _ieee802_1x_abort_auth;
1639 cb.tx_key = _ieee802_1x_tx_key;
1640 cb.eapol_event = ieee802_1x_eapol_event;
1642 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1643 if (hapd->eapol_auth == NULL)
1646 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1647 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
1650 #ifndef CONFIG_NO_RADIUS
1651 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1652 ieee802_1x_receive_auth, hapd))
1654 #endif /* CONFIG_NO_RADIUS */
1656 if (hapd->conf->default_wep_key_len) {
1657 for (i = 0; i < 4; i++)
1658 hapd->drv.set_key(hapd->conf->iface, hapd,
1659 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
1662 ieee802_1x_rekey(hapd, NULL);
1664 if (hapd->eapol_auth->default_wep_key == NULL)
1672 void ieee802_1x_deinit(struct hostapd_data *hapd)
1674 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1676 if (hapd->driver != NULL &&
1677 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1678 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
1680 eapol_auth_deinit(hapd->eapol_auth);
1681 hapd->eapol_auth = NULL;
1685 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1686 const u8 *buf, size_t len, int ack)
1688 struct ieee80211_hdr *hdr;
1689 struct ieee802_1x_hdr *xhdr;
1690 struct ieee802_1x_eapol_key *key;
1692 const unsigned char rfc1042_hdr[ETH_ALEN] =
1693 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1697 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1700 hdr = (struct ieee80211_hdr *) buf;
1701 pos = (u8 *) (hdr + 1);
1702 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1704 pos += sizeof(rfc1042_hdr);
1705 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1709 xhdr = (struct ieee802_1x_hdr *) pos;
1710 pos += sizeof(*xhdr);
1712 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1713 "type=%d length=%d - ack=%d",
1714 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1715 be_to_host16(xhdr->length), ack);
1717 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1718 * or Authenticator state machines, but EAPOL-Key packets are not
1719 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1720 * packets couple of times because otherwise STA keys become
1721 * unsynchronized with AP. */
1722 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1723 pos + sizeof(*key) <= buf + len) {
1724 key = (struct ieee802_1x_eapol_key *) pos;
1725 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1726 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1727 "frame (%scast index=%d)",
1728 key->key_index & BIT(7) ? "uni" : "broad",
1729 key->key_index & ~BIT(7));
1730 /* TODO: re-send EAPOL-Key couple of times (with short delay
1731 * between them?). If all attempt fail, report error and
1732 * deauthenticate STA so that it will get new keys when
1733 * authenticating again (e.g., after returning in range).
1734 * Separate limit/transmit state needed both for unicast and
1735 * broadcast keys(?) */
1737 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1738 * to here and change the key only if the EAPOL-Key packet was Acked.
1745 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1747 if (sm == NULL || sm->identity == NULL)
1750 *len = sm->identity_len;
1751 return sm->identity;
1755 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1758 if (sm == NULL || sm->radius_class.attr == NULL ||
1759 idx >= (int) sm->radius_class.count)
1762 *len = sm->radius_class.attr[idx].len;
1763 return sm->radius_class.attr[idx].data;
1767 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1772 *len = sm->eap_if->eapKeyDataLen;
1773 return sm->eap_if->eapKeyData;
1777 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1782 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1783 eapol_auth_step(sm);
1787 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1792 sm->portValid = valid ? TRUE : FALSE;
1793 eapol_auth_step(sm);
1797 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1802 sm->flags |= EAPOL_SM_PREAUTH;
1804 sm->flags &= ~EAPOL_SM_PREAUTH;
1808 static const char * bool_txt(Boolean bool)
1810 return bool ? "TRUE" : "FALSE";
1814 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1821 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1822 char *buf, size_t buflen)
1825 struct eapol_state_machine *sm = sta->eapol_sm;
1830 ret = os_snprintf(buf + len, buflen - len,
1831 "dot1xPaePortNumber=%d\n"
1832 "dot1xPaePortProtocolVersion=%d\n"
1833 "dot1xPaePortCapabilities=1\n"
1834 "dot1xPaePortInitialize=%d\n"
1835 "dot1xPaePortReauthenticate=FALSE\n",
1839 if (ret < 0 || (size_t) ret >= buflen - len)
1843 /* dot1xAuthConfigTable */
1844 ret = os_snprintf(buf + len, buflen - len,
1845 "dot1xAuthPaeState=%d\n"
1846 "dot1xAuthBackendAuthState=%d\n"
1847 "dot1xAuthAdminControlledDirections=%d\n"
1848 "dot1xAuthOperControlledDirections=%d\n"
1849 "dot1xAuthAuthControlledPortStatus=%d\n"
1850 "dot1xAuthAuthControlledPortControl=%d\n"
1851 "dot1xAuthQuietPeriod=%u\n"
1852 "dot1xAuthServerTimeout=%u\n"
1853 "dot1xAuthReAuthPeriod=%u\n"
1854 "dot1xAuthReAuthEnabled=%s\n"
1855 "dot1xAuthKeyTxEnabled=%s\n",
1856 sm->auth_pae_state + 1,
1857 sm->be_auth_state + 1,
1858 sm->adminControlledDirections,
1859 sm->operControlledDirections,
1865 bool_txt(sm->reAuthEnabled),
1866 bool_txt(sm->keyTxEnabled));
1867 if (ret < 0 || (size_t) ret >= buflen - len)
1871 /* dot1xAuthStatsTable */
1872 ret = os_snprintf(buf + len, buflen - len,
1873 "dot1xAuthEapolFramesRx=%u\n"
1874 "dot1xAuthEapolFramesTx=%u\n"
1875 "dot1xAuthEapolStartFramesRx=%u\n"
1876 "dot1xAuthEapolLogoffFramesRx=%u\n"
1877 "dot1xAuthEapolRespIdFramesRx=%u\n"
1878 "dot1xAuthEapolRespFramesRx=%u\n"
1879 "dot1xAuthEapolReqIdFramesTx=%u\n"
1880 "dot1xAuthEapolReqFramesTx=%u\n"
1881 "dot1xAuthInvalidEapolFramesRx=%u\n"
1882 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1883 "dot1xAuthLastEapolFrameVersion=%u\n"
1884 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1885 sm->dot1xAuthEapolFramesRx,
1886 sm->dot1xAuthEapolFramesTx,
1887 sm->dot1xAuthEapolStartFramesRx,
1888 sm->dot1xAuthEapolLogoffFramesRx,
1889 sm->dot1xAuthEapolRespIdFramesRx,
1890 sm->dot1xAuthEapolRespFramesRx,
1891 sm->dot1xAuthEapolReqIdFramesTx,
1892 sm->dot1xAuthEapolReqFramesTx,
1893 sm->dot1xAuthInvalidEapolFramesRx,
1894 sm->dot1xAuthEapLengthErrorFramesRx,
1895 sm->dot1xAuthLastEapolFrameVersion,
1897 if (ret < 0 || (size_t) ret >= buflen - len)
1901 /* dot1xAuthDiagTable */
1902 ret = os_snprintf(buf + len, buflen - len,
1903 "dot1xAuthEntersConnecting=%u\n"
1904 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1905 "dot1xAuthEntersAuthenticating=%u\n"
1906 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1907 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1908 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1909 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1910 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1911 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1912 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1913 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1914 "dot1xAuthBackendResponses=%u\n"
1915 "dot1xAuthBackendAccessChallenges=%u\n"
1916 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1917 "dot1xAuthBackendAuthSuccesses=%u\n"
1918 "dot1xAuthBackendAuthFails=%u\n",
1919 sm->authEntersConnecting,
1920 sm->authEapLogoffsWhileConnecting,
1921 sm->authEntersAuthenticating,
1922 sm->authAuthSuccessesWhileAuthenticating,
1923 sm->authAuthTimeoutsWhileAuthenticating,
1924 sm->authAuthFailWhileAuthenticating,
1925 sm->authAuthEapStartsWhileAuthenticating,
1926 sm->authAuthEapLogoffWhileAuthenticating,
1927 sm->authAuthReauthsWhileAuthenticated,
1928 sm->authAuthEapStartsWhileAuthenticated,
1929 sm->authAuthEapLogoffWhileAuthenticated,
1930 sm->backendResponses,
1931 sm->backendAccessChallenges,
1932 sm->backendOtherRequestsToSupplicant,
1933 sm->backendAuthSuccesses,
1934 sm->backendAuthFails);
1935 if (ret < 0 || (size_t) ret >= buflen - len)
1939 /* dot1xAuthSessionStatsTable */
1940 ret = os_snprintf(buf + len, buflen - len,
1941 /* TODO: dot1xAuthSessionOctetsRx */
1942 /* TODO: dot1xAuthSessionOctetsTx */
1943 /* TODO: dot1xAuthSessionFramesRx */
1944 /* TODO: dot1xAuthSessionFramesTx */
1945 "dot1xAuthSessionId=%08X-%08X\n"
1946 "dot1xAuthSessionAuthenticMethod=%d\n"
1947 "dot1xAuthSessionTime=%u\n"
1948 "dot1xAuthSessionTerminateCause=999\n"
1949 "dot1xAuthSessionUserName=%s\n",
1950 sta->acct_session_id_hi, sta->acct_session_id_lo,
1951 (wpa_key_mgmt_wpa_ieee8021x(
1952 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1954 (unsigned int) (time(NULL) -
1955 sta->acct_session_start),
1957 if (ret < 0 || (size_t) ret >= buflen - len)
1965 static void ieee802_1x_finished(struct hostapd_data *hapd,
1966 struct sta_info *sta, int success)
1970 /* TODO: get PMKLifetime from WPA parameters */
1971 static const int dot11RSNAConfigPMKLifetime = 43200;
1973 key = ieee802_1x_get_key(sta->eapol_sm, &len);
1974 if (success && key && len >= PMK_LEN &&
1975 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
1976 sta->eapol_sm) == 0) {
1977 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1978 HOSTAPD_LEVEL_DEBUG,
1979 "Added PMKSA cache entry (IEEE 802.1X)");