• 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 &&
977 	    (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
978 	     key_mgmt == WPA_KEY_MGMT_DPP)) {
979 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
980 			   "STA is using PSK");
981 		return;
982 	}
983 
984 	if (!sta->eapol_sm) {
985 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
986 		if (!sta->eapol_sm)
987 			return;
988 
989 #ifdef CONFIG_WPS
990 		if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
991 			u32 wflags = sta->flags & (WLAN_STA_WPS |
992 						   WLAN_STA_WPS2 |
993 						   WLAN_STA_MAYBE_WPS);
994 			if (wflags == WLAN_STA_MAYBE_WPS ||
995 			    wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
996 				/*
997 				 * Delay EAPOL frame transmission until a
998 				 * possible WPS STA initiates the handshake
999 				 * with EAPOL-Start. Only allow the wait to be
1000 				 * skipped if the STA is known to support WPS
1001 				 * 2.0.
1002 				 */
1003 				wpa_printf(MSG_DEBUG, "WPS: Do not start "
1004 					   "EAPOL until EAPOL-Start is "
1005 					   "received");
1006 				sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1007 			}
1008 		}
1009 #endif /* CONFIG_WPS */
1010 
1011 		sta->eapol_sm->eap_if->portEnabled = TRUE;
1012 	}
1013 
1014 	/* since we support version 1, we can ignore version field and proceed
1015 	 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1016 	/* TODO: actually, we are not version 1 anymore.. However, Version 2
1017 	 * does not change frame contents, so should be ok to process frames
1018 	 * more or less identically. Some changes might be needed for
1019 	 * verification of fields. */
1020 
1021 	switch (hdr->type) {
1022 	case IEEE802_1X_TYPE_EAP_PACKET:
1023 		handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1024 		break;
1025 
1026 	case IEEE802_1X_TYPE_EAPOL_START:
1027 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1028 			       HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
1029 			       "from STA");
1030 		sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1031 		pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1032 		if (pmksa) {
1033 			hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1034 				       HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
1035 				       "available - ignore it since "
1036 				       "STA sent EAPOL-Start");
1037 			wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1038 		}
1039 		sta->eapol_sm->eapolStart = TRUE;
1040 		sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1041 		eap_server_clear_identity(sta->eapol_sm->eap);
1042 		wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1043 		break;
1044 
1045 	case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1046 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1047 			       HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
1048 			       "from STA");
1049 		sta->acct_terminate_cause =
1050 			RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1051 		accounting_sta_stop(hapd, sta);
1052 		sta->eapol_sm->eapolLogoff = TRUE;
1053 		sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1054 		eap_server_clear_identity(sta->eapol_sm->eap);
1055 		break;
1056 
1057 	case IEEE802_1X_TYPE_EAPOL_KEY:
1058 		wpa_printf(MSG_DEBUG, "   EAPOL-Key");
1059 		if (!ap_sta_is_authorized(sta)) {
1060 			wpa_printf(MSG_DEBUG, "   Dropped key data from "
1061 				   "unauthorized Supplicant");
1062 			break;
1063 		}
1064 		break;
1065 
1066 	case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1067 		wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
1068 		/* TODO: implement support for this; show data */
1069 		break;
1070 
1071 	default:
1072 		wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
1073 		sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1074 		break;
1075 	}
1076 
1077 	eapol_auth_step(sta->eapol_sm);
1078 }
1079 
1080 
1081 /**
1082  * ieee802_1x_new_station - Start IEEE 802.1X authentication
1083  * @hapd: hostapd BSS data
1084  * @sta: The station
1085  *
1086  * This function is called to start IEEE 802.1X authentication when a new
1087  * station completes IEEE 802.11 association.
1088  */
ieee802_1x_new_station(struct hostapd_data * hapd,struct sta_info * sta)1089 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1090 {
1091 	struct rsn_pmksa_cache_entry *pmksa;
1092 	int reassoc = 1;
1093 	int force_1x = 0;
1094 	int key_mgmt;
1095 
1096 #ifdef CONFIG_WPS
1097 	if (hapd->conf->wps_state &&
1098 	    ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1099 	     (sta->flags & WLAN_STA_WPS))) {
1100 		/*
1101 		 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1102 		 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1103 		 * authentication in this BSS.
1104 		 */
1105 		force_1x = 1;
1106 	}
1107 #endif /* CONFIG_WPS */
1108 
1109 	if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
1110 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1111 			   "802.1X not enabled or forced for WPS");
1112 		/*
1113 		 * Clear any possible EAPOL authenticator state to support
1114 		 * reassociation change from WPS to PSK.
1115 		 */
1116 		ieee802_1x_free_station(hapd, sta);
1117 		return;
1118 	}
1119 
1120 	key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1121 	if (key_mgmt != -1 &&
1122 	    (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1123 	     key_mgmt == WPA_KEY_MGMT_DPP)) {
1124 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1125 		/*
1126 		 * Clear any possible EAPOL authenticator state to support
1127 		 * reassociation change from WPA-EAP to PSK.
1128 		 */
1129 		ieee802_1x_free_station(hapd, sta);
1130 		return;
1131 	}
1132 
1133 	if (sta->eapol_sm == NULL) {
1134 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1135 			       HOSTAPD_LEVEL_DEBUG, "start authentication");
1136 		sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1137 		if (sta->eapol_sm == NULL) {
1138 			hostapd_logger(hapd, sta->addr,
1139 				       HOSTAPD_MODULE_IEEE8021X,
1140 				       HOSTAPD_LEVEL_INFO,
1141 				       "failed to allocate state machine");
1142 			return;
1143 		}
1144 		reassoc = 0;
1145 	}
1146 
1147 #ifdef CONFIG_WPS
1148 	sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1149 	if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1150 	    !(sta->flags & WLAN_STA_WPS2)) {
1151 		/*
1152 		 * Delay EAPOL frame transmission until a possible WPS STA
1153 		 * initiates the handshake with EAPOL-Start. Only allow the
1154 		 * wait to be skipped if the STA is known to support WPS 2.0.
1155 		 */
1156 		wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1157 			   "EAPOL-Start is received");
1158 		sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1159 	}
1160 #endif /* CONFIG_WPS */
1161 
1162 	sta->eapol_sm->eap_if->portEnabled = TRUE;
1163 
1164 #ifdef CONFIG_IEEE80211R_AP
1165 	if (sta->auth_alg == WLAN_AUTH_FT) {
1166 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1167 			       HOSTAPD_LEVEL_DEBUG,
1168 			       "PMK from FT - skip IEEE 802.1X/EAP");
1169 		/* Setup EAPOL state machines to already authenticated state
1170 		 * because of existing FT information from R0KH. */
1171 		sta->eapol_sm->keyRun = TRUE;
1172 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1173 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1174 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1175 		sta->eapol_sm->authSuccess = TRUE;
1176 		sta->eapol_sm->authFail = FALSE;
1177 		sta->eapol_sm->portValid = TRUE;
1178 		if (sta->eapol_sm->eap)
1179 			eap_sm_notify_cached(sta->eapol_sm->eap);
1180 		/* TODO: get vlan_id from R0KH using RRB message */
1181 		return;
1182 	}
1183 #endif /* CONFIG_IEEE80211R_AP */
1184 
1185 #ifdef CONFIG_FILS
1186 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1187 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1188 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
1189 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1190 			       HOSTAPD_LEVEL_DEBUG,
1191 			       "PMK from FILS - skip IEEE 802.1X/EAP");
1192 		/* Setup EAPOL state machines to already authenticated state
1193 		 * because of existing FILS information. */
1194 		sta->eapol_sm->keyRun = TRUE;
1195 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1196 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1197 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1198 		sta->eapol_sm->authSuccess = TRUE;
1199 		sta->eapol_sm->authFail = FALSE;
1200 		sta->eapol_sm->portValid = TRUE;
1201 		if (sta->eapol_sm->eap)
1202 			eap_sm_notify_cached(sta->eapol_sm->eap);
1203 		return;
1204 	}
1205 #endif /* CONFIG_FILS */
1206 
1207 	pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1208 	if (pmksa) {
1209 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1210 			       HOSTAPD_LEVEL_DEBUG,
1211 			       "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1212 		/* Setup EAPOL state machines to already authenticated state
1213 		 * because of existing PMKSA information in the cache. */
1214 		sta->eapol_sm->keyRun = TRUE;
1215 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1216 		sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1217 		sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1218 		sta->eapol_sm->authSuccess = TRUE;
1219 		sta->eapol_sm->authFail = FALSE;
1220 		if (sta->eapol_sm->eap)
1221 			eap_sm_notify_cached(sta->eapol_sm->eap);
1222 		pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
1223 		ap_sta_bind_vlan(hapd, sta);
1224 	} else {
1225 		if (reassoc) {
1226 			/*
1227 			 * Force EAPOL state machines to start
1228 			 * re-authentication without having to wait for the
1229 			 * Supplicant to send EAPOL-Start.
1230 			 */
1231 			sta->eapol_sm->reAuthenticate = TRUE;
1232 		}
1233 		eapol_auth_step(sta->eapol_sm);
1234 	}
1235 }
1236 
1237 
ieee802_1x_free_station(struct hostapd_data * hapd,struct sta_info * sta)1238 void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
1239 {
1240 	struct eapol_state_machine *sm = sta->eapol_sm;
1241 
1242 #ifdef CONFIG_HS20
1243 	eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1244 #endif /* CONFIG_HS20 */
1245 
1246 	if (sta->pending_eapol_rx) {
1247 		wpabuf_free(sta->pending_eapol_rx->buf);
1248 		os_free(sta->pending_eapol_rx);
1249 		sta->pending_eapol_rx = NULL;
1250 	}
1251 
1252 	if (sm == NULL)
1253 		return;
1254 
1255 	sta->eapol_sm = NULL;
1256 
1257 #ifndef CONFIG_NO_RADIUS
1258 	radius_msg_free(sm->last_recv_radius);
1259 	radius_free_class(&sm->radius_class);
1260 #endif /* CONFIG_NO_RADIUS */
1261 
1262 	eapol_auth_free(sm);
1263 }
1264 
1265 
1266 #ifndef CONFIG_NO_RADIUS
ieee802_1x_decapsulate_radius(struct hostapd_data * hapd,struct sta_info * sta)1267 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1268 					  struct sta_info *sta)
1269 {
1270 	struct wpabuf *eap;
1271 	const struct eap_hdr *hdr;
1272 	int eap_type = -1;
1273 	char buf[64];
1274 	struct radius_msg *msg;
1275 	struct eapol_state_machine *sm = sta->eapol_sm;
1276 
1277 	if (sm == NULL || sm->last_recv_radius == NULL) {
1278 		if (sm)
1279 			sm->eap_if->aaaEapNoReq = TRUE;
1280 		return;
1281 	}
1282 
1283 	msg = sm->last_recv_radius;
1284 
1285 	eap = radius_msg_get_eap(msg);
1286 	if (eap == NULL) {
1287 		/* RFC 3579, Chap. 2.6.3:
1288 		 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1289 		 * attribute */
1290 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1291 			       HOSTAPD_LEVEL_WARNING, "could not extract "
1292 			       "EAP-Message from RADIUS message");
1293 		sm->eap_if->aaaEapNoReq = TRUE;
1294 		return;
1295 	}
1296 
1297 	if (wpabuf_len(eap) < sizeof(*hdr)) {
1298 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1299 			       HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1300 			       "received from authentication server");
1301 		wpabuf_free(eap);
1302 		sm->eap_if->aaaEapNoReq = TRUE;
1303 		return;
1304 	}
1305 
1306 	if (wpabuf_len(eap) > sizeof(*hdr))
1307 		eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1308 
1309 	hdr = wpabuf_head(eap);
1310 	switch (hdr->code) {
1311 	case EAP_CODE_REQUEST:
1312 		if (eap_type >= 0)
1313 			sm->eap_type_authsrv = eap_type;
1314 		os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1315 			    eap_server_get_name(0, eap_type), eap_type);
1316 		break;
1317 	case EAP_CODE_RESPONSE:
1318 		os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1319 			    eap_server_get_name(0, eap_type), eap_type);
1320 		break;
1321 	case EAP_CODE_SUCCESS:
1322 		os_strlcpy(buf, "EAP Success", sizeof(buf));
1323 		break;
1324 	case EAP_CODE_FAILURE:
1325 		os_strlcpy(buf, "EAP Failure", sizeof(buf));
1326 		break;
1327 	default:
1328 		os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1329 		break;
1330 	}
1331 	buf[sizeof(buf) - 1] = '\0';
1332 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1333 		       HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1334 		       "id=%d len=%d) from RADIUS server: %s",
1335 		       hdr->code, hdr->identifier, be_to_host16(hdr->length),
1336 		       buf);
1337 	sm->eap_if->aaaEapReq = TRUE;
1338 
1339 	wpabuf_free(sm->eap_if->aaaEapReqData);
1340 	sm->eap_if->aaaEapReqData = eap;
1341 }
1342 
1343 
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)1344 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1345 				struct sta_info *sta, struct radius_msg *msg,
1346 				struct radius_msg *req,
1347 				const u8 *shared_secret,
1348 				size_t shared_secret_len)
1349 {
1350 	struct radius_ms_mppe_keys *keys;
1351 	struct eapol_state_machine *sm = sta->eapol_sm;
1352 	if (sm == NULL)
1353 		return;
1354 
1355 	keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1356 				      shared_secret_len);
1357 
1358 	if (keys && keys->send && keys->recv) {
1359 		size_t len = keys->send_len + keys->recv_len;
1360 		wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1361 				keys->send, keys->send_len);
1362 		wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1363 				keys->recv, keys->recv_len);
1364 
1365 		os_free(sm->eap_if->aaaEapKeyData);
1366 		sm->eap_if->aaaEapKeyData = os_malloc(len);
1367 		if (sm->eap_if->aaaEapKeyData) {
1368 			os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1369 				  keys->recv_len);
1370 			os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1371 				  keys->send, keys->send_len);
1372 			sm->eap_if->aaaEapKeyDataLen = len;
1373 			sm->eap_if->aaaEapKeyAvailable = TRUE;
1374 		}
1375 	} else {
1376 		wpa_printf(MSG_DEBUG,
1377 			   "MS-MPPE: 1x_get_keys, could not get keys: %p  send: %p  recv: %p",
1378 			   keys, keys ? keys->send : NULL,
1379 			   keys ? keys->recv : NULL);
1380 	}
1381 
1382 	if (keys) {
1383 		os_free(keys->send);
1384 		os_free(keys->recv);
1385 		os_free(keys);
1386 	}
1387 }
1388 
1389 
ieee802_1x_store_radius_class(struct hostapd_data * hapd,struct sta_info * sta,struct radius_msg * msg)1390 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1391 					  struct sta_info *sta,
1392 					  struct radius_msg *msg)
1393 {
1394 	u8 *attr_class;
1395 	size_t class_len;
1396 	struct eapol_state_machine *sm = sta->eapol_sm;
1397 	int count, i;
1398 	struct radius_attr_data *nclass;
1399 	size_t nclass_count;
1400 
1401 	if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1402 	    sm == NULL)
1403 		return;
1404 
1405 	radius_free_class(&sm->radius_class);
1406 	count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1407 	if (count <= 0)
1408 		return;
1409 
1410 	nclass = os_calloc(count, sizeof(struct radius_attr_data));
1411 	if (nclass == NULL)
1412 		return;
1413 
1414 	nclass_count = 0;
1415 
1416 	attr_class = NULL;
1417 	for (i = 0; i < count; i++) {
1418 		do {
1419 			if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1420 						    &attr_class, &class_len,
1421 						    attr_class) < 0) {
1422 				i = count;
1423 				break;
1424 			}
1425 		} while (class_len < 1);
1426 
1427 		nclass[nclass_count].data = os_memdup(attr_class, class_len);
1428 		if (nclass[nclass_count].data == NULL)
1429 			break;
1430 
1431 		nclass[nclass_count].len = class_len;
1432 		nclass_count++;
1433 	}
1434 
1435 	sm->radius_class.attr = nclass;
1436 	sm->radius_class.count = nclass_count;
1437 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1438 		   "attributes for " MACSTR,
1439 		   (unsigned long) sm->radius_class.count,
1440 		   MAC2STR(sta->addr));
1441 }
1442 
1443 
1444 /* 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)1445 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1446 					   struct sta_info *sta,
1447 					   struct radius_msg *msg)
1448 {
1449 	u8 *buf, *identity;
1450 	size_t len;
1451 	struct eapol_state_machine *sm = sta->eapol_sm;
1452 
1453 	if (sm == NULL)
1454 		return;
1455 
1456 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1457 				    NULL) < 0)
1458 		return;
1459 
1460 	identity = (u8 *) dup_binstr(buf, len);
1461 	if (identity == NULL)
1462 		return;
1463 
1464 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1465 		       HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1466 		       "User-Name from Access-Accept '%s'",
1467 		       sm->identity ? (char *) sm->identity : "N/A",
1468 		       (char *) identity);
1469 
1470 	os_free(sm->identity);
1471 	sm->identity = identity;
1472 	sm->identity_len = len;
1473 }
1474 
1475 
1476 /* 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)1477 static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1478 				      struct sta_info *sta,
1479 				      struct radius_msg *msg)
1480 {
1481 	struct eapol_state_machine *sm = sta->eapol_sm;
1482 	struct wpabuf *cui;
1483 	u8 *buf;
1484 	size_t len;
1485 
1486 	if (sm == NULL)
1487 		return;
1488 
1489 	if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1490 				    &buf, &len, NULL) < 0)
1491 		return;
1492 
1493 	cui = wpabuf_alloc_copy(buf, len);
1494 	if (cui == NULL)
1495 		return;
1496 
1497 	wpabuf_free(sm->radius_cui);
1498 	sm->radius_cui = cui;
1499 }
1500 
1501 
1502 #ifdef CONFIG_HS20
1503 
ieee802_1x_hs20_sub_rem(struct sta_info * sta,u8 * pos,size_t len)1504 static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1505 {
1506 	sta->remediation = 1;
1507 	os_free(sta->remediation_url);
1508 	if (len > 2) {
1509 		sta->remediation_url = os_malloc(len);
1510 		if (!sta->remediation_url)
1511 			return;
1512 		sta->remediation_method = pos[0];
1513 		os_memcpy(sta->remediation_url, pos + 1, len - 1);
1514 		sta->remediation_url[len - 1] = '\0';
1515 		wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1516 			   "for " MACSTR " - server method %u URL %s",
1517 			   MAC2STR(sta->addr), sta->remediation_method,
1518 			   sta->remediation_url);
1519 	} else {
1520 		sta->remediation_url = NULL;
1521 		wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1522 			   "for " MACSTR, MAC2STR(sta->addr));
1523 	}
1524 	/* TODO: assign the STA into remediation VLAN or add filtering */
1525 }
1526 
1527 
ieee802_1x_hs20_deauth_req(struct hostapd_data * hapd,struct sta_info * sta,u8 * pos,size_t len)1528 static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1529 				       struct sta_info *sta, u8 *pos,
1530 				       size_t len)
1531 {
1532 	if (len < 3)
1533 		return; /* Malformed information */
1534 	sta->hs20_deauth_requested = 1;
1535 	wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u  "
1536 		   "Re-auth Delay %u",
1537 		   *pos, WPA_GET_LE16(pos + 1));
1538 	wpabuf_free(sta->hs20_deauth_req);
1539 	sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1540 	if (sta->hs20_deauth_req) {
1541 		wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1542 		wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1543 		wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1544 	}
1545 	ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1546 }
1547 
1548 
ieee802_1x_hs20_session_info(struct hostapd_data * hapd,struct sta_info * sta,u8 * pos,size_t len,int session_timeout)1549 static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1550 					 struct sta_info *sta, u8 *pos,
1551 					 size_t len, int session_timeout)
1552 {
1553 	unsigned int swt;
1554 	int warning_time, beacon_int;
1555 
1556 	if (len < 1)
1557 		return; /* Malformed information */
1558 	os_free(sta->hs20_session_info_url);
1559 	sta->hs20_session_info_url = os_malloc(len);
1560 	if (sta->hs20_session_info_url == NULL)
1561 		return;
1562 	swt = pos[0];
1563 	os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1564 	sta->hs20_session_info_url[len - 1] = '\0';
1565 	wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1566 		   "(session_timeout=%d)",
1567 		   sta->hs20_session_info_url, swt, session_timeout);
1568 	if (session_timeout < 0) {
1569 		wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1570 		return;
1571 	}
1572 	if (swt == 255)
1573 		swt = 1; /* Use one minute as the AP selected value */
1574 
1575 	if ((unsigned int) session_timeout < swt * 60)
1576 		warning_time = 0;
1577 	else
1578 		warning_time = session_timeout - swt * 60;
1579 
1580 	beacon_int = hapd->iconf->beacon_int;
1581 	if (beacon_int < 1)
1582 		beacon_int = 100; /* best guess */
1583 	sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1584 	if (sta->hs20_disassoc_timer > 65535)
1585 		sta->hs20_disassoc_timer = 65535;
1586 
1587 	ap_sta_session_warning_timeout(hapd, sta, warning_time);
1588 }
1589 
1590 #endif /* CONFIG_HS20 */
1591 
1592 
ieee802_1x_check_hs20(struct hostapd_data * hapd,struct sta_info * sta,struct radius_msg * msg,int session_timeout)1593 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1594 				  struct sta_info *sta,
1595 				  struct radius_msg *msg,
1596 				  int session_timeout)
1597 {
1598 #ifdef CONFIG_HS20
1599 	u8 *buf, *pos, *end, type, sublen;
1600 	size_t len;
1601 
1602 	buf = NULL;
1603 	sta->remediation = 0;
1604 	sta->hs20_deauth_requested = 0;
1605 
1606 	for (;;) {
1607 		if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1608 					    &buf, &len, buf) < 0)
1609 			break;
1610 		if (len < 6)
1611 			continue;
1612 		pos = buf;
1613 		end = buf + len;
1614 		if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1615 			continue;
1616 		pos += 4;
1617 
1618 		type = *pos++;
1619 		sublen = *pos++;
1620 		if (sublen < 2)
1621 			continue; /* invalid length */
1622 		sublen -= 2; /* skip header */
1623 		if (pos + sublen > end)
1624 			continue; /* invalid WFA VSA */
1625 
1626 		switch (type) {
1627 		case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1628 			ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1629 			break;
1630 		case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1631 			ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1632 			break;
1633 		case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1634 			ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1635 						     session_timeout);
1636 			break;
1637 		}
1638 	}
1639 #endif /* CONFIG_HS20 */
1640 }
1641 
1642 
1643 struct sta_id_search {
1644 	u8 identifier;
1645 	struct eapol_state_machine *sm;
1646 };
1647 
1648 
ieee802_1x_select_radius_identifier(struct hostapd_data * hapd,struct sta_info * sta,void * ctx)1649 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1650 					       struct sta_info *sta,
1651 					       void *ctx)
1652 {
1653 	struct sta_id_search *id_search = ctx;
1654 	struct eapol_state_machine *sm = sta->eapol_sm;
1655 
1656 	if (sm && sm->radius_identifier >= 0 &&
1657 	    sm->radius_identifier == id_search->identifier) {
1658 		id_search->sm = sm;
1659 		return 1;
1660 	}
1661 	return 0;
1662 }
1663 
1664 
1665 static struct eapol_state_machine *
ieee802_1x_search_radius_identifier(struct hostapd_data * hapd,u8 identifier)1666 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1667 {
1668 	struct sta_id_search id_search;
1669 	id_search.identifier = identifier;
1670 	id_search.sm = NULL;
1671 	ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1672 	return id_search.sm;
1673 }
1674 
1675 
1676 /**
1677  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1678  * @msg: RADIUS response message
1679  * @req: RADIUS request message
1680  * @shared_secret: RADIUS shared secret
1681  * @shared_secret_len: Length of shared_secret in octets
1682  * @data: Context data (struct hostapd_data *)
1683  * Returns: Processing status
1684  */
1685 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)1686 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1687 			const u8 *shared_secret, size_t shared_secret_len,
1688 			void *data)
1689 {
1690 	struct hostapd_data *hapd = data;
1691 	struct sta_info *sta;
1692 	u32 session_timeout = 0, termination_action, acct_interim_interval;
1693 	int session_timeout_set;
1694 	struct eapol_state_machine *sm;
1695 	int override_eapReq = 0;
1696 	struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1697 	struct vlan_description vlan_desc;
1698 #ifndef CONFIG_NO_VLAN
1699 	int *untagged, *tagged, *notempty;
1700 #endif /* CONFIG_NO_VLAN */
1701 
1702 	os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1703 
1704 	sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1705 	if (sm == NULL) {
1706 		wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1707 			   "station for this RADIUS message");
1708 		return RADIUS_RX_UNKNOWN;
1709 	}
1710 	sta = sm->sta;
1711 
1712 	/* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1713 	 * present when packet contains an EAP-Message attribute */
1714 	if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1715 	    radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1716 				0) < 0 &&
1717 	    radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1718 		wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1719 			   "Message-Authenticator since it does not include "
1720 			   "EAP-Message");
1721 	} else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1722 				     req, 1)) {
1723 		wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
1724 		return RADIUS_RX_INVALID_AUTHENTICATOR;
1725 	}
1726 
1727 	if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1728 	    hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1729 	    hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1730 		wpa_printf(MSG_INFO, "Unknown RADIUS message code");
1731 		return RADIUS_RX_UNKNOWN;
1732 	}
1733 
1734 	sm->radius_identifier = -1;
1735 	wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1736 		   MAC2STR(sta->addr));
1737 
1738 	radius_msg_free(sm->last_recv_radius);
1739 	sm->last_recv_radius = msg;
1740 
1741 	session_timeout_set =
1742 		!radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1743 					   &session_timeout);
1744 	if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1745 				      &termination_action))
1746 		termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1747 
1748 	if (hapd->conf->acct_interim_interval == 0 &&
1749 	    hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1750 	    radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1751 				      &acct_interim_interval) == 0) {
1752 		if (acct_interim_interval < 60) {
1753 			hostapd_logger(hapd, sta->addr,
1754 				       HOSTAPD_MODULE_IEEE8021X,
1755 				       HOSTAPD_LEVEL_INFO,
1756 				       "ignored too small "
1757 				       "Acct-Interim-Interval %d",
1758 				       acct_interim_interval);
1759 		} else
1760 			sta->acct_interim_interval = acct_interim_interval;
1761 	}
1762 
1763 
1764 	switch (hdr->code) {
1765 	case RADIUS_CODE_ACCESS_ACCEPT:
1766 #ifndef CONFIG_NO_VLAN
1767 		if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED) {
1768 			notempty = &vlan_desc.notempty;
1769 			untagged = &vlan_desc.untagged;
1770 			tagged = vlan_desc.tagged;
1771 			*notempty = !!radius_msg_get_vlanid(msg, untagged,
1772 							    MAX_NUM_TAGGED_VLAN,
1773 							    tagged);
1774 		}
1775 
1776 		if (vlan_desc.notempty &&
1777 		    !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1778 			sta->eapol_sm->authFail = TRUE;
1779 			hostapd_logger(hapd, sta->addr,
1780 				       HOSTAPD_MODULE_RADIUS,
1781 				       HOSTAPD_LEVEL_INFO,
1782 				       "Invalid VLAN %d%s received from RADIUS server",
1783 				       vlan_desc.untagged,
1784 				       vlan_desc.tagged[0] ? "+" : "");
1785 			os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1786 			ap_sta_set_vlan(hapd, sta, &vlan_desc);
1787 			break;
1788 		}
1789 
1790 		if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1791 		    !vlan_desc.notempty) {
1792 			sta->eapol_sm->authFail = TRUE;
1793 			hostapd_logger(hapd, sta->addr,
1794 				       HOSTAPD_MODULE_IEEE8021X,
1795 				       HOSTAPD_LEVEL_INFO, "authentication "
1796 				       "server did not include required VLAN "
1797 				       "ID in Access-Accept");
1798 			break;
1799 		}
1800 #endif /* CONFIG_NO_VLAN */
1801 
1802 		if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0)
1803 			break;
1804 
1805 #ifndef CONFIG_NO_VLAN
1806 		if (sta->vlan_id > 0) {
1807 			hostapd_logger(hapd, sta->addr,
1808 				       HOSTAPD_MODULE_RADIUS,
1809 				       HOSTAPD_LEVEL_INFO,
1810 				       "VLAN ID %d", sta->vlan_id);
1811 		}
1812 #endif /* CONFIG_NO_VLAN */
1813 
1814 		if ((sta->flags & WLAN_STA_ASSOC) &&
1815 		    ap_sta_bind_vlan(hapd, sta) < 0)
1816 			break;
1817 
1818 		sta->session_timeout_set = !!session_timeout_set;
1819 		sta->session_timeout = session_timeout;
1820 
1821 		/* RFC 3580, Ch. 3.17 */
1822 		if (session_timeout_set && termination_action ==
1823 		    RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1824 			sm->reAuthPeriod = session_timeout;
1825 		} else if (session_timeout_set)
1826 			ap_sta_session_timeout(hapd, sta, session_timeout);
1827 
1828 		sm->eap_if->aaaSuccess = TRUE;
1829 		override_eapReq = 1;
1830 		ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1831 				    shared_secret_len);
1832 		ieee802_1x_store_radius_class(hapd, sta, msg);
1833 		ieee802_1x_update_sta_identity(hapd, sta, msg);
1834 		ieee802_1x_update_sta_cui(hapd, sta, msg);
1835 		ieee802_1x_check_hs20(hapd, sta, msg,
1836 				      session_timeout_set ?
1837 				      (int) session_timeout : -1);
1838 		break;
1839 	case RADIUS_CODE_ACCESS_REJECT:
1840 		sm->eap_if->aaaFail = TRUE;
1841 		override_eapReq = 1;
1842 		break;
1843 	case RADIUS_CODE_ACCESS_CHALLENGE:
1844 		sm->eap_if->aaaEapReq = TRUE;
1845 		if (session_timeout_set) {
1846 			/* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1847 			sm->eap_if->aaaMethodTimeout = session_timeout;
1848 			hostapd_logger(hapd, sm->addr,
1849 				       HOSTAPD_MODULE_IEEE8021X,
1850 				       HOSTAPD_LEVEL_DEBUG,
1851 				       "using EAP timeout of %d seconds (from "
1852 				       "RADIUS)",
1853 				       sm->eap_if->aaaMethodTimeout);
1854 		} else {
1855 			/*
1856 			 * Use dynamic retransmission behavior per EAP
1857 			 * specification.
1858 			 */
1859 			sm->eap_if->aaaMethodTimeout = 0;
1860 		}
1861 		break;
1862 	}
1863 
1864 	ieee802_1x_decapsulate_radius(hapd, sta);
1865 	if (override_eapReq)
1866 		sm->eap_if->aaaEapReq = FALSE;
1867 
1868 #ifdef CONFIG_FILS
1869 #ifdef NEED_AP_MLME
1870 	if (sta->flags & WLAN_STA_PENDING_FILS_ERP) {
1871 		/* TODO: Add a PMKSA entry on success? */
1872 		ieee802_11_finish_fils_auth(
1873 			hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
1874 			sm->eap_if->aaaEapReqData,
1875 			sm->eap_if->aaaEapKeyData,
1876 			sm->eap_if->aaaEapKeyDataLen);
1877 	}
1878 #endif /* NEED_AP_MLME */
1879 #endif /* CONFIG_FILS */
1880 
1881 	eapol_auth_step(sm);
1882 
1883 	return RADIUS_RX_QUEUED;
1884 }
1885 #endif /* CONFIG_NO_RADIUS */
1886 
1887 
ieee802_1x_abort_auth(struct hostapd_data * hapd,struct sta_info * sta)1888 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1889 {
1890 	struct eapol_state_machine *sm = sta->eapol_sm;
1891 	if (sm == NULL)
1892 		return;
1893 
1894 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1895 		       HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1896 
1897 #ifndef CONFIG_NO_RADIUS
1898 	radius_msg_free(sm->last_recv_radius);
1899 	sm->last_recv_radius = NULL;
1900 #endif /* CONFIG_NO_RADIUS */
1901 
1902 	if (sm->eap_if->eapTimeout) {
1903 		/*
1904 		 * Disconnect the STA since it did not reply to the last EAP
1905 		 * request and we cannot continue EAP processing (EAP-Failure
1906 		 * could only be sent if the EAP peer actually replied).
1907 		 */
1908 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1909 			MAC2STR(sta->addr));
1910 
1911 		sm->eap_if->portEnabled = FALSE;
1912 		ap_sta_disconnect(hapd, sta, sta->addr,
1913 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
1914 	}
1915 }
1916 
1917 
ieee802_1x_rekey_broadcast(struct hostapd_data * hapd)1918 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1919 {
1920 	struct eapol_authenticator *eapol = hapd->eapol_auth;
1921 
1922 	if (hapd->conf->default_wep_key_len < 1)
1923 		return 0;
1924 
1925 	os_free(eapol->default_wep_key);
1926 	eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1927 	if (eapol->default_wep_key == NULL ||
1928 	    random_get_bytes(eapol->default_wep_key,
1929 			     hapd->conf->default_wep_key_len)) {
1930 		wpa_printf(MSG_INFO, "Could not generate random WEP key");
1931 		os_free(eapol->default_wep_key);
1932 		eapol->default_wep_key = NULL;
1933 		return -1;
1934 	}
1935 
1936 	wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1937 			eapol->default_wep_key,
1938 			hapd->conf->default_wep_key_len);
1939 
1940 	return 0;
1941 }
1942 
1943 
ieee802_1x_sta_key_available(struct hostapd_data * hapd,struct sta_info * sta,void * ctx)1944 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1945 					struct sta_info *sta, void *ctx)
1946 {
1947 	if (sta->eapol_sm) {
1948 		sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1949 		eapol_auth_step(sta->eapol_sm);
1950 	}
1951 	return 0;
1952 }
1953 
1954 
ieee802_1x_rekey(void * eloop_ctx,void * timeout_ctx)1955 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1956 {
1957 	struct hostapd_data *hapd = eloop_ctx;
1958 	struct eapol_authenticator *eapol = hapd->eapol_auth;
1959 
1960 	if (eapol->default_wep_key_idx >= 3)
1961 		eapol->default_wep_key_idx =
1962 			hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1963 	else
1964 		eapol->default_wep_key_idx++;
1965 
1966 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1967 		   eapol->default_wep_key_idx);
1968 
1969 	if (ieee802_1x_rekey_broadcast(hapd)) {
1970 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1971 			       HOSTAPD_LEVEL_WARNING, "failed to generate a "
1972 			       "new broadcast key");
1973 		os_free(eapol->default_wep_key);
1974 		eapol->default_wep_key = NULL;
1975 		return;
1976 	}
1977 
1978 	/* TODO: Could setup key for RX here, but change default TX keyid only
1979 	 * after new broadcast key has been sent to all stations. */
1980 	if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1981 				broadcast_ether_addr,
1982 				eapol->default_wep_key_idx, 1, NULL, 0,
1983 				eapol->default_wep_key,
1984 				hapd->conf->default_wep_key_len)) {
1985 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1986 			       HOSTAPD_LEVEL_WARNING, "failed to configure a "
1987 			       "new broadcast key");
1988 		os_free(eapol->default_wep_key);
1989 		eapol->default_wep_key = NULL;
1990 		return;
1991 	}
1992 
1993 	ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1994 
1995 	if (hapd->conf->wep_rekeying_period > 0) {
1996 		eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1997 				       ieee802_1x_rekey, hapd, NULL);
1998 	}
1999 }
2000 
2001 
ieee802_1x_eapol_send(void * ctx,void * sta_ctx,u8 type,const u8 * data,size_t datalen)2002 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2003 				  const u8 *data, size_t datalen)
2004 {
2005 #ifdef CONFIG_WPS
2006 	struct sta_info *sta = sta_ctx;
2007 
2008 	if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2009 	    WLAN_STA_MAYBE_WPS) {
2010 		const u8 *identity;
2011 		size_t identity_len;
2012 		struct eapol_state_machine *sm = sta->eapol_sm;
2013 
2014 		identity = eap_get_identity(sm->eap, &identity_len);
2015 		if (identity &&
2016 		    ((identity_len == WSC_ID_ENROLLEE_LEN &&
2017 		      os_memcmp(identity, WSC_ID_ENROLLEE,
2018 				WSC_ID_ENROLLEE_LEN) == 0) ||
2019 		     (identity_len == WSC_ID_REGISTRAR_LEN &&
2020 		      os_memcmp(identity, WSC_ID_REGISTRAR,
2021 				WSC_ID_REGISTRAR_LEN) == 0))) {
2022 			wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
2023 				   "WLAN_STA_WPS");
2024 			sta->flags |= WLAN_STA_WPS;
2025 		}
2026 	}
2027 #endif /* CONFIG_WPS */
2028 
2029 	ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2030 }
2031 
2032 
ieee802_1x_aaa_send(void * ctx,void * sta_ctx,const u8 * data,size_t datalen)2033 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2034 				const u8 *data, size_t datalen)
2035 {
2036 #ifndef CONFIG_NO_RADIUS
2037 	struct hostapd_data *hapd = ctx;
2038 	struct sta_info *sta = sta_ctx;
2039 
2040 	ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2041 #endif /* CONFIG_NO_RADIUS */
2042 }
2043 
2044 
_ieee802_1x_finished(void * ctx,void * sta_ctx,int success,int preauth,int remediation)2045 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2046 				 int preauth, int remediation)
2047 {
2048 	struct hostapd_data *hapd = ctx;
2049 	struct sta_info *sta = sta_ctx;
2050 	if (preauth)
2051 		rsn_preauth_finished(hapd, sta, success);
2052 	else
2053 		ieee802_1x_finished(hapd, sta, success, remediation);
2054 }
2055 
2056 
ieee802_1x_get_eap_user(void * ctx,const u8 * identity,size_t identity_len,int phase2,struct eap_user * user)2057 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2058 				   size_t identity_len, int phase2,
2059 				   struct eap_user *user)
2060 {
2061 	struct hostapd_data *hapd = ctx;
2062 	const struct hostapd_eap_user *eap_user;
2063 	int i;
2064 	int rv = -1;
2065 
2066 	eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
2067 	if (eap_user == NULL)
2068 		goto out;
2069 
2070 	os_memset(user, 0, sizeof(*user));
2071 	user->phase2 = phase2;
2072 	for (i = 0; i < EAP_MAX_METHODS; i++) {
2073 		user->methods[i].vendor = eap_user->methods[i].vendor;
2074 		user->methods[i].method = eap_user->methods[i].method;
2075 	}
2076 
2077 	if (eap_user->password) {
2078 		user->password = os_memdup(eap_user->password,
2079 					   eap_user->password_len);
2080 		if (user->password == NULL)
2081 			goto out;
2082 		user->password_len = eap_user->password_len;
2083 		user->password_hash = eap_user->password_hash;
2084 	}
2085 	user->force_version = eap_user->force_version;
2086 	user->macacl = eap_user->macacl;
2087 	user->ttls_auth = eap_user->ttls_auth;
2088 	user->remediation = eap_user->remediation;
2089 	rv = 0;
2090 
2091 out:
2092 	if (rv)
2093 		wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2094 
2095 	return rv;
2096 }
2097 
2098 
ieee802_1x_sta_entry_alive(void * ctx,const u8 * addr)2099 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2100 {
2101 	struct hostapd_data *hapd = ctx;
2102 	struct sta_info *sta;
2103 	sta = ap_get_sta(hapd, addr);
2104 	if (sta == NULL || sta->eapol_sm == NULL)
2105 		return 0;
2106 	return 1;
2107 }
2108 
2109 
ieee802_1x_logger(void * ctx,const u8 * addr,eapol_logger_level level,const char * txt)2110 static void ieee802_1x_logger(void *ctx, const u8 *addr,
2111 			      eapol_logger_level level, const char *txt)
2112 {
2113 #ifndef CONFIG_NO_HOSTAPD_LOGGER
2114 	struct hostapd_data *hapd = ctx;
2115 	int hlevel;
2116 
2117 	switch (level) {
2118 	case EAPOL_LOGGER_WARNING:
2119 		hlevel = HOSTAPD_LEVEL_WARNING;
2120 		break;
2121 	case EAPOL_LOGGER_INFO:
2122 		hlevel = HOSTAPD_LEVEL_INFO;
2123 		break;
2124 	case EAPOL_LOGGER_DEBUG:
2125 	default:
2126 		hlevel = HOSTAPD_LEVEL_DEBUG;
2127 		break;
2128 	}
2129 
2130 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2131 		       txt);
2132 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
2133 }
2134 
2135 
ieee802_1x_set_port_authorized(void * ctx,void * sta_ctx,int authorized)2136 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2137 					   int authorized)
2138 {
2139 	struct hostapd_data *hapd = ctx;
2140 	struct sta_info *sta = sta_ctx;
2141 	ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2142 }
2143 
2144 
_ieee802_1x_abort_auth(void * ctx,void * sta_ctx)2145 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2146 {
2147 	struct hostapd_data *hapd = ctx;
2148 	struct sta_info *sta = sta_ctx;
2149 	ieee802_1x_abort_auth(hapd, sta);
2150 }
2151 
2152 
_ieee802_1x_tx_key(void * ctx,void * sta_ctx)2153 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2154 {
2155 #ifndef CONFIG_FIPS
2156 #ifndef CONFIG_NO_RC4
2157 	struct hostapd_data *hapd = ctx;
2158 	struct sta_info *sta = sta_ctx;
2159 	ieee802_1x_tx_key(hapd, sta);
2160 #endif /* CONFIG_NO_RC4 */
2161 #endif /* CONFIG_FIPS */
2162 }
2163 
2164 
ieee802_1x_eapol_event(void * ctx,void * sta_ctx,enum eapol_event type)2165 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2166 				   enum eapol_event type)
2167 {
2168 	/* struct hostapd_data *hapd = ctx; */
2169 	struct sta_info *sta = sta_ctx;
2170 	switch (type) {
2171 	case EAPOL_AUTH_SM_CHANGE:
2172 		wpa_auth_sm_notify(sta->wpa_sm);
2173 		break;
2174 	case EAPOL_AUTH_REAUTHENTICATE:
2175 		wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2176 		break;
2177 	}
2178 }
2179 
2180 
2181 #ifdef CONFIG_ERP
2182 
2183 static struct eap_server_erp_key *
ieee802_1x_erp_get_key(void * ctx,const char * keyname)2184 ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2185 {
2186 	struct hostapd_data *hapd = ctx;
2187 	struct eap_server_erp_key *erp;
2188 
2189 	dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2190 			 list) {
2191 		if (os_strcmp(erp->keyname_nai, keyname) == 0)
2192 			return erp;
2193 	}
2194 
2195 	return NULL;
2196 }
2197 
2198 
ieee802_1x_erp_add_key(void * ctx,struct eap_server_erp_key * erp)2199 static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2200 {
2201 	struct hostapd_data *hapd = ctx;
2202 
2203 	dl_list_add(&hapd->erp_keys, &erp->list);
2204 	return 0;
2205 }
2206 
2207 #endif /* CONFIG_ERP */
2208 
2209 
ieee802_1x_init(struct hostapd_data * hapd)2210 int ieee802_1x_init(struct hostapd_data *hapd)
2211 {
2212 	int i;
2213 	struct eapol_auth_config conf;
2214 	struct eapol_auth_cb cb;
2215 
2216 	dl_list_init(&hapd->erp_keys);
2217 
2218 	os_memset(&conf, 0, sizeof(conf));
2219 	conf.ctx = hapd;
2220 	conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2221 	conf.wpa = hapd->conf->wpa;
2222 	conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2223 	conf.eap_server = hapd->conf->eap_server;
2224 	conf.ssl_ctx = hapd->ssl_ctx;
2225 	conf.msg_ctx = hapd->msg_ctx;
2226 	conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2227 	conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2228 	conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2229 	conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2230 	conf.erp_domain = hapd->conf->erp_domain;
2231 	conf.erp = hapd->conf->eap_server_erp;
2232 	conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
2233 	conf.tls_flags = hapd->conf->tls_flags;
2234 	conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2235 	conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2236 	conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2237 	conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2238 	conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2239 	conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2240 	conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2241 	conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2242 	conf.tnc = hapd->conf->tnc;
2243 	conf.wps = hapd->wps;
2244 	conf.fragment_size = hapd->conf->fragment_size;
2245 	conf.pwd_group = hapd->conf->pwd_group;
2246 	conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
2247 	if (hapd->conf->server_id) {
2248 		conf.server_id = (const u8 *) hapd->conf->server_id;
2249 		conf.server_id_len = os_strlen(hapd->conf->server_id);
2250 	} else {
2251 		conf.server_id = (const u8 *) "hostapd";
2252 		conf.server_id_len = 7;
2253 	}
2254 
2255 	os_memset(&cb, 0, sizeof(cb));
2256 	cb.eapol_send = ieee802_1x_eapol_send;
2257 	cb.aaa_send = ieee802_1x_aaa_send;
2258 	cb.finished = _ieee802_1x_finished;
2259 	cb.get_eap_user = ieee802_1x_get_eap_user;
2260 	cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2261 	cb.logger = ieee802_1x_logger;
2262 	cb.set_port_authorized = ieee802_1x_set_port_authorized;
2263 	cb.abort_auth = _ieee802_1x_abort_auth;
2264 	cb.tx_key = _ieee802_1x_tx_key;
2265 	cb.eapol_event = ieee802_1x_eapol_event;
2266 #ifdef CONFIG_ERP
2267 	cb.erp_get_key = ieee802_1x_erp_get_key;
2268 	cb.erp_add_key = ieee802_1x_erp_add_key;
2269 #endif /* CONFIG_ERP */
2270 
2271 	hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2272 	if (hapd->eapol_auth == NULL)
2273 		return -1;
2274 
2275 	if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2276 	    hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2277 		return -1;
2278 
2279 #ifndef CONFIG_NO_RADIUS
2280 	if (radius_client_register(hapd->radius, RADIUS_AUTH,
2281 				   ieee802_1x_receive_auth, hapd))
2282 		return -1;
2283 #endif /* CONFIG_NO_RADIUS */
2284 
2285 	if (hapd->conf->default_wep_key_len) {
2286 		for (i = 0; i < 4; i++)
2287 			hostapd_drv_set_key(hapd->conf->iface, hapd,
2288 					    WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2289 					    NULL, 0);
2290 
2291 		ieee802_1x_rekey(hapd, NULL);
2292 
2293 		if (hapd->eapol_auth->default_wep_key == NULL)
2294 			return -1;
2295 	}
2296 
2297 	return 0;
2298 }
2299 
2300 
ieee802_1x_erp_flush(struct hostapd_data * hapd)2301 void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2302 {
2303 	struct eap_server_erp_key *erp;
2304 
2305 	while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2306 				    list)) != NULL) {
2307 		dl_list_del(&erp->list);
2308 		bin_clear_free(erp, sizeof(*erp));
2309 	}
2310 }
2311 
2312 
ieee802_1x_deinit(struct hostapd_data * hapd)2313 void ieee802_1x_deinit(struct hostapd_data *hapd)
2314 {
2315 	eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2316 
2317 	if (hapd->driver && hapd->drv_priv &&
2318 	    (hapd->conf->ieee802_1x || hapd->conf->wpa))
2319 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2320 
2321 	eapol_auth_deinit(hapd->eapol_auth);
2322 	hapd->eapol_auth = NULL;
2323 
2324 	ieee802_1x_erp_flush(hapd);
2325 }
2326 
2327 
ieee802_1x_tx_status(struct hostapd_data * hapd,struct sta_info * sta,const u8 * buf,size_t len,int ack)2328 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2329 			 const u8 *buf, size_t len, int ack)
2330 {
2331 	struct ieee80211_hdr *hdr;
2332 	u8 *pos;
2333 	const unsigned char rfc1042_hdr[ETH_ALEN] =
2334 		{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2335 
2336 	if (sta == NULL)
2337 		return -1;
2338 	if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2339 		return 0;
2340 
2341 	hdr = (struct ieee80211_hdr *) buf;
2342 	pos = (u8 *) (hdr + 1);
2343 	if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2344 		return 0;
2345 	pos += sizeof(rfc1042_hdr);
2346 	if (WPA_GET_BE16(pos) != ETH_P_PAE)
2347 		return 0;
2348 	pos += 2;
2349 
2350 	return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2351 					  ack);
2352 }
2353 
2354 
ieee802_1x_eapol_tx_status(struct hostapd_data * hapd,struct sta_info * sta,const u8 * buf,int len,int ack)2355 int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2356 			       const u8 *buf, int len, int ack)
2357 {
2358 	const struct ieee802_1x_hdr *xhdr =
2359 		(const struct ieee802_1x_hdr *) buf;
2360 	const u8 *pos = buf + sizeof(*xhdr);
2361 	struct ieee802_1x_eapol_key *key;
2362 
2363 	if (len < (int) sizeof(*xhdr))
2364 		return 0;
2365 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2366 		   "type=%d length=%d - ack=%d",
2367 		   MAC2STR(sta->addr), xhdr->version, xhdr->type,
2368 		   be_to_host16(xhdr->length), ack);
2369 
2370 #ifdef CONFIG_WPS
2371 	if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2372 	    (sta->flags & WLAN_STA_WPS) &&
2373 	    ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2374 		wpa_printf(MSG_DEBUG,
2375 			   "WPS: Indicate EAP completion on ACK for EAP-Failure");
2376 		hostapd_wps_eap_completed(hapd);
2377 	}
2378 #endif /* CONFIG_WPS */
2379 
2380 	if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2381 		return 0;
2382 
2383 	if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2384 		const struct wpa_eapol_key *wpa;
2385 		wpa = (const struct wpa_eapol_key *) pos;
2386 		if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2387 		    wpa->type == EAPOL_KEY_TYPE_WPA)
2388 			wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2389 						     sta->wpa_sm, ack);
2390 	}
2391 
2392 	/* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2393 	 * or Authenticator state machines, but EAPOL-Key packets are not
2394 	 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2395 	 * packets couple of times because otherwise STA keys become
2396 	 * unsynchronized with AP. */
2397 	if (!ack && pos + sizeof(*key) <= buf + len) {
2398 		key = (struct ieee802_1x_eapol_key *) pos;
2399 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2400 			       HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2401 			       "frame (%scast index=%d)",
2402 			       key->key_index & BIT(7) ? "uni" : "broad",
2403 			       key->key_index & ~BIT(7));
2404 		/* TODO: re-send EAPOL-Key couple of times (with short delay
2405 		 * between them?). If all attempt fail, report error and
2406 		 * deauthenticate STA so that it will get new keys when
2407 		 * authenticating again (e.g., after returning in range).
2408 		 * Separate limit/transmit state needed both for unicast and
2409 		 * broadcast keys(?) */
2410 	}
2411 	/* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2412 	 * to here and change the key only if the EAPOL-Key packet was Acked.
2413 	 */
2414 
2415 	return 1;
2416 }
2417 
2418 
ieee802_1x_get_identity(struct eapol_state_machine * sm,size_t * len)2419 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2420 {
2421 	if (sm == NULL || sm->identity == NULL)
2422 		return NULL;
2423 
2424 	*len = sm->identity_len;
2425 	return sm->identity;
2426 }
2427 
2428 
ieee802_1x_get_radius_class(struct eapol_state_machine * sm,size_t * len,int idx)2429 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2430 				 int idx)
2431 {
2432 	if (sm == NULL || sm->radius_class.attr == NULL ||
2433 	    idx >= (int) sm->radius_class.count)
2434 		return NULL;
2435 
2436 	*len = sm->radius_class.attr[idx].len;
2437 	return sm->radius_class.attr[idx].data;
2438 }
2439 
2440 
ieee802_1x_get_radius_cui(struct eapol_state_machine * sm)2441 struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2442 {
2443 	if (sm == NULL)
2444 		return NULL;
2445 	return sm->radius_cui;
2446 }
2447 
2448 
ieee802_1x_get_key(struct eapol_state_machine * sm,size_t * len)2449 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2450 {
2451 	*len = 0;
2452 	if (sm == NULL)
2453 		return NULL;
2454 
2455 	*len = sm->eap_if->eapKeyDataLen;
2456 	return sm->eap_if->eapKeyData;
2457 }
2458 
2459 
ieee802_1x_notify_port_enabled(struct eapol_state_machine * sm,int enabled)2460 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2461 				    int enabled)
2462 {
2463 	if (sm == NULL)
2464 		return;
2465 	sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2466 	eapol_auth_step(sm);
2467 }
2468 
2469 
ieee802_1x_notify_port_valid(struct eapol_state_machine * sm,int valid)2470 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2471 				  int valid)
2472 {
2473 	if (sm == NULL)
2474 		return;
2475 	sm->portValid = valid ? TRUE : FALSE;
2476 	eapol_auth_step(sm);
2477 }
2478 
2479 
ieee802_1x_notify_pre_auth(struct eapol_state_machine * sm,int pre_auth)2480 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2481 {
2482 	if (sm == NULL)
2483 		return;
2484 	if (pre_auth)
2485 		sm->flags |= EAPOL_SM_PREAUTH;
2486 	else
2487 		sm->flags &= ~EAPOL_SM_PREAUTH;
2488 }
2489 
2490 
bool_txt(Boolean val)2491 static const char * bool_txt(Boolean val)
2492 {
2493 	return val ? "TRUE" : "FALSE";
2494 }
2495 
2496 
ieee802_1x_get_mib(struct hostapd_data * hapd,char * buf,size_t buflen)2497 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2498 {
2499 	/* TODO */
2500 	return 0;
2501 }
2502 
2503 
ieee802_1x_get_mib_sta(struct hostapd_data * hapd,struct sta_info * sta,char * buf,size_t buflen)2504 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2505 			   char *buf, size_t buflen)
2506 {
2507 	int len = 0, ret;
2508 	struct eapol_state_machine *sm = sta->eapol_sm;
2509 	struct os_reltime diff;
2510 	const char *name1;
2511 	const char *name2;
2512 
2513 	if (sm == NULL)
2514 		return 0;
2515 
2516 	ret = os_snprintf(buf + len, buflen - len,
2517 			  "dot1xPaePortNumber=%d\n"
2518 			  "dot1xPaePortProtocolVersion=%d\n"
2519 			  "dot1xPaePortCapabilities=1\n"
2520 			  "dot1xPaePortInitialize=%d\n"
2521 			  "dot1xPaePortReauthenticate=FALSE\n",
2522 			  sta->aid,
2523 			  EAPOL_VERSION,
2524 			  sm->initialize);
2525 	if (os_snprintf_error(buflen - len, ret))
2526 		return len;
2527 	len += ret;
2528 
2529 	/* dot1xAuthConfigTable */
2530 	ret = os_snprintf(buf + len, buflen - len,
2531 			  "dot1xAuthPaeState=%d\n"
2532 			  "dot1xAuthBackendAuthState=%d\n"
2533 			  "dot1xAuthAdminControlledDirections=%d\n"
2534 			  "dot1xAuthOperControlledDirections=%d\n"
2535 			  "dot1xAuthAuthControlledPortStatus=%d\n"
2536 			  "dot1xAuthAuthControlledPortControl=%d\n"
2537 			  "dot1xAuthQuietPeriod=%u\n"
2538 			  "dot1xAuthServerTimeout=%u\n"
2539 			  "dot1xAuthReAuthPeriod=%u\n"
2540 			  "dot1xAuthReAuthEnabled=%s\n"
2541 			  "dot1xAuthKeyTxEnabled=%s\n",
2542 			  sm->auth_pae_state + 1,
2543 			  sm->be_auth_state + 1,
2544 			  sm->adminControlledDirections,
2545 			  sm->operControlledDirections,
2546 			  sm->authPortStatus,
2547 			  sm->portControl,
2548 			  sm->quietPeriod,
2549 			  sm->serverTimeout,
2550 			  sm->reAuthPeriod,
2551 			  bool_txt(sm->reAuthEnabled),
2552 			  bool_txt(sm->keyTxEnabled));
2553 	if (os_snprintf_error(buflen - len, ret))
2554 		return len;
2555 	len += ret;
2556 
2557 	/* dot1xAuthStatsTable */
2558 	ret = os_snprintf(buf + len, buflen - len,
2559 			  "dot1xAuthEapolFramesRx=%u\n"
2560 			  "dot1xAuthEapolFramesTx=%u\n"
2561 			  "dot1xAuthEapolStartFramesRx=%u\n"
2562 			  "dot1xAuthEapolLogoffFramesRx=%u\n"
2563 			  "dot1xAuthEapolRespIdFramesRx=%u\n"
2564 			  "dot1xAuthEapolRespFramesRx=%u\n"
2565 			  "dot1xAuthEapolReqIdFramesTx=%u\n"
2566 			  "dot1xAuthEapolReqFramesTx=%u\n"
2567 			  "dot1xAuthInvalidEapolFramesRx=%u\n"
2568 			  "dot1xAuthEapLengthErrorFramesRx=%u\n"
2569 			  "dot1xAuthLastEapolFrameVersion=%u\n"
2570 			  "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2571 			  sm->dot1xAuthEapolFramesRx,
2572 			  sm->dot1xAuthEapolFramesTx,
2573 			  sm->dot1xAuthEapolStartFramesRx,
2574 			  sm->dot1xAuthEapolLogoffFramesRx,
2575 			  sm->dot1xAuthEapolRespIdFramesRx,
2576 			  sm->dot1xAuthEapolRespFramesRx,
2577 			  sm->dot1xAuthEapolReqIdFramesTx,
2578 			  sm->dot1xAuthEapolReqFramesTx,
2579 			  sm->dot1xAuthInvalidEapolFramesRx,
2580 			  sm->dot1xAuthEapLengthErrorFramesRx,
2581 			  sm->dot1xAuthLastEapolFrameVersion,
2582 			  MAC2STR(sm->addr));
2583 	if (os_snprintf_error(buflen - len, ret))
2584 		return len;
2585 	len += ret;
2586 
2587 	/* dot1xAuthDiagTable */
2588 	ret = os_snprintf(buf + len, buflen - len,
2589 			  "dot1xAuthEntersConnecting=%u\n"
2590 			  "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2591 			  "dot1xAuthEntersAuthenticating=%u\n"
2592 			  "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2593 			  "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2594 			  "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2595 			  "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2596 			  "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2597 			  "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2598 			  "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2599 			  "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2600 			  "dot1xAuthBackendResponses=%u\n"
2601 			  "dot1xAuthBackendAccessChallenges=%u\n"
2602 			  "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2603 			  "dot1xAuthBackendAuthSuccesses=%u\n"
2604 			  "dot1xAuthBackendAuthFails=%u\n",
2605 			  sm->authEntersConnecting,
2606 			  sm->authEapLogoffsWhileConnecting,
2607 			  sm->authEntersAuthenticating,
2608 			  sm->authAuthSuccessesWhileAuthenticating,
2609 			  sm->authAuthTimeoutsWhileAuthenticating,
2610 			  sm->authAuthFailWhileAuthenticating,
2611 			  sm->authAuthEapStartsWhileAuthenticating,
2612 			  sm->authAuthEapLogoffWhileAuthenticating,
2613 			  sm->authAuthReauthsWhileAuthenticated,
2614 			  sm->authAuthEapStartsWhileAuthenticated,
2615 			  sm->authAuthEapLogoffWhileAuthenticated,
2616 			  sm->backendResponses,
2617 			  sm->backendAccessChallenges,
2618 			  sm->backendOtherRequestsToSupplicant,
2619 			  sm->backendAuthSuccesses,
2620 			  sm->backendAuthFails);
2621 	if (os_snprintf_error(buflen - len, ret))
2622 		return len;
2623 	len += ret;
2624 
2625 	/* dot1xAuthSessionStatsTable */
2626 	os_reltime_age(&sta->acct_session_start, &diff);
2627 	ret = os_snprintf(buf + len, buflen - len,
2628 			  /* TODO: dot1xAuthSessionOctetsRx */
2629 			  /* TODO: dot1xAuthSessionOctetsTx */
2630 			  /* TODO: dot1xAuthSessionFramesRx */
2631 			  /* TODO: dot1xAuthSessionFramesTx */
2632 			  "dot1xAuthSessionId=%016llX\n"
2633 			  "dot1xAuthSessionAuthenticMethod=%d\n"
2634 			  "dot1xAuthSessionTime=%u\n"
2635 			  "dot1xAuthSessionTerminateCause=999\n"
2636 			  "dot1xAuthSessionUserName=%s\n",
2637 			  (unsigned long long) sta->acct_session_id,
2638 			  (wpa_key_mgmt_wpa_ieee8021x(
2639 				   wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2640 			  1 : 2,
2641 			  (unsigned int) diff.sec,
2642 			  sm->identity);
2643 	if (os_snprintf_error(buflen - len, ret))
2644 		return len;
2645 	len += ret;
2646 
2647 	if (sm->acct_multi_session_id) {
2648 		ret = os_snprintf(buf + len, buflen - len,
2649 				  "authMultiSessionId=%016llX\n",
2650 				  (unsigned long long)
2651 				  sm->acct_multi_session_id);
2652 		if (os_snprintf_error(buflen - len, ret))
2653 			return len;
2654 		len += ret;
2655 	}
2656 
2657 	name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2658 	name2 = eap_server_get_name(0, sm->eap_type_supp);
2659 	ret = os_snprintf(buf + len, buflen - len,
2660 			  "last_eap_type_as=%d (%s)\n"
2661 			  "last_eap_type_sta=%d (%s)\n",
2662 			  sm->eap_type_authsrv, name1,
2663 			  sm->eap_type_supp, name2);
2664 	if (os_snprintf_error(buflen - len, ret))
2665 		return len;
2666 	len += ret;
2667 
2668 	return len;
2669 }
2670 
2671 
2672 #ifdef CONFIG_HS20
ieee802_1x_wnm_notif_send(void * eloop_ctx,void * timeout_ctx)2673 static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
2674 {
2675 	struct hostapd_data *hapd = eloop_ctx;
2676 	struct sta_info *sta = timeout_ctx;
2677 
2678 	if (sta->remediation) {
2679 		wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2680 			   MACSTR " to indicate Subscription Remediation",
2681 			   MAC2STR(sta->addr));
2682 		hs20_send_wnm_notification(hapd, sta->addr,
2683 					   sta->remediation_method,
2684 					   sta->remediation_url);
2685 		os_free(sta->remediation_url);
2686 		sta->remediation_url = NULL;
2687 	}
2688 
2689 	if (sta->hs20_deauth_req) {
2690 		wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2691 			   MACSTR " to indicate imminent deauthentication",
2692 			   MAC2STR(sta->addr));
2693 		hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
2694 						      sta->hs20_deauth_req);
2695 	}
2696 }
2697 #endif /* CONFIG_HS20 */
2698 
2699 
ieee802_1x_finished(struct hostapd_data * hapd,struct sta_info * sta,int success,int remediation)2700 static void ieee802_1x_finished(struct hostapd_data *hapd,
2701 				struct sta_info *sta, int success,
2702 				int remediation)
2703 {
2704 	const u8 *key;
2705 	size_t len;
2706 	/* TODO: get PMKLifetime from WPA parameters */
2707 	static const int dot11RSNAConfigPMKLifetime = 43200;
2708 	unsigned int session_timeout;
2709 
2710 #ifdef CONFIG_HS20
2711 	if (remediation && !sta->remediation) {
2712 		sta->remediation = 1;
2713 		os_free(sta->remediation_url);
2714 		sta->remediation_url =
2715 			os_strdup(hapd->conf->subscr_remediation_url);
2716 		sta->remediation_method = 1; /* SOAP-XML SPP */
2717 	}
2718 
2719 	if (success && (sta->remediation || sta->hs20_deauth_req)) {
2720 		wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
2721 			   MACSTR " in 100 ms", MAC2STR(sta->addr));
2722 		eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
2723 		eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
2724 				       hapd, sta);
2725 	}
2726 #endif /* CONFIG_HS20 */
2727 
2728 	key = ieee802_1x_get_key(sta->eapol_sm, &len);
2729 	if (sta->session_timeout_set)
2730 		session_timeout = sta->session_timeout;
2731 	else
2732 		session_timeout = dot11RSNAConfigPMKLifetime;
2733 	if (success && key && len >= PMK_LEN && !sta->remediation &&
2734 	    !sta->hs20_deauth_requested &&
2735 	    wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
2736 			       sta->eapol_sm) == 0) {
2737 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2738 			       HOSTAPD_LEVEL_DEBUG,
2739 			       "Added PMKSA cache entry (IEEE 802.1X)");
2740 	}
2741 
2742 	if (!success) {
2743 		/*
2744 		 * Many devices require deauthentication after WPS provisioning
2745 		 * and some may not be be able to do that themselves, so
2746 		 * disconnect the client here. In addition, this may also
2747 		 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2748 		 * the EAPOL PAE state machine would remain in HELD state for
2749 		 * considerable amount of time and some EAP methods, like
2750 		 * EAP-FAST with anonymous provisioning, may require another
2751 		 * EAPOL authentication to be started to complete connection.
2752 		 */
2753 		ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta);
2754 	}
2755 }
2756