Home | History | Annotate | Download | only in ap
      1 /*
      2  * hostapd / IEEE 802.1X-2004 Authenticator
      3  * Copyright (c) 2002-2019, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "utils/includes.h"
     10 
     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"
     23 #include "p2p/p2p.h"
     24 #include "hostapd.h"
     25 #include "accounting.h"
     26 #include "sta_info.h"
     27 #include "wpa_auth.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"
     33 #include "hs20.h"
     34 /* FIX: Not really a good thing to require ieee802_11.h here.. (FILS) */
     35 #include "ieee802_11.h"
     36 #include "ieee802_1x.h"
     37 
     38 
     39 #ifdef CONFIG_HS20
     40 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
     41 #endif /* CONFIG_HS20 */
     42 static void ieee802_1x_finished(struct hostapd_data *hapd,
     43 				struct sta_info *sta, int success,
     44 				int remediation);
     45 
     46 
     47 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
     48 			    u8 type, const u8 *data, size_t datalen)
     49 {
     50 	u8 *buf;
     51 	struct ieee802_1x_hdr *xhdr;
     52 	size_t len;
     53 	int encrypt = 0;
     54 
     55 	len = sizeof(*xhdr) + datalen;
     56 	buf = os_zalloc(len);
     57 	if (buf == NULL) {
     58 		wpa_printf(MSG_ERROR, "malloc() failed for "
     59 			   "ieee802_1x_send(len=%lu)",
     60 			   (unsigned long) len);
     61 		return;
     62 	}
     63 
     64 	xhdr = (struct ieee802_1x_hdr *) buf;
     65 	xhdr->version = hapd->conf->eapol_version;
     66 	xhdr->type = type;
     67 	xhdr->length = host_to_be16(datalen);
     68 
     69 	if (datalen > 0 && data != NULL)
     70 		os_memcpy(xhdr + 1, data, datalen);
     71 
     72 	if (wpa_auth_pairwise_set(sta->wpa_sm))
     73 		encrypt = 1;
     74 #ifdef CONFIG_TESTING_OPTIONS
     75 	if (hapd->ext_eapol_frame_io) {
     76 		size_t hex_len = 2 * len + 1;
     77 		char *hex = os_malloc(hex_len);
     78 
     79 		if (hex) {
     80 			wpa_snprintf_hex(hex, hex_len, buf, len);
     81 			wpa_msg(hapd->msg_ctx, MSG_INFO,
     82 				"EAPOL-TX " MACSTR " %s",
     83 				MAC2STR(sta->addr), hex);
     84 			os_free(hex);
     85 		}
     86 	} else
     87 #endif /* CONFIG_TESTING_OPTIONS */
     88 	if (sta->flags & WLAN_STA_PREAUTH) {
     89 		rsn_preauth_send(hapd, sta, buf, len);
     90 	} else {
     91 		hostapd_drv_hapd_send_eapol(
     92 			hapd, sta->addr, buf, len,
     93 			encrypt, hostapd_sta_flags_to_drv(sta->flags));
     94 	}
     95 
     96 	os_free(buf);
     97 }
     98 
     99 
    100 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
    101 				   struct sta_info *sta, int authorized)
    102 {
    103 	int res;
    104 
    105 	if (sta->flags & WLAN_STA_PREAUTH)
    106 		return;
    107 
    108 	if (authorized) {
    109 		ap_sta_set_authorized(hapd, sta, 1);
    110 		res = hostapd_set_authorized(hapd, sta, 1);
    111 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    112 			       HOSTAPD_LEVEL_DEBUG, "authorizing port");
    113 	} else {
    114 		ap_sta_set_authorized(hapd, sta, 0);
    115 		res = hostapd_set_authorized(hapd, sta, 0);
    116 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    117 			       HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
    118 	}
    119 
    120 	if (res && errno != ENOENT) {
    121 		wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
    122 			   " flags for kernel driver (errno=%d).",
    123 			   MAC2STR(sta->addr), errno);
    124 	}
    125 
    126 	if (authorized) {
    127 		os_get_reltime(&sta->connected_time);
    128 		accounting_sta_start(hapd, sta);
    129 	}
    130 }
    131 
    132 
    133 #ifndef CONFIG_FIPS
    134 #ifndef CONFIG_NO_RC4
    135 
    136 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
    137 				  struct sta_info *sta,
    138 				  int idx, int broadcast,
    139 				  u8 *key_data, size_t key_len)
    140 {
    141 	u8 *buf, *ekey;
    142 	struct ieee802_1x_hdr *hdr;
    143 	struct ieee802_1x_eapol_key *key;
    144 	size_t len, ekey_len;
    145 	struct eapol_state_machine *sm = sta->eapol_sm;
    146 
    147 	if (sm == NULL)
    148 		return;
    149 
    150 	len = sizeof(*key) + key_len;
    151 	buf = os_zalloc(sizeof(*hdr) + len);
    152 	if (buf == NULL)
    153 		return;
    154 
    155 	hdr = (struct ieee802_1x_hdr *) buf;
    156 	key = (struct ieee802_1x_eapol_key *) (hdr + 1);
    157 	key->type = EAPOL_KEY_TYPE_RC4;
    158 	WPA_PUT_BE16(key->key_length, key_len);
    159 	wpa_get_ntp_timestamp(key->replay_counter);
    160 
    161 	if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
    162 		wpa_printf(MSG_ERROR, "Could not get random numbers");
    163 		os_free(buf);
    164 		return;
    165 	}
    166 
    167 	key->key_index = idx | (broadcast ? 0 : BIT(7));
    168 	if (hapd->conf->eapol_key_index_workaround) {
    169 		/* According to some information, WinXP Supplicant seems to
    170 		 * interpret bit7 as an indication whether the key is to be
    171 		 * activated, so make it possible to enable workaround that
    172 		 * sets this bit for all keys. */
    173 		key->key_index |= BIT(7);
    174 	}
    175 
    176 	/* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
    177 	 * MSK[32..63] is used to sign the message. */
    178 	if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
    179 		wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
    180 			   "and signing EAPOL-Key");
    181 		os_free(buf);
    182 		return;
    183 	}
    184 	os_memcpy((u8 *) (key + 1), key_data, key_len);
    185 	ekey_len = sizeof(key->key_iv) + 32;
    186 	ekey = os_malloc(ekey_len);
    187 	if (ekey == NULL) {
    188 		wpa_printf(MSG_ERROR, "Could not encrypt key");
    189 		os_free(buf);
    190 		return;
    191 	}
    192 	os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
    193 	os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
    194 	rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
    195 	os_free(ekey);
    196 
    197 	/* This header is needed here for HMAC-MD5, but it will be regenerated
    198 	 * in ieee802_1x_send() */
    199 	hdr->version = hapd->conf->eapol_version;
    200 	hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
    201 	hdr->length = host_to_be16(len);
    202 	hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
    203 		 key->key_signature);
    204 
    205 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
    206 		   " (%s index=%d)", MAC2STR(sm->addr),
    207 		   broadcast ? "broadcast" : "unicast", idx);
    208 	ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
    209 	if (sta->eapol_sm)
    210 		sta->eapol_sm->dot1xAuthEapolFramesTx++;
    211 	os_free(buf);
    212 }
    213 
    214 
    215 static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
    216 {
    217 	struct eapol_authenticator *eapol = hapd->eapol_auth;
    218 	struct eapol_state_machine *sm = sta->eapol_sm;
    219 
    220 	if (sm == NULL || !sm->eap_if->eapKeyData)
    221 		return;
    222 
    223 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
    224 		   MAC2STR(sta->addr));
    225 
    226 #ifndef CONFIG_NO_VLAN
    227 	if (sta->vlan_id > 0) {
    228 		wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
    229 		return;
    230 	}
    231 #endif /* CONFIG_NO_VLAN */
    232 
    233 	if (eapol->default_wep_key) {
    234 		ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
    235 				      eapol->default_wep_key,
    236 				      hapd->conf->default_wep_key_len);
    237 	}
    238 
    239 	if (hapd->conf->individual_wep_key_len > 0) {
    240 		u8 *ikey;
    241 		ikey = os_malloc(hapd->conf->individual_wep_key_len);
    242 		if (ikey == NULL ||
    243 		    random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
    244 		{
    245 			wpa_printf(MSG_ERROR, "Could not generate random "
    246 				   "individual WEP key.");
    247 			os_free(ikey);
    248 			return;
    249 		}
    250 
    251 		wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
    252 				ikey, hapd->conf->individual_wep_key_len);
    253 
    254 		ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
    255 				      hapd->conf->individual_wep_key_len);
    256 
    257 		/* TODO: set encryption in TX callback, i.e., only after STA
    258 		 * has ACKed EAPOL-Key frame */
    259 		if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
    260 					sta->addr, 0, 1, NULL, 0, ikey,
    261 					hapd->conf->individual_wep_key_len)) {
    262 			wpa_printf(MSG_ERROR, "Could not set individual WEP "
    263 				   "encryption.");
    264 		}
    265 
    266 		os_free(ikey);
    267 	}
    268 }
    269 
    270 #endif /* CONFIG_NO_RC4 */
    271 #endif /* CONFIG_FIPS */
    272 
    273 
    274 const char *radius_mode_txt(struct hostapd_data *hapd)
    275 {
    276 	switch (hapd->iface->conf->hw_mode) {
    277 	case HOSTAPD_MODE_IEEE80211AD:
    278 		return "802.11ad";
    279 	case HOSTAPD_MODE_IEEE80211A:
    280 		return "802.11a";
    281 	case HOSTAPD_MODE_IEEE80211G:
    282 		return "802.11g";
    283 	case HOSTAPD_MODE_IEEE80211B:
    284 	default:
    285 		return "802.11b";
    286 	}
    287 }
    288 
    289 
    290 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
    291 {
    292 	int i;
    293 	u8 rate = 0;
    294 
    295 	for (i = 0; i < sta->supported_rates_len; i++)
    296 		if ((sta->supported_rates[i] & 0x7f) > rate)
    297 			rate = sta->supported_rates[i] & 0x7f;
    298 
    299 	return rate;
    300 }
    301 
    302 
    303 #ifndef CONFIG_NO_RADIUS
    304 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
    305 				      struct eapol_state_machine *sm,
    306 				      const u8 *eap, size_t len)
    307 {
    308 	const u8 *identity;
    309 	size_t identity_len;
    310 	const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
    311 
    312 	if (len <= sizeof(struct eap_hdr) ||
    313 	    (hdr->code == EAP_CODE_RESPONSE &&
    314 	     eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
    315 	    (hdr->code == EAP_CODE_INITIATE &&
    316 	     eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
    317 	    (hdr->code != EAP_CODE_RESPONSE &&
    318 	     hdr->code != EAP_CODE_INITIATE))
    319 		return;
    320 
    321 	eap_erp_update_identity(sm->eap, eap, len);
    322 	identity = eap_get_identity(sm->eap, &identity_len);
    323 	if (identity == NULL)
    324 		return;
    325 
    326 	/* Save station identity for future RADIUS packets */
    327 	os_free(sm->identity);
    328 	sm->identity = (u8 *) dup_binstr(identity, identity_len);
    329 	if (sm->identity == NULL) {
    330 		sm->identity_len = 0;
    331 		return;
    332 	}
    333 
    334 	sm->identity_len = identity_len;
    335 	hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
    336 		       HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
    337 	sm->dot1xAuthEapolRespIdFramesRx++;
    338 }
    339 
    340 
    341 static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
    342 					  struct hostapd_radius_attr *req_attr,
    343 					  struct sta_info *sta,
    344 					  struct radius_msg *msg)
    345 {
    346 	u32 suite;
    347 	int ver, val;
    348 
    349 	ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
    350 	val = wpa_auth_get_pairwise(sta->wpa_sm);
    351 	suite = wpa_cipher_to_suite(ver, val);
    352 	if (val != -1 &&
    353 	    !hostapd_config_get_radius_attr(req_attr,
    354 					    RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
    355 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
    356 				       suite)) {
    357 		wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
    358 		return -1;
    359 	}
    360 
    361 	suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
    362 				     hapd->conf->osen) ?
    363 				    WPA_PROTO_RSN : WPA_PROTO_WPA,
    364 				    hapd->conf->wpa_group);
    365 	if (!hostapd_config_get_radius_attr(req_attr,
    366 					    RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
    367 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
    368 				       suite)) {
    369 		wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
    370 		return -1;
    371 	}
    372 
    373 	val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
    374 	suite = wpa_akm_to_suite(val);
    375 	if (val != -1 &&
    376 	    !hostapd_config_get_radius_attr(req_attr,
    377 					    RADIUS_ATTR_WLAN_AKM_SUITE) &&
    378 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
    379 				       suite)) {
    380 		wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
    381 		return -1;
    382 	}
    383 
    384 #ifdef CONFIG_IEEE80211W
    385 	if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
    386 		suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
    387 					    hapd->conf->group_mgmt_cipher);
    388 		if (!hostapd_config_get_radius_attr(
    389 			    req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
    390 		    !radius_msg_add_attr_int32(
    391 			    msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
    392 			wpa_printf(MSG_ERROR,
    393 				   "Could not add WLAN-Group-Mgmt-Cipher");
    394 			return -1;
    395 		}
    396 	}
    397 #endif /* CONFIG_IEEE80211W */
    398 
    399 	return 0;
    400 }
    401 
    402 
    403 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
    404 				      struct hostapd_radius_attr *req_attr,
    405 				      struct sta_info *sta,
    406 				      struct radius_msg *msg)
    407 {
    408 	char buf[128];
    409 
    410 	if (!hostapd_config_get_radius_attr(req_attr,
    411 					    RADIUS_ATTR_SERVICE_TYPE) &&
    412 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
    413 				       RADIUS_SERVICE_TYPE_FRAMED)) {
    414 		wpa_printf(MSG_ERROR, "Could not add Service-Type");
    415 		return -1;
    416 	}
    417 
    418 	if (!hostapd_config_get_radius_attr(req_attr,
    419 					    RADIUS_ATTR_NAS_PORT) &&
    420 	    sta->aid > 0 &&
    421 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
    422 		wpa_printf(MSG_ERROR, "Could not add NAS-Port");
    423 		return -1;
    424 	}
    425 
    426 	os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
    427 		    MAC2STR(sta->addr));
    428 	buf[sizeof(buf) - 1] = '\0';
    429 	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
    430 				 (u8 *) buf, os_strlen(buf))) {
    431 		wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
    432 		return -1;
    433 	}
    434 
    435 	if (sta->flags & WLAN_STA_PREAUTH) {
    436 		os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
    437 			   sizeof(buf));
    438 	} else {
    439 		os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
    440 			    radius_sta_rate(hapd, sta) / 2,
    441 			    (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
    442 			    radius_mode_txt(hapd));
    443 		buf[sizeof(buf) - 1] = '\0';
    444 	}
    445 	if (!hostapd_config_get_radius_attr(req_attr,
    446 					    RADIUS_ATTR_CONNECT_INFO) &&
    447 	    !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
    448 				 (u8 *) buf, os_strlen(buf))) {
    449 		wpa_printf(MSG_ERROR, "Could not add Connect-Info");
    450 		return -1;
    451 	}
    452 
    453 	if (sta->acct_session_id) {
    454 		os_snprintf(buf, sizeof(buf), "%016llX",
    455 			    (unsigned long long) sta->acct_session_id);
    456 		if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
    457 					 (u8 *) buf, os_strlen(buf))) {
    458 			wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
    459 			return -1;
    460 		}
    461 	}
    462 
    463 	if ((hapd->conf->wpa & 2) &&
    464 	    !hapd->conf->disable_pmksa_caching &&
    465 	    sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
    466 		os_snprintf(buf, sizeof(buf), "%016llX",
    467 			    (unsigned long long)
    468 			    sta->eapol_sm->acct_multi_session_id);
    469 		if (!radius_msg_add_attr(
    470 			    msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
    471 			    (u8 *) buf, os_strlen(buf))) {
    472 			wpa_printf(MSG_INFO,
    473 				   "Could not add Acct-Multi-Session-Id");
    474 			return -1;
    475 		}
    476 	}
    477 
    478 #ifdef CONFIG_IEEE80211R_AP
    479 	if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
    480 	    sta->wpa_sm &&
    481 	    (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
    482 	     sta->auth_alg == WLAN_AUTH_FT) &&
    483 	    !hostapd_config_get_radius_attr(req_attr,
    484 					    RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
    485 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
    486 				       WPA_GET_BE16(
    487 					       hapd->conf->mobility_domain))) {
    488 		wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
    489 		return -1;
    490 	}
    491 #endif /* CONFIG_IEEE80211R_AP */
    492 
    493 	if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
    494 	    add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
    495 		return -1;
    496 
    497 	return 0;
    498 }
    499 
    500 
    501 int add_common_radius_attr(struct hostapd_data *hapd,
    502 			   struct hostapd_radius_attr *req_attr,
    503 			   struct sta_info *sta,
    504 			   struct radius_msg *msg)
    505 {
    506 	char buf[128];
    507 	struct hostapd_radius_attr *attr;
    508 	int len;
    509 
    510 	if (!hostapd_config_get_radius_attr(req_attr,
    511 					    RADIUS_ATTR_NAS_IP_ADDRESS) &&
    512 	    hapd->conf->own_ip_addr.af == AF_INET &&
    513 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
    514 				 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
    515 		wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
    516 		return -1;
    517 	}
    518 
    519 #ifdef CONFIG_IPV6
    520 	if (!hostapd_config_get_radius_attr(req_attr,
    521 					    RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
    522 	    hapd->conf->own_ip_addr.af == AF_INET6 &&
    523 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
    524 				 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
    525 		wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
    526 		return -1;
    527 	}
    528 #endif /* CONFIG_IPV6 */
    529 
    530 	if (!hostapd_config_get_radius_attr(req_attr,
    531 					    RADIUS_ATTR_NAS_IDENTIFIER) &&
    532 	    hapd->conf->nas_identifier &&
    533 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
    534 				 (u8 *) hapd->conf->nas_identifier,
    535 				 os_strlen(hapd->conf->nas_identifier))) {
    536 		wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
    537 		return -1;
    538 	}
    539 
    540 	len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
    541 			  MAC2STR(hapd->own_addr));
    542 	os_memcpy(&buf[len], hapd->conf->ssid.ssid,
    543 		  hapd->conf->ssid.ssid_len);
    544 	len += hapd->conf->ssid.ssid_len;
    545 	if (!hostapd_config_get_radius_attr(req_attr,
    546 					    RADIUS_ATTR_CALLED_STATION_ID) &&
    547 	    !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
    548 				 (u8 *) buf, len)) {
    549 		wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
    550 		return -1;
    551 	}
    552 
    553 	if (!hostapd_config_get_radius_attr(req_attr,
    554 					    RADIUS_ATTR_NAS_PORT_TYPE) &&
    555 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
    556 				       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
    557 		wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
    558 		return -1;
    559 	}
    560 
    561 #ifdef CONFIG_INTERWORKING
    562 	if (hapd->conf->interworking &&
    563 	    !is_zero_ether_addr(hapd->conf->hessid)) {
    564 		os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
    565 			    MAC2STR(hapd->conf->hessid));
    566 		buf[sizeof(buf) - 1] = '\0';
    567 		if (!hostapd_config_get_radius_attr(req_attr,
    568 						    RADIUS_ATTR_WLAN_HESSID) &&
    569 		    !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
    570 					 (u8 *) buf, os_strlen(buf))) {
    571 			wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
    572 			return -1;
    573 		}
    574 	}
    575 #endif /* CONFIG_INTERWORKING */
    576 
    577 	if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
    578 		return -1;
    579 
    580 	for (attr = req_attr; attr; attr = attr->next) {
    581 		if (!radius_msg_add_attr(msg, attr->type,
    582 					 wpabuf_head(attr->val),
    583 					 wpabuf_len(attr->val))) {
    584 			wpa_printf(MSG_ERROR, "Could not add RADIUS "
    585 				   "attribute");
    586 			return -1;
    587 		}
    588 	}
    589 
    590 	return 0;
    591 }
    592 
    593 
    594 void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
    595 				   struct sta_info *sta,
    596 				   const u8 *eap, size_t len)
    597 {
    598 	struct radius_msg *msg;
    599 	struct eapol_state_machine *sm = sta->eapol_sm;
    600 
    601 	if (sm == NULL)
    602 		return;
    603 
    604 	ieee802_1x_learn_identity(hapd, sm, eap, len);
    605 
    606 	wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
    607 		   "packet");
    608 
    609 	sm->radius_identifier = radius_client_get_id(hapd->radius);
    610 	msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
    611 			     sm->radius_identifier);
    612 	if (msg == NULL) {
    613 		wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
    614 		return;
    615 	}
    616 
    617 	if (radius_msg_make_authenticator(msg) < 0) {
    618 		wpa_printf(MSG_INFO, "Could not make Request Authenticator");
    619 		goto fail;
    620 	}
    621 
    622 	if (sm->identity &&
    623 	    !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
    624 				 sm->identity, sm->identity_len)) {
    625 		wpa_printf(MSG_INFO, "Could not add User-Name");
    626 		goto fail;
    627 	}
    628 
    629 	if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
    630 				   msg) < 0)
    631 		goto fail;
    632 
    633 	/* TODO: should probably check MTU from driver config; 2304 is max for
    634 	 * IEEE 802.11, but use 1400 to avoid problems with too large packets
    635 	 */
    636 	if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
    637 					    RADIUS_ATTR_FRAMED_MTU) &&
    638 	    !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
    639 		wpa_printf(MSG_INFO, "Could not add Framed-MTU");
    640 		goto fail;
    641 	}
    642 
    643 	if (!radius_msg_add_eap(msg, eap, len)) {
    644 		wpa_printf(MSG_INFO, "Could not add EAP-Message");
    645 		goto fail;
    646 	}
    647 
    648 	/* State attribute must be copied if and only if this packet is
    649 	 * Access-Request reply to the previous Access-Challenge */
    650 	if (sm->last_recv_radius &&
    651 	    radius_msg_get_hdr(sm->last_recv_radius)->code ==
    652 	    RADIUS_CODE_ACCESS_CHALLENGE) {
    653 		int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
    654 					       RADIUS_ATTR_STATE);
    655 		if (res < 0) {
    656 			wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
    657 			goto fail;
    658 		}
    659 		if (res > 0) {
    660 			wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
    661 		}
    662 	}
    663 
    664 	if (hapd->conf->radius_request_cui) {
    665 		const u8 *cui;
    666 		size_t cui_len;
    667 		/* Add previously learned CUI or nul CUI to request CUI */
    668 		if (sm->radius_cui) {
    669 			cui = wpabuf_head(sm->radius_cui);
    670 			cui_len = wpabuf_len(sm->radius_cui);
    671 		} else {
    672 			cui = (const u8 *) "\0";
    673 			cui_len = 1;
    674 		}
    675 		if (!radius_msg_add_attr(msg,
    676 					 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
    677 					 cui, cui_len)) {
    678 			wpa_printf(MSG_ERROR, "Could not add CUI");
    679 			goto fail;
    680 		}
    681 	}
    682 
    683 #ifdef CONFIG_HS20
    684 	if (hapd->conf->hs20) {
    685 		u8 ver = hapd->conf->hs20_release - 1;
    686 
    687 		if (!radius_msg_add_wfa(
    688 			    msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
    689 			    &ver, 1)) {
    690 			wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
    691 				   "version");
    692 			goto fail;
    693 		}
    694 
    695 		if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
    696 			const u8 *pos;
    697 			u8 buf[3];
    698 			u16 id;
    699 			pos = wpabuf_head_u8(sta->hs20_ie);
    700 			buf[0] = (*pos) >> 4;
    701 			if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
    702 			    wpabuf_len(sta->hs20_ie) >= 3)
    703 				id = WPA_GET_LE16(pos + 1);
    704 			else
    705 				id = 0;
    706 			WPA_PUT_BE16(buf + 1, id);
    707 			if (!radius_msg_add_wfa(
    708 				    msg,
    709 				    RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
    710 				    buf, sizeof(buf))) {
    711 				wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
    712 					   "STA version");
    713 				goto fail;
    714 			}
    715 		}
    716 
    717 		if (sta->roaming_consortium &&
    718 		    !radius_msg_add_wfa(
    719 			    msg, RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM,
    720 			    wpabuf_head(sta->roaming_consortium),
    721 			    wpabuf_len(sta->roaming_consortium))) {
    722 			wpa_printf(MSG_ERROR,
    723 				   "Could not add HS 2.0 Roaming Consortium");
    724 			goto fail;
    725 		}
    726 
    727 		if (hapd->conf->t_c_filename) {
    728 			be32 timestamp;
    729 
    730 			if (!radius_msg_add_wfa(
    731 				    msg,
    732 				    RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME,
    733 				    (const u8 *) hapd->conf->t_c_filename,
    734 				    os_strlen(hapd->conf->t_c_filename))) {
    735 				wpa_printf(MSG_ERROR,
    736 					   "Could not add HS 2.0 T&C Filename");
    737 				goto fail;
    738 			}
    739 
    740 			timestamp = host_to_be32(hapd->conf->t_c_timestamp);
    741 			if (!radius_msg_add_wfa(
    742 				    msg,
    743 				    RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP,
    744 				    (const u8 *) &timestamp,
    745 				    sizeof(timestamp))) {
    746 				wpa_printf(MSG_ERROR,
    747 					   "Could not add HS 2.0 Timestamp");
    748 				goto fail;
    749 			}
    750 		}
    751 	}
    752 #endif /* CONFIG_HS20 */
    753 
    754 	if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
    755 		goto fail;
    756 
    757 	return;
    758 
    759  fail:
    760 	radius_msg_free(msg);
    761 }
    762 #endif /* CONFIG_NO_RADIUS */
    763 
    764 
    765 static void handle_eap_response(struct hostapd_data *hapd,
    766 				struct sta_info *sta, struct eap_hdr *eap,
    767 				size_t len)
    768 {
    769 	u8 type, *data;
    770 	struct eapol_state_machine *sm = sta->eapol_sm;
    771 	if (sm == NULL)
    772 		return;
    773 
    774 	data = (u8 *) (eap + 1);
    775 
    776 	if (len < sizeof(*eap) + 1) {
    777 		wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
    778 		return;
    779 	}
    780 
    781 	sm->eap_type_supp = type = data[0];
    782 
    783 	hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
    784 		       HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
    785 		       "id=%d len=%d) from STA: EAP Response-%s (%d)",
    786 		       eap->code, eap->identifier, be_to_host16(eap->length),
    787 		       eap_server_get_name(0, type), type);
    788 
    789 	sm->dot1xAuthEapolRespFramesRx++;
    790 
    791 	wpabuf_free(sm->eap_if->eapRespData);
    792 	sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
    793 	sm->eapolEap = TRUE;
    794 }
    795 
    796 
    797 static void handle_eap_initiate(struct hostapd_data *hapd,
    798 				struct sta_info *sta, struct eap_hdr *eap,
    799 				size_t len)
    800 {
    801 #ifdef CONFIG_ERP
    802 	u8 type, *data;
    803 	struct eapol_state_machine *sm = sta->eapol_sm;
    804 
    805 	if (sm == NULL)
    806 		return;
    807 
    808 	if (len < sizeof(*eap) + 1) {
    809 		wpa_printf(MSG_INFO,
    810 			   "handle_eap_initiate: too short response data");
    811 		return;
    812 	}
    813 
    814 	data = (u8 *) (eap + 1);
    815 	type = data[0];
    816 
    817 	hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
    818 		       HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
    819 		       "id=%d len=%d) from STA: EAP Initiate type %u",
    820 		       eap->code, eap->identifier, be_to_host16(eap->length),
    821 		       type);
    822 
    823 	wpabuf_free(sm->eap_if->eapRespData);
    824 	sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
    825 	sm->eapolEap = TRUE;
    826 #endif /* CONFIG_ERP */
    827 }
    828 
    829 
    830 /* Process incoming EAP packet from Supplicant */
    831 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
    832 		       u8 *buf, size_t len)
    833 {
    834 	struct eap_hdr *eap;
    835 	u16 eap_len;
    836 
    837 	if (len < sizeof(*eap)) {
    838 		wpa_printf(MSG_INFO, "   too short EAP packet");
    839 		return;
    840 	}
    841 
    842 	eap = (struct eap_hdr *) buf;
    843 
    844 	eap_len = be_to_host16(eap->length);
    845 	wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
    846 		   eap->code, eap->identifier, eap_len);
    847 	if (eap_len < sizeof(*eap)) {
    848 		wpa_printf(MSG_DEBUG, "   Invalid EAP length");
    849 		return;
    850 	} else if (eap_len > len) {
    851 		wpa_printf(MSG_DEBUG, "   Too short frame to contain this EAP "
    852 			   "packet");
    853 		return;
    854 	} else if (eap_len < len) {
    855 		wpa_printf(MSG_DEBUG, "   Ignoring %lu extra bytes after EAP "
    856 			   "packet", (unsigned long) len - eap_len);
    857 	}
    858 
    859 	switch (eap->code) {
    860 	case EAP_CODE_REQUEST:
    861 		wpa_printf(MSG_DEBUG, " (request)");
    862 		return;
    863 	case EAP_CODE_RESPONSE:
    864 		wpa_printf(MSG_DEBUG, " (response)");
    865 		handle_eap_response(hapd, sta, eap, eap_len);
    866 		break;
    867 	case EAP_CODE_SUCCESS:
    868 		wpa_printf(MSG_DEBUG, " (success)");
    869 		return;
    870 	case EAP_CODE_FAILURE:
    871 		wpa_printf(MSG_DEBUG, " (failure)");
    872 		return;
    873 	case EAP_CODE_INITIATE:
    874 		wpa_printf(MSG_DEBUG, " (initiate)");
    875 		handle_eap_initiate(hapd, sta, eap, eap_len);
    876 		break;
    877 	case EAP_CODE_FINISH:
    878 		wpa_printf(MSG_DEBUG, " (finish)");
    879 		break;
    880 	default:
    881 		wpa_printf(MSG_DEBUG, " (unknown code)");
    882 		return;
    883 	}
    884 }
    885 
    886 
    887 struct eapol_state_machine *
    888 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
    889 {
    890 	int flags = 0;
    891 	if (sta->flags & WLAN_STA_PREAUTH)
    892 		flags |= EAPOL_SM_PREAUTH;
    893 	if (sta->wpa_sm) {
    894 		flags |= EAPOL_SM_USES_WPA;
    895 		if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
    896 			flags |= EAPOL_SM_FROM_PMKSA_CACHE;
    897 	}
    898 	return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
    899 				sta->wps_ie, sta->p2p_ie, sta,
    900 				sta->identity, sta->radius_cui);
    901 }
    902 
    903 
    904 static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
    905 				  size_t len)
    906 {
    907 	if (sta->pending_eapol_rx) {
    908 		wpabuf_free(sta->pending_eapol_rx->buf);
    909 	} else {
    910 		sta->pending_eapol_rx =
    911 			os_malloc(sizeof(*sta->pending_eapol_rx));
    912 		if (!sta->pending_eapol_rx)
    913 			return;
    914 	}
    915 
    916 	sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
    917 	if (!sta->pending_eapol_rx->buf) {
    918 		os_free(sta->pending_eapol_rx);
    919 		sta->pending_eapol_rx = NULL;
    920 		return;
    921 	}
    922 
    923 	os_get_reltime(&sta->pending_eapol_rx->rx_time);
    924 }
    925 
    926 
    927 /**
    928  * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
    929  * @hapd: hostapd BSS data
    930  * @sa: Source address (sender of the EAPOL frame)
    931  * @buf: EAPOL frame
    932  * @len: Length of buf in octets
    933  *
    934  * This function is called for each incoming EAPOL frame from the interface
    935  */
    936 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
    937 			size_t len)
    938 {
    939 	struct sta_info *sta;
    940 	struct ieee802_1x_hdr *hdr;
    941 	struct ieee802_1x_eapol_key *key;
    942 	u16 datalen;
    943 	struct rsn_pmksa_cache_entry *pmksa;
    944 	int key_mgmt;
    945 
    946 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
    947 	    !hapd->conf->wps_state)
    948 		return;
    949 
    950 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
    951 		   (unsigned long) len, MAC2STR(sa));
    952 	sta = ap_get_sta(hapd, sa);
    953 	if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
    954 		     !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
    955 		wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
    956 			   "associated/Pre-authenticating STA");
    957 
    958 		if (sta && (sta->flags & WLAN_STA_AUTH)) {
    959 			wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
    960 				   " for later use", MAC2STR(sta->addr));
    961 			ieee802_1x_save_eapol(sta, buf, len);
    962 		}
    963 
    964 		return;
    965 	}
    966 
    967 	if (len < sizeof(*hdr)) {
    968 		wpa_printf(MSG_INFO, "   too short IEEE 802.1X packet");
    969 		return;
    970 	}
    971 
    972 	hdr = (struct ieee802_1x_hdr *) buf;
    973 	datalen = be_to_host16(hdr->length);
    974 	wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
    975 		   hdr->version, hdr->type, datalen);
    976 
    977 	if (len - sizeof(*hdr) < datalen) {
    978 		wpa_printf(MSG_INFO, "   frame too short for this IEEE 802.1X packet");
    979 		if (sta->eapol_sm)
    980 			sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
    981 		return;
    982 	}
    983 	if (len - sizeof(*hdr) > datalen) {
    984 		wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
    985 			   "IEEE 802.1X packet",
    986 			   (unsigned long) len - sizeof(*hdr) - datalen);
    987 	}
    988 
    989 	if (sta->eapol_sm) {
    990 		sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
    991 		sta->eapol_sm->dot1xAuthEapolFramesRx++;
    992 	}
    993 
    994 	key = (struct ieee802_1x_eapol_key *) (hdr + 1);
    995 	if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
    996 	    hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
    997 	    (key->type == EAPOL_KEY_TYPE_WPA ||
    998 	     key->type == EAPOL_KEY_TYPE_RSN)) {
    999 		wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
   1000 			    sizeof(*hdr) + datalen);
   1001 		return;
   1002 	}
   1003 
   1004 	if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
   1005 	    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
   1006 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
   1007 			   "802.1X not enabled and WPS not used");
   1008 		return;
   1009 	}
   1010 
   1011 	key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
   1012 	if (key_mgmt != -1 &&
   1013 	    (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
   1014 	     key_mgmt == WPA_KEY_MGMT_DPP)) {
   1015 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
   1016 			   "STA is using PSK");
   1017 		return;
   1018 	}
   1019 
   1020 	if (!sta->eapol_sm) {
   1021 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
   1022 		if (!sta->eapol_sm)
   1023 			return;
   1024 
   1025 #ifdef CONFIG_WPS
   1026 		if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
   1027 			u32 wflags = sta->flags & (WLAN_STA_WPS |
   1028 						   WLAN_STA_WPS2 |
   1029 						   WLAN_STA_MAYBE_WPS);
   1030 			if (wflags == WLAN_STA_MAYBE_WPS ||
   1031 			    wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
   1032 				/*
   1033 				 * Delay EAPOL frame transmission until a
   1034 				 * possible WPS STA initiates the handshake
   1035 				 * with EAPOL-Start. Only allow the wait to be
   1036 				 * skipped if the STA is known to support WPS
   1037 				 * 2.0.
   1038 				 */
   1039 				wpa_printf(MSG_DEBUG, "WPS: Do not start "
   1040 					   "EAPOL until EAPOL-Start is "
   1041 					   "received");
   1042 				sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
   1043 			}
   1044 		}
   1045 #endif /* CONFIG_WPS */
   1046 
   1047 		sta->eapol_sm->eap_if->portEnabled = TRUE;
   1048 	}
   1049 
   1050 	/* since we support version 1, we can ignore version field and proceed
   1051 	 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
   1052 	/* TODO: actually, we are not version 1 anymore.. However, Version 2
   1053 	 * does not change frame contents, so should be ok to process frames
   1054 	 * more or less identically. Some changes might be needed for
   1055 	 * verification of fields. */
   1056 
   1057 	switch (hdr->type) {
   1058 	case IEEE802_1X_TYPE_EAP_PACKET:
   1059 		handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
   1060 		break;
   1061 
   1062 	case IEEE802_1X_TYPE_EAPOL_START:
   1063 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1064 			       HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
   1065 			       "from STA");
   1066 		sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
   1067 		pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
   1068 		if (pmksa) {
   1069 			hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
   1070 				       HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
   1071 				       "available - ignore it since "
   1072 				       "STA sent EAPOL-Start");
   1073 			wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
   1074 		}
   1075 		sta->eapol_sm->eapolStart = TRUE;
   1076 		sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
   1077 		eap_server_clear_identity(sta->eapol_sm->eap);
   1078 		wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
   1079 		break;
   1080 
   1081 	case IEEE802_1X_TYPE_EAPOL_LOGOFF:
   1082 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1083 			       HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
   1084 			       "from STA");
   1085 		sta->acct_terminate_cause =
   1086 			RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
   1087 		accounting_sta_stop(hapd, sta);
   1088 		sta->eapol_sm->eapolLogoff = TRUE;
   1089 		sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
   1090 		eap_server_clear_identity(sta->eapol_sm->eap);
   1091 		break;
   1092 
   1093 	case IEEE802_1X_TYPE_EAPOL_KEY:
   1094 		wpa_printf(MSG_DEBUG, "   EAPOL-Key");
   1095 		if (!ap_sta_is_authorized(sta)) {
   1096 			wpa_printf(MSG_DEBUG, "   Dropped key data from "
   1097 				   "unauthorized Supplicant");
   1098 			break;
   1099 		}
   1100 		break;
   1101 
   1102 	case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
   1103 		wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
   1104 		/* TODO: implement support for this; show data */
   1105 		break;
   1106 
   1107 	default:
   1108 		wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
   1109 		sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
   1110 		break;
   1111 	}
   1112 
   1113 	eapol_auth_step(sta->eapol_sm);
   1114 }
   1115 
   1116 
   1117 /**
   1118  * ieee802_1x_new_station - Start IEEE 802.1X authentication
   1119  * @hapd: hostapd BSS data
   1120  * @sta: The station
   1121  *
   1122  * This function is called to start IEEE 802.1X authentication when a new
   1123  * station completes IEEE 802.11 association.
   1124  */
   1125 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
   1126 {
   1127 	struct rsn_pmksa_cache_entry *pmksa;
   1128 	int reassoc = 1;
   1129 	int force_1x = 0;
   1130 	int key_mgmt;
   1131 
   1132 #ifdef CONFIG_WPS
   1133 	if (hapd->conf->wps_state &&
   1134 	    ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
   1135 	     (sta->flags & WLAN_STA_WPS))) {
   1136 		/*
   1137 		 * Need to enable IEEE 802.1X/EAPOL state machines for possible
   1138 		 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
   1139 		 * authentication in this BSS.
   1140 		 */
   1141 		force_1x = 1;
   1142 	}
   1143 #endif /* CONFIG_WPS */
   1144 
   1145 	if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
   1146 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
   1147 			   "802.1X not enabled or forced for WPS");
   1148 		/*
   1149 		 * Clear any possible EAPOL authenticator state to support
   1150 		 * reassociation change from WPS to PSK.
   1151 		 */
   1152 		ieee802_1x_free_station(hapd, sta);
   1153 		return;
   1154 	}
   1155 
   1156 	key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
   1157 	if (key_mgmt != -1 &&
   1158 	    (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
   1159 	     key_mgmt == WPA_KEY_MGMT_DPP)) {
   1160 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
   1161 		/*
   1162 		 * Clear any possible EAPOL authenticator state to support
   1163 		 * reassociation change from WPA-EAP to PSK.
   1164 		 */
   1165 		ieee802_1x_free_station(hapd, sta);
   1166 		return;
   1167 	}
   1168 
   1169 	if (sta->eapol_sm == NULL) {
   1170 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1171 			       HOSTAPD_LEVEL_DEBUG, "start authentication");
   1172 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
   1173 		if (sta->eapol_sm == NULL) {
   1174 			hostapd_logger(hapd, sta->addr,
   1175 				       HOSTAPD_MODULE_IEEE8021X,
   1176 				       HOSTAPD_LEVEL_INFO,
   1177 				       "failed to allocate state machine");
   1178 			return;
   1179 		}
   1180 		reassoc = 0;
   1181 	}
   1182 
   1183 #ifdef CONFIG_WPS
   1184 	sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
   1185 	if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
   1186 	    !(sta->flags & WLAN_STA_WPS2)) {
   1187 		/*
   1188 		 * Delay EAPOL frame transmission until a possible WPS STA
   1189 		 * initiates the handshake with EAPOL-Start. Only allow the
   1190 		 * wait to be skipped if the STA is known to support WPS 2.0.
   1191 		 */
   1192 		wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
   1193 			   "EAPOL-Start is received");
   1194 		sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
   1195 	}
   1196 #endif /* CONFIG_WPS */
   1197 
   1198 	sta->eapol_sm->eap_if->portEnabled = TRUE;
   1199 
   1200 #ifdef CONFIG_IEEE80211R_AP
   1201 	if (sta->auth_alg == WLAN_AUTH_FT) {
   1202 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1203 			       HOSTAPD_LEVEL_DEBUG,
   1204 			       "PMK from FT - skip IEEE 802.1X/EAP");
   1205 		/* Setup EAPOL state machines to already authenticated state
   1206 		 * because of existing FT information from R0KH. */
   1207 		sta->eapol_sm->keyRun = TRUE;
   1208 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
   1209 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
   1210 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
   1211 		sta->eapol_sm->authSuccess = TRUE;
   1212 		sta->eapol_sm->authFail = FALSE;
   1213 		sta->eapol_sm->portValid = TRUE;
   1214 		if (sta->eapol_sm->eap)
   1215 			eap_sm_notify_cached(sta->eapol_sm->eap);
   1216 		ap_sta_bind_vlan(hapd, sta);
   1217 		return;
   1218 	}
   1219 #endif /* CONFIG_IEEE80211R_AP */
   1220 
   1221 #ifdef CONFIG_FILS
   1222 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
   1223 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
   1224 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
   1225 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1226 			       HOSTAPD_LEVEL_DEBUG,
   1227 			       "PMK from FILS - skip IEEE 802.1X/EAP");
   1228 		/* Setup EAPOL state machines to already authenticated state
   1229 		 * because of existing FILS information. */
   1230 		sta->eapol_sm->keyRun = TRUE;
   1231 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
   1232 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
   1233 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
   1234 		sta->eapol_sm->authSuccess = TRUE;
   1235 		sta->eapol_sm->authFail = FALSE;
   1236 		sta->eapol_sm->portValid = TRUE;
   1237 		if (sta->eapol_sm->eap)
   1238 			eap_sm_notify_cached(sta->eapol_sm->eap);
   1239 		return;
   1240 	}
   1241 #endif /* CONFIG_FILS */
   1242 
   1243 	pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
   1244 	if (pmksa) {
   1245 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1246 			       HOSTAPD_LEVEL_DEBUG,
   1247 			       "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
   1248 		/* Setup EAPOL state machines to already authenticated state
   1249 		 * because of existing PMKSA information in the cache. */
   1250 		sta->eapol_sm->keyRun = TRUE;
   1251 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
   1252 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
   1253 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
   1254 		sta->eapol_sm->authSuccess = TRUE;
   1255 		sta->eapol_sm->authFail = FALSE;
   1256 		if (sta->eapol_sm->eap)
   1257 			eap_sm_notify_cached(sta->eapol_sm->eap);
   1258 		pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
   1259 		ap_sta_bind_vlan(hapd, sta);
   1260 	} else {
   1261 		if (reassoc) {
   1262 			/*
   1263 			 * Force EAPOL state machines to start
   1264 			 * re-authentication without having to wait for the
   1265 			 * Supplicant to send EAPOL-Start.
   1266 			 */
   1267 			sta->eapol_sm->reAuthenticate = TRUE;
   1268 		}
   1269 		eapol_auth_step(sta->eapol_sm);
   1270 	}
   1271 }
   1272 
   1273 
   1274 void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
   1275 {
   1276 	struct eapol_state_machine *sm = sta->eapol_sm;
   1277 
   1278 #ifdef CONFIG_HS20
   1279 	eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
   1280 #endif /* CONFIG_HS20 */
   1281 
   1282 	if (sta->pending_eapol_rx) {
   1283 		wpabuf_free(sta->pending_eapol_rx->buf);
   1284 		os_free(sta->pending_eapol_rx);
   1285 		sta->pending_eapol_rx = NULL;
   1286 	}
   1287 
   1288 	if (sm == NULL)
   1289 		return;
   1290 
   1291 	sta->eapol_sm = NULL;
   1292 
   1293 #ifndef CONFIG_NO_RADIUS
   1294 	radius_msg_free(sm->last_recv_radius);
   1295 	radius_free_class(&sm->radius_class);
   1296 #endif /* CONFIG_NO_RADIUS */
   1297 
   1298 	eapol_auth_free(sm);
   1299 }
   1300 
   1301 
   1302 #ifndef CONFIG_NO_RADIUS
   1303 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
   1304 					  struct sta_info *sta)
   1305 {
   1306 	struct wpabuf *eap;
   1307 	const struct eap_hdr *hdr;
   1308 	int eap_type = -1;
   1309 	char buf[64];
   1310 	struct radius_msg *msg;
   1311 	struct eapol_state_machine *sm = sta->eapol_sm;
   1312 
   1313 	if (sm == NULL || sm->last_recv_radius == NULL) {
   1314 		if (sm)
   1315 			sm->eap_if->aaaEapNoReq = TRUE;
   1316 		return;
   1317 	}
   1318 
   1319 	msg = sm->last_recv_radius;
   1320 
   1321 	eap = radius_msg_get_eap(msg);
   1322 	if (eap == NULL) {
   1323 		/* RFC 3579, Chap. 2.6.3:
   1324 		 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
   1325 		 * attribute */
   1326 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1327 			       HOSTAPD_LEVEL_WARNING, "could not extract "
   1328 			       "EAP-Message from RADIUS message");
   1329 		sm->eap_if->aaaEapNoReq = TRUE;
   1330 		return;
   1331 	}
   1332 
   1333 	if (wpabuf_len(eap) < sizeof(*hdr)) {
   1334 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1335 			       HOSTAPD_LEVEL_WARNING, "too short EAP packet "
   1336 			       "received from authentication server");
   1337 		wpabuf_free(eap);
   1338 		sm->eap_if->aaaEapNoReq = TRUE;
   1339 		return;
   1340 	}
   1341 
   1342 	if (wpabuf_len(eap) > sizeof(*hdr))
   1343 		eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
   1344 
   1345 	hdr = wpabuf_head(eap);
   1346 	switch (hdr->code) {
   1347 	case EAP_CODE_REQUEST:
   1348 		if (eap_type >= 0)
   1349 			sm->eap_type_authsrv = eap_type;
   1350 		os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
   1351 			    eap_server_get_name(0, eap_type), eap_type);
   1352 		break;
   1353 	case EAP_CODE_RESPONSE:
   1354 		os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
   1355 			    eap_server_get_name(0, eap_type), eap_type);
   1356 		break;
   1357 	case EAP_CODE_SUCCESS:
   1358 		os_strlcpy(buf, "EAP Success", sizeof(buf));
   1359 		break;
   1360 	case EAP_CODE_FAILURE:
   1361 		os_strlcpy(buf, "EAP Failure", sizeof(buf));
   1362 		break;
   1363 	default:
   1364 		os_strlcpy(buf, "unknown EAP code", sizeof(buf));
   1365 		break;
   1366 	}
   1367 	buf[sizeof(buf) - 1] = '\0';
   1368 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1369 		       HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
   1370 		       "id=%d len=%d) from RADIUS server: %s",
   1371 		       hdr->code, hdr->identifier, be_to_host16(hdr->length),
   1372 		       buf);
   1373 	sm->eap_if->aaaEapReq = TRUE;
   1374 
   1375 	wpabuf_free(sm->eap_if->aaaEapReqData);
   1376 	sm->eap_if->aaaEapReqData = eap;
   1377 }
   1378 
   1379 
   1380 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
   1381 				struct sta_info *sta, struct radius_msg *msg,
   1382 				struct radius_msg *req,
   1383 				const u8 *shared_secret,
   1384 				size_t shared_secret_len)
   1385 {
   1386 	struct radius_ms_mppe_keys *keys;
   1387 	struct eapol_state_machine *sm = sta->eapol_sm;
   1388 	if (sm == NULL)
   1389 		return;
   1390 
   1391 	keys = radius_msg_get_ms_keys(msg, req, shared_secret,
   1392 				      shared_secret_len);
   1393 
   1394 	if (keys && keys->send && keys->recv) {
   1395 		size_t len = keys->send_len + keys->recv_len;
   1396 		wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
   1397 				keys->send, keys->send_len);
   1398 		wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
   1399 				keys->recv, keys->recv_len);
   1400 
   1401 		os_free(sm->eap_if->aaaEapKeyData);
   1402 		sm->eap_if->aaaEapKeyData = os_malloc(len);
   1403 		if (sm->eap_if->aaaEapKeyData) {
   1404 			os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
   1405 				  keys->recv_len);
   1406 			os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
   1407 				  keys->send, keys->send_len);
   1408 			sm->eap_if->aaaEapKeyDataLen = len;
   1409 			sm->eap_if->aaaEapKeyAvailable = TRUE;
   1410 		}
   1411 	} else {
   1412 		wpa_printf(MSG_DEBUG,
   1413 			   "MS-MPPE: 1x_get_keys, could not get keys: %p  send: %p  recv: %p",
   1414 			   keys, keys ? keys->send : NULL,
   1415 			   keys ? keys->recv : NULL);
   1416 	}
   1417 
   1418 	if (keys) {
   1419 		os_free(keys->send);
   1420 		os_free(keys->recv);
   1421 		os_free(keys);
   1422 	}
   1423 }
   1424 
   1425 
   1426 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
   1427 					  struct sta_info *sta,
   1428 					  struct radius_msg *msg)
   1429 {
   1430 	u8 *attr_class;
   1431 	size_t class_len;
   1432 	struct eapol_state_machine *sm = sta->eapol_sm;
   1433 	int count, i;
   1434 	struct radius_attr_data *nclass;
   1435 	size_t nclass_count;
   1436 
   1437 	if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
   1438 	    sm == NULL)
   1439 		return;
   1440 
   1441 	radius_free_class(&sm->radius_class);
   1442 	count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
   1443 	if (count <= 0)
   1444 		return;
   1445 
   1446 	nclass = os_calloc(count, sizeof(struct radius_attr_data));
   1447 	if (nclass == NULL)
   1448 		return;
   1449 
   1450 	nclass_count = 0;
   1451 
   1452 	attr_class = NULL;
   1453 	for (i = 0; i < count; i++) {
   1454 		do {
   1455 			if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
   1456 						    &attr_class, &class_len,
   1457 						    attr_class) < 0) {
   1458 				i = count;
   1459 				break;
   1460 			}
   1461 		} while (class_len < 1);
   1462 
   1463 		nclass[nclass_count].data = os_memdup(attr_class, class_len);
   1464 		if (nclass[nclass_count].data == NULL)
   1465 			break;
   1466 
   1467 		nclass[nclass_count].len = class_len;
   1468 		nclass_count++;
   1469 	}
   1470 
   1471 	sm->radius_class.attr = nclass;
   1472 	sm->radius_class.count = nclass_count;
   1473 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
   1474 		   "attributes for " MACSTR,
   1475 		   (unsigned long) sm->radius_class.count,
   1476 		   MAC2STR(sta->addr));
   1477 }
   1478 
   1479 
   1480 /* Update sta->identity based on User-Name attribute in Access-Accept */
   1481 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
   1482 					   struct sta_info *sta,
   1483 					   struct radius_msg *msg)
   1484 {
   1485 	u8 *buf, *identity;
   1486 	size_t len;
   1487 	struct eapol_state_machine *sm = sta->eapol_sm;
   1488 
   1489 	if (sm == NULL)
   1490 		return;
   1491 
   1492 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
   1493 				    NULL) < 0)
   1494 		return;
   1495 
   1496 	identity = (u8 *) dup_binstr(buf, len);
   1497 	if (identity == NULL)
   1498 		return;
   1499 
   1500 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1501 		       HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
   1502 		       "User-Name from Access-Accept '%s'",
   1503 		       sm->identity ? (char *) sm->identity : "N/A",
   1504 		       (char *) identity);
   1505 
   1506 	os_free(sm->identity);
   1507 	sm->identity = identity;
   1508 	sm->identity_len = len;
   1509 }
   1510 
   1511 
   1512 /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
   1513 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
   1514 				      struct sta_info *sta,
   1515 				      struct radius_msg *msg)
   1516 {
   1517 	struct eapol_state_machine *sm = sta->eapol_sm;
   1518 	struct wpabuf *cui;
   1519 	u8 *buf;
   1520 	size_t len;
   1521 
   1522 	if (sm == NULL)
   1523 		return;
   1524 
   1525 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
   1526 				    &buf, &len, NULL) < 0)
   1527 		return;
   1528 
   1529 	cui = wpabuf_alloc_copy(buf, len);
   1530 	if (cui == NULL)
   1531 		return;
   1532 
   1533 	wpabuf_free(sm->radius_cui);
   1534 	sm->radius_cui = cui;
   1535 }
   1536 
   1537 
   1538 #ifdef CONFIG_HS20
   1539 
   1540 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
   1541 {
   1542 	sta->remediation = 1;
   1543 	os_free(sta->remediation_url);
   1544 	if (len > 2) {
   1545 		sta->remediation_url = os_malloc(len);
   1546 		if (!sta->remediation_url)
   1547 			return;
   1548 		sta->remediation_method = pos[0];
   1549 		os_memcpy(sta->remediation_url, pos + 1, len - 1);
   1550 		sta->remediation_url[len - 1] = '\0';
   1551 		wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
   1552 			   "for " MACSTR " - server method %u URL %s",
   1553 			   MAC2STR(sta->addr), sta->remediation_method,
   1554 			   sta->remediation_url);
   1555 	} else {
   1556 		sta->remediation_url = NULL;
   1557 		wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
   1558 			   "for " MACSTR, MAC2STR(sta->addr));
   1559 	}
   1560 	/* TODO: assign the STA into remediation VLAN or add filtering */
   1561 }
   1562 
   1563 
   1564 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
   1565 				       struct sta_info *sta, u8 *pos,
   1566 				       size_t len)
   1567 {
   1568 	if (len < 3)
   1569 		return; /* Malformed information */
   1570 	sta->hs20_deauth_requested = 1;
   1571 	wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u  "
   1572 		   "Re-auth Delay %u",
   1573 		   *pos, WPA_GET_LE16(pos + 1));
   1574 	wpabuf_free(sta->hs20_deauth_req);
   1575 	sta->hs20_deauth_req = wpabuf_alloc(len + 1);
   1576 	if (sta->hs20_deauth_req) {
   1577 		wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
   1578 		wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
   1579 		wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
   1580 	}
   1581 	ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
   1582 }
   1583 
   1584 
   1585 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
   1586 					 struct sta_info *sta, u8 *pos,
   1587 					 size_t len, int session_timeout)
   1588 {
   1589 	unsigned int swt;
   1590 	int warning_time, beacon_int;
   1591 
   1592 	if (len < 1)
   1593 		return; /* Malformed information */
   1594 	os_free(sta->hs20_session_info_url);
   1595 	sta->hs20_session_info_url = os_malloc(len);
   1596 	if (sta->hs20_session_info_url == NULL)
   1597 		return;
   1598 	swt = pos[0];
   1599 	os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
   1600 	sta->hs20_session_info_url[len - 1] = '\0';
   1601 	wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
   1602 		   "(session_timeout=%d)",
   1603 		   sta->hs20_session_info_url, swt, session_timeout);
   1604 	if (session_timeout < 0) {
   1605 		wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
   1606 		return;
   1607 	}
   1608 	if (swt == 255)
   1609 		swt = 1; /* Use one minute as the AP selected value */
   1610 
   1611 	if ((unsigned int) session_timeout < swt * 60)
   1612 		warning_time = 0;
   1613 	else
   1614 		warning_time = session_timeout - swt * 60;
   1615 
   1616 	beacon_int = hapd->iconf->beacon_int;
   1617 	if (beacon_int < 1)
   1618 		beacon_int = 100; /* best guess */
   1619 	sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
   1620 	if (sta->hs20_disassoc_timer > 65535)
   1621 		sta->hs20_disassoc_timer = 65535;
   1622 
   1623 	ap_sta_session_warning_timeout(hapd, sta, warning_time);
   1624 }
   1625 
   1626 
   1627 static void ieee802_1x_hs20_t_c_filtering(struct hostapd_data *hapd,
   1628 					  struct sta_info *sta, u8 *pos,
   1629 					  size_t len)
   1630 {
   1631 	if (len < 4)
   1632 		return; /* Malformed information */
   1633 	wpa_printf(MSG_DEBUG,
   1634 		   "HS 2.0: Terms and Conditions filtering %02x %02x %02x %02x",
   1635 		   pos[0], pos[1], pos[2], pos[3]);
   1636 	hs20_t_c_filtering(hapd, sta, pos[0] & BIT(0));
   1637 }
   1638 
   1639 
   1640 static void ieee802_1x_hs20_t_c_url(struct hostapd_data *hapd,
   1641 				    struct sta_info *sta, u8 *pos, size_t len)
   1642 {
   1643 	os_free(sta->t_c_url);
   1644 	sta->t_c_url = os_malloc(len + 1);
   1645 	if (!sta->t_c_url)
   1646 		return;
   1647 	os_memcpy(sta->t_c_url, pos, len);
   1648 	sta->t_c_url[len] = '\0';
   1649 	wpa_printf(MSG_DEBUG,
   1650 		   "HS 2.0: Terms and Conditions URL %s", sta->t_c_url);
   1651 }
   1652 
   1653 #endif /* CONFIG_HS20 */
   1654 
   1655 
   1656 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
   1657 				  struct sta_info *sta,
   1658 				  struct radius_msg *msg,
   1659 				  int session_timeout)
   1660 {
   1661 #ifdef CONFIG_HS20
   1662 	u8 *buf, *pos, *end, type, sublen;
   1663 	size_t len;
   1664 
   1665 	buf = NULL;
   1666 	sta->remediation = 0;
   1667 	sta->hs20_deauth_requested = 0;
   1668 
   1669 	for (;;) {
   1670 		if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
   1671 					    &buf, &len, buf) < 0)
   1672 			break;
   1673 		if (len < 6)
   1674 			continue;
   1675 		pos = buf;
   1676 		end = buf + len;
   1677 		if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
   1678 			continue;
   1679 		pos += 4;
   1680 
   1681 		type = *pos++;
   1682 		sublen = *pos++;
   1683 		if (sublen < 2)
   1684 			continue; /* invalid length */
   1685 		sublen -= 2; /* skip header */
   1686 		if (pos + sublen > end)
   1687 			continue; /* invalid WFA VSA */
   1688 
   1689 		switch (type) {
   1690 		case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
   1691 			ieee802_1x_hs20_sub_rem(sta, pos, sublen);
   1692 			break;
   1693 		case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
   1694 			ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
   1695 			break;
   1696 		case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
   1697 			ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
   1698 						     session_timeout);
   1699 			break;
   1700 		case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING:
   1701 			ieee802_1x_hs20_t_c_filtering(hapd, sta, pos, sublen);
   1702 			break;
   1703 		case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL:
   1704 			ieee802_1x_hs20_t_c_url(hapd, sta, pos, sublen);
   1705 			break;
   1706 		}
   1707 	}
   1708 #endif /* CONFIG_HS20 */
   1709 }
   1710 
   1711 
   1712 struct sta_id_search {
   1713 	u8 identifier;
   1714 	struct eapol_state_machine *sm;
   1715 };
   1716 
   1717 
   1718 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
   1719 					       struct sta_info *sta,
   1720 					       void *ctx)
   1721 {
   1722 	struct sta_id_search *id_search = ctx;
   1723 	struct eapol_state_machine *sm = sta->eapol_sm;
   1724 
   1725 	if (sm && sm->radius_identifier >= 0 &&
   1726 	    sm->radius_identifier == id_search->identifier) {
   1727 		id_search->sm = sm;
   1728 		return 1;
   1729 	}
   1730 	return 0;
   1731 }
   1732 
   1733 
   1734 static struct eapol_state_machine *
   1735 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
   1736 {
   1737 	struct sta_id_search id_search;
   1738 	id_search.identifier = identifier;
   1739 	id_search.sm = NULL;
   1740 	ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
   1741 	return id_search.sm;
   1742 }
   1743 
   1744 
   1745 #ifndef CONFIG_NO_VLAN
   1746 static int ieee802_1x_update_vlan(struct radius_msg *msg,
   1747 				  struct hostapd_data *hapd,
   1748 				  struct sta_info *sta)
   1749 {
   1750 	struct vlan_description vlan_desc;
   1751 
   1752 	os_memset(&vlan_desc, 0, sizeof(vlan_desc));
   1753 	vlan_desc.notempty = !!radius_msg_get_vlanid(msg, &vlan_desc.untagged,
   1754 						     MAX_NUM_TAGGED_VLAN,
   1755 						     vlan_desc.tagged);
   1756 
   1757 	if (vlan_desc.notempty &&
   1758 	    !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
   1759 		sta->eapol_sm->authFail = TRUE;
   1760 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
   1761 			       HOSTAPD_LEVEL_INFO,
   1762 			       "Invalid VLAN %d%s received from RADIUS server",
   1763 			       vlan_desc.untagged,
   1764 			       vlan_desc.tagged[0] ? "+" : "");
   1765 		os_memset(&vlan_desc, 0, sizeof(vlan_desc));
   1766 		ap_sta_set_vlan(hapd, sta, &vlan_desc);
   1767 		return -1;
   1768 	}
   1769 
   1770 	if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
   1771 	    !vlan_desc.notempty) {
   1772 		sta->eapol_sm->authFail = TRUE;
   1773 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1774 			       HOSTAPD_LEVEL_INFO,
   1775 			       "authentication server did not include required VLAN ID in Access-Accept");
   1776 		return -1;
   1777 	}
   1778 
   1779 	return ap_sta_set_vlan(hapd, sta, &vlan_desc);
   1780 }
   1781 #endif /* CONFIG_NO_VLAN */
   1782 
   1783 
   1784 /**
   1785  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
   1786  * @msg: RADIUS response message
   1787  * @req: RADIUS request message
   1788  * @shared_secret: RADIUS shared secret
   1789  * @shared_secret_len: Length of shared_secret in octets
   1790  * @data: Context data (struct hostapd_data *)
   1791  * Returns: Processing status
   1792  */
   1793 static RadiusRxResult
   1794 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
   1795 			const u8 *shared_secret, size_t shared_secret_len,
   1796 			void *data)
   1797 {
   1798 	struct hostapd_data *hapd = data;
   1799 	struct sta_info *sta;
   1800 	u32 session_timeout = 0, termination_action, acct_interim_interval;
   1801 	int session_timeout_set;
   1802 	u32 reason_code;
   1803 	struct eapol_state_machine *sm;
   1804 	int override_eapReq = 0;
   1805 	struct radius_hdr *hdr = radius_msg_get_hdr(msg);
   1806 
   1807 	sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
   1808 	if (sm == NULL) {
   1809 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
   1810 			   "station for this RADIUS message");
   1811 		return RADIUS_RX_UNKNOWN;
   1812 	}
   1813 	sta = sm->sta;
   1814 
   1815 	/* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
   1816 	 * present when packet contains an EAP-Message attribute */
   1817 	if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
   1818 	    radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
   1819 				0) < 0 &&
   1820 	    radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
   1821 		wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
   1822 			   "Message-Authenticator since it does not include "
   1823 			   "EAP-Message");
   1824 	} else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
   1825 				     req, 1)) {
   1826 		wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
   1827 		return RADIUS_RX_INVALID_AUTHENTICATOR;
   1828 	}
   1829 
   1830 	if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
   1831 	    hdr->code != RADIUS_CODE_ACCESS_REJECT &&
   1832 	    hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
   1833 		wpa_printf(MSG_INFO, "Unknown RADIUS message code");
   1834 		return RADIUS_RX_UNKNOWN;
   1835 	}
   1836 
   1837 	sm->radius_identifier = -1;
   1838 	wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
   1839 		   MAC2STR(sta->addr));
   1840 
   1841 	radius_msg_free(sm->last_recv_radius);
   1842 	sm->last_recv_radius = msg;
   1843 
   1844 	session_timeout_set =
   1845 		!radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
   1846 					   &session_timeout);
   1847 	if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
   1848 				      &termination_action))
   1849 		termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
   1850 
   1851 	if (hapd->conf->acct_interim_interval == 0 &&
   1852 	    hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
   1853 	    radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
   1854 				      &acct_interim_interval) == 0) {
   1855 		if (acct_interim_interval < 60) {
   1856 			hostapd_logger(hapd, sta->addr,
   1857 				       HOSTAPD_MODULE_IEEE8021X,
   1858 				       HOSTAPD_LEVEL_INFO,
   1859 				       "ignored too small "
   1860 				       "Acct-Interim-Interval %d",
   1861 				       acct_interim_interval);
   1862 		} else
   1863 			sta->acct_interim_interval = acct_interim_interval;
   1864 	}
   1865 
   1866 
   1867 	switch (hdr->code) {
   1868 	case RADIUS_CODE_ACCESS_ACCEPT:
   1869 #ifndef CONFIG_NO_VLAN
   1870 		if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
   1871 		    ieee802_1x_update_vlan(msg, hapd, sta) < 0)
   1872 			break;
   1873 
   1874 		if (sta->vlan_id > 0) {
   1875 			hostapd_logger(hapd, sta->addr,
   1876 				       HOSTAPD_MODULE_RADIUS,
   1877 				       HOSTAPD_LEVEL_INFO,
   1878 				       "VLAN ID %d", sta->vlan_id);
   1879 		}
   1880 
   1881 		if ((sta->flags & WLAN_STA_ASSOC) &&
   1882 		    ap_sta_bind_vlan(hapd, sta) < 0)
   1883 			break;
   1884 #endif /* CONFIG_NO_VLAN */
   1885 
   1886 		sta->session_timeout_set = !!session_timeout_set;
   1887 		os_get_reltime(&sta->session_timeout);
   1888 		sta->session_timeout.sec += session_timeout;
   1889 
   1890 		/* RFC 3580, Ch. 3.17 */
   1891 		if (session_timeout_set && termination_action ==
   1892 		    RADIUS_TERMINATION_ACTION_RADIUS_REQUEST)
   1893 			sm->reAuthPeriod = session_timeout;
   1894 		else if (session_timeout_set)
   1895 			ap_sta_session_timeout(hapd, sta, session_timeout);
   1896 		else
   1897 			ap_sta_no_session_timeout(hapd, sta);
   1898 
   1899 		sm->eap_if->aaaSuccess = TRUE;
   1900 		override_eapReq = 1;
   1901 		ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
   1902 				    shared_secret_len);
   1903 		ieee802_1x_store_radius_class(hapd, sta, msg);
   1904 		ieee802_1x_update_sta_identity(hapd, sta, msg);
   1905 		ieee802_1x_update_sta_cui(hapd, sta, msg);
   1906 		ieee802_1x_check_hs20(hapd, sta, msg,
   1907 				      session_timeout_set ?
   1908 				      (int) session_timeout : -1);
   1909 		break;
   1910 	case RADIUS_CODE_ACCESS_REJECT:
   1911 		sm->eap_if->aaaFail = TRUE;
   1912 		override_eapReq = 1;
   1913 		if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_WLAN_REASON_CODE,
   1914 					      &reason_code) == 0) {
   1915 			wpa_printf(MSG_DEBUG,
   1916 				   "RADIUS server indicated WLAN-Reason-Code %u in Access-Reject for "
   1917 				   MACSTR, reason_code, MAC2STR(sta->addr));
   1918 			sta->disconnect_reason_code = reason_code;
   1919 		}
   1920 		break;
   1921 	case RADIUS_CODE_ACCESS_CHALLENGE:
   1922 		sm->eap_if->aaaEapReq = TRUE;
   1923 		if (session_timeout_set) {
   1924 			/* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
   1925 			sm->eap_if->aaaMethodTimeout = session_timeout;
   1926 			hostapd_logger(hapd, sm->addr,
   1927 				       HOSTAPD_MODULE_IEEE8021X,
   1928 				       HOSTAPD_LEVEL_DEBUG,
   1929 				       "using EAP timeout of %d seconds (from "
   1930 				       "RADIUS)",
   1931 				       sm->eap_if->aaaMethodTimeout);
   1932 		} else {
   1933 			/*
   1934 			 * Use dynamic retransmission behavior per EAP
   1935 			 * specification.
   1936 			 */
   1937 			sm->eap_if->aaaMethodTimeout = 0;
   1938 		}
   1939 		break;
   1940 	}
   1941 
   1942 	ieee802_1x_decapsulate_radius(hapd, sta);
   1943 	if (override_eapReq)
   1944 		sm->eap_if->aaaEapReq = FALSE;
   1945 
   1946 #ifdef CONFIG_FILS
   1947 #ifdef NEED_AP_MLME
   1948 	if (sta->flags & WLAN_STA_PENDING_FILS_ERP) {
   1949 		/* TODO: Add a PMKSA entry on success? */
   1950 		ieee802_11_finish_fils_auth(
   1951 			hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
   1952 			sm->eap_if->aaaEapReqData,
   1953 			sm->eap_if->aaaEapKeyData,
   1954 			sm->eap_if->aaaEapKeyDataLen);
   1955 	}
   1956 #endif /* NEED_AP_MLME */
   1957 #endif /* CONFIG_FILS */
   1958 
   1959 	eapol_auth_step(sm);
   1960 
   1961 	return RADIUS_RX_QUEUED;
   1962 }
   1963 #endif /* CONFIG_NO_RADIUS */
   1964 
   1965 
   1966 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
   1967 {
   1968 	struct eapol_state_machine *sm = sta->eapol_sm;
   1969 	if (sm == NULL)
   1970 		return;
   1971 
   1972 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   1973 		       HOSTAPD_LEVEL_DEBUG, "aborting authentication");
   1974 
   1975 #ifndef CONFIG_NO_RADIUS
   1976 	radius_msg_free(sm->last_recv_radius);
   1977 	sm->last_recv_radius = NULL;
   1978 #endif /* CONFIG_NO_RADIUS */
   1979 
   1980 	if (sm->eap_if->eapTimeout) {
   1981 		/*
   1982 		 * Disconnect the STA since it did not reply to the last EAP
   1983 		 * request and we cannot continue EAP processing (EAP-Failure
   1984 		 * could only be sent if the EAP peer actually replied).
   1985 		 */
   1986 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
   1987 			MAC2STR(sta->addr));
   1988 
   1989 		sm->eap_if->portEnabled = FALSE;
   1990 		ap_sta_disconnect(hapd, sta, sta->addr,
   1991 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
   1992 	}
   1993 }
   1994 
   1995 
   1996 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
   1997 {
   1998 	struct eapol_authenticator *eapol = hapd->eapol_auth;
   1999 
   2000 	if (hapd->conf->default_wep_key_len < 1)
   2001 		return 0;
   2002 
   2003 	os_free(eapol->default_wep_key);
   2004 	eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
   2005 	if (eapol->default_wep_key == NULL ||
   2006 	    random_get_bytes(eapol->default_wep_key,
   2007 			     hapd->conf->default_wep_key_len)) {
   2008 		wpa_printf(MSG_INFO, "Could not generate random WEP key");
   2009 		os_free(eapol->default_wep_key);
   2010 		eapol->default_wep_key = NULL;
   2011 		return -1;
   2012 	}
   2013 
   2014 	wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
   2015 			eapol->default_wep_key,
   2016 			hapd->conf->default_wep_key_len);
   2017 
   2018 	return 0;
   2019 }
   2020 
   2021 
   2022 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
   2023 					struct sta_info *sta, void *ctx)
   2024 {
   2025 	if (sta->eapol_sm) {
   2026 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
   2027 		eapol_auth_step(sta->eapol_sm);
   2028 	}
   2029 	return 0;
   2030 }
   2031 
   2032 
   2033 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
   2034 {
   2035 	struct hostapd_data *hapd = eloop_ctx;
   2036 	struct eapol_authenticator *eapol = hapd->eapol_auth;
   2037 
   2038 	if (eapol->default_wep_key_idx >= 3)
   2039 		eapol->default_wep_key_idx =
   2040 			hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
   2041 	else
   2042 		eapol->default_wep_key_idx++;
   2043 
   2044 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
   2045 		   eapol->default_wep_key_idx);
   2046 
   2047 	if (ieee802_1x_rekey_broadcast(hapd)) {
   2048 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
   2049 			       HOSTAPD_LEVEL_WARNING, "failed to generate a "
   2050 			       "new broadcast key");
   2051 		os_free(eapol->default_wep_key);
   2052 		eapol->default_wep_key = NULL;
   2053 		return;
   2054 	}
   2055 
   2056 	/* TODO: Could setup key for RX here, but change default TX keyid only
   2057 	 * after new broadcast key has been sent to all stations. */
   2058 	if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
   2059 				broadcast_ether_addr,
   2060 				eapol->default_wep_key_idx, 1, NULL, 0,
   2061 				eapol->default_wep_key,
   2062 				hapd->conf->default_wep_key_len)) {
   2063 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
   2064 			       HOSTAPD_LEVEL_WARNING, "failed to configure a "
   2065 			       "new broadcast key");
   2066 		os_free(eapol->default_wep_key);
   2067 		eapol->default_wep_key = NULL;
   2068 		return;
   2069 	}
   2070 
   2071 	ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
   2072 
   2073 	if (hapd->conf->wep_rekeying_period > 0) {
   2074 		eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
   2075 				       ieee802_1x_rekey, hapd, NULL);
   2076 	}
   2077 }
   2078 
   2079 
   2080 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
   2081 				  const u8 *data, size_t datalen)
   2082 {
   2083 #ifdef CONFIG_WPS
   2084 	struct sta_info *sta = sta_ctx;
   2085 
   2086 	if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
   2087 	    WLAN_STA_MAYBE_WPS) {
   2088 		const u8 *identity;
   2089 		size_t identity_len;
   2090 		struct eapol_state_machine *sm = sta->eapol_sm;
   2091 
   2092 		identity = eap_get_identity(sm->eap, &identity_len);
   2093 		if (identity &&
   2094 		    ((identity_len == WSC_ID_ENROLLEE_LEN &&
   2095 		      os_memcmp(identity, WSC_ID_ENROLLEE,
   2096 				WSC_ID_ENROLLEE_LEN) == 0) ||
   2097 		     (identity_len == WSC_ID_REGISTRAR_LEN &&
   2098 		      os_memcmp(identity, WSC_ID_REGISTRAR,
   2099 				WSC_ID_REGISTRAR_LEN) == 0))) {
   2100 			wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
   2101 				   "WLAN_STA_WPS");
   2102 			sta->flags |= WLAN_STA_WPS;
   2103 		}
   2104 	}
   2105 #endif /* CONFIG_WPS */
   2106 
   2107 	ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
   2108 }
   2109 
   2110 
   2111 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
   2112 				const u8 *data, size_t datalen)
   2113 {
   2114 #ifndef CONFIG_NO_RADIUS
   2115 	struct hostapd_data *hapd = ctx;
   2116 	struct sta_info *sta = sta_ctx;
   2117 
   2118 	ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
   2119 #endif /* CONFIG_NO_RADIUS */
   2120 }
   2121 
   2122 
   2123 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
   2124 				 int preauth, int remediation)
   2125 {
   2126 	struct hostapd_data *hapd = ctx;
   2127 	struct sta_info *sta = sta_ctx;
   2128 	if (preauth)
   2129 		rsn_preauth_finished(hapd, sta, success);
   2130 	else
   2131 		ieee802_1x_finished(hapd, sta, success, remediation);
   2132 }
   2133 
   2134 
   2135 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
   2136 				   size_t identity_len, int phase2,
   2137 				   struct eap_user *user)
   2138 {
   2139 	struct hostapd_data *hapd = ctx;
   2140 	const struct hostapd_eap_user *eap_user;
   2141 	int i;
   2142 	int rv = -1;
   2143 
   2144 	eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
   2145 	if (eap_user == NULL)
   2146 		goto out;
   2147 
   2148 	os_memset(user, 0, sizeof(*user));
   2149 	user->phase2 = phase2;
   2150 	for (i = 0; i < EAP_MAX_METHODS; i++) {
   2151 		user->methods[i].vendor = eap_user->methods[i].vendor;
   2152 		user->methods[i].method = eap_user->methods[i].method;
   2153 	}
   2154 
   2155 	if (eap_user->password) {
   2156 		user->password = os_memdup(eap_user->password,
   2157 					   eap_user->password_len);
   2158 		if (user->password == NULL)
   2159 			goto out;
   2160 		user->password_len = eap_user->password_len;
   2161 		user->password_hash = eap_user->password_hash;
   2162 		if (eap_user->salt && eap_user->salt_len) {
   2163 			user->salt = os_memdup(eap_user->salt,
   2164 					       eap_user->salt_len);
   2165 			if (!user->salt)
   2166 				goto out;
   2167 			user->salt_len = eap_user->salt_len;
   2168 		}
   2169 	}
   2170 	user->force_version = eap_user->force_version;
   2171 	user->macacl = eap_user->macacl;
   2172 	user->ttls_auth = eap_user->ttls_auth;
   2173 	user->remediation = eap_user->remediation;
   2174 	rv = 0;
   2175 
   2176 out:
   2177 	if (rv)
   2178 		wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
   2179 
   2180 	return rv;
   2181 }
   2182 
   2183 
   2184 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
   2185 {
   2186 	struct hostapd_data *hapd = ctx;
   2187 	struct sta_info *sta;
   2188 	sta = ap_get_sta(hapd, addr);
   2189 	if (sta == NULL || sta->eapol_sm == NULL)
   2190 		return 0;
   2191 	return 1;
   2192 }
   2193 
   2194 
   2195 static void ieee802_1x_logger(void *ctx, const u8 *addr,
   2196 			      eapol_logger_level level, const char *txt)
   2197 {
   2198 #ifndef CONFIG_NO_HOSTAPD_LOGGER
   2199 	struct hostapd_data *hapd = ctx;
   2200 	int hlevel;
   2201 
   2202 	switch (level) {
   2203 	case EAPOL_LOGGER_WARNING:
   2204 		hlevel = HOSTAPD_LEVEL_WARNING;
   2205 		break;
   2206 	case EAPOL_LOGGER_INFO:
   2207 		hlevel = HOSTAPD_LEVEL_INFO;
   2208 		break;
   2209 	case EAPOL_LOGGER_DEBUG:
   2210 	default:
   2211 		hlevel = HOSTAPD_LEVEL_DEBUG;
   2212 		break;
   2213 	}
   2214 
   2215 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
   2216 		       txt);
   2217 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
   2218 }
   2219 
   2220 
   2221 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
   2222 					   int authorized)
   2223 {
   2224 	struct hostapd_data *hapd = ctx;
   2225 	struct sta_info *sta = sta_ctx;
   2226 	ieee802_1x_set_sta_authorized(hapd, sta, authorized);
   2227 }
   2228 
   2229 
   2230 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
   2231 {
   2232 	struct hostapd_data *hapd = ctx;
   2233 	struct sta_info *sta = sta_ctx;
   2234 	ieee802_1x_abort_auth(hapd, sta);
   2235 }
   2236 
   2237 
   2238 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
   2239 {
   2240 #ifndef CONFIG_FIPS
   2241 #ifndef CONFIG_NO_RC4
   2242 	struct hostapd_data *hapd = ctx;
   2243 	struct sta_info *sta = sta_ctx;
   2244 	ieee802_1x_tx_key(hapd, sta);
   2245 #endif /* CONFIG_NO_RC4 */
   2246 #endif /* CONFIG_FIPS */
   2247 }
   2248 
   2249 
   2250 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
   2251 				   enum eapol_event type)
   2252 {
   2253 	/* struct hostapd_data *hapd = ctx; */
   2254 	struct sta_info *sta = sta_ctx;
   2255 	switch (type) {
   2256 	case EAPOL_AUTH_SM_CHANGE:
   2257 		wpa_auth_sm_notify(sta->wpa_sm);
   2258 		break;
   2259 	case EAPOL_AUTH_REAUTHENTICATE:
   2260 		wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
   2261 		break;
   2262 	}
   2263 }
   2264 
   2265 
   2266 #ifdef CONFIG_ERP
   2267 
   2268 static struct eap_server_erp_key *
   2269 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
   2270 {
   2271 	struct hostapd_data *hapd = ctx;
   2272 	struct eap_server_erp_key *erp;
   2273 
   2274 	dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
   2275 			 list) {
   2276 		if (os_strcmp(erp->keyname_nai, keyname) == 0)
   2277 			return erp;
   2278 	}
   2279 
   2280 	return NULL;
   2281 }
   2282 
   2283 
   2284 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
   2285 {
   2286 	struct hostapd_data *hapd = ctx;
   2287 
   2288 	dl_list_add(&hapd->erp_keys, &erp->list);
   2289 	return 0;
   2290 }
   2291 
   2292 #endif /* CONFIG_ERP */
   2293 
   2294 
   2295 int ieee802_1x_init(struct hostapd_data *hapd)
   2296 {
   2297 	int i;
   2298 	struct eapol_auth_config conf;
   2299 	struct eapol_auth_cb cb;
   2300 
   2301 	dl_list_init(&hapd->erp_keys);
   2302 
   2303 	os_memset(&conf, 0, sizeof(conf));
   2304 	conf.ctx = hapd;
   2305 	conf.eap_reauth_period = hapd->conf->eap_reauth_period;
   2306 	conf.wpa = hapd->conf->wpa;
   2307 	conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
   2308 	conf.eap_server = hapd->conf->eap_server;
   2309 	conf.ssl_ctx = hapd->ssl_ctx;
   2310 	conf.msg_ctx = hapd->msg_ctx;
   2311 	conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
   2312 	conf.eap_req_id_text = hapd->conf->eap_req_id_text;
   2313 	conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
   2314 	conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
   2315 	conf.erp_domain = hapd->conf->erp_domain;
   2316 	conf.erp = hapd->conf->eap_server_erp;
   2317 	conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
   2318 	conf.tls_flags = hapd->conf->tls_flags;
   2319 	conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
   2320 	conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
   2321 	conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
   2322 	conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
   2323 	conf.eap_fast_prov = hapd->conf->eap_fast_prov;
   2324 	conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
   2325 	conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
   2326 	conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
   2327 	conf.tnc = hapd->conf->tnc;
   2328 	conf.wps = hapd->wps;
   2329 	conf.fragment_size = hapd->conf->fragment_size;
   2330 	conf.pwd_group = hapd->conf->pwd_group;
   2331 	conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
   2332 	if (hapd->conf->server_id) {
   2333 		conf.server_id = (const u8 *) hapd->conf->server_id;
   2334 		conf.server_id_len = os_strlen(hapd->conf->server_id);
   2335 	} else {
   2336 		conf.server_id = (const u8 *) "hostapd";
   2337 		conf.server_id_len = 7;
   2338 	}
   2339 
   2340 	os_memset(&cb, 0, sizeof(cb));
   2341 	cb.eapol_send = ieee802_1x_eapol_send;
   2342 	cb.aaa_send = ieee802_1x_aaa_send;
   2343 	cb.finished = _ieee802_1x_finished;
   2344 	cb.get_eap_user = ieee802_1x_get_eap_user;
   2345 	cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
   2346 	cb.logger = ieee802_1x_logger;
   2347 	cb.set_port_authorized = ieee802_1x_set_port_authorized;
   2348 	cb.abort_auth = _ieee802_1x_abort_auth;
   2349 	cb.tx_key = _ieee802_1x_tx_key;
   2350 	cb.eapol_event = ieee802_1x_eapol_event;
   2351 #ifdef CONFIG_ERP
   2352 	cb.erp_get_key = ieee802_1x_erp_get_key;
   2353 	cb.erp_add_key = ieee802_1x_erp_add_key;
   2354 #endif /* CONFIG_ERP */
   2355 
   2356 	hapd->eapol_auth = eapol_auth_init(&conf, &cb);
   2357 	if (hapd->eapol_auth == NULL)
   2358 		return -1;
   2359 
   2360 	if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
   2361 	    hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
   2362 		return -1;
   2363 
   2364 #ifndef CONFIG_NO_RADIUS
   2365 	if (radius_client_register(hapd->radius, RADIUS_AUTH,
   2366 				   ieee802_1x_receive_auth, hapd))
   2367 		return -1;
   2368 #endif /* CONFIG_NO_RADIUS */
   2369 
   2370 	if (hapd->conf->default_wep_key_len) {
   2371 		for (i = 0; i < 4; i++)
   2372 			hostapd_drv_set_key(hapd->conf->iface, hapd,
   2373 					    WPA_ALG_NONE, NULL, i, 0, NULL, 0,
   2374 					    NULL, 0);
   2375 
   2376 		ieee802_1x_rekey(hapd, NULL);
   2377 
   2378 		if (hapd->eapol_auth->default_wep_key == NULL)
   2379 			return -1;
   2380 	}
   2381 
   2382 	return 0;
   2383 }
   2384 
   2385 
   2386 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
   2387 {
   2388 	struct eap_server_erp_key *erp;
   2389 
   2390 	while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
   2391 				    list)) != NULL) {
   2392 		dl_list_del(&erp->list);
   2393 		bin_clear_free(erp, sizeof(*erp));
   2394 	}
   2395 }
   2396 
   2397 
   2398 void ieee802_1x_deinit(struct hostapd_data *hapd)
   2399 {
   2400 	eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
   2401 
   2402 	if (hapd->driver && hapd->drv_priv &&
   2403 	    (hapd->conf->ieee802_1x || hapd->conf->wpa))
   2404 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
   2405 
   2406 	eapol_auth_deinit(hapd->eapol_auth);
   2407 	hapd->eapol_auth = NULL;
   2408 
   2409 	ieee802_1x_erp_flush(hapd);
   2410 }
   2411 
   2412 
   2413 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
   2414 			 const u8 *buf, size_t len, int ack)
   2415 {
   2416 	struct ieee80211_hdr *hdr;
   2417 	u8 *pos;
   2418 	const unsigned char rfc1042_hdr[ETH_ALEN] =
   2419 		{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
   2420 
   2421 	if (sta == NULL)
   2422 		return -1;
   2423 	if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
   2424 		return 0;
   2425 
   2426 	hdr = (struct ieee80211_hdr *) buf;
   2427 	pos = (u8 *) (hdr + 1);
   2428 	if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
   2429 		return 0;
   2430 	pos += sizeof(rfc1042_hdr);
   2431 	if (WPA_GET_BE16(pos) != ETH_P_PAE)
   2432 		return 0;
   2433 	pos += 2;
   2434 
   2435 	return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
   2436 					  ack);
   2437 }
   2438 
   2439 
   2440 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
   2441 			       const u8 *buf, int len, int ack)
   2442 {
   2443 	const struct ieee802_1x_hdr *xhdr =
   2444 		(const struct ieee802_1x_hdr *) buf;
   2445 	const u8 *pos = buf + sizeof(*xhdr);
   2446 	struct ieee802_1x_eapol_key *key;
   2447 
   2448 	if (len < (int) sizeof(*xhdr))
   2449 		return 0;
   2450 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
   2451 		   "type=%d length=%d - ack=%d",
   2452 		   MAC2STR(sta->addr), xhdr->version, xhdr->type,
   2453 		   be_to_host16(xhdr->length), ack);
   2454 
   2455 #ifdef CONFIG_WPS
   2456 	if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
   2457 	    (sta->flags & WLAN_STA_WPS) &&
   2458 	    ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
   2459 		wpa_printf(MSG_DEBUG,
   2460 			   "WPS: Indicate EAP completion on ACK for EAP-Failure");
   2461 		hostapd_wps_eap_completed(hapd);
   2462 	}
   2463 #endif /* CONFIG_WPS */
   2464 
   2465 	if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
   2466 		return 0;
   2467 
   2468 	if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
   2469 		const struct wpa_eapol_key *wpa;
   2470 		wpa = (const struct wpa_eapol_key *) pos;
   2471 		if (wpa->type == EAPOL_KEY_TYPE_RSN ||
   2472 		    wpa->type == EAPOL_KEY_TYPE_WPA)
   2473 			wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
   2474 						     sta->wpa_sm, ack);
   2475 	}
   2476 
   2477 	/* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
   2478 	 * or Authenticator state machines, but EAPOL-Key packets are not
   2479 	 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
   2480 	 * packets couple of times because otherwise STA keys become
   2481 	 * unsynchronized with AP. */
   2482 	if (!ack && pos + sizeof(*key) <= buf + len) {
   2483 		key = (struct ieee802_1x_eapol_key *) pos;
   2484 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
   2485 			       HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
   2486 			       "frame (%scast index=%d)",
   2487 			       key->key_index & BIT(7) ? "uni" : "broad",
   2488 			       key->key_index & ~BIT(7));
   2489 		/* TODO: re-send EAPOL-Key couple of times (with short delay
   2490 		 * between them?). If all attempt fail, report error and
   2491 		 * deauthenticate STA so that it will get new keys when
   2492 		 * authenticating again (e.g., after returning in range).
   2493 		 * Separate limit/transmit state needed both for unicast and
   2494 		 * broadcast keys(?) */
   2495 	}
   2496 	/* TODO: could move unicast key configuration from ieee802_1x_tx_key()
   2497 	 * to here and change the key only if the EAPOL-Key packet was Acked.
   2498 	 */
   2499 
   2500 	return 1;
   2501 }
   2502 
   2503 
   2504 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
   2505 {
   2506 	if (sm == NULL || sm->identity == NULL)
   2507 		return NULL;
   2508 
   2509 	*len = sm->identity_len;
   2510 	return sm->identity;
   2511 }
   2512 
   2513 
   2514 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
   2515 				 int idx)
   2516 {
   2517 	if (sm == NULL || sm->radius_class.attr == NULL ||
   2518 	    idx >= (int) sm->radius_class.count)
   2519 		return NULL;
   2520 
   2521 	*len = sm->radius_class.attr[idx].len;
   2522 	return sm->radius_class.attr[idx].data;
   2523 }
   2524 
   2525 
   2526 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
   2527 {
   2528 	if (sm == NULL)
   2529 		return NULL;
   2530 	return sm->radius_cui;
   2531 }
   2532 
   2533 
   2534 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
   2535 {
   2536 	*len = 0;
   2537 	if (sm == NULL)
   2538 		return NULL;
   2539 
   2540 	*len = sm->eap_if->eapKeyDataLen;
   2541 	return sm->eap_if->eapKeyData;
   2542 }
   2543 
   2544 
   2545 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
   2546 				    int enabled)
   2547 {
   2548 	if (sm == NULL)
   2549 		return;
   2550 	sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
   2551 	eapol_auth_step(sm);
   2552 }
   2553 
   2554 
   2555 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
   2556 				  int valid)
   2557 {
   2558 	if (sm == NULL)
   2559 		return;
   2560 	sm->portValid = valid ? TRUE : FALSE;
   2561 	eapol_auth_step(sm);
   2562 }
   2563 
   2564 
   2565 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
   2566 {
   2567 	if (sm == NULL)
   2568 		return;
   2569 	if (pre_auth)
   2570 		sm->flags |= EAPOL_SM_PREAUTH;
   2571 	else
   2572 		sm->flags &= ~EAPOL_SM_PREAUTH;
   2573 }
   2574 
   2575 
   2576 static const char * bool_txt(Boolean val)
   2577 {
   2578 	return val ? "TRUE" : "FALSE";
   2579 }
   2580 
   2581 
   2582 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
   2583 {
   2584 	/* TODO */
   2585 	return 0;
   2586 }
   2587 
   2588 
   2589 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
   2590 			   char *buf, size_t buflen)
   2591 {
   2592 	int len = 0, ret;
   2593 	struct eapol_state_machine *sm = sta->eapol_sm;
   2594 	struct os_reltime diff;
   2595 	const char *name1;
   2596 	const char *name2;
   2597 	char *identity_buf = NULL;
   2598 
   2599 	if (sm == NULL)
   2600 		return 0;
   2601 
   2602 	ret = os_snprintf(buf + len, buflen - len,
   2603 			  "dot1xPaePortNumber=%d\n"
   2604 			  "dot1xPaePortProtocolVersion=%d\n"
   2605 			  "dot1xPaePortCapabilities=1\n"
   2606 			  "dot1xPaePortInitialize=%d\n"
   2607 			  "dot1xPaePortReauthenticate=FALSE\n",
   2608 			  sta->aid,
   2609 			  EAPOL_VERSION,
   2610 			  sm->initialize);
   2611 	if (os_snprintf_error(buflen - len, ret))
   2612 		return len;
   2613 	len += ret;
   2614 
   2615 	/* dot1xAuthConfigTable */
   2616 	ret = os_snprintf(buf + len, buflen - len,
   2617 			  "dot1xAuthPaeState=%d\n"
   2618 			  "dot1xAuthBackendAuthState=%d\n"
   2619 			  "dot1xAuthAdminControlledDirections=%d\n"
   2620 			  "dot1xAuthOperControlledDirections=%d\n"
   2621 			  "dot1xAuthAuthControlledPortStatus=%d\n"
   2622 			  "dot1xAuthAuthControlledPortControl=%d\n"
   2623 			  "dot1xAuthQuietPeriod=%u\n"
   2624 			  "dot1xAuthServerTimeout=%u\n"
   2625 			  "dot1xAuthReAuthPeriod=%u\n"
   2626 			  "dot1xAuthReAuthEnabled=%s\n"
   2627 			  "dot1xAuthKeyTxEnabled=%s\n",
   2628 			  sm->auth_pae_state + 1,
   2629 			  sm->be_auth_state + 1,
   2630 			  sm->adminControlledDirections,
   2631 			  sm->operControlledDirections,
   2632 			  sm->authPortStatus,
   2633 			  sm->portControl,
   2634 			  sm->quietPeriod,
   2635 			  sm->serverTimeout,
   2636 			  sm->reAuthPeriod,
   2637 			  bool_txt(sm->reAuthEnabled),
   2638 			  bool_txt(sm->keyTxEnabled));
   2639 	if (os_snprintf_error(buflen - len, ret))
   2640 		return len;
   2641 	len += ret;
   2642 
   2643 	/* dot1xAuthStatsTable */
   2644 	ret = os_snprintf(buf + len, buflen - len,
   2645 			  "dot1xAuthEapolFramesRx=%u\n"
   2646 			  "dot1xAuthEapolFramesTx=%u\n"
   2647 			  "dot1xAuthEapolStartFramesRx=%u\n"
   2648 			  "dot1xAuthEapolLogoffFramesRx=%u\n"
   2649 			  "dot1xAuthEapolRespIdFramesRx=%u\n"
   2650 			  "dot1xAuthEapolRespFramesRx=%u\n"
   2651 			  "dot1xAuthEapolReqIdFramesTx=%u\n"
   2652 			  "dot1xAuthEapolReqFramesTx=%u\n"
   2653 			  "dot1xAuthInvalidEapolFramesRx=%u\n"
   2654 			  "dot1xAuthEapLengthErrorFramesRx=%u\n"
   2655 			  "dot1xAuthLastEapolFrameVersion=%u\n"
   2656 			  "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
   2657 			  sm->dot1xAuthEapolFramesRx,
   2658 			  sm->dot1xAuthEapolFramesTx,
   2659 			  sm->dot1xAuthEapolStartFramesRx,
   2660 			  sm->dot1xAuthEapolLogoffFramesRx,
   2661 			  sm->dot1xAuthEapolRespIdFramesRx,
   2662 			  sm->dot1xAuthEapolRespFramesRx,
   2663 			  sm->dot1xAuthEapolReqIdFramesTx,
   2664 			  sm->dot1xAuthEapolReqFramesTx,
   2665 			  sm->dot1xAuthInvalidEapolFramesRx,
   2666 			  sm->dot1xAuthEapLengthErrorFramesRx,
   2667 			  sm->dot1xAuthLastEapolFrameVersion,
   2668 			  MAC2STR(sm->addr));
   2669 	if (os_snprintf_error(buflen - len, ret))
   2670 		return len;
   2671 	len += ret;
   2672 
   2673 	/* dot1xAuthDiagTable */
   2674 	ret = os_snprintf(buf + len, buflen - len,
   2675 			  "dot1xAuthEntersConnecting=%u\n"
   2676 			  "dot1xAuthEapLogoffsWhileConnecting=%u\n"
   2677 			  "dot1xAuthEntersAuthenticating=%u\n"
   2678 			  "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
   2679 			  "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
   2680 			  "dot1xAuthAuthFailWhileAuthenticating=%u\n"
   2681 			  "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
   2682 			  "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
   2683 			  "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
   2684 			  "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
   2685 			  "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
   2686 			  "dot1xAuthBackendResponses=%u\n"
   2687 			  "dot1xAuthBackendAccessChallenges=%u\n"
   2688 			  "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
   2689 			  "dot1xAuthBackendAuthSuccesses=%u\n"
   2690 			  "dot1xAuthBackendAuthFails=%u\n",
   2691 			  sm->authEntersConnecting,
   2692 			  sm->authEapLogoffsWhileConnecting,
   2693 			  sm->authEntersAuthenticating,
   2694 			  sm->authAuthSuccessesWhileAuthenticating,
   2695 			  sm->authAuthTimeoutsWhileAuthenticating,
   2696 			  sm->authAuthFailWhileAuthenticating,
   2697 			  sm->authAuthEapStartsWhileAuthenticating,
   2698 			  sm->authAuthEapLogoffWhileAuthenticating,
   2699 			  sm->authAuthReauthsWhileAuthenticated,
   2700 			  sm->authAuthEapStartsWhileAuthenticated,
   2701 			  sm->authAuthEapLogoffWhileAuthenticated,
   2702 			  sm->backendResponses,
   2703 			  sm->backendAccessChallenges,
   2704 			  sm->backendOtherRequestsToSupplicant,
   2705 			  sm->backendAuthSuccesses,
   2706 			  sm->backendAuthFails);
   2707 	if (os_snprintf_error(buflen - len, ret))
   2708 		return len;
   2709 	len += ret;
   2710 
   2711 	/* dot1xAuthSessionStatsTable */
   2712 	os_reltime_age(&sta->acct_session_start, &diff);
   2713 	if (sm->eap && !sm->identity) {
   2714 		const u8 *id;
   2715 		size_t id_len;
   2716 
   2717 		id = eap_get_identity(sm->eap, &id_len);
   2718 		if (id)
   2719 			identity_buf = dup_binstr(id, id_len);
   2720 	}
   2721 	ret = os_snprintf(buf + len, buflen - len,
   2722 			  /* TODO: dot1xAuthSessionOctetsRx */
   2723 			  /* TODO: dot1xAuthSessionOctetsTx */
   2724 			  /* TODO: dot1xAuthSessionFramesRx */
   2725 			  /* TODO: dot1xAuthSessionFramesTx */
   2726 			  "dot1xAuthSessionId=%016llX\n"
   2727 			  "dot1xAuthSessionAuthenticMethod=%d\n"
   2728 			  "dot1xAuthSessionTime=%u\n"
   2729 			  "dot1xAuthSessionTerminateCause=999\n"
   2730 			  "dot1xAuthSessionUserName=%s\n",
   2731 			  (unsigned long long) sta->acct_session_id,
   2732 			  (wpa_key_mgmt_wpa_ieee8021x(
   2733 				   wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
   2734 			  1 : 2,
   2735 			  (unsigned int) diff.sec,
   2736 			  sm->identity ? (char *) sm->identity :
   2737 					 (identity_buf ? identity_buf : "N/A"));
   2738 	os_free(identity_buf);
   2739 	if (os_snprintf_error(buflen - len, ret))
   2740 		return len;
   2741 	len += ret;
   2742 
   2743 	if (sm->acct_multi_session_id) {
   2744 		ret = os_snprintf(buf + len, buflen - len,
   2745 				  "authMultiSessionId=%016llX\n",
   2746 				  (unsigned long long)
   2747 				  sm->acct_multi_session_id);
   2748 		if (os_snprintf_error(buflen - len, ret))
   2749 			return len;
   2750 		len += ret;
   2751 	}
   2752 
   2753 	name1 = eap_server_get_name(0, sm->eap_type_authsrv);
   2754 	name2 = eap_server_get_name(0, sm->eap_type_supp);
   2755 	ret = os_snprintf(buf + len, buflen - len,
   2756 			  "last_eap_type_as=%d (%s)\n"
   2757 			  "last_eap_type_sta=%d (%s)\n",
   2758 			  sm->eap_type_authsrv, name1,
   2759 			  sm->eap_type_supp, name2);
   2760 	if (os_snprintf_error(buflen - len, ret))
   2761 		return len;
   2762 	len += ret;
   2763 
   2764 	return len;
   2765 }
   2766 
   2767 
   2768 #ifdef CONFIG_HS20
   2769 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
   2770 {
   2771 	struct hostapd_data *hapd = eloop_ctx;
   2772 	struct sta_info *sta = timeout_ctx;
   2773 
   2774 	if (sta->remediation) {
   2775 		wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
   2776 			   MACSTR " to indicate Subscription Remediation",
   2777 			   MAC2STR(sta->addr));
   2778 		hs20_send_wnm_notification(hapd, sta->addr,
   2779 					   sta->remediation_method,
   2780 					   sta->remediation_url);
   2781 		os_free(sta->remediation_url);
   2782 		sta->remediation_url = NULL;
   2783 	}
   2784 
   2785 	if (sta->hs20_deauth_req) {
   2786 		wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
   2787 			   MACSTR " to indicate imminent deauthentication",
   2788 			   MAC2STR(sta->addr));
   2789 		hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
   2790 						      sta->hs20_deauth_req);
   2791 	}
   2792 
   2793 	if (sta->hs20_t_c_filtering) {
   2794 		wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
   2795 			   MACSTR " to indicate Terms and Conditions filtering",
   2796 			   MAC2STR(sta->addr));
   2797 		hs20_send_wnm_notification_t_c(hapd, sta->addr, sta->t_c_url);
   2798 		os_free(sta->t_c_url);
   2799 		sta->t_c_url = NULL;
   2800 	}
   2801 }
   2802 #endif /* CONFIG_HS20 */
   2803 
   2804 
   2805 static void ieee802_1x_finished(struct hostapd_data *hapd,
   2806 				struct sta_info *sta, int success,
   2807 				int remediation)
   2808 {
   2809 	const u8 *key;
   2810 	size_t len;
   2811 	/* TODO: get PMKLifetime from WPA parameters */
   2812 	static const int dot11RSNAConfigPMKLifetime = 43200;
   2813 	unsigned int session_timeout;
   2814 	struct os_reltime now, remaining;
   2815 
   2816 #ifdef CONFIG_HS20
   2817 	if (remediation && !sta->remediation) {
   2818 		sta->remediation = 1;
   2819 		os_free(sta->remediation_url);
   2820 		sta->remediation_url =
   2821 			os_strdup(hapd->conf->subscr_remediation_url);
   2822 		sta->remediation_method = 1; /* SOAP-XML SPP */
   2823 	}
   2824 
   2825 	if (success && (sta->remediation || sta->hs20_deauth_req ||
   2826 			sta->hs20_t_c_filtering)) {
   2827 		wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
   2828 			   MACSTR " in 100 ms", MAC2STR(sta->addr));
   2829 		eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
   2830 		eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
   2831 				       hapd, sta);
   2832 	}
   2833 #endif /* CONFIG_HS20 */
   2834 
   2835 	key = ieee802_1x_get_key(sta->eapol_sm, &len);
   2836 	if (sta->session_timeout_set) {
   2837 		os_get_reltime(&now);
   2838 		os_reltime_sub(&sta->session_timeout, &now, &remaining);
   2839 		session_timeout = (remaining.sec > 0) ? remaining.sec : 1;
   2840 	} else {
   2841 		session_timeout = dot11RSNAConfigPMKLifetime;
   2842 	}
   2843 	if (success && key && len >= PMK_LEN && !sta->remediation &&
   2844 	    !sta->hs20_deauth_requested &&
   2845 	    wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
   2846 			       sta->eapol_sm) == 0) {
   2847 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
   2848 			       HOSTAPD_LEVEL_DEBUG,
   2849 			       "Added PMKSA cache entry (IEEE 802.1X)");
   2850 	}
   2851 
   2852 	if (!success) {
   2853 		/*
   2854 		 * Many devices require deauthentication after WPS provisioning
   2855 		 * and some may not be be able to do that themselves, so
   2856 		 * disconnect the client here. In addition, this may also
   2857 		 * benefit IEEE 802.1X/EAPOL authentication cases, too since
   2858 		 * the EAPOL PAE state machine would remain in HELD state for
   2859 		 * considerable amount of time and some EAP methods, like
   2860 		 * EAP-FAST with anonymous provisioning, may require another
   2861 		 * EAPOL authentication to be started to complete connection.
   2862 		 */
   2863 		ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta);
   2864 	}
   2865 }
   2866