blob: fccdc72ee7cdca3dff189150eeadeb45efe02c27 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / IEEE 802.1X-2004 Authenticator
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "crypto/md5.h"
14#include "crypto/crypto.h"
15#include "crypto/random.h"
16#include "common/ieee802_11_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "radius/radius.h"
18#include "radius/radius_client.h"
19#include "eap_server/eap.h"
20#include "eap_common/eap_wsc_common.h"
21#include "eapol_auth/eapol_auth_sm.h"
22#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080023#include "p2p/p2p.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "hostapd.h"
25#include "accounting.h"
26#include "sta_info.h"
27#include "wpa_auth.h"
28#include "preauth_auth.h"
29#include "pmksa_cache_auth.h"
30#include "ap_config.h"
31#include "ap_drv_ops.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080032#include "wps_hostapd.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080033#include "hs20.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080034/* FIX: Not really a good thing to require ieee802_11.h here.. (FILS) */
35#include "ieee802_11.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "ieee802_1x.h"
37
38
Dmitry Shmidtde47be72016-01-07 12:52:55 -080039#ifdef CONFIG_HS20
40static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
41#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080043 struct sta_info *sta, int success,
44 int remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045
46
47static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
48 u8 type, const u8 *data, size_t datalen)
49{
50 u8 *buf;
51 struct ieee802_1x_hdr *xhdr;
52 size_t len;
53 int encrypt = 0;
54
55 len = sizeof(*xhdr) + datalen;
56 buf = os_zalloc(len);
57 if (buf == NULL) {
58 wpa_printf(MSG_ERROR, "malloc() failed for "
59 "ieee802_1x_send(len=%lu)",
60 (unsigned long) len);
61 return;
62 }
63
64 xhdr = (struct ieee802_1x_hdr *) buf;
65 xhdr->version = hapd->conf->eapol_version;
66 xhdr->type = type;
67 xhdr->length = host_to_be16(datalen);
68
69 if (datalen > 0 && data != NULL)
70 os_memcpy(xhdr + 1, data, datalen);
71
72 if (wpa_auth_pairwise_set(sta->wpa_sm))
73 encrypt = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080074#ifdef CONFIG_TESTING_OPTIONS
75 if (hapd->ext_eapol_frame_io) {
76 size_t hex_len = 2 * len + 1;
77 char *hex = os_malloc(hex_len);
78
79 if (hex) {
80 wpa_snprintf_hex(hex, hex_len, buf, len);
81 wpa_msg(hapd->msg_ctx, MSG_INFO,
82 "EAPOL-TX " MACSTR " %s",
83 MAC2STR(sta->addr), hex);
84 os_free(hex);
85 }
86 } else
87#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 if (sta->flags & WLAN_STA_PREAUTH) {
89 rsn_preauth_send(hapd, sta, buf, len);
90 } else {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080091 hostapd_drv_hapd_send_eapol(
92 hapd, sta->addr, buf, len,
93 encrypt, hostapd_sta_flags_to_drv(sta->flags));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 }
95
96 os_free(buf);
97}
98
99
100void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
101 struct sta_info *sta, int authorized)
102{
103 int res;
104
105 if (sta->flags & WLAN_STA_PREAUTH)
106 return;
107
108 if (authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 ap_sta_set_authorized(hapd, sta, 1);
110 res = hostapd_set_authorized(hapd, sta, 1);
111 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
112 HOSTAPD_LEVEL_DEBUG, "authorizing port");
113 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114 ap_sta_set_authorized(hapd, sta, 0);
115 res = hostapd_set_authorized(hapd, sta, 0);
116 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
117 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
118 }
119
120 if (res && errno != ENOENT) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700121 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
122 " flags for kernel driver (errno=%d).",
123 MAC2STR(sta->addr), errno);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124 }
125
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800126 if (authorized) {
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800127 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130}
131
132
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133#ifndef CONFIG_FIPS
134#ifndef CONFIG_NO_RC4
135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
137 struct sta_info *sta,
138 int idx, int broadcast,
139 u8 *key_data, size_t key_len)
140{
141 u8 *buf, *ekey;
142 struct ieee802_1x_hdr *hdr;
143 struct ieee802_1x_eapol_key *key;
144 size_t len, ekey_len;
145 struct eapol_state_machine *sm = sta->eapol_sm;
146
147 if (sm == NULL)
148 return;
149
150 len = sizeof(*key) + key_len;
151 buf = os_zalloc(sizeof(*hdr) + len);
152 if (buf == NULL)
153 return;
154
155 hdr = (struct ieee802_1x_hdr *) buf;
156 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
157 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700158 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 wpa_get_ntp_timestamp(key->replay_counter);
160
161 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
162 wpa_printf(MSG_ERROR, "Could not get random numbers");
163 os_free(buf);
164 return;
165 }
166
167 key->key_index = idx | (broadcast ? 0 : BIT(7));
168 if (hapd->conf->eapol_key_index_workaround) {
169 /* According to some information, WinXP Supplicant seems to
170 * interpret bit7 as an indication whether the key is to be
171 * activated, so make it possible to enable workaround that
172 * sets this bit for all keys. */
173 key->key_index |= BIT(7);
174 }
175
176 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
177 * MSK[32..63] is used to sign the message. */
178 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
179 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
180 "and signing EAPOL-Key");
181 os_free(buf);
182 return;
183 }
184 os_memcpy((u8 *) (key + 1), key_data, key_len);
185 ekey_len = sizeof(key->key_iv) + 32;
186 ekey = os_malloc(ekey_len);
187 if (ekey == NULL) {
188 wpa_printf(MSG_ERROR, "Could not encrypt key");
189 os_free(buf);
190 return;
191 }
192 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
193 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
194 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
195 os_free(ekey);
196
197 /* This header is needed here for HMAC-MD5, but it will be regenerated
198 * in ieee802_1x_send() */
199 hdr->version = hapd->conf->eapol_version;
200 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
201 hdr->length = host_to_be16(len);
202 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
203 key->key_signature);
204
205 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
206 " (%s index=%d)", MAC2STR(sm->addr),
207 broadcast ? "broadcast" : "unicast", idx);
208 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
209 if (sta->eapol_sm)
210 sta->eapol_sm->dot1xAuthEapolFramesTx++;
211 os_free(buf);
212}
213
214
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800215static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216{
217 struct eapol_authenticator *eapol = hapd->eapol_auth;
218 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219
220 if (sm == NULL || !sm->eap_if->eapKeyData)
221 return;
222
223 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
224 MAC2STR(sta->addr));
225
226#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800227 if (sta->vlan_id > 0) {
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700228 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
229 return;
230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 if (eapol->default_wep_key) {
234 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
235 eapol->default_wep_key,
236 hapd->conf->default_wep_key_len);
237 }
238
239 if (hapd->conf->individual_wep_key_len > 0) {
240 u8 *ikey;
241 ikey = os_malloc(hapd->conf->individual_wep_key_len);
242 if (ikey == NULL ||
243 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
244 {
245 wpa_printf(MSG_ERROR, "Could not generate random "
246 "individual WEP key.");
247 os_free(ikey);
248 return;
249 }
250
251 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
252 ikey, hapd->conf->individual_wep_key_len);
253
254 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
255 hapd->conf->individual_wep_key_len);
256
257 /* TODO: set encryption in TX callback, i.e., only after STA
258 * has ACKed EAPOL-Key frame */
259 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
260 sta->addr, 0, 1, NULL, 0, ikey,
261 hapd->conf->individual_wep_key_len)) {
262 wpa_printf(MSG_ERROR, "Could not set individual WEP "
263 "encryption.");
264 }
265
266 os_free(ikey);
267 }
268}
269
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800270#endif /* CONFIG_NO_RC4 */
271#endif /* CONFIG_FIPS */
272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273
274const char *radius_mode_txt(struct hostapd_data *hapd)
275{
276 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800277 case HOSTAPD_MODE_IEEE80211AD:
278 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 case HOSTAPD_MODE_IEEE80211A:
280 return "802.11a";
281 case HOSTAPD_MODE_IEEE80211G:
282 return "802.11g";
283 case HOSTAPD_MODE_IEEE80211B:
284 default:
285 return "802.11b";
286 }
287}
288
289
290int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
291{
292 int i;
293 u8 rate = 0;
294
295 for (i = 0; i < sta->supported_rates_len; i++)
296 if ((sta->supported_rates[i] & 0x7f) > rate)
297 rate = sta->supported_rates[i] & 0x7f;
298
299 return rate;
300}
301
302
303#ifndef CONFIG_NO_RADIUS
304static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
305 struct eapol_state_machine *sm,
306 const u8 *eap, size_t len)
307{
308 const u8 *identity;
309 size_t identity_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800310 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311
312 if (len <= sizeof(struct eap_hdr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800313 (hdr->code == EAP_CODE_RESPONSE &&
314 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
315 (hdr->code == EAP_CODE_INITIATE &&
316 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
317 (hdr->code != EAP_CODE_RESPONSE &&
318 hdr->code != EAP_CODE_INITIATE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 return;
320
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800321 eap_erp_update_identity(sm->eap, eap, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 identity = eap_get_identity(sm->eap, &identity_len);
323 if (identity == NULL)
324 return;
325
326 /* Save station identity for future RADIUS packets */
327 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700328 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 if (sm->identity == NULL) {
330 sm->identity_len = 0;
331 return;
332 }
333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
336 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
337 sm->dot1xAuthEapolRespIdFramesRx++;
338}
339
340
Dmitry Shmidt03658832014-08-13 11:03:49 -0700341static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
342 struct hostapd_radius_attr *req_attr,
343 struct sta_info *sta,
344 struct radius_msg *msg)
345{
346 u32 suite;
347 int ver, val;
348
349 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
350 val = wpa_auth_get_pairwise(sta->wpa_sm);
351 suite = wpa_cipher_to_suite(ver, val);
352 if (val != -1 &&
353 !hostapd_config_get_radius_attr(req_attr,
354 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
355 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
356 suite)) {
357 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
358 return -1;
359 }
360
Dmitry Shmidt41712582015-06-29 11:02:15 -0700361 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
362 hapd->conf->osen) ?
Dmitry Shmidt03658832014-08-13 11:03:49 -0700363 WPA_PROTO_RSN : WPA_PROTO_WPA,
364 hapd->conf->wpa_group);
365 if (!hostapd_config_get_radius_attr(req_attr,
366 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
367 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
368 suite)) {
369 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
370 return -1;
371 }
372
373 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
374 suite = wpa_akm_to_suite(val);
375 if (val != -1 &&
376 !hostapd_config_get_radius_attr(req_attr,
377 RADIUS_ATTR_WLAN_AKM_SUITE) &&
378 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
379 suite)) {
380 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
381 return -1;
382 }
383
384#ifdef CONFIG_IEEE80211W
385 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
386 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
387 hapd->conf->group_mgmt_cipher);
388 if (!hostapd_config_get_radius_attr(
389 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
390 !radius_msg_add_attr_int32(
391 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
392 wpa_printf(MSG_ERROR,
393 "Could not add WLAN-Group-Mgmt-Cipher");
394 return -1;
395 }
396 }
397#endif /* CONFIG_IEEE80211W */
398
399 return 0;
400}
401
402
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700403static int add_common_radius_sta_attr(struct hostapd_data *hapd,
404 struct hostapd_radius_attr *req_attr,
405 struct sta_info *sta,
406 struct radius_msg *msg)
407{
408 char buf[128];
409
410 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800411 RADIUS_ATTR_SERVICE_TYPE) &&
412 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
413 RADIUS_SERVICE_TYPE_FRAMED)) {
414 wpa_printf(MSG_ERROR, "Could not add Service-Type");
415 return -1;
416 }
417
418 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700419 RADIUS_ATTR_NAS_PORT) &&
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700420 sta->aid > 0 &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700421 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
422 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
423 return -1;
424 }
425
426 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
427 MAC2STR(sta->addr));
428 buf[sizeof(buf) - 1] = '\0';
429 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
430 (u8 *) buf, os_strlen(buf))) {
431 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
432 return -1;
433 }
434
435 if (sta->flags & WLAN_STA_PREAUTH) {
436 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
437 sizeof(buf));
438 } else {
439 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
440 radius_sta_rate(hapd, sta) / 2,
441 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
442 radius_mode_txt(hapd));
443 buf[sizeof(buf) - 1] = '\0';
444 }
445 if (!hostapd_config_get_radius_attr(req_attr,
446 RADIUS_ATTR_CONNECT_INFO) &&
447 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
448 (u8 *) buf, os_strlen(buf))) {
449 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
450 return -1;
451 }
452
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800453 if (sta->acct_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800454 os_snprintf(buf, sizeof(buf), "%016llX",
455 (unsigned long long) sta->acct_session_id);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800456 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
457 (u8 *) buf, os_strlen(buf))) {
458 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
459 return -1;
460 }
461 }
462
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800463 if ((hapd->conf->wpa & 2) &&
464 !hapd->conf->disable_pmksa_caching &&
465 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800466 os_snprintf(buf, sizeof(buf), "%016llX",
467 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800468 sta->eapol_sm->acct_multi_session_id);
469 if (!radius_msg_add_attr(
470 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
471 (u8 *) buf, os_strlen(buf))) {
472 wpa_printf(MSG_INFO,
473 "Could not add Acct-Multi-Session-Id");
474 return -1;
475 }
476 }
477
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800478#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt03658832014-08-13 11:03:49 -0700479 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
480 sta->wpa_sm &&
481 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
482 sta->auth_alg == WLAN_AUTH_FT) &&
483 !hostapd_config_get_radius_attr(req_attr,
484 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
485 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
486 WPA_GET_BE16(
487 hapd->conf->mobility_domain))) {
488 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
489 return -1;
490 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800491#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt03658832014-08-13 11:03:49 -0700492
Dmitry Shmidt41712582015-06-29 11:02:15 -0700493 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
Dmitry Shmidt03658832014-08-13 11:03:49 -0700494 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
495 return -1;
496
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700497 return 0;
498}
499
500
501int add_common_radius_attr(struct hostapd_data *hapd,
502 struct hostapd_radius_attr *req_attr,
503 struct sta_info *sta,
504 struct radius_msg *msg)
505{
506 char buf[128];
507 struct hostapd_radius_attr *attr;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800508 int len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700509
510 if (!hostapd_config_get_radius_attr(req_attr,
511 RADIUS_ATTR_NAS_IP_ADDRESS) &&
512 hapd->conf->own_ip_addr.af == AF_INET &&
513 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
514 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
515 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
516 return -1;
517 }
518
519#ifdef CONFIG_IPV6
520 if (!hostapd_config_get_radius_attr(req_attr,
521 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
522 hapd->conf->own_ip_addr.af == AF_INET6 &&
523 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
524 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
525 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
526 return -1;
527 }
528#endif /* CONFIG_IPV6 */
529
530 if (!hostapd_config_get_radius_attr(req_attr,
531 RADIUS_ATTR_NAS_IDENTIFIER) &&
532 hapd->conf->nas_identifier &&
533 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
534 (u8 *) hapd->conf->nas_identifier,
535 os_strlen(hapd->conf->nas_identifier))) {
536 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
537 return -1;
538 }
539
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800540 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
541 MAC2STR(hapd->own_addr));
542 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
543 hapd->conf->ssid.ssid_len);
544 len += hapd->conf->ssid.ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700545 if (!hostapd_config_get_radius_attr(req_attr,
546 RADIUS_ATTR_CALLED_STATION_ID) &&
547 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800548 (u8 *) buf, len)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700549 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
550 return -1;
551 }
552
553 if (!hostapd_config_get_radius_attr(req_attr,
554 RADIUS_ATTR_NAS_PORT_TYPE) &&
555 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
556 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
557 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
558 return -1;
559 }
560
Dmitry Shmidt03658832014-08-13 11:03:49 -0700561#ifdef CONFIG_INTERWORKING
562 if (hapd->conf->interworking &&
563 !is_zero_ether_addr(hapd->conf->hessid)) {
564 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
565 MAC2STR(hapd->conf->hessid));
566 buf[sizeof(buf) - 1] = '\0';
567 if (!hostapd_config_get_radius_attr(req_attr,
568 RADIUS_ATTR_WLAN_HESSID) &&
569 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
570 (u8 *) buf, os_strlen(buf))) {
571 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
572 return -1;
573 }
574 }
575#endif /* CONFIG_INTERWORKING */
576
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700577 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
578 return -1;
579
580 for (attr = req_attr; attr; attr = attr->next) {
581 if (!radius_msg_add_attr(msg, attr->type,
582 wpabuf_head(attr->val),
583 wpabuf_len(attr->val))) {
584 wpa_printf(MSG_ERROR, "Could not add RADIUS "
585 "attribute");
586 return -1;
587 }
588 }
589
590 return 0;
591}
592
593
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800594void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
595 struct sta_info *sta,
596 const u8 *eap, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597{
598 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 struct eapol_state_machine *sm = sta->eapol_sm;
600
601 if (sm == NULL)
602 return;
603
604 ieee802_1x_learn_identity(hapd, sm, eap, len);
605
606 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
607 "packet");
608
609 sm->radius_identifier = radius_client_get_id(hapd->radius);
610 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
611 sm->radius_identifier);
612 if (msg == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800613 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 return;
615 }
616
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800617 if (radius_msg_make_authenticator(msg) < 0) {
618 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
619 goto fail;
620 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621
622 if (sm->identity &&
623 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
624 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800625 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 goto fail;
627 }
628
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700629 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
630 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632
633 /* TODO: should probably check MTU from driver config; 2304 is max for
634 * IEEE 802.11, but use 1400 to avoid problems with too large packets
635 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700636 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
637 RADIUS_ATTR_FRAMED_MTU) &&
638 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800639 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 goto fail;
641 }
642
Dmitry Shmidt41712582015-06-29 11:02:15 -0700643 if (!radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800644 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 goto fail;
646 }
647
648 /* State attribute must be copied if and only if this packet is
649 * Access-Request reply to the previous Access-Challenge */
650 if (sm->last_recv_radius &&
651 radius_msg_get_hdr(sm->last_recv_radius)->code ==
652 RADIUS_CODE_ACCESS_CHALLENGE) {
653 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
654 RADIUS_ATTR_STATE);
655 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800656 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 goto fail;
658 }
659 if (res > 0) {
660 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
661 }
662 }
663
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664 if (hapd->conf->radius_request_cui) {
665 const u8 *cui;
666 size_t cui_len;
667 /* Add previously learned CUI or nul CUI to request CUI */
668 if (sm->radius_cui) {
669 cui = wpabuf_head(sm->radius_cui);
670 cui_len = wpabuf_len(sm->radius_cui);
671 } else {
672 cui = (const u8 *) "\0";
673 cui_len = 1;
674 }
675 if (!radius_msg_add_attr(msg,
676 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
677 cui, cui_len)) {
678 wpa_printf(MSG_ERROR, "Could not add CUI");
679 goto fail;
680 }
681 }
682
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800683#ifdef CONFIG_HS20
684 if (hapd->conf->hs20) {
685 u8 ver = 1; /* Release 2 */
686 if (!radius_msg_add_wfa(
687 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
688 &ver, 1)) {
689 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
690 "version");
691 goto fail;
692 }
693
694 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
695 const u8 *pos;
696 u8 buf[3];
697 u16 id;
698 pos = wpabuf_head_u8(sta->hs20_ie);
699 buf[0] = (*pos) >> 4;
700 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
701 wpabuf_len(sta->hs20_ie) >= 3)
702 id = WPA_GET_LE16(pos + 1);
703 else
704 id = 0;
705 WPA_PUT_BE16(buf + 1, id);
706 if (!radius_msg_add_wfa(
707 msg,
708 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
709 buf, sizeof(buf))) {
710 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
711 "STA version");
712 goto fail;
713 }
714 }
715 }
716#endif /* CONFIG_HS20 */
717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
719 goto fail;
720
721 return;
722
723 fail:
724 radius_msg_free(msg);
725}
726#endif /* CONFIG_NO_RADIUS */
727
728
729static void handle_eap_response(struct hostapd_data *hapd,
730 struct sta_info *sta, struct eap_hdr *eap,
731 size_t len)
732{
733 u8 type, *data;
734 struct eapol_state_machine *sm = sta->eapol_sm;
735 if (sm == NULL)
736 return;
737
738 data = (u8 *) (eap + 1);
739
740 if (len < sizeof(*eap) + 1) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800741 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 return;
743 }
744
745 sm->eap_type_supp = type = data[0];
746
747 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
748 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
749 "id=%d len=%d) from STA: EAP Response-%s (%d)",
750 eap->code, eap->identifier, be_to_host16(eap->length),
751 eap_server_get_name(0, type), type);
752
753 sm->dot1xAuthEapolRespFramesRx++;
754
755 wpabuf_free(sm->eap_if->eapRespData);
756 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
757 sm->eapolEap = TRUE;
758}
759
760
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800761static void handle_eap_initiate(struct hostapd_data *hapd,
762 struct sta_info *sta, struct eap_hdr *eap,
763 size_t len)
764{
765#ifdef CONFIG_ERP
766 u8 type, *data;
767 struct eapol_state_machine *sm = sta->eapol_sm;
768
769 if (sm == NULL)
770 return;
771
772 if (len < sizeof(*eap) + 1) {
773 wpa_printf(MSG_INFO,
774 "handle_eap_initiate: too short response data");
775 return;
776 }
777
778 data = (u8 *) (eap + 1);
779 type = data[0];
780
781 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
782 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
783 "id=%d len=%d) from STA: EAP Initiate type %u",
784 eap->code, eap->identifier, be_to_host16(eap->length),
785 type);
786
787 wpabuf_free(sm->eap_if->eapRespData);
788 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
789 sm->eapolEap = TRUE;
790#endif /* CONFIG_ERP */
791}
792
793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794/* Process incoming EAP packet from Supplicant */
795static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
796 u8 *buf, size_t len)
797{
798 struct eap_hdr *eap;
799 u16 eap_len;
800
801 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800802 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 return;
804 }
805
806 eap = (struct eap_hdr *) buf;
807
808 eap_len = be_to_host16(eap->length);
809 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
810 eap->code, eap->identifier, eap_len);
811 if (eap_len < sizeof(*eap)) {
812 wpa_printf(MSG_DEBUG, " Invalid EAP length");
813 return;
814 } else if (eap_len > len) {
815 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
816 "packet");
817 return;
818 } else if (eap_len < len) {
819 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
820 "packet", (unsigned long) len - eap_len);
821 }
822
823 switch (eap->code) {
824 case EAP_CODE_REQUEST:
825 wpa_printf(MSG_DEBUG, " (request)");
826 return;
827 case EAP_CODE_RESPONSE:
828 wpa_printf(MSG_DEBUG, " (response)");
829 handle_eap_response(hapd, sta, eap, eap_len);
830 break;
831 case EAP_CODE_SUCCESS:
832 wpa_printf(MSG_DEBUG, " (success)");
833 return;
834 case EAP_CODE_FAILURE:
835 wpa_printf(MSG_DEBUG, " (failure)");
836 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800837 case EAP_CODE_INITIATE:
838 wpa_printf(MSG_DEBUG, " (initiate)");
839 handle_eap_initiate(hapd, sta, eap, eap_len);
840 break;
841 case EAP_CODE_FINISH:
842 wpa_printf(MSG_DEBUG, " (finish)");
843 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 default:
845 wpa_printf(MSG_DEBUG, " (unknown code)");
846 return;
847 }
848}
849
850
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800851struct eapol_state_machine *
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
853{
854 int flags = 0;
855 if (sta->flags & WLAN_STA_PREAUTH)
856 flags |= EAPOL_SM_PREAUTH;
857 if (sta->wpa_sm) {
858 flags |= EAPOL_SM_USES_WPA;
859 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
860 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
861 }
862 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700863 sta->wps_ie, sta->p2p_ie, sta,
864 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865}
866
867
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800868static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
869 size_t len)
870{
871 if (sta->pending_eapol_rx) {
872 wpabuf_free(sta->pending_eapol_rx->buf);
873 } else {
874 sta->pending_eapol_rx =
875 os_malloc(sizeof(*sta->pending_eapol_rx));
876 if (!sta->pending_eapol_rx)
877 return;
878 }
879
880 sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
881 if (!sta->pending_eapol_rx->buf) {
882 os_free(sta->pending_eapol_rx);
883 sta->pending_eapol_rx = NULL;
884 return;
885 }
886
887 os_get_reltime(&sta->pending_eapol_rx->rx_time);
888}
889
890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891/**
892 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
893 * @hapd: hostapd BSS data
894 * @sa: Source address (sender of the EAPOL frame)
895 * @buf: EAPOL frame
896 * @len: Length of buf in octets
897 *
898 * This function is called for each incoming EAPOL frame from the interface
899 */
900void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
901 size_t len)
902{
903 struct sta_info *sta;
904 struct ieee802_1x_hdr *hdr;
905 struct ieee802_1x_eapol_key *key;
906 u16 datalen;
907 struct rsn_pmksa_cache_entry *pmksa;
908 int key_mgmt;
909
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800910 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 !hapd->conf->wps_state)
912 return;
913
914 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
915 (unsigned long) len, MAC2STR(sa));
916 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
918 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
920 "associated/Pre-authenticating STA");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800921
922 if (sta && (sta->flags & WLAN_STA_AUTH)) {
923 wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
924 " for later use", MAC2STR(sta->addr));
925 ieee802_1x_save_eapol(sta, buf, len);
926 }
927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 return;
929 }
930
931 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800932 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933 return;
934 }
935
936 hdr = (struct ieee802_1x_hdr *) buf;
937 datalen = be_to_host16(hdr->length);
938 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
939 hdr->version, hdr->type, datalen);
940
941 if (len - sizeof(*hdr) < datalen) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800942 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 if (sta->eapol_sm)
944 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
945 return;
946 }
947 if (len - sizeof(*hdr) > datalen) {
948 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
949 "IEEE 802.1X packet",
950 (unsigned long) len - sizeof(*hdr) - datalen);
951 }
952
953 if (sta->eapol_sm) {
954 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
955 sta->eapol_sm->dot1xAuthEapolFramesRx++;
956 }
957
958 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
959 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
960 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
961 (key->type == EAPOL_KEY_TYPE_WPA ||
962 key->type == EAPOL_KEY_TYPE_RSN)) {
963 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
964 sizeof(*hdr) + datalen);
965 return;
966 }
967
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800968 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
970 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
971 "802.1X not enabled and WPS not used");
972 return;
973 }
974
975 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
976 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
977 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
978 "STA is using PSK");
979 return;
980 }
981
982 if (!sta->eapol_sm) {
983 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
984 if (!sta->eapol_sm)
985 return;
986
987#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800988 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 u32 wflags = sta->flags & (WLAN_STA_WPS |
990 WLAN_STA_WPS2 |
991 WLAN_STA_MAYBE_WPS);
992 if (wflags == WLAN_STA_MAYBE_WPS ||
993 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
994 /*
995 * Delay EAPOL frame transmission until a
996 * possible WPS STA initiates the handshake
997 * with EAPOL-Start. Only allow the wait to be
998 * skipped if the STA is known to support WPS
999 * 2.0.
1000 */
1001 wpa_printf(MSG_DEBUG, "WPS: Do not start "
1002 "EAPOL until EAPOL-Start is "
1003 "received");
1004 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1005 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 }
1007#endif /* CONFIG_WPS */
1008
1009 sta->eapol_sm->eap_if->portEnabled = TRUE;
1010 }
1011
1012 /* since we support version 1, we can ignore version field and proceed
1013 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1014 /* TODO: actually, we are not version 1 anymore.. However, Version 2
1015 * does not change frame contents, so should be ok to process frames
1016 * more or less identically. Some changes might be needed for
1017 * verification of fields. */
1018
1019 switch (hdr->type) {
1020 case IEEE802_1X_TYPE_EAP_PACKET:
1021 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1022 break;
1023
1024 case IEEE802_1X_TYPE_EAPOL_START:
1025 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1026 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
1027 "from STA");
1028 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1029 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1030 if (pmksa) {
1031 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1032 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
1033 "available - ignore it since "
1034 "STA sent EAPOL-Start");
1035 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1036 }
1037 sta->eapol_sm->eapolStart = TRUE;
1038 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1039 eap_server_clear_identity(sta->eapol_sm->eap);
1040 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1041 break;
1042
1043 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1044 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1045 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
1046 "from STA");
1047 sta->acct_terminate_cause =
1048 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1049 accounting_sta_stop(hapd, sta);
1050 sta->eapol_sm->eapolLogoff = TRUE;
1051 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1052 eap_server_clear_identity(sta->eapol_sm->eap);
1053 break;
1054
1055 case IEEE802_1X_TYPE_EAPOL_KEY:
1056 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1057 if (!ap_sta_is_authorized(sta)) {
1058 wpa_printf(MSG_DEBUG, " Dropped key data from "
1059 "unauthorized Supplicant");
1060 break;
1061 }
1062 break;
1063
1064 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1065 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1066 /* TODO: implement support for this; show data */
1067 break;
1068
1069 default:
1070 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1071 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1072 break;
1073 }
1074
1075 eapol_auth_step(sta->eapol_sm);
1076}
1077
1078
1079/**
1080 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1081 * @hapd: hostapd BSS data
1082 * @sta: The station
1083 *
1084 * This function is called to start IEEE 802.1X authentication when a new
1085 * station completes IEEE 802.11 association.
1086 */
1087void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1088{
1089 struct rsn_pmksa_cache_entry *pmksa;
1090 int reassoc = 1;
1091 int force_1x = 0;
1092 int key_mgmt;
1093
1094#ifdef CONFIG_WPS
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001095 if (hapd->conf->wps_state &&
1096 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1097 (sta->flags & WLAN_STA_WPS))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 /*
1099 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1100 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1101 * authentication in this BSS.
1102 */
1103 force_1x = 1;
1104 }
1105#endif /* CONFIG_WPS */
1106
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001107 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1109 "802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001110 /*
1111 * Clear any possible EAPOL authenticator state to support
1112 * reassociation change from WPS to PSK.
1113 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001114 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 return;
1116 }
1117
1118 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1119 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1120 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001121 /*
1122 * Clear any possible EAPOL authenticator state to support
1123 * reassociation change from WPA-EAP to PSK.
1124 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001125 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 return;
1127 }
1128
1129 if (sta->eapol_sm == NULL) {
1130 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1131 HOSTAPD_LEVEL_DEBUG, "start authentication");
1132 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1133 if (sta->eapol_sm == NULL) {
1134 hostapd_logger(hapd, sta->addr,
1135 HOSTAPD_MODULE_IEEE8021X,
1136 HOSTAPD_LEVEL_INFO,
1137 "failed to allocate state machine");
1138 return;
1139 }
1140 reassoc = 0;
1141 }
1142
1143#ifdef CONFIG_WPS
1144 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001145 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1146 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001148 * Delay EAPOL frame transmission until a possible WPS STA
1149 * initiates the handshake with EAPOL-Start. Only allow the
1150 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001152 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1153 "EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1155 }
1156#endif /* CONFIG_WPS */
1157
1158 sta->eapol_sm->eap_if->portEnabled = TRUE;
1159
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001160#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 if (sta->auth_alg == WLAN_AUTH_FT) {
1162 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1163 HOSTAPD_LEVEL_DEBUG,
1164 "PMK from FT - skip IEEE 802.1X/EAP");
1165 /* Setup EAPOL state machines to already authenticated state
1166 * because of existing FT information from R0KH. */
1167 sta->eapol_sm->keyRun = TRUE;
1168 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1169 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1170 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1171 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001172 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001173 sta->eapol_sm->portValid = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 if (sta->eapol_sm->eap)
1175 eap_sm_notify_cached(sta->eapol_sm->eap);
1176 /* TODO: get vlan_id from R0KH using RRB message */
1177 return;
1178 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001179#endif /* CONFIG_IEEE80211R_AP */
1180
1181#ifdef CONFIG_FILS
1182 if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1183 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1184 sta->auth_alg == WLAN_AUTH_FILS_PK) {
1185 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1186 HOSTAPD_LEVEL_DEBUG,
1187 "PMK from FILS - skip IEEE 802.1X/EAP");
1188 /* Setup EAPOL state machines to already authenticated state
1189 * because of existing FILS information. */
1190 sta->eapol_sm->keyRun = TRUE;
1191 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1192 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1193 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1194 sta->eapol_sm->authSuccess = TRUE;
1195 sta->eapol_sm->authFail = FALSE;
1196 sta->eapol_sm->portValid = TRUE;
1197 if (sta->eapol_sm->eap)
1198 eap_sm_notify_cached(sta->eapol_sm->eap);
1199 return;
1200 }
1201#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202
1203 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1204 if (pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1206 HOSTAPD_LEVEL_DEBUG,
1207 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1208 /* Setup EAPOL state machines to already authenticated state
1209 * because of existing PMKSA information in the cache. */
1210 sta->eapol_sm->keyRun = TRUE;
1211 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1212 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1213 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1214 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001215 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 if (sta->eapol_sm->eap)
1217 eap_sm_notify_cached(sta->eapol_sm->eap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001218 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001219 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 } else {
1221 if (reassoc) {
1222 /*
1223 * Force EAPOL state machines to start
1224 * re-authentication without having to wait for the
1225 * Supplicant to send EAPOL-Start.
1226 */
1227 sta->eapol_sm->reAuthenticate = TRUE;
1228 }
1229 eapol_auth_step(sta->eapol_sm);
1230 }
1231}
1232
1233
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001234void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235{
1236 struct eapol_state_machine *sm = sta->eapol_sm;
1237
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001238#ifdef CONFIG_HS20
1239 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1240#endif /* CONFIG_HS20 */
1241
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001242 if (sta->pending_eapol_rx) {
1243 wpabuf_free(sta->pending_eapol_rx->buf);
1244 os_free(sta->pending_eapol_rx);
1245 sta->pending_eapol_rx = NULL;
1246 }
1247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 if (sm == NULL)
1249 return;
1250
1251 sta->eapol_sm = NULL;
1252
1253#ifndef CONFIG_NO_RADIUS
1254 radius_msg_free(sm->last_recv_radius);
1255 radius_free_class(&sm->radius_class);
1256#endif /* CONFIG_NO_RADIUS */
1257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 eapol_auth_free(sm);
1259}
1260
1261
1262#ifndef CONFIG_NO_RADIUS
1263static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1264 struct sta_info *sta)
1265{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001266 struct wpabuf *eap;
1267 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 int eap_type = -1;
1269 char buf[64];
1270 struct radius_msg *msg;
1271 struct eapol_state_machine *sm = sta->eapol_sm;
1272
1273 if (sm == NULL || sm->last_recv_radius == NULL) {
1274 if (sm)
1275 sm->eap_if->aaaEapNoReq = TRUE;
1276 return;
1277 }
1278
1279 msg = sm->last_recv_radius;
1280
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001281 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 if (eap == NULL) {
1283 /* RFC 3579, Chap. 2.6.3:
1284 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1285 * attribute */
1286 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1287 HOSTAPD_LEVEL_WARNING, "could not extract "
1288 "EAP-Message from RADIUS message");
1289 sm->eap_if->aaaEapNoReq = TRUE;
1290 return;
1291 }
1292
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001293 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1295 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1296 "received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001297 wpabuf_free(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 sm->eap_if->aaaEapNoReq = TRUE;
1299 return;
1300 }
1301
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001302 if (wpabuf_len(eap) > sizeof(*hdr))
1303 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001305 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 switch (hdr->code) {
1307 case EAP_CODE_REQUEST:
1308 if (eap_type >= 0)
1309 sm->eap_type_authsrv = eap_type;
1310 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001311 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 break;
1313 case EAP_CODE_RESPONSE:
1314 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001315 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 break;
1317 case EAP_CODE_SUCCESS:
1318 os_strlcpy(buf, "EAP Success", sizeof(buf));
1319 break;
1320 case EAP_CODE_FAILURE:
1321 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1322 break;
1323 default:
1324 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1325 break;
1326 }
1327 buf[sizeof(buf) - 1] = '\0';
1328 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1329 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1330 "id=%d len=%d) from RADIUS server: %s",
1331 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1332 buf);
1333 sm->eap_if->aaaEapReq = TRUE;
1334
1335 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337}
1338
1339
1340static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1341 struct sta_info *sta, struct radius_msg *msg,
1342 struct radius_msg *req,
1343 const u8 *shared_secret,
1344 size_t shared_secret_len)
1345{
1346 struct radius_ms_mppe_keys *keys;
1347 struct eapol_state_machine *sm = sta->eapol_sm;
1348 if (sm == NULL)
1349 return;
1350
1351 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1352 shared_secret_len);
1353
1354 if (keys && keys->send && keys->recv) {
1355 size_t len = keys->send_len + keys->recv_len;
1356 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1357 keys->send, keys->send_len);
1358 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1359 keys->recv, keys->recv_len);
1360
1361 os_free(sm->eap_if->aaaEapKeyData);
1362 sm->eap_if->aaaEapKeyData = os_malloc(len);
1363 if (sm->eap_if->aaaEapKeyData) {
1364 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1365 keys->recv_len);
1366 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1367 keys->send, keys->send_len);
1368 sm->eap_if->aaaEapKeyDataLen = len;
1369 sm->eap_if->aaaEapKeyAvailable = TRUE;
1370 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001371 } else {
1372 wpa_printf(MSG_DEBUG,
1373 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1374 keys, keys ? keys->send : NULL,
1375 keys ? keys->recv : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376 }
1377
1378 if (keys) {
1379 os_free(keys->send);
1380 os_free(keys->recv);
1381 os_free(keys);
1382 }
1383}
1384
1385
1386static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1387 struct sta_info *sta,
1388 struct radius_msg *msg)
1389{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001390 u8 *attr_class;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 size_t class_len;
1392 struct eapol_state_machine *sm = sta->eapol_sm;
1393 int count, i;
1394 struct radius_attr_data *nclass;
1395 size_t nclass_count;
1396
1397 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1398 sm == NULL)
1399 return;
1400
1401 radius_free_class(&sm->radius_class);
1402 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1403 if (count <= 0)
1404 return;
1405
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001406 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 if (nclass == NULL)
1408 return;
1409
1410 nclass_count = 0;
1411
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001412 attr_class = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 for (i = 0; i < count; i++) {
1414 do {
1415 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001416 &attr_class, &class_len,
1417 attr_class) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 i = count;
1419 break;
1420 }
1421 } while (class_len < 1);
1422
1423 nclass[nclass_count].data = os_malloc(class_len);
1424 if (nclass[nclass_count].data == NULL)
1425 break;
1426
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001427 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 nclass[nclass_count].len = class_len;
1429 nclass_count++;
1430 }
1431
1432 sm->radius_class.attr = nclass;
1433 sm->radius_class.count = nclass_count;
1434 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1435 "attributes for " MACSTR,
1436 (unsigned long) sm->radius_class.count,
1437 MAC2STR(sta->addr));
1438}
1439
1440
1441/* Update sta->identity based on User-Name attribute in Access-Accept */
1442static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1443 struct sta_info *sta,
1444 struct radius_msg *msg)
1445{
1446 u8 *buf, *identity;
1447 size_t len;
1448 struct eapol_state_machine *sm = sta->eapol_sm;
1449
1450 if (sm == NULL)
1451 return;
1452
1453 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1454 NULL) < 0)
1455 return;
1456
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001457 identity = (u8 *) dup_binstr(buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 if (identity == NULL)
1459 return;
1460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1462 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1463 "User-Name from Access-Accept '%s'",
1464 sm->identity ? (char *) sm->identity : "N/A",
1465 (char *) identity);
1466
1467 os_free(sm->identity);
1468 sm->identity = identity;
1469 sm->identity_len = len;
1470}
1471
1472
Dmitry Shmidt04949592012-07-19 12:16:46 -07001473/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1474static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1475 struct sta_info *sta,
1476 struct radius_msg *msg)
1477{
1478 struct eapol_state_machine *sm = sta->eapol_sm;
1479 struct wpabuf *cui;
1480 u8 *buf;
1481 size_t len;
1482
1483 if (sm == NULL)
1484 return;
1485
1486 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1487 &buf, &len, NULL) < 0)
1488 return;
1489
1490 cui = wpabuf_alloc_copy(buf, len);
1491 if (cui == NULL)
1492 return;
1493
1494 wpabuf_free(sm->radius_cui);
1495 sm->radius_cui = cui;
1496}
1497
1498
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001499#ifdef CONFIG_HS20
1500
1501static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1502{
1503 sta->remediation = 1;
1504 os_free(sta->remediation_url);
1505 if (len > 2) {
1506 sta->remediation_url = os_malloc(len);
1507 if (!sta->remediation_url)
1508 return;
1509 sta->remediation_method = pos[0];
1510 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1511 sta->remediation_url[len - 1] = '\0';
1512 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1513 "for " MACSTR " - server method %u URL %s",
1514 MAC2STR(sta->addr), sta->remediation_method,
1515 sta->remediation_url);
1516 } else {
1517 sta->remediation_url = NULL;
1518 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1519 "for " MACSTR, MAC2STR(sta->addr));
1520 }
1521 /* TODO: assign the STA into remediation VLAN or add filtering */
1522}
1523
1524
1525static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1526 struct sta_info *sta, u8 *pos,
1527 size_t len)
1528{
1529 if (len < 3)
1530 return; /* Malformed information */
1531 sta->hs20_deauth_requested = 1;
1532 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1533 "Re-auth Delay %u",
1534 *pos, WPA_GET_LE16(pos + 1));
1535 wpabuf_free(sta->hs20_deauth_req);
1536 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1537 if (sta->hs20_deauth_req) {
1538 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1539 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1540 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1541 }
1542 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1543}
1544
1545
1546static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1547 struct sta_info *sta, u8 *pos,
1548 size_t len, int session_timeout)
1549{
1550 unsigned int swt;
1551 int warning_time, beacon_int;
1552
1553 if (len < 1)
1554 return; /* Malformed information */
1555 os_free(sta->hs20_session_info_url);
1556 sta->hs20_session_info_url = os_malloc(len);
1557 if (sta->hs20_session_info_url == NULL)
1558 return;
1559 swt = pos[0];
1560 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1561 sta->hs20_session_info_url[len - 1] = '\0';
1562 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1563 "(session_timeout=%d)",
1564 sta->hs20_session_info_url, swt, session_timeout);
1565 if (session_timeout < 0) {
1566 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1567 return;
1568 }
1569 if (swt == 255)
1570 swt = 1; /* Use one minute as the AP selected value */
1571
1572 if ((unsigned int) session_timeout < swt * 60)
1573 warning_time = 0;
1574 else
1575 warning_time = session_timeout - swt * 60;
1576
1577 beacon_int = hapd->iconf->beacon_int;
1578 if (beacon_int < 1)
1579 beacon_int = 100; /* best guess */
1580 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1581 if (sta->hs20_disassoc_timer > 65535)
1582 sta->hs20_disassoc_timer = 65535;
1583
1584 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1585}
1586
1587#endif /* CONFIG_HS20 */
1588
1589
1590static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1591 struct sta_info *sta,
1592 struct radius_msg *msg,
1593 int session_timeout)
1594{
1595#ifdef CONFIG_HS20
1596 u8 *buf, *pos, *end, type, sublen;
1597 size_t len;
1598
1599 buf = NULL;
1600 sta->remediation = 0;
1601 sta->hs20_deauth_requested = 0;
1602
1603 for (;;) {
1604 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1605 &buf, &len, buf) < 0)
1606 break;
1607 if (len < 6)
1608 continue;
1609 pos = buf;
1610 end = buf + len;
1611 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1612 continue;
1613 pos += 4;
1614
1615 type = *pos++;
1616 sublen = *pos++;
1617 if (sublen < 2)
1618 continue; /* invalid length */
1619 sublen -= 2; /* skip header */
1620 if (pos + sublen > end)
1621 continue; /* invalid WFA VSA */
1622
1623 switch (type) {
1624 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1625 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1626 break;
1627 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1628 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1629 break;
1630 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1631 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1632 session_timeout);
1633 break;
1634 }
1635 }
1636#endif /* CONFIG_HS20 */
1637}
1638
1639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640struct sta_id_search {
1641 u8 identifier;
1642 struct eapol_state_machine *sm;
1643};
1644
1645
1646static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1647 struct sta_info *sta,
1648 void *ctx)
1649{
1650 struct sta_id_search *id_search = ctx;
1651 struct eapol_state_machine *sm = sta->eapol_sm;
1652
1653 if (sm && sm->radius_identifier >= 0 &&
1654 sm->radius_identifier == id_search->identifier) {
1655 id_search->sm = sm;
1656 return 1;
1657 }
1658 return 0;
1659}
1660
1661
1662static struct eapol_state_machine *
1663ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1664{
1665 struct sta_id_search id_search;
1666 id_search.identifier = identifier;
1667 id_search.sm = NULL;
1668 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1669 return id_search.sm;
1670}
1671
1672
1673/**
1674 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1675 * @msg: RADIUS response message
1676 * @req: RADIUS request message
1677 * @shared_secret: RADIUS shared secret
1678 * @shared_secret_len: Length of shared_secret in octets
1679 * @data: Context data (struct hostapd_data *)
1680 * Returns: Processing status
1681 */
1682static RadiusRxResult
1683ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1684 const u8 *shared_secret, size_t shared_secret_len,
1685 void *data)
1686{
1687 struct hostapd_data *hapd = data;
1688 struct sta_info *sta;
1689 u32 session_timeout = 0, termination_action, acct_interim_interval;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001690 int session_timeout_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 struct eapol_state_machine *sm;
1692 int override_eapReq = 0;
1693 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001694 struct vlan_description vlan_desc;
1695#ifndef CONFIG_NO_VLAN
1696 int *untagged, *tagged, *notempty;
1697#endif /* CONFIG_NO_VLAN */
1698
1699 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700
1701 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1702 if (sm == NULL) {
1703 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1704 "station for this RADIUS message");
1705 return RADIUS_RX_UNKNOWN;
1706 }
1707 sta = sm->sta;
1708
1709 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1710 * present when packet contains an EAP-Message attribute */
1711 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1712 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1713 0) < 0 &&
1714 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1715 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1716 "Message-Authenticator since it does not include "
1717 "EAP-Message");
1718 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1719 req, 1)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001720 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 return RADIUS_RX_INVALID_AUTHENTICATOR;
1722 }
1723
1724 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1725 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1726 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001727 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 return RADIUS_RX_UNKNOWN;
1729 }
1730
1731 sm->radius_identifier = -1;
1732 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1733 MAC2STR(sta->addr));
1734
1735 radius_msg_free(sm->last_recv_radius);
1736 sm->last_recv_radius = msg;
1737
1738 session_timeout_set =
1739 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1740 &session_timeout);
1741 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1742 &termination_action))
1743 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1744
1745 if (hapd->conf->acct_interim_interval == 0 &&
1746 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1747 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1748 &acct_interim_interval) == 0) {
1749 if (acct_interim_interval < 60) {
1750 hostapd_logger(hapd, sta->addr,
1751 HOSTAPD_MODULE_IEEE8021X,
1752 HOSTAPD_LEVEL_INFO,
1753 "ignored too small "
1754 "Acct-Interim-Interval %d",
1755 acct_interim_interval);
1756 } else
1757 sta->acct_interim_interval = acct_interim_interval;
1758 }
1759
1760
1761 switch (hdr->code) {
1762 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001764 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED) {
1765 notempty = &vlan_desc.notempty;
1766 untagged = &vlan_desc.untagged;
1767 tagged = vlan_desc.tagged;
1768 *notempty = !!radius_msg_get_vlanid(msg, untagged,
1769 MAX_NUM_TAGGED_VLAN,
1770 tagged);
1771 }
1772
1773 if (vlan_desc.notempty &&
1774 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
Dmitry Shmidt83474442015-04-15 13:47:09 -07001775 sta->eapol_sm->authFail = TRUE;
1776 hostapd_logger(hapd, sta->addr,
1777 HOSTAPD_MODULE_RADIUS,
1778 HOSTAPD_LEVEL_INFO,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001779 "Invalid VLAN %d%s received from RADIUS server",
1780 vlan_desc.untagged,
1781 vlan_desc.tagged[0] ? "+" : "");
1782 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1783 ap_sta_set_vlan(hapd, sta, &vlan_desc);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001784 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001785 }
1786
1787 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1788 !vlan_desc.notempty) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 sta->eapol_sm->authFail = TRUE;
1790 hostapd_logger(hapd, sta->addr,
1791 HOSTAPD_MODULE_IEEE8021X,
1792 HOSTAPD_LEVEL_INFO, "authentication "
1793 "server did not include required VLAN "
1794 "ID in Access-Accept");
1795 break;
1796 }
1797#endif /* CONFIG_NO_VLAN */
1798
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001799 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0)
1800 break;
1801
1802#ifndef CONFIG_NO_VLAN
1803 if (sta->vlan_id > 0) {
1804 hostapd_logger(hapd, sta->addr,
1805 HOSTAPD_MODULE_RADIUS,
1806 HOSTAPD_LEVEL_INFO,
1807 "VLAN ID %d", sta->vlan_id);
1808 }
1809#endif /* CONFIG_NO_VLAN */
1810
Dmitry Shmidt83474442015-04-15 13:47:09 -07001811 if ((sta->flags & WLAN_STA_ASSOC) &&
1812 ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813 break;
1814
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001815 sta->session_timeout_set = !!session_timeout_set;
1816 sta->session_timeout = session_timeout;
1817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 /* RFC 3580, Ch. 3.17 */
1819 if (session_timeout_set && termination_action ==
1820 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1821 sm->reAuthPeriod = session_timeout;
1822 } else if (session_timeout_set)
1823 ap_sta_session_timeout(hapd, sta, session_timeout);
1824
1825 sm->eap_if->aaaSuccess = TRUE;
1826 override_eapReq = 1;
1827 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1828 shared_secret_len);
1829 ieee802_1x_store_radius_class(hapd, sta, msg);
1830 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001831 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001832 ieee802_1x_check_hs20(hapd, sta, msg,
1833 session_timeout_set ?
1834 (int) session_timeout : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 break;
1836 case RADIUS_CODE_ACCESS_REJECT:
1837 sm->eap_if->aaaFail = TRUE;
1838 override_eapReq = 1;
1839 break;
1840 case RADIUS_CODE_ACCESS_CHALLENGE:
1841 sm->eap_if->aaaEapReq = TRUE;
1842 if (session_timeout_set) {
1843 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1844 sm->eap_if->aaaMethodTimeout = session_timeout;
1845 hostapd_logger(hapd, sm->addr,
1846 HOSTAPD_MODULE_IEEE8021X,
1847 HOSTAPD_LEVEL_DEBUG,
1848 "using EAP timeout of %d seconds (from "
1849 "RADIUS)",
1850 sm->eap_if->aaaMethodTimeout);
1851 } else {
1852 /*
1853 * Use dynamic retransmission behavior per EAP
1854 * specification.
1855 */
1856 sm->eap_if->aaaMethodTimeout = 0;
1857 }
1858 break;
1859 }
1860
1861 ieee802_1x_decapsulate_radius(hapd, sta);
1862 if (override_eapReq)
1863 sm->eap_if->aaaEapReq = FALSE;
1864
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001865#ifdef CONFIG_FILS
1866#ifdef NEED_AP_MLME
1867 if (sta->flags & WLAN_STA_PENDING_FILS_ERP) {
1868 /* TODO: Add a PMKSA entry on success? */
1869 ieee802_11_finish_fils_auth(
1870 hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
1871 sm->eap_if->aaaEapReqData,
1872 sm->eap_if->aaaEapKeyData,
1873 sm->eap_if->aaaEapKeyDataLen);
1874 }
1875#endif /* NEED_AP_MLME */
1876#endif /* CONFIG_FILS */
1877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878 eapol_auth_step(sm);
1879
1880 return RADIUS_RX_QUEUED;
1881}
1882#endif /* CONFIG_NO_RADIUS */
1883
1884
1885void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1886{
1887 struct eapol_state_machine *sm = sta->eapol_sm;
1888 if (sm == NULL)
1889 return;
1890
1891 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1892 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1893
1894#ifndef CONFIG_NO_RADIUS
1895 radius_msg_free(sm->last_recv_radius);
1896 sm->last_recv_radius = NULL;
1897#endif /* CONFIG_NO_RADIUS */
1898
1899 if (sm->eap_if->eapTimeout) {
1900 /*
1901 * Disconnect the STA since it did not reply to the last EAP
1902 * request and we cannot continue EAP processing (EAP-Failure
1903 * could only be sent if the EAP peer actually replied).
1904 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001905 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1906 MAC2STR(sta->addr));
1907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 sm->eap_if->portEnabled = FALSE;
1909 ap_sta_disconnect(hapd, sta, sta->addr,
1910 WLAN_REASON_PREV_AUTH_NOT_VALID);
1911 }
1912}
1913
1914
1915static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1916{
1917 struct eapol_authenticator *eapol = hapd->eapol_auth;
1918
1919 if (hapd->conf->default_wep_key_len < 1)
1920 return 0;
1921
1922 os_free(eapol->default_wep_key);
1923 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1924 if (eapol->default_wep_key == NULL ||
1925 random_get_bytes(eapol->default_wep_key,
1926 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001927 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 os_free(eapol->default_wep_key);
1929 eapol->default_wep_key = NULL;
1930 return -1;
1931 }
1932
1933 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1934 eapol->default_wep_key,
1935 hapd->conf->default_wep_key_len);
1936
1937 return 0;
1938}
1939
1940
1941static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1942 struct sta_info *sta, void *ctx)
1943{
1944 if (sta->eapol_sm) {
1945 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1946 eapol_auth_step(sta->eapol_sm);
1947 }
1948 return 0;
1949}
1950
1951
1952static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1953{
1954 struct hostapd_data *hapd = eloop_ctx;
1955 struct eapol_authenticator *eapol = hapd->eapol_auth;
1956
1957 if (eapol->default_wep_key_idx >= 3)
1958 eapol->default_wep_key_idx =
1959 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1960 else
1961 eapol->default_wep_key_idx++;
1962
1963 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1964 eapol->default_wep_key_idx);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 if (ieee802_1x_rekey_broadcast(hapd)) {
1967 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1968 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1969 "new broadcast key");
1970 os_free(eapol->default_wep_key);
1971 eapol->default_wep_key = NULL;
1972 return;
1973 }
1974
1975 /* TODO: Could setup key for RX here, but change default TX keyid only
1976 * after new broadcast key has been sent to all stations. */
1977 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1978 broadcast_ether_addr,
1979 eapol->default_wep_key_idx, 1, NULL, 0,
1980 eapol->default_wep_key,
1981 hapd->conf->default_wep_key_len)) {
1982 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1983 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1984 "new broadcast key");
1985 os_free(eapol->default_wep_key);
1986 eapol->default_wep_key = NULL;
1987 return;
1988 }
1989
1990 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1991
1992 if (hapd->conf->wep_rekeying_period > 0) {
1993 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1994 ieee802_1x_rekey, hapd, NULL);
1995 }
1996}
1997
1998
1999static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2000 const u8 *data, size_t datalen)
2001{
2002#ifdef CONFIG_WPS
2003 struct sta_info *sta = sta_ctx;
2004
2005 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2006 WLAN_STA_MAYBE_WPS) {
2007 const u8 *identity;
2008 size_t identity_len;
2009 struct eapol_state_machine *sm = sta->eapol_sm;
2010
2011 identity = eap_get_identity(sm->eap, &identity_len);
2012 if (identity &&
2013 ((identity_len == WSC_ID_ENROLLEE_LEN &&
2014 os_memcmp(identity, WSC_ID_ENROLLEE,
2015 WSC_ID_ENROLLEE_LEN) == 0) ||
2016 (identity_len == WSC_ID_REGISTRAR_LEN &&
2017 os_memcmp(identity, WSC_ID_REGISTRAR,
2018 WSC_ID_REGISTRAR_LEN) == 0))) {
2019 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
2020 "WLAN_STA_WPS");
2021 sta->flags |= WLAN_STA_WPS;
2022 }
2023 }
2024#endif /* CONFIG_WPS */
2025
2026 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2027}
2028
2029
2030static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2031 const u8 *data, size_t datalen)
2032{
2033#ifndef CONFIG_NO_RADIUS
2034 struct hostapd_data *hapd = ctx;
2035 struct sta_info *sta = sta_ctx;
2036
2037 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2038#endif /* CONFIG_NO_RADIUS */
2039}
2040
2041
2042static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002043 int preauth, int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044{
2045 struct hostapd_data *hapd = ctx;
2046 struct sta_info *sta = sta_ctx;
2047 if (preauth)
2048 rsn_preauth_finished(hapd, sta, success);
2049 else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002050 ieee802_1x_finished(hapd, sta, success, remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051}
2052
2053
2054static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2055 size_t identity_len, int phase2,
2056 struct eap_user *user)
2057{
2058 struct hostapd_data *hapd = ctx;
2059 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060 int i;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002061 int rv = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002063 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 if (eap_user == NULL)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002065 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066
2067 os_memset(user, 0, sizeof(*user));
2068 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002069 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 user->methods[i].vendor = eap_user->methods[i].vendor;
2071 user->methods[i].method = eap_user->methods[i].method;
2072 }
2073
2074 if (eap_user->password) {
2075 user->password = os_malloc(eap_user->password_len);
2076 if (user->password == NULL)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002077 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 os_memcpy(user->password, eap_user->password,
2079 eap_user->password_len);
2080 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002081 user->password_hash = eap_user->password_hash;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 }
2083 user->force_version = eap_user->force_version;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002084 user->macacl = eap_user->macacl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002086 user->remediation = eap_user->remediation;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002087 rv = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002089out:
2090 if (rv)
2091 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2092
2093 return rv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094}
2095
2096
2097static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2098{
2099 struct hostapd_data *hapd = ctx;
2100 struct sta_info *sta;
2101 sta = ap_get_sta(hapd, addr);
2102 if (sta == NULL || sta->eapol_sm == NULL)
2103 return 0;
2104 return 1;
2105}
2106
2107
2108static void ieee802_1x_logger(void *ctx, const u8 *addr,
2109 eapol_logger_level level, const char *txt)
2110{
2111#ifndef CONFIG_NO_HOSTAPD_LOGGER
2112 struct hostapd_data *hapd = ctx;
2113 int hlevel;
2114
2115 switch (level) {
2116 case EAPOL_LOGGER_WARNING:
2117 hlevel = HOSTAPD_LEVEL_WARNING;
2118 break;
2119 case EAPOL_LOGGER_INFO:
2120 hlevel = HOSTAPD_LEVEL_INFO;
2121 break;
2122 case EAPOL_LOGGER_DEBUG:
2123 default:
2124 hlevel = HOSTAPD_LEVEL_DEBUG;
2125 break;
2126 }
2127
2128 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2129 txt);
2130#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2131}
2132
2133
2134static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2135 int authorized)
2136{
2137 struct hostapd_data *hapd = ctx;
2138 struct sta_info *sta = sta_ctx;
2139 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2140}
2141
2142
2143static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2144{
2145 struct hostapd_data *hapd = ctx;
2146 struct sta_info *sta = sta_ctx;
2147 ieee802_1x_abort_auth(hapd, sta);
2148}
2149
2150
2151static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2152{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002153#ifndef CONFIG_FIPS
2154#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 struct hostapd_data *hapd = ctx;
2156 struct sta_info *sta = sta_ctx;
2157 ieee802_1x_tx_key(hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002158#endif /* CONFIG_NO_RC4 */
2159#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160}
2161
2162
2163static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2164 enum eapol_event type)
2165{
2166 /* struct hostapd_data *hapd = ctx; */
2167 struct sta_info *sta = sta_ctx;
2168 switch (type) {
2169 case EAPOL_AUTH_SM_CHANGE:
2170 wpa_auth_sm_notify(sta->wpa_sm);
2171 break;
2172 case EAPOL_AUTH_REAUTHENTICATE:
2173 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2174 break;
2175 }
2176}
2177
2178
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002179#ifdef CONFIG_ERP
2180
2181static struct eap_server_erp_key *
2182ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2183{
2184 struct hostapd_data *hapd = ctx;
2185 struct eap_server_erp_key *erp;
2186
2187 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2188 list) {
2189 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2190 return erp;
2191 }
2192
2193 return NULL;
2194}
2195
2196
2197static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2198{
2199 struct hostapd_data *hapd = ctx;
2200
2201 dl_list_add(&hapd->erp_keys, &erp->list);
2202 return 0;
2203}
2204
2205#endif /* CONFIG_ERP */
2206
2207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208int ieee802_1x_init(struct hostapd_data *hapd)
2209{
2210 int i;
2211 struct eapol_auth_config conf;
2212 struct eapol_auth_cb cb;
2213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002214 dl_list_init(&hapd->erp_keys);
2215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216 os_memset(&conf, 0, sizeof(conf));
2217 conf.ctx = hapd;
2218 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2219 conf.wpa = hapd->conf->wpa;
2220 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2221 conf.eap_server = hapd->conf->eap_server;
2222 conf.ssl_ctx = hapd->ssl_ctx;
2223 conf.msg_ctx = hapd->msg_ctx;
2224 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2225 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2226 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002227 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2228 conf.erp_domain = hapd->conf->erp_domain;
2229 conf.erp = hapd->conf->eap_server_erp;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002230 conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2232 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2233 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2234 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2235 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2236 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2237 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2238 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2239 conf.tnc = hapd->conf->tnc;
2240 conf.wps = hapd->wps;
2241 conf.fragment_size = hapd->conf->fragment_size;
2242 conf.pwd_group = hapd->conf->pwd_group;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002243 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002244 if (hapd->conf->server_id) {
2245 conf.server_id = (const u8 *) hapd->conf->server_id;
2246 conf.server_id_len = os_strlen(hapd->conf->server_id);
2247 } else {
2248 conf.server_id = (const u8 *) "hostapd";
2249 conf.server_id_len = 7;
2250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251
2252 os_memset(&cb, 0, sizeof(cb));
2253 cb.eapol_send = ieee802_1x_eapol_send;
2254 cb.aaa_send = ieee802_1x_aaa_send;
2255 cb.finished = _ieee802_1x_finished;
2256 cb.get_eap_user = ieee802_1x_get_eap_user;
2257 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2258 cb.logger = ieee802_1x_logger;
2259 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2260 cb.abort_auth = _ieee802_1x_abort_auth;
2261 cb.tx_key = _ieee802_1x_tx_key;
2262 cb.eapol_event = ieee802_1x_eapol_event;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002263#ifdef CONFIG_ERP
2264 cb.erp_get_key = ieee802_1x_erp_get_key;
2265 cb.erp_add_key = ieee802_1x_erp_add_key;
2266#endif /* CONFIG_ERP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267
2268 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2269 if (hapd->eapol_auth == NULL)
2270 return -1;
2271
2272 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2273 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2274 return -1;
2275
2276#ifndef CONFIG_NO_RADIUS
2277 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2278 ieee802_1x_receive_auth, hapd))
2279 return -1;
2280#endif /* CONFIG_NO_RADIUS */
2281
2282 if (hapd->conf->default_wep_key_len) {
2283 for (i = 0; i < 4; i++)
2284 hostapd_drv_set_key(hapd->conf->iface, hapd,
2285 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2286 NULL, 0);
2287
2288 ieee802_1x_rekey(hapd, NULL);
2289
2290 if (hapd->eapol_auth->default_wep_key == NULL)
2291 return -1;
2292 }
2293
2294 return 0;
2295}
2296
2297
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002298void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2299{
2300 struct eap_server_erp_key *erp;
2301
2302 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2303 list)) != NULL) {
2304 dl_list_del(&erp->list);
2305 bin_clear_free(erp, sizeof(*erp));
2306 }
2307}
2308
2309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310void ieee802_1x_deinit(struct hostapd_data *hapd)
2311{
2312 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2313
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002314 if (hapd->driver && hapd->drv_priv &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2316 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2317
2318 eapol_auth_deinit(hapd->eapol_auth);
2319 hapd->eapol_auth = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002320
2321 ieee802_1x_erp_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322}
2323
2324
2325int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2326 const u8 *buf, size_t len, int ack)
2327{
2328 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 u8 *pos;
2330 const unsigned char rfc1042_hdr[ETH_ALEN] =
2331 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2332
2333 if (sta == NULL)
2334 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002335 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 return 0;
2337
2338 hdr = (struct ieee80211_hdr *) buf;
2339 pos = (u8 *) (hdr + 1);
2340 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2341 return 0;
2342 pos += sizeof(rfc1042_hdr);
2343 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2344 return 0;
2345 pos += 2;
2346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002347 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2348 ack);
2349}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002351
2352int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2353 const u8 *buf, int len, int ack)
2354{
2355 const struct ieee802_1x_hdr *xhdr =
2356 (const struct ieee802_1x_hdr *) buf;
2357 const u8 *pos = buf + sizeof(*xhdr);
2358 struct ieee802_1x_eapol_key *key;
2359
2360 if (len < (int) sizeof(*xhdr))
2361 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2363 "type=%d length=%d - ack=%d",
2364 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2365 be_to_host16(xhdr->length), ack);
2366
Dmitry Shmidt29333592017-01-09 12:27:11 -08002367#ifdef CONFIG_WPS
2368 if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2369 (sta->flags & WLAN_STA_WPS) &&
2370 ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2371 wpa_printf(MSG_DEBUG,
2372 "WPS: Indicate EAP completion on ACK for EAP-Failure");
2373 hostapd_wps_eap_completed(hapd);
2374 }
2375#endif /* CONFIG_WPS */
2376
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2378 return 0;
2379
2380 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 const struct wpa_eapol_key *wpa;
2382 wpa = (const struct wpa_eapol_key *) pos;
2383 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2384 wpa->type == EAPOL_KEY_TYPE_WPA)
2385 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2386 sta->wpa_sm, ack);
2387 }
2388
2389 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2390 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002391 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 * packets couple of times because otherwise STA keys become
2393 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002394 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 key = (struct ieee802_1x_eapol_key *) pos;
2396 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2397 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2398 "frame (%scast index=%d)",
2399 key->key_index & BIT(7) ? "uni" : "broad",
2400 key->key_index & ~BIT(7));
2401 /* TODO: re-send EAPOL-Key couple of times (with short delay
2402 * between them?). If all attempt fail, report error and
2403 * deauthenticate STA so that it will get new keys when
2404 * authenticating again (e.g., after returning in range).
2405 * Separate limit/transmit state needed both for unicast and
2406 * broadcast keys(?) */
2407 }
2408 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2409 * to here and change the key only if the EAPOL-Key packet was Acked.
2410 */
2411
2412 return 1;
2413}
2414
2415
2416u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2417{
2418 if (sm == NULL || sm->identity == NULL)
2419 return NULL;
2420
2421 *len = sm->identity_len;
2422 return sm->identity;
2423}
2424
2425
2426u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2427 int idx)
2428{
2429 if (sm == NULL || sm->radius_class.attr == NULL ||
2430 idx >= (int) sm->radius_class.count)
2431 return NULL;
2432
2433 *len = sm->radius_class.attr[idx].len;
2434 return sm->radius_class.attr[idx].data;
2435}
2436
2437
Dmitry Shmidt04949592012-07-19 12:16:46 -07002438struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2439{
2440 if (sm == NULL)
2441 return NULL;
2442 return sm->radius_cui;
2443}
2444
2445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2447{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002448 *len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 if (sm == NULL)
2450 return NULL;
2451
2452 *len = sm->eap_if->eapKeyDataLen;
2453 return sm->eap_if->eapKeyData;
2454}
2455
2456
2457void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2458 int enabled)
2459{
2460 if (sm == NULL)
2461 return;
2462 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2463 eapol_auth_step(sm);
2464}
2465
2466
2467void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2468 int valid)
2469{
2470 if (sm == NULL)
2471 return;
2472 sm->portValid = valid ? TRUE : FALSE;
2473 eapol_auth_step(sm);
2474}
2475
2476
2477void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2478{
2479 if (sm == NULL)
2480 return;
2481 if (pre_auth)
2482 sm->flags |= EAPOL_SM_PREAUTH;
2483 else
2484 sm->flags &= ~EAPOL_SM_PREAUTH;
2485}
2486
2487
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002488static const char * bool_txt(Boolean val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002490 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491}
2492
2493
2494int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2495{
2496 /* TODO */
2497 return 0;
2498}
2499
2500
2501int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2502 char *buf, size_t buflen)
2503{
2504 int len = 0, ret;
2505 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002506 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002507 const char *name1;
2508 const char *name2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509
2510 if (sm == NULL)
2511 return 0;
2512
2513 ret = os_snprintf(buf + len, buflen - len,
2514 "dot1xPaePortNumber=%d\n"
2515 "dot1xPaePortProtocolVersion=%d\n"
2516 "dot1xPaePortCapabilities=1\n"
2517 "dot1xPaePortInitialize=%d\n"
2518 "dot1xPaePortReauthenticate=FALSE\n",
2519 sta->aid,
2520 EAPOL_VERSION,
2521 sm->initialize);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002522 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 return len;
2524 len += ret;
2525
2526 /* dot1xAuthConfigTable */
2527 ret = os_snprintf(buf + len, buflen - len,
2528 "dot1xAuthPaeState=%d\n"
2529 "dot1xAuthBackendAuthState=%d\n"
2530 "dot1xAuthAdminControlledDirections=%d\n"
2531 "dot1xAuthOperControlledDirections=%d\n"
2532 "dot1xAuthAuthControlledPortStatus=%d\n"
2533 "dot1xAuthAuthControlledPortControl=%d\n"
2534 "dot1xAuthQuietPeriod=%u\n"
2535 "dot1xAuthServerTimeout=%u\n"
2536 "dot1xAuthReAuthPeriod=%u\n"
2537 "dot1xAuthReAuthEnabled=%s\n"
2538 "dot1xAuthKeyTxEnabled=%s\n",
2539 sm->auth_pae_state + 1,
2540 sm->be_auth_state + 1,
2541 sm->adminControlledDirections,
2542 sm->operControlledDirections,
2543 sm->authPortStatus,
2544 sm->portControl,
2545 sm->quietPeriod,
2546 sm->serverTimeout,
2547 sm->reAuthPeriod,
2548 bool_txt(sm->reAuthEnabled),
2549 bool_txt(sm->keyTxEnabled));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002550 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 return len;
2552 len += ret;
2553
2554 /* dot1xAuthStatsTable */
2555 ret = os_snprintf(buf + len, buflen - len,
2556 "dot1xAuthEapolFramesRx=%u\n"
2557 "dot1xAuthEapolFramesTx=%u\n"
2558 "dot1xAuthEapolStartFramesRx=%u\n"
2559 "dot1xAuthEapolLogoffFramesRx=%u\n"
2560 "dot1xAuthEapolRespIdFramesRx=%u\n"
2561 "dot1xAuthEapolRespFramesRx=%u\n"
2562 "dot1xAuthEapolReqIdFramesTx=%u\n"
2563 "dot1xAuthEapolReqFramesTx=%u\n"
2564 "dot1xAuthInvalidEapolFramesRx=%u\n"
2565 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2566 "dot1xAuthLastEapolFrameVersion=%u\n"
2567 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2568 sm->dot1xAuthEapolFramesRx,
2569 sm->dot1xAuthEapolFramesTx,
2570 sm->dot1xAuthEapolStartFramesRx,
2571 sm->dot1xAuthEapolLogoffFramesRx,
2572 sm->dot1xAuthEapolRespIdFramesRx,
2573 sm->dot1xAuthEapolRespFramesRx,
2574 sm->dot1xAuthEapolReqIdFramesTx,
2575 sm->dot1xAuthEapolReqFramesTx,
2576 sm->dot1xAuthInvalidEapolFramesRx,
2577 sm->dot1xAuthEapLengthErrorFramesRx,
2578 sm->dot1xAuthLastEapolFrameVersion,
2579 MAC2STR(sm->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002580 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 return len;
2582 len += ret;
2583
2584 /* dot1xAuthDiagTable */
2585 ret = os_snprintf(buf + len, buflen - len,
2586 "dot1xAuthEntersConnecting=%u\n"
2587 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2588 "dot1xAuthEntersAuthenticating=%u\n"
2589 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2590 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2591 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2592 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2593 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2594 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2595 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2596 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2597 "dot1xAuthBackendResponses=%u\n"
2598 "dot1xAuthBackendAccessChallenges=%u\n"
2599 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2600 "dot1xAuthBackendAuthSuccesses=%u\n"
2601 "dot1xAuthBackendAuthFails=%u\n",
2602 sm->authEntersConnecting,
2603 sm->authEapLogoffsWhileConnecting,
2604 sm->authEntersAuthenticating,
2605 sm->authAuthSuccessesWhileAuthenticating,
2606 sm->authAuthTimeoutsWhileAuthenticating,
2607 sm->authAuthFailWhileAuthenticating,
2608 sm->authAuthEapStartsWhileAuthenticating,
2609 sm->authAuthEapLogoffWhileAuthenticating,
2610 sm->authAuthReauthsWhileAuthenticated,
2611 sm->authAuthEapStartsWhileAuthenticated,
2612 sm->authAuthEapLogoffWhileAuthenticated,
2613 sm->backendResponses,
2614 sm->backendAccessChallenges,
2615 sm->backendOtherRequestsToSupplicant,
2616 sm->backendAuthSuccesses,
2617 sm->backendAuthFails);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002618 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 return len;
2620 len += ret;
2621
2622 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002623 os_reltime_age(&sta->acct_session_start, &diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 ret = os_snprintf(buf + len, buflen - len,
2625 /* TODO: dot1xAuthSessionOctetsRx */
2626 /* TODO: dot1xAuthSessionOctetsTx */
2627 /* TODO: dot1xAuthSessionFramesRx */
2628 /* TODO: dot1xAuthSessionFramesTx */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002629 "dot1xAuthSessionId=%016llX\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 "dot1xAuthSessionAuthenticMethod=%d\n"
2631 "dot1xAuthSessionTime=%u\n"
2632 "dot1xAuthSessionTerminateCause=999\n"
2633 "dot1xAuthSessionUserName=%s\n",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002634 (unsigned long long) sta->acct_session_id,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 (wpa_key_mgmt_wpa_ieee8021x(
2636 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2637 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002638 (unsigned int) diff.sec,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 sm->identity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002640 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 return len;
2642 len += ret;
2643
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002644 if (sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002645 ret = os_snprintf(buf + len, buflen - len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002646 "authMultiSessionId=%016llX\n",
2647 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002648 sm->acct_multi_session_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002649 if (os_snprintf_error(buflen - len, ret))
2650 return len;
2651 len += ret;
2652 }
2653
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002654 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2655 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002656 ret = os_snprintf(buf + len, buflen - len,
2657 "last_eap_type_as=%d (%s)\n"
2658 "last_eap_type_sta=%d (%s)\n",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002659 sm->eap_type_authsrv, name1,
2660 sm->eap_type_supp, name2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002661 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002662 return len;
2663 len += ret;
2664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665 return len;
2666}
2667
2668
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002669#ifdef CONFIG_HS20
2670static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
2671{
2672 struct hostapd_data *hapd = eloop_ctx;
2673 struct sta_info *sta = timeout_ctx;
2674
2675 if (sta->remediation) {
2676 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2677 MACSTR " to indicate Subscription Remediation",
2678 MAC2STR(sta->addr));
2679 hs20_send_wnm_notification(hapd, sta->addr,
2680 sta->remediation_method,
2681 sta->remediation_url);
2682 os_free(sta->remediation_url);
2683 sta->remediation_url = NULL;
2684 }
2685
2686 if (sta->hs20_deauth_req) {
2687 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2688 MACSTR " to indicate imminent deauthentication",
2689 MAC2STR(sta->addr));
2690 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
2691 sta->hs20_deauth_req);
2692 }
2693}
2694#endif /* CONFIG_HS20 */
2695
2696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002698 struct sta_info *sta, int success,
2699 int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700{
2701 const u8 *key;
2702 size_t len;
2703 /* TODO: get PMKLifetime from WPA parameters */
2704 static const int dot11RSNAConfigPMKLifetime = 43200;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002705 unsigned int session_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002707#ifdef CONFIG_HS20
2708 if (remediation && !sta->remediation) {
2709 sta->remediation = 1;
2710 os_free(sta->remediation_url);
2711 sta->remediation_url =
2712 os_strdup(hapd->conf->subscr_remediation_url);
2713 sta->remediation_method = 1; /* SOAP-XML SPP */
2714 }
2715
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002716 if (success && (sta->remediation || sta->hs20_deauth_req)) {
2717 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
2718 MACSTR " in 100 ms", MAC2STR(sta->addr));
2719 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
2720 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
2721 hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002722 }
2723#endif /* CONFIG_HS20 */
2724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002726 if (sta->session_timeout_set)
2727 session_timeout = sta->session_timeout;
2728 else
2729 session_timeout = dot11RSNAConfigPMKLifetime;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002730 if (success && key && len >= PMK_LEN && !sta->remediation &&
2731 !sta->hs20_deauth_requested &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002732 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 sta->eapol_sm) == 0) {
2734 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2735 HOSTAPD_LEVEL_DEBUG,
2736 "Added PMKSA cache entry (IEEE 802.1X)");
2737 }
2738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002739 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740 /*
2741 * Many devices require deauthentication after WPS provisioning
2742 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002743 * disconnect the client here. In addition, this may also
2744 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2745 * the EAPOL PAE state machine would remain in HELD state for
2746 * considerable amount of time and some EAP methods, like
2747 * EAP-FAST with anonymous provisioning, may require another
2748 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 */
Dmitry Shmidt29333592017-01-09 12:27:11 -08002750 ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752}