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);
129 #ifndef CONFIG_NO_RC4
131 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
132 struct sta_info *sta,
133 int idx, int broadcast,
134 u8 *key_data, size_t key_len)
137 struct ieee802_1x_hdr *hdr;
138 struct ieee802_1x_eapol_key *key;
139 size_t len, ekey_len;
140 struct eapol_state_machine *sm = sta->eapol_sm;
145 len = sizeof(*key) + key_len;
146 buf = os_zalloc(sizeof(*hdr) + len);
150 hdr = (struct ieee802_1x_hdr *) buf;
151 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
152 key->type = EAPOL_KEY_TYPE_RC4;
153 WPA_PUT_BE16(key->key_length, key_len);
154 wpa_get_ntp_timestamp(key->replay_counter);
156 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
157 wpa_printf(MSG_ERROR, "Could not get random numbers");
162 key->key_index = idx | (broadcast ? 0 : BIT(7));
163 if (hapd->conf->eapol_key_index_workaround) {
164 /* According to some information, WinXP Supplicant seems to
165 * interpret bit7 as an indication whether the key is to be
166 * activated, so make it possible to enable workaround that
167 * sets this bit for all keys. */
168 key->key_index |= BIT(7);
171 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
172 * MSK[32..63] is used to sign the message. */
173 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
174 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
175 "and signing EAPOL-Key");
179 os_memcpy((u8 *) (key + 1), key_data, key_len);
180 ekey_len = sizeof(key->key_iv) + 32;
181 ekey = os_malloc(ekey_len);
183 wpa_printf(MSG_ERROR, "Could not encrypt key");
187 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
188 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
189 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
192 /* This header is needed here for HMAC-MD5, but it will be regenerated
193 * in ieee802_1x_send() */
194 hdr->version = hapd->conf->eapol_version;
195 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
196 hdr->length = host_to_be16(len);
197 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
200 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
201 " (%s index=%d)", MAC2STR(sm->addr),
202 broadcast ? "broadcast" : "unicast", idx);
203 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
205 sta->eapol_sm->dot1xAuthEapolFramesTx++;
210 static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
212 struct eapol_authenticator *eapol = hapd->eapol_auth;
213 struct eapol_state_machine *sm = sta->eapol_sm;
215 if (sm == NULL || !sm->eap_if->eapKeyData)
218 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
221 #ifndef CONFIG_NO_VLAN
222 if (sta->vlan_id > 0 && sta->vlan_id <= MAX_VLAN_ID) {
223 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
226 #endif /* CONFIG_NO_VLAN */
228 if (eapol->default_wep_key) {
229 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
230 eapol->default_wep_key,
231 hapd->conf->default_wep_key_len);
234 if (hapd->conf->individual_wep_key_len > 0) {
236 ikey = os_malloc(hapd->conf->individual_wep_key_len);
238 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
240 wpa_printf(MSG_ERROR, "Could not generate random "
241 "individual WEP key.");
246 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
247 ikey, hapd->conf->individual_wep_key_len);
249 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
250 hapd->conf->individual_wep_key_len);
252 /* TODO: set encryption in TX callback, i.e., only after STA
253 * has ACKed EAPOL-Key frame */
254 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
255 sta->addr, 0, 1, NULL, 0, ikey,
256 hapd->conf->individual_wep_key_len)) {
257 wpa_printf(MSG_ERROR, "Could not set individual WEP "
265 #endif /* CONFIG_NO_RC4 */
266 #endif /* CONFIG_FIPS */
269 const char *radius_mode_txt(struct hostapd_data *hapd)
271 switch (hapd->iface->conf->hw_mode) {
272 case HOSTAPD_MODE_IEEE80211AD:
274 case HOSTAPD_MODE_IEEE80211A:
276 case HOSTAPD_MODE_IEEE80211G:
278 case HOSTAPD_MODE_IEEE80211B:
285 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
290 for (i = 0; i < sta->supported_rates_len; i++)
291 if ((sta->supported_rates[i] & 0x7f) > rate)
292 rate = sta->supported_rates[i] & 0x7f;
298 #ifndef CONFIG_NO_RADIUS
299 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
300 struct eapol_state_machine *sm,
301 const u8 *eap, size_t len)
305 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
307 if (len <= sizeof(struct eap_hdr) ||
308 (hdr->code == EAP_CODE_RESPONSE &&
309 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
310 (hdr->code == EAP_CODE_INITIATE &&
311 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
312 (hdr->code != EAP_CODE_RESPONSE &&
313 hdr->code != EAP_CODE_INITIATE))
316 identity = eap_get_identity(sm->eap, &identity_len);
317 if (identity == NULL)
320 /* Save station identity for future RADIUS packets */
321 os_free(sm->identity);
322 sm->identity = (u8 *) dup_binstr(identity, identity_len);
323 if (sm->identity == NULL) {
324 sm->identity_len = 0;
328 sm->identity_len = identity_len;
329 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
330 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
331 sm->dot1xAuthEapolRespIdFramesRx++;
335 static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
336 struct hostapd_radius_attr *req_attr,
337 struct sta_info *sta,
338 struct radius_msg *msg)
343 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
344 val = wpa_auth_get_pairwise(sta->wpa_sm);
345 suite = wpa_cipher_to_suite(ver, val);
347 !hostapd_config_get_radius_attr(req_attr,
348 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
349 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
351 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
355 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
357 WPA_PROTO_RSN : WPA_PROTO_WPA,
358 hapd->conf->wpa_group);
359 if (!hostapd_config_get_radius_attr(req_attr,
360 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
361 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
363 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
367 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
368 suite = wpa_akm_to_suite(val);
370 !hostapd_config_get_radius_attr(req_attr,
371 RADIUS_ATTR_WLAN_AKM_SUITE) &&
372 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
374 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
378 #ifdef CONFIG_IEEE80211W
379 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
380 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
381 hapd->conf->group_mgmt_cipher);
382 if (!hostapd_config_get_radius_attr(
383 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
384 !radius_msg_add_attr_int32(
385 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
386 wpa_printf(MSG_ERROR,
387 "Could not add WLAN-Group-Mgmt-Cipher");
391 #endif /* CONFIG_IEEE80211W */
397 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
398 struct hostapd_radius_attr *req_attr,
399 struct sta_info *sta,
400 struct radius_msg *msg)
404 if (!hostapd_config_get_radius_attr(req_attr,
405 RADIUS_ATTR_NAS_PORT) &&
406 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
407 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
411 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
413 buf[sizeof(buf) - 1] = '\0';
414 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
415 (u8 *) buf, os_strlen(buf))) {
416 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
420 if (sta->flags & WLAN_STA_PREAUTH) {
421 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
424 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
425 radius_sta_rate(hapd, sta) / 2,
426 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
427 radius_mode_txt(hapd));
428 buf[sizeof(buf) - 1] = '\0';
430 if (!hostapd_config_get_radius_attr(req_attr,
431 RADIUS_ATTR_CONNECT_INFO) &&
432 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
433 (u8 *) buf, os_strlen(buf))) {
434 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
438 if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
439 os_snprintf(buf, sizeof(buf), "%08X-%08X",
440 sta->acct_session_id_hi, sta->acct_session_id_lo);
441 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
442 (u8 *) buf, os_strlen(buf))) {
443 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
448 #ifdef CONFIG_IEEE80211R
449 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
451 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
452 sta->auth_alg == WLAN_AUTH_FT) &&
453 !hostapd_config_get_radius_attr(req_attr,
454 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
455 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
457 hapd->conf->mobility_domain))) {
458 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
461 #endif /* CONFIG_IEEE80211R */
463 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
464 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
471 int add_common_radius_attr(struct hostapd_data *hapd,
472 struct hostapd_radius_attr *req_attr,
473 struct sta_info *sta,
474 struct radius_msg *msg)
477 struct hostapd_radius_attr *attr;
479 if (!hostapd_config_get_radius_attr(req_attr,
480 RADIUS_ATTR_NAS_IP_ADDRESS) &&
481 hapd->conf->own_ip_addr.af == AF_INET &&
482 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
483 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
484 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
489 if (!hostapd_config_get_radius_attr(req_attr,
490 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
491 hapd->conf->own_ip_addr.af == AF_INET6 &&
492 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
493 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
494 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
497 #endif /* CONFIG_IPV6 */
499 if (!hostapd_config_get_radius_attr(req_attr,
500 RADIUS_ATTR_NAS_IDENTIFIER) &&
501 hapd->conf->nas_identifier &&
502 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
503 (u8 *) hapd->conf->nas_identifier,
504 os_strlen(hapd->conf->nas_identifier))) {
505 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
509 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
510 MAC2STR(hapd->own_addr),
511 wpa_ssid_txt(hapd->conf->ssid.ssid,
512 hapd->conf->ssid.ssid_len));
513 buf[sizeof(buf) - 1] = '\0';
514 if (!hostapd_config_get_radius_attr(req_attr,
515 RADIUS_ATTR_CALLED_STATION_ID) &&
516 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
517 (u8 *) buf, os_strlen(buf))) {
518 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
522 if (!hostapd_config_get_radius_attr(req_attr,
523 RADIUS_ATTR_NAS_PORT_TYPE) &&
524 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
525 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
526 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
530 #ifdef CONFIG_INTERWORKING
531 if (hapd->conf->interworking &&
532 !is_zero_ether_addr(hapd->conf->hessid)) {
533 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
534 MAC2STR(hapd->conf->hessid));
535 buf[sizeof(buf) - 1] = '\0';
536 if (!hostapd_config_get_radius_attr(req_attr,
537 RADIUS_ATTR_WLAN_HESSID) &&
538 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
539 (u8 *) buf, os_strlen(buf))) {
540 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
544 #endif /* CONFIG_INTERWORKING */
546 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
549 for (attr = req_attr; attr; attr = attr->next) {
550 if (!radius_msg_add_attr(msg, attr->type,
551 wpabuf_head(attr->val),
552 wpabuf_len(attr->val))) {
553 wpa_printf(MSG_ERROR, "Could not add RADIUS "
563 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
564 struct sta_info *sta,
565 const u8 *eap, size_t len)
567 struct radius_msg *msg;
568 struct eapol_state_machine *sm = sta->eapol_sm;
573 ieee802_1x_learn_identity(hapd, sm, eap, len);
575 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
578 sm->radius_identifier = radius_client_get_id(hapd->radius);
579 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
580 sm->radius_identifier);
582 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
586 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
589 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
590 sm->identity, sm->identity_len)) {
591 wpa_printf(MSG_INFO, "Could not add User-Name");
595 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
599 /* TODO: should probably check MTU from driver config; 2304 is max for
600 * IEEE 802.11, but use 1400 to avoid problems with too large packets
602 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
603 RADIUS_ATTR_FRAMED_MTU) &&
604 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
605 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
609 if (!radius_msg_add_eap(msg, eap, len)) {
610 wpa_printf(MSG_INFO, "Could not add EAP-Message");
614 /* State attribute must be copied if and only if this packet is
615 * Access-Request reply to the previous Access-Challenge */
616 if (sm->last_recv_radius &&
617 radius_msg_get_hdr(sm->last_recv_radius)->code ==
618 RADIUS_CODE_ACCESS_CHALLENGE) {
619 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
622 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
626 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
630 if (hapd->conf->radius_request_cui) {
633 /* Add previously learned CUI or nul CUI to request CUI */
634 if (sm->radius_cui) {
635 cui = wpabuf_head(sm->radius_cui);
636 cui_len = wpabuf_len(sm->radius_cui);
638 cui = (const u8 *) "\0";
641 if (!radius_msg_add_attr(msg,
642 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
644 wpa_printf(MSG_ERROR, "Could not add CUI");
650 if (hapd->conf->hs20) {
651 u8 ver = 1; /* Release 2 */
652 if (!radius_msg_add_wfa(
653 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
655 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
660 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
664 pos = wpabuf_head_u8(sta->hs20_ie);
665 buf[0] = (*pos) >> 4;
666 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
667 wpabuf_len(sta->hs20_ie) >= 3)
668 id = WPA_GET_LE16(pos + 1);
671 WPA_PUT_BE16(buf + 1, id);
672 if (!radius_msg_add_wfa(
674 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
676 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
682 #endif /* CONFIG_HS20 */
684 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
690 radius_msg_free(msg);
692 #endif /* CONFIG_NO_RADIUS */
695 static void handle_eap_response(struct hostapd_data *hapd,
696 struct sta_info *sta, struct eap_hdr *eap,
700 struct eapol_state_machine *sm = sta->eapol_sm;
704 data = (u8 *) (eap + 1);
706 if (len < sizeof(*eap) + 1) {
707 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
711 sm->eap_type_supp = type = data[0];
713 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
714 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
715 "id=%d len=%d) from STA: EAP Response-%s (%d)",
716 eap->code, eap->identifier, be_to_host16(eap->length),
717 eap_server_get_name(0, type), type);
719 sm->dot1xAuthEapolRespFramesRx++;
721 wpabuf_free(sm->eap_if->eapRespData);
722 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
727 static void handle_eap_initiate(struct hostapd_data *hapd,
728 struct sta_info *sta, struct eap_hdr *eap,
733 struct eapol_state_machine *sm = sta->eapol_sm;
738 if (len < sizeof(*eap) + 1) {
740 "handle_eap_initiate: too short response data");
744 data = (u8 *) (eap + 1);
747 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
748 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
749 "id=%d len=%d) from STA: EAP Initiate type %u",
750 eap->code, eap->identifier, be_to_host16(eap->length),
753 wpabuf_free(sm->eap_if->eapRespData);
754 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
756 #endif /* CONFIG_ERP */
760 /* Process incoming EAP packet from Supplicant */
761 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
767 if (len < sizeof(*eap)) {
768 wpa_printf(MSG_INFO, " too short EAP packet");
772 eap = (struct eap_hdr *) buf;
774 eap_len = be_to_host16(eap->length);
775 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
776 eap->code, eap->identifier, eap_len);
777 if (eap_len < sizeof(*eap)) {
778 wpa_printf(MSG_DEBUG, " Invalid EAP length");
780 } else if (eap_len > len) {
781 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
784 } else if (eap_len < len) {
785 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
786 "packet", (unsigned long) len - eap_len);
790 case EAP_CODE_REQUEST:
791 wpa_printf(MSG_DEBUG, " (request)");
793 case EAP_CODE_RESPONSE:
794 wpa_printf(MSG_DEBUG, " (response)");
795 handle_eap_response(hapd, sta, eap, eap_len);
797 case EAP_CODE_SUCCESS:
798 wpa_printf(MSG_DEBUG, " (success)");
800 case EAP_CODE_FAILURE:
801 wpa_printf(MSG_DEBUG, " (failure)");
803 case EAP_CODE_INITIATE:
804 wpa_printf(MSG_DEBUG, " (initiate)");
805 handle_eap_initiate(hapd, sta, eap, eap_len);
807 case EAP_CODE_FINISH:
808 wpa_printf(MSG_DEBUG, " (finish)");
811 wpa_printf(MSG_DEBUG, " (unknown code)");
817 static struct eapol_state_machine *
818 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
821 if (sta->flags & WLAN_STA_PREAUTH)
822 flags |= EAPOL_SM_PREAUTH;
824 flags |= EAPOL_SM_USES_WPA;
825 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
826 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
828 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
829 sta->wps_ie, sta->p2p_ie, sta,
830 sta->identity, sta->radius_cui);
835 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
836 * @hapd: hostapd BSS data
837 * @sa: Source address (sender of the EAPOL frame)
839 * @len: Length of buf in octets
841 * This function is called for each incoming EAPOL frame from the interface
843 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
846 struct sta_info *sta;
847 struct ieee802_1x_hdr *hdr;
848 struct ieee802_1x_eapol_key *key;
850 struct rsn_pmksa_cache_entry *pmksa;
853 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
854 !hapd->conf->wps_state)
857 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
858 (unsigned long) len, MAC2STR(sa));
859 sta = ap_get_sta(hapd, sa);
860 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
861 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
862 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
863 "associated/Pre-authenticating STA");
867 if (len < sizeof(*hdr)) {
868 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
872 hdr = (struct ieee802_1x_hdr *) buf;
873 datalen = be_to_host16(hdr->length);
874 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
875 hdr->version, hdr->type, datalen);
877 if (len - sizeof(*hdr) < datalen) {
878 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
880 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
883 if (len - sizeof(*hdr) > datalen) {
884 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
885 "IEEE 802.1X packet",
886 (unsigned long) len - sizeof(*hdr) - datalen);
890 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
891 sta->eapol_sm->dot1xAuthEapolFramesRx++;
894 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
895 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
896 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
897 (key->type == EAPOL_KEY_TYPE_WPA ||
898 key->type == EAPOL_KEY_TYPE_RSN)) {
899 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
900 sizeof(*hdr) + datalen);
904 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
905 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
906 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
907 "802.1X not enabled and WPS not used");
911 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
912 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
913 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
918 if (!sta->eapol_sm) {
919 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
924 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
925 u32 wflags = sta->flags & (WLAN_STA_WPS |
928 if (wflags == WLAN_STA_MAYBE_WPS ||
929 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
931 * Delay EAPOL frame transmission until a
932 * possible WPS STA initiates the handshake
933 * with EAPOL-Start. Only allow the wait to be
934 * skipped if the STA is known to support WPS
937 wpa_printf(MSG_DEBUG, "WPS: Do not start "
938 "EAPOL until EAPOL-Start is "
940 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
943 #endif /* CONFIG_WPS */
945 sta->eapol_sm->eap_if->portEnabled = TRUE;
948 /* since we support version 1, we can ignore version field and proceed
949 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
950 /* TODO: actually, we are not version 1 anymore.. However, Version 2
951 * does not change frame contents, so should be ok to process frames
952 * more or less identically. Some changes might be needed for
953 * verification of fields. */
956 case IEEE802_1X_TYPE_EAP_PACKET:
957 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
960 case IEEE802_1X_TYPE_EAPOL_START:
961 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
962 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
964 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
965 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
967 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
968 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
969 "available - ignore it since "
970 "STA sent EAPOL-Start");
971 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
973 sta->eapol_sm->eapolStart = TRUE;
974 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
975 eap_server_clear_identity(sta->eapol_sm->eap);
976 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
979 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
980 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
981 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
983 sta->acct_terminate_cause =
984 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
985 accounting_sta_stop(hapd, sta);
986 sta->eapol_sm->eapolLogoff = TRUE;
987 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
988 eap_server_clear_identity(sta->eapol_sm->eap);
991 case IEEE802_1X_TYPE_EAPOL_KEY:
992 wpa_printf(MSG_DEBUG, " EAPOL-Key");
993 if (!ap_sta_is_authorized(sta)) {
994 wpa_printf(MSG_DEBUG, " Dropped key data from "
995 "unauthorized Supplicant");
1000 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1001 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1002 /* TODO: implement support for this; show data */
1006 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1007 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1011 eapol_auth_step(sta->eapol_sm);
1016 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1017 * @hapd: hostapd BSS data
1020 * This function is called to start IEEE 802.1X authentication when a new
1021 * station completes IEEE 802.11 association.
1023 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1025 struct rsn_pmksa_cache_entry *pmksa;
1031 if (hapd->conf->wps_state &&
1032 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1033 (sta->flags & WLAN_STA_WPS))) {
1035 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1036 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1037 * authentication in this BSS.
1041 #endif /* CONFIG_WPS */
1043 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1044 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1045 "802.1X not enabled or forced for WPS");
1047 * Clear any possible EAPOL authenticator state to support
1048 * reassociation change from WPS to PSK.
1050 ieee802_1x_free_station(sta);
1054 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1055 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1056 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1058 * Clear any possible EAPOL authenticator state to support
1059 * reassociation change from WPA-EAP to PSK.
1061 ieee802_1x_free_station(sta);
1065 if (sta->eapol_sm == NULL) {
1066 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1067 HOSTAPD_LEVEL_DEBUG, "start authentication");
1068 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1069 if (sta->eapol_sm == NULL) {
1070 hostapd_logger(hapd, sta->addr,
1071 HOSTAPD_MODULE_IEEE8021X,
1073 "failed to allocate state machine");
1080 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1081 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1082 !(sta->flags & WLAN_STA_WPS2)) {
1084 * Delay EAPOL frame transmission until a possible WPS STA
1085 * initiates the handshake with EAPOL-Start. Only allow the
1086 * wait to be skipped if the STA is known to support WPS 2.0.
1088 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1089 "EAPOL-Start is received");
1090 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1092 #endif /* CONFIG_WPS */
1094 sta->eapol_sm->eap_if->portEnabled = TRUE;
1096 #ifdef CONFIG_IEEE80211R
1097 if (sta->auth_alg == WLAN_AUTH_FT) {
1098 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1099 HOSTAPD_LEVEL_DEBUG,
1100 "PMK from FT - skip IEEE 802.1X/EAP");
1101 /* Setup EAPOL state machines to already authenticated state
1102 * because of existing FT information from R0KH. */
1103 sta->eapol_sm->keyRun = TRUE;
1104 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1105 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1106 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1107 sta->eapol_sm->authSuccess = TRUE;
1108 sta->eapol_sm->authFail = FALSE;
1109 if (sta->eapol_sm->eap)
1110 eap_sm_notify_cached(sta->eapol_sm->eap);
1111 /* TODO: get vlan_id from R0KH using RRB message */
1114 #endif /* CONFIG_IEEE80211R */
1116 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1118 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1119 HOSTAPD_LEVEL_DEBUG,
1120 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1121 /* Setup EAPOL state machines to already authenticated state
1122 * because of existing PMKSA information in the cache. */
1123 sta->eapol_sm->keyRun = TRUE;
1124 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1125 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1126 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1127 sta->eapol_sm->authSuccess = TRUE;
1128 sta->eapol_sm->authFail = FALSE;
1129 if (sta->eapol_sm->eap)
1130 eap_sm_notify_cached(sta->eapol_sm->eap);
1131 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
1132 ap_sta_bind_vlan(hapd, sta);
1136 * Force EAPOL state machines to start
1137 * re-authentication without having to wait for the
1138 * Supplicant to send EAPOL-Start.
1140 sta->eapol_sm->reAuthenticate = TRUE;
1142 eapol_auth_step(sta->eapol_sm);
1147 void ieee802_1x_free_station(struct sta_info *sta)
1149 struct eapol_state_machine *sm = sta->eapol_sm;
1154 sta->eapol_sm = NULL;
1156 #ifndef CONFIG_NO_RADIUS
1157 radius_msg_free(sm->last_recv_radius);
1158 radius_free_class(&sm->radius_class);
1159 wpabuf_free(sm->radius_cui);
1160 #endif /* CONFIG_NO_RADIUS */
1162 os_free(sm->identity);
1163 eapol_auth_free(sm);
1167 #ifndef CONFIG_NO_RADIUS
1168 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1169 struct sta_info *sta)
1172 const struct eap_hdr *hdr;
1175 struct radius_msg *msg;
1176 struct eapol_state_machine *sm = sta->eapol_sm;
1178 if (sm == NULL || sm->last_recv_radius == NULL) {
1180 sm->eap_if->aaaEapNoReq = TRUE;
1184 msg = sm->last_recv_radius;
1186 eap = radius_msg_get_eap(msg);
1188 /* RFC 3579, Chap. 2.6.3:
1189 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1191 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1192 HOSTAPD_LEVEL_WARNING, "could not extract "
1193 "EAP-Message from RADIUS message");
1194 sm->eap_if->aaaEapNoReq = TRUE;
1198 if (wpabuf_len(eap) < sizeof(*hdr)) {
1199 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1200 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1201 "received from authentication server");
1203 sm->eap_if->aaaEapNoReq = TRUE;
1207 if (wpabuf_len(eap) > sizeof(*hdr))
1208 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1210 hdr = wpabuf_head(eap);
1211 switch (hdr->code) {
1212 case EAP_CODE_REQUEST:
1214 sm->eap_type_authsrv = eap_type;
1215 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1216 eap_server_get_name(0, eap_type), eap_type);
1218 case EAP_CODE_RESPONSE:
1219 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1220 eap_server_get_name(0, eap_type), eap_type);
1222 case EAP_CODE_SUCCESS:
1223 os_strlcpy(buf, "EAP Success", sizeof(buf));
1225 case EAP_CODE_FAILURE:
1226 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1229 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1232 buf[sizeof(buf) - 1] = '\0';
1233 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1234 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1235 "id=%d len=%d) from RADIUS server: %s",
1236 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1238 sm->eap_if->aaaEapReq = TRUE;
1240 wpabuf_free(sm->eap_if->aaaEapReqData);
1241 sm->eap_if->aaaEapReqData = eap;
1245 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1246 struct sta_info *sta, struct radius_msg *msg,
1247 struct radius_msg *req,
1248 const u8 *shared_secret,
1249 size_t shared_secret_len)
1251 struct radius_ms_mppe_keys *keys;
1252 struct eapol_state_machine *sm = sta->eapol_sm;
1256 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1259 if (keys && keys->send && keys->recv) {
1260 size_t len = keys->send_len + keys->recv_len;
1261 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1262 keys->send, keys->send_len);
1263 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1264 keys->recv, keys->recv_len);
1266 os_free(sm->eap_if->aaaEapKeyData);
1267 sm->eap_if->aaaEapKeyData = os_malloc(len);
1268 if (sm->eap_if->aaaEapKeyData) {
1269 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1271 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1272 keys->send, keys->send_len);
1273 sm->eap_if->aaaEapKeyDataLen = len;
1274 sm->eap_if->aaaEapKeyAvailable = TRUE;
1277 wpa_printf(MSG_DEBUG,
1278 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1279 keys, keys ? keys->send : NULL,
1280 keys ? keys->recv : NULL);
1284 os_free(keys->send);
1285 os_free(keys->recv);
1291 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1292 struct sta_info *sta,
1293 struct radius_msg *msg)
1297 struct eapol_state_machine *sm = sta->eapol_sm;
1299 struct radius_attr_data *nclass;
1300 size_t nclass_count;
1302 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1306 radius_free_class(&sm->radius_class);
1307 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1311 nclass = os_calloc(count, sizeof(struct radius_attr_data));
1318 for (i = 0; i < count; i++) {
1320 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1321 &attr_class, &class_len,
1326 } while (class_len < 1);
1328 nclass[nclass_count].data = os_malloc(class_len);
1329 if (nclass[nclass_count].data == NULL)
1332 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
1333 nclass[nclass_count].len = class_len;
1337 sm->radius_class.attr = nclass;
1338 sm->radius_class.count = nclass_count;
1339 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1340 "attributes for " MACSTR,
1341 (unsigned long) sm->radius_class.count,
1342 MAC2STR(sta->addr));
1346 /* Update sta->identity based on User-Name attribute in Access-Accept */
1347 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1348 struct sta_info *sta,
1349 struct radius_msg *msg)
1353 struct eapol_state_machine *sm = sta->eapol_sm;
1358 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1362 identity = (u8 *) dup_binstr(buf, len);
1363 if (identity == NULL)
1366 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1367 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1368 "User-Name from Access-Accept '%s'",
1369 sm->identity ? (char *) sm->identity : "N/A",
1372 os_free(sm->identity);
1373 sm->identity = identity;
1374 sm->identity_len = len;
1378 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1379 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1380 struct sta_info *sta,
1381 struct radius_msg *msg)
1383 struct eapol_state_machine *sm = sta->eapol_sm;
1391 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1392 &buf, &len, NULL) < 0)
1395 cui = wpabuf_alloc_copy(buf, len);
1399 wpabuf_free(sm->radius_cui);
1400 sm->radius_cui = cui;
1406 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1408 sta->remediation = 1;
1409 os_free(sta->remediation_url);
1411 sta->remediation_url = os_malloc(len);
1412 if (!sta->remediation_url)
1414 sta->remediation_method = pos[0];
1415 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1416 sta->remediation_url[len - 1] = '\0';
1417 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1418 "for " MACSTR " - server method %u URL %s",
1419 MAC2STR(sta->addr), sta->remediation_method,
1420 sta->remediation_url);
1422 sta->remediation_url = NULL;
1423 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1424 "for " MACSTR, MAC2STR(sta->addr));
1426 /* TODO: assign the STA into remediation VLAN or add filtering */
1430 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1431 struct sta_info *sta, u8 *pos,
1435 return; /* Malformed information */
1436 sta->hs20_deauth_requested = 1;
1437 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1439 *pos, WPA_GET_LE16(pos + 1));
1440 wpabuf_free(sta->hs20_deauth_req);
1441 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1442 if (sta->hs20_deauth_req) {
1443 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1444 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1445 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1447 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1451 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1452 struct sta_info *sta, u8 *pos,
1453 size_t len, int session_timeout)
1456 int warning_time, beacon_int;
1459 return; /* Malformed information */
1460 os_free(sta->hs20_session_info_url);
1461 sta->hs20_session_info_url = os_malloc(len);
1462 if (sta->hs20_session_info_url == NULL)
1465 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1466 sta->hs20_session_info_url[len - 1] = '\0';
1467 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1468 "(session_timeout=%d)",
1469 sta->hs20_session_info_url, swt, session_timeout);
1470 if (session_timeout < 0) {
1471 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1475 swt = 1; /* Use one minute as the AP selected value */
1477 if ((unsigned int) session_timeout < swt * 60)
1480 warning_time = session_timeout - swt * 60;
1482 beacon_int = hapd->iconf->beacon_int;
1484 beacon_int = 100; /* best guess */
1485 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1486 if (sta->hs20_disassoc_timer > 65535)
1487 sta->hs20_disassoc_timer = 65535;
1489 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1492 #endif /* CONFIG_HS20 */
1495 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1496 struct sta_info *sta,
1497 struct radius_msg *msg,
1498 int session_timeout)
1501 u8 *buf, *pos, *end, type, sublen;
1505 sta->remediation = 0;
1506 sta->hs20_deauth_requested = 0;
1509 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1510 &buf, &len, buf) < 0)
1516 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1523 continue; /* invalid length */
1524 sublen -= 2; /* skip header */
1525 if (pos + sublen > end)
1526 continue; /* invalid WFA VSA */
1529 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1530 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1532 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1533 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1535 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1536 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1541 #endif /* CONFIG_HS20 */
1545 struct sta_id_search {
1547 struct eapol_state_machine *sm;
1551 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1552 struct sta_info *sta,
1555 struct sta_id_search *id_search = ctx;
1556 struct eapol_state_machine *sm = sta->eapol_sm;
1558 if (sm && sm->radius_identifier >= 0 &&
1559 sm->radius_identifier == id_search->identifier) {
1567 static struct eapol_state_machine *
1568 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1570 struct sta_id_search id_search;
1571 id_search.identifier = identifier;
1572 id_search.sm = NULL;
1573 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1574 return id_search.sm;
1579 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1580 * @msg: RADIUS response message
1581 * @req: RADIUS request message
1582 * @shared_secret: RADIUS shared secret
1583 * @shared_secret_len: Length of shared_secret in octets
1584 * @data: Context data (struct hostapd_data *)
1585 * Returns: Processing status
1587 static RadiusRxResult
1588 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1589 const u8 *shared_secret, size_t shared_secret_len,
1592 struct hostapd_data *hapd = data;
1593 struct sta_info *sta;
1594 u32 session_timeout = 0, termination_action, acct_interim_interval;
1595 int session_timeout_set, vlan_id = 0;
1596 struct eapol_state_machine *sm;
1597 int override_eapReq = 0;
1598 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1600 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1602 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1603 "station for this RADIUS message");
1604 return RADIUS_RX_UNKNOWN;
1608 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1609 * present when packet contains an EAP-Message attribute */
1610 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1611 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1613 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1614 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1615 "Message-Authenticator since it does not include "
1617 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1619 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1620 return RADIUS_RX_INVALID_AUTHENTICATOR;
1623 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1624 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1625 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1626 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1627 return RADIUS_RX_UNKNOWN;
1630 sm->radius_identifier = -1;
1631 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1632 MAC2STR(sta->addr));
1634 radius_msg_free(sm->last_recv_radius);
1635 sm->last_recv_radius = msg;
1637 session_timeout_set =
1638 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1640 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1641 &termination_action))
1642 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1644 if (hapd->conf->acct_interim_interval == 0 &&
1645 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1646 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1647 &acct_interim_interval) == 0) {
1648 if (acct_interim_interval < 60) {
1649 hostapd_logger(hapd, sta->addr,
1650 HOSTAPD_MODULE_IEEE8021X,
1652 "ignored too small "
1653 "Acct-Interim-Interval %d",
1654 acct_interim_interval);
1656 sta->acct_interim_interval = acct_interim_interval;
1660 switch (hdr->code) {
1661 case RADIUS_CODE_ACCESS_ACCEPT:
1662 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1664 #ifndef CONFIG_NO_VLAN
1666 vlan_id = radius_msg_get_vlanid(msg);
1668 hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
1669 hostapd_logger(hapd, sta->addr,
1670 HOSTAPD_MODULE_RADIUS,
1672 "VLAN ID %d", vlan_id);
1673 } else if (vlan_id > 0) {
1674 sta->eapol_sm->authFail = TRUE;
1675 hostapd_logger(hapd, sta->addr,
1676 HOSTAPD_MODULE_RADIUS,
1678 "Invalid VLAN ID %d received from RADIUS server",
1681 } else if (hapd->conf->ssid.dynamic_vlan ==
1682 DYNAMIC_VLAN_REQUIRED) {
1683 sta->eapol_sm->authFail = TRUE;
1684 hostapd_logger(hapd, sta->addr,
1685 HOSTAPD_MODULE_IEEE8021X,
1686 HOSTAPD_LEVEL_INFO, "authentication "
1687 "server did not include required VLAN "
1688 "ID in Access-Accept");
1691 #endif /* CONFIG_NO_VLAN */
1693 sta->vlan_id = vlan_id;
1694 if ((sta->flags & WLAN_STA_ASSOC) &&
1695 ap_sta_bind_vlan(hapd, sta) < 0)
1698 sta->session_timeout_set = !!session_timeout_set;
1699 sta->session_timeout = session_timeout;
1701 /* RFC 3580, Ch. 3.17 */
1702 if (session_timeout_set && termination_action ==
1703 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1704 sm->reAuthPeriod = session_timeout;
1705 } else if (session_timeout_set)
1706 ap_sta_session_timeout(hapd, sta, session_timeout);
1708 sm->eap_if->aaaSuccess = TRUE;
1709 override_eapReq = 1;
1710 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1712 ieee802_1x_store_radius_class(hapd, sta, msg);
1713 ieee802_1x_update_sta_identity(hapd, sta, msg);
1714 ieee802_1x_update_sta_cui(hapd, sta, msg);
1715 ieee802_1x_check_hs20(hapd, sta, msg,
1716 session_timeout_set ?
1717 (int) session_timeout : -1);
1718 if (sm->eap_if->eapKeyAvailable && !sta->remediation &&
1719 !sta->hs20_deauth_requested &&
1720 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1721 session_timeout_set ?
1722 (int) session_timeout : -1, sm) == 0) {
1723 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1724 HOSTAPD_LEVEL_DEBUG,
1725 "Added PMKSA cache entry");
1728 case RADIUS_CODE_ACCESS_REJECT:
1729 sm->eap_if->aaaFail = TRUE;
1730 override_eapReq = 1;
1732 case RADIUS_CODE_ACCESS_CHALLENGE:
1733 sm->eap_if->aaaEapReq = TRUE;
1734 if (session_timeout_set) {
1735 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1736 sm->eap_if->aaaMethodTimeout = session_timeout;
1737 hostapd_logger(hapd, sm->addr,
1738 HOSTAPD_MODULE_IEEE8021X,
1739 HOSTAPD_LEVEL_DEBUG,
1740 "using EAP timeout of %d seconds (from "
1742 sm->eap_if->aaaMethodTimeout);
1745 * Use dynamic retransmission behavior per EAP
1748 sm->eap_if->aaaMethodTimeout = 0;
1753 ieee802_1x_decapsulate_radius(hapd, sta);
1754 if (override_eapReq)
1755 sm->eap_if->aaaEapReq = FALSE;
1757 eapol_auth_step(sm);
1759 return RADIUS_RX_QUEUED;
1761 #endif /* CONFIG_NO_RADIUS */
1764 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1766 struct eapol_state_machine *sm = sta->eapol_sm;
1770 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1771 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1773 #ifndef CONFIG_NO_RADIUS
1774 radius_msg_free(sm->last_recv_radius);
1775 sm->last_recv_radius = NULL;
1776 #endif /* CONFIG_NO_RADIUS */
1778 if (sm->eap_if->eapTimeout) {
1780 * Disconnect the STA since it did not reply to the last EAP
1781 * request and we cannot continue EAP processing (EAP-Failure
1782 * could only be sent if the EAP peer actually replied).
1784 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1785 MAC2STR(sta->addr));
1787 sm->eap_if->portEnabled = FALSE;
1788 ap_sta_disconnect(hapd, sta, sta->addr,
1789 WLAN_REASON_PREV_AUTH_NOT_VALID);
1794 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1796 struct eapol_authenticator *eapol = hapd->eapol_auth;
1798 if (hapd->conf->default_wep_key_len < 1)
1801 os_free(eapol->default_wep_key);
1802 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1803 if (eapol->default_wep_key == NULL ||
1804 random_get_bytes(eapol->default_wep_key,
1805 hapd->conf->default_wep_key_len)) {
1806 wpa_printf(MSG_INFO, "Could not generate random WEP key");
1807 os_free(eapol->default_wep_key);
1808 eapol->default_wep_key = NULL;
1812 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1813 eapol->default_wep_key,
1814 hapd->conf->default_wep_key_len);
1820 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1821 struct sta_info *sta, void *ctx)
1823 if (sta->eapol_sm) {
1824 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1825 eapol_auth_step(sta->eapol_sm);
1831 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1833 struct hostapd_data *hapd = eloop_ctx;
1834 struct eapol_authenticator *eapol = hapd->eapol_auth;
1836 if (eapol->default_wep_key_idx >= 3)
1837 eapol->default_wep_key_idx =
1838 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1840 eapol->default_wep_key_idx++;
1842 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1843 eapol->default_wep_key_idx);
1845 if (ieee802_1x_rekey_broadcast(hapd)) {
1846 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1847 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1848 "new broadcast key");
1849 os_free(eapol->default_wep_key);
1850 eapol->default_wep_key = NULL;
1854 /* TODO: Could setup key for RX here, but change default TX keyid only
1855 * after new broadcast key has been sent to all stations. */
1856 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1857 broadcast_ether_addr,
1858 eapol->default_wep_key_idx, 1, NULL, 0,
1859 eapol->default_wep_key,
1860 hapd->conf->default_wep_key_len)) {
1861 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1862 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1863 "new broadcast key");
1864 os_free(eapol->default_wep_key);
1865 eapol->default_wep_key = NULL;
1869 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1871 if (hapd->conf->wep_rekeying_period > 0) {
1872 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1873 ieee802_1x_rekey, hapd, NULL);
1878 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1879 const u8 *data, size_t datalen)
1882 struct sta_info *sta = sta_ctx;
1884 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1885 WLAN_STA_MAYBE_WPS) {
1887 size_t identity_len;
1888 struct eapol_state_machine *sm = sta->eapol_sm;
1890 identity = eap_get_identity(sm->eap, &identity_len);
1892 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1893 os_memcmp(identity, WSC_ID_ENROLLEE,
1894 WSC_ID_ENROLLEE_LEN) == 0) ||
1895 (identity_len == WSC_ID_REGISTRAR_LEN &&
1896 os_memcmp(identity, WSC_ID_REGISTRAR,
1897 WSC_ID_REGISTRAR_LEN) == 0))) {
1898 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1900 sta->flags |= WLAN_STA_WPS;
1903 #endif /* CONFIG_WPS */
1905 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1909 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1910 const u8 *data, size_t datalen)
1912 #ifndef CONFIG_NO_RADIUS
1913 struct hostapd_data *hapd = ctx;
1914 struct sta_info *sta = sta_ctx;
1916 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1917 #endif /* CONFIG_NO_RADIUS */
1921 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1922 int preauth, int remediation)
1924 struct hostapd_data *hapd = ctx;
1925 struct sta_info *sta = sta_ctx;
1927 rsn_preauth_finished(hapd, sta, success);
1929 ieee802_1x_finished(hapd, sta, success, remediation);
1933 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1934 size_t identity_len, int phase2,
1935 struct eap_user *user)
1937 struct hostapd_data *hapd = ctx;
1938 const struct hostapd_eap_user *eap_user;
1942 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
1943 if (eap_user == NULL)
1946 os_memset(user, 0, sizeof(*user));
1947 user->phase2 = phase2;
1948 for (i = 0; i < EAP_MAX_METHODS; i++) {
1949 user->methods[i].vendor = eap_user->methods[i].vendor;
1950 user->methods[i].method = eap_user->methods[i].method;
1953 if (eap_user->password) {
1954 user->password = os_malloc(eap_user->password_len);
1955 if (user->password == NULL)
1957 os_memcpy(user->password, eap_user->password,
1958 eap_user->password_len);
1959 user->password_len = eap_user->password_len;
1960 user->password_hash = eap_user->password_hash;
1962 user->force_version = eap_user->force_version;
1963 user->macacl = eap_user->macacl;
1964 user->ttls_auth = eap_user->ttls_auth;
1965 user->remediation = eap_user->remediation;
1970 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
1976 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1978 struct hostapd_data *hapd = ctx;
1979 struct sta_info *sta;
1980 sta = ap_get_sta(hapd, addr);
1981 if (sta == NULL || sta->eapol_sm == NULL)
1987 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1988 eapol_logger_level level, const char *txt)
1990 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1991 struct hostapd_data *hapd = ctx;
1995 case EAPOL_LOGGER_WARNING:
1996 hlevel = HOSTAPD_LEVEL_WARNING;
1998 case EAPOL_LOGGER_INFO:
1999 hlevel = HOSTAPD_LEVEL_INFO;
2001 case EAPOL_LOGGER_DEBUG:
2003 hlevel = HOSTAPD_LEVEL_DEBUG;
2007 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2009 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
2013 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2016 struct hostapd_data *hapd = ctx;
2017 struct sta_info *sta = sta_ctx;
2018 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2022 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2024 struct hostapd_data *hapd = ctx;
2025 struct sta_info *sta = sta_ctx;
2026 ieee802_1x_abort_auth(hapd, sta);
2030 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2033 #ifndef CONFIG_NO_RC4
2034 struct hostapd_data *hapd = ctx;
2035 struct sta_info *sta = sta_ctx;
2036 ieee802_1x_tx_key(hapd, sta);
2037 #endif /* CONFIG_NO_RC4 */
2038 #endif /* CONFIG_FIPS */
2042 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2043 enum eapol_event type)
2045 /* struct hostapd_data *hapd = ctx; */
2046 struct sta_info *sta = sta_ctx;
2048 case EAPOL_AUTH_SM_CHANGE:
2049 wpa_auth_sm_notify(sta->wpa_sm);
2051 case EAPOL_AUTH_REAUTHENTICATE:
2052 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2060 static struct eap_server_erp_key *
2061 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2063 struct hostapd_data *hapd = ctx;
2064 struct eap_server_erp_key *erp;
2066 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2068 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2076 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2078 struct hostapd_data *hapd = ctx;
2080 dl_list_add(&hapd->erp_keys, &erp->list);
2084 #endif /* CONFIG_ERP */
2087 int ieee802_1x_init(struct hostapd_data *hapd)
2090 struct eapol_auth_config conf;
2091 struct eapol_auth_cb cb;
2093 dl_list_init(&hapd->erp_keys);
2095 os_memset(&conf, 0, sizeof(conf));
2097 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2098 conf.wpa = hapd->conf->wpa;
2099 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2100 conf.eap_server = hapd->conf->eap_server;
2101 conf.ssl_ctx = hapd->ssl_ctx;
2102 conf.msg_ctx = hapd->msg_ctx;
2103 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2104 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2105 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2106 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2107 conf.erp_domain = hapd->conf->erp_domain;
2108 conf.erp = hapd->conf->eap_server_erp;
2109 conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
2110 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2111 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2112 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2113 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2114 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2115 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2116 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2117 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2118 conf.tnc = hapd->conf->tnc;
2119 conf.wps = hapd->wps;
2120 conf.fragment_size = hapd->conf->fragment_size;
2121 conf.pwd_group = hapd->conf->pwd_group;
2122 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2123 if (hapd->conf->server_id) {
2124 conf.server_id = (const u8 *) hapd->conf->server_id;
2125 conf.server_id_len = os_strlen(hapd->conf->server_id);
2127 conf.server_id = (const u8 *) "hostapd";
2128 conf.server_id_len = 7;
2131 os_memset(&cb, 0, sizeof(cb));
2132 cb.eapol_send = ieee802_1x_eapol_send;
2133 cb.aaa_send = ieee802_1x_aaa_send;
2134 cb.finished = _ieee802_1x_finished;
2135 cb.get_eap_user = ieee802_1x_get_eap_user;
2136 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2137 cb.logger = ieee802_1x_logger;
2138 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2139 cb.abort_auth = _ieee802_1x_abort_auth;
2140 cb.tx_key = _ieee802_1x_tx_key;
2141 cb.eapol_event = ieee802_1x_eapol_event;
2143 cb.erp_get_key = ieee802_1x_erp_get_key;
2144 cb.erp_add_key = ieee802_1x_erp_add_key;
2145 #endif /* CONFIG_ERP */
2147 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2148 if (hapd->eapol_auth == NULL)
2151 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2152 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2155 #ifndef CONFIG_NO_RADIUS
2156 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2157 ieee802_1x_receive_auth, hapd))
2159 #endif /* CONFIG_NO_RADIUS */
2161 if (hapd->conf->default_wep_key_len) {
2162 for (i = 0; i < 4; i++)
2163 hostapd_drv_set_key(hapd->conf->iface, hapd,
2164 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2167 ieee802_1x_rekey(hapd, NULL);
2169 if (hapd->eapol_auth->default_wep_key == NULL)
2177 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2179 struct eap_server_erp_key *erp;
2181 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2183 dl_list_del(&erp->list);
2184 bin_clear_free(erp, sizeof(*erp));
2189 void ieee802_1x_deinit(struct hostapd_data *hapd)
2191 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2193 if (hapd->driver != NULL &&
2194 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2195 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2197 eapol_auth_deinit(hapd->eapol_auth);
2198 hapd->eapol_auth = NULL;
2200 ieee802_1x_erp_flush(hapd);
2204 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2205 const u8 *buf, size_t len, int ack)
2207 struct ieee80211_hdr *hdr;
2209 const unsigned char rfc1042_hdr[ETH_ALEN] =
2210 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2214 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2217 hdr = (struct ieee80211_hdr *) buf;
2218 pos = (u8 *) (hdr + 1);
2219 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2221 pos += sizeof(rfc1042_hdr);
2222 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2226 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2231 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2232 const u8 *buf, int len, int ack)
2234 const struct ieee802_1x_hdr *xhdr =
2235 (const struct ieee802_1x_hdr *) buf;
2236 const u8 *pos = buf + sizeof(*xhdr);
2237 struct ieee802_1x_eapol_key *key;
2239 if (len < (int) sizeof(*xhdr))
2241 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2242 "type=%d length=%d - ack=%d",
2243 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2244 be_to_host16(xhdr->length), ack);
2246 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2249 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2250 const struct wpa_eapol_key *wpa;
2251 wpa = (const struct wpa_eapol_key *) pos;
2252 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2253 wpa->type == EAPOL_KEY_TYPE_WPA)
2254 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2258 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2259 * or Authenticator state machines, but EAPOL-Key packets are not
2260 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2261 * packets couple of times because otherwise STA keys become
2262 * unsynchronized with AP. */
2263 if (!ack && pos + sizeof(*key) <= buf + len) {
2264 key = (struct ieee802_1x_eapol_key *) pos;
2265 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2266 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2267 "frame (%scast index=%d)",
2268 key->key_index & BIT(7) ? "uni" : "broad",
2269 key->key_index & ~BIT(7));
2270 /* TODO: re-send EAPOL-Key couple of times (with short delay
2271 * between them?). If all attempt fail, report error and
2272 * deauthenticate STA so that it will get new keys when
2273 * authenticating again (e.g., after returning in range).
2274 * Separate limit/transmit state needed both for unicast and
2275 * broadcast keys(?) */
2277 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2278 * to here and change the key only if the EAPOL-Key packet was Acked.
2285 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2287 if (sm == NULL || sm->identity == NULL)
2290 *len = sm->identity_len;
2291 return sm->identity;
2295 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2298 if (sm == NULL || sm->radius_class.attr == NULL ||
2299 idx >= (int) sm->radius_class.count)
2302 *len = sm->radius_class.attr[idx].len;
2303 return sm->radius_class.attr[idx].data;
2307 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2311 return sm->radius_cui;
2315 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2321 *len = sm->eap_if->eapKeyDataLen;
2322 return sm->eap_if->eapKeyData;
2326 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2331 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2332 eapol_auth_step(sm);
2336 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2341 sm->portValid = valid ? TRUE : FALSE;
2342 eapol_auth_step(sm);
2346 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2351 sm->flags |= EAPOL_SM_PREAUTH;
2353 sm->flags &= ~EAPOL_SM_PREAUTH;
2357 static const char * bool_txt(Boolean val)
2359 return val ? "TRUE" : "FALSE";
2363 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2370 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2371 char *buf, size_t buflen)
2374 struct eapol_state_machine *sm = sta->eapol_sm;
2375 struct os_reltime diff;
2382 ret = os_snprintf(buf + len, buflen - len,
2383 "dot1xPaePortNumber=%d\n"
2384 "dot1xPaePortProtocolVersion=%d\n"
2385 "dot1xPaePortCapabilities=1\n"
2386 "dot1xPaePortInitialize=%d\n"
2387 "dot1xPaePortReauthenticate=FALSE\n",
2391 if (os_snprintf_error(buflen - len, ret))
2395 /* dot1xAuthConfigTable */
2396 ret = os_snprintf(buf + len, buflen - len,
2397 "dot1xAuthPaeState=%d\n"
2398 "dot1xAuthBackendAuthState=%d\n"
2399 "dot1xAuthAdminControlledDirections=%d\n"
2400 "dot1xAuthOperControlledDirections=%d\n"
2401 "dot1xAuthAuthControlledPortStatus=%d\n"
2402 "dot1xAuthAuthControlledPortControl=%d\n"
2403 "dot1xAuthQuietPeriod=%u\n"
2404 "dot1xAuthServerTimeout=%u\n"
2405 "dot1xAuthReAuthPeriod=%u\n"
2406 "dot1xAuthReAuthEnabled=%s\n"
2407 "dot1xAuthKeyTxEnabled=%s\n",
2408 sm->auth_pae_state + 1,
2409 sm->be_auth_state + 1,
2410 sm->adminControlledDirections,
2411 sm->operControlledDirections,
2417 bool_txt(sm->reAuthEnabled),
2418 bool_txt(sm->keyTxEnabled));
2419 if (os_snprintf_error(buflen - len, ret))
2423 /* dot1xAuthStatsTable */
2424 ret = os_snprintf(buf + len, buflen - len,
2425 "dot1xAuthEapolFramesRx=%u\n"
2426 "dot1xAuthEapolFramesTx=%u\n"
2427 "dot1xAuthEapolStartFramesRx=%u\n"
2428 "dot1xAuthEapolLogoffFramesRx=%u\n"
2429 "dot1xAuthEapolRespIdFramesRx=%u\n"
2430 "dot1xAuthEapolRespFramesRx=%u\n"
2431 "dot1xAuthEapolReqIdFramesTx=%u\n"
2432 "dot1xAuthEapolReqFramesTx=%u\n"
2433 "dot1xAuthInvalidEapolFramesRx=%u\n"
2434 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2435 "dot1xAuthLastEapolFrameVersion=%u\n"
2436 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2437 sm->dot1xAuthEapolFramesRx,
2438 sm->dot1xAuthEapolFramesTx,
2439 sm->dot1xAuthEapolStartFramesRx,
2440 sm->dot1xAuthEapolLogoffFramesRx,
2441 sm->dot1xAuthEapolRespIdFramesRx,
2442 sm->dot1xAuthEapolRespFramesRx,
2443 sm->dot1xAuthEapolReqIdFramesTx,
2444 sm->dot1xAuthEapolReqFramesTx,
2445 sm->dot1xAuthInvalidEapolFramesRx,
2446 sm->dot1xAuthEapLengthErrorFramesRx,
2447 sm->dot1xAuthLastEapolFrameVersion,
2449 if (os_snprintf_error(buflen - len, ret))
2453 /* dot1xAuthDiagTable */
2454 ret = os_snprintf(buf + len, buflen - len,
2455 "dot1xAuthEntersConnecting=%u\n"
2456 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2457 "dot1xAuthEntersAuthenticating=%u\n"
2458 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2459 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2460 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2461 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2462 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2463 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2464 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2465 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2466 "dot1xAuthBackendResponses=%u\n"
2467 "dot1xAuthBackendAccessChallenges=%u\n"
2468 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2469 "dot1xAuthBackendAuthSuccesses=%u\n"
2470 "dot1xAuthBackendAuthFails=%u\n",
2471 sm->authEntersConnecting,
2472 sm->authEapLogoffsWhileConnecting,
2473 sm->authEntersAuthenticating,
2474 sm->authAuthSuccessesWhileAuthenticating,
2475 sm->authAuthTimeoutsWhileAuthenticating,
2476 sm->authAuthFailWhileAuthenticating,
2477 sm->authAuthEapStartsWhileAuthenticating,
2478 sm->authAuthEapLogoffWhileAuthenticating,
2479 sm->authAuthReauthsWhileAuthenticated,
2480 sm->authAuthEapStartsWhileAuthenticated,
2481 sm->authAuthEapLogoffWhileAuthenticated,
2482 sm->backendResponses,
2483 sm->backendAccessChallenges,
2484 sm->backendOtherRequestsToSupplicant,
2485 sm->backendAuthSuccesses,
2486 sm->backendAuthFails);
2487 if (os_snprintf_error(buflen - len, ret))
2491 /* dot1xAuthSessionStatsTable */
2492 os_reltime_age(&sta->acct_session_start, &diff);
2493 ret = os_snprintf(buf + len, buflen - len,
2494 /* TODO: dot1xAuthSessionOctetsRx */
2495 /* TODO: dot1xAuthSessionOctetsTx */
2496 /* TODO: dot1xAuthSessionFramesRx */
2497 /* TODO: dot1xAuthSessionFramesTx */
2498 "dot1xAuthSessionId=%08X-%08X\n"
2499 "dot1xAuthSessionAuthenticMethod=%d\n"
2500 "dot1xAuthSessionTime=%u\n"
2501 "dot1xAuthSessionTerminateCause=999\n"
2502 "dot1xAuthSessionUserName=%s\n",
2503 sta->acct_session_id_hi, sta->acct_session_id_lo,
2504 (wpa_key_mgmt_wpa_ieee8021x(
2505 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2507 (unsigned int) diff.sec,
2509 if (os_snprintf_error(buflen - len, ret))
2513 if (sm->acct_multi_session_id_hi) {
2514 ret = os_snprintf(buf + len, buflen - len,
2515 "authMultiSessionId=%08X+%08X\n",
2516 sm->acct_multi_session_id_hi,
2517 sm->acct_multi_session_id_lo);
2518 if (os_snprintf_error(buflen - len, ret))
2523 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2524 name2 = eap_server_get_name(0, sm->eap_type_supp);
2525 ret = os_snprintf(buf + len, buflen - len,
2526 "last_eap_type_as=%d (%s)\n"
2527 "last_eap_type_sta=%d (%s)\n",
2528 sm->eap_type_authsrv, name1,
2529 sm->eap_type_supp, name2);
2530 if (os_snprintf_error(buflen - len, ret))
2538 static void ieee802_1x_finished(struct hostapd_data *hapd,
2539 struct sta_info *sta, int success,
2544 /* TODO: get PMKLifetime from WPA parameters */
2545 static const int dot11RSNAConfigPMKLifetime = 43200;
2546 unsigned int session_timeout;
2549 if (remediation && !sta->remediation) {
2550 sta->remediation = 1;
2551 os_free(sta->remediation_url);
2552 sta->remediation_url =
2553 os_strdup(hapd->conf->subscr_remediation_url);
2554 sta->remediation_method = 1; /* SOAP-XML SPP */
2558 if (sta->remediation) {
2559 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2560 "to " MACSTR " to indicate Subscription "
2562 MAC2STR(sta->addr));
2563 hs20_send_wnm_notification(hapd, sta->addr,
2564 sta->remediation_method,
2565 sta->remediation_url);
2566 os_free(sta->remediation_url);
2567 sta->remediation_url = NULL;
2570 if (sta->hs20_deauth_req) {
2571 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2572 "to " MACSTR " to indicate imminent "
2573 "deauthentication", MAC2STR(sta->addr));
2574 hs20_send_wnm_notification_deauth_req(
2575 hapd, sta->addr, sta->hs20_deauth_req);
2578 #endif /* CONFIG_HS20 */
2580 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2581 if (sta->session_timeout_set)
2582 session_timeout = sta->session_timeout;
2584 session_timeout = dot11RSNAConfigPMKLifetime;
2585 if (success && key && len >= PMK_LEN && !sta->remediation &&
2586 !sta->hs20_deauth_requested &&
2587 wpa_auth_pmksa_add(sta->wpa_sm, key, session_timeout,
2588 sta->eapol_sm) == 0) {
2589 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2590 HOSTAPD_LEVEL_DEBUG,
2591 "Added PMKSA cache entry (IEEE 802.1X)");
2596 * Many devices require deauthentication after WPS provisioning
2597 * and some may not be be able to do that themselves, so
2598 * disconnect the client here. In addition, this may also
2599 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2600 * the EAPOL PAE state machine would remain in HELD state for
2601 * considerable amount of time and some EAP methods, like
2602 * EAP-FAST with anonymous provisioning, may require another
2603 * EAPOL authentication to be started to complete connection.
2605 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2606 "disconnection after EAP-Failure");
2607 /* Add a small sleep to increase likelihood of previously
2608 * requested EAP-Failure TX getting out before this should the
2609 * driver reorder operations.
2612 ap_sta_disconnect(hapd, sta, sta->addr,
2613 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
2614 hostapd_wps_eap_completed(hapd);