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