]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/ieee802_1x.c
WPS: Wait for EAPOL-Start unless WPS 2.0 station as workaround
[thirdparty/hostap.git] / src / ap / ieee802_1x.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / IEEE 802.1X-2004 Authenticator
55bce124 3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
6fc6879b
JM
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
6226e38d 15#include "utils/includes.h"
6fc6879b 16
6226e38d
JM
17#include "utils/common.h"
18#include "utils/eloop.h"
03da66bd
JM
19#include "crypto/md5.h"
20#include "crypto/crypto.h"
3642c431 21#include "crypto/random.h"
03da66bd
JM
22#include "common/ieee802_11_defs.h"
23#include "common/wpa_ctrl.h"
6fc6879b
JM
24#include "radius/radius.h"
25#include "radius/radius_client.h"
6226e38d 26#include "eap_server/eap.h"
4e22adb4 27#include "eap_common/eap_wsc_common.h"
281c950b 28#include "eapol_auth/eapol_auth_sm.h"
e0e14a7b 29#include "eapol_auth/eapol_auth_sm_i.h"
03da66bd 30#include "hostapd.h"
03da66bd 31#include "accounting.h"
6fc6879b 32#include "sta_info.h"
6226e38d
JM
33#include "wpa_auth.h"
34#include "preauth_auth.h"
35#include "pmksa_cache_auth.h"
36#include "ap_config.h"
3acdf771 37#include "ap_drv_ops.h"
6226e38d 38#include "ieee802_1x.h"
6fc6879b
JM
39
40
41static void ieee802_1x_finished(struct hostapd_data *hapd,
42 struct sta_info *sta, int success);
43
44
45static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
46 u8 type, const u8 *data, size_t datalen)
47{
48 u8 *buf;
49 struct ieee802_1x_hdr *xhdr;
50 size_t len;
51 int encrypt = 0;
52
53 len = sizeof(*xhdr) + datalen;
54 buf = os_zalloc(len);
55 if (buf == NULL) {
56 wpa_printf(MSG_ERROR, "malloc() failed for "
57 "ieee802_1x_send(len=%lu)",
58 (unsigned long) len);
59 return;
60 }
61
62 xhdr = (struct ieee802_1x_hdr *) buf;
63 xhdr->version = hapd->conf->eapol_version;
64 xhdr->type = type;
65 xhdr->length = host_to_be16(datalen);
66
67 if (datalen > 0 && data != NULL)
68 os_memcpy(xhdr + 1, data, datalen);
69
70 if (wpa_auth_pairwise_set(sta->wpa_sm))
71 encrypt = 1;
72 if (sta->flags & WLAN_STA_PREAUTH) {
73 rsn_preauth_send(hapd, sta, buf, len);
74 } else {
3acdf771 75 hostapd_drv_hapd_send_eapol(hapd, sta->addr, buf, len,
4378fc14 76 encrypt, sta->flags);
6fc6879b
JM
77 }
78
79 os_free(buf);
80}
81
82
83void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
84 struct sta_info *sta, int authorized)
85{
86 int res;
87
88 if (sta->flags & WLAN_STA_PREAUTH)
89 return;
90
91 if (authorized) {
6905dcb1 92 if (!ap_sta_is_authorized(sta))
20bd9547
JM
93 wpa_msg(hapd->msg_ctx, MSG_INFO,
94 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
6905dcb1 95 ap_sta_set_authorized(hapd, sta, 1);
0e8a96a9 96 res = hostapd_set_authorized(hapd, sta, 1);
6fc6879b
JM
97 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
98 HOSTAPD_LEVEL_DEBUG, "authorizing port");
99 } else {
6905dcb1 100 if (ap_sta_is_authorized(sta) && (sta->flags & WLAN_STA_ASSOC))
20bd9547
JM
101 wpa_msg(hapd->msg_ctx, MSG_INFO,
102 AP_STA_DISCONNECTED MACSTR,
103 MAC2STR(sta->addr));
6905dcb1 104 ap_sta_set_authorized(hapd, sta, 0);
0e8a96a9 105 res = hostapd_set_authorized(hapd, sta, 0);
6fc6879b
JM
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
3642c431 145 if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
6fc6879b
JM
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 ||
3642c431 220 random_get_bytes(key->key[key->idx], key->default_len)) {
6fc6879b
JM
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
0382097e
JM
234 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
235 broadcast_ether_addr, key->idx, 1,
3acdf771
JM
236 NULL, 0, key->key[key->idx],
237 key->len[key->idx]))
6fc6879b
JM
238 printf("Could not set dynamic VLAN WEP encryption key.\n");
239
0e8a96a9 240 hostapd_set_drv_ieee8021x(hapd, ifname, 1);
6fc6879b
JM
241
242 return key;
243}
244
245
246static struct hostapd_wep_keys *
247ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
248 size_t vlan_id)
249{
250 const char *ifname;
251
252 if (vlan_id == 0)
253 return &ssid->wep;
254
255 if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
256 ssid->dyn_vlan_keys[vlan_id])
257 return ssid->dyn_vlan_keys[vlan_id];
258
259 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
260 "state machine for VLAN ID %lu",
261 (unsigned long) vlan_id);
262
263 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
264 if (ifname == NULL) {
265 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
266 "cannot create group key state machine",
267 (unsigned long) vlan_id);
268 return NULL;
269 }
270
271 if (ssid->dyn_vlan_keys == NULL) {
272 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
273 ssid->dyn_vlan_keys = os_zalloc(size);
274 if (ssid->dyn_vlan_keys == NULL)
275 return NULL;
276 ssid->max_dyn_vlan_keys = vlan_id;
277 }
278
279 if (ssid->max_dyn_vlan_keys < vlan_id) {
280 struct hostapd_wep_keys **na;
281 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
282 na = os_realloc(ssid->dyn_vlan_keys, size);
283 if (na == NULL)
284 return NULL;
285 ssid->dyn_vlan_keys = na;
286 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
287 (vlan_id - ssid->max_dyn_vlan_keys) *
288 sizeof(ssid->dyn_vlan_keys[0]));
289 ssid->max_dyn_vlan_keys = vlan_id;
290 }
291
292 ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
293
294 return ssid->dyn_vlan_keys[vlan_id];
295}
30b32314 296#endif /* CONFIG_NO_VLAN */
6fc6879b
JM
297
298
299void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
300{
f55802e8 301 struct eapol_authenticator *eapol = hapd->eapol_auth;
6fc6879b 302 struct eapol_state_machine *sm = sta->eapol_sm;
30b32314
JM
303#ifndef CONFIG_NO_VLAN
304 struct hostapd_wep_keys *key = NULL;
6fc6879b 305 int vlan_id;
30b32314 306#endif /* CONFIG_NO_VLAN */
6fc6879b
JM
307
308 if (sm == NULL || !sm->eap_if->eapKeyData)
309 return;
310
311 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
312 MAC2STR(sta->addr));
313
30b32314 314#ifndef CONFIG_NO_VLAN
6fc6879b
JM
315 vlan_id = sta->vlan_id;
316 if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
317 vlan_id = 0;
318
319 if (vlan_id) {
320 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
321 if (key && key->key[key->idx])
322 ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
323 key->key[key->idx],
324 key->len[key->idx]);
30b32314
JM
325 } else
326#endif /* CONFIG_NO_VLAN */
f55802e8
JM
327 if (eapol->default_wep_key) {
328 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
329 eapol->default_wep_key,
6fc6879b
JM
330 hapd->conf->default_wep_key_len);
331 }
332
333 if (hapd->conf->individual_wep_key_len > 0) {
334 u8 *ikey;
335 ikey = os_malloc(hapd->conf->individual_wep_key_len);
336 if (ikey == NULL ||
3642c431
JM
337 random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
338 {
6fc6879b
JM
339 wpa_printf(MSG_ERROR, "Could not generate random "
340 "individual WEP key.");
341 os_free(ikey);
342 return;
343 }
344
345 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
346 ikey, hapd->conf->individual_wep_key_len);
347
348 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
349 hapd->conf->individual_wep_key_len);
350
351 /* TODO: set encryption in TX callback, i.e., only after STA
352 * has ACKed EAPOL-Key frame */
3acdf771
JM
353 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
354 sta->addr, 0, 1, NULL, 0, ikey,
355 hapd->conf->individual_wep_key_len)) {
6fc6879b
JM
356 wpa_printf(MSG_ERROR, "Could not set individual WEP "
357 "encryption.");
358 }
359
360 os_free(ikey);
361 }
362}
363
364
365const char *radius_mode_txt(struct hostapd_data *hapd)
366{
45cefa0b 367 switch (hapd->iface->conf->hw_mode) {
6fc6879b
JM
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 */
1489e11a
JM
542 if (sm->last_recv_radius &&
543 radius_msg_get_hdr(sm->last_recv_radius)->code ==
6fc6879b
JM
544 RADIUS_CODE_ACCESS_CHALLENGE) {
545 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
546 RADIUS_ATTR_STATE);
547 if (res < 0) {
548 printf("Could not copy State attribute from previous "
549 "Access-Challenge\n");
550 goto fail;
551 }
552 if (res > 0) {
553 wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
554 }
555 }
556
dbb6ed7e
MH
557 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
558 goto fail;
559
6fc6879b
JM
560 return;
561
562 fail:
563 radius_msg_free(msg);
6fc6879b 564}
f88bd288 565#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
566
567
6fc6879b
JM
568static void handle_eap_response(struct hostapd_data *hapd,
569 struct sta_info *sta, struct eap_hdr *eap,
570 size_t len)
571{
572 u8 type, *data;
573 struct eapol_state_machine *sm = sta->eapol_sm;
574 if (sm == NULL)
575 return;
576
577 data = (u8 *) (eap + 1);
578
579 if (len < sizeof(*eap) + 1) {
580 printf("handle_eap_response: too short response data\n");
581 return;
582 }
583
584 sm->eap_type_supp = type = data[0];
6fc6879b
JM
585
586 hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
587 HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
588 "id=%d len=%d) from STA: EAP Response-%s (%d)",
589 eap->code, eap->identifier, be_to_host16(eap->length),
2773ca09 590 eap_server_get_name(0, type), type);
6fc6879b
JM
591
592 sm->dot1xAuthEapolRespFramesRx++;
593
594 wpabuf_free(sm->eap_if->eapRespData);
595 sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
596 sm->eapolEap = TRUE;
597}
598
599
600/* Process incoming EAP packet from Supplicant */
601static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
602 u8 *buf, size_t len)
603{
604 struct eap_hdr *eap;
605 u16 eap_len;
606
607 if (len < sizeof(*eap)) {
608 printf(" too short EAP packet\n");
609 return;
610 }
611
612 eap = (struct eap_hdr *) buf;
613
614 eap_len = be_to_host16(eap->length);
615 wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
616 eap->code, eap->identifier, eap_len);
617 if (eap_len < sizeof(*eap)) {
618 wpa_printf(MSG_DEBUG, " Invalid EAP length");
619 return;
620 } else if (eap_len > len) {
621 wpa_printf(MSG_DEBUG, " Too short frame to contain this EAP "
622 "packet");
623 return;
624 } else if (eap_len < len) {
625 wpa_printf(MSG_DEBUG, " Ignoring %lu extra bytes after EAP "
626 "packet", (unsigned long) len - eap_len);
627 }
628
629 switch (eap->code) {
630 case EAP_CODE_REQUEST:
631 wpa_printf(MSG_DEBUG, " (request)");
632 return;
633 case EAP_CODE_RESPONSE:
634 wpa_printf(MSG_DEBUG, " (response)");
635 handle_eap_response(hapd, sta, eap, eap_len);
636 break;
637 case EAP_CODE_SUCCESS:
638 wpa_printf(MSG_DEBUG, " (success)");
639 return;
640 case EAP_CODE_FAILURE:
641 wpa_printf(MSG_DEBUG, " (failure)");
642 return;
643 default:
644 wpa_printf(MSG_DEBUG, " (unknown code)");
645 return;
646 }
647}
648
649
c02d52b4
JM
650static struct eapol_state_machine *
651ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
652{
653 int flags = 0;
654 if (sta->flags & WLAN_STA_PREAUTH)
655 flags |= EAPOL_SM_PREAUTH;
656 if (sta->wpa_sm) {
439d4bf9 657 flags |= EAPOL_SM_USES_WPA;
c02d52b4
JM
658 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
659 flags |= EAPOL_SM_FROM_PMKSA_CACHE;
660 }
d79b7792 661 return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
f684e608 662 sta->wps_ie, sta->p2p_ie, sta);
c02d52b4
JM
663}
664
665
1c6e69cc
JM
666/**
667 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
668 * @hapd: hostapd BSS data
669 * @sa: Source address (sender of the EAPOL frame)
670 * @buf: EAPOL frame
671 * @len: Length of buf in octets
672 *
673 * This function is called for each incoming EAPOL frame from the interface
674 */
6fc6879b
JM
675void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
676 size_t len)
677{
678 struct sta_info *sta;
679 struct ieee802_1x_hdr *hdr;
680 struct ieee802_1x_eapol_key *key;
681 u16 datalen;
682 struct rsn_pmksa_cache_entry *pmksa;
df13a1cd 683 int key_mgmt;
6fc6879b 684
ad08c363
JM
685 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
686 !hapd->conf->wps_state)
6fc6879b
JM
687 return;
688
689 wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
690 (unsigned long) len, MAC2STR(sa));
691 sta = ap_get_sta(hapd, sa);
c9265931 692 if (!sta || !(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH))) {
940a0ce9 693 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
c9265931 694 "associated/Pre-authenticating STA");
6fc6879b
JM
695 return;
696 }
697
698 if (len < sizeof(*hdr)) {
699 printf(" too short IEEE 802.1X packet\n");
700 return;
701 }
702
703 hdr = (struct ieee802_1x_hdr *) buf;
704 datalen = be_to_host16(hdr->length);
705 wpa_printf(MSG_DEBUG, " IEEE 802.1X: version=%d type=%d length=%d",
706 hdr->version, hdr->type, datalen);
707
708 if (len - sizeof(*hdr) < datalen) {
709 printf(" frame too short for this IEEE 802.1X packet\n");
710 if (sta->eapol_sm)
711 sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
712 return;
713 }
714 if (len - sizeof(*hdr) > datalen) {
715 wpa_printf(MSG_DEBUG, " ignoring %lu extra octets after "
716 "IEEE 802.1X packet",
717 (unsigned long) len - sizeof(*hdr) - datalen);
718 }
719
720 if (sta->eapol_sm) {
721 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
722 sta->eapol_sm->dot1xAuthEapolFramesRx++;
723 }
724
725 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
726 if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
727 hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
728 (key->type == EAPOL_KEY_TYPE_WPA ||
729 key->type == EAPOL_KEY_TYPE_RSN)) {
730 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
731 sizeof(*hdr) + datalen);
732 return;
733 }
734
df13a1cd
JM
735 if (!hapd->conf->ieee802_1x &&
736 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
737 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
738 "802.1X not enabled and WPS not used");
6fc6879b 739 return;
df13a1cd
JM
740 }
741
742 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
743 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
744 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
745 "STA is using PSK");
746 return;
747 }
6fc6879b
JM
748
749 if (!sta->eapol_sm) {
c02d52b4 750 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
6fc6879b
JM
751 if (!sta->eapol_sm)
752 return;
ad08c363
JM
753
754#ifdef CONFIG_WPS
17f6b900
JM
755 if (!hapd->conf->ieee802_1x) {
756 u32 wflags = sta->flags & (WLAN_STA_WPS |
757 WLAN_STA_WPS2 |
758 WLAN_STA_MAYBE_WPS);
759 if (wflags == WLAN_STA_MAYBE_WPS ||
760 wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
761 /*
762 * Delay EAPOL frame transmission until a
763 * possible WPS STA initiates the handshake
764 * with EAPOL-Start. Only allow the wait to be
765 * skipped if the STA is known to support WPS
766 * 2.0.
767 */
768 wpa_printf(MSG_DEBUG, "WPS: Do not start "
769 "EAPOL until EAPOL-Start is "
770 "received");
771 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
772 }
ad08c363
JM
773 }
774#endif /* CONFIG_WPS */
a60e7213
JM
775
776 sta->eapol_sm->eap_if->portEnabled = TRUE;
6fc6879b
JM
777 }
778
779 /* since we support version 1, we can ignore version field and proceed
780 * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
781 /* TODO: actually, we are not version 1 anymore.. However, Version 2
782 * does not change frame contents, so should be ok to process frames
783 * more or less identically. Some changes might be needed for
784 * verification of fields. */
785
786 switch (hdr->type) {
787 case IEEE802_1X_TYPE_EAP_PACKET:
788 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
789 break;
790
791 case IEEE802_1X_TYPE_EAPOL_START:
792 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
793 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
794 "from STA");
795 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
796 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
797 if (pmksa) {
798 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
799 HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
800 "available - ignore it since "
801 "STA sent EAPOL-Start");
802 wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
803 }
804 sta->eapol_sm->eapolStart = TRUE;
805 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
6fc58a89 806 eap_server_clear_identity(sta->eapol_sm->eap);
6fc6879b
JM
807 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
808 break;
809
810 case IEEE802_1X_TYPE_EAPOL_LOGOFF:
811 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
812 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
813 "from STA");
814 sta->acct_terminate_cause =
815 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
b1fa8bf1 816 accounting_sta_stop(hapd, sta);
6fc6879b
JM
817 sta->eapol_sm->eapolLogoff = TRUE;
818 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
6fc58a89 819 eap_server_clear_identity(sta->eapol_sm->eap);
6fc6879b
JM
820 break;
821
822 case IEEE802_1X_TYPE_EAPOL_KEY:
823 wpa_printf(MSG_DEBUG, " EAPOL-Key");
6905dcb1 824 if (!ap_sta_is_authorized(sta)) {
6fc6879b
JM
825 wpa_printf(MSG_DEBUG, " Dropped key data from "
826 "unauthorized Supplicant");
827 break;
828 }
829 break;
830
831 case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
832 wpa_printf(MSG_DEBUG, " EAPOL-Encapsulated-ASF-Alert");
833 /* TODO: implement support for this; show data */
834 break;
835
836 default:
837 wpa_printf(MSG_DEBUG, " unknown IEEE 802.1X packet type");
838 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
839 break;
840 }
841
842 eapol_auth_step(sta->eapol_sm);
843}
844
845
1c6e69cc
JM
846/**
847 * ieee802_1x_new_station - Start IEEE 802.1X authentication
848 * @hapd: hostapd BSS data
849 * @sta: The station
850 *
851 * This function is called to start IEEE 802.1X authentication when a new
852 * station completes IEEE 802.11 association.
853 */
6fc6879b
JM
854void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
855{
856 struct rsn_pmksa_cache_entry *pmksa;
857 int reassoc = 1;
858 int force_1x = 0;
df13a1cd 859 int key_mgmt;
6fc6879b 860
ad08c363 861#ifdef CONFIG_WPS
a60e7213 862 if (hapd->conf->wps_state && hapd->conf->wpa &&
ad08c363
JM
863 (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
864 /*
865 * Need to enable IEEE 802.1X/EAPOL state machines for possible
866 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
867 * authentication in this BSS.
868 */
869 force_1x = 1;
870 }
871#endif /* CONFIG_WPS */
872
df13a1cd
JM
873 if (!force_1x && !hapd->conf->ieee802_1x) {
874 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
875 "802.1X not enabled or forced for WPS");
6fc6879b 876 return;
df13a1cd
JM
877 }
878
879 key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
880 if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
881 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
882 return;
883 }
6fc6879b
JM
884
885 if (sta->eapol_sm == NULL) {
886 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
887 HOSTAPD_LEVEL_DEBUG, "start authentication");
c02d52b4 888 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
6fc6879b
JM
889 if (sta->eapol_sm == NULL) {
890 hostapd_logger(hapd, sta->addr,
891 HOSTAPD_MODULE_IEEE8021X,
892 HOSTAPD_LEVEL_INFO,
893 "failed to allocate state machine");
894 return;
895 }
896 reassoc = 0;
897 }
898
ad08c363 899#ifdef CONFIG_WPS
a8d05fca 900 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
17f6b900 901 if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS2)) {
ad08c363 902 /*
17f6b900
JM
903 * Delay EAPOL frame transmission until a possible WPS STA
904 * initiates the handshake with EAPOL-Start. Only allow the
905 * wait to be skipped if the STA is known to support WPS 2.0.
ad08c363 906 */
17f6b900
JM
907 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
908 "EAPOL-Start is received");
ad08c363
JM
909 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
910 }
911#endif /* CONFIG_WPS */
912
6fc6879b
JM
913 sta->eapol_sm->eap_if->portEnabled = TRUE;
914
55bce124
JM
915#ifdef CONFIG_IEEE80211R
916 if (sta->auth_alg == WLAN_AUTH_FT) {
917 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
918 HOSTAPD_LEVEL_DEBUG,
919 "PMK from FT - skip IEEE 802.1X/EAP");
920 /* Setup EAPOL state machines to already authenticated state
921 * because of existing FT information from R0KH. */
922 sta->eapol_sm->keyRun = TRUE;
923 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
924 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
925 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
926 sta->eapol_sm->authSuccess = TRUE;
927 if (sta->eapol_sm->eap)
928 eap_sm_notify_cached(sta->eapol_sm->eap);
929 /* TODO: get vlan_id from R0KH using RRB message */
930 return;
931 }
932#endif /* CONFIG_IEEE80211R */
933
6fc6879b
JM
934 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
935 if (pmksa) {
936 int old_vlanid;
937
938 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
939 HOSTAPD_LEVEL_DEBUG,
940 "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
941 /* Setup EAPOL state machines to already authenticated state
942 * because of existing PMKSA information in the cache. */
943 sta->eapol_sm->keyRun = TRUE;
944 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
945 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
946 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
947 sta->eapol_sm->authSuccess = TRUE;
948 if (sta->eapol_sm->eap)
949 eap_sm_notify_cached(sta->eapol_sm->eap);
950 old_vlanid = sta->vlan_id;
951 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
952 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
953 sta->vlan_id = 0;
954 ap_sta_bind_vlan(hapd, sta, old_vlanid);
955 } else {
956 if (reassoc) {
957 /*
958 * Force EAPOL state machines to start
959 * re-authentication without having to wait for the
960 * Supplicant to send EAPOL-Start.
961 */
962 sta->eapol_sm->reAuthenticate = TRUE;
963 }
964 eapol_auth_step(sta->eapol_sm);
965 }
966}
967
968
6fc6879b
JM
969void ieee802_1x_free_station(struct sta_info *sta)
970{
971 struct eapol_state_machine *sm = sta->eapol_sm;
972
6fc6879b
JM
973 if (sm == NULL)
974 return;
975
976 sta->eapol_sm = NULL;
977
f88bd288 978#ifndef CONFIG_NO_RADIUS
9e7245bd 979 radius_msg_free(sm->last_recv_radius);
010dc068 980 radius_free_class(&sm->radius_class);
f88bd288 981#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
982
983 os_free(sm->identity);
6fc6879b
JM
984 eapol_auth_free(sm);
985}
986
987
f88bd288 988#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
989static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
990 struct sta_info *sta)
991{
992 u8 *eap;
993 size_t len;
994 struct eap_hdr *hdr;
995 int eap_type = -1;
996 char buf[64];
997 struct radius_msg *msg;
998 struct eapol_state_machine *sm = sta->eapol_sm;
999
1000 if (sm == NULL || sm->last_recv_radius == NULL) {
1001 if (sm)
1002 sm->eap_if->aaaEapNoReq = TRUE;
1003 return;
1004 }
1005
1006 msg = sm->last_recv_radius;
1007
1008 eap = radius_msg_get_eap(msg, &len);
1009 if (eap == NULL) {
1010 /* RFC 3579, Chap. 2.6.3:
1011 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1012 * attribute */
1013 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1014 HOSTAPD_LEVEL_WARNING, "could not extract "
1015 "EAP-Message from RADIUS message");
1016 sm->eap_if->aaaEapNoReq = TRUE;
1017 return;
1018 }
1019
1020 if (len < sizeof(*hdr)) {
1021 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1022 HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1023 "received from authentication server");
1024 os_free(eap);
1025 sm->eap_if->aaaEapNoReq = TRUE;
1026 return;
1027 }
1028
1029 if (len > sizeof(*hdr))
1030 eap_type = eap[sizeof(*hdr)];
1031
1032 hdr = (struct eap_hdr *) eap;
1033 switch (hdr->code) {
1034 case EAP_CODE_REQUEST:
1035 if (eap_type >= 0)
1036 sm->eap_type_authsrv = eap_type;
1037 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
2773ca09
JM
1038 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
1039 "??",
6fc6879b
JM
1040 eap_type);
1041 break;
1042 case EAP_CODE_RESPONSE:
1043 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
2773ca09
JM
1044 eap_type >= 0 ? eap_server_get_name(0, eap_type) :
1045 "??",
6fc6879b
JM
1046 eap_type);
1047 break;
1048 case EAP_CODE_SUCCESS:
1049 os_strlcpy(buf, "EAP Success", sizeof(buf));
1050 break;
1051 case EAP_CODE_FAILURE:
1052 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1053 break;
1054 default:
1055 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1056 break;
1057 }
1058 buf[sizeof(buf) - 1] = '\0';
1059 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1060 HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1061 "id=%d len=%d) from RADIUS server: %s",
1062 hdr->code, hdr->identifier, be_to_host16(hdr->length),
1063 buf);
1064 sm->eap_if->aaaEapReq = TRUE;
1065
1066 wpabuf_free(sm->eap_if->aaaEapReqData);
1067 sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1068}
1069
1070
1071static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1072 struct sta_info *sta, struct radius_msg *msg,
1073 struct radius_msg *req,
7d02e641
JM
1074 const u8 *shared_secret,
1075 size_t shared_secret_len)
6fc6879b
JM
1076{
1077 struct radius_ms_mppe_keys *keys;
1078 struct eapol_state_machine *sm = sta->eapol_sm;
1079 if (sm == NULL)
1080 return;
1081
1082 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1083 shared_secret_len);
1084
1085 if (keys && keys->send && keys->recv) {
1086 size_t len = keys->send_len + keys->recv_len;
1087 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1088 keys->send, keys->send_len);
1089 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1090 keys->recv, keys->recv_len);
1091
1092 os_free(sm->eap_if->aaaEapKeyData);
1093 sm->eap_if->aaaEapKeyData = os_malloc(len);
1094 if (sm->eap_if->aaaEapKeyData) {
1095 os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1096 keys->recv_len);
1097 os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1098 keys->send, keys->send_len);
1099 sm->eap_if->aaaEapKeyDataLen = len;
1100 sm->eap_if->aaaEapKeyAvailable = TRUE;
1101 }
1102 }
1103
1104 if (keys) {
1105 os_free(keys->send);
1106 os_free(keys->recv);
1107 os_free(keys);
1108 }
1109}
1110
1111
1112static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1113 struct sta_info *sta,
1114 struct radius_msg *msg)
1115{
1116 u8 *class;
1117 size_t class_len;
1118 struct eapol_state_machine *sm = sta->eapol_sm;
1119 int count, i;
1120 struct radius_attr_data *nclass;
1121 size_t nclass_count;
1122
1123 if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1124 sm == NULL)
1125 return;
1126
010dc068 1127 radius_free_class(&sm->radius_class);
6fc6879b
JM
1128 count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1129 if (count <= 0)
1130 return;
1131
1132 nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1133 if (nclass == NULL)
1134 return;
1135
1136 nclass_count = 0;
1137
1138 class = NULL;
1139 for (i = 0; i < count; i++) {
1140 do {
1141 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1142 &class, &class_len,
1143 class) < 0) {
1144 i = count;
1145 break;
1146 }
1147 } while (class_len < 1);
1148
1149 nclass[nclass_count].data = os_malloc(class_len);
1150 if (nclass[nclass_count].data == NULL)
1151 break;
1152
1153 os_memcpy(nclass[nclass_count].data, class, class_len);
1154 nclass[nclass_count].len = class_len;
1155 nclass_count++;
1156 }
1157
1158 sm->radius_class.attr = nclass;
1159 sm->radius_class.count = nclass_count;
1160 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1161 "attributes for " MACSTR,
1162 (unsigned long) sm->radius_class.count,
1163 MAC2STR(sta->addr));
1164}
1165
1166
1167/* Update sta->identity based on User-Name attribute in Access-Accept */
1168static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1169 struct sta_info *sta,
1170 struct radius_msg *msg)
1171{
1172 u8 *buf, *identity;
1173 size_t len;
1174 struct eapol_state_machine *sm = sta->eapol_sm;
1175
1176 if (sm == NULL)
1177 return;
1178
1179 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1180 NULL) < 0)
1181 return;
1182
1183 identity = os_malloc(len + 1);
1184 if (identity == NULL)
1185 return;
1186
1187 os_memcpy(identity, buf, len);
1188 identity[len] = '\0';
1189
1190 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1191 HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1192 "User-Name from Access-Accept '%s'",
1193 sm->identity ? (char *) sm->identity : "N/A",
1194 (char *) identity);
1195
1196 os_free(sm->identity);
1197 sm->identity = identity;
1198 sm->identity_len = len;
1199}
1200
1201
1202struct sta_id_search {
1203 u8 identifier;
1204 struct eapol_state_machine *sm;
1205};
1206
1207
1208static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1209 struct sta_info *sta,
1210 void *ctx)
1211{
1212 struct sta_id_search *id_search = ctx;
1213 struct eapol_state_machine *sm = sta->eapol_sm;
1214
1215 if (sm && sm->radius_identifier >= 0 &&
1216 sm->radius_identifier == id_search->identifier) {
1217 id_search->sm = sm;
1218 return 1;
1219 }
1220 return 0;
1221}
1222
1223
1224static struct eapol_state_machine *
1225ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1226{
1227 struct sta_id_search id_search;
1228 id_search.identifier = identifier;
1229 id_search.sm = NULL;
1230 ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1231 return id_search.sm;
1232}
1233
1234
1c6e69cc
JM
1235/**
1236 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1237 * @msg: RADIUS response message
1238 * @req: RADIUS request message
1239 * @shared_secret: RADIUS shared secret
1240 * @shared_secret_len: Length of shared_secret in octets
1241 * @data: Context data (struct hostapd_data *)
1242 * Returns: Processing status
1243 */
6fc6879b
JM
1244static RadiusRxResult
1245ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
7d02e641 1246 const u8 *shared_secret, size_t shared_secret_len,
6fc6879b
JM
1247 void *data)
1248{
1249 struct hostapd_data *hapd = data;
1250 struct sta_info *sta;
1251 u32 session_timeout = 0, termination_action, acct_interim_interval;
1252 int session_timeout_set, old_vlanid = 0;
6fc6879b
JM
1253 struct eapol_state_machine *sm;
1254 int override_eapReq = 0;
1489e11a 1255 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
6fc6879b 1256
1489e11a 1257 sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
6fc6879b
JM
1258 if (sm == NULL) {
1259 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1260 "station for this RADIUS message");
1261 return RADIUS_RX_UNKNOWN;
1262 }
1263 sta = sm->sta;
1264
1265 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1266 * present when packet contains an EAP-Message attribute */
1489e11a 1267 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
6fc6879b
JM
1268 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1269 0) < 0 &&
1270 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1271 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1272 "Message-Authenticator since it does not include "
1273 "EAP-Message");
1274 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1275 req, 1)) {
1276 printf("Incoming RADIUS packet did not have correct "
1277 "Message-Authenticator - dropped\n");
1278 return RADIUS_RX_INVALID_AUTHENTICATOR;
1279 }
1280
1489e11a
JM
1281 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1282 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1283 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
6fc6879b
JM
1284 printf("Unknown RADIUS message code\n");
1285 return RADIUS_RX_UNKNOWN;
1286 }
1287
1288 sm->radius_identifier = -1;
1289 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1290 MAC2STR(sta->addr));
1291
9e7245bd 1292 radius_msg_free(sm->last_recv_radius);
6fc6879b
JM
1293 sm->last_recv_radius = msg;
1294
1295 session_timeout_set =
1296 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1297 &session_timeout);
1298 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1299 &termination_action))
1300 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1301
5843e1c9 1302 if (hapd->conf->acct_interim_interval == 0 &&
1489e11a 1303 hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
6fc6879b
JM
1304 radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1305 &acct_interim_interval) == 0) {
1306 if (acct_interim_interval < 60) {
1307 hostapd_logger(hapd, sta->addr,
1308 HOSTAPD_MODULE_IEEE8021X,
1309 HOSTAPD_LEVEL_INFO,
1310 "ignored too small "
1311 "Acct-Interim-Interval %d",
1312 acct_interim_interval);
1313 } else
1314 sta->acct_interim_interval = acct_interim_interval;
1315 }
1316
1317
1489e11a 1318 switch (hdr->code) {
6fc6879b
JM
1319 case RADIUS_CODE_ACCESS_ACCEPT:
1320 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1321 sta->vlan_id = 0;
30b32314 1322#ifndef CONFIG_NO_VLAN
6fc6879b
JM
1323 else {
1324 old_vlanid = sta->vlan_id;
1325 sta->vlan_id = radius_msg_get_vlanid(msg);
1326 }
1327 if (sta->vlan_id > 0 &&
1328 hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1329 sta->vlan_id)) {
1330 hostapd_logger(hapd, sta->addr,
1331 HOSTAPD_MODULE_RADIUS,
1332 HOSTAPD_LEVEL_INFO,
1333 "VLAN ID %d", sta->vlan_id);
1334 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1335 sta->eapol_sm->authFail = TRUE;
1336 hostapd_logger(hapd, sta->addr,
1337 HOSTAPD_MODULE_IEEE8021X,
1338 HOSTAPD_LEVEL_INFO, "authentication "
1339 "server did not include required VLAN "
1340 "ID in Access-Accept");
1341 break;
1342 }
30b32314 1343#endif /* CONFIG_NO_VLAN */
6fc6879b 1344
4254100d
JM
1345 if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
1346 break;
6fc6879b
JM
1347
1348 /* RFC 3580, Ch. 3.17 */
1349 if (session_timeout_set && termination_action ==
1350 RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1351 sm->reAuthPeriod = session_timeout;
1352 } else if (session_timeout_set)
1353 ap_sta_session_timeout(hapd, sta, session_timeout);
1354
1355 sm->eap_if->aaaSuccess = TRUE;
1356 override_eapReq = 1;
1357 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1358 shared_secret_len);
1359 ieee802_1x_store_radius_class(hapd, sta, msg);
1360 ieee802_1x_update_sta_identity(hapd, sta, msg);
1361 if (sm->eap_if->eapKeyAvailable &&
1362 wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1363 session_timeout_set ?
1364 (int) session_timeout : -1, sm) == 0) {
1365 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1366 HOSTAPD_LEVEL_DEBUG,
1367 "Added PMKSA cache entry");
1368 }
1369 break;
1370 case RADIUS_CODE_ACCESS_REJECT:
1371 sm->eap_if->aaaFail = TRUE;
1372 override_eapReq = 1;
1373 break;
1374 case RADIUS_CODE_ACCESS_CHALLENGE:
1375 sm->eap_if->aaaEapReq = TRUE;
1376 if (session_timeout_set) {
1377 /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
8e09c6d2
JM
1378 sm->eap_if->aaaMethodTimeout = session_timeout;
1379 hostapd_logger(hapd, sm->addr,
1380 HOSTAPD_MODULE_IEEE8021X,
1381 HOSTAPD_LEVEL_DEBUG,
1382 "using EAP timeout of %d seconds (from "
1383 "RADIUS)",
1384 sm->eap_if->aaaMethodTimeout);
1385 } else {
1386 /*
1387 * Use dynamic retransmission behavior per EAP
1388 * specification.
1389 */
1390 sm->eap_if->aaaMethodTimeout = 0;
1391 }
6fc6879b
JM
1392 break;
1393 }
1394
1395 ieee802_1x_decapsulate_radius(hapd, sta);
1396 if (override_eapReq)
1397 sm->eap_if->aaaEapReq = FALSE;
1398
1399 eapol_auth_step(sm);
1400
1401 return RADIUS_RX_QUEUED;
1402}
f88bd288 1403#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1404
1405
1406void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1407{
1408 struct eapol_state_machine *sm = sta->eapol_sm;
1409 if (sm == NULL)
1410 return;
1411
1412 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1413 HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1414
f88bd288 1415#ifndef CONFIG_NO_RADIUS
9e7245bd
JM
1416 radius_msg_free(sm->last_recv_radius);
1417 sm->last_recv_radius = NULL;
f88bd288 1418#endif /* CONFIG_NO_RADIUS */
805e6dc6
JM
1419
1420 if (sm->eap_if->eapTimeout) {
1421 /*
1422 * Disconnect the STA since it did not reply to the last EAP
1423 * request and we cannot continue EAP processing (EAP-Failure
1424 * could only be sent if the EAP peer actually replied).
1425 */
1426 sm->eap_if->portEnabled = FALSE;
45cefa0b
JM
1427 ap_sta_disconnect(hapd, sta, sta->addr,
1428 WLAN_REASON_PREV_AUTH_NOT_VALID);
805e6dc6 1429 }
6fc6879b
JM
1430}
1431
1432
6fc6879b
JM
1433static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1434{
f55802e8
JM
1435 struct eapol_authenticator *eapol = hapd->eapol_auth;
1436
6fc6879b
JM
1437 if (hapd->conf->default_wep_key_len < 1)
1438 return 0;
1439
f55802e8
JM
1440 os_free(eapol->default_wep_key);
1441 eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1442 if (eapol->default_wep_key == NULL ||
3642c431
JM
1443 random_get_bytes(eapol->default_wep_key,
1444 hapd->conf->default_wep_key_len)) {
6fc6879b 1445 printf("Could not generate random WEP key.\n");
f55802e8
JM
1446 os_free(eapol->default_wep_key);
1447 eapol->default_wep_key = NULL;
6fc6879b
JM
1448 return -1;
1449 }
1450
1451 wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
f55802e8 1452 eapol->default_wep_key,
6fc6879b
JM
1453 hapd->conf->default_wep_key_len);
1454
1455 return 0;
1456}
1457
1458
1459static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1460 struct sta_info *sta, void *ctx)
1461{
1462 if (sta->eapol_sm) {
1463 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1464 eapol_auth_step(sta->eapol_sm);
1465 }
1466 return 0;
1467}
1468
1469
1470static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1471{
1472 struct hostapd_data *hapd = eloop_ctx;
f55802e8 1473 struct eapol_authenticator *eapol = hapd->eapol_auth;
6fc6879b 1474
f55802e8
JM
1475 if (eapol->default_wep_key_idx >= 3)
1476 eapol->default_wep_key_idx =
6fc6879b
JM
1477 hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1478 else
f55802e8 1479 eapol->default_wep_key_idx++;
6fc6879b
JM
1480
1481 wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
f55802e8 1482 eapol->default_wep_key_idx);
6fc6879b
JM
1483
1484 if (ieee802_1x_rekey_broadcast(hapd)) {
1485 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1486 HOSTAPD_LEVEL_WARNING, "failed to generate a "
1487 "new broadcast key");
f55802e8
JM
1488 os_free(eapol->default_wep_key);
1489 eapol->default_wep_key = NULL;
6fc6879b
JM
1490 return;
1491 }
1492
1493 /* TODO: Could setup key for RX here, but change default TX keyid only
1494 * after new broadcast key has been sent to all stations. */
0382097e
JM
1495 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
1496 broadcast_ether_addr,
3acdf771
JM
1497 eapol->default_wep_key_idx, 1, NULL, 0,
1498 eapol->default_wep_key,
1499 hapd->conf->default_wep_key_len)) {
6fc6879b
JM
1500 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1501 HOSTAPD_LEVEL_WARNING, "failed to configure a "
1502 "new broadcast key");
f55802e8
JM
1503 os_free(eapol->default_wep_key);
1504 eapol->default_wep_key = NULL;
6fc6879b
JM
1505 return;
1506 }
1507
1508 ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1509
1510 if (hapd->conf->wep_rekeying_period > 0) {
1511 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1512 ieee802_1x_rekey, hapd, NULL);
1513 }
1514}
1515
1516
1517static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1518 const u8 *data, size_t datalen)
1519{
4e22adb4
JM
1520#ifdef CONFIG_WPS
1521 struct sta_info *sta = sta_ctx;
1522
1523 if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
1524 WLAN_STA_MAYBE_WPS) {
1525 const u8 *identity;
1526 size_t identity_len;
1527 struct eapol_state_machine *sm = sta->eapol_sm;
1528
1529 identity = eap_get_identity(sm->eap, &identity_len);
1530 if (identity &&
1531 ((identity_len == WSC_ID_ENROLLEE_LEN &&
1532 os_memcmp(identity, WSC_ID_ENROLLEE,
1533 WSC_ID_ENROLLEE_LEN) == 0) ||
1534 (identity_len == WSC_ID_REGISTRAR_LEN &&
1535 os_memcmp(identity, WSC_ID_REGISTRAR,
1536 WSC_ID_REGISTRAR_LEN) == 0))) {
1537 wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
1538 "WLAN_STA_WPS");
1539 sta->flags |= WLAN_STA_WPS;
1540 }
1541 }
1542#endif /* CONFIG_WPS */
1543
6fc6879b
JM
1544 ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1545}
1546
1547
1548static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1549 const u8 *data, size_t datalen)
1550{
f88bd288 1551#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1552 struct hostapd_data *hapd = ctx;
1553 struct sta_info *sta = sta_ctx;
1554
1555 ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
f88bd288 1556#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1557}
1558
1559
1560static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1561 int preauth)
1562{
1563 struct hostapd_data *hapd = ctx;
1564 struct sta_info *sta = sta_ctx;
1565 if (preauth)
1566 rsn_preauth_finished(hapd, sta, success);
1567 else
1568 ieee802_1x_finished(hapd, sta, success);
1569}
1570
1571
1572static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1573 size_t identity_len, int phase2,
1574 struct eap_user *user)
1575{
1576 struct hostapd_data *hapd = ctx;
1577 const struct hostapd_eap_user *eap_user;
1578 int i, count;
1579
1580 eap_user = hostapd_get_eap_user(hapd->conf, identity,
1581 identity_len, phase2);
1582 if (eap_user == NULL)
1583 return -1;
1584
1585 os_memset(user, 0, sizeof(*user));
1586 user->phase2 = phase2;
1587 count = EAP_USER_MAX_METHODS;
1588 if (count > EAP_MAX_METHODS)
1589 count = EAP_MAX_METHODS;
1590 for (i = 0; i < count; i++) {
1591 user->methods[i].vendor = eap_user->methods[i].vendor;
1592 user->methods[i].method = eap_user->methods[i].method;
1593 }
1594
1595 if (eap_user->password) {
1596 user->password = os_malloc(eap_user->password_len);
1597 if (user->password == NULL)
1598 return -1;
1599 os_memcpy(user->password, eap_user->password,
1600 eap_user->password_len);
1601 user->password_len = eap_user->password_len;
1602 }
1603 user->force_version = eap_user->force_version;
1604 user->ttls_auth = eap_user->ttls_auth;
1605
1606 return 0;
1607}
1608
1609
1610static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1611{
1612 struct hostapd_data *hapd = ctx;
1613 struct sta_info *sta;
1614 sta = ap_get_sta(hapd, addr);
1615 if (sta == NULL || sta->eapol_sm == NULL)
1616 return 0;
1617 return 1;
1618}
1619
1620
1621static void ieee802_1x_logger(void *ctx, const u8 *addr,
1622 eapol_logger_level level, const char *txt)
1623{
71f04b3c 1624#ifndef CONFIG_NO_HOSTAPD_LOGGER
6fc6879b
JM
1625 struct hostapd_data *hapd = ctx;
1626 int hlevel;
1627
1628 switch (level) {
1629 case EAPOL_LOGGER_WARNING:
1630 hlevel = HOSTAPD_LEVEL_WARNING;
1631 break;
1632 case EAPOL_LOGGER_INFO:
1633 hlevel = HOSTAPD_LEVEL_INFO;
1634 break;
1635 case EAPOL_LOGGER_DEBUG:
1636 default:
1637 hlevel = HOSTAPD_LEVEL_DEBUG;
1638 break;
1639 }
1640
1641 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1642 txt);
71f04b3c 1643#endif /* CONFIG_NO_HOSTAPD_LOGGER */
6fc6879b
JM
1644}
1645
1646
1647static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1648 int authorized)
1649{
1650 struct hostapd_data *hapd = ctx;
1651 struct sta_info *sta = sta_ctx;
1652 ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1653}
1654
1655
1656static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1657{
1658 struct hostapd_data *hapd = ctx;
1659 struct sta_info *sta = sta_ctx;
1660 ieee802_1x_abort_auth(hapd, sta);
1661}
1662
1663
1664static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1665{
1666 struct hostapd_data *hapd = ctx;
1667 struct sta_info *sta = sta_ctx;
1668 ieee802_1x_tx_key(hapd, sta);
1669}
1670
1671
38294200
JM
1672static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1673 enum eapol_event type)
1674{
1675 /* struct hostapd_data *hapd = ctx; */
1676 struct sta_info *sta = sta_ctx;
1677 switch (type) {
1678 case EAPOL_AUTH_SM_CHANGE:
1679 wpa_auth_sm_notify(sta->wpa_sm);
1680 break;
1681 case EAPOL_AUTH_REAUTHENTICATE:
1682 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1683 break;
1684 }
1685}
1686
1687
6fc6879b
JM
1688int ieee802_1x_init(struct hostapd_data *hapd)
1689{
1690 int i;
1691 struct eapol_auth_config conf;
1692 struct eapol_auth_cb cb;
1693
1694 os_memset(&conf, 0, sizeof(conf));
a2befd37 1695 conf.ctx = hapd;
6fc6879b
JM
1696 conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1697 conf.wpa = hapd->conf->wpa;
1698 conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1699 conf.eap_server = hapd->conf->eap_server;
1700 conf.ssl_ctx = hapd->ssl_ctx;
bb437f28 1701 conf.msg_ctx = hapd->msg_ctx;
6fc6879b
JM
1702 conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1703 conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1704 conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1705 conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1706 conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
2d867244
JM
1707 conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1708 conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
378eae5e 1709 conf.eap_fast_prov = hapd->conf->eap_fast_prov;
a11c90a6
JM
1710 conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1711 conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
6fc6879b 1712 conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
c3e258ae 1713 conf.tnc = hapd->conf->tnc;
ad08c363 1714 conf.wps = hapd->wps;
7f6ec672 1715 conf.fragment_size = hapd->conf->fragment_size;
df684d82 1716 conf.pwd_group = hapd->conf->pwd_group;
fa516558 1717 conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
6fc6879b
JM
1718
1719 os_memset(&cb, 0, sizeof(cb));
1720 cb.eapol_send = ieee802_1x_eapol_send;
1721 cb.aaa_send = ieee802_1x_aaa_send;
1722 cb.finished = _ieee802_1x_finished;
1723 cb.get_eap_user = ieee802_1x_get_eap_user;
1724 cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1725 cb.logger = ieee802_1x_logger;
1726 cb.set_port_authorized = ieee802_1x_set_port_authorized;
1727 cb.abort_auth = _ieee802_1x_abort_auth;
1728 cb.tx_key = _ieee802_1x_tx_key;
38294200 1729 cb.eapol_event = ieee802_1x_eapol_event;
6fc6879b
JM
1730
1731 hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1732 if (hapd->eapol_auth == NULL)
1733 return -1;
1734
1735 if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
0e8a96a9 1736 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
6fc6879b
JM
1737 return -1;
1738
f88bd288 1739#ifndef CONFIG_NO_RADIUS
6fc6879b
JM
1740 if (radius_client_register(hapd->radius, RADIUS_AUTH,
1741 ieee802_1x_receive_auth, hapd))
1742 return -1;
f88bd288 1743#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
1744
1745 if (hapd->conf->default_wep_key_len) {
6fc6879b 1746 for (i = 0; i < 4; i++)
3acdf771
JM
1747 hostapd_drv_set_key(hapd->conf->iface, hapd,
1748 WPA_ALG_NONE, NULL, i, 0, NULL, 0,
1749 NULL, 0);
6fc6879b
JM
1750
1751 ieee802_1x_rekey(hapd, NULL);
1752
f55802e8 1753 if (hapd->eapol_auth->default_wep_key == NULL)
6fc6879b
JM
1754 return -1;
1755 }
1756
1757 return 0;
1758}
1759
1760
1761void ieee802_1x_deinit(struct hostapd_data *hapd)
1762{
1763 eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1764
1765 if (hapd->driver != NULL &&
1766 (hapd->conf->ieee802_1x || hapd->conf->wpa))
0e8a96a9 1767 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
6fc6879b
JM
1768
1769 eapol_auth_deinit(hapd->eapol_auth);
1770 hapd->eapol_auth = NULL;
1771}
1772
1773
6fc6879b 1774int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
8607f4c3 1775 const u8 *buf, size_t len, int ack)
6fc6879b
JM
1776{
1777 struct ieee80211_hdr *hdr;
1778 struct ieee802_1x_hdr *xhdr;
1779 struct ieee802_1x_eapol_key *key;
1780 u8 *pos;
1781 const unsigned char rfc1042_hdr[ETH_ALEN] =
1782 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1783
1784 if (sta == NULL)
1785 return -1;
1786 if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1787 return 0;
1788
1789 hdr = (struct ieee80211_hdr *) buf;
1790 pos = (u8 *) (hdr + 1);
1791 if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1792 return 0;
1793 pos += sizeof(rfc1042_hdr);
1794 if (WPA_GET_BE16(pos) != ETH_P_PAE)
1795 return 0;
1796 pos += 2;
1797
1798 xhdr = (struct ieee802_1x_hdr *) pos;
1799 pos += sizeof(*xhdr);
1800
1801 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1802 "type=%d length=%d - ack=%d",
1803 MAC2STR(sta->addr), xhdr->version, xhdr->type,
1804 be_to_host16(xhdr->length), ack);
1805
e4bf4db9
JM
1806 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
1807 pos + sizeof(struct wpa_eapol_key) <= buf + len) {
1808 const struct wpa_eapol_key *wpa;
1809 wpa = (const struct wpa_eapol_key *) pos;
1810 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
1811 wpa->type == EAPOL_KEY_TYPE_WPA)
1812 wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
1813 sta->wpa_sm, ack);
1814 }
1815
6fc6879b
JM
1816 /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1817 * or Authenticator state machines, but EAPOL-Key packets are not
1818 * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1819 * packets couple of times because otherwise STA keys become
1820 * unsynchronized with AP. */
1821 if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1822 pos + sizeof(*key) <= buf + len) {
1823 key = (struct ieee802_1x_eapol_key *) pos;
1824 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1825 HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1826 "frame (%scast index=%d)",
1827 key->key_index & BIT(7) ? "uni" : "broad",
1828 key->key_index & ~BIT(7));
1829 /* TODO: re-send EAPOL-Key couple of times (with short delay
1830 * between them?). If all attempt fail, report error and
1831 * deauthenticate STA so that it will get new keys when
1832 * authenticating again (e.g., after returning in range).
1833 * Separate limit/transmit state needed both for unicast and
1834 * broadcast keys(?) */
1835 }
1836 /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1837 * to here and change the key only if the EAPOL-Key packet was Acked.
1838 */
1839
1840 return 1;
1841}
1842
1843
1844u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1845{
1846 if (sm == NULL || sm->identity == NULL)
1847 return NULL;
1848
1849 *len = sm->identity_len;
1850 return sm->identity;
1851}
1852
1853
1854u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1855 int idx)
1856{
1857 if (sm == NULL || sm->radius_class.attr == NULL ||
1858 idx >= (int) sm->radius_class.count)
1859 return NULL;
1860
1861 *len = sm->radius_class.attr[idx].len;
1862 return sm->radius_class.attr[idx].data;
1863}
1864
1865
1866const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1867{
55e632df 1868 *len = 0;
6fc6879b
JM
1869 if (sm == NULL)
1870 return NULL;
1871
1872 *len = sm->eap_if->eapKeyDataLen;
1873 return sm->eap_if->eapKeyData;
1874}
1875
1876
1877void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1878 int enabled)
1879{
1880 if (sm == NULL)
1881 return;
1882 sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1883 eapol_auth_step(sm);
1884}
1885
1886
1887void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1888 int valid)
1889{
1890 if (sm == NULL)
1891 return;
1892 sm->portValid = valid ? TRUE : FALSE;
1893 eapol_auth_step(sm);
1894}
1895
1896
1897void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1898{
1899 if (sm == NULL)
1900 return;
1901 if (pre_auth)
1902 sm->flags |= EAPOL_SM_PREAUTH;
1903 else
1904 sm->flags &= ~EAPOL_SM_PREAUTH;
1905}
1906
1907
1908static const char * bool_txt(Boolean bool)
1909{
1910 return bool ? "TRUE" : "FALSE";
1911}
1912
1913
1914int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1915{
1916 /* TODO */
1917 return 0;
1918}
1919
1920
1921int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1922 char *buf, size_t buflen)
1923{
1924 int len = 0, ret;
1925 struct eapol_state_machine *sm = sta->eapol_sm;
1926
1927 if (sm == NULL)
1928 return 0;
1929
1930 ret = os_snprintf(buf + len, buflen - len,
1931 "dot1xPaePortNumber=%d\n"
1932 "dot1xPaePortProtocolVersion=%d\n"
1933 "dot1xPaePortCapabilities=1\n"
1934 "dot1xPaePortInitialize=%d\n"
1935 "dot1xPaePortReauthenticate=FALSE\n",
1936 sta->aid,
1937 EAPOL_VERSION,
1938 sm->initialize);
1939 if (ret < 0 || (size_t) ret >= buflen - len)
1940 return len;
1941 len += ret;
1942
1943 /* dot1xAuthConfigTable */
1944 ret = os_snprintf(buf + len, buflen - len,
1945 "dot1xAuthPaeState=%d\n"
1946 "dot1xAuthBackendAuthState=%d\n"
1947 "dot1xAuthAdminControlledDirections=%d\n"
1948 "dot1xAuthOperControlledDirections=%d\n"
1949 "dot1xAuthAuthControlledPortStatus=%d\n"
1950 "dot1xAuthAuthControlledPortControl=%d\n"
1951 "dot1xAuthQuietPeriod=%u\n"
1952 "dot1xAuthServerTimeout=%u\n"
1953 "dot1xAuthReAuthPeriod=%u\n"
1954 "dot1xAuthReAuthEnabled=%s\n"
1955 "dot1xAuthKeyTxEnabled=%s\n",
1956 sm->auth_pae_state + 1,
1957 sm->be_auth_state + 1,
1958 sm->adminControlledDirections,
1959 sm->operControlledDirections,
1960 sm->authPortStatus,
1961 sm->portControl,
1962 sm->quietPeriod,
1963 sm->serverTimeout,
1964 sm->reAuthPeriod,
1965 bool_txt(sm->reAuthEnabled),
1966 bool_txt(sm->keyTxEnabled));
1967 if (ret < 0 || (size_t) ret >= buflen - len)
1968 return len;
1969 len += ret;
1970
1971 /* dot1xAuthStatsTable */
1972 ret = os_snprintf(buf + len, buflen - len,
1973 "dot1xAuthEapolFramesRx=%u\n"
1974 "dot1xAuthEapolFramesTx=%u\n"
1975 "dot1xAuthEapolStartFramesRx=%u\n"
1976 "dot1xAuthEapolLogoffFramesRx=%u\n"
1977 "dot1xAuthEapolRespIdFramesRx=%u\n"
1978 "dot1xAuthEapolRespFramesRx=%u\n"
1979 "dot1xAuthEapolReqIdFramesTx=%u\n"
1980 "dot1xAuthEapolReqFramesTx=%u\n"
1981 "dot1xAuthInvalidEapolFramesRx=%u\n"
1982 "dot1xAuthEapLengthErrorFramesRx=%u\n"
1983 "dot1xAuthLastEapolFrameVersion=%u\n"
1984 "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1985 sm->dot1xAuthEapolFramesRx,
1986 sm->dot1xAuthEapolFramesTx,
1987 sm->dot1xAuthEapolStartFramesRx,
1988 sm->dot1xAuthEapolLogoffFramesRx,
1989 sm->dot1xAuthEapolRespIdFramesRx,
1990 sm->dot1xAuthEapolRespFramesRx,
1991 sm->dot1xAuthEapolReqIdFramesTx,
1992 sm->dot1xAuthEapolReqFramesTx,
1993 sm->dot1xAuthInvalidEapolFramesRx,
1994 sm->dot1xAuthEapLengthErrorFramesRx,
1995 sm->dot1xAuthLastEapolFrameVersion,
1996 MAC2STR(sm->addr));
1997 if (ret < 0 || (size_t) ret >= buflen - len)
1998 return len;
1999 len += ret;
2000
2001 /* dot1xAuthDiagTable */
2002 ret = os_snprintf(buf + len, buflen - len,
2003 "dot1xAuthEntersConnecting=%u\n"
2004 "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2005 "dot1xAuthEntersAuthenticating=%u\n"
2006 "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2007 "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2008 "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2009 "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2010 "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2011 "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2012 "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2013 "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2014 "dot1xAuthBackendResponses=%u\n"
2015 "dot1xAuthBackendAccessChallenges=%u\n"
2016 "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2017 "dot1xAuthBackendAuthSuccesses=%u\n"
2018 "dot1xAuthBackendAuthFails=%u\n",
2019 sm->authEntersConnecting,
2020 sm->authEapLogoffsWhileConnecting,
2021 sm->authEntersAuthenticating,
2022 sm->authAuthSuccessesWhileAuthenticating,
2023 sm->authAuthTimeoutsWhileAuthenticating,
2024 sm->authAuthFailWhileAuthenticating,
2025 sm->authAuthEapStartsWhileAuthenticating,
2026 sm->authAuthEapLogoffWhileAuthenticating,
2027 sm->authAuthReauthsWhileAuthenticated,
2028 sm->authAuthEapStartsWhileAuthenticated,
2029 sm->authAuthEapLogoffWhileAuthenticated,
2030 sm->backendResponses,
2031 sm->backendAccessChallenges,
2032 sm->backendOtherRequestsToSupplicant,
2033 sm->backendAuthSuccesses,
2034 sm->backendAuthFails);
2035 if (ret < 0 || (size_t) ret >= buflen - len)
2036 return len;
2037 len += ret;
2038
2039 /* dot1xAuthSessionStatsTable */
2040 ret = os_snprintf(buf + len, buflen - len,
2041 /* TODO: dot1xAuthSessionOctetsRx */
2042 /* TODO: dot1xAuthSessionOctetsTx */
2043 /* TODO: dot1xAuthSessionFramesRx */
2044 /* TODO: dot1xAuthSessionFramesTx */
2045 "dot1xAuthSessionId=%08X-%08X\n"
2046 "dot1xAuthSessionAuthenticMethod=%d\n"
2047 "dot1xAuthSessionTime=%u\n"
2048 "dot1xAuthSessionTerminateCause=999\n"
2049 "dot1xAuthSessionUserName=%s\n",
2050 sta->acct_session_id_hi, sta->acct_session_id_lo,
56586197
JM
2051 (wpa_key_mgmt_wpa_ieee8021x(
2052 wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2053 1 : 2,
6fc6879b
JM
2054 (unsigned int) (time(NULL) -
2055 sta->acct_session_start),
2056 sm->identity);
2057 if (ret < 0 || (size_t) ret >= buflen - len)
2058 return len;
2059 len += ret;
2060
2061 return len;
2062}
2063
2064
2065static void ieee802_1x_finished(struct hostapd_data *hapd,
2066 struct sta_info *sta, int success)
2067{
2068 const u8 *key;
2069 size_t len;
2070 /* TODO: get PMKLifetime from WPA parameters */
2071 static const int dot11RSNAConfigPMKLifetime = 43200;
2072
2073 key = ieee802_1x_get_key(sta->eapol_sm, &len);
2074 if (success && key && len >= PMK_LEN &&
2075 wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2076 sta->eapol_sm) == 0) {
2077 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2078 HOSTAPD_LEVEL_DEBUG,
2079 "Added PMKSA cache entry (IEEE 802.1X)");
2080 }
32397063
JM
2081
2082#ifdef CONFIG_WPS
2083 if (!success && (sta->flags & WLAN_STA_WPS)) {
2084 /*
2085 * Many devices require deauthentication after WPS provisioning
2086 * and some may not be be able to do that themselves, so
2087 * disconnect the client here.
2088 */
2089 wpa_printf(MSG_DEBUG, "WPS: Force disconnection after "
2090 "EAP-Failure");
2091 /* Add a small sleep to increase likelihood of previously
2092 * requested EAP-Failure TX getting out before this should the
2093 * driver reorder operations.
2094 */
2095 os_sleep(0, 10000);
2096 ap_sta_disconnect(hapd, sta, sta->addr,
2097 WLAN_REASON_PREV_AUTH_NOT_VALID);
2098 }
2099#endif /* CONFIG_WPS */
6fc6879b 2100}