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