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