WPS: Add option for forcing Registrar to use PSK format in Credential
[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 &&
547             radius_msg_get_hdr(sm->last_recv_radius)->code ==
548             RADIUS_CODE_ACCESS_CHALLENGE) {
549                 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
550                                                RADIUS_ATTR_STATE);
551                 if (res < 0) {
552                         printf("Could not copy State attribute from previous "
553                                "Access-Challenge\n");
554                         goto fail;
555                 }
556                 if (res > 0) {
557                         wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
558                 }
559         }
560
561         radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
562         return;
563
564  fail:
565         radius_msg_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         radius_msg_free(sm->last_recv_radius);
930         radius_free_class(&sm->radius_class);
931 #endif /* CONFIG_NO_RADIUS */
932
933         os_free(sm->identity);
934         eapol_auth_free(sm);
935 }
936
937
938 #ifndef CONFIG_NO_RADIUS
939 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
940                                           struct sta_info *sta)
941 {
942         u8 *eap;
943         size_t len;
944         struct eap_hdr *hdr;
945         int eap_type = -1;
946         char buf[64];
947         struct radius_msg *msg;
948         struct eapol_state_machine *sm = sta->eapol_sm;
949
950         if (sm == NULL || sm->last_recv_radius == NULL) {
951                 if (sm)
952                         sm->eap_if->aaaEapNoReq = TRUE;
953                 return;
954         }
955
956         msg = sm->last_recv_radius;
957
958         eap = radius_msg_get_eap(msg, &len);
959         if (eap == NULL) {
960                 /* RFC 3579, Chap. 2.6.3:
961                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
962                  * attribute */
963                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
964                                HOSTAPD_LEVEL_WARNING, "could not extract "
965                                "EAP-Message from RADIUS message");
966                 sm->eap_if->aaaEapNoReq = TRUE;
967                 return;
968         }
969
970         if (len < sizeof(*hdr)) {
971                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
972                                HOSTAPD_LEVEL_WARNING, "too short EAP packet "
973                                "received from authentication server");
974                 os_free(eap);
975                 sm->eap_if->aaaEapNoReq = TRUE;
976                 return;
977         }
978
979         if (len > sizeof(*hdr))
980                 eap_type = eap[sizeof(*hdr)];
981
982         hdr = (struct eap_hdr *) eap;
983         switch (hdr->code) {
984         case EAP_CODE_REQUEST:
985                 if (eap_type >= 0)
986                         sm->eap_type_authsrv = eap_type;
987                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
988                             eap_type >= 0 ? eap_server_get_name(0, eap_type) :
989                             "??",
990                             eap_type);
991                 break;
992         case EAP_CODE_RESPONSE:
993                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
994                             eap_type >= 0 ? eap_server_get_name(0, eap_type) :
995                             "??",
996                             eap_type);
997                 break;
998         case EAP_CODE_SUCCESS:
999                 os_strlcpy(buf, "EAP Success", sizeof(buf));
1000                 break;
1001         case EAP_CODE_FAILURE:
1002                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
1003                 break;
1004         default:
1005                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1006                 break;
1007         }
1008         buf[sizeof(buf) - 1] = '\0';
1009         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1010                        HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
1011                        "id=%d len=%d) from RADIUS server: %s",
1012                        hdr->code, hdr->identifier, be_to_host16(hdr->length),
1013                        buf);
1014         sm->eap_if->aaaEapReq = TRUE;
1015
1016         wpabuf_free(sm->eap_if->aaaEapReqData);
1017         sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
1018 }
1019
1020
1021 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1022                                 struct sta_info *sta, struct radius_msg *msg,
1023                                 struct radius_msg *req,
1024                                 const u8 *shared_secret,
1025                                 size_t shared_secret_len)
1026 {
1027         struct radius_ms_mppe_keys *keys;
1028         struct eapol_state_machine *sm = sta->eapol_sm;
1029         if (sm == NULL)
1030                 return;
1031
1032         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1033                                       shared_secret_len);
1034
1035         if (keys && keys->send && keys->recv) {
1036                 size_t len = keys->send_len + keys->recv_len;
1037                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1038                                 keys->send, keys->send_len);
1039                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1040                                 keys->recv, keys->recv_len);
1041
1042                 os_free(sm->eap_if->aaaEapKeyData);
1043                 sm->eap_if->aaaEapKeyData = os_malloc(len);
1044                 if (sm->eap_if->aaaEapKeyData) {
1045                         os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1046                                   keys->recv_len);
1047                         os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1048                                   keys->send, keys->send_len);
1049                         sm->eap_if->aaaEapKeyDataLen = len;
1050                         sm->eap_if->aaaEapKeyAvailable = TRUE;
1051                 }
1052         }
1053
1054         if (keys) {
1055                 os_free(keys->send);
1056                 os_free(keys->recv);
1057                 os_free(keys);
1058         }
1059 }
1060
1061
1062 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1063                                           struct sta_info *sta,
1064                                           struct radius_msg *msg)
1065 {
1066         u8 *class;
1067         size_t class_len;
1068         struct eapol_state_machine *sm = sta->eapol_sm;
1069         int count, i;
1070         struct radius_attr_data *nclass;
1071         size_t nclass_count;
1072
1073         if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
1074             sm == NULL)
1075                 return;
1076
1077         radius_free_class(&sm->radius_class);
1078         count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1079         if (count <= 0)
1080                 return;
1081
1082         nclass = os_zalloc(count * sizeof(struct radius_attr_data));
1083         if (nclass == NULL)
1084                 return;
1085
1086         nclass_count = 0;
1087
1088         class = NULL;
1089         for (i = 0; i < count; i++) {
1090                 do {
1091                         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1092                                                     &class, &class_len,
1093                                                     class) < 0) {
1094                                 i = count;
1095                                 break;
1096                         }
1097                 } while (class_len < 1);
1098
1099                 nclass[nclass_count].data = os_malloc(class_len);
1100                 if (nclass[nclass_count].data == NULL)
1101                         break;
1102
1103                 os_memcpy(nclass[nclass_count].data, class, class_len);
1104                 nclass[nclass_count].len = class_len;
1105                 nclass_count++;
1106         }
1107
1108         sm->radius_class.attr = nclass;
1109         sm->radius_class.count = nclass_count;
1110         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
1111                    "attributes for " MACSTR,
1112                    (unsigned long) sm->radius_class.count,
1113                    MAC2STR(sta->addr));
1114 }
1115
1116
1117 /* Update sta->identity based on User-Name attribute in Access-Accept */
1118 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1119                                            struct sta_info *sta,
1120                                            struct radius_msg *msg)
1121 {
1122         u8 *buf, *identity;
1123         size_t len;
1124         struct eapol_state_machine *sm = sta->eapol_sm;
1125
1126         if (sm == NULL)
1127                 return;
1128
1129         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1130                                     NULL) < 0)
1131                 return;
1132
1133         identity = os_malloc(len + 1);
1134         if (identity == NULL)
1135                 return;
1136
1137         os_memcpy(identity, buf, len);
1138         identity[len] = '\0';
1139
1140         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1141                        HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
1142                        "User-Name from Access-Accept '%s'",
1143                        sm->identity ? (char *) sm->identity : "N/A",
1144                        (char *) identity);
1145
1146         os_free(sm->identity);
1147         sm->identity = identity;
1148         sm->identity_len = len;
1149 }
1150
1151
1152 struct sta_id_search {
1153         u8 identifier;
1154         struct eapol_state_machine *sm;
1155 };
1156
1157
1158 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1159                                                struct sta_info *sta,
1160                                                void *ctx)
1161 {
1162         struct sta_id_search *id_search = ctx;
1163         struct eapol_state_machine *sm = sta->eapol_sm;
1164
1165         if (sm && sm->radius_identifier >= 0 &&
1166             sm->radius_identifier == id_search->identifier) {
1167                 id_search->sm = sm;
1168                 return 1;
1169         }
1170         return 0;
1171 }
1172
1173
1174 static struct eapol_state_machine *
1175 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1176 {
1177         struct sta_id_search id_search;
1178         id_search.identifier = identifier;
1179         id_search.sm = NULL;
1180         ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1181         return id_search.sm;
1182 }
1183
1184
1185 /**
1186  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
1187  * @msg: RADIUS response message
1188  * @req: RADIUS request message
1189  * @shared_secret: RADIUS shared secret
1190  * @shared_secret_len: Length of shared_secret in octets
1191  * @data: Context data (struct hostapd_data *)
1192  * Returns: Processing status
1193  */
1194 static RadiusRxResult
1195 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
1196                         const u8 *shared_secret, size_t shared_secret_len,
1197                         void *data)
1198 {
1199         struct hostapd_data *hapd = data;
1200         struct sta_info *sta;
1201         u32 session_timeout = 0, termination_action, acct_interim_interval;
1202         int session_timeout_set, old_vlanid = 0;
1203         struct eapol_state_machine *sm;
1204         int override_eapReq = 0;
1205         struct radius_hdr *hdr = radius_msg_get_hdr(msg);
1206
1207         sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
1208         if (sm == NULL) {
1209                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
1210                            "station for this RADIUS message");
1211                 return RADIUS_RX_UNKNOWN;
1212         }
1213         sta = sm->sta;
1214
1215         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
1216          * present when packet contains an EAP-Message attribute */
1217         if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
1218             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
1219                                 0) < 0 &&
1220             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
1221                 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
1222                            "Message-Authenticator since it does not include "
1223                            "EAP-Message");
1224         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
1225                                      req, 1)) {
1226                 printf("Incoming RADIUS packet did not have correct "
1227                        "Message-Authenticator - dropped\n");
1228                 return RADIUS_RX_INVALID_AUTHENTICATOR;
1229         }
1230
1231         if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
1232             hdr->code != RADIUS_CODE_ACCESS_REJECT &&
1233             hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
1234                 printf("Unknown RADIUS message code\n");
1235                 return RADIUS_RX_UNKNOWN;
1236         }
1237
1238         sm->radius_identifier = -1;
1239         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
1240                    MAC2STR(sta->addr));
1241
1242         radius_msg_free(sm->last_recv_radius);
1243         sm->last_recv_radius = msg;
1244
1245         session_timeout_set =
1246                 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
1247                                            &session_timeout);
1248         if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
1249                                       &termination_action))
1250                 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
1251
1252         if (hapd->conf->acct_interim_interval == 0 &&
1253             hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
1254             radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
1255                                       &acct_interim_interval) == 0) {
1256                 if (acct_interim_interval < 60) {
1257                         hostapd_logger(hapd, sta->addr,
1258                                        HOSTAPD_MODULE_IEEE8021X,
1259                                        HOSTAPD_LEVEL_INFO,
1260                                        "ignored too small "
1261                                        "Acct-Interim-Interval %d",
1262                                        acct_interim_interval);
1263                 } else
1264                         sta->acct_interim_interval = acct_interim_interval;
1265         }
1266
1267
1268         switch (hdr->code) {
1269         case RADIUS_CODE_ACCESS_ACCEPT:
1270                 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1271                         sta->vlan_id = 0;
1272 #ifndef CONFIG_NO_VLAN
1273                 else {
1274                         old_vlanid = sta->vlan_id;
1275                         sta->vlan_id = radius_msg_get_vlanid(msg);
1276                 }
1277                 if (sta->vlan_id > 0 &&
1278                     hostapd_get_vlan_id_ifname(hapd->conf->vlan,
1279                                                sta->vlan_id)) {
1280                         hostapd_logger(hapd, sta->addr,
1281                                        HOSTAPD_MODULE_RADIUS,
1282                                        HOSTAPD_LEVEL_INFO,
1283                                        "VLAN ID %d", sta->vlan_id);
1284                 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
1285                         sta->eapol_sm->authFail = TRUE;
1286                         hostapd_logger(hapd, sta->addr,
1287                                        HOSTAPD_MODULE_IEEE8021X,
1288                                        HOSTAPD_LEVEL_INFO, "authentication "
1289                                        "server did not include required VLAN "
1290                                        "ID in Access-Accept");
1291                         break;
1292                 }
1293 #endif /* CONFIG_NO_VLAN */
1294
1295                 ap_sta_bind_vlan(hapd, sta, old_vlanid);
1296
1297                 /* RFC 3580, Ch. 3.17 */
1298                 if (session_timeout_set && termination_action ==
1299                     RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
1300                         sm->reAuthPeriod = session_timeout;
1301                 } else if (session_timeout_set)
1302                         ap_sta_session_timeout(hapd, sta, session_timeout);
1303
1304                 sm->eap_if->aaaSuccess = TRUE;
1305                 override_eapReq = 1;
1306                 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
1307                                     shared_secret_len);
1308                 ieee802_1x_store_radius_class(hapd, sta, msg);
1309                 ieee802_1x_update_sta_identity(hapd, sta, msg);
1310                 if (sm->eap_if->eapKeyAvailable &&
1311                     wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
1312                                        session_timeout_set ?
1313                                        (int) session_timeout : -1, sm) == 0) {
1314                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1315                                        HOSTAPD_LEVEL_DEBUG,
1316                                        "Added PMKSA cache entry");
1317                 }
1318                 break;
1319         case RADIUS_CODE_ACCESS_REJECT:
1320                 sm->eap_if->aaaFail = TRUE;
1321                 override_eapReq = 1;
1322                 break;
1323         case RADIUS_CODE_ACCESS_CHALLENGE:
1324                 sm->eap_if->aaaEapReq = TRUE;
1325                 if (session_timeout_set) {
1326                         /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
1327                         sm->eap_if->aaaMethodTimeout = session_timeout;
1328                         hostapd_logger(hapd, sm->addr,
1329                                        HOSTAPD_MODULE_IEEE8021X,
1330                                        HOSTAPD_LEVEL_DEBUG,
1331                                        "using EAP timeout of %d seconds (from "
1332                                        "RADIUS)",
1333                                        sm->eap_if->aaaMethodTimeout);
1334                 } else {
1335                         /*
1336                          * Use dynamic retransmission behavior per EAP
1337                          * specification.
1338                          */
1339                         sm->eap_if->aaaMethodTimeout = 0;
1340                 }
1341                 break;
1342         }
1343
1344         ieee802_1x_decapsulate_radius(hapd, sta);
1345         if (override_eapReq)
1346                 sm->eap_if->aaaEapReq = FALSE;
1347
1348         eapol_auth_step(sm);
1349
1350         return RADIUS_RX_QUEUED;
1351 }
1352 #endif /* CONFIG_NO_RADIUS */
1353
1354
1355 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
1356 {
1357         struct eapol_state_machine *sm = sta->eapol_sm;
1358         if (sm == NULL)
1359                 return;
1360
1361         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1362                        HOSTAPD_LEVEL_DEBUG, "aborting authentication");
1363
1364 #ifndef CONFIG_NO_RADIUS
1365         radius_msg_free(sm->last_recv_radius);
1366         sm->last_recv_radius = NULL;
1367 #endif /* CONFIG_NO_RADIUS */
1368
1369         if (sm->eap_if->eapTimeout) {
1370                 /*
1371                  * Disconnect the STA since it did not reply to the last EAP
1372                  * request and we cannot continue EAP processing (EAP-Failure
1373                  * could only be sent if the EAP peer actually replied).
1374                  */
1375                 sm->eap_if->portEnabled = FALSE;
1376                 hostapd_sta_deauth(hapd, sta->addr,
1377                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
1378                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1379                                 WLAN_STA_AUTHORIZED);
1380                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1381                 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
1382                 sta->timeout_next = STA_REMOVE;
1383         }
1384 }
1385
1386
1387 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
1388 {
1389         struct eapol_authenticator *eapol = hapd->eapol_auth;
1390
1391         if (hapd->conf->default_wep_key_len < 1)
1392                 return 0;
1393
1394         os_free(eapol->default_wep_key);
1395         eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
1396         if (eapol->default_wep_key == NULL ||
1397             os_get_random(eapol->default_wep_key,
1398                           hapd->conf->default_wep_key_len)) {
1399                 printf("Could not generate random WEP key.\n");
1400                 os_free(eapol->default_wep_key);
1401                 eapol->default_wep_key = NULL;
1402                 return -1;
1403         }
1404
1405         wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
1406                         eapol->default_wep_key,
1407                         hapd->conf->default_wep_key_len);
1408
1409         return 0;
1410 }
1411
1412
1413 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
1414                                         struct sta_info *sta, void *ctx)
1415 {
1416         if (sta->eapol_sm) {
1417                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
1418                 eapol_auth_step(sta->eapol_sm);
1419         }
1420         return 0;
1421 }
1422
1423
1424 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
1425 {
1426         struct hostapd_data *hapd = eloop_ctx;
1427         struct eapol_authenticator *eapol = hapd->eapol_auth;
1428
1429         if (eapol->default_wep_key_idx >= 3)
1430                 eapol->default_wep_key_idx =
1431                         hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
1432         else
1433                 eapol->default_wep_key_idx++;
1434
1435         wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
1436                    eapol->default_wep_key_idx);
1437                       
1438         if (ieee802_1x_rekey_broadcast(hapd)) {
1439                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1440                                HOSTAPD_LEVEL_WARNING, "failed to generate a "
1441                                "new broadcast key");
1442                 os_free(eapol->default_wep_key);
1443                 eapol->default_wep_key = NULL;
1444                 return;
1445         }
1446
1447         /* TODO: Could setup key for RX here, but change default TX keyid only
1448          * after new broadcast key has been sent to all stations. */
1449         if (hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
1450                             eapol->default_wep_key_idx, 1, NULL, 0,
1451                             eapol->default_wep_key,
1452                             hapd->conf->default_wep_key_len)) {
1453                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
1454                                HOSTAPD_LEVEL_WARNING, "failed to configure a "
1455                                "new broadcast key");
1456                 os_free(eapol->default_wep_key);
1457                 eapol->default_wep_key = NULL;
1458                 return;
1459         }
1460
1461         ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
1462
1463         if (hapd->conf->wep_rekeying_period > 0) {
1464                 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
1465                                        ieee802_1x_rekey, hapd, NULL);
1466         }
1467 }
1468
1469
1470 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
1471                                   const u8 *data, size_t datalen)
1472 {
1473         ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
1474 }
1475
1476
1477 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
1478                                 const u8 *data, size_t datalen)
1479 {
1480 #ifndef CONFIG_NO_RADIUS
1481         struct hostapd_data *hapd = ctx;
1482         struct sta_info *sta = sta_ctx;
1483
1484         ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
1485 #endif /* CONFIG_NO_RADIUS */
1486 }
1487
1488
1489 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
1490                                  int preauth)
1491 {
1492         struct hostapd_data *hapd = ctx;
1493         struct sta_info *sta = sta_ctx;
1494         if (preauth)
1495                 rsn_preauth_finished(hapd, sta, success);
1496         else
1497                 ieee802_1x_finished(hapd, sta, success);
1498 }
1499
1500
1501 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
1502                                    size_t identity_len, int phase2,
1503                                    struct eap_user *user)
1504 {
1505         struct hostapd_data *hapd = ctx;
1506         const struct hostapd_eap_user *eap_user;
1507         int i, count;
1508
1509         eap_user = hostapd_get_eap_user(hapd->conf, identity,
1510                                         identity_len, phase2);
1511         if (eap_user == NULL)
1512                 return -1;
1513
1514         os_memset(user, 0, sizeof(*user));
1515         user->phase2 = phase2;
1516         count = EAP_USER_MAX_METHODS;
1517         if (count > EAP_MAX_METHODS)
1518                 count = EAP_MAX_METHODS;
1519         for (i = 0; i < count; i++) {
1520                 user->methods[i].vendor = eap_user->methods[i].vendor;
1521                 user->methods[i].method = eap_user->methods[i].method;
1522         }
1523
1524         if (eap_user->password) {
1525                 user->password = os_malloc(eap_user->password_len);
1526                 if (user->password == NULL)
1527                         return -1;
1528                 os_memcpy(user->password, eap_user->password,
1529                           eap_user->password_len);
1530                 user->password_len = eap_user->password_len;
1531         }
1532         user->force_version = eap_user->force_version;
1533         user->ttls_auth = eap_user->ttls_auth;
1534
1535         return 0;
1536 }
1537
1538
1539 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
1540 {
1541         struct hostapd_data *hapd = ctx;
1542         struct sta_info *sta;
1543         sta = ap_get_sta(hapd, addr);
1544         if (sta == NULL || sta->eapol_sm == NULL)
1545                 return 0;
1546         return 1;
1547 }
1548
1549
1550 static void ieee802_1x_logger(void *ctx, const u8 *addr,
1551                               eapol_logger_level level, const char *txt)
1552 {
1553 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1554         struct hostapd_data *hapd = ctx;
1555         int hlevel;
1556
1557         switch (level) {
1558         case EAPOL_LOGGER_WARNING:
1559                 hlevel = HOSTAPD_LEVEL_WARNING;
1560                 break;
1561         case EAPOL_LOGGER_INFO:
1562                 hlevel = HOSTAPD_LEVEL_INFO;
1563                 break;
1564         case EAPOL_LOGGER_DEBUG:
1565         default:
1566                 hlevel = HOSTAPD_LEVEL_DEBUG;
1567                 break;
1568         }
1569
1570         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
1571                        txt);
1572 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1573 }
1574
1575
1576 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
1577                                            int authorized)
1578 {
1579         struct hostapd_data *hapd = ctx;
1580         struct sta_info *sta = sta_ctx;
1581         ieee802_1x_set_sta_authorized(hapd, sta, authorized);
1582 }
1583
1584
1585 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
1586 {
1587         struct hostapd_data *hapd = ctx;
1588         struct sta_info *sta = sta_ctx;
1589         ieee802_1x_abort_auth(hapd, sta);
1590 }
1591
1592
1593 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
1594 {
1595         struct hostapd_data *hapd = ctx;
1596         struct sta_info *sta = sta_ctx;
1597         ieee802_1x_tx_key(hapd, sta);
1598 }
1599
1600
1601 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
1602                                    enum eapol_event type)
1603 {
1604         /* struct hostapd_data *hapd = ctx; */
1605         struct sta_info *sta = sta_ctx;
1606         switch (type) {
1607         case EAPOL_AUTH_SM_CHANGE:
1608                 wpa_auth_sm_notify(sta->wpa_sm);
1609                 break;
1610         case EAPOL_AUTH_REAUTHENTICATE:
1611                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1612                 break;
1613         }
1614 }
1615
1616
1617 int ieee802_1x_init(struct hostapd_data *hapd)
1618 {
1619         int i;
1620         struct eapol_auth_config conf;
1621         struct eapol_auth_cb cb;
1622
1623         os_memset(&conf, 0, sizeof(conf));
1624         conf.ctx = hapd;
1625         conf.eap_reauth_period = hapd->conf->eap_reauth_period;
1626         conf.wpa = hapd->conf->wpa;
1627         conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
1628         conf.eap_server = hapd->conf->eap_server;
1629         conf.ssl_ctx = hapd->ssl_ctx;
1630         conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
1631         conf.eap_req_id_text = hapd->conf->eap_req_id_text;
1632         conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
1633         conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
1634         conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
1635         conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
1636         conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
1637         conf.eap_fast_prov = hapd->conf->eap_fast_prov;
1638         conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
1639         conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
1640         conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
1641         conf.tnc = hapd->conf->tnc;
1642         conf.wps = hapd->wps;
1643
1644         os_memset(&cb, 0, sizeof(cb));
1645         cb.eapol_send = ieee802_1x_eapol_send;
1646         cb.aaa_send = ieee802_1x_aaa_send;
1647         cb.finished = _ieee802_1x_finished;
1648         cb.get_eap_user = ieee802_1x_get_eap_user;
1649         cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
1650         cb.logger = ieee802_1x_logger;
1651         cb.set_port_authorized = ieee802_1x_set_port_authorized;
1652         cb.abort_auth = _ieee802_1x_abort_auth;
1653         cb.tx_key = _ieee802_1x_tx_key;
1654         cb.eapol_event = ieee802_1x_eapol_event;
1655
1656         hapd->eapol_auth = eapol_auth_init(&conf, &cb);
1657         if (hapd->eapol_auth == NULL)
1658                 return -1;
1659
1660         if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
1661             hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
1662                 return -1;
1663
1664 #ifndef CONFIG_NO_RADIUS
1665         if (radius_client_register(hapd->radius, RADIUS_AUTH,
1666                                    ieee802_1x_receive_auth, hapd))
1667                 return -1;
1668 #endif /* CONFIG_NO_RADIUS */
1669
1670         if (hapd->conf->default_wep_key_len) {
1671                 hostapd_set_privacy(hapd, 1);
1672
1673                 for (i = 0; i < 4; i++)
1674                         hostapd_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
1675                                         NULL, i, 0, NULL, 0, NULL, 0);
1676
1677                 ieee802_1x_rekey(hapd, NULL);
1678
1679                 if (hapd->eapol_auth->default_wep_key == NULL)
1680                         return -1;
1681         }
1682
1683         return 0;
1684 }
1685
1686
1687 void ieee802_1x_deinit(struct hostapd_data *hapd)
1688 {
1689         eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
1690
1691         if (hapd->driver != NULL &&
1692             (hapd->conf->ieee802_1x || hapd->conf->wpa))
1693                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
1694
1695         eapol_auth_deinit(hapd->eapol_auth);
1696         hapd->eapol_auth = NULL;
1697 }
1698
1699
1700 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
1701                          const u8 *buf, size_t len, int ack)
1702 {
1703         struct ieee80211_hdr *hdr;
1704         struct ieee802_1x_hdr *xhdr;
1705         struct ieee802_1x_eapol_key *key;
1706         u8 *pos;
1707         const unsigned char rfc1042_hdr[ETH_ALEN] =
1708                 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1709
1710         if (sta == NULL)
1711                 return -1;
1712         if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
1713                 return 0;
1714
1715         hdr = (struct ieee80211_hdr *) buf;
1716         pos = (u8 *) (hdr + 1);
1717         if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
1718                 return 0;
1719         pos += sizeof(rfc1042_hdr);
1720         if (WPA_GET_BE16(pos) != ETH_P_PAE)
1721                 return 0;
1722         pos += 2;
1723
1724         xhdr = (struct ieee802_1x_hdr *) pos;
1725         pos += sizeof(*xhdr);
1726
1727         wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
1728                    "type=%d length=%d - ack=%d",
1729                    MAC2STR(sta->addr), xhdr->version, xhdr->type,
1730                    be_to_host16(xhdr->length), ack);
1731
1732         /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
1733          * or Authenticator state machines, but EAPOL-Key packets are not
1734          * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
1735          * packets couple of times because otherwise STA keys become
1736          * unsynchronized with AP. */
1737         if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
1738             pos + sizeof(*key) <= buf + len) {
1739                 key = (struct ieee802_1x_eapol_key *) pos;
1740                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1741                                HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
1742                                "frame (%scast index=%d)",
1743                                key->key_index & BIT(7) ? "uni" : "broad",
1744                                key->key_index & ~BIT(7));
1745                 /* TODO: re-send EAPOL-Key couple of times (with short delay
1746                  * between them?). If all attempt fail, report error and
1747                  * deauthenticate STA so that it will get new keys when
1748                  * authenticating again (e.g., after returning in range).
1749                  * Separate limit/transmit state needed both for unicast and
1750                  * broadcast keys(?) */
1751         }
1752         /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
1753          * to here and change the key only if the EAPOL-Key packet was Acked.
1754          */
1755
1756         return 1;
1757 }
1758
1759
1760 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
1761 {
1762         if (sm == NULL || sm->identity == NULL)
1763                 return NULL;
1764
1765         *len = sm->identity_len;
1766         return sm->identity;
1767 }
1768
1769
1770 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
1771                                  int idx)
1772 {
1773         if (sm == NULL || sm->radius_class.attr == NULL ||
1774             idx >= (int) sm->radius_class.count)
1775                 return NULL;
1776
1777         *len = sm->radius_class.attr[idx].len;
1778         return sm->radius_class.attr[idx].data;
1779 }
1780
1781
1782 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
1783 {
1784         if (sm == NULL)
1785                 return NULL;
1786
1787         *len = sm->eap_if->eapKeyDataLen;
1788         return sm->eap_if->eapKeyData;
1789 }
1790
1791
1792 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
1793                                     int enabled)
1794 {
1795         if (sm == NULL)
1796                 return;
1797         sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
1798         eapol_auth_step(sm);
1799 }
1800
1801
1802 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
1803                                   int valid)
1804 {
1805         if (sm == NULL)
1806                 return;
1807         sm->portValid = valid ? TRUE : FALSE;
1808         eapol_auth_step(sm);
1809 }
1810
1811
1812 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
1813 {
1814         if (sm == NULL)
1815                 return;
1816         if (pre_auth)
1817                 sm->flags |= EAPOL_SM_PREAUTH;
1818         else
1819                 sm->flags &= ~EAPOL_SM_PREAUTH;
1820 }
1821
1822
1823 static const char * bool_txt(Boolean bool)
1824 {
1825         return bool ? "TRUE" : "FALSE";
1826 }
1827
1828
1829 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1830 {
1831         /* TODO */
1832         return 0;
1833 }
1834
1835
1836 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1837                            char *buf, size_t buflen)
1838 {
1839         int len = 0, ret;
1840         struct eapol_state_machine *sm = sta->eapol_sm;
1841
1842         if (sm == NULL)
1843                 return 0;
1844
1845         ret = os_snprintf(buf + len, buflen - len,
1846                           "dot1xPaePortNumber=%d\n"
1847                           "dot1xPaePortProtocolVersion=%d\n"
1848                           "dot1xPaePortCapabilities=1\n"
1849                           "dot1xPaePortInitialize=%d\n"
1850                           "dot1xPaePortReauthenticate=FALSE\n",
1851                           sta->aid,
1852                           EAPOL_VERSION,
1853                           sm->initialize);
1854         if (ret < 0 || (size_t) ret >= buflen - len)
1855                 return len;
1856         len += ret;
1857
1858         /* dot1xAuthConfigTable */
1859         ret = os_snprintf(buf + len, buflen - len,
1860                           "dot1xAuthPaeState=%d\n"
1861                           "dot1xAuthBackendAuthState=%d\n"
1862                           "dot1xAuthAdminControlledDirections=%d\n"
1863                           "dot1xAuthOperControlledDirections=%d\n"
1864                           "dot1xAuthAuthControlledPortStatus=%d\n"
1865                           "dot1xAuthAuthControlledPortControl=%d\n"
1866                           "dot1xAuthQuietPeriod=%u\n"
1867                           "dot1xAuthServerTimeout=%u\n"
1868                           "dot1xAuthReAuthPeriod=%u\n"
1869                           "dot1xAuthReAuthEnabled=%s\n"
1870                           "dot1xAuthKeyTxEnabled=%s\n",
1871                           sm->auth_pae_state + 1,
1872                           sm->be_auth_state + 1,
1873                           sm->adminControlledDirections,
1874                           sm->operControlledDirections,
1875                           sm->authPortStatus,
1876                           sm->portControl,
1877                           sm->quietPeriod,
1878                           sm->serverTimeout,
1879                           sm->reAuthPeriod,
1880                           bool_txt(sm->reAuthEnabled),
1881                           bool_txt(sm->keyTxEnabled));
1882         if (ret < 0 || (size_t) ret >= buflen - len)
1883                 return len;
1884         len += ret;
1885
1886         /* dot1xAuthStatsTable */
1887         ret = os_snprintf(buf + len, buflen - len,
1888                           "dot1xAuthEapolFramesRx=%u\n"
1889                           "dot1xAuthEapolFramesTx=%u\n"
1890                           "dot1xAuthEapolStartFramesRx=%u\n"
1891                           "dot1xAuthEapolLogoffFramesRx=%u\n"
1892                           "dot1xAuthEapolRespIdFramesRx=%u\n"
1893                           "dot1xAuthEapolRespFramesRx=%u\n"
1894                           "dot1xAuthEapolReqIdFramesTx=%u\n"
1895                           "dot1xAuthEapolReqFramesTx=%u\n"
1896                           "dot1xAuthInvalidEapolFramesRx=%u\n"
1897                           "dot1xAuthEapLengthErrorFramesRx=%u\n"
1898                           "dot1xAuthLastEapolFrameVersion=%u\n"
1899                           "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
1900                           sm->dot1xAuthEapolFramesRx,
1901                           sm->dot1xAuthEapolFramesTx,
1902                           sm->dot1xAuthEapolStartFramesRx,
1903                           sm->dot1xAuthEapolLogoffFramesRx,
1904                           sm->dot1xAuthEapolRespIdFramesRx,
1905                           sm->dot1xAuthEapolRespFramesRx,
1906                           sm->dot1xAuthEapolReqIdFramesTx,
1907                           sm->dot1xAuthEapolReqFramesTx,
1908                           sm->dot1xAuthInvalidEapolFramesRx,
1909                           sm->dot1xAuthEapLengthErrorFramesRx,
1910                           sm->dot1xAuthLastEapolFrameVersion,
1911                           MAC2STR(sm->addr));
1912         if (ret < 0 || (size_t) ret >= buflen - len)
1913                 return len;
1914         len += ret;
1915
1916         /* dot1xAuthDiagTable */
1917         ret = os_snprintf(buf + len, buflen - len,
1918                           "dot1xAuthEntersConnecting=%u\n"
1919                           "dot1xAuthEapLogoffsWhileConnecting=%u\n"
1920                           "dot1xAuthEntersAuthenticating=%u\n"
1921                           "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
1922                           "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
1923                           "dot1xAuthAuthFailWhileAuthenticating=%u\n"
1924                           "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
1925                           "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
1926                           "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
1927                           "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
1928                           "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
1929                           "dot1xAuthBackendResponses=%u\n"
1930                           "dot1xAuthBackendAccessChallenges=%u\n"
1931                           "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
1932                           "dot1xAuthBackendAuthSuccesses=%u\n"
1933                           "dot1xAuthBackendAuthFails=%u\n",
1934                           sm->authEntersConnecting,
1935                           sm->authEapLogoffsWhileConnecting,
1936                           sm->authEntersAuthenticating,
1937                           sm->authAuthSuccessesWhileAuthenticating,
1938                           sm->authAuthTimeoutsWhileAuthenticating,
1939                           sm->authAuthFailWhileAuthenticating,
1940                           sm->authAuthEapStartsWhileAuthenticating,
1941                           sm->authAuthEapLogoffWhileAuthenticating,
1942                           sm->authAuthReauthsWhileAuthenticated,
1943                           sm->authAuthEapStartsWhileAuthenticated,
1944                           sm->authAuthEapLogoffWhileAuthenticated,
1945                           sm->backendResponses,
1946                           sm->backendAccessChallenges,
1947                           sm->backendOtherRequestsToSupplicant,
1948                           sm->backendAuthSuccesses,
1949                           sm->backendAuthFails);
1950         if (ret < 0 || (size_t) ret >= buflen - len)
1951                 return len;
1952         len += ret;
1953
1954         /* dot1xAuthSessionStatsTable */
1955         ret = os_snprintf(buf + len, buflen - len,
1956                           /* TODO: dot1xAuthSessionOctetsRx */
1957                           /* TODO: dot1xAuthSessionOctetsTx */
1958                           /* TODO: dot1xAuthSessionFramesRx */
1959                           /* TODO: dot1xAuthSessionFramesTx */
1960                           "dot1xAuthSessionId=%08X-%08X\n"
1961                           "dot1xAuthSessionAuthenticMethod=%d\n"
1962                           "dot1xAuthSessionTime=%u\n"
1963                           "dot1xAuthSessionTerminateCause=999\n"
1964                           "dot1xAuthSessionUserName=%s\n",
1965                           sta->acct_session_id_hi, sta->acct_session_id_lo,
1966                           (wpa_key_mgmt_wpa_ieee8021x(
1967                                    wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
1968                           1 : 2,
1969                           (unsigned int) (time(NULL) -
1970                                           sta->acct_session_start),
1971                           sm->identity);
1972         if (ret < 0 || (size_t) ret >= buflen - len)
1973                 return len;
1974         len += ret;
1975
1976         return len;
1977 }
1978
1979
1980 static void ieee802_1x_finished(struct hostapd_data *hapd,
1981                                 struct sta_info *sta, int success)
1982 {
1983         const u8 *key;
1984         size_t len;
1985         /* TODO: get PMKLifetime from WPA parameters */
1986         static const int dot11RSNAConfigPMKLifetime = 43200;
1987
1988         key = ieee802_1x_get_key(sta->eapol_sm, &len);
1989         if (success && key && len >= PMK_LEN &&
1990             wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
1991                                sta->eapol_sm) == 0) {
1992                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1993                                HOSTAPD_LEVEL_DEBUG,
1994                                "Added PMKSA cache entry (IEEE 802.1X)");
1995         }
1996 }