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) ||
351 WPA_PROTO_RSN : WPA_PROTO_WPA,
352 hapd->conf->wpa_group);
353 if (!hostapd_config_get_radius_attr(req_attr,
354 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
355 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
357 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
361 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
362 suite = wpa_akm_to_suite(val);
364 !hostapd_config_get_radius_attr(req_attr,
365 RADIUS_ATTR_WLAN_AKM_SUITE) &&
366 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
368 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
372 #ifdef CONFIG_IEEE80211W
373 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
374 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
375 hapd->conf->group_mgmt_cipher);
376 if (!hostapd_config_get_radius_attr(
377 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
378 !radius_msg_add_attr_int32(
379 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
380 wpa_printf(MSG_ERROR,
381 "Could not add WLAN-Group-Mgmt-Cipher");
385 #endif /* CONFIG_IEEE80211W */
391 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
392 struct hostapd_radius_attr *req_attr,
393 struct sta_info *sta,
394 struct radius_msg *msg)
398 if (!hostapd_config_get_radius_attr(req_attr,
399 RADIUS_ATTR_NAS_PORT) &&
400 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
401 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
405 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
407 buf[sizeof(buf) - 1] = '\0';
408 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
409 (u8 *) buf, os_strlen(buf))) {
410 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
414 if (sta->flags & WLAN_STA_PREAUTH) {
415 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
418 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
419 radius_sta_rate(hapd, sta) / 2,
420 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
421 radius_mode_txt(hapd));
422 buf[sizeof(buf) - 1] = '\0';
424 if (!hostapd_config_get_radius_attr(req_attr,
425 RADIUS_ATTR_CONNECT_INFO) &&
426 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
427 (u8 *) buf, os_strlen(buf))) {
428 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
432 if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
433 os_snprintf(buf, sizeof(buf), "%08X-%08X",
434 sta->acct_session_id_hi, sta->acct_session_id_lo);
435 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
436 (u8 *) buf, os_strlen(buf))) {
437 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
442 #ifdef CONFIG_IEEE80211R
443 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
445 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
446 sta->auth_alg == WLAN_AUTH_FT) &&
447 !hostapd_config_get_radius_attr(req_attr,
448 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
449 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
451 hapd->conf->mobility_domain))) {
452 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
455 #endif /* CONFIG_IEEE80211R */
457 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
458 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
465 int add_common_radius_attr(struct hostapd_data *hapd,
466 struct hostapd_radius_attr *req_attr,
467 struct sta_info *sta,
468 struct radius_msg *msg)
471 struct hostapd_radius_attr *attr;
473 if (!hostapd_config_get_radius_attr(req_attr,
474 RADIUS_ATTR_NAS_IP_ADDRESS) &&
475 hapd->conf->own_ip_addr.af == AF_INET &&
476 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
477 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
478 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
483 if (!hostapd_config_get_radius_attr(req_attr,
484 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
485 hapd->conf->own_ip_addr.af == AF_INET6 &&
486 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
487 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
488 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
491 #endif /* CONFIG_IPV6 */
493 if (!hostapd_config_get_radius_attr(req_attr,
494 RADIUS_ATTR_NAS_IDENTIFIER) &&
495 hapd->conf->nas_identifier &&
496 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
497 (u8 *) hapd->conf->nas_identifier,
498 os_strlen(hapd->conf->nas_identifier))) {
499 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
503 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
504 MAC2STR(hapd->own_addr),
505 wpa_ssid_txt(hapd->conf->ssid.ssid,
506 hapd->conf->ssid.ssid_len));
507 buf[sizeof(buf) - 1] = '\0';
508 if (!hostapd_config_get_radius_attr(req_attr,
509 RADIUS_ATTR_CALLED_STATION_ID) &&
510 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
511 (u8 *) buf, os_strlen(buf))) {
512 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
516 if (!hostapd_config_get_radius_attr(req_attr,
517 RADIUS_ATTR_NAS_PORT_TYPE) &&
518 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
519 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
520 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
524 #ifdef CONFIG_INTERWORKING
525 if (hapd->conf->interworking &&
526 !is_zero_ether_addr(hapd->conf->hessid)) {
527 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
528 MAC2STR(hapd->conf->hessid));
529 buf[sizeof(buf) - 1] = '\0';
530 if (!hostapd_config_get_radius_attr(req_attr,
531 RADIUS_ATTR_WLAN_HESSID) &&
532 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
533 (u8 *) buf, os_strlen(buf))) {
534 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
538 #endif /* CONFIG_INTERWORKING */
540 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
543 for (attr = req_attr; attr; attr = attr->next) {
544 if (!radius_msg_add_attr(msg, attr->type,
545 wpabuf_head(attr->val),
546 wpabuf_len(attr->val))) {
547 wpa_printf(MSG_ERROR, "Could not add RADIUS "
557 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
558 struct sta_info *sta,
559 const u8 *eap, size_t len)
561 struct radius_msg *msg;
562 struct eapol_state_machine *sm = sta->eapol_sm;
567 ieee802_1x_learn_identity(hapd, sm, eap, len);
569 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
572 sm->radius_identifier = radius_client_get_id(hapd->radius);
573 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
574 sm->radius_identifier);
576 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
580 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
583 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
584 sm->identity, sm->identity_len)) {
585 wpa_printf(MSG_INFO, "Could not add User-Name");
589 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
593 /* TODO: should probably check MTU from driver config; 2304 is max for
594 * IEEE 802.11, but use 1400 to avoid problems with too large packets
596 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
597 RADIUS_ATTR_FRAMED_MTU) &&
598 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
599 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
603 if (eap && !radius_msg_add_eap(msg, eap, len)) {
604 wpa_printf(MSG_INFO, "Could not add EAP-Message");
608 /* State attribute must be copied if and only if this packet is
609 * Access-Request reply to the previous Access-Challenge */
610 if (sm->last_recv_radius &&
611 radius_msg_get_hdr(sm->last_recv_radius)->code ==
612 RADIUS_CODE_ACCESS_CHALLENGE) {
613 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
616 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
620 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
624 if (hapd->conf->radius_request_cui) {
627 /* Add previously learned CUI or nul CUI to request CUI */
628 if (sm->radius_cui) {
629 cui = wpabuf_head(sm->radius_cui);
630 cui_len = wpabuf_len(sm->radius_cui);
632 cui = (const u8 *) "\0";
635 if (!radius_msg_add_attr(msg,
636 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
638 wpa_printf(MSG_ERROR, "Could not add CUI");
644 if (hapd->conf->hs20) {
645 u8 ver = 1; /* Release 2 */
646 if (!radius_msg_add_wfa(
647 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
649 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
654 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
658 pos = wpabuf_head_u8(sta->hs20_ie);
659 buf[0] = (*pos) >> 4;
660 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
661 wpabuf_len(sta->hs20_ie) >= 3)
662 id = WPA_GET_LE16(pos + 1);
665 WPA_PUT_BE16(buf + 1, id);
666 if (!radius_msg_add_wfa(
668 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
670 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
676 #endif /* CONFIG_HS20 */
678 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
684 radius_msg_free(msg);
686 #endif /* CONFIG_NO_RADIUS */
689 static void handle_eap_response(struct hostapd_data *hapd,
690 struct sta_info *sta, struct eap_hdr *eap,
694 struct eapol_state_machine *sm = sta->eapol_sm;
698 data = (u8 *) (eap + 1);
700 if (len < sizeof(*eap) + 1) {
701 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
705 sm->eap_type_supp = type = data[0];
707 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
708 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
709 "id=%d len=%d) from STA: EAP Response-%s (%d)",
710 eap->code, eap->identifier, be_to_host16(eap->length),
711 eap_server_get_name(0, type), type);
713 sm->dot1xAuthEapolRespFramesRx++;
715 wpabuf_free(sm->eap_if->eapRespData);
716 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
721 static void handle_eap_initiate(struct hostapd_data *hapd,
722 struct sta_info *sta, struct eap_hdr *eap,
727 struct eapol_state_machine *sm = sta->eapol_sm;
732 if (len < sizeof(*eap) + 1) {
734 "handle_eap_initiate: too short response data");
738 data = (u8 *) (eap + 1);
741 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
742 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
743 "id=%d len=%d) from STA: EAP Initiate type %u",
744 eap->code, eap->identifier, be_to_host16(eap->length),
747 wpabuf_free(sm->eap_if->eapRespData);
748 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
750 #endif /* CONFIG_ERP */
754 /* Process incoming EAP packet from Supplicant */
755 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
761 if (len < sizeof(*eap)) {
762 wpa_printf(MSG_INFO, " too short EAP packet");
766 eap = (struct eap_hdr *) buf;
768 eap_len = be_to_host16(eap->length);
769 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
770 eap->code, eap->identifier, eap_len);
771 if (eap_len < sizeof(*eap)) {
772 wpa_printf(MSG_DEBUG, " Invalid EAP length");
774 } else if (eap_len > len) {
775 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
778 } else if (eap_len < len) {
779 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
780 "packet", (unsigned long) len - eap_len);
784 case EAP_CODE_REQUEST:
785 wpa_printf(MSG_DEBUG, " (request)");
787 case EAP_CODE_RESPONSE:
788 wpa_printf(MSG_DEBUG, " (response)");
789 handle_eap_response(hapd, sta, eap, eap_len);
791 case EAP_CODE_SUCCESS:
792 wpa_printf(MSG_DEBUG, " (success)");
794 case EAP_CODE_FAILURE:
795 wpa_printf(MSG_DEBUG, " (failure)");
797 case EAP_CODE_INITIATE:
798 wpa_printf(MSG_DEBUG, " (initiate)");
799 handle_eap_initiate(hapd, sta, eap, eap_len);
801 case EAP_CODE_FINISH:
802 wpa_printf(MSG_DEBUG, " (finish)");
805 wpa_printf(MSG_DEBUG, " (unknown code)");
811 static struct eapol_state_machine *
812 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
815 if (sta->flags & WLAN_STA_PREAUTH)
816 flags |= EAPOL_SM_PREAUTH;
818 flags |= EAPOL_SM_USES_WPA;
819 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
820 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
822 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
823 sta->wps_ie, sta->p2p_ie, sta,
824 sta->identity, sta->radius_cui);
829 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
830 * @hapd: hostapd BSS data
831 * @sa: Source address (sender of the EAPOL frame)
833 * @len: Length of buf in octets
835 * This function is called for each incoming EAPOL frame from the interface
837 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
840 struct sta_info *sta;
841 struct ieee802_1x_hdr *hdr;
842 struct ieee802_1x_eapol_key *key;
844 struct rsn_pmksa_cache_entry *pmksa;
847 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
848 !hapd->conf->wps_state)
851 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
852 (unsigned long) len, MAC2STR(sa));
853 sta = ap_get_sta(hapd, sa);
854 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
855 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
856 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
857 "associated/Pre-authenticating STA");
861 if (len < sizeof(*hdr)) {
862 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
866 hdr = (struct ieee802_1x_hdr *) buf;
867 datalen = be_to_host16(hdr->length);
868 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
869 hdr->version, hdr->type, datalen);
871 if (len - sizeof(*hdr) < datalen) {
872 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
874 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
877 if (len - sizeof(*hdr) > datalen) {
878 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
879 "IEEE 802.1X packet",
880 (unsigned long) len - sizeof(*hdr) - datalen);
884 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
885 sta->eapol_sm->dot1xAuthEapolFramesRx++;
888 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
889 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
890 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
891 (key->type == EAPOL_KEY_TYPE_WPA ||
892 key->type == EAPOL_KEY_TYPE_RSN)) {
893 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
894 sizeof(*hdr) + datalen);
898 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
899 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
900 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
901 "802.1X not enabled and WPS not used");
905 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
906 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
907 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
912 if (!sta->eapol_sm) {
913 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
918 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
919 u32 wflags = sta->flags & (WLAN_STA_WPS |
922 if (wflags == WLAN_STA_MAYBE_WPS ||
923 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
925 * Delay EAPOL frame transmission until a
926 * possible WPS STA initiates the handshake
927 * with EAPOL-Start. Only allow the wait to be
928 * skipped if the STA is known to support WPS
931 wpa_printf(MSG_DEBUG, "WPS: Do not start "
932 "EAPOL until EAPOL-Start is "
934 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
937 #endif /* CONFIG_WPS */
939 sta->eapol_sm->eap_if->portEnabled = TRUE;
942 /* since we support version 1, we can ignore version field and proceed
943 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
944 /* TODO: actually, we are not version 1 anymore.. However, Version 2
945 * does not change frame contents, so should be ok to process frames
946 * more or less identically. Some changes might be needed for
947 * verification of fields. */
950 case IEEE802_1X_TYPE_EAP_PACKET:
951 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
954 case IEEE802_1X_TYPE_EAPOL_START:
955 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
956 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
958 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
959 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
961 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
962 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
963 "available - ignore it since "
964 "STA sent EAPOL-Start");
965 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
967 sta->eapol_sm->eapolStart = TRUE;
968 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
969 eap_server_clear_identity(sta->eapol_sm->eap);
970 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
973 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
974 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
975 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
977 sta->acct_terminate_cause =
978 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
979 accounting_sta_stop(hapd, sta);
980 sta->eapol_sm->eapolLogoff = TRUE;
981 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
982 eap_server_clear_identity(sta->eapol_sm->eap);
985 case IEEE802_1X_TYPE_EAPOL_KEY:
986 wpa_printf(MSG_DEBUG, " EAPOL-Key");
987 if (!ap_sta_is_authorized(sta)) {
988 wpa_printf(MSG_DEBUG, " Dropped key data from "
989 "unauthorized Supplicant");
994 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
995 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
996 /* TODO: implement support for this; show data */
1000 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1001 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1005 eapol_auth_step(sta->eapol_sm);
1010 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1011 * @hapd: hostapd BSS data
1014 * This function is called to start IEEE 802.1X authentication when a new
1015 * station completes IEEE 802.11 association.
1017 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1019 struct rsn_pmksa_cache_entry *pmksa;
1025 if (hapd->conf->wps_state &&
1026 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1027 (sta->flags & WLAN_STA_WPS))) {
1029 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1030 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1031 * authentication in this BSS.
1035 #endif /* CONFIG_WPS */
1037 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1038 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1039 "802.1X not enabled or forced for WPS");
1041 * Clear any possible EAPOL authenticator state to support
1042 * reassociation change from WPS to PSK.
1044 ieee802_1x_free_station(sta);
1048 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1049 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1050 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1052 * Clear any possible EAPOL authenticator state to support
1053 * reassociation change from WPA-EAP to PSK.
1055 ieee802_1x_free_station(sta);
1059 if (sta->eapol_sm == NULL) {
1060 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1061 HOSTAPD_LEVEL_DEBUG, "start authentication");
1062 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1063 if (sta->eapol_sm == NULL) {
1064 hostapd_logger(hapd, sta->addr,
1065 HOSTAPD_MODULE_IEEE8021X,
1067 "failed to allocate state machine");
1074 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1075 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1076 !(sta->flags & WLAN_STA_WPS2)) {
1078 * Delay EAPOL frame transmission until a possible WPS STA
1079 * initiates the handshake with EAPOL-Start. Only allow the
1080 * wait to be skipped if the STA is known to support WPS 2.0.
1082 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1083 "EAPOL-Start is received");
1084 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1086 #endif /* CONFIG_WPS */
1088 sta->eapol_sm->eap_if->portEnabled = TRUE;
1090 #ifdef CONFIG_IEEE80211R
1091 if (sta->auth_alg == WLAN_AUTH_FT) {
1092 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1093 HOSTAPD_LEVEL_DEBUG,
1094 "PMK from FT - skip IEEE 802.1X/EAP");
1095 /* Setup EAPOL state machines to already authenticated state
1096 * because of existing FT information from R0KH. */
1097 sta->eapol_sm->keyRun = TRUE;
1098 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1099 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1100 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1101 sta->eapol_sm->authSuccess = TRUE;
1102 sta->eapol_sm->authFail = FALSE;
1103 if (sta->eapol_sm->eap)
1104 eap_sm_notify_cached(sta->eapol_sm->eap);
1105 /* TODO: get vlan_id from R0KH using RRB message */
1108 #endif /* CONFIG_IEEE80211R */
1110 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1112 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1113 HOSTAPD_LEVEL_DEBUG,
1114 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1115 /* Setup EAPOL state machines to already authenticated state
1116 * because of existing PMKSA information in the cache. */
1117 sta->eapol_sm->keyRun = TRUE;
1118 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1119 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1120 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1121 sta->eapol_sm->authSuccess = TRUE;
1122 sta->eapol_sm->authFail = FALSE;
1123 if (sta->eapol_sm->eap)
1124 eap_sm_notify_cached(sta->eapol_sm->eap);
1125 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
1126 ap_sta_bind_vlan(hapd, sta);
1130 * Force EAPOL state machines to start
1131 * re-authentication without having to wait for the
1132 * Supplicant to send EAPOL-Start.
1134 sta->eapol_sm->reAuthenticate = TRUE;
1136 eapol_auth_step(sta->eapol_sm);
1141 void ieee802_1x_free_station(struct sta_info *sta)
1143 struct eapol_state_machine *sm = sta->eapol_sm;
1148 sta->eapol_sm = NULL;
1150 #ifndef CONFIG_NO_RADIUS
1151 radius_msg_free(sm->last_recv_radius);
1152 radius_free_class(&sm->radius_class);
1153 wpabuf_free(sm->radius_cui);
1154 #endif /* CONFIG_NO_RADIUS */
1156 os_free(sm->identity);
1157 eapol_auth_free(sm);
1161 #ifndef CONFIG_NO_RADIUS
1162 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1163 struct sta_info *sta)
1166 const struct eap_hdr *hdr;
1169 struct radius_msg *msg;
1170 struct eapol_state_machine *sm = sta->eapol_sm;
1172 if (sm == NULL || sm->last_recv_radius == NULL) {
1174 sm->eap_if->aaaEapNoReq = TRUE;
1178 msg = sm->last_recv_radius;
1180 eap = radius_msg_get_eap(msg);
1182 /* RFC 3579, Chap. 2.6.3:
1183 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1185 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1186 HOSTAPD_LEVEL_WARNING, "could not extract "
1187 "EAP-Message from RADIUS message");
1188 sm->eap_if->aaaEapNoReq = TRUE;
1192 if (wpabuf_len(eap) < sizeof(*hdr)) {
1193 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1194 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1195 "received from authentication server");
1197 sm->eap_if->aaaEapNoReq = TRUE;
1201 if (wpabuf_len(eap) > sizeof(*hdr))
1202 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1204 hdr = wpabuf_head(eap);
1205 switch (hdr->code) {
1206 case EAP_CODE_REQUEST:
1208 sm->eap_type_authsrv = eap_type;
1209 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1210 eap_server_get_name(0, eap_type), eap_type);
1212 case EAP_CODE_RESPONSE:
1213 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1214 eap_server_get_name(0, eap_type), eap_type);
1216 case EAP_CODE_SUCCESS:
1217 os_strlcpy(buf, "EAP Success", sizeof(buf));
1219 case EAP_CODE_FAILURE:
1220 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1223 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1226 buf[sizeof(buf) - 1] = '\0';
1227 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1228 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1229 "id=%d len=%d) from RADIUS server: %s",
1230 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1232 sm->eap_if->aaaEapReq = TRUE;
1234 wpabuf_free(sm->eap_if->aaaEapReqData);
1235 sm->eap_if->aaaEapReqData = eap;
1239 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1240 struct sta_info *sta, struct radius_msg *msg,
1241 struct radius_msg *req,
1242 const u8 *shared_secret,
1243 size_t shared_secret_len)
1245 struct radius_ms_mppe_keys *keys;
1246 struct eapol_state_machine *sm = sta->eapol_sm;
1250 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1253 if (keys && keys->send && keys->recv) {
1254 size_t len = keys->send_len + keys->recv_len;
1255 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1256 keys->send, keys->send_len);
1257 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1258 keys->recv, keys->recv_len);
1260 os_free(sm->eap_if->aaaEapKeyData);
1261 sm->eap_if->aaaEapKeyData = os_malloc(len);
1262 if (sm->eap_if->aaaEapKeyData) {
1263 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1265 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1266 keys->send, keys->send_len);
1267 sm->eap_if->aaaEapKeyDataLen = len;
1268 sm->eap_if->aaaEapKeyAvailable = TRUE;
1271 wpa_printf(MSG_DEBUG,
1272 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1273 keys, keys ? keys->send : NULL,
1274 keys ? keys->recv : NULL);
1278 os_free(keys->send);
1279 os_free(keys->recv);
1285 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1286 struct sta_info *sta,
1287 struct radius_msg *msg)
1291 struct eapol_state_machine *sm = sta->eapol_sm;
1293 struct radius_attr_data *nclass;
1294 size_t nclass_count;
1296 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1300 radius_free_class(&sm->radius_class);
1301 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1305 nclass = os_calloc(count, sizeof(struct radius_attr_data));
1312 for (i = 0; i < count; i++) {
1314 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1315 &attr_class, &class_len,
1320 } while (class_len < 1);
1322 nclass[nclass_count].data = os_malloc(class_len);
1323 if (nclass[nclass_count].data == NULL)
1326 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
1327 nclass[nclass_count].len = class_len;
1331 sm->radius_class.attr = nclass;
1332 sm->radius_class.count = nclass_count;
1333 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1334 "attributes for " MACSTR,
1335 (unsigned long) sm->radius_class.count,
1336 MAC2STR(sta->addr));
1340 /* Update sta->identity based on User-Name attribute in Access-Accept */
1341 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1342 struct sta_info *sta,
1343 struct radius_msg *msg)
1347 struct eapol_state_machine *sm = sta->eapol_sm;
1352 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1356 identity = (u8 *) dup_binstr(buf, len);
1357 if (identity == NULL)
1360 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1361 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1362 "User-Name from Access-Accept '%s'",
1363 sm->identity ? (char *) sm->identity : "N/A",
1366 os_free(sm->identity);
1367 sm->identity = identity;
1368 sm->identity_len = len;
1372 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1373 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1374 struct sta_info *sta,
1375 struct radius_msg *msg)
1377 struct eapol_state_machine *sm = sta->eapol_sm;
1385 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1386 &buf, &len, NULL) < 0)
1389 cui = wpabuf_alloc_copy(buf, len);
1393 wpabuf_free(sm->radius_cui);
1394 sm->radius_cui = cui;
1400 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1402 sta->remediation = 1;
1403 os_free(sta->remediation_url);
1405 sta->remediation_url = os_malloc(len);
1406 if (!sta->remediation_url)
1408 sta->remediation_method = pos[0];
1409 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1410 sta->remediation_url[len - 1] = '\0';
1411 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1412 "for " MACSTR " - server method %u URL %s",
1413 MAC2STR(sta->addr), sta->remediation_method,
1414 sta->remediation_url);
1416 sta->remediation_url = NULL;
1417 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1418 "for " MACSTR, MAC2STR(sta->addr));
1420 /* TODO: assign the STA into remediation VLAN or add filtering */
1424 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1425 struct sta_info *sta, u8 *pos,
1429 return; /* Malformed information */
1430 sta->hs20_deauth_requested = 1;
1431 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1433 *pos, WPA_GET_LE16(pos + 1));
1434 wpabuf_free(sta->hs20_deauth_req);
1435 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1436 if (sta->hs20_deauth_req) {
1437 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1438 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1439 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1441 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1445 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1446 struct sta_info *sta, u8 *pos,
1447 size_t len, int session_timeout)
1450 int warning_time, beacon_int;
1453 return; /* Malformed information */
1454 os_free(sta->hs20_session_info_url);
1455 sta->hs20_session_info_url = os_malloc(len);
1456 if (sta->hs20_session_info_url == NULL)
1459 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1460 sta->hs20_session_info_url[len - 1] = '\0';
1461 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1462 "(session_timeout=%d)",
1463 sta->hs20_session_info_url, swt, session_timeout);
1464 if (session_timeout < 0) {
1465 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1469 swt = 1; /* Use one minute as the AP selected value */
1471 if ((unsigned int) session_timeout < swt * 60)
1474 warning_time = session_timeout - swt * 60;
1476 beacon_int = hapd->iconf->beacon_int;
1478 beacon_int = 100; /* best guess */
1479 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1480 if (sta->hs20_disassoc_timer > 65535)
1481 sta->hs20_disassoc_timer = 65535;
1483 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1486 #endif /* CONFIG_HS20 */
1489 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1490 struct sta_info *sta,
1491 struct radius_msg *msg,
1492 int session_timeout)
1495 u8 *buf, *pos, *end, type, sublen;
1499 sta->remediation = 0;
1500 sta->hs20_deauth_requested = 0;
1503 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1504 &buf, &len, buf) < 0)
1510 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1517 continue; /* invalid length */
1518 sublen -= 2; /* skip header */
1519 if (pos + sublen > end)
1520 continue; /* invalid WFA VSA */
1523 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1524 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1526 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1527 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1529 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1530 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1535 #endif /* CONFIG_HS20 */
1539 struct sta_id_search {
1541 struct eapol_state_machine *sm;
1545 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1546 struct sta_info *sta,
1549 struct sta_id_search *id_search = ctx;
1550 struct eapol_state_machine *sm = sta->eapol_sm;
1552 if (sm && sm->radius_identifier >= 0 &&
1553 sm->radius_identifier == id_search->identifier) {
1561 static struct eapol_state_machine *
1562 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1564 struct sta_id_search id_search;
1565 id_search.identifier = identifier;
1566 id_search.sm = NULL;
1567 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1568 return id_search.sm;
1573 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1574 * @msg: RADIUS response message
1575 * @req: RADIUS request message
1576 * @shared_secret: RADIUS shared secret
1577 * @shared_secret_len: Length of shared_secret in octets
1578 * @data: Context data (struct hostapd_data *)
1579 * Returns: Processing status
1581 static RadiusRxResult
1582 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1583 const u8 *shared_secret, size_t shared_secret_len,
1586 struct hostapd_data *hapd = data;
1587 struct sta_info *sta;
1588 u32 session_timeout = 0, termination_action, acct_interim_interval;
1589 int session_timeout_set, vlan_id = 0;
1590 struct eapol_state_machine *sm;
1591 int override_eapReq = 0;
1592 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1594 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1596 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1597 "station for this RADIUS message");
1598 return RADIUS_RX_UNKNOWN;
1602 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1603 * present when packet contains an EAP-Message attribute */
1604 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1605 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1607 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1608 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1609 "Message-Authenticator since it does not include "
1611 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1613 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1614 return RADIUS_RX_INVALID_AUTHENTICATOR;
1617 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1618 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1619 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1620 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1621 return RADIUS_RX_UNKNOWN;
1624 sm->radius_identifier = -1;
1625 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1626 MAC2STR(sta->addr));
1628 radius_msg_free(sm->last_recv_radius);
1629 sm->last_recv_radius = msg;
1631 session_timeout_set =
1632 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1634 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1635 &termination_action))
1636 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1638 if (hapd->conf->acct_interim_interval == 0 &&
1639 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1640 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1641 &acct_interim_interval) == 0) {
1642 if (acct_interim_interval < 60) {
1643 hostapd_logger(hapd, sta->addr,
1644 HOSTAPD_MODULE_IEEE8021X,
1646 "ignored too small "
1647 "Acct-Interim-Interval %d",
1648 acct_interim_interval);
1650 sta->acct_interim_interval = acct_interim_interval;
1654 switch (hdr->code) {
1655 case RADIUS_CODE_ACCESS_ACCEPT:
1656 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1658 #ifndef CONFIG_NO_VLAN
1660 vlan_id = radius_msg_get_vlanid(msg);
1662 hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
1663 hostapd_logger(hapd, sta->addr,
1664 HOSTAPD_MODULE_RADIUS,
1666 "VLAN ID %d", vlan_id);
1667 } else if (vlan_id > 0) {
1668 sta->eapol_sm->authFail = TRUE;
1669 hostapd_logger(hapd, sta->addr,
1670 HOSTAPD_MODULE_RADIUS,
1672 "Invalid VLAN ID %d received from RADIUS server",
1675 } else if (hapd->conf->ssid.dynamic_vlan ==
1676 DYNAMIC_VLAN_REQUIRED) {
1677 sta->eapol_sm->authFail = TRUE;
1678 hostapd_logger(hapd, sta->addr,
1679 HOSTAPD_MODULE_IEEE8021X,
1680 HOSTAPD_LEVEL_INFO, "authentication "
1681 "server did not include required VLAN "
1682 "ID in Access-Accept");
1685 #endif /* CONFIG_NO_VLAN */
1687 sta->vlan_id = vlan_id;
1688 if ((sta->flags & WLAN_STA_ASSOC) &&
1689 ap_sta_bind_vlan(hapd, sta) < 0)
1692 sta->session_timeout_set = !!session_timeout_set;
1693 sta->session_timeout = session_timeout;
1695 /* RFC 3580, Ch. 3.17 */
1696 if (session_timeout_set && termination_action ==
1697 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1698 sm->reAuthPeriod = session_timeout;
1699 } else if (session_timeout_set)
1700 ap_sta_session_timeout(hapd, sta, session_timeout);
1702 sm->eap_if->aaaSuccess = TRUE;
1703 override_eapReq = 1;
1704 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1706 ieee802_1x_store_radius_class(hapd, sta, msg);
1707 ieee802_1x_update_sta_identity(hapd, sta, msg);
1708 ieee802_1x_update_sta_cui(hapd, sta, msg);
1709 ieee802_1x_check_hs20(hapd, sta, msg,
1710 session_timeout_set ?
1711 (int) session_timeout : -1);
1712 if (sm->eap_if->eapKeyAvailable && !sta->remediation &&
1713 !sta->hs20_deauth_requested &&
1714 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1715 session_timeout_set ?
1716 (int) session_timeout : -1, sm) == 0) {
1717 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1718 HOSTAPD_LEVEL_DEBUG,
1719 "Added PMKSA cache entry");
1722 case RADIUS_CODE_ACCESS_REJECT:
1723 sm->eap_if->aaaFail = TRUE;
1724 override_eapReq = 1;
1726 case RADIUS_CODE_ACCESS_CHALLENGE:
1727 sm->eap_if->aaaEapReq = TRUE;
1728 if (session_timeout_set) {
1729 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1730 sm->eap_if->aaaMethodTimeout = session_timeout;
1731 hostapd_logger(hapd, sm->addr,
1732 HOSTAPD_MODULE_IEEE8021X,
1733 HOSTAPD_LEVEL_DEBUG,
1734 "using EAP timeout of %d seconds (from "
1736 sm->eap_if->aaaMethodTimeout);
1739 * Use dynamic retransmission behavior per EAP
1742 sm->eap_if->aaaMethodTimeout = 0;
1747 ieee802_1x_decapsulate_radius(hapd, sta);
1748 if (override_eapReq)
1749 sm->eap_if->aaaEapReq = FALSE;
1751 eapol_auth_step(sm);
1753 return RADIUS_RX_QUEUED;
1755 #endif /* CONFIG_NO_RADIUS */
1758 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1760 struct eapol_state_machine *sm = sta->eapol_sm;
1764 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1765 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1767 #ifndef CONFIG_NO_RADIUS
1768 radius_msg_free(sm->last_recv_radius);
1769 sm->last_recv_radius = NULL;
1770 #endif /* CONFIG_NO_RADIUS */
1772 if (sm->eap_if->eapTimeout) {
1774 * Disconnect the STA since it did not reply to the last EAP
1775 * request and we cannot continue EAP processing (EAP-Failure
1776 * could only be sent if the EAP peer actually replied).
1778 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1779 MAC2STR(sta->addr));
1781 sm->eap_if->portEnabled = FALSE;
1782 ap_sta_disconnect(hapd, sta, sta->addr,
1783 WLAN_REASON_PREV_AUTH_NOT_VALID);
1788 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1790 struct eapol_authenticator *eapol = hapd->eapol_auth;
1792 if (hapd->conf->default_wep_key_len < 1)
1795 os_free(eapol->default_wep_key);
1796 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1797 if (eapol->default_wep_key == NULL ||
1798 random_get_bytes(eapol->default_wep_key,
1799 hapd->conf->default_wep_key_len)) {
1800 wpa_printf(MSG_INFO, "Could not generate random WEP key");
1801 os_free(eapol->default_wep_key);
1802 eapol->default_wep_key = NULL;
1806 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1807 eapol->default_wep_key,
1808 hapd->conf->default_wep_key_len);
1814 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1815 struct sta_info *sta, void *ctx)
1817 if (sta->eapol_sm) {
1818 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1819 eapol_auth_step(sta->eapol_sm);
1825 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1827 struct hostapd_data *hapd = eloop_ctx;
1828 struct eapol_authenticator *eapol = hapd->eapol_auth;
1830 if (eapol->default_wep_key_idx >= 3)
1831 eapol->default_wep_key_idx =
1832 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1834 eapol->default_wep_key_idx++;
1836 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1837 eapol->default_wep_key_idx);
1839 if (ieee802_1x_rekey_broadcast(hapd)) {
1840 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1841 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1842 "new broadcast key");
1843 os_free(eapol->default_wep_key);
1844 eapol->default_wep_key = NULL;
1848 /* TODO: Could setup key for RX here, but change default TX keyid only
1849 * after new broadcast key has been sent to all stations. */
1850 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1851 broadcast_ether_addr,
1852 eapol->default_wep_key_idx, 1, NULL, 0,
1853 eapol->default_wep_key,
1854 hapd->conf->default_wep_key_len)) {
1855 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1856 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1857 "new broadcast key");
1858 os_free(eapol->default_wep_key);
1859 eapol->default_wep_key = NULL;
1863 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1865 if (hapd->conf->wep_rekeying_period > 0) {
1866 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1867 ieee802_1x_rekey, hapd, NULL);
1872 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1873 const u8 *data, size_t datalen)
1876 struct sta_info *sta = sta_ctx;
1878 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1879 WLAN_STA_MAYBE_WPS) {
1881 size_t identity_len;
1882 struct eapol_state_machine *sm = sta->eapol_sm;
1884 identity = eap_get_identity(sm->eap, &identity_len);
1886 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1887 os_memcmp(identity, WSC_ID_ENROLLEE,
1888 WSC_ID_ENROLLEE_LEN) == 0) ||
1889 (identity_len == WSC_ID_REGISTRAR_LEN &&
1890 os_memcmp(identity, WSC_ID_REGISTRAR,
1891 WSC_ID_REGISTRAR_LEN) == 0))) {
1892 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1894 sta->flags |= WLAN_STA_WPS;
1897 #endif /* CONFIG_WPS */
1899 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1903 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1904 const u8 *data, size_t datalen)
1906 #ifndef CONFIG_NO_RADIUS
1907 struct hostapd_data *hapd = ctx;
1908 struct sta_info *sta = sta_ctx;
1910 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1911 #endif /* CONFIG_NO_RADIUS */
1915 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1916 int preauth, int remediation)
1918 struct hostapd_data *hapd = ctx;
1919 struct sta_info *sta = sta_ctx;
1921 rsn_preauth_finished(hapd, sta, success);
1923 ieee802_1x_finished(hapd, sta, success, remediation);
1927 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1928 size_t identity_len, int phase2,
1929 struct eap_user *user)
1931 struct hostapd_data *hapd = ctx;
1932 const struct hostapd_eap_user *eap_user;
1936 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
1937 if (eap_user == NULL)
1940 os_memset(user, 0, sizeof(*user));
1941 user->phase2 = phase2;
1942 for (i = 0; i < EAP_MAX_METHODS; i++) {
1943 user->methods[i].vendor = eap_user->methods[i].vendor;
1944 user->methods[i].method = eap_user->methods[i].method;
1947 if (eap_user->password) {
1948 user->password = os_malloc(eap_user->password_len);
1949 if (user->password == NULL)
1951 os_memcpy(user->password, eap_user->password,
1952 eap_user->password_len);
1953 user->password_len = eap_user->password_len;
1954 user->password_hash = eap_user->password_hash;
1956 user->force_version = eap_user->force_version;
1957 user->macacl = eap_user->macacl;
1958 user->ttls_auth = eap_user->ttls_auth;
1959 user->remediation = eap_user->remediation;
1964 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
1970 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1972 struct hostapd_data *hapd = ctx;
1973 struct sta_info *sta;
1974 sta = ap_get_sta(hapd, addr);
1975 if (sta == NULL || sta->eapol_sm == NULL)
1981 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1982 eapol_logger_level level, const char *txt)
1984 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1985 struct hostapd_data *hapd = ctx;
1989 case EAPOL_LOGGER_WARNING:
1990 hlevel = HOSTAPD_LEVEL_WARNING;
1992 case EAPOL_LOGGER_INFO:
1993 hlevel = HOSTAPD_LEVEL_INFO;
1995 case EAPOL_LOGGER_DEBUG:
1997 hlevel = HOSTAPD_LEVEL_DEBUG;
2001 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2003 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
2007 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2010 struct hostapd_data *hapd = ctx;
2011 struct sta_info *sta = sta_ctx;
2012 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2016 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2018 struct hostapd_data *hapd = ctx;
2019 struct sta_info *sta = sta_ctx;
2020 ieee802_1x_abort_auth(hapd, sta);
2024 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2026 struct hostapd_data *hapd = ctx;
2027 struct sta_info *sta = sta_ctx;
2028 ieee802_1x_tx_key(hapd, sta);
2032 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2033 enum eapol_event type)
2035 /* struct hostapd_data *hapd = ctx; */
2036 struct sta_info *sta = sta_ctx;
2038 case EAPOL_AUTH_SM_CHANGE:
2039 wpa_auth_sm_notify(sta->wpa_sm);
2041 case EAPOL_AUTH_REAUTHENTICATE:
2042 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2050 static struct eap_server_erp_key *
2051 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2053 struct hostapd_data *hapd = ctx;
2054 struct eap_server_erp_key *erp;
2056 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2058 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2066 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2068 struct hostapd_data *hapd = ctx;
2070 dl_list_add(&hapd->erp_keys, &erp->list);
2074 #endif /* CONFIG_ERP */
2077 int ieee802_1x_init(struct hostapd_data *hapd)
2080 struct eapol_auth_config conf;
2081 struct eapol_auth_cb cb;
2083 dl_list_init(&hapd->erp_keys);
2085 os_memset(&conf, 0, sizeof(conf));
2087 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2088 conf.wpa = hapd->conf->wpa;
2089 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2090 conf.eap_server = hapd->conf->eap_server;
2091 conf.ssl_ctx = hapd->ssl_ctx;
2092 conf.msg_ctx = hapd->msg_ctx;
2093 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2094 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2095 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2096 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2097 conf.erp_domain = hapd->conf->erp_domain;
2098 conf.erp = hapd->conf->eap_server_erp;
2099 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2100 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2101 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2102 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2103 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2104 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2105 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2106 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2107 conf.tnc = hapd->conf->tnc;
2108 conf.wps = hapd->wps;
2109 conf.fragment_size = hapd->conf->fragment_size;
2110 conf.pwd_group = hapd->conf->pwd_group;
2111 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2112 if (hapd->conf->server_id) {
2113 conf.server_id = (const u8 *) hapd->conf->server_id;
2114 conf.server_id_len = os_strlen(hapd->conf->server_id);
2116 conf.server_id = (const u8 *) "hostapd";
2117 conf.server_id_len = 7;
2120 os_memset(&cb, 0, sizeof(cb));
2121 cb.eapol_send = ieee802_1x_eapol_send;
2122 cb.aaa_send = ieee802_1x_aaa_send;
2123 cb.finished = _ieee802_1x_finished;
2124 cb.get_eap_user = ieee802_1x_get_eap_user;
2125 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2126 cb.logger = ieee802_1x_logger;
2127 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2128 cb.abort_auth = _ieee802_1x_abort_auth;
2129 cb.tx_key = _ieee802_1x_tx_key;
2130 cb.eapol_event = ieee802_1x_eapol_event;
2132 cb.erp_get_key = ieee802_1x_erp_get_key;
2133 cb.erp_add_key = ieee802_1x_erp_add_key;
2134 #endif /* CONFIG_ERP */
2136 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2137 if (hapd->eapol_auth == NULL)
2140 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2141 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2144 #ifndef CONFIG_NO_RADIUS
2145 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2146 ieee802_1x_receive_auth, hapd))
2148 #endif /* CONFIG_NO_RADIUS */
2150 if (hapd->conf->default_wep_key_len) {
2151 for (i = 0; i < 4; i++)
2152 hostapd_drv_set_key(hapd->conf->iface, hapd,
2153 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2156 ieee802_1x_rekey(hapd, NULL);
2158 if (hapd->eapol_auth->default_wep_key == NULL)
2166 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2168 struct eap_server_erp_key *erp;
2170 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2172 dl_list_del(&erp->list);
2173 bin_clear_free(erp, sizeof(*erp));
2178 void ieee802_1x_deinit(struct hostapd_data *hapd)
2180 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2182 if (hapd->driver != NULL &&
2183 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2184 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2186 eapol_auth_deinit(hapd->eapol_auth);
2187 hapd->eapol_auth = NULL;
2189 ieee802_1x_erp_flush(hapd);
2193 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2194 const u8 *buf, size_t len, int ack)
2196 struct ieee80211_hdr *hdr;
2198 const unsigned char rfc1042_hdr[ETH_ALEN] =
2199 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2203 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2206 hdr = (struct ieee80211_hdr *) buf;
2207 pos = (u8 *) (hdr + 1);
2208 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2210 pos += sizeof(rfc1042_hdr);
2211 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2215 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2220 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2221 const u8 *buf, int len, int ack)
2223 const struct ieee802_1x_hdr *xhdr =
2224 (const struct ieee802_1x_hdr *) buf;
2225 const u8 *pos = buf + sizeof(*xhdr);
2226 struct ieee802_1x_eapol_key *key;
2228 if (len < (int) sizeof(*xhdr))
2230 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2231 "type=%d length=%d - ack=%d",
2232 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2233 be_to_host16(xhdr->length), ack);
2235 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2238 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2239 const struct wpa_eapol_key *wpa;
2240 wpa = (const struct wpa_eapol_key *) pos;
2241 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2242 wpa->type == EAPOL_KEY_TYPE_WPA)
2243 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2247 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2248 * or Authenticator state machines, but EAPOL-Key packets are not
2249 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2250 * packets couple of times because otherwise STA keys become
2251 * unsynchronized with AP. */
2252 if (!ack && pos + sizeof(*key) <= buf + len) {
2253 key = (struct ieee802_1x_eapol_key *) pos;
2254 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2255 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2256 "frame (%scast index=%d)",
2257 key->key_index & BIT(7) ? "uni" : "broad",
2258 key->key_index & ~BIT(7));
2259 /* TODO: re-send EAPOL-Key couple of times (with short delay
2260 * between them?). If all attempt fail, report error and
2261 * deauthenticate STA so that it will get new keys when
2262 * authenticating again (e.g., after returning in range).
2263 * Separate limit/transmit state needed both for unicast and
2264 * broadcast keys(?) */
2266 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2267 * to here and change the key only if the EAPOL-Key packet was Acked.
2274 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2276 if (sm == NULL || sm->identity == NULL)
2279 *len = sm->identity_len;
2280 return sm->identity;
2284 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2287 if (sm == NULL || sm->radius_class.attr == NULL ||
2288 idx >= (int) sm->radius_class.count)
2291 *len = sm->radius_class.attr[idx].len;
2292 return sm->radius_class.attr[idx].data;
2296 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2300 return sm->radius_cui;
2304 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2310 *len = sm->eap_if->eapKeyDataLen;
2311 return sm->eap_if->eapKeyData;
2315 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2320 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2321 eapol_auth_step(sm);
2325 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2330 sm->portValid = valid ? TRUE : FALSE;
2331 eapol_auth_step(sm);
2335 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2340 sm->flags |= EAPOL_SM_PREAUTH;
2342 sm->flags &= ~EAPOL_SM_PREAUTH;
2346 static const char * bool_txt(Boolean val)
2348 return val ? "TRUE" : "FALSE";
2352 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2359 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2360 char *buf, size_t buflen)
2363 struct eapol_state_machine *sm = sta->eapol_sm;
2364 struct os_reltime diff;
2371 ret = os_snprintf(buf + len, buflen - len,
2372 "dot1xPaePortNumber=%d\n"
2373 "dot1xPaePortProtocolVersion=%d\n"
2374 "dot1xPaePortCapabilities=1\n"
2375 "dot1xPaePortInitialize=%d\n"
2376 "dot1xPaePortReauthenticate=FALSE\n",
2380 if (os_snprintf_error(buflen - len, ret))
2384 /* dot1xAuthConfigTable */
2385 ret = os_snprintf(buf + len, buflen - len,
2386 "dot1xAuthPaeState=%d\n"
2387 "dot1xAuthBackendAuthState=%d\n"
2388 "dot1xAuthAdminControlledDirections=%d\n"
2389 "dot1xAuthOperControlledDirections=%d\n"
2390 "dot1xAuthAuthControlledPortStatus=%d\n"
2391 "dot1xAuthAuthControlledPortControl=%d\n"
2392 "dot1xAuthQuietPeriod=%u\n"
2393 "dot1xAuthServerTimeout=%u\n"
2394 "dot1xAuthReAuthPeriod=%u\n"
2395 "dot1xAuthReAuthEnabled=%s\n"
2396 "dot1xAuthKeyTxEnabled=%s\n",
2397 sm->auth_pae_state + 1,
2398 sm->be_auth_state + 1,
2399 sm->adminControlledDirections,
2400 sm->operControlledDirections,
2406 bool_txt(sm->reAuthEnabled),
2407 bool_txt(sm->keyTxEnabled));
2408 if (os_snprintf_error(buflen - len, ret))
2412 /* dot1xAuthStatsTable */
2413 ret = os_snprintf(buf + len, buflen - len,
2414 "dot1xAuthEapolFramesRx=%u\n"
2415 "dot1xAuthEapolFramesTx=%u\n"
2416 "dot1xAuthEapolStartFramesRx=%u\n"
2417 "dot1xAuthEapolLogoffFramesRx=%u\n"
2418 "dot1xAuthEapolRespIdFramesRx=%u\n"
2419 "dot1xAuthEapolRespFramesRx=%u\n"
2420 "dot1xAuthEapolReqIdFramesTx=%u\n"
2421 "dot1xAuthEapolReqFramesTx=%u\n"
2422 "dot1xAuthInvalidEapolFramesRx=%u\n"
2423 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2424 "dot1xAuthLastEapolFrameVersion=%u\n"
2425 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2426 sm->dot1xAuthEapolFramesRx,
2427 sm->dot1xAuthEapolFramesTx,
2428 sm->dot1xAuthEapolStartFramesRx,
2429 sm->dot1xAuthEapolLogoffFramesRx,
2430 sm->dot1xAuthEapolRespIdFramesRx,
2431 sm->dot1xAuthEapolRespFramesRx,
2432 sm->dot1xAuthEapolReqIdFramesTx,
2433 sm->dot1xAuthEapolReqFramesTx,
2434 sm->dot1xAuthInvalidEapolFramesRx,
2435 sm->dot1xAuthEapLengthErrorFramesRx,
2436 sm->dot1xAuthLastEapolFrameVersion,
2438 if (os_snprintf_error(buflen - len, ret))
2442 /* dot1xAuthDiagTable */
2443 ret = os_snprintf(buf + len, buflen - len,
2444 "dot1xAuthEntersConnecting=%u\n"
2445 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2446 "dot1xAuthEntersAuthenticating=%u\n"
2447 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2448 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2449 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2450 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2451 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2452 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2453 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2454 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2455 "dot1xAuthBackendResponses=%u\n"
2456 "dot1xAuthBackendAccessChallenges=%u\n"
2457 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2458 "dot1xAuthBackendAuthSuccesses=%u\n"
2459 "dot1xAuthBackendAuthFails=%u\n",
2460 sm->authEntersConnecting,
2461 sm->authEapLogoffsWhileConnecting,
2462 sm->authEntersAuthenticating,
2463 sm->authAuthSuccessesWhileAuthenticating,
2464 sm->authAuthTimeoutsWhileAuthenticating,
2465 sm->authAuthFailWhileAuthenticating,
2466 sm->authAuthEapStartsWhileAuthenticating,
2467 sm->authAuthEapLogoffWhileAuthenticating,
2468 sm->authAuthReauthsWhileAuthenticated,
2469 sm->authAuthEapStartsWhileAuthenticated,
2470 sm->authAuthEapLogoffWhileAuthenticated,
2471 sm->backendResponses,
2472 sm->backendAccessChallenges,
2473 sm->backendOtherRequestsToSupplicant,
2474 sm->backendAuthSuccesses,
2475 sm->backendAuthFails);
2476 if (os_snprintf_error(buflen - len, ret))
2480 /* dot1xAuthSessionStatsTable */
2481 os_reltime_age(&sta->acct_session_start, &diff);
2482 ret = os_snprintf(buf + len, buflen - len,
2483 /* TODO: dot1xAuthSessionOctetsRx */
2484 /* TODO: dot1xAuthSessionOctetsTx */
2485 /* TODO: dot1xAuthSessionFramesRx */
2486 /* TODO: dot1xAuthSessionFramesTx */
2487 "dot1xAuthSessionId=%08X-%08X\n"
2488 "dot1xAuthSessionAuthenticMethod=%d\n"
2489 "dot1xAuthSessionTime=%u\n"
2490 "dot1xAuthSessionTerminateCause=999\n"
2491 "dot1xAuthSessionUserName=%s\n",
2492 sta->acct_session_id_hi, sta->acct_session_id_lo,
2493 (wpa_key_mgmt_wpa_ieee8021x(
2494 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2496 (unsigned int) diff.sec,
2498 if (os_snprintf_error(buflen - len, ret))
2502 if (sm->acct_multi_session_id_hi) {
2503 ret = os_snprintf(buf + len, buflen - len,
2504 "authMultiSessionId=%08X+%08X\n",
2505 sm->acct_multi_session_id_hi,
2506 sm->acct_multi_session_id_lo);
2507 if (os_snprintf_error(buflen - len, ret))
2512 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2513 name2 = eap_server_get_name(0, sm->eap_type_supp);
2514 ret = os_snprintf(buf + len, buflen - len,
2515 "last_eap_type_as=%d (%s)\n"
2516 "last_eap_type_sta=%d (%s)\n",
2517 sm->eap_type_authsrv, name1,
2518 sm->eap_type_supp, name2);
2519 if (os_snprintf_error(buflen - len, ret))
2527 static void ieee802_1x_finished(struct hostapd_data *hapd,
2528 struct sta_info *sta, int success,
2533 /* TODO: get PMKLifetime from WPA parameters */
2534 static const int dot11RSNAConfigPMKLifetime = 43200;
2535 unsigned int session_timeout;
2538 if (remediation && !sta->remediation) {
2539 sta->remediation = 1;
2540 os_free(sta->remediation_url);
2541 sta->remediation_url =
2542 os_strdup(hapd->conf->subscr_remediation_url);
2543 sta->remediation_method = 1; /* SOAP-XML SPP */
2547 if (sta->remediation) {
2548 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2549 "to " MACSTR " to indicate Subscription "
2551 MAC2STR(sta->addr));
2552 hs20_send_wnm_notification(hapd, sta->addr,
2553 sta->remediation_method,
2554 sta->remediation_url);
2555 os_free(sta->remediation_url);
2556 sta->remediation_url = NULL;
2559 if (sta->hs20_deauth_req) {
2560 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2561 "to " MACSTR " to indicate imminent "
2562 "deauthentication", MAC2STR(sta->addr));
2563 hs20_send_wnm_notification_deauth_req(
2564 hapd, sta->addr, sta->hs20_deauth_req);
2567 #endif /* CONFIG_HS20 */
2569 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2570 if (sta->session_timeout_set)
2571 session_timeout = sta->session_timeout;
2573 session_timeout = dot11RSNAConfigPMKLifetime;
2574 if (success && key && len >= PMK_LEN && !sta->remediation &&
2575 !sta->hs20_deauth_requested &&
2576 wpa_auth_pmksa_add(sta->wpa_sm, key, session_timeout,
2577 sta->eapol_sm) == 0) {
2578 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2579 HOSTAPD_LEVEL_DEBUG,
2580 "Added PMKSA cache entry (IEEE 802.1X)");
2585 * Many devices require deauthentication after WPS provisioning
2586 * and some may not be be able to do that themselves, so
2587 * disconnect the client here. In addition, this may also
2588 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2589 * the EAPOL PAE state machine would remain in HELD state for
2590 * considerable amount of time and some EAP methods, like
2591 * EAP-FAST with anonymous provisioning, may require another
2592 * EAPOL authentication to be started to complete connection.
2594 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2595 "disconnection after EAP-Failure");
2596 /* Add a small sleep to increase likelihood of previously
2597 * requested EAP-Failure TX getting out before this should the
2598 * driver reorder operations.
2601 ap_sta_disconnect(hapd, sta, sta->addr,
2602 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
2603 hostapd_wps_eap_completed(hapd);