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