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