Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / contrib / hostapd / 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_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_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 /**
651  * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
652  * @hapd: hostapd BSS data
653  * @sa: Source address (sender of the EAPOL frame)
654  * @buf: EAPOL frame
655  * @len: Length of buf in octets
656  *
657  * This function is called for each incoming EAPOL frame from the interface
658  */
659 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
660                         size_t len)
661 {
662         struct sta_info *sta;
663         struct ieee802_1x_hdr *hdr;
664         struct ieee802_1x_eapol_key *key;
665         u16 datalen;
666         struct rsn_pmksa_cache_entry *pmksa;
667
668         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
669             !hapd->conf->wps_state)
670                 return;
671
672         wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
673                    (unsigned long) len, MAC2STR(sa));
674         sta = ap_get_sta(hapd, sa);
675         if (!sta) {
676                 printf("   no station information available\n");
677                 return;
678         }
679
680         if (len < sizeof(*hdr)) {
681                 printf("   too short IEEE 802.1X packet\n");
682                 return;
683         }
684
685         hdr = (struct ieee802_1x_hdr *) buf;
686         datalen = be_to_host16(hdr->length);
687         wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
688                    hdr->version, hdr->type, datalen);
689
690         if (len - sizeof(*hdr) < datalen) {
691                 printf("   frame too short for this IEEE 802.1X packet\n");
692                 if (sta->eapol_sm)
693                         sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
694                 return;
695         }
696         if (len - sizeof(*hdr) > datalen) {
697                 wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
698                            "IEEE 802.1X packet",
699                            (unsigned long) len - sizeof(*hdr) - datalen);
700         }
701
702         if (sta->eapol_sm) {
703                 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
704                 sta->eapol_sm->dot1xAuthEapolFramesRx++;
705         }
706
707         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
708         if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
709             hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
710             (key->type == EAPOL_KEY_TYPE_WPA ||
711              key->type == EAPOL_KEY_TYPE_RSN)) {
712                 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
713                             sizeof(*hdr) + datalen);
714                 return;
715         }
716
717         if ((!hapd->conf->ieee802_1x &&
718              !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
719             wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
720                 return;
721
722         if (!sta->eapol_sm) {
723                 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
724                                                  sta->flags & WLAN_STA_PREAUTH,
725                                                  sta);
726                 if (!sta->eapol_sm)
727                         return;
728
729 #ifdef CONFIG_WPS
730                 if (!hapd->conf->ieee802_1x &&
731                     ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
732                      WLAN_STA_MAYBE_WPS)) {
733                         /*
734                          * Delay EAPOL frame transmission until a possible WPS
735                          * STA initiates the handshake with EAPOL-Start.
736                          */
737                         sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
738                 }
739 #endif /* CONFIG_WPS */
740
741                 sta->eapol_sm->eap_if->portEnabled = TRUE;
742         }
743
744         /* since we support version 1, we can ignore version field and proceed
745          * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
746         /* TODO: actually, we are not version 1 anymore.. However, Version 2
747          * does not change frame contents, so should be ok to process frames
748          * more or less identically. Some changes might be needed for
749          * verification of fields. */
750
751         switch (hdr->type) {
752         case IEEE802_1X_TYPE_EAP_PACKET:
753                 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
754                 break;
755
756         case IEEE802_1X_TYPE_EAPOL_START:
757                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
758                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
759                                "from STA");
760                 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
761                 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
762                 if (pmksa) {
763                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
764                                        HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
765                                        "available - ignore it since "
766                                        "STA sent EAPOL-Start");
767                         wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
768                 }
769                 sta->eapol_sm->eapolStart = TRUE;
770                 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
771                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
772                 break;
773
774         case IEEE802_1X_TYPE_EAPOL_LOGOFF:
775                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
776                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
777                                "from STA");
778                 sta->acct_terminate_cause =
779                         RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
780                 accounting_sta_stop(hapd, sta);
781                 sta->eapol_sm->eapolLogoff = TRUE;
782                 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
783                 break;
784
785         case IEEE802_1X_TYPE_EAPOL_KEY:
786                 wpa_printf(MSG_DEBUG, "   EAPOL-Key");
787                 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
788                         wpa_printf(MSG_DEBUG, "   Dropped key data from "
789                                    "unauthorized Supplicant");
790                         break;
791                 }
792                 break;
793
794         case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
795                 wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
796                 /* TODO: implement support for this; show data */
797                 break;
798
799         default:
800                 wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
801                 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
802                 break;
803         }
804
805         eapol_auth_step(sta->eapol_sm);
806 }
807
808
809 /**
810  * ieee802_1x_new_station - Start IEEE 802.1X authentication
811  * @hapd: hostapd BSS data
812  * @sta: The station
813  *
814  * This function is called to start IEEE 802.1X authentication when a new
815  * station completes IEEE 802.11 association.
816  */
817 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
818 {
819         struct rsn_pmksa_cache_entry *pmksa;
820         int reassoc = 1;
821         int force_1x = 0;
822
823 #ifdef CONFIG_WPS
824         if (hapd->conf->wps_state && hapd->conf->wpa &&
825             (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
826                 /*
827                  * Need to enable IEEE 802.1X/EAPOL state machines for possible
828                  * WPS handshake even if IEEE 802.1X/EAPOL is not used for
829                  * authentication in this BSS.
830                  */
831                 force_1x = 1;
832         }
833 #endif /* CONFIG_WPS */
834
835         if ((!force_1x && !hapd->conf->ieee802_1x) ||
836             wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
837                 return;
838
839         if (sta->eapol_sm == NULL) {
840                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
841                                HOSTAPD_LEVEL_DEBUG, "start authentication");
842                 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
843                                                  sta->flags & WLAN_STA_PREAUTH,
844                                                  sta);
845                 if (sta->eapol_sm == NULL) {
846                         hostapd_logger(hapd, sta->addr,
847                                        HOSTAPD_MODULE_IEEE8021X,
848                                        HOSTAPD_LEVEL_INFO,
849                                        "failed to allocate state machine");
850                         return;
851                 }
852                 reassoc = 0;
853         }
854
855 #ifdef CONFIG_WPS
856         sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
857         if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
858                 /*
859                  * Delay EAPOL frame transmission until a possible WPS
860                  * initiates the handshake with EAPOL-Start.
861                  */
862                 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
863         }
864 #endif /* CONFIG_WPS */
865
866         sta->eapol_sm->eap_if->portEnabled = TRUE;
867
868         pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
869         if (pmksa) {
870                 int old_vlanid;
871
872                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
873                                HOSTAPD_LEVEL_DEBUG,
874                                "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
875                 /* Setup EAPOL state machines to already authenticated state
876                  * because of existing PMKSA information in the cache. */
877                 sta->eapol_sm->keyRun = TRUE;
878                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
879                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
880                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
881                 sta->eapol_sm->authSuccess = TRUE;
882                 if (sta->eapol_sm->eap)
883                         eap_sm_notify_cached(sta->eapol_sm->eap);
884                 old_vlanid = sta->vlan_id;
885                 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
886                 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
887                         sta->vlan_id = 0;
888                 ap_sta_bind_vlan(hapd, sta, old_vlanid);
889         } else {
890                 if (reassoc) {
891                         /*
892                          * Force EAPOL state machines to start
893                          * re-authentication without having to wait for the
894                          * Supplicant to send EAPOL-Start.
895                          */
896                         sta->eapol_sm->reAuthenticate = TRUE;
897                 }
898                 eapol_auth_step(sta->eapol_sm);
899         }
900 }
901
902
903 void ieee802_1x_free_radius_class(struct radius_class_data *class)
904 {
905         size_t i;
906         if (class == NULL)
907                 return;
908         for (i = 0; i < class->count; i++)
909                 os_free(class->attr[i].data);
910         os_free(class->attr);
911         class->attr = NULL;
912         class->count = 0;
913 }
914
915
916 int ieee802_1x_copy_radius_class(struct radius_class_data *dst,
917                                  const struct radius_class_data *src)
918 {
919         size_t i;
920
921         if (src->attr == NULL)
922                 return 0;
923
924         dst->attr = os_zalloc(src->count * sizeof(struct radius_attr_data));
925         if (dst->attr == NULL)
926                 return -1;
927
928         dst->count = 0;
929
930         for (i = 0; i < src->count; i++) {
931                 dst->attr[i].data = os_malloc(src->attr[i].len);
932                 if (dst->attr[i].data == NULL)
933                         break;
934                 dst->count++;
935                 os_memcpy(dst->attr[i].data, src->attr[i].data,
936                           src->attr[i].len);
937                 dst->attr[i].len = src->attr[i].len;
938         }
939
940         return 0;
941 }
942
943
944 void ieee802_1x_free_station(struct sta_info *sta)
945 {
946         struct eapol_state_machine *sm = sta->eapol_sm;
947
948         if (sm == NULL)
949                 return;
950
951         sta->eapol_sm = NULL;
952
953         if (sm->last_recv_radius) {
954                 radius_msg_free(sm->last_recv_radius);
955                 os_free(sm->last_recv_radius);
956         }
957
958         os_free(sm->identity);
959         ieee802_1x_free_radius_class(&sm->radius_class);
960         eapol_auth_free(sm);
961 }
962
963
964 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
965                                           struct sta_info *sta)
966 {
967         u8 *eap;
968         size_t len;
969         struct eap_hdr *hdr;
970         int eap_type = -1;
971         char buf[64];
972         struct radius_msg *msg;
973         struct eapol_state_machine *sm = sta->eapol_sm;
974
975         if (sm == NULL || sm->last_recv_radius == NULL) {
976                 if (sm)
977                         sm->eap_if->aaaEapNoReq = TRUE;
978                 return;
979         }
980
981         msg = sm->last_recv_radius;
982
983         eap = radius_msg_get_eap(msg, &len);
984         if (eap == NULL) {
985                 /* RFC 3579, Chap. 2.6.3:
986                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
987                  * attribute */
988                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
989                                HOSTAPD_LEVEL_WARNING, "could not extract "
990                                "EAP-Message from RADIUS message");
991                 sm->eap_if->aaaEapNoReq = TRUE;
992                 return;
993         }
994
995         if (len < sizeof(*hdr)) {
996                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
997                                HOSTAPD_LEVEL_WARNING, "too short EAP packet "
998                                "received from authentication server");
999                 os_free(eap);
1000                 sm->eap_if->aaaEapNoReq = TRUE;
1001                 return;
1002         }
1003
1004         if (len > sizeof(*hdr))
1005                 eap_type = eap[sizeof(*hdr)];
1006
1007         hdr = (struct eap_hdr *) eap;
1008         switch (hdr->code) {
1009         case EAP_CODE_REQUEST:
1010                 if (eap_type >= 0)
1011                         sm->eap_type_authsrv = eap_type;
1012                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1013                             eap_type >= 0 ? eap_type_text(eap_type) : "??",
1014                             eap_type);
1015                 break;
1016         case EAP_CODE_RESPONSE:
1017                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1018                             eap_type >= 0 ? eap_type_text(eap_type) : "??",
1019                             eap_type);
1020                 break;
1021         case EAP_CODE_SUCCESS:
1022                 os_strlcpy(buf, "EAP Success", sizeof(buf));
1023                 break;
1024         case EAP_CODE_FAILURE:
1025                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1026                 break;
1027         default:
1028                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1029                 break;
1030         }
1031         buf[sizeof(buf) - 1] = '\0';
1032         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1033                        HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1034                        "id=%d len=%d) from RADIUS server: %s",
1035                        hdr->code, hdr->identifier, be_to_host16(hdr->length),
1036                        buf);
1037         sm->eap_if->aaaEapReq = TRUE;
1038
1039         wpabuf_free(sm->eap_if->aaaEapReqData);
1040         sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1041 }
1042
1043
1044 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1045                                 struct sta_info *sta, struct radius_msg *msg,
1046                                 struct radius_msg *req,
1047                                 const u8 *shared_secret,
1048                                 size_t shared_secret_len)
1049 {
1050         struct radius_ms_mppe_keys *keys;
1051         struct eapol_state_machine *sm = sta->eapol_sm;
1052         if (sm == NULL)
1053                 return;
1054
1055         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1056                                       shared_secret_len);
1057
1058         if (keys && keys->send && keys->recv) {
1059                 size_t len = keys->send_len + keys->recv_len;
1060                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1061                                 keys->send, keys->send_len);
1062                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1063                                 keys->recv, keys->recv_len);
1064
1065                 os_free(sm->eap_if->aaaEapKeyData);
1066                 sm->eap_if->aaaEapKeyData = os_malloc(len);
1067                 if (sm->eap_if->aaaEapKeyData) {
1068                         os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1069                                   keys->recv_len);
1070                         os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1071                                   keys->send, keys->send_len);
1072                         sm->eap_if->aaaEapKeyDataLen = len;
1073                         sm->eap_if->aaaEapKeyAvailable = TRUE;
1074                 }
1075         }
1076
1077         if (keys) {
1078                 os_free(keys->send);
1079                 os_free(keys->recv);
1080                 os_free(keys);
1081         }
1082 }
1083
1084
1085 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1086                                           struct sta_info *sta,
1087                                           struct radius_msg *msg)
1088 {
1089         u8 *class;
1090         size_t class_len;
1091         struct eapol_state_machine *sm = sta->eapol_sm;
1092         int count, i;
1093         struct radius_attr_data *nclass;
1094         size_t nclass_count;
1095
1096         if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1097             sm == NULL)
1098                 return;
1099
1100         ieee802_1x_free_radius_class(&sm->radius_class);
1101         count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1102         if (count <= 0)
1103                 return;
1104
1105         nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1106         if (nclass == NULL)
1107                 return;
1108
1109         nclass_count = 0;
1110
1111         class = NULL;
1112         for (i = 0; i < count; i++) {
1113                 do {
1114                         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1115                                                     &class, &class_len,
1116                                                     class) < 0) {
1117                                 i = count;
1118                                 break;
1119                         }
1120                 } while (class_len < 1);
1121
1122                 nclass[nclass_count].data = os_malloc(class_len);
1123                 if (nclass[nclass_count].data == NULL)
1124                         break;
1125
1126                 os_memcpy(nclass[nclass_count].data, class, class_len);
1127                 nclass[nclass_count].len = class_len;
1128                 nclass_count++;
1129         }
1130
1131         sm->radius_class.attr = nclass;
1132         sm->radius_class.count = nclass_count;
1133         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1134                    "attributes for " MACSTR,
1135                    (unsigned long) sm->radius_class.count,
1136                    MAC2STR(sta->addr));
1137 }
1138
1139
1140 /* Update sta->identity based on User-Name attribute in Access-Accept */
1141 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1142                                            struct sta_info *sta,
1143                                            struct radius_msg *msg)
1144 {
1145         u8 *buf, *identity;
1146         size_t len;
1147         struct eapol_state_machine *sm = sta->eapol_sm;
1148
1149         if (sm == NULL)
1150                 return;
1151
1152         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1153                                     NULL) < 0)
1154                 return;
1155
1156         identity = os_malloc(len + 1);
1157         if (identity == NULL)
1158                 return;
1159
1160         os_memcpy(identity, buf, len);
1161         identity[len] = '\0';
1162
1163         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1164                        HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1165                        "User-Name from Access-Accept '%s'",
1166                        sm->identity ? (char *) sm->identity : "N/A",
1167                        (char *) identity);
1168
1169         os_free(sm->identity);
1170         sm->identity = identity;
1171         sm->identity_len = len;
1172 }
1173
1174
1175 struct sta_id_search {
1176         u8 identifier;
1177         struct eapol_state_machine *sm;
1178 };
1179
1180
1181 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1182                                                struct sta_info *sta,
1183                                                void *ctx)
1184 {
1185         struct sta_id_search *id_search = ctx;
1186         struct eapol_state_machine *sm = sta->eapol_sm;
1187
1188         if (sm && sm->radius_identifier >= 0 &&
1189             sm->radius_identifier == id_search->identifier) {
1190                 id_search->sm = sm;
1191                 return 1;
1192         }
1193         return 0;
1194 }
1195
1196
1197 static struct eapol_state_machine *
1198 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1199 {
1200         struct sta_id_search id_search;
1201         id_search.identifier = identifier;
1202         id_search.sm = NULL;
1203         ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1204         return id_search.sm;
1205 }
1206
1207
1208 /**
1209  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1210  * @msg: RADIUS response message
1211  * @req: RADIUS request message
1212  * @shared_secret: RADIUS shared secret
1213  * @shared_secret_len: Length of shared_secret in octets
1214  * @data: Context data (struct hostapd_data *)
1215  * Returns: Processing status
1216  */
1217 static RadiusRxResult
1218 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1219                         const u8 *shared_secret, size_t shared_secret_len,
1220                         void *data)
1221 {
1222         struct hostapd_data *hapd = data;
1223         struct sta_info *sta;
1224         u32 session_timeout = 0, termination_action, acct_interim_interval;
1225         int session_timeout_set, old_vlanid = 0;
1226         struct eapol_state_machine *sm;
1227         int override_eapReq = 0;
1228
1229         sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1230         if (sm == NULL) {
1231                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1232                            "station for this RADIUS message");
1233                 return RADIUS_RX_UNKNOWN;
1234         }
1235         sta = sm->sta;
1236
1237         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1238          * present when packet contains an EAP-Message attribute */
1239         if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1240             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1241                                 0) < 0 &&
1242             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1243                 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1244                            "Message-Authenticator since it does not include "
1245                            "EAP-Message");
1246         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1247                                      req, 1)) {
1248                 printf("Incoming RADIUS packet did not have correct "
1249                        "Message-Authenticator - dropped\n");
1250                 return RADIUS_RX_INVALID_AUTHENTICATOR;
1251         }
1252
1253         if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1254             msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1255             msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1256                 printf("Unknown RADIUS message code\n");
1257                 return RADIUS_RX_UNKNOWN;
1258         }
1259
1260         sm->radius_identifier = -1;
1261         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1262                    MAC2STR(sta->addr));
1263
1264         if (sm->last_recv_radius) {
1265                 radius_msg_free(sm->last_recv_radius);
1266                 os_free(sm->last_recv_radius);
1267         }
1268
1269         sm->last_recv_radius = msg;
1270
1271         session_timeout_set =
1272                 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1273                                            &session_timeout);
1274         if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1275                                       &termination_action))
1276                 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1277
1278         if (hapd->conf->radius->acct_interim_interval == 0 &&
1279             msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1280             radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1281                                       &acct_interim_interval) == 0) {
1282                 if (acct_interim_interval < 60) {
1283                         hostapd_logger(hapd, sta->addr,
1284                                        HOSTAPD_MODULE_IEEE8021X,
1285                                        HOSTAPD_LEVEL_INFO,
1286                                        "ignored too small "
1287                                        "Acct-Interim-Interval %d",
1288                                        acct_interim_interval);
1289                 } else
1290                         sta->acct_interim_interval = acct_interim_interval;
1291         }
1292
1293
1294         switch (msg->hdr->code) {
1295         case RADIUS_CODE_ACCESS_ACCEPT:
1296                 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1297                         sta->vlan_id = 0;
1298                 else {
1299                         old_vlanid = sta->vlan_id;
1300                         sta->vlan_id = radius_msg_get_vlanid(msg);
1301                 }
1302                 if (sta->vlan_id > 0 &&
1303                     hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1304                                                sta->vlan_id)) {
1305                         hostapd_logger(hapd, sta->addr,
1306                                        HOSTAPD_MODULE_RADIUS,
1307                                        HOSTAPD_LEVEL_INFO,
1308                                        "VLAN ID %d", sta->vlan_id);
1309                 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1310                         sta->eapol_sm->authFail = TRUE;
1311                         hostapd_logger(hapd, sta->addr,
1312                                        HOSTAPD_MODULE_IEEE8021X,
1313                                        HOSTAPD_LEVEL_INFO, "authentication "
1314                                        "server did not include required VLAN "
1315                                        "ID in Access-Accept");
1316                         break;
1317                 }
1318
1319                 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1320
1321                 /* RFC 3580, Ch. 3.17 */
1322                 if (session_timeout_set && termination_action ==
1323                     RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1324                         sm->reAuthPeriod = session_timeout;
1325                 } else if (session_timeout_set)
1326                         ap_sta_session_timeout(hapd, sta, session_timeout);
1327
1328                 sm->eap_if->aaaSuccess = TRUE;
1329                 override_eapReq = 1;
1330                 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1331                                     shared_secret_len);
1332                 ieee802_1x_store_radius_class(hapd, sta, msg);
1333                 ieee802_1x_update_sta_identity(hapd, sta, msg);
1334                 if (sm->eap_if->eapKeyAvailable &&
1335                     wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1336                                        session_timeout_set ?
1337                                        (int) session_timeout : -1, sm) == 0) {
1338                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1339                                        HOSTAPD_LEVEL_DEBUG,
1340                                        "Added PMKSA cache entry");
1341                 }
1342                 break;
1343         case RADIUS_CODE_ACCESS_REJECT:
1344                 sm->eap_if->aaaFail = TRUE;
1345                 override_eapReq = 1;
1346                 break;
1347         case RADIUS_CODE_ACCESS_CHALLENGE:
1348                 sm->eap_if->aaaEapReq = TRUE;
1349                 if (session_timeout_set) {
1350                         /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1351                         sm->eap_if->aaaMethodTimeout = session_timeout;
1352                         hostapd_logger(hapd, sm->addr,
1353                                        HOSTAPD_MODULE_IEEE8021X,
1354                                        HOSTAPD_LEVEL_DEBUG,
1355                                        "using EAP timeout of %d seconds (from "
1356                                        "RADIUS)",
1357                                        sm->eap_if->aaaMethodTimeout);
1358                 } else {
1359                         /*
1360                          * Use dynamic retransmission behavior per EAP
1361                          * specification.
1362                          */
1363                         sm->eap_if->aaaMethodTimeout = 0;
1364                 }
1365                 break;
1366         }
1367
1368         ieee802_1x_decapsulate_radius(hapd, sta);
1369         if (override_eapReq)
1370                 sm->eap_if->aaaEapReq = FALSE;
1371
1372         eapol_auth_step(sm);
1373
1374         return RADIUS_RX_QUEUED;
1375 }
1376
1377
1378 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1379 {
1380         struct eapol_state_machine *sm = sta->eapol_sm;
1381         if (sm == NULL)
1382                 return;
1383
1384         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1385                        HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1386
1387         if (sm->last_recv_radius) {
1388                 radius_msg_free(sm->last_recv_radius);
1389                 os_free(sm->last_recv_radius);
1390                 sm->last_recv_radius = NULL;
1391         }
1392
1393         if (sm->eap_if->eapTimeout) {
1394                 /*
1395                  * Disconnect the STA since it did not reply to the last EAP
1396                  * request and we cannot continue EAP processing (EAP-Failure
1397                  * could only be sent if the EAP peer actually replied).
1398                  */
1399                 sm->eap_if->portEnabled = FALSE;
1400                 hostapd_sta_deauth(hapd, sta->addr,
1401                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
1402                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1403                                 WLAN_STA_AUTHORIZED);
1404                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1405                 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1406                 sta->timeout_next = STA_REMOVE;
1407         }
1408 }
1409
1410
1411 #ifdef HOSTAPD_DUMP_STATE
1412 static void fprint_char(FILE *f, char c)
1413 {
1414         if (c >= 32 && c < 127)
1415                 fprintf(f, "%c", c);
1416         else
1417                 fprintf(f, "<%02x>", c);
1418 }
1419
1420
1421 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1422 {
1423         struct eapol_state_machine *sm = sta->eapol_sm;
1424         if (sm == NULL)
1425                 return;
1426
1427         fprintf(f, "%sIEEE 802.1X:\n", prefix);
1428
1429         if (sm->identity) {
1430                 size_t i;
1431                 fprintf(f, "%sidentity=", prefix);
1432                 for (i = 0; i < sm->identity_len; i++)
1433                         fprint_char(f, sm->identity[i]);
1434                 fprintf(f, "\n");
1435         }
1436
1437         fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1438                 "Supplicant: %d (%s)\n", prefix,
1439                 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1440                 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1441
1442         fprintf(f, "%scached_packets=%s\n", prefix,
1443                 sm->last_recv_radius ? "[RX RADIUS]" : "");
1444
1445         eapol_auth_dump_state(f, prefix, sm);
1446 }
1447 #endif /* HOSTAPD_DUMP_STATE */
1448
1449
1450 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1451 {
1452         if (hapd->conf->default_wep_key_len < 1)
1453                 return 0;
1454
1455         os_free(hapd->default_wep_key);
1456         hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1457         if (hapd->default_wep_key == NULL ||
1458             os_get_random(hapd->default_wep_key,
1459                           hapd->conf->default_wep_key_len)) {
1460                 printf("Could not generate random WEP key.\n");
1461                 os_free(hapd->default_wep_key);
1462                 hapd->default_wep_key = NULL;
1463                 return -1;
1464         }
1465
1466         wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1467                         hapd->default_wep_key,
1468                         hapd->conf->default_wep_key_len);
1469
1470         return 0;
1471 }
1472
1473
1474 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1475                                         struct sta_info *sta, void *ctx)
1476 {
1477         if (sta->eapol_sm) {
1478                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1479                 eapol_auth_step(sta->eapol_sm);
1480         }
1481         return 0;
1482 }
1483
1484
1485 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1486 {
1487         struct hostapd_data *hapd = eloop_ctx;
1488
1489         if (hapd->default_wep_key_idx >= 3)
1490                 hapd->default_wep_key_idx =
1491                         hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1492         else
1493                 hapd->default_wep_key_idx++;
1494
1495         wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1496                    hapd->default_wep_key_idx);
1497                       
1498         if (ieee802_1x_rekey_broadcast(hapd)) {
1499                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1500                                HOSTAPD_LEVEL_WARNING, "failed to generate a "
1501                                "new broadcast key");
1502                 os_free(hapd->default_wep_key);
1503                 hapd->default_wep_key = NULL;
1504                 return;
1505         }
1506
1507         /* TODO: Could setup key for RX here, but change default TX keyid only
1508          * after new broadcast key has been sent to all stations. */
1509         if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1510                                    hapd->default_wep_key_idx,
1511                                    hapd->default_wep_key,
1512                                    hapd->conf->default_wep_key_len, 1)) {
1513                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1514                                HOSTAPD_LEVEL_WARNING, "failed to configure a "
1515                                "new broadcast key");
1516                 os_free(hapd->default_wep_key);
1517                 hapd->default_wep_key = NULL;
1518                 return;
1519         }
1520
1521         ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1522
1523         if (hapd->conf->wep_rekeying_period > 0) {
1524                 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1525                                        ieee802_1x_rekey, hapd, NULL);
1526         }
1527 }
1528
1529
1530 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1531                                   const u8 *data, size_t datalen)
1532 {
1533         ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1534 }
1535
1536
1537 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1538                                 const u8 *data, size_t datalen)
1539 {
1540         struct hostapd_data *hapd = ctx;
1541         struct sta_info *sta = sta_ctx;
1542
1543         ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1544 }
1545
1546
1547 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1548                                  int preauth)
1549 {
1550         struct hostapd_data *hapd = ctx;
1551         struct sta_info *sta = sta_ctx;
1552         if (preauth)
1553                 rsn_preauth_finished(hapd, sta, success);
1554         else
1555                 ieee802_1x_finished(hapd, sta, success);
1556 }
1557
1558
1559 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1560                                    size_t identity_len, int phase2,
1561                                    struct eap_user *user)
1562 {
1563         struct hostapd_data *hapd = ctx;
1564         const struct hostapd_eap_user *eap_user;
1565         int i, count;
1566
1567         eap_user = hostapd_get_eap_user(hapd->conf, identity,
1568                                         identity_len, phase2);
1569         if (eap_user == NULL)
1570                 return -1;
1571
1572         os_memset(user, 0, sizeof(*user));
1573         user->phase2 = phase2;
1574         count = EAP_USER_MAX_METHODS;
1575         if (count > EAP_MAX_METHODS)
1576                 count = EAP_MAX_METHODS;
1577         for (i = 0; i < count; i++) {
1578                 user->methods[i].vendor = eap_user->methods[i].vendor;
1579                 user->methods[i].method = eap_user->methods[i].method;
1580         }
1581
1582         if (eap_user->password) {
1583                 user->password = os_malloc(eap_user->password_len);
1584                 if (user->password == NULL)
1585                         return -1;
1586                 os_memcpy(user->password, eap_user->password,
1587                           eap_user->password_len);
1588                 user->password_len = eap_user->password_len;
1589         }
1590         user->force_version = eap_user->force_version;
1591         user->ttls_auth = eap_user->ttls_auth;
1592
1593         return 0;
1594 }
1595
1596
1597 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1598 {
1599         struct hostapd_data *hapd = ctx;
1600         struct sta_info *sta;
1601         sta = ap_get_sta(hapd, addr);
1602         if (sta == NULL || sta->eapol_sm == NULL)
1603                 return 0;
1604         return 1;
1605 }
1606
1607
1608 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1609                               eapol_logger_level level, const char *txt)
1610 {
1611         struct hostapd_data *hapd = ctx;
1612         int hlevel;
1613
1614         switch (level) {
1615         case EAPOL_LOGGER_WARNING:
1616                 hlevel = HOSTAPD_LEVEL_WARNING;
1617                 break;
1618         case EAPOL_LOGGER_INFO:
1619                 hlevel = HOSTAPD_LEVEL_INFO;
1620                 break;
1621         case EAPOL_LOGGER_DEBUG:
1622         default:
1623                 hlevel = HOSTAPD_LEVEL_DEBUG;
1624                 break;
1625         }
1626
1627         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1628                        txt);
1629 }
1630
1631
1632 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1633                                            int authorized)
1634 {
1635         struct hostapd_data *hapd = ctx;
1636         struct sta_info *sta = sta_ctx;
1637         ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1638 }
1639
1640
1641 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1642 {
1643         struct hostapd_data *hapd = ctx;
1644         struct sta_info *sta = sta_ctx;
1645         ieee802_1x_abort_auth(hapd, sta);
1646 }
1647
1648
1649 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1650 {
1651         struct hostapd_data *hapd = ctx;
1652         struct sta_info *sta = sta_ctx;
1653         ieee802_1x_tx_key(hapd, sta);
1654 }
1655
1656
1657 int ieee802_1x_init(struct hostapd_data *hapd)
1658 {
1659         int i;
1660         struct eapol_auth_config conf;
1661         struct eapol_auth_cb cb;
1662
1663         os_memset(&conf, 0, sizeof(conf));
1664         conf.hapd = hapd;
1665         conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1666         conf.wpa = hapd->conf->wpa;
1667         conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1668         conf.eap_server = hapd->conf->eap_server;
1669         conf.ssl_ctx = hapd->ssl_ctx;
1670         conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1671         conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1672         conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1673         conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1674         conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1675         conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1676         conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1677         conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1678         conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1679         conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1680         conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1681         conf.tnc = hapd->conf->tnc;
1682         conf.wps = hapd->wps;
1683
1684         os_memset(&cb, 0, sizeof(cb));
1685         cb.eapol_send = ieee802_1x_eapol_send;
1686         cb.aaa_send = ieee802_1x_aaa_send;
1687         cb.finished = _ieee802_1x_finished;
1688         cb.get_eap_user = ieee802_1x_get_eap_user;
1689         cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1690         cb.logger = ieee802_1x_logger;
1691         cb.set_port_authorized = ieee802_1x_set_port_authorized;
1692         cb.abort_auth = _ieee802_1x_abort_auth;
1693         cb.tx_key = _ieee802_1x_tx_key;
1694
1695         hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1696         if (hapd->eapol_auth == NULL)
1697                 return -1;
1698
1699         if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1700             hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1701                 return -1;
1702
1703         if (radius_client_register(hapd->radius, RADIUS_AUTH,
1704                                    ieee802_1x_receive_auth, hapd))
1705                 return -1;
1706
1707         if (hapd->conf->default_wep_key_len) {
1708                 hostapd_set_privacy(hapd, 1);
1709
1710                 for (i = 0; i < 4; i++)
1711                         hostapd_set_encryption(hapd->conf->iface, hapd,
1712                                                "none", NULL, i, NULL, 0, 0);
1713
1714                 ieee802_1x_rekey(hapd, NULL);
1715
1716                 if (hapd->default_wep_key == NULL)
1717                         return -1;
1718         }
1719
1720         return 0;
1721 }
1722
1723
1724 void ieee802_1x_deinit(struct hostapd_data *hapd)
1725 {
1726         eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1727
1728         if (hapd->driver != NULL &&
1729             (hapd->conf->ieee802_1x || hapd->conf->wpa))
1730                 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1731
1732         eapol_auth_deinit(hapd->eapol_auth);
1733         hapd->eapol_auth = NULL;
1734 }
1735
1736
1737 int ieee802_1x_reconfig(struct hostapd_data *hapd, 
1738                         struct hostapd_config *oldconf,
1739                         struct hostapd_bss_config *oldbss)
1740 {
1741         ieee802_1x_deinit(hapd);
1742         return ieee802_1x_init(hapd);
1743 }
1744
1745
1746 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1747                          u8 *buf, size_t len, int ack)
1748 {
1749         struct ieee80211_hdr *hdr;
1750         struct ieee802_1x_hdr *xhdr;
1751         struct ieee802_1x_eapol_key *key;
1752         u8 *pos;
1753         const unsigned char rfc1042_hdr[ETH_ALEN] =
1754                 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1755
1756         if (sta == NULL)
1757                 return -1;
1758         if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1759                 return 0;
1760
1761         hdr = (struct ieee80211_hdr *) buf;
1762         pos = (u8 *) (hdr + 1);
1763         if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1764                 return 0;
1765         pos += sizeof(rfc1042_hdr);
1766         if (WPA_GET_BE16(pos) != ETH_P_PAE)
1767                 return 0;
1768         pos += 2;
1769
1770         xhdr = (struct ieee802_1x_hdr *) pos;
1771         pos += sizeof(*xhdr);
1772
1773         wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1774                    "type=%d length=%d - ack=%d",
1775                    MAC2STR(sta->addr), xhdr->version, xhdr->type,
1776                    be_to_host16(xhdr->length), ack);
1777
1778         /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1779          * or Authenticator state machines, but EAPOL-Key packets are not
1780          * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1781          * packets couple of times because otherwise STA keys become
1782          * unsynchronized with AP. */
1783         if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1784             pos + sizeof(*key) <= buf + len) {
1785                 key = (struct ieee802_1x_eapol_key *) pos;
1786                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1787                                HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1788                                "frame (%scast index=%d)",
1789                                key->key_index & BIT(7) ? "uni" : "broad",
1790                                key->key_index & ~BIT(7));
1791                 /* TODO: re-send EAPOL-Key couple of times (with short delay
1792                  * between them?). If all attempt fail, report error and
1793                  * deauthenticate STA so that it will get new keys when
1794                  * authenticating again (e.g., after returning in range).
1795                  * Separate limit/transmit state needed both for unicast and
1796                  * broadcast keys(?) */
1797         }
1798         /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1799          * to here and change the key only if the EAPOL-Key packet was Acked.
1800          */
1801
1802         return 1;
1803 }
1804
1805
1806 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1807 {
1808         if (sm == NULL || sm->identity == NULL)
1809                 return NULL;
1810
1811         *len = sm->identity_len;
1812         return sm->identity;
1813 }
1814
1815
1816 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1817                                  int idx)
1818 {
1819         if (sm == NULL || sm->radius_class.attr == NULL ||
1820             idx >= (int) sm->radius_class.count)
1821                 return NULL;
1822
1823         *len = sm->radius_class.attr[idx].len;
1824         return sm->radius_class.attr[idx].data;
1825 }
1826
1827
1828 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1829 {
1830         if (sm == NULL)
1831                 return NULL;
1832
1833         *len = sm->eap_if->eapKeyDataLen;
1834         return sm->eap_if->eapKeyData;
1835 }
1836
1837
1838 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1839                                     int enabled)
1840 {
1841         if (sm == NULL)
1842                 return;
1843         sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1844         eapol_auth_step(sm);
1845 }
1846
1847
1848 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1849                                   int valid)
1850 {
1851         if (sm == NULL)
1852                 return;
1853         sm->portValid = valid ? TRUE : FALSE;
1854         eapol_auth_step(sm);
1855 }
1856
1857
1858 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1859 {
1860         if (sm == NULL)
1861                 return;
1862         if (pre_auth)
1863                 sm->flags |= EAPOL_SM_PREAUTH;
1864         else
1865                 sm->flags &= ~EAPOL_SM_PREAUTH;
1866 }
1867
1868
1869 static const char * bool_txt(Boolean bool)
1870 {
1871         return bool ? "TRUE" : "FALSE";
1872 }
1873
1874
1875 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1876 {
1877         /* TODO */
1878         return 0;
1879 }
1880
1881
1882 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1883                            char *buf, size_t buflen)
1884 {
1885         int len = 0, ret;
1886         struct eapol_state_machine *sm = sta->eapol_sm;
1887
1888         if (sm == NULL)
1889                 return 0;
1890
1891         ret = os_snprintf(buf + len, buflen - len,
1892                           "dot1xPaePortNumber=%d\n"
1893                           "dot1xPaePortProtocolVersion=%d\n"
1894                           "dot1xPaePortCapabilities=1\n"
1895                           "dot1xPaePortInitialize=%d\n"
1896                           "dot1xPaePortReauthenticate=FALSE\n",
1897                           sta->aid,
1898                           EAPOL_VERSION,
1899                           sm->initialize);
1900         if (ret < 0 || (size_t) ret >= buflen - len)
1901                 return len;
1902         len += ret;
1903
1904         /* dot1xAuthConfigTable */
1905         ret = os_snprintf(buf + len, buflen - len,
1906                           "dot1xAuthPaeState=%d\n"
1907                           "dot1xAuthBackendAuthState=%d\n"
1908                           "dot1xAuthAdminControlledDirections=%d\n"
1909                           "dot1xAuthOperControlledDirections=%d\n"
1910                           "dot1xAuthAuthControlledPortStatus=%d\n"
1911                           "dot1xAuthAuthControlledPortControl=%d\n"
1912                           "dot1xAuthQuietPeriod=%u\n"
1913                           "dot1xAuthServerTimeout=%u\n"
1914                           "dot1xAuthReAuthPeriod=%u\n"
1915                           "dot1xAuthReAuthEnabled=%s\n"
1916                           "dot1xAuthKeyTxEnabled=%s\n",
1917                           sm->auth_pae_state + 1,
1918                           sm->be_auth_state + 1,
1919                           sm->adminControlledDirections,
1920                           sm->operControlledDirections,
1921                           sm->authPortStatus,
1922                           sm->portControl,
1923                           sm->quietPeriod,
1924                           sm->serverTimeout,
1925                           sm->reAuthPeriod,
1926                           bool_txt(sm->reAuthEnabled),
1927                           bool_txt(sm->keyTxEnabled));
1928         if (ret < 0 || (size_t) ret >= buflen - len)
1929                 return len;
1930         len += ret;
1931
1932         /* dot1xAuthStatsTable */
1933         ret = os_snprintf(buf + len, buflen - len,
1934                           "dot1xAuthEapolFramesRx=%u\n"
1935                           "dot1xAuthEapolFramesTx=%u\n"
1936                           "dot1xAuthEapolStartFramesRx=%u\n"
1937                           "dot1xAuthEapolLogoffFramesRx=%u\n"
1938                           "dot1xAuthEapolRespIdFramesRx=%u\n"
1939                           "dot1xAuthEapolRespFramesRx=%u\n"
1940                           "dot1xAuthEapolReqIdFramesTx=%u\n"
1941                           "dot1xAuthEapolReqFramesTx=%u\n"
1942                           "dot1xAuthInvalidEapolFramesRx=%u\n"
1943                           "dot1xAuthEapLengthErrorFramesRx=%u\n"
1944                           "dot1xAuthLastEapolFrameVersion=%u\n"
1945                           "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1946                           sm->dot1xAuthEapolFramesRx,
1947                           sm->dot1xAuthEapolFramesTx,
1948                           sm->dot1xAuthEapolStartFramesRx,
1949                           sm->dot1xAuthEapolLogoffFramesRx,
1950                           sm->dot1xAuthEapolRespIdFramesRx,
1951                           sm->dot1xAuthEapolRespFramesRx,
1952                           sm->dot1xAuthEapolReqIdFramesTx,
1953                           sm->dot1xAuthEapolReqFramesTx,
1954                           sm->dot1xAuthInvalidEapolFramesRx,
1955                           sm->dot1xAuthEapLengthErrorFramesRx,
1956                           sm->dot1xAuthLastEapolFrameVersion,
1957                           MAC2STR(sm->addr));
1958         if (ret < 0 || (size_t) ret >= buflen - len)
1959                 return len;
1960         len += ret;
1961
1962         /* dot1xAuthDiagTable */
1963         ret = os_snprintf(buf + len, buflen - len,
1964                           "dot1xAuthEntersConnecting=%u\n"
1965                           "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1966                           "dot1xAuthEntersAuthenticating=%u\n"
1967                           "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1968                           "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1969                           "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1970                           "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1971                           "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1972                           "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1973                           "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1974                           "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1975                           "dot1xAuthBackendResponses=%u\n"
1976                           "dot1xAuthBackendAccessChallenges=%u\n"
1977                           "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1978                           "dot1xAuthBackendAuthSuccesses=%u\n"
1979                           "dot1xAuthBackendAuthFails=%u\n",
1980                           sm->authEntersConnecting,
1981                           sm->authEapLogoffsWhileConnecting,
1982                           sm->authEntersAuthenticating,
1983                           sm->authAuthSuccessesWhileAuthenticating,
1984                           sm->authAuthTimeoutsWhileAuthenticating,
1985                           sm->authAuthFailWhileAuthenticating,
1986                           sm->authAuthEapStartsWhileAuthenticating,
1987                           sm->authAuthEapLogoffWhileAuthenticating,
1988                           sm->authAuthReauthsWhileAuthenticated,
1989                           sm->authAuthEapStartsWhileAuthenticated,
1990                           sm->authAuthEapLogoffWhileAuthenticated,
1991                           sm->backendResponses,
1992                           sm->backendAccessChallenges,
1993                           sm->backendOtherRequestsToSupplicant,
1994                           sm->backendAuthSuccesses,
1995                           sm->backendAuthFails);
1996         if (ret < 0 || (size_t) ret >= buflen - len)
1997                 return len;
1998         len += ret;
1999
2000         /* dot1xAuthSessionStatsTable */
2001         ret = os_snprintf(buf + len, buflen - len,
2002                           /* TODO: dot1xAuthSessionOctetsRx */
2003                           /* TODO: dot1xAuthSessionOctetsTx */
2004                           /* TODO: dot1xAuthSessionFramesRx */
2005                           /* TODO: dot1xAuthSessionFramesTx */
2006                           "dot1xAuthSessionId=%08X-%08X\n"
2007                           "dot1xAuthSessionAuthenticMethod=%d\n"
2008                           "dot1xAuthSessionTime=%u\n"
2009                           "dot1xAuthSessionTerminateCause=999\n"
2010                           "dot1xAuthSessionUserName=%s\n",
2011                           sta->acct_session_id_hi, sta->acct_session_id_lo,
2012                           (wpa_key_mgmt_wpa_ieee8021x(
2013                                    wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2014                           1 : 2,
2015                           (unsigned int) (time(NULL) -
2016                                           sta->acct_session_start),
2017                           sm->identity);
2018         if (ret < 0 || (size_t) ret >= buflen - len)
2019                 return len;
2020         len += ret;
2021
2022         return len;
2023 }
2024
2025
2026 static void ieee802_1x_finished(struct hostapd_data *hapd,
2027                                 struct sta_info *sta, int success)
2028 {
2029         const u8 *key;
2030         size_t len;
2031         /* TODO: get PMKLifetime from WPA parameters */
2032         static const int dot11RSNAConfigPMKLifetime = 43200;
2033
2034         key = ieee802_1x_get_key(sta->eapol_sm, &len);
2035         if (success && key && len >= PMK_LEN &&
2036             wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2037                                sta->eapol_sm) == 0) {
2038                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2039                                HOSTAPD_LEVEL_DEBUG,
2040                                "Added PMKSA cache entry (IEEE 802.1X)");
2041         }
2042 }