2 * hostapd / IEEE 802.1X-2004 Authenticator
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "crypto/md5.h"
14 #include "crypto/crypto.h"
15 #include "crypto/random.h"
16 #include "common/ieee802_11_defs.h"
17 #include "radius/radius.h"
18 #include "radius/radius_client.h"
19 #include "eap_server/eap.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "eapol_auth/eapol_auth_sm_i.h"
25 #include "accounting.h"
28 #include "preauth_auth.h"
29 #include "pmksa_cache_auth.h"
30 #include "ap_config.h"
31 #include "ap_drv_ops.h"
32 #include "wps_hostapd.h"
34 #include "ieee802_1x.h"
38 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
39 #endif /* CONFIG_HS20 */
40 static void ieee802_1x_finished(struct hostapd_data *hapd,
41 struct sta_info *sta, int success,
45 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
46 u8 type, const u8 *data, size_t datalen)
49 struct ieee802_1x_hdr *xhdr;
53 len = sizeof(*xhdr) + datalen;
56 wpa_printf(MSG_ERROR, "malloc() failed for "
57 "ieee802_1x_send(len=%lu)",
62 xhdr = (struct ieee802_1x_hdr *) buf;
63 xhdr->version = hapd->conf->eapol_version;
65 xhdr->length = host_to_be16(datalen);
67 if (datalen > 0 && data != NULL)
68 os_memcpy(xhdr + 1, data, datalen);
70 if (wpa_auth_pairwise_set(sta->wpa_sm))
72 #ifdef CONFIG_TESTING_OPTIONS
73 if (hapd->ext_eapol_frame_io) {
74 size_t hex_len = 2 * len + 1;
75 char *hex = os_malloc(hex_len);
78 wpa_snprintf_hex(hex, hex_len, buf, len);
79 wpa_msg(hapd->msg_ctx, MSG_INFO,
80 "EAPOL-TX " MACSTR " %s",
81 MAC2STR(sta->addr), hex);
85 #endif /* CONFIG_TESTING_OPTIONS */
86 if (sta->flags & WLAN_STA_PREAUTH) {
87 rsn_preauth_send(hapd, sta, buf, len);
89 hostapd_drv_hapd_send_eapol(
90 hapd, sta->addr, buf, len,
91 encrypt, hostapd_sta_flags_to_drv(sta->flags));
98 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
99 struct sta_info *sta, int authorized)
103 if (sta->flags & WLAN_STA_PREAUTH)
107 ap_sta_set_authorized(hapd, sta, 1);
108 res = hostapd_set_authorized(hapd, sta, 1);
109 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
110 HOSTAPD_LEVEL_DEBUG, "authorizing port");
112 ap_sta_set_authorized(hapd, sta, 0);
113 res = hostapd_set_authorized(hapd, sta, 0);
114 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
115 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
118 if (res && errno != ENOENT) {
119 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
120 " flags for kernel driver (errno=%d).",
121 MAC2STR(sta->addr), errno);
125 os_get_reltime(&sta->connected_time);
126 accounting_sta_start(hapd, sta);
132 #ifndef CONFIG_NO_RC4
134 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
135 struct sta_info *sta,
136 int idx, int broadcast,
137 u8 *key_data, size_t key_len)
140 struct ieee802_1x_hdr *hdr;
141 struct ieee802_1x_eapol_key *key;
142 size_t len, ekey_len;
143 struct eapol_state_machine *sm = sta->eapol_sm;
148 len = sizeof(*key) + key_len;
149 buf = os_zalloc(sizeof(*hdr) + len);
153 hdr = (struct ieee802_1x_hdr *) buf;
154 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
155 key->type = EAPOL_KEY_TYPE_RC4;
156 WPA_PUT_BE16(key->key_length, key_len);
157 wpa_get_ntp_timestamp(key->replay_counter);
159 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
160 wpa_printf(MSG_ERROR, "Could not get random numbers");
165 key->key_index = idx | (broadcast ? 0 : BIT(7));
166 if (hapd->conf->eapol_key_index_workaround) {
167 /* According to some information, WinXP Supplicant seems to
168 * interpret bit7 as an indication whether the key is to be
169 * activated, so make it possible to enable workaround that
170 * sets this bit for all keys. */
171 key->key_index |= BIT(7);
174 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
175 * MSK[32..63] is used to sign the message. */
176 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
177 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
178 "and signing EAPOL-Key");
182 os_memcpy((u8 *) (key + 1), key_data, key_len);
183 ekey_len = sizeof(key->key_iv) + 32;
184 ekey = os_malloc(ekey_len);
186 wpa_printf(MSG_ERROR, "Could not encrypt key");
190 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
191 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
192 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
195 /* This header is needed here for HMAC-MD5, but it will be regenerated
196 * in ieee802_1x_send() */
197 hdr->version = hapd->conf->eapol_version;
198 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
199 hdr->length = host_to_be16(len);
200 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
203 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
204 " (%s index=%d)", MAC2STR(sm->addr),
205 broadcast ? "broadcast" : "unicast", idx);
206 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
208 sta->eapol_sm->dot1xAuthEapolFramesTx++;
213 static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
215 struct eapol_authenticator *eapol = hapd->eapol_auth;
216 struct eapol_state_machine *sm = sta->eapol_sm;
218 if (sm == NULL || !sm->eap_if->eapKeyData)
221 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
224 #ifndef CONFIG_NO_VLAN
225 if (sta->vlan_id > 0) {
226 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
229 #endif /* CONFIG_NO_VLAN */
231 if (eapol->default_wep_key) {
232 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
233 eapol->default_wep_key,
234 hapd->conf->default_wep_key_len);
237 if (hapd->conf->individual_wep_key_len > 0) {
239 ikey = os_malloc(hapd->conf->individual_wep_key_len);
241 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
243 wpa_printf(MSG_ERROR, "Could not generate random "
244 "individual WEP key.");
249 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
250 ikey, hapd->conf->individual_wep_key_len);
252 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
253 hapd->conf->individual_wep_key_len);
255 /* TODO: set encryption in TX callback, i.e., only after STA
256 * has ACKed EAPOL-Key frame */
257 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
258 sta->addr, 0, 1, NULL, 0, ikey,
259 hapd->conf->individual_wep_key_len)) {
260 wpa_printf(MSG_ERROR, "Could not set individual WEP "
268 #endif /* CONFIG_NO_RC4 */
269 #endif /* CONFIG_FIPS */
272 const char *radius_mode_txt(struct hostapd_data *hapd)
274 switch (hapd->iface->conf->hw_mode) {
275 case HOSTAPD_MODE_IEEE80211AD:
277 case HOSTAPD_MODE_IEEE80211A:
279 case HOSTAPD_MODE_IEEE80211G:
281 case HOSTAPD_MODE_IEEE80211B:
288 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
293 for (i = 0; i < sta->supported_rates_len; i++)
294 if ((sta->supported_rates[i] & 0x7f) > rate)
295 rate = sta->supported_rates[i] & 0x7f;
301 #ifndef CONFIG_NO_RADIUS
302 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
303 struct eapol_state_machine *sm,
304 const u8 *eap, size_t len)
308 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
310 if (len <= sizeof(struct eap_hdr) ||
311 (hdr->code == EAP_CODE_RESPONSE &&
312 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
313 (hdr->code == EAP_CODE_INITIATE &&
314 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
315 (hdr->code != EAP_CODE_RESPONSE &&
316 hdr->code != EAP_CODE_INITIATE))
319 identity = eap_get_identity(sm->eap, &identity_len);
320 if (identity == NULL)
323 /* Save station identity for future RADIUS packets */
324 os_free(sm->identity);
325 sm->identity = (u8 *) dup_binstr(identity, identity_len);
326 if (sm->identity == NULL) {
327 sm->identity_len = 0;
331 sm->identity_len = identity_len;
332 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
333 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
334 sm->dot1xAuthEapolRespIdFramesRx++;
338 static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
339 struct hostapd_radius_attr *req_attr,
340 struct sta_info *sta,
341 struct radius_msg *msg)
346 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
347 val = wpa_auth_get_pairwise(sta->wpa_sm);
348 suite = wpa_cipher_to_suite(ver, val);
350 !hostapd_config_get_radius_attr(req_attr,
351 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
352 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
354 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
358 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
360 WPA_PROTO_RSN : WPA_PROTO_WPA,
361 hapd->conf->wpa_group);
362 if (!hostapd_config_get_radius_attr(req_attr,
363 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
364 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
366 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
370 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
371 suite = wpa_akm_to_suite(val);
373 !hostapd_config_get_radius_attr(req_attr,
374 RADIUS_ATTR_WLAN_AKM_SUITE) &&
375 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
377 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
381 #ifdef CONFIG_IEEE80211W
382 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
383 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
384 hapd->conf->group_mgmt_cipher);
385 if (!hostapd_config_get_radius_attr(
386 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
387 !radius_msg_add_attr_int32(
388 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
389 wpa_printf(MSG_ERROR,
390 "Could not add WLAN-Group-Mgmt-Cipher");
394 #endif /* CONFIG_IEEE80211W */
400 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
401 struct hostapd_radius_attr *req_attr,
402 struct sta_info *sta,
403 struct radius_msg *msg)
407 if (!hostapd_config_get_radius_attr(req_attr,
408 RADIUS_ATTR_SERVICE_TYPE) &&
409 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
410 RADIUS_SERVICE_TYPE_FRAMED)) {
411 wpa_printf(MSG_ERROR, "Could not add Service-Type");
415 if (!hostapd_config_get_radius_attr(req_attr,
416 RADIUS_ATTR_NAS_PORT) &&
418 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
419 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
423 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
425 buf[sizeof(buf) - 1] = '\0';
426 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
427 (u8 *) buf, os_strlen(buf))) {
428 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
432 if (sta->flags & WLAN_STA_PREAUTH) {
433 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
436 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
437 radius_sta_rate(hapd, sta) / 2,
438 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
439 radius_mode_txt(hapd));
440 buf[sizeof(buf) - 1] = '\0';
442 if (!hostapd_config_get_radius_attr(req_attr,
443 RADIUS_ATTR_CONNECT_INFO) &&
444 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
445 (u8 *) buf, os_strlen(buf))) {
446 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
450 if (sta->acct_session_id) {
451 os_snprintf(buf, sizeof(buf), "%016llX",
452 (unsigned long long) sta->acct_session_id);
453 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
454 (u8 *) buf, os_strlen(buf))) {
455 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
460 if ((hapd->conf->wpa & 2) &&
461 !hapd->conf->disable_pmksa_caching &&
462 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
463 os_snprintf(buf, sizeof(buf), "%016llX",
465 sta->eapol_sm->acct_multi_session_id);
466 if (!radius_msg_add_attr(
467 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
468 (u8 *) buf, os_strlen(buf))) {
470 "Could not add Acct-Multi-Session-Id");
475 #ifdef CONFIG_IEEE80211R
476 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
478 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
479 sta->auth_alg == WLAN_AUTH_FT) &&
480 !hostapd_config_get_radius_attr(req_attr,
481 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
482 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
484 hapd->conf->mobility_domain))) {
485 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
488 #endif /* CONFIG_IEEE80211R */
490 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
491 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
498 int add_common_radius_attr(struct hostapd_data *hapd,
499 struct hostapd_radius_attr *req_attr,
500 struct sta_info *sta,
501 struct radius_msg *msg)
504 struct hostapd_radius_attr *attr;
507 if (!hostapd_config_get_radius_attr(req_attr,
508 RADIUS_ATTR_NAS_IP_ADDRESS) &&
509 hapd->conf->own_ip_addr.af == AF_INET &&
510 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
511 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
512 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
517 if (!hostapd_config_get_radius_attr(req_attr,
518 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
519 hapd->conf->own_ip_addr.af == AF_INET6 &&
520 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
521 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
522 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
525 #endif /* CONFIG_IPV6 */
527 if (!hostapd_config_get_radius_attr(req_attr,
528 RADIUS_ATTR_NAS_IDENTIFIER) &&
529 hapd->conf->nas_identifier &&
530 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
531 (u8 *) hapd->conf->nas_identifier,
532 os_strlen(hapd->conf->nas_identifier))) {
533 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
537 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
538 MAC2STR(hapd->own_addr));
539 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
540 hapd->conf->ssid.ssid_len);
541 len += hapd->conf->ssid.ssid_len;
542 if (!hostapd_config_get_radius_attr(req_attr,
543 RADIUS_ATTR_CALLED_STATION_ID) &&
544 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
546 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
550 if (!hostapd_config_get_radius_attr(req_attr,
551 RADIUS_ATTR_NAS_PORT_TYPE) &&
552 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
553 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
554 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
558 #ifdef CONFIG_INTERWORKING
559 if (hapd->conf->interworking &&
560 !is_zero_ether_addr(hapd->conf->hessid)) {
561 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
562 MAC2STR(hapd->conf->hessid));
563 buf[sizeof(buf) - 1] = '\0';
564 if (!hostapd_config_get_radius_attr(req_attr,
565 RADIUS_ATTR_WLAN_HESSID) &&
566 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
567 (u8 *) buf, os_strlen(buf))) {
568 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
572 #endif /* CONFIG_INTERWORKING */
574 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
577 for (attr = req_attr; attr; attr = attr->next) {
578 if (!radius_msg_add_attr(msg, attr->type,
579 wpabuf_head(attr->val),
580 wpabuf_len(attr->val))) {
581 wpa_printf(MSG_ERROR, "Could not add RADIUS "
591 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
592 struct sta_info *sta,
593 const u8 *eap, size_t len)
595 struct radius_msg *msg;
596 struct eapol_state_machine *sm = sta->eapol_sm;
601 ieee802_1x_learn_identity(hapd, sm, eap, len);
603 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
606 sm->radius_identifier = radius_client_get_id(hapd->radius);
607 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
608 sm->radius_identifier);
610 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
614 if (radius_msg_make_authenticator(msg) < 0) {
615 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
620 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
621 sm->identity, sm->identity_len)) {
622 wpa_printf(MSG_INFO, "Could not add User-Name");
626 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
630 /* TODO: should probably check MTU from driver config; 2304 is max for
631 * IEEE 802.11, but use 1400 to avoid problems with too large packets
633 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
634 RADIUS_ATTR_FRAMED_MTU) &&
635 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
636 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
640 if (!radius_msg_add_eap(msg, eap, len)) {
641 wpa_printf(MSG_INFO, "Could not add EAP-Message");
645 /* State attribute must be copied if and only if this packet is
646 * Access-Request reply to the previous Access-Challenge */
647 if (sm->last_recv_radius &&
648 radius_msg_get_hdr(sm->last_recv_radius)->code ==
649 RADIUS_CODE_ACCESS_CHALLENGE) {
650 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
653 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
657 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
661 if (hapd->conf->radius_request_cui) {
664 /* Add previously learned CUI or nul CUI to request CUI */
665 if (sm->radius_cui) {
666 cui = wpabuf_head(sm->radius_cui);
667 cui_len = wpabuf_len(sm->radius_cui);
669 cui = (const u8 *) "\0";
672 if (!radius_msg_add_attr(msg,
673 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
675 wpa_printf(MSG_ERROR, "Could not add CUI");
681 if (hapd->conf->hs20) {
682 u8 ver = 1; /* Release 2 */
683 if (!radius_msg_add_wfa(
684 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
686 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
691 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
695 pos = wpabuf_head_u8(sta->hs20_ie);
696 buf[0] = (*pos) >> 4;
697 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
698 wpabuf_len(sta->hs20_ie) >= 3)
699 id = WPA_GET_LE16(pos + 1);
702 WPA_PUT_BE16(buf + 1, id);
703 if (!radius_msg_add_wfa(
705 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
707 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
713 #endif /* CONFIG_HS20 */
715 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
721 radius_msg_free(msg);
723 #endif /* CONFIG_NO_RADIUS */
726 static void handle_eap_response(struct hostapd_data *hapd,
727 struct sta_info *sta, struct eap_hdr *eap,
731 struct eapol_state_machine *sm = sta->eapol_sm;
735 data = (u8 *) (eap + 1);
737 if (len < sizeof(*eap) + 1) {
738 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
742 sm->eap_type_supp = type = data[0];
744 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
745 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
746 "id=%d len=%d) from STA: EAP Response-%s (%d)",
747 eap->code, eap->identifier, be_to_host16(eap->length),
748 eap_server_get_name(0, type), type);
750 sm->dot1xAuthEapolRespFramesRx++;
752 wpabuf_free(sm->eap_if->eapRespData);
753 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
758 static void handle_eap_initiate(struct hostapd_data *hapd,
759 struct sta_info *sta, struct eap_hdr *eap,
764 struct eapol_state_machine *sm = sta->eapol_sm;
769 if (len < sizeof(*eap) + 1) {
771 "handle_eap_initiate: too short response data");
775 data = (u8 *) (eap + 1);
778 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
779 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
780 "id=%d len=%d) from STA: EAP Initiate type %u",
781 eap->code, eap->identifier, be_to_host16(eap->length),
784 wpabuf_free(sm->eap_if->eapRespData);
785 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
787 #endif /* CONFIG_ERP */
791 /* Process incoming EAP packet from Supplicant */
792 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
798 if (len < sizeof(*eap)) {
799 wpa_printf(MSG_INFO, " too short EAP packet");
803 eap = (struct eap_hdr *) buf;
805 eap_len = be_to_host16(eap->length);
806 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
807 eap->code, eap->identifier, eap_len);
808 if (eap_len < sizeof(*eap)) {
809 wpa_printf(MSG_DEBUG, " Invalid EAP length");
811 } else if (eap_len > len) {
812 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
815 } else if (eap_len < len) {
816 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
817 "packet", (unsigned long) len - eap_len);
821 case EAP_CODE_REQUEST:
822 wpa_printf(MSG_DEBUG, " (request)");
824 case EAP_CODE_RESPONSE:
825 wpa_printf(MSG_DEBUG, " (response)");
826 handle_eap_response(hapd, sta, eap, eap_len);
828 case EAP_CODE_SUCCESS:
829 wpa_printf(MSG_DEBUG, " (success)");
831 case EAP_CODE_FAILURE:
832 wpa_printf(MSG_DEBUG, " (failure)");
834 case EAP_CODE_INITIATE:
835 wpa_printf(MSG_DEBUG, " (initiate)");
836 handle_eap_initiate(hapd, sta, eap, eap_len);
838 case EAP_CODE_FINISH:
839 wpa_printf(MSG_DEBUG, " (finish)");
842 wpa_printf(MSG_DEBUG, " (unknown code)");
848 static struct eapol_state_machine *
849 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
852 if (sta->flags & WLAN_STA_PREAUTH)
853 flags |= EAPOL_SM_PREAUTH;
855 flags |= EAPOL_SM_USES_WPA;
856 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
857 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
859 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
860 sta->wps_ie, sta->p2p_ie, sta,
861 sta->identity, sta->radius_cui);
865 static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
868 if (sta->pending_eapol_rx) {
869 wpabuf_free(sta->pending_eapol_rx->buf);
871 sta->pending_eapol_rx =
872 os_malloc(sizeof(*sta->pending_eapol_rx));
873 if (!sta->pending_eapol_rx)
877 sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
878 if (!sta->pending_eapol_rx->buf) {
879 os_free(sta->pending_eapol_rx);
880 sta->pending_eapol_rx = NULL;
884 os_get_reltime(&sta->pending_eapol_rx->rx_time);
889 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
890 * @hapd: hostapd BSS data
891 * @sa: Source address (sender of the EAPOL frame)
893 * @len: Length of buf in octets
895 * This function is called for each incoming EAPOL frame from the interface
897 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
900 struct sta_info *sta;
901 struct ieee802_1x_hdr *hdr;
902 struct ieee802_1x_eapol_key *key;
904 struct rsn_pmksa_cache_entry *pmksa;
907 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
908 !hapd->conf->wps_state)
911 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
912 (unsigned long) len, MAC2STR(sa));
913 sta = ap_get_sta(hapd, sa);
914 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
915 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
916 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
917 "associated/Pre-authenticating STA");
919 if (sta && (sta->flags & WLAN_STA_AUTH)) {
920 wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
921 " for later use", MAC2STR(sta->addr));
922 ieee802_1x_save_eapol(sta, buf, len);
928 if (len < sizeof(*hdr)) {
929 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
933 hdr = (struct ieee802_1x_hdr *) buf;
934 datalen = be_to_host16(hdr->length);
935 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
936 hdr->version, hdr->type, datalen);
938 if (len - sizeof(*hdr) < datalen) {
939 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
941 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
944 if (len - sizeof(*hdr) > datalen) {
945 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
946 "IEEE 802.1X packet",
947 (unsigned long) len - sizeof(*hdr) - datalen);
951 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
952 sta->eapol_sm->dot1xAuthEapolFramesRx++;
955 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
956 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
957 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
958 (key->type == EAPOL_KEY_TYPE_WPA ||
959 key->type == EAPOL_KEY_TYPE_RSN)) {
960 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
961 sizeof(*hdr) + datalen);
965 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
966 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
967 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
968 "802.1X not enabled and WPS not used");
972 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
973 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
974 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
979 if (!sta->eapol_sm) {
980 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
985 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
986 u32 wflags = sta->flags & (WLAN_STA_WPS |
989 if (wflags == WLAN_STA_MAYBE_WPS ||
990 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
992 * Delay EAPOL frame transmission until a
993 * possible WPS STA initiates the handshake
994 * with EAPOL-Start. Only allow the wait to be
995 * skipped if the STA is known to support WPS
998 wpa_printf(MSG_DEBUG, "WPS: Do not start "
999 "EAPOL until EAPOL-Start is "
1001 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1004 #endif /* CONFIG_WPS */
1006 sta->eapol_sm->eap_if->portEnabled = TRUE;
1009 /* since we support version 1, we can ignore version field and proceed
1010 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1011 /* TODO: actually, we are not version 1 anymore.. However, Version 2
1012 * does not change frame contents, so should be ok to process frames
1013 * more or less identically. Some changes might be needed for
1014 * verification of fields. */
1016 switch (hdr->type) {
1017 case IEEE802_1X_TYPE_EAP_PACKET:
1018 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1021 case IEEE802_1X_TYPE_EAPOL_START:
1022 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1023 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
1025 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1026 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1028 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1029 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
1030 "available - ignore it since "
1031 "STA sent EAPOL-Start");
1032 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1034 sta->eapol_sm->eapolStart = TRUE;
1035 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1036 eap_server_clear_identity(sta->eapol_sm->eap);
1037 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1040 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1041 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1042 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
1044 sta->acct_terminate_cause =
1045 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1046 accounting_sta_stop(hapd, sta);
1047 sta->eapol_sm->eapolLogoff = TRUE;
1048 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1049 eap_server_clear_identity(sta->eapol_sm->eap);
1052 case IEEE802_1X_TYPE_EAPOL_KEY:
1053 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1054 if (!ap_sta_is_authorized(sta)) {
1055 wpa_printf(MSG_DEBUG, " Dropped key data from "
1056 "unauthorized Supplicant");
1061 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1062 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1063 /* TODO: implement support for this; show data */
1067 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1068 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1072 eapol_auth_step(sta->eapol_sm);
1077 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1078 * @hapd: hostapd BSS data
1081 * This function is called to start IEEE 802.1X authentication when a new
1082 * station completes IEEE 802.11 association.
1084 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1086 struct rsn_pmksa_cache_entry *pmksa;
1092 if (hapd->conf->wps_state &&
1093 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1094 (sta->flags & WLAN_STA_WPS))) {
1096 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1097 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1098 * authentication in this BSS.
1102 #endif /* CONFIG_WPS */
1104 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1105 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1106 "802.1X not enabled or forced for WPS");
1108 * Clear any possible EAPOL authenticator state to support
1109 * reassociation change from WPS to PSK.
1111 ieee802_1x_free_station(hapd, sta);
1115 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1116 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1117 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1119 * Clear any possible EAPOL authenticator state to support
1120 * reassociation change from WPA-EAP to PSK.
1122 ieee802_1x_free_station(hapd, sta);
1126 if (sta->eapol_sm == NULL) {
1127 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1128 HOSTAPD_LEVEL_DEBUG, "start authentication");
1129 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1130 if (sta->eapol_sm == NULL) {
1131 hostapd_logger(hapd, sta->addr,
1132 HOSTAPD_MODULE_IEEE8021X,
1134 "failed to allocate state machine");
1141 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1142 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1143 !(sta->flags & WLAN_STA_WPS2)) {
1145 * Delay EAPOL frame transmission until a possible WPS STA
1146 * initiates the handshake with EAPOL-Start. Only allow the
1147 * wait to be skipped if the STA is known to support WPS 2.0.
1149 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1150 "EAPOL-Start is received");
1151 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1153 #endif /* CONFIG_WPS */
1155 sta->eapol_sm->eap_if->portEnabled = TRUE;
1157 #ifdef CONFIG_IEEE80211R
1158 if (sta->auth_alg == WLAN_AUTH_FT) {
1159 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1160 HOSTAPD_LEVEL_DEBUG,
1161 "PMK from FT - skip IEEE 802.1X/EAP");
1162 /* Setup EAPOL state machines to already authenticated state
1163 * because of existing FT information from R0KH. */
1164 sta->eapol_sm->keyRun = TRUE;
1165 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1166 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1167 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1168 sta->eapol_sm->authSuccess = TRUE;
1169 sta->eapol_sm->authFail = FALSE;
1170 sta->eapol_sm->portValid = TRUE;
1171 if (sta->eapol_sm->eap)
1172 eap_sm_notify_cached(sta->eapol_sm->eap);
1173 /* TODO: get vlan_id from R0KH using RRB message */
1176 #endif /* CONFIG_IEEE80211R */
1178 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1180 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1181 HOSTAPD_LEVEL_DEBUG,
1182 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1183 /* Setup EAPOL state machines to already authenticated state
1184 * because of existing PMKSA information in the cache. */
1185 sta->eapol_sm->keyRun = TRUE;
1186 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1187 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1188 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1189 sta->eapol_sm->authSuccess = TRUE;
1190 sta->eapol_sm->authFail = FALSE;
1191 if (sta->eapol_sm->eap)
1192 eap_sm_notify_cached(sta->eapol_sm->eap);
1193 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
1194 ap_sta_bind_vlan(hapd, sta);
1198 * Force EAPOL state machines to start
1199 * re-authentication without having to wait for the
1200 * Supplicant to send EAPOL-Start.
1202 sta->eapol_sm->reAuthenticate = TRUE;
1204 eapol_auth_step(sta->eapol_sm);
1209 void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
1211 struct eapol_state_machine *sm = sta->eapol_sm;
1214 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1215 #endif /* CONFIG_HS20 */
1217 if (sta->pending_eapol_rx) {
1218 wpabuf_free(sta->pending_eapol_rx->buf);
1219 os_free(sta->pending_eapol_rx);
1220 sta->pending_eapol_rx = NULL;
1226 sta->eapol_sm = NULL;
1228 #ifndef CONFIG_NO_RADIUS
1229 radius_msg_free(sm->last_recv_radius);
1230 radius_free_class(&sm->radius_class);
1231 #endif /* CONFIG_NO_RADIUS */
1233 eapol_auth_free(sm);
1237 #ifndef CONFIG_NO_RADIUS
1238 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1239 struct sta_info *sta)
1242 const struct eap_hdr *hdr;
1245 struct radius_msg *msg;
1246 struct eapol_state_machine *sm = sta->eapol_sm;
1248 if (sm == NULL || sm->last_recv_radius == NULL) {
1250 sm->eap_if->aaaEapNoReq = TRUE;
1254 msg = sm->last_recv_radius;
1256 eap = radius_msg_get_eap(msg);
1258 /* RFC 3579, Chap. 2.6.3:
1259 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1261 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1262 HOSTAPD_LEVEL_WARNING, "could not extract "
1263 "EAP-Message from RADIUS message");
1264 sm->eap_if->aaaEapNoReq = TRUE;
1268 if (wpabuf_len(eap) < sizeof(*hdr)) {
1269 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1270 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1271 "received from authentication server");
1273 sm->eap_if->aaaEapNoReq = TRUE;
1277 if (wpabuf_len(eap) > sizeof(*hdr))
1278 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1280 hdr = wpabuf_head(eap);
1281 switch (hdr->code) {
1282 case EAP_CODE_REQUEST:
1284 sm->eap_type_authsrv = eap_type;
1285 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1286 eap_server_get_name(0, eap_type), eap_type);
1288 case EAP_CODE_RESPONSE:
1289 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1290 eap_server_get_name(0, eap_type), eap_type);
1292 case EAP_CODE_SUCCESS:
1293 os_strlcpy(buf, "EAP Success", sizeof(buf));
1295 case EAP_CODE_FAILURE:
1296 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1299 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1302 buf[sizeof(buf) - 1] = '\0';
1303 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1304 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1305 "id=%d len=%d) from RADIUS server: %s",
1306 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1308 sm->eap_if->aaaEapReq = TRUE;
1310 wpabuf_free(sm->eap_if->aaaEapReqData);
1311 sm->eap_if->aaaEapReqData = eap;
1315 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1316 struct sta_info *sta, struct radius_msg *msg,
1317 struct radius_msg *req,
1318 const u8 *shared_secret,
1319 size_t shared_secret_len)
1321 struct radius_ms_mppe_keys *keys;
1322 struct eapol_state_machine *sm = sta->eapol_sm;
1326 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1329 if (keys && keys->send && keys->recv) {
1330 size_t len = keys->send_len + keys->recv_len;
1331 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1332 keys->send, keys->send_len);
1333 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1334 keys->recv, keys->recv_len);
1336 os_free(sm->eap_if->aaaEapKeyData);
1337 sm->eap_if->aaaEapKeyData = os_malloc(len);
1338 if (sm->eap_if->aaaEapKeyData) {
1339 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1341 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1342 keys->send, keys->send_len);
1343 sm->eap_if->aaaEapKeyDataLen = len;
1344 sm->eap_if->aaaEapKeyAvailable = TRUE;
1347 wpa_printf(MSG_DEBUG,
1348 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1349 keys, keys ? keys->send : NULL,
1350 keys ? keys->recv : NULL);
1354 os_free(keys->send);
1355 os_free(keys->recv);
1361 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1362 struct sta_info *sta,
1363 struct radius_msg *msg)
1367 struct eapol_state_machine *sm = sta->eapol_sm;
1369 struct radius_attr_data *nclass;
1370 size_t nclass_count;
1372 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1376 radius_free_class(&sm->radius_class);
1377 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1381 nclass = os_calloc(count, sizeof(struct radius_attr_data));
1388 for (i = 0; i < count; i++) {
1390 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1391 &attr_class, &class_len,
1396 } while (class_len < 1);
1398 nclass[nclass_count].data = os_malloc(class_len);
1399 if (nclass[nclass_count].data == NULL)
1402 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
1403 nclass[nclass_count].len = class_len;
1407 sm->radius_class.attr = nclass;
1408 sm->radius_class.count = nclass_count;
1409 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1410 "attributes for " MACSTR,
1411 (unsigned long) sm->radius_class.count,
1412 MAC2STR(sta->addr));
1416 /* Update sta->identity based on User-Name attribute in Access-Accept */
1417 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1418 struct sta_info *sta,
1419 struct radius_msg *msg)
1423 struct eapol_state_machine *sm = sta->eapol_sm;
1428 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1432 identity = (u8 *) dup_binstr(buf, len);
1433 if (identity == NULL)
1436 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1437 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1438 "User-Name from Access-Accept '%s'",
1439 sm->identity ? (char *) sm->identity : "N/A",
1442 os_free(sm->identity);
1443 sm->identity = identity;
1444 sm->identity_len = len;
1448 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1449 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1450 struct sta_info *sta,
1451 struct radius_msg *msg)
1453 struct eapol_state_machine *sm = sta->eapol_sm;
1461 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1462 &buf, &len, NULL) < 0)
1465 cui = wpabuf_alloc_copy(buf, len);
1469 wpabuf_free(sm->radius_cui);
1470 sm->radius_cui = cui;
1476 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1478 sta->remediation = 1;
1479 os_free(sta->remediation_url);
1481 sta->remediation_url = os_malloc(len);
1482 if (!sta->remediation_url)
1484 sta->remediation_method = pos[0];
1485 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1486 sta->remediation_url[len - 1] = '\0';
1487 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1488 "for " MACSTR " - server method %u URL %s",
1489 MAC2STR(sta->addr), sta->remediation_method,
1490 sta->remediation_url);
1492 sta->remediation_url = NULL;
1493 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1494 "for " MACSTR, MAC2STR(sta->addr));
1496 /* TODO: assign the STA into remediation VLAN or add filtering */
1500 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1501 struct sta_info *sta, u8 *pos,
1505 return; /* Malformed information */
1506 sta->hs20_deauth_requested = 1;
1507 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1509 *pos, WPA_GET_LE16(pos + 1));
1510 wpabuf_free(sta->hs20_deauth_req);
1511 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1512 if (sta->hs20_deauth_req) {
1513 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1514 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1515 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1517 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1521 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1522 struct sta_info *sta, u8 *pos,
1523 size_t len, int session_timeout)
1526 int warning_time, beacon_int;
1529 return; /* Malformed information */
1530 os_free(sta->hs20_session_info_url);
1531 sta->hs20_session_info_url = os_malloc(len);
1532 if (sta->hs20_session_info_url == NULL)
1535 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1536 sta->hs20_session_info_url[len - 1] = '\0';
1537 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1538 "(session_timeout=%d)",
1539 sta->hs20_session_info_url, swt, session_timeout);
1540 if (session_timeout < 0) {
1541 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1545 swt = 1; /* Use one minute as the AP selected value */
1547 if ((unsigned int) session_timeout < swt * 60)
1550 warning_time = session_timeout - swt * 60;
1552 beacon_int = hapd->iconf->beacon_int;
1554 beacon_int = 100; /* best guess */
1555 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1556 if (sta->hs20_disassoc_timer > 65535)
1557 sta->hs20_disassoc_timer = 65535;
1559 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1562 #endif /* CONFIG_HS20 */
1565 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1566 struct sta_info *sta,
1567 struct radius_msg *msg,
1568 int session_timeout)
1571 u8 *buf, *pos, *end, type, sublen;
1575 sta->remediation = 0;
1576 sta->hs20_deauth_requested = 0;
1579 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1580 &buf, &len, buf) < 0)
1586 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1593 continue; /* invalid length */
1594 sublen -= 2; /* skip header */
1595 if (pos + sublen > end)
1596 continue; /* invalid WFA VSA */
1599 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1600 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1602 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1603 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1605 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1606 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1611 #endif /* CONFIG_HS20 */
1615 struct sta_id_search {
1617 struct eapol_state_machine *sm;
1621 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1622 struct sta_info *sta,
1625 struct sta_id_search *id_search = ctx;
1626 struct eapol_state_machine *sm = sta->eapol_sm;
1628 if (sm && sm->radius_identifier >= 0 &&
1629 sm->radius_identifier == id_search->identifier) {
1637 static struct eapol_state_machine *
1638 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1640 struct sta_id_search id_search;
1641 id_search.identifier = identifier;
1642 id_search.sm = NULL;
1643 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1644 return id_search.sm;
1649 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1650 * @msg: RADIUS response message
1651 * @req: RADIUS request message
1652 * @shared_secret: RADIUS shared secret
1653 * @shared_secret_len: Length of shared_secret in octets
1654 * @data: Context data (struct hostapd_data *)
1655 * Returns: Processing status
1657 static RadiusRxResult
1658 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1659 const u8 *shared_secret, size_t shared_secret_len,
1662 struct hostapd_data *hapd = data;
1663 struct sta_info *sta;
1664 u32 session_timeout = 0, termination_action, acct_interim_interval;
1665 int session_timeout_set;
1666 struct eapol_state_machine *sm;
1667 int override_eapReq = 0;
1668 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1669 struct vlan_description vlan_desc;
1670 #ifndef CONFIG_NO_VLAN
1671 int *untagged, *tagged, *notempty;
1672 #endif /* CONFIG_NO_VLAN */
1674 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1676 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1678 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1679 "station for this RADIUS message");
1680 return RADIUS_RX_UNKNOWN;
1684 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1685 * present when packet contains an EAP-Message attribute */
1686 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1687 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1689 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1690 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1691 "Message-Authenticator since it does not include "
1693 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1695 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1696 return RADIUS_RX_INVALID_AUTHENTICATOR;
1699 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1700 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1701 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1702 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1703 return RADIUS_RX_UNKNOWN;
1706 sm->radius_identifier = -1;
1707 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1708 MAC2STR(sta->addr));
1710 radius_msg_free(sm->last_recv_radius);
1711 sm->last_recv_radius = msg;
1713 session_timeout_set =
1714 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1716 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1717 &termination_action))
1718 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1720 if (hapd->conf->acct_interim_interval == 0 &&
1721 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1722 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1723 &acct_interim_interval) == 0) {
1724 if (acct_interim_interval < 60) {
1725 hostapd_logger(hapd, sta->addr,
1726 HOSTAPD_MODULE_IEEE8021X,
1728 "ignored too small "
1729 "Acct-Interim-Interval %d",
1730 acct_interim_interval);
1732 sta->acct_interim_interval = acct_interim_interval;
1736 switch (hdr->code) {
1737 case RADIUS_CODE_ACCESS_ACCEPT:
1738 #ifndef CONFIG_NO_VLAN
1739 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED) {
1740 notempty = &vlan_desc.notempty;
1741 untagged = &vlan_desc.untagged;
1742 tagged = vlan_desc.tagged;
1743 *notempty = !!radius_msg_get_vlanid(msg, untagged,
1744 MAX_NUM_TAGGED_VLAN,
1748 if (vlan_desc.notempty &&
1749 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1750 sta->eapol_sm->authFail = TRUE;
1751 hostapd_logger(hapd, sta->addr,
1752 HOSTAPD_MODULE_RADIUS,
1754 "Invalid VLAN %d%s received from RADIUS server",
1756 vlan_desc.tagged[0] ? "+" : "");
1757 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1758 ap_sta_set_vlan(hapd, sta, &vlan_desc);
1762 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1763 !vlan_desc.notempty) {
1764 sta->eapol_sm->authFail = TRUE;
1765 hostapd_logger(hapd, sta->addr,
1766 HOSTAPD_MODULE_IEEE8021X,
1767 HOSTAPD_LEVEL_INFO, "authentication "
1768 "server did not include required VLAN "
1769 "ID in Access-Accept");
1772 #endif /* CONFIG_NO_VLAN */
1774 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0)
1777 #ifndef CONFIG_NO_VLAN
1778 if (sta->vlan_id > 0) {
1779 hostapd_logger(hapd, sta->addr,
1780 HOSTAPD_MODULE_RADIUS,
1782 "VLAN ID %d", sta->vlan_id);
1784 #endif /* CONFIG_NO_VLAN */
1786 if ((sta->flags & WLAN_STA_ASSOC) &&
1787 ap_sta_bind_vlan(hapd, sta) < 0)
1790 sta->session_timeout_set = !!session_timeout_set;
1791 sta->session_timeout = session_timeout;
1793 /* RFC 3580, Ch. 3.17 */
1794 if (session_timeout_set && termination_action ==
1795 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1796 sm->reAuthPeriod = session_timeout;
1797 } else if (session_timeout_set)
1798 ap_sta_session_timeout(hapd, sta, session_timeout);
1800 sm->eap_if->aaaSuccess = TRUE;
1801 override_eapReq = 1;
1802 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1804 ieee802_1x_store_radius_class(hapd, sta, msg);
1805 ieee802_1x_update_sta_identity(hapd, sta, msg);
1806 ieee802_1x_update_sta_cui(hapd, sta, msg);
1807 ieee802_1x_check_hs20(hapd, sta, msg,
1808 session_timeout_set ?
1809 (int) session_timeout : -1);
1811 case RADIUS_CODE_ACCESS_REJECT:
1812 sm->eap_if->aaaFail = TRUE;
1813 override_eapReq = 1;
1815 case RADIUS_CODE_ACCESS_CHALLENGE:
1816 sm->eap_if->aaaEapReq = TRUE;
1817 if (session_timeout_set) {
1818 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1819 sm->eap_if->aaaMethodTimeout = session_timeout;
1820 hostapd_logger(hapd, sm->addr,
1821 HOSTAPD_MODULE_IEEE8021X,
1822 HOSTAPD_LEVEL_DEBUG,
1823 "using EAP timeout of %d seconds (from "
1825 sm->eap_if->aaaMethodTimeout);
1828 * Use dynamic retransmission behavior per EAP
1831 sm->eap_if->aaaMethodTimeout = 0;
1836 ieee802_1x_decapsulate_radius(hapd, sta);
1837 if (override_eapReq)
1838 sm->eap_if->aaaEapReq = FALSE;
1840 eapol_auth_step(sm);
1842 return RADIUS_RX_QUEUED;
1844 #endif /* CONFIG_NO_RADIUS */
1847 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1849 struct eapol_state_machine *sm = sta->eapol_sm;
1853 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1854 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1856 #ifndef CONFIG_NO_RADIUS
1857 radius_msg_free(sm->last_recv_radius);
1858 sm->last_recv_radius = NULL;
1859 #endif /* CONFIG_NO_RADIUS */
1861 if (sm->eap_if->eapTimeout) {
1863 * Disconnect the STA since it did not reply to the last EAP
1864 * request and we cannot continue EAP processing (EAP-Failure
1865 * could only be sent if the EAP peer actually replied).
1867 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1868 MAC2STR(sta->addr));
1870 sm->eap_if->portEnabled = FALSE;
1871 ap_sta_disconnect(hapd, sta, sta->addr,
1872 WLAN_REASON_PREV_AUTH_NOT_VALID);
1877 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1879 struct eapol_authenticator *eapol = hapd->eapol_auth;
1881 if (hapd->conf->default_wep_key_len < 1)
1884 os_free(eapol->default_wep_key);
1885 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1886 if (eapol->default_wep_key == NULL ||
1887 random_get_bytes(eapol->default_wep_key,
1888 hapd->conf->default_wep_key_len)) {
1889 wpa_printf(MSG_INFO, "Could not generate random WEP key");
1890 os_free(eapol->default_wep_key);
1891 eapol->default_wep_key = NULL;
1895 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1896 eapol->default_wep_key,
1897 hapd->conf->default_wep_key_len);
1903 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1904 struct sta_info *sta, void *ctx)
1906 if (sta->eapol_sm) {
1907 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1908 eapol_auth_step(sta->eapol_sm);
1914 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1916 struct hostapd_data *hapd = eloop_ctx;
1917 struct eapol_authenticator *eapol = hapd->eapol_auth;
1919 if (eapol->default_wep_key_idx >= 3)
1920 eapol->default_wep_key_idx =
1921 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1923 eapol->default_wep_key_idx++;
1925 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1926 eapol->default_wep_key_idx);
1928 if (ieee802_1x_rekey_broadcast(hapd)) {
1929 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1930 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1931 "new broadcast key");
1932 os_free(eapol->default_wep_key);
1933 eapol->default_wep_key = NULL;
1937 /* TODO: Could setup key for RX here, but change default TX keyid only
1938 * after new broadcast key has been sent to all stations. */
1939 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1940 broadcast_ether_addr,
1941 eapol->default_wep_key_idx, 1, NULL, 0,
1942 eapol->default_wep_key,
1943 hapd->conf->default_wep_key_len)) {
1944 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1945 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1946 "new broadcast key");
1947 os_free(eapol->default_wep_key);
1948 eapol->default_wep_key = NULL;
1952 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1954 if (hapd->conf->wep_rekeying_period > 0) {
1955 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1956 ieee802_1x_rekey, hapd, NULL);
1961 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1962 const u8 *data, size_t datalen)
1965 struct sta_info *sta = sta_ctx;
1967 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1968 WLAN_STA_MAYBE_WPS) {
1970 size_t identity_len;
1971 struct eapol_state_machine *sm = sta->eapol_sm;
1973 identity = eap_get_identity(sm->eap, &identity_len);
1975 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1976 os_memcmp(identity, WSC_ID_ENROLLEE,
1977 WSC_ID_ENROLLEE_LEN) == 0) ||
1978 (identity_len == WSC_ID_REGISTRAR_LEN &&
1979 os_memcmp(identity, WSC_ID_REGISTRAR,
1980 WSC_ID_REGISTRAR_LEN) == 0))) {
1981 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1983 sta->flags |= WLAN_STA_WPS;
1986 #endif /* CONFIG_WPS */
1988 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1992 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1993 const u8 *data, size_t datalen)
1995 #ifndef CONFIG_NO_RADIUS
1996 struct hostapd_data *hapd = ctx;
1997 struct sta_info *sta = sta_ctx;
1999 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2000 #endif /* CONFIG_NO_RADIUS */
2004 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2005 int preauth, int remediation)
2007 struct hostapd_data *hapd = ctx;
2008 struct sta_info *sta = sta_ctx;
2010 rsn_preauth_finished(hapd, sta, success);
2012 ieee802_1x_finished(hapd, sta, success, remediation);
2016 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2017 size_t identity_len, int phase2,
2018 struct eap_user *user)
2020 struct hostapd_data *hapd = ctx;
2021 const struct hostapd_eap_user *eap_user;
2025 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
2026 if (eap_user == NULL)
2029 os_memset(user, 0, sizeof(*user));
2030 user->phase2 = phase2;
2031 for (i = 0; i < EAP_MAX_METHODS; i++) {
2032 user->methods[i].vendor = eap_user->methods[i].vendor;
2033 user->methods[i].method = eap_user->methods[i].method;
2036 if (eap_user->password) {
2037 user->password = os_malloc(eap_user->password_len);
2038 if (user->password == NULL)
2040 os_memcpy(user->password, eap_user->password,
2041 eap_user->password_len);
2042 user->password_len = eap_user->password_len;
2043 user->password_hash = eap_user->password_hash;
2045 user->force_version = eap_user->force_version;
2046 user->macacl = eap_user->macacl;
2047 user->ttls_auth = eap_user->ttls_auth;
2048 user->remediation = eap_user->remediation;
2053 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2059 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2061 struct hostapd_data *hapd = ctx;
2062 struct sta_info *sta;
2063 sta = ap_get_sta(hapd, addr);
2064 if (sta == NULL || sta->eapol_sm == NULL)
2070 static void ieee802_1x_logger(void *ctx, const u8 *addr,
2071 eapol_logger_level level, const char *txt)
2073 #ifndef CONFIG_NO_HOSTAPD_LOGGER
2074 struct hostapd_data *hapd = ctx;
2078 case EAPOL_LOGGER_WARNING:
2079 hlevel = HOSTAPD_LEVEL_WARNING;
2081 case EAPOL_LOGGER_INFO:
2082 hlevel = HOSTAPD_LEVEL_INFO;
2084 case EAPOL_LOGGER_DEBUG:
2086 hlevel = HOSTAPD_LEVEL_DEBUG;
2090 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2092 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
2096 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2099 struct hostapd_data *hapd = ctx;
2100 struct sta_info *sta = sta_ctx;
2101 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2105 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2107 struct hostapd_data *hapd = ctx;
2108 struct sta_info *sta = sta_ctx;
2109 ieee802_1x_abort_auth(hapd, sta);
2113 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2116 #ifndef CONFIG_NO_RC4
2117 struct hostapd_data *hapd = ctx;
2118 struct sta_info *sta = sta_ctx;
2119 ieee802_1x_tx_key(hapd, sta);
2120 #endif /* CONFIG_NO_RC4 */
2121 #endif /* CONFIG_FIPS */
2125 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2126 enum eapol_event type)
2128 /* struct hostapd_data *hapd = ctx; */
2129 struct sta_info *sta = sta_ctx;
2131 case EAPOL_AUTH_SM_CHANGE:
2132 wpa_auth_sm_notify(sta->wpa_sm);
2134 case EAPOL_AUTH_REAUTHENTICATE:
2135 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2143 static struct eap_server_erp_key *
2144 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2146 struct hostapd_data *hapd = ctx;
2147 struct eap_server_erp_key *erp;
2149 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2151 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2159 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2161 struct hostapd_data *hapd = ctx;
2163 dl_list_add(&hapd->erp_keys, &erp->list);
2167 #endif /* CONFIG_ERP */
2170 int ieee802_1x_init(struct hostapd_data *hapd)
2173 struct eapol_auth_config conf;
2174 struct eapol_auth_cb cb;
2176 dl_list_init(&hapd->erp_keys);
2178 os_memset(&conf, 0, sizeof(conf));
2180 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2181 conf.wpa = hapd->conf->wpa;
2182 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2183 conf.eap_server = hapd->conf->eap_server;
2184 conf.ssl_ctx = hapd->ssl_ctx;
2185 conf.msg_ctx = hapd->msg_ctx;
2186 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2187 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2188 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2189 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2190 conf.erp_domain = hapd->conf->erp_domain;
2191 conf.erp = hapd->conf->eap_server_erp;
2192 conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
2193 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2194 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2195 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2196 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2197 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2198 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2199 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2200 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2201 conf.tnc = hapd->conf->tnc;
2202 conf.wps = hapd->wps;
2203 conf.fragment_size = hapd->conf->fragment_size;
2204 conf.pwd_group = hapd->conf->pwd_group;
2205 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2206 if (hapd->conf->server_id) {
2207 conf.server_id = (const u8 *) hapd->conf->server_id;
2208 conf.server_id_len = os_strlen(hapd->conf->server_id);
2210 conf.server_id = (const u8 *) "hostapd";
2211 conf.server_id_len = 7;
2214 os_memset(&cb, 0, sizeof(cb));
2215 cb.eapol_send = ieee802_1x_eapol_send;
2216 cb.aaa_send = ieee802_1x_aaa_send;
2217 cb.finished = _ieee802_1x_finished;
2218 cb.get_eap_user = ieee802_1x_get_eap_user;
2219 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2220 cb.logger = ieee802_1x_logger;
2221 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2222 cb.abort_auth = _ieee802_1x_abort_auth;
2223 cb.tx_key = _ieee802_1x_tx_key;
2224 cb.eapol_event = ieee802_1x_eapol_event;
2226 cb.erp_get_key = ieee802_1x_erp_get_key;
2227 cb.erp_add_key = ieee802_1x_erp_add_key;
2228 #endif /* CONFIG_ERP */
2230 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2231 if (hapd->eapol_auth == NULL)
2234 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2235 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2238 #ifndef CONFIG_NO_RADIUS
2239 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2240 ieee802_1x_receive_auth, hapd))
2242 #endif /* CONFIG_NO_RADIUS */
2244 if (hapd->conf->default_wep_key_len) {
2245 for (i = 0; i < 4; i++)
2246 hostapd_drv_set_key(hapd->conf->iface, hapd,
2247 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2250 ieee802_1x_rekey(hapd, NULL);
2252 if (hapd->eapol_auth->default_wep_key == NULL)
2260 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2262 struct eap_server_erp_key *erp;
2264 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2266 dl_list_del(&erp->list);
2267 bin_clear_free(erp, sizeof(*erp));
2272 void ieee802_1x_deinit(struct hostapd_data *hapd)
2274 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2276 if (hapd->driver && hapd->drv_priv &&
2277 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2278 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2280 eapol_auth_deinit(hapd->eapol_auth);
2281 hapd->eapol_auth = NULL;
2283 ieee802_1x_erp_flush(hapd);
2287 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2288 const u8 *buf, size_t len, int ack)
2290 struct ieee80211_hdr *hdr;
2292 const unsigned char rfc1042_hdr[ETH_ALEN] =
2293 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2297 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2300 hdr = (struct ieee80211_hdr *) buf;
2301 pos = (u8 *) (hdr + 1);
2302 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2304 pos += sizeof(rfc1042_hdr);
2305 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2309 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2314 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2315 const u8 *buf, int len, int ack)
2317 const struct ieee802_1x_hdr *xhdr =
2318 (const struct ieee802_1x_hdr *) buf;
2319 const u8 *pos = buf + sizeof(*xhdr);
2320 struct ieee802_1x_eapol_key *key;
2322 if (len < (int) sizeof(*xhdr))
2324 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2325 "type=%d length=%d - ack=%d",
2326 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2327 be_to_host16(xhdr->length), ack);
2329 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2332 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2333 const struct wpa_eapol_key *wpa;
2334 wpa = (const struct wpa_eapol_key *) pos;
2335 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2336 wpa->type == EAPOL_KEY_TYPE_WPA)
2337 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2341 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2342 * or Authenticator state machines, but EAPOL-Key packets are not
2343 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2344 * packets couple of times because otherwise STA keys become
2345 * unsynchronized with AP. */
2346 if (!ack && pos + sizeof(*key) <= buf + len) {
2347 key = (struct ieee802_1x_eapol_key *) pos;
2348 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2349 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2350 "frame (%scast index=%d)",
2351 key->key_index & BIT(7) ? "uni" : "broad",
2352 key->key_index & ~BIT(7));
2353 /* TODO: re-send EAPOL-Key couple of times (with short delay
2354 * between them?). If all attempt fail, report error and
2355 * deauthenticate STA so that it will get new keys when
2356 * authenticating again (e.g., after returning in range).
2357 * Separate limit/transmit state needed both for unicast and
2358 * broadcast keys(?) */
2360 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2361 * to here and change the key only if the EAPOL-Key packet was Acked.
2368 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2370 if (sm == NULL || sm->identity == NULL)
2373 *len = sm->identity_len;
2374 return sm->identity;
2378 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2381 if (sm == NULL || sm->radius_class.attr == NULL ||
2382 idx >= (int) sm->radius_class.count)
2385 *len = sm->radius_class.attr[idx].len;
2386 return sm->radius_class.attr[idx].data;
2390 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2394 return sm->radius_cui;
2398 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2404 *len = sm->eap_if->eapKeyDataLen;
2405 return sm->eap_if->eapKeyData;
2409 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2414 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2415 eapol_auth_step(sm);
2419 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2424 sm->portValid = valid ? TRUE : FALSE;
2425 eapol_auth_step(sm);
2429 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2434 sm->flags |= EAPOL_SM_PREAUTH;
2436 sm->flags &= ~EAPOL_SM_PREAUTH;
2440 static const char * bool_txt(Boolean val)
2442 return val ? "TRUE" : "FALSE";
2446 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2453 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2454 char *buf, size_t buflen)
2457 struct eapol_state_machine *sm = sta->eapol_sm;
2458 struct os_reltime diff;
2465 ret = os_snprintf(buf + len, buflen - len,
2466 "dot1xPaePortNumber=%d\n"
2467 "dot1xPaePortProtocolVersion=%d\n"
2468 "dot1xPaePortCapabilities=1\n"
2469 "dot1xPaePortInitialize=%d\n"
2470 "dot1xPaePortReauthenticate=FALSE\n",
2474 if (os_snprintf_error(buflen - len, ret))
2478 /* dot1xAuthConfigTable */
2479 ret = os_snprintf(buf + len, buflen - len,
2480 "dot1xAuthPaeState=%d\n"
2481 "dot1xAuthBackendAuthState=%d\n"
2482 "dot1xAuthAdminControlledDirections=%d\n"
2483 "dot1xAuthOperControlledDirections=%d\n"
2484 "dot1xAuthAuthControlledPortStatus=%d\n"
2485 "dot1xAuthAuthControlledPortControl=%d\n"
2486 "dot1xAuthQuietPeriod=%u\n"
2487 "dot1xAuthServerTimeout=%u\n"
2488 "dot1xAuthReAuthPeriod=%u\n"
2489 "dot1xAuthReAuthEnabled=%s\n"
2490 "dot1xAuthKeyTxEnabled=%s\n",
2491 sm->auth_pae_state + 1,
2492 sm->be_auth_state + 1,
2493 sm->adminControlledDirections,
2494 sm->operControlledDirections,
2500 bool_txt(sm->reAuthEnabled),
2501 bool_txt(sm->keyTxEnabled));
2502 if (os_snprintf_error(buflen - len, ret))
2506 /* dot1xAuthStatsTable */
2507 ret = os_snprintf(buf + len, buflen - len,
2508 "dot1xAuthEapolFramesRx=%u\n"
2509 "dot1xAuthEapolFramesTx=%u\n"
2510 "dot1xAuthEapolStartFramesRx=%u\n"
2511 "dot1xAuthEapolLogoffFramesRx=%u\n"
2512 "dot1xAuthEapolRespIdFramesRx=%u\n"
2513 "dot1xAuthEapolRespFramesRx=%u\n"
2514 "dot1xAuthEapolReqIdFramesTx=%u\n"
2515 "dot1xAuthEapolReqFramesTx=%u\n"
2516 "dot1xAuthInvalidEapolFramesRx=%u\n"
2517 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2518 "dot1xAuthLastEapolFrameVersion=%u\n"
2519 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2520 sm->dot1xAuthEapolFramesRx,
2521 sm->dot1xAuthEapolFramesTx,
2522 sm->dot1xAuthEapolStartFramesRx,
2523 sm->dot1xAuthEapolLogoffFramesRx,
2524 sm->dot1xAuthEapolRespIdFramesRx,
2525 sm->dot1xAuthEapolRespFramesRx,
2526 sm->dot1xAuthEapolReqIdFramesTx,
2527 sm->dot1xAuthEapolReqFramesTx,
2528 sm->dot1xAuthInvalidEapolFramesRx,
2529 sm->dot1xAuthEapLengthErrorFramesRx,
2530 sm->dot1xAuthLastEapolFrameVersion,
2532 if (os_snprintf_error(buflen - len, ret))
2536 /* dot1xAuthDiagTable */
2537 ret = os_snprintf(buf + len, buflen - len,
2538 "dot1xAuthEntersConnecting=%u\n"
2539 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2540 "dot1xAuthEntersAuthenticating=%u\n"
2541 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2542 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2543 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2544 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2545 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2546 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2547 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2548 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2549 "dot1xAuthBackendResponses=%u\n"
2550 "dot1xAuthBackendAccessChallenges=%u\n"
2551 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2552 "dot1xAuthBackendAuthSuccesses=%u\n"
2553 "dot1xAuthBackendAuthFails=%u\n",
2554 sm->authEntersConnecting,
2555 sm->authEapLogoffsWhileConnecting,
2556 sm->authEntersAuthenticating,
2557 sm->authAuthSuccessesWhileAuthenticating,
2558 sm->authAuthTimeoutsWhileAuthenticating,
2559 sm->authAuthFailWhileAuthenticating,
2560 sm->authAuthEapStartsWhileAuthenticating,
2561 sm->authAuthEapLogoffWhileAuthenticating,
2562 sm->authAuthReauthsWhileAuthenticated,
2563 sm->authAuthEapStartsWhileAuthenticated,
2564 sm->authAuthEapLogoffWhileAuthenticated,
2565 sm->backendResponses,
2566 sm->backendAccessChallenges,
2567 sm->backendOtherRequestsToSupplicant,
2568 sm->backendAuthSuccesses,
2569 sm->backendAuthFails);
2570 if (os_snprintf_error(buflen - len, ret))
2574 /* dot1xAuthSessionStatsTable */
2575 os_reltime_age(&sta->acct_session_start, &diff);
2576 ret = os_snprintf(buf + len, buflen - len,
2577 /* TODO: dot1xAuthSessionOctetsRx */
2578 /* TODO: dot1xAuthSessionOctetsTx */
2579 /* TODO: dot1xAuthSessionFramesRx */
2580 /* TODO: dot1xAuthSessionFramesTx */
2581 "dot1xAuthSessionId=%016llX\n"
2582 "dot1xAuthSessionAuthenticMethod=%d\n"
2583 "dot1xAuthSessionTime=%u\n"
2584 "dot1xAuthSessionTerminateCause=999\n"
2585 "dot1xAuthSessionUserName=%s\n",
2586 (unsigned long long) sta->acct_session_id,
2587 (wpa_key_mgmt_wpa_ieee8021x(
2588 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2590 (unsigned int) diff.sec,
2592 if (os_snprintf_error(buflen - len, ret))
2596 if (sm->acct_multi_session_id) {
2597 ret = os_snprintf(buf + len, buflen - len,
2598 "authMultiSessionId=%016llX\n",
2599 (unsigned long long)
2600 sm->acct_multi_session_id);
2601 if (os_snprintf_error(buflen - len, ret))
2606 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2607 name2 = eap_server_get_name(0, sm->eap_type_supp);
2608 ret = os_snprintf(buf + len, buflen - len,
2609 "last_eap_type_as=%d (%s)\n"
2610 "last_eap_type_sta=%d (%s)\n",
2611 sm->eap_type_authsrv, name1,
2612 sm->eap_type_supp, name2);
2613 if (os_snprintf_error(buflen - len, ret))
2622 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
2624 struct hostapd_data *hapd = eloop_ctx;
2625 struct sta_info *sta = timeout_ctx;
2627 if (sta->remediation) {
2628 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2629 MACSTR " to indicate Subscription Remediation",
2630 MAC2STR(sta->addr));
2631 hs20_send_wnm_notification(hapd, sta->addr,
2632 sta->remediation_method,
2633 sta->remediation_url);
2634 os_free(sta->remediation_url);
2635 sta->remediation_url = NULL;
2638 if (sta->hs20_deauth_req) {
2639 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2640 MACSTR " to indicate imminent deauthentication",
2641 MAC2STR(sta->addr));
2642 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
2643 sta->hs20_deauth_req);
2646 #endif /* CONFIG_HS20 */
2649 static void ieee802_1x_finished(struct hostapd_data *hapd,
2650 struct sta_info *sta, int success,
2655 /* TODO: get PMKLifetime from WPA parameters */
2656 static const int dot11RSNAConfigPMKLifetime = 43200;
2657 unsigned int session_timeout;
2660 if (remediation && !sta->remediation) {
2661 sta->remediation = 1;
2662 os_free(sta->remediation_url);
2663 sta->remediation_url =
2664 os_strdup(hapd->conf->subscr_remediation_url);
2665 sta->remediation_method = 1; /* SOAP-XML SPP */
2668 if (success && (sta->remediation || sta->hs20_deauth_req)) {
2669 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
2670 MACSTR " in 100 ms", MAC2STR(sta->addr));
2671 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
2672 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
2675 #endif /* CONFIG_HS20 */
2677 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2678 if (sta->session_timeout_set)
2679 session_timeout = sta->session_timeout;
2681 session_timeout = dot11RSNAConfigPMKLifetime;
2682 if (success && key && len >= PMK_LEN && !sta->remediation &&
2683 !sta->hs20_deauth_requested &&
2684 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
2685 sta->eapol_sm) == 0) {
2686 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2687 HOSTAPD_LEVEL_DEBUG,
2688 "Added PMKSA cache entry (IEEE 802.1X)");
2693 * Many devices require deauthentication after WPS provisioning
2694 * and some may not be be able to do that themselves, so
2695 * disconnect the client here. In addition, this may also
2696 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2697 * the EAPOL PAE state machine would remain in HELD state for
2698 * considerable amount of time and some EAP methods, like
2699 * EAP-FAST with anonymous provisioning, may require another
2700 * EAPOL authentication to be started to complete connection.
2702 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2703 "disconnection after EAP-Failure");
2704 /* Add a small sleep to increase likelihood of previously
2705 * requested EAP-Failure TX getting out before this should the
2706 * driver reorder operations.
2709 ap_sta_disconnect(hapd, sta, sta->addr,
2710 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
2711 hostapd_wps_eap_completed(hapd);