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