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