blob: b12c9d6c72df38ab715229dbeb8bf345d3937ce2 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / IEEE 802.1X-2004 Authenticator
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "crypto/md5.h"
14#include "crypto/crypto.h"
15#include "crypto/random.h"
16#include "common/ieee802_11_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "radius/radius.h"
18#include "radius/radius_client.h"
19#include "eap_server/eap.h"
20#include "eap_common/eap_wsc_common.h"
21#include "eapol_auth/eapol_auth_sm.h"
22#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080023#include "p2p/p2p.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "hostapd.h"
25#include "accounting.h"
26#include "sta_info.h"
27#include "wpa_auth.h"
28#include "preauth_auth.h"
29#include "pmksa_cache_auth.h"
30#include "ap_config.h"
31#include "ap_drv_ops.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080032#include "wps_hostapd.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080033#include "hs20.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "ieee802_1x.h"
35
36
37static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080038 struct sta_info *sta, int success,
39 int remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
43 u8 type, const u8 *data, size_t datalen)
44{
45 u8 *buf;
46 struct ieee802_1x_hdr *xhdr;
47 size_t len;
48 int encrypt = 0;
49
50 len = sizeof(*xhdr) + datalen;
51 buf = os_zalloc(len);
52 if (buf == NULL) {
53 wpa_printf(MSG_ERROR, "malloc() failed for "
54 "ieee802_1x_send(len=%lu)",
55 (unsigned long) len);
56 return;
57 }
58
59 xhdr = (struct ieee802_1x_hdr *) buf;
60 xhdr->version = hapd->conf->eapol_version;
61 xhdr->type = type;
62 xhdr->length = host_to_be16(datalen);
63
64 if (datalen > 0 && data != NULL)
65 os_memcpy(xhdr + 1, data, datalen);
66
67 if (wpa_auth_pairwise_set(sta->wpa_sm))
68 encrypt = 1;
69 if (sta->flags & WLAN_STA_PREAUTH) {
70 rsn_preauth_send(hapd, sta, buf, len);
71 } else {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080072 hostapd_drv_hapd_send_eapol(
73 hapd, sta->addr, buf, len,
74 encrypt, hostapd_sta_flags_to_drv(sta->flags));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075 }
76
77 os_free(buf);
78}
79
80
81void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
82 struct sta_info *sta, int authorized)
83{
84 int res;
85
86 if (sta->flags & WLAN_STA_PREAUTH)
87 return;
88
89 if (authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090 ap_sta_set_authorized(hapd, sta, 1);
91 res = hostapd_set_authorized(hapd, sta, 1);
92 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
93 HOSTAPD_LEVEL_DEBUG, "authorizing port");
94 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 ap_sta_set_authorized(hapd, sta, 0);
96 res = hostapd_set_authorized(hapd, sta, 0);
97 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
98 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
99 }
100
101 if (res && errno != ENOENT) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700102 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
103 " flags for kernel driver (errno=%d).",
104 MAC2STR(sta->addr), errno);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 }
106
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800107 if (authorized) {
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800108 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
115 struct sta_info *sta,
116 int idx, int broadcast,
117 u8 *key_data, size_t key_len)
118{
119 u8 *buf, *ekey;
120 struct ieee802_1x_hdr *hdr;
121 struct ieee802_1x_eapol_key *key;
122 size_t len, ekey_len;
123 struct eapol_state_machine *sm = sta->eapol_sm;
124
125 if (sm == NULL)
126 return;
127
128 len = sizeof(*key) + key_len;
129 buf = os_zalloc(sizeof(*hdr) + len);
130 if (buf == NULL)
131 return;
132
133 hdr = (struct ieee802_1x_hdr *) buf;
134 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
135 key->type = EAPOL_KEY_TYPE_RC4;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700136 WPA_PUT_BE16(key->key_length, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 wpa_get_ntp_timestamp(key->replay_counter);
138
139 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
140 wpa_printf(MSG_ERROR, "Could not get random numbers");
141 os_free(buf);
142 return;
143 }
144
145 key->key_index = idx | (broadcast ? 0 : BIT(7));
146 if (hapd->conf->eapol_key_index_workaround) {
147 /* According to some information, WinXP Supplicant seems to
148 * interpret bit7 as an indication whether the key is to be
149 * activated, so make it possible to enable workaround that
150 * sets this bit for all keys. */
151 key->key_index |= BIT(7);
152 }
153
154 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
155 * MSK[32..63] is used to sign the message. */
156 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
157 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
158 "and signing EAPOL-Key");
159 os_free(buf);
160 return;
161 }
162 os_memcpy((u8 *) (key + 1), key_data, key_len);
163 ekey_len = sizeof(key->key_iv) + 32;
164 ekey = os_malloc(ekey_len);
165 if (ekey == NULL) {
166 wpa_printf(MSG_ERROR, "Could not encrypt key");
167 os_free(buf);
168 return;
169 }
170 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
171 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
172 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
173 os_free(ekey);
174
175 /* This header is needed here for HMAC-MD5, but it will be regenerated
176 * in ieee802_1x_send() */
177 hdr->version = hapd->conf->eapol_version;
178 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
179 hdr->length = host_to_be16(len);
180 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
181 key->key_signature);
182
183 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
184 " (%s index=%d)", MAC2STR(sm->addr),
185 broadcast ? "broadcast" : "unicast", idx);
186 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
187 if (sta->eapol_sm)
188 sta->eapol_sm->dot1xAuthEapolFramesTx++;
189 os_free(buf);
190}
191
192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
194{
195 struct eapol_authenticator *eapol = hapd->eapol_auth;
196 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197
198 if (sm == NULL || !sm->eap_if->eapKeyData)
199 return;
200
201 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
202 MAC2STR(sta->addr));
203
204#ifndef CONFIG_NO_VLAN
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700205 if (sta->vlan_id > 0 && sta->vlan_id <= MAX_VLAN_ID) {
206 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
207 return;
208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209#endif /* CONFIG_NO_VLAN */
Dmitry Shmidt5393a0f2013-08-08 11:23:34 -0700210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 if (eapol->default_wep_key) {
212 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
213 eapol->default_wep_key,
214 hapd->conf->default_wep_key_len);
215 }
216
217 if (hapd->conf->individual_wep_key_len > 0) {
218 u8 *ikey;
219 ikey = os_malloc(hapd->conf->individual_wep_key_len);
220 if (ikey == NULL ||
221 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
222 {
223 wpa_printf(MSG_ERROR, "Could not generate random "
224 "individual WEP key.");
225 os_free(ikey);
226 return;
227 }
228
229 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
230 ikey, hapd->conf->individual_wep_key_len);
231
232 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
233 hapd->conf->individual_wep_key_len);
234
235 /* TODO: set encryption in TX callback, i.e., only after STA
236 * has ACKed EAPOL-Key frame */
237 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
238 sta->addr, 0, 1, NULL, 0, ikey,
239 hapd->conf->individual_wep_key_len)) {
240 wpa_printf(MSG_ERROR, "Could not set individual WEP "
241 "encryption.");
242 }
243
244 os_free(ikey);
245 }
246}
247
248
249const char *radius_mode_txt(struct hostapd_data *hapd)
250{
251 switch (hapd->iface->conf->hw_mode) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800252 case HOSTAPD_MODE_IEEE80211AD:
253 return "802.11ad";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 case HOSTAPD_MODE_IEEE80211A:
255 return "802.11a";
256 case HOSTAPD_MODE_IEEE80211G:
257 return "802.11g";
258 case HOSTAPD_MODE_IEEE80211B:
259 default:
260 return "802.11b";
261 }
262}
263
264
265int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
266{
267 int i;
268 u8 rate = 0;
269
270 for (i = 0; i < sta->supported_rates_len; i++)
271 if ((sta->supported_rates[i] & 0x7f) > rate)
272 rate = sta->supported_rates[i] & 0x7f;
273
274 return rate;
275}
276
277
278#ifndef CONFIG_NO_RADIUS
279static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
280 struct eapol_state_machine *sm,
281 const u8 *eap, size_t len)
282{
283 const u8 *identity;
284 size_t identity_len;
285
286 if (len <= sizeof(struct eap_hdr) ||
287 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
288 return;
289
290 identity = eap_get_identity(sm->eap, &identity_len);
291 if (identity == NULL)
292 return;
293
294 /* Save station identity for future RADIUS packets */
295 os_free(sm->identity);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700296 sm->identity = (u8 *) dup_binstr(identity, identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 if (sm->identity == NULL) {
298 sm->identity_len = 0;
299 return;
300 }
301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 sm->identity_len = identity_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
304 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
305 sm->dot1xAuthEapolRespIdFramesRx++;
306}
307
308
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309static int add_common_radius_sta_attr(struct hostapd_data *hapd,
310 struct hostapd_radius_attr *req_attr,
311 struct sta_info *sta,
312 struct radius_msg *msg)
313{
314 char buf[128];
315
316 if (!hostapd_config_get_radius_attr(req_attr,
317 RADIUS_ATTR_NAS_PORT) &&
318 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
319 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
320 return -1;
321 }
322
323 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
324 MAC2STR(sta->addr));
325 buf[sizeof(buf) - 1] = '\0';
326 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
327 (u8 *) buf, os_strlen(buf))) {
328 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
329 return -1;
330 }
331
332 if (sta->flags & WLAN_STA_PREAUTH) {
333 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
334 sizeof(buf));
335 } else {
336 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
337 radius_sta_rate(hapd, sta) / 2,
338 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
339 radius_mode_txt(hapd));
340 buf[sizeof(buf) - 1] = '\0';
341 }
342 if (!hostapd_config_get_radius_attr(req_attr,
343 RADIUS_ATTR_CONNECT_INFO) &&
344 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
345 (u8 *) buf, os_strlen(buf))) {
346 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
347 return -1;
348 }
349
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800350 if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
351 os_snprintf(buf, sizeof(buf), "%08X-%08X",
352 sta->acct_session_id_hi, sta->acct_session_id_lo);
353 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
354 (u8 *) buf, os_strlen(buf))) {
355 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
356 return -1;
357 }
358 }
359
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 return 0;
361}
362
363
364int add_common_radius_attr(struct hostapd_data *hapd,
365 struct hostapd_radius_attr *req_attr,
366 struct sta_info *sta,
367 struct radius_msg *msg)
368{
369 char buf[128];
370 struct hostapd_radius_attr *attr;
371
372 if (!hostapd_config_get_radius_attr(req_attr,
373 RADIUS_ATTR_NAS_IP_ADDRESS) &&
374 hapd->conf->own_ip_addr.af == AF_INET &&
375 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
376 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
377 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
378 return -1;
379 }
380
381#ifdef CONFIG_IPV6
382 if (!hostapd_config_get_radius_attr(req_attr,
383 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
384 hapd->conf->own_ip_addr.af == AF_INET6 &&
385 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
386 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
387 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
388 return -1;
389 }
390#endif /* CONFIG_IPV6 */
391
392 if (!hostapd_config_get_radius_attr(req_attr,
393 RADIUS_ATTR_NAS_IDENTIFIER) &&
394 hapd->conf->nas_identifier &&
395 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
396 (u8 *) hapd->conf->nas_identifier,
397 os_strlen(hapd->conf->nas_identifier))) {
398 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
399 return -1;
400 }
401
402 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
403 MAC2STR(hapd->own_addr),
404 wpa_ssid_txt(hapd->conf->ssid.ssid,
405 hapd->conf->ssid.ssid_len));
406 buf[sizeof(buf) - 1] = '\0';
407 if (!hostapd_config_get_radius_attr(req_attr,
408 RADIUS_ATTR_CALLED_STATION_ID) &&
409 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
410 (u8 *) buf, os_strlen(buf))) {
411 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
412 return -1;
413 }
414
415 if (!hostapd_config_get_radius_attr(req_attr,
416 RADIUS_ATTR_NAS_PORT_TYPE) &&
417 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
418 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
419 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
420 return -1;
421 }
422
423 if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
424 return -1;
425
426 for (attr = req_attr; attr; attr = attr->next) {
427 if (!radius_msg_add_attr(msg, attr->type,
428 wpabuf_head(attr->val),
429 wpabuf_len(attr->val))) {
430 wpa_printf(MSG_ERROR, "Could not add RADIUS "
431 "attribute");
432 return -1;
433 }
434 }
435
436 return 0;
437}
438
439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
441 struct sta_info *sta,
442 const u8 *eap, size_t len)
443{
444 struct radius_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 struct eapol_state_machine *sm = sta->eapol_sm;
446
447 if (sm == NULL)
448 return;
449
450 ieee802_1x_learn_identity(hapd, sm, eap, len);
451
452 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
453 "packet");
454
455 sm->radius_identifier = radius_client_get_id(hapd->radius);
456 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
457 sm->radius_identifier);
458 if (msg == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800459 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 return;
461 }
462
463 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
464
465 if (sm->identity &&
466 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
467 sm->identity, sm->identity_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800468 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 goto fail;
470 }
471
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700472 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
473 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475
476 /* TODO: should probably check MTU from driver config; 2304 is max for
477 * IEEE 802.11, but use 1400 to avoid problems with too large packets
478 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700479 if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
480 RADIUS_ATTR_FRAMED_MTU) &&
481 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800482 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 goto fail;
484 }
485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 if (eap && !radius_msg_add_eap(msg, eap, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800487 wpa_printf(MSG_INFO, "Could not add EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 goto fail;
489 }
490
491 /* State attribute must be copied if and only if this packet is
492 * Access-Request reply to the previous Access-Challenge */
493 if (sm->last_recv_radius &&
494 radius_msg_get_hdr(sm->last_recv_radius)->code ==
495 RADIUS_CODE_ACCESS_CHALLENGE) {
496 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
497 RADIUS_ATTR_STATE);
498 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800499 wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 goto fail;
501 }
502 if (res > 0) {
503 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
504 }
505 }
506
Dmitry Shmidt04949592012-07-19 12:16:46 -0700507 if (hapd->conf->radius_request_cui) {
508 const u8 *cui;
509 size_t cui_len;
510 /* Add previously learned CUI or nul CUI to request CUI */
511 if (sm->radius_cui) {
512 cui = wpabuf_head(sm->radius_cui);
513 cui_len = wpabuf_len(sm->radius_cui);
514 } else {
515 cui = (const u8 *) "\0";
516 cui_len = 1;
517 }
518 if (!radius_msg_add_attr(msg,
519 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
520 cui, cui_len)) {
521 wpa_printf(MSG_ERROR, "Could not add CUI");
522 goto fail;
523 }
524 }
525
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800526#ifdef CONFIG_HS20
527 if (hapd->conf->hs20) {
528 u8 ver = 1; /* Release 2 */
529 if (!radius_msg_add_wfa(
530 msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
531 &ver, 1)) {
532 wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
533 "version");
534 goto fail;
535 }
536
537 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
538 const u8 *pos;
539 u8 buf[3];
540 u16 id;
541 pos = wpabuf_head_u8(sta->hs20_ie);
542 buf[0] = (*pos) >> 4;
543 if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
544 wpabuf_len(sta->hs20_ie) >= 3)
545 id = WPA_GET_LE16(pos + 1);
546 else
547 id = 0;
548 WPA_PUT_BE16(buf + 1, id);
549 if (!radius_msg_add_wfa(
550 msg,
551 RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
552 buf, sizeof(buf))) {
553 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
554 "STA version");
555 goto fail;
556 }
557 }
558 }
559#endif /* CONFIG_HS20 */
560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
562 goto fail;
563
564 return;
565
566 fail:
567 radius_msg_free(msg);
568}
569#endif /* CONFIG_NO_RADIUS */
570
571
572static void handle_eap_response(struct hostapd_data *hapd,
573 struct sta_info *sta, struct eap_hdr *eap,
574 size_t len)
575{
576 u8 type, *data;
577 struct eapol_state_machine *sm = sta->eapol_sm;
578 if (sm == NULL)
579 return;
580
581 data = (u8 *) (eap + 1);
582
583 if (len < sizeof(*eap) + 1) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800584 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 return;
586 }
587
588 sm->eap_type_supp = type = data[0];
589
590 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
591 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
592 "id=%d len=%d) from STA: EAP Response-%s (%d)",
593 eap->code, eap->identifier, be_to_host16(eap->length),
594 eap_server_get_name(0, type), type);
595
596 sm->dot1xAuthEapolRespFramesRx++;
597
598 wpabuf_free(sm->eap_if->eapRespData);
599 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
600 sm->eapolEap = TRUE;
601}
602
603
604/* Process incoming EAP packet from Supplicant */
605static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
606 u8 *buf, size_t len)
607{
608 struct eap_hdr *eap;
609 u16 eap_len;
610
611 if (len < sizeof(*eap)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800612 wpa_printf(MSG_INFO, " too short EAP packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 return;
614 }
615
616 eap = (struct eap_hdr *) buf;
617
618 eap_len = be_to_host16(eap->length);
619 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
620 eap->code, eap->identifier, eap_len);
621 if (eap_len < sizeof(*eap)) {
622 wpa_printf(MSG_DEBUG, " Invalid EAP length");
623 return;
624 } else if (eap_len > len) {
625 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
626 "packet");
627 return;
628 } else if (eap_len < len) {
629 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
630 "packet", (unsigned long) len - eap_len);
631 }
632
633 switch (eap->code) {
634 case EAP_CODE_REQUEST:
635 wpa_printf(MSG_DEBUG, " (request)");
636 return;
637 case EAP_CODE_RESPONSE:
638 wpa_printf(MSG_DEBUG, " (response)");
639 handle_eap_response(hapd, sta, eap, eap_len);
640 break;
641 case EAP_CODE_SUCCESS:
642 wpa_printf(MSG_DEBUG, " (success)");
643 return;
644 case EAP_CODE_FAILURE:
645 wpa_printf(MSG_DEBUG, " (failure)");
646 return;
647 default:
648 wpa_printf(MSG_DEBUG, " (unknown code)");
649 return;
650 }
651}
652
653
654static struct eapol_state_machine *
655ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
656{
657 int flags = 0;
658 if (sta->flags & WLAN_STA_PREAUTH)
659 flags |= EAPOL_SM_PREAUTH;
660 if (sta->wpa_sm) {
661 flags |= EAPOL_SM_USES_WPA;
662 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
663 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
664 }
665 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700666 sta->wps_ie, sta->p2p_ie, sta,
667 sta->identity, sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668}
669
670
671/**
672 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
673 * @hapd: hostapd BSS data
674 * @sa: Source address (sender of the EAPOL frame)
675 * @buf: EAPOL frame
676 * @len: Length of buf in octets
677 *
678 * This function is called for each incoming EAPOL frame from the interface
679 */
680void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
681 size_t len)
682{
683 struct sta_info *sta;
684 struct ieee802_1x_hdr *hdr;
685 struct ieee802_1x_eapol_key *key;
686 u16 datalen;
687 struct rsn_pmksa_cache_entry *pmksa;
688 int key_mgmt;
689
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800690 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 !hapd->conf->wps_state)
692 return;
693
694 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
695 (unsigned long) len, MAC2STR(sa));
696 sta = ap_get_sta(hapd, sa);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800697 if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
698 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
700 "associated/Pre-authenticating STA");
701 return;
702 }
703
704 if (len < sizeof(*hdr)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800705 wpa_printf(MSG_INFO, " too short IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 return;
707 }
708
709 hdr = (struct ieee802_1x_hdr *) buf;
710 datalen = be_to_host16(hdr->length);
711 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
712 hdr->version, hdr->type, datalen);
713
714 if (len - sizeof(*hdr) < datalen) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800715 wpa_printf(MSG_INFO, " frame too short for this IEEE 802.1X packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 if (sta->eapol_sm)
717 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
718 return;
719 }
720 if (len - sizeof(*hdr) > datalen) {
721 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
722 "IEEE 802.1X packet",
723 (unsigned long) len - sizeof(*hdr) - datalen);
724 }
725
726 if (sta->eapol_sm) {
727 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
728 sta->eapol_sm->dot1xAuthEapolFramesRx++;
729 }
730
731 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
732 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
733 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
734 (key->type == EAPOL_KEY_TYPE_WPA ||
735 key->type == EAPOL_KEY_TYPE_RSN)) {
736 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
737 sizeof(*hdr) + datalen);
738 return;
739 }
740
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800741 if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
743 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
744 "802.1X not enabled and WPS not used");
745 return;
746 }
747
748 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
749 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
750 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
751 "STA is using PSK");
752 return;
753 }
754
755 if (!sta->eapol_sm) {
756 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
757 if (!sta->eapol_sm)
758 return;
759
760#ifdef CONFIG_WPS
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800761 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762 u32 wflags = sta->flags & (WLAN_STA_WPS |
763 WLAN_STA_WPS2 |
764 WLAN_STA_MAYBE_WPS);
765 if (wflags == WLAN_STA_MAYBE_WPS ||
766 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
767 /*
768 * Delay EAPOL frame transmission until a
769 * possible WPS STA initiates the handshake
770 * with EAPOL-Start. Only allow the wait to be
771 * skipped if the STA is known to support WPS
772 * 2.0.
773 */
774 wpa_printf(MSG_DEBUG, "WPS: Do not start "
775 "EAPOL until EAPOL-Start is "
776 "received");
777 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 }
780#endif /* CONFIG_WPS */
781
782 sta->eapol_sm->eap_if->portEnabled = TRUE;
783 }
784
785 /* since we support version 1, we can ignore version field and proceed
786 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
787 /* TODO: actually, we are not version 1 anymore.. However, Version 2
788 * does not change frame contents, so should be ok to process frames
789 * more or less identically. Some changes might be needed for
790 * verification of fields. */
791
792 switch (hdr->type) {
793 case IEEE802_1X_TYPE_EAP_PACKET:
794 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
795 break;
796
797 case IEEE802_1X_TYPE_EAPOL_START:
798 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
799 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
800 "from STA");
801 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
802 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
803 if (pmksa) {
804 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
805 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
806 "available - ignore it since "
807 "STA sent EAPOL-Start");
808 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
809 }
810 sta->eapol_sm->eapolStart = TRUE;
811 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
812 eap_server_clear_identity(sta->eapol_sm->eap);
813 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
814 break;
815
816 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
817 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
818 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
819 "from STA");
820 sta->acct_terminate_cause =
821 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
822 accounting_sta_stop(hapd, sta);
823 sta->eapol_sm->eapolLogoff = TRUE;
824 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
825 eap_server_clear_identity(sta->eapol_sm->eap);
826 break;
827
828 case IEEE802_1X_TYPE_EAPOL_KEY:
829 wpa_printf(MSG_DEBUG, " EAPOL-Key");
830 if (!ap_sta_is_authorized(sta)) {
831 wpa_printf(MSG_DEBUG, " Dropped key data from "
832 "unauthorized Supplicant");
833 break;
834 }
835 break;
836
837 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
838 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
839 /* TODO: implement support for this; show data */
840 break;
841
842 default:
843 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
844 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
845 break;
846 }
847
848 eapol_auth_step(sta->eapol_sm);
849}
850
851
852/**
853 * ieee802_1x_new_station - Start IEEE 802.1X authentication
854 * @hapd: hostapd BSS data
855 * @sta: The station
856 *
857 * This function is called to start IEEE 802.1X authentication when a new
858 * station completes IEEE 802.11 association.
859 */
860void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
861{
862 struct rsn_pmksa_cache_entry *pmksa;
863 int reassoc = 1;
864 int force_1x = 0;
865 int key_mgmt;
866
867#ifdef CONFIG_WPS
868 if (hapd->conf->wps_state && hapd->conf->wpa &&
869 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
870 /*
871 * Need to enable IEEE 802.1X/EAPOL state machines for possible
872 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
873 * authentication in this BSS.
874 */
875 force_1x = 1;
876 }
877#endif /* CONFIG_WPS */
878
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800879 if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
881 "802.1X not enabled or forced for WPS");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700882 /*
883 * Clear any possible EAPOL authenticator state to support
884 * reassociation change from WPS to PSK.
885 */
886 ieee802_1x_free_station(sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 return;
888 }
889
890 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
891 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
892 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 /*
894 * Clear any possible EAPOL authenticator state to support
895 * reassociation change from WPA-EAP to PSK.
896 */
897 ieee802_1x_free_station(sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 return;
899 }
900
901 if (sta->eapol_sm == NULL) {
902 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
903 HOSTAPD_LEVEL_DEBUG, "start authentication");
904 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
905 if (sta->eapol_sm == NULL) {
906 hostapd_logger(hapd, sta->addr,
907 HOSTAPD_MODULE_IEEE8021X,
908 HOSTAPD_LEVEL_INFO,
909 "failed to allocate state machine");
910 return;
911 }
912 reassoc = 0;
913 }
914
915#ifdef CONFIG_WPS
916 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800917 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
918 !(sta->flags & WLAN_STA_WPS2)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800920 * Delay EAPOL frame transmission until a possible WPS STA
921 * initiates the handshake with EAPOL-Start. Only allow the
922 * wait to be skipped if the STA is known to support WPS 2.0.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
925 "EAPOL-Start is received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
927 }
928#endif /* CONFIG_WPS */
929
930 sta->eapol_sm->eap_if->portEnabled = TRUE;
931
932#ifdef CONFIG_IEEE80211R
933 if (sta->auth_alg == WLAN_AUTH_FT) {
934 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
935 HOSTAPD_LEVEL_DEBUG,
936 "PMK from FT - skip IEEE 802.1X/EAP");
937 /* Setup EAPOL state machines to already authenticated state
938 * because of existing FT information from R0KH. */
939 sta->eapol_sm->keyRun = TRUE;
940 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
941 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
942 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
943 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800944 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 if (sta->eapol_sm->eap)
946 eap_sm_notify_cached(sta->eapol_sm->eap);
947 /* TODO: get vlan_id from R0KH using RRB message */
948 return;
949 }
950#endif /* CONFIG_IEEE80211R */
951
952 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
953 if (pmksa) {
954 int old_vlanid;
955
956 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
957 HOSTAPD_LEVEL_DEBUG,
958 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
959 /* Setup EAPOL state machines to already authenticated state
960 * because of existing PMKSA information in the cache. */
961 sta->eapol_sm->keyRun = TRUE;
962 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
963 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
964 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
965 sta->eapol_sm->authSuccess = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800966 sta->eapol_sm->authFail = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 if (sta->eapol_sm->eap)
968 eap_sm_notify_cached(sta->eapol_sm->eap);
969 old_vlanid = sta->vlan_id;
970 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
971 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
972 sta->vlan_id = 0;
973 ap_sta_bind_vlan(hapd, sta, old_vlanid);
974 } else {
975 if (reassoc) {
976 /*
977 * Force EAPOL state machines to start
978 * re-authentication without having to wait for the
979 * Supplicant to send EAPOL-Start.
980 */
981 sta->eapol_sm->reAuthenticate = TRUE;
982 }
983 eapol_auth_step(sta->eapol_sm);
984 }
985}
986
987
988void ieee802_1x_free_station(struct sta_info *sta)
989{
990 struct eapol_state_machine *sm = sta->eapol_sm;
991
992 if (sm == NULL)
993 return;
994
995 sta->eapol_sm = NULL;
996
997#ifndef CONFIG_NO_RADIUS
998 radius_msg_free(sm->last_recv_radius);
999 radius_free_class(&sm->radius_class);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 wpabuf_free(sm->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001#endif /* CONFIG_NO_RADIUS */
1002
1003 os_free(sm->identity);
1004 eapol_auth_free(sm);
1005}
1006
1007
1008#ifndef CONFIG_NO_RADIUS
1009static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1010 struct sta_info *sta)
1011{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 struct wpabuf *eap;
1013 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 int eap_type = -1;
1015 char buf[64];
1016 struct radius_msg *msg;
1017 struct eapol_state_machine *sm = sta->eapol_sm;
1018
1019 if (sm == NULL || sm->last_recv_radius == NULL) {
1020 if (sm)
1021 sm->eap_if->aaaEapNoReq = TRUE;
1022 return;
1023 }
1024
1025 msg = sm->last_recv_radius;
1026
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001027 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 if (eap == NULL) {
1029 /* RFC 3579, Chap. 2.6.3:
1030 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1031 * attribute */
1032 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1033 HOSTAPD_LEVEL_WARNING, "could not extract "
1034 "EAP-Message from RADIUS message");
1035 sm->eap_if->aaaEapNoReq = TRUE;
1036 return;
1037 }
1038
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001039 if (wpabuf_len(eap) < sizeof(*hdr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001040 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1041 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1042 "received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001043 wpabuf_free(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 sm->eap_if->aaaEapNoReq = TRUE;
1045 return;
1046 }
1047
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001048 if (wpabuf_len(eap) > sizeof(*hdr))
1049 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001051 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 switch (hdr->code) {
1053 case EAP_CODE_REQUEST:
1054 if (eap_type >= 0)
1055 sm->eap_type_authsrv = eap_type;
1056 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1057 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
1058 "??",
1059 eap_type);
1060 break;
1061 case EAP_CODE_RESPONSE:
1062 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1063 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
1064 "??",
1065 eap_type);
1066 break;
1067 case EAP_CODE_SUCCESS:
1068 os_strlcpy(buf, "EAP Success", sizeof(buf));
1069 break;
1070 case EAP_CODE_FAILURE:
1071 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1072 break;
1073 default:
1074 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1075 break;
1076 }
1077 buf[sizeof(buf) - 1] = '\0';
1078 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1079 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1080 "id=%d len=%d) from RADIUS server: %s",
1081 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1082 buf);
1083 sm->eap_if->aaaEapReq = TRUE;
1084
1085 wpabuf_free(sm->eap_if->aaaEapReqData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001086 sm->eap_if->aaaEapReqData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087}
1088
1089
1090static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1091 struct sta_info *sta, struct radius_msg *msg,
1092 struct radius_msg *req,
1093 const u8 *shared_secret,
1094 size_t shared_secret_len)
1095{
1096 struct radius_ms_mppe_keys *keys;
1097 struct eapol_state_machine *sm = sta->eapol_sm;
1098 if (sm == NULL)
1099 return;
1100
1101 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1102 shared_secret_len);
1103
1104 if (keys && keys->send && keys->recv) {
1105 size_t len = keys->send_len + keys->recv_len;
1106 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1107 keys->send, keys->send_len);
1108 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1109 keys->recv, keys->recv_len);
1110
1111 os_free(sm->eap_if->aaaEapKeyData);
1112 sm->eap_if->aaaEapKeyData = os_malloc(len);
1113 if (sm->eap_if->aaaEapKeyData) {
1114 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1115 keys->recv_len);
1116 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1117 keys->send, keys->send_len);
1118 sm->eap_if->aaaEapKeyDataLen = len;
1119 sm->eap_if->aaaEapKeyAvailable = TRUE;
1120 }
1121 }
1122
1123 if (keys) {
1124 os_free(keys->send);
1125 os_free(keys->recv);
1126 os_free(keys);
1127 }
1128}
1129
1130
1131static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1132 struct sta_info *sta,
1133 struct radius_msg *msg)
1134{
1135 u8 *class;
1136 size_t class_len;
1137 struct eapol_state_machine *sm = sta->eapol_sm;
1138 int count, i;
1139 struct radius_attr_data *nclass;
1140 size_t nclass_count;
1141
1142 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1143 sm == NULL)
1144 return;
1145
1146 radius_free_class(&sm->radius_class);
1147 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1148 if (count <= 0)
1149 return;
1150
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001151 nclass = os_calloc(count, sizeof(struct radius_attr_data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 if (nclass == NULL)
1153 return;
1154
1155 nclass_count = 0;
1156
1157 class = NULL;
1158 for (i = 0; i < count; i++) {
1159 do {
1160 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1161 &class, &class_len,
1162 class) < 0) {
1163 i = count;
1164 break;
1165 }
1166 } while (class_len < 1);
1167
1168 nclass[nclass_count].data = os_malloc(class_len);
1169 if (nclass[nclass_count].data == NULL)
1170 break;
1171
1172 os_memcpy(nclass[nclass_count].data, class, class_len);
1173 nclass[nclass_count].len = class_len;
1174 nclass_count++;
1175 }
1176
1177 sm->radius_class.attr = nclass;
1178 sm->radius_class.count = nclass_count;
1179 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1180 "attributes for " MACSTR,
1181 (unsigned long) sm->radius_class.count,
1182 MAC2STR(sta->addr));
1183}
1184
1185
1186/* Update sta->identity based on User-Name attribute in Access-Accept */
1187static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1188 struct sta_info *sta,
1189 struct radius_msg *msg)
1190{
1191 u8 *buf, *identity;
1192 size_t len;
1193 struct eapol_state_machine *sm = sta->eapol_sm;
1194
1195 if (sm == NULL)
1196 return;
1197
1198 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1199 NULL) < 0)
1200 return;
1201
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001202 identity = (u8 *) dup_binstr(buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 if (identity == NULL)
1204 return;
1205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1207 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1208 "User-Name from Access-Accept '%s'",
1209 sm->identity ? (char *) sm->identity : "N/A",
1210 (char *) identity);
1211
1212 os_free(sm->identity);
1213 sm->identity = identity;
1214 sm->identity_len = len;
1215}
1216
1217
Dmitry Shmidt04949592012-07-19 12:16:46 -07001218/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1219static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1220 struct sta_info *sta,
1221 struct radius_msg *msg)
1222{
1223 struct eapol_state_machine *sm = sta->eapol_sm;
1224 struct wpabuf *cui;
1225 u8 *buf;
1226 size_t len;
1227
1228 if (sm == NULL)
1229 return;
1230
1231 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1232 &buf, &len, NULL) < 0)
1233 return;
1234
1235 cui = wpabuf_alloc_copy(buf, len);
1236 if (cui == NULL)
1237 return;
1238
1239 wpabuf_free(sm->radius_cui);
1240 sm->radius_cui = cui;
1241}
1242
1243
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001244#ifdef CONFIG_HS20
1245
1246static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
1247{
1248 sta->remediation = 1;
1249 os_free(sta->remediation_url);
1250 if (len > 2) {
1251 sta->remediation_url = os_malloc(len);
1252 if (!sta->remediation_url)
1253 return;
1254 sta->remediation_method = pos[0];
1255 os_memcpy(sta->remediation_url, pos + 1, len - 1);
1256 sta->remediation_url[len - 1] = '\0';
1257 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1258 "for " MACSTR " - server method %u URL %s",
1259 MAC2STR(sta->addr), sta->remediation_method,
1260 sta->remediation_url);
1261 } else {
1262 sta->remediation_url = NULL;
1263 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
1264 "for " MACSTR, MAC2STR(sta->addr));
1265 }
1266 /* TODO: assign the STA into remediation VLAN or add filtering */
1267}
1268
1269
1270static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1271 struct sta_info *sta, u8 *pos,
1272 size_t len)
1273{
1274 if (len < 3)
1275 return; /* Malformed information */
1276 sta->hs20_deauth_requested = 1;
1277 wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u "
1278 "Re-auth Delay %u",
1279 *pos, WPA_GET_LE16(pos + 1));
1280 wpabuf_free(sta->hs20_deauth_req);
1281 sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1282 if (sta->hs20_deauth_req) {
1283 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1284 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
1285 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
1286 }
1287 ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
1288}
1289
1290
1291static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1292 struct sta_info *sta, u8 *pos,
1293 size_t len, int session_timeout)
1294{
1295 unsigned int swt;
1296 int warning_time, beacon_int;
1297
1298 if (len < 1)
1299 return; /* Malformed information */
1300 os_free(sta->hs20_session_info_url);
1301 sta->hs20_session_info_url = os_malloc(len);
1302 if (sta->hs20_session_info_url == NULL)
1303 return;
1304 swt = pos[0];
1305 os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1306 sta->hs20_session_info_url[len - 1] = '\0';
1307 wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
1308 "(session_timeout=%d)",
1309 sta->hs20_session_info_url, swt, session_timeout);
1310 if (session_timeout < 0) {
1311 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
1312 return;
1313 }
1314 if (swt == 255)
1315 swt = 1; /* Use one minute as the AP selected value */
1316
1317 if ((unsigned int) session_timeout < swt * 60)
1318 warning_time = 0;
1319 else
1320 warning_time = session_timeout - swt * 60;
1321
1322 beacon_int = hapd->iconf->beacon_int;
1323 if (beacon_int < 1)
1324 beacon_int = 100; /* best guess */
1325 sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1326 if (sta->hs20_disassoc_timer > 65535)
1327 sta->hs20_disassoc_timer = 65535;
1328
1329 ap_sta_session_warning_timeout(hapd, sta, warning_time);
1330}
1331
1332#endif /* CONFIG_HS20 */
1333
1334
1335static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1336 struct sta_info *sta,
1337 struct radius_msg *msg,
1338 int session_timeout)
1339{
1340#ifdef CONFIG_HS20
1341 u8 *buf, *pos, *end, type, sublen;
1342 size_t len;
1343
1344 buf = NULL;
1345 sta->remediation = 0;
1346 sta->hs20_deauth_requested = 0;
1347
1348 for (;;) {
1349 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1350 &buf, &len, buf) < 0)
1351 break;
1352 if (len < 6)
1353 continue;
1354 pos = buf;
1355 end = buf + len;
1356 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1357 continue;
1358 pos += 4;
1359
1360 type = *pos++;
1361 sublen = *pos++;
1362 if (sublen < 2)
1363 continue; /* invalid length */
1364 sublen -= 2; /* skip header */
1365 if (pos + sublen > end)
1366 continue; /* invalid WFA VSA */
1367
1368 switch (type) {
1369 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
1370 ieee802_1x_hs20_sub_rem(sta, pos, sublen);
1371 break;
1372 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1373 ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1374 break;
1375 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1376 ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1377 session_timeout);
1378 break;
1379 }
1380 }
1381#endif /* CONFIG_HS20 */
1382}
1383
1384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385struct sta_id_search {
1386 u8 identifier;
1387 struct eapol_state_machine *sm;
1388};
1389
1390
1391static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1392 struct sta_info *sta,
1393 void *ctx)
1394{
1395 struct sta_id_search *id_search = ctx;
1396 struct eapol_state_machine *sm = sta->eapol_sm;
1397
1398 if (sm && sm->radius_identifier >= 0 &&
1399 sm->radius_identifier == id_search->identifier) {
1400 id_search->sm = sm;
1401 return 1;
1402 }
1403 return 0;
1404}
1405
1406
1407static struct eapol_state_machine *
1408ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1409{
1410 struct sta_id_search id_search;
1411 id_search.identifier = identifier;
1412 id_search.sm = NULL;
1413 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1414 return id_search.sm;
1415}
1416
1417
1418/**
1419 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1420 * @msg: RADIUS response message
1421 * @req: RADIUS request message
1422 * @shared_secret: RADIUS shared secret
1423 * @shared_secret_len: Length of shared_secret in octets
1424 * @data: Context data (struct hostapd_data *)
1425 * Returns: Processing status
1426 */
1427static RadiusRxResult
1428ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1429 const u8 *shared_secret, size_t shared_secret_len,
1430 void *data)
1431{
1432 struct hostapd_data *hapd = data;
1433 struct sta_info *sta;
1434 u32 session_timeout = 0, termination_action, acct_interim_interval;
1435 int session_timeout_set, old_vlanid = 0;
1436 struct eapol_state_machine *sm;
1437 int override_eapReq = 0;
1438 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1439
1440 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1441 if (sm == NULL) {
1442 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1443 "station for this RADIUS message");
1444 return RADIUS_RX_UNKNOWN;
1445 }
1446 sta = sm->sta;
1447
1448 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1449 * present when packet contains an EAP-Message attribute */
1450 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1451 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1452 0) < 0 &&
1453 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1454 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1455 "Message-Authenticator since it does not include "
1456 "EAP-Message");
1457 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1458 req, 1)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001459 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 return RADIUS_RX_INVALID_AUTHENTICATOR;
1461 }
1462
1463 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1464 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1465 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001466 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 return RADIUS_RX_UNKNOWN;
1468 }
1469
1470 sm->radius_identifier = -1;
1471 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1472 MAC2STR(sta->addr));
1473
1474 radius_msg_free(sm->last_recv_radius);
1475 sm->last_recv_radius = msg;
1476
1477 session_timeout_set =
1478 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1479 &session_timeout);
1480 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1481 &termination_action))
1482 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1483
1484 if (hapd->conf->acct_interim_interval == 0 &&
1485 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1486 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1487 &acct_interim_interval) == 0) {
1488 if (acct_interim_interval < 60) {
1489 hostapd_logger(hapd, sta->addr,
1490 HOSTAPD_MODULE_IEEE8021X,
1491 HOSTAPD_LEVEL_INFO,
1492 "ignored too small "
1493 "Acct-Interim-Interval %d",
1494 acct_interim_interval);
1495 } else
1496 sta->acct_interim_interval = acct_interim_interval;
1497 }
1498
1499
1500 switch (hdr->code) {
1501 case RADIUS_CODE_ACCESS_ACCEPT:
1502 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1503 sta->vlan_id = 0;
1504#ifndef CONFIG_NO_VLAN
1505 else {
1506 old_vlanid = sta->vlan_id;
1507 sta->vlan_id = radius_msg_get_vlanid(msg);
1508 }
1509 if (sta->vlan_id > 0 &&
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001510 hostapd_vlan_id_valid(hapd->conf->vlan, sta->vlan_id)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 hostapd_logger(hapd, sta->addr,
1512 HOSTAPD_MODULE_RADIUS,
1513 HOSTAPD_LEVEL_INFO,
1514 "VLAN ID %d", sta->vlan_id);
1515 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1516 sta->eapol_sm->authFail = TRUE;
1517 hostapd_logger(hapd, sta->addr,
1518 HOSTAPD_MODULE_IEEE8021X,
1519 HOSTAPD_LEVEL_INFO, "authentication "
1520 "server did not include required VLAN "
1521 "ID in Access-Accept");
1522 break;
1523 }
1524#endif /* CONFIG_NO_VLAN */
1525
1526 if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
1527 break;
1528
1529 /* RFC 3580, Ch. 3.17 */
1530 if (session_timeout_set && termination_action ==
1531 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1532 sm->reAuthPeriod = session_timeout;
1533 } else if (session_timeout_set)
1534 ap_sta_session_timeout(hapd, sta, session_timeout);
1535
1536 sm->eap_if->aaaSuccess = TRUE;
1537 override_eapReq = 1;
1538 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1539 shared_secret_len);
1540 ieee802_1x_store_radius_class(hapd, sta, msg);
1541 ieee802_1x_update_sta_identity(hapd, sta, msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001542 ieee802_1x_update_sta_cui(hapd, sta, msg);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001543 ieee802_1x_check_hs20(hapd, sta, msg,
1544 session_timeout_set ?
1545 (int) session_timeout : -1);
1546 if (sm->eap_if->eapKeyAvailable && !sta->remediation &&
1547 !sta->hs20_deauth_requested &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1549 session_timeout_set ?
1550 (int) session_timeout : -1, sm) == 0) {
1551 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1552 HOSTAPD_LEVEL_DEBUG,
1553 "Added PMKSA cache entry");
1554 }
1555 break;
1556 case RADIUS_CODE_ACCESS_REJECT:
1557 sm->eap_if->aaaFail = TRUE;
1558 override_eapReq = 1;
1559 break;
1560 case RADIUS_CODE_ACCESS_CHALLENGE:
1561 sm->eap_if->aaaEapReq = TRUE;
1562 if (session_timeout_set) {
1563 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1564 sm->eap_if->aaaMethodTimeout = session_timeout;
1565 hostapd_logger(hapd, sm->addr,
1566 HOSTAPD_MODULE_IEEE8021X,
1567 HOSTAPD_LEVEL_DEBUG,
1568 "using EAP timeout of %d seconds (from "
1569 "RADIUS)",
1570 sm->eap_if->aaaMethodTimeout);
1571 } else {
1572 /*
1573 * Use dynamic retransmission behavior per EAP
1574 * specification.
1575 */
1576 sm->eap_if->aaaMethodTimeout = 0;
1577 }
1578 break;
1579 }
1580
1581 ieee802_1x_decapsulate_radius(hapd, sta);
1582 if (override_eapReq)
1583 sm->eap_if->aaaEapReq = FALSE;
1584
1585 eapol_auth_step(sm);
1586
1587 return RADIUS_RX_QUEUED;
1588}
1589#endif /* CONFIG_NO_RADIUS */
1590
1591
1592void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1593{
1594 struct eapol_state_machine *sm = sta->eapol_sm;
1595 if (sm == NULL)
1596 return;
1597
1598 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1599 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1600
1601#ifndef CONFIG_NO_RADIUS
1602 radius_msg_free(sm->last_recv_radius);
1603 sm->last_recv_radius = NULL;
1604#endif /* CONFIG_NO_RADIUS */
1605
1606 if (sm->eap_if->eapTimeout) {
1607 /*
1608 * Disconnect the STA since it did not reply to the last EAP
1609 * request and we cannot continue EAP processing (EAP-Failure
1610 * could only be sent if the EAP peer actually replied).
1611 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
1613 MAC2STR(sta->addr));
1614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 sm->eap_if->portEnabled = FALSE;
1616 ap_sta_disconnect(hapd, sta, sta->addr,
1617 WLAN_REASON_PREV_AUTH_NOT_VALID);
1618 }
1619}
1620
1621
1622static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1623{
1624 struct eapol_authenticator *eapol = hapd->eapol_auth;
1625
1626 if (hapd->conf->default_wep_key_len < 1)
1627 return 0;
1628
1629 os_free(eapol->default_wep_key);
1630 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1631 if (eapol->default_wep_key == NULL ||
1632 random_get_bytes(eapol->default_wep_key,
1633 hapd->conf->default_wep_key_len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001634 wpa_printf(MSG_INFO, "Could not generate random WEP key");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 os_free(eapol->default_wep_key);
1636 eapol->default_wep_key = NULL;
1637 return -1;
1638 }
1639
1640 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1641 eapol->default_wep_key,
1642 hapd->conf->default_wep_key_len);
1643
1644 return 0;
1645}
1646
1647
1648static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1649 struct sta_info *sta, void *ctx)
1650{
1651 if (sta->eapol_sm) {
1652 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1653 eapol_auth_step(sta->eapol_sm);
1654 }
1655 return 0;
1656}
1657
1658
1659static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1660{
1661 struct hostapd_data *hapd = eloop_ctx;
1662 struct eapol_authenticator *eapol = hapd->eapol_auth;
1663
1664 if (eapol->default_wep_key_idx >= 3)
1665 eapol->default_wep_key_idx =
1666 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1667 else
1668 eapol->default_wep_key_idx++;
1669
1670 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1671 eapol->default_wep_key_idx);
1672
1673 if (ieee802_1x_rekey_broadcast(hapd)) {
1674 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1675 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1676 "new broadcast key");
1677 os_free(eapol->default_wep_key);
1678 eapol->default_wep_key = NULL;
1679 return;
1680 }
1681
1682 /* TODO: Could setup key for RX here, but change default TX keyid only
1683 * after new broadcast key has been sent to all stations. */
1684 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1685 broadcast_ether_addr,
1686 eapol->default_wep_key_idx, 1, NULL, 0,
1687 eapol->default_wep_key,
1688 hapd->conf->default_wep_key_len)) {
1689 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1690 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1691 "new broadcast key");
1692 os_free(eapol->default_wep_key);
1693 eapol->default_wep_key = NULL;
1694 return;
1695 }
1696
1697 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1698
1699 if (hapd->conf->wep_rekeying_period > 0) {
1700 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1701 ieee802_1x_rekey, hapd, NULL);
1702 }
1703}
1704
1705
1706static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1707 const u8 *data, size_t datalen)
1708{
1709#ifdef CONFIG_WPS
1710 struct sta_info *sta = sta_ctx;
1711
1712 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1713 WLAN_STA_MAYBE_WPS) {
1714 const u8 *identity;
1715 size_t identity_len;
1716 struct eapol_state_machine *sm = sta->eapol_sm;
1717
1718 identity = eap_get_identity(sm->eap, &identity_len);
1719 if (identity &&
1720 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1721 os_memcmp(identity, WSC_ID_ENROLLEE,
1722 WSC_ID_ENROLLEE_LEN) == 0) ||
1723 (identity_len == WSC_ID_REGISTRAR_LEN &&
1724 os_memcmp(identity, WSC_ID_REGISTRAR,
1725 WSC_ID_REGISTRAR_LEN) == 0))) {
1726 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1727 "WLAN_STA_WPS");
1728 sta->flags |= WLAN_STA_WPS;
1729 }
1730 }
1731#endif /* CONFIG_WPS */
1732
1733 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1734}
1735
1736
1737static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1738 const u8 *data, size_t datalen)
1739{
1740#ifndef CONFIG_NO_RADIUS
1741 struct hostapd_data *hapd = ctx;
1742 struct sta_info *sta = sta_ctx;
1743
1744 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1745#endif /* CONFIG_NO_RADIUS */
1746}
1747
1748
1749static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001750 int preauth, int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751{
1752 struct hostapd_data *hapd = ctx;
1753 struct sta_info *sta = sta_ctx;
1754 if (preauth)
1755 rsn_preauth_finished(hapd, sta, success);
1756 else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001757 ieee802_1x_finished(hapd, sta, success, remediation);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758}
1759
1760
1761static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1762 size_t identity_len, int phase2,
1763 struct eap_user *user)
1764{
1765 struct hostapd_data *hapd = ctx;
1766 const struct hostapd_eap_user *eap_user;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001767 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001769 eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 if (eap_user == NULL)
1771 return -1;
1772
1773 os_memset(user, 0, sizeof(*user));
1774 user->phase2 = phase2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001775 for (i = 0; i < EAP_MAX_METHODS; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 user->methods[i].vendor = eap_user->methods[i].vendor;
1777 user->methods[i].method = eap_user->methods[i].method;
1778 }
1779
1780 if (eap_user->password) {
1781 user->password = os_malloc(eap_user->password_len);
1782 if (user->password == NULL)
1783 return -1;
1784 os_memcpy(user->password, eap_user->password,
1785 eap_user->password_len);
1786 user->password_len = eap_user->password_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001787 user->password_hash = eap_user->password_hash;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 }
1789 user->force_version = eap_user->force_version;
1790 user->ttls_auth = eap_user->ttls_auth;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001791 user->remediation = eap_user->remediation;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792
1793 return 0;
1794}
1795
1796
1797static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1798{
1799 struct hostapd_data *hapd = ctx;
1800 struct sta_info *sta;
1801 sta = ap_get_sta(hapd, addr);
1802 if (sta == NULL || sta->eapol_sm == NULL)
1803 return 0;
1804 return 1;
1805}
1806
1807
1808static void ieee802_1x_logger(void *ctx, const u8 *addr,
1809 eapol_logger_level level, const char *txt)
1810{
1811#ifndef CONFIG_NO_HOSTAPD_LOGGER
1812 struct hostapd_data *hapd = ctx;
1813 int hlevel;
1814
1815 switch (level) {
1816 case EAPOL_LOGGER_WARNING:
1817 hlevel = HOSTAPD_LEVEL_WARNING;
1818 break;
1819 case EAPOL_LOGGER_INFO:
1820 hlevel = HOSTAPD_LEVEL_INFO;
1821 break;
1822 case EAPOL_LOGGER_DEBUG:
1823 default:
1824 hlevel = HOSTAPD_LEVEL_DEBUG;
1825 break;
1826 }
1827
1828 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1829 txt);
1830#endif /* CONFIG_NO_HOSTAPD_LOGGER */
1831}
1832
1833
1834static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1835 int authorized)
1836{
1837 struct hostapd_data *hapd = ctx;
1838 struct sta_info *sta = sta_ctx;
1839 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1840}
1841
1842
1843static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1844{
1845 struct hostapd_data *hapd = ctx;
1846 struct sta_info *sta = sta_ctx;
1847 ieee802_1x_abort_auth(hapd, sta);
1848}
1849
1850
1851static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1852{
1853 struct hostapd_data *hapd = ctx;
1854 struct sta_info *sta = sta_ctx;
1855 ieee802_1x_tx_key(hapd, sta);
1856}
1857
1858
1859static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1860 enum eapol_event type)
1861{
1862 /* struct hostapd_data *hapd = ctx; */
1863 struct sta_info *sta = sta_ctx;
1864 switch (type) {
1865 case EAPOL_AUTH_SM_CHANGE:
1866 wpa_auth_sm_notify(sta->wpa_sm);
1867 break;
1868 case EAPOL_AUTH_REAUTHENTICATE:
1869 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1870 break;
1871 }
1872}
1873
1874
1875int ieee802_1x_init(struct hostapd_data *hapd)
1876{
1877 int i;
1878 struct eapol_auth_config conf;
1879 struct eapol_auth_cb cb;
1880
1881 os_memset(&conf, 0, sizeof(conf));
1882 conf.ctx = hapd;
1883 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1884 conf.wpa = hapd->conf->wpa;
1885 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1886 conf.eap_server = hapd->conf->eap_server;
1887 conf.ssl_ctx = hapd->ssl_ctx;
1888 conf.msg_ctx = hapd->msg_ctx;
1889 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1890 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1891 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1892 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1893 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1894 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1895 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1896 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1897 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1898 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1899 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1900 conf.tnc = hapd->conf->tnc;
1901 conf.wps = hapd->wps;
1902 conf.fragment_size = hapd->conf->fragment_size;
1903 conf.pwd_group = hapd->conf->pwd_group;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001904 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001905 if (hapd->conf->server_id) {
1906 conf.server_id = (const u8 *) hapd->conf->server_id;
1907 conf.server_id_len = os_strlen(hapd->conf->server_id);
1908 } else {
1909 conf.server_id = (const u8 *) "hostapd";
1910 conf.server_id_len = 7;
1911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912
1913 os_memset(&cb, 0, sizeof(cb));
1914 cb.eapol_send = ieee802_1x_eapol_send;
1915 cb.aaa_send = ieee802_1x_aaa_send;
1916 cb.finished = _ieee802_1x_finished;
1917 cb.get_eap_user = ieee802_1x_get_eap_user;
1918 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1919 cb.logger = ieee802_1x_logger;
1920 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1921 cb.abort_auth = _ieee802_1x_abort_auth;
1922 cb.tx_key = _ieee802_1x_tx_key;
1923 cb.eapol_event = ieee802_1x_eapol_event;
1924
1925 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1926 if (hapd->eapol_auth == NULL)
1927 return -1;
1928
1929 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1930 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
1931 return -1;
1932
1933#ifndef CONFIG_NO_RADIUS
1934 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1935 ieee802_1x_receive_auth, hapd))
1936 return -1;
1937#endif /* CONFIG_NO_RADIUS */
1938
1939 if (hapd->conf->default_wep_key_len) {
1940 for (i = 0; i < 4; i++)
1941 hostapd_drv_set_key(hapd->conf->iface, hapd,
1942 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
1943 NULL, 0);
1944
1945 ieee802_1x_rekey(hapd, NULL);
1946
1947 if (hapd->eapol_auth->default_wep_key == NULL)
1948 return -1;
1949 }
1950
1951 return 0;
1952}
1953
1954
1955void ieee802_1x_deinit(struct hostapd_data *hapd)
1956{
1957 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1958
1959 if (hapd->driver != NULL &&
1960 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1961 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
1962
1963 eapol_auth_deinit(hapd->eapol_auth);
1964 hapd->eapol_auth = NULL;
1965}
1966
1967
1968int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1969 const u8 *buf, size_t len, int ack)
1970{
1971 struct ieee80211_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 u8 *pos;
1973 const unsigned char rfc1042_hdr[ETH_ALEN] =
1974 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1975
1976 if (sta == NULL)
1977 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001978 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 return 0;
1980
1981 hdr = (struct ieee80211_hdr *) buf;
1982 pos = (u8 *) (hdr + 1);
1983 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1984 return 0;
1985 pos += sizeof(rfc1042_hdr);
1986 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1987 return 0;
1988 pos += 2;
1989
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001990 return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
1991 ack);
1992}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001994
1995int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1996 const u8 *buf, int len, int ack)
1997{
1998 const struct ieee802_1x_hdr *xhdr =
1999 (const struct ieee802_1x_hdr *) buf;
2000 const u8 *pos = buf + sizeof(*xhdr);
2001 struct ieee802_1x_eapol_key *key;
2002
2003 if (len < (int) sizeof(*xhdr))
2004 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
2006 "type=%d length=%d - ack=%d",
2007 MAC2STR(sta->addr), xhdr->version, xhdr->type,
2008 be_to_host16(xhdr->length), ack);
2009
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002010 if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2011 return 0;
2012
2013 if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014 const struct wpa_eapol_key *wpa;
2015 wpa = (const struct wpa_eapol_key *) pos;
2016 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2017 wpa->type == EAPOL_KEY_TYPE_WPA)
2018 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2019 sta->wpa_sm, ack);
2020 }
2021
2022 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2023 * or Authenticator state machines, but EAPOL-Key packets are not
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002024 * retransmitted in case of failure. Try to re-send failed EAPOL-Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 * packets couple of times because otherwise STA keys become
2026 * unsynchronized with AP. */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002027 if (!ack && pos + sizeof(*key) <= buf + len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 key = (struct ieee802_1x_eapol_key *) pos;
2029 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2030 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
2031 "frame (%scast index=%d)",
2032 key->key_index & BIT(7) ? "uni" : "broad",
2033 key->key_index & ~BIT(7));
2034 /* TODO: re-send EAPOL-Key couple of times (with short delay
2035 * between them?). If all attempt fail, report error and
2036 * deauthenticate STA so that it will get new keys when
2037 * authenticating again (e.g., after returning in range).
2038 * Separate limit/transmit state needed both for unicast and
2039 * broadcast keys(?) */
2040 }
2041 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2042 * to here and change the key only if the EAPOL-Key packet was Acked.
2043 */
2044
2045 return 1;
2046}
2047
2048
2049u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2050{
2051 if (sm == NULL || sm->identity == NULL)
2052 return NULL;
2053
2054 *len = sm->identity_len;
2055 return sm->identity;
2056}
2057
2058
2059u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2060 int idx)
2061{
2062 if (sm == NULL || sm->radius_class.attr == NULL ||
2063 idx >= (int) sm->radius_class.count)
2064 return NULL;
2065
2066 *len = sm->radius_class.attr[idx].len;
2067 return sm->radius_class.attr[idx].data;
2068}
2069
2070
Dmitry Shmidt04949592012-07-19 12:16:46 -07002071struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2072{
2073 if (sm == NULL)
2074 return NULL;
2075 return sm->radius_cui;
2076}
2077
2078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2080{
Jouni Malinen75ecf522011-06-27 15:19:46 -07002081 *len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 if (sm == NULL)
2083 return NULL;
2084
2085 *len = sm->eap_if->eapKeyDataLen;
2086 return sm->eap_if->eapKeyData;
2087}
2088
2089
2090void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2091 int enabled)
2092{
2093 if (sm == NULL)
2094 return;
2095 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
2096 eapol_auth_step(sm);
2097}
2098
2099
2100void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
2101 int valid)
2102{
2103 if (sm == NULL)
2104 return;
2105 sm->portValid = valid ? TRUE : FALSE;
2106 eapol_auth_step(sm);
2107}
2108
2109
2110void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
2111{
2112 if (sm == NULL)
2113 return;
2114 if (pre_auth)
2115 sm->flags |= EAPOL_SM_PREAUTH;
2116 else
2117 sm->flags &= ~EAPOL_SM_PREAUTH;
2118}
2119
2120
2121static const char * bool_txt(Boolean bool)
2122{
2123 return bool ? "TRUE" : "FALSE";
2124}
2125
2126
2127int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2128{
2129 /* TODO */
2130 return 0;
2131}
2132
2133
2134int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2135 char *buf, size_t buflen)
2136{
2137 int len = 0, ret;
2138 struct eapol_state_machine *sm = sta->eapol_sm;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002139 struct os_reltime diff;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002140 const char *name1;
2141 const char *name2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142
2143 if (sm == NULL)
2144 return 0;
2145
2146 ret = os_snprintf(buf + len, buflen - len,
2147 "dot1xPaePortNumber=%d\n"
2148 "dot1xPaePortProtocolVersion=%d\n"
2149 "dot1xPaePortCapabilities=1\n"
2150 "dot1xPaePortInitialize=%d\n"
2151 "dot1xPaePortReauthenticate=FALSE\n",
2152 sta->aid,
2153 EAPOL_VERSION,
2154 sm->initialize);
2155 if (ret < 0 || (size_t) ret >= buflen - len)
2156 return len;
2157 len += ret;
2158
2159 /* dot1xAuthConfigTable */
2160 ret = os_snprintf(buf + len, buflen - len,
2161 "dot1xAuthPaeState=%d\n"
2162 "dot1xAuthBackendAuthState=%d\n"
2163 "dot1xAuthAdminControlledDirections=%d\n"
2164 "dot1xAuthOperControlledDirections=%d\n"
2165 "dot1xAuthAuthControlledPortStatus=%d\n"
2166 "dot1xAuthAuthControlledPortControl=%d\n"
2167 "dot1xAuthQuietPeriod=%u\n"
2168 "dot1xAuthServerTimeout=%u\n"
2169 "dot1xAuthReAuthPeriod=%u\n"
2170 "dot1xAuthReAuthEnabled=%s\n"
2171 "dot1xAuthKeyTxEnabled=%s\n",
2172 sm->auth_pae_state + 1,
2173 sm->be_auth_state + 1,
2174 sm->adminControlledDirections,
2175 sm->operControlledDirections,
2176 sm->authPortStatus,
2177 sm->portControl,
2178 sm->quietPeriod,
2179 sm->serverTimeout,
2180 sm->reAuthPeriod,
2181 bool_txt(sm->reAuthEnabled),
2182 bool_txt(sm->keyTxEnabled));
2183 if (ret < 0 || (size_t) ret >= buflen - len)
2184 return len;
2185 len += ret;
2186
2187 /* dot1xAuthStatsTable */
2188 ret = os_snprintf(buf + len, buflen - len,
2189 "dot1xAuthEapolFramesRx=%u\n"
2190 "dot1xAuthEapolFramesTx=%u\n"
2191 "dot1xAuthEapolStartFramesRx=%u\n"
2192 "dot1xAuthEapolLogoffFramesRx=%u\n"
2193 "dot1xAuthEapolRespIdFramesRx=%u\n"
2194 "dot1xAuthEapolRespFramesRx=%u\n"
2195 "dot1xAuthEapolReqIdFramesTx=%u\n"
2196 "dot1xAuthEapolReqFramesTx=%u\n"
2197 "dot1xAuthInvalidEapolFramesRx=%u\n"
2198 "dot1xAuthEapLengthErrorFramesRx=%u\n"
2199 "dot1xAuthLastEapolFrameVersion=%u\n"
2200 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2201 sm->dot1xAuthEapolFramesRx,
2202 sm->dot1xAuthEapolFramesTx,
2203 sm->dot1xAuthEapolStartFramesRx,
2204 sm->dot1xAuthEapolLogoffFramesRx,
2205 sm->dot1xAuthEapolRespIdFramesRx,
2206 sm->dot1xAuthEapolRespFramesRx,
2207 sm->dot1xAuthEapolReqIdFramesTx,
2208 sm->dot1xAuthEapolReqFramesTx,
2209 sm->dot1xAuthInvalidEapolFramesRx,
2210 sm->dot1xAuthEapLengthErrorFramesRx,
2211 sm->dot1xAuthLastEapolFrameVersion,
2212 MAC2STR(sm->addr));
2213 if (ret < 0 || (size_t) ret >= buflen - len)
2214 return len;
2215 len += ret;
2216
2217 /* dot1xAuthDiagTable */
2218 ret = os_snprintf(buf + len, buflen - len,
2219 "dot1xAuthEntersConnecting=%u\n"
2220 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2221 "dot1xAuthEntersAuthenticating=%u\n"
2222 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2223 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2224 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2225 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2226 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2227 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2228 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2229 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2230 "dot1xAuthBackendResponses=%u\n"
2231 "dot1xAuthBackendAccessChallenges=%u\n"
2232 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2233 "dot1xAuthBackendAuthSuccesses=%u\n"
2234 "dot1xAuthBackendAuthFails=%u\n",
2235 sm->authEntersConnecting,
2236 sm->authEapLogoffsWhileConnecting,
2237 sm->authEntersAuthenticating,
2238 sm->authAuthSuccessesWhileAuthenticating,
2239 sm->authAuthTimeoutsWhileAuthenticating,
2240 sm->authAuthFailWhileAuthenticating,
2241 sm->authAuthEapStartsWhileAuthenticating,
2242 sm->authAuthEapLogoffWhileAuthenticating,
2243 sm->authAuthReauthsWhileAuthenticated,
2244 sm->authAuthEapStartsWhileAuthenticated,
2245 sm->authAuthEapLogoffWhileAuthenticated,
2246 sm->backendResponses,
2247 sm->backendAccessChallenges,
2248 sm->backendOtherRequestsToSupplicant,
2249 sm->backendAuthSuccesses,
2250 sm->backendAuthFails);
2251 if (ret < 0 || (size_t) ret >= buflen - len)
2252 return len;
2253 len += ret;
2254
2255 /* dot1xAuthSessionStatsTable */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002256 os_reltime_age(&sta->acct_session_start, &diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 ret = os_snprintf(buf + len, buflen - len,
2258 /* TODO: dot1xAuthSessionOctetsRx */
2259 /* TODO: dot1xAuthSessionOctetsTx */
2260 /* TODO: dot1xAuthSessionFramesRx */
2261 /* TODO: dot1xAuthSessionFramesTx */
2262 "dot1xAuthSessionId=%08X-%08X\n"
2263 "dot1xAuthSessionAuthenticMethod=%d\n"
2264 "dot1xAuthSessionTime=%u\n"
2265 "dot1xAuthSessionTerminateCause=999\n"
2266 "dot1xAuthSessionUserName=%s\n",
2267 sta->acct_session_id_hi, sta->acct_session_id_lo,
2268 (wpa_key_mgmt_wpa_ieee8021x(
2269 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2270 1 : 2,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002271 (unsigned int) diff.sec,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 sm->identity);
2273 if (ret < 0 || (size_t) ret >= buflen - len)
2274 return len;
2275 len += ret;
2276
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002277 name1 = eap_server_get_name(0, sm->eap_type_authsrv);
2278 name2 = eap_server_get_name(0, sm->eap_type_supp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002279 ret = os_snprintf(buf + len, buflen - len,
2280 "last_eap_type_as=%d (%s)\n"
2281 "last_eap_type_sta=%d (%s)\n",
2282 sm->eap_type_authsrv,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002283 name1 ? name1 : "",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002284 sm->eap_type_supp,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002285 name2 ? name2 : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002286 if (ret < 0 || (size_t) ret >= buflen - len)
2287 return len;
2288 len += ret;
2289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 return len;
2291}
2292
2293
2294static void ieee802_1x_finished(struct hostapd_data *hapd,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002295 struct sta_info *sta, int success,
2296 int remediation)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297{
2298 const u8 *key;
2299 size_t len;
2300 /* TODO: get PMKLifetime from WPA parameters */
2301 static const int dot11RSNAConfigPMKLifetime = 43200;
2302
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002303#ifdef CONFIG_HS20
2304 if (remediation && !sta->remediation) {
2305 sta->remediation = 1;
2306 os_free(sta->remediation_url);
2307 sta->remediation_url =
2308 os_strdup(hapd->conf->subscr_remediation_url);
2309 sta->remediation_method = 1; /* SOAP-XML SPP */
2310 }
2311
2312 if (success) {
2313 if (sta->remediation) {
2314 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2315 "to " MACSTR " to indicate Subscription "
2316 "Remediation",
2317 MAC2STR(sta->addr));
2318 hs20_send_wnm_notification(hapd, sta->addr,
2319 sta->remediation_method,
2320 sta->remediation_url);
2321 os_free(sta->remediation_url);
2322 sta->remediation_url = NULL;
2323 }
2324
2325 if (sta->hs20_deauth_req) {
2326 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
2327 "to " MACSTR " to indicate imminent "
2328 "deauthentication", MAC2STR(sta->addr));
2329 hs20_send_wnm_notification_deauth_req(
2330 hapd, sta->addr, sta->hs20_deauth_req);
2331 }
2332 }
2333#endif /* CONFIG_HS20 */
2334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 key = ieee802_1x_get_key(sta->eapol_sm, &len);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002336 if (success && key && len >= PMK_LEN && !sta->remediation &&
2337 !sta->hs20_deauth_requested &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2339 sta->eapol_sm) == 0) {
2340 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2341 HOSTAPD_LEVEL_DEBUG,
2342 "Added PMKSA cache entry (IEEE 802.1X)");
2343 }
2344
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002345 if (!success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 /*
2347 * Many devices require deauthentication after WPS provisioning
2348 * and some may not be be able to do that themselves, so
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002349 * disconnect the client here. In addition, this may also
2350 * benefit IEEE 802.1X/EAPOL authentication cases, too since
2351 * the EAPOL PAE state machine would remain in HELD state for
2352 * considerable amount of time and some EAP methods, like
2353 * EAP-FAST with anonymous provisioning, may require another
2354 * EAPOL authentication to be started to complete connection.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002356 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
2357 "disconnection after EAP-Failure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 /* Add a small sleep to increase likelihood of previously
2359 * requested EAP-Failure TX getting out before this should the
2360 * driver reorder operations.
2361 */
2362 os_sleep(0, 10000);
2363 ap_sta_disconnect(hapd, sta, sta->addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002364 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002365 hostapd_wps_eap_completed(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367}