ieee802_1x.c
Go to the documentation of this file.
00001 /*
00002  * hostapd / IEEE 802.1X-2004 Authenticator
00003  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "utils/includes.h"
00016 
00017 #include "utils/common.h"
00018 #include "utils/eloop.h"
00019 #include "crypto/md5.h"
00020 #include "crypto/crypto.h"
00021 #include "common/ieee802_11_defs.h"
00022 #include "common/wpa_ctrl.h"
00023 #include "radius/radius.h"
00024 #include "radius/radius_client.h"
00025 #include "eap_server/eap.h"
00026 #include "eapol_auth/eapol_auth_sm.h"
00027 #include "eapol_auth/eapol_auth_sm_i.h"
00028 #include "hostapd.h"
00029 #include "accounting.h"
00030 #include "sta_info.h"
00031 #include "wpa_auth.h"
00032 #include "preauth_auth.h"
00033 #include "pmksa_cache_auth.h"
00034 #include "ap_config.h"
00035 #include "ieee802_1x.h"
00036 
00037 
00038 static void ieee802_1x_finished(struct hostapd_data *hapd,
00039                                 struct sta_info *sta, int success);
00040 
00041 
00042 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
00043                             u8 type, const u8 *data, size_t datalen)
00044 {
00045         u8 *buf;
00046         struct ieee802_1x_hdr *xhdr;
00047         size_t len;
00048         int encrypt = 0;
00049 
00050         len = sizeof(*xhdr) + datalen;
00051         buf = os_zalloc(len);
00052         if (buf == NULL) {
00053                 wpa_printf(MSG_ERROR, "malloc() failed for "
00054                            "ieee802_1x_send(len=%lu)",
00055                            (unsigned long) len);
00056                 return;
00057         }
00058 
00059         xhdr = (struct ieee802_1x_hdr *) buf;
00060         xhdr->version = hapd->conf->eapol_version;
00061         xhdr->type = type;
00062         xhdr->length = host_to_be16(datalen);
00063 
00064         if (datalen > 0 && data != NULL)
00065                 os_memcpy(xhdr + 1, data, datalen);
00066 
00067         if (wpa_auth_pairwise_set(sta->wpa_sm))
00068                 encrypt = 1;
00069         if (sta->flags & WLAN_STA_PREAUTH) {
00070                 rsn_preauth_send(hapd, sta, buf, len);
00071         } else {
00072                 hapd->drv.send_eapol(hapd, sta->addr, buf, len, encrypt);
00073         }
00074 
00075         os_free(buf);
00076 }
00077 
00078 
00079 void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
00080                                    struct sta_info *sta, int authorized)
00081 {
00082         int res;
00083 
00084         if (sta->flags & WLAN_STA_PREAUTH)
00085                 return;
00086 
00087         if (authorized) {
00088                 if (!(sta->flags & WLAN_STA_AUTHORIZED))
00089                         wpa_msg(hapd->msg_ctx, MSG_INFO,
00090                                 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
00091                 sta->flags |= WLAN_STA_AUTHORIZED;
00092                 res = hapd->drv.set_authorized(hapd, sta, 1);
00093                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00094                                HOSTAPD_LEVEL_DEBUG, "authorizing port");
00095         } else {
00096                 if ((sta->flags & (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC)) ==
00097                     (WLAN_STA_AUTHORIZED | WLAN_STA_ASSOC))
00098                         wpa_msg(hapd->msg_ctx, MSG_INFO,
00099                                 AP_STA_DISCONNECTED MACSTR,
00100                                 MAC2STR(sta->addr));
00101                 sta->flags &= ~WLAN_STA_AUTHORIZED;
00102                 res = hapd->drv.set_authorized(hapd, sta, 0);
00103                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00104                                HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
00105         }
00106 
00107         if (res && errno != ENOENT) {
00108                 printf("Could not set station " MACSTR " flags for kernel "
00109                        "driver (errno=%d).\n", MAC2STR(sta->addr), errno);
00110         }
00111 
00112         if (authorized)
00113                 accounting_sta_start(hapd, sta);
00114 }
00115 
00116 
00117 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
00118                                   struct sta_info *sta,
00119                                   int idx, int broadcast,
00120                                   u8 *key_data, size_t key_len)
00121 {
00122         u8 *buf, *ekey;
00123         struct ieee802_1x_hdr *hdr;
00124         struct ieee802_1x_eapol_key *key;
00125         size_t len, ekey_len;
00126         struct eapol_state_machine *sm = sta->eapol_sm;
00127 
00128         if (sm == NULL)
00129                 return;
00130 
00131         len = sizeof(*key) + key_len;
00132         buf = os_zalloc(sizeof(*hdr) + len);
00133         if (buf == NULL)
00134                 return;
00135 
00136         hdr = (struct ieee802_1x_hdr *) buf;
00137         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
00138         key->type = EAPOL_KEY_TYPE_RC4;
00139         key->key_length = htons(key_len);
00140         wpa_get_ntp_timestamp(key->replay_counter);
00141 
00142         if (os_get_random(key->key_iv, sizeof(key->key_iv))) {
00143                 wpa_printf(MSG_ERROR, "Could not get random numbers");
00144                 os_free(buf);
00145                 return;
00146         }
00147 
00148         key->key_index = idx | (broadcast ? 0 : BIT(7));
00149         if (hapd->conf->eapol_key_index_workaround) {
00150                 /* According to some information, WinXP Supplicant seems to
00151                  * interpret bit7 as an indication whether the key is to be
00152                  * activated, so make it possible to enable workaround that
00153                  * sets this bit for all keys. */
00154                 key->key_index |= BIT(7);
00155         }
00156 
00157         /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
00158          * MSK[32..63] is used to sign the message. */
00159         if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
00160                 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
00161                            "and signing EAPOL-Key");
00162                 os_free(buf);
00163                 return;
00164         }
00165         os_memcpy((u8 *) (key + 1), key_data, key_len);
00166         ekey_len = sizeof(key->key_iv) + 32;
00167         ekey = os_malloc(ekey_len);
00168         if (ekey == NULL) {
00169                 wpa_printf(MSG_ERROR, "Could not encrypt key");
00170                 os_free(buf);
00171                 return;
00172         }
00173         os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
00174         os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
00175         rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
00176         os_free(ekey);
00177 
00178         /* This header is needed here for HMAC-MD5, but it will be regenerated
00179          * in ieee802_1x_send() */
00180         hdr->version = hapd->conf->eapol_version;
00181         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
00182         hdr->length = host_to_be16(len);
00183         hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
00184                  key->key_signature);
00185 
00186         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
00187                    " (%s index=%d)", MAC2STR(sm->addr),
00188                    broadcast ? "broadcast" : "unicast", idx);
00189         ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
00190         if (sta->eapol_sm)
00191                 sta->eapol_sm->dot1xAuthEapolFramesTx++;
00192         os_free(buf);
00193 }
00194 
00195 
00196 #ifndef CONFIG_NO_VLAN
00197 static struct hostapd_wep_keys *
00198 ieee802_1x_group_alloc(struct hostapd_data *hapd, const char *ifname)
00199 {
00200         struct hostapd_wep_keys *key;
00201 
00202         key = os_zalloc(sizeof(*key));
00203         if (key == NULL)
00204                 return NULL;
00205 
00206         key->default_len = hapd->conf->default_wep_key_len;
00207 
00208         if (key->idx >= hapd->conf->broadcast_key_idx_max ||
00209             key->idx < hapd->conf->broadcast_key_idx_min)
00210                 key->idx = hapd->conf->broadcast_key_idx_min;
00211         else
00212                 key->idx++;
00213 
00214         if (!key->key[key->idx])
00215                 key->key[key->idx] = os_malloc(key->default_len);
00216         if (key->key[key->idx] == NULL ||
00217             os_get_random(key->key[key->idx], key->default_len)) {
00218                 printf("Could not generate random WEP key (dynamic VLAN).\n");
00219                 os_free(key->key[key->idx]);
00220                 key->key[key->idx] = NULL;
00221                 os_free(key);
00222                 return NULL;
00223         }
00224         key->len[key->idx] = key->default_len;
00225 
00226         wpa_printf(MSG_DEBUG, "%s: Default WEP idx %d for dynamic VLAN\n",
00227                    ifname, key->idx);
00228         wpa_hexdump_key(MSG_DEBUG, "Default WEP key (dynamic VLAN)",
00229                         key->key[key->idx], key->len[key->idx]);
00230 
00231         if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL, key->idx, 1,
00232                               NULL, 0, key->key[key->idx], key->len[key->idx]))
00233                 printf("Could not set dynamic VLAN WEP encryption key.\n");
00234 
00235         hapd->drv.set_drv_ieee8021x(hapd, ifname, 1);
00236 
00237         return key;
00238 }
00239 
00240 
00241 static struct hostapd_wep_keys *
00242 ieee802_1x_get_group(struct hostapd_data *hapd, struct hostapd_ssid *ssid,
00243                      size_t vlan_id)
00244 {
00245         const char *ifname;
00246 
00247         if (vlan_id == 0)
00248                 return &ssid->wep;
00249 
00250         if (vlan_id <= ssid->max_dyn_vlan_keys && ssid->dyn_vlan_keys &&
00251             ssid->dyn_vlan_keys[vlan_id])
00252                 return ssid->dyn_vlan_keys[vlan_id];
00253 
00254         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Creating new group "
00255                    "state machine for VLAN ID %lu",
00256                    (unsigned long) vlan_id);
00257 
00258         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
00259         if (ifname == NULL) {
00260                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Unknown VLAN ID %lu - "
00261                            "cannot create group key state machine",
00262                            (unsigned long) vlan_id);
00263                 return NULL;
00264         }
00265 
00266         if (ssid->dyn_vlan_keys == NULL) {
00267                 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
00268                 ssid->dyn_vlan_keys = os_zalloc(size);
00269                 if (ssid->dyn_vlan_keys == NULL)
00270                         return NULL;
00271                 ssid->max_dyn_vlan_keys = vlan_id;
00272         }
00273 
00274         if (ssid->max_dyn_vlan_keys < vlan_id) {
00275                 struct hostapd_wep_keys **na;
00276                 int size = (vlan_id + 1) * sizeof(ssid->dyn_vlan_keys[0]);
00277                 na = os_realloc(ssid->dyn_vlan_keys, size);
00278                 if (na == NULL)
00279                         return NULL;
00280                 ssid->dyn_vlan_keys = na;
00281                 os_memset(&ssid->dyn_vlan_keys[ssid->max_dyn_vlan_keys + 1], 0,
00282                           (vlan_id - ssid->max_dyn_vlan_keys) *
00283                           sizeof(ssid->dyn_vlan_keys[0]));
00284                 ssid->max_dyn_vlan_keys = vlan_id;
00285         }
00286 
00287         ssid->dyn_vlan_keys[vlan_id] = ieee802_1x_group_alloc(hapd, ifname);
00288 
00289         return ssid->dyn_vlan_keys[vlan_id];
00290 }
00291 #endif /* CONFIG_NO_VLAN */
00292 
00293 
00294 void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
00295 {
00296         struct eapol_authenticator *eapol = hapd->eapol_auth;
00297         struct eapol_state_machine *sm = sta->eapol_sm;
00298 #ifndef CONFIG_NO_VLAN
00299         struct hostapd_wep_keys *key = NULL;
00300         int vlan_id;
00301 #endif /* CONFIG_NO_VLAN */
00302 
00303         if (sm == NULL || !sm->eap_if->eapKeyData)
00304                 return;
00305 
00306         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
00307                    MAC2STR(sta->addr));
00308 
00309 #ifndef CONFIG_NO_VLAN
00310         vlan_id = sta->vlan_id;
00311         if (vlan_id < 0 || vlan_id > MAX_VLAN_ID)
00312                 vlan_id = 0;
00313 
00314         if (vlan_id) {
00315                 key = ieee802_1x_get_group(hapd, sta->ssid, vlan_id);
00316                 if (key && key->key[key->idx])
00317                         ieee802_1x_tx_key_one(hapd, sta, key->idx, 1,
00318                                               key->key[key->idx],
00319                                               key->len[key->idx]);
00320         } else
00321 #endif /* CONFIG_NO_VLAN */
00322         if (eapol->default_wep_key) {
00323                 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
00324                                       eapol->default_wep_key,
00325                                       hapd->conf->default_wep_key_len);
00326         }
00327 
00328         if (hapd->conf->individual_wep_key_len > 0) {
00329                 u8 *ikey;
00330                 ikey = os_malloc(hapd->conf->individual_wep_key_len);
00331                 if (ikey == NULL ||
00332                     os_get_random(ikey, hapd->conf->individual_wep_key_len)) {
00333                         wpa_printf(MSG_ERROR, "Could not generate random "
00334                                    "individual WEP key.");
00335                         os_free(ikey);
00336                         return;
00337                 }
00338 
00339                 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
00340                                 ikey, hapd->conf->individual_wep_key_len);
00341 
00342                 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
00343                                       hapd->conf->individual_wep_key_len);
00344 
00345                 /* TODO: set encryption in TX callback, i.e., only after STA
00346                  * has ACKed EAPOL-Key frame */
00347                 if (hapd->drv.set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
00348                                       sta->addr, 0, 1, NULL, 0, ikey,
00349                                       hapd->conf->individual_wep_key_len)) {
00350                         wpa_printf(MSG_ERROR, "Could not set individual WEP "
00351                                    "encryption.");
00352                 }
00353 
00354                 os_free(ikey);
00355         }
00356 }
00357 
00358 
00359 const char *radius_mode_txt(struct hostapd_data *hapd)
00360 {
00361         switch (hapd->iface->conf->hw_mode) {
00362         case HOSTAPD_MODE_IEEE80211A:
00363                 return "802.11a";
00364         case HOSTAPD_MODE_IEEE80211G:
00365                 return "802.11g";
00366         case HOSTAPD_MODE_IEEE80211B:
00367         default:
00368                 return "802.11b";
00369         }
00370 }
00371 
00372 
00373 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
00374 {
00375         int i;
00376         u8 rate = 0;
00377 
00378         for (i = 0; i < sta->supported_rates_len; i++)
00379                 if ((sta->supported_rates[i] & 0x7f) > rate)
00380                         rate = sta->supported_rates[i] & 0x7f;
00381 
00382         return rate;
00383 }
00384 
00385 
00386 #ifndef CONFIG_NO_RADIUS
00387 static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
00388                                       struct eapol_state_machine *sm,
00389                                       const u8 *eap, size_t len)
00390 {
00391         const u8 *identity;
00392         size_t identity_len;
00393 
00394         if (len <= sizeof(struct eap_hdr) ||
00395             eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
00396                 return;
00397 
00398         identity = eap_get_identity(sm->eap, &identity_len);
00399         if (identity == NULL)
00400                 return;
00401 
00402         /* Save station identity for future RADIUS packets */
00403         os_free(sm->identity);
00404         sm->identity = os_malloc(identity_len + 1);
00405         if (sm->identity == NULL) {
00406                 sm->identity_len = 0;
00407                 return;
00408         }
00409 
00410         os_memcpy(sm->identity, identity, identity_len);
00411         sm->identity_len = identity_len;
00412         sm->identity[identity_len] = '\0';
00413         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
00414                        HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
00415         sm->dot1xAuthEapolRespIdFramesRx++;
00416 }
00417 
00418 
00419 static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
00420                                           struct sta_info *sta,
00421                                           const u8 *eap, size_t len)
00422 {
00423         struct radius_msg *msg;
00424         char buf[128];
00425         struct eapol_state_machine *sm = sta->eapol_sm;
00426 
00427         if (sm == NULL)
00428                 return;
00429 
00430         ieee802_1x_learn_identity(hapd, sm, eap, len);
00431 
00432         wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
00433                    "packet");
00434 
00435         sm->radius_identifier = radius_client_get_id(hapd->radius);
00436         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
00437                              sm->radius_identifier);
00438         if (msg == NULL) {
00439                 printf("Could not create net RADIUS packet\n");
00440                 return;
00441         }
00442 
00443         radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
00444 
00445         if (sm->identity &&
00446             !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
00447                                  sm->identity, sm->identity_len)) {
00448                 printf("Could not add User-Name\n");
00449                 goto fail;
00450         }
00451 
00452         if (hapd->conf->own_ip_addr.af == AF_INET &&
00453             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
00454                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
00455                 printf("Could not add NAS-IP-Address\n");
00456                 goto fail;
00457         }
00458 
00459 #ifdef CONFIG_IPV6
00460         if (hapd->conf->own_ip_addr.af == AF_INET6 &&
00461             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
00462                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
00463                 printf("Could not add NAS-IPv6-Address\n");
00464                 goto fail;
00465         }
00466 #endif /* CONFIG_IPV6 */
00467 
00468         if (hapd->conf->nas_identifier &&
00469             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
00470                                  (u8 *) hapd->conf->nas_identifier,
00471                                  os_strlen(hapd->conf->nas_identifier))) {
00472                 printf("Could not add NAS-Identifier\n");
00473                 goto fail;
00474         }
00475 
00476         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
00477                 printf("Could not add NAS-Port\n");
00478                 goto fail;
00479         }
00480 
00481         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
00482                     MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
00483         buf[sizeof(buf) - 1] = '\0';
00484         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
00485                                  (u8 *) buf, os_strlen(buf))) {
00486                 printf("Could not add Called-Station-Id\n");
00487                 goto fail;
00488         }
00489 
00490         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
00491                     MAC2STR(sta->addr));
00492         buf[sizeof(buf) - 1] = '\0';
00493         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
00494                                  (u8 *) buf, os_strlen(buf))) {
00495                 printf("Could not add Calling-Station-Id\n");
00496                 goto fail;
00497         }
00498 
00499         /* TODO: should probably check MTU from driver config; 2304 is max for
00500          * IEEE 802.11, but use 1400 to avoid problems with too large packets
00501          */
00502         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
00503                 printf("Could not add Framed-MTU\n");
00504                 goto fail;
00505         }
00506 
00507         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
00508                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
00509                 printf("Could not add NAS-Port-Type\n");
00510                 goto fail;
00511         }
00512 
00513         if (sta->flags & WLAN_STA_PREAUTH) {
00514                 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
00515                            sizeof(buf));
00516         } else {
00517                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
00518                             radius_sta_rate(hapd, sta) / 2,
00519                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
00520                             radius_mode_txt(hapd));
00521                 buf[sizeof(buf) - 1] = '\0';
00522         }
00523         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
00524                                  (u8 *) buf, os_strlen(buf))) {
00525                 printf("Could not add Connect-Info\n");
00526                 goto fail;
00527         }
00528 
00529         if (eap && !radius_msg_add_eap(msg, eap, len)) {
00530                 printf("Could not add EAP-Message\n");
00531                 goto fail;
00532         }
00533 
00534         /* State attribute must be copied if and only if this packet is
00535          * Access-Request reply to the previous Access-Challenge */
00536         if (sm->last_recv_radius &&
00537             radius_msg_get_hdr(sm->last_recv_radius)->code ==
00538             RADIUS_CODE_ACCESS_CHALLENGE) {
00539                 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
00540                                                RADIUS_ATTR_STATE);
00541                 if (res < 0) {
00542                         printf("Could not copy State attribute from previous "
00543                                "Access-Challenge\n");
00544                         goto fail;
00545                 }
00546                 if (res > 0) {
00547                         wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
00548                 }
00549         }
00550 
00551         radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr);
00552         return;
00553 
00554  fail:
00555         radius_msg_free(msg);
00556 }
00557 #endif /* CONFIG_NO_RADIUS */
00558 
00559 
00560 static void handle_eap_response(struct hostapd_data *hapd,
00561                                 struct sta_info *sta, struct eap_hdr *eap,
00562                                 size_t len)
00563 {
00564         u8 type, *data;
00565         struct eapol_state_machine *sm = sta->eapol_sm;
00566         if (sm == NULL)
00567                 return;
00568 
00569         data = (u8 *) (eap + 1);
00570 
00571         if (len < sizeof(*eap) + 1) {
00572                 printf("handle_eap_response: too short response data\n");
00573                 return;
00574         }
00575 
00576         sm->eap_type_supp = type = data[0];
00577 
00578         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
00579                        HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
00580                        "id=%d len=%d) from STA: EAP Response-%s (%d)",
00581                        eap->code, eap->identifier, be_to_host16(eap->length),
00582                        eap_server_get_name(0, type), type);
00583 
00584         sm->dot1xAuthEapolRespFramesRx++;
00585 
00586         wpabuf_free(sm->eap_if->eapRespData);
00587         sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
00588         sm->eapolEap = TRUE;
00589 }
00590 
00591 
00592 /* Process incoming EAP packet from Supplicant */
00593 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
00594                        u8 *buf, size_t len)
00595 {
00596         struct eap_hdr *eap;
00597         u16 eap_len;
00598 
00599         if (len < sizeof(*eap)) {
00600                 printf("   too short EAP packet\n");
00601                 return;
00602         }
00603 
00604         eap = (struct eap_hdr *) buf;
00605 
00606         eap_len = be_to_host16(eap->length);
00607         wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
00608                    eap->code, eap->identifier, eap_len);
00609         if (eap_len < sizeof(*eap)) {
00610                 wpa_printf(MSG_DEBUG, "   Invalid EAP length");
00611                 return;
00612         } else if (eap_len > len) {
00613                 wpa_printf(MSG_DEBUG, "   Too short frame to contain this EAP "
00614                            "packet");
00615                 return;
00616         } else if (eap_len < len) {
00617                 wpa_printf(MSG_DEBUG, "   Ignoring %lu extra bytes after EAP "
00618                            "packet", (unsigned long) len - eap_len);
00619         }
00620 
00621         switch (eap->code) {
00622         case EAP_CODE_REQUEST:
00623                 wpa_printf(MSG_DEBUG, " (request)");
00624                 return;
00625         case EAP_CODE_RESPONSE:
00626                 wpa_printf(MSG_DEBUG, " (response)");
00627                 handle_eap_response(hapd, sta, eap, eap_len);
00628                 break;
00629         case EAP_CODE_SUCCESS:
00630                 wpa_printf(MSG_DEBUG, " (success)");
00631                 return;
00632         case EAP_CODE_FAILURE:
00633                 wpa_printf(MSG_DEBUG, " (failure)");
00634                 return;
00635         default:
00636                 wpa_printf(MSG_DEBUG, " (unknown code)");
00637                 return;
00638         }
00639 }
00640 
00641 
00642 static struct eapol_state_machine *
00643 ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
00644 {
00645         int flags = 0;
00646         if (sta->flags & WLAN_STA_PREAUTH)
00647                 flags |= EAPOL_SM_PREAUTH;
00648         if (sta->wpa_sm) {
00649                 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
00650                         flags |= EAPOL_SM_USES_WPA;
00651                 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
00652                         flags |= EAPOL_SM_FROM_PMKSA_CACHE;
00653         }
00654         return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
00655                                 sta->wps_ie, sta);
00656 }
00657 
00658 
00668 void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
00669                         size_t len)
00670 {
00671         struct sta_info *sta;
00672         struct ieee802_1x_hdr *hdr;
00673         struct ieee802_1x_eapol_key *key;
00674         u16 datalen;
00675         struct rsn_pmksa_cache_entry *pmksa;
00676 
00677         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
00678             !hapd->conf->wps_state)
00679                 return;
00680 
00681         wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
00682                    (unsigned long) len, MAC2STR(sa));
00683         sta = ap_get_sta(hapd, sa);
00684         if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
00685                 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
00686                            "associated STA");
00687                 return;
00688         }
00689 
00690         if (len < sizeof(*hdr)) {
00691                 printf("   too short IEEE 802.1X packet\n");
00692                 return;
00693         }
00694 
00695         hdr = (struct ieee802_1x_hdr *) buf;
00696         datalen = be_to_host16(hdr->length);
00697         wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
00698                    hdr->version, hdr->type, datalen);
00699 
00700         if (len - sizeof(*hdr) < datalen) {
00701                 printf("   frame too short for this IEEE 802.1X packet\n");
00702                 if (sta->eapol_sm)
00703                         sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
00704                 return;
00705         }
00706         if (len - sizeof(*hdr) > datalen) {
00707                 wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
00708                            "IEEE 802.1X packet",
00709                            (unsigned long) len - sizeof(*hdr) - datalen);
00710         }
00711 
00712         if (sta->eapol_sm) {
00713                 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
00714                 sta->eapol_sm->dot1xAuthEapolFramesRx++;
00715         }
00716 
00717         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
00718         if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
00719             hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
00720             (key->type == EAPOL_KEY_TYPE_WPA ||
00721              key->type == EAPOL_KEY_TYPE_RSN)) {
00722                 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
00723                             sizeof(*hdr) + datalen);
00724                 return;
00725         }
00726 
00727         if ((!hapd->conf->ieee802_1x &&
00728              !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) ||
00729             wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
00730                 return;
00731 
00732         if (!sta->eapol_sm) {
00733                 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
00734                 if (!sta->eapol_sm)
00735                         return;
00736 
00737 #ifdef CONFIG_WPS
00738                 if (!hapd->conf->ieee802_1x &&
00739                     ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
00740                      WLAN_STA_MAYBE_WPS)) {
00741                         /*
00742                          * Delay EAPOL frame transmission until a possible WPS
00743                          * STA initiates the handshake with EAPOL-Start.
00744                          */
00745                         sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
00746                 }
00747 #endif /* CONFIG_WPS */
00748 
00749                 sta->eapol_sm->eap_if->portEnabled = TRUE;
00750         }
00751 
00752         /* since we support version 1, we can ignore version field and proceed
00753          * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
00754         /* TODO: actually, we are not version 1 anymore.. However, Version 2
00755          * does not change frame contents, so should be ok to process frames
00756          * more or less identically. Some changes might be needed for
00757          * verification of fields. */
00758 
00759         switch (hdr->type) {
00760         case IEEE802_1X_TYPE_EAP_PACKET:
00761                 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
00762                 break;
00763 
00764         case IEEE802_1X_TYPE_EAPOL_START:
00765                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00766                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
00767                                "from STA");
00768                 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
00769                 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
00770                 if (pmksa) {
00771                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
00772                                        HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
00773                                        "available - ignore it since "
00774                                        "STA sent EAPOL-Start");
00775                         wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
00776                 }
00777                 sta->eapol_sm->eapolStart = TRUE;
00778                 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
00779                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
00780                 break;
00781 
00782         case IEEE802_1X_TYPE_EAPOL_LOGOFF:
00783                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00784                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
00785                                "from STA");
00786                 sta->acct_terminate_cause =
00787                         RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
00788                 accounting_sta_stop(hapd, sta);
00789                 sta->eapol_sm->eapolLogoff = TRUE;
00790                 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
00791                 break;
00792 
00793         case IEEE802_1X_TYPE_EAPOL_KEY:
00794                 wpa_printf(MSG_DEBUG, "   EAPOL-Key");
00795                 if (!(sta->flags & WLAN_STA_AUTHORIZED)) {
00796                         wpa_printf(MSG_DEBUG, "   Dropped key data from "
00797                                    "unauthorized Supplicant");
00798                         break;
00799                 }
00800                 break;
00801 
00802         case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
00803                 wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
00804                 /* TODO: implement support for this; show data */
00805                 break;
00806 
00807         default:
00808                 wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
00809                 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
00810                 break;
00811         }
00812 
00813         eapol_auth_step(sta->eapol_sm);
00814 }
00815 
00816 
00825 void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
00826 {
00827         struct rsn_pmksa_cache_entry *pmksa;
00828         int reassoc = 1;
00829         int force_1x = 0;
00830 
00831 #ifdef CONFIG_WPS
00832         if (hapd->conf->wps_state && hapd->conf->wpa &&
00833             (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
00834                 /*
00835                  * Need to enable IEEE 802.1X/EAPOL state machines for possible
00836                  * WPS handshake even if IEEE 802.1X/EAPOL is not used for
00837                  * authentication in this BSS.
00838                  */
00839                 force_1x = 1;
00840         }
00841 #endif /* CONFIG_WPS */
00842 
00843         if ((!force_1x && !hapd->conf->ieee802_1x) ||
00844             wpa_key_mgmt_wpa_psk(wpa_auth_sta_key_mgmt(sta->wpa_sm)))
00845                 return;
00846 
00847         if (sta->eapol_sm == NULL) {
00848                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00849                                HOSTAPD_LEVEL_DEBUG, "start authentication");
00850                 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
00851                 if (sta->eapol_sm == NULL) {
00852                         hostapd_logger(hapd, sta->addr,
00853                                        HOSTAPD_MODULE_IEEE8021X,
00854                                        HOSTAPD_LEVEL_INFO,
00855                                        "failed to allocate state machine");
00856                         return;
00857                 }
00858                 reassoc = 0;
00859         }
00860 
00861 #ifdef CONFIG_WPS
00862         sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
00863         if (!hapd->conf->ieee802_1x && !(sta->flags & WLAN_STA_WPS)) {
00864                 /*
00865                  * Delay EAPOL frame transmission until a possible WPS
00866                  * initiates the handshake with EAPOL-Start.
00867                  */
00868                 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
00869         }
00870 #endif /* CONFIG_WPS */
00871 
00872         sta->eapol_sm->eap_if->portEnabled = TRUE;
00873 
00874         pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
00875         if (pmksa) {
00876                 int old_vlanid;
00877 
00878                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00879                                HOSTAPD_LEVEL_DEBUG,
00880                                "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
00881                 /* Setup EAPOL state machines to already authenticated state
00882                  * because of existing PMKSA information in the cache. */
00883                 sta->eapol_sm->keyRun = TRUE;
00884                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
00885                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
00886                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
00887                 sta->eapol_sm->authSuccess = TRUE;
00888                 if (sta->eapol_sm->eap)
00889                         eap_sm_notify_cached(sta->eapol_sm->eap);
00890                 old_vlanid = sta->vlan_id;
00891                 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
00892                 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
00893                         sta->vlan_id = 0;
00894                 ap_sta_bind_vlan(hapd, sta, old_vlanid);
00895         } else {
00896                 if (reassoc) {
00897                         /*
00898                          * Force EAPOL state machines to start
00899                          * re-authentication without having to wait for the
00900                          * Supplicant to send EAPOL-Start.
00901                          */
00902                         sta->eapol_sm->reAuthenticate = TRUE;
00903                 }
00904                 eapol_auth_step(sta->eapol_sm);
00905         }
00906 }
00907 
00908 
00909 void ieee802_1x_free_station(struct sta_info *sta)
00910 {
00911         struct eapol_state_machine *sm = sta->eapol_sm;
00912 
00913         if (sm == NULL)
00914                 return;
00915 
00916         sta->eapol_sm = NULL;
00917 
00918 #ifndef CONFIG_NO_RADIUS
00919         radius_msg_free(sm->last_recv_radius);
00920         radius_free_class(&sm->radius_class);
00921 #endif /* CONFIG_NO_RADIUS */
00922 
00923         os_free(sm->identity);
00924         eapol_auth_free(sm);
00925 }
00926 
00927 
00928 #ifndef CONFIG_NO_RADIUS
00929 static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
00930                                           struct sta_info *sta)
00931 {
00932         u8 *eap;
00933         size_t len;
00934         struct eap_hdr *hdr;
00935         int eap_type = -1;
00936         char buf[64];
00937         struct radius_msg *msg;
00938         struct eapol_state_machine *sm = sta->eapol_sm;
00939 
00940         if (sm == NULL || sm->last_recv_radius == NULL) {
00941                 if (sm)
00942                         sm->eap_if->aaaEapNoReq = TRUE;
00943                 return;
00944         }
00945 
00946         msg = sm->last_recv_radius;
00947 
00948         eap = radius_msg_get_eap(msg, &len);
00949         if (eap == NULL) {
00950                 /* RFC 3579, Chap. 2.6.3:
00951                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
00952                  * attribute */
00953                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00954                                HOSTAPD_LEVEL_WARNING, "could not extract "
00955                                "EAP-Message from RADIUS message");
00956                 sm->eap_if->aaaEapNoReq = TRUE;
00957                 return;
00958         }
00959 
00960         if (len < sizeof(*hdr)) {
00961                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
00962                                HOSTAPD_LEVEL_WARNING, "too short EAP packet "
00963                                "received from authentication server");
00964                 os_free(eap);
00965                 sm->eap_if->aaaEapNoReq = TRUE;
00966                 return;
00967         }
00968 
00969         if (len > sizeof(*hdr))
00970                 eap_type = eap[sizeof(*hdr)];
00971 
00972         hdr = (struct eap_hdr *) eap;
00973         switch (hdr->code) {
00974         case EAP_CODE_REQUEST:
00975                 if (eap_type >= 0)
00976                         sm->eap_type_authsrv = eap_type;
00977                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
00978                             eap_type >= 0 ? eap_server_get_name(0, eap_type) :
00979                             "??",
00980                             eap_type);
00981                 break;
00982         case EAP_CODE_RESPONSE:
00983                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
00984                             eap_type >= 0 ? eap_server_get_name(0, eap_type) :
00985                             "??",
00986                             eap_type);
00987                 break;
00988         case EAP_CODE_SUCCESS:
00989                 os_strlcpy(buf, "EAP Success", sizeof(buf));
00990                 break;
00991         case EAP_CODE_FAILURE:
00992                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
00993                 break;
00994         default:
00995                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
00996                 break;
00997         }
00998         buf[sizeof(buf) - 1] = '\0';
00999         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
01000                        HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
01001                        "id=%d len=%d) from RADIUS server: %s",
01002                        hdr->code, hdr->identifier, be_to_host16(hdr->length),
01003                        buf);
01004         sm->eap_if->aaaEapReq = TRUE;
01005 
01006         wpabuf_free(sm->eap_if->aaaEapReqData);
01007         sm->eap_if->aaaEapReqData = wpabuf_alloc_ext_data(eap, len);
01008 }
01009 
01010 
01011 static void ieee802_1x_get_keys(struct hostapd_data *hapd,
01012                                 struct sta_info *sta, struct radius_msg *msg,
01013                                 struct radius_msg *req,
01014                                 const u8 *shared_secret,
01015                                 size_t shared_secret_len)
01016 {
01017         struct radius_ms_mppe_keys *keys;
01018         struct eapol_state_machine *sm = sta->eapol_sm;
01019         if (sm == NULL)
01020                 return;
01021 
01022         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
01023                                       shared_secret_len);
01024 
01025         if (keys && keys->send && keys->recv) {
01026                 size_t len = keys->send_len + keys->recv_len;
01027                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
01028                                 keys->send, keys->send_len);
01029                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
01030                                 keys->recv, keys->recv_len);
01031 
01032                 os_free(sm->eap_if->aaaEapKeyData);
01033                 sm->eap_if->aaaEapKeyData = os_malloc(len);
01034                 if (sm->eap_if->aaaEapKeyData) {
01035                         os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
01036                                   keys->recv_len);
01037                         os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
01038                                   keys->send, keys->send_len);
01039                         sm->eap_if->aaaEapKeyDataLen = len;
01040                         sm->eap_if->aaaEapKeyAvailable = TRUE;
01041                 }
01042         }
01043 
01044         if (keys) {
01045                 os_free(keys->send);
01046                 os_free(keys->recv);
01047                 os_free(keys);
01048         }
01049 }
01050 
01051 
01052 static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
01053                                           struct sta_info *sta,
01054                                           struct radius_msg *msg)
01055 {
01056         u8 *class;
01057         size_t class_len;
01058         struct eapol_state_machine *sm = sta->eapol_sm;
01059         int count, i;
01060         struct radius_attr_data *nclass;
01061         size_t nclass_count;
01062 
01063         if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
01064             sm == NULL)
01065                 return;
01066 
01067         radius_free_class(&sm->radius_class);
01068         count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
01069         if (count <= 0)
01070                 return;
01071 
01072         nclass = os_zalloc(count * sizeof(struct radius_attr_data));
01073         if (nclass == NULL)
01074                 return;
01075 
01076         nclass_count = 0;
01077 
01078         class = NULL;
01079         for (i = 0; i < count; i++) {
01080                 do {
01081                         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
01082                                                     &class, &class_len,
01083                                                     class) < 0) {
01084                                 i = count;
01085                                 break;
01086                         }
01087                 } while (class_len < 1);
01088 
01089                 nclass[nclass_count].data = os_malloc(class_len);
01090                 if (nclass[nclass_count].data == NULL)
01091                         break;
01092 
01093                 os_memcpy(nclass[nclass_count].data, class, class_len);
01094                 nclass[nclass_count].len = class_len;
01095                 nclass_count++;
01096         }
01097 
01098         sm->radius_class.attr = nclass;
01099         sm->radius_class.count = nclass_count;
01100         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
01101                    "attributes for " MACSTR,
01102                    (unsigned long) sm->radius_class.count,
01103                    MAC2STR(sta->addr));
01104 }
01105 
01106 
01107 /* Update sta->identity based on User-Name attribute in Access-Accept */
01108 static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
01109                                            struct sta_info *sta,
01110                                            struct radius_msg *msg)
01111 {
01112         u8 *buf, *identity;
01113         size_t len;
01114         struct eapol_state_machine *sm = sta->eapol_sm;
01115 
01116         if (sm == NULL)
01117                 return;
01118 
01119         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
01120                                     NULL) < 0)
01121                 return;
01122 
01123         identity = os_malloc(len + 1);
01124         if (identity == NULL)
01125                 return;
01126 
01127         os_memcpy(identity, buf, len);
01128         identity[len] = '\0';
01129 
01130         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
01131                        HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
01132                        "User-Name from Access-Accept '%s'",
01133                        sm->identity ? (char *) sm->identity : "N/A",
01134                        (char *) identity);
01135 
01136         os_free(sm->identity);
01137         sm->identity = identity;
01138         sm->identity_len = len;
01139 }
01140 
01141 
01142 struct sta_id_search {
01143         u8 identifier;
01144         struct eapol_state_machine *sm;
01145 };
01146 
01147 
01148 static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
01149                                                struct sta_info *sta,
01150                                                void *ctx)
01151 {
01152         struct sta_id_search *id_search = ctx;
01153         struct eapol_state_machine *sm = sta->eapol_sm;
01154 
01155         if (sm && sm->radius_identifier >= 0 &&
01156             sm->radius_identifier == id_search->identifier) {
01157                 id_search->sm = sm;
01158                 return 1;
01159         }
01160         return 0;
01161 }
01162 
01163 
01164 static struct eapol_state_machine *
01165 ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
01166 {
01167         struct sta_id_search id_search;
01168         id_search.identifier = identifier;
01169         id_search.sm = NULL;
01170         ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
01171         return id_search.sm;
01172 }
01173 
01174 
01184 static RadiusRxResult
01185 ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
01186                         const u8 *shared_secret, size_t shared_secret_len,
01187                         void *data)
01188 {
01189         struct hostapd_data *hapd = data;
01190         struct sta_info *sta;
01191         u32 session_timeout = 0, termination_action, acct_interim_interval;
01192         int session_timeout_set, old_vlanid = 0;
01193         struct eapol_state_machine *sm;
01194         int override_eapReq = 0;
01195         struct radius_hdr *hdr = radius_msg_get_hdr(msg);
01196 
01197         sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
01198         if (sm == NULL) {
01199                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
01200                            "station for this RADIUS message");
01201                 return RADIUS_RX_UNKNOWN;
01202         }
01203         sta = sm->sta;
01204 
01205         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
01206          * present when packet contains an EAP-Message attribute */
01207         if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
01208             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
01209                                 0) < 0 &&
01210             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
01211                 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
01212                            "Message-Authenticator since it does not include "
01213                            "EAP-Message");
01214         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
01215                                      req, 1)) {
01216                 printf("Incoming RADIUS packet did not have correct "
01217                        "Message-Authenticator - dropped\n");
01218                 return RADIUS_RX_INVALID_AUTHENTICATOR;
01219         }
01220 
01221         if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
01222             hdr->code != RADIUS_CODE_ACCESS_REJECT &&
01223             hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
01224                 printf("Unknown RADIUS message code\n");
01225                 return RADIUS_RX_UNKNOWN;
01226         }
01227 
01228         sm->radius_identifier = -1;
01229         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
01230                    MAC2STR(sta->addr));
01231 
01232         radius_msg_free(sm->last_recv_radius);
01233         sm->last_recv_radius = msg;
01234 
01235         session_timeout_set =
01236                 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
01237                                            &session_timeout);
01238         if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
01239                                       &termination_action))
01240                 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
01241 
01242         if (hapd->conf->acct_interim_interval == 0 &&
01243             hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
01244             radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
01245                                       &acct_interim_interval) == 0) {
01246                 if (acct_interim_interval < 60) {
01247                         hostapd_logger(hapd, sta->addr,
01248                                        HOSTAPD_MODULE_IEEE8021X,
01249                                        HOSTAPD_LEVEL_INFO,
01250                                        "ignored too small "
01251                                        "Acct-Interim-Interval %d",
01252                                        acct_interim_interval);
01253                 } else
01254                         sta->acct_interim_interval = acct_interim_interval;
01255         }
01256 
01257 
01258         switch (hdr->code) {
01259         case RADIUS_CODE_ACCESS_ACCEPT:
01260                 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
01261                         sta->vlan_id = 0;
01262 #ifndef CONFIG_NO_VLAN
01263                 else {
01264                         old_vlanid = sta->vlan_id;
01265                         sta->vlan_id = radius_msg_get_vlanid(msg);
01266                 }
01267                 if (sta->vlan_id > 0 &&
01268                     hostapd_get_vlan_id_ifname(hapd->conf->vlan,
01269                                                sta->vlan_id)) {
01270                         hostapd_logger(hapd, sta->addr,
01271                                        HOSTAPD_MODULE_RADIUS,
01272                                        HOSTAPD_LEVEL_INFO,
01273                                        "VLAN ID %d", sta->vlan_id);
01274                 } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
01275                         sta->eapol_sm->authFail = TRUE;
01276                         hostapd_logger(hapd, sta->addr,
01277                                        HOSTAPD_MODULE_IEEE8021X,
01278                                        HOSTAPD_LEVEL_INFO, "authentication "
01279                                        "server did not include required VLAN "
01280                                        "ID in Access-Accept");
01281                         break;
01282                 }
01283 #endif /* CONFIG_NO_VLAN */
01284 
01285                 if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
01286                         break;
01287 
01288                 /* RFC 3580, Ch. 3.17 */
01289                 if (session_timeout_set && termination_action ==
01290                     RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
01291                         sm->reAuthPeriod = session_timeout;
01292                 } else if (session_timeout_set)
01293                         ap_sta_session_timeout(hapd, sta, session_timeout);
01294 
01295                 sm->eap_if->aaaSuccess = TRUE;
01296                 override_eapReq = 1;
01297                 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
01298                                     shared_secret_len);
01299                 ieee802_1x_store_radius_class(hapd, sta, msg);
01300                 ieee802_1x_update_sta_identity(hapd, sta, msg);
01301                 if (sm->eap_if->eapKeyAvailable &&
01302                     wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
01303                                        session_timeout_set ?
01304                                        (int) session_timeout : -1, sm) == 0) {
01305                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
01306                                        HOSTAPD_LEVEL_DEBUG,
01307                                        "Added PMKSA cache entry");
01308                 }
01309                 break;
01310         case RADIUS_CODE_ACCESS_REJECT:
01311                 sm->eap_if->aaaFail = TRUE;
01312                 override_eapReq = 1;
01313                 break;
01314         case RADIUS_CODE_ACCESS_CHALLENGE:
01315                 sm->eap_if->aaaEapReq = TRUE;
01316                 if (session_timeout_set) {
01317                         /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
01318                         sm->eap_if->aaaMethodTimeout = session_timeout;
01319                         hostapd_logger(hapd, sm->addr,
01320                                        HOSTAPD_MODULE_IEEE8021X,
01321                                        HOSTAPD_LEVEL_DEBUG,
01322                                        "using EAP timeout of %d seconds (from "
01323                                        "RADIUS)",
01324                                        sm->eap_if->aaaMethodTimeout);
01325                 } else {
01326                         /*
01327                          * Use dynamic retransmission behavior per EAP
01328                          * specification.
01329                          */
01330                         sm->eap_if->aaaMethodTimeout = 0;
01331                 }
01332                 break;
01333         }
01334 
01335         ieee802_1x_decapsulate_radius(hapd, sta);
01336         if (override_eapReq)
01337                 sm->eap_if->aaaEapReq = FALSE;
01338 
01339         eapol_auth_step(sm);
01340 
01341         return RADIUS_RX_QUEUED;
01342 }
01343 #endif /* CONFIG_NO_RADIUS */
01344 
01345 
01346 void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
01347 {
01348         struct eapol_state_machine *sm = sta->eapol_sm;
01349         if (sm == NULL)
01350                 return;
01351 
01352         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
01353                        HOSTAPD_LEVEL_DEBUG, "aborting authentication");
01354 
01355 #ifndef CONFIG_NO_RADIUS
01356         radius_msg_free(sm->last_recv_radius);
01357         sm->last_recv_radius = NULL;
01358 #endif /* CONFIG_NO_RADIUS */
01359 
01360         if (sm->eap_if->eapTimeout) {
01361                 /*
01362                  * Disconnect the STA since it did not reply to the last EAP
01363                  * request and we cannot continue EAP processing (EAP-Failure
01364                  * could only be sent if the EAP peer actually replied).
01365                  */
01366                 sm->eap_if->portEnabled = FALSE;
01367                 ap_sta_disconnect(hapd, sta, sta->addr,
01368                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
01369         }
01370 }
01371 
01372 
01373 static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
01374 {
01375         struct eapol_authenticator *eapol = hapd->eapol_auth;
01376 
01377         if (hapd->conf->default_wep_key_len < 1)
01378                 return 0;
01379 
01380         os_free(eapol->default_wep_key);
01381         eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
01382         if (eapol->default_wep_key == NULL ||
01383             os_get_random(eapol->default_wep_key,
01384                           hapd->conf->default_wep_key_len)) {
01385                 printf("Could not generate random WEP key.\n");
01386                 os_free(eapol->default_wep_key);
01387                 eapol->default_wep_key = NULL;
01388                 return -1;
01389         }
01390 
01391         wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
01392                         eapol->default_wep_key,
01393                         hapd->conf->default_wep_key_len);
01394 
01395         return 0;
01396 }
01397 
01398 
01399 static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
01400                                         struct sta_info *sta, void *ctx)
01401 {
01402         if (sta->eapol_sm) {
01403                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
01404                 eapol_auth_step(sta->eapol_sm);
01405         }
01406         return 0;
01407 }
01408 
01409 
01410 static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
01411 {
01412         struct hostapd_data *hapd = eloop_ctx;
01413         struct eapol_authenticator *eapol = hapd->eapol_auth;
01414 
01415         if (eapol->default_wep_key_idx >= 3)
01416                 eapol->default_wep_key_idx =
01417                         hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
01418         else
01419                 eapol->default_wep_key_idx++;
01420 
01421         wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
01422                    eapol->default_wep_key_idx);
01423                       
01424         if (ieee802_1x_rekey_broadcast(hapd)) {
01425                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
01426                                HOSTAPD_LEVEL_WARNING, "failed to generate a "
01427                                "new broadcast key");
01428                 os_free(eapol->default_wep_key);
01429                 eapol->default_wep_key = NULL;
01430                 return;
01431         }
01432 
01433         /* TODO: Could setup key for RX here, but change default TX keyid only
01434          * after new broadcast key has been sent to all stations. */
01435         if (hapd->drv.set_key(hapd->conf->iface, hapd, WPA_ALG_WEP, NULL,
01436                               eapol->default_wep_key_idx, 1, NULL, 0,
01437                               eapol->default_wep_key,
01438                               hapd->conf->default_wep_key_len)) {
01439                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
01440                                HOSTAPD_LEVEL_WARNING, "failed to configure a "
01441                                "new broadcast key");
01442                 os_free(eapol->default_wep_key);
01443                 eapol->default_wep_key = NULL;
01444                 return;
01445         }
01446 
01447         ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
01448 
01449         if (hapd->conf->wep_rekeying_period > 0) {
01450                 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
01451                                        ieee802_1x_rekey, hapd, NULL);
01452         }
01453 }
01454 
01455 
01456 static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
01457                                   const u8 *data, size_t datalen)
01458 {
01459         ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
01460 }
01461 
01462 
01463 static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
01464                                 const u8 *data, size_t datalen)
01465 {
01466 #ifndef CONFIG_NO_RADIUS
01467         struct hostapd_data *hapd = ctx;
01468         struct sta_info *sta = sta_ctx;
01469 
01470         ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
01471 #endif /* CONFIG_NO_RADIUS */
01472 }
01473 
01474 
01475 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
01476                                  int preauth)
01477 {
01478         struct hostapd_data *hapd = ctx;
01479         struct sta_info *sta = sta_ctx;
01480         if (preauth)
01481                 rsn_preauth_finished(hapd, sta, success);
01482         else
01483                 ieee802_1x_finished(hapd, sta, success);
01484 }
01485 
01486 
01487 static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
01488                                    size_t identity_len, int phase2,
01489                                    struct eap_user *user)
01490 {
01491         struct hostapd_data *hapd = ctx;
01492         const struct hostapd_eap_user *eap_user;
01493         int i, count;
01494 
01495         eap_user = hostapd_get_eap_user(hapd->conf, identity,
01496                                         identity_len, phase2);
01497         if (eap_user == NULL)
01498                 return -1;
01499 
01500         os_memset(user, 0, sizeof(*user));
01501         user->phase2 = phase2;
01502         count = EAP_USER_MAX_METHODS;
01503         if (count > EAP_MAX_METHODS)
01504                 count = EAP_MAX_METHODS;
01505         for (i = 0; i < count; i++) {
01506                 user->methods[i].vendor = eap_user->methods[i].vendor;
01507                 user->methods[i].method = eap_user->methods[i].method;
01508         }
01509 
01510         if (eap_user->password) {
01511                 user->password = os_malloc(eap_user->password_len);
01512                 if (user->password == NULL)
01513                         return -1;
01514                 os_memcpy(user->password, eap_user->password,
01515                           eap_user->password_len);
01516                 user->password_len = eap_user->password_len;
01517         }
01518         user->force_version = eap_user->force_version;
01519         user->ttls_auth = eap_user->ttls_auth;
01520 
01521         return 0;
01522 }
01523 
01524 
01525 static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
01526 {
01527         struct hostapd_data *hapd = ctx;
01528         struct sta_info *sta;
01529         sta = ap_get_sta(hapd, addr);
01530         if (sta == NULL || sta->eapol_sm == NULL)
01531                 return 0;
01532         return 1;
01533 }
01534 
01535 
01536 static void ieee802_1x_logger(void *ctx, const u8 *addr,
01537                               eapol_logger_level level, const char *txt)
01538 {
01539 #ifndef CONFIG_NO_HOSTAPD_LOGGER
01540         struct hostapd_data *hapd = ctx;
01541         int hlevel;
01542 
01543         switch (level) {
01544         case EAPOL_LOGGER_WARNING:
01545                 hlevel = HOSTAPD_LEVEL_WARNING;
01546                 break;
01547         case EAPOL_LOGGER_INFO:
01548                 hlevel = HOSTAPD_LEVEL_INFO;
01549                 break;
01550         case EAPOL_LOGGER_DEBUG:
01551         default:
01552                 hlevel = HOSTAPD_LEVEL_DEBUG;
01553                 break;
01554         }
01555 
01556         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
01557                        txt);
01558 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
01559 }
01560 
01561 
01562 static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
01563                                            int authorized)
01564 {
01565         struct hostapd_data *hapd = ctx;
01566         struct sta_info *sta = sta_ctx;
01567         ieee802_1x_set_sta_authorized(hapd, sta, authorized);
01568 }
01569 
01570 
01571 static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
01572 {
01573         struct hostapd_data *hapd = ctx;
01574         struct sta_info *sta = sta_ctx;
01575         ieee802_1x_abort_auth(hapd, sta);
01576 }
01577 
01578 
01579 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
01580 {
01581         struct hostapd_data *hapd = ctx;
01582         struct sta_info *sta = sta_ctx;
01583         ieee802_1x_tx_key(hapd, sta);
01584 }
01585 
01586 
01587 static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
01588                                    enum eapol_event type)
01589 {
01590         /* struct hostapd_data *hapd = ctx; */
01591         struct sta_info *sta = sta_ctx;
01592         switch (type) {
01593         case EAPOL_AUTH_SM_CHANGE:
01594                 wpa_auth_sm_notify(sta->wpa_sm);
01595                 break;
01596         case EAPOL_AUTH_REAUTHENTICATE:
01597                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
01598                 break;
01599         }
01600 }
01601 
01602 
01603 int ieee802_1x_init(struct hostapd_data *hapd)
01604 {
01605         int i;
01606         struct eapol_auth_config conf;
01607         struct eapol_auth_cb cb;
01608 
01609         os_memset(&conf, 0, sizeof(conf));
01610         conf.ctx = hapd;
01611         conf.eap_reauth_period = hapd->conf->eap_reauth_period;
01612         conf.wpa = hapd->conf->wpa;
01613         conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
01614         conf.eap_server = hapd->conf->eap_server;
01615         conf.ssl_ctx = hapd->ssl_ctx;
01616         conf.msg_ctx = hapd->msg_ctx;
01617         conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
01618         conf.eap_req_id_text = hapd->conf->eap_req_id_text;
01619         conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
01620         conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
01621         conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
01622         conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
01623         conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
01624         conf.eap_fast_prov = hapd->conf->eap_fast_prov;
01625         conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
01626         conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
01627         conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
01628         conf.tnc = hapd->conf->tnc;
01629         conf.wps = hapd->wps;
01630 
01631         os_memset(&cb, 0, sizeof(cb));
01632         cb.eapol_send = ieee802_1x_eapol_send;
01633         cb.aaa_send = ieee802_1x_aaa_send;
01634         cb.finished = _ieee802_1x_finished;
01635         cb.get_eap_user = ieee802_1x_get_eap_user;
01636         cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
01637         cb.logger = ieee802_1x_logger;
01638         cb.set_port_authorized = ieee802_1x_set_port_authorized;
01639         cb.abort_auth = _ieee802_1x_abort_auth;
01640         cb.tx_key = _ieee802_1x_tx_key;
01641         cb.eapol_event = ieee802_1x_eapol_event;
01642 
01643         hapd->eapol_auth = eapol_auth_init(&conf, &cb);
01644         if (hapd->eapol_auth == NULL)
01645                 return -1;
01646 
01647         if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
01648             hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
01649                 return -1;
01650 
01651 #ifndef CONFIG_NO_RADIUS
01652         if (radius_client_register(hapd->radius, RADIUS_AUTH,
01653                                    ieee802_1x_receive_auth, hapd))
01654                 return -1;
01655 #endif /* CONFIG_NO_RADIUS */
01656 
01657         if (hapd->conf->default_wep_key_len) {
01658                 for (i = 0; i < 4; i++)
01659                         hapd->drv.set_key(hapd->conf->iface, hapd,
01660                                           WPA_ALG_NONE, NULL, i, 0, NULL, 0,
01661                                           NULL, 0);
01662 
01663                 ieee802_1x_rekey(hapd, NULL);
01664 
01665                 if (hapd->eapol_auth->default_wep_key == NULL)
01666                         return -1;
01667         }
01668 
01669         return 0;
01670 }
01671 
01672 
01673 void ieee802_1x_deinit(struct hostapd_data *hapd)
01674 {
01675         eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
01676 
01677         if (hapd->driver != NULL &&
01678             (hapd->conf->ieee802_1x || hapd->conf->wpa))
01679                 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
01680 
01681         eapol_auth_deinit(hapd->eapol_auth);
01682         hapd->eapol_auth = NULL;
01683 }
01684 
01685 
01686 int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
01687                          const u8 *buf, size_t len, int ack)
01688 {
01689         struct ieee80211_hdr *hdr;
01690         struct ieee802_1x_hdr *xhdr;
01691         struct ieee802_1x_eapol_key *key;
01692         u8 *pos;
01693         const unsigned char rfc1042_hdr[ETH_ALEN] =
01694                 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
01695 
01696         if (sta == NULL)
01697                 return -1;
01698         if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2 + sizeof(*xhdr))
01699                 return 0;
01700 
01701         hdr = (struct ieee80211_hdr *) buf;
01702         pos = (u8 *) (hdr + 1);
01703         if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
01704                 return 0;
01705         pos += sizeof(rfc1042_hdr);
01706         if (WPA_GET_BE16(pos) != ETH_P_PAE)
01707                 return 0;
01708         pos += 2;
01709 
01710         xhdr = (struct ieee802_1x_hdr *) pos;
01711         pos += sizeof(*xhdr);
01712 
01713         wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
01714                    "type=%d length=%d - ack=%d",
01715                    MAC2STR(sta->addr), xhdr->version, xhdr->type,
01716                    be_to_host16(xhdr->length), ack);
01717 
01718         /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
01719          * or Authenticator state machines, but EAPOL-Key packets are not
01720          * retransmitted in case of failure. Try to re-sent failed EAPOL-Key
01721          * packets couple of times because otherwise STA keys become
01722          * unsynchronized with AP. */
01723         if (xhdr->type == IEEE802_1X_TYPE_EAPOL_KEY && !ack &&
01724             pos + sizeof(*key) <= buf + len) {
01725                 key = (struct ieee802_1x_eapol_key *) pos;
01726                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
01727                                HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
01728                                "frame (%scast index=%d)",
01729                                key->key_index & BIT(7) ? "uni" : "broad",
01730                                key->key_index & ~BIT(7));
01731                 /* TODO: re-send EAPOL-Key couple of times (with short delay
01732                  * between them?). If all attempt fail, report error and
01733                  * deauthenticate STA so that it will get new keys when
01734                  * authenticating again (e.g., after returning in range).
01735                  * Separate limit/transmit state needed both for unicast and
01736                  * broadcast keys(?) */
01737         }
01738         /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
01739          * to here and change the key only if the EAPOL-Key packet was Acked.
01740          */
01741 
01742         return 1;
01743 }
01744 
01745 
01746 u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
01747 {
01748         if (sm == NULL || sm->identity == NULL)
01749                 return NULL;
01750 
01751         *len = sm->identity_len;
01752         return sm->identity;
01753 }
01754 
01755 
01756 u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
01757                                  int idx)
01758 {
01759         if (sm == NULL || sm->radius_class.attr == NULL ||
01760             idx >= (int) sm->radius_class.count)
01761                 return NULL;
01762 
01763         *len = sm->radius_class.attr[idx].len;
01764         return sm->radius_class.attr[idx].data;
01765 }
01766 
01767 
01768 const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
01769 {
01770         if (sm == NULL)
01771                 return NULL;
01772 
01773         *len = sm->eap_if->eapKeyDataLen;
01774         return sm->eap_if->eapKeyData;
01775 }
01776 
01777 
01778 void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
01779                                     int enabled)
01780 {
01781         if (sm == NULL)
01782                 return;
01783         sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
01784         eapol_auth_step(sm);
01785 }
01786 
01787 
01788 void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
01789                                   int valid)
01790 {
01791         if (sm == NULL)
01792                 return;
01793         sm->portValid = valid ? TRUE : FALSE;
01794         eapol_auth_step(sm);
01795 }
01796 
01797 
01798 void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
01799 {
01800         if (sm == NULL)
01801                 return;
01802         if (pre_auth)
01803                 sm->flags |= EAPOL_SM_PREAUTH;
01804         else
01805                 sm->flags &= ~EAPOL_SM_PREAUTH;
01806 }
01807 
01808 
01809 static const char * bool_txt(Boolean bool)
01810 {
01811         return bool ? "TRUE" : "FALSE";
01812 }
01813 
01814 
01815 int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
01816 {
01817         /* TODO */
01818         return 0;
01819 }
01820 
01821 
01822 int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
01823                            char *buf, size_t buflen)
01824 {
01825         int len = 0, ret;
01826         struct eapol_state_machine *sm = sta->eapol_sm;
01827 
01828         if (sm == NULL)
01829                 return 0;
01830 
01831         ret = os_snprintf(buf + len, buflen - len,
01832                           "dot1xPaePortNumber=%d\n"
01833                           "dot1xPaePortProtocolVersion=%d\n"
01834                           "dot1xPaePortCapabilities=1\n"
01835                           "dot1xPaePortInitialize=%d\n"
01836                           "dot1xPaePortReauthenticate=FALSE\n",
01837                           sta->aid,
01838                           EAPOL_VERSION,
01839                           sm->initialize);
01840         if (ret < 0 || (size_t) ret >= buflen - len)
01841                 return len;
01842         len += ret;
01843 
01844         /* dot1xAuthConfigTable */
01845         ret = os_snprintf(buf + len, buflen - len,
01846                           "dot1xAuthPaeState=%d\n"
01847                           "dot1xAuthBackendAuthState=%d\n"
01848                           "dot1xAuthAdminControlledDirections=%d\n"
01849                           "dot1xAuthOperControlledDirections=%d\n"
01850                           "dot1xAuthAuthControlledPortStatus=%d\n"
01851                           "dot1xAuthAuthControlledPortControl=%d\n"
01852                           "dot1xAuthQuietPeriod=%u\n"
01853                           "dot1xAuthServerTimeout=%u\n"
01854                           "dot1xAuthReAuthPeriod=%u\n"
01855                           "dot1xAuthReAuthEnabled=%s\n"
01856                           "dot1xAuthKeyTxEnabled=%s\n",
01857                           sm->auth_pae_state + 1,
01858                           sm->be_auth_state + 1,
01859                           sm->adminControlledDirections,
01860                           sm->operControlledDirections,
01861                           sm->authPortStatus,
01862                           sm->portControl,
01863                           sm->quietPeriod,
01864                           sm->serverTimeout,
01865                           sm->reAuthPeriod,
01866                           bool_txt(sm->reAuthEnabled),
01867                           bool_txt(sm->keyTxEnabled));
01868         if (ret < 0 || (size_t) ret >= buflen - len)
01869                 return len;
01870         len += ret;
01871 
01872         /* dot1xAuthStatsTable */
01873         ret = os_snprintf(buf + len, buflen - len,
01874                           "dot1xAuthEapolFramesRx=%u\n"
01875                           "dot1xAuthEapolFramesTx=%u\n"
01876                           "dot1xAuthEapolStartFramesRx=%u\n"
01877                           "dot1xAuthEapolLogoffFramesRx=%u\n"
01878                           "dot1xAuthEapolRespIdFramesRx=%u\n"
01879                           "dot1xAuthEapolRespFramesRx=%u\n"
01880                           "dot1xAuthEapolReqIdFramesTx=%u\n"
01881                           "dot1xAuthEapolReqFramesTx=%u\n"
01882                           "dot1xAuthInvalidEapolFramesRx=%u\n"
01883                           "dot1xAuthEapLengthErrorFramesRx=%u\n"
01884                           "dot1xAuthLastEapolFrameVersion=%u\n"
01885                           "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
01886                           sm->dot1xAuthEapolFramesRx,
01887                           sm->dot1xAuthEapolFramesTx,
01888                           sm->dot1xAuthEapolStartFramesRx,
01889                           sm->dot1xAuthEapolLogoffFramesRx,
01890                           sm->dot1xAuthEapolRespIdFramesRx,
01891                           sm->dot1xAuthEapolRespFramesRx,
01892                           sm->dot1xAuthEapolReqIdFramesTx,
01893                           sm->dot1xAuthEapolReqFramesTx,
01894                           sm->dot1xAuthInvalidEapolFramesRx,
01895                           sm->dot1xAuthEapLengthErrorFramesRx,
01896                           sm->dot1xAuthLastEapolFrameVersion,
01897                           MAC2STR(sm->addr));
01898         if (ret < 0 || (size_t) ret >= buflen - len)
01899                 return len;
01900         len += ret;
01901 
01902         /* dot1xAuthDiagTable */
01903         ret = os_snprintf(buf + len, buflen - len,
01904                           "dot1xAuthEntersConnecting=%u\n"
01905                           "dot1xAuthEapLogoffsWhileConnecting=%u\n"
01906                           "dot1xAuthEntersAuthenticating=%u\n"
01907                           "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
01908                           "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
01909                           "dot1xAuthAuthFailWhileAuthenticating=%u\n"
01910                           "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
01911                           "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
01912                           "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
01913                           "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
01914                           "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
01915                           "dot1xAuthBackendResponses=%u\n"
01916                           "dot1xAuthBackendAccessChallenges=%u\n"
01917                           "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
01918                           "dot1xAuthBackendAuthSuccesses=%u\n"
01919                           "dot1xAuthBackendAuthFails=%u\n",
01920                           sm->authEntersConnecting,
01921                           sm->authEapLogoffsWhileConnecting,
01922                           sm->authEntersAuthenticating,
01923                           sm->authAuthSuccessesWhileAuthenticating,
01924                           sm->authAuthTimeoutsWhileAuthenticating,
01925                           sm->authAuthFailWhileAuthenticating,
01926                           sm->authAuthEapStartsWhileAuthenticating,
01927                           sm->authAuthEapLogoffWhileAuthenticating,
01928                           sm->authAuthReauthsWhileAuthenticated,
01929                           sm->authAuthEapStartsWhileAuthenticated,
01930                           sm->authAuthEapLogoffWhileAuthenticated,
01931                           sm->backendResponses,
01932                           sm->backendAccessChallenges,
01933                           sm->backendOtherRequestsToSupplicant,
01934                           sm->backendAuthSuccesses,
01935                           sm->backendAuthFails);
01936         if (ret < 0 || (size_t) ret >= buflen - len)
01937                 return len;
01938         len += ret;
01939 
01940         /* dot1xAuthSessionStatsTable */
01941         ret = os_snprintf(buf + len, buflen - len,
01942                           /* TODO: dot1xAuthSessionOctetsRx */
01943                           /* TODO: dot1xAuthSessionOctetsTx */
01944                           /* TODO: dot1xAuthSessionFramesRx */
01945                           /* TODO: dot1xAuthSessionFramesTx */
01946                           "dot1xAuthSessionId=%08X-%08X\n"
01947                           "dot1xAuthSessionAuthenticMethod=%d\n"
01948                           "dot1xAuthSessionTime=%u\n"
01949                           "dot1xAuthSessionTerminateCause=999\n"
01950                           "dot1xAuthSessionUserName=%s\n",
01951                           sta->acct_session_id_hi, sta->acct_session_id_lo,
01952                           (wpa_key_mgmt_wpa_ieee8021x(
01953                                    wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
01954                           1 : 2,
01955                           (unsigned int) (time(NULL) -
01956                                           sta->acct_session_start),
01957                           sm->identity);
01958         if (ret < 0 || (size_t) ret >= buflen - len)
01959                 return len;
01960         len += ret;
01961 
01962         return len;
01963 }
01964 
01965 
01966 static void ieee802_1x_finished(struct hostapd_data *hapd,
01967                                 struct sta_info *sta, int success)
01968 {
01969         const u8 *key;
01970         size_t len;
01971         /* TODO: get PMKLifetime from WPA parameters */
01972         static const int dot11RSNAConfigPMKLifetime = 43200;
01973 
01974         key = ieee802_1x_get_key(sta->eapol_sm, &len);
01975         if (success && key && len >= PMK_LEN &&
01976             wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
01977                                sta->eapol_sm) == 0) {
01978                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
01979                                HOSTAPD_LEVEL_DEBUG,
01980                                "Added PMKSA cache entry (IEEE 802.1X)");
01981         }
01982 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:25:14