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"
37 static void ieee802_1x_finished(struct hostapd_data *hapd,
38 struct sta_info *sta, int success,
42 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
43 u8 type, const u8 *data, size_t datalen)
46 struct ieee802_1x_hdr *xhdr;
50 len = sizeof(*xhdr) + datalen;
53 wpa_printf(MSG_ERROR, "malloc() failed for "
54 "ieee802_1x_send(len=%lu)",
59 xhdr = (struct ieee802_1x_hdr *) buf;
60 xhdr->version = hapd->conf->eapol_version;
62 xhdr->length = host_to_be16(datalen);
64 if (datalen > 0 && data != NULL)
65 os_memcpy(xhdr + 1, data, datalen);
67 if (wpa_auth_pairwise_set(sta->wpa_sm))
69 #ifdef CONFIG_TESTING_OPTIONS
70 if (hapd->ext_eapol_frame_io) {
71 size_t hex_len = 2 * len + 1;
72 char *hex = os_malloc(hex_len);
75 wpa_snprintf_hex(hex, hex_len, buf, len);
76 wpa_msg(hapd->msg_ctx, MSG_INFO,
77 "EAPOL-TX " MACSTR " %s",
78 MAC2STR(sta->addr), hex);
82 #endif /* CONFIG_TESTING_OPTIONS */
83 if (sta->flags & WLAN_STA_PREAUTH) {
84 rsn_preauth_send(hapd, sta, buf, len);
86 hostapd_drv_hapd_send_eapol(
87 hapd, sta->addr, buf, len,
88 encrypt, hostapd_sta_flags_to_drv(sta->flags));
95 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
96 struct sta_info *sta, int authorized)
100 if (sta->flags & WLAN_STA_PREAUTH)
104 ap_sta_set_authorized(hapd, sta, 1);
105 res = hostapd_set_authorized(hapd, sta, 1);
106 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
107 HOSTAPD_LEVEL_DEBUG, "authorizing port");
109 ap_sta_set_authorized(hapd, sta, 0);
110 res = hostapd_set_authorized(hapd, sta, 0);
111 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
112 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
115 if (res && errno != ENOENT) {
116 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
117 " flags for kernel driver (errno=%d).",
118 MAC2STR(sta->addr), errno);
122 os_get_reltime(&sta->connected_time);
123 accounting_sta_start(hapd, sta);
128 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
129 struct sta_info *sta,
130 int idx, int broadcast,
131 u8 *key_data, size_t key_len)
134 struct ieee802_1x_hdr *hdr;
135 struct ieee802_1x_eapol_key *key;
136 size_t len, ekey_len;
137 struct eapol_state_machine *sm = sta->eapol_sm;
142 len = sizeof(*key) + key_len;
143 buf = os_zalloc(sizeof(*hdr) + len);
147 hdr = (struct ieee802_1x_hdr *) buf;
148 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
149 key->type = EAPOL_KEY_TYPE_RC4;
150 WPA_PUT_BE16(key->key_length, key_len);
151 wpa_get_ntp_timestamp(key->replay_counter);
153 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
154 wpa_printf(MSG_ERROR, "Could not get random numbers");
159 key->key_index = idx | (broadcast ? 0 : BIT(7));
160 if (hapd->conf->eapol_key_index_workaround) {
161 /* According to some information, WinXP Supplicant seems to
162 * interpret bit7 as an indication whether the key is to be
163 * activated, so make it possible to enable workaround that
164 * sets this bit for all keys. */
165 key->key_index |= BIT(7);
168 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
169 * MSK[32..63] is used to sign the message. */
170 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
171 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
172 "and signing EAPOL-Key");
176 os_memcpy((u8 *) (key + 1), key_data, key_len);
177 ekey_len = sizeof(key->key_iv) + 32;
178 ekey = os_malloc(ekey_len);
180 wpa_printf(MSG_ERROR, "Could not encrypt key");
184 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
185 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
186 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
189 /* This header is needed here for HMAC-MD5, but it will be regenerated
190 * in ieee802_1x_send() */
191 hdr->version = hapd->conf->eapol_version;
192 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
193 hdr->length = host_to_be16(len);
194 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
197 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
198 " (%s index=%d)", MAC2STR(sm->addr),
199 broadcast ? "broadcast" : "unicast", idx);
200 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
202 sta->eapol_sm->dot1xAuthEapolFramesTx++;
207 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
209 struct eapol_authenticator *eapol = hapd->eapol_auth;
210 struct eapol_state_machine *sm = sta->eapol_sm;
212 if (sm == NULL || !sm->eap_if->eapKeyData)
215 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
218 #ifndef CONFIG_NO_VLAN
219 if (sta->vlan_id > 0 && sta->vlan_id <= MAX_VLAN_ID) {
220 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
223 #endif /* CONFIG_NO_VLAN */
225 if (eapol->default_wep_key) {
226 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
227 eapol->default_wep_key,
228 hapd->conf->default_wep_key_len);
231 if (hapd->conf->individual_wep_key_len > 0) {
233 ikey = os_malloc(hapd->conf->individual_wep_key_len);
235 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
237 wpa_printf(MSG_ERROR, "Could not generate random "
238 "individual WEP key.");
243 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
244 ikey, hapd->conf->individual_wep_key_len);
246 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
247 hapd->conf->individual_wep_key_len);
249 /* TODO: set encryption in TX callback, i.e., only after STA
250 * has ACKed EAPOL-Key frame */
251 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
252 sta->addr, 0, 1, NULL, 0, ikey,
253 hapd->conf->individual_wep_key_len)) {
254 wpa_printf(MSG_ERROR, "Could not set individual WEP "
263 const char *radius_mode_txt(struct hostapd_data *hapd)
265 switch (hapd->iface->conf->hw_mode) {
266 case HOSTAPD_MODE_IEEE80211AD:
268 case HOSTAPD_MODE_IEEE80211A:
270 case HOSTAPD_MODE_IEEE80211G:
272 case HOSTAPD_MODE_IEEE80211B:
279 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
284 for (i = 0; i < sta->supported_rates_len; i++)
285 if ((sta->supported_rates[i] & 0x7f) > rate)
286 rate = sta->supported_rates[i] & 0x7f;
292 #ifndef CONFIG_NO_RADIUS
293 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
294 struct eapol_state_machine *sm,
295 const u8 *eap, size_t len)
299 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
301 if (len <= sizeof(struct eap_hdr) ||
302 (hdr->code == EAP_CODE_RESPONSE &&
303 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
304 (hdr->code == EAP_CODE_INITIATE &&
305 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
306 (hdr->code != EAP_CODE_RESPONSE &&
307 hdr->code != EAP_CODE_INITIATE))
310 identity = eap_get_identity(sm->eap, &identity_len);
311 if (identity == NULL)
314 /* Save station identity for future RADIUS packets */
315 os_free(sm->identity);
316 sm->identity = (u8 *) dup_binstr(identity, identity_len);
317 if (sm->identity == NULL) {
318 sm->identity_len = 0;
322 sm->identity_len = identity_len;
323 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
324 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
325 sm->dot1xAuthEapolRespIdFramesRx++;
329 static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
330 struct hostapd_radius_attr *req_attr,
331 struct sta_info *sta,
332 struct radius_msg *msg)
337 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
338 val = wpa_auth_get_pairwise(sta->wpa_sm);
339 suite = wpa_cipher_to_suite(ver, val);
341 !hostapd_config_get_radius_attr(req_attr,
342 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
343 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
345 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
349 suite = wpa_cipher_to_suite((hapd->conf->wpa & 0x2) ?
350 WPA_PROTO_RSN : WPA_PROTO_WPA,
351 hapd->conf->wpa_group);
352 if (!hostapd_config_get_radius_attr(req_attr,
353 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
354 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
356 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
360 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
361 suite = wpa_akm_to_suite(val);
363 !hostapd_config_get_radius_attr(req_attr,
364 RADIUS_ATTR_WLAN_AKM_SUITE) &&
365 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
367 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
371 #ifdef CONFIG_IEEE80211W
372 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
373 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
374 hapd->conf->group_mgmt_cipher);
375 if (!hostapd_config_get_radius_attr(
376 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
377 !radius_msg_add_attr_int32(
378 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
379 wpa_printf(MSG_ERROR,
380 "Could not add WLAN-Group-Mgmt-Cipher");
384 #endif /* CONFIG_IEEE80211W */
390 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
391 struct hostapd_radius_attr *req_attr,
392 struct sta_info *sta,
393 struct radius_msg *msg)
397 if (!hostapd_config_get_radius_attr(req_attr,
398 RADIUS_ATTR_NAS_PORT) &&
399 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
400 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
404 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
406 buf[sizeof(buf) - 1] = '\0';
407 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
408 (u8 *) buf, os_strlen(buf))) {
409 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
413 if (sta->flags & WLAN_STA_PREAUTH) {
414 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
417 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
418 radius_sta_rate(hapd, sta) / 2,
419 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
420 radius_mode_txt(hapd));
421 buf[sizeof(buf) - 1] = '\0';
423 if (!hostapd_config_get_radius_attr(req_attr,
424 RADIUS_ATTR_CONNECT_INFO) &&
425 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
426 (u8 *) buf, os_strlen(buf))) {
427 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
431 if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
432 os_snprintf(buf, sizeof(buf), "%08X-%08X",
433 sta->acct_session_id_hi, sta->acct_session_id_lo);
434 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
435 (u8 *) buf, os_strlen(buf))) {
436 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
441 #ifdef CONFIG_IEEE80211R
442 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
444 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
445 sta->auth_alg == WLAN_AUTH_FT) &&
446 !hostapd_config_get_radius_attr(req_attr,
447 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
448 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
450 hapd->conf->mobility_domain))) {
451 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
454 #endif /* CONFIG_IEEE80211R */
456 if (hapd->conf->wpa && sta->wpa_sm &&
457 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
464 int add_common_radius_attr(struct hostapd_data *hapd,
465 struct hostapd_radius_attr *req_attr,
466 struct sta_info *sta,
467 struct radius_msg *msg)
470 struct hostapd_radius_attr *attr;
472 if (!hostapd_config_get_radius_attr(req_attr,
473 RADIUS_ATTR_NAS_IP_ADDRESS) &&
474 hapd->conf->own_ip_addr.af == AF_INET &&
475 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
476 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
477 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
482 if (!hostapd_config_get_radius_attr(req_attr,
483 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
484 hapd->conf->own_ip_addr.af == AF_INET6 &&
485 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
486 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
487 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
490 #endif /* CONFIG_IPV6 */
492 if (!hostapd_config_get_radius_attr(req_attr,
493 RADIUS_ATTR_NAS_IDENTIFIER) &&
494 hapd->conf->nas_identifier &&
495 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
496 (u8 *) hapd->conf->nas_identifier,
497 os_strlen(hapd->conf->nas_identifier))) {
498 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
502 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
503 MAC2STR(hapd->own_addr),
504 wpa_ssid_txt(hapd->conf->ssid.ssid,
505 hapd->conf->ssid.ssid_len));
506 buf[sizeof(buf) - 1] = '\0';
507 if (!hostapd_config_get_radius_attr(req_attr,
508 RADIUS_ATTR_CALLED_STATION_ID) &&
509 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
510 (u8 *) buf, os_strlen(buf))) {
511 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
515 if (!hostapd_config_get_radius_attr(req_attr,
516 RADIUS_ATTR_NAS_PORT_TYPE) &&
517 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
518 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
519 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
523 #ifdef CONFIG_INTERWORKING
524 if (hapd->conf->interworking &&
525 !is_zero_ether_addr(hapd->conf->hessid)) {
526 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
527 MAC2STR(hapd->conf->hessid));
528 buf[sizeof(buf) - 1] = '\0';
529 if (!hostapd_config_get_radius_attr(req_attr,
530 RADIUS_ATTR_WLAN_HESSID) &&
531 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
532 (u8 *) buf, os_strlen(buf))) {
533 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
537 #endif /* CONFIG_INTERWORKING */
539 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
542 for (attr = req_attr; attr; attr = attr->next) {
543 if (!radius_msg_add_attr(msg, attr->type,
544 wpabuf_head(attr->val),
545 wpabuf_len(attr->val))) {
546 wpa_printf(MSG_ERROR, "Could not add RADIUS "
556 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
557 struct sta_info *sta,
558 const u8 *eap, size_t len)
560 struct radius_msg *msg;
561 struct eapol_state_machine *sm = sta->eapol_sm;
566 ieee802_1x_learn_identity(hapd, sm, eap, len);
568 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
571 sm->radius_identifier = radius_client_get_id(hapd->radius);
572 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
573 sm->radius_identifier);
575 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
579 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
582 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
583 sm->identity, sm->identity_len)) {
584 wpa_printf(MSG_INFO, "Could not add User-Name");
588 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
592 /* TODO: should probably check MTU from driver config; 2304 is max for
593 * IEEE 802.11, but use 1400 to avoid problems with too large packets
595 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
596 RADIUS_ATTR_FRAMED_MTU) &&
597 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
598 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
602 if (eap && !radius_msg_add_eap(msg, eap, len)) {
603 wpa_printf(MSG_INFO, "Could not add EAP-Message");
607 /* State attribute must be copied if and only if this packet is
608 * Access-Request reply to the previous Access-Challenge */
609 if (sm->last_recv_radius &&
610 radius_msg_get_hdr(sm->last_recv_radius)->code ==
611 RADIUS_CODE_ACCESS_CHALLENGE) {
612 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
615 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
619 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
623 if (hapd->conf->radius_request_cui) {
626 /* Add previously learned CUI or nul CUI to request CUI */
627 if (sm->radius_cui) {
628 cui = wpabuf_head(sm->radius_cui);
629 cui_len = wpabuf_len(sm->radius_cui);
631 cui = (const u8 *) "\0";
634 if (!radius_msg_add_attr(msg,
635 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
637 wpa_printf(MSG_ERROR, "Could not add CUI");
643 if (hapd->conf->hs20) {
644 u8 ver = 1; /* Release 2 */
645 if (!radius_msg_add_wfa(
646 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
648 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
653 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
657 pos = wpabuf_head_u8(sta->hs20_ie);
658 buf[0] = (*pos) >> 4;
659 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
660 wpabuf_len(sta->hs20_ie) >= 3)
661 id = WPA_GET_LE16(pos + 1);
664 WPA_PUT_BE16(buf + 1, id);
665 if (!radius_msg_add_wfa(
667 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
669 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
675 #endif /* CONFIG_HS20 */
677 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
683 radius_msg_free(msg);
685 #endif /* CONFIG_NO_RADIUS */
688 static void handle_eap_response(struct hostapd_data *hapd,
689 struct sta_info *sta, struct eap_hdr *eap,
693 struct eapol_state_machine *sm = sta->eapol_sm;
697 data = (u8 *) (eap + 1);
699 if (len < sizeof(*eap) + 1) {
700 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
704 sm->eap_type_supp = type = data[0];
706 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
707 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
708 "id=%d len=%d) from STA: EAP Response-%s (%d)",
709 eap->code, eap->identifier, be_to_host16(eap->length),
710 eap_server_get_name(0, type), type);
712 sm->dot1xAuthEapolRespFramesRx++;
714 wpabuf_free(sm->eap_if->eapRespData);
715 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
720 static void handle_eap_initiate(struct hostapd_data *hapd,
721 struct sta_info *sta, struct eap_hdr *eap,
726 struct eapol_state_machine *sm = sta->eapol_sm;
731 if (len < sizeof(*eap) + 1) {
733 "handle_eap_initiate: too short response data");
737 data = (u8 *) (eap + 1);
740 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
741 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
742 "id=%d len=%d) from STA: EAP Initiate type %u",
743 eap->code, eap->identifier, be_to_host16(eap->length),
746 wpabuf_free(sm->eap_if->eapRespData);
747 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
749 #endif /* CONFIG_ERP */
753 /* Process incoming EAP packet from Supplicant */
754 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
760 if (len < sizeof(*eap)) {
761 wpa_printf(MSG_INFO, " too short EAP packet");
765 eap = (struct eap_hdr *) buf;
767 eap_len = be_to_host16(eap->length);
768 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
769 eap->code, eap->identifier, eap_len);
770 if (eap_len < sizeof(*eap)) {
771 wpa_printf(MSG_DEBUG, " Invalid EAP length");
773 } else if (eap_len > len) {
774 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
777 } else if (eap_len < len) {
778 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
779 "packet", (unsigned long) len - eap_len);
783 case EAP_CODE_REQUEST:
784 wpa_printf(MSG_DEBUG, " (request)");
786 case EAP_CODE_RESPONSE:
787 wpa_printf(MSG_DEBUG, " (response)");
788 handle_eap_response(hapd, sta, eap, eap_len);
790 case EAP_CODE_SUCCESS:
791 wpa_printf(MSG_DEBUG, " (success)");
793 case EAP_CODE_FAILURE:
794 wpa_printf(MSG_DEBUG, " (failure)");
796 case EAP_CODE_INITIATE:
797 wpa_printf(MSG_DEBUG, " (initiate)");
798 handle_eap_initiate(hapd, sta, eap, eap_len);
800 case EAP_CODE_FINISH:
801 wpa_printf(MSG_DEBUG, " (finish)");
804 wpa_printf(MSG_DEBUG, " (unknown code)");
810 static struct eapol_state_machine *
811 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
814 if (sta->flags & WLAN_STA_PREAUTH)
815 flags |= EAPOL_SM_PREAUTH;
817 flags |= EAPOL_SM_USES_WPA;
818 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
819 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
821 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
822 sta->wps_ie, sta->p2p_ie, sta,
823 sta->identity, sta->radius_cui);
828 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
829 * @hapd: hostapd BSS data
830 * @sa: Source address (sender of the EAPOL frame)
832 * @len: Length of buf in octets
834 * This function is called for each incoming EAPOL frame from the interface
836 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
839 struct sta_info *sta;
840 struct ieee802_1x_hdr *hdr;
841 struct ieee802_1x_eapol_key *key;
843 struct rsn_pmksa_cache_entry *pmksa;
846 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
847 !hapd->conf->wps_state)
850 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
851 (unsigned long) len, MAC2STR(sa));
852 sta = ap_get_sta(hapd, sa);
853 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
854 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
855 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
856 "associated/Pre-authenticating STA");
860 if (len < sizeof(*hdr)) {
861 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
865 hdr = (struct ieee802_1x_hdr *) buf;
866 datalen = be_to_host16(hdr->length);
867 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
868 hdr->version, hdr->type, datalen);
870 if (len - sizeof(*hdr) < datalen) {
871 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
873 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
876 if (len - sizeof(*hdr) > datalen) {
877 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
878 "IEEE 802.1X packet",
879 (unsigned long) len - sizeof(*hdr) - datalen);
883 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
884 sta->eapol_sm->dot1xAuthEapolFramesRx++;
887 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
888 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
889 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
890 (key->type == EAPOL_KEY_TYPE_WPA ||
891 key->type == EAPOL_KEY_TYPE_RSN)) {
892 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
893 sizeof(*hdr) + datalen);
897 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
898 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
899 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
900 "802.1X not enabled and WPS not used");
904 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
905 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
906 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
911 if (!sta->eapol_sm) {
912 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
917 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
918 u32 wflags = sta->flags & (WLAN_STA_WPS |
921 if (wflags == WLAN_STA_MAYBE_WPS ||
922 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
924 * Delay EAPOL frame transmission until a
925 * possible WPS STA initiates the handshake
926 * with EAPOL-Start. Only allow the wait to be
927 * skipped if the STA is known to support WPS
930 wpa_printf(MSG_DEBUG, "WPS: Do not start "
931 "EAPOL until EAPOL-Start is "
933 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
936 #endif /* CONFIG_WPS */
938 sta->eapol_sm->eap_if->portEnabled = TRUE;
941 /* since we support version 1, we can ignore version field and proceed
942 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
943 /* TODO: actually, we are not version 1 anymore.. However, Version 2
944 * does not change frame contents, so should be ok to process frames
945 * more or less identically. Some changes might be needed for
946 * verification of fields. */
949 case IEEE802_1X_TYPE_EAP_PACKET:
950 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
953 case IEEE802_1X_TYPE_EAPOL_START:
954 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
955 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
957 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
958 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
960 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
961 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
962 "available - ignore it since "
963 "STA sent EAPOL-Start");
964 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
966 sta->eapol_sm->eapolStart = TRUE;
967 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
968 eap_server_clear_identity(sta->eapol_sm->eap);
969 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
972 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
973 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
974 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
976 sta->acct_terminate_cause =
977 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
978 accounting_sta_stop(hapd, sta);
979 sta->eapol_sm->eapolLogoff = TRUE;
980 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
981 eap_server_clear_identity(sta->eapol_sm->eap);
984 case IEEE802_1X_TYPE_EAPOL_KEY:
985 wpa_printf(MSG_DEBUG, " EAPOL-Key");
986 if (!ap_sta_is_authorized(sta)) {
987 wpa_printf(MSG_DEBUG, " Dropped key data from "
988 "unauthorized Supplicant");
993 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
994 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
995 /* TODO: implement support for this; show data */
999 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1000 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1004 eapol_auth_step(sta->eapol_sm);
1009 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1010 * @hapd: hostapd BSS data
1013 * This function is called to start IEEE 802.1X authentication when a new
1014 * station completes IEEE 802.11 association.
1016 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1018 struct rsn_pmksa_cache_entry *pmksa;
1024 if (hapd->conf->wps_state &&
1025 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1026 (sta->flags & WLAN_STA_WPS))) {
1028 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1029 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1030 * authentication in this BSS.
1034 #endif /* CONFIG_WPS */
1036 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1037 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1038 "802.1X not enabled or forced for WPS");
1040 * Clear any possible EAPOL authenticator state to support
1041 * reassociation change from WPS to PSK.
1043 ieee802_1x_free_station(sta);
1047 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1048 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1049 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1051 * Clear any possible EAPOL authenticator state to support
1052 * reassociation change from WPA-EAP to PSK.
1054 ieee802_1x_free_station(sta);
1058 if (sta->eapol_sm == NULL) {
1059 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1060 HOSTAPD_LEVEL_DEBUG, "start authentication");
1061 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1062 if (sta->eapol_sm == NULL) {
1063 hostapd_logger(hapd, sta->addr,
1064 HOSTAPD_MODULE_IEEE8021X,
1066 "failed to allocate state machine");
1073 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1074 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1075 !(sta->flags & WLAN_STA_WPS2)) {
1077 * Delay EAPOL frame transmission until a possible WPS STA
1078 * initiates the handshake with EAPOL-Start. Only allow the
1079 * wait to be skipped if the STA is known to support WPS 2.0.
1081 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1082 "EAPOL-Start is received");
1083 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1085 #endif /* CONFIG_WPS */
1087 sta->eapol_sm->eap_if->portEnabled = TRUE;
1089 #ifdef CONFIG_IEEE80211R
1090 if (sta->auth_alg == WLAN_AUTH_FT) {
1091 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1092 HOSTAPD_LEVEL_DEBUG,
1093 "PMK from FT - skip IEEE 802.1X/EAP");
1094 /* Setup EAPOL state machines to already authenticated state
1095 * because of existing FT information from R0KH. */
1096 sta->eapol_sm->keyRun = TRUE;
1097 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1098 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1099 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1100 sta->eapol_sm->authSuccess = TRUE;
1101 sta->eapol_sm->authFail = FALSE;
1102 if (sta->eapol_sm->eap)
1103 eap_sm_notify_cached(sta->eapol_sm->eap);
1104 /* TODO: get vlan_id from R0KH using RRB message */
1107 #endif /* CONFIG_IEEE80211R */
1109 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1113 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1114 HOSTAPD_LEVEL_DEBUG,
1115 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1116 /* Setup EAPOL state machines to already authenticated state
1117 * because of existing PMKSA information in the cache. */
1118 sta->eapol_sm->keyRun = TRUE;
1119 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1120 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1121 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1122 sta->eapol_sm->authSuccess = TRUE;
1123 sta->eapol_sm->authFail = FALSE;
1124 if (sta->eapol_sm->eap)
1125 eap_sm_notify_cached(sta->eapol_sm->eap);
1126 old_vlanid = sta->vlan_id;
1127 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
1128 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1130 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1134 * Force EAPOL state machines to start
1135 * re-authentication without having to wait for the
1136 * Supplicant to send EAPOL-Start.
1138 sta->eapol_sm->reAuthenticate = TRUE;
1140 eapol_auth_step(sta->eapol_sm);
1145 void ieee802_1x_free_station(struct sta_info *sta)
1147 struct eapol_state_machine *sm = sta->eapol_sm;
1152 sta->eapol_sm = NULL;
1154 #ifndef CONFIG_NO_RADIUS
1155 radius_msg_free(sm->last_recv_radius);
1156 radius_free_class(&sm->radius_class);
1157 wpabuf_free(sm->radius_cui);
1158 #endif /* CONFIG_NO_RADIUS */
1160 os_free(sm->identity);
1161 eapol_auth_free(sm);
1165 #ifndef CONFIG_NO_RADIUS
1166 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1167 struct sta_info *sta)
1170 const struct eap_hdr *hdr;
1173 struct radius_msg *msg;
1174 struct eapol_state_machine *sm = sta->eapol_sm;
1176 if (sm == NULL || sm->last_recv_radius == NULL) {
1178 sm->eap_if->aaaEapNoReq = TRUE;
1182 msg = sm->last_recv_radius;
1184 eap = radius_msg_get_eap(msg);
1186 /* RFC 3579, Chap. 2.6.3:
1187 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1189 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1190 HOSTAPD_LEVEL_WARNING, "could not extract "
1191 "EAP-Message from RADIUS message");
1192 sm->eap_if->aaaEapNoReq = TRUE;
1196 if (wpabuf_len(eap) < sizeof(*hdr)) {
1197 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1198 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1199 "received from authentication server");
1201 sm->eap_if->aaaEapNoReq = TRUE;
1205 if (wpabuf_len(eap) > sizeof(*hdr))
1206 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1208 hdr = wpabuf_head(eap);
1209 switch (hdr->code) {
1210 case EAP_CODE_REQUEST:
1212 sm->eap_type_authsrv = eap_type;
1213 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1214 eap_server_get_name(0, eap_type), eap_type);
1216 case EAP_CODE_RESPONSE:
1217 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1218 eap_server_get_name(0, eap_type), eap_type);
1220 case EAP_CODE_SUCCESS:
1221 os_strlcpy(buf, "EAP Success", sizeof(buf));
1223 case EAP_CODE_FAILURE:
1224 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1227 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1230 buf[sizeof(buf) - 1] = '\0';
1231 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1232 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1233 "id=%d len=%d) from RADIUS server: %s",
1234 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1236 sm->eap_if->aaaEapReq = TRUE;
1238 wpabuf_free(sm->eap_if->aaaEapReqData);
1239 sm->eap_if->aaaEapReqData = eap;
1243 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1244 struct sta_info *sta, struct radius_msg *msg,
1245 struct radius_msg *req,
1246 const u8 *shared_secret,
1247 size_t shared_secret_len)
1249 struct radius_ms_mppe_keys *keys;
1250 struct eapol_state_machine *sm = sta->eapol_sm;
1254 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1257 if (keys && keys->send && keys->recv) {
1258 size_t len = keys->send_len + keys->recv_len;
1259 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1260 keys->send, keys->send_len);
1261 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1262 keys->recv, keys->recv_len);
1264 os_free(sm->eap_if->aaaEapKeyData);
1265 sm->eap_if->aaaEapKeyData = os_malloc(len);
1266 if (sm->eap_if->aaaEapKeyData) {
1267 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1269 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1270 keys->send, keys->send_len);
1271 sm->eap_if->aaaEapKeyDataLen = len;
1272 sm->eap_if->aaaEapKeyAvailable = TRUE;
1275 wpa_printf(MSG_DEBUG,
1276 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1277 keys, keys ? keys->send : NULL,
1278 keys ? keys->recv : NULL);
1282 os_free(keys->send);
1283 os_free(keys->recv);
1289 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1290 struct sta_info *sta,
1291 struct radius_msg *msg)
1295 struct eapol_state_machine *sm = sta->eapol_sm;
1297 struct radius_attr_data *nclass;
1298 size_t nclass_count;
1300 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1304 radius_free_class(&sm->radius_class);
1305 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1309 nclass = os_calloc(count, sizeof(struct radius_attr_data));
1316 for (i = 0; i < count; i++) {
1318 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1324 } while (class_len < 1);
1326 nclass[nclass_count].data = os_malloc(class_len);
1327 if (nclass[nclass_count].data == NULL)
1330 os_memcpy(nclass[nclass_count].data, class, class_len);
1331 nclass[nclass_count].len = class_len;
1335 sm->radius_class.attr = nclass;
1336 sm->radius_class.count = nclass_count;
1337 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1338 "attributes for " MACSTR,
1339 (unsigned long) sm->radius_class.count,
1340 MAC2STR(sta->addr));
1344 /* Update sta->identity based on User-Name attribute in Access-Accept */
1345 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1346 struct sta_info *sta,
1347 struct radius_msg *msg)
1351 struct eapol_state_machine *sm = sta->eapol_sm;
1356 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1360 identity = (u8 *) dup_binstr(buf, len);
1361 if (identity == NULL)
1364 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1365 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1366 "User-Name from Access-Accept '%s'",
1367 sm->identity ? (char *) sm->identity : "N/A",
1370 os_free(sm->identity);
1371 sm->identity = identity;
1372 sm->identity_len = len;
1376 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1377 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1378 struct sta_info *sta,
1379 struct radius_msg *msg)
1381 struct eapol_state_machine *sm = sta->eapol_sm;
1389 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1390 &buf, &len, NULL) < 0)
1393 cui = wpabuf_alloc_copy(buf, len);
1397 wpabuf_free(sm->radius_cui);
1398 sm->radius_cui = cui;
1404 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1406 sta->remediation = 1;
1407 os_free(sta->remediation_url);
1409 sta->remediation_url = os_malloc(len);
1410 if (!sta->remediation_url)
1412 sta->remediation_method = pos[0];
1413 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1414 sta->remediation_url[len - 1] = '\0';
1415 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1416 "for " MACSTR " - server method %u URL %s",
1417 MAC2STR(sta->addr), sta->remediation_method,
1418 sta->remediation_url);
1420 sta->remediation_url = NULL;
1421 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1422 "for " MACSTR, MAC2STR(sta->addr));
1424 /* TODO: assign the STA into remediation VLAN or add filtering */
1428 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1429 struct sta_info *sta, u8 *pos,
1433 return; /* Malformed information */
1434 sta->hs20_deauth_requested = 1;
1435 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1437 *pos, WPA_GET_LE16(pos + 1));
1438 wpabuf_free(sta->hs20_deauth_req);
1439 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1440 if (sta->hs20_deauth_req) {
1441 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1442 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1443 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1445 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1449 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1450 struct sta_info *sta, u8 *pos,
1451 size_t len, int session_timeout)
1454 int warning_time, beacon_int;
1457 return; /* Malformed information */
1458 os_free(sta->hs20_session_info_url);
1459 sta->hs20_session_info_url = os_malloc(len);
1460 if (sta->hs20_session_info_url == NULL)
1463 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1464 sta->hs20_session_info_url[len - 1] = '\0';
1465 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1466 "(session_timeout=%d)",
1467 sta->hs20_session_info_url, swt, session_timeout);
1468 if (session_timeout < 0) {
1469 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1473 swt = 1; /* Use one minute as the AP selected value */
1475 if ((unsigned int) session_timeout < swt * 60)
1478 warning_time = session_timeout - swt * 60;
1480 beacon_int = hapd->iconf->beacon_int;
1482 beacon_int = 100; /* best guess */
1483 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1484 if (sta->hs20_disassoc_timer > 65535)
1485 sta->hs20_disassoc_timer = 65535;
1487 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1490 #endif /* CONFIG_HS20 */
1493 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1494 struct sta_info *sta,
1495 struct radius_msg *msg,
1496 int session_timeout)
1499 u8 *buf, *pos, *end, type, sublen;
1503 sta->remediation = 0;
1504 sta->hs20_deauth_requested = 0;
1507 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1508 &buf, &len, buf) < 0)
1514 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1521 continue; /* invalid length */
1522 sublen -= 2; /* skip header */
1523 if (pos + sublen > end)
1524 continue; /* invalid WFA VSA */
1527 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1528 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1530 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1531 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1533 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1534 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1539 #endif /* CONFIG_HS20 */
1543 struct sta_id_search {
1545 struct eapol_state_machine *sm;
1549 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1550 struct sta_info *sta,
1553 struct sta_id_search *id_search = ctx;
1554 struct eapol_state_machine *sm = sta->eapol_sm;
1556 if (sm && sm->radius_identifier >= 0 &&
1557 sm->radius_identifier == id_search->identifier) {
1565 static struct eapol_state_machine *
1566 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1568 struct sta_id_search id_search;
1569 id_search.identifier = identifier;
1570 id_search.sm = NULL;
1571 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1572 return id_search.sm;
1577 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1578 * @msg: RADIUS response message
1579 * @req: RADIUS request message
1580 * @shared_secret: RADIUS shared secret
1581 * @shared_secret_len: Length of shared_secret in octets
1582 * @data: Context data (struct hostapd_data *)
1583 * Returns: Processing status
1585 static RadiusRxResult
1586 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1587 const u8 *shared_secret, size_t shared_secret_len,
1590 struct hostapd_data *hapd = data;
1591 struct sta_info *sta;
1592 u32 session_timeout = 0, termination_action, acct_interim_interval;
1593 int session_timeout_set, old_vlanid = 0;
1594 struct eapol_state_machine *sm;
1595 int override_eapReq = 0;
1596 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1598 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1600 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1601 "station for this RADIUS message");
1602 return RADIUS_RX_UNKNOWN;
1606 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1607 * present when packet contains an EAP-Message attribute */
1608 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1609 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1611 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1612 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1613 "Message-Authenticator since it does not include "
1615 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1617 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1618 return RADIUS_RX_INVALID_AUTHENTICATOR;
1621 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1622 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1623 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1624 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1625 return RADIUS_RX_UNKNOWN;
1628 sm->radius_identifier = -1;
1629 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1630 MAC2STR(sta->addr));
1632 radius_msg_free(sm->last_recv_radius);
1633 sm->last_recv_radius = msg;
1635 session_timeout_set =
1636 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1638 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1639 &termination_action))
1640 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1642 if (hapd->conf->acct_interim_interval == 0 &&
1643 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1644 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1645 &acct_interim_interval) == 0) {
1646 if (acct_interim_interval < 60) {
1647 hostapd_logger(hapd, sta->addr,
1648 HOSTAPD_MODULE_IEEE8021X,
1650 "ignored too small "
1651 "Acct-Interim-Interval %d",
1652 acct_interim_interval);
1654 sta->acct_interim_interval = acct_interim_interval;
1658 switch (hdr->code) {
1659 case RADIUS_CODE_ACCESS_ACCEPT:
1660 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1662 #ifndef CONFIG_NO_VLAN
1664 old_vlanid = sta->vlan_id;
1665 sta->vlan_id = radius_msg_get_vlanid(msg);
1667 if (sta->vlan_id > 0 &&
1668 hostapd_vlan_id_valid(hapd->conf->vlan, sta->vlan_id)) {
1669 hostapd_logger(hapd, sta->addr,
1670 HOSTAPD_MODULE_RADIUS,
1672 "VLAN ID %d", sta->vlan_id);
1673 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1674 sta->eapol_sm->authFail = TRUE;
1675 hostapd_logger(hapd, sta->addr,
1676 HOSTAPD_MODULE_IEEE8021X,
1677 HOSTAPD_LEVEL_INFO, "authentication "
1678 "server did not include required VLAN "
1679 "ID in Access-Accept");
1682 #endif /* CONFIG_NO_VLAN */
1684 if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
1687 sta->session_timeout_set = !!session_timeout_set;
1688 sta->session_timeout = session_timeout;
1690 /* RFC 3580, Ch. 3.17 */
1691 if (session_timeout_set && termination_action ==
1692 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1693 sm->reAuthPeriod = session_timeout;
1694 } else if (session_timeout_set)
1695 ap_sta_session_timeout(hapd, sta, session_timeout);
1697 sm->eap_if->aaaSuccess = TRUE;
1698 override_eapReq = 1;
1699 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1701 ieee802_1x_store_radius_class(hapd, sta, msg);
1702 ieee802_1x_update_sta_identity(hapd, sta, msg);
1703 ieee802_1x_update_sta_cui(hapd, sta, msg);
1704 ieee802_1x_check_hs20(hapd, sta, msg,
1705 session_timeout_set ?
1706 (int) session_timeout : -1);
1707 if (sm->eap_if->eapKeyAvailable && !sta->remediation &&
1708 !sta->hs20_deauth_requested &&
1709 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1710 session_timeout_set ?
1711 (int) session_timeout : -1, sm) == 0) {
1712 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1713 HOSTAPD_LEVEL_DEBUG,
1714 "Added PMKSA cache entry");
1717 case RADIUS_CODE_ACCESS_REJECT:
1718 sm->eap_if->aaaFail = TRUE;
1719 override_eapReq = 1;
1721 case RADIUS_CODE_ACCESS_CHALLENGE:
1722 sm->eap_if->aaaEapReq = TRUE;
1723 if (session_timeout_set) {
1724 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1725 sm->eap_if->aaaMethodTimeout = session_timeout;
1726 hostapd_logger(hapd, sm->addr,
1727 HOSTAPD_MODULE_IEEE8021X,
1728 HOSTAPD_LEVEL_DEBUG,
1729 "using EAP timeout of %d seconds (from "
1731 sm->eap_if->aaaMethodTimeout);
1734 * Use dynamic retransmission behavior per EAP
1737 sm->eap_if->aaaMethodTimeout = 0;
1742 ieee802_1x_decapsulate_radius(hapd, sta);
1743 if (override_eapReq)
1744 sm->eap_if->aaaEapReq = FALSE;
1746 eapol_auth_step(sm);
1748 return RADIUS_RX_QUEUED;
1750 #endif /* CONFIG_NO_RADIUS */
1753 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1755 struct eapol_state_machine *sm = sta->eapol_sm;
1759 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1760 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1762 #ifndef CONFIG_NO_RADIUS
1763 radius_msg_free(sm->last_recv_radius);
1764 sm->last_recv_radius = NULL;
1765 #endif /* CONFIG_NO_RADIUS */
1767 if (sm->eap_if->eapTimeout) {
1769 * Disconnect the STA since it did not reply to the last EAP
1770 * request and we cannot continue EAP processing (EAP-Failure
1771 * could only be sent if the EAP peer actually replied).
1773 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1774 MAC2STR(sta->addr));
1776 sm->eap_if->portEnabled = FALSE;
1777 ap_sta_disconnect(hapd, sta, sta->addr,
1778 WLAN_REASON_PREV_AUTH_NOT_VALID);
1783 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1785 struct eapol_authenticator *eapol = hapd->eapol_auth;
1787 if (hapd->conf->default_wep_key_len < 1)
1790 os_free(eapol->default_wep_key);
1791 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1792 if (eapol->default_wep_key == NULL ||
1793 random_get_bytes(eapol->default_wep_key,
1794 hapd->conf->default_wep_key_len)) {
1795 wpa_printf(MSG_INFO, "Could not generate random WEP key");
1796 os_free(eapol->default_wep_key);
1797 eapol->default_wep_key = NULL;
1801 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1802 eapol->default_wep_key,
1803 hapd->conf->default_wep_key_len);
1809 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1810 struct sta_info *sta, void *ctx)
1812 if (sta->eapol_sm) {
1813 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1814 eapol_auth_step(sta->eapol_sm);
1820 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1822 struct hostapd_data *hapd = eloop_ctx;
1823 struct eapol_authenticator *eapol = hapd->eapol_auth;
1825 if (eapol->default_wep_key_idx >= 3)
1826 eapol->default_wep_key_idx =
1827 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1829 eapol->default_wep_key_idx++;
1831 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1832 eapol->default_wep_key_idx);
1834 if (ieee802_1x_rekey_broadcast(hapd)) {
1835 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1836 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1837 "new broadcast key");
1838 os_free(eapol->default_wep_key);
1839 eapol->default_wep_key = NULL;
1843 /* TODO: Could setup key for RX here, but change default TX keyid only
1844 * after new broadcast key has been sent to all stations. */
1845 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1846 broadcast_ether_addr,
1847 eapol->default_wep_key_idx, 1, NULL, 0,
1848 eapol->default_wep_key,
1849 hapd->conf->default_wep_key_len)) {
1850 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1851 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1852 "new broadcast key");
1853 os_free(eapol->default_wep_key);
1854 eapol->default_wep_key = NULL;
1858 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1860 if (hapd->conf->wep_rekeying_period > 0) {
1861 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1862 ieee802_1x_rekey, hapd, NULL);
1867 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1868 const u8 *data, size_t datalen)
1871 struct sta_info *sta = sta_ctx;
1873 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1874 WLAN_STA_MAYBE_WPS) {
1876 size_t identity_len;
1877 struct eapol_state_machine *sm = sta->eapol_sm;
1879 identity = eap_get_identity(sm->eap, &identity_len);
1881 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1882 os_memcmp(identity, WSC_ID_ENROLLEE,
1883 WSC_ID_ENROLLEE_LEN) == 0) ||
1884 (identity_len == WSC_ID_REGISTRAR_LEN &&
1885 os_memcmp(identity, WSC_ID_REGISTRAR,
1886 WSC_ID_REGISTRAR_LEN) == 0))) {
1887 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1889 sta->flags |= WLAN_STA_WPS;
1892 #endif /* CONFIG_WPS */
1894 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1898 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1899 const u8 *data, size_t datalen)
1901 #ifndef CONFIG_NO_RADIUS
1902 struct hostapd_data *hapd = ctx;
1903 struct sta_info *sta = sta_ctx;
1905 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1906 #endif /* CONFIG_NO_RADIUS */
1910 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1911 int preauth, int remediation)
1913 struct hostapd_data *hapd = ctx;
1914 struct sta_info *sta = sta_ctx;
1916 rsn_preauth_finished(hapd, sta, success);
1918 ieee802_1x_finished(hapd, sta, success, remediation);
1922 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1923 size_t identity_len, int phase2,
1924 struct eap_user *user)
1926 struct hostapd_data *hapd = ctx;
1927 const struct hostapd_eap_user *eap_user;
1930 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
1931 if (eap_user == NULL)
1934 os_memset(user, 0, sizeof(*user));
1935 user->phase2 = phase2;
1936 for (i = 0; i < EAP_MAX_METHODS; i++) {
1937 user->methods[i].vendor = eap_user->methods[i].vendor;
1938 user->methods[i].method = eap_user->methods[i].method;
1941 if (eap_user->password) {
1942 user->password = os_malloc(eap_user->password_len);
1943 if (user->password == NULL)
1945 os_memcpy(user->password, eap_user->password,
1946 eap_user->password_len);
1947 user->password_len = eap_user->password_len;
1948 user->password_hash = eap_user->password_hash;
1950 user->force_version = eap_user->force_version;
1951 user->macacl = eap_user->macacl;
1952 user->ttls_auth = eap_user->ttls_auth;
1953 user->remediation = eap_user->remediation;
1959 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1961 struct hostapd_data *hapd = ctx;
1962 struct sta_info *sta;
1963 sta = ap_get_sta(hapd, addr);
1964 if (sta == NULL || sta->eapol_sm == NULL)
1970 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1971 eapol_logger_level level, const char *txt)
1973 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1974 struct hostapd_data *hapd = ctx;
1978 case EAPOL_LOGGER_WARNING:
1979 hlevel = HOSTAPD_LEVEL_WARNING;
1981 case EAPOL_LOGGER_INFO:
1982 hlevel = HOSTAPD_LEVEL_INFO;
1984 case EAPOL_LOGGER_DEBUG:
1986 hlevel = HOSTAPD_LEVEL_DEBUG;
1990 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1992 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1996 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1999 struct hostapd_data *hapd = ctx;
2000 struct sta_info *sta = sta_ctx;
2001 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2005 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2007 struct hostapd_data *hapd = ctx;
2008 struct sta_info *sta = sta_ctx;
2009 ieee802_1x_abort_auth(hapd, sta);
2013 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2015 struct hostapd_data *hapd = ctx;
2016 struct sta_info *sta = sta_ctx;
2017 ieee802_1x_tx_key(hapd, sta);
2021 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2022 enum eapol_event type)
2024 /* struct hostapd_data *hapd = ctx; */
2025 struct sta_info *sta = sta_ctx;
2027 case EAPOL_AUTH_SM_CHANGE:
2028 wpa_auth_sm_notify(sta->wpa_sm);
2030 case EAPOL_AUTH_REAUTHENTICATE:
2031 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2039 static struct eap_server_erp_key *
2040 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2042 struct hostapd_data *hapd = ctx;
2043 struct eap_server_erp_key *erp;
2045 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2047 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2055 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2057 struct hostapd_data *hapd = ctx;
2059 dl_list_add(&hapd->erp_keys, &erp->list);
2063 #endif /* CONFIG_ERP */
2066 int ieee802_1x_init(struct hostapd_data *hapd)
2069 struct eapol_auth_config conf;
2070 struct eapol_auth_cb cb;
2072 dl_list_init(&hapd->erp_keys);
2074 os_memset(&conf, 0, sizeof(conf));
2076 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2077 conf.wpa = hapd->conf->wpa;
2078 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2079 conf.eap_server = hapd->conf->eap_server;
2080 conf.ssl_ctx = hapd->ssl_ctx;
2081 conf.msg_ctx = hapd->msg_ctx;
2082 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2083 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2084 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2085 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2086 conf.erp_domain = hapd->conf->erp_domain;
2087 conf.erp = hapd->conf->eap_server_erp;
2088 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2089 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2090 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2091 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2092 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2093 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2094 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2095 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2096 conf.tnc = hapd->conf->tnc;
2097 conf.wps = hapd->wps;
2098 conf.fragment_size = hapd->conf->fragment_size;
2099 conf.pwd_group = hapd->conf->pwd_group;
2100 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2101 if (hapd->conf->server_id) {
2102 conf.server_id = (const u8 *) hapd->conf->server_id;
2103 conf.server_id_len = os_strlen(hapd->conf->server_id);
2105 conf.server_id = (const u8 *) "hostapd";
2106 conf.server_id_len = 7;
2109 os_memset(&cb, 0, sizeof(cb));
2110 cb.eapol_send = ieee802_1x_eapol_send;
2111 cb.aaa_send = ieee802_1x_aaa_send;
2112 cb.finished = _ieee802_1x_finished;
2113 cb.get_eap_user = ieee802_1x_get_eap_user;
2114 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2115 cb.logger = ieee802_1x_logger;
2116 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2117 cb.abort_auth = _ieee802_1x_abort_auth;
2118 cb.tx_key = _ieee802_1x_tx_key;
2119 cb.eapol_event = ieee802_1x_eapol_event;
2121 cb.erp_get_key = ieee802_1x_erp_get_key;
2122 cb.erp_add_key = ieee802_1x_erp_add_key;
2123 #endif /* CONFIG_ERP */
2125 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2126 if (hapd->eapol_auth == NULL)
2129 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2130 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2133 #ifndef CONFIG_NO_RADIUS
2134 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2135 ieee802_1x_receive_auth, hapd))
2137 #endif /* CONFIG_NO_RADIUS */
2139 if (hapd->conf->default_wep_key_len) {
2140 for (i = 0; i < 4; i++)
2141 hostapd_drv_set_key(hapd->conf->iface, hapd,
2142 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2145 ieee802_1x_rekey(hapd, NULL);
2147 if (hapd->eapol_auth->default_wep_key == NULL)
2155 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2157 struct eap_server_erp_key *erp;
2159 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2161 dl_list_del(&erp->list);
2162 bin_clear_free(erp, sizeof(*erp));
2167 void ieee802_1x_deinit(struct hostapd_data *hapd)
2169 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2171 if (hapd->driver != NULL &&
2172 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2173 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2175 eapol_auth_deinit(hapd->eapol_auth);
2176 hapd->eapol_auth = NULL;
2178 ieee802_1x_erp_flush(hapd);
2182 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2183 const u8 *buf, size_t len, int ack)
2185 struct ieee80211_hdr *hdr;
2187 const unsigned char rfc1042_hdr[ETH_ALEN] =
2188 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2192 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2195 hdr = (struct ieee80211_hdr *) buf;
2196 pos = (u8 *) (hdr + 1);
2197 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2199 pos += sizeof(rfc1042_hdr);
2200 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2204 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2209 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2210 const u8 *buf, int len, int ack)
2212 const struct ieee802_1x_hdr *xhdr =
2213 (const struct ieee802_1x_hdr *) buf;
2214 const u8 *pos = buf + sizeof(*xhdr);
2215 struct ieee802_1x_eapol_key *key;
2217 if (len < (int) sizeof(*xhdr))
2219 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2220 "type=%d length=%d - ack=%d",
2221 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2222 be_to_host16(xhdr->length), ack);
2224 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2227 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2228 const struct wpa_eapol_key *wpa;
2229 wpa = (const struct wpa_eapol_key *) pos;
2230 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2231 wpa->type == EAPOL_KEY_TYPE_WPA)
2232 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2236 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2237 * or Authenticator state machines, but EAPOL-Key packets are not
2238 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2239 * packets couple of times because otherwise STA keys become
2240 * unsynchronized with AP. */
2241 if (!ack && pos + sizeof(*key) <= buf + len) {
2242 key = (struct ieee802_1x_eapol_key *) pos;
2243 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2244 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2245 "frame (%scast index=%d)",
2246 key->key_index & BIT(7) ? "uni" : "broad",
2247 key->key_index & ~BIT(7));
2248 /* TODO: re-send EAPOL-Key couple of times (with short delay
2249 * between them?). If all attempt fail, report error and
2250 * deauthenticate STA so that it will get new keys when
2251 * authenticating again (e.g., after returning in range).
2252 * Separate limit/transmit state needed both for unicast and
2253 * broadcast keys(?) */
2255 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2256 * to here and change the key only if the EAPOL-Key packet was Acked.
2263 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2265 if (sm == NULL || sm->identity == NULL)
2268 *len = sm->identity_len;
2269 return sm->identity;
2273 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2276 if (sm == NULL || sm->radius_class.attr == NULL ||
2277 idx >= (int) sm->radius_class.count)
2280 *len = sm->radius_class.attr[idx].len;
2281 return sm->radius_class.attr[idx].data;
2285 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2289 return sm->radius_cui;
2293 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2299 *len = sm->eap_if->eapKeyDataLen;
2300 return sm->eap_if->eapKeyData;
2304 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2309 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2310 eapol_auth_step(sm);
2314 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2319 sm->portValid = valid ? TRUE : FALSE;
2320 eapol_auth_step(sm);
2324 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2329 sm->flags |= EAPOL_SM_PREAUTH;
2331 sm->flags &= ~EAPOL_SM_PREAUTH;
2335 static const char * bool_txt(Boolean bool)
2337 return bool ? "TRUE" : "FALSE";
2341 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2348 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2349 char *buf, size_t buflen)
2352 struct eapol_state_machine *sm = sta->eapol_sm;
2353 struct os_reltime diff;
2360 ret = os_snprintf(buf + len, buflen - len,
2361 "dot1xPaePortNumber=%d\n"
2362 "dot1xPaePortProtocolVersion=%d\n"
2363 "dot1xPaePortCapabilities=1\n"
2364 "dot1xPaePortInitialize=%d\n"
2365 "dot1xPaePortReauthenticate=FALSE\n",
2369 if (os_snprintf_error(buflen - len, ret))
2373 /* dot1xAuthConfigTable */
2374 ret = os_snprintf(buf + len, buflen - len,
2375 "dot1xAuthPaeState=%d\n"
2376 "dot1xAuthBackendAuthState=%d\n"
2377 "dot1xAuthAdminControlledDirections=%d\n"
2378 "dot1xAuthOperControlledDirections=%d\n"
2379 "dot1xAuthAuthControlledPortStatus=%d\n"
2380 "dot1xAuthAuthControlledPortControl=%d\n"
2381 "dot1xAuthQuietPeriod=%u\n"
2382 "dot1xAuthServerTimeout=%u\n"
2383 "dot1xAuthReAuthPeriod=%u\n"
2384 "dot1xAuthReAuthEnabled=%s\n"
2385 "dot1xAuthKeyTxEnabled=%s\n",
2386 sm->auth_pae_state + 1,
2387 sm->be_auth_state + 1,
2388 sm->adminControlledDirections,
2389 sm->operControlledDirections,
2395 bool_txt(sm->reAuthEnabled),
2396 bool_txt(sm->keyTxEnabled));
2397 if (os_snprintf_error(buflen - len, ret))
2401 /* dot1xAuthStatsTable */
2402 ret = os_snprintf(buf + len, buflen - len,
2403 "dot1xAuthEapolFramesRx=%u\n"
2404 "dot1xAuthEapolFramesTx=%u\n"
2405 "dot1xAuthEapolStartFramesRx=%u\n"
2406 "dot1xAuthEapolLogoffFramesRx=%u\n"
2407 "dot1xAuthEapolRespIdFramesRx=%u\n"
2408 "dot1xAuthEapolRespFramesRx=%u\n"
2409 "dot1xAuthEapolReqIdFramesTx=%u\n"
2410 "dot1xAuthEapolReqFramesTx=%u\n"
2411 "dot1xAuthInvalidEapolFramesRx=%u\n"
2412 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2413 "dot1xAuthLastEapolFrameVersion=%u\n"
2414 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2415 sm->dot1xAuthEapolFramesRx,
2416 sm->dot1xAuthEapolFramesTx,
2417 sm->dot1xAuthEapolStartFramesRx,
2418 sm->dot1xAuthEapolLogoffFramesRx,
2419 sm->dot1xAuthEapolRespIdFramesRx,
2420 sm->dot1xAuthEapolRespFramesRx,
2421 sm->dot1xAuthEapolReqIdFramesTx,
2422 sm->dot1xAuthEapolReqFramesTx,
2423 sm->dot1xAuthInvalidEapolFramesRx,
2424 sm->dot1xAuthEapLengthErrorFramesRx,
2425 sm->dot1xAuthLastEapolFrameVersion,
2427 if (os_snprintf_error(buflen - len, ret))
2431 /* dot1xAuthDiagTable */
2432 ret = os_snprintf(buf + len, buflen - len,
2433 "dot1xAuthEntersConnecting=%u\n"
2434 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2435 "dot1xAuthEntersAuthenticating=%u\n"
2436 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2437 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2438 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2439 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2440 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2441 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2442 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2443 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2444 "dot1xAuthBackendResponses=%u\n"
2445 "dot1xAuthBackendAccessChallenges=%u\n"
2446 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2447 "dot1xAuthBackendAuthSuccesses=%u\n"
2448 "dot1xAuthBackendAuthFails=%u\n",
2449 sm->authEntersConnecting,
2450 sm->authEapLogoffsWhileConnecting,
2451 sm->authEntersAuthenticating,
2452 sm->authAuthSuccessesWhileAuthenticating,
2453 sm->authAuthTimeoutsWhileAuthenticating,
2454 sm->authAuthFailWhileAuthenticating,
2455 sm->authAuthEapStartsWhileAuthenticating,
2456 sm->authAuthEapLogoffWhileAuthenticating,
2457 sm->authAuthReauthsWhileAuthenticated,
2458 sm->authAuthEapStartsWhileAuthenticated,
2459 sm->authAuthEapLogoffWhileAuthenticated,
2460 sm->backendResponses,
2461 sm->backendAccessChallenges,
2462 sm->backendOtherRequestsToSupplicant,
2463 sm->backendAuthSuccesses,
2464 sm->backendAuthFails);
2465 if (os_snprintf_error(buflen - len, ret))
2469 /* dot1xAuthSessionStatsTable */
2470 os_reltime_age(&sta->acct_session_start, &diff);
2471 ret = os_snprintf(buf + len, buflen - len,
2472 /* TODO: dot1xAuthSessionOctetsRx */
2473 /* TODO: dot1xAuthSessionOctetsTx */
2474 /* TODO: dot1xAuthSessionFramesRx */
2475 /* TODO: dot1xAuthSessionFramesTx */
2476 "dot1xAuthSessionId=%08X-%08X\n"
2477 "dot1xAuthSessionAuthenticMethod=%d\n"
2478 "dot1xAuthSessionTime=%u\n"
2479 "dot1xAuthSessionTerminateCause=999\n"
2480 "dot1xAuthSessionUserName=%s\n",
2481 sta->acct_session_id_hi, sta->acct_session_id_lo,
2482 (wpa_key_mgmt_wpa_ieee8021x(
2483 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2485 (unsigned int) diff.sec,
2487 if (os_snprintf_error(buflen - len, ret))
2491 if (sm->acct_multi_session_id_hi) {
2492 ret = os_snprintf(buf + len, buflen - len,
2493 "authMultiSessionId=%08X+%08X\n",
2494 sm->acct_multi_session_id_hi,
2495 sm->acct_multi_session_id_lo);
2496 if (os_snprintf_error(buflen - len, ret))
2501 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2502 name2 = eap_server_get_name(0, sm->eap_type_supp);
2503 ret = os_snprintf(buf + len, buflen - len,
2504 "last_eap_type_as=%d (%s)\n"
2505 "last_eap_type_sta=%d (%s)\n",
2506 sm->eap_type_authsrv, name1,
2507 sm->eap_type_supp, name2);
2508 if (os_snprintf_error(buflen - len, ret))
2516 static void ieee802_1x_finished(struct hostapd_data *hapd,
2517 struct sta_info *sta, int success,
2522 /* TODO: get PMKLifetime from WPA parameters */
2523 static const int dot11RSNAConfigPMKLifetime = 43200;
2524 unsigned int session_timeout;
2527 if (remediation && !sta->remediation) {
2528 sta->remediation = 1;
2529 os_free(sta->remediation_url);
2530 sta->remediation_url =
2531 os_strdup(hapd->conf->subscr_remediation_url);
2532 sta->remediation_method = 1; /* SOAP-XML SPP */
2536 if (sta->remediation) {
2537 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2538 "to " MACSTR " to indicate Subscription "
2540 MAC2STR(sta->addr));
2541 hs20_send_wnm_notification(hapd, sta->addr,
2542 sta->remediation_method,
2543 sta->remediation_url);
2544 os_free(sta->remediation_url);
2545 sta->remediation_url = NULL;
2548 if (sta->hs20_deauth_req) {
2549 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2550 "to " MACSTR " to indicate imminent "
2551 "deauthentication", MAC2STR(sta->addr));
2552 hs20_send_wnm_notification_deauth_req(
2553 hapd, sta->addr, sta->hs20_deauth_req);
2556 #endif /* CONFIG_HS20 */
2558 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2559 if (sta->session_timeout_set)
2560 session_timeout = sta->session_timeout;
2562 session_timeout = dot11RSNAConfigPMKLifetime;
2563 if (success && key && len >= PMK_LEN && !sta->remediation &&
2564 !sta->hs20_deauth_requested &&
2565 wpa_auth_pmksa_add(sta->wpa_sm, key, session_timeout,
2566 sta->eapol_sm) == 0) {
2567 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2568 HOSTAPD_LEVEL_DEBUG,
2569 "Added PMKSA cache entry (IEEE 802.1X)");
2574 * Many devices require deauthentication after WPS provisioning
2575 * and some may not be be able to do that themselves, so
2576 * disconnect the client here. In addition, this may also
2577 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2578 * the EAPOL PAE state machine would remain in HELD state for
2579 * considerable amount of time and some EAP methods, like
2580 * EAP-FAST with anonymous provisioning, may require another
2581 * EAPOL authentication to be started to complete connection.
2583 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2584 "disconnection after EAP-Failure");
2585 /* Add a small sleep to increase likelihood of previously
2586 * requested EAP-Failure TX getting out before this should the
2587 * driver reorder operations.
2590 ap_sta_disconnect(hapd, sta, sta->addr,
2591 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
2592 hostapd_wps_eap_completed(hapd);