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