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