blob: e3b3d94e5b44804f713073693c8d0803ac858870 [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 Shmidt8d520ff2011-05-09 14:06:53 -070034#include "ieee802_1x.h"
35
36
37static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080038 struct sta_info *sta, int success,
39 int remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
43 u8 type, const u8 *data, size_t datalen)
44{
45 u8 *buf;
46 struct ieee802_1x_hdr *xhdr;
47 size_t len;
48 int encrypt = 0;
49
50 len = sizeof(*xhdr) + datalen;
51 buf = os_zalloc(len);
52 if (buf == NULL) {
53 wpa_printf(MSG_ERROR, "malloc() failed for "
54 "ieee802_1x_send(len=%lu)",
55 (unsigned long) len);
56 return;
57 }
58
59 xhdr = (struct ieee802_1x_hdr *) buf;
60 xhdr->version = hapd->conf->eapol_version;
61 xhdr->type = type;
62 xhdr->length = host_to_be16(datalen);
63
64 if (datalen > 0 && data != NULL)
65 os_memcpy(xhdr + 1, data, datalen);
66
67 if (wpa_auth_pairwise_set(sta->wpa_sm))
68 encrypt = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080069#ifdef CONFIG_TESTING_OPTIONS
70 if (hapd->ext_eapol_frame_io) {
71 size_t hex_len = 2 * len + 1;
72 char *hex = os_malloc(hex_len);
73
74 if (hex) {
75 wpa_snprintf_hex(hex, hex_len, buf, len);
76 wpa_msg(hapd->msg_ctx, MSG_INFO,
77 "EAPOL-TX " MACSTR " %s",
78 MAC2STR(sta->addr), hex);
79 os_free(hex);
80 }
81 } else
82#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 if (sta->flags & WLAN_STA_PREAUTH) {
84 rsn_preauth_send(hapd, sta, buf, len);
85 } else {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080086 hostapd_drv_hapd_send_eapol(
87 hapd, sta->addr, buf, len,
88 encrypt, hostapd_sta_flags_to_drv(sta->flags));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 }
90
91 os_free(buf);
92}
93
94
95void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
96 struct sta_info *sta, int authorized)
97{
98 int res;
99
100 if (sta->flags & WLAN_STA_PREAUTH)
101 return;
102
103 if (authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 ap_sta_set_authorized(hapd, sta, 1);
105 res = hostapd_set_authorized(hapd, sta, 1);
106 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
107 HOSTAPD_LEVEL_DEBUG, "authorizing port");
108 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 ap_sta_set_authorized(hapd, sta, 0);
110 res = hostapd_set_authorized(hapd, sta, 0);
111 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
112 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
113 }
114
115 if (res && errno != ENOENT) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700116 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
117 " flags for kernel driver (errno=%d).",
118 MAC2STR(sta->addr), errno);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119 }
120
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800121 if (authorized) {
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800122 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800124 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125}
126
127
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800128#ifndef CONFIG_FIPS
129#ifndef CONFIG_NO_RC4
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
132 struct sta_info *sta,
133 int idx, int broadcast,
134 u8 *key_data, size_t key_len)
135{
136 u8 *buf, *ekey;
137 struct ieee802_1x_hdr *hdr;
138 struct ieee802_1x_eapol_key *key;
139 size_t len, ekey_len;
140 struct eapol_state_machine *sm = sta->eapol_sm;
141
142 if (sm == NULL)
143 return;
144
145 len = sizeof(*key) + key_len;
146 buf = os_zalloc(sizeof(*hdr) + len);
147 if (buf == NULL)
148 return;
149
150 hdr = (struct ieee802_1x_hdr *) buf;
151 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
152 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700153 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 wpa_get_ntp_timestamp(key->replay_counter);
155
156 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
157 wpa_printf(MSG_ERROR, "Could not get random numbers");
158 os_free(buf);
159 return;
160 }
161
162 key->key_index = idx | (broadcast ? 0 : BIT(7));
163 if (hapd->conf->eapol_key_index_workaround) {
164 /* According to some information, WinXP Supplicant seems to
165 * interpret bit7 as an indication whether the key is to be
166 * activated, so make it possible to enable workaround that
167 * sets this bit for all keys. */
168 key->key_index |= BIT(7);
169 }
170
171 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
172 * MSK[32..63] is used to sign the message. */
173 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
174 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
175 "and signing EAPOL-Key");
176 os_free(buf);
177 return;
178 }
179 os_memcpy((u8 *) (key + 1), key_data, key_len);
180 ekey_len = sizeof(key->key_iv) + 32;
181 ekey = os_malloc(ekey_len);
182 if (ekey == NULL) {
183 wpa_printf(MSG_ERROR, "Could not encrypt key");
184 os_free(buf);
185 return;
186 }
187 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
188 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
189 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
190 os_free(ekey);
191
192 /* This header is needed here for HMAC-MD5, but it will be regenerated
193 * in ieee802_1x_send() */
194 hdr->version = hapd->conf->eapol_version;
195 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
196 hdr->length = host_to_be16(len);
197 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
198 key->key_signature);
199
200 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
201 " (%s index=%d)", MAC2STR(sm->addr),
202 broadcast ? "broadcast" : "unicast", idx);
203 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
204 if (sta->eapol_sm)
205 sta->eapol_sm->dot1xAuthEapolFramesTx++;
206 os_free(buf);
207}
208
209
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800210static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211{
212 struct eapol_authenticator *eapol = hapd->eapol_auth;
213 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214
215 if (sm == NULL || !sm->eap_if->eapKeyData)
216 return;
217
218 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
219 MAC2STR(sta->addr));
220
221#ifndef CONFIG_NO_VLAN
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700222 if (sta->vlan_id > 0 && sta->vlan_id <= MAX_VLAN_ID) {
223 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
224 return;
225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 if (eapol->default_wep_key) {
229 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
230 eapol->default_wep_key,
231 hapd->conf->default_wep_key_len);
232 }
233
234 if (hapd->conf->individual_wep_key_len > 0) {
235 u8 *ikey;
236 ikey = os_malloc(hapd->conf->individual_wep_key_len);
237 if (ikey == NULL ||
238 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
239 {
240 wpa_printf(MSG_ERROR, "Could not generate random "
241 "individual WEP key.");
242 os_free(ikey);
243 return;
244 }
245
246 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
247 ikey, hapd->conf->individual_wep_key_len);
248
249 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
250 hapd->conf->individual_wep_key_len);
251
252 /* TODO: set encryption in TX callback, i.e., only after STA
253 * has ACKed EAPOL-Key frame */
254 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
255 sta->addr, 0, 1, NULL, 0, ikey,
256 hapd->conf->individual_wep_key_len)) {
257 wpa_printf(MSG_ERROR, "Could not set individual WEP "
258 "encryption.");
259 }
260
261 os_free(ikey);
262 }
263}
264
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800265#endif /* CONFIG_NO_RC4 */
266#endif /* CONFIG_FIPS */
267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268
269const char *radius_mode_txt(struct hostapd_data *hapd)
270{
271 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800272 case HOSTAPD_MODE_IEEE80211AD:
273 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 case HOSTAPD_MODE_IEEE80211A:
275 return "802.11a";
276 case HOSTAPD_MODE_IEEE80211G:
277 return "802.11g";
278 case HOSTAPD_MODE_IEEE80211B:
279 default:
280 return "802.11b";
281 }
282}
283
284
285int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
286{
287 int i;
288 u8 rate = 0;
289
290 for (i = 0; i < sta->supported_rates_len; i++)
291 if ((sta->supported_rates[i] & 0x7f) > rate)
292 rate = sta->supported_rates[i] & 0x7f;
293
294 return rate;
295}
296
297
298#ifndef CONFIG_NO_RADIUS
299static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
300 struct eapol_state_machine *sm,
301 const u8 *eap, size_t len)
302{
303 const u8 *identity;
304 size_t identity_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800305 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306
307 if (len <= sizeof(struct eap_hdr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800308 (hdr->code == EAP_CODE_RESPONSE &&
309 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
310 (hdr->code == EAP_CODE_INITIATE &&
311 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
312 (hdr->code != EAP_CODE_RESPONSE &&
313 hdr->code != EAP_CODE_INITIATE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 return;
315
316 identity = eap_get_identity(sm->eap, &identity_len);
317 if (identity == NULL)
318 return;
319
320 /* Save station identity for future RADIUS packets */
321 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700322 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 if (sm->identity == NULL) {
324 sm->identity_len = 0;
325 return;
326 }
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
330 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
331 sm->dot1xAuthEapolRespIdFramesRx++;
332}
333
334
Dmitry Shmidt03658832014-08-13 11:03:49 -0700335static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
336 struct hostapd_radius_attr *req_attr,
337 struct sta_info *sta,
338 struct radius_msg *msg)
339{
340 u32 suite;
341 int ver, val;
342
343 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
344 val = wpa_auth_get_pairwise(sta->wpa_sm);
345 suite = wpa_cipher_to_suite(ver, val);
346 if (val != -1 &&
347 !hostapd_config_get_radius_attr(req_attr,
348 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
349 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
350 suite)) {
351 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
352 return -1;
353 }
354
Dmitry Shmidt41712582015-06-29 11:02:15 -0700355 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
356 hapd->conf->osen) ?
Dmitry Shmidt03658832014-08-13 11:03:49 -0700357 WPA_PROTO_RSN : WPA_PROTO_WPA,
358 hapd->conf->wpa_group);
359 if (!hostapd_config_get_radius_attr(req_attr,
360 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
361 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
362 suite)) {
363 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
364 return -1;
365 }
366
367 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
368 suite = wpa_akm_to_suite(val);
369 if (val != -1 &&
370 !hostapd_config_get_radius_attr(req_attr,
371 RADIUS_ATTR_WLAN_AKM_SUITE) &&
372 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
373 suite)) {
374 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
375 return -1;
376 }
377
378#ifdef CONFIG_IEEE80211W
379 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
380 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
381 hapd->conf->group_mgmt_cipher);
382 if (!hostapd_config_get_radius_attr(
383 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
384 !radius_msg_add_attr_int32(
385 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
386 wpa_printf(MSG_ERROR,
387 "Could not add WLAN-Group-Mgmt-Cipher");
388 return -1;
389 }
390 }
391#endif /* CONFIG_IEEE80211W */
392
393 return 0;
394}
395
396
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700397static int add_common_radius_sta_attr(struct hostapd_data *hapd,
398 struct hostapd_radius_attr *req_attr,
399 struct sta_info *sta,
400 struct radius_msg *msg)
401{
402 char buf[128];
403
404 if (!hostapd_config_get_radius_attr(req_attr,
405 RADIUS_ATTR_NAS_PORT) &&
406 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
407 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
408 return -1;
409 }
410
411 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
412 MAC2STR(sta->addr));
413 buf[sizeof(buf) - 1] = '\0';
414 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
415 (u8 *) buf, os_strlen(buf))) {
416 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
417 return -1;
418 }
419
420 if (sta->flags & WLAN_STA_PREAUTH) {
421 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
422 sizeof(buf));
423 } else {
424 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
425 radius_sta_rate(hapd, sta) / 2,
426 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
427 radius_mode_txt(hapd));
428 buf[sizeof(buf) - 1] = '\0';
429 }
430 if (!hostapd_config_get_radius_attr(req_attr,
431 RADIUS_ATTR_CONNECT_INFO) &&
432 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
433 (u8 *) buf, os_strlen(buf))) {
434 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
435 return -1;
436 }
437
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800438 if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
439 os_snprintf(buf, sizeof(buf), "%08X-%08X",
440 sta->acct_session_id_hi, sta->acct_session_id_lo);
441 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
442 (u8 *) buf, os_strlen(buf))) {
443 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
444 return -1;
445 }
446 }
447
Dmitry Shmidt03658832014-08-13 11:03:49 -0700448#ifdef CONFIG_IEEE80211R
449 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
450 sta->wpa_sm &&
451 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
452 sta->auth_alg == WLAN_AUTH_FT) &&
453 !hostapd_config_get_radius_attr(req_attr,
454 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
455 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
456 WPA_GET_BE16(
457 hapd->conf->mobility_domain))) {
458 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
459 return -1;
460 }
461#endif /* CONFIG_IEEE80211R */
462
Dmitry Shmidt41712582015-06-29 11:02:15 -0700463 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
Dmitry Shmidt03658832014-08-13 11:03:49 -0700464 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
465 return -1;
466
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700467 return 0;
468}
469
470
471int add_common_radius_attr(struct hostapd_data *hapd,
472 struct hostapd_radius_attr *req_attr,
473 struct sta_info *sta,
474 struct radius_msg *msg)
475{
476 char buf[128];
477 struct hostapd_radius_attr *attr;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800478 int len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479
480 if (!hostapd_config_get_radius_attr(req_attr,
481 RADIUS_ATTR_NAS_IP_ADDRESS) &&
482 hapd->conf->own_ip_addr.af == AF_INET &&
483 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
484 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
485 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
486 return -1;
487 }
488
489#ifdef CONFIG_IPV6
490 if (!hostapd_config_get_radius_attr(req_attr,
491 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
492 hapd->conf->own_ip_addr.af == AF_INET6 &&
493 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
494 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
495 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
496 return -1;
497 }
498#endif /* CONFIG_IPV6 */
499
500 if (!hostapd_config_get_radius_attr(req_attr,
501 RADIUS_ATTR_NAS_IDENTIFIER) &&
502 hapd->conf->nas_identifier &&
503 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
504 (u8 *) hapd->conf->nas_identifier,
505 os_strlen(hapd->conf->nas_identifier))) {
506 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
507 return -1;
508 }
509
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800510 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
511 MAC2STR(hapd->own_addr));
512 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
513 hapd->conf->ssid.ssid_len);
514 len += hapd->conf->ssid.ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700515 if (!hostapd_config_get_radius_attr(req_attr,
516 RADIUS_ATTR_CALLED_STATION_ID) &&
517 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800518 (u8 *) buf, len)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700519 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
520 return -1;
521 }
522
523 if (!hostapd_config_get_radius_attr(req_attr,
524 RADIUS_ATTR_NAS_PORT_TYPE) &&
525 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
526 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
527 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
528 return -1;
529 }
530
Dmitry Shmidt03658832014-08-13 11:03:49 -0700531#ifdef CONFIG_INTERWORKING
532 if (hapd->conf->interworking &&
533 !is_zero_ether_addr(hapd->conf->hessid)) {
534 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
535 MAC2STR(hapd->conf->hessid));
536 buf[sizeof(buf) - 1] = '\0';
537 if (!hostapd_config_get_radius_attr(req_attr,
538 RADIUS_ATTR_WLAN_HESSID) &&
539 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
540 (u8 *) buf, os_strlen(buf))) {
541 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
542 return -1;
543 }
544 }
545#endif /* CONFIG_INTERWORKING */
546
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700547 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
548 return -1;
549
550 for (attr = req_attr; attr; attr = attr->next) {
551 if (!radius_msg_add_attr(msg, attr->type,
552 wpabuf_head(attr->val),
553 wpabuf_len(attr->val))) {
554 wpa_printf(MSG_ERROR, "Could not add RADIUS "
555 "attribute");
556 return -1;
557 }
558 }
559
560 return 0;
561}
562
563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
565 struct sta_info *sta,
566 const u8 *eap, size_t len)
567{
568 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 struct eapol_state_machine *sm = sta->eapol_sm;
570
571 if (sm == NULL)
572 return;
573
574 ieee802_1x_learn_identity(hapd, sm, eap, len);
575
576 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
577 "packet");
578
579 sm->radius_identifier = radius_client_get_id(hapd->radius);
580 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
581 sm->radius_identifier);
582 if (msg == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800583 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 return;
585 }
586
587 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
588
589 if (sm->identity &&
590 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
591 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800592 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 goto fail;
594 }
595
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700596 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
597 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599
600 /* TODO: should probably check MTU from driver config; 2304 is max for
601 * IEEE 802.11, but use 1400 to avoid problems with too large packets
602 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
604 RADIUS_ATTR_FRAMED_MTU) &&
605 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800606 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 goto fail;
608 }
609
Dmitry Shmidt41712582015-06-29 11:02:15 -0700610 if (!radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800611 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 goto fail;
613 }
614
615 /* State attribute must be copied if and only if this packet is
616 * Access-Request reply to the previous Access-Challenge */
617 if (sm->last_recv_radius &&
618 radius_msg_get_hdr(sm->last_recv_radius)->code ==
619 RADIUS_CODE_ACCESS_CHALLENGE) {
620 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
621 RADIUS_ATTR_STATE);
622 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800623 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624 goto fail;
625 }
626 if (res > 0) {
627 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
628 }
629 }
630
Dmitry Shmidt04949592012-07-19 12:16:46 -0700631 if (hapd->conf->radius_request_cui) {
632 const u8 *cui;
633 size_t cui_len;
634 /* Add previously learned CUI or nul CUI to request CUI */
635 if (sm->radius_cui) {
636 cui = wpabuf_head(sm->radius_cui);
637 cui_len = wpabuf_len(sm->radius_cui);
638 } else {
639 cui = (const u8 *) "\0";
640 cui_len = 1;
641 }
642 if (!radius_msg_add_attr(msg,
643 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
644 cui, cui_len)) {
645 wpa_printf(MSG_ERROR, "Could not add CUI");
646 goto fail;
647 }
648 }
649
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800650#ifdef CONFIG_HS20
651 if (hapd->conf->hs20) {
652 u8 ver = 1; /* Release 2 */
653 if (!radius_msg_add_wfa(
654 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
655 &ver, 1)) {
656 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
657 "version");
658 goto fail;
659 }
660
661 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
662 const u8 *pos;
663 u8 buf[3];
664 u16 id;
665 pos = wpabuf_head_u8(sta->hs20_ie);
666 buf[0] = (*pos) >> 4;
667 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
668 wpabuf_len(sta->hs20_ie) >= 3)
669 id = WPA_GET_LE16(pos + 1);
670 else
671 id = 0;
672 WPA_PUT_BE16(buf + 1, id);
673 if (!radius_msg_add_wfa(
674 msg,
675 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
676 buf, sizeof(buf))) {
677 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
678 "STA version");
679 goto fail;
680 }
681 }
682 }
683#endif /* CONFIG_HS20 */
684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
686 goto fail;
687
688 return;
689
690 fail:
691 radius_msg_free(msg);
692}
693#endif /* CONFIG_NO_RADIUS */
694
695
696static void handle_eap_response(struct hostapd_data *hapd,
697 struct sta_info *sta, struct eap_hdr *eap,
698 size_t len)
699{
700 u8 type, *data;
701 struct eapol_state_machine *sm = sta->eapol_sm;
702 if (sm == NULL)
703 return;
704
705 data = (u8 *) (eap + 1);
706
707 if (len < sizeof(*eap) + 1) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800708 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 return;
710 }
711
712 sm->eap_type_supp = type = data[0];
713
714 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
715 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
716 "id=%d len=%d) from STA: EAP Response-%s (%d)",
717 eap->code, eap->identifier, be_to_host16(eap->length),
718 eap_server_get_name(0, type), type);
719
720 sm->dot1xAuthEapolRespFramesRx++;
721
722 wpabuf_free(sm->eap_if->eapRespData);
723 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
724 sm->eapolEap = TRUE;
725}
726
727
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800728static void handle_eap_initiate(struct hostapd_data *hapd,
729 struct sta_info *sta, struct eap_hdr *eap,
730 size_t len)
731{
732#ifdef CONFIG_ERP
733 u8 type, *data;
734 struct eapol_state_machine *sm = sta->eapol_sm;
735
736 if (sm == NULL)
737 return;
738
739 if (len < sizeof(*eap) + 1) {
740 wpa_printf(MSG_INFO,
741 "handle_eap_initiate: too short response data");
742 return;
743 }
744
745 data = (u8 *) (eap + 1);
746 type = data[0];
747
748 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
749 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
750 "id=%d len=%d) from STA: EAP Initiate type %u",
751 eap->code, eap->identifier, be_to_host16(eap->length),
752 type);
753
754 wpabuf_free(sm->eap_if->eapRespData);
755 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
756 sm->eapolEap = TRUE;
757#endif /* CONFIG_ERP */
758}
759
760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761/* Process incoming EAP packet from Supplicant */
762static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
763 u8 *buf, size_t len)
764{
765 struct eap_hdr *eap;
766 u16 eap_len;
767
768 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800769 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 return;
771 }
772
773 eap = (struct eap_hdr *) buf;
774
775 eap_len = be_to_host16(eap->length);
776 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
777 eap->code, eap->identifier, eap_len);
778 if (eap_len < sizeof(*eap)) {
779 wpa_printf(MSG_DEBUG, " Invalid EAP length");
780 return;
781 } else if (eap_len > len) {
782 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
783 "packet");
784 return;
785 } else if (eap_len < len) {
786 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
787 "packet", (unsigned long) len - eap_len);
788 }
789
790 switch (eap->code) {
791 case EAP_CODE_REQUEST:
792 wpa_printf(MSG_DEBUG, " (request)");
793 return;
794 case EAP_CODE_RESPONSE:
795 wpa_printf(MSG_DEBUG, " (response)");
796 handle_eap_response(hapd, sta, eap, eap_len);
797 break;
798 case EAP_CODE_SUCCESS:
799 wpa_printf(MSG_DEBUG, " (success)");
800 return;
801 case EAP_CODE_FAILURE:
802 wpa_printf(MSG_DEBUG, " (failure)");
803 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800804 case EAP_CODE_INITIATE:
805 wpa_printf(MSG_DEBUG, " (initiate)");
806 handle_eap_initiate(hapd, sta, eap, eap_len);
807 break;
808 case EAP_CODE_FINISH:
809 wpa_printf(MSG_DEBUG, " (finish)");
810 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 default:
812 wpa_printf(MSG_DEBUG, " (unknown code)");
813 return;
814 }
815}
816
817
818static struct eapol_state_machine *
819ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
820{
821 int flags = 0;
822 if (sta->flags & WLAN_STA_PREAUTH)
823 flags |= EAPOL_SM_PREAUTH;
824 if (sta->wpa_sm) {
825 flags |= EAPOL_SM_USES_WPA;
826 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
827 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
828 }
829 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700830 sta->wps_ie, sta->p2p_ie, sta,
831 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832}
833
834
835/**
836 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
837 * @hapd: hostapd BSS data
838 * @sa: Source address (sender of the EAPOL frame)
839 * @buf: EAPOL frame
840 * @len: Length of buf in octets
841 *
842 * This function is called for each incoming EAPOL frame from the interface
843 */
844void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
845 size_t len)
846{
847 struct sta_info *sta;
848 struct ieee802_1x_hdr *hdr;
849 struct ieee802_1x_eapol_key *key;
850 u16 datalen;
851 struct rsn_pmksa_cache_entry *pmksa;
852 int key_mgmt;
853
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800854 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 !hapd->conf->wps_state)
856 return;
857
858 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
859 (unsigned long) len, MAC2STR(sa));
860 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800861 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
862 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
864 "associated/Pre-authenticating STA");
865 return;
866 }
867
868 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800869 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 return;
871 }
872
873 hdr = (struct ieee802_1x_hdr *) buf;
874 datalen = be_to_host16(hdr->length);
875 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
876 hdr->version, hdr->type, datalen);
877
878 if (len - sizeof(*hdr) < datalen) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800879 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (sta->eapol_sm)
881 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
882 return;
883 }
884 if (len - sizeof(*hdr) > datalen) {
885 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
886 "IEEE 802.1X packet",
887 (unsigned long) len - sizeof(*hdr) - datalen);
888 }
889
890 if (sta->eapol_sm) {
891 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
892 sta->eapol_sm->dot1xAuthEapolFramesRx++;
893 }
894
895 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
896 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
897 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
898 (key->type == EAPOL_KEY_TYPE_WPA ||
899 key->type == EAPOL_KEY_TYPE_RSN)) {
900 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
901 sizeof(*hdr) + datalen);
902 return;
903 }
904
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800905 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
907 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
908 "802.1X not enabled and WPS not used");
909 return;
910 }
911
912 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
913 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
914 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
915 "STA is using PSK");
916 return;
917 }
918
919 if (!sta->eapol_sm) {
920 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
921 if (!sta->eapol_sm)
922 return;
923
924#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800925 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800926 u32 wflags = sta->flags & (WLAN_STA_WPS |
927 WLAN_STA_WPS2 |
928 WLAN_STA_MAYBE_WPS);
929 if (wflags == WLAN_STA_MAYBE_WPS ||
930 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
931 /*
932 * Delay EAPOL frame transmission until a
933 * possible WPS STA initiates the handshake
934 * with EAPOL-Start. Only allow the wait to be
935 * skipped if the STA is known to support WPS
936 * 2.0.
937 */
938 wpa_printf(MSG_DEBUG, "WPS: Do not start "
939 "EAPOL until EAPOL-Start is "
940 "received");
941 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 }
944#endif /* CONFIG_WPS */
945
946 sta->eapol_sm->eap_if->portEnabled = TRUE;
947 }
948
949 /* since we support version 1, we can ignore version field and proceed
950 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
951 /* TODO: actually, we are not version 1 anymore.. However, Version 2
952 * does not change frame contents, so should be ok to process frames
953 * more or less identically. Some changes might be needed for
954 * verification of fields. */
955
956 switch (hdr->type) {
957 case IEEE802_1X_TYPE_EAP_PACKET:
958 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
959 break;
960
961 case IEEE802_1X_TYPE_EAPOL_START:
962 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
963 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
964 "from STA");
965 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
966 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
967 if (pmksa) {
968 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
969 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
970 "available - ignore it since "
971 "STA sent EAPOL-Start");
972 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
973 }
974 sta->eapol_sm->eapolStart = TRUE;
975 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
976 eap_server_clear_identity(sta->eapol_sm->eap);
977 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
978 break;
979
980 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
981 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
982 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
983 "from STA");
984 sta->acct_terminate_cause =
985 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
986 accounting_sta_stop(hapd, sta);
987 sta->eapol_sm->eapolLogoff = TRUE;
988 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
989 eap_server_clear_identity(sta->eapol_sm->eap);
990 break;
991
992 case IEEE802_1X_TYPE_EAPOL_KEY:
993 wpa_printf(MSG_DEBUG, " EAPOL-Key");
994 if (!ap_sta_is_authorized(sta)) {
995 wpa_printf(MSG_DEBUG, " Dropped key data from "
996 "unauthorized Supplicant");
997 break;
998 }
999 break;
1000
1001 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1002 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1003 /* TODO: implement support for this; show data */
1004 break;
1005
1006 default:
1007 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1008 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1009 break;
1010 }
1011
1012 eapol_auth_step(sta->eapol_sm);
1013}
1014
1015
1016/**
1017 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1018 * @hapd: hostapd BSS data
1019 * @sta: The station
1020 *
1021 * This function is called to start IEEE 802.1X authentication when a new
1022 * station completes IEEE 802.11 association.
1023 */
1024void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1025{
1026 struct rsn_pmksa_cache_entry *pmksa;
1027 int reassoc = 1;
1028 int force_1x = 0;
1029 int key_mgmt;
1030
1031#ifdef CONFIG_WPS
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001032 if (hapd->conf->wps_state &&
1033 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1034 (sta->flags & WLAN_STA_WPS))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 /*
1036 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1037 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1038 * authentication in this BSS.
1039 */
1040 force_1x = 1;
1041 }
1042#endif /* CONFIG_WPS */
1043
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001044 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1046 "802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047 /*
1048 * Clear any possible EAPOL authenticator state to support
1049 * reassociation change from WPS to PSK.
1050 */
1051 ieee802_1x_free_station(sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 return;
1053 }
1054
1055 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1056 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1057 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001058 /*
1059 * Clear any possible EAPOL authenticator state to support
1060 * reassociation change from WPA-EAP to PSK.
1061 */
1062 ieee802_1x_free_station(sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 return;
1064 }
1065
1066 if (sta->eapol_sm == NULL) {
1067 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1068 HOSTAPD_LEVEL_DEBUG, "start authentication");
1069 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1070 if (sta->eapol_sm == NULL) {
1071 hostapd_logger(hapd, sta->addr,
1072 HOSTAPD_MODULE_IEEE8021X,
1073 HOSTAPD_LEVEL_INFO,
1074 "failed to allocate state machine");
1075 return;
1076 }
1077 reassoc = 0;
1078 }
1079
1080#ifdef CONFIG_WPS
1081 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001082 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1083 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 * Delay EAPOL frame transmission until a possible WPS STA
1086 * initiates the handshake with EAPOL-Start. Only allow the
1087 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001089 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1090 "EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1092 }
1093#endif /* CONFIG_WPS */
1094
1095 sta->eapol_sm->eap_if->portEnabled = TRUE;
1096
1097#ifdef CONFIG_IEEE80211R
1098 if (sta->auth_alg == WLAN_AUTH_FT) {
1099 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1100 HOSTAPD_LEVEL_DEBUG,
1101 "PMK from FT - skip IEEE 802.1X/EAP");
1102 /* Setup EAPOL state machines to already authenticated state
1103 * because of existing FT information from R0KH. */
1104 sta->eapol_sm->keyRun = TRUE;
1105 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1106 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1107 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1108 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001109 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001110 sta->eapol_sm->portValid = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 if (sta->eapol_sm->eap)
1112 eap_sm_notify_cached(sta->eapol_sm->eap);
1113 /* TODO: get vlan_id from R0KH using RRB message */
1114 return;
1115 }
1116#endif /* CONFIG_IEEE80211R */
1117
1118 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1119 if (pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1121 HOSTAPD_LEVEL_DEBUG,
1122 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1123 /* Setup EAPOL state machines to already authenticated state
1124 * because of existing PMKSA information in the cache. */
1125 sta->eapol_sm->keyRun = TRUE;
1126 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1127 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1128 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1129 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001130 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 if (sta->eapol_sm->eap)
1132 eap_sm_notify_cached(sta->eapol_sm->eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001134 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 } else {
1136 if (reassoc) {
1137 /*
1138 * Force EAPOL state machines to start
1139 * re-authentication without having to wait for the
1140 * Supplicant to send EAPOL-Start.
1141 */
1142 sta->eapol_sm->reAuthenticate = TRUE;
1143 }
1144 eapol_auth_step(sta->eapol_sm);
1145 }
1146}
1147
1148
1149void ieee802_1x_free_station(struct sta_info *sta)
1150{
1151 struct eapol_state_machine *sm = sta->eapol_sm;
1152
1153 if (sm == NULL)
1154 return;
1155
1156 sta->eapol_sm = NULL;
1157
1158#ifndef CONFIG_NO_RADIUS
1159 radius_msg_free(sm->last_recv_radius);
1160 radius_free_class(&sm->radius_class);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001161 wpabuf_free(sm->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162#endif /* CONFIG_NO_RADIUS */
1163
1164 os_free(sm->identity);
1165 eapol_auth_free(sm);
1166}
1167
1168
1169#ifndef CONFIG_NO_RADIUS
1170static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1171 struct sta_info *sta)
1172{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001173 struct wpabuf *eap;
1174 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 int eap_type = -1;
1176 char buf[64];
1177 struct radius_msg *msg;
1178 struct eapol_state_machine *sm = sta->eapol_sm;
1179
1180 if (sm == NULL || sm->last_recv_radius == NULL) {
1181 if (sm)
1182 sm->eap_if->aaaEapNoReq = TRUE;
1183 return;
1184 }
1185
1186 msg = sm->last_recv_radius;
1187
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001188 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 if (eap == NULL) {
1190 /* RFC 3579, Chap. 2.6.3:
1191 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1192 * attribute */
1193 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1194 HOSTAPD_LEVEL_WARNING, "could not extract "
1195 "EAP-Message from RADIUS message");
1196 sm->eap_if->aaaEapNoReq = TRUE;
1197 return;
1198 }
1199
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001200 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1202 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1203 "received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001204 wpabuf_free(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 sm->eap_if->aaaEapNoReq = TRUE;
1206 return;
1207 }
1208
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001209 if (wpabuf_len(eap) > sizeof(*hdr))
1210 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001212 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 switch (hdr->code) {
1214 case EAP_CODE_REQUEST:
1215 if (eap_type >= 0)
1216 sm->eap_type_authsrv = eap_type;
1217 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001218 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 break;
1220 case EAP_CODE_RESPONSE:
1221 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001222 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 break;
1224 case EAP_CODE_SUCCESS:
1225 os_strlcpy(buf, "EAP Success", sizeof(buf));
1226 break;
1227 case EAP_CODE_FAILURE:
1228 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1229 break;
1230 default:
1231 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1232 break;
1233 }
1234 buf[sizeof(buf) - 1] = '\0';
1235 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1236 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1237 "id=%d len=%d) from RADIUS server: %s",
1238 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1239 buf);
1240 sm->eap_if->aaaEapReq = TRUE;
1241
1242 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001243 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244}
1245
1246
1247static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1248 struct sta_info *sta, struct radius_msg *msg,
1249 struct radius_msg *req,
1250 const u8 *shared_secret,
1251 size_t shared_secret_len)
1252{
1253 struct radius_ms_mppe_keys *keys;
1254 struct eapol_state_machine *sm = sta->eapol_sm;
1255 if (sm == NULL)
1256 return;
1257
1258 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1259 shared_secret_len);
1260
1261 if (keys && keys->send && keys->recv) {
1262 size_t len = keys->send_len + keys->recv_len;
1263 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1264 keys->send, keys->send_len);
1265 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1266 keys->recv, keys->recv_len);
1267
1268 os_free(sm->eap_if->aaaEapKeyData);
1269 sm->eap_if->aaaEapKeyData = os_malloc(len);
1270 if (sm->eap_if->aaaEapKeyData) {
1271 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1272 keys->recv_len);
1273 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1274 keys->send, keys->send_len);
1275 sm->eap_if->aaaEapKeyDataLen = len;
1276 sm->eap_if->aaaEapKeyAvailable = TRUE;
1277 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001278 } else {
1279 wpa_printf(MSG_DEBUG,
1280 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1281 keys, keys ? keys->send : NULL,
1282 keys ? keys->recv : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 }
1284
1285 if (keys) {
1286 os_free(keys->send);
1287 os_free(keys->recv);
1288 os_free(keys);
1289 }
1290}
1291
1292
1293static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1294 struct sta_info *sta,
1295 struct radius_msg *msg)
1296{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001297 u8 *attr_class;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 size_t class_len;
1299 struct eapol_state_machine *sm = sta->eapol_sm;
1300 int count, i;
1301 struct radius_attr_data *nclass;
1302 size_t nclass_count;
1303
1304 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1305 sm == NULL)
1306 return;
1307
1308 radius_free_class(&sm->radius_class);
1309 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1310 if (count <= 0)
1311 return;
1312
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001313 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 if (nclass == NULL)
1315 return;
1316
1317 nclass_count = 0;
1318
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001319 attr_class = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 for (i = 0; i < count; i++) {
1321 do {
1322 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001323 &attr_class, &class_len,
1324 attr_class) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 i = count;
1326 break;
1327 }
1328 } while (class_len < 1);
1329
1330 nclass[nclass_count].data = os_malloc(class_len);
1331 if (nclass[nclass_count].data == NULL)
1332 break;
1333
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001334 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 nclass[nclass_count].len = class_len;
1336 nclass_count++;
1337 }
1338
1339 sm->radius_class.attr = nclass;
1340 sm->radius_class.count = nclass_count;
1341 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1342 "attributes for " MACSTR,
1343 (unsigned long) sm->radius_class.count,
1344 MAC2STR(sta->addr));
1345}
1346
1347
1348/* Update sta->identity based on User-Name attribute in Access-Accept */
1349static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1350 struct sta_info *sta,
1351 struct radius_msg *msg)
1352{
1353 u8 *buf, *identity;
1354 size_t len;
1355 struct eapol_state_machine *sm = sta->eapol_sm;
1356
1357 if (sm == NULL)
1358 return;
1359
1360 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1361 NULL) < 0)
1362 return;
1363
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001364 identity = (u8 *) dup_binstr(buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 if (identity == NULL)
1366 return;
1367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1369 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1370 "User-Name from Access-Accept '%s'",
1371 sm->identity ? (char *) sm->identity : "N/A",
1372 (char *) identity);
1373
1374 os_free(sm->identity);
1375 sm->identity = identity;
1376 sm->identity_len = len;
1377}
1378
1379
Dmitry Shmidt04949592012-07-19 12:16:46 -07001380/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1381static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1382 struct sta_info *sta,
1383 struct radius_msg *msg)
1384{
1385 struct eapol_state_machine *sm = sta->eapol_sm;
1386 struct wpabuf *cui;
1387 u8 *buf;
1388 size_t len;
1389
1390 if (sm == NULL)
1391 return;
1392
1393 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1394 &buf, &len, NULL) < 0)
1395 return;
1396
1397 cui = wpabuf_alloc_copy(buf, len);
1398 if (cui == NULL)
1399 return;
1400
1401 wpabuf_free(sm->radius_cui);
1402 sm->radius_cui = cui;
1403}
1404
1405
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001406#ifdef CONFIG_HS20
1407
1408static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1409{
1410 sta->remediation = 1;
1411 os_free(sta->remediation_url);
1412 if (len > 2) {
1413 sta->remediation_url = os_malloc(len);
1414 if (!sta->remediation_url)
1415 return;
1416 sta->remediation_method = pos[0];
1417 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1418 sta->remediation_url[len - 1] = '\0';
1419 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1420 "for " MACSTR " - server method %u URL %s",
1421 MAC2STR(sta->addr), sta->remediation_method,
1422 sta->remediation_url);
1423 } else {
1424 sta->remediation_url = NULL;
1425 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1426 "for " MACSTR, MAC2STR(sta->addr));
1427 }
1428 /* TODO: assign the STA into remediation VLAN or add filtering */
1429}
1430
1431
1432static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1433 struct sta_info *sta, u8 *pos,
1434 size_t len)
1435{
1436 if (len < 3)
1437 return; /* Malformed information */
1438 sta->hs20_deauth_requested = 1;
1439 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1440 "Re-auth Delay %u",
1441 *pos, WPA_GET_LE16(pos + 1));
1442 wpabuf_free(sta->hs20_deauth_req);
1443 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1444 if (sta->hs20_deauth_req) {
1445 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1446 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1447 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1448 }
1449 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1450}
1451
1452
1453static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1454 struct sta_info *sta, u8 *pos,
1455 size_t len, int session_timeout)
1456{
1457 unsigned int swt;
1458 int warning_time, beacon_int;
1459
1460 if (len < 1)
1461 return; /* Malformed information */
1462 os_free(sta->hs20_session_info_url);
1463 sta->hs20_session_info_url = os_malloc(len);
1464 if (sta->hs20_session_info_url == NULL)
1465 return;
1466 swt = pos[0];
1467 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1468 sta->hs20_session_info_url[len - 1] = '\0';
1469 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1470 "(session_timeout=%d)",
1471 sta->hs20_session_info_url, swt, session_timeout);
1472 if (session_timeout < 0) {
1473 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1474 return;
1475 }
1476 if (swt == 255)
1477 swt = 1; /* Use one minute as the AP selected value */
1478
1479 if ((unsigned int) session_timeout < swt * 60)
1480 warning_time = 0;
1481 else
1482 warning_time = session_timeout - swt * 60;
1483
1484 beacon_int = hapd->iconf->beacon_int;
1485 if (beacon_int < 1)
1486 beacon_int = 100; /* best guess */
1487 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1488 if (sta->hs20_disassoc_timer > 65535)
1489 sta->hs20_disassoc_timer = 65535;
1490
1491 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1492}
1493
1494#endif /* CONFIG_HS20 */
1495
1496
1497static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1498 struct sta_info *sta,
1499 struct radius_msg *msg,
1500 int session_timeout)
1501{
1502#ifdef CONFIG_HS20
1503 u8 *buf, *pos, *end, type, sublen;
1504 size_t len;
1505
1506 buf = NULL;
1507 sta->remediation = 0;
1508 sta->hs20_deauth_requested = 0;
1509
1510 for (;;) {
1511 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1512 &buf, &len, buf) < 0)
1513 break;
1514 if (len < 6)
1515 continue;
1516 pos = buf;
1517 end = buf + len;
1518 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1519 continue;
1520 pos += 4;
1521
1522 type = *pos++;
1523 sublen = *pos++;
1524 if (sublen < 2)
1525 continue; /* invalid length */
1526 sublen -= 2; /* skip header */
1527 if (pos + sublen > end)
1528 continue; /* invalid WFA VSA */
1529
1530 switch (type) {
1531 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1532 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1533 break;
1534 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1535 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1536 break;
1537 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1538 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1539 session_timeout);
1540 break;
1541 }
1542 }
1543#endif /* CONFIG_HS20 */
1544}
1545
1546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547struct sta_id_search {
1548 u8 identifier;
1549 struct eapol_state_machine *sm;
1550};
1551
1552
1553static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1554 struct sta_info *sta,
1555 void *ctx)
1556{
1557 struct sta_id_search *id_search = ctx;
1558 struct eapol_state_machine *sm = sta->eapol_sm;
1559
1560 if (sm && sm->radius_identifier >= 0 &&
1561 sm->radius_identifier == id_search->identifier) {
1562 id_search->sm = sm;
1563 return 1;
1564 }
1565 return 0;
1566}
1567
1568
1569static struct eapol_state_machine *
1570ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1571{
1572 struct sta_id_search id_search;
1573 id_search.identifier = identifier;
1574 id_search.sm = NULL;
1575 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1576 return id_search.sm;
1577}
1578
1579
1580/**
1581 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1582 * @msg: RADIUS response message
1583 * @req: RADIUS request message
1584 * @shared_secret: RADIUS shared secret
1585 * @shared_secret_len: Length of shared_secret in octets
1586 * @data: Context data (struct hostapd_data *)
1587 * Returns: Processing status
1588 */
1589static RadiusRxResult
1590ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1591 const u8 *shared_secret, size_t shared_secret_len,
1592 void *data)
1593{
1594 struct hostapd_data *hapd = data;
1595 struct sta_info *sta;
1596 u32 session_timeout = 0, termination_action, acct_interim_interval;
Dmitry Shmidt83474442015-04-15 13:47:09 -07001597 int session_timeout_set, vlan_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 struct eapol_state_machine *sm;
1599 int override_eapReq = 0;
1600 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1601
1602 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1603 if (sm == NULL) {
1604 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1605 "station for this RADIUS message");
1606 return RADIUS_RX_UNKNOWN;
1607 }
1608 sta = sm->sta;
1609
1610 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1611 * present when packet contains an EAP-Message attribute */
1612 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1613 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1614 0) < 0 &&
1615 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1616 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1617 "Message-Authenticator since it does not include "
1618 "EAP-Message");
1619 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1620 req, 1)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001621 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 return RADIUS_RX_INVALID_AUTHENTICATOR;
1623 }
1624
1625 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1626 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1627 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001628 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 return RADIUS_RX_UNKNOWN;
1630 }
1631
1632 sm->radius_identifier = -1;
1633 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1634 MAC2STR(sta->addr));
1635
1636 radius_msg_free(sm->last_recv_radius);
1637 sm->last_recv_radius = msg;
1638
1639 session_timeout_set =
1640 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1641 &session_timeout);
1642 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1643 &termination_action))
1644 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1645
1646 if (hapd->conf->acct_interim_interval == 0 &&
1647 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1648 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1649 &acct_interim_interval) == 0) {
1650 if (acct_interim_interval < 60) {
1651 hostapd_logger(hapd, sta->addr,
1652 HOSTAPD_MODULE_IEEE8021X,
1653 HOSTAPD_LEVEL_INFO,
1654 "ignored too small "
1655 "Acct-Interim-Interval %d",
1656 acct_interim_interval);
1657 } else
1658 sta->acct_interim_interval = acct_interim_interval;
1659 }
1660
1661
1662 switch (hdr->code) {
1663 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001664 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
Dmitry Shmidt83474442015-04-15 13:47:09 -07001665 vlan_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666#ifndef CONFIG_NO_VLAN
Dmitry Shmidt83474442015-04-15 13:47:09 -07001667 else
1668 vlan_id = radius_msg_get_vlanid(msg);
1669 if (vlan_id > 0 &&
1670 hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 hostapd_logger(hapd, sta->addr,
1672 HOSTAPD_MODULE_RADIUS,
1673 HOSTAPD_LEVEL_INFO,
Dmitry Shmidt83474442015-04-15 13:47:09 -07001674 "VLAN ID %d", vlan_id);
1675 } else if (vlan_id > 0) {
1676 sta->eapol_sm->authFail = TRUE;
1677 hostapd_logger(hapd, sta->addr,
1678 HOSTAPD_MODULE_RADIUS,
1679 HOSTAPD_LEVEL_INFO,
1680 "Invalid VLAN ID %d received from RADIUS server",
1681 vlan_id);
1682 break;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001683 } else if (hapd->conf->ssid.dynamic_vlan ==
1684 DYNAMIC_VLAN_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 sta->eapol_sm->authFail = TRUE;
1686 hostapd_logger(hapd, sta->addr,
1687 HOSTAPD_MODULE_IEEE8021X,
1688 HOSTAPD_LEVEL_INFO, "authentication "
1689 "server did not include required VLAN "
1690 "ID in Access-Accept");
1691 break;
1692 }
1693#endif /* CONFIG_NO_VLAN */
1694
Dmitry Shmidt83474442015-04-15 13:47:09 -07001695 sta->vlan_id = vlan_id;
1696 if ((sta->flags & WLAN_STA_ASSOC) &&
1697 ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 break;
1699
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001700 sta->session_timeout_set = !!session_timeout_set;
1701 sta->session_timeout = session_timeout;
1702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 /* RFC 3580, Ch. 3.17 */
1704 if (session_timeout_set && termination_action ==
1705 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1706 sm->reAuthPeriod = session_timeout;
1707 } else if (session_timeout_set)
1708 ap_sta_session_timeout(hapd, sta, session_timeout);
1709
1710 sm->eap_if->aaaSuccess = TRUE;
1711 override_eapReq = 1;
1712 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1713 shared_secret_len);
1714 ieee802_1x_store_radius_class(hapd, sta, msg);
1715 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001716 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001717 ieee802_1x_check_hs20(hapd, sta, msg,
1718 session_timeout_set ?
1719 (int) session_timeout : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 break;
1721 case RADIUS_CODE_ACCESS_REJECT:
1722 sm->eap_if->aaaFail = TRUE;
1723 override_eapReq = 1;
1724 break;
1725 case RADIUS_CODE_ACCESS_CHALLENGE:
1726 sm->eap_if->aaaEapReq = TRUE;
1727 if (session_timeout_set) {
1728 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1729 sm->eap_if->aaaMethodTimeout = session_timeout;
1730 hostapd_logger(hapd, sm->addr,
1731 HOSTAPD_MODULE_IEEE8021X,
1732 HOSTAPD_LEVEL_DEBUG,
1733 "using EAP timeout of %d seconds (from "
1734 "RADIUS)",
1735 sm->eap_if->aaaMethodTimeout);
1736 } else {
1737 /*
1738 * Use dynamic retransmission behavior per EAP
1739 * specification.
1740 */
1741 sm->eap_if->aaaMethodTimeout = 0;
1742 }
1743 break;
1744 }
1745
1746 ieee802_1x_decapsulate_radius(hapd, sta);
1747 if (override_eapReq)
1748 sm->eap_if->aaaEapReq = FALSE;
1749
1750 eapol_auth_step(sm);
1751
1752 return RADIUS_RX_QUEUED;
1753}
1754#endif /* CONFIG_NO_RADIUS */
1755
1756
1757void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1758{
1759 struct eapol_state_machine *sm = sta->eapol_sm;
1760 if (sm == NULL)
1761 return;
1762
1763 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1764 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1765
1766#ifndef CONFIG_NO_RADIUS
1767 radius_msg_free(sm->last_recv_radius);
1768 sm->last_recv_radius = NULL;
1769#endif /* CONFIG_NO_RADIUS */
1770
1771 if (sm->eap_if->eapTimeout) {
1772 /*
1773 * Disconnect the STA since it did not reply to the last EAP
1774 * request and we cannot continue EAP processing (EAP-Failure
1775 * could only be sent if the EAP peer actually replied).
1776 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001777 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1778 MAC2STR(sta->addr));
1779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 sm->eap_if->portEnabled = FALSE;
1781 ap_sta_disconnect(hapd, sta, sta->addr,
1782 WLAN_REASON_PREV_AUTH_NOT_VALID);
1783 }
1784}
1785
1786
1787static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1788{
1789 struct eapol_authenticator *eapol = hapd->eapol_auth;
1790
1791 if (hapd->conf->default_wep_key_len < 1)
1792 return 0;
1793
1794 os_free(eapol->default_wep_key);
1795 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1796 if (eapol->default_wep_key == NULL ||
1797 random_get_bytes(eapol->default_wep_key,
1798 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001799 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800 os_free(eapol->default_wep_key);
1801 eapol->default_wep_key = NULL;
1802 return -1;
1803 }
1804
1805 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1806 eapol->default_wep_key,
1807 hapd->conf->default_wep_key_len);
1808
1809 return 0;
1810}
1811
1812
1813static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1814 struct sta_info *sta, void *ctx)
1815{
1816 if (sta->eapol_sm) {
1817 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1818 eapol_auth_step(sta->eapol_sm);
1819 }
1820 return 0;
1821}
1822
1823
1824static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1825{
1826 struct hostapd_data *hapd = eloop_ctx;
1827 struct eapol_authenticator *eapol = hapd->eapol_auth;
1828
1829 if (eapol->default_wep_key_idx >= 3)
1830 eapol->default_wep_key_idx =
1831 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1832 else
1833 eapol->default_wep_key_idx++;
1834
1835 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1836 eapol->default_wep_key_idx);
1837
1838 if (ieee802_1x_rekey_broadcast(hapd)) {
1839 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1840 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1841 "new broadcast key");
1842 os_free(eapol->default_wep_key);
1843 eapol->default_wep_key = NULL;
1844 return;
1845 }
1846
1847 /* TODO: Could setup key for RX here, but change default TX keyid only
1848 * after new broadcast key has been sent to all stations. */
1849 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1850 broadcast_ether_addr,
1851 eapol->default_wep_key_idx, 1, NULL, 0,
1852 eapol->default_wep_key,
1853 hapd->conf->default_wep_key_len)) {
1854 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1855 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1856 "new broadcast key");
1857 os_free(eapol->default_wep_key);
1858 eapol->default_wep_key = NULL;
1859 return;
1860 }
1861
1862 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1863
1864 if (hapd->conf->wep_rekeying_period > 0) {
1865 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1866 ieee802_1x_rekey, hapd, NULL);
1867 }
1868}
1869
1870
1871static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1872 const u8 *data, size_t datalen)
1873{
1874#ifdef CONFIG_WPS
1875 struct sta_info *sta = sta_ctx;
1876
1877 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1878 WLAN_STA_MAYBE_WPS) {
1879 const u8 *identity;
1880 size_t identity_len;
1881 struct eapol_state_machine *sm = sta->eapol_sm;
1882
1883 identity = eap_get_identity(sm->eap, &identity_len);
1884 if (identity &&
1885 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1886 os_memcmp(identity, WSC_ID_ENROLLEE,
1887 WSC_ID_ENROLLEE_LEN) == 0) ||
1888 (identity_len == WSC_ID_REGISTRAR_LEN &&
1889 os_memcmp(identity, WSC_ID_REGISTRAR,
1890 WSC_ID_REGISTRAR_LEN) == 0))) {
1891 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1892 "WLAN_STA_WPS");
1893 sta->flags |= WLAN_STA_WPS;
1894 }
1895 }
1896#endif /* CONFIG_WPS */
1897
1898 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1899}
1900
1901
1902static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1903 const u8 *data, size_t datalen)
1904{
1905#ifndef CONFIG_NO_RADIUS
1906 struct hostapd_data *hapd = ctx;
1907 struct sta_info *sta = sta_ctx;
1908
1909 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1910#endif /* CONFIG_NO_RADIUS */
1911}
1912
1913
1914static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001915 int preauth, int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916{
1917 struct hostapd_data *hapd = ctx;
1918 struct sta_info *sta = sta_ctx;
1919 if (preauth)
1920 rsn_preauth_finished(hapd, sta, success);
1921 else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001922 ieee802_1x_finished(hapd, sta, success, remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923}
1924
1925
1926static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1927 size_t identity_len, int phase2,
1928 struct eap_user *user)
1929{
1930 struct hostapd_data *hapd = ctx;
1931 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001932 int i;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001933 int rv = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001935 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 if (eap_user == NULL)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001937 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938
1939 os_memset(user, 0, sizeof(*user));
1940 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001941 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 user->methods[i].vendor = eap_user->methods[i].vendor;
1943 user->methods[i].method = eap_user->methods[i].method;
1944 }
1945
1946 if (eap_user->password) {
1947 user->password = os_malloc(eap_user->password_len);
1948 if (user->password == NULL)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001949 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 os_memcpy(user->password, eap_user->password,
1951 eap_user->password_len);
1952 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001953 user->password_hash = eap_user->password_hash;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 }
1955 user->force_version = eap_user->force_version;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001956 user->macacl = eap_user->macacl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001958 user->remediation = eap_user->remediation;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001959 rv = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001961out:
1962 if (rv)
1963 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
1964
1965 return rv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966}
1967
1968
1969static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1970{
1971 struct hostapd_data *hapd = ctx;
1972 struct sta_info *sta;
1973 sta = ap_get_sta(hapd, addr);
1974 if (sta == NULL || sta->eapol_sm == NULL)
1975 return 0;
1976 return 1;
1977}
1978
1979
1980static void ieee802_1x_logger(void *ctx, const u8 *addr,
1981 eapol_logger_level level, const char *txt)
1982{
1983#ifndef CONFIG_NO_HOSTAPD_LOGGER
1984 struct hostapd_data *hapd = ctx;
1985 int hlevel;
1986
1987 switch (level) {
1988 case EAPOL_LOGGER_WARNING:
1989 hlevel = HOSTAPD_LEVEL_WARNING;
1990 break;
1991 case EAPOL_LOGGER_INFO:
1992 hlevel = HOSTAPD_LEVEL_INFO;
1993 break;
1994 case EAPOL_LOGGER_DEBUG:
1995 default:
1996 hlevel = HOSTAPD_LEVEL_DEBUG;
1997 break;
1998 }
1999
2000 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2001 txt);
2002#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2003}
2004
2005
2006static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2007 int authorized)
2008{
2009 struct hostapd_data *hapd = ctx;
2010 struct sta_info *sta = sta_ctx;
2011 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2012}
2013
2014
2015static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2016{
2017 struct hostapd_data *hapd = ctx;
2018 struct sta_info *sta = sta_ctx;
2019 ieee802_1x_abort_auth(hapd, sta);
2020}
2021
2022
2023static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2024{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002025#ifndef CONFIG_FIPS
2026#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 struct hostapd_data *hapd = ctx;
2028 struct sta_info *sta = sta_ctx;
2029 ieee802_1x_tx_key(hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002030#endif /* CONFIG_NO_RC4 */
2031#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032}
2033
2034
2035static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2036 enum eapol_event type)
2037{
2038 /* struct hostapd_data *hapd = ctx; */
2039 struct sta_info *sta = sta_ctx;
2040 switch (type) {
2041 case EAPOL_AUTH_SM_CHANGE:
2042 wpa_auth_sm_notify(sta->wpa_sm);
2043 break;
2044 case EAPOL_AUTH_REAUTHENTICATE:
2045 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2046 break;
2047 }
2048}
2049
2050
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002051#ifdef CONFIG_ERP
2052
2053static struct eap_server_erp_key *
2054ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2055{
2056 struct hostapd_data *hapd = ctx;
2057 struct eap_server_erp_key *erp;
2058
2059 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2060 list) {
2061 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2062 return erp;
2063 }
2064
2065 return NULL;
2066}
2067
2068
2069static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2070{
2071 struct hostapd_data *hapd = ctx;
2072
2073 dl_list_add(&hapd->erp_keys, &erp->list);
2074 return 0;
2075}
2076
2077#endif /* CONFIG_ERP */
2078
2079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080int ieee802_1x_init(struct hostapd_data *hapd)
2081{
2082 int i;
2083 struct eapol_auth_config conf;
2084 struct eapol_auth_cb cb;
2085
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002086 dl_list_init(&hapd->erp_keys);
2087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 os_memset(&conf, 0, sizeof(conf));
2089 conf.ctx = hapd;
2090 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2091 conf.wpa = hapd->conf->wpa;
2092 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2093 conf.eap_server = hapd->conf->eap_server;
2094 conf.ssl_ctx = hapd->ssl_ctx;
2095 conf.msg_ctx = hapd->msg_ctx;
2096 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2097 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2098 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002099 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2100 conf.erp_domain = hapd->conf->erp_domain;
2101 conf.erp = hapd->conf->eap_server_erp;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002102 conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2104 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2105 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2106 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2107 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2108 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2109 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2110 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2111 conf.tnc = hapd->conf->tnc;
2112 conf.wps = hapd->wps;
2113 conf.fragment_size = hapd->conf->fragment_size;
2114 conf.pwd_group = hapd->conf->pwd_group;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002115 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002116 if (hapd->conf->server_id) {
2117 conf.server_id = (const u8 *) hapd->conf->server_id;
2118 conf.server_id_len = os_strlen(hapd->conf->server_id);
2119 } else {
2120 conf.server_id = (const u8 *) "hostapd";
2121 conf.server_id_len = 7;
2122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123
2124 os_memset(&cb, 0, sizeof(cb));
2125 cb.eapol_send = ieee802_1x_eapol_send;
2126 cb.aaa_send = ieee802_1x_aaa_send;
2127 cb.finished = _ieee802_1x_finished;
2128 cb.get_eap_user = ieee802_1x_get_eap_user;
2129 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2130 cb.logger = ieee802_1x_logger;
2131 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2132 cb.abort_auth = _ieee802_1x_abort_auth;
2133 cb.tx_key = _ieee802_1x_tx_key;
2134 cb.eapol_event = ieee802_1x_eapol_event;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002135#ifdef CONFIG_ERP
2136 cb.erp_get_key = ieee802_1x_erp_get_key;
2137 cb.erp_add_key = ieee802_1x_erp_add_key;
2138#endif /* CONFIG_ERP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139
2140 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2141 if (hapd->eapol_auth == NULL)
2142 return -1;
2143
2144 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2145 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2146 return -1;
2147
2148#ifndef CONFIG_NO_RADIUS
2149 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2150 ieee802_1x_receive_auth, hapd))
2151 return -1;
2152#endif /* CONFIG_NO_RADIUS */
2153
2154 if (hapd->conf->default_wep_key_len) {
2155 for (i = 0; i < 4; i++)
2156 hostapd_drv_set_key(hapd->conf->iface, hapd,
2157 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2158 NULL, 0);
2159
2160 ieee802_1x_rekey(hapd, NULL);
2161
2162 if (hapd->eapol_auth->default_wep_key == NULL)
2163 return -1;
2164 }
2165
2166 return 0;
2167}
2168
2169
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002170void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2171{
2172 struct eap_server_erp_key *erp;
2173
2174 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2175 list)) != NULL) {
2176 dl_list_del(&erp->list);
2177 bin_clear_free(erp, sizeof(*erp));
2178 }
2179}
2180
2181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182void ieee802_1x_deinit(struct hostapd_data *hapd)
2183{
2184 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002186 if (hapd->driver && hapd->drv_priv &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2188 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2189
2190 eapol_auth_deinit(hapd->eapol_auth);
2191 hapd->eapol_auth = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002192
2193 ieee802_1x_erp_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194}
2195
2196
2197int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2198 const u8 *buf, size_t len, int ack)
2199{
2200 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 u8 *pos;
2202 const unsigned char rfc1042_hdr[ETH_ALEN] =
2203 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2204
2205 if (sta == NULL)
2206 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002207 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 return 0;
2209
2210 hdr = (struct ieee80211_hdr *) buf;
2211 pos = (u8 *) (hdr + 1);
2212 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2213 return 0;
2214 pos += sizeof(rfc1042_hdr);
2215 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2216 return 0;
2217 pos += 2;
2218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002219 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2220 ack);
2221}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002223
2224int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2225 const u8 *buf, int len, int ack)
2226{
2227 const struct ieee802_1x_hdr *xhdr =
2228 (const struct ieee802_1x_hdr *) buf;
2229 const u8 *pos = buf + sizeof(*xhdr);
2230 struct ieee802_1x_eapol_key *key;
2231
2232 if (len < (int) sizeof(*xhdr))
2233 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2235 "type=%d length=%d - ack=%d",
2236 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2237 be_to_host16(xhdr->length), ack);
2238
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002239 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2240 return 0;
2241
2242 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 const struct wpa_eapol_key *wpa;
2244 wpa = (const struct wpa_eapol_key *) pos;
2245 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2246 wpa->type == EAPOL_KEY_TYPE_WPA)
2247 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2248 sta->wpa_sm, ack);
2249 }
2250
2251 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2252 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002253 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002254 * packets couple of times because otherwise STA keys become
2255 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002256 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 key = (struct ieee802_1x_eapol_key *) pos;
2258 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2259 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2260 "frame (%scast index=%d)",
2261 key->key_index & BIT(7) ? "uni" : "broad",
2262 key->key_index & ~BIT(7));
2263 /* TODO: re-send EAPOL-Key couple of times (with short delay
2264 * between them?). If all attempt fail, report error and
2265 * deauthenticate STA so that it will get new keys when
2266 * authenticating again (e.g., after returning in range).
2267 * Separate limit/transmit state needed both for unicast and
2268 * broadcast keys(?) */
2269 }
2270 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2271 * to here and change the key only if the EAPOL-Key packet was Acked.
2272 */
2273
2274 return 1;
2275}
2276
2277
2278u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2279{
2280 if (sm == NULL || sm->identity == NULL)
2281 return NULL;
2282
2283 *len = sm->identity_len;
2284 return sm->identity;
2285}
2286
2287
2288u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2289 int idx)
2290{
2291 if (sm == NULL || sm->radius_class.attr == NULL ||
2292 idx >= (int) sm->radius_class.count)
2293 return NULL;
2294
2295 *len = sm->radius_class.attr[idx].len;
2296 return sm->radius_class.attr[idx].data;
2297}
2298
2299
Dmitry Shmidt04949592012-07-19 12:16:46 -07002300struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2301{
2302 if (sm == NULL)
2303 return NULL;
2304 return sm->radius_cui;
2305}
2306
2307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2309{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002310 *len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 if (sm == NULL)
2312 return NULL;
2313
2314 *len = sm->eap_if->eapKeyDataLen;
2315 return sm->eap_if->eapKeyData;
2316}
2317
2318
2319void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2320 int enabled)
2321{
2322 if (sm == NULL)
2323 return;
2324 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2325 eapol_auth_step(sm);
2326}
2327
2328
2329void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2330 int valid)
2331{
2332 if (sm == NULL)
2333 return;
2334 sm->portValid = valid ? TRUE : FALSE;
2335 eapol_auth_step(sm);
2336}
2337
2338
2339void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2340{
2341 if (sm == NULL)
2342 return;
2343 if (pre_auth)
2344 sm->flags |= EAPOL_SM_PREAUTH;
2345 else
2346 sm->flags &= ~EAPOL_SM_PREAUTH;
2347}
2348
2349
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002350static const char * bool_txt(Boolean val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002352 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353}
2354
2355
2356int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2357{
2358 /* TODO */
2359 return 0;
2360}
2361
2362
2363int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2364 char *buf, size_t buflen)
2365{
2366 int len = 0, ret;
2367 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002368 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002369 const char *name1;
2370 const char *name2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371
2372 if (sm == NULL)
2373 return 0;
2374
2375 ret = os_snprintf(buf + len, buflen - len,
2376 "dot1xPaePortNumber=%d\n"
2377 "dot1xPaePortProtocolVersion=%d\n"
2378 "dot1xPaePortCapabilities=1\n"
2379 "dot1xPaePortInitialize=%d\n"
2380 "dot1xPaePortReauthenticate=FALSE\n",
2381 sta->aid,
2382 EAPOL_VERSION,
2383 sm->initialize);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002384 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 return len;
2386 len += ret;
2387
2388 /* dot1xAuthConfigTable */
2389 ret = os_snprintf(buf + len, buflen - len,
2390 "dot1xAuthPaeState=%d\n"
2391 "dot1xAuthBackendAuthState=%d\n"
2392 "dot1xAuthAdminControlledDirections=%d\n"
2393 "dot1xAuthOperControlledDirections=%d\n"
2394 "dot1xAuthAuthControlledPortStatus=%d\n"
2395 "dot1xAuthAuthControlledPortControl=%d\n"
2396 "dot1xAuthQuietPeriod=%u\n"
2397 "dot1xAuthServerTimeout=%u\n"
2398 "dot1xAuthReAuthPeriod=%u\n"
2399 "dot1xAuthReAuthEnabled=%s\n"
2400 "dot1xAuthKeyTxEnabled=%s\n",
2401 sm->auth_pae_state + 1,
2402 sm->be_auth_state + 1,
2403 sm->adminControlledDirections,
2404 sm->operControlledDirections,
2405 sm->authPortStatus,
2406 sm->portControl,
2407 sm->quietPeriod,
2408 sm->serverTimeout,
2409 sm->reAuthPeriod,
2410 bool_txt(sm->reAuthEnabled),
2411 bool_txt(sm->keyTxEnabled));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002412 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 return len;
2414 len += ret;
2415
2416 /* dot1xAuthStatsTable */
2417 ret = os_snprintf(buf + len, buflen - len,
2418 "dot1xAuthEapolFramesRx=%u\n"
2419 "dot1xAuthEapolFramesTx=%u\n"
2420 "dot1xAuthEapolStartFramesRx=%u\n"
2421 "dot1xAuthEapolLogoffFramesRx=%u\n"
2422 "dot1xAuthEapolRespIdFramesRx=%u\n"
2423 "dot1xAuthEapolRespFramesRx=%u\n"
2424 "dot1xAuthEapolReqIdFramesTx=%u\n"
2425 "dot1xAuthEapolReqFramesTx=%u\n"
2426 "dot1xAuthInvalidEapolFramesRx=%u\n"
2427 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2428 "dot1xAuthLastEapolFrameVersion=%u\n"
2429 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2430 sm->dot1xAuthEapolFramesRx,
2431 sm->dot1xAuthEapolFramesTx,
2432 sm->dot1xAuthEapolStartFramesRx,
2433 sm->dot1xAuthEapolLogoffFramesRx,
2434 sm->dot1xAuthEapolRespIdFramesRx,
2435 sm->dot1xAuthEapolRespFramesRx,
2436 sm->dot1xAuthEapolReqIdFramesTx,
2437 sm->dot1xAuthEapolReqFramesTx,
2438 sm->dot1xAuthInvalidEapolFramesRx,
2439 sm->dot1xAuthEapLengthErrorFramesRx,
2440 sm->dot1xAuthLastEapolFrameVersion,
2441 MAC2STR(sm->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002442 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443 return len;
2444 len += ret;
2445
2446 /* dot1xAuthDiagTable */
2447 ret = os_snprintf(buf + len, buflen - len,
2448 "dot1xAuthEntersConnecting=%u\n"
2449 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2450 "dot1xAuthEntersAuthenticating=%u\n"
2451 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2452 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2453 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2454 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2455 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2456 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2457 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2458 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2459 "dot1xAuthBackendResponses=%u\n"
2460 "dot1xAuthBackendAccessChallenges=%u\n"
2461 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2462 "dot1xAuthBackendAuthSuccesses=%u\n"
2463 "dot1xAuthBackendAuthFails=%u\n",
2464 sm->authEntersConnecting,
2465 sm->authEapLogoffsWhileConnecting,
2466 sm->authEntersAuthenticating,
2467 sm->authAuthSuccessesWhileAuthenticating,
2468 sm->authAuthTimeoutsWhileAuthenticating,
2469 sm->authAuthFailWhileAuthenticating,
2470 sm->authAuthEapStartsWhileAuthenticating,
2471 sm->authAuthEapLogoffWhileAuthenticating,
2472 sm->authAuthReauthsWhileAuthenticated,
2473 sm->authAuthEapStartsWhileAuthenticated,
2474 sm->authAuthEapLogoffWhileAuthenticated,
2475 sm->backendResponses,
2476 sm->backendAccessChallenges,
2477 sm->backendOtherRequestsToSupplicant,
2478 sm->backendAuthSuccesses,
2479 sm->backendAuthFails);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002480 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 return len;
2482 len += ret;
2483
2484 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002485 os_reltime_age(&sta->acct_session_start, &diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 ret = os_snprintf(buf + len, buflen - len,
2487 /* TODO: dot1xAuthSessionOctetsRx */
2488 /* TODO: dot1xAuthSessionOctetsTx */
2489 /* TODO: dot1xAuthSessionFramesRx */
2490 /* TODO: dot1xAuthSessionFramesTx */
2491 "dot1xAuthSessionId=%08X-%08X\n"
2492 "dot1xAuthSessionAuthenticMethod=%d\n"
2493 "dot1xAuthSessionTime=%u\n"
2494 "dot1xAuthSessionTerminateCause=999\n"
2495 "dot1xAuthSessionUserName=%s\n",
2496 sta->acct_session_id_hi, sta->acct_session_id_lo,
2497 (wpa_key_mgmt_wpa_ieee8021x(
2498 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2499 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002500 (unsigned int) diff.sec,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 sm->identity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002502 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 return len;
2504 len += ret;
2505
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002506 if (sm->acct_multi_session_id_hi) {
2507 ret = os_snprintf(buf + len, buflen - len,
2508 "authMultiSessionId=%08X+%08X\n",
2509 sm->acct_multi_session_id_hi,
2510 sm->acct_multi_session_id_lo);
2511 if (os_snprintf_error(buflen - len, ret))
2512 return len;
2513 len += ret;
2514 }
2515
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002516 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2517 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002518 ret = os_snprintf(buf + len, buflen - len,
2519 "last_eap_type_as=%d (%s)\n"
2520 "last_eap_type_sta=%d (%s)\n",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002521 sm->eap_type_authsrv, name1,
2522 sm->eap_type_supp, name2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002523 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002524 return len;
2525 len += ret;
2526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 return len;
2528}
2529
2530
2531static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002532 struct sta_info *sta, int success,
2533 int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534{
2535 const u8 *key;
2536 size_t len;
2537 /* TODO: get PMKLifetime from WPA parameters */
2538 static const int dot11RSNAConfigPMKLifetime = 43200;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002539 unsigned int session_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002541#ifdef CONFIG_HS20
2542 if (remediation && !sta->remediation) {
2543 sta->remediation = 1;
2544 os_free(sta->remediation_url);
2545 sta->remediation_url =
2546 os_strdup(hapd->conf->subscr_remediation_url);
2547 sta->remediation_method = 1; /* SOAP-XML SPP */
2548 }
2549
2550 if (success) {
2551 if (sta->remediation) {
2552 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2553 "to " MACSTR " to indicate Subscription "
2554 "Remediation",
2555 MAC2STR(sta->addr));
2556 hs20_send_wnm_notification(hapd, sta->addr,
2557 sta->remediation_method,
2558 sta->remediation_url);
2559 os_free(sta->remediation_url);
2560 sta->remediation_url = NULL;
2561 }
2562
2563 if (sta->hs20_deauth_req) {
2564 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2565 "to " MACSTR " to indicate imminent "
2566 "deauthentication", MAC2STR(sta->addr));
2567 hs20_send_wnm_notification_deauth_req(
2568 hapd, sta->addr, sta->hs20_deauth_req);
2569 }
2570 }
2571#endif /* CONFIG_HS20 */
2572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002574 if (sta->session_timeout_set)
2575 session_timeout = sta->session_timeout;
2576 else
2577 session_timeout = dot11RSNAConfigPMKLifetime;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002578 if (success && key && len >= PMK_LEN && !sta->remediation &&
2579 !sta->hs20_deauth_requested &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002580 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 sta->eapol_sm) == 0) {
2582 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2583 HOSTAPD_LEVEL_DEBUG,
2584 "Added PMKSA cache entry (IEEE 802.1X)");
2585 }
2586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002587 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588 /*
2589 * Many devices require deauthentication after WPS provisioning
2590 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591 * disconnect the client here. In addition, this may also
2592 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2593 * the EAPOL PAE state machine would remain in HELD state for
2594 * considerable amount of time and some EAP methods, like
2595 * EAP-FAST with anonymous provisioning, may require another
2596 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002598 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2599 "disconnection after EAP-Failure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 /* Add a small sleep to increase likelihood of previously
2601 * requested EAP-Failure TX getting out before this should the
2602 * driver reorder operations.
2603 */
2604 os_sleep(0, 10000);
2605 ap_sta_disconnect(hapd, sta, sta->addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002606 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002607 hostapd_wps_eap_completed(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609}