blob: f13c60a9ee1a0cec6abe6d6e19ed2eca9bcb5e08 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / IEEE 802.1X-2004 Authenticator
Hai Shalom74f70d42019-02-11 14:42:39 -08003 * Copyright (c) 2002-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
Hai Shalomc3565922019-10-28 11:58:20 -070010#ifdef CONFIG_SQLITE
11#include <sqlite3.h>
12#endif /* CONFIG_SQLITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013
14#include "utils/common.h"
15#include "utils/eloop.h"
16#include "crypto/md5.h"
17#include "crypto/crypto.h"
18#include "crypto/random.h"
19#include "common/ieee802_11_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "radius/radius.h"
21#include "radius/radius_client.h"
22#include "eap_server/eap.h"
23#include "eap_common/eap_wsc_common.h"
24#include "eapol_auth/eapol_auth_sm.h"
25#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080026#include "p2p/p2p.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "hostapd.h"
28#include "accounting.h"
29#include "sta_info.h"
30#include "wpa_auth.h"
31#include "preauth_auth.h"
32#include "pmksa_cache_auth.h"
33#include "ap_config.h"
34#include "ap_drv_ops.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080035#include "wps_hostapd.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080036#include "hs20.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080037/* FIX: Not really a good thing to require ieee802_11.h here.. (FILS) */
38#include "ieee802_11.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "ieee802_1x.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070040#include "wpa_auth_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041
42
Dmitry Shmidtde47be72016-01-07 12:52:55 -080043#ifdef CONFIG_HS20
44static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
45#endif /* CONFIG_HS20 */
Sunil Ravi640215c2023-06-28 23:08:09 +000046static bool ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080047 struct sta_info *sta, int success,
Sunil Ravi640215c2023-06-28 23:08:09 +000048 int remediation, bool logoff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049
50
51static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
52 u8 type, const u8 *data, size_t datalen)
53{
54 u8 *buf;
55 struct ieee802_1x_hdr *xhdr;
56 size_t len;
57 int encrypt = 0;
58
59 len = sizeof(*xhdr) + datalen;
60 buf = os_zalloc(len);
Hai Shalomc3565922019-10-28 11:58:20 -070061 if (!buf) {
62 wpa_printf(MSG_ERROR, "malloc() failed for %s(len=%lu)",
63 __func__, (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 return;
65 }
66
67 xhdr = (struct ieee802_1x_hdr *) buf;
68 xhdr->version = hapd->conf->eapol_version;
Hai Shalom81f62d82019-07-22 12:10:00 -070069#ifdef CONFIG_MACSEC
70 if (xhdr->version > 2 && hapd->conf->macsec_policy == 0)
71 xhdr->version = 2;
72#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 xhdr->type = type;
74 xhdr->length = host_to_be16(datalen);
75
76 if (datalen > 0 && data != NULL)
77 os_memcpy(xhdr + 1, data, datalen);
78
79 if (wpa_auth_pairwise_set(sta->wpa_sm))
80 encrypt = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080081#ifdef CONFIG_TESTING_OPTIONS
82 if (hapd->ext_eapol_frame_io) {
83 size_t hex_len = 2 * len + 1;
84 char *hex = os_malloc(hex_len);
85
86 if (hex) {
87 wpa_snprintf_hex(hex, hex_len, buf, len);
88 wpa_msg(hapd->msg_ctx, MSG_INFO,
89 "EAPOL-TX " MACSTR " %s",
90 MAC2STR(sta->addr), hex);
91 os_free(hex);
92 }
93 } else
94#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 if (sta->flags & WLAN_STA_PREAUTH) {
96 rsn_preauth_send(hapd, sta, buf, len);
97 } else {
Sunil Ravi2a14cf12023-11-21 00:54:38 +000098 int link_id = -1;
99
100#ifdef CONFIG_IEEE80211BE
101 link_id = hapd->conf->mld_ap ? hapd->mld_link_id : -1;
102#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800103 hostapd_drv_hapd_send_eapol(
104 hapd, sta->addr, buf, len,
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000105 encrypt, hostapd_sta_flags_to_drv(sta->flags), link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 }
107
108 os_free(buf);
109}
110
111
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000112static void ieee802_1x_set_authorized(struct hostapd_data *hapd,
113 struct sta_info *sta,
114 bool authorized, bool mld)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115{
116 int res;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000117 bool update;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118
119 if (sta->flags & WLAN_STA_PREAUTH)
120 return;
121
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000122 update = ap_sta_set_authorized_flag(hapd, sta, authorized);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000123 res = hostapd_set_authorized(hapd, sta, authorized);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000124 if (update)
125 ap_sta_set_authorized_event(hapd, sta, authorized);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000126 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
127 HOSTAPD_LEVEL_DEBUG, "%sauthorizing port",
128 authorized ? "" : "un");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000130 if (!mld && res && errno != ENOENT) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700131 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
132 " flags for kernel driver (errno=%d).",
133 MAC2STR(sta->addr), errno);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000134 } else if (mld && res) {
135 wpa_printf(MSG_DEBUG,
136 "MLD: Could not set station " MACSTR " flags",
137 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 }
139
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800140 if (authorized) {
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800141 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144}
145
146
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000147static void ieee802_1x_ml_set_sta_authorized(struct hostapd_data *hapd,
148 struct sta_info *sta,
149 bool authorized)
150{
151#ifdef CONFIG_IEEE80211BE
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 Ravi88611412024-06-28 17:34:56 +0000175 if (!tmp_hapd->conf->mld_ap ||
176 hapd->conf->mld_id != tmp_hapd->conf->mld_id)
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000177 continue;
178
179 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
180 tmp_sta = tmp_sta->next) {
181 if (tmp_sta == sta ||
182 tmp_sta->mld_assoc_link_id !=
183 sta->mld_assoc_link_id ||
184 tmp_sta->aid != sta->aid)
185 continue;
186
187 ieee802_1x_set_authorized(tmp_hapd, tmp_sta,
188 authorized, true);
189 break;
190 }
191 }
192 }
193#endif /* CONFIG_IEEE80211BE */
194}
195
196
197
198void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
199 struct sta_info *sta, int authorized)
200{
201 ieee802_1x_set_authorized(hapd, sta, authorized, false);
202 ieee802_1x_ml_set_sta_authorized(hapd, sta, !!authorized);
203}
204
205
Hai Shalomfdcde762020-04-02 11:19:20 -0700206#ifdef CONFIG_WEP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800207#ifndef CONFIG_FIPS
208#ifndef CONFIG_NO_RC4
209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
211 struct sta_info *sta,
212 int idx, int broadcast,
213 u8 *key_data, size_t key_len)
214{
215 u8 *buf, *ekey;
216 struct ieee802_1x_hdr *hdr;
217 struct ieee802_1x_eapol_key *key;
218 size_t len, ekey_len;
219 struct eapol_state_machine *sm = sta->eapol_sm;
220
Hai Shalomc3565922019-10-28 11:58:20 -0700221 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 return;
223
224 len = sizeof(*key) + key_len;
225 buf = os_zalloc(sizeof(*hdr) + len);
Hai Shalomc3565922019-10-28 11:58:20 -0700226 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 return;
228
229 hdr = (struct ieee802_1x_hdr *) buf;
230 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
231 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700232 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 wpa_get_ntp_timestamp(key->replay_counter);
Hai Shalom81f62d82019-07-22 12:10:00 -0700234 if (os_memcmp(key->replay_counter,
235 hapd->last_1x_eapol_key_replay_counter,
236 IEEE8021X_REPLAY_COUNTER_LEN) <= 0) {
237 /* NTP timestamp did not increment from last EAPOL-Key frame;
238 * use previously used value + 1 instead. */
239 inc_byte_array(hapd->last_1x_eapol_key_replay_counter,
240 IEEE8021X_REPLAY_COUNTER_LEN);
241 os_memcpy(key->replay_counter,
242 hapd->last_1x_eapol_key_replay_counter,
243 IEEE8021X_REPLAY_COUNTER_LEN);
244 } else {
245 os_memcpy(hapd->last_1x_eapol_key_replay_counter,
246 key->replay_counter,
247 IEEE8021X_REPLAY_COUNTER_LEN);
248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249
250 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
251 wpa_printf(MSG_ERROR, "Could not get random numbers");
252 os_free(buf);
253 return;
254 }
255
256 key->key_index = idx | (broadcast ? 0 : BIT(7));
257 if (hapd->conf->eapol_key_index_workaround) {
258 /* According to some information, WinXP Supplicant seems to
259 * interpret bit7 as an indication whether the key is to be
260 * activated, so make it possible to enable workaround that
261 * sets this bit for all keys. */
262 key->key_index |= BIT(7);
263 }
264
265 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
266 * MSK[32..63] is used to sign the message. */
Hai Shalomc3565922019-10-28 11:58:20 -0700267 if (!sm->eap_if->eapKeyData || sm->eap_if->eapKeyDataLen < 64) {
268 wpa_printf(MSG_ERROR,
269 "No eapKeyData available for encrypting and signing EAPOL-Key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 os_free(buf);
271 return;
272 }
273 os_memcpy((u8 *) (key + 1), key_data, key_len);
274 ekey_len = sizeof(key->key_iv) + 32;
275 ekey = os_malloc(ekey_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700276 if (!ekey) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 wpa_printf(MSG_ERROR, "Could not encrypt key");
278 os_free(buf);
279 return;
280 }
281 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
282 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
283 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
284 os_free(ekey);
285
286 /* This header is needed here for HMAC-MD5, but it will be regenerated
287 * in ieee802_1x_send() */
288 hdr->version = hapd->conf->eapol_version;
Hai Shalom81f62d82019-07-22 12:10:00 -0700289#ifdef CONFIG_MACSEC
290 if (hdr->version > 2)
291 hdr->version = 2;
292#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
294 hdr->length = host_to_be16(len);
295 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
296 key->key_signature);
297
298 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
299 " (%s index=%d)", MAC2STR(sm->addr),
300 broadcast ? "broadcast" : "unicast", idx);
301 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
302 if (sta->eapol_sm)
303 sta->eapol_sm->dot1xAuthEapolFramesTx++;
304 os_free(buf);
305}
306
307
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800308static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309{
310 struct eapol_authenticator *eapol = hapd->eapol_auth;
311 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312
Hai Shalomc3565922019-10-28 11:58:20 -0700313 if (!sm || !sm->eap_if->eapKeyData)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 return;
315
316 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
317 MAC2STR(sta->addr));
318
319#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800320 if (sta->vlan_id > 0) {
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700321 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
322 return;
323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 if (eapol->default_wep_key) {
327 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
328 eapol->default_wep_key,
329 hapd->conf->default_wep_key_len);
330 }
331
332 if (hapd->conf->individual_wep_key_len > 0) {
333 u8 *ikey;
Hai Shalomc3565922019-10-28 11:58:20 -0700334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 ikey = os_malloc(hapd->conf->individual_wep_key_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700336 if (!ikey ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
338 {
Hai Shalomc3565922019-10-28 11:58:20 -0700339 wpa_printf(MSG_ERROR,
340 "Could not generate random individual WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 os_free(ikey);
342 return;
343 }
344
345 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
346 ikey, hapd->conf->individual_wep_key_len);
347
348 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
349 hapd->conf->individual_wep_key_len);
350
351 /* TODO: set encryption in TX callback, i.e., only after STA
352 * has ACKed EAPOL-Key frame */
353 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
Hai Shalomfdcde762020-04-02 11:19:20 -0700354 sta->addr, 0, 0, 1, NULL, 0, ikey,
355 hapd->conf->individual_wep_key_len,
356 KEY_FLAG_PAIRWISE_RX_TX)) {
Hai Shalomc3565922019-10-28 11:58:20 -0700357 wpa_printf(MSG_ERROR,
358 "Could not set individual WEP encryption");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 }
360
361 os_free(ikey);
362 }
363}
364
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800365#endif /* CONFIG_NO_RC4 */
366#endif /* CONFIG_FIPS */
Hai Shalomfdcde762020-04-02 11:19:20 -0700367#endif /* CONFIG_WEP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
370const char *radius_mode_txt(struct hostapd_data *hapd)
371{
372 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800373 case HOSTAPD_MODE_IEEE80211AD:
374 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 case HOSTAPD_MODE_IEEE80211A:
376 return "802.11a";
377 case HOSTAPD_MODE_IEEE80211G:
378 return "802.11g";
379 case HOSTAPD_MODE_IEEE80211B:
380 default:
381 return "802.11b";
382 }
383}
384
385
386int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
387{
388 int i;
389 u8 rate = 0;
390
391 for (i = 0; i < sta->supported_rates_len; i++)
392 if ((sta->supported_rates[i] & 0x7f) > rate)
393 rate = sta->supported_rates[i] & 0x7f;
394
395 return rate;
396}
397
398
399#ifndef CONFIG_NO_RADIUS
400static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
401 struct eapol_state_machine *sm,
402 const u8 *eap, size_t len)
403{
404 const u8 *identity;
405 size_t identity_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800406 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407
408 if (len <= sizeof(struct eap_hdr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800409 (hdr->code == EAP_CODE_RESPONSE &&
410 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
411 (hdr->code == EAP_CODE_INITIATE &&
412 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
413 (hdr->code != EAP_CODE_RESPONSE &&
414 hdr->code != EAP_CODE_INITIATE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 return;
416
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800417 eap_erp_update_identity(sm->eap, eap, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 identity = eap_get_identity(sm->eap, &identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700419 if (!identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 return;
421
422 /* Save station identity for future RADIUS packets */
423 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700424 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Hai Shalomc3565922019-10-28 11:58:20 -0700425 if (!sm->identity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 sm->identity_len = 0;
427 return;
428 }
429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
432 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
433 sm->dot1xAuthEapolRespIdFramesRx++;
434}
435
436
Dmitry Shmidt03658832014-08-13 11:03:49 -0700437static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
438 struct hostapd_radius_attr *req_attr,
439 struct sta_info *sta,
440 struct radius_msg *msg)
441{
442 u32 suite;
443 int ver, val;
444
445 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
446 val = wpa_auth_get_pairwise(sta->wpa_sm);
447 suite = wpa_cipher_to_suite(ver, val);
448 if (val != -1 &&
449 !hostapd_config_get_radius_attr(req_attr,
450 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
451 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
452 suite)) {
453 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
454 return -1;
455 }
456
Dmitry Shmidt41712582015-06-29 11:02:15 -0700457 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
458 hapd->conf->osen) ?
Dmitry Shmidt03658832014-08-13 11:03:49 -0700459 WPA_PROTO_RSN : WPA_PROTO_WPA,
460 hapd->conf->wpa_group);
461 if (!hostapd_config_get_radius_attr(req_attr,
462 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
463 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
464 suite)) {
465 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
466 return -1;
467 }
468
469 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
470 suite = wpa_akm_to_suite(val);
471 if (val != -1 &&
472 !hostapd_config_get_radius_attr(req_attr,
473 RADIUS_ATTR_WLAN_AKM_SUITE) &&
474 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
475 suite)) {
476 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
477 return -1;
478 }
479
Dmitry Shmidt03658832014-08-13 11:03:49 -0700480 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
481 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
482 hapd->conf->group_mgmt_cipher);
483 if (!hostapd_config_get_radius_attr(
484 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
485 !radius_msg_add_attr_int32(
486 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
487 wpa_printf(MSG_ERROR,
488 "Could not add WLAN-Group-Mgmt-Cipher");
489 return -1;
490 }
491 }
Dmitry Shmidt03658832014-08-13 11:03:49 -0700492
493 return 0;
494}
495
496
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700497static int add_common_radius_sta_attr(struct hostapd_data *hapd,
498 struct hostapd_radius_attr *req_attr,
499 struct sta_info *sta,
500 struct radius_msg *msg)
501{
502 char buf[128];
503
504 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800505 RADIUS_ATTR_SERVICE_TYPE) &&
506 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
507 RADIUS_SERVICE_TYPE_FRAMED)) {
508 wpa_printf(MSG_ERROR, "Could not add Service-Type");
509 return -1;
510 }
511
512 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700513 RADIUS_ATTR_NAS_PORT) &&
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700514 sta->aid > 0 &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700515 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
516 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
517 return -1;
518 }
519
520 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
521 MAC2STR(sta->addr));
522 buf[sizeof(buf) - 1] = '\0';
523 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
524 (u8 *) buf, os_strlen(buf))) {
525 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
526 return -1;
527 }
528
529 if (sta->flags & WLAN_STA_PREAUTH) {
530 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
531 sizeof(buf));
532 } else {
533 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
534 radius_sta_rate(hapd, sta) / 2,
535 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
536 radius_mode_txt(hapd));
537 buf[sizeof(buf) - 1] = '\0';
538 }
539 if (!hostapd_config_get_radius_attr(req_attr,
540 RADIUS_ATTR_CONNECT_INFO) &&
541 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
542 (u8 *) buf, os_strlen(buf))) {
543 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
544 return -1;
545 }
546
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800547 if (sta->acct_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800548 os_snprintf(buf, sizeof(buf), "%016llX",
549 (unsigned long long) sta->acct_session_id);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800550 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
551 (u8 *) buf, os_strlen(buf))) {
552 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
553 return -1;
554 }
555 }
556
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800557 if ((hapd->conf->wpa & 2) &&
558 !hapd->conf->disable_pmksa_caching &&
559 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800560 os_snprintf(buf, sizeof(buf), "%016llX",
561 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800562 sta->eapol_sm->acct_multi_session_id);
563 if (!radius_msg_add_attr(
564 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
565 (u8 *) buf, os_strlen(buf))) {
566 wpa_printf(MSG_INFO,
567 "Could not add Acct-Multi-Session-Id");
568 return -1;
569 }
570 }
571
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800572#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt03658832014-08-13 11:03:49 -0700573 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
574 sta->wpa_sm &&
575 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
576 sta->auth_alg == WLAN_AUTH_FT) &&
577 !hostapd_config_get_radius_attr(req_attr,
578 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
579 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
580 WPA_GET_BE16(
581 hapd->conf->mobility_domain))) {
582 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
583 return -1;
584 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800585#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt03658832014-08-13 11:03:49 -0700586
Dmitry Shmidt41712582015-06-29 11:02:15 -0700587 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
Dmitry Shmidt03658832014-08-13 11:03:49 -0700588 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
589 return -1;
590
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700591 return 0;
592}
593
594
595int add_common_radius_attr(struct hostapd_data *hapd,
596 struct hostapd_radius_attr *req_attr,
597 struct sta_info *sta,
598 struct radius_msg *msg)
599{
600 char buf[128];
601 struct hostapd_radius_attr *attr;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800602 int len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700603
604 if (!hostapd_config_get_radius_attr(req_attr,
605 RADIUS_ATTR_NAS_IP_ADDRESS) &&
606 hapd->conf->own_ip_addr.af == AF_INET &&
607 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
608 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
609 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
610 return -1;
611 }
612
613#ifdef CONFIG_IPV6
614 if (!hostapd_config_get_radius_attr(req_attr,
615 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
616 hapd->conf->own_ip_addr.af == AF_INET6 &&
617 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
618 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
619 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
620 return -1;
621 }
622#endif /* CONFIG_IPV6 */
623
624 if (!hostapd_config_get_radius_attr(req_attr,
625 RADIUS_ATTR_NAS_IDENTIFIER) &&
626 hapd->conf->nas_identifier &&
627 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
628 (u8 *) hapd->conf->nas_identifier,
629 os_strlen(hapd->conf->nas_identifier))) {
630 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
631 return -1;
632 }
633
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800634 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
635 MAC2STR(hapd->own_addr));
636 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
637 hapd->conf->ssid.ssid_len);
638 len += hapd->conf->ssid.ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700639 if (!hostapd_config_get_radius_attr(req_attr,
640 RADIUS_ATTR_CALLED_STATION_ID) &&
641 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800642 (u8 *) buf, len)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700643 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
644 return -1;
645 }
646
647 if (!hostapd_config_get_radius_attr(req_attr,
648 RADIUS_ATTR_NAS_PORT_TYPE) &&
649 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
650 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
651 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
652 return -1;
653 }
654
Dmitry Shmidt03658832014-08-13 11:03:49 -0700655#ifdef CONFIG_INTERWORKING
656 if (hapd->conf->interworking &&
657 !is_zero_ether_addr(hapd->conf->hessid)) {
658 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
659 MAC2STR(hapd->conf->hessid));
660 buf[sizeof(buf) - 1] = '\0';
661 if (!hostapd_config_get_radius_attr(req_attr,
662 RADIUS_ATTR_WLAN_HESSID) &&
663 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
664 (u8 *) buf, os_strlen(buf))) {
665 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
666 return -1;
667 }
668 }
669#endif /* CONFIG_INTERWORKING */
670
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700671 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
672 return -1;
673
674 for (attr = req_attr; attr; attr = attr->next) {
675 if (!radius_msg_add_attr(msg, attr->type,
676 wpabuf_head(attr->val),
677 wpabuf_len(attr->val))) {
Hai Shalomc3565922019-10-28 11:58:20 -0700678 wpa_printf(MSG_ERROR, "Could not add RADIUS attribute");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700679 return -1;
680 }
681 }
682
683 return 0;
684}
685
686
Hai Shalomc3565922019-10-28 11:58:20 -0700687int add_sqlite_radius_attr(struct hostapd_data *hapd, struct sta_info *sta,
688 struct radius_msg *msg, int acct)
689{
690#ifdef CONFIG_SQLITE
691 const char *attrtxt;
692 char addrtxt[3 * ETH_ALEN];
693 char *sql;
694 sqlite3_stmt *stmt = NULL;
695
696 if (!hapd->rad_attr_db)
697 return 0;
698
699 os_snprintf(addrtxt, sizeof(addrtxt), MACSTR, MAC2STR(sta->addr));
700
701 sql = "SELECT attr FROM radius_attributes WHERE sta=? AND (reqtype=? OR reqtype IS NULL);";
702 if (sqlite3_prepare_v2(hapd->rad_attr_db, sql, os_strlen(sql), &stmt,
703 NULL) != SQLITE_OK) {
704 wpa_printf(MSG_ERROR, "DB: Failed to prepare SQL statement: %s",
705 sqlite3_errmsg(hapd->rad_attr_db));
706 return -1;
707 }
708 sqlite3_bind_text(stmt, 1, addrtxt, os_strlen(addrtxt), SQLITE_STATIC);
709 sqlite3_bind_text(stmt, 2, acct ? "acct" : "auth", 4, SQLITE_STATIC);
710 while (sqlite3_step(stmt) == SQLITE_ROW) {
711 struct hostapd_radius_attr *attr;
712 struct radius_attr_hdr *hdr;
713
714 attrtxt = (const char *) sqlite3_column_text(stmt, 0);
715 attr = hostapd_parse_radius_attr(attrtxt);
716 if (!attr) {
717 wpa_printf(MSG_ERROR,
718 "Skipping invalid attribute from SQL: %s",
719 attrtxt);
720 continue;
721 }
722 wpa_printf(MSG_DEBUG, "Adding RADIUS attribute from SQL: %s",
723 attrtxt);
724 hdr = radius_msg_add_attr(msg, attr->type,
725 wpabuf_head(attr->val),
726 wpabuf_len(attr->val));
727 hostapd_config_free_radius_attr(attr);
728 if (!hdr) {
729 wpa_printf(MSG_ERROR,
730 "Could not add RADIUS attribute from SQL");
731 continue;
732 }
733 }
734
735 sqlite3_reset(stmt);
736 sqlite3_clear_bindings(stmt);
737 sqlite3_finalize(stmt);
738#endif /* CONFIG_SQLITE */
739
740 return 0;
741}
742
743
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800744void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
745 struct sta_info *sta,
746 const u8 *eap, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747{
748 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 struct eapol_state_machine *sm = sta->eapol_sm;
750
Hai Shalomc3565922019-10-28 11:58:20 -0700751 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 return;
753
754 ieee802_1x_learn_identity(hapd, sm, eap, len);
755
Hai Shalomc3565922019-10-28 11:58:20 -0700756 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757
758 sm->radius_identifier = radius_client_get_id(hapd->radius);
759 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
760 sm->radius_identifier);
Hai Shalomc3565922019-10-28 11:58:20 -0700761 if (!msg) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800762 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 return;
764 }
765
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800766 if (radius_msg_make_authenticator(msg) < 0) {
767 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
768 goto fail;
769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770
771 if (sm->identity &&
772 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
773 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800774 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 goto fail;
776 }
777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
779 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781
Hai Shalomc3565922019-10-28 11:58:20 -0700782 if (sta && add_sqlite_radius_attr(hapd, sta, msg, 0) < 0)
783 goto fail;
784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 /* TODO: should probably check MTU from driver config; 2304 is max for
786 * IEEE 802.11, but use 1400 to avoid problems with too large packets
787 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700788 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
789 RADIUS_ATTR_FRAMED_MTU) &&
790 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800791 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 goto fail;
793 }
794
Dmitry Shmidt41712582015-06-29 11:02:15 -0700795 if (!radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800796 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 goto fail;
798 }
799
800 /* State attribute must be copied if and only if this packet is
801 * Access-Request reply to the previous Access-Challenge */
802 if (sm->last_recv_radius &&
803 radius_msg_get_hdr(sm->last_recv_radius)->code ==
804 RADIUS_CODE_ACCESS_CHALLENGE) {
805 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
806 RADIUS_ATTR_STATE);
807 if (res < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -0700808 wpa_printf(MSG_INFO,
809 "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 goto fail;
811 }
Hai Shalomc3565922019-10-28 11:58:20 -0700812 if (res > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 }
815
Dmitry Shmidt04949592012-07-19 12:16:46 -0700816 if (hapd->conf->radius_request_cui) {
817 const u8 *cui;
818 size_t cui_len;
819 /* Add previously learned CUI or nul CUI to request CUI */
820 if (sm->radius_cui) {
821 cui = wpabuf_head(sm->radius_cui);
822 cui_len = wpabuf_len(sm->radius_cui);
823 } else {
824 cui = (const u8 *) "\0";
825 cui_len = 1;
826 }
827 if (!radius_msg_add_attr(msg,
828 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
829 cui, cui_len)) {
830 wpa_printf(MSG_ERROR, "Could not add CUI");
831 goto fail;
832 }
833 }
834
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800835#ifdef CONFIG_HS20
836 if (hapd->conf->hs20) {
Hai Shalom74f70d42019-02-11 14:42:39 -0800837 u8 ver = hapd->conf->hs20_release - 1;
838
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800839 if (!radius_msg_add_wfa(
840 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
841 &ver, 1)) {
Hai Shalomc3565922019-10-28 11:58:20 -0700842 wpa_printf(MSG_ERROR,
843 "Could not add HS 2.0 AP version");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800844 goto fail;
845 }
846
847 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
848 const u8 *pos;
849 u8 buf[3];
850 u16 id;
Hai Shalomc3565922019-10-28 11:58:20 -0700851
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800852 pos = wpabuf_head_u8(sta->hs20_ie);
853 buf[0] = (*pos) >> 4;
854 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
855 wpabuf_len(sta->hs20_ie) >= 3)
856 id = WPA_GET_LE16(pos + 1);
857 else
858 id = 0;
859 WPA_PUT_BE16(buf + 1, id);
860 if (!radius_msg_add_wfa(
861 msg,
862 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
863 buf, sizeof(buf))) {
Hai Shalomc3565922019-10-28 11:58:20 -0700864 wpa_printf(MSG_ERROR,
865 "Could not add HS 2.0 STA version");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800866 goto fail;
867 }
868 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700869
870 if (sta->roaming_consortium &&
871 !radius_msg_add_wfa(
872 msg, RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM,
873 wpabuf_head(sta->roaming_consortium),
874 wpabuf_len(sta->roaming_consortium))) {
875 wpa_printf(MSG_ERROR,
876 "Could not add HS 2.0 Roaming Consortium");
877 goto fail;
878 }
879
880 if (hapd->conf->t_c_filename) {
881 be32 timestamp;
882
883 if (!radius_msg_add_wfa(
884 msg,
885 RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME,
886 (const u8 *) hapd->conf->t_c_filename,
887 os_strlen(hapd->conf->t_c_filename))) {
888 wpa_printf(MSG_ERROR,
889 "Could not add HS 2.0 T&C Filename");
890 goto fail;
891 }
892
893 timestamp = host_to_be32(hapd->conf->t_c_timestamp);
894 if (!radius_msg_add_wfa(
895 msg,
896 RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP,
897 (const u8 *) &timestamp,
898 sizeof(timestamp))) {
899 wpa_printf(MSG_ERROR,
900 "Could not add HS 2.0 Timestamp");
901 goto fail;
902 }
903 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800904 }
905#endif /* CONFIG_HS20 */
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
908 goto fail;
909
910 return;
911
912 fail:
913 radius_msg_free(msg);
914}
915#endif /* CONFIG_NO_RADIUS */
916
917
918static void handle_eap_response(struct hostapd_data *hapd,
919 struct sta_info *sta, struct eap_hdr *eap,
920 size_t len)
921{
922 u8 type, *data;
923 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -0700924
925 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 return;
927
928 data = (u8 *) (eap + 1);
929
930 if (len < sizeof(*eap) + 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700931 wpa_printf(MSG_INFO, "%s: too short response data", __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 return;
933 }
934
935 sm->eap_type_supp = type = data[0];
936
937 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
938 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
939 "id=%d len=%d) from STA: EAP Response-%s (%d)",
940 eap->code, eap->identifier, be_to_host16(eap->length),
941 eap_server_get_name(0, type), type);
942
943 sm->dot1xAuthEapolRespFramesRx++;
944
945 wpabuf_free(sm->eap_if->eapRespData);
946 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
Hai Shalome21d4e82020-04-29 16:34:06 -0700947 sm->eapolEap = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948}
949
950
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800951static void handle_eap_initiate(struct hostapd_data *hapd,
952 struct sta_info *sta, struct eap_hdr *eap,
953 size_t len)
954{
955#ifdef CONFIG_ERP
956 u8 type, *data;
957 struct eapol_state_machine *sm = sta->eapol_sm;
958
Hai Shalomc3565922019-10-28 11:58:20 -0700959 if (!sm)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 return;
961
962 if (len < sizeof(*eap) + 1) {
Hai Shalomc3565922019-10-28 11:58:20 -0700963 wpa_printf(MSG_INFO, "%s: too short response data", __func__);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800964 return;
965 }
966
967 data = (u8 *) (eap + 1);
968 type = data[0];
969
970 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -0700971 HOSTAPD_LEVEL_DEBUG,
972 "received EAP packet (code=%d id=%d len=%d) from STA: EAP Initiate type %u",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 eap->code, eap->identifier, be_to_host16(eap->length),
974 type);
975
976 wpabuf_free(sm->eap_if->eapRespData);
977 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
Hai Shalome21d4e82020-04-29 16:34:06 -0700978 sm->eapolEap = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800979#endif /* CONFIG_ERP */
980}
981
982
Hai Shalomc3565922019-10-28 11:58:20 -0700983#ifndef CONFIG_NO_STDOUT_DEBUG
984static const char * eap_code_str(u8 code)
985{
986 switch (code) {
987 case EAP_CODE_REQUEST:
988 return "request";
989 case EAP_CODE_RESPONSE:
990 return "response";
991 case EAP_CODE_SUCCESS:
992 return "success";
993 case EAP_CODE_FAILURE:
994 return "failure";
995 case EAP_CODE_INITIATE:
996 return "initiate";
997 case EAP_CODE_FINISH:
998 return "finish";
999 default:
1000 return "unknown";
1001 }
1002}
1003#endif /* CONFIG_NO_STDOUT_DEBUG */
1004
1005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006/* Process incoming EAP packet from Supplicant */
1007static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
1008 u8 *buf, size_t len)
1009{
1010 struct eap_hdr *eap;
1011 u16 eap_len;
1012
1013 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001014 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 return;
1016 }
1017
1018 eap = (struct eap_hdr *) buf;
1019
1020 eap_len = be_to_host16(eap->length);
Hai Shalomc3565922019-10-28 11:58:20 -07001021 wpa_printf(MSG_DEBUG, "EAP: code=%d (%s) identifier=%d length=%d",
1022 eap->code, eap_code_str(eap->code), eap->identifier,
1023 eap_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 if (eap_len < sizeof(*eap)) {
1025 wpa_printf(MSG_DEBUG, " Invalid EAP length");
1026 return;
1027 } else if (eap_len > len) {
Hai Shalomc3565922019-10-28 11:58:20 -07001028 wpa_printf(MSG_DEBUG,
1029 " Too short frame to contain this EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 return;
1031 } else if (eap_len < len) {
Hai Shalomc3565922019-10-28 11:58:20 -07001032 wpa_printf(MSG_DEBUG,
1033 " Ignoring %lu extra bytes after EAP packet",
1034 (unsigned long) len - eap_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 }
1036
1037 switch (eap->code) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 case EAP_CODE_RESPONSE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 handle_eap_response(hapd, sta, eap, eap_len);
1040 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001041 case EAP_CODE_INITIATE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 handle_eap_initiate(hapd, sta, eap, eap_len);
1043 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 }
1045}
1046
1047
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001048struct eapol_state_machine *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
1050{
1051 int flags = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 if (sta->flags & WLAN_STA_PREAUTH)
1054 flags |= EAPOL_SM_PREAUTH;
1055 if (sta->wpa_sm) {
1056 flags |= EAPOL_SM_USES_WPA;
1057 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
1058 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
1059 }
1060 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001061 sta->wps_ie, sta->p2p_ie, sta,
1062 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063}
1064
1065
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001066static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
Sunil8cd6f4d2022-06-28 18:40:46 +00001067 size_t len, enum frame_encryption encrypted)
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001068{
1069 if (sta->pending_eapol_rx) {
1070 wpabuf_free(sta->pending_eapol_rx->buf);
1071 } else {
1072 sta->pending_eapol_rx =
1073 os_malloc(sizeof(*sta->pending_eapol_rx));
1074 if (!sta->pending_eapol_rx)
1075 return;
1076 }
1077
1078 sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
1079 if (!sta->pending_eapol_rx->buf) {
1080 os_free(sta->pending_eapol_rx);
1081 sta->pending_eapol_rx = NULL;
1082 return;
1083 }
1084
Sunil8cd6f4d2022-06-28 18:40:46 +00001085 sta->pending_eapol_rx->encrypted = encrypted;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001086 os_get_reltime(&sta->pending_eapol_rx->rx_time);
1087}
1088
1089
Sunil8cd6f4d2022-06-28 18:40:46 +00001090static bool ieee802_1x_check_encryption(struct sta_info *sta,
1091 enum frame_encryption encrypted,
1092 u8 type)
1093{
1094 if (encrypted != FRAME_NOT_ENCRYPTED)
1095 return true;
1096 if (type != IEEE802_1X_TYPE_EAP_PACKET &&
1097 type != IEEE802_1X_TYPE_EAPOL_START &&
1098 type != IEEE802_1X_TYPE_EAPOL_LOGOFF)
1099 return true;
1100 if (!(sta->flags & WLAN_STA_MFP))
1101 return true;
1102 return !wpa_auth_pairwise_set(sta->wpa_sm);
1103}
1104
1105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106/**
1107 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
1108 * @hapd: hostapd BSS data
1109 * @sa: Source address (sender of the EAPOL frame)
1110 * @buf: EAPOL frame
1111 * @len: Length of buf in octets
Sunil8cd6f4d2022-06-28 18:40:46 +00001112 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 *
1114 * This function is called for each incoming EAPOL frame from the interface
1115 */
1116void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
Sunil8cd6f4d2022-06-28 18:40:46 +00001117 size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118{
1119 struct sta_info *sta;
1120 struct ieee802_1x_hdr *hdr;
1121 struct ieee802_1x_eapol_key *key;
1122 u16 datalen;
1123 struct rsn_pmksa_cache_entry *pmksa;
1124 int key_mgmt;
1125
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001126 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 !hapd->conf->wps_state)
1128 return;
1129
Sunil8cd6f4d2022-06-28 18:40:46 +00001130 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR
1131 " (encrypted=%d)",
1132 (unsigned long) len, MAC2STR(sa), encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001134 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
1135 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001136 wpa_printf(MSG_DEBUG,
1137 "IEEE 802.1X data frame from not associated/Pre-authenticating STA");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001138
1139 if (sta && (sta->flags & WLAN_STA_AUTH)) {
1140 wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
1141 " for later use", MAC2STR(sta->addr));
Sunil8cd6f4d2022-06-28 18:40:46 +00001142 ieee802_1x_save_eapol(sta, buf, len, encrypted);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001143 }
1144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 return;
1146 }
1147
1148 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001149 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 return;
1151 }
1152
1153 hdr = (struct ieee802_1x_hdr *) buf;
1154 datalen = be_to_host16(hdr->length);
1155 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
1156 hdr->version, hdr->type, datalen);
1157
1158 if (len - sizeof(*hdr) < datalen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001159 wpa_printf(MSG_INFO,
1160 " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 if (sta->eapol_sm)
1162 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
1163 return;
1164 }
1165 if (len - sizeof(*hdr) > datalen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001166 wpa_printf(MSG_DEBUG,
1167 " ignoring %lu extra octets after IEEE 802.1X packet",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 (unsigned long) len - sizeof(*hdr) - datalen);
1169 }
1170
1171 if (sta->eapol_sm) {
1172 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
1173 sta->eapol_sm->dot1xAuthEapolFramesRx++;
1174 }
1175
1176 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
1177 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
1178 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
1179 (key->type == EAPOL_KEY_TYPE_WPA ||
1180 key->type == EAPOL_KEY_TYPE_RSN)) {
1181 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
1182 sizeof(*hdr) + datalen);
1183 return;
1184 }
1185
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001186 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001188 wpa_printf(MSG_DEBUG,
1189 "IEEE 802.1X: Ignore EAPOL message - 802.1X not enabled and WPS not used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 return;
1191 }
1192
1193 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001194 if (key_mgmt != -1 &&
1195 (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1196 key_mgmt == WPA_KEY_MGMT_DPP)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001197 wpa_printf(MSG_DEBUG,
1198 "IEEE 802.1X: Ignore EAPOL message - STA is using PSK");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 return;
1200 }
1201
Sunil8cd6f4d2022-06-28 18:40:46 +00001202 if (!ieee802_1x_check_encryption(sta, encrypted, hdr->type)) {
1203 wpa_printf(MSG_DEBUG,
1204 "IEEE 802.1X: Discard unencrypted EAPOL message - encryption was expected");
1205 return;
1206 }
1207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 if (!sta->eapol_sm) {
1209 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1210 if (!sta->eapol_sm)
1211 return;
1212
1213#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001214 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001215 u32 wflags = sta->flags & (WLAN_STA_WPS |
1216 WLAN_STA_WPS2 |
1217 WLAN_STA_MAYBE_WPS);
1218 if (wflags == WLAN_STA_MAYBE_WPS ||
1219 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
1220 /*
1221 * Delay EAPOL frame transmission until a
1222 * possible WPS STA initiates the handshake
1223 * with EAPOL-Start. Only allow the wait to be
1224 * skipped if the STA is known to support WPS
1225 * 2.0.
1226 */
Hai Shalomc3565922019-10-28 11:58:20 -07001227 wpa_printf(MSG_DEBUG,
1228 "WPS: Do not start EAPOL until EAPOL-Start is received");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 }
1232#endif /* CONFIG_WPS */
1233
Hai Shalome21d4e82020-04-29 16:34:06 -07001234 sta->eapol_sm->eap_if->portEnabled = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 }
1236
1237 /* since we support version 1, we can ignore version field and proceed
1238 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1239 /* TODO: actually, we are not version 1 anymore.. However, Version 2
1240 * does not change frame contents, so should be ok to process frames
1241 * more or less identically. Some changes might be needed for
1242 * verification of fields. */
1243
1244 switch (hdr->type) {
1245 case IEEE802_1X_TYPE_EAP_PACKET:
1246 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1247 break;
1248
1249 case IEEE802_1X_TYPE_EAPOL_START:
1250 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001251 HOSTAPD_LEVEL_DEBUG,
1252 "received EAPOL-Start from STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1254 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1255 if (pmksa) {
1256 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
Hai Shalomc3565922019-10-28 11:58:20 -07001257 HOSTAPD_LEVEL_DEBUG,
1258 "cached PMKSA available - ignore it since STA sent EAPOL-Start");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1260 }
Hai Shalome21d4e82020-04-29 16:34:06 -07001261 sta->eapol_sm->eapolStart = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1263 eap_server_clear_identity(sta->eapol_sm->eap);
1264 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1265 break;
1266
1267 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1268 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001269 HOSTAPD_LEVEL_DEBUG,
1270 "received EAPOL-Logoff from STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 sta->acct_terminate_cause =
1272 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1273 accounting_sta_stop(hapd, sta);
Hai Shalome21d4e82020-04-29 16:34:06 -07001274 sta->eapol_sm->eapolLogoff = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1276 eap_server_clear_identity(sta->eapol_sm->eap);
1277 break;
1278
1279 case IEEE802_1X_TYPE_EAPOL_KEY:
1280 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1281 if (!ap_sta_is_authorized(sta)) {
Hai Shalomc3565922019-10-28 11:58:20 -07001282 wpa_printf(MSG_DEBUG,
1283 " Dropped key data from unauthorized Supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 break;
1285 }
1286 break;
1287
1288 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1289 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1290 /* TODO: implement support for this; show data */
1291 break;
1292
Hai Shalom81f62d82019-07-22 12:10:00 -07001293#ifdef CONFIG_MACSEC
1294 case IEEE802_1X_TYPE_EAPOL_MKA:
1295 wpa_printf(MSG_EXCESSIVE,
1296 "EAPOL type %d will be handled by MKA", hdr->type);
1297 break;
1298#endif /* CONFIG_MACSEC */
1299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 default:
1301 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1302 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1303 break;
1304 }
1305
1306 eapol_auth_step(sta->eapol_sm);
1307}
1308
1309
1310/**
1311 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1312 * @hapd: hostapd BSS data
1313 * @sta: The station
1314 *
1315 * This function is called to start IEEE 802.1X authentication when a new
1316 * station completes IEEE 802.11 association.
1317 */
1318void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1319{
1320 struct rsn_pmksa_cache_entry *pmksa;
1321 int reassoc = 1;
1322 int force_1x = 0;
1323 int key_mgmt;
1324
1325#ifdef CONFIG_WPS
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001326 if (hapd->conf->wps_state &&
1327 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1328 (sta->flags & WLAN_STA_WPS))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 /*
1330 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1331 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1332 * authentication in this BSS.
1333 */
1334 force_1x = 1;
1335 }
1336#endif /* CONFIG_WPS */
1337
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001338 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Hai Shalomc3565922019-10-28 11:58:20 -07001339 wpa_printf(MSG_DEBUG,
1340 "IEEE 802.1X: Ignore STA - 802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001341 /*
1342 * Clear any possible EAPOL authenticator state to support
1343 * reassociation change from WPS to PSK.
1344 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001345 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 return;
1347 }
1348
1349 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001350 if (key_mgmt != -1 &&
1351 (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1352 key_mgmt == WPA_KEY_MGMT_DPP)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001354 /*
1355 * Clear any possible EAPOL authenticator state to support
1356 * reassociation change from WPA-EAP to PSK.
1357 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001358 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 return;
1360 }
1361
Hai Shalomc3565922019-10-28 11:58:20 -07001362 if (!sta->eapol_sm) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1364 HOSTAPD_LEVEL_DEBUG, "start authentication");
1365 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
Hai Shalomc3565922019-10-28 11:58:20 -07001366 if (!sta->eapol_sm) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 hostapd_logger(hapd, sta->addr,
1368 HOSTAPD_MODULE_IEEE8021X,
1369 HOSTAPD_LEVEL_INFO,
1370 "failed to allocate state machine");
1371 return;
1372 }
1373 reassoc = 0;
1374 }
1375
1376#ifdef CONFIG_WPS
1377 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001378 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1379 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001381 * Delay EAPOL frame transmission until a possible WPS STA
1382 * initiates the handshake with EAPOL-Start. Only allow the
1383 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 */
Hai Shalomc3565922019-10-28 11:58:20 -07001385 wpa_printf(MSG_DEBUG,
1386 "WPS: Do not start EAPOL until EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1388 }
1389#endif /* CONFIG_WPS */
1390
Hai Shalome21d4e82020-04-29 16:34:06 -07001391 sta->eapol_sm->eap_if->portEnabled = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001393#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 if (sta->auth_alg == WLAN_AUTH_FT) {
1395 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1396 HOSTAPD_LEVEL_DEBUG,
1397 "PMK from FT - skip IEEE 802.1X/EAP");
1398 /* Setup EAPOL state machines to already authenticated state
1399 * because of existing FT information from R0KH. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001400 sta->eapol_sm->keyRun = true;
1401 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1403 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001404 sta->eapol_sm->authSuccess = true;
1405 sta->eapol_sm->authFail = false;
1406 sta->eapol_sm->portValid = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 if (sta->eapol_sm->eap)
1408 eap_sm_notify_cached(sta->eapol_sm->eap);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001409 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 return;
1411 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001412#endif /* CONFIG_IEEE80211R_AP */
1413
1414#ifdef CONFIG_FILS
1415 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1416 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1417 sta->auth_alg == WLAN_AUTH_FILS_PK) {
1418 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1419 HOSTAPD_LEVEL_DEBUG,
1420 "PMK from FILS - skip IEEE 802.1X/EAP");
1421 /* Setup EAPOL state machines to already authenticated state
1422 * because of existing FILS information. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001423 sta->eapol_sm->keyRun = true;
1424 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001425 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1426 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001427 sta->eapol_sm->authSuccess = true;
1428 sta->eapol_sm->authFail = false;
1429 sta->eapol_sm->portValid = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001430 if (sta->eapol_sm->eap)
1431 eap_sm_notify_cached(sta->eapol_sm->eap);
Hai Shalom81f62d82019-07-22 12:10:00 -07001432 wpa_auth_set_ptk_rekey_timer(sta->wpa_sm);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001433 return;
1434 }
1435#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436
1437 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1438 if (pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1440 HOSTAPD_LEVEL_DEBUG,
1441 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1442 /* Setup EAPOL state machines to already authenticated state
1443 * because of existing PMKSA information in the cache. */
Hai Shalome21d4e82020-04-29 16:34:06 -07001444 sta->eapol_sm->keyRun = true;
1445 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1447 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
Hai Shalome21d4e82020-04-29 16:34:06 -07001448 sta->eapol_sm->authSuccess = true;
1449 sta->eapol_sm->authFail = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 if (sta->eapol_sm->eap)
1451 eap_sm_notify_cached(sta->eapol_sm->eap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001452 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001453 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 } else {
1455 if (reassoc) {
1456 /*
1457 * Force EAPOL state machines to start
1458 * re-authentication without having to wait for the
1459 * Supplicant to send EAPOL-Start.
1460 */
Hai Shalome21d4e82020-04-29 16:34:06 -07001461 sta->eapol_sm->reAuthenticate = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 }
1463 eapol_auth_step(sta->eapol_sm);
1464 }
1465}
1466
1467
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001468void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469{
1470 struct eapol_state_machine *sm = sta->eapol_sm;
1471
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001472#ifdef CONFIG_HS20
1473 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1474#endif /* CONFIG_HS20 */
1475
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001476 if (sta->pending_eapol_rx) {
1477 wpabuf_free(sta->pending_eapol_rx->buf);
1478 os_free(sta->pending_eapol_rx);
1479 sta->pending_eapol_rx = NULL;
1480 }
1481
Hai Shalomc3565922019-10-28 11:58:20 -07001482 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 return;
1484
1485 sta->eapol_sm = NULL;
1486
1487#ifndef CONFIG_NO_RADIUS
1488 radius_msg_free(sm->last_recv_radius);
1489 radius_free_class(&sm->radius_class);
1490#endif /* CONFIG_NO_RADIUS */
1491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492 eapol_auth_free(sm);
1493}
1494
1495
1496#ifndef CONFIG_NO_RADIUS
1497static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1498 struct sta_info *sta)
1499{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001500 struct wpabuf *eap;
1501 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 int eap_type = -1;
1503 char buf[64];
1504 struct radius_msg *msg;
1505 struct eapol_state_machine *sm = sta->eapol_sm;
1506
Hai Shalomc3565922019-10-28 11:58:20 -07001507 if (!sm || !sm->last_recv_radius) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 if (sm)
Hai Shalome21d4e82020-04-29 16:34:06 -07001509 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 return;
1511 }
1512
1513 msg = sm->last_recv_radius;
1514
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001515 eap = radius_msg_get_eap(msg);
Hai Shalomc3565922019-10-28 11:58:20 -07001516 if (!eap) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517 /* RFC 3579, Chap. 2.6.3:
1518 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1519 * attribute */
1520 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001521 HOSTAPD_LEVEL_WARNING,
1522 "could not extract EAP-Message from RADIUS message");
Hai Shalome21d4e82020-04-29 16:34:06 -07001523 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 return;
1525 }
1526
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001527 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001529 HOSTAPD_LEVEL_WARNING,
1530 "too short EAP packet received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001531 wpabuf_free(eap);
Hai Shalome21d4e82020-04-29 16:34:06 -07001532 sm->eap_if->aaaEapNoReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 return;
1534 }
1535
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001536 if (wpabuf_len(eap) > sizeof(*hdr))
1537 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001539 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540 switch (hdr->code) {
1541 case EAP_CODE_REQUEST:
1542 if (eap_type >= 0)
1543 sm->eap_type_authsrv = eap_type;
1544 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001545 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 break;
1547 case EAP_CODE_RESPONSE:
1548 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001549 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 break;
1551 case EAP_CODE_SUCCESS:
1552 os_strlcpy(buf, "EAP Success", sizeof(buf));
1553 break;
1554 case EAP_CODE_FAILURE:
1555 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1556 break;
1557 default:
1558 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1559 break;
1560 }
1561 buf[sizeof(buf) - 1] = '\0';
1562 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001563 HOSTAPD_LEVEL_DEBUG,
1564 "decapsulated EAP packet (code=%d id=%d len=%d) from RADIUS server: %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1566 buf);
Hai Shalome21d4e82020-04-29 16:34:06 -07001567 sm->eap_if->aaaEapReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568
1569 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001570 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571}
1572
1573
1574static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1575 struct sta_info *sta, struct radius_msg *msg,
1576 struct radius_msg *req,
1577 const u8 *shared_secret,
1578 size_t shared_secret_len)
1579{
1580 struct radius_ms_mppe_keys *keys;
Hai Shalom81f62d82019-07-22 12:10:00 -07001581 u8 *buf;
1582 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -07001584
1585 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 return;
1587
1588 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1589 shared_secret_len);
1590
1591 if (keys && keys->send && keys->recv) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001592 len = keys->send_len + keys->recv_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1594 keys->send, keys->send_len);
1595 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1596 keys->recv, keys->recv_len);
1597
1598 os_free(sm->eap_if->aaaEapKeyData);
1599 sm->eap_if->aaaEapKeyData = os_malloc(len);
1600 if (sm->eap_if->aaaEapKeyData) {
1601 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1602 keys->recv_len);
1603 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1604 keys->send, keys->send_len);
1605 sm->eap_if->aaaEapKeyDataLen = len;
Hai Shalome21d4e82020-04-29 16:34:06 -07001606 sm->eap_if->aaaEapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001608 } else {
1609 wpa_printf(MSG_DEBUG,
1610 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1611 keys, keys ? keys->send : NULL,
1612 keys ? keys->recv : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 }
1614
1615 if (keys) {
1616 os_free(keys->send);
1617 os_free(keys->recv);
1618 os_free(keys);
1619 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001620
1621 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
1622 NULL) == 0) {
1623 os_free(sm->eap_if->eapSessionId);
1624 sm->eap_if->eapSessionId = os_memdup(buf, len);
1625 if (sm->eap_if->eapSessionId) {
1626 sm->eap_if->eapSessionIdLen = len;
1627 wpa_hexdump(MSG_DEBUG, "EAP-Key Name",
1628 sm->eap_if->eapSessionId,
1629 sm->eap_if->eapSessionIdLen);
1630 }
1631 } else {
1632 sm->eap_if->eapSessionIdLen = 0;
1633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634}
1635
1636
1637static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1638 struct sta_info *sta,
1639 struct radius_msg *msg)
1640{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001641 u8 *attr_class;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 size_t class_len;
1643 struct eapol_state_machine *sm = sta->eapol_sm;
1644 int count, i;
1645 struct radius_attr_data *nclass;
1646 size_t nclass_count;
1647
Hai Shalomc3565922019-10-28 11:58:20 -07001648 if (!hapd->conf->radius->acct_server || !hapd->radius || !sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 return;
1650
1651 radius_free_class(&sm->radius_class);
1652 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1653 if (count <= 0)
1654 return;
1655
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001656 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Hai Shalomc3565922019-10-28 11:58:20 -07001657 if (!nclass)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 return;
1659
1660 nclass_count = 0;
1661
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001662 attr_class = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 for (i = 0; i < count; i++) {
1664 do {
1665 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001666 &attr_class, &class_len,
1667 attr_class) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 i = count;
1669 break;
1670 }
1671 } while (class_len < 1);
1672
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001673 nclass[nclass_count].data = os_memdup(attr_class, class_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001674 if (!nclass[nclass_count].data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 break;
1676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 nclass[nclass_count].len = class_len;
1678 nclass_count++;
1679 }
1680
1681 sm->radius_class.attr = nclass;
1682 sm->radius_class.count = nclass_count;
Hai Shalomc3565922019-10-28 11:58:20 -07001683 wpa_printf(MSG_DEBUG,
1684 "IEEE 802.1X: Stored %lu RADIUS Class attributes for "
1685 MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 (unsigned long) sm->radius_class.count,
1687 MAC2STR(sta->addr));
1688}
1689
1690
1691/* Update sta->identity based on User-Name attribute in Access-Accept */
1692static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1693 struct sta_info *sta,
1694 struct radius_msg *msg)
1695{
1696 u8 *buf, *identity;
1697 size_t len;
1698 struct eapol_state_machine *sm = sta->eapol_sm;
1699
Hai Shalomc3565922019-10-28 11:58:20 -07001700 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 return;
1702
1703 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1704 NULL) < 0)
1705 return;
1706
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001707 identity = (u8 *) dup_binstr(buf, len);
Hai Shalomc3565922019-10-28 11:58:20 -07001708 if (!identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 return;
1710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07001712 HOSTAPD_LEVEL_DEBUG,
1713 "old identity '%s' updated with User-Name from Access-Accept '%s'",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 sm->identity ? (char *) sm->identity : "N/A",
1715 (char *) identity);
1716
1717 os_free(sm->identity);
1718 sm->identity = identity;
1719 sm->identity_len = len;
1720}
1721
1722
Dmitry Shmidt04949592012-07-19 12:16:46 -07001723/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1724static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1725 struct sta_info *sta,
1726 struct radius_msg *msg)
1727{
1728 struct eapol_state_machine *sm = sta->eapol_sm;
1729 struct wpabuf *cui;
1730 u8 *buf;
1731 size_t len;
1732
Hai Shalomc3565922019-10-28 11:58:20 -07001733 if (!sm)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001734 return;
1735
1736 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1737 &buf, &len, NULL) < 0)
1738 return;
1739
1740 cui = wpabuf_alloc_copy(buf, len);
Hai Shalomc3565922019-10-28 11:58:20 -07001741 if (!cui)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001742 return;
1743
1744 wpabuf_free(sm->radius_cui);
1745 sm->radius_cui = cui;
1746}
1747
1748
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001749#ifdef CONFIG_HS20
1750
1751static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1752{
1753 sta->remediation = 1;
1754 os_free(sta->remediation_url);
1755 if (len > 2) {
1756 sta->remediation_url = os_malloc(len);
1757 if (!sta->remediation_url)
1758 return;
1759 sta->remediation_method = pos[0];
1760 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1761 sta->remediation_url[len - 1] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001762 wpa_printf(MSG_DEBUG,
1763 "HS 2.0: Subscription remediation needed for "
1764 MACSTR " - server method %u URL %s",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001765 MAC2STR(sta->addr), sta->remediation_method,
1766 sta->remediation_url);
1767 } else {
1768 sta->remediation_url = NULL;
Hai Shalomc3565922019-10-28 11:58:20 -07001769 wpa_printf(MSG_DEBUG,
1770 "HS 2.0: Subscription remediation needed for "
1771 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001772 }
1773 /* TODO: assign the STA into remediation VLAN or add filtering */
1774}
1775
1776
1777static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001778 struct sta_info *sta, const u8 *pos,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001779 size_t len)
1780{
Sunil Ravi77d572f2023-01-17 23:58:31 +00001781 size_t url_len;
1782 unsigned int timeout;
1783
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001784 if (len < 3)
1785 return; /* Malformed information */
Sunil Ravi77d572f2023-01-17 23:58:31 +00001786 url_len = len - 3;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001787 sta->hs20_deauth_requested = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001788 sta->hs20_deauth_on_ack = url_len == 0;
Hai Shalomc3565922019-10-28 11:58:20 -07001789 wpa_printf(MSG_DEBUG,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001790 "HS 2.0: Deauthentication request - Code %u Re-auth Delay %u URL length %zu",
1791 *pos, WPA_GET_LE16(pos + 1), url_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001792 wpabuf_free(sta->hs20_deauth_req);
1793 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1794 if (sta->hs20_deauth_req) {
1795 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
Sunil Ravi77d572f2023-01-17 23:58:31 +00001796 wpabuf_put_u8(sta->hs20_deauth_req, url_len);
1797 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, url_len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001798 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001799 timeout = hapd->conf->hs20_deauth_req_timeout;
1800 /* If there is no URL, no need to provide time to fetch it. Use a short
1801 * timeout here to allow maximum time for completing 4-way handshake and
1802 * WNM-Notification delivery. Acknowledgement of the frame will result
1803 * in cutting this wait further. */
1804 if (!url_len && timeout > 2)
1805 timeout = 2;
1806 ap_sta_session_timeout(hapd, sta, timeout);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001807}
1808
1809
1810static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1811 struct sta_info *sta, u8 *pos,
1812 size_t len, int session_timeout)
1813{
1814 unsigned int swt;
1815 int warning_time, beacon_int;
1816
1817 if (len < 1)
1818 return; /* Malformed information */
1819 os_free(sta->hs20_session_info_url);
1820 sta->hs20_session_info_url = os_malloc(len);
Hai Shalomc3565922019-10-28 11:58:20 -07001821 if (!sta->hs20_session_info_url)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001822 return;
1823 swt = pos[0];
1824 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1825 sta->hs20_session_info_url[len - 1] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001826 wpa_printf(MSG_DEBUG,
1827 "HS 2.0: Session Information URL='%s' SWT=%u (session_timeout=%d)",
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001828 sta->hs20_session_info_url, swt, session_timeout);
1829 if (session_timeout < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -07001830 wpa_printf(MSG_DEBUG,
1831 "HS 2.0: No Session-Timeout set - ignore session info URL");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001832 return;
1833 }
1834 if (swt == 255)
1835 swt = 1; /* Use one minute as the AP selected value */
1836
1837 if ((unsigned int) session_timeout < swt * 60)
1838 warning_time = 0;
1839 else
1840 warning_time = session_timeout - swt * 60;
1841
1842 beacon_int = hapd->iconf->beacon_int;
1843 if (beacon_int < 1)
1844 beacon_int = 100; /* best guess */
1845 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1846 if (sta->hs20_disassoc_timer > 65535)
1847 sta->hs20_disassoc_timer = 65535;
1848
1849 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1850}
1851
Roshan Pius3a1667e2018-07-03 15:17:14 -07001852
1853static void ieee802_1x_hs20_t_c_filtering(struct hostapd_data *hapd,
1854 struct sta_info *sta, u8 *pos,
1855 size_t len)
1856{
1857 if (len < 4)
1858 return; /* Malformed information */
1859 wpa_printf(MSG_DEBUG,
1860 "HS 2.0: Terms and Conditions filtering %02x %02x %02x %02x",
1861 pos[0], pos[1], pos[2], pos[3]);
1862 hs20_t_c_filtering(hapd, sta, pos[0] & BIT(0));
1863}
1864
1865
1866static void ieee802_1x_hs20_t_c_url(struct hostapd_data *hapd,
1867 struct sta_info *sta, u8 *pos, size_t len)
1868{
1869 os_free(sta->t_c_url);
1870 sta->t_c_url = os_malloc(len + 1);
1871 if (!sta->t_c_url)
1872 return;
1873 os_memcpy(sta->t_c_url, pos, len);
1874 sta->t_c_url[len] = '\0';
1875 wpa_printf(MSG_DEBUG,
1876 "HS 2.0: Terms and Conditions URL %s", sta->t_c_url);
1877}
1878
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001879#endif /* CONFIG_HS20 */
1880
1881
1882static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1883 struct sta_info *sta,
1884 struct radius_msg *msg,
1885 int session_timeout)
1886{
1887#ifdef CONFIG_HS20
1888 u8 *buf, *pos, *end, type, sublen;
1889 size_t len;
1890
1891 buf = NULL;
1892 sta->remediation = 0;
1893 sta->hs20_deauth_requested = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001894 sta->hs20_deauth_on_ack = 0;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001895
1896 for (;;) {
1897 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1898 &buf, &len, buf) < 0)
1899 break;
1900 if (len < 6)
1901 continue;
1902 pos = buf;
1903 end = buf + len;
1904 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1905 continue;
1906 pos += 4;
1907
1908 type = *pos++;
1909 sublen = *pos++;
1910 if (sublen < 2)
1911 continue; /* invalid length */
1912 sublen -= 2; /* skip header */
1913 if (pos + sublen > end)
1914 continue; /* invalid WFA VSA */
1915
1916 switch (type) {
1917 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1918 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1919 break;
1920 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1921 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1922 break;
1923 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1924 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1925 session_timeout);
1926 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001927 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING:
1928 ieee802_1x_hs20_t_c_filtering(hapd, sta, pos, sublen);
1929 break;
1930 case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL:
1931 ieee802_1x_hs20_t_c_url(hapd, sta, pos, sublen);
1932 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001933 }
1934 }
1935#endif /* CONFIG_HS20 */
1936}
1937
1938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939struct sta_id_search {
1940 u8 identifier;
1941 struct eapol_state_machine *sm;
1942};
1943
1944
1945static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1946 struct sta_info *sta,
1947 void *ctx)
1948{
1949 struct sta_id_search *id_search = ctx;
1950 struct eapol_state_machine *sm = sta->eapol_sm;
1951
1952 if (sm && sm->radius_identifier >= 0 &&
1953 sm->radius_identifier == id_search->identifier) {
1954 id_search->sm = sm;
1955 return 1;
1956 }
1957 return 0;
1958}
1959
1960
1961static struct eapol_state_machine *
1962ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1963{
1964 struct sta_id_search id_search;
Hai Shalomc3565922019-10-28 11:58:20 -07001965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 id_search.identifier = identifier;
1967 id_search.sm = NULL;
1968 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1969 return id_search.sm;
1970}
1971
1972
Hai Shalom74f70d42019-02-11 14:42:39 -08001973#ifndef CONFIG_NO_VLAN
1974static int ieee802_1x_update_vlan(struct radius_msg *msg,
1975 struct hostapd_data *hapd,
1976 struct sta_info *sta)
1977{
1978 struct vlan_description vlan_desc;
1979
1980 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1981 vlan_desc.notempty = !!radius_msg_get_vlanid(msg, &vlan_desc.untagged,
1982 MAX_NUM_TAGGED_VLAN,
1983 vlan_desc.tagged);
1984
1985 if (vlan_desc.notempty &&
1986 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
Hai Shalome21d4e82020-04-29 16:34:06 -07001987 sta->eapol_sm->authFail = true;
Hai Shalom74f70d42019-02-11 14:42:39 -08001988 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1989 HOSTAPD_LEVEL_INFO,
1990 "Invalid VLAN %d%s received from RADIUS server",
1991 vlan_desc.untagged,
1992 vlan_desc.tagged[0] ? "+" : "");
1993 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1994 ap_sta_set_vlan(hapd, sta, &vlan_desc);
1995 return -1;
1996 }
1997
1998 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1999 !vlan_desc.notempty) {
Hai Shalome21d4e82020-04-29 16:34:06 -07002000 sta->eapol_sm->authFail = true;
Hai Shalom74f70d42019-02-11 14:42:39 -08002001 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2002 HOSTAPD_LEVEL_INFO,
2003 "authentication server did not include required VLAN ID in Access-Accept");
2004 return -1;
2005 }
2006
2007 return ap_sta_set_vlan(hapd, sta, &vlan_desc);
2008}
2009#endif /* CONFIG_NO_VLAN */
2010
2011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012/**
2013 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
2014 * @msg: RADIUS response message
2015 * @req: RADIUS request message
2016 * @shared_secret: RADIUS shared secret
2017 * @shared_secret_len: Length of shared_secret in octets
2018 * @data: Context data (struct hostapd_data *)
2019 * Returns: Processing status
2020 */
2021static RadiusRxResult
2022ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
2023 const u8 *shared_secret, size_t shared_secret_len,
2024 void *data)
2025{
2026 struct hostapd_data *hapd = data;
2027 struct sta_info *sta;
2028 u32 session_timeout = 0, termination_action, acct_interim_interval;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002029 int session_timeout_set;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002030 u32 reason_code;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 struct eapol_state_machine *sm;
2032 int override_eapReq = 0;
2033 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
2034
2035 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
Hai Shalomc3565922019-10-28 11:58:20 -07002036 if (!sm) {
2037 wpa_printf(MSG_DEBUG,
2038 "IEEE 802.1X: Could not find matching station for this RADIUS message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 return RADIUS_RX_UNKNOWN;
2040 }
2041 sta = sm->sta;
2042
2043 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
2044 * present when packet contains an EAP-Message attribute */
2045 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
2046 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
2047 0) < 0 &&
2048 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
Hai Shalomc3565922019-10-28 11:58:20 -07002049 wpa_printf(MSG_DEBUG,
2050 "Allowing RADIUS Access-Reject without Message-Authenticator since it does not include EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
2052 req, 1)) {
Hai Shalomc3565922019-10-28 11:58:20 -07002053 wpa_printf(MSG_INFO,
2054 "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 return RADIUS_RX_INVALID_AUTHENTICATOR;
2056 }
2057
2058 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
2059 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
2060 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002061 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 return RADIUS_RX_UNKNOWN;
2063 }
2064
2065 sm->radius_identifier = -1;
2066 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
2067 MAC2STR(sta->addr));
2068
2069 radius_msg_free(sm->last_recv_radius);
2070 sm->last_recv_radius = msg;
2071
2072 session_timeout_set =
2073 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
2074 &session_timeout);
2075 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
2076 &termination_action))
2077 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
2078
2079 if (hapd->conf->acct_interim_interval == 0 &&
2080 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
2081 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
2082 &acct_interim_interval) == 0) {
2083 if (acct_interim_interval < 60) {
2084 hostapd_logger(hapd, sta->addr,
2085 HOSTAPD_MODULE_IEEE8021X,
2086 HOSTAPD_LEVEL_INFO,
Hai Shalomc3565922019-10-28 11:58:20 -07002087 "ignored too small Acct-Interim-Interval %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 acct_interim_interval);
2089 } else
2090 sta->acct_interim_interval = acct_interim_interval;
2091 }
2092
2093
2094 switch (hdr->code) {
2095 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096#ifndef CONFIG_NO_VLAN
Hai Shalom74f70d42019-02-11 14:42:39 -08002097 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
2098 ieee802_1x_update_vlan(msg, hapd, sta) < 0)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002099 break;
2100
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002101 if (sta->vlan_id > 0) {
2102 hostapd_logger(hapd, sta->addr,
2103 HOSTAPD_MODULE_RADIUS,
2104 HOSTAPD_LEVEL_INFO,
2105 "VLAN ID %d", sta->vlan_id);
2106 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002107
Dmitry Shmidt83474442015-04-15 13:47:09 -07002108 if ((sta->flags & WLAN_STA_ASSOC) &&
2109 ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08002111#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002113 sta->session_timeout_set = !!session_timeout_set;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002114 os_get_reltime(&sta->session_timeout);
2115 sta->session_timeout.sec += session_timeout;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 /* RFC 3580, Ch. 3.17 */
2118 if (session_timeout_set && termination_action ==
Roshan Pius3a1667e2018-07-03 15:17:14 -07002119 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 sm->reAuthPeriod = session_timeout;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002121 else if (session_timeout_set)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 ap_sta_session_timeout(hapd, sta, session_timeout);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002123 else
2124 ap_sta_no_session_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
Hai Shalome21d4e82020-04-29 16:34:06 -07002126 sm->eap_if->aaaSuccess = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 override_eapReq = 1;
2128 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
2129 shared_secret_len);
2130 ieee802_1x_store_radius_class(hapd, sta, msg);
2131 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002132 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002133 ieee802_1x_check_hs20(hapd, sta, msg,
2134 session_timeout_set ?
2135 (int) session_timeout : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136 break;
2137 case RADIUS_CODE_ACCESS_REJECT:
Hai Shalome21d4e82020-04-29 16:34:06 -07002138 sm->eap_if->aaaFail = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 override_eapReq = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002140 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_WLAN_REASON_CODE,
2141 &reason_code) == 0) {
2142 wpa_printf(MSG_DEBUG,
2143 "RADIUS server indicated WLAN-Reason-Code %u in Access-Reject for "
2144 MACSTR, reason_code, MAC2STR(sta->addr));
2145 sta->disconnect_reason_code = reason_code;
2146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 break;
2148 case RADIUS_CODE_ACCESS_CHALLENGE:
Hai Shalome21d4e82020-04-29 16:34:06 -07002149 sm->eap_if->aaaEapReq = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 if (session_timeout_set) {
2151 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
2152 sm->eap_if->aaaMethodTimeout = session_timeout;
2153 hostapd_logger(hapd, sm->addr,
2154 HOSTAPD_MODULE_IEEE8021X,
2155 HOSTAPD_LEVEL_DEBUG,
Hai Shalomc3565922019-10-28 11:58:20 -07002156 "using EAP timeout of %d seconds (from RADIUS)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157 sm->eap_if->aaaMethodTimeout);
2158 } else {
2159 /*
2160 * Use dynamic retransmission behavior per EAP
2161 * specification.
2162 */
2163 sm->eap_if->aaaMethodTimeout = 0;
2164 }
2165 break;
2166 }
2167
2168 ieee802_1x_decapsulate_radius(hapd, sta);
2169 if (override_eapReq)
Hai Shalome21d4e82020-04-29 16:34:06 -07002170 sm->eap_if->aaaEapReq = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002172#ifdef CONFIG_FILS
2173#ifdef NEED_AP_MLME
Hai Shalom60840252021-02-19 19:02:11 -08002174 if (sta->flags &
2175 (WLAN_STA_PENDING_FILS_ERP | WLAN_STA_PENDING_PASN_FILS_ERP)) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002176 /* TODO: Add a PMKSA entry on success? */
2177 ieee802_11_finish_fils_auth(
2178 hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
2179 sm->eap_if->aaaEapReqData,
2180 sm->eap_if->aaaEapKeyData,
2181 sm->eap_if->aaaEapKeyDataLen);
2182 }
2183#endif /* NEED_AP_MLME */
2184#endif /* CONFIG_FILS */
2185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186 eapol_auth_step(sm);
2187
2188 return RADIUS_RX_QUEUED;
2189}
2190#endif /* CONFIG_NO_RADIUS */
2191
2192
2193void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
2194{
2195 struct eapol_state_machine *sm = sta->eapol_sm;
Hai Shalomc3565922019-10-28 11:58:20 -07002196
2197 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002198 return;
2199
2200 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2201 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
2202
2203#ifndef CONFIG_NO_RADIUS
2204 radius_msg_free(sm->last_recv_radius);
2205 sm->last_recv_radius = NULL;
2206#endif /* CONFIG_NO_RADIUS */
2207
2208 if (sm->eap_if->eapTimeout) {
2209 /*
2210 * Disconnect the STA since it did not reply to the last EAP
2211 * request and we cannot continue EAP processing (EAP-Failure
2212 * could only be sent if the EAP peer actually replied).
2213 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002214 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
2215 MAC2STR(sta->addr));
2216
Hai Shalome21d4e82020-04-29 16:34:06 -07002217 sm->eap_if->portEnabled = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218 ap_sta_disconnect(hapd, sta, sta->addr,
2219 WLAN_REASON_PREV_AUTH_NOT_VALID);
2220 }
2221}
2222
2223
Hai Shalomfdcde762020-04-02 11:19:20 -07002224#ifdef CONFIG_WEP
2225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
2227{
2228 struct eapol_authenticator *eapol = hapd->eapol_auth;
2229
2230 if (hapd->conf->default_wep_key_len < 1)
2231 return 0;
2232
2233 os_free(eapol->default_wep_key);
2234 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002235 if (!eapol->default_wep_key ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 random_get_bytes(eapol->default_wep_key,
2237 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002238 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 os_free(eapol->default_wep_key);
2240 eapol->default_wep_key = NULL;
2241 return -1;
2242 }
2243
2244 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
2245 eapol->default_wep_key,
2246 hapd->conf->default_wep_key_len);
2247
2248 return 0;
2249}
2250
2251
2252static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
2253 struct sta_info *sta, void *ctx)
2254{
2255 if (sta->eapol_sm) {
Hai Shalome21d4e82020-04-29 16:34:06 -07002256 sta->eapol_sm->eap_if->eapKeyAvailable = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 eapol_auth_step(sta->eapol_sm);
2258 }
2259 return 0;
2260}
2261
2262
2263static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
2264{
2265 struct hostapd_data *hapd = eloop_ctx;
2266 struct eapol_authenticator *eapol = hapd->eapol_auth;
2267
2268 if (eapol->default_wep_key_idx >= 3)
2269 eapol->default_wep_key_idx =
2270 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
2271 else
2272 eapol->default_wep_key_idx++;
2273
2274 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
2275 eapol->default_wep_key_idx);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277 if (ieee802_1x_rekey_broadcast(hapd)) {
2278 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002279 HOSTAPD_LEVEL_WARNING,
2280 "failed to generate a new broadcast key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 os_free(eapol->default_wep_key);
2282 eapol->default_wep_key = NULL;
2283 return;
2284 }
2285
2286 /* TODO: Could setup key for RX here, but change default TX keyid only
2287 * after new broadcast key has been sent to all stations. */
2288 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
2289 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002290 eapol->default_wep_key_idx, 0, 1, NULL, 0,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 eapol->default_wep_key,
Hai Shalomfdcde762020-04-02 11:19:20 -07002292 hapd->conf->default_wep_key_len,
2293 KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002295 HOSTAPD_LEVEL_WARNING,
2296 "failed to configure a new broadcast key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 os_free(eapol->default_wep_key);
2298 eapol->default_wep_key = NULL;
2299 return;
2300 }
2301
2302 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
2303
2304 if (hapd->conf->wep_rekeying_period > 0) {
2305 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
2306 ieee802_1x_rekey, hapd, NULL);
2307 }
2308}
2309
Hai Shalomfdcde762020-04-02 11:19:20 -07002310#endif /* CONFIG_WEP */
2311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312
2313static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2314 const u8 *data, size_t datalen)
2315{
2316#ifdef CONFIG_WPS
2317 struct sta_info *sta = sta_ctx;
2318
2319 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2320 WLAN_STA_MAYBE_WPS) {
2321 const u8 *identity;
2322 size_t identity_len;
2323 struct eapol_state_machine *sm = sta->eapol_sm;
2324
2325 identity = eap_get_identity(sm->eap, &identity_len);
2326 if (identity &&
2327 ((identity_len == WSC_ID_ENROLLEE_LEN &&
2328 os_memcmp(identity, WSC_ID_ENROLLEE,
2329 WSC_ID_ENROLLEE_LEN) == 0) ||
2330 (identity_len == WSC_ID_REGISTRAR_LEN &&
2331 os_memcmp(identity, WSC_ID_REGISTRAR,
2332 WSC_ID_REGISTRAR_LEN) == 0))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002333 wpa_printf(MSG_DEBUG,
2334 "WPS: WLAN_STA_MAYBE_WPS -> WLAN_STA_WPS");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 sta->flags |= WLAN_STA_WPS;
2336 }
2337 }
2338#endif /* CONFIG_WPS */
2339
2340 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2341}
2342
2343
2344static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2345 const u8 *data, size_t datalen)
2346{
2347#ifndef CONFIG_NO_RADIUS
2348 struct hostapd_data *hapd = ctx;
2349 struct sta_info *sta = sta_ctx;
2350
2351 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2352#endif /* CONFIG_NO_RADIUS */
2353}
2354
2355
Sunil Ravi640215c2023-06-28 23:08:09 +00002356static bool _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2357 int preauth, int remediation, bool logoff)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358{
2359 struct hostapd_data *hapd = ctx;
2360 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002361
Sunil Ravi640215c2023-06-28 23:08:09 +00002362 if (preauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 rsn_preauth_finished(hapd, sta, success);
Sunil Ravi640215c2023-06-28 23:08:09 +00002364 return false;
2365 }
2366
2367 return ieee802_1x_finished(hapd, sta, success, remediation, logoff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368}
2369
2370
2371static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2372 size_t identity_len, int phase2,
2373 struct eap_user *user)
2374{
2375 struct hostapd_data *hapd = ctx;
2376 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377 int i;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002378 int rv = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002380 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002381 if (!eap_user)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002382 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383
2384 os_memset(user, 0, sizeof(*user));
2385 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 user->methods[i].vendor = eap_user->methods[i].vendor;
2388 user->methods[i].method = eap_user->methods[i].method;
2389 }
2390
2391 if (eap_user->password) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002392 user->password = os_memdup(eap_user->password,
2393 eap_user->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002394 if (!user->password)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002395 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002397 user->password_hash = eap_user->password_hash;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002398 if (eap_user->salt && eap_user->salt_len) {
2399 user->salt = os_memdup(eap_user->salt,
2400 eap_user->salt_len);
2401 if (!user->salt)
2402 goto out;
2403 user->salt_len = eap_user->salt_len;
2404 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 }
2406 user->force_version = eap_user->force_version;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002407 user->macacl = eap_user->macacl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002409 user->remediation = eap_user->remediation;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002410 rv = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002412out:
2413 if (rv)
2414 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2415
2416 return rv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417}
2418
2419
2420static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2421{
2422 struct hostapd_data *hapd = ctx;
2423 struct sta_info *sta;
Hai Shalomc3565922019-10-28 11:58:20 -07002424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 sta = ap_get_sta(hapd, addr);
Hai Shalomc3565922019-10-28 11:58:20 -07002426 if (!sta || !sta->eapol_sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 return 0;
2428 return 1;
2429}
2430
2431
2432static void ieee802_1x_logger(void *ctx, const u8 *addr,
2433 eapol_logger_level level, const char *txt)
2434{
2435#ifndef CONFIG_NO_HOSTAPD_LOGGER
2436 struct hostapd_data *hapd = ctx;
2437 int hlevel;
2438
2439 switch (level) {
2440 case EAPOL_LOGGER_WARNING:
2441 hlevel = HOSTAPD_LEVEL_WARNING;
2442 break;
2443 case EAPOL_LOGGER_INFO:
2444 hlevel = HOSTAPD_LEVEL_INFO;
2445 break;
2446 case EAPOL_LOGGER_DEBUG:
2447 default:
2448 hlevel = HOSTAPD_LEVEL_DEBUG;
2449 break;
2450 }
2451
2452 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2453 txt);
2454#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2455}
2456
2457
2458static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2459 int authorized)
2460{
2461 struct hostapd_data *hapd = ctx;
2462 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2465}
2466
2467
2468static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2469{
2470 struct hostapd_data *hapd = ctx;
2471 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 ieee802_1x_abort_auth(hapd, sta);
2474}
2475
2476
Hai Shalomfdcde762020-04-02 11:19:20 -07002477#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2479{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002480#ifndef CONFIG_FIPS
2481#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 struct hostapd_data *hapd = ctx;
2483 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485 ieee802_1x_tx_key(hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002486#endif /* CONFIG_NO_RC4 */
2487#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488}
Hai Shalomfdcde762020-04-02 11:19:20 -07002489#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
2491
2492static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2493 enum eapol_event type)
2494{
2495 /* struct hostapd_data *hapd = ctx; */
2496 struct sta_info *sta = sta_ctx;
Hai Shalomc3565922019-10-28 11:58:20 -07002497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 switch (type) {
2499 case EAPOL_AUTH_SM_CHANGE:
2500 wpa_auth_sm_notify(sta->wpa_sm);
2501 break;
2502 case EAPOL_AUTH_REAUTHENTICATE:
2503 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2504 break;
2505 }
2506}
2507
2508
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002509#ifdef CONFIG_ERP
2510
2511static struct eap_server_erp_key *
2512ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2513{
2514 struct hostapd_data *hapd = ctx;
2515 struct eap_server_erp_key *erp;
2516
2517 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2518 list) {
2519 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2520 return erp;
2521 }
2522
2523 return NULL;
2524}
2525
2526
2527static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2528{
2529 struct hostapd_data *hapd = ctx;
2530
2531 dl_list_add(&hapd->erp_keys, &erp->list);
2532 return 0;
2533}
2534
2535#endif /* CONFIG_ERP */
2536
2537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538int ieee802_1x_init(struct hostapd_data *hapd)
2539{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 struct eapol_auth_config conf;
2541 struct eapol_auth_cb cb;
2542
Sunil Ravi88611412024-06-28 17:34:56 +00002543 if (hapd->mld_first_bss) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002544 wpa_printf(MSG_DEBUG,
2545 "MLD: Using IEEE 802.1X state machine of the first BSS");
2546
Sunil Ravi88611412024-06-28 17:34:56 +00002547 hapd->eapol_auth = hapd->mld_first_bss->eapol_auth;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002548 return 0;
2549 }
2550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002551 dl_list_init(&hapd->erp_keys);
2552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 os_memset(&conf, 0, sizeof(conf));
Hai Shalomc3565922019-10-28 11:58:20 -07002554 conf.eap_cfg = hapd->eap_cfg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 conf.ctx = hapd;
2556 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2557 conf.wpa = hapd->conf->wpa;
Hai Shalomfdcde762020-04-02 11:19:20 -07002558#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
Hai Shalomfdcde762020-04-02 11:19:20 -07002560#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2562 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002563 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2564 conf.erp_domain = hapd->conf->erp_domain;
Sunil Ravia04bd252022-05-02 22:54:18 -07002565#ifdef CONFIG_TESTING_OPTIONS
2566 conf.eap_skip_prot_success = hapd->conf->eap_skip_prot_success;
2567#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568
2569 os_memset(&cb, 0, sizeof(cb));
2570 cb.eapol_send = ieee802_1x_eapol_send;
2571 cb.aaa_send = ieee802_1x_aaa_send;
2572 cb.finished = _ieee802_1x_finished;
2573 cb.get_eap_user = ieee802_1x_get_eap_user;
2574 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2575 cb.logger = ieee802_1x_logger;
2576 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2577 cb.abort_auth = _ieee802_1x_abort_auth;
Hai Shalomfdcde762020-04-02 11:19:20 -07002578#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 cb.tx_key = _ieee802_1x_tx_key;
Hai Shalomfdcde762020-04-02 11:19:20 -07002580#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 cb.eapol_event = ieee802_1x_eapol_event;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002582#ifdef CONFIG_ERP
2583 cb.erp_get_key = ieee802_1x_erp_get_key;
2584 cb.erp_add_key = ieee802_1x_erp_add_key;
2585#endif /* CONFIG_ERP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586
2587 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
Hai Shalomc3565922019-10-28 11:58:20 -07002588 if (!hapd->eapol_auth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 return -1;
2590
2591 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2592 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2593 return -1;
2594
2595#ifndef CONFIG_NO_RADIUS
2596 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2597 ieee802_1x_receive_auth, hapd))
2598 return -1;
2599#endif /* CONFIG_NO_RADIUS */
2600
Hai Shalomfdcde762020-04-02 11:19:20 -07002601#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 if (hapd->conf->default_wep_key_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002603 int i;
2604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 for (i = 0; i < 4; i++)
2606 hostapd_drv_set_key(hapd->conf->iface, hapd,
Hai Shalomfdcde762020-04-02 11:19:20 -07002607 WPA_ALG_NONE, NULL, i, 0, 0, NULL,
2608 0, NULL, 0, KEY_FLAG_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609
2610 ieee802_1x_rekey(hapd, NULL);
2611
Hai Shalomc3565922019-10-28 11:58:20 -07002612 if (!hapd->eapol_auth->default_wep_key)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 return -1;
2614 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002615#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616
2617 return 0;
2618}
2619
2620
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002621void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2622{
2623 struct eap_server_erp_key *erp;
2624
2625 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2626 list)) != NULL) {
2627 dl_list_del(&erp->list);
2628 bin_clear_free(erp, sizeof(*erp));
2629 }
2630}
2631
2632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633void ieee802_1x_deinit(struct hostapd_data *hapd)
2634{
Sunil Ravi88611412024-06-28 17:34:56 +00002635 if (hapd->mld_first_bss) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002636 wpa_printf(MSG_DEBUG,
2637 "MLD: Deinit IEEE 802.1X state machine of a non-first BSS");
2638
2639 hapd->eapol_auth = NULL;
2640 return;
2641 }
2642
Hai Shalomfdcde762020-04-02 11:19:20 -07002643#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07002645#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002647 if (hapd->driver && hapd->drv_priv &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2649 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2650
2651 eapol_auth_deinit(hapd->eapol_auth);
2652 hapd->eapol_auth = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002653
2654 ieee802_1x_erp_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655}
2656
2657
2658int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2659 const u8 *buf, size_t len, int ack)
2660{
2661 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 u8 *pos;
2663 const unsigned char rfc1042_hdr[ETH_ALEN] =
2664 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2665
Hai Shalomc3565922019-10-28 11:58:20 -07002666 if (!sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002668 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 return 0;
2670
2671 hdr = (struct ieee80211_hdr *) buf;
2672 pos = (u8 *) (hdr + 1);
2673 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2674 return 0;
2675 pos += sizeof(rfc1042_hdr);
2676 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2677 return 0;
2678 pos += 2;
2679
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002680 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2681 ack);
2682}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002684
2685int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2686 const u8 *buf, int len, int ack)
2687{
2688 const struct ieee802_1x_hdr *xhdr =
2689 (const struct ieee802_1x_hdr *) buf;
2690 const u8 *pos = buf + sizeof(*xhdr);
2691 struct ieee802_1x_eapol_key *key;
2692
2693 if (len < (int) sizeof(*xhdr))
2694 return 0;
Hai Shalomc3565922019-10-28 11:58:20 -07002695 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
2696 " TX status - version=%d type=%d length=%d - ack=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2698 be_to_host16(xhdr->length), ack);
2699
Dmitry Shmidt29333592017-01-09 12:27:11 -08002700#ifdef CONFIG_WPS
2701 if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2702 (sta->flags & WLAN_STA_WPS) &&
2703 ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2704 wpa_printf(MSG_DEBUG,
2705 "WPS: Indicate EAP completion on ACK for EAP-Failure");
2706 hostapd_wps_eap_completed(hapd);
2707 }
2708#endif /* CONFIG_WPS */
2709
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002710 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2711 return 0;
2712
2713 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714 const struct wpa_eapol_key *wpa;
Hai Shalomc3565922019-10-28 11:58:20 -07002715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 wpa = (const struct wpa_eapol_key *) pos;
2717 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2718 wpa->type == EAPOL_KEY_TYPE_WPA)
2719 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2720 sta->wpa_sm, ack);
2721 }
2722
2723 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2724 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726 * packets couple of times because otherwise STA keys become
2727 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002728 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729 key = (struct ieee802_1x_eapol_key *) pos;
2730 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
Hai Shalomc3565922019-10-28 11:58:20 -07002731 HOSTAPD_LEVEL_DEBUG,
2732 "did not Ack EAPOL-Key frame (%scast index=%d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 key->key_index & BIT(7) ? "uni" : "broad",
2734 key->key_index & ~BIT(7));
2735 /* TODO: re-send EAPOL-Key couple of times (with short delay
2736 * between them?). If all attempt fail, report error and
2737 * deauthenticate STA so that it will get new keys when
2738 * authenticating again (e.g., after returning in range).
2739 * Separate limit/transmit state needed both for unicast and
2740 * broadcast keys(?) */
2741 }
2742 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2743 * to here and change the key only if the EAPOL-Key packet was Acked.
2744 */
2745
2746 return 1;
2747}
2748
2749
2750u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2751{
Hai Shalomc3565922019-10-28 11:58:20 -07002752 if (!sm || !sm->identity)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753 return NULL;
2754
2755 *len = sm->identity_len;
2756 return sm->identity;
2757}
2758
2759
2760u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2761 int idx)
2762{
Hai Shalomc3565922019-10-28 11:58:20 -07002763 if (!sm || !sm->radius_class.attr ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 idx >= (int) sm->radius_class.count)
2765 return NULL;
2766
2767 *len = sm->radius_class.attr[idx].len;
2768 return sm->radius_class.attr[idx].data;
2769}
2770
2771
Dmitry Shmidt04949592012-07-19 12:16:46 -07002772struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2773{
Hai Shalomc3565922019-10-28 11:58:20 -07002774 if (!sm)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002775 return NULL;
2776 return sm->radius_cui;
2777}
2778
2779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2781{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002782 *len = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07002783 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784 return NULL;
2785
2786 *len = sm->eap_if->eapKeyDataLen;
2787 return sm->eap_if->eapKeyData;
2788}
2789
2790
Hai Shalom81f62d82019-07-22 12:10:00 -07002791#ifdef CONFIG_MACSEC
2792const u8 * ieee802_1x_get_session_id(struct eapol_state_machine *sm,
2793 size_t *len)
2794{
2795 *len = 0;
2796 if (!sm || !sm->eap_if)
2797 return NULL;
2798
2799 *len = sm->eap_if->eapSessionIdLen;
2800 return sm->eap_if->eapSessionId;
2801}
2802#endif /* CONFIG_MACSEC */
2803
2804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
Hai Shalome21d4e82020-04-29 16:34:06 -07002806 bool enabled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807{
Hai Shalomc3565922019-10-28 11:58:20 -07002808 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 return;
Hai Shalome21d4e82020-04-29 16:34:06 -07002810 sm->eap_if->portEnabled = enabled;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 eapol_auth_step(sm);
2812}
2813
2814
Hai Shalome21d4e82020-04-29 16:34:06 -07002815void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm, bool valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816{
Hai Shalomc3565922019-10-28 11:58:20 -07002817 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 return;
Hai Shalome21d4e82020-04-29 16:34:06 -07002819 sm->portValid = valid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 eapol_auth_step(sm);
2821}
2822
2823
Hai Shalome21d4e82020-04-29 16:34:06 -07002824void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, bool pre_auth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825{
Hai Shalomc3565922019-10-28 11:58:20 -07002826 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 return;
2828 if (pre_auth)
2829 sm->flags |= EAPOL_SM_PREAUTH;
2830 else
2831 sm->flags &= ~EAPOL_SM_PREAUTH;
2832}
2833
2834
Hai Shalome21d4e82020-04-29 16:34:06 -07002835static const char * bool_txt(bool val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002837 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838}
2839
2840
2841int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2842{
2843 /* TODO */
2844 return 0;
2845}
2846
2847
2848int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2849 char *buf, size_t buflen)
2850{
2851 int len = 0, ret;
2852 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002853 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002854 const char *name1;
2855 const char *name2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002856 char *identity_buf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857
Hai Shalomc3565922019-10-28 11:58:20 -07002858 if (!sm)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 return 0;
2860
2861 ret = os_snprintf(buf + len, buflen - len,
2862 "dot1xPaePortNumber=%d\n"
2863 "dot1xPaePortProtocolVersion=%d\n"
2864 "dot1xPaePortCapabilities=1\n"
2865 "dot1xPaePortInitialize=%d\n"
2866 "dot1xPaePortReauthenticate=FALSE\n",
2867 sta->aid,
2868 EAPOL_VERSION,
2869 sm->initialize);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002870 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871 return len;
2872 len += ret;
2873
2874 /* dot1xAuthConfigTable */
2875 ret = os_snprintf(buf + len, buflen - len,
2876 "dot1xAuthPaeState=%d\n"
2877 "dot1xAuthBackendAuthState=%d\n"
2878 "dot1xAuthAdminControlledDirections=%d\n"
2879 "dot1xAuthOperControlledDirections=%d\n"
2880 "dot1xAuthAuthControlledPortStatus=%d\n"
2881 "dot1xAuthAuthControlledPortControl=%d\n"
2882 "dot1xAuthQuietPeriod=%u\n"
2883 "dot1xAuthServerTimeout=%u\n"
2884 "dot1xAuthReAuthPeriod=%u\n"
2885 "dot1xAuthReAuthEnabled=%s\n"
2886 "dot1xAuthKeyTxEnabled=%s\n",
2887 sm->auth_pae_state + 1,
2888 sm->be_auth_state + 1,
2889 sm->adminControlledDirections,
2890 sm->operControlledDirections,
2891 sm->authPortStatus,
2892 sm->portControl,
2893 sm->quietPeriod,
2894 sm->serverTimeout,
2895 sm->reAuthPeriod,
2896 bool_txt(sm->reAuthEnabled),
2897 bool_txt(sm->keyTxEnabled));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002898 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 return len;
2900 len += ret;
2901
2902 /* dot1xAuthStatsTable */
2903 ret = os_snprintf(buf + len, buflen - len,
2904 "dot1xAuthEapolFramesRx=%u\n"
2905 "dot1xAuthEapolFramesTx=%u\n"
2906 "dot1xAuthEapolStartFramesRx=%u\n"
2907 "dot1xAuthEapolLogoffFramesRx=%u\n"
2908 "dot1xAuthEapolRespIdFramesRx=%u\n"
2909 "dot1xAuthEapolRespFramesRx=%u\n"
2910 "dot1xAuthEapolReqIdFramesTx=%u\n"
2911 "dot1xAuthEapolReqFramesTx=%u\n"
2912 "dot1xAuthInvalidEapolFramesRx=%u\n"
2913 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2914 "dot1xAuthLastEapolFrameVersion=%u\n"
2915 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2916 sm->dot1xAuthEapolFramesRx,
2917 sm->dot1xAuthEapolFramesTx,
2918 sm->dot1xAuthEapolStartFramesRx,
2919 sm->dot1xAuthEapolLogoffFramesRx,
2920 sm->dot1xAuthEapolRespIdFramesRx,
2921 sm->dot1xAuthEapolRespFramesRx,
2922 sm->dot1xAuthEapolReqIdFramesTx,
2923 sm->dot1xAuthEapolReqFramesTx,
2924 sm->dot1xAuthInvalidEapolFramesRx,
2925 sm->dot1xAuthEapLengthErrorFramesRx,
2926 sm->dot1xAuthLastEapolFrameVersion,
2927 MAC2STR(sm->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002928 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 return len;
2930 len += ret;
2931
2932 /* dot1xAuthDiagTable */
2933 ret = os_snprintf(buf + len, buflen - len,
2934 "dot1xAuthEntersConnecting=%u\n"
2935 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2936 "dot1xAuthEntersAuthenticating=%u\n"
2937 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2938 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2939 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2940 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2941 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2942 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2943 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2944 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2945 "dot1xAuthBackendResponses=%u\n"
2946 "dot1xAuthBackendAccessChallenges=%u\n"
2947 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2948 "dot1xAuthBackendAuthSuccesses=%u\n"
2949 "dot1xAuthBackendAuthFails=%u\n",
2950 sm->authEntersConnecting,
2951 sm->authEapLogoffsWhileConnecting,
2952 sm->authEntersAuthenticating,
2953 sm->authAuthSuccessesWhileAuthenticating,
2954 sm->authAuthTimeoutsWhileAuthenticating,
2955 sm->authAuthFailWhileAuthenticating,
2956 sm->authAuthEapStartsWhileAuthenticating,
2957 sm->authAuthEapLogoffWhileAuthenticating,
2958 sm->authAuthReauthsWhileAuthenticated,
2959 sm->authAuthEapStartsWhileAuthenticated,
2960 sm->authAuthEapLogoffWhileAuthenticated,
2961 sm->backendResponses,
2962 sm->backendAccessChallenges,
2963 sm->backendOtherRequestsToSupplicant,
2964 sm->backendAuthSuccesses,
2965 sm->backendAuthFails);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002966 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 return len;
2968 len += ret;
2969
2970 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002971 os_reltime_age(&sta->acct_session_start, &diff);
Hai Shalom74f70d42019-02-11 14:42:39 -08002972 if (sm->eap && !sm->identity) {
2973 const u8 *id;
2974 size_t id_len;
2975
2976 id = eap_get_identity(sm->eap, &id_len);
2977 if (id)
2978 identity_buf = dup_binstr(id, id_len);
2979 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 ret = os_snprintf(buf + len, buflen - len,
2981 /* TODO: dot1xAuthSessionOctetsRx */
2982 /* TODO: dot1xAuthSessionOctetsTx */
2983 /* TODO: dot1xAuthSessionFramesRx */
2984 /* TODO: dot1xAuthSessionFramesTx */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002985 "dot1xAuthSessionId=%016llX\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 "dot1xAuthSessionAuthenticMethod=%d\n"
2987 "dot1xAuthSessionTime=%u\n"
2988 "dot1xAuthSessionTerminateCause=999\n"
2989 "dot1xAuthSessionUserName=%s\n",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002990 (unsigned long long) sta->acct_session_id,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 (wpa_key_mgmt_wpa_ieee8021x(
2992 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2993 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002994 (unsigned int) diff.sec,
Hai Shalom021b0b52019-04-10 11:17:58 -07002995 sm->identity ? (char *) sm->identity :
2996 (identity_buf ? identity_buf : "N/A"));
Hai Shalom74f70d42019-02-11 14:42:39 -08002997 os_free(identity_buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 return len;
3000 len += ret;
3001
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003002 if (sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003003 ret = os_snprintf(buf + len, buflen - len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003004 "authMultiSessionId=%016llX\n",
3005 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08003006 sm->acct_multi_session_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003007 if (os_snprintf_error(buflen - len, ret))
3008 return len;
3009 len += ret;
3010 }
3011
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003012 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
3013 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003014 ret = os_snprintf(buf + len, buflen - len,
3015 "last_eap_type_as=%d (%s)\n"
3016 "last_eap_type_sta=%d (%s)\n",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003017 sm->eap_type_authsrv, name1,
3018 sm->eap_type_supp, name2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003019 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003020 return len;
3021 len += ret;
3022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 return len;
3024}
3025
3026
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003027#ifdef CONFIG_HS20
3028static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
3029{
3030 struct hostapd_data *hapd = eloop_ctx;
3031 struct sta_info *sta = timeout_ctx;
3032
3033 if (sta->remediation) {
3034 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3035 MACSTR " to indicate Subscription Remediation",
3036 MAC2STR(sta->addr));
3037 hs20_send_wnm_notification(hapd, sta->addr,
3038 sta->remediation_method,
3039 sta->remediation_url);
3040 os_free(sta->remediation_url);
3041 sta->remediation_url = NULL;
3042 }
3043
3044 if (sta->hs20_deauth_req) {
3045 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3046 MACSTR " to indicate imminent deauthentication",
3047 MAC2STR(sta->addr));
3048 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
3049 sta->hs20_deauth_req);
3050 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003051
3052 if (sta->hs20_t_c_filtering) {
3053 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3054 MACSTR " to indicate Terms and Conditions filtering",
3055 MAC2STR(sta->addr));
3056 hs20_send_wnm_notification_t_c(hapd, sta->addr, sta->t_c_url);
3057 os_free(sta->t_c_url);
3058 sta->t_c_url = NULL;
3059 }
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003060}
3061#endif /* CONFIG_HS20 */
3062
3063
Sunil Ravi640215c2023-06-28 23:08:09 +00003064static bool ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003065 struct sta_info *sta, int success,
Sunil Ravi640215c2023-06-28 23:08:09 +00003066 int remediation, bool logoff)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067{
3068 const u8 *key;
3069 size_t len;
3070 /* TODO: get PMKLifetime from WPA parameters */
3071 static const int dot11RSNAConfigPMKLifetime = 43200;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003072 unsigned int session_timeout;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003073 struct os_reltime now, remaining;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003075#ifdef CONFIG_HS20
3076 if (remediation && !sta->remediation) {
3077 sta->remediation = 1;
3078 os_free(sta->remediation_url);
3079 sta->remediation_url =
3080 os_strdup(hapd->conf->subscr_remediation_url);
3081 sta->remediation_method = 1; /* SOAP-XML SPP */
3082 }
3083
Roshan Pius3a1667e2018-07-03 15:17:14 -07003084 if (success && (sta->remediation || sta->hs20_deauth_req ||
3085 sta->hs20_t_c_filtering)) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003086 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
3087 MACSTR " in 100 ms", MAC2STR(sta->addr));
3088 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
3089 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
3090 hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003091 }
3092#endif /* CONFIG_HS20 */
3093
Hai Shalom81f62d82019-07-22 12:10:00 -07003094#ifdef CONFIG_MACSEC
3095 ieee802_1x_notify_create_actor_hapd(hapd, sta);
3096#endif /* CONFIG_MACSEC */
3097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003099 if (sta->session_timeout_set) {
3100 os_get_reltime(&now);
3101 os_reltime_sub(&sta->session_timeout, &now, &remaining);
3102 session_timeout = (remaining.sec > 0) ? remaining.sec : 1;
3103 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003104 session_timeout = dot11RSNAConfigPMKLifetime;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003105 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003106 if (success && key && len >= PMK_LEN && !sta->remediation &&
3107 !sta->hs20_deauth_requested &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003108 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 sta->eapol_sm) == 0) {
3110 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
3111 HOSTAPD_LEVEL_DEBUG,
3112 "Added PMKSA cache entry (IEEE 802.1X)");
3113 }
3114
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003115 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 /*
3117 * Many devices require deauthentication after WPS provisioning
3118 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003119 * disconnect the client here. In addition, this may also
3120 * benefit IEEE 802.1X/EAPOL authentication cases, too since
3121 * the EAPOL PAE state machine would remain in HELD state for
3122 * considerable amount of time and some EAP methods, like
3123 * EAP-FAST with anonymous provisioning, may require another
3124 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125 */
Sunil Ravi640215c2023-06-28 23:08:09 +00003126 ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta,
3127 logoff ? 0 : 10);
3128 if (logoff && sta->wpa_sm)
3129 return true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130 }
Sunil Ravi640215c2023-06-28 23:08:09 +00003131
3132 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133}