Moved TX status processing for STA entries away from driver_*.c
[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 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 #ifndef CONFIG_NO_RADIUS
375 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
376                                       struct eapol_state_machine *sm,
377                                       const u8 *eap, size_t len)
378 {
379         const u8 *identity;
380         size_t identity_len;
381
382         if (len <= sizeof(struct eap_hdr) ||
383             eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
384                 return;
385
386         identity = eap_get_identity(sm->eap, &identity_len);
387         if (identity == NULL)
388                 return;
389
390         /* Save station identity for future RADIUS packets */
391         os_free(sm->identity);
392         sm->identity = os_malloc(identity_len + 1);
393         if (sm->identity == NULL) {
394                 sm->identity_len = 0;
395                 return;
396         }
397
398         os_memcpy(sm->identity, identity, identity_len);
399         sm->identity_len = identity_len;
400         sm->identity[identity_len] = '\0';
401         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
402                        HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
403         sm->dot1xAuthEapolRespIdFramesRx++;
404 }
405
406
407 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
408                                           struct sta_info *sta,
409                                           const u8 *eap, size_t len)
410 {
411         struct radius_msg *msg;
412         char buf[128];
413         struct eapol_state_machine *sm = sta->eapol_sm;
414
415         if (sm == NULL)
416                 return;
417
418         ieee802_1x_learn_identity(hapd, sm, eap, len);
419
420         wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
421                    "packet");
422
423         sm->radius_identifier = radius_client_get_id(hapd->radius);
424         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
425                              sm->radius_identifier);
426         if (msg == NULL) {
427                 printf("Could not create net RADIUS packet\n");
428                 return;
429         }
430
431         radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
432
433         if (sm->identity &&
434             !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
435                                  sm->identity, sm->identity_len)) {
436                 printf("Could not add User-Name\n");
437                 goto fail;
438         }
439
440         if (hapd->conf->own_ip_addr.af == AF_INET &&
441             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
442                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
443                 printf("Could not add NAS-IP-Address\n");
444                 goto fail;
445         }
446
447 #ifdef CONFIG_IPV6
448         if (hapd->conf->own_ip_addr.af == AF_INET6 &&
449             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
450                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
451                 printf("Could not add NAS-IPv6-Address\n");
452                 goto fail;
453         }
454 #endif /* CONFIG_IPV6 */
455
456         if (hapd->conf->nas_identifier &&
457             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
458                                  (u8 *) hapd->conf->nas_identifier,
459                                  os_strlen(hapd->conf->nas_identifier))) {
460                 printf("Could not add NAS-Identifier\n");
461                 goto fail;
462         }
463
464         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
465                 printf("Could not add NAS-Port\n");
466                 goto fail;
467         }
468
469         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
470                     MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
471         buf[sizeof(buf) - 1] = '\0';
472         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
473                                  (u8 *) buf, os_strlen(buf))) {
474                 printf("Could not add Called-Station-Id\n");
475                 goto fail;
476         }
477
478         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
479                     MAC2STR(sta->addr));
480         buf[sizeof(buf) - 1] = '\0';
481         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
482                                  (u8 *) buf, os_strlen(buf))) {
483                 printf("Could not add Calling-Station-Id\n");
484                 goto fail;
485         }
486
487         /* TODO: should probably check MTU from driver config; 2304 is max for
488          * IEEE 802.11, but use 1400 to avoid problems with too large packets
489          */
490         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
491                 printf("Could not add Framed-MTU\n");
492                 goto fail;
493         }
494
495         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
496                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
497                 printf("Could not add NAS-Port-Type\n");
498                 goto fail;
499         }
500
501         if (sta->flags & WLAN_STA_PREAUTH) {
502                 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
503                            sizeof(buf));
504         } else {
505                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
506                             radius_sta_rate(hapd, sta) / 2,
507                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
508                             radius_mode_txt(hapd));
509                 buf[sizeof(buf) - 1] = '\0';
510         }
511         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
512                                  (u8 *) buf, os_strlen(buf))) {
513                 printf("Could not add Connect-Info\n");
514                 goto fail;
515         }
516
517         if (eap && !radius_msg_add_eap(msg, eap, len)) {
518                 printf("Could not add EAP-Message\n");
519                 goto fail;
520         }
521
522         /* State attribute must be copied if and only if this packet is
523          * Access-Request reply to the previous Access-Challenge */
524         if (sm->last_recv_radius && sm->last_recv_radius->hdr->code ==
525             RADIUS_CODE_ACCESS_CHALLENGE) {
526                 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
527                                                RADIUS_ATTR_STATE);
528                 if (res < 0) {
529                         printf("Could not copy State attribute from previous "
530                                "Access-Challenge\n");
531                         goto fail;
532                 }
533                 if (res > 0) {
534                         wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
535                 }
536         }
537
538         radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
539         return;
540
541  fail:
542         radius_msg_free(msg);
543         os_free(msg);
544 }
545 #endif /* CONFIG_NO_RADIUS */
546
547
548 char *eap_type_text(u8 type)
549 {
550         switch (type) {
551         case EAP_TYPE_IDENTITY: return "Identity";
552         case EAP_TYPE_NOTIFICATION: return "Notification";
553         case EAP_TYPE_NAK: return "Nak";
554         case EAP_TYPE_MD5: return "MD5-Challenge";
555         case EAP_TYPE_OTP: return "One-Time Password";
556         case EAP_TYPE_GTC: return "Generic Token Card";
557         case EAP_TYPE_TLS: return "TLS";
558         case EAP_TYPE_TTLS: return "TTLS";
559         case EAP_TYPE_PEAP: return "PEAP";
560         case EAP_TYPE_SIM: return "SIM";
561         case EAP_TYPE_FAST: return "FAST";
562         case EAP_TYPE_SAKE: return "SAKE";
563         case EAP_TYPE_PSK: return "PSK";
564         case EAP_TYPE_PAX: return "PAX";
565         default: return "Unknown";
566         }
567 }
568
569
570 static void handle_eap_response(struct hostapd_data *hapd,
571                                 struct sta_info *sta, struct eap_hdr *eap,
572                                 size_t len)
573 {
574         u8 type, *data;
575         struct eapol_state_machine *sm = sta->eapol_sm;
576         if (sm == NULL)
577                 return;
578
579         data = (u8 *) (eap + 1);
580
581         if (len < sizeof(*eap) + 1) {
582                 printf("handle_eap_response: too short response data\n");
583                 return;
584         }
585
586         sm->eap_type_supp = type = data[0];
587
588         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
589                        HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
590                        "id=%d len=%d) from STA: EAP Response-%s (%d)",
591                        eap->code, eap->identifier, be_to_host16(eap->length),
592                        eap_type_text(type), type);
593
594         sm->dot1xAuthEapolRespFramesRx++;
595
596         wpabuf_free(sm->eap_if->eapRespData);
597         sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
598         sm->eapolEap = TRUE;
599 }
600
601
602 /* Process incoming EAP packet from Supplicant */
603 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
604                        u8 *buf, size_t len)
605 {
606         struct eap_hdr *eap;
607         u16 eap_len;
608
609         if (len < sizeof(*eap)) {
610                 printf("   too short EAP packet\n");
611                 return;
612         }
613
614         eap = (struct eap_hdr *) buf;
615
616         eap_len = be_to_host16(eap->length);
617         wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
618                    eap->code, eap->identifier, eap_len);
619         if (eap_len < sizeof(*eap)) {
620                 wpa_printf(MSG_DEBUG, "   Invalid EAP length");
621                 return;
622         } else if (eap_len > len) {
623                 wpa_printf(MSG_DEBUG, "   Too short frame to contain this EAP "
624                            "packet");
625                 return;
626         } else if (eap_len < len) {
627                 wpa_printf(MSG_DEBUG, "   Ignoring %lu extra bytes after EAP "
628                            "packet", (unsigned long) len - eap_len);
629         }
630
631         switch (eap->code) {
632         case EAP_CODE_REQUEST:
633                 wpa_printf(MSG_DEBUG, " (request)");
634                 return;
635         case EAP_CODE_RESPONSE:
636                 wpa_printf(MSG_DEBUG, " (response)");
637                 handle_eap_response(hapd, sta, eap, eap_len);
638                 break;
639         case EAP_CODE_SUCCESS:
640                 wpa_printf(MSG_DEBUG, " (success)");
641                 return;
642         case EAP_CODE_FAILURE:
643                 wpa_printf(MSG_DEBUG, " (failure)");
644                 return;
645         default:
646                 wpa_printf(MSG_DEBUG, " (unknown code)");
647                 return;
648         }
649 }
650
651
652 /**
653  * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
654  * @hapd: hostapd BSS data
655  * @sa: Source address (sender of the EAPOL frame)
656  * @buf: EAPOL frame
657  * @len: Length of buf in octets
658  *
659  * This function is called for each incoming EAPOL frame from the interface
660  */
661 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
662                         size_t len)
663 {
664         struct sta_info *sta;
665         struct ieee802_1x_hdr *hdr;
666         struct ieee802_1x_eapol_key *key;
667         u16 datalen;
668         struct rsn_pmksa_cache_entry *pmksa;
669
670         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
671             !hapd->conf->wps_state)
672                 return;
673
674         wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
675                    (unsigned long) len, MAC2STR(sa));
676         sta = ap_get_sta(hapd, sa);
677         if (!sta) {
678                 printf("   no station information available\n");
679                 return;
680         }
681
682         if (len < sizeof(*hdr)) {
683                 printf("   too short IEEE 802.1X packet\n");
684                 return;
685         }
686
687         hdr = (struct ieee802_1x_hdr *) buf;
688         datalen = be_to_host16(hdr->length);
689         wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
690                    hdr->version, hdr->type, datalen);
691
692         if (len - sizeof(*hdr) < datalen) {
693                 printf("   frame too short for this IEEE 802.1X packet\n");
694                 if (sta->eapol_sm)
695                         sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
696                 return;
697         }
698         if (len - sizeof(*hdr) > datalen) {
699                 wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
700                            "IEEE 802.1X packet",
701                            (unsigned long) len - sizeof(*hdr) - datalen);
702         }
703
704         if (sta->eapol_sm) {
705                 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
706                 sta->eapol_sm->dot1xAuthEapolFramesRx++;
707         }
708
709         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
710         if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
711             hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
712             (key->type == EAPOL_KEY_TYPE_WPA ||
713              key->type == EAPOL_KEY_TYPE_RSN)) {
714                 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
715                             sizeof(*hdr) + datalen);
716                 return;
717         }
718
719         if ((!hapd->conf->ieee802_1x &&
720              !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
721             wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
722                 return;
723
724         if (!sta->eapol_sm) {
725                 sta->eapol_sm = eapol_auth_alloc(hapd->eapol_auth, sta->addr,
726                                                  sta->flags & WLAN_STA_PREAUTH,
727                                                  sta);
728                 if (!sta->eapol_sm)
729                         return;
730
731 #ifdef CONFIG_WPS
732                 if (!hapd->conf->ieee802_1x &&
733                     ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
734                      WLAN_STA_MAYBE_WPS)) {
735                         /*
736                          * Delay EAPOL frame transmission until a possible WPS
737                          * STA initiates the handshake with EAPOL-Start.
738                          */
739                         sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
740                 }
741 #endif /* CONFIG_WPS */
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 &&
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 #ifndef CONFIG_NO_RADIUS
954         if (sm->last_recv_radius) {
955                 radius_msg_free(sm->last_recv_radius);
956                 os_free(sm->last_recv_radius);
957         }
958 #endif /* CONFIG_NO_RADIUS */
959
960         os_free(sm->identity);
961         ieee802_1x_free_radius_class(&sm->radius_class);
962         eapol_auth_free(sm);
963 }
964
965
966 #ifndef CONFIG_NO_RADIUS
967 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
968                                           struct sta_info *sta)
969 {
970         u8 *eap;
971         size_t len;
972         struct eap_hdr *hdr;
973         int eap_type = -1;
974         char buf[64];
975         struct radius_msg *msg;
976         struct eapol_state_machine *sm = sta->eapol_sm;
977
978         if (sm == NULL || sm->last_recv_radius == NULL) {
979                 if (sm)
980                         sm->eap_if->aaaEapNoReq = TRUE;
981                 return;
982         }
983
984         msg = sm->last_recv_radius;
985
986         eap = radius_msg_get_eap(msg, &len);
987         if (eap == NULL) {
988                 /* RFC 3579, Chap. 2.6.3:
989                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
990                  * attribute */
991                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
992                                HOSTAPD_LEVEL_WARNING, "could not extract "
993                                "EAP-Message from RADIUS message");
994                 sm->eap_if->aaaEapNoReq = TRUE;
995                 return;
996         }
997
998         if (len < sizeof(*hdr)) {
999                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1000                                HOSTAPD_LEVEL_WARNING, "too short EAP packet "
1001                                "received from authentication server");
1002                 os_free(eap);
1003                 sm->eap_if->aaaEapNoReq = TRUE;
1004                 return;
1005         }
1006
1007         if (len > sizeof(*hdr))
1008                 eap_type = eap[sizeof(*hdr)];
1009
1010         hdr = (struct eap_hdr *) eap;
1011         switch (hdr->code) {
1012         case EAP_CODE_REQUEST:
1013                 if (eap_type >= 0)
1014                         sm->eap_type_authsrv = eap_type;
1015                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1016                             eap_type >= 0 ? eap_type_text(eap_type) : "??",
1017                             eap_type);
1018                 break;
1019         case EAP_CODE_RESPONSE:
1020                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1021                             eap_type >= 0 ? eap_type_text(eap_type) : "??",
1022                             eap_type);
1023                 break;
1024         case EAP_CODE_SUCCESS:
1025                 os_strlcpy(buf, "EAP Success", sizeof(buf));
1026                 break;
1027         case EAP_CODE_FAILURE:
1028                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1029                 break;
1030         default:
1031                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1032                 break;
1033         }
1034         buf[sizeof(buf) - 1] = '\0';
1035         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1036                        HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1037                        "id=%d len=%d) from RADIUS server: %s",
1038                        hdr->code, hdr->identifier, be_to_host16(hdr->length),
1039                        buf);
1040         sm->eap_if->aaaEapReq = TRUE;
1041
1042         wpabuf_free(sm->eap_if->aaaEapReqData);
1043         sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1044 }
1045
1046
1047 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1048                                 struct sta_info *sta, struct radius_msg *msg,
1049                                 struct radius_msg *req,
1050                                 const u8 *shared_secret,
1051                                 size_t shared_secret_len)
1052 {
1053         struct radius_ms_mppe_keys *keys;
1054         struct eapol_state_machine *sm = sta->eapol_sm;
1055         if (sm == NULL)
1056                 return;
1057
1058         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1059                                       shared_secret_len);
1060
1061         if (keys && keys->send && keys->recv) {
1062                 size_t len = keys->send_len + keys->recv_len;
1063                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1064                                 keys->send, keys->send_len);
1065                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1066                                 keys->recv, keys->recv_len);
1067
1068                 os_free(sm->eap_if->aaaEapKeyData);
1069                 sm->eap_if->aaaEapKeyData = os_malloc(len);
1070                 if (sm->eap_if->aaaEapKeyData) {
1071                         os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1072                                   keys->recv_len);
1073                         os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1074                                   keys->send, keys->send_len);
1075                         sm->eap_if->aaaEapKeyDataLen = len;
1076                         sm->eap_if->aaaEapKeyAvailable = TRUE;
1077                 }
1078         }
1079
1080         if (keys) {
1081                 os_free(keys->send);
1082                 os_free(keys->recv);
1083                 os_free(keys);
1084         }
1085 }
1086
1087
1088 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1089                                           struct sta_info *sta,
1090                                           struct radius_msg *msg)
1091 {
1092         u8 *class;
1093         size_t class_len;
1094         struct eapol_state_machine *sm = sta->eapol_sm;
1095         int count, i;
1096         struct radius_attr_data *nclass;
1097         size_t nclass_count;
1098
1099         if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1100             sm == NULL)
1101                 return;
1102
1103         ieee802_1x_free_radius_class(&sm->radius_class);
1104         count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1105         if (count <= 0)
1106                 return;
1107
1108         nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1109         if (nclass == NULL)
1110                 return;
1111
1112         nclass_count = 0;
1113
1114         class = NULL;
1115         for (i = 0; i < count; i++) {
1116                 do {
1117                         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1118                                                     &class, &class_len,
1119                                                     class) < 0) {
1120                                 i = count;
1121                                 break;
1122                         }
1123                 } while (class_len < 1);
1124
1125                 nclass[nclass_count].data = os_malloc(class_len);
1126                 if (nclass[nclass_count].data == NULL)
1127                         break;
1128
1129                 os_memcpy(nclass[nclass_count].data, class, class_len);
1130                 nclass[nclass_count].len = class_len;
1131                 nclass_count++;
1132         }
1133
1134         sm->radius_class.attr = nclass;
1135         sm->radius_class.count = nclass_count;
1136         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1137                    "attributes for " MACSTR,
1138                    (unsigned long) sm->radius_class.count,
1139                    MAC2STR(sta->addr));
1140 }
1141
1142
1143 /* Update sta->identity based on User-Name attribute in Access-Accept */
1144 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1145                                            struct sta_info *sta,
1146                                            struct radius_msg *msg)
1147 {
1148         u8 *buf, *identity;
1149         size_t len;
1150         struct eapol_state_machine *sm = sta->eapol_sm;
1151
1152         if (sm == NULL)
1153                 return;
1154
1155         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1156                                     NULL) < 0)
1157                 return;
1158
1159         identity = os_malloc(len + 1);
1160         if (identity == NULL)
1161                 return;
1162
1163         os_memcpy(identity, buf, len);
1164         identity[len] = '\0';
1165
1166         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1167                        HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1168                        "User-Name from Access-Accept '%s'",
1169                        sm->identity ? (char *) sm->identity : "N/A",
1170                        (char *) identity);
1171
1172         os_free(sm->identity);
1173         sm->identity = identity;
1174         sm->identity_len = len;
1175 }
1176
1177
1178 struct sta_id_search {
1179         u8 identifier;
1180         struct eapol_state_machine *sm;
1181 };
1182
1183
1184 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1185                                                struct sta_info *sta,
1186                                                void *ctx)
1187 {
1188         struct sta_id_search *id_search = ctx;
1189         struct eapol_state_machine *sm = sta->eapol_sm;
1190
1191         if (sm && sm->radius_identifier >= 0 &&
1192             sm->radius_identifier == id_search->identifier) {
1193                 id_search->sm = sm;
1194                 return 1;
1195         }
1196         return 0;
1197 }
1198
1199
1200 static struct eapol_state_machine *
1201 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1202 {
1203         struct sta_id_search id_search;
1204         id_search.identifier = identifier;
1205         id_search.sm = NULL;
1206         ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1207         return id_search.sm;
1208 }
1209
1210
1211 /**
1212  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1213  * @msg: RADIUS response message
1214  * @req: RADIUS request message
1215  * @shared_secret: RADIUS shared secret
1216  * @shared_secret_len: Length of shared_secret in octets
1217  * @data: Context data (struct hostapd_data *)
1218  * Returns: Processing status
1219  */
1220 static RadiusRxResult
1221 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1222                         const u8 *shared_secret, size_t shared_secret_len,
1223                         void *data)
1224 {
1225         struct hostapd_data *hapd = data;
1226         struct sta_info *sta;
1227         u32 session_timeout = 0, termination_action, acct_interim_interval;
1228         int session_timeout_set, old_vlanid = 0;
1229         struct eapol_state_machine *sm;
1230         int override_eapReq = 0;
1231
1232         sm = ieee802_1x_search_radius_identifier(hapd, msg->hdr->identifier);
1233         if (sm == NULL) {
1234                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1235                            "station for this RADIUS message");
1236                 return RADIUS_RX_UNKNOWN;
1237         }
1238         sta = sm->sta;
1239
1240         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1241          * present when packet contains an EAP-Message attribute */
1242         if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1243             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1244                                 0) < 0 &&
1245             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1246                 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1247                            "Message-Authenticator since it does not include "
1248                            "EAP-Message");
1249         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1250                                      req, 1)) {
1251                 printf("Incoming RADIUS packet did not have correct "
1252                        "Message-Authenticator - dropped\n");
1253                 return RADIUS_RX_INVALID_AUTHENTICATOR;
1254         }
1255
1256         if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1257             msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1258             msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1259                 printf("Unknown RADIUS message code\n");
1260                 return RADIUS_RX_UNKNOWN;
1261         }
1262
1263         sm->radius_identifier = -1;
1264         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1265                    MAC2STR(sta->addr));
1266
1267         if (sm->last_recv_radius) {
1268                 radius_msg_free(sm->last_recv_radius);
1269                 os_free(sm->last_recv_radius);
1270         }
1271
1272         sm->last_recv_radius = msg;
1273
1274         session_timeout_set =
1275                 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1276                                            &session_timeout);
1277         if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1278                                       &termination_action))
1279                 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1280
1281         if (hapd->conf->radius->acct_interim_interval == 0 &&
1282             msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1283             radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1284                                       &acct_interim_interval) == 0) {
1285                 if (acct_interim_interval < 60) {
1286                         hostapd_logger(hapd, sta->addr,
1287                                        HOSTAPD_MODULE_IEEE8021X,
1288                                        HOSTAPD_LEVEL_INFO,
1289                                        "ignored too small "
1290                                        "Acct-Interim-Interval %d",
1291                                        acct_interim_interval);
1292                 } else
1293                         sta->acct_interim_interval = acct_interim_interval;
1294         }
1295
1296
1297         switch (msg->hdr->code) {
1298         case RADIUS_CODE_ACCESS_ACCEPT:
1299                 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1300                         sta->vlan_id = 0;
1301                 else {
1302                         old_vlanid = sta->vlan_id;
1303                         sta->vlan_id = radius_msg_get_vlanid(msg);
1304                 }
1305                 if (sta->vlan_id > 0 &&
1306                     hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1307                                                sta->vlan_id)) {
1308                         hostapd_logger(hapd, sta->addr,
1309                                        HOSTAPD_MODULE_RADIUS,
1310                                        HOSTAPD_LEVEL_INFO,
1311                                        "VLAN ID %d", sta->vlan_id);
1312                 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1313                         sta->eapol_sm->authFail = TRUE;
1314                         hostapd_logger(hapd, sta->addr,
1315                                        HOSTAPD_MODULE_IEEE8021X,
1316                                        HOSTAPD_LEVEL_INFO, "authentication "
1317                                        "server did not include required VLAN "
1318                                        "ID in Access-Accept");
1319                         break;
1320                 }
1321
1322                 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1323
1324                 /* RFC 3580, Ch. 3.17 */
1325                 if (session_timeout_set && termination_action ==
1326                     RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1327                         sm->reAuthPeriod = session_timeout;
1328                 } else if (session_timeout_set)
1329                         ap_sta_session_timeout(hapd, sta, session_timeout);
1330
1331                 sm->eap_if->aaaSuccess = TRUE;
1332                 override_eapReq = 1;
1333                 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1334                                     shared_secret_len);
1335                 ieee802_1x_store_radius_class(hapd, sta, msg);
1336                 ieee802_1x_update_sta_identity(hapd, sta, msg);
1337                 if (sm->eap_if->eapKeyAvailable &&
1338                     wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1339                                        session_timeout_set ?
1340                                        (int) session_timeout : -1, sm) == 0) {
1341                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1342                                        HOSTAPD_LEVEL_DEBUG,
1343                                        "Added PMKSA cache entry");
1344                 }
1345                 break;
1346         case RADIUS_CODE_ACCESS_REJECT:
1347                 sm->eap_if->aaaFail = TRUE;
1348                 override_eapReq = 1;
1349                 break;
1350         case RADIUS_CODE_ACCESS_CHALLENGE:
1351                 sm->eap_if->aaaEapReq = TRUE;
1352                 if (session_timeout_set) {
1353                         /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1354                         sm->eap_if->aaaMethodTimeout = session_timeout;
1355                         hostapd_logger(hapd, sm->addr,
1356                                        HOSTAPD_MODULE_IEEE8021X,
1357                                        HOSTAPD_LEVEL_DEBUG,
1358                                        "using EAP timeout of %d seconds (from "
1359                                        "RADIUS)",
1360                                        sm->eap_if->aaaMethodTimeout);
1361                 } else {
1362                         /*
1363                          * Use dynamic retransmission behavior per EAP
1364                          * specification.
1365                          */
1366                         sm->eap_if->aaaMethodTimeout = 0;
1367                 }
1368                 break;
1369         }
1370
1371         ieee802_1x_decapsulate_radius(hapd, sta);
1372         if (override_eapReq)
1373                 sm->eap_if->aaaEapReq = FALSE;
1374
1375         eapol_auth_step(sm);
1376
1377         return RADIUS_RX_QUEUED;
1378 }
1379 #endif /* CONFIG_NO_RADIUS */
1380
1381
1382 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1383 {
1384         struct eapol_state_machine *sm = sta->eapol_sm;
1385         if (sm == NULL)
1386                 return;
1387
1388         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1389                        HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1390
1391 #ifndef CONFIG_NO_RADIUS
1392         if (sm->last_recv_radius) {
1393                 radius_msg_free(sm->last_recv_radius);
1394                 os_free(sm->last_recv_radius);
1395                 sm->last_recv_radius = NULL;
1396         }
1397 #endif /* CONFIG_NO_RADIUS */
1398
1399         if (sm->eap_if->eapTimeout) {
1400                 /*
1401                  * Disconnect the STA since it did not reply to the last EAP
1402                  * request and we cannot continue EAP processing (EAP-Failure
1403                  * could only be sent if the EAP peer actually replied).
1404                  */
1405                 sm->eap_if->portEnabled = FALSE;
1406                 hostapd_sta_deauth(hapd, sta->addr,
1407                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
1408                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1409                                 WLAN_STA_AUTHORIZED);
1410                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1411                 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1412                 sta->timeout_next = STA_REMOVE;
1413         }
1414 }
1415
1416
1417 #ifdef HOSTAPD_DUMP_STATE
1418 static void fprint_char(FILE *f, char c)
1419 {
1420         if (c >= 32 && c < 127)
1421                 fprintf(f, "%c", c);
1422         else
1423                 fprintf(f, "<%02x>", c);
1424 }
1425
1426
1427 void ieee802_1x_dump_state(FILE *f, const char *prefix, struct sta_info *sta)
1428 {
1429         struct eapol_state_machine *sm = sta->eapol_sm;
1430         if (sm == NULL)
1431                 return;
1432
1433         fprintf(f, "%sIEEE 802.1X:\n", prefix);
1434
1435         if (sm->identity) {
1436                 size_t i;
1437                 fprintf(f, "%sidentity=", prefix);
1438                 for (i = 0; i < sm->identity_len; i++)
1439                         fprint_char(f, sm->identity[i]);
1440                 fprintf(f, "\n");
1441         }
1442
1443         fprintf(f, "%slast EAP type: Authentication Server: %d (%s) "
1444                 "Supplicant: %d (%s)\n", prefix,
1445                 sm->eap_type_authsrv, eap_type_text(sm->eap_type_authsrv),
1446                 sm->eap_type_supp, eap_type_text(sm->eap_type_supp));
1447
1448         fprintf(f, "%scached_packets=%s\n", prefix,
1449                 sm->last_recv_radius ? "[RX RADIUS]" : "");
1450
1451         eapol_auth_dump_state(f, prefix, sm);
1452 }
1453 #endif /* HOSTAPD_DUMP_STATE */
1454
1455
1456 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1457 {
1458         if (hapd->conf->default_wep_key_len < 1)
1459                 return 0;
1460
1461         os_free(hapd->default_wep_key);
1462         hapd->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1463         if (hapd->default_wep_key == NULL ||
1464             os_get_random(hapd->default_wep_key,
1465                           hapd->conf->default_wep_key_len)) {
1466                 printf("Could not generate random WEP key.\n");
1467                 os_free(hapd->default_wep_key);
1468                 hapd->default_wep_key = NULL;
1469                 return -1;
1470         }
1471
1472         wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1473                         hapd->default_wep_key,
1474                         hapd->conf->default_wep_key_len);
1475
1476         return 0;
1477 }
1478
1479
1480 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1481                                         struct sta_info *sta, void *ctx)
1482 {
1483         if (sta->eapol_sm) {
1484                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1485                 eapol_auth_step(sta->eapol_sm);
1486         }
1487         return 0;
1488 }
1489
1490
1491 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1492 {
1493         struct hostapd_data *hapd = eloop_ctx;
1494
1495         if (hapd->default_wep_key_idx >= 3)
1496                 hapd->default_wep_key_idx =
1497                         hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1498         else
1499                 hapd->default_wep_key_idx++;
1500
1501         wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1502                    hapd->default_wep_key_idx);
1503                       
1504         if (ieee802_1x_rekey_broadcast(hapd)) {
1505                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1506                                HOSTAPD_LEVEL_WARNING, "failed to generate a "
1507                                "new broadcast key");
1508                 os_free(hapd->default_wep_key);
1509                 hapd->default_wep_key = NULL;
1510                 return;
1511         }
1512
1513         /* TODO: Could setup key for RX here, but change default TX keyid only
1514          * after new broadcast key has been sent to all stations. */
1515         if (hostapd_set_encryption(hapd->conf->iface, hapd, "WEP", NULL,
1516                                    hapd->default_wep_key_idx,
1517                                    hapd->default_wep_key,
1518                                    hapd->conf->default_wep_key_len, 1)) {
1519                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1520                                HOSTAPD_LEVEL_WARNING, "failed to configure a "
1521                                "new broadcast key");
1522                 os_free(hapd->default_wep_key);
1523                 hapd->default_wep_key = NULL;
1524                 return;
1525         }
1526
1527         ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1528
1529         if (hapd->conf->wep_rekeying_period > 0) {
1530                 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1531                                        ieee802_1x_rekey, hapd, NULL);
1532         }
1533 }
1534
1535
1536 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1537                                   const u8 *data, size_t datalen)
1538 {
1539         ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1540 }
1541
1542
1543 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1544                                 const u8 *data, size_t datalen)
1545 {
1546 #ifndef CONFIG_NO_RADIUS
1547         struct hostapd_data *hapd = ctx;
1548         struct sta_info *sta = sta_ctx;
1549
1550         ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1551 #endif /* CONFIG_NO_RADIUS */
1552 }
1553
1554
1555 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1556                                  int preauth)
1557 {
1558         struct hostapd_data *hapd = ctx;
1559         struct sta_info *sta = sta_ctx;
1560         if (preauth)
1561                 rsn_preauth_finished(hapd, sta, success);
1562         else
1563                 ieee802_1x_finished(hapd, sta, success);
1564 }
1565
1566
1567 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1568                                    size_t identity_len, int phase2,
1569                                    struct eap_user *user)
1570 {
1571         struct hostapd_data *hapd = ctx;
1572         const struct hostapd_eap_user *eap_user;
1573         int i, count;
1574
1575         eap_user = hostapd_get_eap_user(hapd->conf, identity,
1576                                         identity_len, phase2);
1577         if (eap_user == NULL)
1578                 return -1;
1579
1580         os_memset(user, 0, sizeof(*user));
1581         user->phase2 = phase2;
1582         count = EAP_USER_MAX_METHODS;
1583         if (count > EAP_MAX_METHODS)
1584                 count = EAP_MAX_METHODS;
1585         for (i = 0; i < count; i++) {
1586                 user->methods[i].vendor = eap_user->methods[i].vendor;
1587                 user->methods[i].method = eap_user->methods[i].method;
1588         }
1589
1590         if (eap_user->password) {
1591                 user->password = os_malloc(eap_user->password_len);
1592                 if (user->password == NULL)
1593                         return -1;
1594                 os_memcpy(user->password, eap_user->password,
1595                           eap_user->password_len);
1596                 user->password_len = eap_user->password_len;
1597         }
1598         user->force_version = eap_user->force_version;
1599         user->ttls_auth = eap_user->ttls_auth;
1600
1601         return 0;
1602 }
1603
1604
1605 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1606 {
1607         struct hostapd_data *hapd = ctx;
1608         struct sta_info *sta;
1609         sta = ap_get_sta(hapd, addr);
1610         if (sta == NULL || sta->eapol_sm == NULL)
1611                 return 0;
1612         return 1;
1613 }
1614
1615
1616 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1617                               eapol_logger_level level, const char *txt)
1618 {
1619         struct hostapd_data *hapd = ctx;
1620         int hlevel;
1621
1622         switch (level) {
1623         case EAPOL_LOGGER_WARNING:
1624                 hlevel = HOSTAPD_LEVEL_WARNING;
1625                 break;
1626         case EAPOL_LOGGER_INFO:
1627                 hlevel = HOSTAPD_LEVEL_INFO;
1628                 break;
1629         case EAPOL_LOGGER_DEBUG:
1630         default:
1631                 hlevel = HOSTAPD_LEVEL_DEBUG;
1632                 break;
1633         }
1634
1635         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1636                        txt);
1637 }
1638
1639
1640 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1641                                            int authorized)
1642 {
1643         struct hostapd_data *hapd = ctx;
1644         struct sta_info *sta = sta_ctx;
1645         ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1646 }
1647
1648
1649 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1650 {
1651         struct hostapd_data *hapd = ctx;
1652         struct sta_info *sta = sta_ctx;
1653         ieee802_1x_abort_auth(hapd, sta);
1654 }
1655
1656
1657 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1658 {
1659         struct hostapd_data *hapd = ctx;
1660         struct sta_info *sta = sta_ctx;
1661         ieee802_1x_tx_key(hapd, sta);
1662 }
1663
1664
1665 int ieee802_1x_init(struct hostapd_data *hapd)
1666 {
1667         int i;
1668         struct eapol_auth_config conf;
1669         struct eapol_auth_cb cb;
1670
1671         os_memset(&conf, 0, sizeof(conf));
1672         conf.hapd = hapd;
1673         conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1674         conf.wpa = hapd->conf->wpa;
1675         conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1676         conf.eap_server = hapd->conf->eap_server;
1677         conf.ssl_ctx = hapd->ssl_ctx;
1678         conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1679         conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1680         conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1681         conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1682         conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1683         conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1684         conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1685         conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1686         conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1687         conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1688         conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1689         conf.tnc = hapd->conf->tnc;
1690         conf.wps = hapd->wps;
1691
1692         os_memset(&cb, 0, sizeof(cb));
1693         cb.eapol_send = ieee802_1x_eapol_send;
1694         cb.aaa_send = ieee802_1x_aaa_send;
1695         cb.finished = _ieee802_1x_finished;
1696         cb.get_eap_user = ieee802_1x_get_eap_user;
1697         cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1698         cb.logger = ieee802_1x_logger;
1699         cb.set_port_authorized = ieee802_1x_set_port_authorized;
1700         cb.abort_auth = _ieee802_1x_abort_auth;
1701         cb.tx_key = _ieee802_1x_tx_key;
1702
1703         hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1704         if (hapd->eapol_auth == NULL)
1705                 return -1;
1706
1707         if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1708             hostapd_set_ieee8021x(hapd->conf->iface, hapd, 1))
1709                 return -1;
1710
1711 #ifndef CONFIG_NO_RADIUS
1712         if (radius_client_register(hapd->radius, RADIUS_AUTH,
1713                                    ieee802_1x_receive_auth, hapd))
1714                 return -1;
1715 #endif /* CONFIG_NO_RADIUS */
1716
1717         if (hapd->conf->default_wep_key_len) {
1718                 hostapd_set_privacy(hapd, 1);
1719
1720                 for (i = 0; i < 4; i++)
1721                         hostapd_set_encryption(hapd->conf->iface, hapd,
1722                                                "none", NULL, i, NULL, 0, 0);
1723
1724                 ieee802_1x_rekey(hapd, NULL);
1725
1726                 if (hapd->default_wep_key == NULL)
1727                         return -1;
1728         }
1729
1730         return 0;
1731 }
1732
1733
1734 void ieee802_1x_deinit(struct hostapd_data *hapd)
1735 {
1736         eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1737
1738         if (hapd->driver != NULL &&
1739             (hapd->conf->ieee802_1x || hapd->conf->wpa))
1740                 hostapd_set_ieee8021x(hapd->conf->iface, hapd, 0);
1741
1742         eapol_auth_deinit(hapd->eapol_auth);
1743         hapd->eapol_auth = NULL;
1744 }
1745
1746
1747 int ieee802_1x_reconfig(struct hostapd_data *hapd, 
1748                         struct hostapd_config *oldconf,
1749                         struct hostapd_bss_config *oldbss)
1750 {
1751         ieee802_1x_deinit(hapd);
1752         return ieee802_1x_init(hapd);
1753 }
1754
1755
1756 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1757                          const u8 *buf, size_t len, int ack)
1758 {
1759         struct ieee80211_hdr *hdr;
1760         struct ieee802_1x_hdr *xhdr;
1761         struct ieee802_1x_eapol_key *key;
1762         u8 *pos;
1763         const unsigned char rfc1042_hdr[ETH_ALEN] =
1764                 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1765
1766         if (sta == NULL)
1767                 return -1;
1768         if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1769                 return 0;
1770
1771         hdr = (struct ieee80211_hdr *) buf;
1772         pos = (u8 *) (hdr + 1);
1773         if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1774                 return 0;
1775         pos += sizeof(rfc1042_hdr);
1776         if (WPA_GET_BE16(pos) != ETH_P_PAE)
1777                 return 0;
1778         pos += 2;
1779
1780         xhdr = (struct ieee802_1x_hdr *) pos;
1781         pos += sizeof(*xhdr);
1782
1783         wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1784                    "type=%d length=%d - ack=%d",
1785                    MAC2STR(sta->addr), xhdr->version, xhdr->type,
1786                    be_to_host16(xhdr->length), ack);
1787
1788         /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1789          * or Authenticator state machines, but EAPOL-Key packets are not
1790          * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1791          * packets couple of times because otherwise STA keys become
1792          * unsynchronized with AP. */
1793         if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1794             pos + sizeof(*key) <= buf + len) {
1795                 key = (struct ieee802_1x_eapol_key *) pos;
1796                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1797                                HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1798                                "frame (%scast index=%d)",
1799                                key->key_index & BIT(7) ? "uni" : "broad",
1800                                key->key_index & ~BIT(7));
1801                 /* TODO: re-send EAPOL-Key couple of times (with short delay
1802                  * between them?). If all attempt fail, report error and
1803                  * deauthenticate STA so that it will get new keys when
1804                  * authenticating again (e.g., after returning in range).
1805                  * Separate limit/transmit state needed both for unicast and
1806                  * broadcast keys(?) */
1807         }
1808         /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1809          * to here and change the key only if the EAPOL-Key packet was Acked.
1810          */
1811
1812         return 1;
1813 }
1814
1815
1816 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1817 {
1818         if (sm == NULL || sm->identity == NULL)
1819                 return NULL;
1820
1821         *len = sm->identity_len;
1822         return sm->identity;
1823 }
1824
1825
1826 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1827                                  int idx)
1828 {
1829         if (sm == NULL || sm->radius_class.attr == NULL ||
1830             idx >= (int) sm->radius_class.count)
1831                 return NULL;
1832
1833         *len = sm->radius_class.attr[idx].len;
1834         return sm->radius_class.attr[idx].data;
1835 }
1836
1837
1838 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1839 {
1840         if (sm == NULL)
1841                 return NULL;
1842
1843         *len = sm->eap_if->eapKeyDataLen;
1844         return sm->eap_if->eapKeyData;
1845 }
1846
1847
1848 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1849                                     int enabled)
1850 {
1851         if (sm == NULL)
1852                 return;
1853         sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1854         eapol_auth_step(sm);
1855 }
1856
1857
1858 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1859                                   int valid)
1860 {
1861         if (sm == NULL)
1862                 return;
1863         sm->portValid = valid ? TRUE : FALSE;
1864         eapol_auth_step(sm);
1865 }
1866
1867
1868 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1869 {
1870         if (sm == NULL)
1871                 return;
1872         if (pre_auth)
1873                 sm->flags |= EAPOL_SM_PREAUTH;
1874         else
1875                 sm->flags &= ~EAPOL_SM_PREAUTH;
1876 }
1877
1878
1879 static const char * bool_txt(Boolean bool)
1880 {
1881         return bool ? "TRUE" : "FALSE";
1882 }
1883
1884
1885 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1886 {
1887         /* TODO */
1888         return 0;
1889 }
1890
1891
1892 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1893                            char *buf, size_t buflen)
1894 {
1895         int len = 0, ret;
1896         struct eapol_state_machine *sm = sta->eapol_sm;
1897
1898         if (sm == NULL)
1899                 return 0;
1900
1901         ret = os_snprintf(buf + len, buflen - len,
1902                           "dot1xPaePortNumber=%d\n"
1903                           "dot1xPaePortProtocolVersion=%d\n"
1904                           "dot1xPaePortCapabilities=1\n"
1905                           "dot1xPaePortInitialize=%d\n"
1906                           "dot1xPaePortReauthenticate=FALSE\n",
1907                           sta->aid,
1908                           EAPOL_VERSION,
1909                           sm->initialize);
1910         if (ret < 0 || (size_t) ret >= buflen - len)
1911                 return len;
1912         len += ret;
1913
1914         /* dot1xAuthConfigTable */
1915         ret = os_snprintf(buf + len, buflen - len,
1916                           "dot1xAuthPaeState=%d\n"
1917                           "dot1xAuthBackendAuthState=%d\n"
1918                           "dot1xAuthAdminControlledDirections=%d\n"
1919                           "dot1xAuthOperControlledDirections=%d\n"
1920                           "dot1xAuthAuthControlledPortStatus=%d\n"
1921                           "dot1xAuthAuthControlledPortControl=%d\n"
1922                           "dot1xAuthQuietPeriod=%u\n"
1923                           "dot1xAuthServerTimeout=%u\n"
1924                           "dot1xAuthReAuthPeriod=%u\n"
1925                           "dot1xAuthReAuthEnabled=%s\n"
1926                           "dot1xAuthKeyTxEnabled=%s\n",
1927                           sm->auth_pae_state + 1,
1928                           sm->be_auth_state + 1,
1929                           sm->adminControlledDirections,
1930                           sm->operControlledDirections,
1931                           sm->authPortStatus,
1932                           sm->portControl,
1933                           sm->quietPeriod,
1934                           sm->serverTimeout,
1935                           sm->reAuthPeriod,
1936                           bool_txt(sm->reAuthEnabled),
1937                           bool_txt(sm->keyTxEnabled));
1938         if (ret < 0 || (size_t) ret >= buflen - len)
1939                 return len;
1940         len += ret;
1941
1942         /* dot1xAuthStatsTable */
1943         ret = os_snprintf(buf + len, buflen - len,
1944                           "dot1xAuthEapolFramesRx=%u\n"
1945                           "dot1xAuthEapolFramesTx=%u\n"
1946                           "dot1xAuthEapolStartFramesRx=%u\n"
1947                           "dot1xAuthEapolLogoffFramesRx=%u\n"
1948                           "dot1xAuthEapolRespIdFramesRx=%u\n"
1949                           "dot1xAuthEapolRespFramesRx=%u\n"
1950                           "dot1xAuthEapolReqIdFramesTx=%u\n"
1951                           "dot1xAuthEapolReqFramesTx=%u\n"
1952                           "dot1xAuthInvalidEapolFramesRx=%u\n"
1953                           "dot1xAuthEapLengthErrorFramesRx=%u\n"
1954                           "dot1xAuthLastEapolFrameVersion=%u\n"
1955                           "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1956                           sm->dot1xAuthEapolFramesRx,
1957                           sm->dot1xAuthEapolFramesTx,
1958                           sm->dot1xAuthEapolStartFramesRx,
1959                           sm->dot1xAuthEapolLogoffFramesRx,
1960                           sm->dot1xAuthEapolRespIdFramesRx,
1961                           sm->dot1xAuthEapolRespFramesRx,
1962                           sm->dot1xAuthEapolReqIdFramesTx,
1963                           sm->dot1xAuthEapolReqFramesTx,
1964                           sm->dot1xAuthInvalidEapolFramesRx,
1965                           sm->dot1xAuthEapLengthErrorFramesRx,
1966                           sm->dot1xAuthLastEapolFrameVersion,
1967                           MAC2STR(sm->addr));
1968         if (ret < 0 || (size_t) ret >= buflen - len)
1969                 return len;
1970         len += ret;
1971
1972         /* dot1xAuthDiagTable */
1973         ret = os_snprintf(buf + len, buflen - len,
1974                           "dot1xAuthEntersConnecting=%u\n"
1975                           "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1976                           "dot1xAuthEntersAuthenticating=%u\n"
1977                           "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1978                           "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1979                           "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1980                           "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1981                           "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1982                           "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1983                           "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1984                           "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1985                           "dot1xAuthBackendResponses=%u\n"
1986                           "dot1xAuthBackendAccessChallenges=%u\n"
1987                           "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1988                           "dot1xAuthBackendAuthSuccesses=%u\n"
1989                           "dot1xAuthBackendAuthFails=%u\n",
1990                           sm->authEntersConnecting,
1991                           sm->authEapLogoffsWhileConnecting,
1992                           sm->authEntersAuthenticating,
1993                           sm->authAuthSuccessesWhileAuthenticating,
1994                           sm->authAuthTimeoutsWhileAuthenticating,
1995                           sm->authAuthFailWhileAuthenticating,
1996                           sm->authAuthEapStartsWhileAuthenticating,
1997                           sm->authAuthEapLogoffWhileAuthenticating,
1998                           sm->authAuthReauthsWhileAuthenticated,
1999                           sm->authAuthEapStartsWhileAuthenticated,
2000                           sm->authAuthEapLogoffWhileAuthenticated,
2001                           sm->backendResponses,
2002                           sm->backendAccessChallenges,
2003                           sm->backendOtherRequestsToSupplicant,
2004                           sm->backendAuthSuccesses,
2005                           sm->backendAuthFails);
2006         if (ret < 0 || (size_t) ret >= buflen - len)
2007                 return len;
2008         len += ret;
2009
2010         /* dot1xAuthSessionStatsTable */
2011         ret = os_snprintf(buf + len, buflen - len,
2012                           /* TODO: dot1xAuthSessionOctetsRx */
2013                           /* TODO: dot1xAuthSessionOctetsTx */
2014                           /* TODO: dot1xAuthSessionFramesRx */
2015                           /* TODO: dot1xAuthSessionFramesTx */
2016                           "dot1xAuthSessionId=%08X-%08X\n"
2017                           "dot1xAuthSessionAuthenticMethod=%d\n"
2018                           "dot1xAuthSessionTime=%u\n"
2019                           "dot1xAuthSessionTerminateCause=999\n"
2020                           "dot1xAuthSessionUserName=%s\n",
2021                           sta->acct_session_id_hi, sta->acct_session_id_lo,
2022                           (wpa_key_mgmt_wpa_ieee8021x(
2023                                    wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2024                           1 : 2,
2025                           (unsigned int) (time(NULL) -
2026                                           sta->acct_session_start),
2027                           sm->identity);
2028         if (ret < 0 || (size_t) ret >= buflen - len)
2029                 return len;
2030         len += ret;
2031
2032         return len;
2033 }
2034
2035
2036 static void ieee802_1x_finished(struct hostapd_data *hapd,
2037                                 struct sta_info *sta, int success)
2038 {
2039         const u8 *key;
2040         size_t len;
2041         /* TODO: get PMKLifetime from WPA parameters */
2042         static const int dot11RSNAConfigPMKLifetime = 43200;
2043
2044         key = ieee802_1x_get_key(sta->eapol_sm, &len);
2045         if (success && key && len >= PMK_LEN &&
2046             wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
2047                                sta->eapol_sm) == 0) {
2048                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2049                                HOSTAPD_LEVEL_DEBUG,
2050                                "Added PMKSA cache entry (IEEE 802.1X)");
2051         }
2052 }