blob: 8e98b65216952a20c2eae24f3dfc3b7199f751b9 [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 Ravi72e01222024-03-09 01:25:43 +0000117 bool update;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118
119 if (sta->flags & WLAN_STA_PREAUTH)
120 return;
121
Sunil Ravi72e01222024-03-09 01:25:43 +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 Ravi72e01222024-03-09 01:25:43 +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
152 unsigned int i, link_id;
153
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
164 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
165 struct mld_link_info *link = &sta->mld_info.links[link_id];
166
167 if (!link->valid)
168 continue;
169
170 for (i = 0; i < hapd->iface->interfaces->count; i++) {
171 struct sta_info *tmp_sta;
172 struct hostapd_data *tmp_hapd =
173 hapd->iface->interfaces->iface[i]->bss[0];
174
Sunil Raviaf399a82024-05-05 20:56:55 +0000175 if (!hostapd_is_ml_partner(hapd, tmp_hapd))
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000176 continue;
177
178 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
179 tmp_sta = tmp_sta->next) {
180 if (tmp_sta == sta ||
181 tmp_sta->mld_assoc_link_id !=
182 sta->mld_assoc_link_id ||
183 tmp_sta->aid != sta->aid)
184 continue;
185
186 ieee802_1x_set_authorized(tmp_hapd, tmp_sta,
187 authorized, true);
188 break;
189 }
190 }
191 }
192#endif /* CONFIG_IEEE80211BE */
193}
194
195
196
197void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
198 struct sta_info *sta, int authorized)
199{
200 ieee802_1x_set_authorized(hapd, sta, authorized, false);
201 ieee802_1x_ml_set_sta_authorized(hapd, sta, !!authorized);
202}
203
204
Hai Shalomfdcde762020-04-02 11:19:20 -0700205#ifdef CONFIG_WEP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800206#ifndef CONFIG_FIPS
207#ifndef CONFIG_NO_RC4
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
210 struct sta_info *sta,
211 int idx, int broadcast,
212 u8 *key_data, size_t key_len)
213{
214 u8 *buf, *ekey;
215 struct ieee802_1x_hdr *hdr;
216 struct ieee802_1x_eapol_key *key;
217 size_t len, ekey_len;
218 struct eapol_state_machine *sm = sta->eapol_sm;
219
Hai Shalomc3565922019-10-28 11:58:20 -0700220 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 return;
222
223 len = sizeof(*key) + key_len;
224 buf = os_zalloc(sizeof(*hdr) + len);
Hai Shalomc3565922019-10-28 11:58:20 -0700225 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 return;
227
228 hdr = (struct ieee802_1x_hdr *) buf;
229 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
230 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700231 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 wpa_get_ntp_timestamp(key->replay_counter);
Hai Shalom81f62d82019-07-22 12:10:00 -0700233 if (os_memcmp(key->replay_counter,
234 hapd->last_1x_eapol_key_replay_counter,
235 IEEE8021X_REPLAY_COUNTER_LEN) <= 0) {
236 /* NTP timestamp did not increment from last EAPOL-Key frame;
237 * use previously used value + 1 instead. */
238 inc_byte_array(hapd->last_1x_eapol_key_replay_counter,
239 IEEE8021X_REPLAY_COUNTER_LEN);
240 os_memcpy(key->replay_counter,
241 hapd->last_1x_eapol_key_replay_counter,
242 IEEE8021X_REPLAY_COUNTER_LEN);
243 } else {
244 os_memcpy(hapd->last_1x_eapol_key_replay_counter,
245 key->replay_counter,
246 IEEE8021X_REPLAY_COUNTER_LEN);
247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
250 wpa_printf(MSG_ERROR, "Could not get random numbers");
251 os_free(buf);
252 return;
253 }
254
255 key->key_index = idx | (broadcast ? 0 : BIT(7));
256 if (hapd->conf->eapol_key_index_workaround) {
257 /* According to some information, WinXP Supplicant seems to
258 * interpret bit7 as an indication whether the key is to be
259 * activated, so make it possible to enable workaround that
260 * sets this bit for all keys. */
261 key->key_index |= BIT(7);
262 }
263
264 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
265 * MSK[32..63] is used to sign the message. */
Hai Shalomc3565922019-10-28 11:58:20 -0700266 if (!sm->eap_if->eapKeyData || sm->eap_if->eapKeyDataLen < 64) {
267 wpa_printf(MSG_ERROR,
268 "No eapKeyData available for encrypting and signing EAPOL-Key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 os_free(buf);
270 return;
271 }
272 os_memcpy((u8 *) (key + 1), key_data, key_len);
273 ekey_len = sizeof(key->key_iv) + 32;
274 ekey = os_malloc(ekey_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700275 if (!ekey) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 wpa_printf(MSG_ERROR, "Could not encrypt key");
277 os_free(buf);
278 return;
279 }
280 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
281 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
282 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
283 os_free(ekey);
284
285 /* This header is needed here for HMAC-MD5, but it will be regenerated
286 * in ieee802_1x_send() */
287 hdr->version = hapd->conf->eapol_version;
Hai Shalom81f62d82019-07-22 12:10:00 -0700288#ifdef CONFIG_MACSEC
289 if (hdr->version > 2)
290 hdr->version = 2;
291#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
293 hdr->length = host_to_be16(len);
294 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
295 key->key_signature);
296
297 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
298 " (%s index=%d)", MAC2STR(sm->addr),
299 broadcast ? "broadcast" : "unicast", idx);
300 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
301 if (sta->eapol_sm)
302 sta->eapol_sm->dot1xAuthEapolFramesTx++;
303 os_free(buf);
304}
305
306
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800307static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308{
309 struct eapol_authenticator *eapol = hapd->eapol_auth;
310 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311
Hai Shalomc3565922019-10-28 11:58:20 -0700312 if (!sm || !sm->eap_if->eapKeyData)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 return;
314
315 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
316 MAC2STR(sta->addr));
317
318#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800319 if (sta->vlan_id > 0) {
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700320 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
321 return;
322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 if (eapol->default_wep_key) {
326 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
327 eapol->default_wep_key,
328 hapd->conf->default_wep_key_len);
329 }
330
331 if (hapd->conf->individual_wep_key_len > 0) {
332 u8 *ikey;
Hai Shalomc3565922019-10-28 11:58:20 -0700333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 ikey = os_malloc(hapd->conf->individual_wep_key_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700335 if (!ikey ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
337 {
Hai Shalomc3565922019-10-28 11:58:20 -0700338 wpa_printf(MSG_ERROR,
339 "Could not generate random individual WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 os_free(ikey);
341 return;
342 }
343
344 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
345 ikey, hapd->conf->individual_wep_key_len);
346
347 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
348 hapd->conf->individual_wep_key_len);
349
350 /* TODO: set encryption in TX callback, i.e., only after STA
351 * has ACKed EAPOL-Key frame */
352 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
Hai Shalomfdcde762020-04-02 11:19:20 -0700353 sta->addr, 0, 0, 1, NULL, 0, ikey,
354 hapd->conf->individual_wep_key_len,
355 KEY_FLAG_PAIRWISE_RX_TX)) {
Hai Shalomc3565922019-10-28 11:58:20 -0700356 wpa_printf(MSG_ERROR,
357 "Could not set individual WEP encryption");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 }
359
360 os_free(ikey);
361 }
362}
363
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364#endif /* CONFIG_NO_RC4 */
365#endif /* CONFIG_FIPS */
Hai Shalomfdcde762020-04-02 11:19:20 -0700366#endif /* CONFIG_WEP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368
369const char *radius_mode_txt(struct hostapd_data *hapd)
370{
371 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800372 case HOSTAPD_MODE_IEEE80211AD:
373 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 case HOSTAPD_MODE_IEEE80211A:
375 return "802.11a";
376 case HOSTAPD_MODE_IEEE80211G:
377 return "802.11g";
378 case HOSTAPD_MODE_IEEE80211B:
379 default:
380 return "802.11b";
381 }
382}
383
384
385int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
386{
387 int i;
388 u8 rate = 0;
389
390 for (i = 0; i < sta->supported_rates_len; i++)
391 if ((sta->supported_rates[i] & 0x7f) > rate)
392 rate = sta->supported_rates[i] & 0x7f;
393
394 return rate;
395}
396
397
398#ifndef CONFIG_NO_RADIUS
399static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
400 struct eapol_state_machine *sm,
401 const u8 *eap, size_t len)
402{
403 const u8 *identity;
404 size_t identity_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800405 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406
407 if (len <= sizeof(struct eap_hdr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408 (hdr->code == EAP_CODE_RESPONSE &&
409 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
410 (hdr->code == EAP_CODE_INITIATE &&
411 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
412 (hdr->code != EAP_CODE_RESPONSE &&
413 hdr->code != EAP_CODE_INITIATE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 return;
415
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800416 eap_erp_update_identity(sm->eap, eap, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 identity = eap_get_identity(sm->eap, &identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700418 if (!identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 return;
420
421 /* Save station identity for future RADIUS packets */
422 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700423 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700424 if (!sm->identity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 sm->identity_len = 0;
426 return;
427 }
428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
431 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
432 sm->dot1xAuthEapolRespIdFramesRx++;
433}
434
435
Dmitry Shmidt03658832014-08-13 11:03:49 -0700436static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
437 struct hostapd_radius_attr *req_attr,
438 struct sta_info *sta,
439 struct radius_msg *msg)
440{
441 u32 suite;
442 int ver, val;
443
444 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
445 val = wpa_auth_get_pairwise(sta->wpa_sm);
446 suite = wpa_cipher_to_suite(ver, val);
447 if (val != -1 &&
448 !hostapd_config_get_radius_attr(req_attr,
449 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
450 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
451 suite)) {
452 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
453 return -1;
454 }
455
Dmitry Shmidt41712582015-06-29 11:02:15 -0700456 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
457 hapd->conf->osen) ?
Dmitry Shmidt03658832014-08-13 11:03:49 -0700458 WPA_PROTO_RSN : WPA_PROTO_WPA,
459 hapd->conf->wpa_group);
460 if (!hostapd_config_get_radius_attr(req_attr,
461 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
462 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
463 suite)) {
464 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
465 return -1;
466 }
467
468 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
469 suite = wpa_akm_to_suite(val);
470 if (val != -1 &&
471 !hostapd_config_get_radius_attr(req_attr,
472 RADIUS_ATTR_WLAN_AKM_SUITE) &&
473 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
474 suite)) {
475 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
476 return -1;
477 }
478
Dmitry Shmidt03658832014-08-13 11:03:49 -0700479 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
480 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
481 hapd->conf->group_mgmt_cipher);
482 if (!hostapd_config_get_radius_attr(
483 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
484 !radius_msg_add_attr_int32(
485 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
486 wpa_printf(MSG_ERROR,
487 "Could not add WLAN-Group-Mgmt-Cipher");
488 return -1;
489 }
490 }
Dmitry Shmidt03658832014-08-13 11:03:49 -0700491
492 return 0;
493}
494
495
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700496static int add_common_radius_sta_attr(struct hostapd_data *hapd,
497 struct hostapd_radius_attr *req_attr,
498 struct sta_info *sta,
499 struct radius_msg *msg)
500{
501 char buf[128];
502
503 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800504 RADIUS_ATTR_SERVICE_TYPE) &&
505 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
506 RADIUS_SERVICE_TYPE_FRAMED)) {
507 wpa_printf(MSG_ERROR, "Could not add Service-Type");
508 return -1;
509 }
510
511 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700512 RADIUS_ATTR_NAS_PORT) &&
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700513 sta->aid > 0 &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
515 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
516 return -1;
517 }
518
519 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
520 MAC2STR(sta->addr));
521 buf[sizeof(buf) - 1] = '\0';
522 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
523 (u8 *) buf, os_strlen(buf))) {
524 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
525 return -1;
526 }
527
528 if (sta->flags & WLAN_STA_PREAUTH) {
529 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
530 sizeof(buf));
531 } else {
532 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
533 radius_sta_rate(hapd, sta) / 2,
534 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
535 radius_mode_txt(hapd));
536 buf[sizeof(buf) - 1] = '\0';
537 }
538 if (!hostapd_config_get_radius_attr(req_attr,
539 RADIUS_ATTR_CONNECT_INFO) &&
540 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
541 (u8 *) buf, os_strlen(buf))) {
542 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
543 return -1;
544 }
545
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800546 if (sta->acct_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800547 os_snprintf(buf, sizeof(buf), "%016llX",
548 (unsigned long long) sta->acct_session_id);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800549 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
550 (u8 *) buf, os_strlen(buf))) {
551 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
552 return -1;
553 }
554 }
555
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800556 if ((hapd->conf->wpa & 2) &&
557 !hapd->conf->disable_pmksa_caching &&
558 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800559 os_snprintf(buf, sizeof(buf), "%016llX",
560 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800561 sta->eapol_sm->acct_multi_session_id);
562 if (!radius_msg_add_attr(
563 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
564 (u8 *) buf, os_strlen(buf))) {
565 wpa_printf(MSG_INFO,
566 "Could not add Acct-Multi-Session-Id");
567 return -1;
568 }
569 }
570
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800571#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt03658832014-08-13 11:03:49 -0700572 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
573 sta->wpa_sm &&
574 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
575 sta->auth_alg == WLAN_AUTH_FT) &&
576 !hostapd_config_get_radius_attr(req_attr,
577 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
578 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
579 WPA_GET_BE16(
580 hapd->conf->mobility_domain))) {
581 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
582 return -1;
583 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800584#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt03658832014-08-13 11:03:49 -0700585
Dmitry Shmidt41712582015-06-29 11:02:15 -0700586 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
Dmitry Shmidt03658832014-08-13 11:03:49 -0700587 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
588 return -1;
589
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700590 return 0;
591}
592
593
594int add_common_radius_attr(struct hostapd_data *hapd,
595 struct hostapd_radius_attr *req_attr,
596 struct sta_info *sta,
597 struct radius_msg *msg)
598{
599 char buf[128];
600 struct hostapd_radius_attr *attr;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800601 int len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700602
603 if (!hostapd_config_get_radius_attr(req_attr,
604 RADIUS_ATTR_NAS_IP_ADDRESS) &&
605 hapd->conf->own_ip_addr.af == AF_INET &&
606 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
607 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
608 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
609 return -1;
610 }
611
612#ifdef CONFIG_IPV6
613 if (!hostapd_config_get_radius_attr(req_attr,
614 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
615 hapd->conf->own_ip_addr.af == AF_INET6 &&
616 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
617 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
618 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
619 return -1;
620 }
621#endif /* CONFIG_IPV6 */
622
623 if (!hostapd_config_get_radius_attr(req_attr,
624 RADIUS_ATTR_NAS_IDENTIFIER) &&
625 hapd->conf->nas_identifier &&
626 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
627 (u8 *) hapd->conf->nas_identifier,
628 os_strlen(hapd->conf->nas_identifier))) {
629 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
630 return -1;
631 }
632
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800633 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
634 MAC2STR(hapd->own_addr));
635 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
636 hapd->conf->ssid.ssid_len);
637 len += hapd->conf->ssid.ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700638 if (!hostapd_config_get_radius_attr(req_attr,
639 RADIUS_ATTR_CALLED_STATION_ID) &&
640 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800641 (u8 *) buf, len)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700642 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
643 return -1;
644 }
645
646 if (!hostapd_config_get_radius_attr(req_attr,
647 RADIUS_ATTR_NAS_PORT_TYPE) &&
648 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
649 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
650 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
651 return -1;
652 }
653
Dmitry Shmidt03658832014-08-13 11:03:49 -0700654#ifdef CONFIG_INTERWORKING
655 if (hapd->conf->interworking &&
656 !is_zero_ether_addr(hapd->conf->hessid)) {
657 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
658 MAC2STR(hapd->conf->hessid));
659 buf[sizeof(buf) - 1] = '\0';
660 if (!hostapd_config_get_radius_attr(req_attr,
661 RADIUS_ATTR_WLAN_HESSID) &&
662 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
663 (u8 *) buf, os_strlen(buf))) {
664 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
665 return -1;
666 }
667 }
668#endif /* CONFIG_INTERWORKING */
669
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700670 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
671 return -1;
672
673 for (attr = req_attr; attr; attr = attr->next) {
674 if (!radius_msg_add_attr(msg, attr->type,
675 wpabuf_head(attr->val),
676 wpabuf_len(attr->val))) {
Hai Shalomc3565922019-10-28 11:58:20 -0700677 wpa_printf(MSG_ERROR, "Could not add RADIUS attribute");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700678 return -1;
679 }
680 }
681
682 return 0;
683}
684
685
Hai Shalomc3565922019-10-28 11:58:20 -0700686int add_sqlite_radius_attr(struct hostapd_data *hapd, struct sta_info *sta,
687 struct radius_msg *msg, int acct)
688{
689#ifdef CONFIG_SQLITE
690 const char *attrtxt;
691 char addrtxt[3 * ETH_ALEN];
692 char *sql;
693 sqlite3_stmt *stmt = NULL;
694
695 if (!hapd->rad_attr_db)
696 return 0;
697
698 os_snprintf(addrtxt, sizeof(addrtxt), MACSTR, MAC2STR(sta->addr));
699
700 sql = "SELECT attr FROM radius_attributes WHERE sta=? AND (reqtype=? OR reqtype IS NULL);";
701 if (sqlite3_prepare_v2(hapd->rad_attr_db, sql, os_strlen(sql), &stmt,
702 NULL) != SQLITE_OK) {
703 wpa_printf(MSG_ERROR, "DB: Failed to prepare SQL statement: %s",
704 sqlite3_errmsg(hapd->rad_attr_db));
705 return -1;
706 }
707 sqlite3_bind_text(stmt, 1, addrtxt, os_strlen(addrtxt), SQLITE_STATIC);
708 sqlite3_bind_text(stmt, 2, acct ? "acct" : "auth", 4, SQLITE_STATIC);
709 while (sqlite3_step(stmt) == SQLITE_ROW) {
710 struct hostapd_radius_attr *attr;
711 struct radius_attr_hdr *hdr;
712
713 attrtxt = (const char *) sqlite3_column_text(stmt, 0);
714 attr = hostapd_parse_radius_attr(attrtxt);
715 if (!attr) {
716 wpa_printf(MSG_ERROR,
717 "Skipping invalid attribute from SQL: %s",
718 attrtxt);
719 continue;
720 }
721 wpa_printf(MSG_DEBUG, "Adding RADIUS attribute from SQL: %s",
722 attrtxt);
723 hdr = radius_msg_add_attr(msg, attr->type,
724 wpabuf_head(attr->val),
725 wpabuf_len(attr->val));
726 hostapd_config_free_radius_attr(attr);
727 if (!hdr) {
728 wpa_printf(MSG_ERROR,
729 "Could not add RADIUS attribute from SQL");
730 continue;
731 }
732 }
733
734 sqlite3_reset(stmt);
735 sqlite3_clear_bindings(stmt);
736 sqlite3_finalize(stmt);
737#endif /* CONFIG_SQLITE */
738
739 return 0;
740}
741
742
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800743void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
744 struct sta_info *sta,
745 const u8 *eap, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746{
747 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 struct eapol_state_machine *sm = sta->eapol_sm;
749
Hai Shalomc3565922019-10-28 11:58:20 -0700750 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 return;
752
753 ieee802_1x_learn_identity(hapd, sm, eap, len);
754
Hai Shalomc3565922019-10-28 11:58:20 -0700755 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756
757 sm->radius_identifier = radius_client_get_id(hapd->radius);
758 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
759 sm->radius_identifier);
Hai Shalomc3565922019-10-28 11:58:20 -0700760 if (!msg) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800761 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 return;
763 }
764
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800765 if (radius_msg_make_authenticator(msg) < 0) {
766 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
767 goto fail;
768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769
770 if (sm->identity &&
771 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
772 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800773 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 goto fail;
775 }
776
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700777 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
778 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780
Hai Shalomc3565922019-10-28 11:58:20 -0700781 if (sta && add_sqlite_radius_attr(hapd, sta, msg, 0) < 0)
782 goto fail;
783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 /* TODO: should probably check MTU from driver config; 2304 is max for
785 * IEEE 802.11, but use 1400 to avoid problems with too large packets
786 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700787 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
788 RADIUS_ATTR_FRAMED_MTU) &&
789 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800790 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 goto fail;
792 }
793
Dmitry Shmidt41712582015-06-29 11:02:15 -0700794 if (!radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800795 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 goto fail;
797 }
798
799 /* State attribute must be copied if and only if this packet is
800 * Access-Request reply to the previous Access-Challenge */
801 if (sm->last_recv_radius &&
802 radius_msg_get_hdr(sm->last_recv_radius)->code ==
803 RADIUS_CODE_ACCESS_CHALLENGE) {
804 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
805 RADIUS_ATTR_STATE);
806 if (res < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -0700807 wpa_printf(MSG_INFO,
808 "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 goto fail;
810 }
Hai Shalomc3565922019-10-28 11:58:20 -0700811 if (res > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 }
814
Dmitry Shmidt04949592012-07-19 12:16:46 -0700815 if (hapd->conf->radius_request_cui) {
816 const u8 *cui;
817 size_t cui_len;
818 /* Add previously learned CUI or nul CUI to request CUI */
819 if (sm->radius_cui) {
820 cui = wpabuf_head(sm->radius_cui);
821 cui_len = wpabuf_len(sm->radius_cui);
822 } else {
823 cui = (const u8 *) "\0";
824 cui_len = 1;
825 }
826 if (!radius_msg_add_attr(msg,
827 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
828 cui, cui_len)) {
829 wpa_printf(MSG_ERROR, "Could not add CUI");
830 goto fail;
831 }
832 }
833
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800834#ifdef CONFIG_HS20
835 if (hapd->conf->hs20) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800836 u8 ver = hapd->conf->hs20_release - 1;
837
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800838 if (!radius_msg_add_wfa(
839 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
840 &ver, 1)) {
Hai Shalomc3565922019-10-28 11:58:20 -0700841 wpa_printf(MSG_ERROR,
842 "Could not add HS 2.0 AP version");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800843 goto fail;
844 }
845
846 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
847 const u8 *pos;
848 u8 buf[3];
849 u16 id;
Hai Shalomc3565922019-10-28 11:58:20 -0700850
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800851 pos = wpabuf_head_u8(sta->hs20_ie);
852 buf[0] = (*pos) >> 4;
853 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
854 wpabuf_len(sta->hs20_ie) >= 3)
855 id = WPA_GET_LE16(pos + 1);
856 else
857 id = 0;
858 WPA_PUT_BE16(buf + 1, id);
859 if (!radius_msg_add_wfa(
860 msg,
861 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
862 buf, sizeof(buf))) {
Hai Shalomc3565922019-10-28 11:58:20 -0700863 wpa_printf(MSG_ERROR,
864 "Could not add HS 2.0 STA version");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800865 goto fail;
866 }
867 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700868
869 if (sta->roaming_consortium &&
870 !radius_msg_add_wfa(
871 msg, RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM,
872 wpabuf_head(sta->roaming_consortium),
873 wpabuf_len(sta->roaming_consortium))) {
874 wpa_printf(MSG_ERROR,
875 "Could not add HS 2.0 Roaming Consortium");
876 goto fail;
877 }
878
879 if (hapd->conf->t_c_filename) {
880 be32 timestamp;
881
882 if (!radius_msg_add_wfa(
883 msg,
884 RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME,
885 (const u8 *) hapd->conf->t_c_filename,
886 os_strlen(hapd->conf->t_c_filename))) {
887 wpa_printf(MSG_ERROR,
888 "Could not add HS 2.0 T&C Filename");
889 goto fail;
890 }
891
892 timestamp = host_to_be32(hapd->conf->t_c_timestamp);
893 if (!radius_msg_add_wfa(
894 msg,
895 RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP,
896 (const u8 *) &timestamp,
897 sizeof(timestamp))) {
898 wpa_printf(MSG_ERROR,
899 "Could not add HS 2.0 Timestamp");
900 goto fail;
901 }
902 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800903 }
904#endif /* CONFIG_HS20 */
905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
907 goto fail;
908
909 return;
910
911 fail:
912 radius_msg_free(msg);
913}
914#endif /* CONFIG_NO_RADIUS */
915
916
917static void handle_eap_response(struct hostapd_data *hapd,
918 struct sta_info *sta, struct eap_hdr *eap,
919 size_t len)
920{
921 u8 type, *data;
922 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -0700923
924 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 return;
926
927 data = (u8 *) (eap + 1);
928
929 if (len < sizeof(*eap) + 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700930 wpa_printf(MSG_INFO, "%s: too short response data", __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 return;
932 }
933
934 sm->eap_type_supp = type = data[0];
935
936 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
937 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
938 "id=%d len=%d) from STA: EAP Response-%s (%d)",
939 eap->code, eap->identifier, be_to_host16(eap->length),
940 eap_server_get_name(0, type), type);
941
942 sm->dot1xAuthEapolRespFramesRx++;
943
944 wpabuf_free(sm->eap_if->eapRespData);
945 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
Hai Shalome21d4e82020-04-29 16:34:06 -0700946 sm->eapolEap = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947}
948
949
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800950static void handle_eap_initiate(struct hostapd_data *hapd,
951 struct sta_info *sta, struct eap_hdr *eap,
952 size_t len)
953{
954#ifdef CONFIG_ERP
955 u8 type, *data;
956 struct eapol_state_machine *sm = sta->eapol_sm;
957
Hai Shalomc3565922019-10-28 11:58:20 -0700958 if (!sm)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800959 return;
960
961 if (len < sizeof(*eap) + 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700962 wpa_printf(MSG_INFO, "%s: too short response data", __func__);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800963 return;
964 }
965
966 data = (u8 *) (eap + 1);
967 type = data[0];
968
969 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -0700970 HOSTAPD_LEVEL_DEBUG,
971 "received EAP packet (code=%d id=%d len=%d) from STA: EAP Initiate type %u",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800972 eap->code, eap->identifier, be_to_host16(eap->length),
973 type);
974
975 wpabuf_free(sm->eap_if->eapRespData);
976 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
Hai Shalome21d4e82020-04-29 16:34:06 -0700977 sm->eapolEap = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800978#endif /* CONFIG_ERP */
979}
980
981
Hai Shalomc3565922019-10-28 11:58:20 -0700982#ifndef CONFIG_NO_STDOUT_DEBUG
983static const char * eap_code_str(u8 code)
984{
985 switch (code) {
986 case EAP_CODE_REQUEST:
987 return "request";
988 case EAP_CODE_RESPONSE:
989 return "response";
990 case EAP_CODE_SUCCESS:
991 return "success";
992 case EAP_CODE_FAILURE:
993 return "failure";
994 case EAP_CODE_INITIATE:
995 return "initiate";
996 case EAP_CODE_FINISH:
997 return "finish";
998 default:
999 return "unknown";
1000 }
1001}
1002#endif /* CONFIG_NO_STDOUT_DEBUG */
1003
1004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005/* Process incoming EAP packet from Supplicant */
1006static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
1007 u8 *buf, size_t len)
1008{
1009 struct eap_hdr *eap;
1010 u16 eap_len;
1011
1012 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001013 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 return;
1015 }
1016
1017 eap = (struct eap_hdr *) buf;
1018
1019 eap_len = be_to_host16(eap->length);
Hai Shalomc3565922019-10-28 11:58:20 -07001020 wpa_printf(MSG_DEBUG, "EAP: code=%d (%s) identifier=%d length=%d",
1021 eap->code, eap_code_str(eap->code), eap->identifier,
1022 eap_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 if (eap_len < sizeof(*eap)) {
1024 wpa_printf(MSG_DEBUG, " Invalid EAP length");
1025 return;
1026 } else if (eap_len > len) {
Hai Shalomc3565922019-10-28 11:58:20 -07001027 wpa_printf(MSG_DEBUG,
1028 " Too short frame to contain this EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 return;
1030 } else if (eap_len < len) {
Hai Shalomc3565922019-10-28 11:58:20 -07001031 wpa_printf(MSG_DEBUG,
1032 " Ignoring %lu extra bytes after EAP packet",
1033 (unsigned long) len - eap_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 }
1035
1036 switch (eap->code) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 case EAP_CODE_RESPONSE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 handle_eap_response(hapd, sta, eap, eap_len);
1039 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 case EAP_CODE_INITIATE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001041 handle_eap_initiate(hapd, sta, eap, eap_len);
1042 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 }
1044}
1045
1046
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001047struct eapol_state_machine *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
1049{
1050 int flags = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 if (sta->flags & WLAN_STA_PREAUTH)
1053 flags |= EAPOL_SM_PREAUTH;
1054 if (sta->wpa_sm) {
1055 flags |= EAPOL_SM_USES_WPA;
1056 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
1057 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
1058 }
1059 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001060 sta->wps_ie, sta->p2p_ie, sta,
1061 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062}
1063
1064
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001065static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
Sunil8cd6f4d2022-06-28 18:40:46 +00001066 size_t len, enum frame_encryption encrypted)
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001067{
1068 if (sta->pending_eapol_rx) {
1069 wpabuf_free(sta->pending_eapol_rx->buf);
1070 } else {
1071 sta->pending_eapol_rx =
1072 os_malloc(sizeof(*sta->pending_eapol_rx));
1073 if (!sta->pending_eapol_rx)
1074 return;
1075 }
1076
1077 sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
1078 if (!sta->pending_eapol_rx->buf) {
1079 os_free(sta->pending_eapol_rx);
1080 sta->pending_eapol_rx = NULL;
1081 return;
1082 }
1083
Sunil8cd6f4d2022-06-28 18:40:46 +00001084 sta->pending_eapol_rx->encrypted = encrypted;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001085 os_get_reltime(&sta->pending_eapol_rx->rx_time);
1086}
1087
1088
Sunil8cd6f4d2022-06-28 18:40:46 +00001089static bool ieee802_1x_check_encryption(struct sta_info *sta,
1090 enum frame_encryption encrypted,
1091 u8 type)
1092{
1093 if (encrypted != FRAME_NOT_ENCRYPTED)
1094 return true;
1095 if (type != IEEE802_1X_TYPE_EAP_PACKET &&
1096 type != IEEE802_1X_TYPE_EAPOL_START &&
1097 type != IEEE802_1X_TYPE_EAPOL_LOGOFF)
1098 return true;
1099 if (!(sta->flags & WLAN_STA_MFP))
1100 return true;
1101 return !wpa_auth_pairwise_set(sta->wpa_sm);
1102}
1103
1104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105/**
1106 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
1107 * @hapd: hostapd BSS data
1108 * @sa: Source address (sender of the EAPOL frame)
1109 * @buf: EAPOL frame
1110 * @len: Length of buf in octets
Sunil8cd6f4d2022-06-28 18:40:46 +00001111 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 *
1113 * This function is called for each incoming EAPOL frame from the interface
1114 */
1115void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
Sunil8cd6f4d2022-06-28 18:40:46 +00001116 size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117{
1118 struct sta_info *sta;
1119 struct ieee802_1x_hdr *hdr;
1120 struct ieee802_1x_eapol_key *key;
1121 u16 datalen;
1122 struct rsn_pmksa_cache_entry *pmksa;
1123 int key_mgmt;
1124
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001125 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 !hapd->conf->wps_state)
1127 return;
1128
Sunil8cd6f4d2022-06-28 18:40:46 +00001129 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR
1130 " (encrypted=%d)",
1131 (unsigned long) len, MAC2STR(sa), encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001133 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
1134 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001135 wpa_printf(MSG_DEBUG,
1136 "IEEE 802.1X data frame from not associated/Pre-authenticating STA");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001137
1138 if (sta && (sta->flags & WLAN_STA_AUTH)) {
1139 wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
1140 " for later use", MAC2STR(sta->addr));
Sunil8cd6f4d2022-06-28 18:40:46 +00001141 ieee802_1x_save_eapol(sta, buf, len, encrypted);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001142 }
1143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 return;
1145 }
1146
1147 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001148 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 return;
1150 }
1151
1152 hdr = (struct ieee802_1x_hdr *) buf;
1153 datalen = be_to_host16(hdr->length);
1154 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
1155 hdr->version, hdr->type, datalen);
1156
1157 if (len - sizeof(*hdr) < datalen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001158 wpa_printf(MSG_INFO,
1159 " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 if (sta->eapol_sm)
1161 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
1162 return;
1163 }
1164 if (len - sizeof(*hdr) > datalen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001165 wpa_printf(MSG_DEBUG,
1166 " ignoring %lu extra octets after IEEE 802.1X packet",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 (unsigned long) len - sizeof(*hdr) - datalen);
1168 }
1169
1170 if (sta->eapol_sm) {
1171 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
1172 sta->eapol_sm->dot1xAuthEapolFramesRx++;
1173 }
1174
1175 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
1176 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
1177 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
1178 (key->type == EAPOL_KEY_TYPE_WPA ||
1179 key->type == EAPOL_KEY_TYPE_RSN)) {
1180 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
1181 sizeof(*hdr) + datalen);
1182 return;
1183 }
1184
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001185 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001187 wpa_printf(MSG_DEBUG,
1188 "IEEE 802.1X: Ignore EAPOL message - 802.1X not enabled and WPS not used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 return;
1190 }
1191
1192 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001193 if (key_mgmt != -1 &&
1194 (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1195 key_mgmt == WPA_KEY_MGMT_DPP)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001196 wpa_printf(MSG_DEBUG,
1197 "IEEE 802.1X: Ignore EAPOL message - STA is using PSK");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 return;
1199 }
1200
Sunil8cd6f4d2022-06-28 18:40:46 +00001201 if (!ieee802_1x_check_encryption(sta, encrypted, hdr->type)) {
1202 wpa_printf(MSG_DEBUG,
1203 "IEEE 802.1X: Discard unencrypted EAPOL message - encryption was expected");
1204 return;
1205 }
1206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 if (!sta->eapol_sm) {
1208 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1209 if (!sta->eapol_sm)
1210 return;
1211
1212#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001213 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001214 u32 wflags = sta->flags & (WLAN_STA_WPS |
1215 WLAN_STA_WPS2 |
1216 WLAN_STA_MAYBE_WPS);
1217 if (wflags == WLAN_STA_MAYBE_WPS ||
1218 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
1219 /*
1220 * Delay EAPOL frame transmission until a
1221 * possible WPS STA initiates the handshake
1222 * with EAPOL-Start. Only allow the wait to be
1223 * skipped if the STA is known to support WPS
1224 * 2.0.
1225 */
Hai Shalomc3565922019-10-28 11:58:20 -07001226 wpa_printf(MSG_DEBUG,
1227 "WPS: Do not start EAPOL until EAPOL-Start is received");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 }
1231#endif /* CONFIG_WPS */
1232
Hai Shalome21d4e82020-04-29 16:34:06 -07001233 sta->eapol_sm->eap_if->portEnabled = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 }
1235
1236 /* since we support version 1, we can ignore version field and proceed
1237 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1238 /* TODO: actually, we are not version 1 anymore.. However, Version 2
1239 * does not change frame contents, so should be ok to process frames
1240 * more or less identically. Some changes might be needed for
1241 * verification of fields. */
1242
1243 switch (hdr->type) {
1244 case IEEE802_1X_TYPE_EAP_PACKET:
1245 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1246 break;
1247
1248 case IEEE802_1X_TYPE_EAPOL_START:
1249 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001250 HOSTAPD_LEVEL_DEBUG,
1251 "received EAPOL-Start from STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1253 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1254 if (pmksa) {
1255 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
Hai Shalomc3565922019-10-28 11:58:20 -07001256 HOSTAPD_LEVEL_DEBUG,
1257 "cached PMKSA available - ignore it since STA sent EAPOL-Start");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1259 }
Hai Shalome21d4e82020-04-29 16:34:06 -07001260 sta->eapol_sm->eapolStart = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1262 eap_server_clear_identity(sta->eapol_sm->eap);
1263 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1264 break;
1265
1266 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1267 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001268 HOSTAPD_LEVEL_DEBUG,
1269 "received EAPOL-Logoff from STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 sta->acct_terminate_cause =
1271 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1272 accounting_sta_stop(hapd, sta);
Hai Shalome21d4e82020-04-29 16:34:06 -07001273 sta->eapol_sm->eapolLogoff = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1275 eap_server_clear_identity(sta->eapol_sm->eap);
1276 break;
1277
1278 case IEEE802_1X_TYPE_EAPOL_KEY:
1279 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1280 if (!ap_sta_is_authorized(sta)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001281 wpa_printf(MSG_DEBUG,
1282 " Dropped key data from unauthorized Supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 break;
1284 }
1285 break;
1286
1287 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1288 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1289 /* TODO: implement support for this; show data */
1290 break;
1291
Hai Shalom81f62d82019-07-22 12:10:00 -07001292#ifdef CONFIG_MACSEC
1293 case IEEE802_1X_TYPE_EAPOL_MKA:
1294 wpa_printf(MSG_EXCESSIVE,
1295 "EAPOL type %d will be handled by MKA", hdr->type);
1296 break;
1297#endif /* CONFIG_MACSEC */
1298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 default:
1300 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1301 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1302 break;
1303 }
1304
1305 eapol_auth_step(sta->eapol_sm);
1306}
1307
1308
1309/**
1310 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1311 * @hapd: hostapd BSS data
1312 * @sta: The station
1313 *
1314 * This function is called to start IEEE 802.1X authentication when a new
1315 * station completes IEEE 802.11 association.
1316 */
1317void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1318{
1319 struct rsn_pmksa_cache_entry *pmksa;
1320 int reassoc = 1;
1321 int force_1x = 0;
1322 int key_mgmt;
1323
1324#ifdef CONFIG_WPS
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001325 if (hapd->conf->wps_state &&
1326 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1327 (sta->flags & WLAN_STA_WPS))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 /*
1329 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1330 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1331 * authentication in this BSS.
1332 */
1333 force_1x = 1;
1334 }
1335#endif /* CONFIG_WPS */
1336
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001337 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001338 wpa_printf(MSG_DEBUG,
1339 "IEEE 802.1X: Ignore STA - 802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001340 /*
1341 * Clear any possible EAPOL authenticator state to support
1342 * reassociation change from WPS to PSK.
1343 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001344 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 return;
1346 }
1347
1348 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001349 if (key_mgmt != -1 &&
1350 (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1351 key_mgmt == WPA_KEY_MGMT_DPP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001353 /*
1354 * Clear any possible EAPOL authenticator state to support
1355 * reassociation change from WPA-EAP to PSK.
1356 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001357 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 return;
1359 }
1360
Hai Shalomc3565922019-10-28 11:58:20 -07001361 if (!sta->eapol_sm) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1363 HOSTAPD_LEVEL_DEBUG, "start authentication");
1364 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
Hai Shalomc3565922019-10-28 11:58:20 -07001365 if (!sta->eapol_sm) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 hostapd_logger(hapd, sta->addr,
1367 HOSTAPD_MODULE_IEEE8021X,
1368 HOSTAPD_LEVEL_INFO,
1369 "failed to allocate state machine");
1370 return;
1371 }
1372 reassoc = 0;
1373 }
1374
1375#ifdef CONFIG_WPS
1376 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001377 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1378 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001380 * Delay EAPOL frame transmission until a possible WPS STA
1381 * initiates the handshake with EAPOL-Start. Only allow the
1382 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 */
Hai Shalomc3565922019-10-28 11:58:20 -07001384 wpa_printf(MSG_DEBUG,
1385 "WPS: Do not start EAPOL until EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1387 }
1388#endif /* CONFIG_WPS */
1389
Hai Shalome21d4e82020-04-29 16:34:06 -07001390 sta->eapol_sm->eap_if->portEnabled = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001392#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 if (sta->auth_alg == WLAN_AUTH_FT) {
1394 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1395 HOSTAPD_LEVEL_DEBUG,
1396 "PMK from FT - skip IEEE 802.1X/EAP");
1397 /* Setup EAPOL state machines to already authenticated state
1398 * because of existing FT information from R0KH. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001399 sta->eapol_sm->keyRun = true;
1400 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1402 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001403 sta->eapol_sm->authSuccess = true;
1404 sta->eapol_sm->authFail = false;
1405 sta->eapol_sm->portValid = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 if (sta->eapol_sm->eap)
1407 eap_sm_notify_cached(sta->eapol_sm->eap);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001408 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 return;
1410 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001411#endif /* CONFIG_IEEE80211R_AP */
1412
1413#ifdef CONFIG_FILS
1414 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1415 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1416 sta->auth_alg == WLAN_AUTH_FILS_PK) {
1417 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1418 HOSTAPD_LEVEL_DEBUG,
1419 "PMK from FILS - skip IEEE 802.1X/EAP");
1420 /* Setup EAPOL state machines to already authenticated state
1421 * because of existing FILS information. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001422 sta->eapol_sm->keyRun = true;
1423 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001424 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1425 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001426 sta->eapol_sm->authSuccess = true;
1427 sta->eapol_sm->authFail = false;
1428 sta->eapol_sm->portValid = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001429 if (sta->eapol_sm->eap)
1430 eap_sm_notify_cached(sta->eapol_sm->eap);
Hai Shalom81f62d82019-07-22 12:10:00 -07001431 wpa_auth_set_ptk_rekey_timer(sta->wpa_sm);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001432 return;
1433 }
1434#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435
1436 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1437 if (pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1439 HOSTAPD_LEVEL_DEBUG,
1440 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1441 /* Setup EAPOL state machines to already authenticated state
1442 * because of existing PMKSA information in the cache. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001443 sta->eapol_sm->keyRun = true;
1444 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1446 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001447 sta->eapol_sm->authSuccess = true;
1448 sta->eapol_sm->authFail = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 if (sta->eapol_sm->eap)
1450 eap_sm_notify_cached(sta->eapol_sm->eap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001451 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001452 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 } else {
1454 if (reassoc) {
1455 /*
1456 * Force EAPOL state machines to start
1457 * re-authentication without having to wait for the
1458 * Supplicant to send EAPOL-Start.
1459 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001460 sta->eapol_sm->reAuthenticate = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 }
1462 eapol_auth_step(sta->eapol_sm);
1463 }
1464}
1465
1466
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001467void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468{
1469 struct eapol_state_machine *sm = sta->eapol_sm;
1470
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001471#ifdef CONFIG_HS20
1472 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1473#endif /* CONFIG_HS20 */
1474
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001475 if (sta->pending_eapol_rx) {
1476 wpabuf_free(sta->pending_eapol_rx->buf);
1477 os_free(sta->pending_eapol_rx);
1478 sta->pending_eapol_rx = NULL;
1479 }
1480
Hai Shalomc3565922019-10-28 11:58:20 -07001481 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 return;
1483
1484 sta->eapol_sm = NULL;
1485
1486#ifndef CONFIG_NO_RADIUS
1487 radius_msg_free(sm->last_recv_radius);
1488 radius_free_class(&sm->radius_class);
1489#endif /* CONFIG_NO_RADIUS */
1490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 eapol_auth_free(sm);
1492}
1493
1494
1495#ifndef CONFIG_NO_RADIUS
1496static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1497 struct sta_info *sta)
1498{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001499 struct wpabuf *eap;
1500 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 int eap_type = -1;
1502 char buf[64];
1503 struct radius_msg *msg;
1504 struct eapol_state_machine *sm = sta->eapol_sm;
1505
Hai Shalomc3565922019-10-28 11:58:20 -07001506 if (!sm || !sm->last_recv_radius) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 if (sm)
Hai Shalome21d4e82020-04-29 16:34:06 -07001508 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 return;
1510 }
1511
1512 msg = sm->last_recv_radius;
1513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001514 eap = radius_msg_get_eap(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07001515 if (!eap) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 /* RFC 3579, Chap. 2.6.3:
1517 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1518 * attribute */
1519 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001520 HOSTAPD_LEVEL_WARNING,
1521 "could not extract EAP-Message from RADIUS message");
Hai Shalome21d4e82020-04-29 16:34:06 -07001522 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 return;
1524 }
1525
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001526 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001528 HOSTAPD_LEVEL_WARNING,
1529 "too short EAP packet received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001530 wpabuf_free(eap);
Hai Shalome21d4e82020-04-29 16:34:06 -07001531 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 return;
1533 }
1534
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001535 if (wpabuf_len(eap) > sizeof(*hdr))
1536 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001538 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 switch (hdr->code) {
1540 case EAP_CODE_REQUEST:
1541 if (eap_type >= 0)
1542 sm->eap_type_authsrv = eap_type;
1543 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001544 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 break;
1546 case EAP_CODE_RESPONSE:
1547 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001548 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549 break;
1550 case EAP_CODE_SUCCESS:
1551 os_strlcpy(buf, "EAP Success", sizeof(buf));
1552 break;
1553 case EAP_CODE_FAILURE:
1554 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1555 break;
1556 default:
1557 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1558 break;
1559 }
1560 buf[sizeof(buf) - 1] = '\0';
1561 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001562 HOSTAPD_LEVEL_DEBUG,
1563 "decapsulated EAP packet (code=%d id=%d len=%d) from RADIUS server: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1565 buf);
Hai Shalome21d4e82020-04-29 16:34:06 -07001566 sm->eap_if->aaaEapReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567
1568 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001569 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570}
1571
1572
1573static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1574 struct sta_info *sta, struct radius_msg *msg,
1575 struct radius_msg *req,
1576 const u8 *shared_secret,
1577 size_t shared_secret_len)
1578{
1579 struct radius_ms_mppe_keys *keys;
Hai Shalom81f62d82019-07-22 12:10:00 -07001580 u8 *buf;
1581 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -07001583
1584 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 return;
1586
1587 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1588 shared_secret_len);
1589
1590 if (keys && keys->send && keys->recv) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001591 len = keys->send_len + keys->recv_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1593 keys->send, keys->send_len);
1594 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1595 keys->recv, keys->recv_len);
1596
1597 os_free(sm->eap_if->aaaEapKeyData);
1598 sm->eap_if->aaaEapKeyData = os_malloc(len);
1599 if (sm->eap_if->aaaEapKeyData) {
1600 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1601 keys->recv_len);
1602 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1603 keys->send, keys->send_len);
1604 sm->eap_if->aaaEapKeyDataLen = len;
Hai Shalome21d4e82020-04-29 16:34:06 -07001605 sm->eap_if->aaaEapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001607 } else {
1608 wpa_printf(MSG_DEBUG,
1609 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1610 keys, keys ? keys->send : NULL,
1611 keys ? keys->recv : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612 }
1613
1614 if (keys) {
1615 os_free(keys->send);
1616 os_free(keys->recv);
1617 os_free(keys);
1618 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001619
1620 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
1621 NULL) == 0) {
1622 os_free(sm->eap_if->eapSessionId);
1623 sm->eap_if->eapSessionId = os_memdup(buf, len);
1624 if (sm->eap_if->eapSessionId) {
1625 sm->eap_if->eapSessionIdLen = len;
1626 wpa_hexdump(MSG_DEBUG, "EAP-Key Name",
1627 sm->eap_if->eapSessionId,
1628 sm->eap_if->eapSessionIdLen);
1629 }
1630 } else {
1631 sm->eap_if->eapSessionIdLen = 0;
1632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633}
1634
1635
1636static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1637 struct sta_info *sta,
1638 struct radius_msg *msg)
1639{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001640 u8 *attr_class;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 size_t class_len;
1642 struct eapol_state_machine *sm = sta->eapol_sm;
1643 int count, i;
1644 struct radius_attr_data *nclass;
1645 size_t nclass_count;
1646
Hai Shalomc3565922019-10-28 11:58:20 -07001647 if (!hapd->conf->radius->acct_server || !hapd->radius || !sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 return;
1649
1650 radius_free_class(&sm->radius_class);
1651 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1652 if (count <= 0)
1653 return;
1654
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001655 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Hai Shalomc3565922019-10-28 11:58:20 -07001656 if (!nclass)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 return;
1658
1659 nclass_count = 0;
1660
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001661 attr_class = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 for (i = 0; i < count; i++) {
1663 do {
1664 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001665 &attr_class, &class_len,
1666 attr_class) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 i = count;
1668 break;
1669 }
1670 } while (class_len < 1);
1671
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001672 nclass[nclass_count].data = os_memdup(attr_class, class_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001673 if (!nclass[nclass_count].data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 break;
1675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 nclass[nclass_count].len = class_len;
1677 nclass_count++;
1678 }
1679
1680 sm->radius_class.attr = nclass;
1681 sm->radius_class.count = nclass_count;
Hai Shalomc3565922019-10-28 11:58:20 -07001682 wpa_printf(MSG_DEBUG,
1683 "IEEE 802.1X: Stored %lu RADIUS Class attributes for "
1684 MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 (unsigned long) sm->radius_class.count,
1686 MAC2STR(sta->addr));
1687}
1688
1689
1690/* Update sta->identity based on User-Name attribute in Access-Accept */
1691static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1692 struct sta_info *sta,
1693 struct radius_msg *msg)
1694{
1695 u8 *buf, *identity;
1696 size_t len;
1697 struct eapol_state_machine *sm = sta->eapol_sm;
1698
Hai Shalomc3565922019-10-28 11:58:20 -07001699 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 return;
1701
1702 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1703 NULL) < 0)
1704 return;
1705
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001706 identity = (u8 *) dup_binstr(buf, len);
Hai Shalomc3565922019-10-28 11:58:20 -07001707 if (!identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 return;
1709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001711 HOSTAPD_LEVEL_DEBUG,
1712 "old identity '%s' updated with User-Name from Access-Accept '%s'",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 sm->identity ? (char *) sm->identity : "N/A",
1714 (char *) identity);
1715
1716 os_free(sm->identity);
1717 sm->identity = identity;
1718 sm->identity_len = len;
1719}
1720
1721
Dmitry Shmidt04949592012-07-19 12:16:46 -07001722/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1723static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1724 struct sta_info *sta,
1725 struct radius_msg *msg)
1726{
1727 struct eapol_state_machine *sm = sta->eapol_sm;
1728 struct wpabuf *cui;
1729 u8 *buf;
1730 size_t len;
1731
Hai Shalomc3565922019-10-28 11:58:20 -07001732 if (!sm)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001733 return;
1734
1735 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1736 &buf, &len, NULL) < 0)
1737 return;
1738
1739 cui = wpabuf_alloc_copy(buf, len);
Hai Shalomc3565922019-10-28 11:58:20 -07001740 if (!cui)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001741 return;
1742
1743 wpabuf_free(sm->radius_cui);
1744 sm->radius_cui = cui;
1745}
1746
1747
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001748#ifdef CONFIG_HS20
1749
1750static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1751{
1752 sta->remediation = 1;
1753 os_free(sta->remediation_url);
1754 if (len > 2) {
1755 sta->remediation_url = os_malloc(len);
1756 if (!sta->remediation_url)
1757 return;
1758 sta->remediation_method = pos[0];
1759 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1760 sta->remediation_url[len - 1] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001761 wpa_printf(MSG_DEBUG,
1762 "HS 2.0: Subscription remediation needed for "
1763 MACSTR " - server method %u URL %s",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001764 MAC2STR(sta->addr), sta->remediation_method,
1765 sta->remediation_url);
1766 } else {
1767 sta->remediation_url = NULL;
Hai Shalomc3565922019-10-28 11:58:20 -07001768 wpa_printf(MSG_DEBUG,
1769 "HS 2.0: Subscription remediation needed for "
1770 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001771 }
1772 /* TODO: assign the STA into remediation VLAN or add filtering */
1773}
1774
1775
1776static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001777 struct sta_info *sta, const u8 *pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001778 size_t len)
1779{
Sunil Ravi77d572f2023-01-17 23:58:31 +00001780 size_t url_len;
1781 unsigned int timeout;
1782
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001783 if (len < 3)
1784 return; /* Malformed information */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001785 url_len = len - 3;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001786 sta->hs20_deauth_requested = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001787 sta->hs20_deauth_on_ack = url_len == 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001788 wpa_printf(MSG_DEBUG,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001789 "HS 2.0: Deauthentication request - Code %u Re-auth Delay %u URL length %zu",
1790 *pos, WPA_GET_LE16(pos + 1), url_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001791 wpabuf_free(sta->hs20_deauth_req);
1792 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1793 if (sta->hs20_deauth_req) {
1794 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
Sunil Ravi77d572f2023-01-17 23:58:31 +00001795 wpabuf_put_u8(sta->hs20_deauth_req, url_len);
1796 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, url_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001797 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001798 timeout = hapd->conf->hs20_deauth_req_timeout;
1799 /* If there is no URL, no need to provide time to fetch it. Use a short
1800 * timeout here to allow maximum time for completing 4-way handshake and
1801 * WNM-Notification delivery. Acknowledgement of the frame will result
1802 * in cutting this wait further. */
1803 if (!url_len && timeout > 2)
1804 timeout = 2;
1805 ap_sta_session_timeout(hapd, sta, timeout);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001806}
1807
1808
1809static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1810 struct sta_info *sta, u8 *pos,
1811 size_t len, int session_timeout)
1812{
1813 unsigned int swt;
1814 int warning_time, beacon_int;
1815
1816 if (len < 1)
1817 return; /* Malformed information */
1818 os_free(sta->hs20_session_info_url);
1819 sta->hs20_session_info_url = os_malloc(len);
Hai Shalomc3565922019-10-28 11:58:20 -07001820 if (!sta->hs20_session_info_url)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001821 return;
1822 swt = pos[0];
1823 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1824 sta->hs20_session_info_url[len - 1] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001825 wpa_printf(MSG_DEBUG,
1826 "HS 2.0: Session Information URL='%s' SWT=%u (session_timeout=%d)",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001827 sta->hs20_session_info_url, swt, session_timeout);
1828 if (session_timeout < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -07001829 wpa_printf(MSG_DEBUG,
1830 "HS 2.0: No Session-Timeout set - ignore session info URL");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001831 return;
1832 }
1833 if (swt == 255)
1834 swt = 1; /* Use one minute as the AP selected value */
1835
1836 if ((unsigned int) session_timeout < swt * 60)
1837 warning_time = 0;
1838 else
1839 warning_time = session_timeout - swt * 60;
1840
1841 beacon_int = hapd->iconf->beacon_int;
1842 if (beacon_int < 1)
1843 beacon_int = 100; /* best guess */
1844 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1845 if (sta->hs20_disassoc_timer > 65535)
1846 sta->hs20_disassoc_timer = 65535;
1847
1848 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1849}
1850
Roshan Pius3a1667e2018-07-03 15:17:14 -07001851
1852static void ieee802_1x_hs20_t_c_filtering(struct hostapd_data *hapd,
1853 struct sta_info *sta, u8 *pos,
1854 size_t len)
1855{
1856 if (len < 4)
1857 return; /* Malformed information */
1858 wpa_printf(MSG_DEBUG,
1859 "HS 2.0: Terms and Conditions filtering %02x %02x %02x %02x",
1860 pos[0], pos[1], pos[2], pos[3]);
1861 hs20_t_c_filtering(hapd, sta, pos[0] & BIT(0));
1862}
1863
1864
1865static void ieee802_1x_hs20_t_c_url(struct hostapd_data *hapd,
1866 struct sta_info *sta, u8 *pos, size_t len)
1867{
1868 os_free(sta->t_c_url);
1869 sta->t_c_url = os_malloc(len + 1);
1870 if (!sta->t_c_url)
1871 return;
1872 os_memcpy(sta->t_c_url, pos, len);
1873 sta->t_c_url[len] = '\0';
1874 wpa_printf(MSG_DEBUG,
1875 "HS 2.0: Terms and Conditions URL %s", sta->t_c_url);
1876}
1877
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001878#endif /* CONFIG_HS20 */
1879
1880
1881static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1882 struct sta_info *sta,
1883 struct radius_msg *msg,
1884 int session_timeout)
1885{
1886#ifdef CONFIG_HS20
1887 u8 *buf, *pos, *end, type, sublen;
1888 size_t len;
1889
1890 buf = NULL;
1891 sta->remediation = 0;
1892 sta->hs20_deauth_requested = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001893 sta->hs20_deauth_on_ack = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001894
1895 for (;;) {
1896 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1897 &buf, &len, buf) < 0)
1898 break;
1899 if (len < 6)
1900 continue;
1901 pos = buf;
1902 end = buf + len;
1903 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1904 continue;
1905 pos += 4;
1906
1907 type = *pos++;
1908 sublen = *pos++;
1909 if (sublen < 2)
1910 continue; /* invalid length */
1911 sublen -= 2; /* skip header */
1912 if (pos + sublen > end)
1913 continue; /* invalid WFA VSA */
1914
1915 switch (type) {
1916 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1917 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1918 break;
1919 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1920 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1921 break;
1922 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1923 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1924 session_timeout);
1925 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001926 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING:
1927 ieee802_1x_hs20_t_c_filtering(hapd, sta, pos, sublen);
1928 break;
1929 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL:
1930 ieee802_1x_hs20_t_c_url(hapd, sta, pos, sublen);
1931 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001932 }
1933 }
1934#endif /* CONFIG_HS20 */
1935}
1936
1937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938struct sta_id_search {
1939 u8 identifier;
1940 struct eapol_state_machine *sm;
1941};
1942
1943
1944static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1945 struct sta_info *sta,
1946 void *ctx)
1947{
1948 struct sta_id_search *id_search = ctx;
1949 struct eapol_state_machine *sm = sta->eapol_sm;
1950
1951 if (sm && sm->radius_identifier >= 0 &&
1952 sm->radius_identifier == id_search->identifier) {
1953 id_search->sm = sm;
1954 return 1;
1955 }
1956 return 0;
1957}
1958
1959
1960static struct eapol_state_machine *
1961ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1962{
1963 struct sta_id_search id_search;
Hai Shalomc3565922019-10-28 11:58:20 -07001964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 id_search.identifier = identifier;
1966 id_search.sm = NULL;
1967 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1968 return id_search.sm;
1969}
1970
1971
Hai Shalom74f70d42019-02-11 14:42:39 -08001972#ifndef CONFIG_NO_VLAN
1973static int ieee802_1x_update_vlan(struct radius_msg *msg,
1974 struct hostapd_data *hapd,
1975 struct sta_info *sta)
1976{
1977 struct vlan_description vlan_desc;
1978
1979 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1980 vlan_desc.notempty = !!radius_msg_get_vlanid(msg, &vlan_desc.untagged,
1981 MAX_NUM_TAGGED_VLAN,
1982 vlan_desc.tagged);
1983
1984 if (vlan_desc.notempty &&
1985 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
Hai Shalome21d4e82020-04-29 16:34:06 -07001986 sta->eapol_sm->authFail = true;
Hai Shalom74f70d42019-02-11 14:42:39 -08001987 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1988 HOSTAPD_LEVEL_INFO,
1989 "Invalid VLAN %d%s received from RADIUS server",
1990 vlan_desc.untagged,
1991 vlan_desc.tagged[0] ? "+" : "");
1992 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1993 ap_sta_set_vlan(hapd, sta, &vlan_desc);
1994 return -1;
1995 }
1996
1997 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1998 !vlan_desc.notempty) {
Hai Shalome21d4e82020-04-29 16:34:06 -07001999 sta->eapol_sm->authFail = true;
Hai Shalom74f70d42019-02-11 14:42:39 -08002000 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2001 HOSTAPD_LEVEL_INFO,
2002 "authentication server did not include required VLAN ID in Access-Accept");
2003 return -1;
2004 }
2005
2006 return ap_sta_set_vlan(hapd, sta, &vlan_desc);
2007}
2008#endif /* CONFIG_NO_VLAN */
2009
2010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011/**
2012 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
2013 * @msg: RADIUS response message
2014 * @req: RADIUS request message
2015 * @shared_secret: RADIUS shared secret
2016 * @shared_secret_len: Length of shared_secret in octets
2017 * @data: Context data (struct hostapd_data *)
2018 * Returns: Processing status
2019 */
2020static RadiusRxResult
2021ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
2022 const u8 *shared_secret, size_t shared_secret_len,
2023 void *data)
2024{
2025 struct hostapd_data *hapd = data;
2026 struct sta_info *sta;
2027 u32 session_timeout = 0, termination_action, acct_interim_interval;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002028 int session_timeout_set;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002029 u32 reason_code;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 struct eapol_state_machine *sm;
2031 int override_eapReq = 0;
2032 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
2033
2034 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
Hai Shalomc3565922019-10-28 11:58:20 -07002035 if (!sm) {
2036 wpa_printf(MSG_DEBUG,
2037 "IEEE 802.1X: Could not find matching station for this RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 return RADIUS_RX_UNKNOWN;
2039 }
2040 sta = sm->sta;
2041
2042 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
2043 * present when packet contains an EAP-Message attribute */
2044 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
2045 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
2046 0) < 0 &&
2047 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -07002048 wpa_printf(MSG_DEBUG,
2049 "Allowing RADIUS Access-Reject without Message-Authenticator since it does not include EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
2051 req, 1)) {
Hai Shalomc3565922019-10-28 11:58:20 -07002052 wpa_printf(MSG_INFO,
2053 "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 return RADIUS_RX_INVALID_AUTHENTICATOR;
2055 }
2056
2057 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
2058 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
2059 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002060 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 return RADIUS_RX_UNKNOWN;
2062 }
2063
2064 sm->radius_identifier = -1;
2065 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
2066 MAC2STR(sta->addr));
2067
2068 radius_msg_free(sm->last_recv_radius);
2069 sm->last_recv_radius = msg;
2070
2071 session_timeout_set =
2072 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
2073 &session_timeout);
2074 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
2075 &termination_action))
2076 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
2077
2078 if (hapd->conf->acct_interim_interval == 0 &&
2079 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
2080 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
2081 &acct_interim_interval) == 0) {
2082 if (acct_interim_interval < 60) {
2083 hostapd_logger(hapd, sta->addr,
2084 HOSTAPD_MODULE_IEEE8021X,
2085 HOSTAPD_LEVEL_INFO,
Hai Shalomc3565922019-10-28 11:58:20 -07002086 "ignored too small Acct-Interim-Interval %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 acct_interim_interval);
2088 } else
2089 sta->acct_interim_interval = acct_interim_interval;
2090 }
2091
2092
2093 switch (hdr->code) {
2094 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095#ifndef CONFIG_NO_VLAN
Hai Shalom74f70d42019-02-11 14:42:39 -08002096 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
2097 ieee802_1x_update_vlan(msg, hapd, sta) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002098 break;
2099
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002100 if (sta->vlan_id > 0) {
2101 hostapd_logger(hapd, sta->addr,
2102 HOSTAPD_MODULE_RADIUS,
2103 HOSTAPD_LEVEL_INFO,
2104 "VLAN ID %d", sta->vlan_id);
2105 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002106
Dmitry Shmidt83474442015-04-15 13:47:09 -07002107 if ((sta->flags & WLAN_STA_ASSOC) &&
2108 ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08002110#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002112 sta->session_timeout_set = !!session_timeout_set;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002113 os_get_reltime(&sta->session_timeout);
2114 sta->session_timeout.sec += session_timeout;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 /* RFC 3580, Ch. 3.17 */
2117 if (session_timeout_set && termination_action ==
Roshan Pius3a1667e2018-07-03 15:17:14 -07002118 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 sm->reAuthPeriod = session_timeout;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002120 else if (session_timeout_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 ap_sta_session_timeout(hapd, sta, session_timeout);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002122 else
2123 ap_sta_no_session_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124
Hai Shalome21d4e82020-04-29 16:34:06 -07002125 sm->eap_if->aaaSuccess = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 override_eapReq = 1;
2127 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
2128 shared_secret_len);
2129 ieee802_1x_store_radius_class(hapd, sta, msg);
2130 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002131 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002132 ieee802_1x_check_hs20(hapd, sta, msg,
2133 session_timeout_set ?
2134 (int) session_timeout : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 break;
2136 case RADIUS_CODE_ACCESS_REJECT:
Hai Shalome21d4e82020-04-29 16:34:06 -07002137 sm->eap_if->aaaFail = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 override_eapReq = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002139 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_WLAN_REASON_CODE,
2140 &reason_code) == 0) {
2141 wpa_printf(MSG_DEBUG,
2142 "RADIUS server indicated WLAN-Reason-Code %u in Access-Reject for "
2143 MACSTR, reason_code, MAC2STR(sta->addr));
2144 sta->disconnect_reason_code = reason_code;
2145 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 break;
2147 case RADIUS_CODE_ACCESS_CHALLENGE:
Hai Shalome21d4e82020-04-29 16:34:06 -07002148 sm->eap_if->aaaEapReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 if (session_timeout_set) {
2150 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
2151 sm->eap_if->aaaMethodTimeout = session_timeout;
2152 hostapd_logger(hapd, sm->addr,
2153 HOSTAPD_MODULE_IEEE8021X,
2154 HOSTAPD_LEVEL_DEBUG,
Hai Shalomc3565922019-10-28 11:58:20 -07002155 "using EAP timeout of %d seconds (from RADIUS)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 sm->eap_if->aaaMethodTimeout);
2157 } else {
2158 /*
2159 * Use dynamic retransmission behavior per EAP
2160 * specification.
2161 */
2162 sm->eap_if->aaaMethodTimeout = 0;
2163 }
2164 break;
2165 }
2166
2167 ieee802_1x_decapsulate_radius(hapd, sta);
2168 if (override_eapReq)
Hai Shalome21d4e82020-04-29 16:34:06 -07002169 sm->eap_if->aaaEapReq = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002171#ifdef CONFIG_FILS
2172#ifdef NEED_AP_MLME
Hai Shalom60840252021-02-19 19:02:11 -08002173 if (sta->flags &
2174 (WLAN_STA_PENDING_FILS_ERP | WLAN_STA_PENDING_PASN_FILS_ERP)) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002175 /* TODO: Add a PMKSA entry on success? */
2176 ieee802_11_finish_fils_auth(
2177 hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
2178 sm->eap_if->aaaEapReqData,
2179 sm->eap_if->aaaEapKeyData,
2180 sm->eap_if->aaaEapKeyDataLen);
2181 }
2182#endif /* NEED_AP_MLME */
2183#endif /* CONFIG_FILS */
2184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 eapol_auth_step(sm);
2186
2187 return RADIUS_RX_QUEUED;
2188}
2189#endif /* CONFIG_NO_RADIUS */
2190
2191
2192void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
2193{
2194 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -07002195
2196 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 return;
2198
2199 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2200 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
2201
2202#ifndef CONFIG_NO_RADIUS
2203 radius_msg_free(sm->last_recv_radius);
2204 sm->last_recv_radius = NULL;
2205#endif /* CONFIG_NO_RADIUS */
2206
2207 if (sm->eap_if->eapTimeout) {
2208 /*
2209 * Disconnect the STA since it did not reply to the last EAP
2210 * request and we cannot continue EAP processing (EAP-Failure
2211 * could only be sent if the EAP peer actually replied).
2212 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002213 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
2214 MAC2STR(sta->addr));
2215
Hai Shalome21d4e82020-04-29 16:34:06 -07002216 sm->eap_if->portEnabled = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 ap_sta_disconnect(hapd, sta, sta->addr,
2218 WLAN_REASON_PREV_AUTH_NOT_VALID);
2219 }
2220}
2221
2222
Hai Shalomfdcde762020-04-02 11:19:20 -07002223#ifdef CONFIG_WEP
2224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
2226{
2227 struct eapol_authenticator *eapol = hapd->eapol_auth;
2228
2229 if (hapd->conf->default_wep_key_len < 1)
2230 return 0;
2231
2232 os_free(eapol->default_wep_key);
2233 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002234 if (!eapol->default_wep_key ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 random_get_bytes(eapol->default_wep_key,
2236 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002237 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 os_free(eapol->default_wep_key);
2239 eapol->default_wep_key = NULL;
2240 return -1;
2241 }
2242
2243 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
2244 eapol->default_wep_key,
2245 hapd->conf->default_wep_key_len);
2246
2247 return 0;
2248}
2249
2250
2251static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
2252 struct sta_info *sta, void *ctx)
2253{
2254 if (sta->eapol_sm) {
Hai Shalome21d4e82020-04-29 16:34:06 -07002255 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 eapol_auth_step(sta->eapol_sm);
2257 }
2258 return 0;
2259}
2260
2261
2262static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
2263{
2264 struct hostapd_data *hapd = eloop_ctx;
2265 struct eapol_authenticator *eapol = hapd->eapol_auth;
2266
2267 if (eapol->default_wep_key_idx >= 3)
2268 eapol->default_wep_key_idx =
2269 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
2270 else
2271 eapol->default_wep_key_idx++;
2272
2273 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
2274 eapol->default_wep_key_idx);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 if (ieee802_1x_rekey_broadcast(hapd)) {
2277 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002278 HOSTAPD_LEVEL_WARNING,
2279 "failed to generate a new broadcast key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280 os_free(eapol->default_wep_key);
2281 eapol->default_wep_key = NULL;
2282 return;
2283 }
2284
2285 /* TODO: Could setup key for RX here, but change default TX keyid only
2286 * after new broadcast key has been sent to all stations. */
2287 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
2288 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002289 eapol->default_wep_key_idx, 0, 1, NULL, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 eapol->default_wep_key,
Hai Shalomfdcde762020-04-02 11:19:20 -07002291 hapd->conf->default_wep_key_len,
2292 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002294 HOSTAPD_LEVEL_WARNING,
2295 "failed to configure a new broadcast key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 os_free(eapol->default_wep_key);
2297 eapol->default_wep_key = NULL;
2298 return;
2299 }
2300
2301 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
2302
2303 if (hapd->conf->wep_rekeying_period > 0) {
2304 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
2305 ieee802_1x_rekey, hapd, NULL);
2306 }
2307}
2308
Hai Shalomfdcde762020-04-02 11:19:20 -07002309#endif /* CONFIG_WEP */
2310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311
2312static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2313 const u8 *data, size_t datalen)
2314{
2315#ifdef CONFIG_WPS
2316 struct sta_info *sta = sta_ctx;
2317
2318 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2319 WLAN_STA_MAYBE_WPS) {
2320 const u8 *identity;
2321 size_t identity_len;
2322 struct eapol_state_machine *sm = sta->eapol_sm;
2323
2324 identity = eap_get_identity(sm->eap, &identity_len);
2325 if (identity &&
2326 ((identity_len == WSC_ID_ENROLLEE_LEN &&
2327 os_memcmp(identity, WSC_ID_ENROLLEE,
2328 WSC_ID_ENROLLEE_LEN) == 0) ||
2329 (identity_len == WSC_ID_REGISTRAR_LEN &&
2330 os_memcmp(identity, WSC_ID_REGISTRAR,
2331 WSC_ID_REGISTRAR_LEN) == 0))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002332 wpa_printf(MSG_DEBUG,
2333 "WPS: WLAN_STA_MAYBE_WPS -> WLAN_STA_WPS");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 sta->flags |= WLAN_STA_WPS;
2335 }
2336 }
2337#endif /* CONFIG_WPS */
2338
2339 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2340}
2341
2342
2343static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2344 const u8 *data, size_t datalen)
2345{
2346#ifndef CONFIG_NO_RADIUS
2347 struct hostapd_data *hapd = ctx;
2348 struct sta_info *sta = sta_ctx;
2349
2350 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2351#endif /* CONFIG_NO_RADIUS */
2352}
2353
2354
Sunil Ravi640215c2023-06-28 23:08:09 +00002355static bool _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2356 int preauth, int remediation, bool logoff)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357{
2358 struct hostapd_data *hapd = ctx;
2359 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002360
Sunil Ravi640215c2023-06-28 23:08:09 +00002361 if (preauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 rsn_preauth_finished(hapd, sta, success);
Sunil Ravi640215c2023-06-28 23:08:09 +00002363 return false;
2364 }
2365
2366 return ieee802_1x_finished(hapd, sta, success, remediation, logoff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367}
2368
2369
2370static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2371 size_t identity_len, int phase2,
2372 struct eap_user *user)
2373{
2374 struct hostapd_data *hapd = ctx;
2375 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376 int i;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002377 int rv = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002379 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002380 if (!eap_user)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002381 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382
2383 os_memset(user, 0, sizeof(*user));
2384 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386 user->methods[i].vendor = eap_user->methods[i].vendor;
2387 user->methods[i].method = eap_user->methods[i].method;
2388 }
2389
2390 if (eap_user->password) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002391 user->password = os_memdup(eap_user->password,
2392 eap_user->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002393 if (!user->password)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002394 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002396 user->password_hash = eap_user->password_hash;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002397 if (eap_user->salt && eap_user->salt_len) {
2398 user->salt = os_memdup(eap_user->salt,
2399 eap_user->salt_len);
2400 if (!user->salt)
2401 goto out;
2402 user->salt_len = eap_user->salt_len;
2403 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 }
2405 user->force_version = eap_user->force_version;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002406 user->macacl = eap_user->macacl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002408 user->remediation = eap_user->remediation;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002409 rv = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002411out:
2412 if (rv)
2413 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2414
2415 return rv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416}
2417
2418
2419static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2420{
2421 struct hostapd_data *hapd = ctx;
2422 struct sta_info *sta;
Hai Shalomc3565922019-10-28 11:58:20 -07002423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424 sta = ap_get_sta(hapd, addr);
Hai Shalomc3565922019-10-28 11:58:20 -07002425 if (!sta || !sta->eapol_sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426 return 0;
2427 return 1;
2428}
2429
2430
2431static void ieee802_1x_logger(void *ctx, const u8 *addr,
2432 eapol_logger_level level, const char *txt)
2433{
2434#ifndef CONFIG_NO_HOSTAPD_LOGGER
2435 struct hostapd_data *hapd = ctx;
2436 int hlevel;
2437
2438 switch (level) {
2439 case EAPOL_LOGGER_WARNING:
2440 hlevel = HOSTAPD_LEVEL_WARNING;
2441 break;
2442 case EAPOL_LOGGER_INFO:
2443 hlevel = HOSTAPD_LEVEL_INFO;
2444 break;
2445 case EAPOL_LOGGER_DEBUG:
2446 default:
2447 hlevel = HOSTAPD_LEVEL_DEBUG;
2448 break;
2449 }
2450
2451 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2452 txt);
2453#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2454}
2455
2456
2457static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2458 int authorized)
2459{
2460 struct hostapd_data *hapd = ctx;
2461 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2464}
2465
2466
2467static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2468{
2469 struct hostapd_data *hapd = ctx;
2470 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 ieee802_1x_abort_auth(hapd, sta);
2473}
2474
2475
Hai Shalomfdcde762020-04-02 11:19:20 -07002476#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2478{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002479#ifndef CONFIG_FIPS
2480#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 struct hostapd_data *hapd = ctx;
2482 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484 ieee802_1x_tx_key(hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002485#endif /* CONFIG_NO_RC4 */
2486#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487}
Hai Shalomfdcde762020-04-02 11:19:20 -07002488#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489
2490
2491static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2492 enum eapol_event type)
2493{
2494 /* 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 switch (type) {
2498 case EAPOL_AUTH_SM_CHANGE:
2499 wpa_auth_sm_notify(sta->wpa_sm);
2500 break;
2501 case EAPOL_AUTH_REAUTHENTICATE:
2502 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2503 break;
2504 }
2505}
2506
2507
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002508#ifdef CONFIG_ERP
2509
2510static struct eap_server_erp_key *
2511ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2512{
2513 struct hostapd_data *hapd = ctx;
2514 struct eap_server_erp_key *erp;
2515
2516 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2517 list) {
2518 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2519 return erp;
2520 }
2521
2522 return NULL;
2523}
2524
2525
2526static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2527{
2528 struct hostapd_data *hapd = ctx;
2529
2530 dl_list_add(&hapd->erp_keys, &erp->list);
2531 return 0;
2532}
2533
2534#endif /* CONFIG_ERP */
2535
2536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537int ieee802_1x_init(struct hostapd_data *hapd)
2538{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 struct eapol_auth_config conf;
2540 struct eapol_auth_cb cb;
2541
Sunil Raviaf399a82024-05-05 20:56:55 +00002542#ifdef CONFIG_IEEE80211BE
2543 if (!hostapd_mld_is_first_bss(hapd)) {
2544 struct hostapd_data *first;
2545
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002546 wpa_printf(MSG_DEBUG,
2547 "MLD: Using IEEE 802.1X state machine of the first BSS");
2548
Sunil Raviaf399a82024-05-05 20:56:55 +00002549 first = hostapd_mld_get_first_bss(hapd);
2550 if (!first)
2551 return -1;
2552 hapd->eapol_auth = first->eapol_auth;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002553 return 0;
2554 }
Sunil Raviaf399a82024-05-05 20:56:55 +00002555#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002557 dl_list_init(&hapd->erp_keys);
2558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 os_memset(&conf, 0, sizeof(conf));
Hai Shalomc3565922019-10-28 11:58:20 -07002560 conf.eap_cfg = hapd->eap_cfg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 conf.ctx = hapd;
2562 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2563 conf.wpa = hapd->conf->wpa;
Hai Shalomfdcde762020-04-02 11:19:20 -07002564#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
Hai Shalomfdcde762020-04-02 11:19:20 -07002566#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2568 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002569 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2570 conf.erp_domain = hapd->conf->erp_domain;
Sunil Ravia04bd252022-05-02 22:54:18 -07002571#ifdef CONFIG_TESTING_OPTIONS
2572 conf.eap_skip_prot_success = hapd->conf->eap_skip_prot_success;
2573#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574
2575 os_memset(&cb, 0, sizeof(cb));
2576 cb.eapol_send = ieee802_1x_eapol_send;
2577 cb.aaa_send = ieee802_1x_aaa_send;
2578 cb.finished = _ieee802_1x_finished;
2579 cb.get_eap_user = ieee802_1x_get_eap_user;
2580 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2581 cb.logger = ieee802_1x_logger;
2582 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2583 cb.abort_auth = _ieee802_1x_abort_auth;
Hai Shalomfdcde762020-04-02 11:19:20 -07002584#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585 cb.tx_key = _ieee802_1x_tx_key;
Hai Shalomfdcde762020-04-02 11:19:20 -07002586#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 cb.eapol_event = ieee802_1x_eapol_event;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002588#ifdef CONFIG_ERP
2589 cb.erp_get_key = ieee802_1x_erp_get_key;
2590 cb.erp_add_key = ieee802_1x_erp_add_key;
2591#endif /* CONFIG_ERP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592
2593 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
Hai Shalomc3565922019-10-28 11:58:20 -07002594 if (!hapd->eapol_auth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002595 return -1;
2596
2597 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2598 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2599 return -1;
2600
2601#ifndef CONFIG_NO_RADIUS
2602 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2603 ieee802_1x_receive_auth, hapd))
2604 return -1;
2605#endif /* CONFIG_NO_RADIUS */
2606
Hai Shalomfdcde762020-04-02 11:19:20 -07002607#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 if (hapd->conf->default_wep_key_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002609 int i;
2610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611 for (i = 0; i < 4; i++)
2612 hostapd_drv_set_key(hapd->conf->iface, hapd,
Hai Shalomfdcde762020-04-02 11:19:20 -07002613 WPA_ALG_NONE, NULL, i, 0, 0, NULL,
2614 0, NULL, 0, KEY_FLAG_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615
2616 ieee802_1x_rekey(hapd, NULL);
2617
Hai Shalomc3565922019-10-28 11:58:20 -07002618 if (!hapd->eapol_auth->default_wep_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 return -1;
2620 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002621#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622
2623 return 0;
2624}
2625
2626
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002627void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2628{
2629 struct eap_server_erp_key *erp;
2630
2631 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2632 list)) != NULL) {
2633 dl_list_del(&erp->list);
2634 bin_clear_free(erp, sizeof(*erp));
2635 }
2636}
2637
2638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639void ieee802_1x_deinit(struct hostapd_data *hapd)
2640{
Sunil Raviaf399a82024-05-05 20:56:55 +00002641#ifdef CONFIG_IEEE80211BE
2642 if (!hostapd_mld_is_first_bss(hapd)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002643 wpa_printf(MSG_DEBUG,
2644 "MLD: Deinit IEEE 802.1X state machine of a non-first BSS");
2645
2646 hapd->eapol_auth = NULL;
2647 return;
2648 }
Sunil Raviaf399a82024-05-05 20:56:55 +00002649#endif /* CONFIG_IEEE80211BE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002650
Hai Shalomfdcde762020-04-02 11:19:20 -07002651#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07002653#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002655 if (hapd->driver && hapd->drv_priv &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2657 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2658
2659 eapol_auth_deinit(hapd->eapol_auth);
2660 hapd->eapol_auth = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002661
2662 ieee802_1x_erp_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663}
2664
2665
2666int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2667 const u8 *buf, size_t len, int ack)
2668{
2669 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 u8 *pos;
2671 const unsigned char rfc1042_hdr[ETH_ALEN] =
2672 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2673
Hai Shalomc3565922019-10-28 11:58:20 -07002674 if (!sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677 return 0;
2678
2679 hdr = (struct ieee80211_hdr *) buf;
2680 pos = (u8 *) (hdr + 1);
2681 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2682 return 0;
2683 pos += sizeof(rfc1042_hdr);
2684 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2685 return 0;
2686 pos += 2;
2687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002688 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2689 ack);
2690}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002692
2693int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2694 const u8 *buf, int len, int ack)
2695{
2696 const struct ieee802_1x_hdr *xhdr =
2697 (const struct ieee802_1x_hdr *) buf;
2698 const u8 *pos = buf + sizeof(*xhdr);
2699 struct ieee802_1x_eapol_key *key;
2700
2701 if (len < (int) sizeof(*xhdr))
2702 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -07002703 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
2704 " TX status - version=%d type=%d length=%d - ack=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2706 be_to_host16(xhdr->length), ack);
2707
Dmitry Shmidt29333592017-01-09 12:27:11 -08002708#ifdef CONFIG_WPS
2709 if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2710 (sta->flags & WLAN_STA_WPS) &&
2711 ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2712 wpa_printf(MSG_DEBUG,
2713 "WPS: Indicate EAP completion on ACK for EAP-Failure");
2714 hostapd_wps_eap_completed(hapd);
2715 }
2716#endif /* CONFIG_WPS */
2717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002718 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2719 return 0;
2720
2721 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 const struct wpa_eapol_key *wpa;
Hai Shalomc3565922019-10-28 11:58:20 -07002723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002724 wpa = (const struct wpa_eapol_key *) pos;
2725 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2726 wpa->type == EAPOL_KEY_TYPE_WPA)
2727 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2728 sta->wpa_sm, ack);
2729 }
2730
2731 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2732 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002733 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 * packets couple of times because otherwise STA keys become
2735 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 key = (struct ieee802_1x_eapol_key *) pos;
2738 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002739 HOSTAPD_LEVEL_DEBUG,
2740 "did not Ack EAPOL-Key frame (%scast index=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 key->key_index & BIT(7) ? "uni" : "broad",
2742 key->key_index & ~BIT(7));
2743 /* TODO: re-send EAPOL-Key couple of times (with short delay
2744 * between them?). If all attempt fail, report error and
2745 * deauthenticate STA so that it will get new keys when
2746 * authenticating again (e.g., after returning in range).
2747 * Separate limit/transmit state needed both for unicast and
2748 * broadcast keys(?) */
2749 }
2750 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2751 * to here and change the key only if the EAPOL-Key packet was Acked.
2752 */
2753
2754 return 1;
2755}
2756
2757
2758u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2759{
Hai Shalomc3565922019-10-28 11:58:20 -07002760 if (!sm || !sm->identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 return NULL;
2762
2763 *len = sm->identity_len;
2764 return sm->identity;
2765}
2766
2767
2768u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2769 int idx)
2770{
Hai Shalomc3565922019-10-28 11:58:20 -07002771 if (!sm || !sm->radius_class.attr ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 idx >= (int) sm->radius_class.count)
2773 return NULL;
2774
2775 *len = sm->radius_class.attr[idx].len;
2776 return sm->radius_class.attr[idx].data;
2777}
2778
2779
Dmitry Shmidt04949592012-07-19 12:16:46 -07002780struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2781{
Hai Shalomc3565922019-10-28 11:58:20 -07002782 if (!sm)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002783 return NULL;
2784 return sm->radius_cui;
2785}
2786
2787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2789{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002790 *len = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07002791 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792 return NULL;
2793
2794 *len = sm->eap_if->eapKeyDataLen;
2795 return sm->eap_if->eapKeyData;
2796}
2797
2798
Hai Shalom81f62d82019-07-22 12:10:00 -07002799#ifdef CONFIG_MACSEC
2800const u8 * ieee802_1x_get_session_id(struct eapol_state_machine *sm,
2801 size_t *len)
2802{
2803 *len = 0;
2804 if (!sm || !sm->eap_if)
2805 return NULL;
2806
2807 *len = sm->eap_if->eapSessionIdLen;
2808 return sm->eap_if->eapSessionId;
2809}
2810#endif /* CONFIG_MACSEC */
2811
2812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
Hai Shalome21d4e82020-04-29 16:34:06 -07002814 bool enabled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815{
Hai Shalomc3565922019-10-28 11:58:20 -07002816 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817 return;
Hai Shalome21d4e82020-04-29 16:34:06 -07002818 sm->eap_if->portEnabled = enabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002819 eapol_auth_step(sm);
2820}
2821
2822
Hai Shalome21d4e82020-04-29 16:34:06 -07002823void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm, bool valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824{
Hai Shalomc3565922019-10-28 11:58:20 -07002825 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826 return;
Hai Shalome21d4e82020-04-29 16:34:06 -07002827 sm->portValid = valid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828 eapol_auth_step(sm);
2829}
2830
2831
Hai Shalome21d4e82020-04-29 16:34:06 -07002832void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, bool pre_auth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833{
Hai Shalomc3565922019-10-28 11:58:20 -07002834 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835 return;
2836 if (pre_auth)
2837 sm->flags |= EAPOL_SM_PREAUTH;
2838 else
2839 sm->flags &= ~EAPOL_SM_PREAUTH;
2840}
2841
2842
Hai Shalome21d4e82020-04-29 16:34:06 -07002843static const char * bool_txt(bool val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002845 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846}
2847
2848
2849int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2850{
2851 /* TODO */
2852 return 0;
2853}
2854
2855
2856int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2857 char *buf, size_t buflen)
2858{
2859 int len = 0, ret;
2860 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002861 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002862 const char *name1;
2863 const char *name2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002864 char *identity_buf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865
Hai Shalomc3565922019-10-28 11:58:20 -07002866 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 return 0;
2868
2869 ret = os_snprintf(buf + len, buflen - len,
2870 "dot1xPaePortNumber=%d\n"
2871 "dot1xPaePortProtocolVersion=%d\n"
2872 "dot1xPaePortCapabilities=1\n"
2873 "dot1xPaePortInitialize=%d\n"
2874 "dot1xPaePortReauthenticate=FALSE\n",
2875 sta->aid,
2876 EAPOL_VERSION,
2877 sm->initialize);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002878 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 return len;
2880 len += ret;
2881
2882 /* dot1xAuthConfigTable */
2883 ret = os_snprintf(buf + len, buflen - len,
2884 "dot1xAuthPaeState=%d\n"
2885 "dot1xAuthBackendAuthState=%d\n"
2886 "dot1xAuthAdminControlledDirections=%d\n"
2887 "dot1xAuthOperControlledDirections=%d\n"
2888 "dot1xAuthAuthControlledPortStatus=%d\n"
2889 "dot1xAuthAuthControlledPortControl=%d\n"
2890 "dot1xAuthQuietPeriod=%u\n"
2891 "dot1xAuthServerTimeout=%u\n"
2892 "dot1xAuthReAuthPeriod=%u\n"
2893 "dot1xAuthReAuthEnabled=%s\n"
2894 "dot1xAuthKeyTxEnabled=%s\n",
2895 sm->auth_pae_state + 1,
2896 sm->be_auth_state + 1,
2897 sm->adminControlledDirections,
2898 sm->operControlledDirections,
2899 sm->authPortStatus,
2900 sm->portControl,
2901 sm->quietPeriod,
2902 sm->serverTimeout,
2903 sm->reAuthPeriod,
2904 bool_txt(sm->reAuthEnabled),
2905 bool_txt(sm->keyTxEnabled));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002906 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 return len;
2908 len += ret;
2909
2910 /* dot1xAuthStatsTable */
2911 ret = os_snprintf(buf + len, buflen - len,
2912 "dot1xAuthEapolFramesRx=%u\n"
2913 "dot1xAuthEapolFramesTx=%u\n"
2914 "dot1xAuthEapolStartFramesRx=%u\n"
2915 "dot1xAuthEapolLogoffFramesRx=%u\n"
2916 "dot1xAuthEapolRespIdFramesRx=%u\n"
2917 "dot1xAuthEapolRespFramesRx=%u\n"
2918 "dot1xAuthEapolReqIdFramesTx=%u\n"
2919 "dot1xAuthEapolReqFramesTx=%u\n"
2920 "dot1xAuthInvalidEapolFramesRx=%u\n"
2921 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2922 "dot1xAuthLastEapolFrameVersion=%u\n"
2923 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2924 sm->dot1xAuthEapolFramesRx,
2925 sm->dot1xAuthEapolFramesTx,
2926 sm->dot1xAuthEapolStartFramesRx,
2927 sm->dot1xAuthEapolLogoffFramesRx,
2928 sm->dot1xAuthEapolRespIdFramesRx,
2929 sm->dot1xAuthEapolRespFramesRx,
2930 sm->dot1xAuthEapolReqIdFramesTx,
2931 sm->dot1xAuthEapolReqFramesTx,
2932 sm->dot1xAuthInvalidEapolFramesRx,
2933 sm->dot1xAuthEapLengthErrorFramesRx,
2934 sm->dot1xAuthLastEapolFrameVersion,
2935 MAC2STR(sm->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002936 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 return len;
2938 len += ret;
2939
2940 /* dot1xAuthDiagTable */
2941 ret = os_snprintf(buf + len, buflen - len,
2942 "dot1xAuthEntersConnecting=%u\n"
2943 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2944 "dot1xAuthEntersAuthenticating=%u\n"
2945 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2946 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2947 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2948 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2949 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2950 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2951 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2952 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2953 "dot1xAuthBackendResponses=%u\n"
2954 "dot1xAuthBackendAccessChallenges=%u\n"
2955 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2956 "dot1xAuthBackendAuthSuccesses=%u\n"
2957 "dot1xAuthBackendAuthFails=%u\n",
2958 sm->authEntersConnecting,
2959 sm->authEapLogoffsWhileConnecting,
2960 sm->authEntersAuthenticating,
2961 sm->authAuthSuccessesWhileAuthenticating,
2962 sm->authAuthTimeoutsWhileAuthenticating,
2963 sm->authAuthFailWhileAuthenticating,
2964 sm->authAuthEapStartsWhileAuthenticating,
2965 sm->authAuthEapLogoffWhileAuthenticating,
2966 sm->authAuthReauthsWhileAuthenticated,
2967 sm->authAuthEapStartsWhileAuthenticated,
2968 sm->authAuthEapLogoffWhileAuthenticated,
2969 sm->backendResponses,
2970 sm->backendAccessChallenges,
2971 sm->backendOtherRequestsToSupplicant,
2972 sm->backendAuthSuccesses,
2973 sm->backendAuthFails);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002974 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return len;
2976 len += ret;
2977
2978 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002979 os_reltime_age(&sta->acct_session_start, &diff);
Hai Shalom74f70d42019-02-11 14:42:39 -08002980 if (sm->eap && !sm->identity) {
2981 const u8 *id;
2982 size_t id_len;
2983
2984 id = eap_get_identity(sm->eap, &id_len);
2985 if (id)
2986 identity_buf = dup_binstr(id, id_len);
2987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 ret = os_snprintf(buf + len, buflen - len,
2989 /* TODO: dot1xAuthSessionOctetsRx */
2990 /* TODO: dot1xAuthSessionOctetsTx */
2991 /* TODO: dot1xAuthSessionFramesRx */
2992 /* TODO: dot1xAuthSessionFramesTx */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002993 "dot1xAuthSessionId=%016llX\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994 "dot1xAuthSessionAuthenticMethod=%d\n"
2995 "dot1xAuthSessionTime=%u\n"
2996 "dot1xAuthSessionTerminateCause=999\n"
2997 "dot1xAuthSessionUserName=%s\n",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002998 (unsigned long long) sta->acct_session_id,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 (wpa_key_mgmt_wpa_ieee8021x(
3000 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
3001 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003002 (unsigned int) diff.sec,
Hai Shalom021b0b52019-04-10 11:17:58 -07003003 sm->identity ? (char *) sm->identity :
3004 (identity_buf ? identity_buf : "N/A"));
Hai Shalom74f70d42019-02-11 14:42:39 -08003005 os_free(identity_buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003006 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 return len;
3008 len += ret;
3009
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003010 if (sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003011 ret = os_snprintf(buf + len, buflen - len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003012 "authMultiSessionId=%016llX\n",
3013 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003014 sm->acct_multi_session_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003015 if (os_snprintf_error(buflen - len, ret))
3016 return len;
3017 len += ret;
3018 }
3019
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003020 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
3021 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003022 ret = os_snprintf(buf + len, buflen - len,
3023 "last_eap_type_as=%d (%s)\n"
3024 "last_eap_type_sta=%d (%s)\n",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003025 sm->eap_type_authsrv, name1,
3026 sm->eap_type_supp, name2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003027 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003028 return len;
3029 len += ret;
3030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 return len;
3032}
3033
3034
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003035#ifdef CONFIG_HS20
3036static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
3037{
3038 struct hostapd_data *hapd = eloop_ctx;
3039 struct sta_info *sta = timeout_ctx;
3040
3041 if (sta->remediation) {
3042 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3043 MACSTR " to indicate Subscription Remediation",
3044 MAC2STR(sta->addr));
3045 hs20_send_wnm_notification(hapd, sta->addr,
3046 sta->remediation_method,
3047 sta->remediation_url);
3048 os_free(sta->remediation_url);
3049 sta->remediation_url = NULL;
3050 }
3051
3052 if (sta->hs20_deauth_req) {
3053 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3054 MACSTR " to indicate imminent deauthentication",
3055 MAC2STR(sta->addr));
3056 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
3057 sta->hs20_deauth_req);
3058 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003059
3060 if (sta->hs20_t_c_filtering) {
3061 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3062 MACSTR " to indicate Terms and Conditions filtering",
3063 MAC2STR(sta->addr));
3064 hs20_send_wnm_notification_t_c(hapd, sta->addr, sta->t_c_url);
3065 os_free(sta->t_c_url);
3066 sta->t_c_url = NULL;
3067 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003068}
3069#endif /* CONFIG_HS20 */
3070
3071
Sunil Ravi640215c2023-06-28 23:08:09 +00003072static bool ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003073 struct sta_info *sta, int success,
Sunil Ravi640215c2023-06-28 23:08:09 +00003074 int remediation, bool logoff)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075{
3076 const u8 *key;
3077 size_t len;
3078 /* TODO: get PMKLifetime from WPA parameters */
3079 static const int dot11RSNAConfigPMKLifetime = 43200;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003080 unsigned int session_timeout;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003081 struct os_reltime now, remaining;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003083#ifdef CONFIG_HS20
3084 if (remediation && !sta->remediation) {
3085 sta->remediation = 1;
3086 os_free(sta->remediation_url);
3087 sta->remediation_url =
3088 os_strdup(hapd->conf->subscr_remediation_url);
3089 sta->remediation_method = 1; /* SOAP-XML SPP */
3090 }
3091
Roshan Pius3a1667e2018-07-03 15:17:14 -07003092 if (success && (sta->remediation || sta->hs20_deauth_req ||
3093 sta->hs20_t_c_filtering)) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003094 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
3095 MACSTR " in 100 ms", MAC2STR(sta->addr));
3096 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
3097 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
3098 hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003099 }
3100#endif /* CONFIG_HS20 */
3101
Hai Shalom81f62d82019-07-22 12:10:00 -07003102#ifdef CONFIG_MACSEC
3103 ieee802_1x_notify_create_actor_hapd(hapd, sta);
3104#endif /* CONFIG_MACSEC */
3105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003107 if (sta->session_timeout_set) {
3108 os_get_reltime(&now);
3109 os_reltime_sub(&sta->session_timeout, &now, &remaining);
3110 session_timeout = (remaining.sec > 0) ? remaining.sec : 1;
3111 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003112 session_timeout = dot11RSNAConfigPMKLifetime;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003113 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003114 if (success && key && len >= PMK_LEN && !sta->remediation &&
3115 !sta->hs20_deauth_requested &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003116 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 sta->eapol_sm) == 0) {
3118 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
3119 HOSTAPD_LEVEL_DEBUG,
3120 "Added PMKSA cache entry (IEEE 802.1X)");
3121 }
3122
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003123 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124 /*
3125 * Many devices require deauthentication after WPS provisioning
3126 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003127 * disconnect the client here. In addition, this may also
3128 * benefit IEEE 802.1X/EAPOL authentication cases, too since
3129 * the EAPOL PAE state machine would remain in HELD state for
3130 * considerable amount of time and some EAP methods, like
3131 * EAP-FAST with anonymous provisioning, may require another
3132 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 */
Sunil Ravi640215c2023-06-28 23:08:09 +00003134 ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta,
3135 logoff ? 0 : 10);
3136 if (logoff && sta->wpa_sm)
3137 return true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 }
Sunil Ravi640215c2023-06-28 23:08:09 +00003139
3140 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141}