blob: e8d21ff7ac0b7293234c07ff792aafff76b76fb1 [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"
Hai Shalomc3565922019-10-28 11:58:20 -070010#ifdef CONFIG_SQLITE
11#include <sqlite3.h>
12#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013
14#include "utils/common.h"
15#include "utils/eloop.h"
16#include "crypto/md5.h"
17#include "crypto/crypto.h"
18#include "crypto/random.h"
19#include "common/ieee802_11_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "radius/radius.h"
21#include "radius/radius_client.h"
22#include "eap_server/eap.h"
23#include "eap_common/eap_wsc_common.h"
24#include "eapol_auth/eapol_auth_sm.h"
25#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080026#include "p2p/p2p.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "hostapd.h"
28#include "accounting.h"
29#include "sta_info.h"
30#include "wpa_auth.h"
31#include "preauth_auth.h"
32#include "pmksa_cache_auth.h"
33#include "ap_config.h"
34#include "ap_drv_ops.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080035#include "wps_hostapd.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080036#include "hs20.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080037/* FIX: Not really a good thing to require ieee802_11.h here.. (FILS) */
38#include "ieee802_11.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "ieee802_1x.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070040#include "wpa_auth_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041
42
Dmitry Shmidtde47be72016-01-07 12:52:55 -080043#ifdef CONFIG_HS20
44static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
45#endif /* CONFIG_HS20 */
Sunil Ravi640215c2023-06-28 23:08:09 +000046static bool ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080047 struct sta_info *sta, int success,
Sunil Ravi640215c2023-06-28 23:08:09 +000048 int remediation, bool logoff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049
50
51static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
52 u8 type, const u8 *data, size_t datalen)
53{
54 u8 *buf;
55 struct ieee802_1x_hdr *xhdr;
56 size_t len;
57 int encrypt = 0;
58
59 len = sizeof(*xhdr) + datalen;
60 buf = os_zalloc(len);
Hai Shalomc3565922019-10-28 11:58:20 -070061 if (!buf) {
62 wpa_printf(MSG_ERROR, "malloc() failed for %s(len=%lu)",
63 __func__, (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 return;
65 }
66
67 xhdr = (struct ieee802_1x_hdr *) buf;
68 xhdr->version = hapd->conf->eapol_version;
Hai Shalom81f62d82019-07-22 12:10:00 -070069#ifdef CONFIG_MACSEC
70 if (xhdr->version > 2 && hapd->conf->macsec_policy == 0)
71 xhdr->version = 2;
72#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 xhdr->type = type;
74 xhdr->length = host_to_be16(datalen);
75
76 if (datalen > 0 && data != NULL)
77 os_memcpy(xhdr + 1, data, datalen);
78
79 if (wpa_auth_pairwise_set(sta->wpa_sm))
80 encrypt = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080081#ifdef CONFIG_TESTING_OPTIONS
82 if (hapd->ext_eapol_frame_io) {
83 size_t hex_len = 2 * len + 1;
84 char *hex = os_malloc(hex_len);
85
86 if (hex) {
87 wpa_snprintf_hex(hex, hex_len, buf, len);
88 wpa_msg(hapd->msg_ctx, MSG_INFO,
89 "EAPOL-TX " MACSTR " %s",
90 MAC2STR(sta->addr), hex);
91 os_free(hex);
92 }
93 } else
94#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 if (sta->flags & WLAN_STA_PREAUTH) {
96 rsn_preauth_send(hapd, sta, buf, len);
97 } else {
Sunil Ravi2a14cf12023-11-21 00:54:38 +000098 int link_id = -1;
99
100#ifdef CONFIG_IEEE80211BE
101 link_id = hapd->conf->mld_ap ? hapd->mld_link_id : -1;
102#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800103 hostapd_drv_hapd_send_eapol(
104 hapd, sta->addr, buf, len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000105 encrypt, hostapd_sta_flags_to_drv(sta->flags), link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 }
107
108 os_free(buf);
109}
110
111
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000112static void ieee802_1x_set_authorized(struct hostapd_data *hapd,
113 struct sta_info *sta,
114 bool authorized, bool mld)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115{
116 int res;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000117 bool update;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118
119 if (sta->flags & WLAN_STA_PREAUTH)
120 return;
121
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000122 update = ap_sta_set_authorized_flag(hapd, sta, authorized);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000123 res = hostapd_set_authorized(hapd, sta, authorized);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000124 if (update)
125 ap_sta_set_authorized_event(hapd, sta, authorized);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000126 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
127 HOSTAPD_LEVEL_DEBUG, "%sauthorizing port",
128 authorized ? "" : "un");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000130 if (!mld && res && errno != ENOENT) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700131 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
132 " flags for kernel driver (errno=%d).",
133 MAC2STR(sta->addr), errno);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000134 } else if (mld && res) {
135 wpa_printf(MSG_DEBUG,
136 "MLD: Could not set station " MACSTR " flags",
137 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 }
139
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800140 if (authorized) {
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800141 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144}
145
146
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000147static void ieee802_1x_ml_set_sta_authorized(struct hostapd_data *hapd,
148 struct sta_info *sta,
149 bool authorized)
150{
151#ifdef CONFIG_IEEE80211BE
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000152 unsigned int i;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000153
154 if (!hostapd_is_mld_ap(hapd))
155 return;
156
157 /*
158 * Authorizing the station should be done only in the station
159 * performing the association
160 */
161 if (authorized && hapd->mld_link_id != sta->mld_assoc_link_id)
162 return;
163
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000164 for (i = 0; i < hapd->iface->interfaces->count; i++) {
165 struct sta_info *tmp_sta;
166 struct mld_link_info *link;
167 struct hostapd_data *tmp_hapd =
168 hapd->iface->interfaces->iface[i]->bss[0];
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000169
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000170 if (!hostapd_is_ml_partner(hapd, tmp_hapd))
171 continue;
172
173 link = &sta->mld_info.links[tmp_hapd->mld_link_id];
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000174 if (!link->valid)
175 continue;
176
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000177 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
178 tmp_sta = tmp_sta->next) {
179 if (tmp_sta == sta ||
180 tmp_sta->mld_assoc_link_id !=
181 sta->mld_assoc_link_id ||
182 tmp_sta->aid != sta->aid)
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000183 continue;
184
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000185 ieee802_1x_set_authorized(tmp_hapd, tmp_sta,
186 authorized, true);
187 break;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000188 }
189 }
190#endif /* CONFIG_IEEE80211BE */
191}
192
193
194
195void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
196 struct sta_info *sta, int authorized)
197{
198 ieee802_1x_set_authorized(hapd, sta, authorized, false);
199 ieee802_1x_ml_set_sta_authorized(hapd, sta, !!authorized);
200}
201
202
Hai Shalomfdcde762020-04-02 11:19:20 -0700203#ifdef CONFIG_WEP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800204#ifndef CONFIG_FIPS
205#ifndef CONFIG_NO_RC4
206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
208 struct sta_info *sta,
209 int idx, int broadcast,
210 u8 *key_data, size_t key_len)
211{
212 u8 *buf, *ekey;
213 struct ieee802_1x_hdr *hdr;
214 struct ieee802_1x_eapol_key *key;
215 size_t len, ekey_len;
216 struct eapol_state_machine *sm = sta->eapol_sm;
217
Hai Shalomc3565922019-10-28 11:58:20 -0700218 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 return;
220
221 len = sizeof(*key) + key_len;
222 buf = os_zalloc(sizeof(*hdr) + len);
Hai Shalomc3565922019-10-28 11:58:20 -0700223 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 return;
225
226 hdr = (struct ieee802_1x_hdr *) buf;
227 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
228 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700229 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 wpa_get_ntp_timestamp(key->replay_counter);
Hai Shalom81f62d82019-07-22 12:10:00 -0700231 if (os_memcmp(key->replay_counter,
232 hapd->last_1x_eapol_key_replay_counter,
233 IEEE8021X_REPLAY_COUNTER_LEN) <= 0) {
234 /* NTP timestamp did not increment from last EAPOL-Key frame;
235 * use previously used value + 1 instead. */
236 inc_byte_array(hapd->last_1x_eapol_key_replay_counter,
237 IEEE8021X_REPLAY_COUNTER_LEN);
238 os_memcpy(key->replay_counter,
239 hapd->last_1x_eapol_key_replay_counter,
240 IEEE8021X_REPLAY_COUNTER_LEN);
241 } else {
242 os_memcpy(hapd->last_1x_eapol_key_replay_counter,
243 key->replay_counter,
244 IEEE8021X_REPLAY_COUNTER_LEN);
245 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246
247 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
248 wpa_printf(MSG_ERROR, "Could not get random numbers");
249 os_free(buf);
250 return;
251 }
252
253 key->key_index = idx | (broadcast ? 0 : BIT(7));
254 if (hapd->conf->eapol_key_index_workaround) {
255 /* According to some information, WinXP Supplicant seems to
256 * interpret bit7 as an indication whether the key is to be
257 * activated, so make it possible to enable workaround that
258 * sets this bit for all keys. */
259 key->key_index |= BIT(7);
260 }
261
262 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
263 * MSK[32..63] is used to sign the message. */
Hai Shalomc3565922019-10-28 11:58:20 -0700264 if (!sm->eap_if->eapKeyData || sm->eap_if->eapKeyDataLen < 64) {
265 wpa_printf(MSG_ERROR,
266 "No eapKeyData available for encrypting and signing EAPOL-Key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 os_free(buf);
268 return;
269 }
270 os_memcpy((u8 *) (key + 1), key_data, key_len);
271 ekey_len = sizeof(key->key_iv) + 32;
272 ekey = os_malloc(ekey_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700273 if (!ekey) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 wpa_printf(MSG_ERROR, "Could not encrypt key");
275 os_free(buf);
276 return;
277 }
278 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
279 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
280 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
281 os_free(ekey);
282
283 /* This header is needed here for HMAC-MD5, but it will be regenerated
284 * in ieee802_1x_send() */
285 hdr->version = hapd->conf->eapol_version;
Hai Shalom81f62d82019-07-22 12:10:00 -0700286#ifdef CONFIG_MACSEC
287 if (hdr->version > 2)
288 hdr->version = 2;
289#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
291 hdr->length = host_to_be16(len);
292 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
293 key->key_signature);
294
295 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
296 " (%s index=%d)", MAC2STR(sm->addr),
297 broadcast ? "broadcast" : "unicast", idx);
298 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
299 if (sta->eapol_sm)
300 sta->eapol_sm->dot1xAuthEapolFramesTx++;
301 os_free(buf);
302}
303
304
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800305static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306{
307 struct eapol_authenticator *eapol = hapd->eapol_auth;
308 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309
Hai Shalomc3565922019-10-28 11:58:20 -0700310 if (!sm || !sm->eap_if->eapKeyData)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 return;
312
313 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
314 MAC2STR(sta->addr));
315
316#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800317 if (sta->vlan_id > 0) {
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700318 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
319 return;
320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 if (eapol->default_wep_key) {
324 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
325 eapol->default_wep_key,
326 hapd->conf->default_wep_key_len);
327 }
328
329 if (hapd->conf->individual_wep_key_len > 0) {
330 u8 *ikey;
Hai Shalomc3565922019-10-28 11:58:20 -0700331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 ikey = os_malloc(hapd->conf->individual_wep_key_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700333 if (!ikey ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
335 {
Hai Shalomc3565922019-10-28 11:58:20 -0700336 wpa_printf(MSG_ERROR,
337 "Could not generate random individual WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 os_free(ikey);
339 return;
340 }
341
342 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
343 ikey, hapd->conf->individual_wep_key_len);
344
345 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
346 hapd->conf->individual_wep_key_len);
347
348 /* TODO: set encryption in TX callback, i.e., only after STA
349 * has ACKed EAPOL-Key frame */
350 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
Hai Shalomfdcde762020-04-02 11:19:20 -0700351 sta->addr, 0, 0, 1, NULL, 0, ikey,
352 hapd->conf->individual_wep_key_len,
353 KEY_FLAG_PAIRWISE_RX_TX)) {
Hai Shalomc3565922019-10-28 11:58:20 -0700354 wpa_printf(MSG_ERROR,
355 "Could not set individual WEP encryption");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 }
357
358 os_free(ikey);
359 }
360}
361
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800362#endif /* CONFIG_NO_RC4 */
363#endif /* CONFIG_FIPS */
Hai Shalomfdcde762020-04-02 11:19:20 -0700364#endif /* CONFIG_WEP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366
367const char *radius_mode_txt(struct hostapd_data *hapd)
368{
369 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800370 case HOSTAPD_MODE_IEEE80211AD:
371 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 case HOSTAPD_MODE_IEEE80211A:
373 return "802.11a";
374 case HOSTAPD_MODE_IEEE80211G:
375 return "802.11g";
376 case HOSTAPD_MODE_IEEE80211B:
377 default:
378 return "802.11b";
379 }
380}
381
382
383int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
384{
385 int i;
386 u8 rate = 0;
387
388 for (i = 0; i < sta->supported_rates_len; i++)
389 if ((sta->supported_rates[i] & 0x7f) > rate)
390 rate = sta->supported_rates[i] & 0x7f;
391
392 return rate;
393}
394
395
396#ifndef CONFIG_NO_RADIUS
397static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
398 struct eapol_state_machine *sm,
399 const u8 *eap, size_t len)
400{
401 const u8 *identity;
402 size_t identity_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800403 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404
405 if (len <= sizeof(struct eap_hdr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800406 (hdr->code == EAP_CODE_RESPONSE &&
407 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
408 (hdr->code == EAP_CODE_INITIATE &&
409 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
410 (hdr->code != EAP_CODE_RESPONSE &&
411 hdr->code != EAP_CODE_INITIATE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 return;
413
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800414 eap_erp_update_identity(sm->eap, eap, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 identity = eap_get_identity(sm->eap, &identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700416 if (!identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 return;
418
419 /* Save station identity for future RADIUS packets */
420 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700421 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700422 if (!sm->identity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 sm->identity_len = 0;
424 return;
425 }
426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
429 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
430 sm->dot1xAuthEapolRespIdFramesRx++;
431}
432
433
Dmitry Shmidt03658832014-08-13 11:03:49 -0700434static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
435 struct hostapd_radius_attr *req_attr,
436 struct sta_info *sta,
437 struct radius_msg *msg)
438{
439 u32 suite;
440 int ver, val;
441
442 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
443 val = wpa_auth_get_pairwise(sta->wpa_sm);
444 suite = wpa_cipher_to_suite(ver, val);
445 if (val != -1 &&
446 !hostapd_config_get_radius_attr(req_attr,
447 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
448 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
449 suite)) {
450 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
451 return -1;
452 }
453
Dmitry Shmidt41712582015-06-29 11:02:15 -0700454 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
455 hapd->conf->osen) ?
Dmitry Shmidt03658832014-08-13 11:03:49 -0700456 WPA_PROTO_RSN : WPA_PROTO_WPA,
457 hapd->conf->wpa_group);
458 if (!hostapd_config_get_radius_attr(req_attr,
459 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
460 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
461 suite)) {
462 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
463 return -1;
464 }
465
466 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
467 suite = wpa_akm_to_suite(val);
468 if (val != -1 &&
469 !hostapd_config_get_radius_attr(req_attr,
470 RADIUS_ATTR_WLAN_AKM_SUITE) &&
471 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
472 suite)) {
473 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
474 return -1;
475 }
476
Dmitry Shmidt03658832014-08-13 11:03:49 -0700477 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
478 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
479 hapd->conf->group_mgmt_cipher);
480 if (!hostapd_config_get_radius_attr(
481 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
482 !radius_msg_add_attr_int32(
483 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
484 wpa_printf(MSG_ERROR,
485 "Could not add WLAN-Group-Mgmt-Cipher");
486 return -1;
487 }
488 }
Dmitry Shmidt03658832014-08-13 11:03:49 -0700489
490 return 0;
491}
492
493
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700494static int add_common_radius_sta_attr(struct hostapd_data *hapd,
495 struct hostapd_radius_attr *req_attr,
496 struct sta_info *sta,
497 struct radius_msg *msg)
498{
499 char buf[128];
500
501 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800502 RADIUS_ATTR_SERVICE_TYPE) &&
503 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
504 RADIUS_SERVICE_TYPE_FRAMED)) {
505 wpa_printf(MSG_ERROR, "Could not add Service-Type");
506 return -1;
507 }
508
509 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700510 RADIUS_ATTR_NAS_PORT) &&
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700511 sta->aid > 0 &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700512 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
513 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
514 return -1;
515 }
516
517 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
518 MAC2STR(sta->addr));
519 buf[sizeof(buf) - 1] = '\0';
520 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
521 (u8 *) buf, os_strlen(buf))) {
522 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
523 return -1;
524 }
525
526 if (sta->flags & WLAN_STA_PREAUTH) {
527 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
528 sizeof(buf));
529 } else {
530 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
531 radius_sta_rate(hapd, sta) / 2,
532 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
533 radius_mode_txt(hapd));
534 buf[sizeof(buf) - 1] = '\0';
535 }
536 if (!hostapd_config_get_radius_attr(req_attr,
537 RADIUS_ATTR_CONNECT_INFO) &&
538 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
539 (u8 *) buf, os_strlen(buf))) {
540 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
541 return -1;
542 }
543
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800544 if (sta->acct_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800545 os_snprintf(buf, sizeof(buf), "%016llX",
546 (unsigned long long) sta->acct_session_id);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800547 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
548 (u8 *) buf, os_strlen(buf))) {
549 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
550 return -1;
551 }
552 }
553
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800554 if ((hapd->conf->wpa & 2) &&
555 !hapd->conf->disable_pmksa_caching &&
556 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800557 os_snprintf(buf, sizeof(buf), "%016llX",
558 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800559 sta->eapol_sm->acct_multi_session_id);
560 if (!radius_msg_add_attr(
561 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
562 (u8 *) buf, os_strlen(buf))) {
563 wpa_printf(MSG_INFO,
564 "Could not add Acct-Multi-Session-Id");
565 return -1;
566 }
567 }
568
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800569#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt03658832014-08-13 11:03:49 -0700570 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
571 sta->wpa_sm &&
572 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
573 sta->auth_alg == WLAN_AUTH_FT) &&
574 !hostapd_config_get_radius_attr(req_attr,
575 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
576 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
577 WPA_GET_BE16(
578 hapd->conf->mobility_domain))) {
579 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
580 return -1;
581 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800582#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt03658832014-08-13 11:03:49 -0700583
Dmitry Shmidt41712582015-06-29 11:02:15 -0700584 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
Dmitry Shmidt03658832014-08-13 11:03:49 -0700585 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
586 return -1;
587
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700588 return 0;
589}
590
591
592int add_common_radius_attr(struct hostapd_data *hapd,
593 struct hostapd_radius_attr *req_attr,
594 struct sta_info *sta,
595 struct radius_msg *msg)
596{
597 char buf[128];
598 struct hostapd_radius_attr *attr;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800599 int len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700600
601 if (!hostapd_config_get_radius_attr(req_attr,
602 RADIUS_ATTR_NAS_IP_ADDRESS) &&
603 hapd->conf->own_ip_addr.af == AF_INET &&
604 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
605 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
606 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
607 return -1;
608 }
609
610#ifdef CONFIG_IPV6
611 if (!hostapd_config_get_radius_attr(req_attr,
612 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
613 hapd->conf->own_ip_addr.af == AF_INET6 &&
614 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
615 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
616 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
617 return -1;
618 }
619#endif /* CONFIG_IPV6 */
620
621 if (!hostapd_config_get_radius_attr(req_attr,
622 RADIUS_ATTR_NAS_IDENTIFIER) &&
623 hapd->conf->nas_identifier &&
624 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
625 (u8 *) hapd->conf->nas_identifier,
626 os_strlen(hapd->conf->nas_identifier))) {
627 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
628 return -1;
629 }
630
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800631 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
632 MAC2STR(hapd->own_addr));
633 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
634 hapd->conf->ssid.ssid_len);
635 len += hapd->conf->ssid.ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700636 if (!hostapd_config_get_radius_attr(req_attr,
637 RADIUS_ATTR_CALLED_STATION_ID) &&
638 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800639 (u8 *) buf, len)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700640 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
641 return -1;
642 }
643
644 if (!hostapd_config_get_radius_attr(req_attr,
645 RADIUS_ATTR_NAS_PORT_TYPE) &&
646 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
647 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
648 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
649 return -1;
650 }
651
Dmitry Shmidt03658832014-08-13 11:03:49 -0700652#ifdef CONFIG_INTERWORKING
653 if (hapd->conf->interworking &&
654 !is_zero_ether_addr(hapd->conf->hessid)) {
655 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
656 MAC2STR(hapd->conf->hessid));
657 buf[sizeof(buf) - 1] = '\0';
658 if (!hostapd_config_get_radius_attr(req_attr,
659 RADIUS_ATTR_WLAN_HESSID) &&
660 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
661 (u8 *) buf, os_strlen(buf))) {
662 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
663 return -1;
664 }
665 }
666#endif /* CONFIG_INTERWORKING */
667
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700668 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
669 return -1;
670
671 for (attr = req_attr; attr; attr = attr->next) {
672 if (!radius_msg_add_attr(msg, attr->type,
673 wpabuf_head(attr->val),
674 wpabuf_len(attr->val))) {
Hai Shalomc3565922019-10-28 11:58:20 -0700675 wpa_printf(MSG_ERROR, "Could not add RADIUS attribute");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700676 return -1;
677 }
678 }
679
680 return 0;
681}
682
683
Hai Shalomc3565922019-10-28 11:58:20 -0700684int add_sqlite_radius_attr(struct hostapd_data *hapd, struct sta_info *sta,
685 struct radius_msg *msg, int acct)
686{
687#ifdef CONFIG_SQLITE
688 const char *attrtxt;
689 char addrtxt[3 * ETH_ALEN];
690 char *sql;
691 sqlite3_stmt *stmt = NULL;
692
693 if (!hapd->rad_attr_db)
694 return 0;
695
696 os_snprintf(addrtxt, sizeof(addrtxt), MACSTR, MAC2STR(sta->addr));
697
698 sql = "SELECT attr FROM radius_attributes WHERE sta=? AND (reqtype=? OR reqtype IS NULL);";
699 if (sqlite3_prepare_v2(hapd->rad_attr_db, sql, os_strlen(sql), &stmt,
700 NULL) != SQLITE_OK) {
701 wpa_printf(MSG_ERROR, "DB: Failed to prepare SQL statement: %s",
702 sqlite3_errmsg(hapd->rad_attr_db));
703 return -1;
704 }
705 sqlite3_bind_text(stmt, 1, addrtxt, os_strlen(addrtxt), SQLITE_STATIC);
706 sqlite3_bind_text(stmt, 2, acct ? "acct" : "auth", 4, SQLITE_STATIC);
707 while (sqlite3_step(stmt) == SQLITE_ROW) {
708 struct hostapd_radius_attr *attr;
709 struct radius_attr_hdr *hdr;
710
711 attrtxt = (const char *) sqlite3_column_text(stmt, 0);
712 attr = hostapd_parse_radius_attr(attrtxt);
713 if (!attr) {
714 wpa_printf(MSG_ERROR,
715 "Skipping invalid attribute from SQL: %s",
716 attrtxt);
717 continue;
718 }
719 wpa_printf(MSG_DEBUG, "Adding RADIUS attribute from SQL: %s",
720 attrtxt);
721 hdr = radius_msg_add_attr(msg, attr->type,
722 wpabuf_head(attr->val),
723 wpabuf_len(attr->val));
724 hostapd_config_free_radius_attr(attr);
725 if (!hdr) {
726 wpa_printf(MSG_ERROR,
727 "Could not add RADIUS attribute from SQL");
728 continue;
729 }
730 }
731
732 sqlite3_reset(stmt);
733 sqlite3_clear_bindings(stmt);
734 sqlite3_finalize(stmt);
735#endif /* CONFIG_SQLITE */
736
737 return 0;
738}
739
740
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800741void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
742 struct sta_info *sta,
743 const u8 *eap, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744{
745 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746 struct eapol_state_machine *sm = sta->eapol_sm;
747
Hai Shalomc3565922019-10-28 11:58:20 -0700748 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 return;
750
751 ieee802_1x_learn_identity(hapd, sm, eap, len);
752
Hai Shalomc3565922019-10-28 11:58:20 -0700753 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754
755 sm->radius_identifier = radius_client_get_id(hapd->radius);
756 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
757 sm->radius_identifier);
Hai Shalomc3565922019-10-28 11:58:20 -0700758 if (!msg) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800759 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 return;
761 }
762
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800763 if (radius_msg_make_authenticator(msg) < 0) {
764 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
765 goto fail;
766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767
Sunil Ravi7f769292024-07-23 22:21:32 +0000768 if (!radius_msg_add_msg_auth(msg))
769 goto fail;
770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 if (sm->identity &&
772 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
773 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800774 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 goto fail;
776 }
777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
779 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781
Hai Shalomc3565922019-10-28 11:58:20 -0700782 if (sta && add_sqlite_radius_attr(hapd, sta, msg, 0) < 0)
783 goto fail;
784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 /* TODO: should probably check MTU from driver config; 2304 is max for
786 * IEEE 802.11, but use 1400 to avoid problems with too large packets
787 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700788 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
789 RADIUS_ATTR_FRAMED_MTU) &&
790 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800791 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 goto fail;
793 }
794
Dmitry Shmidt41712582015-06-29 11:02:15 -0700795 if (!radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800796 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 goto fail;
798 }
799
800 /* State attribute must be copied if and only if this packet is
801 * Access-Request reply to the previous Access-Challenge */
802 if (sm->last_recv_radius &&
803 radius_msg_get_hdr(sm->last_recv_radius)->code ==
804 RADIUS_CODE_ACCESS_CHALLENGE) {
805 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
806 RADIUS_ATTR_STATE);
807 if (res < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -0700808 wpa_printf(MSG_INFO,
809 "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 goto fail;
811 }
Hai Shalomc3565922019-10-28 11:58:20 -0700812 if (res > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 }
815
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816 if (hapd->conf->radius_request_cui) {
817 const u8 *cui;
818 size_t cui_len;
819 /* Add previously learned CUI or nul CUI to request CUI */
820 if (sm->radius_cui) {
821 cui = wpabuf_head(sm->radius_cui);
822 cui_len = wpabuf_len(sm->radius_cui);
823 } else {
824 cui = (const u8 *) "\0";
825 cui_len = 1;
826 }
827 if (!radius_msg_add_attr(msg,
828 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
829 cui, cui_len)) {
830 wpa_printf(MSG_ERROR, "Could not add CUI");
831 goto fail;
832 }
833 }
834
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800835#ifdef CONFIG_HS20
836 if (hapd->conf->hs20) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800837 u8 ver = hapd->conf->hs20_release - 1;
838
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800839 if (!radius_msg_add_wfa(
840 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
841 &ver, 1)) {
Hai Shalomc3565922019-10-28 11:58:20 -0700842 wpa_printf(MSG_ERROR,
843 "Could not add HS 2.0 AP version");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800844 goto fail;
845 }
846
847 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
848 const u8 *pos;
849 u8 buf[3];
850 u16 id;
Hai Shalomc3565922019-10-28 11:58:20 -0700851
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800852 pos = wpabuf_head_u8(sta->hs20_ie);
853 buf[0] = (*pos) >> 4;
854 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
855 wpabuf_len(sta->hs20_ie) >= 3)
856 id = WPA_GET_LE16(pos + 1);
857 else
858 id = 0;
859 WPA_PUT_BE16(buf + 1, id);
860 if (!radius_msg_add_wfa(
861 msg,
862 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
863 buf, sizeof(buf))) {
Hai Shalomc3565922019-10-28 11:58:20 -0700864 wpa_printf(MSG_ERROR,
865 "Could not add HS 2.0 STA version");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800866 goto fail;
867 }
868 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700869
870 if (sta->roaming_consortium &&
871 !radius_msg_add_wfa(
872 msg, RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM,
873 wpabuf_head(sta->roaming_consortium),
874 wpabuf_len(sta->roaming_consortium))) {
875 wpa_printf(MSG_ERROR,
876 "Could not add HS 2.0 Roaming Consortium");
877 goto fail;
878 }
879
880 if (hapd->conf->t_c_filename) {
881 be32 timestamp;
882
883 if (!radius_msg_add_wfa(
884 msg,
885 RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME,
886 (const u8 *) hapd->conf->t_c_filename,
887 os_strlen(hapd->conf->t_c_filename))) {
888 wpa_printf(MSG_ERROR,
889 "Could not add HS 2.0 T&C Filename");
890 goto fail;
891 }
892
893 timestamp = host_to_be32(hapd->conf->t_c_timestamp);
894 if (!radius_msg_add_wfa(
895 msg,
896 RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP,
897 (const u8 *) &timestamp,
898 sizeof(timestamp))) {
899 wpa_printf(MSG_ERROR,
900 "Could not add HS 2.0 Timestamp");
901 goto fail;
902 }
903 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800904 }
905#endif /* CONFIG_HS20 */
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
908 goto fail;
909
910 return;
911
912 fail:
913 radius_msg_free(msg);
914}
915#endif /* CONFIG_NO_RADIUS */
916
917
918static void handle_eap_response(struct hostapd_data *hapd,
919 struct sta_info *sta, struct eap_hdr *eap,
920 size_t len)
921{
922 u8 type, *data;
923 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -0700924
925 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 return;
927
928 data = (u8 *) (eap + 1);
929
930 if (len < sizeof(*eap) + 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700931 wpa_printf(MSG_INFO, "%s: too short response data", __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 return;
933 }
934
935 sm->eap_type_supp = type = data[0];
936
937 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
938 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
939 "id=%d len=%d) from STA: EAP Response-%s (%d)",
940 eap->code, eap->identifier, be_to_host16(eap->length),
941 eap_server_get_name(0, type), type);
942
943 sm->dot1xAuthEapolRespFramesRx++;
944
945 wpabuf_free(sm->eap_if->eapRespData);
946 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
Hai Shalome21d4e82020-04-29 16:34:06 -0700947 sm->eapolEap = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948}
949
950
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800951static void handle_eap_initiate(struct hostapd_data *hapd,
952 struct sta_info *sta, struct eap_hdr *eap,
953 size_t len)
954{
955#ifdef CONFIG_ERP
956 u8 type, *data;
957 struct eapol_state_machine *sm = sta->eapol_sm;
958
Hai Shalomc3565922019-10-28 11:58:20 -0700959 if (!sm)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 return;
961
962 if (len < sizeof(*eap) + 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700963 wpa_printf(MSG_INFO, "%s: too short response data", __func__);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800964 return;
965 }
966
967 data = (u8 *) (eap + 1);
968 type = data[0];
969
970 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -0700971 HOSTAPD_LEVEL_DEBUG,
972 "received EAP packet (code=%d id=%d len=%d) from STA: EAP Initiate type %u",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 eap->code, eap->identifier, be_to_host16(eap->length),
974 type);
975
976 wpabuf_free(sm->eap_if->eapRespData);
977 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
Hai Shalome21d4e82020-04-29 16:34:06 -0700978 sm->eapolEap = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800979#endif /* CONFIG_ERP */
980}
981
982
Hai Shalomc3565922019-10-28 11:58:20 -0700983#ifndef CONFIG_NO_STDOUT_DEBUG
984static const char * eap_code_str(u8 code)
985{
986 switch (code) {
987 case EAP_CODE_REQUEST:
988 return "request";
989 case EAP_CODE_RESPONSE:
990 return "response";
991 case EAP_CODE_SUCCESS:
992 return "success";
993 case EAP_CODE_FAILURE:
994 return "failure";
995 case EAP_CODE_INITIATE:
996 return "initiate";
997 case EAP_CODE_FINISH:
998 return "finish";
999 default:
1000 return "unknown";
1001 }
1002}
1003#endif /* CONFIG_NO_STDOUT_DEBUG */
1004
1005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006/* Process incoming EAP packet from Supplicant */
1007static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
1008 u8 *buf, size_t len)
1009{
1010 struct eap_hdr *eap;
1011 u16 eap_len;
1012
1013 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001014 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 return;
1016 }
1017
1018 eap = (struct eap_hdr *) buf;
1019
1020 eap_len = be_to_host16(eap->length);
Hai Shalomc3565922019-10-28 11:58:20 -07001021 wpa_printf(MSG_DEBUG, "EAP: code=%d (%s) identifier=%d length=%d",
1022 eap->code, eap_code_str(eap->code), eap->identifier,
1023 eap_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 if (eap_len < sizeof(*eap)) {
1025 wpa_printf(MSG_DEBUG, " Invalid EAP length");
1026 return;
1027 } else if (eap_len > len) {
Hai Shalomc3565922019-10-28 11:58:20 -07001028 wpa_printf(MSG_DEBUG,
1029 " Too short frame to contain this EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 return;
1031 } else if (eap_len < len) {
Hai Shalomc3565922019-10-28 11:58:20 -07001032 wpa_printf(MSG_DEBUG,
1033 " Ignoring %lu extra bytes after EAP packet",
1034 (unsigned long) len - eap_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 }
1036
1037 switch (eap->code) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 case EAP_CODE_RESPONSE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 handle_eap_response(hapd, sta, eap, eap_len);
1040 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001041 case EAP_CODE_INITIATE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 handle_eap_initiate(hapd, sta, eap, eap_len);
1043 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 }
1045}
1046
1047
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001048struct eapol_state_machine *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
1050{
1051 int flags = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 if (sta->flags & WLAN_STA_PREAUTH)
1054 flags |= EAPOL_SM_PREAUTH;
1055 if (sta->wpa_sm) {
1056 flags |= EAPOL_SM_USES_WPA;
1057 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
1058 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
1059 }
1060 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001061 sta->wps_ie, sta->p2p_ie, sta,
1062 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063}
1064
1065
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001066static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
Sunil8cd6f4d2022-06-28 18:40:46 +00001067 size_t len, enum frame_encryption encrypted)
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001068{
1069 if (sta->pending_eapol_rx) {
1070 wpabuf_free(sta->pending_eapol_rx->buf);
1071 } else {
1072 sta->pending_eapol_rx =
1073 os_malloc(sizeof(*sta->pending_eapol_rx));
1074 if (!sta->pending_eapol_rx)
1075 return;
1076 }
1077
1078 sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
1079 if (!sta->pending_eapol_rx->buf) {
1080 os_free(sta->pending_eapol_rx);
1081 sta->pending_eapol_rx = NULL;
1082 return;
1083 }
1084
Sunil8cd6f4d2022-06-28 18:40:46 +00001085 sta->pending_eapol_rx->encrypted = encrypted;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001086 os_get_reltime(&sta->pending_eapol_rx->rx_time);
1087}
1088
1089
Sunil8cd6f4d2022-06-28 18:40:46 +00001090static bool ieee802_1x_check_encryption(struct sta_info *sta,
1091 enum frame_encryption encrypted,
1092 u8 type)
1093{
1094 if (encrypted != FRAME_NOT_ENCRYPTED)
1095 return true;
1096 if (type != IEEE802_1X_TYPE_EAP_PACKET &&
1097 type != IEEE802_1X_TYPE_EAPOL_START &&
1098 type != IEEE802_1X_TYPE_EAPOL_LOGOFF)
1099 return true;
1100 if (!(sta->flags & WLAN_STA_MFP))
1101 return true;
1102 return !wpa_auth_pairwise_set(sta->wpa_sm);
1103}
1104
1105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106/**
1107 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
1108 * @hapd: hostapd BSS data
1109 * @sa: Source address (sender of the EAPOL frame)
1110 * @buf: EAPOL frame
1111 * @len: Length of buf in octets
Sunil8cd6f4d2022-06-28 18:40:46 +00001112 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 *
1114 * This function is called for each incoming EAPOL frame from the interface
1115 */
1116void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
Sunil8cd6f4d2022-06-28 18:40:46 +00001117 size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118{
1119 struct sta_info *sta;
1120 struct ieee802_1x_hdr *hdr;
1121 struct ieee802_1x_eapol_key *key;
1122 u16 datalen;
1123 struct rsn_pmksa_cache_entry *pmksa;
1124 int key_mgmt;
1125
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001126 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 !hapd->conf->wps_state)
1128 return;
1129
Sunil8cd6f4d2022-06-28 18:40:46 +00001130 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR
1131 " (encrypted=%d)",
1132 (unsigned long) len, MAC2STR(sa), encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001134 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
1135 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001136 wpa_printf(MSG_DEBUG,
1137 "IEEE 802.1X data frame from not associated/Pre-authenticating STA");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001138
1139 if (sta && (sta->flags & WLAN_STA_AUTH)) {
1140 wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
1141 " for later use", MAC2STR(sta->addr));
Sunil8cd6f4d2022-06-28 18:40:46 +00001142 ieee802_1x_save_eapol(sta, buf, len, encrypted);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001143 }
1144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 return;
1146 }
1147
1148 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001149 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 return;
1151 }
1152
1153 hdr = (struct ieee802_1x_hdr *) buf;
1154 datalen = be_to_host16(hdr->length);
1155 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
1156 hdr->version, hdr->type, datalen);
1157
1158 if (len - sizeof(*hdr) < datalen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001159 wpa_printf(MSG_INFO,
1160 " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 if (sta->eapol_sm)
1162 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
1163 return;
1164 }
1165 if (len - sizeof(*hdr) > datalen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001166 wpa_printf(MSG_DEBUG,
1167 " ignoring %lu extra octets after IEEE 802.1X packet",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 (unsigned long) len - sizeof(*hdr) - datalen);
1169 }
1170
1171 if (sta->eapol_sm) {
1172 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
1173 sta->eapol_sm->dot1xAuthEapolFramesRx++;
1174 }
1175
1176 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
1177 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
1178 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
1179 (key->type == EAPOL_KEY_TYPE_WPA ||
1180 key->type == EAPOL_KEY_TYPE_RSN)) {
1181 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
1182 sizeof(*hdr) + datalen);
1183 return;
1184 }
1185
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001186 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001188 wpa_printf(MSG_DEBUG,
1189 "IEEE 802.1X: Ignore EAPOL message - 802.1X not enabled and WPS not used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 return;
1191 }
1192
1193 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001194 if (key_mgmt != -1 &&
1195 (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1196 key_mgmt == WPA_KEY_MGMT_DPP)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001197 wpa_printf(MSG_DEBUG,
1198 "IEEE 802.1X: Ignore EAPOL message - STA is using PSK");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 return;
1200 }
1201
Sunil8cd6f4d2022-06-28 18:40:46 +00001202 if (!ieee802_1x_check_encryption(sta, encrypted, hdr->type)) {
1203 wpa_printf(MSG_DEBUG,
1204 "IEEE 802.1X: Discard unencrypted EAPOL message - encryption was expected");
1205 return;
1206 }
1207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 if (!sta->eapol_sm) {
1209 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1210 if (!sta->eapol_sm)
1211 return;
1212
1213#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001214 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001215 u32 wflags = sta->flags & (WLAN_STA_WPS |
1216 WLAN_STA_WPS2 |
1217 WLAN_STA_MAYBE_WPS);
1218 if (wflags == WLAN_STA_MAYBE_WPS ||
1219 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
1220 /*
1221 * Delay EAPOL frame transmission until a
1222 * possible WPS STA initiates the handshake
1223 * with EAPOL-Start. Only allow the wait to be
1224 * skipped if the STA is known to support WPS
1225 * 2.0.
1226 */
Hai Shalomc3565922019-10-28 11:58:20 -07001227 wpa_printf(MSG_DEBUG,
1228 "WPS: Do not start EAPOL until EAPOL-Start is received");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 }
1232#endif /* CONFIG_WPS */
1233
Hai Shalome21d4e82020-04-29 16:34:06 -07001234 sta->eapol_sm->eap_if->portEnabled = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 }
1236
1237 /* since we support version 1, we can ignore version field and proceed
1238 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1239 /* TODO: actually, we are not version 1 anymore.. However, Version 2
1240 * does not change frame contents, so should be ok to process frames
1241 * more or less identically. Some changes might be needed for
1242 * verification of fields. */
1243
1244 switch (hdr->type) {
1245 case IEEE802_1X_TYPE_EAP_PACKET:
1246 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1247 break;
1248
1249 case IEEE802_1X_TYPE_EAPOL_START:
1250 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001251 HOSTAPD_LEVEL_DEBUG,
1252 "received EAPOL-Start from STA");
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001253#ifdef CONFIG_IEEE80211R_AP
1254 if (hapd->conf->wpa && sta->wpa_sm &&
1255 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
1256 sta->auth_alg == WLAN_AUTH_FT)) {
1257 /* When FT is used, reauthentication to generate a new
1258 * PMK-R0 would be complicated since the current AP
1259 * might not be the one with which the currently used
1260 * PMK-R0 was generated. IEEE Std 802.11-2020, 13.4.2
1261 * (FT initial mobility domain association in an RSN)
1262 * mandates STA to perform a new FT initial mobility
1263 * domain association whenever its Supplicant would
1264 * trigger sending of an EAPOL-Start frame. As such,
1265 * this EAPOL-Start frame should not have been sent.
1266 * Discard it to avoid unexpected behavior. */
1267 hostapd_logger(hapd, sta->addr,
1268 HOSTAPD_MODULE_IEEE8021X,
1269 HOSTAPD_LEVEL_DEBUG,
1270 "discard unexpected EAPOL-Start from STA that uses FT");
1271 break;
1272 }
1273#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1275 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1276 if (pmksa) {
1277 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
Hai Shalomc3565922019-10-28 11:58:20 -07001278 HOSTAPD_LEVEL_DEBUG,
1279 "cached PMKSA available - ignore it since STA sent EAPOL-Start");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1281 }
Hai Shalome21d4e82020-04-29 16:34:06 -07001282 sta->eapol_sm->eapolStart = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1284 eap_server_clear_identity(sta->eapol_sm->eap);
1285 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1286 break;
1287
1288 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1289 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001290 HOSTAPD_LEVEL_DEBUG,
1291 "received EAPOL-Logoff from STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 sta->acct_terminate_cause =
1293 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1294 accounting_sta_stop(hapd, sta);
Hai Shalome21d4e82020-04-29 16:34:06 -07001295 sta->eapol_sm->eapolLogoff = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1297 eap_server_clear_identity(sta->eapol_sm->eap);
1298 break;
1299
1300 case IEEE802_1X_TYPE_EAPOL_KEY:
1301 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1302 if (!ap_sta_is_authorized(sta)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001303 wpa_printf(MSG_DEBUG,
1304 " Dropped key data from unauthorized Supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 break;
1306 }
1307 break;
1308
1309 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1310 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1311 /* TODO: implement support for this; show data */
1312 break;
1313
Hai Shalom81f62d82019-07-22 12:10:00 -07001314#ifdef CONFIG_MACSEC
1315 case IEEE802_1X_TYPE_EAPOL_MKA:
1316 wpa_printf(MSG_EXCESSIVE,
1317 "EAPOL type %d will be handled by MKA", hdr->type);
1318 break;
1319#endif /* CONFIG_MACSEC */
1320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 default:
1322 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1323 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1324 break;
1325 }
1326
1327 eapol_auth_step(sta->eapol_sm);
1328}
1329
1330
1331/**
1332 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1333 * @hapd: hostapd BSS data
1334 * @sta: The station
1335 *
1336 * This function is called to start IEEE 802.1X authentication when a new
1337 * station completes IEEE 802.11 association.
1338 */
1339void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1340{
1341 struct rsn_pmksa_cache_entry *pmksa;
1342 int reassoc = 1;
1343 int force_1x = 0;
1344 int key_mgmt;
1345
1346#ifdef CONFIG_WPS
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001347 if (hapd->conf->wps_state &&
1348 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1349 (sta->flags & WLAN_STA_WPS))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 /*
1351 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1352 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1353 * authentication in this BSS.
1354 */
1355 force_1x = 1;
1356 }
1357#endif /* CONFIG_WPS */
1358
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001359 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001360 wpa_printf(MSG_DEBUG,
1361 "IEEE 802.1X: Ignore STA - 802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001362 /*
1363 * Clear any possible EAPOL authenticator state to support
1364 * reassociation change from WPS to PSK.
1365 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001366 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 return;
1368 }
1369
1370 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001371 if (key_mgmt != -1 &&
1372 (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1373 key_mgmt == WPA_KEY_MGMT_DPP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001375 /*
1376 * Clear any possible EAPOL authenticator state to support
1377 * reassociation change from WPA-EAP to PSK.
1378 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001379 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 return;
1381 }
1382
Hai Shalomc3565922019-10-28 11:58:20 -07001383 if (!sta->eapol_sm) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1385 HOSTAPD_LEVEL_DEBUG, "start authentication");
1386 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
Hai Shalomc3565922019-10-28 11:58:20 -07001387 if (!sta->eapol_sm) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 hostapd_logger(hapd, sta->addr,
1389 HOSTAPD_MODULE_IEEE8021X,
1390 HOSTAPD_LEVEL_INFO,
1391 "failed to allocate state machine");
1392 return;
1393 }
1394 reassoc = 0;
1395 }
1396
1397#ifdef CONFIG_WPS
1398 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001399 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1400 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001402 * Delay EAPOL frame transmission until a possible WPS STA
1403 * initiates the handshake with EAPOL-Start. Only allow the
1404 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 */
Hai Shalomc3565922019-10-28 11:58:20 -07001406 wpa_printf(MSG_DEBUG,
1407 "WPS: Do not start EAPOL until EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1409 }
1410#endif /* CONFIG_WPS */
1411
Hai Shalome21d4e82020-04-29 16:34:06 -07001412 sta->eapol_sm->eap_if->portEnabled = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001414#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 if (sta->auth_alg == WLAN_AUTH_FT) {
1416 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1417 HOSTAPD_LEVEL_DEBUG,
1418 "PMK from FT - skip IEEE 802.1X/EAP");
1419 /* Setup EAPOL state machines to already authenticated state
1420 * because of existing FT information from R0KH. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001421 sta->eapol_sm->keyRun = true;
1422 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1424 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001425 sta->eapol_sm->authSuccess = true;
1426 sta->eapol_sm->authFail = false;
1427 sta->eapol_sm->portValid = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 if (sta->eapol_sm->eap)
1429 eap_sm_notify_cached(sta->eapol_sm->eap);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001430 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 return;
1432 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001433#endif /* CONFIG_IEEE80211R_AP */
1434
1435#ifdef CONFIG_FILS
1436 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1437 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1438 sta->auth_alg == WLAN_AUTH_FILS_PK) {
1439 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1440 HOSTAPD_LEVEL_DEBUG,
1441 "PMK from FILS - skip IEEE 802.1X/EAP");
1442 /* Setup EAPOL state machines to already authenticated state
1443 * because of existing FILS information. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001444 sta->eapol_sm->keyRun = true;
1445 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001446 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1447 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001448 sta->eapol_sm->authSuccess = true;
1449 sta->eapol_sm->authFail = false;
1450 sta->eapol_sm->portValid = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001451 if (sta->eapol_sm->eap)
1452 eap_sm_notify_cached(sta->eapol_sm->eap);
Hai Shalom81f62d82019-07-22 12:10:00 -07001453 wpa_auth_set_ptk_rekey_timer(sta->wpa_sm);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001454 return;
1455 }
1456#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457
1458 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1459 if (pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1461 HOSTAPD_LEVEL_DEBUG,
1462 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1463 /* Setup EAPOL state machines to already authenticated state
1464 * because of existing PMKSA information in the cache. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001465 sta->eapol_sm->keyRun = true;
1466 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1468 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001469 sta->eapol_sm->authSuccess = true;
1470 sta->eapol_sm->authFail = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 if (sta->eapol_sm->eap)
1472 eap_sm_notify_cached(sta->eapol_sm->eap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001473 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001474 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 } else {
1476 if (reassoc) {
1477 /*
1478 * Force EAPOL state machines to start
1479 * re-authentication without having to wait for the
1480 * Supplicant to send EAPOL-Start.
1481 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001482 sta->eapol_sm->reAuthenticate = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 }
1484 eapol_auth_step(sta->eapol_sm);
1485 }
1486}
1487
1488
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001489void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490{
1491 struct eapol_state_machine *sm = sta->eapol_sm;
1492
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001493#ifdef CONFIG_HS20
1494 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1495#endif /* CONFIG_HS20 */
1496
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001497 if (sta->pending_eapol_rx) {
1498 wpabuf_free(sta->pending_eapol_rx->buf);
1499 os_free(sta->pending_eapol_rx);
1500 sta->pending_eapol_rx = NULL;
1501 }
1502
Hai Shalomc3565922019-10-28 11:58:20 -07001503 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 return;
1505
1506 sta->eapol_sm = NULL;
1507
1508#ifndef CONFIG_NO_RADIUS
1509 radius_msg_free(sm->last_recv_radius);
1510 radius_free_class(&sm->radius_class);
1511#endif /* CONFIG_NO_RADIUS */
1512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 eapol_auth_free(sm);
1514}
1515
1516
1517#ifndef CONFIG_NO_RADIUS
1518static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1519 struct sta_info *sta)
1520{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001521 struct wpabuf *eap;
1522 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 int eap_type = -1;
1524 char buf[64];
1525 struct radius_msg *msg;
1526 struct eapol_state_machine *sm = sta->eapol_sm;
1527
Hai Shalomc3565922019-10-28 11:58:20 -07001528 if (!sm || !sm->last_recv_radius) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 if (sm)
Hai Shalome21d4e82020-04-29 16:34:06 -07001530 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 return;
1532 }
1533
1534 msg = sm->last_recv_radius;
1535
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001536 eap = radius_msg_get_eap(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07001537 if (!eap) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 /* RFC 3579, Chap. 2.6.3:
1539 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1540 * attribute */
1541 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001542 HOSTAPD_LEVEL_WARNING,
1543 "could not extract EAP-Message from RADIUS message");
Hai Shalome21d4e82020-04-29 16:34:06 -07001544 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 return;
1546 }
1547
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001548 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001550 HOSTAPD_LEVEL_WARNING,
1551 "too short EAP packet received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001552 wpabuf_free(eap);
Hai Shalome21d4e82020-04-29 16:34:06 -07001553 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 return;
1555 }
1556
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001557 if (wpabuf_len(eap) > sizeof(*hdr))
1558 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001560 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 switch (hdr->code) {
1562 case EAP_CODE_REQUEST:
1563 if (eap_type >= 0)
1564 sm->eap_type_authsrv = eap_type;
1565 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001566 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 break;
1568 case EAP_CODE_RESPONSE:
1569 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001570 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 break;
1572 case EAP_CODE_SUCCESS:
1573 os_strlcpy(buf, "EAP Success", sizeof(buf));
1574 break;
1575 case EAP_CODE_FAILURE:
1576 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1577 break;
1578 default:
1579 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1580 break;
1581 }
1582 buf[sizeof(buf) - 1] = '\0';
1583 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001584 HOSTAPD_LEVEL_DEBUG,
1585 "decapsulated EAP packet (code=%d id=%d len=%d) from RADIUS server: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1587 buf);
Hai Shalome21d4e82020-04-29 16:34:06 -07001588 sm->eap_if->aaaEapReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589
1590 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001591 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592}
1593
1594
1595static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1596 struct sta_info *sta, struct radius_msg *msg,
1597 struct radius_msg *req,
1598 const u8 *shared_secret,
1599 size_t shared_secret_len)
1600{
1601 struct radius_ms_mppe_keys *keys;
Hai Shalom81f62d82019-07-22 12:10:00 -07001602 u8 *buf;
1603 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -07001605
1606 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 return;
1608
1609 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1610 shared_secret_len);
1611
1612 if (keys && keys->send && keys->recv) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001613 len = keys->send_len + keys->recv_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1615 keys->send, keys->send_len);
1616 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1617 keys->recv, keys->recv_len);
1618
1619 os_free(sm->eap_if->aaaEapKeyData);
1620 sm->eap_if->aaaEapKeyData = os_malloc(len);
1621 if (sm->eap_if->aaaEapKeyData) {
1622 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1623 keys->recv_len);
1624 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1625 keys->send, keys->send_len);
1626 sm->eap_if->aaaEapKeyDataLen = len;
Hai Shalome21d4e82020-04-29 16:34:06 -07001627 sm->eap_if->aaaEapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001629 } else {
1630 wpa_printf(MSG_DEBUG,
1631 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1632 keys, keys ? keys->send : NULL,
1633 keys ? keys->recv : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 }
1635
1636 if (keys) {
1637 os_free(keys->send);
1638 os_free(keys->recv);
1639 os_free(keys);
1640 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001641
1642 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
1643 NULL) == 0) {
1644 os_free(sm->eap_if->eapSessionId);
1645 sm->eap_if->eapSessionId = os_memdup(buf, len);
1646 if (sm->eap_if->eapSessionId) {
1647 sm->eap_if->eapSessionIdLen = len;
1648 wpa_hexdump(MSG_DEBUG, "EAP-Key Name",
1649 sm->eap_if->eapSessionId,
1650 sm->eap_if->eapSessionIdLen);
1651 }
1652 } else {
1653 sm->eap_if->eapSessionIdLen = 0;
1654 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655}
1656
1657
1658static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1659 struct sta_info *sta,
1660 struct radius_msg *msg)
1661{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001662 u8 *attr_class;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 size_t class_len;
1664 struct eapol_state_machine *sm = sta->eapol_sm;
1665 int count, i;
1666 struct radius_attr_data *nclass;
1667 size_t nclass_count;
1668
Hai Shalomc3565922019-10-28 11:58:20 -07001669 if (!hapd->conf->radius->acct_server || !hapd->radius || !sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 return;
1671
1672 radius_free_class(&sm->radius_class);
1673 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1674 if (count <= 0)
1675 return;
1676
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001677 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Hai Shalomc3565922019-10-28 11:58:20 -07001678 if (!nclass)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 return;
1680
1681 nclass_count = 0;
1682
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001683 attr_class = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 for (i = 0; i < count; i++) {
1685 do {
1686 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001687 &attr_class, &class_len,
1688 attr_class) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 i = count;
1690 break;
1691 }
1692 } while (class_len < 1);
1693
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001694 nclass[nclass_count].data = os_memdup(attr_class, class_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001695 if (!nclass[nclass_count].data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 break;
1697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 nclass[nclass_count].len = class_len;
1699 nclass_count++;
1700 }
1701
1702 sm->radius_class.attr = nclass;
1703 sm->radius_class.count = nclass_count;
Hai Shalomc3565922019-10-28 11:58:20 -07001704 wpa_printf(MSG_DEBUG,
1705 "IEEE 802.1X: Stored %lu RADIUS Class attributes for "
1706 MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 (unsigned long) sm->radius_class.count,
1708 MAC2STR(sta->addr));
1709}
1710
1711
1712/* Update sta->identity based on User-Name attribute in Access-Accept */
1713static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1714 struct sta_info *sta,
1715 struct radius_msg *msg)
1716{
1717 u8 *buf, *identity;
1718 size_t len;
1719 struct eapol_state_machine *sm = sta->eapol_sm;
1720
Hai Shalomc3565922019-10-28 11:58:20 -07001721 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 return;
1723
1724 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1725 NULL) < 0)
1726 return;
1727
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001728 identity = (u8 *) dup_binstr(buf, len);
Hai Shalomc3565922019-10-28 11:58:20 -07001729 if (!identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 return;
1731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001733 HOSTAPD_LEVEL_DEBUG,
1734 "old identity '%s' updated with User-Name from Access-Accept '%s'",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 sm->identity ? (char *) sm->identity : "N/A",
1736 (char *) identity);
1737
1738 os_free(sm->identity);
1739 sm->identity = identity;
1740 sm->identity_len = len;
1741}
1742
1743
Dmitry Shmidt04949592012-07-19 12:16:46 -07001744/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1745static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1746 struct sta_info *sta,
1747 struct radius_msg *msg)
1748{
1749 struct eapol_state_machine *sm = sta->eapol_sm;
1750 struct wpabuf *cui;
1751 u8 *buf;
1752 size_t len;
1753
Hai Shalomc3565922019-10-28 11:58:20 -07001754 if (!sm)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001755 return;
1756
1757 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1758 &buf, &len, NULL) < 0)
1759 return;
1760
1761 cui = wpabuf_alloc_copy(buf, len);
Hai Shalomc3565922019-10-28 11:58:20 -07001762 if (!cui)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001763 return;
1764
1765 wpabuf_free(sm->radius_cui);
1766 sm->radius_cui = cui;
1767}
1768
1769
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001770#ifdef CONFIG_HS20
1771
1772static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1773{
1774 sta->remediation = 1;
1775 os_free(sta->remediation_url);
1776 if (len > 2) {
1777 sta->remediation_url = os_malloc(len);
1778 if (!sta->remediation_url)
1779 return;
1780 sta->remediation_method = pos[0];
1781 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1782 sta->remediation_url[len - 1] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001783 wpa_printf(MSG_DEBUG,
1784 "HS 2.0: Subscription remediation needed for "
1785 MACSTR " - server method %u URL %s",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001786 MAC2STR(sta->addr), sta->remediation_method,
1787 sta->remediation_url);
1788 } else {
1789 sta->remediation_url = NULL;
Hai Shalomc3565922019-10-28 11:58:20 -07001790 wpa_printf(MSG_DEBUG,
1791 "HS 2.0: Subscription remediation needed for "
1792 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001793 }
1794 /* TODO: assign the STA into remediation VLAN or add filtering */
1795}
1796
1797
1798static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001799 struct sta_info *sta, const u8 *pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001800 size_t len)
1801{
Sunil Ravi77d572f2023-01-17 23:58:31 +00001802 size_t url_len;
1803 unsigned int timeout;
1804
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001805 if (len < 3)
1806 return; /* Malformed information */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001807 url_len = len - 3;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001808 sta->hs20_deauth_requested = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001809 sta->hs20_deauth_on_ack = url_len == 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001810 wpa_printf(MSG_DEBUG,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001811 "HS 2.0: Deauthentication request - Code %u Re-auth Delay %u URL length %zu",
1812 *pos, WPA_GET_LE16(pos + 1), url_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001813 wpabuf_free(sta->hs20_deauth_req);
1814 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1815 if (sta->hs20_deauth_req) {
1816 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
Sunil Ravi77d572f2023-01-17 23:58:31 +00001817 wpabuf_put_u8(sta->hs20_deauth_req, url_len);
1818 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, url_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001819 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001820 timeout = hapd->conf->hs20_deauth_req_timeout;
1821 /* If there is no URL, no need to provide time to fetch it. Use a short
1822 * timeout here to allow maximum time for completing 4-way handshake and
1823 * WNM-Notification delivery. Acknowledgement of the frame will result
1824 * in cutting this wait further. */
1825 if (!url_len && timeout > 2)
1826 timeout = 2;
1827 ap_sta_session_timeout(hapd, sta, timeout);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001828}
1829
1830
1831static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1832 struct sta_info *sta, u8 *pos,
1833 size_t len, int session_timeout)
1834{
1835 unsigned int swt;
1836 int warning_time, beacon_int;
1837
1838 if (len < 1)
1839 return; /* Malformed information */
1840 os_free(sta->hs20_session_info_url);
1841 sta->hs20_session_info_url = os_malloc(len);
Hai Shalomc3565922019-10-28 11:58:20 -07001842 if (!sta->hs20_session_info_url)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001843 return;
1844 swt = pos[0];
1845 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1846 sta->hs20_session_info_url[len - 1] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001847 wpa_printf(MSG_DEBUG,
1848 "HS 2.0: Session Information URL='%s' SWT=%u (session_timeout=%d)",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001849 sta->hs20_session_info_url, swt, session_timeout);
1850 if (session_timeout < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -07001851 wpa_printf(MSG_DEBUG,
1852 "HS 2.0: No Session-Timeout set - ignore session info URL");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001853 return;
1854 }
1855 if (swt == 255)
1856 swt = 1; /* Use one minute as the AP selected value */
1857
1858 if ((unsigned int) session_timeout < swt * 60)
1859 warning_time = 0;
1860 else
1861 warning_time = session_timeout - swt * 60;
1862
1863 beacon_int = hapd->iconf->beacon_int;
1864 if (beacon_int < 1)
1865 beacon_int = 100; /* best guess */
1866 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1867 if (sta->hs20_disassoc_timer > 65535)
1868 sta->hs20_disassoc_timer = 65535;
1869
1870 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1871}
1872
Roshan Pius3a1667e2018-07-03 15:17:14 -07001873
1874static void ieee802_1x_hs20_t_c_filtering(struct hostapd_data *hapd,
1875 struct sta_info *sta, u8 *pos,
1876 size_t len)
1877{
1878 if (len < 4)
1879 return; /* Malformed information */
1880 wpa_printf(MSG_DEBUG,
1881 "HS 2.0: Terms and Conditions filtering %02x %02x %02x %02x",
1882 pos[0], pos[1], pos[2], pos[3]);
1883 hs20_t_c_filtering(hapd, sta, pos[0] & BIT(0));
1884}
1885
1886
1887static void ieee802_1x_hs20_t_c_url(struct hostapd_data *hapd,
1888 struct sta_info *sta, u8 *pos, size_t len)
1889{
1890 os_free(sta->t_c_url);
1891 sta->t_c_url = os_malloc(len + 1);
1892 if (!sta->t_c_url)
1893 return;
1894 os_memcpy(sta->t_c_url, pos, len);
1895 sta->t_c_url[len] = '\0';
1896 wpa_printf(MSG_DEBUG,
1897 "HS 2.0: Terms and Conditions URL %s", sta->t_c_url);
1898}
1899
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001900#endif /* CONFIG_HS20 */
1901
1902
1903static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1904 struct sta_info *sta,
1905 struct radius_msg *msg,
1906 int session_timeout)
1907{
1908#ifdef CONFIG_HS20
1909 u8 *buf, *pos, *end, type, sublen;
1910 size_t len;
1911
1912 buf = NULL;
1913 sta->remediation = 0;
1914 sta->hs20_deauth_requested = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001915 sta->hs20_deauth_on_ack = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001916
1917 for (;;) {
1918 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1919 &buf, &len, buf) < 0)
1920 break;
1921 if (len < 6)
1922 continue;
1923 pos = buf;
1924 end = buf + len;
1925 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1926 continue;
1927 pos += 4;
1928
1929 type = *pos++;
1930 sublen = *pos++;
1931 if (sublen < 2)
1932 continue; /* invalid length */
1933 sublen -= 2; /* skip header */
1934 if (pos + sublen > end)
1935 continue; /* invalid WFA VSA */
1936
1937 switch (type) {
1938 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1939 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1940 break;
1941 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1942 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1943 break;
1944 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1945 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1946 session_timeout);
1947 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001948 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING:
1949 ieee802_1x_hs20_t_c_filtering(hapd, sta, pos, sublen);
1950 break;
1951 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL:
1952 ieee802_1x_hs20_t_c_url(hapd, sta, pos, sublen);
1953 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001954 }
1955 }
1956#endif /* CONFIG_HS20 */
1957}
1958
1959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960struct sta_id_search {
1961 u8 identifier;
1962 struct eapol_state_machine *sm;
1963};
1964
1965
1966static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1967 struct sta_info *sta,
1968 void *ctx)
1969{
1970 struct sta_id_search *id_search = ctx;
1971 struct eapol_state_machine *sm = sta->eapol_sm;
1972
1973 if (sm && sm->radius_identifier >= 0 &&
1974 sm->radius_identifier == id_search->identifier) {
1975 id_search->sm = sm;
1976 return 1;
1977 }
1978 return 0;
1979}
1980
1981
1982static struct eapol_state_machine *
1983ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1984{
1985 struct sta_id_search id_search;
Hai Shalomc3565922019-10-28 11:58:20 -07001986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 id_search.identifier = identifier;
1988 id_search.sm = NULL;
1989 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1990 return id_search.sm;
1991}
1992
1993
Hai Shalom74f70d42019-02-11 14:42:39 -08001994#ifndef CONFIG_NO_VLAN
1995static int ieee802_1x_update_vlan(struct radius_msg *msg,
1996 struct hostapd_data *hapd,
1997 struct sta_info *sta)
1998{
1999 struct vlan_description vlan_desc;
2000
2001 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
2002 vlan_desc.notempty = !!radius_msg_get_vlanid(msg, &vlan_desc.untagged,
2003 MAX_NUM_TAGGED_VLAN,
2004 vlan_desc.tagged);
2005
2006 if (vlan_desc.notempty &&
2007 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
Hai Shalome21d4e82020-04-29 16:34:06 -07002008 sta->eapol_sm->authFail = true;
Hai Shalom74f70d42019-02-11 14:42:39 -08002009 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
2010 HOSTAPD_LEVEL_INFO,
2011 "Invalid VLAN %d%s received from RADIUS server",
2012 vlan_desc.untagged,
2013 vlan_desc.tagged[0] ? "+" : "");
2014 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
2015 ap_sta_set_vlan(hapd, sta, &vlan_desc);
2016 return -1;
2017 }
2018
2019 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
2020 !vlan_desc.notempty) {
Hai Shalome21d4e82020-04-29 16:34:06 -07002021 sta->eapol_sm->authFail = true;
Hai Shalom74f70d42019-02-11 14:42:39 -08002022 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2023 HOSTAPD_LEVEL_INFO,
2024 "authentication server did not include required VLAN ID in Access-Accept");
2025 return -1;
2026 }
2027
2028 return ap_sta_set_vlan(hapd, sta, &vlan_desc);
2029}
2030#endif /* CONFIG_NO_VLAN */
2031
2032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033/**
2034 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
2035 * @msg: RADIUS response message
2036 * @req: RADIUS request message
2037 * @shared_secret: RADIUS shared secret
2038 * @shared_secret_len: Length of shared_secret in octets
2039 * @data: Context data (struct hostapd_data *)
2040 * Returns: Processing status
2041 */
2042static RadiusRxResult
2043ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
2044 const u8 *shared_secret, size_t shared_secret_len,
2045 void *data)
2046{
2047 struct hostapd_data *hapd = data;
2048 struct sta_info *sta;
2049 u32 session_timeout = 0, termination_action, acct_interim_interval;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002050 int session_timeout_set;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002051 u32 reason_code;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 struct eapol_state_machine *sm;
2053 int override_eapReq = 0;
2054 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
2055
2056 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
Hai Shalomc3565922019-10-28 11:58:20 -07002057 if (!sm) {
2058 wpa_printf(MSG_DEBUG,
2059 "IEEE 802.1X: Could not find matching station for this RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 return RADIUS_RX_UNKNOWN;
2061 }
2062 sta = sm->sta;
2063
Sunil Ravi7f769292024-07-23 22:21:32 +00002064 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 1)) {
Hai Shalomc3565922019-10-28 11:58:20 -07002065 wpa_printf(MSG_INFO,
2066 "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 return RADIUS_RX_INVALID_AUTHENTICATOR;
2068 }
2069
2070 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
2071 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
2072 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002073 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 return RADIUS_RX_UNKNOWN;
2075 }
2076
2077 sm->radius_identifier = -1;
2078 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
2079 MAC2STR(sta->addr));
2080
2081 radius_msg_free(sm->last_recv_radius);
2082 sm->last_recv_radius = msg;
2083
2084 session_timeout_set =
2085 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
2086 &session_timeout);
2087 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
2088 &termination_action))
2089 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
2090
2091 if (hapd->conf->acct_interim_interval == 0 &&
2092 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
2093 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
2094 &acct_interim_interval) == 0) {
2095 if (acct_interim_interval < 60) {
2096 hostapd_logger(hapd, sta->addr,
2097 HOSTAPD_MODULE_IEEE8021X,
2098 HOSTAPD_LEVEL_INFO,
Hai Shalomc3565922019-10-28 11:58:20 -07002099 "ignored too small Acct-Interim-Interval %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 acct_interim_interval);
2101 } else
2102 sta->acct_interim_interval = acct_interim_interval;
2103 }
2104
2105
2106 switch (hdr->code) {
2107 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108#ifndef CONFIG_NO_VLAN
Hai Shalom74f70d42019-02-11 14:42:39 -08002109 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
2110 ieee802_1x_update_vlan(msg, hapd, sta) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002111 break;
2112
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002113 if (sta->vlan_id > 0) {
2114 hostapd_logger(hapd, sta->addr,
2115 HOSTAPD_MODULE_RADIUS,
2116 HOSTAPD_LEVEL_INFO,
2117 "VLAN ID %d", sta->vlan_id);
2118 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002119
Dmitry Shmidt83474442015-04-15 13:47:09 -07002120 if ((sta->flags & WLAN_STA_ASSOC) &&
2121 ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08002123#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002125 sta->session_timeout_set = !!session_timeout_set;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002126 os_get_reltime(&sta->session_timeout);
2127 sta->session_timeout.sec += session_timeout;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 /* RFC 3580, Ch. 3.17 */
2130 if (session_timeout_set && termination_action ==
Roshan Pius3a1667e2018-07-03 15:17:14 -07002131 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 sm->reAuthPeriod = session_timeout;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002133 else if (session_timeout_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 ap_sta_session_timeout(hapd, sta, session_timeout);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002135 else
2136 ap_sta_no_session_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137
Hai Shalome21d4e82020-04-29 16:34:06 -07002138 sm->eap_if->aaaSuccess = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 override_eapReq = 1;
2140 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
2141 shared_secret_len);
2142 ieee802_1x_store_radius_class(hapd, sta, msg);
2143 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002144 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002145 ieee802_1x_check_hs20(hapd, sta, msg,
2146 session_timeout_set ?
2147 (int) session_timeout : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 break;
2149 case RADIUS_CODE_ACCESS_REJECT:
Hai Shalome21d4e82020-04-29 16:34:06 -07002150 sm->eap_if->aaaFail = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 override_eapReq = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002152 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_WLAN_REASON_CODE,
2153 &reason_code) == 0) {
2154 wpa_printf(MSG_DEBUG,
2155 "RADIUS server indicated WLAN-Reason-Code %u in Access-Reject for "
2156 MACSTR, reason_code, MAC2STR(sta->addr));
2157 sta->disconnect_reason_code = reason_code;
2158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 break;
2160 case RADIUS_CODE_ACCESS_CHALLENGE:
Hai Shalome21d4e82020-04-29 16:34:06 -07002161 sm->eap_if->aaaEapReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 if (session_timeout_set) {
2163 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
2164 sm->eap_if->aaaMethodTimeout = session_timeout;
2165 hostapd_logger(hapd, sm->addr,
2166 HOSTAPD_MODULE_IEEE8021X,
2167 HOSTAPD_LEVEL_DEBUG,
Hai Shalomc3565922019-10-28 11:58:20 -07002168 "using EAP timeout of %d seconds (from RADIUS)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 sm->eap_if->aaaMethodTimeout);
2170 } else {
2171 /*
2172 * Use dynamic retransmission behavior per EAP
2173 * specification.
2174 */
2175 sm->eap_if->aaaMethodTimeout = 0;
2176 }
2177 break;
2178 }
2179
2180 ieee802_1x_decapsulate_radius(hapd, sta);
2181 if (override_eapReq)
Hai Shalome21d4e82020-04-29 16:34:06 -07002182 sm->eap_if->aaaEapReq = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002184#ifdef CONFIG_FILS
2185#ifdef NEED_AP_MLME
Hai Shalom60840252021-02-19 19:02:11 -08002186 if (sta->flags &
2187 (WLAN_STA_PENDING_FILS_ERP | WLAN_STA_PENDING_PASN_FILS_ERP)) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002188 /* TODO: Add a PMKSA entry on success? */
2189 ieee802_11_finish_fils_auth(
2190 hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
2191 sm->eap_if->aaaEapReqData,
2192 sm->eap_if->aaaEapKeyData,
2193 sm->eap_if->aaaEapKeyDataLen);
2194 }
2195#endif /* NEED_AP_MLME */
2196#endif /* CONFIG_FILS */
2197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 eapol_auth_step(sm);
2199
2200 return RADIUS_RX_QUEUED;
2201}
2202#endif /* CONFIG_NO_RADIUS */
2203
2204
2205void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
2206{
2207 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -07002208
2209 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002210 return;
2211
2212 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2213 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
2214
2215#ifndef CONFIG_NO_RADIUS
2216 radius_msg_free(sm->last_recv_radius);
2217 sm->last_recv_radius = NULL;
2218#endif /* CONFIG_NO_RADIUS */
2219
2220 if (sm->eap_if->eapTimeout) {
2221 /*
2222 * Disconnect the STA since it did not reply to the last EAP
2223 * request and we cannot continue EAP processing (EAP-Failure
2224 * could only be sent if the EAP peer actually replied).
2225 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002226 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
2227 MAC2STR(sta->addr));
2228
Hai Shalome21d4e82020-04-29 16:34:06 -07002229 sm->eap_if->portEnabled = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 ap_sta_disconnect(hapd, sta, sta->addr,
2231 WLAN_REASON_PREV_AUTH_NOT_VALID);
2232 }
2233}
2234
2235
Hai Shalomfdcde762020-04-02 11:19:20 -07002236#ifdef CONFIG_WEP
2237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
2239{
2240 struct eapol_authenticator *eapol = hapd->eapol_auth;
2241
2242 if (hapd->conf->default_wep_key_len < 1)
2243 return 0;
2244
2245 os_free(eapol->default_wep_key);
2246 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002247 if (!eapol->default_wep_key ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 random_get_bytes(eapol->default_wep_key,
2249 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002250 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 os_free(eapol->default_wep_key);
2252 eapol->default_wep_key = NULL;
2253 return -1;
2254 }
2255
2256 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
2257 eapol->default_wep_key,
2258 hapd->conf->default_wep_key_len);
2259
2260 return 0;
2261}
2262
2263
2264static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
2265 struct sta_info *sta, void *ctx)
2266{
2267 if (sta->eapol_sm) {
Hai Shalome21d4e82020-04-29 16:34:06 -07002268 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 eapol_auth_step(sta->eapol_sm);
2270 }
2271 return 0;
2272}
2273
2274
2275static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
2276{
2277 struct hostapd_data *hapd = eloop_ctx;
2278 struct eapol_authenticator *eapol = hapd->eapol_auth;
2279
2280 if (eapol->default_wep_key_idx >= 3)
2281 eapol->default_wep_key_idx =
2282 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
2283 else
2284 eapol->default_wep_key_idx++;
2285
2286 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
2287 eapol->default_wep_key_idx);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 if (ieee802_1x_rekey_broadcast(hapd)) {
2290 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002291 HOSTAPD_LEVEL_WARNING,
2292 "failed to generate a new broadcast key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 os_free(eapol->default_wep_key);
2294 eapol->default_wep_key = NULL;
2295 return;
2296 }
2297
2298 /* TODO: Could setup key for RX here, but change default TX keyid only
2299 * after new broadcast key has been sent to all stations. */
2300 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
2301 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002302 eapol->default_wep_key_idx, 0, 1, NULL, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 eapol->default_wep_key,
Hai Shalomfdcde762020-04-02 11:19:20 -07002304 hapd->conf->default_wep_key_len,
2305 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002307 HOSTAPD_LEVEL_WARNING,
2308 "failed to configure a new broadcast key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 os_free(eapol->default_wep_key);
2310 eapol->default_wep_key = NULL;
2311 return;
2312 }
2313
2314 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
2315
2316 if (hapd->conf->wep_rekeying_period > 0) {
2317 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
2318 ieee802_1x_rekey, hapd, NULL);
2319 }
2320}
2321
Hai Shalomfdcde762020-04-02 11:19:20 -07002322#endif /* CONFIG_WEP */
2323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324
2325static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2326 const u8 *data, size_t datalen)
2327{
2328#ifdef CONFIG_WPS
2329 struct sta_info *sta = sta_ctx;
2330
2331 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2332 WLAN_STA_MAYBE_WPS) {
2333 const u8 *identity;
2334 size_t identity_len;
2335 struct eapol_state_machine *sm = sta->eapol_sm;
2336
2337 identity = eap_get_identity(sm->eap, &identity_len);
2338 if (identity &&
2339 ((identity_len == WSC_ID_ENROLLEE_LEN &&
2340 os_memcmp(identity, WSC_ID_ENROLLEE,
2341 WSC_ID_ENROLLEE_LEN) == 0) ||
2342 (identity_len == WSC_ID_REGISTRAR_LEN &&
2343 os_memcmp(identity, WSC_ID_REGISTRAR,
2344 WSC_ID_REGISTRAR_LEN) == 0))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002345 wpa_printf(MSG_DEBUG,
2346 "WPS: WLAN_STA_MAYBE_WPS -> WLAN_STA_WPS");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 sta->flags |= WLAN_STA_WPS;
2348 }
2349 }
2350#endif /* CONFIG_WPS */
2351
2352 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2353}
2354
2355
2356static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2357 const u8 *data, size_t datalen)
2358{
2359#ifndef CONFIG_NO_RADIUS
2360 struct hostapd_data *hapd = ctx;
2361 struct sta_info *sta = sta_ctx;
2362
2363 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2364#endif /* CONFIG_NO_RADIUS */
2365}
2366
2367
Sunil Ravi640215c2023-06-28 23:08:09 +00002368static bool _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2369 int preauth, int remediation, bool logoff)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370{
2371 struct hostapd_data *hapd = ctx;
2372 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002373
Sunil Ravi640215c2023-06-28 23:08:09 +00002374 if (preauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375 rsn_preauth_finished(hapd, sta, success);
Sunil Ravi640215c2023-06-28 23:08:09 +00002376 return false;
2377 }
2378
2379 return ieee802_1x_finished(hapd, sta, success, remediation, logoff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380}
2381
2382
2383static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2384 size_t identity_len, int phase2,
2385 struct eap_user *user)
2386{
2387 struct hostapd_data *hapd = ctx;
2388 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002389 int i;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002390 int rv = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002392 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002393 if (!eap_user)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002394 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395
2396 os_memset(user, 0, sizeof(*user));
2397 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002398 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 user->methods[i].vendor = eap_user->methods[i].vendor;
2400 user->methods[i].method = eap_user->methods[i].method;
2401 }
2402
2403 if (eap_user->password) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002404 user->password = os_memdup(eap_user->password,
2405 eap_user->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002406 if (!user->password)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002407 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002409 user->password_hash = eap_user->password_hash;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002410 if (eap_user->salt && eap_user->salt_len) {
2411 user->salt = os_memdup(eap_user->salt,
2412 eap_user->salt_len);
2413 if (!user->salt)
2414 goto out;
2415 user->salt_len = eap_user->salt_len;
2416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 }
2418 user->force_version = eap_user->force_version;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002419 user->macacl = eap_user->macacl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002421 user->remediation = eap_user->remediation;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002422 rv = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002424out:
2425 if (rv)
2426 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2427
2428 return rv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429}
2430
2431
2432static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2433{
2434 struct hostapd_data *hapd = ctx;
2435 struct sta_info *sta;
Hai Shalomc3565922019-10-28 11:58:20 -07002436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 sta = ap_get_sta(hapd, addr);
Hai Shalomc3565922019-10-28 11:58:20 -07002438 if (!sta || !sta->eapol_sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 return 0;
2440 return 1;
2441}
2442
2443
2444static void ieee802_1x_logger(void *ctx, const u8 *addr,
2445 eapol_logger_level level, const char *txt)
2446{
2447#ifndef CONFIG_NO_HOSTAPD_LOGGER
2448 struct hostapd_data *hapd = ctx;
2449 int hlevel;
2450
2451 switch (level) {
2452 case EAPOL_LOGGER_WARNING:
2453 hlevel = HOSTAPD_LEVEL_WARNING;
2454 break;
2455 case EAPOL_LOGGER_INFO:
2456 hlevel = HOSTAPD_LEVEL_INFO;
2457 break;
2458 case EAPOL_LOGGER_DEBUG:
2459 default:
2460 hlevel = HOSTAPD_LEVEL_DEBUG;
2461 break;
2462 }
2463
2464 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2465 txt);
2466#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2467}
2468
2469
2470static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2471 int authorized)
2472{
2473 struct hostapd_data *hapd = ctx;
2474 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002475
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2477}
2478
2479
2480static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2481{
2482 struct hostapd_data *hapd = ctx;
2483 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 ieee802_1x_abort_auth(hapd, sta);
2486}
2487
2488
Hai Shalomfdcde762020-04-02 11:19:20 -07002489#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2491{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002492#ifndef CONFIG_FIPS
2493#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 struct hostapd_data *hapd = ctx;
2495 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 ieee802_1x_tx_key(hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002498#endif /* CONFIG_NO_RC4 */
2499#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500}
Hai Shalomfdcde762020-04-02 11:19:20 -07002501#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502
2503
2504static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2505 enum eapol_event type)
2506{
2507 /* struct hostapd_data *hapd = ctx; */
2508 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 switch (type) {
2511 case EAPOL_AUTH_SM_CHANGE:
2512 wpa_auth_sm_notify(sta->wpa_sm);
2513 break;
2514 case EAPOL_AUTH_REAUTHENTICATE:
2515 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2516 break;
2517 }
2518}
2519
2520
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002521#ifdef CONFIG_ERP
2522
2523static struct eap_server_erp_key *
2524ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2525{
2526 struct hostapd_data *hapd = ctx;
2527 struct eap_server_erp_key *erp;
2528
2529 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2530 list) {
2531 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2532 return erp;
2533 }
2534
2535 return NULL;
2536}
2537
2538
2539static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2540{
2541 struct hostapd_data *hapd = ctx;
2542
2543 dl_list_add(&hapd->erp_keys, &erp->list);
2544 return 0;
2545}
2546
2547#endif /* CONFIG_ERP */
2548
2549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550int ieee802_1x_init(struct hostapd_data *hapd)
2551{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552 struct eapol_auth_config conf;
2553 struct eapol_auth_cb cb;
2554
Sunil Ravi99c035e2024-07-12 01:42:03 +00002555#ifdef CONFIG_IEEE80211BE
2556 if (!hostapd_mld_is_first_bss(hapd)) {
2557 struct hostapd_data *first;
2558
Sunil Ravi99c035e2024-07-12 01:42:03 +00002559 first = hostapd_mld_get_first_bss(hapd);
2560 if (!first)
2561 return -1;
Sunil Ravi7f769292024-07-23 22:21:32 +00002562
2563 if (!first->eapol_auth) {
2564 wpa_printf(MSG_DEBUG,
2565 "MLD: First BSS IEEE 802.1X state machine does not exist. Init on its behalf");
2566
2567 if (ieee802_1x_init(first))
2568 return -1;
2569 }
2570
2571 wpa_printf(MSG_DEBUG,
2572 "MLD: Using IEEE 802.1X state machine of the first BSS");
2573
Sunil Ravi99c035e2024-07-12 01:42:03 +00002574 hapd->eapol_auth = first->eapol_auth;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002575 return 0;
2576 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00002577#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002578
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002579 dl_list_init(&hapd->erp_keys);
2580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 os_memset(&conf, 0, sizeof(conf));
Hai Shalomc3565922019-10-28 11:58:20 -07002582 conf.eap_cfg = hapd->eap_cfg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 conf.ctx = hapd;
2584 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2585 conf.wpa = hapd->conf->wpa;
Hai Shalomfdcde762020-04-02 11:19:20 -07002586#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
Hai Shalomfdcde762020-04-02 11:19:20 -07002588#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2590 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002591 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2592 conf.erp_domain = hapd->conf->erp_domain;
Sunil Ravia04bd252022-05-02 22:54:18 -07002593#ifdef CONFIG_TESTING_OPTIONS
2594 conf.eap_skip_prot_success = hapd->conf->eap_skip_prot_success;
2595#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596
2597 os_memset(&cb, 0, sizeof(cb));
2598 cb.eapol_send = ieee802_1x_eapol_send;
2599 cb.aaa_send = ieee802_1x_aaa_send;
2600 cb.finished = _ieee802_1x_finished;
2601 cb.get_eap_user = ieee802_1x_get_eap_user;
2602 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2603 cb.logger = ieee802_1x_logger;
2604 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2605 cb.abort_auth = _ieee802_1x_abort_auth;
Hai Shalomfdcde762020-04-02 11:19:20 -07002606#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 cb.tx_key = _ieee802_1x_tx_key;
Hai Shalomfdcde762020-04-02 11:19:20 -07002608#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 cb.eapol_event = ieee802_1x_eapol_event;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002610#ifdef CONFIG_ERP
2611 cb.erp_get_key = ieee802_1x_erp_get_key;
2612 cb.erp_add_key = ieee802_1x_erp_add_key;
2613#endif /* CONFIG_ERP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614
2615 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
Hai Shalomc3565922019-10-28 11:58:20 -07002616 if (!hapd->eapol_auth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617 return -1;
2618
2619 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2620 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2621 return -1;
2622
2623#ifndef CONFIG_NO_RADIUS
2624 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2625 ieee802_1x_receive_auth, hapd))
2626 return -1;
2627#endif /* CONFIG_NO_RADIUS */
2628
Hai Shalomfdcde762020-04-02 11:19:20 -07002629#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 if (hapd->conf->default_wep_key_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002631 int i;
2632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633 for (i = 0; i < 4; i++)
2634 hostapd_drv_set_key(hapd->conf->iface, hapd,
Hai Shalomfdcde762020-04-02 11:19:20 -07002635 WPA_ALG_NONE, NULL, i, 0, 0, NULL,
2636 0, NULL, 0, KEY_FLAG_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637
2638 ieee802_1x_rekey(hapd, NULL);
2639
Hai Shalomc3565922019-10-28 11:58:20 -07002640 if (!hapd->eapol_auth->default_wep_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 return -1;
2642 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002643#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644
2645 return 0;
2646}
2647
2648
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002649void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2650{
2651 struct eap_server_erp_key *erp;
2652
2653 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2654 list)) != NULL) {
2655 dl_list_del(&erp->list);
2656 bin_clear_free(erp, sizeof(*erp));
2657 }
2658}
2659
2660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661void ieee802_1x_deinit(struct hostapd_data *hapd)
2662{
Sunil Ravi99c035e2024-07-12 01:42:03 +00002663#ifdef CONFIG_IEEE80211BE
2664 if (!hostapd_mld_is_first_bss(hapd)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002665 wpa_printf(MSG_DEBUG,
2666 "MLD: Deinit IEEE 802.1X state machine of a non-first BSS");
2667
2668 hapd->eapol_auth = NULL;
2669 return;
2670 }
Sunil Ravi99c035e2024-07-12 01:42:03 +00002671#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002672
Hai Shalomfdcde762020-04-02 11:19:20 -07002673#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07002675#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002677 if (hapd->driver && hapd->drv_priv &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2679 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2680
2681 eapol_auth_deinit(hapd->eapol_auth);
2682 hapd->eapol_auth = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002683
2684 ieee802_1x_erp_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685}
2686
2687
2688int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2689 const u8 *buf, size_t len, int ack)
2690{
2691 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 u8 *pos;
2693 const unsigned char rfc1042_hdr[ETH_ALEN] =
2694 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2695
Hai Shalomc3565922019-10-28 11:58:20 -07002696 if (!sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002698 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 return 0;
2700
2701 hdr = (struct ieee80211_hdr *) buf;
2702 pos = (u8 *) (hdr + 1);
2703 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2704 return 0;
2705 pos += sizeof(rfc1042_hdr);
2706 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2707 return 0;
2708 pos += 2;
2709
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002710 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2711 ack);
2712}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002714
2715int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2716 const u8 *buf, int len, int ack)
2717{
2718 const struct ieee802_1x_hdr *xhdr =
2719 (const struct ieee802_1x_hdr *) buf;
2720 const u8 *pos = buf + sizeof(*xhdr);
2721 struct ieee802_1x_eapol_key *key;
2722
2723 if (len < (int) sizeof(*xhdr))
2724 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -07002725 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
2726 " TX status - version=%d type=%d length=%d - ack=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2728 be_to_host16(xhdr->length), ack);
2729
Dmitry Shmidt29333592017-01-09 12:27:11 -08002730#ifdef CONFIG_WPS
2731 if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2732 (sta->flags & WLAN_STA_WPS) &&
2733 ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2734 wpa_printf(MSG_DEBUG,
2735 "WPS: Indicate EAP completion on ACK for EAP-Failure");
2736 hostapd_wps_eap_completed(hapd);
2737 }
2738#endif /* CONFIG_WPS */
2739
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002740 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2741 return 0;
2742
2743 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 const struct wpa_eapol_key *wpa;
Hai Shalomc3565922019-10-28 11:58:20 -07002745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 wpa = (const struct wpa_eapol_key *) pos;
2747 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2748 wpa->type == EAPOL_KEY_TYPE_WPA)
2749 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2750 sta->wpa_sm, ack);
2751 }
2752
2753 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2754 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002755 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 * packets couple of times because otherwise STA keys become
2757 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002758 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759 key = (struct ieee802_1x_eapol_key *) pos;
2760 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002761 HOSTAPD_LEVEL_DEBUG,
2762 "did not Ack EAPOL-Key frame (%scast index=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 key->key_index & BIT(7) ? "uni" : "broad",
2764 key->key_index & ~BIT(7));
2765 /* TODO: re-send EAPOL-Key couple of times (with short delay
2766 * between them?). If all attempt fail, report error and
2767 * deauthenticate STA so that it will get new keys when
2768 * authenticating again (e.g., after returning in range).
2769 * Separate limit/transmit state needed both for unicast and
2770 * broadcast keys(?) */
2771 }
2772 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2773 * to here and change the key only if the EAPOL-Key packet was Acked.
2774 */
2775
2776 return 1;
2777}
2778
2779
2780u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2781{
Hai Shalomc3565922019-10-28 11:58:20 -07002782 if (!sm || !sm->identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 return NULL;
2784
2785 *len = sm->identity_len;
2786 return sm->identity;
2787}
2788
2789
2790u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2791 int idx)
2792{
Hai Shalomc3565922019-10-28 11:58:20 -07002793 if (!sm || !sm->radius_class.attr ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 idx >= (int) sm->radius_class.count)
2795 return NULL;
2796
2797 *len = sm->radius_class.attr[idx].len;
2798 return sm->radius_class.attr[idx].data;
2799}
2800
2801
Dmitry Shmidt04949592012-07-19 12:16:46 -07002802struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2803{
Hai Shalomc3565922019-10-28 11:58:20 -07002804 if (!sm)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002805 return NULL;
2806 return sm->radius_cui;
2807}
2808
2809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2811{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002812 *len = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07002813 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 return NULL;
2815
2816 *len = sm->eap_if->eapKeyDataLen;
2817 return sm->eap_if->eapKeyData;
2818}
2819
2820
Hai Shalom81f62d82019-07-22 12:10:00 -07002821#ifdef CONFIG_MACSEC
2822const u8 * ieee802_1x_get_session_id(struct eapol_state_machine *sm,
2823 size_t *len)
2824{
2825 *len = 0;
2826 if (!sm || !sm->eap_if)
2827 return NULL;
2828
2829 *len = sm->eap_if->eapSessionIdLen;
2830 return sm->eap_if->eapSessionId;
2831}
2832#endif /* CONFIG_MACSEC */
2833
2834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
Hai Shalome21d4e82020-04-29 16:34:06 -07002836 bool enabled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837{
Hai Shalomc3565922019-10-28 11:58:20 -07002838 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839 return;
Hai Shalome21d4e82020-04-29 16:34:06 -07002840 sm->eap_if->portEnabled = enabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 eapol_auth_step(sm);
2842}
2843
2844
Hai Shalome21d4e82020-04-29 16:34:06 -07002845void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm, bool valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846{
Hai Shalomc3565922019-10-28 11:58:20 -07002847 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848 return;
Hai Shalome21d4e82020-04-29 16:34:06 -07002849 sm->portValid = valid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 eapol_auth_step(sm);
2851}
2852
2853
Hai Shalome21d4e82020-04-29 16:34:06 -07002854void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, bool pre_auth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855{
Hai Shalomc3565922019-10-28 11:58:20 -07002856 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 return;
2858 if (pre_auth)
2859 sm->flags |= EAPOL_SM_PREAUTH;
2860 else
2861 sm->flags &= ~EAPOL_SM_PREAUTH;
2862}
2863
2864
Hai Shalome21d4e82020-04-29 16:34:06 -07002865static const char * bool_txt(bool val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002867 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868}
2869
2870
2871int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2872{
2873 /* TODO */
2874 return 0;
2875}
2876
2877
2878int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2879 char *buf, size_t buflen)
2880{
2881 int len = 0, ret;
2882 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002883 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002884 const char *name1;
2885 const char *name2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002886 char *identity_buf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887
Hai Shalomc3565922019-10-28 11:58:20 -07002888 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 return 0;
2890
2891 ret = os_snprintf(buf + len, buflen - len,
2892 "dot1xPaePortNumber=%d\n"
2893 "dot1xPaePortProtocolVersion=%d\n"
2894 "dot1xPaePortCapabilities=1\n"
2895 "dot1xPaePortInitialize=%d\n"
2896 "dot1xPaePortReauthenticate=FALSE\n",
2897 sta->aid,
2898 EAPOL_VERSION,
2899 sm->initialize);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002900 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 return len;
2902 len += ret;
2903
2904 /* dot1xAuthConfigTable */
2905 ret = os_snprintf(buf + len, buflen - len,
2906 "dot1xAuthPaeState=%d\n"
2907 "dot1xAuthBackendAuthState=%d\n"
2908 "dot1xAuthAdminControlledDirections=%d\n"
2909 "dot1xAuthOperControlledDirections=%d\n"
2910 "dot1xAuthAuthControlledPortStatus=%d\n"
2911 "dot1xAuthAuthControlledPortControl=%d\n"
2912 "dot1xAuthQuietPeriod=%u\n"
2913 "dot1xAuthServerTimeout=%u\n"
2914 "dot1xAuthReAuthPeriod=%u\n"
2915 "dot1xAuthReAuthEnabled=%s\n"
2916 "dot1xAuthKeyTxEnabled=%s\n",
2917 sm->auth_pae_state + 1,
2918 sm->be_auth_state + 1,
2919 sm->adminControlledDirections,
2920 sm->operControlledDirections,
2921 sm->authPortStatus,
2922 sm->portControl,
2923 sm->quietPeriod,
2924 sm->serverTimeout,
2925 sm->reAuthPeriod,
2926 bool_txt(sm->reAuthEnabled),
2927 bool_txt(sm->keyTxEnabled));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002928 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 return len;
2930 len += ret;
2931
2932 /* dot1xAuthStatsTable */
2933 ret = os_snprintf(buf + len, buflen - len,
2934 "dot1xAuthEapolFramesRx=%u\n"
2935 "dot1xAuthEapolFramesTx=%u\n"
2936 "dot1xAuthEapolStartFramesRx=%u\n"
2937 "dot1xAuthEapolLogoffFramesRx=%u\n"
2938 "dot1xAuthEapolRespIdFramesRx=%u\n"
2939 "dot1xAuthEapolRespFramesRx=%u\n"
2940 "dot1xAuthEapolReqIdFramesTx=%u\n"
2941 "dot1xAuthEapolReqFramesTx=%u\n"
2942 "dot1xAuthInvalidEapolFramesRx=%u\n"
2943 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2944 "dot1xAuthLastEapolFrameVersion=%u\n"
2945 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2946 sm->dot1xAuthEapolFramesRx,
2947 sm->dot1xAuthEapolFramesTx,
2948 sm->dot1xAuthEapolStartFramesRx,
2949 sm->dot1xAuthEapolLogoffFramesRx,
2950 sm->dot1xAuthEapolRespIdFramesRx,
2951 sm->dot1xAuthEapolRespFramesRx,
2952 sm->dot1xAuthEapolReqIdFramesTx,
2953 sm->dot1xAuthEapolReqFramesTx,
2954 sm->dot1xAuthInvalidEapolFramesRx,
2955 sm->dot1xAuthEapLengthErrorFramesRx,
2956 sm->dot1xAuthLastEapolFrameVersion,
2957 MAC2STR(sm->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002958 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 return len;
2960 len += ret;
2961
2962 /* dot1xAuthDiagTable */
2963 ret = os_snprintf(buf + len, buflen - len,
2964 "dot1xAuthEntersConnecting=%u\n"
2965 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2966 "dot1xAuthEntersAuthenticating=%u\n"
2967 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2968 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2969 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2970 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2971 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2972 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2973 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2974 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2975 "dot1xAuthBackendResponses=%u\n"
2976 "dot1xAuthBackendAccessChallenges=%u\n"
2977 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2978 "dot1xAuthBackendAuthSuccesses=%u\n"
2979 "dot1xAuthBackendAuthFails=%u\n",
2980 sm->authEntersConnecting,
2981 sm->authEapLogoffsWhileConnecting,
2982 sm->authEntersAuthenticating,
2983 sm->authAuthSuccessesWhileAuthenticating,
2984 sm->authAuthTimeoutsWhileAuthenticating,
2985 sm->authAuthFailWhileAuthenticating,
2986 sm->authAuthEapStartsWhileAuthenticating,
2987 sm->authAuthEapLogoffWhileAuthenticating,
2988 sm->authAuthReauthsWhileAuthenticated,
2989 sm->authAuthEapStartsWhileAuthenticated,
2990 sm->authAuthEapLogoffWhileAuthenticated,
2991 sm->backendResponses,
2992 sm->backendAccessChallenges,
2993 sm->backendOtherRequestsToSupplicant,
2994 sm->backendAuthSuccesses,
2995 sm->backendAuthFails);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002996 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 return len;
2998 len += ret;
2999
3000 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003001 os_reltime_age(&sta->acct_session_start, &diff);
Hai Shalom74f70d42019-02-11 14:42:39 -08003002 if (sm->eap && !sm->identity) {
3003 const u8 *id;
3004 size_t id_len;
3005
3006 id = eap_get_identity(sm->eap, &id_len);
3007 if (id)
3008 identity_buf = dup_binstr(id, id_len);
3009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 ret = os_snprintf(buf + len, buflen - len,
3011 /* TODO: dot1xAuthSessionOctetsRx */
3012 /* TODO: dot1xAuthSessionOctetsTx */
3013 /* TODO: dot1xAuthSessionFramesRx */
3014 /* TODO: dot1xAuthSessionFramesTx */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003015 "dot1xAuthSessionId=%016llX\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 "dot1xAuthSessionAuthenticMethod=%d\n"
3017 "dot1xAuthSessionTime=%u\n"
3018 "dot1xAuthSessionTerminateCause=999\n"
3019 "dot1xAuthSessionUserName=%s\n",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003020 (unsigned long long) sta->acct_session_id,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 (wpa_key_mgmt_wpa_ieee8021x(
3022 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
3023 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003024 (unsigned int) diff.sec,
Hai Shalom021b0b52019-04-10 11:17:58 -07003025 sm->identity ? (char *) sm->identity :
3026 (identity_buf ? identity_buf : "N/A"));
Hai Shalom74f70d42019-02-11 14:42:39 -08003027 os_free(identity_buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003028 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 return len;
3030 len += ret;
3031
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003032 if (sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003033 ret = os_snprintf(buf + len, buflen - len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003034 "authMultiSessionId=%016llX\n",
3035 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003036 sm->acct_multi_session_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003037 if (os_snprintf_error(buflen - len, ret))
3038 return len;
3039 len += ret;
3040 }
3041
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003042 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
3043 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003044 ret = os_snprintf(buf + len, buflen - len,
3045 "last_eap_type_as=%d (%s)\n"
3046 "last_eap_type_sta=%d (%s)\n",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003047 sm->eap_type_authsrv, name1,
3048 sm->eap_type_supp, name2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003049 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003050 return len;
3051 len += ret;
3052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 return len;
3054}
3055
3056
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003057#ifdef CONFIG_HS20
3058static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
3059{
3060 struct hostapd_data *hapd = eloop_ctx;
3061 struct sta_info *sta = timeout_ctx;
3062
3063 if (sta->remediation) {
3064 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3065 MACSTR " to indicate Subscription Remediation",
3066 MAC2STR(sta->addr));
3067 hs20_send_wnm_notification(hapd, sta->addr,
3068 sta->remediation_method,
3069 sta->remediation_url);
3070 os_free(sta->remediation_url);
3071 sta->remediation_url = NULL;
3072 }
3073
3074 if (sta->hs20_deauth_req) {
3075 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3076 MACSTR " to indicate imminent deauthentication",
3077 MAC2STR(sta->addr));
3078 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
3079 sta->hs20_deauth_req);
3080 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003081
3082 if (sta->hs20_t_c_filtering) {
3083 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3084 MACSTR " to indicate Terms and Conditions filtering",
3085 MAC2STR(sta->addr));
3086 hs20_send_wnm_notification_t_c(hapd, sta->addr, sta->t_c_url);
3087 os_free(sta->t_c_url);
3088 sta->t_c_url = NULL;
3089 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003090}
3091#endif /* CONFIG_HS20 */
3092
3093
Sunil Ravi640215c2023-06-28 23:08:09 +00003094static bool ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003095 struct sta_info *sta, int success,
Sunil Ravi640215c2023-06-28 23:08:09 +00003096 int remediation, bool logoff)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003097{
3098 const u8 *key;
3099 size_t len;
3100 /* TODO: get PMKLifetime from WPA parameters */
3101 static const int dot11RSNAConfigPMKLifetime = 43200;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003102 unsigned int session_timeout;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003103 struct os_reltime now, remaining;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003105#ifdef CONFIG_HS20
3106 if (remediation && !sta->remediation) {
3107 sta->remediation = 1;
3108 os_free(sta->remediation_url);
3109 sta->remediation_url =
3110 os_strdup(hapd->conf->subscr_remediation_url);
3111 sta->remediation_method = 1; /* SOAP-XML SPP */
3112 }
3113
Roshan Pius3a1667e2018-07-03 15:17:14 -07003114 if (success && (sta->remediation || sta->hs20_deauth_req ||
3115 sta->hs20_t_c_filtering)) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003116 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
3117 MACSTR " in 100 ms", MAC2STR(sta->addr));
3118 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
3119 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
3120 hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003121 }
3122#endif /* CONFIG_HS20 */
3123
Hai Shalom81f62d82019-07-22 12:10:00 -07003124#ifdef CONFIG_MACSEC
3125 ieee802_1x_notify_create_actor_hapd(hapd, sta);
3126#endif /* CONFIG_MACSEC */
3127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003129 if (sta->session_timeout_set) {
3130 os_get_reltime(&now);
3131 os_reltime_sub(&sta->session_timeout, &now, &remaining);
3132 session_timeout = (remaining.sec > 0) ? remaining.sec : 1;
3133 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003134 session_timeout = dot11RSNAConfigPMKLifetime;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003135 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003136 if (success && key && len >= PMK_LEN && !sta->remediation &&
3137 !sta->hs20_deauth_requested &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003138 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 sta->eapol_sm) == 0) {
3140 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
3141 HOSTAPD_LEVEL_DEBUG,
3142 "Added PMKSA cache entry (IEEE 802.1X)");
3143 }
3144
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003145 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 /*
3147 * Many devices require deauthentication after WPS provisioning
3148 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003149 * disconnect the client here. In addition, this may also
3150 * benefit IEEE 802.1X/EAPOL authentication cases, too since
3151 * the EAPOL PAE state machine would remain in HELD state for
3152 * considerable amount of time and some EAP methods, like
3153 * EAP-FAST with anonymous provisioning, may require another
3154 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 */
Sunil Ravi640215c2023-06-28 23:08:09 +00003156 ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta,
3157 logoff ? 0 : 10);
3158 if (logoff && sta->wpa_sm)
3159 return true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003160 }
Sunil Ravi640215c2023-06-28 23:08:09 +00003161
3162 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163}