blob: 42b0299514fdc93e13deaaa01e50cc724460daa7 [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
Dmitry Shmidtde47be72016-01-07 12:52:55 -080037#ifdef CONFIG_HS20
38static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
39#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080041 struct sta_info *sta, int success,
42 int remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043
44
45static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
46 u8 type, const u8 *data, size_t datalen)
47{
48 u8 *buf;
49 struct ieee802_1x_hdr *xhdr;
50 size_t len;
51 int encrypt = 0;
52
53 len = sizeof(*xhdr) + datalen;
54 buf = os_zalloc(len);
55 if (buf == NULL) {
56 wpa_printf(MSG_ERROR, "malloc() failed for "
57 "ieee802_1x_send(len=%lu)",
58 (unsigned long) len);
59 return;
60 }
61
62 xhdr = (struct ieee802_1x_hdr *) buf;
63 xhdr->version = hapd->conf->eapol_version;
64 xhdr->type = type;
65 xhdr->length = host_to_be16(datalen);
66
67 if (datalen > 0 && data != NULL)
68 os_memcpy(xhdr + 1, data, datalen);
69
70 if (wpa_auth_pairwise_set(sta->wpa_sm))
71 encrypt = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080072#ifdef CONFIG_TESTING_OPTIONS
73 if (hapd->ext_eapol_frame_io) {
74 size_t hex_len = 2 * len + 1;
75 char *hex = os_malloc(hex_len);
76
77 if (hex) {
78 wpa_snprintf_hex(hex, hex_len, buf, len);
79 wpa_msg(hapd->msg_ctx, MSG_INFO,
80 "EAPOL-TX " MACSTR " %s",
81 MAC2STR(sta->addr), hex);
82 os_free(hex);
83 }
84 } else
85#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 if (sta->flags & WLAN_STA_PREAUTH) {
87 rsn_preauth_send(hapd, sta, buf, len);
88 } else {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080089 hostapd_drv_hapd_send_eapol(
90 hapd, sta->addr, buf, len,
91 encrypt, hostapd_sta_flags_to_drv(sta->flags));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 }
93
94 os_free(buf);
95}
96
97
98void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
99 struct sta_info *sta, int authorized)
100{
101 int res;
102
103 if (sta->flags & WLAN_STA_PREAUTH)
104 return;
105
106 if (authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 ap_sta_set_authorized(hapd, sta, 1);
108 res = hostapd_set_authorized(hapd, sta, 1);
109 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
110 HOSTAPD_LEVEL_DEBUG, "authorizing port");
111 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 ap_sta_set_authorized(hapd, sta, 0);
113 res = hostapd_set_authorized(hapd, sta, 0);
114 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
115 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
116 }
117
118 if (res && errno != ENOENT) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700119 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
120 " flags for kernel driver (errno=%d).",
121 MAC2STR(sta->addr), errno);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 }
123
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800124 if (authorized) {
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800125 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128}
129
130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800131#ifndef CONFIG_FIPS
132#ifndef CONFIG_NO_RC4
133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
135 struct sta_info *sta,
136 int idx, int broadcast,
137 u8 *key_data, size_t key_len)
138{
139 u8 *buf, *ekey;
140 struct ieee802_1x_hdr *hdr;
141 struct ieee802_1x_eapol_key *key;
142 size_t len, ekey_len;
143 struct eapol_state_machine *sm = sta->eapol_sm;
144
145 if (sm == NULL)
146 return;
147
148 len = sizeof(*key) + key_len;
149 buf = os_zalloc(sizeof(*hdr) + len);
150 if (buf == NULL)
151 return;
152
153 hdr = (struct ieee802_1x_hdr *) buf;
154 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
155 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700156 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 wpa_get_ntp_timestamp(key->replay_counter);
158
159 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
160 wpa_printf(MSG_ERROR, "Could not get random numbers");
161 os_free(buf);
162 return;
163 }
164
165 key->key_index = idx | (broadcast ? 0 : BIT(7));
166 if (hapd->conf->eapol_key_index_workaround) {
167 /* According to some information, WinXP Supplicant seems to
168 * interpret bit7 as an indication whether the key is to be
169 * activated, so make it possible to enable workaround that
170 * sets this bit for all keys. */
171 key->key_index |= BIT(7);
172 }
173
174 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
175 * MSK[32..63] is used to sign the message. */
176 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
177 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
178 "and signing EAPOL-Key");
179 os_free(buf);
180 return;
181 }
182 os_memcpy((u8 *) (key + 1), key_data, key_len);
183 ekey_len = sizeof(key->key_iv) + 32;
184 ekey = os_malloc(ekey_len);
185 if (ekey == NULL) {
186 wpa_printf(MSG_ERROR, "Could not encrypt key");
187 os_free(buf);
188 return;
189 }
190 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
191 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
192 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
193 os_free(ekey);
194
195 /* This header is needed here for HMAC-MD5, but it will be regenerated
196 * in ieee802_1x_send() */
197 hdr->version = hapd->conf->eapol_version;
198 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
199 hdr->length = host_to_be16(len);
200 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
201 key->key_signature);
202
203 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
204 " (%s index=%d)", MAC2STR(sm->addr),
205 broadcast ? "broadcast" : "unicast", idx);
206 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
207 if (sta->eapol_sm)
208 sta->eapol_sm->dot1xAuthEapolFramesTx++;
209 os_free(buf);
210}
211
212
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800213static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214{
215 struct eapol_authenticator *eapol = hapd->eapol_auth;
216 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217
218 if (sm == NULL || !sm->eap_if->eapKeyData)
219 return;
220
221 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
222 MAC2STR(sta->addr));
223
224#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800225 if (sta->vlan_id > 0) {
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700226 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
227 return;
228 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 if (eapol->default_wep_key) {
232 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
233 eapol->default_wep_key,
234 hapd->conf->default_wep_key_len);
235 }
236
237 if (hapd->conf->individual_wep_key_len > 0) {
238 u8 *ikey;
239 ikey = os_malloc(hapd->conf->individual_wep_key_len);
240 if (ikey == NULL ||
241 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
242 {
243 wpa_printf(MSG_ERROR, "Could not generate random "
244 "individual WEP key.");
245 os_free(ikey);
246 return;
247 }
248
249 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
250 ikey, hapd->conf->individual_wep_key_len);
251
252 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
253 hapd->conf->individual_wep_key_len);
254
255 /* TODO: set encryption in TX callback, i.e., only after STA
256 * has ACKed EAPOL-Key frame */
257 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
258 sta->addr, 0, 1, NULL, 0, ikey,
259 hapd->conf->individual_wep_key_len)) {
260 wpa_printf(MSG_ERROR, "Could not set individual WEP "
261 "encryption.");
262 }
263
264 os_free(ikey);
265 }
266}
267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800268#endif /* CONFIG_NO_RC4 */
269#endif /* CONFIG_FIPS */
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271
272const char *radius_mode_txt(struct hostapd_data *hapd)
273{
274 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800275 case HOSTAPD_MODE_IEEE80211AD:
276 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 case HOSTAPD_MODE_IEEE80211A:
278 return "802.11a";
279 case HOSTAPD_MODE_IEEE80211G:
280 return "802.11g";
281 case HOSTAPD_MODE_IEEE80211B:
282 default:
283 return "802.11b";
284 }
285}
286
287
288int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
289{
290 int i;
291 u8 rate = 0;
292
293 for (i = 0; i < sta->supported_rates_len; i++)
294 if ((sta->supported_rates[i] & 0x7f) > rate)
295 rate = sta->supported_rates[i] & 0x7f;
296
297 return rate;
298}
299
300
301#ifndef CONFIG_NO_RADIUS
302static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
303 struct eapol_state_machine *sm,
304 const u8 *eap, size_t len)
305{
306 const u8 *identity;
307 size_t identity_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800308 const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309
310 if (len <= sizeof(struct eap_hdr) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800311 (hdr->code == EAP_CODE_RESPONSE &&
312 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
313 (hdr->code == EAP_CODE_INITIATE &&
314 eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
315 (hdr->code != EAP_CODE_RESPONSE &&
316 hdr->code != EAP_CODE_INITIATE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 return;
318
319 identity = eap_get_identity(sm->eap, &identity_len);
320 if (identity == NULL)
321 return;
322
323 /* Save station identity for future RADIUS packets */
324 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700325 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 if (sm->identity == NULL) {
327 sm->identity_len = 0;
328 return;
329 }
330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
333 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
334 sm->dot1xAuthEapolRespIdFramesRx++;
335}
336
337
Dmitry Shmidt03658832014-08-13 11:03:49 -0700338static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
339 struct hostapd_radius_attr *req_attr,
340 struct sta_info *sta,
341 struct radius_msg *msg)
342{
343 u32 suite;
344 int ver, val;
345
346 ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
347 val = wpa_auth_get_pairwise(sta->wpa_sm);
348 suite = wpa_cipher_to_suite(ver, val);
349 if (val != -1 &&
350 !hostapd_config_get_radius_attr(req_attr,
351 RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
352 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
353 suite)) {
354 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
355 return -1;
356 }
357
Dmitry Shmidt41712582015-06-29 11:02:15 -0700358 suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
359 hapd->conf->osen) ?
Dmitry Shmidt03658832014-08-13 11:03:49 -0700360 WPA_PROTO_RSN : WPA_PROTO_WPA,
361 hapd->conf->wpa_group);
362 if (!hostapd_config_get_radius_attr(req_attr,
363 RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
364 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
365 suite)) {
366 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
367 return -1;
368 }
369
370 val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
371 suite = wpa_akm_to_suite(val);
372 if (val != -1 &&
373 !hostapd_config_get_radius_attr(req_attr,
374 RADIUS_ATTR_WLAN_AKM_SUITE) &&
375 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
376 suite)) {
377 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
378 return -1;
379 }
380
381#ifdef CONFIG_IEEE80211W
382 if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
383 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
384 hapd->conf->group_mgmt_cipher);
385 if (!hostapd_config_get_radius_attr(
386 req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
387 !radius_msg_add_attr_int32(
388 msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
389 wpa_printf(MSG_ERROR,
390 "Could not add WLAN-Group-Mgmt-Cipher");
391 return -1;
392 }
393 }
394#endif /* CONFIG_IEEE80211W */
395
396 return 0;
397}
398
399
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700400static int add_common_radius_sta_attr(struct hostapd_data *hapd,
401 struct hostapd_radius_attr *req_attr,
402 struct sta_info *sta,
403 struct radius_msg *msg)
404{
405 char buf[128];
406
407 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800408 RADIUS_ATTR_SERVICE_TYPE) &&
409 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
410 RADIUS_SERVICE_TYPE_FRAMED)) {
411 wpa_printf(MSG_ERROR, "Could not add Service-Type");
412 return -1;
413 }
414
415 if (!hostapd_config_get_radius_attr(req_attr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416 RADIUS_ATTR_NAS_PORT) &&
417 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
418 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
419 return -1;
420 }
421
422 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
423 MAC2STR(sta->addr));
424 buf[sizeof(buf) - 1] = '\0';
425 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
426 (u8 *) buf, os_strlen(buf))) {
427 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
428 return -1;
429 }
430
431 if (sta->flags & WLAN_STA_PREAUTH) {
432 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
433 sizeof(buf));
434 } else {
435 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
436 radius_sta_rate(hapd, sta) / 2,
437 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
438 radius_mode_txt(hapd));
439 buf[sizeof(buf) - 1] = '\0';
440 }
441 if (!hostapd_config_get_radius_attr(req_attr,
442 RADIUS_ATTR_CONNECT_INFO) &&
443 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
444 (u8 *) buf, os_strlen(buf))) {
445 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
446 return -1;
447 }
448
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800449 if (sta->acct_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800450 os_snprintf(buf, sizeof(buf), "%016llX",
451 (unsigned long long) sta->acct_session_id);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800452 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
453 (u8 *) buf, os_strlen(buf))) {
454 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
455 return -1;
456 }
457 }
458
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800459 if ((hapd->conf->wpa & 2) &&
460 !hapd->conf->disable_pmksa_caching &&
461 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800462 os_snprintf(buf, sizeof(buf), "%016llX",
463 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800464 sta->eapol_sm->acct_multi_session_id);
465 if (!radius_msg_add_attr(
466 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
467 (u8 *) buf, os_strlen(buf))) {
468 wpa_printf(MSG_INFO,
469 "Could not add Acct-Multi-Session-Id");
470 return -1;
471 }
472 }
473
Dmitry Shmidt03658832014-08-13 11:03:49 -0700474#ifdef CONFIG_IEEE80211R
475 if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
476 sta->wpa_sm &&
477 (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
478 sta->auth_alg == WLAN_AUTH_FT) &&
479 !hostapd_config_get_radius_attr(req_attr,
480 RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
481 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
482 WPA_GET_BE16(
483 hapd->conf->mobility_domain))) {
484 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
485 return -1;
486 }
487#endif /* CONFIG_IEEE80211R */
488
Dmitry Shmidt41712582015-06-29 11:02:15 -0700489 if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
Dmitry Shmidt03658832014-08-13 11:03:49 -0700490 add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
491 return -1;
492
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700493 return 0;
494}
495
496
497int add_common_radius_attr(struct hostapd_data *hapd,
498 struct hostapd_radius_attr *req_attr,
499 struct sta_info *sta,
500 struct radius_msg *msg)
501{
502 char buf[128];
503 struct hostapd_radius_attr *attr;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800504 int len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700505
506 if (!hostapd_config_get_radius_attr(req_attr,
507 RADIUS_ATTR_NAS_IP_ADDRESS) &&
508 hapd->conf->own_ip_addr.af == AF_INET &&
509 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
510 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
511 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
512 return -1;
513 }
514
515#ifdef CONFIG_IPV6
516 if (!hostapd_config_get_radius_attr(req_attr,
517 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
518 hapd->conf->own_ip_addr.af == AF_INET6 &&
519 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
520 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
521 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
522 return -1;
523 }
524#endif /* CONFIG_IPV6 */
525
526 if (!hostapd_config_get_radius_attr(req_attr,
527 RADIUS_ATTR_NAS_IDENTIFIER) &&
528 hapd->conf->nas_identifier &&
529 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
530 (u8 *) hapd->conf->nas_identifier,
531 os_strlen(hapd->conf->nas_identifier))) {
532 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
533 return -1;
534 }
535
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800536 len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
537 MAC2STR(hapd->own_addr));
538 os_memcpy(&buf[len], hapd->conf->ssid.ssid,
539 hapd->conf->ssid.ssid_len);
540 len += hapd->conf->ssid.ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700541 if (!hostapd_config_get_radius_attr(req_attr,
542 RADIUS_ATTR_CALLED_STATION_ID) &&
543 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800544 (u8 *) buf, len)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700545 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
546 return -1;
547 }
548
549 if (!hostapd_config_get_radius_attr(req_attr,
550 RADIUS_ATTR_NAS_PORT_TYPE) &&
551 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
552 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
553 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
554 return -1;
555 }
556
Dmitry Shmidt03658832014-08-13 11:03:49 -0700557#ifdef CONFIG_INTERWORKING
558 if (hapd->conf->interworking &&
559 !is_zero_ether_addr(hapd->conf->hessid)) {
560 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
561 MAC2STR(hapd->conf->hessid));
562 buf[sizeof(buf) - 1] = '\0';
563 if (!hostapd_config_get_radius_attr(req_attr,
564 RADIUS_ATTR_WLAN_HESSID) &&
565 !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
566 (u8 *) buf, os_strlen(buf))) {
567 wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
568 return -1;
569 }
570 }
571#endif /* CONFIG_INTERWORKING */
572
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700573 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
574 return -1;
575
576 for (attr = req_attr; attr; attr = attr->next) {
577 if (!radius_msg_add_attr(msg, attr->type,
578 wpabuf_head(attr->val),
579 wpabuf_len(attr->val))) {
580 wpa_printf(MSG_ERROR, "Could not add RADIUS "
581 "attribute");
582 return -1;
583 }
584 }
585
586 return 0;
587}
588
589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
591 struct sta_info *sta,
592 const u8 *eap, size_t len)
593{
594 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 struct eapol_state_machine *sm = sta->eapol_sm;
596
597 if (sm == NULL)
598 return;
599
600 ieee802_1x_learn_identity(hapd, sm, eap, len);
601
602 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
603 "packet");
604
605 sm->radius_identifier = radius_client_get_id(hapd->radius);
606 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
607 sm->radius_identifier);
608 if (msg == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800609 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 return;
611 }
612
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800613 if (radius_msg_make_authenticator(msg) < 0) {
614 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
615 goto fail;
616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617
618 if (sm->identity &&
619 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
620 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800621 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 goto fail;
623 }
624
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700625 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
626 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628
629 /* TODO: should probably check MTU from driver config; 2304 is max for
630 * IEEE 802.11, but use 1400 to avoid problems with too large packets
631 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700632 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
633 RADIUS_ATTR_FRAMED_MTU) &&
634 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800635 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 goto fail;
637 }
638
Dmitry Shmidt41712582015-06-29 11:02:15 -0700639 if (!radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800640 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 goto fail;
642 }
643
644 /* State attribute must be copied if and only if this packet is
645 * Access-Request reply to the previous Access-Challenge */
646 if (sm->last_recv_radius &&
647 radius_msg_get_hdr(sm->last_recv_radius)->code ==
648 RADIUS_CODE_ACCESS_CHALLENGE) {
649 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
650 RADIUS_ATTR_STATE);
651 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800652 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 goto fail;
654 }
655 if (res > 0) {
656 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
657 }
658 }
659
Dmitry Shmidt04949592012-07-19 12:16:46 -0700660 if (hapd->conf->radius_request_cui) {
661 const u8 *cui;
662 size_t cui_len;
663 /* Add previously learned CUI or nul CUI to request CUI */
664 if (sm->radius_cui) {
665 cui = wpabuf_head(sm->radius_cui);
666 cui_len = wpabuf_len(sm->radius_cui);
667 } else {
668 cui = (const u8 *) "\0";
669 cui_len = 1;
670 }
671 if (!radius_msg_add_attr(msg,
672 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
673 cui, cui_len)) {
674 wpa_printf(MSG_ERROR, "Could not add CUI");
675 goto fail;
676 }
677 }
678
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800679#ifdef CONFIG_HS20
680 if (hapd->conf->hs20) {
681 u8 ver = 1; /* Release 2 */
682 if (!radius_msg_add_wfa(
683 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
684 &ver, 1)) {
685 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
686 "version");
687 goto fail;
688 }
689
690 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
691 const u8 *pos;
692 u8 buf[3];
693 u16 id;
694 pos = wpabuf_head_u8(sta->hs20_ie);
695 buf[0] = (*pos) >> 4;
696 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
697 wpabuf_len(sta->hs20_ie) >= 3)
698 id = WPA_GET_LE16(pos + 1);
699 else
700 id = 0;
701 WPA_PUT_BE16(buf + 1, id);
702 if (!radius_msg_add_wfa(
703 msg,
704 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
705 buf, sizeof(buf))) {
706 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
707 "STA version");
708 goto fail;
709 }
710 }
711 }
712#endif /* CONFIG_HS20 */
713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
715 goto fail;
716
717 return;
718
719 fail:
720 radius_msg_free(msg);
721}
722#endif /* CONFIG_NO_RADIUS */
723
724
725static void handle_eap_response(struct hostapd_data *hapd,
726 struct sta_info *sta, struct eap_hdr *eap,
727 size_t len)
728{
729 u8 type, *data;
730 struct eapol_state_machine *sm = sta->eapol_sm;
731 if (sm == NULL)
732 return;
733
734 data = (u8 *) (eap + 1);
735
736 if (len < sizeof(*eap) + 1) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800737 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 return;
739 }
740
741 sm->eap_type_supp = type = data[0];
742
743 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
744 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
745 "id=%d len=%d) from STA: EAP Response-%s (%d)",
746 eap->code, eap->identifier, be_to_host16(eap->length),
747 eap_server_get_name(0, type), type);
748
749 sm->dot1xAuthEapolRespFramesRx++;
750
751 wpabuf_free(sm->eap_if->eapRespData);
752 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
753 sm->eapolEap = TRUE;
754}
755
756
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800757static void handle_eap_initiate(struct hostapd_data *hapd,
758 struct sta_info *sta, struct eap_hdr *eap,
759 size_t len)
760{
761#ifdef CONFIG_ERP
762 u8 type, *data;
763 struct eapol_state_machine *sm = sta->eapol_sm;
764
765 if (sm == NULL)
766 return;
767
768 if (len < sizeof(*eap) + 1) {
769 wpa_printf(MSG_INFO,
770 "handle_eap_initiate: too short response data");
771 return;
772 }
773
774 data = (u8 *) (eap + 1);
775 type = data[0];
776
777 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
778 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
779 "id=%d len=%d) from STA: EAP Initiate type %u",
780 eap->code, eap->identifier, be_to_host16(eap->length),
781 type);
782
783 wpabuf_free(sm->eap_if->eapRespData);
784 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
785 sm->eapolEap = TRUE;
786#endif /* CONFIG_ERP */
787}
788
789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790/* Process incoming EAP packet from Supplicant */
791static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
792 u8 *buf, size_t len)
793{
794 struct eap_hdr *eap;
795 u16 eap_len;
796
797 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800798 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 return;
800 }
801
802 eap = (struct eap_hdr *) buf;
803
804 eap_len = be_to_host16(eap->length);
805 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
806 eap->code, eap->identifier, eap_len);
807 if (eap_len < sizeof(*eap)) {
808 wpa_printf(MSG_DEBUG, " Invalid EAP length");
809 return;
810 } else if (eap_len > len) {
811 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
812 "packet");
813 return;
814 } else if (eap_len < len) {
815 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
816 "packet", (unsigned long) len - eap_len);
817 }
818
819 switch (eap->code) {
820 case EAP_CODE_REQUEST:
821 wpa_printf(MSG_DEBUG, " (request)");
822 return;
823 case EAP_CODE_RESPONSE:
824 wpa_printf(MSG_DEBUG, " (response)");
825 handle_eap_response(hapd, sta, eap, eap_len);
826 break;
827 case EAP_CODE_SUCCESS:
828 wpa_printf(MSG_DEBUG, " (success)");
829 return;
830 case EAP_CODE_FAILURE:
831 wpa_printf(MSG_DEBUG, " (failure)");
832 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800833 case EAP_CODE_INITIATE:
834 wpa_printf(MSG_DEBUG, " (initiate)");
835 handle_eap_initiate(hapd, sta, eap, eap_len);
836 break;
837 case EAP_CODE_FINISH:
838 wpa_printf(MSG_DEBUG, " (finish)");
839 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 default:
841 wpa_printf(MSG_DEBUG, " (unknown code)");
842 return;
843 }
844}
845
846
847static struct eapol_state_machine *
848ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
849{
850 int flags = 0;
851 if (sta->flags & WLAN_STA_PREAUTH)
852 flags |= EAPOL_SM_PREAUTH;
853 if (sta->wpa_sm) {
854 flags |= EAPOL_SM_USES_WPA;
855 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
856 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
857 }
858 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700859 sta->wps_ie, sta->p2p_ie, sta,
860 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861}
862
863
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800864static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
865 size_t len)
866{
867 if (sta->pending_eapol_rx) {
868 wpabuf_free(sta->pending_eapol_rx->buf);
869 } else {
870 sta->pending_eapol_rx =
871 os_malloc(sizeof(*sta->pending_eapol_rx));
872 if (!sta->pending_eapol_rx)
873 return;
874 }
875
876 sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
877 if (!sta->pending_eapol_rx->buf) {
878 os_free(sta->pending_eapol_rx);
879 sta->pending_eapol_rx = NULL;
880 return;
881 }
882
883 os_get_reltime(&sta->pending_eapol_rx->rx_time);
884}
885
886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887/**
888 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
889 * @hapd: hostapd BSS data
890 * @sa: Source address (sender of the EAPOL frame)
891 * @buf: EAPOL frame
892 * @len: Length of buf in octets
893 *
894 * This function is called for each incoming EAPOL frame from the interface
895 */
896void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
897 size_t len)
898{
899 struct sta_info *sta;
900 struct ieee802_1x_hdr *hdr;
901 struct ieee802_1x_eapol_key *key;
902 u16 datalen;
903 struct rsn_pmksa_cache_entry *pmksa;
904 int key_mgmt;
905
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800906 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 !hapd->conf->wps_state)
908 return;
909
910 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
911 (unsigned long) len, MAC2STR(sa));
912 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800913 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
914 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
916 "associated/Pre-authenticating STA");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800917
918 if (sta && (sta->flags & WLAN_STA_AUTH)) {
919 wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
920 " for later use", MAC2STR(sta->addr));
921 ieee802_1x_save_eapol(sta, buf, len);
922 }
923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 return;
925 }
926
927 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800928 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 return;
930 }
931
932 hdr = (struct ieee802_1x_hdr *) buf;
933 datalen = be_to_host16(hdr->length);
934 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
935 hdr->version, hdr->type, datalen);
936
937 if (len - sizeof(*hdr) < datalen) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800938 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 if (sta->eapol_sm)
940 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
941 return;
942 }
943 if (len - sizeof(*hdr) > datalen) {
944 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
945 "IEEE 802.1X packet",
946 (unsigned long) len - sizeof(*hdr) - datalen);
947 }
948
949 if (sta->eapol_sm) {
950 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
951 sta->eapol_sm->dot1xAuthEapolFramesRx++;
952 }
953
954 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
955 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
956 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
957 (key->type == EAPOL_KEY_TYPE_WPA ||
958 key->type == EAPOL_KEY_TYPE_RSN)) {
959 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
960 sizeof(*hdr) + datalen);
961 return;
962 }
963
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800964 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
966 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
967 "802.1X not enabled and WPS not used");
968 return;
969 }
970
971 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
972 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
973 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
974 "STA is using PSK");
975 return;
976 }
977
978 if (!sta->eapol_sm) {
979 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
980 if (!sta->eapol_sm)
981 return;
982
983#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800984 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985 u32 wflags = sta->flags & (WLAN_STA_WPS |
986 WLAN_STA_WPS2 |
987 WLAN_STA_MAYBE_WPS);
988 if (wflags == WLAN_STA_MAYBE_WPS ||
989 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
990 /*
991 * Delay EAPOL frame transmission until a
992 * possible WPS STA initiates the handshake
993 * with EAPOL-Start. Only allow the wait to be
994 * skipped if the STA is known to support WPS
995 * 2.0.
996 */
997 wpa_printf(MSG_DEBUG, "WPS: Do not start "
998 "EAPOL until EAPOL-Start is "
999 "received");
1000 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1001 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 }
1003#endif /* CONFIG_WPS */
1004
1005 sta->eapol_sm->eap_if->portEnabled = TRUE;
1006 }
1007
1008 /* since we support version 1, we can ignore version field and proceed
1009 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1010 /* TODO: actually, we are not version 1 anymore.. However, Version 2
1011 * does not change frame contents, so should be ok to process frames
1012 * more or less identically. Some changes might be needed for
1013 * verification of fields. */
1014
1015 switch (hdr->type) {
1016 case IEEE802_1X_TYPE_EAP_PACKET:
1017 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1018 break;
1019
1020 case IEEE802_1X_TYPE_EAPOL_START:
1021 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1022 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
1023 "from STA");
1024 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1025 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1026 if (pmksa) {
1027 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1028 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
1029 "available - ignore it since "
1030 "STA sent EAPOL-Start");
1031 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1032 }
1033 sta->eapol_sm->eapolStart = TRUE;
1034 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1035 eap_server_clear_identity(sta->eapol_sm->eap);
1036 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1037 break;
1038
1039 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1040 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1041 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
1042 "from STA");
1043 sta->acct_terminate_cause =
1044 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1045 accounting_sta_stop(hapd, sta);
1046 sta->eapol_sm->eapolLogoff = TRUE;
1047 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1048 eap_server_clear_identity(sta->eapol_sm->eap);
1049 break;
1050
1051 case IEEE802_1X_TYPE_EAPOL_KEY:
1052 wpa_printf(MSG_DEBUG, " EAPOL-Key");
1053 if (!ap_sta_is_authorized(sta)) {
1054 wpa_printf(MSG_DEBUG, " Dropped key data from "
1055 "unauthorized Supplicant");
1056 break;
1057 }
1058 break;
1059
1060 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1061 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
1062 /* TODO: implement support for this; show data */
1063 break;
1064
1065 default:
1066 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
1067 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1068 break;
1069 }
1070
1071 eapol_auth_step(sta->eapol_sm);
1072}
1073
1074
1075/**
1076 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1077 * @hapd: hostapd BSS data
1078 * @sta: The station
1079 *
1080 * This function is called to start IEEE 802.1X authentication when a new
1081 * station completes IEEE 802.11 association.
1082 */
1083void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1084{
1085 struct rsn_pmksa_cache_entry *pmksa;
1086 int reassoc = 1;
1087 int force_1x = 0;
1088 int key_mgmt;
1089
1090#ifdef CONFIG_WPS
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001091 if (hapd->conf->wps_state &&
1092 ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1093 (sta->flags & WLAN_STA_WPS))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 /*
1095 * Need to enable IEEE 802.1X/EAPOL state machines for possible
1096 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1097 * authentication in this BSS.
1098 */
1099 force_1x = 1;
1100 }
1101#endif /* CONFIG_WPS */
1102
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001103 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
1105 "802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001106 /*
1107 * Clear any possible EAPOL authenticator state to support
1108 * reassociation change from WPS to PSK.
1109 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001110 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 return;
1112 }
1113
1114 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1115 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
1116 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001117 /*
1118 * Clear any possible EAPOL authenticator state to support
1119 * reassociation change from WPA-EAP to PSK.
1120 */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001121 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 return;
1123 }
1124
1125 if (sta->eapol_sm == NULL) {
1126 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1127 HOSTAPD_LEVEL_DEBUG, "start authentication");
1128 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1129 if (sta->eapol_sm == NULL) {
1130 hostapd_logger(hapd, sta->addr,
1131 HOSTAPD_MODULE_IEEE8021X,
1132 HOSTAPD_LEVEL_INFO,
1133 "failed to allocate state machine");
1134 return;
1135 }
1136 reassoc = 0;
1137 }
1138
1139#ifdef CONFIG_WPS
1140 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001141 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1142 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001144 * Delay EAPOL frame transmission until a possible WPS STA
1145 * initiates the handshake with EAPOL-Start. Only allow the
1146 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001148 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
1149 "EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1151 }
1152#endif /* CONFIG_WPS */
1153
1154 sta->eapol_sm->eap_if->portEnabled = TRUE;
1155
1156#ifdef CONFIG_IEEE80211R
1157 if (sta->auth_alg == WLAN_AUTH_FT) {
1158 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1159 HOSTAPD_LEVEL_DEBUG,
1160 "PMK from FT - skip IEEE 802.1X/EAP");
1161 /* Setup EAPOL state machines to already authenticated state
1162 * because of existing FT information from R0KH. */
1163 sta->eapol_sm->keyRun = TRUE;
1164 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1165 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1166 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1167 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001168 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001169 sta->eapol_sm->portValid = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 if (sta->eapol_sm->eap)
1171 eap_sm_notify_cached(sta->eapol_sm->eap);
1172 /* TODO: get vlan_id from R0KH using RRB message */
1173 return;
1174 }
1175#endif /* CONFIG_IEEE80211R */
1176
1177 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1178 if (pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1180 HOSTAPD_LEVEL_DEBUG,
1181 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1182 /* Setup EAPOL state machines to already authenticated state
1183 * because of existing PMKSA information in the cache. */
1184 sta->eapol_sm->keyRun = TRUE;
1185 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1186 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1187 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1188 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001189 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 if (sta->eapol_sm->eap)
1191 eap_sm_notify_cached(sta->eapol_sm->eap);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001192 pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001193 ap_sta_bind_vlan(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 } else {
1195 if (reassoc) {
1196 /*
1197 * Force EAPOL state machines to start
1198 * re-authentication without having to wait for the
1199 * Supplicant to send EAPOL-Start.
1200 */
1201 sta->eapol_sm->reAuthenticate = TRUE;
1202 }
1203 eapol_auth_step(sta->eapol_sm);
1204 }
1205}
1206
1207
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001208void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209{
1210 struct eapol_state_machine *sm = sta->eapol_sm;
1211
Dmitry Shmidtde47be72016-01-07 12:52:55 -08001212#ifdef CONFIG_HS20
1213 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
1214#endif /* CONFIG_HS20 */
1215
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08001216 if (sta->pending_eapol_rx) {
1217 wpabuf_free(sta->pending_eapol_rx->buf);
1218 os_free(sta->pending_eapol_rx);
1219 sta->pending_eapol_rx = NULL;
1220 }
1221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 if (sm == NULL)
1223 return;
1224
1225 sta->eapol_sm = NULL;
1226
1227#ifndef CONFIG_NO_RADIUS
1228 radius_msg_free(sm->last_recv_radius);
1229 radius_free_class(&sm->radius_class);
1230#endif /* CONFIG_NO_RADIUS */
1231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 eapol_auth_free(sm);
1233}
1234
1235
1236#ifndef CONFIG_NO_RADIUS
1237static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1238 struct sta_info *sta)
1239{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 struct wpabuf *eap;
1241 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 int eap_type = -1;
1243 char buf[64];
1244 struct radius_msg *msg;
1245 struct eapol_state_machine *sm = sta->eapol_sm;
1246
1247 if (sm == NULL || sm->last_recv_radius == NULL) {
1248 if (sm)
1249 sm->eap_if->aaaEapNoReq = TRUE;
1250 return;
1251 }
1252
1253 msg = sm->last_recv_radius;
1254
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001255 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 if (eap == NULL) {
1257 /* RFC 3579, Chap. 2.6.3:
1258 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1259 * attribute */
1260 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1261 HOSTAPD_LEVEL_WARNING, "could not extract "
1262 "EAP-Message from RADIUS message");
1263 sm->eap_if->aaaEapNoReq = TRUE;
1264 return;
1265 }
1266
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001267 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1269 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1270 "received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001271 wpabuf_free(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 sm->eap_if->aaaEapNoReq = TRUE;
1273 return;
1274 }
1275
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001276 if (wpabuf_len(eap) > sizeof(*hdr))
1277 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001279 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 switch (hdr->code) {
1281 case EAP_CODE_REQUEST:
1282 if (eap_type >= 0)
1283 sm->eap_type_authsrv = eap_type;
1284 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001285 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 break;
1287 case EAP_CODE_RESPONSE:
1288 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001289 eap_server_get_name(0, eap_type), eap_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 break;
1291 case EAP_CODE_SUCCESS:
1292 os_strlcpy(buf, "EAP Success", sizeof(buf));
1293 break;
1294 case EAP_CODE_FAILURE:
1295 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1296 break;
1297 default:
1298 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1299 break;
1300 }
1301 buf[sizeof(buf) - 1] = '\0';
1302 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1303 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1304 "id=%d len=%d) from RADIUS server: %s",
1305 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1306 buf);
1307 sm->eap_if->aaaEapReq = TRUE;
1308
1309 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001310 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311}
1312
1313
1314static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1315 struct sta_info *sta, struct radius_msg *msg,
1316 struct radius_msg *req,
1317 const u8 *shared_secret,
1318 size_t shared_secret_len)
1319{
1320 struct radius_ms_mppe_keys *keys;
1321 struct eapol_state_machine *sm = sta->eapol_sm;
1322 if (sm == NULL)
1323 return;
1324
1325 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1326 shared_secret_len);
1327
1328 if (keys && keys->send && keys->recv) {
1329 size_t len = keys->send_len + keys->recv_len;
1330 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1331 keys->send, keys->send_len);
1332 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1333 keys->recv, keys->recv_len);
1334
1335 os_free(sm->eap_if->aaaEapKeyData);
1336 sm->eap_if->aaaEapKeyData = os_malloc(len);
1337 if (sm->eap_if->aaaEapKeyData) {
1338 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1339 keys->recv_len);
1340 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1341 keys->send, keys->send_len);
1342 sm->eap_if->aaaEapKeyDataLen = len;
1343 sm->eap_if->aaaEapKeyAvailable = TRUE;
1344 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001345 } else {
1346 wpa_printf(MSG_DEBUG,
1347 "MS-MPPE: 1x_get_keys, could not get keys: %p send: %p recv: %p",
1348 keys, keys ? keys->send : NULL,
1349 keys ? keys->recv : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 }
1351
1352 if (keys) {
1353 os_free(keys->send);
1354 os_free(keys->recv);
1355 os_free(keys);
1356 }
1357}
1358
1359
1360static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1361 struct sta_info *sta,
1362 struct radius_msg *msg)
1363{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001364 u8 *attr_class;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 size_t class_len;
1366 struct eapol_state_machine *sm = sta->eapol_sm;
1367 int count, i;
1368 struct radius_attr_data *nclass;
1369 size_t nclass_count;
1370
1371 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1372 sm == NULL)
1373 return;
1374
1375 radius_free_class(&sm->radius_class);
1376 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1377 if (count <= 0)
1378 return;
1379
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001380 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001381 if (nclass == NULL)
1382 return;
1383
1384 nclass_count = 0;
1385
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001386 attr_class = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 for (i = 0; i < count; i++) {
1388 do {
1389 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001390 &attr_class, &class_len,
1391 attr_class) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 i = count;
1393 break;
1394 }
1395 } while (class_len < 1);
1396
1397 nclass[nclass_count].data = os_malloc(class_len);
1398 if (nclass[nclass_count].data == NULL)
1399 break;
1400
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07001401 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 nclass[nclass_count].len = class_len;
1403 nclass_count++;
1404 }
1405
1406 sm->radius_class.attr = nclass;
1407 sm->radius_class.count = nclass_count;
1408 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1409 "attributes for " MACSTR,
1410 (unsigned long) sm->radius_class.count,
1411 MAC2STR(sta->addr));
1412}
1413
1414
1415/* Update sta->identity based on User-Name attribute in Access-Accept */
1416static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1417 struct sta_info *sta,
1418 struct radius_msg *msg)
1419{
1420 u8 *buf, *identity;
1421 size_t len;
1422 struct eapol_state_machine *sm = sta->eapol_sm;
1423
1424 if (sm == NULL)
1425 return;
1426
1427 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1428 NULL) < 0)
1429 return;
1430
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001431 identity = (u8 *) dup_binstr(buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 if (identity == NULL)
1433 return;
1434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1436 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1437 "User-Name from Access-Accept '%s'",
1438 sm->identity ? (char *) sm->identity : "N/A",
1439 (char *) identity);
1440
1441 os_free(sm->identity);
1442 sm->identity = identity;
1443 sm->identity_len = len;
1444}
1445
1446
Dmitry Shmidt04949592012-07-19 12:16:46 -07001447/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1448static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1449 struct sta_info *sta,
1450 struct radius_msg *msg)
1451{
1452 struct eapol_state_machine *sm = sta->eapol_sm;
1453 struct wpabuf *cui;
1454 u8 *buf;
1455 size_t len;
1456
1457 if (sm == NULL)
1458 return;
1459
1460 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1461 &buf, &len, NULL) < 0)
1462 return;
1463
1464 cui = wpabuf_alloc_copy(buf, len);
1465 if (cui == NULL)
1466 return;
1467
1468 wpabuf_free(sm->radius_cui);
1469 sm->radius_cui = cui;
1470}
1471
1472
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001473#ifdef CONFIG_HS20
1474
1475static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1476{
1477 sta->remediation = 1;
1478 os_free(sta->remediation_url);
1479 if (len > 2) {
1480 sta->remediation_url = os_malloc(len);
1481 if (!sta->remediation_url)
1482 return;
1483 sta->remediation_method = pos[0];
1484 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1485 sta->remediation_url[len - 1] = '\0';
1486 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1487 "for " MACSTR " - server method %u URL %s",
1488 MAC2STR(sta->addr), sta->remediation_method,
1489 sta->remediation_url);
1490 } else {
1491 sta->remediation_url = NULL;
1492 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1493 "for " MACSTR, MAC2STR(sta->addr));
1494 }
1495 /* TODO: assign the STA into remediation VLAN or add filtering */
1496}
1497
1498
1499static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1500 struct sta_info *sta, u8 *pos,
1501 size_t len)
1502{
1503 if (len < 3)
1504 return; /* Malformed information */
1505 sta->hs20_deauth_requested = 1;
1506 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1507 "Re-auth Delay %u",
1508 *pos, WPA_GET_LE16(pos + 1));
1509 wpabuf_free(sta->hs20_deauth_req);
1510 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1511 if (sta->hs20_deauth_req) {
1512 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1513 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1514 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1515 }
1516 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1517}
1518
1519
1520static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1521 struct sta_info *sta, u8 *pos,
1522 size_t len, int session_timeout)
1523{
1524 unsigned int swt;
1525 int warning_time, beacon_int;
1526
1527 if (len < 1)
1528 return; /* Malformed information */
1529 os_free(sta->hs20_session_info_url);
1530 sta->hs20_session_info_url = os_malloc(len);
1531 if (sta->hs20_session_info_url == NULL)
1532 return;
1533 swt = pos[0];
1534 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1535 sta->hs20_session_info_url[len - 1] = '\0';
1536 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1537 "(session_timeout=%d)",
1538 sta->hs20_session_info_url, swt, session_timeout);
1539 if (session_timeout < 0) {
1540 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1541 return;
1542 }
1543 if (swt == 255)
1544 swt = 1; /* Use one minute as the AP selected value */
1545
1546 if ((unsigned int) session_timeout < swt * 60)
1547 warning_time = 0;
1548 else
1549 warning_time = session_timeout - swt * 60;
1550
1551 beacon_int = hapd->iconf->beacon_int;
1552 if (beacon_int < 1)
1553 beacon_int = 100; /* best guess */
1554 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1555 if (sta->hs20_disassoc_timer > 65535)
1556 sta->hs20_disassoc_timer = 65535;
1557
1558 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1559}
1560
1561#endif /* CONFIG_HS20 */
1562
1563
1564static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1565 struct sta_info *sta,
1566 struct radius_msg *msg,
1567 int session_timeout)
1568{
1569#ifdef CONFIG_HS20
1570 u8 *buf, *pos, *end, type, sublen;
1571 size_t len;
1572
1573 buf = NULL;
1574 sta->remediation = 0;
1575 sta->hs20_deauth_requested = 0;
1576
1577 for (;;) {
1578 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1579 &buf, &len, buf) < 0)
1580 break;
1581 if (len < 6)
1582 continue;
1583 pos = buf;
1584 end = buf + len;
1585 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1586 continue;
1587 pos += 4;
1588
1589 type = *pos++;
1590 sublen = *pos++;
1591 if (sublen < 2)
1592 continue; /* invalid length */
1593 sublen -= 2; /* skip header */
1594 if (pos + sublen > end)
1595 continue; /* invalid WFA VSA */
1596
1597 switch (type) {
1598 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1599 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1600 break;
1601 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1602 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1603 break;
1604 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1605 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1606 session_timeout);
1607 break;
1608 }
1609 }
1610#endif /* CONFIG_HS20 */
1611}
1612
1613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614struct sta_id_search {
1615 u8 identifier;
1616 struct eapol_state_machine *sm;
1617};
1618
1619
1620static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1621 struct sta_info *sta,
1622 void *ctx)
1623{
1624 struct sta_id_search *id_search = ctx;
1625 struct eapol_state_machine *sm = sta->eapol_sm;
1626
1627 if (sm && sm->radius_identifier >= 0 &&
1628 sm->radius_identifier == id_search->identifier) {
1629 id_search->sm = sm;
1630 return 1;
1631 }
1632 return 0;
1633}
1634
1635
1636static struct eapol_state_machine *
1637ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1638{
1639 struct sta_id_search id_search;
1640 id_search.identifier = identifier;
1641 id_search.sm = NULL;
1642 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1643 return id_search.sm;
1644}
1645
1646
1647/**
1648 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1649 * @msg: RADIUS response message
1650 * @req: RADIUS request message
1651 * @shared_secret: RADIUS shared secret
1652 * @shared_secret_len: Length of shared_secret in octets
1653 * @data: Context data (struct hostapd_data *)
1654 * Returns: Processing status
1655 */
1656static RadiusRxResult
1657ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1658 const u8 *shared_secret, size_t shared_secret_len,
1659 void *data)
1660{
1661 struct hostapd_data *hapd = data;
1662 struct sta_info *sta;
1663 u32 session_timeout = 0, termination_action, acct_interim_interval;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001664 int session_timeout_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 struct eapol_state_machine *sm;
1666 int override_eapReq = 0;
1667 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001668 struct vlan_description vlan_desc;
1669#ifndef CONFIG_NO_VLAN
1670 int *untagged, *tagged, *notempty;
1671#endif /* CONFIG_NO_VLAN */
1672
1673 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674
1675 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1676 if (sm == NULL) {
1677 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1678 "station for this RADIUS message");
1679 return RADIUS_RX_UNKNOWN;
1680 }
1681 sta = sm->sta;
1682
1683 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1684 * present when packet contains an EAP-Message attribute */
1685 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1686 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1687 0) < 0 &&
1688 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1689 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1690 "Message-Authenticator since it does not include "
1691 "EAP-Message");
1692 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1693 req, 1)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001694 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 return RADIUS_RX_INVALID_AUTHENTICATOR;
1696 }
1697
1698 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1699 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1700 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001701 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 return RADIUS_RX_UNKNOWN;
1703 }
1704
1705 sm->radius_identifier = -1;
1706 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1707 MAC2STR(sta->addr));
1708
1709 radius_msg_free(sm->last_recv_radius);
1710 sm->last_recv_radius = msg;
1711
1712 session_timeout_set =
1713 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1714 &session_timeout);
1715 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1716 &termination_action))
1717 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1718
1719 if (hapd->conf->acct_interim_interval == 0 &&
1720 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1721 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1722 &acct_interim_interval) == 0) {
1723 if (acct_interim_interval < 60) {
1724 hostapd_logger(hapd, sta->addr,
1725 HOSTAPD_MODULE_IEEE8021X,
1726 HOSTAPD_LEVEL_INFO,
1727 "ignored too small "
1728 "Acct-Interim-Interval %d",
1729 acct_interim_interval);
1730 } else
1731 sta->acct_interim_interval = acct_interim_interval;
1732 }
1733
1734
1735 switch (hdr->code) {
1736 case RADIUS_CODE_ACCESS_ACCEPT:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737#ifndef CONFIG_NO_VLAN
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001738 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED) {
1739 notempty = &vlan_desc.notempty;
1740 untagged = &vlan_desc.untagged;
1741 tagged = vlan_desc.tagged;
1742 *notempty = !!radius_msg_get_vlanid(msg, untagged,
1743 MAX_NUM_TAGGED_VLAN,
1744 tagged);
1745 }
1746
1747 if (vlan_desc.notempty &&
1748 !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
Dmitry Shmidt83474442015-04-15 13:47:09 -07001749 sta->eapol_sm->authFail = TRUE;
1750 hostapd_logger(hapd, sta->addr,
1751 HOSTAPD_MODULE_RADIUS,
1752 HOSTAPD_LEVEL_INFO,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001753 "Invalid VLAN %d%s received from RADIUS server",
1754 vlan_desc.untagged,
1755 vlan_desc.tagged[0] ? "+" : "");
1756 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1757 ap_sta_set_vlan(hapd, sta, &vlan_desc);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001758 break;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001759 }
1760
1761 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1762 !vlan_desc.notempty) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 sta->eapol_sm->authFail = TRUE;
1764 hostapd_logger(hapd, sta->addr,
1765 HOSTAPD_MODULE_IEEE8021X,
1766 HOSTAPD_LEVEL_INFO, "authentication "
1767 "server did not include required VLAN "
1768 "ID in Access-Accept");
1769 break;
1770 }
1771#endif /* CONFIG_NO_VLAN */
1772
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001773 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0)
1774 break;
1775
1776#ifndef CONFIG_NO_VLAN
1777 if (sta->vlan_id > 0) {
1778 hostapd_logger(hapd, sta->addr,
1779 HOSTAPD_MODULE_RADIUS,
1780 HOSTAPD_LEVEL_INFO,
1781 "VLAN ID %d", sta->vlan_id);
1782 }
1783#endif /* CONFIG_NO_VLAN */
1784
Dmitry Shmidt83474442015-04-15 13:47:09 -07001785 if ((sta->flags & WLAN_STA_ASSOC) &&
1786 ap_sta_bind_vlan(hapd, sta) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 break;
1788
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001789 sta->session_timeout_set = !!session_timeout_set;
1790 sta->session_timeout = session_timeout;
1791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 /* RFC 3580, Ch. 3.17 */
1793 if (session_timeout_set && termination_action ==
1794 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1795 sm->reAuthPeriod = session_timeout;
1796 } else if (session_timeout_set)
1797 ap_sta_session_timeout(hapd, sta, session_timeout);
1798
1799 sm->eap_if->aaaSuccess = TRUE;
1800 override_eapReq = 1;
1801 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1802 shared_secret_len);
1803 ieee802_1x_store_radius_class(hapd, sta, msg);
1804 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001805 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001806 ieee802_1x_check_hs20(hapd, sta, msg,
1807 session_timeout_set ?
1808 (int) session_timeout : -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 break;
1810 case RADIUS_CODE_ACCESS_REJECT:
1811 sm->eap_if->aaaFail = TRUE;
1812 override_eapReq = 1;
1813 break;
1814 case RADIUS_CODE_ACCESS_CHALLENGE:
1815 sm->eap_if->aaaEapReq = TRUE;
1816 if (session_timeout_set) {
1817 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1818 sm->eap_if->aaaMethodTimeout = session_timeout;
1819 hostapd_logger(hapd, sm->addr,
1820 HOSTAPD_MODULE_IEEE8021X,
1821 HOSTAPD_LEVEL_DEBUG,
1822 "using EAP timeout of %d seconds (from "
1823 "RADIUS)",
1824 sm->eap_if->aaaMethodTimeout);
1825 } else {
1826 /*
1827 * Use dynamic retransmission behavior per EAP
1828 * specification.
1829 */
1830 sm->eap_if->aaaMethodTimeout = 0;
1831 }
1832 break;
1833 }
1834
1835 ieee802_1x_decapsulate_radius(hapd, sta);
1836 if (override_eapReq)
1837 sm->eap_if->aaaEapReq = FALSE;
1838
1839 eapol_auth_step(sm);
1840
1841 return RADIUS_RX_QUEUED;
1842}
1843#endif /* CONFIG_NO_RADIUS */
1844
1845
1846void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1847{
1848 struct eapol_state_machine *sm = sta->eapol_sm;
1849 if (sm == NULL)
1850 return;
1851
1852 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1853 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1854
1855#ifndef CONFIG_NO_RADIUS
1856 radius_msg_free(sm->last_recv_radius);
1857 sm->last_recv_radius = NULL;
1858#endif /* CONFIG_NO_RADIUS */
1859
1860 if (sm->eap_if->eapTimeout) {
1861 /*
1862 * Disconnect the STA since it did not reply to the last EAP
1863 * request and we cannot continue EAP processing (EAP-Failure
1864 * could only be sent if the EAP peer actually replied).
1865 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1867 MAC2STR(sta->addr));
1868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 sm->eap_if->portEnabled = FALSE;
1870 ap_sta_disconnect(hapd, sta, sta->addr,
1871 WLAN_REASON_PREV_AUTH_NOT_VALID);
1872 }
1873}
1874
1875
1876static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1877{
1878 struct eapol_authenticator *eapol = hapd->eapol_auth;
1879
1880 if (hapd->conf->default_wep_key_len < 1)
1881 return 0;
1882
1883 os_free(eapol->default_wep_key);
1884 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1885 if (eapol->default_wep_key == NULL ||
1886 random_get_bytes(eapol->default_wep_key,
1887 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001888 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 os_free(eapol->default_wep_key);
1890 eapol->default_wep_key = NULL;
1891 return -1;
1892 }
1893
1894 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1895 eapol->default_wep_key,
1896 hapd->conf->default_wep_key_len);
1897
1898 return 0;
1899}
1900
1901
1902static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1903 struct sta_info *sta, void *ctx)
1904{
1905 if (sta->eapol_sm) {
1906 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1907 eapol_auth_step(sta->eapol_sm);
1908 }
1909 return 0;
1910}
1911
1912
1913static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1914{
1915 struct hostapd_data *hapd = eloop_ctx;
1916 struct eapol_authenticator *eapol = hapd->eapol_auth;
1917
1918 if (eapol->default_wep_key_idx >= 3)
1919 eapol->default_wep_key_idx =
1920 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1921 else
1922 eapol->default_wep_key_idx++;
1923
1924 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1925 eapol->default_wep_key_idx);
1926
1927 if (ieee802_1x_rekey_broadcast(hapd)) {
1928 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1929 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1930 "new broadcast key");
1931 os_free(eapol->default_wep_key);
1932 eapol->default_wep_key = NULL;
1933 return;
1934 }
1935
1936 /* TODO: Could setup key for RX here, but change default TX keyid only
1937 * after new broadcast key has been sent to all stations. */
1938 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1939 broadcast_ether_addr,
1940 eapol->default_wep_key_idx, 1, NULL, 0,
1941 eapol->default_wep_key,
1942 hapd->conf->default_wep_key_len)) {
1943 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1944 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1945 "new broadcast key");
1946 os_free(eapol->default_wep_key);
1947 eapol->default_wep_key = NULL;
1948 return;
1949 }
1950
1951 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1952
1953 if (hapd->conf->wep_rekeying_period > 0) {
1954 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1955 ieee802_1x_rekey, hapd, NULL);
1956 }
1957}
1958
1959
1960static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1961 const u8 *data, size_t datalen)
1962{
1963#ifdef CONFIG_WPS
1964 struct sta_info *sta = sta_ctx;
1965
1966 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1967 WLAN_STA_MAYBE_WPS) {
1968 const u8 *identity;
1969 size_t identity_len;
1970 struct eapol_state_machine *sm = sta->eapol_sm;
1971
1972 identity = eap_get_identity(sm->eap, &identity_len);
1973 if (identity &&
1974 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1975 os_memcmp(identity, WSC_ID_ENROLLEE,
1976 WSC_ID_ENROLLEE_LEN) == 0) ||
1977 (identity_len == WSC_ID_REGISTRAR_LEN &&
1978 os_memcmp(identity, WSC_ID_REGISTRAR,
1979 WSC_ID_REGISTRAR_LEN) == 0))) {
1980 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1981 "WLAN_STA_WPS");
1982 sta->flags |= WLAN_STA_WPS;
1983 }
1984 }
1985#endif /* CONFIG_WPS */
1986
1987 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1988}
1989
1990
1991static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1992 const u8 *data, size_t datalen)
1993{
1994#ifndef CONFIG_NO_RADIUS
1995 struct hostapd_data *hapd = ctx;
1996 struct sta_info *sta = sta_ctx;
1997
1998 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1999#endif /* CONFIG_NO_RADIUS */
2000}
2001
2002
2003static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002004 int preauth, int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005{
2006 struct hostapd_data *hapd = ctx;
2007 struct sta_info *sta = sta_ctx;
2008 if (preauth)
2009 rsn_preauth_finished(hapd, sta, success);
2010 else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002011 ieee802_1x_finished(hapd, sta, success, remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012}
2013
2014
2015static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2016 size_t identity_len, int phase2,
2017 struct eap_user *user)
2018{
2019 struct hostapd_data *hapd = ctx;
2020 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002021 int i;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002022 int rv = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002024 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 if (eap_user == NULL)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002026 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027
2028 os_memset(user, 0, sizeof(*user));
2029 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002030 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 user->methods[i].vendor = eap_user->methods[i].vendor;
2032 user->methods[i].method = eap_user->methods[i].method;
2033 }
2034
2035 if (eap_user->password) {
2036 user->password = os_malloc(eap_user->password_len);
2037 if (user->password == NULL)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002038 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 os_memcpy(user->password, eap_user->password,
2040 eap_user->password_len);
2041 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002042 user->password_hash = eap_user->password_hash;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 }
2044 user->force_version = eap_user->force_version;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002045 user->macacl = eap_user->macacl;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002047 user->remediation = eap_user->remediation;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002048 rv = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002050out:
2051 if (rv)
2052 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2053
2054 return rv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055}
2056
2057
2058static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2059{
2060 struct hostapd_data *hapd = ctx;
2061 struct sta_info *sta;
2062 sta = ap_get_sta(hapd, addr);
2063 if (sta == NULL || sta->eapol_sm == NULL)
2064 return 0;
2065 return 1;
2066}
2067
2068
2069static void ieee802_1x_logger(void *ctx, const u8 *addr,
2070 eapol_logger_level level, const char *txt)
2071{
2072#ifndef CONFIG_NO_HOSTAPD_LOGGER
2073 struct hostapd_data *hapd = ctx;
2074 int hlevel;
2075
2076 switch (level) {
2077 case EAPOL_LOGGER_WARNING:
2078 hlevel = HOSTAPD_LEVEL_WARNING;
2079 break;
2080 case EAPOL_LOGGER_INFO:
2081 hlevel = HOSTAPD_LEVEL_INFO;
2082 break;
2083 case EAPOL_LOGGER_DEBUG:
2084 default:
2085 hlevel = HOSTAPD_LEVEL_DEBUG;
2086 break;
2087 }
2088
2089 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2090 txt);
2091#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2092}
2093
2094
2095static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2096 int authorized)
2097{
2098 struct hostapd_data *hapd = ctx;
2099 struct sta_info *sta = sta_ctx;
2100 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2101}
2102
2103
2104static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2105{
2106 struct hostapd_data *hapd = ctx;
2107 struct sta_info *sta = sta_ctx;
2108 ieee802_1x_abort_auth(hapd, sta);
2109}
2110
2111
2112static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2113{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002114#ifndef CONFIG_FIPS
2115#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 struct hostapd_data *hapd = ctx;
2117 struct sta_info *sta = sta_ctx;
2118 ieee802_1x_tx_key(hapd, sta);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002119#endif /* CONFIG_NO_RC4 */
2120#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121}
2122
2123
2124static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2125 enum eapol_event type)
2126{
2127 /* struct hostapd_data *hapd = ctx; */
2128 struct sta_info *sta = sta_ctx;
2129 switch (type) {
2130 case EAPOL_AUTH_SM_CHANGE:
2131 wpa_auth_sm_notify(sta->wpa_sm);
2132 break;
2133 case EAPOL_AUTH_REAUTHENTICATE:
2134 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2135 break;
2136 }
2137}
2138
2139
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002140#ifdef CONFIG_ERP
2141
2142static struct eap_server_erp_key *
2143ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2144{
2145 struct hostapd_data *hapd = ctx;
2146 struct eap_server_erp_key *erp;
2147
2148 dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2149 list) {
2150 if (os_strcmp(erp->keyname_nai, keyname) == 0)
2151 return erp;
2152 }
2153
2154 return NULL;
2155}
2156
2157
2158static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2159{
2160 struct hostapd_data *hapd = ctx;
2161
2162 dl_list_add(&hapd->erp_keys, &erp->list);
2163 return 0;
2164}
2165
2166#endif /* CONFIG_ERP */
2167
2168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169int ieee802_1x_init(struct hostapd_data *hapd)
2170{
2171 int i;
2172 struct eapol_auth_config conf;
2173 struct eapol_auth_cb cb;
2174
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002175 dl_list_init(&hapd->erp_keys);
2176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 os_memset(&conf, 0, sizeof(conf));
2178 conf.ctx = hapd;
2179 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2180 conf.wpa = hapd->conf->wpa;
2181 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2182 conf.eap_server = hapd->conf->eap_server;
2183 conf.ssl_ctx = hapd->ssl_ctx;
2184 conf.msg_ctx = hapd->msg_ctx;
2185 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
2186 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2187 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002188 conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2189 conf.erp_domain = hapd->conf->erp_domain;
2190 conf.erp = hapd->conf->eap_server_erp;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002191 conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
2193 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2194 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
2195 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
2196 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
2197 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
2198 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
2199 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
2200 conf.tnc = hapd->conf->tnc;
2201 conf.wps = hapd->wps;
2202 conf.fragment_size = hapd->conf->fragment_size;
2203 conf.pwd_group = hapd->conf->pwd_group;
Jouni Malinen87fd2792011-05-16 18:35:42 +03002204 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002205 if (hapd->conf->server_id) {
2206 conf.server_id = (const u8 *) hapd->conf->server_id;
2207 conf.server_id_len = os_strlen(hapd->conf->server_id);
2208 } else {
2209 conf.server_id = (const u8 *) "hostapd";
2210 conf.server_id_len = 7;
2211 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212
2213 os_memset(&cb, 0, sizeof(cb));
2214 cb.eapol_send = ieee802_1x_eapol_send;
2215 cb.aaa_send = ieee802_1x_aaa_send;
2216 cb.finished = _ieee802_1x_finished;
2217 cb.get_eap_user = ieee802_1x_get_eap_user;
2218 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2219 cb.logger = ieee802_1x_logger;
2220 cb.set_port_authorized = ieee802_1x_set_port_authorized;
2221 cb.abort_auth = _ieee802_1x_abort_auth;
2222 cb.tx_key = _ieee802_1x_tx_key;
2223 cb.eapol_event = ieee802_1x_eapol_event;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002224#ifdef CONFIG_ERP
2225 cb.erp_get_key = ieee802_1x_erp_get_key;
2226 cb.erp_add_key = ieee802_1x_erp_add_key;
2227#endif /* CONFIG_ERP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228
2229 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2230 if (hapd->eapol_auth == NULL)
2231 return -1;
2232
2233 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2234 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2235 return -1;
2236
2237#ifndef CONFIG_NO_RADIUS
2238 if (radius_client_register(hapd->radius, RADIUS_AUTH,
2239 ieee802_1x_receive_auth, hapd))
2240 return -1;
2241#endif /* CONFIG_NO_RADIUS */
2242
2243 if (hapd->conf->default_wep_key_len) {
2244 for (i = 0; i < 4; i++)
2245 hostapd_drv_set_key(hapd->conf->iface, hapd,
2246 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
2247 NULL, 0);
2248
2249 ieee802_1x_rekey(hapd, NULL);
2250
2251 if (hapd->eapol_auth->default_wep_key == NULL)
2252 return -1;
2253 }
2254
2255 return 0;
2256}
2257
2258
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002259void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2260{
2261 struct eap_server_erp_key *erp;
2262
2263 while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2264 list)) != NULL) {
2265 dl_list_del(&erp->list);
2266 bin_clear_free(erp, sizeof(*erp));
2267 }
2268}
2269
2270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271void ieee802_1x_deinit(struct hostapd_data *hapd)
2272{
2273 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002275 if (hapd->driver && hapd->drv_priv &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 (hapd->conf->ieee802_1x || hapd->conf->wpa))
2277 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2278
2279 eapol_auth_deinit(hapd->eapol_auth);
2280 hapd->eapol_auth = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002281
2282 ieee802_1x_erp_flush(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283}
2284
2285
2286int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2287 const u8 *buf, size_t len, int ack)
2288{
2289 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 u8 *pos;
2291 const unsigned char rfc1042_hdr[ETH_ALEN] =
2292 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2293
2294 if (sta == NULL)
2295 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002296 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 return 0;
2298
2299 hdr = (struct ieee80211_hdr *) buf;
2300 pos = (u8 *) (hdr + 1);
2301 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2302 return 0;
2303 pos += sizeof(rfc1042_hdr);
2304 if (WPA_GET_BE16(pos) != ETH_P_PAE)
2305 return 0;
2306 pos += 2;
2307
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002308 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2309 ack);
2310}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312
2313int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2314 const u8 *buf, int len, int ack)
2315{
2316 const struct ieee802_1x_hdr *xhdr =
2317 (const struct ieee802_1x_hdr *) buf;
2318 const u8 *pos = buf + sizeof(*xhdr);
2319 struct ieee802_1x_eapol_key *key;
2320
2321 if (len < (int) sizeof(*xhdr))
2322 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2324 "type=%d length=%d - ack=%d",
2325 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2326 be_to_host16(xhdr->length), ack);
2327
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002328 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2329 return 0;
2330
2331 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332 const struct wpa_eapol_key *wpa;
2333 wpa = (const struct wpa_eapol_key *) pos;
2334 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2335 wpa->type == EAPOL_KEY_TYPE_WPA)
2336 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2337 sta->wpa_sm, ack);
2338 }
2339
2340 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2341 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002342 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 * packets couple of times because otherwise STA keys become
2344 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002345 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 key = (struct ieee802_1x_eapol_key *) pos;
2347 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2348 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2349 "frame (%scast index=%d)",
2350 key->key_index & BIT(7) ? "uni" : "broad",
2351 key->key_index & ~BIT(7));
2352 /* TODO: re-send EAPOL-Key couple of times (with short delay
2353 * between them?). If all attempt fail, report error and
2354 * deauthenticate STA so that it will get new keys when
2355 * authenticating again (e.g., after returning in range).
2356 * Separate limit/transmit state needed both for unicast and
2357 * broadcast keys(?) */
2358 }
2359 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2360 * to here and change the key only if the EAPOL-Key packet was Acked.
2361 */
2362
2363 return 1;
2364}
2365
2366
2367u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2368{
2369 if (sm == NULL || sm->identity == NULL)
2370 return NULL;
2371
2372 *len = sm->identity_len;
2373 return sm->identity;
2374}
2375
2376
2377u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2378 int idx)
2379{
2380 if (sm == NULL || sm->radius_class.attr == NULL ||
2381 idx >= (int) sm->radius_class.count)
2382 return NULL;
2383
2384 *len = sm->radius_class.attr[idx].len;
2385 return sm->radius_class.attr[idx].data;
2386}
2387
2388
Dmitry Shmidt04949592012-07-19 12:16:46 -07002389struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2390{
2391 if (sm == NULL)
2392 return NULL;
2393 return sm->radius_cui;
2394}
2395
2396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2398{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002399 *len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 if (sm == NULL)
2401 return NULL;
2402
2403 *len = sm->eap_if->eapKeyDataLen;
2404 return sm->eap_if->eapKeyData;
2405}
2406
2407
2408void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2409 int enabled)
2410{
2411 if (sm == NULL)
2412 return;
2413 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2414 eapol_auth_step(sm);
2415}
2416
2417
2418void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2419 int valid)
2420{
2421 if (sm == NULL)
2422 return;
2423 sm->portValid = valid ? TRUE : FALSE;
2424 eapol_auth_step(sm);
2425}
2426
2427
2428void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2429{
2430 if (sm == NULL)
2431 return;
2432 if (pre_auth)
2433 sm->flags |= EAPOL_SM_PREAUTH;
2434 else
2435 sm->flags &= ~EAPOL_SM_PREAUTH;
2436}
2437
2438
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002439static const char * bool_txt(Boolean val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002441 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442}
2443
2444
2445int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2446{
2447 /* TODO */
2448 return 0;
2449}
2450
2451
2452int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2453 char *buf, size_t buflen)
2454{
2455 int len = 0, ret;
2456 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002457 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002458 const char *name1;
2459 const char *name2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460
2461 if (sm == NULL)
2462 return 0;
2463
2464 ret = os_snprintf(buf + len, buflen - len,
2465 "dot1xPaePortNumber=%d\n"
2466 "dot1xPaePortProtocolVersion=%d\n"
2467 "dot1xPaePortCapabilities=1\n"
2468 "dot1xPaePortInitialize=%d\n"
2469 "dot1xPaePortReauthenticate=FALSE\n",
2470 sta->aid,
2471 EAPOL_VERSION,
2472 sm->initialize);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002473 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 return len;
2475 len += ret;
2476
2477 /* dot1xAuthConfigTable */
2478 ret = os_snprintf(buf + len, buflen - len,
2479 "dot1xAuthPaeState=%d\n"
2480 "dot1xAuthBackendAuthState=%d\n"
2481 "dot1xAuthAdminControlledDirections=%d\n"
2482 "dot1xAuthOperControlledDirections=%d\n"
2483 "dot1xAuthAuthControlledPortStatus=%d\n"
2484 "dot1xAuthAuthControlledPortControl=%d\n"
2485 "dot1xAuthQuietPeriod=%u\n"
2486 "dot1xAuthServerTimeout=%u\n"
2487 "dot1xAuthReAuthPeriod=%u\n"
2488 "dot1xAuthReAuthEnabled=%s\n"
2489 "dot1xAuthKeyTxEnabled=%s\n",
2490 sm->auth_pae_state + 1,
2491 sm->be_auth_state + 1,
2492 sm->adminControlledDirections,
2493 sm->operControlledDirections,
2494 sm->authPortStatus,
2495 sm->portControl,
2496 sm->quietPeriod,
2497 sm->serverTimeout,
2498 sm->reAuthPeriod,
2499 bool_txt(sm->reAuthEnabled),
2500 bool_txt(sm->keyTxEnabled));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002501 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 return len;
2503 len += ret;
2504
2505 /* dot1xAuthStatsTable */
2506 ret = os_snprintf(buf + len, buflen - len,
2507 "dot1xAuthEapolFramesRx=%u\n"
2508 "dot1xAuthEapolFramesTx=%u\n"
2509 "dot1xAuthEapolStartFramesRx=%u\n"
2510 "dot1xAuthEapolLogoffFramesRx=%u\n"
2511 "dot1xAuthEapolRespIdFramesRx=%u\n"
2512 "dot1xAuthEapolRespFramesRx=%u\n"
2513 "dot1xAuthEapolReqIdFramesTx=%u\n"
2514 "dot1xAuthEapolReqFramesTx=%u\n"
2515 "dot1xAuthInvalidEapolFramesRx=%u\n"
2516 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2517 "dot1xAuthLastEapolFrameVersion=%u\n"
2518 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2519 sm->dot1xAuthEapolFramesRx,
2520 sm->dot1xAuthEapolFramesTx,
2521 sm->dot1xAuthEapolStartFramesRx,
2522 sm->dot1xAuthEapolLogoffFramesRx,
2523 sm->dot1xAuthEapolRespIdFramesRx,
2524 sm->dot1xAuthEapolRespFramesRx,
2525 sm->dot1xAuthEapolReqIdFramesTx,
2526 sm->dot1xAuthEapolReqFramesTx,
2527 sm->dot1xAuthInvalidEapolFramesRx,
2528 sm->dot1xAuthEapLengthErrorFramesRx,
2529 sm->dot1xAuthLastEapolFrameVersion,
2530 MAC2STR(sm->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002531 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 return len;
2533 len += ret;
2534
2535 /* dot1xAuthDiagTable */
2536 ret = os_snprintf(buf + len, buflen - len,
2537 "dot1xAuthEntersConnecting=%u\n"
2538 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2539 "dot1xAuthEntersAuthenticating=%u\n"
2540 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2541 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2542 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2543 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2544 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2545 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2546 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2547 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2548 "dot1xAuthBackendResponses=%u\n"
2549 "dot1xAuthBackendAccessChallenges=%u\n"
2550 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2551 "dot1xAuthBackendAuthSuccesses=%u\n"
2552 "dot1xAuthBackendAuthFails=%u\n",
2553 sm->authEntersConnecting,
2554 sm->authEapLogoffsWhileConnecting,
2555 sm->authEntersAuthenticating,
2556 sm->authAuthSuccessesWhileAuthenticating,
2557 sm->authAuthTimeoutsWhileAuthenticating,
2558 sm->authAuthFailWhileAuthenticating,
2559 sm->authAuthEapStartsWhileAuthenticating,
2560 sm->authAuthEapLogoffWhileAuthenticating,
2561 sm->authAuthReauthsWhileAuthenticated,
2562 sm->authAuthEapStartsWhileAuthenticated,
2563 sm->authAuthEapLogoffWhileAuthenticated,
2564 sm->backendResponses,
2565 sm->backendAccessChallenges,
2566 sm->backendOtherRequestsToSupplicant,
2567 sm->backendAuthSuccesses,
2568 sm->backendAuthFails);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002569 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570 return len;
2571 len += ret;
2572
2573 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002574 os_reltime_age(&sta->acct_session_start, &diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 ret = os_snprintf(buf + len, buflen - len,
2576 /* TODO: dot1xAuthSessionOctetsRx */
2577 /* TODO: dot1xAuthSessionOctetsTx */
2578 /* TODO: dot1xAuthSessionFramesRx */
2579 /* TODO: dot1xAuthSessionFramesTx */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002580 "dot1xAuthSessionId=%016llX\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 "dot1xAuthSessionAuthenticMethod=%d\n"
2582 "dot1xAuthSessionTime=%u\n"
2583 "dot1xAuthSessionTerminateCause=999\n"
2584 "dot1xAuthSessionUserName=%s\n",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002585 (unsigned long long) sta->acct_session_id,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586 (wpa_key_mgmt_wpa_ieee8021x(
2587 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2588 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002589 (unsigned int) diff.sec,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 sm->identity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002591 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592 return len;
2593 len += ret;
2594
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002595 if (sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002596 ret = os_snprintf(buf + len, buflen - len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002597 "authMultiSessionId=%016llX\n",
2598 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -08002599 sm->acct_multi_session_id);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002600 if (os_snprintf_error(buflen - len, ret))
2601 return len;
2602 len += ret;
2603 }
2604
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002605 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2606 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002607 ret = os_snprintf(buf + len, buflen - len,
2608 "last_eap_type_as=%d (%s)\n"
2609 "last_eap_type_sta=%d (%s)\n",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002610 sm->eap_type_authsrv, name1,
2611 sm->eap_type_supp, name2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002612 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002613 return len;
2614 len += ret;
2615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 return len;
2617}
2618
2619
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002620#ifdef CONFIG_HS20
2621static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
2622{
2623 struct hostapd_data *hapd = eloop_ctx;
2624 struct sta_info *sta = timeout_ctx;
2625
2626 if (sta->remediation) {
2627 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2628 MACSTR " to indicate Subscription Remediation",
2629 MAC2STR(sta->addr));
2630 hs20_send_wnm_notification(hapd, sta->addr,
2631 sta->remediation_method,
2632 sta->remediation_url);
2633 os_free(sta->remediation_url);
2634 sta->remediation_url = NULL;
2635 }
2636
2637 if (sta->hs20_deauth_req) {
2638 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
2639 MACSTR " to indicate imminent deauthentication",
2640 MAC2STR(sta->addr));
2641 hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
2642 sta->hs20_deauth_req);
2643 }
2644}
2645#endif /* CONFIG_HS20 */
2646
2647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002649 struct sta_info *sta, int success,
2650 int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651{
2652 const u8 *key;
2653 size_t len;
2654 /* TODO: get PMKLifetime from WPA parameters */
2655 static const int dot11RSNAConfigPMKLifetime = 43200;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002656 unsigned int session_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002658#ifdef CONFIG_HS20
2659 if (remediation && !sta->remediation) {
2660 sta->remediation = 1;
2661 os_free(sta->remediation_url);
2662 sta->remediation_url =
2663 os_strdup(hapd->conf->subscr_remediation_url);
2664 sta->remediation_method = 1; /* SOAP-XML SPP */
2665 }
2666
Dmitry Shmidtde47be72016-01-07 12:52:55 -08002667 if (success && (sta->remediation || sta->hs20_deauth_req)) {
2668 wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
2669 MACSTR " in 100 ms", MAC2STR(sta->addr));
2670 eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
2671 eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
2672 hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002673 }
2674#endif /* CONFIG_HS20 */
2675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002677 if (sta->session_timeout_set)
2678 session_timeout = sta->session_timeout;
2679 else
2680 session_timeout = dot11RSNAConfigPMKLifetime;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002681 if (success && key && len >= PMK_LEN && !sta->remediation &&
2682 !sta->hs20_deauth_requested &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002683 wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 sta->eapol_sm) == 0) {
2685 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2686 HOSTAPD_LEVEL_DEBUG,
2687 "Added PMKSA cache entry (IEEE 802.1X)");
2688 }
2689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002690 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691 /*
2692 * Many devices require deauthentication after WPS provisioning
2693 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002694 * disconnect the client here. In addition, this may also
2695 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2696 * the EAPOL PAE state machine would remain in HELD state for
2697 * considerable amount of time and some EAP methods, like
2698 * EAP-FAST with anonymous provisioning, may require another
2699 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002701 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2702 "disconnection after EAP-Failure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 /* Add a small sleep to increase likelihood of previously
2704 * requested EAP-Failure TX getting out before this should the
2705 * driver reorder operations.
2706 */
2707 os_sleep(0, 10000);
2708 ap_sta_disconnect(hapd, sta, sta->addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002709 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002710 hostapd_wps_eap_completed(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712}