xref: /netbsd-src/external/bsd/wpa/dist/src/ap/ieee802_1x.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*
2  * hostapd / IEEE 802.1X-2004 Authenticator
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "utils/includes.h"
16 
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "crypto/md5.h"
20 #include "crypto/crypto.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/wpa_ctrl.h"
23 #include "radius/radius.h"
24 #include "radius/radius_client.h"
25 #include "eap_server/eap.h"
26 #include "eapol_auth/eapol_auth_sm.h"
27 #include "eapol_auth/eapol_auth_sm_i.h"
28 #include "hostapd.h"
29 #include "accounting.h"
30 #include "sta_info.h"
31 #include "wpa_auth.h"
32 #include "preauth_auth.h"
33 #include "pmksa_cache_auth.h"
34 #include "ap_config.h"
35 #include "ieee802_1x.h"
36 
37 
38 static void ieee802_1x_finished(struct hostapd_data *hapd,
39 				struct sta_info *sta, int success);
40 
41 
42 static 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 		hapd->drv.send_eapol(hapd, sta->addr, buf, len, encrypt);
73 	}
74 
75 	os_free(buf);
76 }
77 
78 
79 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
80 				   struct sta_info *sta, int authorized)
81 {
82 	int res;
83 
84 	if (sta->flags & WLAN_STA_PREAUTH)
85 		return;
86 
87 	if (authorized) {
88 		if (!(sta->flags & WLAN_STA_AUTHORIZED))
89 			wpa_msg(hapd->msg_ctx, MSG_INFO,
90 				AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
91 		sta->flags |= WLAN_STA_AUTHORIZED;
92 		res = hapd->drv.set_authorized(hapd, sta, 1);
93 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
94 			       HOSTAPD_LEVEL_DEBUG, "authorizing port");
95 	} else {
96 		if ((sta->flags & (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC)) ==
97 		    (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC))
98 			wpa_msg(hapd->msg_ctx, MSG_INFO,
99 				AP_STA_DISCONNECTED MACSTR,
100 				MAC2STR(sta->addr));
101 		sta->flags &= ~WLAN_STA_AUTHORIZED;
102 		res = hapd->drv.set_authorized(hapd, sta, 0);
103 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
104 			       HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
105 	}
106 
107 	if (res && errno != ENOENT) {
108 		printf("Could not set station " MACSTR " flags for kernel "
109 		       "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
110 	}
111 
112 	if (authorized)
113 		accounting_sta_start(hapd, sta);
114 }
115 
116 
117 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
118 				  struct sta_info *sta,
119 				  int idx, int broadcast,
120 				  u8 *key_data, size_t key_len)
121 {
122 	u8 *buf, *ekey;
123 	struct ieee802_1x_hdr *hdr;
124 	struct ieee802_1x_eapol_key *key;
125 	size_t len, ekey_len;
126 	struct eapol_state_machine *sm = sta->eapol_sm;
127 
128 	if (sm == NULL)
129 		return;
130 
131 	len = sizeof(*key) + key_len;
132 	buf = os_zalloc(sizeof(*hdr) + len);
133 	if (buf == NULL)
134 		return;
135 
136 	hdr = (struct ieee802_1x_hdr *) buf;
137 	key = (struct ieee802_1x_eapol_key *) (hdr + 1);
138 	key->type = EAPOL_KEY_TYPE_RC4;
139 	key->key_length = htons(key_len);
140 	wpa_get_ntp_timestamp(key->replay_counter);
141 
142 	if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
143 		wpa_printf(MSG_ERROR, "Could not get random numbers");
144 		os_free(buf);
145 		return;
146 	}
147 
148 	key->key_index = idx | (broadcast ? 0 : BIT(7));
149 	if (hapd->conf->eapol_key_index_workaround) {
150 		/* According to some information, WinXP Supplicant seems to
151 		 * interpret bit7 as an indication whether the key is to be
152 		 * activated, so make it possible to enable workaround that
153 		 * sets this bit for all keys. */
154 		key->key_index |= BIT(7);
155 	}
156 
157 	/* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
158 	 * MSK[32..63] is used to sign the message. */
159 	if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
160 		wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
161 			   "and signing EAPOL-Key");
162 		os_free(buf);
163 		return;
164 	}
165 	os_memcpy((u8 *) (key + 1), key_data, key_len);
166 	ekey_len = sizeof(key->key_iv) + 32;
167 	ekey = os_malloc(ekey_len);
168 	if (ekey == NULL) {
169 		wpa_printf(MSG_ERROR, "Could not encrypt key");
170 		os_free(buf);
171 		return;
172 	}
173 	os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
174 	os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
175 	rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
176 	os_free(ekey);
177 
178 	/* This header is needed here for HMAC-MD5, but it will be regenerated
179 	 * in ieee802_1x_send() */
180 	hdr->version = hapd->conf->eapol_version;
181 	hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
182 	hdr->length = host_to_be16(len);
183 	hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
184 		 key->key_signature);
185 
186 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
187 		   " (%s index=%d)", MAC2STR(sm->addr),
188 		   broadcast ? "broadcast" : "unicast", idx);
189 	ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
190 	if (sta->eapol_sm)
191 		sta->eapol_sm->dot1xAuthEapolFramesTx++;
192 	os_free(buf);
193 }
194 
195 
196 #ifndef CONFIG_NO_VLAN
197 static struct hostapd_wep_keys *
198 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
199 {
200 	struct hostapd_wep_keys *key;
201 
202 	key = os_zalloc(sizeof(*key));
203 	if (key == NULL)
204 		return NULL;
205 
206 	key->default_len = hapd->conf->default_wep_key_len;
207 
208 	if (key->idx >= hapd->conf->broadcast_key_idx_max ||
209 	    key->idx < hapd->conf->broadcast_key_idx_min)
210 		key->idx = hapd->conf->broadcast_key_idx_min;
211 	else
212 		key->idx++;
213 
214 	if (!key->key[key->idx])
215 		key->key[key->idx] = os_malloc(key->default_len);
216 	if (key->key[key->idx] == NULL ||
217 	    os_get_random(key->key[key->idx], key->default_len)) {
218 		printf("Could not generate random WEP key (dynamic VLAN).\n");
219 		os_free(key->key[key->idx]);
220 		key->key[key->idx] = NULL;
221 		os_free(key);
222 		return NULL;
223 	}
224 	key->len[key->idx] = key->default_len;
225 
226 	wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
227 		   ifname, key->idx);
228 	wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
229 			key->key[key->idx], key->len[key->idx]);
230 
231 	if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL, key->idx, 1,
232 			      NULL, 0, key->key[key->idx], key->len[key->idx]))
233 		printf("Could not set dynamic VLAN WEP encryption key.\n");
234 
235 	hapd->drv.set_drv_ieee8021x(hapd, ifname, 1);
236 
237 	return key;
238 }
239 
240 
241 static struct hostapd_wep_keys *
242 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
243 		     size_t vlan_id)
244 {
245 	const char *ifname;
246 
247 	if (vlan_id == 0)
248 		return &ssid->wep;
249 
250 	if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
251 	    ssid->dyn_vlan_keys[vlan_id])
252 		return ssid->dyn_vlan_keys[vlan_id];
253 
254 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
255 		   "state machine for VLAN ID %lu",
256 		   (unsigned long) vlan_id);
257 
258 	ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
259 	if (ifname == NULL) {
260 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
261 			   "cannot create group key state machine",
262 			   (unsigned long) vlan_id);
263 		return NULL;
264 	}
265 
266 	if (ssid->dyn_vlan_keys == NULL) {
267 		int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
268 		ssid->dyn_vlan_keys = os_zalloc(size);
269 		if (ssid->dyn_vlan_keys == NULL)
270 			return NULL;
271 		ssid->max_dyn_vlan_keys = vlan_id;
272 	}
273 
274 	if (ssid->max_dyn_vlan_keys < vlan_id) {
275 		struct hostapd_wep_keys **na;
276 		int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
277 		na = os_realloc(ssid->dyn_vlan_keys, size);
278 		if (na == NULL)
279 			return NULL;
280 		ssid->dyn_vlan_keys = na;
281 		os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
282 			  (vlan_id - ssid->max_dyn_vlan_keys) *
283 			  sizeof(ssid->dyn_vlan_keys[0]));
284 		ssid->max_dyn_vlan_keys = vlan_id;
285 	}
286 
287 	ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
288 
289 	return ssid->dyn_vlan_keys[vlan_id];
290 }
291 #endif /* CONFIG_NO_VLAN */
292 
293 
294 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
295 {
296 	struct eapol_authenticator *eapol = hapd->eapol_auth;
297 	struct eapol_state_machine *sm = sta->eapol_sm;
298 #ifndef CONFIG_NO_VLAN
299 	struct hostapd_wep_keys *key = NULL;
300 	int vlan_id;
301 #endif /* CONFIG_NO_VLAN */
302 
303 	if (sm == NULL || !sm->eap_if->eapKeyData)
304 		return;
305 
306 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
307 		   MAC2STR(sta->addr));
308 
309 #ifndef CONFIG_NO_VLAN
310 	vlan_id = sta->vlan_id;
311 	if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
312 		vlan_id = 0;
313 
314 	if (vlan_id) {
315 		key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
316 		if (key && key->key[key->idx])
317 			ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
318 					      key->key[key->idx],
319 					      key->len[key->idx]);
320 	} else
321 #endif /* CONFIG_NO_VLAN */
322 	if (eapol->default_wep_key) {
323 		ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
324 				      eapol->default_wep_key,
325 				      hapd->conf->default_wep_key_len);
326 	}
327 
328 	if (hapd->conf->individual_wep_key_len > 0) {
329 		u8 *ikey;
330 		ikey = os_malloc(hapd->conf->individual_wep_key_len);
331 		if (ikey == NULL ||
332 		    os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
333 			wpa_printf(MSG_ERROR, "Could not generate random "
334 				   "individual WEP key.");
335 			os_free(ikey);
336 			return;
337 		}
338 
339 		wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
340 				ikey, hapd->conf->individual_wep_key_len);
341 
342 		ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
343 				      hapd->conf->individual_wep_key_len);
344 
345 		/* TODO: set encryption in TX callback, i.e., only after STA
346 		 * has ACKed EAPOL-Key frame */
347 		if (hapd->drv.set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
348 				      sta->addr, 0, 1, NULL, 0, ikey,
349 				      hapd->conf->individual_wep_key_len)) {
350 			wpa_printf(MSG_ERROR, "Could not set individual WEP "
351 				   "encryption.");
352 		}
353 
354 		os_free(ikey);
355 	}
356 }
357 
358 
359 const char *radius_mode_txt(struct hostapd_data *hapd)
360 {
361 	switch (hapd->iface->conf->hw_mode) {
362 	case HOSTAPD_MODE_IEEE80211A:
363 		return "802.11a";
364 	case HOSTAPD_MODE_IEEE80211G:
365 		return "802.11g";
366 	case HOSTAPD_MODE_IEEE80211B:
367 	default:
368 		return "802.11b";
369 	}
370 }
371 
372 
373 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
374 {
375 	int i;
376 	u8 rate = 0;
377 
378 	for (i = 0; i < sta->supported_rates_len; i++)
379 		if ((sta->supported_rates[i] & 0x7f) > rate)
380 			rate = sta->supported_rates[i] & 0x7f;
381 
382 	return rate;
383 }
384 
385 
386 #ifndef CONFIG_NO_RADIUS
387 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
388 				      struct eapol_state_machine *sm,
389 				      const u8 *eap, size_t len)
390 {
391 	const u8 *identity;
392 	size_t identity_len;
393 
394 	if (len <= sizeof(struct eap_hdr) ||
395 	    eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
396 		return;
397 
398 	identity = eap_get_identity(sm->eap, &identity_len);
399 	if (identity == NULL)
400 		return;
401 
402 	/* Save station identity for future RADIUS packets */
403 	os_free(sm->identity);
404 	sm->identity = os_malloc(identity_len + 1);
405 	if (sm->identity == NULL) {
406 		sm->identity_len = 0;
407 		return;
408 	}
409 
410 	os_memcpy(sm->identity, identity, identity_len);
411 	sm->identity_len = identity_len;
412 	sm->identity[identity_len] = '\0';
413 	hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
414 		       HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
415 	sm->dot1xAuthEapolRespIdFramesRx++;
416 }
417 
418 
419 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
420 					  struct sta_info *sta,
421 					  const u8 *eap, size_t len)
422 {
423 	struct radius_msg *msg;
424 	char buf[128];
425 	struct eapol_state_machine *sm = sta->eapol_sm;
426 
427 	if (sm == NULL)
428 		return;
429 
430 	ieee802_1x_learn_identity(hapd, sm, eap, len);
431 
432 	wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
433 		   "packet");
434 
435 	sm->radius_identifier = radius_client_get_id(hapd->radius);
436 	msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
437 			     sm->radius_identifier);
438 	if (msg == NULL) {
439 		printf("Could not create net RADIUS packet\n");
440 		return;
441 	}
442 
443 	radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
444 
445 	if (sm->identity &&
446 	    !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
447 				 sm->identity, sm->identity_len)) {
448 		printf("Could not add User-Name\n");
449 		goto fail;
450 	}
451 
452 	if (hapd->conf->own_ip_addr.af == AF_INET &&
453 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
454 				 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
455 		printf("Could not add NAS-IP-Address\n");
456 		goto fail;
457 	}
458 
459 #ifdef CONFIG_IPV6
460 	if (hapd->conf->own_ip_addr.af == AF_INET6 &&
461 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
462 				 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
463 		printf("Could not add NAS-IPv6-Address\n");
464 		goto fail;
465 	}
466 #endif /* CONFIG_IPV6 */
467 
468 	if (hapd->conf->nas_identifier &&
469 	    !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
470 				 (u8 *) hapd->conf->nas_identifier,
471 				 os_strlen(hapd->conf->nas_identifier))) {
472 		printf("Could not add NAS-Identifier\n");
473 		goto fail;
474 	}
475 
476 	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
477 		printf("Could not add NAS-Port\n");
478 		goto fail;
479 	}
480 
481 	os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
482 		    MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
483 	buf[sizeof(buf) - 1] = '\0';
484 	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
485 				 (u8 *) buf, os_strlen(buf))) {
486 		printf("Could not add Called-Station-Id\n");
487 		goto fail;
488 	}
489 
490 	os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
491 		    MAC2STR(sta->addr));
492 	buf[sizeof(buf) - 1] = '\0';
493 	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
494 				 (u8 *) buf, os_strlen(buf))) {
495 		printf("Could not add Calling-Station-Id\n");
496 		goto fail;
497 	}
498 
499 	/* TODO: should probably check MTU from driver config; 2304 is max for
500 	 * IEEE 802.11, but use 1400 to avoid problems with too large packets
501 	 */
502 	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
503 		printf("Could not add Framed-MTU\n");
504 		goto fail;
505 	}
506 
507 	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
508 				       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
509 		printf("Could not add NAS-Port-Type\n");
510 		goto fail;
511 	}
512 
513 	if (sta->flags & WLAN_STA_PREAUTH) {
514 		os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
515 			   sizeof(buf));
516 	} else {
517 		os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
518 			    radius_sta_rate(hapd, sta) / 2,
519 			    (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
520 			    radius_mode_txt(hapd));
521 		buf[sizeof(buf) - 1] = '\0';
522 	}
523 	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
524 				 (u8 *) buf, os_strlen(buf))) {
525 		printf("Could not add Connect-Info\n");
526 		goto fail;
527 	}
528 
529 	if (eap && !radius_msg_add_eap(msg, eap, len)) {
530 		printf("Could not add EAP-Message\n");
531 		goto fail;
532 	}
533 
534 	/* State attribute must be copied if and only if this packet is
535 	 * Access-Request reply to the previous Access-Challenge */
536 	if (sm->last_recv_radius &&
537 	    radius_msg_get_hdr(sm->last_recv_radius)->code ==
538 	    RADIUS_CODE_ACCESS_CHALLENGE) {
539 		int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
540 					       RADIUS_ATTR_STATE);
541 		if (res < 0) {
542 			printf("Could not copy State attribute from previous "
543 			       "Access-Challenge\n");
544 			goto fail;
545 		}
546 		if (res > 0) {
547 			wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
548 		}
549 	}
550 
551 	radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
552 	return;
553 
554  fail:
555 	radius_msg_free(msg);
556 }
557 #endif /* CONFIG_NO_RADIUS */
558 
559 
560 static void handle_eap_response(struct hostapd_data *hapd,
561 				struct sta_info *sta, struct eap_hdr *eap,
562 				size_t len)
563 {
564 	u8 type, *data;
565 	struct eapol_state_machine *sm = sta->eapol_sm;
566 	if (sm == NULL)
567 		return;
568 
569 	data = (u8 *) (eap + 1);
570 
571 	if (len < sizeof(*eap) + 1) {
572 		printf("handle_eap_response: too short response data\n");
573 		return;
574 	}
575 
576 	sm->eap_type_supp = type = data[0];
577 
578 	hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
579 		       HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
580 		       "id=%d len=%d) from STA: EAP Response-%s (%d)",
581 		       eap->code, eap->identifier, be_to_host16(eap->length),
582 		       eap_server_get_name(0, type), type);
583 
584 	sm->dot1xAuthEapolRespFramesRx++;
585 
586 	wpabuf_free(sm->eap_if->eapRespData);
587 	sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
588 	sm->eapolEap = TRUE;
589 }
590 
591 
592 /* Process incoming EAP packet from Supplicant */
593 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
594 		       u8 *buf, size_t len)
595 {
596 	struct eap_hdr *eap;
597 	u16 eap_len;
598 
599 	if (len < sizeof(*eap)) {
600 		printf("   too short EAP packet\n");
601 		return;
602 	}
603 
604 	eap = (struct eap_hdr *) buf;
605 
606 	eap_len = be_to_host16(eap->length);
607 	wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
608 		   eap->code, eap->identifier, eap_len);
609 	if (eap_len < sizeof(*eap)) {
610 		wpa_printf(MSG_DEBUG, "   Invalid EAP length");
611 		return;
612 	} else if (eap_len > len) {
613 		wpa_printf(MSG_DEBUG, "   Too short frame to contain this EAP "
614 			   "packet");
615 		return;
616 	} else if (eap_len < len) {
617 		wpa_printf(MSG_DEBUG, "   Ignoring %lu extra bytes after EAP "
618 			   "packet", (unsigned long) len - eap_len);
619 	}
620 
621 	switch (eap->code) {
622 	case EAP_CODE_REQUEST:
623 		wpa_printf(MSG_DEBUG, " (request)");
624 		return;
625 	case EAP_CODE_RESPONSE:
626 		wpa_printf(MSG_DEBUG, " (response)");
627 		handle_eap_response(hapd, sta, eap, eap_len);
628 		break;
629 	case EAP_CODE_SUCCESS:
630 		wpa_printf(MSG_DEBUG, " (success)");
631 		return;
632 	case EAP_CODE_FAILURE:
633 		wpa_printf(MSG_DEBUG, " (failure)");
634 		return;
635 	default:
636 		wpa_printf(MSG_DEBUG, " (unknown code)");
637 		return;
638 	}
639 }
640 
641 
642 static struct eapol_state_machine *
643 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
644 {
645 	int flags = 0;
646 	if (sta->flags & WLAN_STA_PREAUTH)
647 		flags |= EAPOL_SM_PREAUTH;
648 	if (sta->wpa_sm) {
649 		if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
650 			flags |= EAPOL_SM_USES_WPA;
651 		if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
652 			flags |= EAPOL_SM_FROM_PMKSA_CACHE;
653 	}
654 	return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
655 				sta->wps_ie, sta);
656 }
657 
658 
659 /**
660  * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
661  * @hapd: hostapd BSS data
662  * @sa: Source address (sender of the EAPOL frame)
663  * @buf: EAPOL frame
664  * @len: Length of buf in octets
665  *
666  * This function is called for each incoming EAPOL frame from the interface
667  */
668 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
669 			size_t len)
670 {
671 	struct sta_info *sta;
672 	struct ieee802_1x_hdr *hdr;
673 	struct ieee802_1x_eapol_key *key;
674 	u16 datalen;
675 	struct rsn_pmksa_cache_entry *pmksa;
676 
677 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
678 	    !hapd->conf->wps_state)
679 		return;
680 
681 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
682 		   (unsigned long) len, MAC2STR(sa));
683 	sta = ap_get_sta(hapd, sa);
684 	if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
685 		wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
686 			   "associated STA");
687 		return;
688 	}
689 
690 	if (len < sizeof(*hdr)) {
691 		printf("   too short IEEE 802.1X packet\n");
692 		return;
693 	}
694 
695 	hdr = (struct ieee802_1x_hdr *) buf;
696 	datalen = be_to_host16(hdr->length);
697 	wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
698 		   hdr->version, hdr->type, datalen);
699 
700 	if (len - sizeof(*hdr) < datalen) {
701 		printf("   frame too short for this IEEE 802.1X packet\n");
702 		if (sta->eapol_sm)
703 			sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
704 		return;
705 	}
706 	if (len - sizeof(*hdr) > datalen) {
707 		wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
708 			   "IEEE 802.1X packet",
709 			   (unsigned long) len - sizeof(*hdr) - datalen);
710 	}
711 
712 	if (sta->eapol_sm) {
713 		sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
714 		sta->eapol_sm->dot1xAuthEapolFramesRx++;
715 	}
716 
717 	key = (struct ieee802_1x_eapol_key *) (hdr + 1);
718 	if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
719 	    hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
720 	    (key->type == EAPOL_KEY_TYPE_WPA ||
721 	     key->type == EAPOL_KEY_TYPE_RSN)) {
722 		wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
723 			    sizeof(*hdr) + datalen);
724 		return;
725 	}
726 
727 	if ((!hapd->conf->ieee802_1x &&
728 	     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
729 	    wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
730 		return;
731 
732 	if (!sta->eapol_sm) {
733 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
734 		if (!sta->eapol_sm)
735 			return;
736 
737 #ifdef CONFIG_WPS
738 		if (!hapd->conf->ieee802_1x &&
739 		    ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
740 		     WLAN_STA_MAYBE_WPS)) {
741 			/*
742 			 * Delay EAPOL frame transmission until a possible WPS
743 			 * STA initiates the handshake with EAPOL-Start.
744 			 */
745 			sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
746 		}
747 #endif /* CONFIG_WPS */
748 
749 		sta->eapol_sm->eap_if->portEnabled = TRUE;
750 	}
751 
752 	/* since we support version 1, we can ignore version field and proceed
753 	 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
754 	/* TODO: actually, we are not version 1 anymore.. However, Version 2
755 	 * does not change frame contents, so should be ok to process frames
756 	 * more or less identically. Some changes might be needed for
757 	 * verification of fields. */
758 
759 	switch (hdr->type) {
760 	case IEEE802_1X_TYPE_EAP_PACKET:
761 		handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
762 		break;
763 
764 	case IEEE802_1X_TYPE_EAPOL_START:
765 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
766 			       HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
767 			       "from STA");
768 		sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
769 		pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
770 		if (pmksa) {
771 			hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
772 				       HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
773 				       "available - ignore it since "
774 				       "STA sent EAPOL-Start");
775 			wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
776 		}
777 		sta->eapol_sm->eapolStart = TRUE;
778 		sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
779 		wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
780 		break;
781 
782 	case IEEE802_1X_TYPE_EAPOL_LOGOFF:
783 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
784 			       HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
785 			       "from STA");
786 		sta->acct_terminate_cause =
787 			RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
788 		accounting_sta_stop(hapd, sta);
789 		sta->eapol_sm->eapolLogoff = TRUE;
790 		sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
791 		break;
792 
793 	case IEEE802_1X_TYPE_EAPOL_KEY:
794 		wpa_printf(MSG_DEBUG, "   EAPOL-Key");
795 		if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
796 			wpa_printf(MSG_DEBUG, "   Dropped key data from "
797 				   "unauthorized Supplicant");
798 			break;
799 		}
800 		break;
801 
802 	case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
803 		wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
804 		/* TODO: implement support for this; show data */
805 		break;
806 
807 	default:
808 		wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
809 		sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
810 		break;
811 	}
812 
813 	eapol_auth_step(sta->eapol_sm);
814 }
815 
816 
817 /**
818  * ieee802_1x_new_station - Start IEEE 802.1X authentication
819  * @hapd: hostapd BSS data
820  * @sta: The station
821  *
822  * This function is called to start IEEE 802.1X authentication when a new
823  * station completes IEEE 802.11 association.
824  */
825 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
826 {
827 	struct rsn_pmksa_cache_entry *pmksa;
828 	int reassoc = 1;
829 	int force_1x = 0;
830 
831 #ifdef CONFIG_WPS
832 	if (hapd->conf->wps_state && hapd->conf->wpa &&
833 	    (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
834 		/*
835 		 * Need to enable IEEE 802.1X/EAPOL state machines for possible
836 		 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
837 		 * authentication in this BSS.
838 		 */
839 		force_1x = 1;
840 	}
841 #endif /* CONFIG_WPS */
842 
843 	if ((!force_1x && !hapd->conf->ieee802_1x) ||
844 	    wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
845 		return;
846 
847 	if (sta->eapol_sm == NULL) {
848 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
849 			       HOSTAPD_LEVEL_DEBUG, "start authentication");
850 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
851 		if (sta->eapol_sm == NULL) {
852 			hostapd_logger(hapd, sta->addr,
853 				       HOSTAPD_MODULE_IEEE8021X,
854 				       HOSTAPD_LEVEL_INFO,
855 				       "failed to allocate state machine");
856 			return;
857 		}
858 		reassoc = 0;
859 	}
860 
861 #ifdef CONFIG_WPS
862 	sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
863 	if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
864 		/*
865 		 * Delay EAPOL frame transmission until a possible WPS
866 		 * initiates the handshake with EAPOL-Start.
867 		 */
868 		sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
869 	}
870 #endif /* CONFIG_WPS */
871 
872 	sta->eapol_sm->eap_if->portEnabled = TRUE;
873 
874 	pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
875 	if (pmksa) {
876 		int old_vlanid;
877 
878 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
879 			       HOSTAPD_LEVEL_DEBUG,
880 			       "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
881 		/* Setup EAPOL state machines to already authenticated state
882 		 * because of existing PMKSA information in the cache. */
883 		sta->eapol_sm->keyRun = TRUE;
884 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
885 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
886 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
887 		sta->eapol_sm->authSuccess = TRUE;
888 		if (sta->eapol_sm->eap)
889 			eap_sm_notify_cached(sta->eapol_sm->eap);
890 		old_vlanid = sta->vlan_id;
891 		pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
892 		if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
893 			sta->vlan_id = 0;
894 		ap_sta_bind_vlan(hapd, sta, old_vlanid);
895 	} else {
896 		if (reassoc) {
897 			/*
898 			 * Force EAPOL state machines to start
899 			 * re-authentication without having to wait for the
900 			 * Supplicant to send EAPOL-Start.
901 			 */
902 			sta->eapol_sm->reAuthenticate = TRUE;
903 		}
904 		eapol_auth_step(sta->eapol_sm);
905 	}
906 }
907 
908 
909 void ieee802_1x_free_station(struct sta_info *sta)
910 {
911 	struct eapol_state_machine *sm = sta->eapol_sm;
912 
913 	if (sm == NULL)
914 		return;
915 
916 	sta->eapol_sm = NULL;
917 
918 #ifndef CONFIG_NO_RADIUS
919 	radius_msg_free(sm->last_recv_radius);
920 	radius_free_class(&sm->radius_class);
921 #endif /* CONFIG_NO_RADIUS */
922 
923 	os_free(sm->identity);
924 	eapol_auth_free(sm);
925 }
926 
927 
928 #ifndef CONFIG_NO_RADIUS
929 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
930 					  struct sta_info *sta)
931 {
932 	u8 *eap;
933 	size_t len;
934 	struct eap_hdr *hdr;
935 	int eap_type = -1;
936 	char buf[64];
937 	struct radius_msg *msg;
938 	struct eapol_state_machine *sm = sta->eapol_sm;
939 
940 	if (sm == NULL || sm->last_recv_radius == NULL) {
941 		if (sm)
942 			sm->eap_if->aaaEapNoReq = TRUE;
943 		return;
944 	}
945 
946 	msg = sm->last_recv_radius;
947 
948 	eap = radius_msg_get_eap(msg, &len);
949 	if (eap == NULL) {
950 		/* RFC 3579, Chap. 2.6.3:
951 		 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
952 		 * attribute */
953 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
954 			       HOSTAPD_LEVEL_WARNING, "could not extract "
955 			       "EAP-Message from RADIUS message");
956 		sm->eap_if->aaaEapNoReq = TRUE;
957 		return;
958 	}
959 
960 	if (len < sizeof(*hdr)) {
961 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
962 			       HOSTAPD_LEVEL_WARNING, "too short EAP packet "
963 			       "received from authentication server");
964 		os_free(eap);
965 		sm->eap_if->aaaEapNoReq = TRUE;
966 		return;
967 	}
968 
969 	if (len > sizeof(*hdr))
970 		eap_type = eap[sizeof(*hdr)];
971 
972 	hdr = (struct eap_hdr *) eap;
973 	switch (hdr->code) {
974 	case EAP_CODE_REQUEST:
975 		if (eap_type >= 0)
976 			sm->eap_type_authsrv = eap_type;
977 		os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
978 			    eap_type >= 0 ? eap_server_get_name(0, eap_type) :
979 			    "??",
980 			    eap_type);
981 		break;
982 	case EAP_CODE_RESPONSE:
983 		os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
984 			    eap_type >= 0 ? eap_server_get_name(0, eap_type) :
985 			    "??",
986 			    eap_type);
987 		break;
988 	case EAP_CODE_SUCCESS:
989 		os_strlcpy(buf, "EAP Success", sizeof(buf));
990 		break;
991 	case EAP_CODE_FAILURE:
992 		os_strlcpy(buf, "EAP Failure", sizeof(buf));
993 		break;
994 	default:
995 		os_strlcpy(buf, "unknown EAP code", sizeof(buf));
996 		break;
997 	}
998 	buf[sizeof(buf) - 1] = '\0';
999 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1000 		       HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1001 		       "id=%d len=%d) from RADIUS server: %s",
1002 		       hdr->code, hdr->identifier, be_to_host16(hdr->length),
1003 		       buf);
1004 	sm->eap_if->aaaEapReq = TRUE;
1005 
1006 	wpabuf_free(sm->eap_if->aaaEapReqData);
1007 	sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1008 }
1009 
1010 
1011 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1012 				struct sta_info *sta, struct radius_msg *msg,
1013 				struct radius_msg *req,
1014 				const u8 *shared_secret,
1015 				size_t shared_secret_len)
1016 {
1017 	struct radius_ms_mppe_keys *keys;
1018 	struct eapol_state_machine *sm = sta->eapol_sm;
1019 	if (sm == NULL)
1020 		return;
1021 
1022 	keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1023 				      shared_secret_len);
1024 
1025 	if (keys && keys->send && keys->recv) {
1026 		size_t len = keys->send_len + keys->recv_len;
1027 		wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1028 				keys->send, keys->send_len);
1029 		wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1030 				keys->recv, keys->recv_len);
1031 
1032 		os_free(sm->eap_if->aaaEapKeyData);
1033 		sm->eap_if->aaaEapKeyData = os_malloc(len);
1034 		if (sm->eap_if->aaaEapKeyData) {
1035 			os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1036 				  keys->recv_len);
1037 			os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1038 				  keys->send, keys->send_len);
1039 			sm->eap_if->aaaEapKeyDataLen = len;
1040 			sm->eap_if->aaaEapKeyAvailable = TRUE;
1041 		}
1042 	}
1043 
1044 	if (keys) {
1045 		os_free(keys->send);
1046 		os_free(keys->recv);
1047 		os_free(keys);
1048 	}
1049 }
1050 
1051 
1052 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1053 					  struct sta_info *sta,
1054 					  struct radius_msg *msg)
1055 {
1056 	u8 *class;
1057 	size_t class_len;
1058 	struct eapol_state_machine *sm = sta->eapol_sm;
1059 	int count, i;
1060 	struct radius_attr_data *nclass;
1061 	size_t nclass_count;
1062 
1063 	if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1064 	    sm == NULL)
1065 		return;
1066 
1067 	radius_free_class(&sm->radius_class);
1068 	count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1069 	if (count <= 0)
1070 		return;
1071 
1072 	nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1073 	if (nclass == NULL)
1074 		return;
1075 
1076 	nclass_count = 0;
1077 
1078 	class = NULL;
1079 	for (i = 0; i < count; i++) {
1080 		do {
1081 			if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1082 						    &class, &class_len,
1083 						    class) < 0) {
1084 				i = count;
1085 				break;
1086 			}
1087 		} while (class_len < 1);
1088 
1089 		nclass[nclass_count].data = os_malloc(class_len);
1090 		if (nclass[nclass_count].data == NULL)
1091 			break;
1092 
1093 		os_memcpy(nclass[nclass_count].data, class, class_len);
1094 		nclass[nclass_count].len = class_len;
1095 		nclass_count++;
1096 	}
1097 
1098 	sm->radius_class.attr = nclass;
1099 	sm->radius_class.count = nclass_count;
1100 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1101 		   "attributes for " MACSTR,
1102 		   (unsigned long) sm->radius_class.count,
1103 		   MAC2STR(sta->addr));
1104 }
1105 
1106 
1107 /* Update sta->identity based on User-Name attribute in Access-Accept */
1108 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1109 					   struct sta_info *sta,
1110 					   struct radius_msg *msg)
1111 {
1112 	u8 *buf, *identity;
1113 	size_t len;
1114 	struct eapol_state_machine *sm = sta->eapol_sm;
1115 
1116 	if (sm == NULL)
1117 		return;
1118 
1119 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1120 				    NULL) < 0)
1121 		return;
1122 
1123 	identity = os_malloc(len + 1);
1124 	if (identity == NULL)
1125 		return;
1126 
1127 	os_memcpy(identity, buf, len);
1128 	identity[len] = '\0';
1129 
1130 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1131 		       HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1132 		       "User-Name from Access-Accept '%s'",
1133 		       sm->identity ? (char *) sm->identity : "N/A",
1134 		       (char *) identity);
1135 
1136 	os_free(sm->identity);
1137 	sm->identity = identity;
1138 	sm->identity_len = len;
1139 }
1140 
1141 
1142 struct sta_id_search {
1143 	u8 identifier;
1144 	struct eapol_state_machine *sm;
1145 };
1146 
1147 
1148 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1149 					       struct sta_info *sta,
1150 					       void *ctx)
1151 {
1152 	struct sta_id_search *id_search = ctx;
1153 	struct eapol_state_machine *sm = sta->eapol_sm;
1154 
1155 	if (sm && sm->radius_identifier >= 0 &&
1156 	    sm->radius_identifier == id_search->identifier) {
1157 		id_search->sm = sm;
1158 		return 1;
1159 	}
1160 	return 0;
1161 }
1162 
1163 
1164 static struct eapol_state_machine *
1165 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1166 {
1167 	struct sta_id_search id_search;
1168 	id_search.identifier = identifier;
1169 	id_search.sm = NULL;
1170 	ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1171 	return id_search.sm;
1172 }
1173 
1174 
1175 /**
1176  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1177  * @msg: RADIUS response message
1178  * @req: RADIUS request message
1179  * @shared_secret: RADIUS shared secret
1180  * @shared_secret_len: Length of shared_secret in octets
1181  * @data: Context data (struct hostapd_data *)
1182  * Returns: Processing status
1183  */
1184 static RadiusRxResult
1185 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1186 			const u8 *shared_secret, size_t shared_secret_len,
1187 			void *data)
1188 {
1189 	struct hostapd_data *hapd = data;
1190 	struct sta_info *sta;
1191 	u32 session_timeout = 0, termination_action, acct_interim_interval;
1192 	int session_timeout_set, old_vlanid = 0;
1193 	struct eapol_state_machine *sm;
1194 	int override_eapReq = 0;
1195 	struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1196 
1197 	sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1198 	if (sm == NULL) {
1199 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1200 			   "station for this RADIUS message");
1201 		return RADIUS_RX_UNKNOWN;
1202 	}
1203 	sta = sm->sta;
1204 
1205 	/* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1206 	 * present when packet contains an EAP-Message attribute */
1207 	if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1208 	    radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1209 				0) < 0 &&
1210 	    radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1211 		wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1212 			   "Message-Authenticator since it does not include "
1213 			   "EAP-Message");
1214 	} else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1215 				     req, 1)) {
1216 		printf("Incoming RADIUS packet did not have correct "
1217 		       "Message-Authenticator - dropped\n");
1218 		return RADIUS_RX_INVALID_AUTHENTICATOR;
1219 	}
1220 
1221 	if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1222 	    hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1223 	    hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1224 		printf("Unknown RADIUS message code\n");
1225 		return RADIUS_RX_UNKNOWN;
1226 	}
1227 
1228 	sm->radius_identifier = -1;
1229 	wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1230 		   MAC2STR(sta->addr));
1231 
1232 	radius_msg_free(sm->last_recv_radius);
1233 	sm->last_recv_radius = msg;
1234 
1235 	session_timeout_set =
1236 		!radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1237 					   &session_timeout);
1238 	if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1239 				      &termination_action))
1240 		termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1241 
1242 	if (hapd->conf->acct_interim_interval == 0 &&
1243 	    hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1244 	    radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1245 				      &acct_interim_interval) == 0) {
1246 		if (acct_interim_interval < 60) {
1247 			hostapd_logger(hapd, sta->addr,
1248 				       HOSTAPD_MODULE_IEEE8021X,
1249 				       HOSTAPD_LEVEL_INFO,
1250 				       "ignored too small "
1251 				       "Acct-Interim-Interval %d",
1252 				       acct_interim_interval);
1253 		} else
1254 			sta->acct_interim_interval = acct_interim_interval;
1255 	}
1256 
1257 
1258 	switch (hdr->code) {
1259 	case RADIUS_CODE_ACCESS_ACCEPT:
1260 		if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1261 			sta->vlan_id = 0;
1262 #ifndef CONFIG_NO_VLAN
1263 		else {
1264 			old_vlanid = sta->vlan_id;
1265 			sta->vlan_id = radius_msg_get_vlanid(msg);
1266 		}
1267 		if (sta->vlan_id > 0 &&
1268 		    hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1269 					       sta->vlan_id)) {
1270 			hostapd_logger(hapd, sta->addr,
1271 				       HOSTAPD_MODULE_RADIUS,
1272 				       HOSTAPD_LEVEL_INFO,
1273 				       "VLAN ID %d", sta->vlan_id);
1274 		} else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1275 			sta->eapol_sm->authFail = TRUE;
1276 			hostapd_logger(hapd, sta->addr,
1277 				       HOSTAPD_MODULE_IEEE8021X,
1278 				       HOSTAPD_LEVEL_INFO, "authentication "
1279 				       "server did not include required VLAN "
1280 				       "ID in Access-Accept");
1281 			break;
1282 		}
1283 #endif /* CONFIG_NO_VLAN */
1284 
1285 		if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
1286 			break;
1287 
1288 		/* RFC 3580, Ch. 3.17 */
1289 		if (session_timeout_set && termination_action ==
1290 		    RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1291 			sm->reAuthPeriod = session_timeout;
1292 		} else if (session_timeout_set)
1293 			ap_sta_session_timeout(hapd, sta, session_timeout);
1294 
1295 		sm->eap_if->aaaSuccess = TRUE;
1296 		override_eapReq = 1;
1297 		ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1298 				    shared_secret_len);
1299 		ieee802_1x_store_radius_class(hapd, sta, msg);
1300 		ieee802_1x_update_sta_identity(hapd, sta, msg);
1301 		if (sm->eap_if->eapKeyAvailable &&
1302 		    wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1303 				       session_timeout_set ?
1304 				       (int) session_timeout : -1, sm) == 0) {
1305 			hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1306 				       HOSTAPD_LEVEL_DEBUG,
1307 				       "Added PMKSA cache entry");
1308 		}
1309 		break;
1310 	case RADIUS_CODE_ACCESS_REJECT:
1311 		sm->eap_if->aaaFail = TRUE;
1312 		override_eapReq = 1;
1313 		break;
1314 	case RADIUS_CODE_ACCESS_CHALLENGE:
1315 		sm->eap_if->aaaEapReq = TRUE;
1316 		if (session_timeout_set) {
1317 			/* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1318 			sm->eap_if->aaaMethodTimeout = session_timeout;
1319 			hostapd_logger(hapd, sm->addr,
1320 				       HOSTAPD_MODULE_IEEE8021X,
1321 				       HOSTAPD_LEVEL_DEBUG,
1322 				       "using EAP timeout of %d seconds (from "
1323 				       "RADIUS)",
1324 				       sm->eap_if->aaaMethodTimeout);
1325 		} else {
1326 			/*
1327 			 * Use dynamic retransmission behavior per EAP
1328 			 * specification.
1329 			 */
1330 			sm->eap_if->aaaMethodTimeout = 0;
1331 		}
1332 		break;
1333 	}
1334 
1335 	ieee802_1x_decapsulate_radius(hapd, sta);
1336 	if (override_eapReq)
1337 		sm->eap_if->aaaEapReq = FALSE;
1338 
1339 	eapol_auth_step(sm);
1340 
1341 	return RADIUS_RX_QUEUED;
1342 }
1343 #endif /* CONFIG_NO_RADIUS */
1344 
1345 
1346 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1347 {
1348 	struct eapol_state_machine *sm = sta->eapol_sm;
1349 	if (sm == NULL)
1350 		return;
1351 
1352 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1353 		       HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1354 
1355 #ifndef CONFIG_NO_RADIUS
1356 	radius_msg_free(sm->last_recv_radius);
1357 	sm->last_recv_radius = NULL;
1358 #endif /* CONFIG_NO_RADIUS */
1359 
1360 	if (sm->eap_if->eapTimeout) {
1361 		/*
1362 		 * Disconnect the STA since it did not reply to the last EAP
1363 		 * request and we cannot continue EAP processing (EAP-Failure
1364 		 * could only be sent if the EAP peer actually replied).
1365 		 */
1366 		sm->eap_if->portEnabled = FALSE;
1367 		ap_sta_disconnect(hapd, sta, sta->addr,
1368 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
1369 	}
1370 }
1371 
1372 
1373 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1374 {
1375 	struct eapol_authenticator *eapol = hapd->eapol_auth;
1376 
1377 	if (hapd->conf->default_wep_key_len < 1)
1378 		return 0;
1379 
1380 	os_free(eapol->default_wep_key);
1381 	eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1382 	if (eapol->default_wep_key == NULL ||
1383 	    os_get_random(eapol->default_wep_key,
1384 			  hapd->conf->default_wep_key_len)) {
1385 		printf("Could not generate random WEP key.\n");
1386 		os_free(eapol->default_wep_key);
1387 		eapol->default_wep_key = NULL;
1388 		return -1;
1389 	}
1390 
1391 	wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1392 			eapol->default_wep_key,
1393 			hapd->conf->default_wep_key_len);
1394 
1395 	return 0;
1396 }
1397 
1398 
1399 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1400 					struct sta_info *sta, void *ctx)
1401 {
1402 	if (sta->eapol_sm) {
1403 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1404 		eapol_auth_step(sta->eapol_sm);
1405 	}
1406 	return 0;
1407 }
1408 
1409 
1410 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1411 {
1412 	struct hostapd_data *hapd = eloop_ctx;
1413 	struct eapol_authenticator *eapol = hapd->eapol_auth;
1414 
1415 	if (eapol->default_wep_key_idx >= 3)
1416 		eapol->default_wep_key_idx =
1417 			hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1418 	else
1419 		eapol->default_wep_key_idx++;
1420 
1421 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1422 		   eapol->default_wep_key_idx);
1423 
1424 	if (ieee802_1x_rekey_broadcast(hapd)) {
1425 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1426 			       HOSTAPD_LEVEL_WARNING, "failed to generate a "
1427 			       "new broadcast key");
1428 		os_free(eapol->default_wep_key);
1429 		eapol->default_wep_key = NULL;
1430 		return;
1431 	}
1432 
1433 	/* TODO: Could setup key for RX here, but change default TX keyid only
1434 	 * after new broadcast key has been sent to all stations. */
1435 	if (hapd->drv.set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
1436 			      eapol->default_wep_key_idx, 1, NULL, 0,
1437 			      eapol->default_wep_key,
1438 			      hapd->conf->default_wep_key_len)) {
1439 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1440 			       HOSTAPD_LEVEL_WARNING, "failed to configure a "
1441 			       "new broadcast key");
1442 		os_free(eapol->default_wep_key);
1443 		eapol->default_wep_key = NULL;
1444 		return;
1445 	}
1446 
1447 	ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1448 
1449 	if (hapd->conf->wep_rekeying_period > 0) {
1450 		eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1451 				       ieee802_1x_rekey, hapd, NULL);
1452 	}
1453 }
1454 
1455 
1456 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1457 				  const u8 *data, size_t datalen)
1458 {
1459 	ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1460 }
1461 
1462 
1463 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1464 				const u8 *data, size_t datalen)
1465 {
1466 #ifndef CONFIG_NO_RADIUS
1467 	struct hostapd_data *hapd = ctx;
1468 	struct sta_info *sta = sta_ctx;
1469 
1470 	ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1471 #endif /* CONFIG_NO_RADIUS */
1472 }
1473 
1474 
1475 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1476 				 int preauth)
1477 {
1478 	struct hostapd_data *hapd = ctx;
1479 	struct sta_info *sta = sta_ctx;
1480 	if (preauth)
1481 		rsn_preauth_finished(hapd, sta, success);
1482 	else
1483 		ieee802_1x_finished(hapd, sta, success);
1484 }
1485 
1486 
1487 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1488 				   size_t identity_len, int phase2,
1489 				   struct eap_user *user)
1490 {
1491 	struct hostapd_data *hapd = ctx;
1492 	const struct hostapd_eap_user *eap_user;
1493 	int i, count;
1494 
1495 	eap_user = hostapd_get_eap_user(hapd->conf, identity,
1496 					identity_len, phase2);
1497 	if (eap_user == NULL)
1498 		return -1;
1499 
1500 	os_memset(user, 0, sizeof(*user));
1501 	user->phase2 = phase2;
1502 	count = EAP_USER_MAX_METHODS;
1503 	if (count > EAP_MAX_METHODS)
1504 		count = EAP_MAX_METHODS;
1505 	for (i = 0; i < count; i++) {
1506 		user->methods[i].vendor = eap_user->methods[i].vendor;
1507 		user->methods[i].method = eap_user->methods[i].method;
1508 	}
1509 
1510 	if (eap_user->password) {
1511 		user->password = os_malloc(eap_user->password_len);
1512 		if (user->password == NULL)
1513 			return -1;
1514 		os_memcpy(user->password, eap_user->password,
1515 			  eap_user->password_len);
1516 		user->password_len = eap_user->password_len;
1517 	}
1518 	user->force_version = eap_user->force_version;
1519 	user->ttls_auth = eap_user->ttls_auth;
1520 
1521 	return 0;
1522 }
1523 
1524 
1525 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1526 {
1527 	struct hostapd_data *hapd = ctx;
1528 	struct sta_info *sta;
1529 	sta = ap_get_sta(hapd, addr);
1530 	if (sta == NULL || sta->eapol_sm == NULL)
1531 		return 0;
1532 	return 1;
1533 }
1534 
1535 
1536 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1537 			      eapol_logger_level level, const char *txt)
1538 {
1539 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1540 	struct hostapd_data *hapd = ctx;
1541 	int hlevel;
1542 
1543 	switch (level) {
1544 	case EAPOL_LOGGER_WARNING:
1545 		hlevel = HOSTAPD_LEVEL_WARNING;
1546 		break;
1547 	case EAPOL_LOGGER_INFO:
1548 		hlevel = HOSTAPD_LEVEL_INFO;
1549 		break;
1550 	case EAPOL_LOGGER_DEBUG:
1551 	default:
1552 		hlevel = HOSTAPD_LEVEL_DEBUG;
1553 		break;
1554 	}
1555 
1556 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1557 		       txt);
1558 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1559 }
1560 
1561 
1562 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1563 					   int authorized)
1564 {
1565 	struct hostapd_data *hapd = ctx;
1566 	struct sta_info *sta = sta_ctx;
1567 	ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1568 }
1569 
1570 
1571 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1572 {
1573 	struct hostapd_data *hapd = ctx;
1574 	struct sta_info *sta = sta_ctx;
1575 	ieee802_1x_abort_auth(hapd, sta);
1576 }
1577 
1578 
1579 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1580 {
1581 	struct hostapd_data *hapd = ctx;
1582 	struct sta_info *sta = sta_ctx;
1583 	ieee802_1x_tx_key(hapd, sta);
1584 }
1585 
1586 
1587 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1588 				   enum eapol_event type)
1589 {
1590 	/* struct hostapd_data *hapd = ctx; */
1591 	struct sta_info *sta = sta_ctx;
1592 	switch (type) {
1593 	case EAPOL_AUTH_SM_CHANGE:
1594 		wpa_auth_sm_notify(sta->wpa_sm);
1595 		break;
1596 	case EAPOL_AUTH_REAUTHENTICATE:
1597 		wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1598 		break;
1599 	}
1600 }
1601 
1602 
1603 int ieee802_1x_init(struct hostapd_data *hapd)
1604 {
1605 	int i;
1606 	struct eapol_auth_config conf;
1607 	struct eapol_auth_cb cb;
1608 
1609 	os_memset(&conf, 0, sizeof(conf));
1610 	conf.ctx = hapd;
1611 	conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1612 	conf.wpa = hapd->conf->wpa;
1613 	conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1614 	conf.eap_server = hapd->conf->eap_server;
1615 	conf.ssl_ctx = hapd->ssl_ctx;
1616 	conf.msg_ctx = hapd->msg_ctx;
1617 	conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1618 	conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1619 	conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1620 	conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1621 	conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1622 	conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1623 	conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1624 	conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1625 	conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1626 	conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1627 	conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1628 	conf.tnc = hapd->conf->tnc;
1629 	conf.wps = hapd->wps;
1630 
1631 	os_memset(&cb, 0, sizeof(cb));
1632 	cb.eapol_send = ieee802_1x_eapol_send;
1633 	cb.aaa_send = ieee802_1x_aaa_send;
1634 	cb.finished = _ieee802_1x_finished;
1635 	cb.get_eap_user = ieee802_1x_get_eap_user;
1636 	cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1637 	cb.logger = ieee802_1x_logger;
1638 	cb.set_port_authorized = ieee802_1x_set_port_authorized;
1639 	cb.abort_auth = _ieee802_1x_abort_auth;
1640 	cb.tx_key = _ieee802_1x_tx_key;
1641 	cb.eapol_event = ieee802_1x_eapol_event;
1642 
1643 	hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1644 	if (hapd->eapol_auth == NULL)
1645 		return -1;
1646 
1647 	if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1648 	    hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
1649 		return -1;
1650 
1651 #ifndef CONFIG_NO_RADIUS
1652 	if (radius_client_register(hapd->radius, RADIUS_AUTH,
1653 				   ieee802_1x_receive_auth, hapd))
1654 		return -1;
1655 #endif /* CONFIG_NO_RADIUS */
1656 
1657 	if (hapd->conf->default_wep_key_len) {
1658 		for (i = 0; i < 4; i++)
1659 			hapd->drv.set_key(hapd->conf->iface, hapd,
1660 					  WPA_ALG_NONE, NULL, i, 0, NULL, 0,
1661 					  NULL, 0);
1662 
1663 		ieee802_1x_rekey(hapd, NULL);
1664 
1665 		if (hapd->eapol_auth->default_wep_key == NULL)
1666 			return -1;
1667 	}
1668 
1669 	return 0;
1670 }
1671 
1672 
1673 void ieee802_1x_deinit(struct hostapd_data *hapd)
1674 {
1675 	eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1676 
1677 	if (hapd->driver != NULL &&
1678 	    (hapd->conf->ieee802_1x || hapd->conf->wpa))
1679 		hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
1680 
1681 	eapol_auth_deinit(hapd->eapol_auth);
1682 	hapd->eapol_auth = NULL;
1683 }
1684 
1685 
1686 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1687 			 const u8 *buf, size_t len, int ack)
1688 {
1689 	struct ieee80211_hdr *hdr;
1690 	struct ieee802_1x_hdr *xhdr;
1691 	struct ieee802_1x_eapol_key *key;
1692 	u8 *pos;
1693 	const unsigned char rfc1042_hdr[ETH_ALEN] =
1694 		{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1695 
1696 	if (sta == NULL)
1697 		return -1;
1698 	if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1699 		return 0;
1700 
1701 	hdr = (struct ieee80211_hdr *) buf;
1702 	pos = (u8 *) (hdr + 1);
1703 	if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1704 		return 0;
1705 	pos += sizeof(rfc1042_hdr);
1706 	if (WPA_GET_BE16(pos) != ETH_P_PAE)
1707 		return 0;
1708 	pos += 2;
1709 
1710 	xhdr = (struct ieee802_1x_hdr *) pos;
1711 	pos += sizeof(*xhdr);
1712 
1713 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1714 		   "type=%d length=%d - ack=%d",
1715 		   MAC2STR(sta->addr), xhdr->version, xhdr->type,
1716 		   be_to_host16(xhdr->length), ack);
1717 
1718 	/* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1719 	 * or Authenticator state machines, but EAPOL-Key packets are not
1720 	 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1721 	 * packets couple of times because otherwise STA keys become
1722 	 * unsynchronized with AP. */
1723 	if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1724 	    pos + sizeof(*key) <= buf + len) {
1725 		key = (struct ieee802_1x_eapol_key *) pos;
1726 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1727 			       HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1728 			       "frame (%scast index=%d)",
1729 			       key->key_index & BIT(7) ? "uni" : "broad",
1730 			       key->key_index & ~BIT(7));
1731 		/* TODO: re-send EAPOL-Key couple of times (with short delay
1732 		 * between them?). If all attempt fail, report error and
1733 		 * deauthenticate STA so that it will get new keys when
1734 		 * authenticating again (e.g., after returning in range).
1735 		 * Separate limit/transmit state needed both for unicast and
1736 		 * broadcast keys(?) */
1737 	}
1738 	/* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1739 	 * to here and change the key only if the EAPOL-Key packet was Acked.
1740 	 */
1741 
1742 	return 1;
1743 }
1744 
1745 
1746 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1747 {
1748 	if (sm == NULL || sm->identity == NULL)
1749 		return NULL;
1750 
1751 	*len = sm->identity_len;
1752 	return sm->identity;
1753 }
1754 
1755 
1756 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1757 				 int idx)
1758 {
1759 	if (sm == NULL || sm->radius_class.attr == NULL ||
1760 	    idx >= (int) sm->radius_class.count)
1761 		return NULL;
1762 
1763 	*len = sm->radius_class.attr[idx].len;
1764 	return sm->radius_class.attr[idx].data;
1765 }
1766 
1767 
1768 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1769 {
1770 	if (sm == NULL)
1771 		return NULL;
1772 
1773 	*len = sm->eap_if->eapKeyDataLen;
1774 	return sm->eap_if->eapKeyData;
1775 }
1776 
1777 
1778 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1779 				    int enabled)
1780 {
1781 	if (sm == NULL)
1782 		return;
1783 	sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1784 	eapol_auth_step(sm);
1785 }
1786 
1787 
1788 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1789 				  int valid)
1790 {
1791 	if (sm == NULL)
1792 		return;
1793 	sm->portValid = valid ? TRUE : FALSE;
1794 	eapol_auth_step(sm);
1795 }
1796 
1797 
1798 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1799 {
1800 	if (sm == NULL)
1801 		return;
1802 	if (pre_auth)
1803 		sm->flags |= EAPOL_SM_PREAUTH;
1804 	else
1805 		sm->flags &= ~EAPOL_SM_PREAUTH;
1806 }
1807 
1808 
1809 static const char * bool_txt(Boolean bool)
1810 {
1811 	return bool ? "TRUE" : "FALSE";
1812 }
1813 
1814 
1815 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1816 {
1817 	/* TODO */
1818 	return 0;
1819 }
1820 
1821 
1822 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1823 			   char *buf, size_t buflen)
1824 {
1825 	int len = 0, ret;
1826 	struct eapol_state_machine *sm = sta->eapol_sm;
1827 
1828 	if (sm == NULL)
1829 		return 0;
1830 
1831 	ret = os_snprintf(buf + len, buflen - len,
1832 			  "dot1xPaePortNumber=%d\n"
1833 			  "dot1xPaePortProtocolVersion=%d\n"
1834 			  "dot1xPaePortCapabilities=1\n"
1835 			  "dot1xPaePortInitialize=%d\n"
1836 			  "dot1xPaePortReauthenticate=FALSE\n",
1837 			  sta->aid,
1838 			  EAPOL_VERSION,
1839 			  sm->initialize);
1840 	if (ret < 0 || (size_t) ret >= buflen - len)
1841 		return len;
1842 	len += ret;
1843 
1844 	/* dot1xAuthConfigTable */
1845 	ret = os_snprintf(buf + len, buflen - len,
1846 			  "dot1xAuthPaeState=%d\n"
1847 			  "dot1xAuthBackendAuthState=%d\n"
1848 			  "dot1xAuthAdminControlledDirections=%d\n"
1849 			  "dot1xAuthOperControlledDirections=%d\n"
1850 			  "dot1xAuthAuthControlledPortStatus=%d\n"
1851 			  "dot1xAuthAuthControlledPortControl=%d\n"
1852 			  "dot1xAuthQuietPeriod=%u\n"
1853 			  "dot1xAuthServerTimeout=%u\n"
1854 			  "dot1xAuthReAuthPeriod=%u\n"
1855 			  "dot1xAuthReAuthEnabled=%s\n"
1856 			  "dot1xAuthKeyTxEnabled=%s\n",
1857 			  sm->auth_pae_state + 1,
1858 			  sm->be_auth_state + 1,
1859 			  sm->adminControlledDirections,
1860 			  sm->operControlledDirections,
1861 			  sm->authPortStatus,
1862 			  sm->portControl,
1863 			  sm->quietPeriod,
1864 			  sm->serverTimeout,
1865 			  sm->reAuthPeriod,
1866 			  bool_txt(sm->reAuthEnabled),
1867 			  bool_txt(sm->keyTxEnabled));
1868 	if (ret < 0 || (size_t) ret >= buflen - len)
1869 		return len;
1870 	len += ret;
1871 
1872 	/* dot1xAuthStatsTable */
1873 	ret = os_snprintf(buf + len, buflen - len,
1874 			  "dot1xAuthEapolFramesRx=%u\n"
1875 			  "dot1xAuthEapolFramesTx=%u\n"
1876 			  "dot1xAuthEapolStartFramesRx=%u\n"
1877 			  "dot1xAuthEapolLogoffFramesRx=%u\n"
1878 			  "dot1xAuthEapolRespIdFramesRx=%u\n"
1879 			  "dot1xAuthEapolRespFramesRx=%u\n"
1880 			  "dot1xAuthEapolReqIdFramesTx=%u\n"
1881 			  "dot1xAuthEapolReqFramesTx=%u\n"
1882 			  "dot1xAuthInvalidEapolFramesRx=%u\n"
1883 			  "dot1xAuthEapLengthErrorFramesRx=%u\n"
1884 			  "dot1xAuthLastEapolFrameVersion=%u\n"
1885 			  "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1886 			  sm->dot1xAuthEapolFramesRx,
1887 			  sm->dot1xAuthEapolFramesTx,
1888 			  sm->dot1xAuthEapolStartFramesRx,
1889 			  sm->dot1xAuthEapolLogoffFramesRx,
1890 			  sm->dot1xAuthEapolRespIdFramesRx,
1891 			  sm->dot1xAuthEapolRespFramesRx,
1892 			  sm->dot1xAuthEapolReqIdFramesTx,
1893 			  sm->dot1xAuthEapolReqFramesTx,
1894 			  sm->dot1xAuthInvalidEapolFramesRx,
1895 			  sm->dot1xAuthEapLengthErrorFramesRx,
1896 			  sm->dot1xAuthLastEapolFrameVersion,
1897 			  MAC2STR(sm->addr));
1898 	if (ret < 0 || (size_t) ret >= buflen - len)
1899 		return len;
1900 	len += ret;
1901 
1902 	/* dot1xAuthDiagTable */
1903 	ret = os_snprintf(buf + len, buflen - len,
1904 			  "dot1xAuthEntersConnecting=%u\n"
1905 			  "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1906 			  "dot1xAuthEntersAuthenticating=%u\n"
1907 			  "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1908 			  "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1909 			  "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1910 			  "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1911 			  "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1912 			  "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1913 			  "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1914 			  "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1915 			  "dot1xAuthBackendResponses=%u\n"
1916 			  "dot1xAuthBackendAccessChallenges=%u\n"
1917 			  "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1918 			  "dot1xAuthBackendAuthSuccesses=%u\n"
1919 			  "dot1xAuthBackendAuthFails=%u\n",
1920 			  sm->authEntersConnecting,
1921 			  sm->authEapLogoffsWhileConnecting,
1922 			  sm->authEntersAuthenticating,
1923 			  sm->authAuthSuccessesWhileAuthenticating,
1924 			  sm->authAuthTimeoutsWhileAuthenticating,
1925 			  sm->authAuthFailWhileAuthenticating,
1926 			  sm->authAuthEapStartsWhileAuthenticating,
1927 			  sm->authAuthEapLogoffWhileAuthenticating,
1928 			  sm->authAuthReauthsWhileAuthenticated,
1929 			  sm->authAuthEapStartsWhileAuthenticated,
1930 			  sm->authAuthEapLogoffWhileAuthenticated,
1931 			  sm->backendResponses,
1932 			  sm->backendAccessChallenges,
1933 			  sm->backendOtherRequestsToSupplicant,
1934 			  sm->backendAuthSuccesses,
1935 			  sm->backendAuthFails);
1936 	if (ret < 0 || (size_t) ret >= buflen - len)
1937 		return len;
1938 	len += ret;
1939 
1940 	/* dot1xAuthSessionStatsTable */
1941 	ret = os_snprintf(buf + len, buflen - len,
1942 			  /* TODO: dot1xAuthSessionOctetsRx */
1943 			  /* TODO: dot1xAuthSessionOctetsTx */
1944 			  /* TODO: dot1xAuthSessionFramesRx */
1945 			  /* TODO: dot1xAuthSessionFramesTx */
1946 			  "dot1xAuthSessionId=%08X-%08X\n"
1947 			  "dot1xAuthSessionAuthenticMethod=%d\n"
1948 			  "dot1xAuthSessionTime=%u\n"
1949 			  "dot1xAuthSessionTerminateCause=999\n"
1950 			  "dot1xAuthSessionUserName=%s\n",
1951 			  sta->acct_session_id_hi, sta->acct_session_id_lo,
1952 			  (wpa_key_mgmt_wpa_ieee8021x(
1953 				   wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1954 			  1 : 2,
1955 			  (unsigned int) (time(NULL) -
1956 					  sta->acct_session_start),
1957 			  sm->identity);
1958 	if (ret < 0 || (size_t) ret >= buflen - len)
1959 		return len;
1960 	len += ret;
1961 
1962 	return len;
1963 }
1964 
1965 
1966 static void ieee802_1x_finished(struct hostapd_data *hapd,
1967 				struct sta_info *sta, int success)
1968 {
1969 	const u8 *key;
1970 	size_t len;
1971 	/* TODO: get PMKLifetime from WPA parameters */
1972 	static const int dot11RSNAConfigPMKLifetime = 43200;
1973 
1974 	key = ieee802_1x_get_key(sta->eapol_sm, &len);
1975 	if (success && key && len >= PMK_LEN &&
1976 	    wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
1977 			       sta->eapol_sm) == 0) {
1978 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1979 			       HOSTAPD_LEVEL_DEBUG,
1980 			       "Added PMKSA cache entry (IEEE 802.1X)");
1981 	}
1982 }
1983