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