]> git.ipfire.org Git - thirdparty/hostap.git/blame - hostapd/ieee802_1x.c
Remove src/crypto from default include path
[thirdparty/hostap.git] / hostapd / ieee802_1x.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / IEEE 802.1X-2004 Authenticator
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
4dbfe5c5 17#include "common.h"
03da66bd
JM
18#include "eloop.h"
19#include "crypto/md5.h"
20#include "crypto/crypto.h"
21#include "common/ieee802_11_defs.h"
22#include "common/wpa_ctrl.h"
6fc6879b
JM
23#include "radius/radius.h"
24#include "radius/radius_client.h"
281c950b 25#include "eapol_auth/eapol_auth_sm.h"
03da66bd
JM
26#include "hostapd.h"
27#include "ieee802_1x.h"
28#include "accounting.h"
bcd154c3 29#include "sta_flags.h"
6fc6879b
JM
30#include "sta_info.h"
31#include "wpa.h"
32#include "preauth.h"
33#include "pmksa_cache.h"
bfddd95c 34#include "driver_i.h"
6fc6879b
JM
35#include "hw_features.h"
36#include "eap_server/eap.h"
37
38
39static void ieee802_1x_finished(struct hostapd_data *hapd,
40 struct sta_info *sta, int success);
41
42
43static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
44 u8 type, const u8 *data, size_t datalen)
45{
46 u8 *buf;
47 struct ieee802_1x_hdr *xhdr;
48 size_t len;
49 int encrypt = 0;
50
51 len = sizeof(*xhdr) + datalen;
52 buf = os_zalloc(len);
53 if (buf == NULL) {
54 wpa_printf(MSG_ERROR, "malloc() failed for "
55 "ieee802_1x_send(len=%lu)",
56 (unsigned long) len);
57 return;
58 }
59
60 xhdr = (struct ieee802_1x_hdr *) buf;
61 xhdr->version = hapd->conf->eapol_version;
62 xhdr->type = type;
63 xhdr->length = host_to_be16(datalen);
64
65 if (datalen > 0 && data != NULL)
66 os_memcpy(xhdr + 1, data, datalen);
67
68 if (wpa_auth_pairwise_set(sta->wpa_sm))
69 encrypt = 1;
70 if (sta->flags & WLAN_STA_PREAUTH) {
71 rsn_preauth_send(hapd, sta, buf, len);
72 } else {
73 hostapd_send_eapol(hapd, sta->addr, buf, len, encrypt);
74 }
75
76 os_free(buf);
77}
78
79
80void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
81 struct sta_info *sta, int authorized)
82{
83 int res;
84
85 if (sta->flags & WLAN_STA_PREAUTH)
86 return;
87
88 if (authorized) {
20bd9547
JM
89 if (!(sta->flags & WLAN_STA_AUTHORIZED))
90 wpa_msg(hapd->msg_ctx, MSG_INFO,
91 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
6fc6879b
JM
92 sta->flags |= WLAN_STA_AUTHORIZED;
93 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
94 WLAN_STA_AUTHORIZED, ~0);
95 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
96 HOSTAPD_LEVEL_DEBUG, "authorizing port");
97 } else {
20bd9547
JM
98 if ((sta->flags & (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC)) ==
99 (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC))
100 wpa_msg(hapd->msg_ctx, MSG_INFO,
101 AP_STA_DISCONNECTED MACSTR,
102 MAC2STR(sta->addr));
6fc6879b
JM
103 sta->flags &= ~WLAN_STA_AUTHORIZED;
104 res = hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
105 0, ~WLAN_STA_AUTHORIZED);
106 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
107 HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
108 }
109
110 if (res && errno != ENOENT) {
111 printf("Could not set station " MACSTR " flags for kernel "
112 "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
113 }
114
115 if (authorized)
116 accounting_sta_start(hapd, sta);
117}
118
119
6fc6879b
JM
120static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
121 struct sta_info *sta,
122 int idx, int broadcast,
123 u8 *key_data, size_t key_len)
124{
125 u8 *buf, *ekey;
126 struct ieee802_1x_hdr *hdr;
127 struct ieee802_1x_eapol_key *key;
128 size_t len, ekey_len;
129 struct eapol_state_machine *sm = sta->eapol_sm;
130
131 if (sm == NULL)
132 return;
133
134 len = sizeof(*key) + key_len;
135 buf = os_zalloc(sizeof(*hdr) + len);
136 if (buf == NULL)
137 return;
138
139 hdr = (struct ieee802_1x_hdr *) buf;
140 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
141 key->type = EAPOL_KEY_TYPE_RC4;
142 key->key_length = htons(key_len);
143 wpa_get_ntp_timestamp(key->replay_counter);
144
145 if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
146 wpa_printf(MSG_ERROR, "Could not get random numbers");
147 os_free(buf);
148 return;
149 }
150
151 key->key_index = idx | (broadcast ? 0 : BIT(7));
152 if (hapd->conf->eapol_key_index_workaround) {
153 /* According to some information, WinXP Supplicant seems to
154 * interpret bit7 as an indication whether the key is to be
155 * activated, so make it possible to enable workaround that
156 * sets this bit for all keys. */
157 key->key_index |= BIT(7);
158 }
159
160 /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
161 * MSK[32..63] is used to sign the message. */
162 if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
163 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
164 "and signing EAPOL-Key");
165 os_free(buf);
166 return;
167 }
168 os_memcpy((u8 *) (key + 1), key_data, key_len);
169 ekey_len = sizeof(key->key_iv) + 32;
170 ekey = os_malloc(ekey_len);
171 if (ekey == NULL) {
172 wpa_printf(MSG_ERROR, "Could not encrypt key");
173 os_free(buf);
174 return;
175 }
176 os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
177 os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
8ef16831 178 rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
6fc6879b
JM
179 os_free(ekey);
180
181 /* This header is needed here for HMAC-MD5, but it will be regenerated
182 * in ieee802_1x_send() */
183 hdr->version = hapd->conf->eapol_version;
184 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
185 hdr->length = host_to_be16(len);
186 hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
187 key->key_signature);
188
189 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
190 " (%s index=%d)", MAC2STR(sm->addr),
191 broadcast ? "broadcast" : "unicast", idx);
192 ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
193 if (sta->eapol_sm)
194 sta->eapol_sm->dot1xAuthEapolFramesTx++;
195 os_free(buf);
196}
197
198
30b32314 199#ifndef CONFIG_NO_VLAN
6fc6879b
JM
200static struct hostapd_wep_keys *
201ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
202{
203 struct hostapd_wep_keys *key;
204
205 key = os_zalloc(sizeof(*key));
206 if (key == NULL)
207 return NULL;
208
209 key->default_len = hapd->conf->default_wep_key_len;
210
211 if (key->idx >= hapd->conf->broadcast_key_idx_max ||
212 key->idx < hapd->conf->broadcast_key_idx_min)
213 key->idx = hapd->conf->broadcast_key_idx_min;
214 else
215 key->idx++;
216
217 if (!key->key[key->idx])
218 key->key[key->idx] = os_malloc(key->default_len);
219 if (key->key[key->idx] == NULL ||
220 os_get_random(key->key[key->idx], key->default_len)) {
221 printf("Could not generate random WEP key (dynamic VLAN).\n");
222 os_free(key->key[key->idx]);
223 key->key[key->idx] = NULL;
224 os_free(key);
225 return NULL;
226 }
227 key->len[key->idx] = key->default_len;
228
229 wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
230 ifname, key->idx);
231 wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
232 key->key[key->idx], key->len[key->idx]);
233
89d39d9d
JM
234 if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL, key->idx, 1,
235 NULL, 0, key->key[key->idx], key->len[key->idx]))
6fc6879b
JM
236 printf("Could not set dynamic VLAN WEP encryption key.\n");
237
238 hostapd_set_ieee8021x(ifname, hapd, 1);
239
240 return key;
241}
242
243
244static struct hostapd_wep_keys *
245ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
246 size_t vlan_id)
247{
248 const char *ifname;
249
250 if (vlan_id == 0)
251 return &ssid->wep;
252
253 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
254 ssid->dyn_vlan_keys[vlan_id])
255 return ssid->dyn_vlan_keys[vlan_id];
256
257 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
258 "state machine for VLAN ID %lu",
259 (unsigned long) vlan_id);
260
261 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
262 if (ifname == NULL) {
263 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
264 "cannot create group key state machine",
265 (unsigned long) vlan_id);
266 return NULL;
267 }
268
269 if (ssid->dyn_vlan_keys == NULL) {
270 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
271 ssid->dyn_vlan_keys = os_zalloc(size);
272 if (ssid->dyn_vlan_keys == NULL)
273 return NULL;
274 ssid->max_dyn_vlan_keys = vlan_id;
275 }
276
277 if (ssid->max_dyn_vlan_keys < vlan_id) {
278 struct hostapd_wep_keys **na;
279 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
280 na = os_realloc(ssid->dyn_vlan_keys, size);
281 if (na == NULL)
282 return NULL;
283 ssid->dyn_vlan_keys = na;
284 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
285 (vlan_id - ssid->max_dyn_vlan_keys) *
286 sizeof(ssid->dyn_vlan_keys[0]));
287 ssid->max_dyn_vlan_keys = vlan_id;
288 }
289
290 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
291
292 return ssid->dyn_vlan_keys[vlan_id];
293}
30b32314 294#endif /* CONFIG_NO_VLAN */
6fc6879b
JM
295
296
297void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
298{
f55802e8 299 struct eapol_authenticator *eapol = hapd->eapol_auth;
6fc6879b 300 struct eapol_state_machine *sm = sta->eapol_sm;
30b32314
JM
301#ifndef CONFIG_NO_VLAN
302 struct hostapd_wep_keys *key = NULL;
6fc6879b 303 int vlan_id;
30b32314 304#endif /* CONFIG_NO_VLAN */
6fc6879b
JM
305
306 if (sm == NULL || !sm->eap_if->eapKeyData)
307 return;
308
309 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
310 MAC2STR(sta->addr));
311
30b32314 312#ifndef CONFIG_NO_VLAN
6fc6879b
JM
313 vlan_id = sta->vlan_id;
314 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
315 vlan_id = 0;
316
317 if (vlan_id) {
318 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
319 if (key && key->key[key->idx])
320 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
321 key->key[key->idx],
322 key->len[key->idx]);
30b32314
JM
323 } else
324#endif /* CONFIG_NO_VLAN */
f55802e8
JM
325 if (eapol->default_wep_key) {
326 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
327 eapol->default_wep_key,
6fc6879b
JM
328 hapd->conf->default_wep_key_len);
329 }
330
331 if (hapd->conf->individual_wep_key_len > 0) {
332 u8 *ikey;
333 ikey = os_malloc(hapd->conf->individual_wep_key_len);
334 if (ikey == NULL ||
335 os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
336 wpa_printf(MSG_ERROR, "Could not generate random "
337 "individual WEP key.");
338 os_free(ikey);
339 return;
340 }
341
342 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
343 ikey, hapd->conf->individual_wep_key_len);
344
345 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
346 hapd->conf->individual_wep_key_len);
347
348 /* TODO: set encryption in TX callback, i.e., only after STA
349 * has ACKed EAPOL-Key frame */
89d39d9d
JM
350 if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
351 sta->addr, 0, 1, NULL, 0, ikey,
352 hapd->conf->individual_wep_key_len)) {
6fc6879b
JM
353 wpa_printf(MSG_ERROR, "Could not set individual WEP "
354 "encryption.");
355 }
356
357 os_free(ikey);
358 }
359}
360
361
362const char *radius_mode_txt(struct hostapd_data *hapd)
363{
364 if (hapd->iface->current_mode == NULL)
365 return "802.11";
366
367 switch (hapd->iface->current_mode->mode) {
368 case HOSTAPD_MODE_IEEE80211A:
369 return "802.11a";
370 case HOSTAPD_MODE_IEEE80211G:
371 return "802.11g";
372 case HOSTAPD_MODE_IEEE80211B:
373 default:
374 return "802.11b";
375 }
376}
377
378
379int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
380{
381 int i;
382 u8 rate = 0;
383
384 for (i = 0; i < sta->supported_rates_len; i++)
385 if ((sta->supported_rates[i] & 0x7f) > rate)
386 rate = sta->supported_rates[i] & 0x7f;
387
388 return rate;
389}
390
391
f88bd288 392#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
393static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
394 struct eapol_state_machine *sm,
395 const u8 *eap, size_t len)
396{
397 const u8 *identity;
398 size_t identity_len;
399
400 if (len <= sizeof(struct eap_hdr) ||
401 eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
402 return;
403
404 identity = eap_get_identity(sm->eap, &identity_len);
405 if (identity == NULL)
406 return;
407
408 /* Save station identity for future RADIUS packets */
409 os_free(sm->identity);
410 sm->identity = os_malloc(identity_len + 1);
411 if (sm->identity == NULL) {
412 sm->identity_len = 0;
413 return;
414 }
415
416 os_memcpy(sm->identity, identity, identity_len);
417 sm->identity_len = identity_len;
418 sm->identity[identity_len] = '\0';
419 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
420 HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
421 sm->dot1xAuthEapolRespIdFramesRx++;
422}
423
424
425static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
426 struct sta_info *sta,
427 const u8 *eap, size_t len)
428{
429 struct radius_msg *msg;
430 char buf[128];
431 struct eapol_state_machine *sm = sta->eapol_sm;
432
433 if (sm == NULL)
434 return;
435
436 ieee802_1x_learn_identity(hapd, sm, eap, len);
437
438 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
439 "packet");
440
441 sm->radius_identifier = radius_client_get_id(hapd->radius);
442 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
443 sm->radius_identifier);
444 if (msg == NULL) {
445 printf("Could not create net RADIUS packet\n");
446 return;
447 }
448
449 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
450
451 if (sm->identity &&
452 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
453 sm->identity, sm->identity_len)) {
454 printf("Could not add User-Name\n");
455 goto fail;
456 }
457
458 if (hapd->conf->own_ip_addr.af == AF_INET &&
459 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
460 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
461 printf("Could not add NAS-IP-Address\n");
462 goto fail;
463 }
464
465#ifdef CONFIG_IPV6
466 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
467 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
468 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
469 printf("Could not add NAS-IPv6-Address\n");
470 goto fail;
471 }
472#endif /* CONFIG_IPV6 */
473
474 if (hapd->conf->nas_identifier &&
475 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
476 (u8 *) hapd->conf->nas_identifier,
477 os_strlen(hapd->conf->nas_identifier))) {
478 printf("Could not add NAS-Identifier\n");
479 goto fail;
480 }
481
482 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
483 printf("Could not add NAS-Port\n");
484 goto fail;
485 }
486
487 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
488 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
489 buf[sizeof(buf) - 1] = '\0';
490 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
491 (u8 *) buf, os_strlen(buf))) {
492 printf("Could not add Called-Station-Id\n");
493 goto fail;
494 }
495
496 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
497 MAC2STR(sta->addr));
498 buf[sizeof(buf) - 1] = '\0';
499 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
500 (u8 *) buf, os_strlen(buf))) {
501 printf("Could not add Calling-Station-Id\n");
502 goto fail;
503 }
504
505 /* TODO: should probably check MTU from driver config; 2304 is max for
506 * IEEE 802.11, but use 1400 to avoid problems with too large packets
507 */
508 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
509 printf("Could not add Framed-MTU\n");
510 goto fail;
511 }
512
513 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
514 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
515 printf("Could not add NAS-Port-Type\n");
516 goto fail;
517 }
518
519 if (sta->flags & WLAN_STA_PREAUTH) {
520 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
521 sizeof(buf));
522 } else {
523 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
524 radius_sta_rate(hapd, sta) / 2,
525 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
526 radius_mode_txt(hapd));
527 buf[sizeof(buf) - 1] = '\0';
528 }
529 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
530 (u8 *) buf, os_strlen(buf))) {
531 printf("Could not add Connect-Info\n");
532 goto fail;
533 }
534
535 if (eap && !radius_msg_add_eap(msg, eap, len)) {
536 printf("Could not add EAP-Message\n");
537 goto fail;
538 }
539
540 /* State attribute must be copied if and only if this packet is
541 * Access-Request reply to the previous Access-Challenge */
542 if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
543 RADIUS_CODE_ACCESS_CHALLENGE) {
544 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
545 RADIUS_ATTR_STATE);
546 if (res < 0) {
547 printf("Could not copy State attribute from previous "
548 "Access-Challenge\n");
549 goto fail;
550 }
551 if (res > 0) {
552 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
553 }
554 }
555
556 radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
557 return;
558
559 fail:
560 radius_msg_free(msg);
561 os_free(msg);
562}
f88bd288 563#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
564
565
6fc6879b
JM
566static void handle_eap_response(struct hostapd_data *hapd,
567 struct sta_info *sta, struct eap_hdr *eap,
568 size_t len)
569{
570 u8 type, *data;
571 struct eapol_state_machine *sm = sta->eapol_sm;
572 if (sm == NULL)
573 return;
574
575 data = (u8 *) (eap + 1);
576
577 if (len < sizeof(*eap) + 1) {
578 printf("handle_eap_response: too short response data\n");
579 return;
580 }
581
582 sm->eap_type_supp = type = data[0];
6fc6879b
JM
583
584 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
585 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
586 "id=%d len=%d) from STA: EAP Response-%s (%d)",
587 eap->code, eap->identifier, be_to_host16(eap->length),
2773ca09 588 eap_server_get_name(0, type), type);
6fc6879b
JM
589
590 sm->dot1xAuthEapolRespFramesRx++;
591
592 wpabuf_free(sm->eap_if->eapRespData);
593 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
594 sm->eapolEap = TRUE;
595}
596
597
598/* Process incoming EAP packet from Supplicant */
599static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
600 u8 *buf, size_t len)
601{
602 struct eap_hdr *eap;
603 u16 eap_len;
604
605 if (len < sizeof(*eap)) {
606 printf(" too short EAP packet\n");
607 return;
608 }
609
610 eap = (struct eap_hdr *) buf;
611
612 eap_len = be_to_host16(eap->length);
613 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
614 eap->code, eap->identifier, eap_len);
615 if (eap_len < sizeof(*eap)) {
616 wpa_printf(MSG_DEBUG, " Invalid EAP length");
617 return;
618 } else if (eap_len > len) {
619 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
620 "packet");
621 return;
622 } else if (eap_len < len) {
623 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
624 "packet", (unsigned long) len - eap_len);
625 }
626
627 switch (eap->code) {
628 case EAP_CODE_REQUEST:
629 wpa_printf(MSG_DEBUG, " (request)");
630 return;
631 case EAP_CODE_RESPONSE:
632 wpa_printf(MSG_DEBUG, " (response)");
633 handle_eap_response(hapd, sta, eap, eap_len);
634 break;
635 case EAP_CODE_SUCCESS:
636 wpa_printf(MSG_DEBUG, " (success)");
637 return;
638 case EAP_CODE_FAILURE:
639 wpa_printf(MSG_DEBUG, " (failure)");
640 return;
641 default:
642 wpa_printf(MSG_DEBUG, " (unknown code)");
643 return;
644 }
645}
646
647
c02d52b4
JM
648static struct eapol_state_machine *
649ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
650{
651 int flags = 0;
652 if (sta->flags & WLAN_STA_PREAUTH)
653 flags |= EAPOL_SM_PREAUTH;
654 if (sta->wpa_sm) {
655 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
656 flags |= EAPOL_SM_USES_WPA;
657 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
658 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
659 }
d79b7792
JM
660 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
661 sta->wps_ie, sta);
c02d52b4
JM
662}
663
664
1c6e69cc
JM
665/**
666 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
667 * @hapd: hostapd BSS data
668 * @sa: Source address (sender of the EAPOL frame)
669 * @buf: EAPOL frame
670 * @len: Length of buf in octets
671 *
672 * This function is called for each incoming EAPOL frame from the interface
673 */
6fc6879b
JM
674void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
675 size_t len)
676{
677 struct sta_info *sta;
678 struct ieee802_1x_hdr *hdr;
679 struct ieee802_1x_eapol_key *key;
680 u16 datalen;
681 struct rsn_pmksa_cache_entry *pmksa;
682
ad08c363
JM
683 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
684 !hapd->conf->wps_state)
6fc6879b
JM
685 return;
686
687 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
688 (unsigned long) len, MAC2STR(sa));
689 sta = ap_get_sta(hapd, sa);
940a0ce9
JM
690 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
691 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
692 "associated STA");
6fc6879b
JM
693 return;
694 }
695
696 if (len < sizeof(*hdr)) {
697 printf(" too short IEEE 802.1X packet\n");
698 return;
699 }
700
701 hdr = (struct ieee802_1x_hdr *) buf;
702 datalen = be_to_host16(hdr->length);
703 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
704 hdr->version, hdr->type, datalen);
705
706 if (len - sizeof(*hdr) < datalen) {
707 printf(" frame too short for this IEEE 802.1X packet\n");
708 if (sta->eapol_sm)
709 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
710 return;
711 }
712 if (len - sizeof(*hdr) > datalen) {
713 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
714 "IEEE 802.1X packet",
715 (unsigned long) len - sizeof(*hdr) - datalen);
716 }
717
718 if (sta->eapol_sm) {
719 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
720 sta->eapol_sm->dot1xAuthEapolFramesRx++;
721 }
722
723 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
724 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
725 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
726 (key->type == EAPOL_KEY_TYPE_WPA ||
727 key->type == EAPOL_KEY_TYPE_RSN)) {
728 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
729 sizeof(*hdr) + datalen);
730 return;
731 }
732
ad08c363
JM
733 if ((!hapd->conf->ieee802_1x &&
734 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
56586197 735 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
6fc6879b
JM
736 return;
737
738 if (!sta->eapol_sm) {
c02d52b4 739 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
6fc6879b
JM
740 if (!sta->eapol_sm)
741 return;
ad08c363
JM
742
743#ifdef CONFIG_WPS
744 if (!hapd->conf->ieee802_1x &&
745 ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
746 WLAN_STA_MAYBE_WPS)) {
747 /*
748 * Delay EAPOL frame transmission until a possible WPS
749 * STA initiates the handshake with EAPOL-Start.
750 */
751 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
752 }
753#endif /* CONFIG_WPS */
a60e7213
JM
754
755 sta->eapol_sm->eap_if->portEnabled = TRUE;
6fc6879b
JM
756 }
757
758 /* since we support version 1, we can ignore version field and proceed
759 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
760 /* TODO: actually, we are not version 1 anymore.. However, Version 2
761 * does not change frame contents, so should be ok to process frames
762 * more or less identically. Some changes might be needed for
763 * verification of fields. */
764
765 switch (hdr->type) {
766 case IEEE802_1X_TYPE_EAP_PACKET:
767 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
768 break;
769
770 case IEEE802_1X_TYPE_EAPOL_START:
771 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
772 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
773 "from STA");
774 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
775 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
776 if (pmksa) {
777 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
778 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
779 "available - ignore it since "
780 "STA sent EAPOL-Start");
781 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
782 }
783 sta->eapol_sm->eapolStart = TRUE;
784 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
785 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
786 break;
787
788 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
789 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
790 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
791 "from STA");
792 sta->acct_terminate_cause =
793 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
b1fa8bf1 794 accounting_sta_stop(hapd, sta);
6fc6879b
JM
795 sta->eapol_sm->eapolLogoff = TRUE;
796 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
797 break;
798
799 case IEEE802_1X_TYPE_EAPOL_KEY:
800 wpa_printf(MSG_DEBUG, " EAPOL-Key");
801 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
802 wpa_printf(MSG_DEBUG, " Dropped key data from "
803 "unauthorized Supplicant");
804 break;
805 }
806 break;
807
808 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
809 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
810 /* TODO: implement support for this; show data */
811 break;
812
813 default:
814 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
815 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
816 break;
817 }
818
819 eapol_auth_step(sta->eapol_sm);
820}
821
822
1c6e69cc
JM
823/**
824 * ieee802_1x_new_station - Start IEEE 802.1X authentication
825 * @hapd: hostapd BSS data
826 * @sta: The station
827 *
828 * This function is called to start IEEE 802.1X authentication when a new
829 * station completes IEEE 802.11 association.
830 */
6fc6879b
JM
831void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
832{
833 struct rsn_pmksa_cache_entry *pmksa;
834 int reassoc = 1;
835 int force_1x = 0;
836
ad08c363 837#ifdef CONFIG_WPS
a60e7213 838 if (hapd->conf->wps_state && hapd->conf->wpa &&
ad08c363
JM
839 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
840 /*
841 * Need to enable IEEE 802.1X/EAPOL state machines for possible
842 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
843 * authentication in this BSS.
844 */
845 force_1x = 1;
846 }
847#endif /* CONFIG_WPS */
848
6fc6879b 849 if ((!force_1x && !hapd->conf->ieee802_1x) ||
56586197 850 wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
6fc6879b
JM
851 return;
852
853 if (sta->eapol_sm == NULL) {
854 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
855 HOSTAPD_LEVEL_DEBUG, "start authentication");
c02d52b4 856 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
6fc6879b
JM
857 if (sta->eapol_sm == NULL) {
858 hostapd_logger(hapd, sta->addr,
859 HOSTAPD_MODULE_IEEE8021X,
860 HOSTAPD_LEVEL_INFO,
861 "failed to allocate state machine");
862 return;
863 }
864 reassoc = 0;
865 }
866
ad08c363 867#ifdef CONFIG_WPS
a8d05fca 868 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
ad08c363
JM
869 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
870 /*
871 * Delay EAPOL frame transmission until a possible WPS
872 * initiates the handshake with EAPOL-Start.
873 */
874 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
875 }
876#endif /* CONFIG_WPS */
877
6fc6879b
JM
878 sta->eapol_sm->eap_if->portEnabled = TRUE;
879
880 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
881 if (pmksa) {
882 int old_vlanid;
883
884 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
885 HOSTAPD_LEVEL_DEBUG,
886 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
887 /* Setup EAPOL state machines to already authenticated state
888 * because of existing PMKSA information in the cache. */
889 sta->eapol_sm->keyRun = TRUE;
890 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
891 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
892 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
893 sta->eapol_sm->authSuccess = TRUE;
894 if (sta->eapol_sm->eap)
895 eap_sm_notify_cached(sta->eapol_sm->eap);
896 old_vlanid = sta->vlan_id;
897 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
898 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
899 sta->vlan_id = 0;
900 ap_sta_bind_vlan(hapd, sta, old_vlanid);
901 } else {
902 if (reassoc) {
903 /*
904 * Force EAPOL state machines to start
905 * re-authentication without having to wait for the
906 * Supplicant to send EAPOL-Start.
907 */
908 sta->eapol_sm->reAuthenticate = TRUE;
909 }
910 eapol_auth_step(sta->eapol_sm);
911 }
912}
913
914
6fc6879b
JM
915void ieee802_1x_free_station(struct sta_info *sta)
916{
917 struct eapol_state_machine *sm = sta->eapol_sm;
918
6fc6879b
JM
919 if (sm == NULL)
920 return;
921
922 sta->eapol_sm = NULL;
923
f88bd288 924#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
925 if (sm->last_recv_radius) {
926 radius_msg_free(sm->last_recv_radius);
927 os_free(sm->last_recv_radius);
928 }
010dc068 929 radius_free_class(&sm->radius_class);
f88bd288 930#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
931
932 os_free(sm->identity);
6fc6879b
JM
933 eapol_auth_free(sm);
934}
935
936
f88bd288 937#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
938static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
939 struct sta_info *sta)
940{
941 u8 *eap;
942 size_t len;
943 struct eap_hdr *hdr;
944 int eap_type = -1;
945 char buf[64];
946 struct radius_msg *msg;
947 struct eapol_state_machine *sm = sta->eapol_sm;
948
949 if (sm == NULL || sm->last_recv_radius == NULL) {
950 if (sm)
951 sm->eap_if->aaaEapNoReq = TRUE;
952 return;
953 }
954
955 msg = sm->last_recv_radius;
956
957 eap = radius_msg_get_eap(msg, &len);
958 if (eap == NULL) {
959 /* RFC 3579, Chap. 2.6.3:
960 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
961 * attribute */
962 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
963 HOSTAPD_LEVEL_WARNING, "could not extract "
964 "EAP-Message from RADIUS message");
965 sm->eap_if->aaaEapNoReq = TRUE;
966 return;
967 }
968
969 if (len < sizeof(*hdr)) {
970 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
971 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
972 "received from authentication server");
973 os_free(eap);
974 sm->eap_if->aaaEapNoReq = TRUE;
975 return;
976 }
977
978 if (len > sizeof(*hdr))
979 eap_type = eap[sizeof(*hdr)];
980
981 hdr = (struct eap_hdr *) eap;
982 switch (hdr->code) {
983 case EAP_CODE_REQUEST:
984 if (eap_type >= 0)
985 sm->eap_type_authsrv = eap_type;
986 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
2773ca09
JM
987 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
988 "??",
6fc6879b
JM
989 eap_type);
990 break;
991 case EAP_CODE_RESPONSE:
992 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
2773ca09
JM
993 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
994 "??",
6fc6879b
JM
995 eap_type);
996 break;
997 case EAP_CODE_SUCCESS:
998 os_strlcpy(buf, "EAP Success", sizeof(buf));
999 break;
1000 case EAP_CODE_FAILURE:
1001 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1002 break;
1003 default:
1004 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1005 break;
1006 }
1007 buf[sizeof(buf) - 1] = '\0';
1008 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1009 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1010 "id=%d len=%d) from RADIUS server: %s",
1011 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1012 buf);
1013 sm->eap_if->aaaEapReq = TRUE;
1014
1015 wpabuf_free(sm->eap_if->aaaEapReqData);
1016 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1017}
1018
1019
1020static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1021 struct sta_info *sta, struct radius_msg *msg,
1022 struct radius_msg *req,
7d02e641
JM
1023 const u8 *shared_secret,
1024 size_t shared_secret_len)
6fc6879b
JM
1025{
1026 struct radius_ms_mppe_keys *keys;
1027 struct eapol_state_machine *sm = sta->eapol_sm;
1028 if (sm == NULL)
1029 return;
1030
1031 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1032 shared_secret_len);
1033
1034 if (keys && keys->send && keys->recv) {
1035 size_t len = keys->send_len + keys->recv_len;
1036 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1037 keys->send, keys->send_len);
1038 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1039 keys->recv, keys->recv_len);
1040
1041 os_free(sm->eap_if->aaaEapKeyData);
1042 sm->eap_if->aaaEapKeyData = os_malloc(len);
1043 if (sm->eap_if->aaaEapKeyData) {
1044 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1045 keys->recv_len);
1046 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1047 keys->send, keys->send_len);
1048 sm->eap_if->aaaEapKeyDataLen = len;
1049 sm->eap_if->aaaEapKeyAvailable = TRUE;
1050 }
1051 }
1052
1053 if (keys) {
1054 os_free(keys->send);
1055 os_free(keys->recv);
1056 os_free(keys);
1057 }
1058}
1059
1060
1061static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1062 struct sta_info *sta,
1063 struct radius_msg *msg)
1064{
1065 u8 *class;
1066 size_t class_len;
1067 struct eapol_state_machine *sm = sta->eapol_sm;
1068 int count, i;
1069 struct radius_attr_data *nclass;
1070 size_t nclass_count;
1071
1072 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1073 sm == NULL)
1074 return;
1075
010dc068 1076 radius_free_class(&sm->radius_class);
6fc6879b
JM
1077 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1078 if (count <= 0)
1079 return;
1080
1081 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1082 if (nclass == NULL)
1083 return;
1084
1085 nclass_count = 0;
1086
1087 class = NULL;
1088 for (i = 0; i < count; i++) {
1089 do {
1090 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1091 &class, &class_len,
1092 class) < 0) {
1093 i = count;
1094 break;
1095 }
1096 } while (class_len < 1);
1097
1098 nclass[nclass_count].data = os_malloc(class_len);
1099 if (nclass[nclass_count].data == NULL)
1100 break;
1101
1102 os_memcpy(nclass[nclass_count].data, class, class_len);
1103 nclass[nclass_count].len = class_len;
1104 nclass_count++;
1105 }
1106
1107 sm->radius_class.attr = nclass;
1108 sm->radius_class.count = nclass_count;
1109 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1110 "attributes for " MACSTR,
1111 (unsigned long) sm->radius_class.count,
1112 MAC2STR(sta->addr));
1113}
1114
1115
1116/* Update sta->identity based on User-Name attribute in Access-Accept */
1117static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1118 struct sta_info *sta,
1119 struct radius_msg *msg)
1120{
1121 u8 *buf, *identity;
1122 size_t len;
1123 struct eapol_state_machine *sm = sta->eapol_sm;
1124
1125 if (sm == NULL)
1126 return;
1127
1128 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1129 NULL) < 0)
1130 return;
1131
1132 identity = os_malloc(len + 1);
1133 if (identity == NULL)
1134 return;
1135
1136 os_memcpy(identity, buf, len);
1137 identity[len] = '\0';
1138
1139 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1140 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1141 "User-Name from Access-Accept '%s'",
1142 sm->identity ? (char *) sm->identity : "N/A",
1143 (char *) identity);
1144
1145 os_free(sm->identity);
1146 sm->identity = identity;
1147 sm->identity_len = len;
1148}
1149
1150
1151struct sta_id_search {
1152 u8 identifier;
1153 struct eapol_state_machine *sm;
1154};
1155
1156
1157static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1158 struct sta_info *sta,
1159 void *ctx)
1160{
1161 struct sta_id_search *id_search = ctx;
1162 struct eapol_state_machine *sm = sta->eapol_sm;
1163
1164 if (sm && sm->radius_identifier >= 0 &&
1165 sm->radius_identifier == id_search->identifier) {
1166 id_search->sm = sm;
1167 return 1;
1168 }
1169 return 0;
1170}
1171
1172
1173static struct eapol_state_machine *
1174ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1175{
1176 struct sta_id_search id_search;
1177 id_search.identifier = identifier;
1178 id_search.sm = NULL;
1179 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1180 return id_search.sm;
1181}
1182
1183
1c6e69cc
JM
1184/**
1185 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1186 * @msg: RADIUS response message
1187 * @req: RADIUS request message
1188 * @shared_secret: RADIUS shared secret
1189 * @shared_secret_len: Length of shared_secret in octets
1190 * @data: Context data (struct hostapd_data *)
1191 * Returns: Processing status
1192 */
6fc6879b
JM
1193static RadiusRxResult
1194ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
7d02e641 1195 const u8 *shared_secret, size_t shared_secret_len,
6fc6879b
JM
1196 void *data)
1197{
1198 struct hostapd_data *hapd = data;
1199 struct sta_info *sta;
1200 u32 session_timeout = 0, termination_action, acct_interim_interval;
1201 int session_timeout_set, old_vlanid = 0;
6fc6879b
JM
1202 struct eapol_state_machine *sm;
1203 int override_eapReq = 0;
1204
1205 sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1206 if (sm == NULL) {
1207 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1208 "station for this RADIUS message");
1209 return RADIUS_RX_UNKNOWN;
1210 }
1211 sta = sm->sta;
1212
1213 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1214 * present when packet contains an EAP-Message attribute */
1215 if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1216 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1217 0) < 0 &&
1218 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1219 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1220 "Message-Authenticator since it does not include "
1221 "EAP-Message");
1222 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1223 req, 1)) {
1224 printf("Incoming RADIUS packet did not have correct "
1225 "Message-Authenticator - dropped\n");
1226 return RADIUS_RX_INVALID_AUTHENTICATOR;
1227 }
1228
1229 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1230 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1231 msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1232 printf("Unknown RADIUS message code\n");
1233 return RADIUS_RX_UNKNOWN;
1234 }
1235
1236 sm->radius_identifier = -1;
1237 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1238 MAC2STR(sta->addr));
1239
1240 if (sm->last_recv_radius) {
1241 radius_msg_free(sm->last_recv_radius);
1242 os_free(sm->last_recv_radius);
1243 }
1244
1245 sm->last_recv_radius = msg;
1246
1247 session_timeout_set =
1248 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1249 &session_timeout);
1250 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1251 &termination_action))
1252 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1253
5843e1c9 1254 if (hapd->conf->acct_interim_interval == 0 &&
6fc6879b
JM
1255 msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1256 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1257 &acct_interim_interval) == 0) {
1258 if (acct_interim_interval < 60) {
1259 hostapd_logger(hapd, sta->addr,
1260 HOSTAPD_MODULE_IEEE8021X,
1261 HOSTAPD_LEVEL_INFO,
1262 "ignored too small "
1263 "Acct-Interim-Interval %d",
1264 acct_interim_interval);
1265 } else
1266 sta->acct_interim_interval = acct_interim_interval;
1267 }
1268
1269
1270 switch (msg->hdr->code) {
1271 case RADIUS_CODE_ACCESS_ACCEPT:
1272 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1273 sta->vlan_id = 0;
30b32314 1274#ifndef CONFIG_NO_VLAN
6fc6879b
JM
1275 else {
1276 old_vlanid = sta->vlan_id;
1277 sta->vlan_id = radius_msg_get_vlanid(msg);
1278 }
1279 if (sta->vlan_id > 0 &&
1280 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1281 sta->vlan_id)) {
1282 hostapd_logger(hapd, sta->addr,
1283 HOSTAPD_MODULE_RADIUS,
1284 HOSTAPD_LEVEL_INFO,
1285 "VLAN ID %d", sta->vlan_id);
1286 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1287 sta->eapol_sm->authFail = TRUE;
1288 hostapd_logger(hapd, sta->addr,
1289 HOSTAPD_MODULE_IEEE8021X,
1290 HOSTAPD_LEVEL_INFO, "authentication "
1291 "server did not include required VLAN "
1292 "ID in Access-Accept");
1293 break;
1294 }
30b32314 1295#endif /* CONFIG_NO_VLAN */
6fc6879b
JM
1296
1297 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1298
1299 /* RFC 3580, Ch. 3.17 */
1300 if (session_timeout_set && termination_action ==
1301 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1302 sm->reAuthPeriod = session_timeout;
1303 } else if (session_timeout_set)
1304 ap_sta_session_timeout(hapd, sta, session_timeout);
1305
1306 sm->eap_if->aaaSuccess = TRUE;
1307 override_eapReq = 1;
1308 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1309 shared_secret_len);
1310 ieee802_1x_store_radius_class(hapd, sta, msg);
1311 ieee802_1x_update_sta_identity(hapd, sta, msg);
1312 if (sm->eap_if->eapKeyAvailable &&
1313 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1314 session_timeout_set ?
1315 (int) session_timeout : -1, sm) == 0) {
1316 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1317 HOSTAPD_LEVEL_DEBUG,
1318 "Added PMKSA cache entry");
1319 }
1320 break;
1321 case RADIUS_CODE_ACCESS_REJECT:
1322 sm->eap_if->aaaFail = TRUE;
1323 override_eapReq = 1;
1324 break;
1325 case RADIUS_CODE_ACCESS_CHALLENGE:
1326 sm->eap_if->aaaEapReq = TRUE;
1327 if (session_timeout_set) {
1328 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
8e09c6d2
JM
1329 sm->eap_if->aaaMethodTimeout = session_timeout;
1330 hostapd_logger(hapd, sm->addr,
1331 HOSTAPD_MODULE_IEEE8021X,
1332 HOSTAPD_LEVEL_DEBUG,
1333 "using EAP timeout of %d seconds (from "
1334 "RADIUS)",
1335 sm->eap_if->aaaMethodTimeout);
1336 } else {
1337 /*
1338 * Use dynamic retransmission behavior per EAP
1339 * specification.
1340 */
1341 sm->eap_if->aaaMethodTimeout = 0;
1342 }
6fc6879b
JM
1343 break;
1344 }
1345
1346 ieee802_1x_decapsulate_radius(hapd, sta);
1347 if (override_eapReq)
1348 sm->eap_if->aaaEapReq = FALSE;
1349
1350 eapol_auth_step(sm);
1351
1352 return RADIUS_RX_QUEUED;
1353}
f88bd288 1354#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1355
1356
1357void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1358{
1359 struct eapol_state_machine *sm = sta->eapol_sm;
1360 if (sm == NULL)
1361 return;
1362
1363 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1364 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1365
f88bd288 1366#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1367 if (sm->last_recv_radius) {
1368 radius_msg_free(sm->last_recv_radius);
1369 os_free(sm->last_recv_radius);
1370 sm->last_recv_radius = NULL;
1371 }
f88bd288 1372#endif /* CONFIG_NO_RADIUS */
805e6dc6
JM
1373
1374 if (sm->eap_if->eapTimeout) {
1375 /*
1376 * Disconnect the STA since it did not reply to the last EAP
1377 * request and we cannot continue EAP processing (EAP-Failure
1378 * could only be sent if the EAP peer actually replied).
1379 */
1380 sm->eap_if->portEnabled = FALSE;
1381 hostapd_sta_deauth(hapd, sta->addr,
1382 WLAN_REASON_PREV_AUTH_NOT_VALID);
1383 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1384 WLAN_STA_AUTHORIZED);
1385 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1386 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1387 sta->timeout_next = STA_REMOVE;
1388 }
6fc6879b
JM
1389}
1390
1391
6fc6879b
JM
1392static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1393{
f55802e8
JM
1394 struct eapol_authenticator *eapol = hapd->eapol_auth;
1395
6fc6879b
JM
1396 if (hapd->conf->default_wep_key_len < 1)
1397 return 0;
1398
f55802e8
JM
1399 os_free(eapol->default_wep_key);
1400 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1401 if (eapol->default_wep_key == NULL ||
1402 os_get_random(eapol->default_wep_key,
6fc6879b
JM
1403 hapd->conf->default_wep_key_len)) {
1404 printf("Could not generate random WEP key.\n");
f55802e8
JM
1405 os_free(eapol->default_wep_key);
1406 eapol->default_wep_key = NULL;
6fc6879b
JM
1407 return -1;
1408 }
1409
1410 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
f55802e8 1411 eapol->default_wep_key,
6fc6879b
JM
1412 hapd->conf->default_wep_key_len);
1413
1414 return 0;
1415}
1416
1417
1418static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1419 struct sta_info *sta, void *ctx)
1420{
1421 if (sta->eapol_sm) {
1422 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1423 eapol_auth_step(sta->eapol_sm);
1424 }
1425 return 0;
1426}
1427
1428
1429static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1430{
1431 struct hostapd_data *hapd = eloop_ctx;
f55802e8 1432 struct eapol_authenticator *eapol = hapd->eapol_auth;
6fc6879b 1433
f55802e8
JM
1434 if (eapol->default_wep_key_idx >= 3)
1435 eapol->default_wep_key_idx =
6fc6879b
JM
1436 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1437 else
f55802e8 1438 eapol->default_wep_key_idx++;
6fc6879b
JM
1439
1440 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
f55802e8 1441 eapol->default_wep_key_idx);
6fc6879b
JM
1442
1443 if (ieee802_1x_rekey_broadcast(hapd)) {
1444 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1445 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1446 "new broadcast key");
f55802e8
JM
1447 os_free(eapol->default_wep_key);
1448 eapol->default_wep_key = NULL;
6fc6879b
JM
1449 return;
1450 }
1451
1452 /* TODO: Could setup key for RX here, but change default TX keyid only
1453 * after new broadcast key has been sent to all stations. */
89d39d9d 1454 if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
f55802e8
JM
1455 eapol->default_wep_key_idx, 1, NULL, 0,
1456 eapol->default_wep_key,
89d39d9d 1457 hapd->conf->default_wep_key_len)) {
6fc6879b
JM
1458 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1459 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1460 "new broadcast key");
f55802e8
JM
1461 os_free(eapol->default_wep_key);
1462 eapol->default_wep_key = NULL;
6fc6879b
JM
1463 return;
1464 }
1465
1466 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1467
1468 if (hapd->conf->wep_rekeying_period > 0) {
1469 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1470 ieee802_1x_rekey, hapd, NULL);
1471 }
1472}
1473
1474
1475static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1476 const u8 *data, size_t datalen)
1477{
1478 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1479}
1480
1481
1482static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1483 const u8 *data, size_t datalen)
1484{
f88bd288 1485#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1486 struct hostapd_data *hapd = ctx;
1487 struct sta_info *sta = sta_ctx;
1488
1489 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
f88bd288 1490#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1491}
1492
1493
1494static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1495 int preauth)
1496{
1497 struct hostapd_data *hapd = ctx;
1498 struct sta_info *sta = sta_ctx;
1499 if (preauth)
1500 rsn_preauth_finished(hapd, sta, success);
1501 else
1502 ieee802_1x_finished(hapd, sta, success);
1503}
1504
1505
1506static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1507 size_t identity_len, int phase2,
1508 struct eap_user *user)
1509{
1510 struct hostapd_data *hapd = ctx;
1511 const struct hostapd_eap_user *eap_user;
1512 int i, count;
1513
1514 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1515 identity_len, phase2);
1516 if (eap_user == NULL)
1517 return -1;
1518
1519 os_memset(user, 0, sizeof(*user));
1520 user->phase2 = phase2;
1521 count = EAP_USER_MAX_METHODS;
1522 if (count > EAP_MAX_METHODS)
1523 count = EAP_MAX_METHODS;
1524 for (i = 0; i < count; i++) {
1525 user->methods[i].vendor = eap_user->methods[i].vendor;
1526 user->methods[i].method = eap_user->methods[i].method;
1527 }
1528
1529 if (eap_user->password) {
1530 user->password = os_malloc(eap_user->password_len);
1531 if (user->password == NULL)
1532 return -1;
1533 os_memcpy(user->password, eap_user->password,
1534 eap_user->password_len);
1535 user->password_len = eap_user->password_len;
1536 }
1537 user->force_version = eap_user->force_version;
1538 user->ttls_auth = eap_user->ttls_auth;
1539
1540 return 0;
1541}
1542
1543
1544static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1545{
1546 struct hostapd_data *hapd = ctx;
1547 struct sta_info *sta;
1548 sta = ap_get_sta(hapd, addr);
1549 if (sta == NULL || sta->eapol_sm == NULL)
1550 return 0;
1551 return 1;
1552}
1553
1554
1555static void ieee802_1x_logger(void *ctx, const u8 *addr,
1556 eapol_logger_level level, const char *txt)
1557{
71f04b3c 1558#ifndef CONFIG_NO_HOSTAPD_LOGGER
6fc6879b
JM
1559 struct hostapd_data *hapd = ctx;
1560 int hlevel;
1561
1562 switch (level) {
1563 case EAPOL_LOGGER_WARNING:
1564 hlevel = HOSTAPD_LEVEL_WARNING;
1565 break;
1566 case EAPOL_LOGGER_INFO:
1567 hlevel = HOSTAPD_LEVEL_INFO;
1568 break;
1569 case EAPOL_LOGGER_DEBUG:
1570 default:
1571 hlevel = HOSTAPD_LEVEL_DEBUG;
1572 break;
1573 }
1574
1575 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1576 txt);
71f04b3c 1577#endif /* CONFIG_NO_HOSTAPD_LOGGER */
6fc6879b
JM
1578}
1579
1580
1581static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1582 int authorized)
1583{
1584 struct hostapd_data *hapd = ctx;
1585 struct sta_info *sta = sta_ctx;
1586 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1587}
1588
1589
1590static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1591{
1592 struct hostapd_data *hapd = ctx;
1593 struct sta_info *sta = sta_ctx;
1594 ieee802_1x_abort_auth(hapd, sta);
1595}
1596
1597
1598static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1599{
1600 struct hostapd_data *hapd = ctx;
1601 struct sta_info *sta = sta_ctx;
1602 ieee802_1x_tx_key(hapd, sta);
1603}
1604
1605
38294200
JM
1606static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1607 enum eapol_event type)
1608{
1609 /* struct hostapd_data *hapd = ctx; */
1610 struct sta_info *sta = sta_ctx;
1611 switch (type) {
1612 case EAPOL_AUTH_SM_CHANGE:
1613 wpa_auth_sm_notify(sta->wpa_sm);
1614 break;
1615 case EAPOL_AUTH_REAUTHENTICATE:
1616 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1617 break;
1618 }
1619}
1620
1621
6fc6879b
JM
1622int ieee802_1x_init(struct hostapd_data *hapd)
1623{
1624 int i;
1625 struct eapol_auth_config conf;
1626 struct eapol_auth_cb cb;
1627
1628 os_memset(&conf, 0, sizeof(conf));
a2befd37 1629 conf.ctx = hapd;
6fc6879b
JM
1630 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1631 conf.wpa = hapd->conf->wpa;
1632 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1633 conf.eap_server = hapd->conf->eap_server;
1634 conf.ssl_ctx = hapd->ssl_ctx;
1635 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1636 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1637 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1638 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1639 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2d867244
JM
1640 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1641 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
378eae5e 1642 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
a11c90a6
JM
1643 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1644 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
6fc6879b 1645 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
c3e258ae 1646 conf.tnc = hapd->conf->tnc;
ad08c363 1647 conf.wps = hapd->wps;
6fc6879b
JM
1648
1649 os_memset(&cb, 0, sizeof(cb));
1650 cb.eapol_send = ieee802_1x_eapol_send;
1651 cb.aaa_send = ieee802_1x_aaa_send;
1652 cb.finished = _ieee802_1x_finished;
1653 cb.get_eap_user = ieee802_1x_get_eap_user;
1654 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1655 cb.logger = ieee802_1x_logger;
1656 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1657 cb.abort_auth = _ieee802_1x_abort_auth;
1658 cb.tx_key = _ieee802_1x_tx_key;
38294200 1659 cb.eapol_event = ieee802_1x_eapol_event;
6fc6879b
JM
1660
1661 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1662 if (hapd->eapol_auth == NULL)
1663 return -1;
1664
1665 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1666 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1667 return -1;
1668
f88bd288 1669#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1670 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1671 ieee802_1x_receive_auth, hapd))
1672 return -1;
f88bd288 1673#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1674
1675 if (hapd->conf->default_wep_key_len) {
1676 hostapd_set_privacy(hapd, 1);
1677
1678 for (i = 0; i < 4; i++)
89d39d9d
JM
1679 hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
1680 NULL, i, 0, NULL, 0, NULL, 0);
6fc6879b
JM
1681
1682 ieee802_1x_rekey(hapd, NULL);
1683
f55802e8 1684 if (hapd->eapol_auth->default_wep_key == NULL)
6fc6879b
JM
1685 return -1;
1686 }
1687
1688 return 0;
1689}
1690
1691
1692void ieee802_1x_deinit(struct hostapd_data *hapd)
1693{
1694 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1695
1696 if (hapd->driver != NULL &&
1697 (hapd->conf->ieee802_1x || hapd->conf->wpa))
1698 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1699
1700 eapol_auth_deinit(hapd->eapol_auth);
1701 hapd->eapol_auth = NULL;
1702}
1703
1704
1705int ieee802_1x_reconfig(struct hostapd_data *hapd,
1706 struct hostapd_config *oldconf,
1707 struct hostapd_bss_config *oldbss)
1708{
1709 ieee802_1x_deinit(hapd);
1710 return ieee802_1x_init(hapd);
1711}
1712
1713
1714int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
8607f4c3 1715 const u8 *buf, size_t len, int ack)
6fc6879b
JM
1716{
1717 struct ieee80211_hdr *hdr;
1718 struct ieee802_1x_hdr *xhdr;
1719 struct ieee802_1x_eapol_key *key;
1720 u8 *pos;
1721 const unsigned char rfc1042_hdr[ETH_ALEN] =
1722 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1723
1724 if (sta == NULL)
1725 return -1;
1726 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1727 return 0;
1728
1729 hdr = (struct ieee80211_hdr *) buf;
1730 pos = (u8 *) (hdr + 1);
1731 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1732 return 0;
1733 pos += sizeof(rfc1042_hdr);
1734 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1735 return 0;
1736 pos += 2;
1737
1738 xhdr = (struct ieee802_1x_hdr *) pos;
1739 pos += sizeof(*xhdr);
1740
1741 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1742 "type=%d length=%d - ack=%d",
1743 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1744 be_to_host16(xhdr->length), ack);
1745
1746 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1747 * or Authenticator state machines, but EAPOL-Key packets are not
1748 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1749 * packets couple of times because otherwise STA keys become
1750 * unsynchronized with AP. */
1751 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1752 pos + sizeof(*key) <= buf + len) {
1753 key = (struct ieee802_1x_eapol_key *) pos;
1754 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1755 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1756 "frame (%scast index=%d)",
1757 key->key_index & BIT(7) ? "uni" : "broad",
1758 key->key_index & ~BIT(7));
1759 /* TODO: re-send EAPOL-Key couple of times (with short delay
1760 * between them?). If all attempt fail, report error and
1761 * deauthenticate STA so that it will get new keys when
1762 * authenticating again (e.g., after returning in range).
1763 * Separate limit/transmit state needed both for unicast and
1764 * broadcast keys(?) */
1765 }
1766 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1767 * to here and change the key only if the EAPOL-Key packet was Acked.
1768 */
1769
1770 return 1;
1771}
1772
1773
1774u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1775{
1776 if (sm == NULL || sm->identity == NULL)
1777 return NULL;
1778
1779 *len = sm->identity_len;
1780 return sm->identity;
1781}
1782
1783
1784u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1785 int idx)
1786{
1787 if (sm == NULL || sm->radius_class.attr == NULL ||
1788 idx >= (int) sm->radius_class.count)
1789 return NULL;
1790
1791 *len = sm->radius_class.attr[idx].len;
1792 return sm->radius_class.attr[idx].data;
1793}
1794
1795
1796const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1797{
1798 if (sm == NULL)
1799 return NULL;
1800
1801 *len = sm->eap_if->eapKeyDataLen;
1802 return sm->eap_if->eapKeyData;
1803}
1804
1805
1806void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1807 int enabled)
1808{
1809 if (sm == NULL)
1810 return;
1811 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1812 eapol_auth_step(sm);
1813}
1814
1815
1816void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1817 int valid)
1818{
1819 if (sm == NULL)
1820 return;
1821 sm->portValid = valid ? TRUE : FALSE;
1822 eapol_auth_step(sm);
1823}
1824
1825
1826void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1827{
1828 if (sm == NULL)
1829 return;
1830 if (pre_auth)
1831 sm->flags |= EAPOL_SM_PREAUTH;
1832 else
1833 sm->flags &= ~EAPOL_SM_PREAUTH;
1834}
1835
1836
1837static const char * bool_txt(Boolean bool)
1838{
1839 return bool ? "TRUE" : "FALSE";
1840}
1841
1842
1843int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1844{
1845 /* TODO */
1846 return 0;
1847}
1848
1849
1850int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1851 char *buf, size_t buflen)
1852{
1853 int len = 0, ret;
1854 struct eapol_state_machine *sm = sta->eapol_sm;
1855
1856 if (sm == NULL)
1857 return 0;
1858
1859 ret = os_snprintf(buf + len, buflen - len,
1860 "dot1xPaePortNumber=%d\n"
1861 "dot1xPaePortProtocolVersion=%d\n"
1862 "dot1xPaePortCapabilities=1\n"
1863 "dot1xPaePortInitialize=%d\n"
1864 "dot1xPaePortReauthenticate=FALSE\n",
1865 sta->aid,
1866 EAPOL_VERSION,
1867 sm->initialize);
1868 if (ret < 0 || (size_t) ret >= buflen - len)
1869 return len;
1870 len += ret;
1871
1872 /* dot1xAuthConfigTable */
1873 ret = os_snprintf(buf + len, buflen - len,
1874 "dot1xAuthPaeState=%d\n"
1875 "dot1xAuthBackendAuthState=%d\n"
1876 "dot1xAuthAdminControlledDirections=%d\n"
1877 "dot1xAuthOperControlledDirections=%d\n"
1878 "dot1xAuthAuthControlledPortStatus=%d\n"
1879 "dot1xAuthAuthControlledPortControl=%d\n"
1880 "dot1xAuthQuietPeriod=%u\n"
1881 "dot1xAuthServerTimeout=%u\n"
1882 "dot1xAuthReAuthPeriod=%u\n"
1883 "dot1xAuthReAuthEnabled=%s\n"
1884 "dot1xAuthKeyTxEnabled=%s\n",
1885 sm->auth_pae_state + 1,
1886 sm->be_auth_state + 1,
1887 sm->adminControlledDirections,
1888 sm->operControlledDirections,
1889 sm->authPortStatus,
1890 sm->portControl,
1891 sm->quietPeriod,
1892 sm->serverTimeout,
1893 sm->reAuthPeriod,
1894 bool_txt(sm->reAuthEnabled),
1895 bool_txt(sm->keyTxEnabled));
1896 if (ret < 0 || (size_t) ret >= buflen - len)
1897 return len;
1898 len += ret;
1899
1900 /* dot1xAuthStatsTable */
1901 ret = os_snprintf(buf + len, buflen - len,
1902 "dot1xAuthEapolFramesRx=%u\n"
1903 "dot1xAuthEapolFramesTx=%u\n"
1904 "dot1xAuthEapolStartFramesRx=%u\n"
1905 "dot1xAuthEapolLogoffFramesRx=%u\n"
1906 "dot1xAuthEapolRespIdFramesRx=%u\n"
1907 "dot1xAuthEapolRespFramesRx=%u\n"
1908 "dot1xAuthEapolReqIdFramesTx=%u\n"
1909 "dot1xAuthEapolReqFramesTx=%u\n"
1910 "dot1xAuthInvalidEapolFramesRx=%u\n"
1911 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1912 "dot1xAuthLastEapolFrameVersion=%u\n"
1913 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1914 sm->dot1xAuthEapolFramesRx,
1915 sm->dot1xAuthEapolFramesTx,
1916 sm->dot1xAuthEapolStartFramesRx,
1917 sm->dot1xAuthEapolLogoffFramesRx,
1918 sm->dot1xAuthEapolRespIdFramesRx,
1919 sm->dot1xAuthEapolRespFramesRx,
1920 sm->dot1xAuthEapolReqIdFramesTx,
1921 sm->dot1xAuthEapolReqFramesTx,
1922 sm->dot1xAuthInvalidEapolFramesRx,
1923 sm->dot1xAuthEapLengthErrorFramesRx,
1924 sm->dot1xAuthLastEapolFrameVersion,
1925 MAC2STR(sm->addr));
1926 if (ret < 0 || (size_t) ret >= buflen - len)
1927 return len;
1928 len += ret;
1929
1930 /* dot1xAuthDiagTable */
1931 ret = os_snprintf(buf + len, buflen - len,
1932 "dot1xAuthEntersConnecting=%u\n"
1933 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1934 "dot1xAuthEntersAuthenticating=%u\n"
1935 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1936 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1937 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1938 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1939 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1940 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1941 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1942 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1943 "dot1xAuthBackendResponses=%u\n"
1944 "dot1xAuthBackendAccessChallenges=%u\n"
1945 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1946 "dot1xAuthBackendAuthSuccesses=%u\n"
1947 "dot1xAuthBackendAuthFails=%u\n",
1948 sm->authEntersConnecting,
1949 sm->authEapLogoffsWhileConnecting,
1950 sm->authEntersAuthenticating,
1951 sm->authAuthSuccessesWhileAuthenticating,
1952 sm->authAuthTimeoutsWhileAuthenticating,
1953 sm->authAuthFailWhileAuthenticating,
1954 sm->authAuthEapStartsWhileAuthenticating,
1955 sm->authAuthEapLogoffWhileAuthenticating,
1956 sm->authAuthReauthsWhileAuthenticated,
1957 sm->authAuthEapStartsWhileAuthenticated,
1958 sm->authAuthEapLogoffWhileAuthenticated,
1959 sm->backendResponses,
1960 sm->backendAccessChallenges,
1961 sm->backendOtherRequestsToSupplicant,
1962 sm->backendAuthSuccesses,
1963 sm->backendAuthFails);
1964 if (ret < 0 || (size_t) ret >= buflen - len)
1965 return len;
1966 len += ret;
1967
1968 /* dot1xAuthSessionStatsTable */
1969 ret = os_snprintf(buf + len, buflen - len,
1970 /* TODO: dot1xAuthSessionOctetsRx */
1971 /* TODO: dot1xAuthSessionOctetsTx */
1972 /* TODO: dot1xAuthSessionFramesRx */
1973 /* TODO: dot1xAuthSessionFramesTx */
1974 "dot1xAuthSessionId=%08X-%08X\n"
1975 "dot1xAuthSessionAuthenticMethod=%d\n"
1976 "dot1xAuthSessionTime=%u\n"
1977 "dot1xAuthSessionTerminateCause=999\n"
1978 "dot1xAuthSessionUserName=%s\n",
1979 sta->acct_session_id_hi, sta->acct_session_id_lo,
56586197
JM
1980 (wpa_key_mgmt_wpa_ieee8021x(
1981 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1982 1 : 2,
6fc6879b
JM
1983 (unsigned int) (time(NULL) -
1984 sta->acct_session_start),
1985 sm->identity);
1986 if (ret < 0 || (size_t) ret >= buflen - len)
1987 return len;
1988 len += ret;
1989
1990 return len;
1991}
1992
1993
1994static void ieee802_1x_finished(struct hostapd_data *hapd,
1995 struct sta_info *sta, int success)
1996{
1997 const u8 *key;
1998 size_t len;
1999 /* TODO: get PMKLifetime from WPA parameters */
2000 static const int dot11RSNAConfigPMKLifetime = 43200;
2001
2002 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2003 if (success && key && len >= PMK_LEN &&
2004 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2005 sta->eapol_sm) == 0) {
2006 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2007 HOSTAPD_LEVEL_DEBUG,
2008 "Added PMKSA cache entry (IEEE 802.1X)");
2009 }
2010}