2 * hostapd / IEEE 802.1X-2004 Authenticator
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "crypto/md5.h"
14 #include "crypto/crypto.h"
15 #include "crypto/random.h"
16 #include "common/ieee802_11_defs.h"
17 #include "radius/radius.h"
18 #include "radius/radius_client.h"
19 #include "eap_server/eap.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "eapol_auth/eapol_auth_sm_i.h"
25 #include "accounting.h"
28 #include "preauth_auth.h"
29 #include "pmksa_cache_auth.h"
30 #include "ap_config.h"
31 #include "ap_drv_ops.h"
32 #include "wps_hostapd.h"
34 #include "ieee802_1x.h"
38 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
39 #endif /* CONFIG_HS20 */
40 static void ieee802_1x_finished(struct hostapd_data *hapd,
41 struct sta_info *sta, int success,
45 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
46 u8 type, const u8 *data, size_t datalen)
49 struct ieee802_1x_hdr *xhdr;
53 len = sizeof(*xhdr) + datalen;
56 wpa_printf(MSG_ERROR, "malloc() failed for "
57 "ieee802_1x_send(len=%lu)",
62 xhdr = (struct ieee802_1x_hdr *) buf;
63 xhdr->version = hapd->conf->eapol_version;
65 xhdr->length = host_to_be16(datalen);
67 if (datalen > 0 && data != NULL)
68 os_memcpy(xhdr + 1, data, datalen);
70 if (wpa_auth_pairwise_set(sta->wpa_sm))
72 #ifdef CONFIG_TESTING_OPTIONS
73 if (hapd->ext_eapol_frame_io) {
74 size_t hex_len = 2 * len + 1;
75 char *hex = os_malloc(hex_len);
78 wpa_snprintf_hex(hex, hex_len, buf, len);
79 wpa_msg(hapd->msg_ctx, MSG_INFO,
80 "EAPOL-TX " MACSTR " %s",
81 MAC2STR(sta->addr), hex);
85 #endif /* CONFIG_TESTING_OPTIONS */
86 if (sta->flags & WLAN_STA_PREAUTH) {
87 rsn_preauth_send(hapd, sta, buf, len);
89 hostapd_drv_hapd_send_eapol(
90 hapd, sta->addr, buf, len,
91 encrypt, hostapd_sta_flags_to_drv(sta->flags));
98 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
99 struct sta_info *sta, int authorized)
103 if (sta->flags & WLAN_STA_PREAUTH)
107 ap_sta_set_authorized(hapd, sta, 1);
108 res = hostapd_set_authorized(hapd, sta, 1);
109 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
110 HOSTAPD_LEVEL_DEBUG, "authorizing port");
112 ap_sta_set_authorized(hapd, sta, 0);
113 res = hostapd_set_authorized(hapd, sta, 0);
114 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
115 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
118 if (res && errno != ENOENT) {
119 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
120 " flags for kernel driver (errno=%d).",
121 MAC2STR(sta->addr), errno);
125 os_get_reltime(&sta->connected_time);
126 accounting_sta_start(hapd, sta);
132 #ifndef CONFIG_NO_RC4
134 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
135 struct sta_info *sta,
136 int idx, int broadcast,
137 u8 *key_data, size_t key_len)
140 struct ieee802_1x_hdr *hdr;
141 struct ieee802_1x_eapol_key *key;
142 size_t len, ekey_len;
143 struct eapol_state_machine *sm = sta->eapol_sm;
148 len = sizeof(*key) + key_len;
149 buf = os_zalloc(sizeof(*hdr) + len);
153 hdr = (struct ieee802_1x_hdr *) buf;
154 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
155 key->type = EAPOL_KEY_TYPE_RC4;
156 WPA_PUT_BE16(key->key_length, key_len);
157 wpa_get_ntp_timestamp(key->replay_counter);
159 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
160 wpa_printf(MSG_ERROR, "Could not get random numbers");
165 key->key_index = idx | (broadcast ? 0 : BIT(7));
166 if (hapd->conf->eapol_key_index_workaround) {
167 /* According to some information, WinXP Supplicant seems to
168 * interpret bit7 as an indication whether the key is to be
169 * activated, so make it possible to enable workaround that
170 * sets this bit for all keys. */
171 key->key_index |= BIT(7);
174 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
175 * MSK[32..63] is used to sign the message. */
176 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
177 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
178 "and signing EAPOL-Key");
182 os_memcpy((u8 *) (key + 1), key_data, key_len);
183 ekey_len = sizeof(key->key_iv) + 32;
184 ekey = os_malloc(ekey_len);
186 wpa_printf(MSG_ERROR, "Could not encrypt key");
190 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
191 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
192 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
195 /* This header is needed here for HMAC-MD5, but it will be regenerated
196 * in ieee802_1x_send() */
197 hdr->version = hapd->conf->eapol_version;
198 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
199 hdr->length = host_to_be16(len);
200 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
203 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
204 " (%s index=%d)", MAC2STR(sm->addr),
205 broadcast ? "broadcast" : "unicast", idx);
206 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
208 sta->eapol_sm->dot1xAuthEapolFramesTx++;
213 static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
215 struct eapol_authenticator *eapol = hapd->eapol_auth;
216 struct eapol_state_machine *sm = sta->eapol_sm;
218 if (sm == NULL || !sm->eap_if->eapKeyData)
221 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
224 #ifndef CONFIG_NO_VLAN
225 if (sta->vlan_id > 0) {
226 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
229 #endif /* CONFIG_NO_VLAN */
231 if (eapol->default_wep_key) {
232 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
233 eapol->default_wep_key,
234 hapd->conf->default_wep_key_len);
237 if (hapd->conf->individual_wep_key_len > 0) {
239 ikey = os_malloc(hapd->conf->individual_wep_key_len);
241 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
243 wpa_printf(MSG_ERROR, "Could not generate random "
244 "individual WEP key.");
249 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
250 ikey, hapd->conf->individual_wep_key_len);
252 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
253 hapd->conf->individual_wep_key_len);
255 /* TODO: set encryption in TX callback, i.e., only after STA
256 * has ACKed EAPOL-Key frame */
257 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
258 sta->addr, 0, 1, NULL, 0, ikey,
259 hapd->conf->individual_wep_key_len)) {
260 wpa_printf(MSG_ERROR, "Could not set individual WEP "
268 #endif /* CONFIG_NO_RC4 */
269 #endif /* CONFIG_FIPS */
272 const char *radius_mode_txt(struct hostapd_data *hapd)
274 switch (hapd->iface->conf->hw_mode) {
275 case HOSTAPD_MODE_IEEE80211AD:
277 case HOSTAPD_MODE_IEEE80211A:
279 case HOSTAPD_MODE_IEEE80211G:
281 case HOSTAPD_MODE_IEEE80211B:
288 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
293 for (i = 0; i < sta->supported_rates_len; i++)
294 if ((sta->supported_rates[i] & 0x7f) > rate)
295 rate = sta->supported_rates[i] & 0x7f;
301 #ifndef CONFIG_NO_RADIUS
302 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
303 struct eapol_state_machine *sm,
304 const u8 *eap, size_t len)
308 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
310 if (len <= sizeof(struct eap_hdr) ||
311 (hdr->code == EAP_CODE_RESPONSE &&
312 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
313 (hdr->code == EAP_CODE_INITIATE &&
314 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
315 (hdr->code != EAP_CODE_RESPONSE &&
316 hdr->code != EAP_CODE_INITIATE))
319 identity = eap_get_identity(sm->eap, &identity_len);
320 if (identity == NULL)
323 /* Save station identity for future RADIUS packets */
324 os_free(sm->identity);
325 sm->identity = (u8 *) dup_binstr(identity, identity_len);
326 if (sm->identity == NULL) {
327 sm->identity_len = 0;
331 sm->identity_len = identity_len;
332 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
333 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
334 sm->dot1xAuthEapolRespIdFramesRx++;
338 static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
339 struct hostapd_radius_attr *req_attr,
340 struct sta_info *sta,
341 struct radius_msg *msg)
346 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
347 val = wpa_auth_get_pairwise(sta->wpa_sm);
348 suite = wpa_cipher_to_suite(ver, val);
350 !hostapd_config_get_radius_attr(req_attr,
351 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
352 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
354 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
358 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
360 WPA_PROTO_RSN : WPA_PROTO_WPA,
361 hapd->conf->wpa_group);
362 if (!hostapd_config_get_radius_attr(req_attr,
363 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
364 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
366 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
370 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
371 suite = wpa_akm_to_suite(val);
373 !hostapd_config_get_radius_attr(req_attr,
374 RADIUS_ATTR_WLAN_AKM_SUITE) &&
375 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
377 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
381 #ifdef CONFIG_IEEE80211W
382 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
383 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
384 hapd->conf->group_mgmt_cipher);
385 if (!hostapd_config_get_radius_attr(
386 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
387 !radius_msg_add_attr_int32(
388 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
389 wpa_printf(MSG_ERROR,
390 "Could not add WLAN-Group-Mgmt-Cipher");
394 #endif /* CONFIG_IEEE80211W */
400 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
401 struct hostapd_radius_attr *req_attr,
402 struct sta_info *sta,
403 struct radius_msg *msg)
407 if (!hostapd_config_get_radius_attr(req_attr,
408 RADIUS_ATTR_SERVICE_TYPE) &&
409 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
410 RADIUS_SERVICE_TYPE_FRAMED)) {
411 wpa_printf(MSG_ERROR, "Could not add Service-Type");
415 if (!hostapd_config_get_radius_attr(req_attr,
416 RADIUS_ATTR_NAS_PORT) &&
417 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
418 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
422 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
424 buf[sizeof(buf) - 1] = '\0';
425 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
426 (u8 *) buf, os_strlen(buf))) {
427 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
431 if (sta->flags & WLAN_STA_PREAUTH) {
432 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
435 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
436 radius_sta_rate(hapd, sta) / 2,
437 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
438 radius_mode_txt(hapd));
439 buf[sizeof(buf) - 1] = '\0';
441 if (!hostapd_config_get_radius_attr(req_attr,
442 RADIUS_ATTR_CONNECT_INFO) &&
443 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
444 (u8 *) buf, os_strlen(buf))) {
445 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
449 if (sta->acct_session_id) {
450 os_snprintf(buf, sizeof(buf), "%016llX",
451 (unsigned long long) sta->acct_session_id);
452 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
453 (u8 *) buf, os_strlen(buf))) {
454 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
459 if ((hapd->conf->wpa & 2) &&
460 !hapd->conf->disable_pmksa_caching &&
461 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
462 os_snprintf(buf, sizeof(buf), "%016llX",
464 sta->eapol_sm->acct_multi_session_id);
465 if (!radius_msg_add_attr(
466 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
467 (u8 *) buf, os_strlen(buf))) {
469 "Could not add Acct-Multi-Session-Id");
474 #ifdef CONFIG_IEEE80211R
475 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
477 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
478 sta->auth_alg == WLAN_AUTH_FT) &&
479 !hostapd_config_get_radius_attr(req_attr,
480 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
481 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
483 hapd->conf->mobility_domain))) {
484 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
487 #endif /* CONFIG_IEEE80211R */
489 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
490 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
497 int add_common_radius_attr(struct hostapd_data *hapd,
498 struct hostapd_radius_attr *req_attr,
499 struct sta_info *sta,
500 struct radius_msg *msg)
503 struct hostapd_radius_attr *attr;
506 if (!hostapd_config_get_radius_attr(req_attr,
507 RADIUS_ATTR_NAS_IP_ADDRESS) &&
508 hapd->conf->own_ip_addr.af == AF_INET &&
509 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
510 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
511 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
516 if (!hostapd_config_get_radius_attr(req_attr,
517 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
518 hapd->conf->own_ip_addr.af == AF_INET6 &&
519 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
520 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
521 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
524 #endif /* CONFIG_IPV6 */
526 if (!hostapd_config_get_radius_attr(req_attr,
527 RADIUS_ATTR_NAS_IDENTIFIER) &&
528 hapd->conf->nas_identifier &&
529 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
530 (u8 *) hapd->conf->nas_identifier,
531 os_strlen(hapd->conf->nas_identifier))) {
532 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
536 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
537 MAC2STR(hapd->own_addr));
538 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
539 hapd->conf->ssid.ssid_len);
540 len += hapd->conf->ssid.ssid_len;
541 if (!hostapd_config_get_radius_attr(req_attr,
542 RADIUS_ATTR_CALLED_STATION_ID) &&
543 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
545 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
549 if (!hostapd_config_get_radius_attr(req_attr,
550 RADIUS_ATTR_NAS_PORT_TYPE) &&
551 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
552 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
553 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
557 #ifdef CONFIG_INTERWORKING
558 if (hapd->conf->interworking &&
559 !is_zero_ether_addr(hapd->conf->hessid)) {
560 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
561 MAC2STR(hapd->conf->hessid));
562 buf[sizeof(buf) - 1] = '\0';
563 if (!hostapd_config_get_radius_attr(req_attr,
564 RADIUS_ATTR_WLAN_HESSID) &&
565 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
566 (u8 *) buf, os_strlen(buf))) {
567 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
571 #endif /* CONFIG_INTERWORKING */
573 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
576 for (attr = req_attr; attr; attr = attr->next) {
577 if (!radius_msg_add_attr(msg, attr->type,
578 wpabuf_head(attr->val),
579 wpabuf_len(attr->val))) {
580 wpa_printf(MSG_ERROR, "Could not add RADIUS "
590 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
591 struct sta_info *sta,
592 const u8 *eap, size_t len)
594 struct radius_msg *msg;
595 struct eapol_state_machine *sm = sta->eapol_sm;
600 ieee802_1x_learn_identity(hapd, sm, eap, len);
602 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
605 sm->radius_identifier = radius_client_get_id(hapd->radius);
606 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
607 sm->radius_identifier);
609 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
613 if (radius_msg_make_authenticator(msg) < 0) {
614 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
619 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
620 sm->identity, sm->identity_len)) {
621 wpa_printf(MSG_INFO, "Could not add User-Name");
625 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
629 /* TODO: should probably check MTU from driver config; 2304 is max for
630 * IEEE 802.11, but use 1400 to avoid problems with too large packets
632 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
633 RADIUS_ATTR_FRAMED_MTU) &&
634 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
635 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
639 if (!radius_msg_add_eap(msg, eap, len)) {
640 wpa_printf(MSG_INFO, "Could not add EAP-Message");
644 /* State attribute must be copied if and only if this packet is
645 * Access-Request reply to the previous Access-Challenge */
646 if (sm->last_recv_radius &&
647 radius_msg_get_hdr(sm->last_recv_radius)->code ==
648 RADIUS_CODE_ACCESS_CHALLENGE) {
649 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
652 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
656 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
660 if (hapd->conf->radius_request_cui) {
663 /* Add previously learned CUI or nul CUI to request CUI */
664 if (sm->radius_cui) {
665 cui = wpabuf_head(sm->radius_cui);
666 cui_len = wpabuf_len(sm->radius_cui);
668 cui = (const u8 *) "\0";
671 if (!radius_msg_add_attr(msg,
672 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
674 wpa_printf(MSG_ERROR, "Could not add CUI");
680 if (hapd->conf->hs20) {
681 u8 ver = 1; /* Release 2 */
682 if (!radius_msg_add_wfa(
683 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
685 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
690 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
694 pos = wpabuf_head_u8(sta->hs20_ie);
695 buf[0] = (*pos) >> 4;
696 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
697 wpabuf_len(sta->hs20_ie) >= 3)
698 id = WPA_GET_LE16(pos + 1);
701 WPA_PUT_BE16(buf + 1, id);
702 if (!radius_msg_add_wfa(
704 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
706 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
712 #endif /* CONFIG_HS20 */
714 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
720 radius_msg_free(msg);
722 #endif /* CONFIG_NO_RADIUS */
725 static void handle_eap_response(struct hostapd_data *hapd,
726 struct sta_info *sta, struct eap_hdr *eap,
730 struct eapol_state_machine *sm = sta->eapol_sm;
734 data = (u8 *) (eap + 1);
736 if (len < sizeof(*eap) + 1) {
737 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
741 sm->eap_type_supp = type = data[0];
743 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
744 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
745 "id=%d len=%d) from STA: EAP Response-%s (%d)",
746 eap->code, eap->identifier, be_to_host16(eap->length),
747 eap_server_get_name(0, type), type);
749 sm->dot1xAuthEapolRespFramesRx++;
751 wpabuf_free(sm->eap_if->eapRespData);
752 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
757 static void handle_eap_initiate(struct hostapd_data *hapd,
758 struct sta_info *sta, struct eap_hdr *eap,
763 struct eapol_state_machine *sm = sta->eapol_sm;
768 if (len < sizeof(*eap) + 1) {
770 "handle_eap_initiate: too short response data");
774 data = (u8 *) (eap + 1);
777 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
778 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
779 "id=%d len=%d) from STA: EAP Initiate type %u",
780 eap->code, eap->identifier, be_to_host16(eap->length),
783 wpabuf_free(sm->eap_if->eapRespData);
784 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
786 #endif /* CONFIG_ERP */
790 /* Process incoming EAP packet from Supplicant */
791 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
797 if (len < sizeof(*eap)) {
798 wpa_printf(MSG_INFO, " too short EAP packet");
802 eap = (struct eap_hdr *) buf;
804 eap_len = be_to_host16(eap->length);
805 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
806 eap->code, eap->identifier, eap_len);
807 if (eap_len < sizeof(*eap)) {
808 wpa_printf(MSG_DEBUG, " Invalid EAP length");
810 } else if (eap_len > len) {
811 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
814 } else if (eap_len < len) {
815 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
816 "packet", (unsigned long) len - eap_len);
820 case EAP_CODE_REQUEST:
821 wpa_printf(MSG_DEBUG, " (request)");
823 case EAP_CODE_RESPONSE:
824 wpa_printf(MSG_DEBUG, " (response)");
825 handle_eap_response(hapd, sta, eap, eap_len);
827 case EAP_CODE_SUCCESS:
828 wpa_printf(MSG_DEBUG, " (success)");
830 case EAP_CODE_FAILURE:
831 wpa_printf(MSG_DEBUG, " (failure)");
833 case EAP_CODE_INITIATE:
834 wpa_printf(MSG_DEBUG, " (initiate)");
835 handle_eap_initiate(hapd, sta, eap, eap_len);
837 case EAP_CODE_FINISH:
838 wpa_printf(MSG_DEBUG, " (finish)");
841 wpa_printf(MSG_DEBUG, " (unknown code)");
847 static struct eapol_state_machine *
848 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
851 if (sta->flags & WLAN_STA_PREAUTH)
852 flags |= EAPOL_SM_PREAUTH;
854 flags |= EAPOL_SM_USES_WPA;
855 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
856 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
858 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
859 sta->wps_ie, sta->p2p_ie, sta,
860 sta->identity, sta->radius_cui);
865 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
866 * @hapd: hostapd BSS data
867 * @sa: Source address (sender of the EAPOL frame)
869 * @len: Length of buf in octets
871 * This function is called for each incoming EAPOL frame from the interface
873 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
876 struct sta_info *sta;
877 struct ieee802_1x_hdr *hdr;
878 struct ieee802_1x_eapol_key *key;
880 struct rsn_pmksa_cache_entry *pmksa;
883 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
884 !hapd->conf->wps_state)
887 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
888 (unsigned long) len, MAC2STR(sa));
889 sta = ap_get_sta(hapd, sa);
890 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
891 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
892 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
893 "associated/Pre-authenticating STA");
897 if (len < sizeof(*hdr)) {
898 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
902 hdr = (struct ieee802_1x_hdr *) buf;
903 datalen = be_to_host16(hdr->length);
904 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
905 hdr->version, hdr->type, datalen);
907 if (len - sizeof(*hdr) < datalen) {
908 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
910 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
913 if (len - sizeof(*hdr) > datalen) {
914 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
915 "IEEE 802.1X packet",
916 (unsigned long) len - sizeof(*hdr) - datalen);
920 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
921 sta->eapol_sm->dot1xAuthEapolFramesRx++;
924 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
925 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
926 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
927 (key->type == EAPOL_KEY_TYPE_WPA ||
928 key->type == EAPOL_KEY_TYPE_RSN)) {
929 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
930 sizeof(*hdr) + datalen);
934 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
935 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
936 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
937 "802.1X not enabled and WPS not used");
941 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
942 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
943 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
948 if (!sta->eapol_sm) {
949 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
954 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
955 u32 wflags = sta->flags & (WLAN_STA_WPS |
958 if (wflags == WLAN_STA_MAYBE_WPS ||
959 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
961 * Delay EAPOL frame transmission until a
962 * possible WPS STA initiates the handshake
963 * with EAPOL-Start. Only allow the wait to be
964 * skipped if the STA is known to support WPS
967 wpa_printf(MSG_DEBUG, "WPS: Do not start "
968 "EAPOL until EAPOL-Start is "
970 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
973 #endif /* CONFIG_WPS */
975 sta->eapol_sm->eap_if->portEnabled = TRUE;
978 /* since we support version 1, we can ignore version field and proceed
979 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
980 /* TODO: actually, we are not version 1 anymore.. However, Version 2
981 * does not change frame contents, so should be ok to process frames
982 * more or less identically. Some changes might be needed for
983 * verification of fields. */
986 case IEEE802_1X_TYPE_EAP_PACKET:
987 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
990 case IEEE802_1X_TYPE_EAPOL_START:
991 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
992 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
994 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
995 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
997 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
998 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
999 "available - ignore it since "
1000 "STA sent EAPOL-Start");
1001 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1003 sta->eapol_sm->eapolStart = TRUE;
1004 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1005 eap_server_clear_identity(sta->eapol_sm->eap);
1006 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1009 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1010 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1011 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
1013 sta->acct_terminate_cause =
1014 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1015 accounting_sta_stop(hapd, sta);
1016 sta->eapol_sm->eapolLogoff = TRUE;
1017 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1018 eap_server_clear_identity(sta->eapol_sm->eap);
1021 case IEEE802_1X_TYPE_EAPOL_KEY:
1022 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1023 if (!ap_sta_is_authorized(sta)) {
1024 wpa_printf(MSG_DEBUG, " Dropped key data from "
1025 "unauthorized Supplicant");
1030 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1031 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1032 /* TODO: implement support for this; show data */
1036 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1037 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1041 eapol_auth_step(sta->eapol_sm);
1046 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1047 * @hapd: hostapd BSS data
1050 * This function is called to start IEEE 802.1X authentication when a new
1051 * station completes IEEE 802.11 association.
1053 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1055 struct rsn_pmksa_cache_entry *pmksa;
1061 if (hapd->conf->wps_state &&
1062 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1063 (sta->flags & WLAN_STA_WPS))) {
1065 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1066 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1067 * authentication in this BSS.
1071 #endif /* CONFIG_WPS */
1073 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1074 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1075 "802.1X not enabled or forced for WPS");
1077 * Clear any possible EAPOL authenticator state to support
1078 * reassociation change from WPS to PSK.
1080 ieee802_1x_free_station(hapd, sta);
1084 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1085 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1086 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1088 * Clear any possible EAPOL authenticator state to support
1089 * reassociation change from WPA-EAP to PSK.
1091 ieee802_1x_free_station(hapd, sta);
1095 if (sta->eapol_sm == NULL) {
1096 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1097 HOSTAPD_LEVEL_DEBUG, "start authentication");
1098 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1099 if (sta->eapol_sm == NULL) {
1100 hostapd_logger(hapd, sta->addr,
1101 HOSTAPD_MODULE_IEEE8021X,
1103 "failed to allocate state machine");
1110 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1111 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1112 !(sta->flags & WLAN_STA_WPS2)) {
1114 * Delay EAPOL frame transmission until a possible WPS STA
1115 * initiates the handshake with EAPOL-Start. Only allow the
1116 * wait to be skipped if the STA is known to support WPS 2.0.
1118 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1119 "EAPOL-Start is received");
1120 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1122 #endif /* CONFIG_WPS */
1124 sta->eapol_sm->eap_if->portEnabled = TRUE;
1126 #ifdef CONFIG_IEEE80211R
1127 if (sta->auth_alg == WLAN_AUTH_FT) {
1128 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1129 HOSTAPD_LEVEL_DEBUG,
1130 "PMK from FT - skip IEEE 802.1X/EAP");
1131 /* Setup EAPOL state machines to already authenticated state
1132 * because of existing FT information from R0KH. */
1133 sta->eapol_sm->keyRun = TRUE;
1134 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1135 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1136 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1137 sta->eapol_sm->authSuccess = TRUE;
1138 sta->eapol_sm->authFail = FALSE;
1139 sta->eapol_sm->portValid = TRUE;
1140 if (sta->eapol_sm->eap)
1141 eap_sm_notify_cached(sta->eapol_sm->eap);
1142 /* TODO: get vlan_id from R0KH using RRB message */
1145 #endif /* CONFIG_IEEE80211R */
1147 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1149 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1150 HOSTAPD_LEVEL_DEBUG,
1151 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1152 /* Setup EAPOL state machines to already authenticated state
1153 * because of existing PMKSA information in the cache. */
1154 sta->eapol_sm->keyRun = TRUE;
1155 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1156 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1157 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1158 sta->eapol_sm->authSuccess = TRUE;
1159 sta->eapol_sm->authFail = FALSE;
1160 if (sta->eapol_sm->eap)
1161 eap_sm_notify_cached(sta->eapol_sm->eap);
1162 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
1163 ap_sta_bind_vlan(hapd, sta);
1167 * Force EAPOL state machines to start
1168 * re-authentication without having to wait for the
1169 * Supplicant to send EAPOL-Start.
1171 sta->eapol_sm->reAuthenticate = TRUE;
1173 eapol_auth_step(sta->eapol_sm);
1178 void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
1180 struct eapol_state_machine *sm = sta->eapol_sm;
1183 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1184 #endif /* CONFIG_HS20 */
1189 sta->eapol_sm = NULL;
1191 #ifndef CONFIG_NO_RADIUS
1192 radius_msg_free(sm->last_recv_radius);
1193 radius_free_class(&sm->radius_class);
1194 #endif /* CONFIG_NO_RADIUS */
1196 eapol_auth_free(sm);
1200 #ifndef CONFIG_NO_RADIUS
1201 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1202 struct sta_info *sta)
1205 const struct eap_hdr *hdr;
1208 struct radius_msg *msg;
1209 struct eapol_state_machine *sm = sta->eapol_sm;
1211 if (sm == NULL || sm->last_recv_radius == NULL) {
1213 sm->eap_if->aaaEapNoReq = TRUE;
1217 msg = sm->last_recv_radius;
1219 eap = radius_msg_get_eap(msg);
1221 /* RFC 3579, Chap. 2.6.3:
1222 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1224 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1225 HOSTAPD_LEVEL_WARNING, "could not extract "
1226 "EAP-Message from RADIUS message");
1227 sm->eap_if->aaaEapNoReq = TRUE;
1231 if (wpabuf_len(eap) < sizeof(*hdr)) {
1232 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1233 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1234 "received from authentication server");
1236 sm->eap_if->aaaEapNoReq = TRUE;
1240 if (wpabuf_len(eap) > sizeof(*hdr))
1241 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1243 hdr = wpabuf_head(eap);
1244 switch (hdr->code) {
1245 case EAP_CODE_REQUEST:
1247 sm->eap_type_authsrv = eap_type;
1248 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1249 eap_server_get_name(0, eap_type), eap_type);
1251 case EAP_CODE_RESPONSE:
1252 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1253 eap_server_get_name(0, eap_type), eap_type);
1255 case EAP_CODE_SUCCESS:
1256 os_strlcpy(buf, "EAP Success", sizeof(buf));
1258 case EAP_CODE_FAILURE:
1259 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1262 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1265 buf[sizeof(buf) - 1] = '\0';
1266 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1267 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1268 "id=%d len=%d) from RADIUS server: %s",
1269 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1271 sm->eap_if->aaaEapReq = TRUE;
1273 wpabuf_free(sm->eap_if->aaaEapReqData);
1274 sm->eap_if->aaaEapReqData = eap;
1278 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1279 struct sta_info *sta, struct radius_msg *msg,
1280 struct radius_msg *req,
1281 const u8 *shared_secret,
1282 size_t shared_secret_len)
1284 struct radius_ms_mppe_keys *keys;
1285 struct eapol_state_machine *sm = sta->eapol_sm;
1289 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1292 if (keys && keys->send && keys->recv) {
1293 size_t len = keys->send_len + keys->recv_len;
1294 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1295 keys->send, keys->send_len);
1296 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1297 keys->recv, keys->recv_len);
1299 os_free(sm->eap_if->aaaEapKeyData);
1300 sm->eap_if->aaaEapKeyData = os_malloc(len);
1301 if (sm->eap_if->aaaEapKeyData) {
1302 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1304 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1305 keys->send, keys->send_len);
1306 sm->eap_if->aaaEapKeyDataLen = len;
1307 sm->eap_if->aaaEapKeyAvailable = TRUE;
1310 wpa_printf(MSG_DEBUG,
1311 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1312 keys, keys ? keys->send : NULL,
1313 keys ? keys->recv : NULL);
1317 os_free(keys->send);
1318 os_free(keys->recv);
1324 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1325 struct sta_info *sta,
1326 struct radius_msg *msg)
1330 struct eapol_state_machine *sm = sta->eapol_sm;
1332 struct radius_attr_data *nclass;
1333 size_t nclass_count;
1335 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1339 radius_free_class(&sm->radius_class);
1340 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1344 nclass = os_calloc(count, sizeof(struct radius_attr_data));
1351 for (i = 0; i < count; i++) {
1353 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1354 &attr_class, &class_len,
1359 } while (class_len < 1);
1361 nclass[nclass_count].data = os_malloc(class_len);
1362 if (nclass[nclass_count].data == NULL)
1365 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
1366 nclass[nclass_count].len = class_len;
1370 sm->radius_class.attr = nclass;
1371 sm->radius_class.count = nclass_count;
1372 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1373 "attributes for " MACSTR,
1374 (unsigned long) sm->radius_class.count,
1375 MAC2STR(sta->addr));
1379 /* Update sta->identity based on User-Name attribute in Access-Accept */
1380 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1381 struct sta_info *sta,
1382 struct radius_msg *msg)
1386 struct eapol_state_machine *sm = sta->eapol_sm;
1391 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1395 identity = (u8 *) dup_binstr(buf, len);
1396 if (identity == NULL)
1399 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1400 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1401 "User-Name from Access-Accept '%s'",
1402 sm->identity ? (char *) sm->identity : "N/A",
1405 os_free(sm->identity);
1406 sm->identity = identity;
1407 sm->identity_len = len;
1411 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1412 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1413 struct sta_info *sta,
1414 struct radius_msg *msg)
1416 struct eapol_state_machine *sm = sta->eapol_sm;
1424 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1425 &buf, &len, NULL) < 0)
1428 cui = wpabuf_alloc_copy(buf, len);
1432 wpabuf_free(sm->radius_cui);
1433 sm->radius_cui = cui;
1439 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1441 sta->remediation = 1;
1442 os_free(sta->remediation_url);
1444 sta->remediation_url = os_malloc(len);
1445 if (!sta->remediation_url)
1447 sta->remediation_method = pos[0];
1448 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1449 sta->remediation_url[len - 1] = '\0';
1450 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1451 "for " MACSTR " - server method %u URL %s",
1452 MAC2STR(sta->addr), sta->remediation_method,
1453 sta->remediation_url);
1455 sta->remediation_url = NULL;
1456 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1457 "for " MACSTR, MAC2STR(sta->addr));
1459 /* TODO: assign the STA into remediation VLAN or add filtering */
1463 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1464 struct sta_info *sta, u8 *pos,
1468 return; /* Malformed information */
1469 sta->hs20_deauth_requested = 1;
1470 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1472 *pos, WPA_GET_LE16(pos + 1));
1473 wpabuf_free(sta->hs20_deauth_req);
1474 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1475 if (sta->hs20_deauth_req) {
1476 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1477 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1478 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1480 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1484 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1485 struct sta_info *sta, u8 *pos,
1486 size_t len, int session_timeout)
1489 int warning_time, beacon_int;
1492 return; /* Malformed information */
1493 os_free(sta->hs20_session_info_url);
1494 sta->hs20_session_info_url = os_malloc(len);
1495 if (sta->hs20_session_info_url == NULL)
1498 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1499 sta->hs20_session_info_url[len - 1] = '\0';
1500 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1501 "(session_timeout=%d)",
1502 sta->hs20_session_info_url, swt, session_timeout);
1503 if (session_timeout < 0) {
1504 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1508 swt = 1; /* Use one minute as the AP selected value */
1510 if ((unsigned int) session_timeout < swt * 60)
1513 warning_time = session_timeout - swt * 60;
1515 beacon_int = hapd->iconf->beacon_int;
1517 beacon_int = 100; /* best guess */
1518 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1519 if (sta->hs20_disassoc_timer > 65535)
1520 sta->hs20_disassoc_timer = 65535;
1522 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1525 #endif /* CONFIG_HS20 */
1528 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1529 struct sta_info *sta,
1530 struct radius_msg *msg,
1531 int session_timeout)
1534 u8 *buf, *pos, *end, type, sublen;
1538 sta->remediation = 0;
1539 sta->hs20_deauth_requested = 0;
1542 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1543 &buf, &len, buf) < 0)
1549 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1556 continue; /* invalid length */
1557 sublen -= 2; /* skip header */
1558 if (pos + sublen > end)
1559 continue; /* invalid WFA VSA */
1562 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1563 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1565 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1566 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1568 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1569 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1574 #endif /* CONFIG_HS20 */
1578 struct sta_id_search {
1580 struct eapol_state_machine *sm;
1584 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1585 struct sta_info *sta,
1588 struct sta_id_search *id_search = ctx;
1589 struct eapol_state_machine *sm = sta->eapol_sm;
1591 if (sm && sm->radius_identifier >= 0 &&
1592 sm->radius_identifier == id_search->identifier) {
1600 static struct eapol_state_machine *
1601 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1603 struct sta_id_search id_search;
1604 id_search.identifier = identifier;
1605 id_search.sm = NULL;
1606 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1607 return id_search.sm;
1612 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1613 * @msg: RADIUS response message
1614 * @req: RADIUS request message
1615 * @shared_secret: RADIUS shared secret
1616 * @shared_secret_len: Length of shared_secret in octets
1617 * @data: Context data (struct hostapd_data *)
1618 * Returns: Processing status
1620 static RadiusRxResult
1621 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1622 const u8 *shared_secret, size_t shared_secret_len,
1625 struct hostapd_data *hapd = data;
1626 struct sta_info *sta;
1627 u32 session_timeout = 0, termination_action, acct_interim_interval;
1628 int session_timeout_set;
1629 struct eapol_state_machine *sm;
1630 int override_eapReq = 0;
1631 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1632 struct vlan_description vlan_desc;
1633 #ifndef CONFIG_NO_VLAN
1634 int *untagged, *tagged, *notempty;
1635 #endif /* CONFIG_NO_VLAN */
1637 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1639 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1641 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1642 "station for this RADIUS message");
1643 return RADIUS_RX_UNKNOWN;
1647 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1648 * present when packet contains an EAP-Message attribute */
1649 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1650 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1652 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1653 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1654 "Message-Authenticator since it does not include "
1656 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1658 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1659 return RADIUS_RX_INVALID_AUTHENTICATOR;
1662 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1663 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1664 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1665 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1666 return RADIUS_RX_UNKNOWN;
1669 sm->radius_identifier = -1;
1670 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1671 MAC2STR(sta->addr));
1673 radius_msg_free(sm->last_recv_radius);
1674 sm->last_recv_radius = msg;
1676 session_timeout_set =
1677 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1679 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1680 &termination_action))
1681 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1683 if (hapd->conf->acct_interim_interval == 0 &&
1684 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1685 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1686 &acct_interim_interval) == 0) {
1687 if (acct_interim_interval < 60) {
1688 hostapd_logger(hapd, sta->addr,
1689 HOSTAPD_MODULE_IEEE8021X,
1691 "ignored too small "
1692 "Acct-Interim-Interval %d",
1693 acct_interim_interval);
1695 sta->acct_interim_interval = acct_interim_interval;
1699 switch (hdr->code) {
1700 case RADIUS_CODE_ACCESS_ACCEPT:
1701 #ifndef CONFIG_NO_VLAN
1702 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED) {
1703 notempty = &vlan_desc.notempty;
1704 untagged = &vlan_desc.untagged;
1705 tagged = vlan_desc.tagged;
1706 *notempty = !!radius_msg_get_vlanid(msg, untagged,
1707 MAX_NUM_TAGGED_VLAN,
1711 if (vlan_desc.notempty &&
1712 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1713 sta->eapol_sm->authFail = TRUE;
1714 hostapd_logger(hapd, sta->addr,
1715 HOSTAPD_MODULE_RADIUS,
1717 "Invalid VLAN %d%s received from RADIUS server",
1719 vlan_desc.tagged[0] ? "+" : "");
1720 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1721 ap_sta_set_vlan(hapd, sta, &vlan_desc);
1725 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1726 !vlan_desc.notempty) {
1727 sta->eapol_sm->authFail = TRUE;
1728 hostapd_logger(hapd, sta->addr,
1729 HOSTAPD_MODULE_IEEE8021X,
1730 HOSTAPD_LEVEL_INFO, "authentication "
1731 "server did not include required VLAN "
1732 "ID in Access-Accept");
1735 #endif /* CONFIG_NO_VLAN */
1737 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0)
1740 #ifndef CONFIG_NO_VLAN
1741 if (sta->vlan_id > 0) {
1742 hostapd_logger(hapd, sta->addr,
1743 HOSTAPD_MODULE_RADIUS,
1745 "VLAN ID %d", sta->vlan_id);
1747 #endif /* CONFIG_NO_VLAN */
1749 if ((sta->flags & WLAN_STA_ASSOC) &&
1750 ap_sta_bind_vlan(hapd, sta) < 0)
1753 sta->session_timeout_set = !!session_timeout_set;
1754 sta->session_timeout = session_timeout;
1756 /* RFC 3580, Ch. 3.17 */
1757 if (session_timeout_set && termination_action ==
1758 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1759 sm->reAuthPeriod = session_timeout;
1760 } else if (session_timeout_set)
1761 ap_sta_session_timeout(hapd, sta, session_timeout);
1763 sm->eap_if->aaaSuccess = TRUE;
1764 override_eapReq = 1;
1765 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1767 ieee802_1x_store_radius_class(hapd, sta, msg);
1768 ieee802_1x_update_sta_identity(hapd, sta, msg);
1769 ieee802_1x_update_sta_cui(hapd, sta, msg);
1770 ieee802_1x_check_hs20(hapd, sta, msg,
1771 session_timeout_set ?
1772 (int) session_timeout : -1);
1774 case RADIUS_CODE_ACCESS_REJECT:
1775 sm->eap_if->aaaFail = TRUE;
1776 override_eapReq = 1;
1778 case RADIUS_CODE_ACCESS_CHALLENGE:
1779 sm->eap_if->aaaEapReq = TRUE;
1780 if (session_timeout_set) {
1781 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1782 sm->eap_if->aaaMethodTimeout = session_timeout;
1783 hostapd_logger(hapd, sm->addr,
1784 HOSTAPD_MODULE_IEEE8021X,
1785 HOSTAPD_LEVEL_DEBUG,
1786 "using EAP timeout of %d seconds (from "
1788 sm->eap_if->aaaMethodTimeout);
1791 * Use dynamic retransmission behavior per EAP
1794 sm->eap_if->aaaMethodTimeout = 0;
1799 ieee802_1x_decapsulate_radius(hapd, sta);
1800 if (override_eapReq)
1801 sm->eap_if->aaaEapReq = FALSE;
1803 eapol_auth_step(sm);
1805 return RADIUS_RX_QUEUED;
1807 #endif /* CONFIG_NO_RADIUS */
1810 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1812 struct eapol_state_machine *sm = sta->eapol_sm;
1816 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1817 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1819 #ifndef CONFIG_NO_RADIUS
1820 radius_msg_free(sm->last_recv_radius);
1821 sm->last_recv_radius = NULL;
1822 #endif /* CONFIG_NO_RADIUS */
1824 if (sm->eap_if->eapTimeout) {
1826 * Disconnect the STA since it did not reply to the last EAP
1827 * request and we cannot continue EAP processing (EAP-Failure
1828 * could only be sent if the EAP peer actually replied).
1830 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1831 MAC2STR(sta->addr));
1833 sm->eap_if->portEnabled = FALSE;
1834 ap_sta_disconnect(hapd, sta, sta->addr,
1835 WLAN_REASON_PREV_AUTH_NOT_VALID);
1840 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1842 struct eapol_authenticator *eapol = hapd->eapol_auth;
1844 if (hapd->conf->default_wep_key_len < 1)
1847 os_free(eapol->default_wep_key);
1848 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1849 if (eapol->default_wep_key == NULL ||
1850 random_get_bytes(eapol->default_wep_key,
1851 hapd->conf->default_wep_key_len)) {
1852 wpa_printf(MSG_INFO, "Could not generate random WEP key");
1853 os_free(eapol->default_wep_key);
1854 eapol->default_wep_key = NULL;
1858 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1859 eapol->default_wep_key,
1860 hapd->conf->default_wep_key_len);
1866 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1867 struct sta_info *sta, void *ctx)
1869 if (sta->eapol_sm) {
1870 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1871 eapol_auth_step(sta->eapol_sm);
1877 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1879 struct hostapd_data *hapd = eloop_ctx;
1880 struct eapol_authenticator *eapol = hapd->eapol_auth;
1882 if (eapol->default_wep_key_idx >= 3)
1883 eapol->default_wep_key_idx =
1884 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1886 eapol->default_wep_key_idx++;
1888 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1889 eapol->default_wep_key_idx);
1891 if (ieee802_1x_rekey_broadcast(hapd)) {
1892 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1893 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1894 "new broadcast key");
1895 os_free(eapol->default_wep_key);
1896 eapol->default_wep_key = NULL;
1900 /* TODO: Could setup key for RX here, but change default TX keyid only
1901 * after new broadcast key has been sent to all stations. */
1902 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1903 broadcast_ether_addr,
1904 eapol->default_wep_key_idx, 1, NULL, 0,
1905 eapol->default_wep_key,
1906 hapd->conf->default_wep_key_len)) {
1907 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1908 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1909 "new broadcast key");
1910 os_free(eapol->default_wep_key);
1911 eapol->default_wep_key = NULL;
1915 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1917 if (hapd->conf->wep_rekeying_period > 0) {
1918 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1919 ieee802_1x_rekey, hapd, NULL);
1924 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1925 const u8 *data, size_t datalen)
1928 struct sta_info *sta = sta_ctx;
1930 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1931 WLAN_STA_MAYBE_WPS) {
1933 size_t identity_len;
1934 struct eapol_state_machine *sm = sta->eapol_sm;
1936 identity = eap_get_identity(sm->eap, &identity_len);
1938 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1939 os_memcmp(identity, WSC_ID_ENROLLEE,
1940 WSC_ID_ENROLLEE_LEN) == 0) ||
1941 (identity_len == WSC_ID_REGISTRAR_LEN &&
1942 os_memcmp(identity, WSC_ID_REGISTRAR,
1943 WSC_ID_REGISTRAR_LEN) == 0))) {
1944 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1946 sta->flags |= WLAN_STA_WPS;
1949 #endif /* CONFIG_WPS */
1951 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1955 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1956 const u8 *data, size_t datalen)
1958 #ifndef CONFIG_NO_RADIUS
1959 struct hostapd_data *hapd = ctx;
1960 struct sta_info *sta = sta_ctx;
1962 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1963 #endif /* CONFIG_NO_RADIUS */
1967 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1968 int preauth, int remediation)
1970 struct hostapd_data *hapd = ctx;
1971 struct sta_info *sta = sta_ctx;
1973 rsn_preauth_finished(hapd, sta, success);
1975 ieee802_1x_finished(hapd, sta, success, remediation);
1979 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1980 size_t identity_len, int phase2,
1981 struct eap_user *user)
1983 struct hostapd_data *hapd = ctx;
1984 const struct hostapd_eap_user *eap_user;
1988 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
1989 if (eap_user == NULL)
1992 os_memset(user, 0, sizeof(*user));
1993 user->phase2 = phase2;
1994 for (i = 0; i < EAP_MAX_METHODS; i++) {
1995 user->methods[i].vendor = eap_user->methods[i].vendor;
1996 user->methods[i].method = eap_user->methods[i].method;
1999 if (eap_user->password) {
2000 user->password = os_malloc(eap_user->password_len);
2001 if (user->password == NULL)
2003 os_memcpy(user->password, eap_user->password,
2004 eap_user->password_len);
2005 user->password_len = eap_user->password_len;
2006 user->password_hash = eap_user->password_hash;
2008 user->force_version = eap_user->force_version;
2009 user->macacl = eap_user->macacl;
2010 user->ttls_auth = eap_user->ttls_auth;
2011 user->remediation = eap_user->remediation;
2016 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2022 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2024 struct hostapd_data *hapd = ctx;
2025 struct sta_info *sta;
2026 sta = ap_get_sta(hapd, addr);
2027 if (sta == NULL || sta->eapol_sm == NULL)
2033 static void ieee802_1x_logger(void *ctx, const u8 *addr,
2034 eapol_logger_level level, const char *txt)
2036 #ifndef CONFIG_NO_HOSTAPD_LOGGER
2037 struct hostapd_data *hapd = ctx;
2041 case EAPOL_LOGGER_WARNING:
2042 hlevel = HOSTAPD_LEVEL_WARNING;
2044 case EAPOL_LOGGER_INFO:
2045 hlevel = HOSTAPD_LEVEL_INFO;
2047 case EAPOL_LOGGER_DEBUG:
2049 hlevel = HOSTAPD_LEVEL_DEBUG;
2053 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2055 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
2059 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2062 struct hostapd_data *hapd = ctx;
2063 struct sta_info *sta = sta_ctx;
2064 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2068 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2070 struct hostapd_data *hapd = ctx;
2071 struct sta_info *sta = sta_ctx;
2072 ieee802_1x_abort_auth(hapd, sta);
2076 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2079 #ifndef CONFIG_NO_RC4
2080 struct hostapd_data *hapd = ctx;
2081 struct sta_info *sta = sta_ctx;
2082 ieee802_1x_tx_key(hapd, sta);
2083 #endif /* CONFIG_NO_RC4 */
2084 #endif /* CONFIG_FIPS */
2088 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2089 enum eapol_event type)
2091 /* struct hostapd_data *hapd = ctx; */
2092 struct sta_info *sta = sta_ctx;
2094 case EAPOL_AUTH_SM_CHANGE:
2095 wpa_auth_sm_notify(sta->wpa_sm);
2097 case EAPOL_AUTH_REAUTHENTICATE:
2098 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2106 static struct eap_server_erp_key *
2107 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2109 struct hostapd_data *hapd = ctx;
2110 struct eap_server_erp_key *erp;
2112 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2114 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2122 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2124 struct hostapd_data *hapd = ctx;
2126 dl_list_add(&hapd->erp_keys, &erp->list);
2130 #endif /* CONFIG_ERP */
2133 int ieee802_1x_init(struct hostapd_data *hapd)
2136 struct eapol_auth_config conf;
2137 struct eapol_auth_cb cb;
2139 dl_list_init(&hapd->erp_keys);
2141 os_memset(&conf, 0, sizeof(conf));
2143 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2144 conf.wpa = hapd->conf->wpa;
2145 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2146 conf.eap_server = hapd->conf->eap_server;
2147 conf.ssl_ctx = hapd->ssl_ctx;
2148 conf.msg_ctx = hapd->msg_ctx;
2149 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2150 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2151 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2152 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2153 conf.erp_domain = hapd->conf->erp_domain;
2154 conf.erp = hapd->conf->eap_server_erp;
2155 conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
2156 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2157 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2158 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2159 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2160 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2161 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2162 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2163 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2164 conf.tnc = hapd->conf->tnc;
2165 conf.wps = hapd->wps;
2166 conf.fragment_size = hapd->conf->fragment_size;
2167 conf.pwd_group = hapd->conf->pwd_group;
2168 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2169 if (hapd->conf->server_id) {
2170 conf.server_id = (const u8 *) hapd->conf->server_id;
2171 conf.server_id_len = os_strlen(hapd->conf->server_id);
2173 conf.server_id = (const u8 *) "hostapd";
2174 conf.server_id_len = 7;
2177 os_memset(&cb, 0, sizeof(cb));
2178 cb.eapol_send = ieee802_1x_eapol_send;
2179 cb.aaa_send = ieee802_1x_aaa_send;
2180 cb.finished = _ieee802_1x_finished;
2181 cb.get_eap_user = ieee802_1x_get_eap_user;
2182 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2183 cb.logger = ieee802_1x_logger;
2184 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2185 cb.abort_auth = _ieee802_1x_abort_auth;
2186 cb.tx_key = _ieee802_1x_tx_key;
2187 cb.eapol_event = ieee802_1x_eapol_event;
2189 cb.erp_get_key = ieee802_1x_erp_get_key;
2190 cb.erp_add_key = ieee802_1x_erp_add_key;
2191 #endif /* CONFIG_ERP */
2193 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2194 if (hapd->eapol_auth == NULL)
2197 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2198 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2201 #ifndef CONFIG_NO_RADIUS
2202 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2203 ieee802_1x_receive_auth, hapd))
2205 #endif /* CONFIG_NO_RADIUS */
2207 if (hapd->conf->default_wep_key_len) {
2208 for (i = 0; i < 4; i++)
2209 hostapd_drv_set_key(hapd->conf->iface, hapd,
2210 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2213 ieee802_1x_rekey(hapd, NULL);
2215 if (hapd->eapol_auth->default_wep_key == NULL)
2223 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2225 struct eap_server_erp_key *erp;
2227 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2229 dl_list_del(&erp->list);
2230 bin_clear_free(erp, sizeof(*erp));
2235 void ieee802_1x_deinit(struct hostapd_data *hapd)
2237 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2239 if (hapd->driver && hapd->drv_priv &&
2240 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2241 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2243 eapol_auth_deinit(hapd->eapol_auth);
2244 hapd->eapol_auth = NULL;
2246 ieee802_1x_erp_flush(hapd);
2250 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2251 const u8 *buf, size_t len, int ack)
2253 struct ieee80211_hdr *hdr;
2255 const unsigned char rfc1042_hdr[ETH_ALEN] =
2256 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2260 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2263 hdr = (struct ieee80211_hdr *) buf;
2264 pos = (u8 *) (hdr + 1);
2265 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2267 pos += sizeof(rfc1042_hdr);
2268 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2272 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2277 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2278 const u8 *buf, int len, int ack)
2280 const struct ieee802_1x_hdr *xhdr =
2281 (const struct ieee802_1x_hdr *) buf;
2282 const u8 *pos = buf + sizeof(*xhdr);
2283 struct ieee802_1x_eapol_key *key;
2285 if (len < (int) sizeof(*xhdr))
2287 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2288 "type=%d length=%d - ack=%d",
2289 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2290 be_to_host16(xhdr->length), ack);
2292 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2295 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2296 const struct wpa_eapol_key *wpa;
2297 wpa = (const struct wpa_eapol_key *) pos;
2298 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2299 wpa->type == EAPOL_KEY_TYPE_WPA)
2300 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2304 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2305 * or Authenticator state machines, but EAPOL-Key packets are not
2306 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2307 * packets couple of times because otherwise STA keys become
2308 * unsynchronized with AP. */
2309 if (!ack && pos + sizeof(*key) <= buf + len) {
2310 key = (struct ieee802_1x_eapol_key *) pos;
2311 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2312 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2313 "frame (%scast index=%d)",
2314 key->key_index & BIT(7) ? "uni" : "broad",
2315 key->key_index & ~BIT(7));
2316 /* TODO: re-send EAPOL-Key couple of times (with short delay
2317 * between them?). If all attempt fail, report error and
2318 * deauthenticate STA so that it will get new keys when
2319 * authenticating again (e.g., after returning in range).
2320 * Separate limit/transmit state needed both for unicast and
2321 * broadcast keys(?) */
2323 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2324 * to here and change the key only if the EAPOL-Key packet was Acked.
2331 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2333 if (sm == NULL || sm->identity == NULL)
2336 *len = sm->identity_len;
2337 return sm->identity;
2341 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2344 if (sm == NULL || sm->radius_class.attr == NULL ||
2345 idx >= (int) sm->radius_class.count)
2348 *len = sm->radius_class.attr[idx].len;
2349 return sm->radius_class.attr[idx].data;
2353 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2357 return sm->radius_cui;
2361 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2367 *len = sm->eap_if->eapKeyDataLen;
2368 return sm->eap_if->eapKeyData;
2372 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2377 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2378 eapol_auth_step(sm);
2382 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2387 sm->portValid = valid ? TRUE : FALSE;
2388 eapol_auth_step(sm);
2392 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2397 sm->flags |= EAPOL_SM_PREAUTH;
2399 sm->flags &= ~EAPOL_SM_PREAUTH;
2403 static const char * bool_txt(Boolean val)
2405 return val ? "TRUE" : "FALSE";
2409 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2416 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2417 char *buf, size_t buflen)
2420 struct eapol_state_machine *sm = sta->eapol_sm;
2421 struct os_reltime diff;
2428 ret = os_snprintf(buf + len, buflen - len,
2429 "dot1xPaePortNumber=%d\n"
2430 "dot1xPaePortProtocolVersion=%d\n"
2431 "dot1xPaePortCapabilities=1\n"
2432 "dot1xPaePortInitialize=%d\n"
2433 "dot1xPaePortReauthenticate=FALSE\n",
2437 if (os_snprintf_error(buflen - len, ret))
2441 /* dot1xAuthConfigTable */
2442 ret = os_snprintf(buf + len, buflen - len,
2443 "dot1xAuthPaeState=%d\n"
2444 "dot1xAuthBackendAuthState=%d\n"
2445 "dot1xAuthAdminControlledDirections=%d\n"
2446 "dot1xAuthOperControlledDirections=%d\n"
2447 "dot1xAuthAuthControlledPortStatus=%d\n"
2448 "dot1xAuthAuthControlledPortControl=%d\n"
2449 "dot1xAuthQuietPeriod=%u\n"
2450 "dot1xAuthServerTimeout=%u\n"
2451 "dot1xAuthReAuthPeriod=%u\n"
2452 "dot1xAuthReAuthEnabled=%s\n"
2453 "dot1xAuthKeyTxEnabled=%s\n",
2454 sm->auth_pae_state + 1,
2455 sm->be_auth_state + 1,
2456 sm->adminControlledDirections,
2457 sm->operControlledDirections,
2463 bool_txt(sm->reAuthEnabled),
2464 bool_txt(sm->keyTxEnabled));
2465 if (os_snprintf_error(buflen - len, ret))
2469 /* dot1xAuthStatsTable */
2470 ret = os_snprintf(buf + len, buflen - len,
2471 "dot1xAuthEapolFramesRx=%u\n"
2472 "dot1xAuthEapolFramesTx=%u\n"
2473 "dot1xAuthEapolStartFramesRx=%u\n"
2474 "dot1xAuthEapolLogoffFramesRx=%u\n"
2475 "dot1xAuthEapolRespIdFramesRx=%u\n"
2476 "dot1xAuthEapolRespFramesRx=%u\n"
2477 "dot1xAuthEapolReqIdFramesTx=%u\n"
2478 "dot1xAuthEapolReqFramesTx=%u\n"
2479 "dot1xAuthInvalidEapolFramesRx=%u\n"
2480 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2481 "dot1xAuthLastEapolFrameVersion=%u\n"
2482 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2483 sm->dot1xAuthEapolFramesRx,
2484 sm->dot1xAuthEapolFramesTx,
2485 sm->dot1xAuthEapolStartFramesRx,
2486 sm->dot1xAuthEapolLogoffFramesRx,
2487 sm->dot1xAuthEapolRespIdFramesRx,
2488 sm->dot1xAuthEapolRespFramesRx,
2489 sm->dot1xAuthEapolReqIdFramesTx,
2490 sm->dot1xAuthEapolReqFramesTx,
2491 sm->dot1xAuthInvalidEapolFramesRx,
2492 sm->dot1xAuthEapLengthErrorFramesRx,
2493 sm->dot1xAuthLastEapolFrameVersion,
2495 if (os_snprintf_error(buflen - len, ret))
2499 /* dot1xAuthDiagTable */
2500 ret = os_snprintf(buf + len, buflen - len,
2501 "dot1xAuthEntersConnecting=%u\n"
2502 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2503 "dot1xAuthEntersAuthenticating=%u\n"
2504 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2505 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2506 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2507 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2508 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2509 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2510 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2511 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2512 "dot1xAuthBackendResponses=%u\n"
2513 "dot1xAuthBackendAccessChallenges=%u\n"
2514 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2515 "dot1xAuthBackendAuthSuccesses=%u\n"
2516 "dot1xAuthBackendAuthFails=%u\n",
2517 sm->authEntersConnecting,
2518 sm->authEapLogoffsWhileConnecting,
2519 sm->authEntersAuthenticating,
2520 sm->authAuthSuccessesWhileAuthenticating,
2521 sm->authAuthTimeoutsWhileAuthenticating,
2522 sm->authAuthFailWhileAuthenticating,
2523 sm->authAuthEapStartsWhileAuthenticating,
2524 sm->authAuthEapLogoffWhileAuthenticating,
2525 sm->authAuthReauthsWhileAuthenticated,
2526 sm->authAuthEapStartsWhileAuthenticated,
2527 sm->authAuthEapLogoffWhileAuthenticated,
2528 sm->backendResponses,
2529 sm->backendAccessChallenges,
2530 sm->backendOtherRequestsToSupplicant,
2531 sm->backendAuthSuccesses,
2532 sm->backendAuthFails);
2533 if (os_snprintf_error(buflen - len, ret))
2537 /* dot1xAuthSessionStatsTable */
2538 os_reltime_age(&sta->acct_session_start, &diff);
2539 ret = os_snprintf(buf + len, buflen - len,
2540 /* TODO: dot1xAuthSessionOctetsRx */
2541 /* TODO: dot1xAuthSessionOctetsTx */
2542 /* TODO: dot1xAuthSessionFramesRx */
2543 /* TODO: dot1xAuthSessionFramesTx */
2544 "dot1xAuthSessionId=%016llX\n"
2545 "dot1xAuthSessionAuthenticMethod=%d\n"
2546 "dot1xAuthSessionTime=%u\n"
2547 "dot1xAuthSessionTerminateCause=999\n"
2548 "dot1xAuthSessionUserName=%s\n",
2549 (unsigned long long) sta->acct_session_id,
2550 (wpa_key_mgmt_wpa_ieee8021x(
2551 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2553 (unsigned int) diff.sec,
2555 if (os_snprintf_error(buflen - len, ret))
2559 if (sm->acct_multi_session_id) {
2560 ret = os_snprintf(buf + len, buflen - len,
2561 "authMultiSessionId=%016llX\n",
2562 (unsigned long long)
2563 sm->acct_multi_session_id);
2564 if (os_snprintf_error(buflen - len, ret))
2569 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2570 name2 = eap_server_get_name(0, sm->eap_type_supp);
2571 ret = os_snprintf(buf + len, buflen - len,
2572 "last_eap_type_as=%d (%s)\n"
2573 "last_eap_type_sta=%d (%s)\n",
2574 sm->eap_type_authsrv, name1,
2575 sm->eap_type_supp, name2);
2576 if (os_snprintf_error(buflen - len, ret))
2585 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
2587 struct hostapd_data *hapd = eloop_ctx;
2588 struct sta_info *sta = timeout_ctx;
2590 if (sta->remediation) {
2591 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2592 MACSTR " to indicate Subscription Remediation",
2593 MAC2STR(sta->addr));
2594 hs20_send_wnm_notification(hapd, sta->addr,
2595 sta->remediation_method,
2596 sta->remediation_url);
2597 os_free(sta->remediation_url);
2598 sta->remediation_url = NULL;
2601 if (sta->hs20_deauth_req) {
2602 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2603 MACSTR " to indicate imminent deauthentication",
2604 MAC2STR(sta->addr));
2605 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
2606 sta->hs20_deauth_req);
2609 #endif /* CONFIG_HS20 */
2612 static void ieee802_1x_finished(struct hostapd_data *hapd,
2613 struct sta_info *sta, int success,
2618 /* TODO: get PMKLifetime from WPA parameters */
2619 static const int dot11RSNAConfigPMKLifetime = 43200;
2620 unsigned int session_timeout;
2623 if (remediation && !sta->remediation) {
2624 sta->remediation = 1;
2625 os_free(sta->remediation_url);
2626 sta->remediation_url =
2627 os_strdup(hapd->conf->subscr_remediation_url);
2628 sta->remediation_method = 1; /* SOAP-XML SPP */
2631 if (success && (sta->remediation || sta->hs20_deauth_req)) {
2632 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
2633 MACSTR " in 100 ms", MAC2STR(sta->addr));
2634 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
2635 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
2638 #endif /* CONFIG_HS20 */
2640 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2641 if (sta->session_timeout_set)
2642 session_timeout = sta->session_timeout;
2644 session_timeout = dot11RSNAConfigPMKLifetime;
2645 if (success && key && len >= PMK_LEN && !sta->remediation &&
2646 !sta->hs20_deauth_requested &&
2647 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
2648 sta->eapol_sm) == 0) {
2649 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2650 HOSTAPD_LEVEL_DEBUG,
2651 "Added PMKSA cache entry (IEEE 802.1X)");
2656 * Many devices require deauthentication after WPS provisioning
2657 * and some may not be be able to do that themselves, so
2658 * disconnect the client here. In addition, this may also
2659 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2660 * the EAPOL PAE state machine would remain in HELD state for
2661 * considerable amount of time and some EAP methods, like
2662 * EAP-FAST with anonymous provisioning, may require another
2663 * EAPOL authentication to be started to complete connection.
2665 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2666 "disconnection after EAP-Failure");
2667 /* Add a small sleep to increase likelihood of previously
2668 * requested EAP-Failure TX getting out before this should the
2669 * driver reorder operations.
2672 ap_sta_disconnect(hapd, sta, sta->addr,
2673 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
2674 hostapd_wps_eap_completed(hapd);