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