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