LCOV - code coverage report
Current view: top level - src/ap - ieee802_1x.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 1047 1282 81.7 %
Date: 2015-09-27 Functions: 61 62 98.4 %

          Line data    Source code
       1             : /*
       2             :  * hostapd / IEEE 802.1X-2004 Authenticator
       3             :  * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "utils/includes.h"
      10             : 
      11             : #include "utils/common.h"
      12             : #include "utils/eloop.h"
      13             : #include "crypto/md5.h"
      14             : #include "crypto/crypto.h"
      15             : #include "crypto/random.h"
      16             : #include "common/ieee802_11_defs.h"
      17             : #include "radius/radius.h"
      18             : #include "radius/radius_client.h"
      19             : #include "eap_server/eap.h"
      20             : #include "eap_common/eap_wsc_common.h"
      21             : #include "eapol_auth/eapol_auth_sm.h"
      22             : #include "eapol_auth/eapol_auth_sm_i.h"
      23             : #include "p2p/p2p.h"
      24             : #include "hostapd.h"
      25             : #include "accounting.h"
      26             : #include "sta_info.h"
      27             : #include "wpa_auth.h"
      28             : #include "preauth_auth.h"
      29             : #include "pmksa_cache_auth.h"
      30             : #include "ap_config.h"
      31             : #include "ap_drv_ops.h"
      32             : #include "wps_hostapd.h"
      33             : #include "hs20.h"
      34             : #include "ieee802_1x.h"
      35             : 
      36             : 
      37             : static void ieee802_1x_finished(struct hostapd_data *hapd,
      38             :                                 struct sta_info *sta, int success,
      39             :                                 int remediation);
      40             : 
      41             : 
      42        8383 : static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
      43             :                             u8 type, const u8 *data, size_t datalen)
      44             : {
      45             :         u8 *buf;
      46             :         struct ieee802_1x_hdr *xhdr;
      47             :         size_t len;
      48        8383 :         int encrypt = 0;
      49             : 
      50        8383 :         len = sizeof(*xhdr) + datalen;
      51        8383 :         buf = os_zalloc(len);
      52        8383 :         if (buf == NULL) {
      53           6 :                 wpa_printf(MSG_ERROR, "malloc() failed for "
      54             :                            "ieee802_1x_send(len=%lu)",
      55             :                            (unsigned long) len);
      56        8389 :                 return;
      57             :         }
      58             : 
      59        8377 :         xhdr = (struct ieee802_1x_hdr *) buf;
      60        8377 :         xhdr->version = hapd->conf->eapol_version;
      61        8377 :         xhdr->type = type;
      62        8377 :         xhdr->length = host_to_be16(datalen);
      63             : 
      64        8377 :         if (datalen > 0 && data != NULL)
      65        8377 :                 os_memcpy(xhdr + 1, data, datalen);
      66             : 
      67        8377 :         if (wpa_auth_pairwise_set(sta->wpa_sm))
      68         459 :                 encrypt = 1;
      69             : #ifdef CONFIG_TESTING_OPTIONS
      70        8377 :         if (hapd->ext_eapol_frame_io) {
      71         403 :                 size_t hex_len = 2 * len + 1;
      72         403 :                 char *hex = os_malloc(hex_len);
      73             : 
      74         403 :                 if (hex) {
      75         403 :                         wpa_snprintf_hex(hex, hex_len, buf, len);
      76        2418 :                         wpa_msg(hapd->msg_ctx, MSG_INFO,
      77             :                                 "EAPOL-TX " MACSTR " %s",
      78        2418 :                                 MAC2STR(sta->addr), hex);
      79         403 :                         os_free(hex);
      80             :                 }
      81             :         } else
      82             : #endif /* CONFIG_TESTING_OPTIONS */
      83        7974 :         if (sta->flags & WLAN_STA_PREAUTH) {
      84          18 :                 rsn_preauth_send(hapd, sta, buf, len);
      85             :         } else {
      86       15912 :                 hostapd_drv_hapd_send_eapol(
      87        7956 :                         hapd, sta->addr, buf, len,
      88             :                         encrypt, hostapd_sta_flags_to_drv(sta->flags));
      89             :         }
      90             : 
      91        8377 :         os_free(buf);
      92             : }
      93             : 
      94             : 
      95        5149 : void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
      96             :                                    struct sta_info *sta, int authorized)
      97             : {
      98             :         int res;
      99             : 
     100        5149 :         if (sta->flags & WLAN_STA_PREAUTH)
     101        5159 :                 return;
     102             : 
     103        5139 :         if (authorized) {
     104        1563 :                 ap_sta_set_authorized(hapd, sta, 1);
     105        1563 :                 res = hostapd_set_authorized(hapd, sta, 1);
     106        1563 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
     107             :                                HOSTAPD_LEVEL_DEBUG, "authorizing port");
     108             :         } else {
     109        3576 :                 ap_sta_set_authorized(hapd, sta, 0);
     110        3576 :                 res = hostapd_set_authorized(hapd, sta, 0);
     111        3576 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
     112             :                                HOSTAPD_LEVEL_DEBUG, "unauthorizing port");
     113             :         }
     114             : 
     115        5139 :         if (res && errno != ENOENT) {
     116           7 :                 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
     117             :                            " flags for kernel driver (errno=%d).",
     118           7 :                            MAC2STR(sta->addr), errno);
     119             :         }
     120             : 
     121        5139 :         if (authorized) {
     122        1563 :                 os_get_reltime(&sta->connected_time);
     123        1563 :                 accounting_sta_start(hapd, sta);
     124             :         }
     125             : }
     126             : 
     127             : 
     128             : #ifndef CONFIG_FIPS
     129             : #ifndef CONFIG_NO_RC4
     130             : 
     131           6 : static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
     132             :                                   struct sta_info *sta,
     133             :                                   int idx, int broadcast,
     134             :                                   u8 *key_data, size_t key_len)
     135             : {
     136             :         u8 *buf, *ekey;
     137             :         struct ieee802_1x_hdr *hdr;
     138             :         struct ieee802_1x_eapol_key *key;
     139             :         size_t len, ekey_len;
     140           6 :         struct eapol_state_machine *sm = sta->eapol_sm;
     141             : 
     142           6 :         if (sm == NULL)
     143           0 :                 return;
     144             : 
     145           6 :         len = sizeof(*key) + key_len;
     146           6 :         buf = os_zalloc(sizeof(*hdr) + len);
     147           6 :         if (buf == NULL)
     148           0 :                 return;
     149             : 
     150           6 :         hdr = (struct ieee802_1x_hdr *) buf;
     151           6 :         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
     152           6 :         key->type = EAPOL_KEY_TYPE_RC4;
     153           6 :         WPA_PUT_BE16(key->key_length, key_len);
     154           6 :         wpa_get_ntp_timestamp(key->replay_counter);
     155             : 
     156           6 :         if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
     157           0 :                 wpa_printf(MSG_ERROR, "Could not get random numbers");
     158           0 :                 os_free(buf);
     159           0 :                 return;
     160             :         }
     161             : 
     162           6 :         key->key_index = idx | (broadcast ? 0 : BIT(7));
     163           6 :         if (hapd->conf->eapol_key_index_workaround) {
     164             :                 /* According to some information, WinXP Supplicant seems to
     165             :                  * interpret bit7 as an indication whether the key is to be
     166             :                  * activated, so make it possible to enable workaround that
     167             :                  * sets this bit for all keys. */
     168           0 :                 key->key_index |= BIT(7);
     169             :         }
     170             : 
     171             :         /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
     172             :          * MSK[32..63] is used to sign the message. */
     173           6 :         if (sm->eap_if->eapKeyData == NULL || sm->eap_if->eapKeyDataLen < 64) {
     174           0 :                 wpa_printf(MSG_ERROR, "No eapKeyData available for encrypting "
     175             :                            "and signing EAPOL-Key");
     176           0 :                 os_free(buf);
     177           0 :                 return;
     178             :         }
     179           6 :         os_memcpy((u8 *) (key + 1), key_data, key_len);
     180           6 :         ekey_len = sizeof(key->key_iv) + 32;
     181           6 :         ekey = os_malloc(ekey_len);
     182           6 :         if (ekey == NULL) {
     183           0 :                 wpa_printf(MSG_ERROR, "Could not encrypt key");
     184           0 :                 os_free(buf);
     185           0 :                 return;
     186             :         }
     187           6 :         os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
     188           6 :         os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
     189           6 :         rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
     190           6 :         os_free(ekey);
     191             : 
     192             :         /* This header is needed here for HMAC-MD5, but it will be regenerated
     193             :          * in ieee802_1x_send() */
     194           6 :         hdr->version = hapd->conf->eapol_version;
     195           6 :         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
     196           6 :         hdr->length = host_to_be16(len);
     197           6 :         hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
     198           6 :                  key->key_signature);
     199             : 
     200          42 :         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
     201          36 :                    " (%s index=%d)", MAC2STR(sm->addr),
     202             :                    broadcast ? "broadcast" : "unicast", idx);
     203           6 :         ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
     204           6 :         if (sta->eapol_sm)
     205           6 :                 sta->eapol_sm->dot1xAuthEapolFramesTx++;
     206           6 :         os_free(buf);
     207             : }
     208             : 
     209             : 
     210           4 : static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
     211             : {
     212           4 :         struct eapol_authenticator *eapol = hapd->eapol_auth;
     213           4 :         struct eapol_state_machine *sm = sta->eapol_sm;
     214             : 
     215           4 :         if (sm == NULL || !sm->eap_if->eapKeyData)
     216           0 :                 return;
     217             : 
     218          24 :         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
     219          24 :                    MAC2STR(sta->addr));
     220             : 
     221             : #ifndef CONFIG_NO_VLAN
     222           4 :         if (sta->vlan_id > 0 && sta->vlan_id <= MAX_VLAN_ID) {
     223           0 :                 wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
     224           0 :                 return;
     225             :         }
     226             : #endif /* CONFIG_NO_VLAN */
     227             : 
     228           4 :         if (eapol->default_wep_key) {
     229           3 :                 ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
     230             :                                       eapol->default_wep_key,
     231           3 :                                       hapd->conf->default_wep_key_len);
     232             :         }
     233             : 
     234           4 :         if (hapd->conf->individual_wep_key_len > 0) {
     235             :                 u8 *ikey;
     236           3 :                 ikey = os_malloc(hapd->conf->individual_wep_key_len);
     237           6 :                 if (ikey == NULL ||
     238           3 :                     random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
     239             :                 {
     240           0 :                         wpa_printf(MSG_ERROR, "Could not generate random "
     241             :                                    "individual WEP key.");
     242           0 :                         os_free(ikey);
     243           0 :                         return;
     244             :                 }
     245             : 
     246           3 :                 wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
     247           3 :                                 ikey, hapd->conf->individual_wep_key_len);
     248             : 
     249           3 :                 ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
     250           3 :                                       hapd->conf->individual_wep_key_len);
     251             : 
     252             :                 /* TODO: set encryption in TX callback, i.e., only after STA
     253             :                  * has ACKed EAPOL-Key frame */
     254           6 :                 if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
     255           3 :                                         sta->addr, 0, 1, NULL, 0, ikey,
     256           3 :                                         hapd->conf->individual_wep_key_len)) {
     257           0 :                         wpa_printf(MSG_ERROR, "Could not set individual WEP "
     258             :                                    "encryption.");
     259             :                 }
     260             : 
     261           3 :                 os_free(ikey);
     262             :         }
     263             : }
     264             : 
     265             : #endif /* CONFIG_NO_RC4 */
     266             : #endif /* CONFIG_FIPS */
     267             : 
     268             : 
     269        4191 : const char *radius_mode_txt(struct hostapd_data *hapd)
     270             : {
     271        4191 :         switch (hapd->iface->conf->hw_mode) {
     272             :         case HOSTAPD_MODE_IEEE80211AD:
     273           0 :                 return "802.11ad";
     274             :         case HOSTAPD_MODE_IEEE80211A:
     275           0 :                 return "802.11a";
     276             :         case HOSTAPD_MODE_IEEE80211G:
     277        4191 :                 return "802.11g";
     278             :         case HOSTAPD_MODE_IEEE80211B:
     279             :         default:
     280           0 :                 return "802.11b";
     281             :         }
     282             : }
     283             : 
     284             : 
     285        8382 : int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
     286             : {
     287             :         int i;
     288        8382 :         u8 rate = 0;
     289             : 
     290      108966 :         for (i = 0; i < sta->supported_rates_len; i++)
     291      100584 :                 if ((sta->supported_rates[i] & 0x7f) > rate)
     292       83820 :                         rate = sta->supported_rates[i] & 0x7f;
     293             : 
     294        8382 :         return rate;
     295             : }
     296             : 
     297             : 
     298             : #ifndef CONFIG_NO_RADIUS
     299        3768 : static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
     300             :                                       struct eapol_state_machine *sm,
     301             :                                       const u8 *eap, size_t len)
     302             : {
     303             :         const u8 *identity;
     304             :         size_t identity_len;
     305        3768 :         const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
     306             : 
     307        7536 :         if (len <= sizeof(struct eap_hdr) ||
     308        7520 :             (hdr->code == EAP_CODE_RESPONSE &&
     309        4819 :              eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
     310        1083 :             (hdr->code == EAP_CODE_INITIATE &&
     311        1083 :              eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
     312        1083 :             (hdr->code != EAP_CODE_RESPONSE &&
     313          16 :              hdr->code != EAP_CODE_INITIATE))
     314        5402 :                 return;
     315             : 
     316        1067 :         identity = eap_get_identity(sm->eap, &identity_len);
     317        1067 :         if (identity == NULL)
     318           0 :                 return;
     319             : 
     320             :         /* Save station identity for future RADIUS packets */
     321        1067 :         os_free(sm->identity);
     322        1067 :         sm->identity = (u8 *) dup_binstr(identity, identity_len);
     323        1067 :         if (sm->identity == NULL) {
     324           0 :                 sm->identity_len = 0;
     325           0 :                 return;
     326             :         }
     327             : 
     328        1067 :         sm->identity_len = identity_len;
     329        1067 :         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
     330             :                        HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
     331        1067 :         sm->dot1xAuthEapolRespIdFramesRx++;
     332             : }
     333             : 
     334             : 
     335        4130 : static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
     336             :                                           struct hostapd_radius_attr *req_attr,
     337             :                                           struct sta_info *sta,
     338             :                                           struct radius_msg *msg)
     339             : {
     340             :         u32 suite;
     341             :         int ver, val;
     342             : 
     343        4130 :         ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
     344        4130 :         val = wpa_auth_get_pairwise(sta->wpa_sm);
     345        4130 :         suite = wpa_cipher_to_suite(ver, val);
     346        8260 :         if (val != -1 &&
     347        4130 :             !hostapd_config_get_radius_attr(req_attr,
     348        4130 :                                             RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
     349        4130 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
     350             :                                        suite)) {
     351           0 :                 wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
     352           0 :                 return -1;
     353             :         }
     354             : 
     355        4160 :         suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
     356          30 :                                      hapd->conf->osen) ?
     357             :                                     WPA_PROTO_RSN : WPA_PROTO_WPA,
     358        4130 :                                     hapd->conf->wpa_group);
     359        4130 :         if (!hostapd_config_get_radius_attr(req_attr,
     360        4130 :                                             RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
     361        4130 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
     362             :                                        suite)) {
     363           0 :                 wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
     364           0 :                 return -1;
     365             :         }
     366             : 
     367        4130 :         val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
     368        4130 :         suite = wpa_akm_to_suite(val);
     369        8260 :         if (val != -1 &&
     370        4130 :             !hostapd_config_get_radius_attr(req_attr,
     371        4130 :                                             RADIUS_ATTR_WLAN_AKM_SUITE) &&
     372        4130 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
     373             :                                        suite)) {
     374           0 :                 wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
     375           0 :                 return -1;
     376             :         }
     377             : 
     378             : #ifdef CONFIG_IEEE80211W
     379        4130 :         if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
     380         634 :                 suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
     381         634 :                                             hapd->conf->group_mgmt_cipher);
     382         634 :                 if (!hostapd_config_get_radius_attr(
     383         634 :                             req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
     384         634 :                     !radius_msg_add_attr_int32(
     385             :                             msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
     386           0 :                         wpa_printf(MSG_ERROR,
     387             :                                    "Could not add WLAN-Group-Mgmt-Cipher");
     388           0 :                         return -1;
     389             :                 }
     390             :         }
     391             : #endif /* CONFIG_IEEE80211W */
     392             : 
     393        4130 :         return 0;
     394             : }
     395             : 
     396             : 
     397        4203 : static int add_common_radius_sta_attr(struct hostapd_data *hapd,
     398             :                                       struct hostapd_radius_attr *req_attr,
     399             :                                       struct sta_info *sta,
     400             :                                       struct radius_msg *msg)
     401             : {
     402             :         char buf[128];
     403             : 
     404        4203 :         if (!hostapd_config_get_radius_attr(req_attr,
     405        4203 :                                             RADIUS_ATTR_NAS_PORT) &&
     406        4203 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
     407           0 :                 wpa_printf(MSG_ERROR, "Could not add NAS-Port");
     408           0 :                 return -1;
     409             :         }
     410             : 
     411       25218 :         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
     412       25218 :                     MAC2STR(sta->addr));
     413        4203 :         buf[sizeof(buf) - 1] = '\0';
     414        4203 :         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
     415             :                                  (u8 *) buf, os_strlen(buf))) {
     416           0 :                 wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
     417           0 :                 return -1;
     418             :         }
     419             : 
     420        4203 :         if (sta->flags & WLAN_STA_PREAUTH) {
     421          12 :                 os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
     422             :                            sizeof(buf));
     423             :         } else {
     424       12573 :                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
     425        4191 :                             radius_sta_rate(hapd, sta) / 2,
     426        4191 :                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
     427             :                             radius_mode_txt(hapd));
     428        4191 :                 buf[sizeof(buf) - 1] = '\0';
     429             :         }
     430        4203 :         if (!hostapd_config_get_radius_attr(req_attr,
     431        4188 :                                             RADIUS_ATTR_CONNECT_INFO) &&
     432        4188 :             !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
     433             :                                  (u8 *) buf, os_strlen(buf))) {
     434           0 :                 wpa_printf(MSG_ERROR, "Could not add Connect-Info");
     435           0 :                 return -1;
     436             :         }
     437             : 
     438        4203 :         if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
     439        4203 :                 os_snprintf(buf, sizeof(buf), "%08X-%08X",
     440             :                             sta->acct_session_id_hi, sta->acct_session_id_lo);
     441        4203 :                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
     442             :                                          (u8 *) buf, os_strlen(buf))) {
     443           0 :                         wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
     444           0 :                         return -1;
     445             :                 }
     446             :         }
     447             : 
     448             : #ifdef CONFIG_IEEE80211R
     449        4215 :         if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
     450          24 :             sta->wpa_sm &&
     451          12 :             (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
     452          12 :              sta->auth_alg == WLAN_AUTH_FT) &&
     453          12 :             !hostapd_config_get_radius_attr(req_attr,
     454          12 :                                             RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
     455          12 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
     456          12 :                                        WPA_GET_BE16(
     457          12 :                                                hapd->conf->mobility_domain))) {
     458           0 :                 wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
     459           0 :                 return -1;
     460             :         }
     461             : #endif /* CONFIG_IEEE80211R */
     462             : 
     463        8333 :         if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
     464        4130 :             add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
     465           0 :                 return -1;
     466             : 
     467        4203 :         return 0;
     468             : }
     469             : 
     470             : 
     471        4319 : int add_common_radius_attr(struct hostapd_data *hapd,
     472             :                            struct hostapd_radius_attr *req_attr,
     473             :                            struct sta_info *sta,
     474             :                            struct radius_msg *msg)
     475             : {
     476             :         char buf[128];
     477             :         struct hostapd_radius_attr *attr;
     478             : 
     479        4319 :         if (!hostapd_config_get_radius_attr(req_attr,
     480        4319 :                                             RADIUS_ATTR_NAS_IP_ADDRESS) &&
     481        4352 :             hapd->conf->own_ip_addr.af == AF_INET &&
     482          33 :             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
     483          33 :                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
     484           0 :                 wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
     485           0 :                 return -1;
     486             :         }
     487             : 
     488             : #ifdef CONFIG_IPV6
     489        4319 :         if (!hostapd_config_get_radius_attr(req_attr,
     490        4319 :                                             RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
     491        4326 :             hapd->conf->own_ip_addr.af == AF_INET6 &&
     492           7 :             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
     493           7 :                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
     494           0 :                 wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
     495           0 :                 return -1;
     496             :         }
     497             : #endif /* CONFIG_IPV6 */
     498             : 
     499        4319 :         if (!hostapd_config_get_radius_attr(req_attr,
     500        4319 :                                             RADIUS_ATTR_NAS_IDENTIFIER) &&
     501        8033 :             hapd->conf->nas_identifier &&
     502        7428 :             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
     503        3714 :                                  (u8 *) hapd->conf->nas_identifier,
     504        3714 :                                  os_strlen(hapd->conf->nas_identifier))) {
     505           0 :                 wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
     506           0 :                 return -1;
     507             :         }
     508             : 
     509       34552 :         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
     510       25914 :                     MAC2STR(hapd->own_addr),
     511        4319 :                     wpa_ssid_txt(hapd->conf->ssid.ssid,
     512        4319 :                                  hapd->conf->ssid.ssid_len));
     513        4319 :         buf[sizeof(buf) - 1] = '\0';
     514        4319 :         if (!hostapd_config_get_radius_attr(req_attr,
     515        4319 :                                             RADIUS_ATTR_CALLED_STATION_ID) &&
     516        4319 :             !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
     517             :                                  (u8 *) buf, os_strlen(buf))) {
     518           0 :                 wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
     519           0 :                 return -1;
     520             :         }
     521             : 
     522        4319 :         if (!hostapd_config_get_radius_attr(req_attr,
     523        4319 :                                             RADIUS_ATTR_NAS_PORT_TYPE) &&
     524        4319 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
     525             :                                        RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
     526           0 :                 wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
     527           0 :                 return -1;
     528             :         }
     529             : 
     530             : #ifdef CONFIG_INTERWORKING
     531        4917 :         if (hapd->conf->interworking &&
     532         598 :             !is_zero_ether_addr(hapd->conf->hessid)) {
     533        1278 :                 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
     534        1278 :                             MAC2STR(hapd->conf->hessid));
     535         213 :                 buf[sizeof(buf) - 1] = '\0';
     536         213 :                 if (!hostapd_config_get_radius_attr(req_attr,
     537         213 :                                                     RADIUS_ATTR_WLAN_HESSID) &&
     538         213 :                     !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
     539             :                                          (u8 *) buf, os_strlen(buf))) {
     540           0 :                         wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
     541           0 :                         return -1;
     542             :                 }
     543             :         }
     544             : #endif /* CONFIG_INTERWORKING */
     545             : 
     546        4319 :         if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
     547           0 :                 return -1;
     548             : 
     549        4353 :         for (attr = req_attr; attr; attr = attr->next) {
     550          68 :                 if (!radius_msg_add_attr(msg, attr->type,
     551          34 :                                          wpabuf_head(attr->val),
     552          34 :                                          wpabuf_len(attr->val))) {
     553           0 :                         wpa_printf(MSG_ERROR, "Could not add RADIUS "
     554             :                                    "attribute");
     555           0 :                         return -1;
     556             :                 }
     557             :         }
     558             : 
     559        4319 :         return 0;
     560             : }
     561             : 
     562             : 
     563        3768 : static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
     564             :                                           struct sta_info *sta,
     565             :                                           const u8 *eap, size_t len)
     566             : {
     567             :         struct radius_msg *msg;
     568        3768 :         struct eapol_state_machine *sm = sta->eapol_sm;
     569             : 
     570        3768 :         if (sm == NULL)
     571           0 :                 return;
     572             : 
     573        3768 :         ieee802_1x_learn_identity(hapd, sm, eap, len);
     574             : 
     575        3768 :         wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
     576             :                    "packet");
     577             : 
     578        3768 :         sm->radius_identifier = radius_client_get_id(hapd->radius);
     579        3768 :         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
     580        3768 :                              sm->radius_identifier);
     581        3768 :         if (msg == NULL) {
     582           6 :                 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
     583           6 :                 return;
     584             :         }
     585             : 
     586        3762 :         radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
     587             : 
     588        7524 :         if (sm->identity &&
     589        7524 :             !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
     590        3762 :                                  sm->identity, sm->identity_len)) {
     591           0 :                 wpa_printf(MSG_INFO, "Could not add User-Name");
     592           0 :                 goto fail;
     593             :         }
     594             : 
     595        3762 :         if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
     596             :                                    msg) < 0)
     597           0 :                 goto fail;
     598             : 
     599             :         /* TODO: should probably check MTU from driver config; 2304 is max for
     600             :          * IEEE 802.11, but use 1400 to avoid problems with too large packets
     601             :          */
     602        3762 :         if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
     603        3762 :                                             RADIUS_ATTR_FRAMED_MTU) &&
     604        3762 :             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
     605           0 :                 wpa_printf(MSG_INFO, "Could not add Framed-MTU");
     606           0 :                 goto fail;
     607             :         }
     608             : 
     609        3762 :         if (!radius_msg_add_eap(msg, eap, len)) {
     610           0 :                 wpa_printf(MSG_INFO, "Could not add EAP-Message");
     611           0 :                 goto fail;
     612             :         }
     613             : 
     614             :         /* State attribute must be copied if and only if this packet is
     615             :          * Access-Request reply to the previous Access-Challenge */
     616        6584 :         if (sm->last_recv_radius &&
     617        2822 :             radius_msg_get_hdr(sm->last_recv_radius)->code ==
     618             :             RADIUS_CODE_ACCESS_CHALLENGE) {
     619        2697 :                 int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
     620             :                                                RADIUS_ATTR_STATE);
     621        2697 :                 if (res < 0) {
     622           0 :                         wpa_printf(MSG_INFO, "Could not copy State attribute from previous Access-Challenge");
     623           0 :                         goto fail;
     624             :                 }
     625        2697 :                 if (res > 0) {
     626        2486 :                         wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
     627             :                 }
     628             :         }
     629             : 
     630        3762 :         if (hapd->conf->radius_request_cui) {
     631             :                 const u8 *cui;
     632             :                 size_t cui_len;
     633             :                 /* Add previously learned CUI or nul CUI to request CUI */
     634           6 :                 if (sm->radius_cui) {
     635           3 :                         cui = wpabuf_head(sm->radius_cui);
     636           3 :                         cui_len = wpabuf_len(sm->radius_cui);
     637             :                 } else {
     638           3 :                         cui = (const u8 *) "\0";
     639           3 :                         cui_len = 1;
     640             :                 }
     641           6 :                 if (!radius_msg_add_attr(msg,
     642             :                                          RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
     643             :                                          cui, cui_len)) {
     644           0 :                         wpa_printf(MSG_ERROR, "Could not add CUI");
     645           0 :                         goto fail;
     646             :                 }
     647             :         }
     648             : 
     649             : #ifdef CONFIG_HS20
     650        3762 :         if (hapd->conf->hs20) {
     651         598 :                 u8 ver = 1; /* Release 2 */
     652         598 :                 if (!radius_msg_add_wfa(
     653             :                             msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
     654             :                             &ver, 1)) {
     655           0 :                         wpa_printf(MSG_ERROR, "Could not add HS 2.0 AP "
     656             :                                    "version");
     657           0 :                         goto fail;
     658             :                 }
     659             : 
     660         598 :                 if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
     661             :                         const u8 *pos;
     662             :                         u8 buf[3];
     663             :                         u16 id;
     664         568 :                         pos = wpabuf_head_u8(sta->hs20_ie);
     665         568 :                         buf[0] = (*pos) >> 4;
     666        1136 :                         if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
     667         568 :                             wpabuf_len(sta->hs20_ie) >= 3)
     668         568 :                                 id = WPA_GET_LE16(pos + 1);
     669             :                         else
     670           0 :                                 id = 0;
     671         568 :                         WPA_PUT_BE16(buf + 1, id);
     672         568 :                         if (!radius_msg_add_wfa(
     673             :                                     msg,
     674             :                                     RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
     675             :                                     buf, sizeof(buf))) {
     676           0 :                                 wpa_printf(MSG_ERROR, "Could not add HS 2.0 "
     677             :                                            "STA version");
     678           0 :                                 goto fail;
     679             :                         }
     680             :                 }
     681             :         }
     682             : #endif /* CONFIG_HS20 */
     683             : 
     684        3762 :         if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
     685           2 :                 goto fail;
     686             : 
     687        3760 :         return;
     688             : 
     689             :  fail:
     690           2 :         radius_msg_free(msg);
     691             : }
     692             : #endif /* CONFIG_NO_RADIUS */
     693             : 
     694             : 
     695        6669 : static void handle_eap_response(struct hostapd_data *hapd,
     696             :                                 struct sta_info *sta, struct eap_hdr *eap,
     697             :                                 size_t len)
     698             : {
     699             :         u8 type, *data;
     700        6669 :         struct eapol_state_machine *sm = sta->eapol_sm;
     701        6669 :         if (sm == NULL)
     702           0 :                 return;
     703             : 
     704        6669 :         data = (u8 *) (eap + 1);
     705             : 
     706        6669 :         if (len < sizeof(*eap) + 1) {
     707           0 :                 wpa_printf(MSG_INFO, "handle_eap_response: too short response data");
     708           0 :                 return;
     709             :         }
     710             : 
     711        6669 :         sm->eap_type_supp = type = data[0];
     712             : 
     713       26676 :         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
     714             :                        HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
     715             :                        "id=%d len=%d) from STA: EAP Response-%s (%d)",
     716       20007 :                        eap->code, eap->identifier, be_to_host16(eap->length),
     717             :                        eap_server_get_name(0, type), type);
     718             : 
     719        6669 :         sm->dot1xAuthEapolRespFramesRx++;
     720             : 
     721        6669 :         wpabuf_free(sm->eap_if->eapRespData);
     722        6669 :         sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
     723        6669 :         sm->eapolEap = TRUE;
     724             : }
     725             : 
     726             : 
     727          21 : static void handle_eap_initiate(struct hostapd_data *hapd,
     728             :                                 struct sta_info *sta, struct eap_hdr *eap,
     729             :                                 size_t len)
     730             : {
     731             : #ifdef CONFIG_ERP
     732             :         u8 type, *data;
     733          21 :         struct eapol_state_machine *sm = sta->eapol_sm;
     734             : 
     735          21 :         if (sm == NULL)
     736           0 :                 return;
     737             : 
     738          21 :         if (len < sizeof(*eap) + 1) {
     739           0 :                 wpa_printf(MSG_INFO,
     740             :                            "handle_eap_initiate: too short response data");
     741           0 :                 return;
     742             :         }
     743             : 
     744          21 :         data = (u8 *) (eap + 1);
     745          21 :         type = data[0];
     746             : 
     747          84 :         hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
     748             :                        HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
     749             :                        "id=%d len=%d) from STA: EAP Initiate type %u",
     750          63 :                        eap->code, eap->identifier, be_to_host16(eap->length),
     751             :                        type);
     752             : 
     753          21 :         wpabuf_free(sm->eap_if->eapRespData);
     754          21 :         sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
     755          21 :         sm->eapolEap = TRUE;
     756             : #endif /* CONFIG_ERP */
     757             : }
     758             : 
     759             : 
     760             : /* Process incoming EAP packet from Supplicant */
     761        6691 : static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
     762             :                        u8 *buf, size_t len)
     763             : {
     764             :         struct eap_hdr *eap;
     765             :         u16 eap_len;
     766             : 
     767        6691 :         if (len < sizeof(*eap)) {
     768           0 :                 wpa_printf(MSG_INFO, "   too short EAP packet");
     769           0 :                 return;
     770             :         }
     771             : 
     772        6691 :         eap = (struct eap_hdr *) buf;
     773             : 
     774        6691 :         eap_len = be_to_host16(eap->length);
     775       20073 :         wpa_printf(MSG_DEBUG, "EAP: code=%d identifier=%d length=%d",
     776       13382 :                    eap->code, eap->identifier, eap_len);
     777        6691 :         if (eap_len < sizeof(*eap)) {
     778           0 :                 wpa_printf(MSG_DEBUG, "   Invalid EAP length");
     779           0 :                 return;
     780        6691 :         } else if (eap_len > len) {
     781           0 :                 wpa_printf(MSG_DEBUG, "   Too short frame to contain this EAP "
     782             :                            "packet");
     783           0 :                 return;
     784        6691 :         } else if (eap_len < len) {
     785           0 :                 wpa_printf(MSG_DEBUG, "   Ignoring %lu extra bytes after EAP "
     786             :                            "packet", (unsigned long) len - eap_len);
     787             :         }
     788             : 
     789        6691 :         switch (eap->code) {
     790             :         case EAP_CODE_REQUEST:
     791           1 :                 wpa_printf(MSG_DEBUG, " (request)");
     792           1 :                 return;
     793             :         case EAP_CODE_RESPONSE:
     794        6669 :                 wpa_printf(MSG_DEBUG, " (response)");
     795        6669 :                 handle_eap_response(hapd, sta, eap, eap_len);
     796        6669 :                 break;
     797             :         case EAP_CODE_SUCCESS:
     798           0 :                 wpa_printf(MSG_DEBUG, " (success)");
     799           0 :                 return;
     800             :         case EAP_CODE_FAILURE:
     801           0 :                 wpa_printf(MSG_DEBUG, " (failure)");
     802           0 :                 return;
     803             :         case EAP_CODE_INITIATE:
     804          21 :                 wpa_printf(MSG_DEBUG, " (initiate)");
     805          21 :                 handle_eap_initiate(hapd, sta, eap, eap_len);
     806          21 :                 break;
     807             :         case EAP_CODE_FINISH:
     808           0 :                 wpa_printf(MSG_DEBUG, " (finish)");
     809           0 :                 break;
     810             :         default:
     811           0 :                 wpa_printf(MSG_DEBUG, " (unknown code)");
     812           0 :                 return;
     813             :         }
     814             : }
     815             : 
     816             : 
     817             : static struct eapol_state_machine *
     818        1542 : ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
     819             : {
     820        1542 :         int flags = 0;
     821        1542 :         if (sta->flags & WLAN_STA_PREAUTH)
     822           6 :                 flags |= EAPOL_SM_PREAUTH;
     823        1542 :         if (sta->wpa_sm) {
     824        1061 :                 flags |= EAPOL_SM_USES_WPA;
     825        1061 :                 if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
     826          33 :                         flags |= EAPOL_SM_FROM_PMKSA_CACHE;
     827             :         }
     828        1542 :         return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
     829        1542 :                                 sta->wps_ie, sta->p2p_ie, sta,
     830        1542 :                                 sta->identity, sta->radius_cui);
     831             : }
     832             : 
     833             : 
     834             : /**
     835             :  * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
     836             :  * @hapd: hostapd BSS data
     837             :  * @sa: Source address (sender of the EAPOL frame)
     838             :  * @buf: EAPOL frame
     839             :  * @len: Length of buf in octets
     840             :  *
     841             :  * This function is called for each incoming EAPOL frame from the interface
     842             :  */
     843       10026 : void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
     844             :                         size_t len)
     845             : {
     846             :         struct sta_info *sta;
     847             :         struct ieee802_1x_hdr *hdr;
     848             :         struct ieee802_1x_eapol_key *key;
     849             :         u16 datalen;
     850             :         struct rsn_pmksa_cache_entry *pmksa;
     851             :         int key_mgmt;
     852             : 
     853       10117 :         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen &&
     854          91 :             !hapd->conf->wps_state)
     855           0 :                 return;
     856             : 
     857       60156 :         wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR,
     858       60156 :                    (unsigned long) len, MAC2STR(sa));
     859       10026 :         sta = ap_get_sta(hapd, sa);
     860       10034 :         if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
     861           8 :                      !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
     862          23 :                 wpa_printf(MSG_DEBUG, "IEEE 802.1X data frame from not "
     863             :                            "associated/Pre-authenticating STA");
     864          23 :                 return;
     865             :         }
     866             : 
     867       10003 :         if (len < sizeof(*hdr)) {
     868           0 :                 wpa_printf(MSG_INFO, "   too short IEEE 802.1X packet");
     869           0 :                 return;
     870             :         }
     871             : 
     872       10003 :         hdr = (struct ieee802_1x_hdr *) buf;
     873       10003 :         datalen = be_to_host16(hdr->length);
     874       30009 :         wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
     875       20006 :                    hdr->version, hdr->type, datalen);
     876             : 
     877       10003 :         if (len - sizeof(*hdr) < datalen) {
     878           1 :                 wpa_printf(MSG_INFO, "   frame too short for this IEEE 802.1X packet");
     879           1 :                 if (sta->eapol_sm)
     880           0 :                         sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
     881           1 :                 return;
     882             :         }
     883       10002 :         if (len - sizeof(*hdr) > datalen) {
     884           1 :                 wpa_printf(MSG_DEBUG, "   ignoring %lu extra octets after "
     885             :                            "IEEE 802.1X packet",
     886           1 :                            (unsigned long) len - sizeof(*hdr) - datalen);
     887             :         }
     888             : 
     889       10002 :         if (sta->eapol_sm) {
     890        8309 :                 sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
     891        8309 :                 sta->eapol_sm->dot1xAuthEapolFramesRx++;
     892             :         }
     893             : 
     894       10002 :         key = (struct ieee802_1x_eapol_key *) (hdr + 1);
     895       16606 :         if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
     896        9796 :             hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
     897        6298 :             (key->type == EAPOL_KEY_TYPE_WPA ||
     898        3106 :              key->type == EAPOL_KEY_TYPE_RSN)) {
     899        3191 :                 wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
     900             :                             sizeof(*hdr) + datalen);
     901        3191 :                 return;
     902             :         }
     903             : 
     904        9038 :         if (!hapd->conf->ieee802_1x && !hapd->conf->osen &&
     905        2227 :             !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
     906           1 :                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
     907             :                            "802.1X not enabled and WPS not used");
     908           1 :                 return;
     909             :         }
     910             : 
     911        6810 :         key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
     912        6810 :         if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
     913           0 :                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore EAPOL message - "
     914             :                            "STA is using PSK");
     915           0 :                 return;
     916             :         }
     917             : 
     918        6810 :         if (!sta->eapol_sm) {
     919           2 :                 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
     920           2 :                 if (!sta->eapol_sm)
     921           0 :                         return;
     922             : 
     923             : #ifdef CONFIG_WPS
     924           2 :                 if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
     925           0 :                         u32 wflags = sta->flags & (WLAN_STA_WPS |
     926             :                                                    WLAN_STA_WPS2 |
     927             :                                                    WLAN_STA_MAYBE_WPS);
     928           0 :                         if (wflags == WLAN_STA_MAYBE_WPS ||
     929             :                             wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
     930             :                                 /*
     931             :                                  * Delay EAPOL frame transmission until a
     932             :                                  * possible WPS STA initiates the handshake
     933             :                                  * with EAPOL-Start. Only allow the wait to be
     934             :                                  * skipped if the STA is known to support WPS
     935             :                                  * 2.0.
     936             :                                  */
     937           0 :                                 wpa_printf(MSG_DEBUG, "WPS: Do not start "
     938             :                                            "EAPOL until EAPOL-Start is "
     939             :                                            "received");
     940           0 :                                 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
     941             :                         }
     942             :                 }
     943             : #endif /* CONFIG_WPS */
     944             : 
     945           2 :                 sta->eapol_sm->eap_if->portEnabled = TRUE;
     946             :         }
     947             : 
     948             :         /* since we support version 1, we can ignore version field and proceed
     949             :          * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
     950             :         /* TODO: actually, we are not version 1 anymore.. However, Version 2
     951             :          * does not change frame contents, so should be ok to process frames
     952             :          * more or less identically. Some changes might be needed for
     953             :          * verification of fields. */
     954             : 
     955        6810 :         switch (hdr->type) {
     956             :         case IEEE802_1X_TYPE_EAP_PACKET:
     957        6691 :                 handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
     958        6691 :                 break;
     959             : 
     960             :         case IEEE802_1X_TYPE_EAPOL_START:
     961         117 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
     962             :                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Start "
     963             :                                "from STA");
     964         117 :                 sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
     965         117 :                 pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
     966         117 :                 if (pmksa) {
     967           1 :                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
     968             :                                        HOSTAPD_LEVEL_DEBUG, "cached PMKSA "
     969             :                                        "available - ignore it since "
     970             :                                        "STA sent EAPOL-Start");
     971           1 :                         wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
     972             :                 }
     973         117 :                 sta->eapol_sm->eapolStart = TRUE;
     974         117 :                 sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
     975         117 :                 eap_server_clear_identity(sta->eapol_sm->eap);
     976         117 :                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
     977         117 :                 break;
     978             : 
     979             :         case IEEE802_1X_TYPE_EAPOL_LOGOFF:
     980           2 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
     981             :                                HOSTAPD_LEVEL_DEBUG, "received EAPOL-Logoff "
     982             :                                "from STA");
     983           2 :                 sta->acct_terminate_cause =
     984             :                         RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
     985           2 :                 accounting_sta_stop(hapd, sta);
     986           2 :                 sta->eapol_sm->eapolLogoff = TRUE;
     987           2 :                 sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
     988           2 :                 eap_server_clear_identity(sta->eapol_sm->eap);
     989           2 :                 break;
     990             : 
     991             :         case IEEE802_1X_TYPE_EAPOL_KEY:
     992           0 :                 wpa_printf(MSG_DEBUG, "   EAPOL-Key");
     993           0 :                 if (!ap_sta_is_authorized(sta)) {
     994           0 :                         wpa_printf(MSG_DEBUG, "   Dropped key data from "
     995             :                                    "unauthorized Supplicant");
     996           0 :                         break;
     997             :                 }
     998           0 :                 break;
     999             : 
    1000             :         case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
    1001           0 :                 wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
    1002             :                 /* TODO: implement support for this; show data */
    1003           0 :                 break;
    1004             : 
    1005             :         default:
    1006           0 :                 wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
    1007           0 :                 sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
    1008           0 :                 break;
    1009             :         }
    1010             : 
    1011        6810 :         eapol_auth_step(sta->eapol_sm);
    1012             : }
    1013             : 
    1014             : 
    1015             : /**
    1016             :  * ieee802_1x_new_station - Start IEEE 802.1X authentication
    1017             :  * @hapd: hostapd BSS data
    1018             :  * @sta: The station
    1019             :  *
    1020             :  * This function is called to start IEEE 802.1X authentication when a new
    1021             :  * station completes IEEE 802.11 association.
    1022             :  */
    1023        3312 : void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
    1024             : {
    1025             :         struct rsn_pmksa_cache_entry *pmksa;
    1026        3312 :         int reassoc = 1;
    1027        3312 :         int force_1x = 0;
    1028             :         int key_mgmt;
    1029             : 
    1030             : #ifdef CONFIG_WPS
    1031        4121 :         if (hapd->conf->wps_state &&
    1032        2391 :             ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
    1033         809 :              (sta->flags & WLAN_STA_WPS))) {
    1034             :                 /*
    1035             :                  * Need to enable IEEE 802.1X/EAPOL state machines for possible
    1036             :                  * WPS handshake even if IEEE 802.1X/EAPOL is not used for
    1037             :                  * authentication in this BSS.
    1038             :                  */
    1039         462 :                 force_1x = 1;
    1040             :         }
    1041             : #endif /* CONFIG_WPS */
    1042             : 
    1043        3312 :         if (!force_1x && !hapd->conf->ieee802_1x && !hapd->conf->osen) {
    1044        1663 :                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - "
    1045             :                            "802.1X not enabled or forced for WPS");
    1046             :                 /*
    1047             :                  * Clear any possible EAPOL authenticator state to support
    1048             :                  * reassociation change from WPS to PSK.
    1049             :                  */
    1050        1663 :                 ieee802_1x_free_station(sta);
    1051        1663 :                 return;
    1052             :         }
    1053             : 
    1054        1649 :         key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
    1055        1649 :         if (key_mgmt != -1 && wpa_key_mgmt_wpa_psk(key_mgmt)) {
    1056           6 :                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
    1057             :                 /*
    1058             :                  * Clear any possible EAPOL authenticator state to support
    1059             :                  * reassociation change from WPA-EAP to PSK.
    1060             :                  */
    1061           6 :                 ieee802_1x_free_station(sta);
    1062           6 :                 return;
    1063             :         }
    1064             : 
    1065        1643 :         if (sta->eapol_sm == NULL) {
    1066        1540 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1067             :                                HOSTAPD_LEVEL_DEBUG, "start authentication");
    1068        1540 :                 sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
    1069        1540 :                 if (sta->eapol_sm == NULL) {
    1070           1 :                         hostapd_logger(hapd, sta->addr,
    1071             :                                        HOSTAPD_MODULE_IEEE8021X,
    1072             :                                        HOSTAPD_LEVEL_INFO,
    1073             :                                        "failed to allocate state machine");
    1074           1 :                         return;
    1075             :                 }
    1076        1539 :                 reassoc = 0;
    1077             :         }
    1078             : 
    1079             : #ifdef CONFIG_WPS
    1080        1642 :         sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
    1081        2104 :         if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
    1082         462 :             !(sta->flags & WLAN_STA_WPS2)) {
    1083             :                 /*
    1084             :                  * Delay EAPOL frame transmission until a possible WPS STA
    1085             :                  * initiates the handshake with EAPOL-Start. Only allow the
    1086             :                  * wait to be skipped if the STA is known to support WPS 2.0.
    1087             :                  */
    1088           3 :                 wpa_printf(MSG_DEBUG, "WPS: Do not start EAPOL until "
    1089             :                            "EAPOL-Start is received");
    1090           3 :                 sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
    1091             :         }
    1092             : #endif /* CONFIG_WPS */
    1093             : 
    1094        1642 :         sta->eapol_sm->eap_if->portEnabled = TRUE;
    1095             : 
    1096             : #ifdef CONFIG_IEEE80211R
    1097        1642 :         if (sta->auth_alg == WLAN_AUTH_FT) {
    1098           4 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1099             :                                HOSTAPD_LEVEL_DEBUG,
    1100             :                                "PMK from FT - skip IEEE 802.1X/EAP");
    1101             :                 /* Setup EAPOL state machines to already authenticated state
    1102             :                  * because of existing FT information from R0KH. */
    1103           4 :                 sta->eapol_sm->keyRun = TRUE;
    1104           4 :                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
    1105           4 :                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
    1106           4 :                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
    1107           4 :                 sta->eapol_sm->authSuccess = TRUE;
    1108           4 :                 sta->eapol_sm->authFail = FALSE;
    1109           4 :                 if (sta->eapol_sm->eap)
    1110           4 :                         eap_sm_notify_cached(sta->eapol_sm->eap);
    1111             :                 /* TODO: get vlan_id from R0KH using RRB message */
    1112           4 :                 return;
    1113             :         }
    1114             : #endif /* CONFIG_IEEE80211R */
    1115             : 
    1116        1638 :         pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
    1117        1638 :         if (pmksa) {
    1118          33 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1119             :                                HOSTAPD_LEVEL_DEBUG,
    1120             :                                "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
    1121             :                 /* Setup EAPOL state machines to already authenticated state
    1122             :                  * because of existing PMKSA information in the cache. */
    1123          33 :                 sta->eapol_sm->keyRun = TRUE;
    1124          33 :                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
    1125          33 :                 sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
    1126          33 :                 sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
    1127          33 :                 sta->eapol_sm->authSuccess = TRUE;
    1128          33 :                 sta->eapol_sm->authFail = FALSE;
    1129          33 :                 if (sta->eapol_sm->eap)
    1130          33 :                         eap_sm_notify_cached(sta->eapol_sm->eap);
    1131          33 :                 pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
    1132          33 :                 ap_sta_bind_vlan(hapd, sta);
    1133             :         } else {
    1134        1605 :                 if (reassoc) {
    1135             :                         /*
    1136             :                          * Force EAPOL state machines to start
    1137             :                          * re-authentication without having to wait for the
    1138             :                          * Supplicant to send EAPOL-Start.
    1139             :                          */
    1140         103 :                         sta->eapol_sm->reAuthenticate = TRUE;
    1141             :                 }
    1142        1605 :                 eapol_auth_step(sta->eapol_sm);
    1143             :         }
    1144             : }
    1145             : 
    1146             : 
    1147        4969 : void ieee802_1x_free_station(struct sta_info *sta)
    1148             : {
    1149        4969 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1150             : 
    1151        4969 :         if (sm == NULL)
    1152        8397 :                 return;
    1153             : 
    1154        1541 :         sta->eapol_sm = NULL;
    1155             : 
    1156             : #ifndef CONFIG_NO_RADIUS
    1157        1342 :         radius_msg_free(sm->last_recv_radius);
    1158        1342 :         radius_free_class(&sm->radius_class);
    1159        1342 :         wpabuf_free(sm->radius_cui);
    1160             : #endif /* CONFIG_NO_RADIUS */
    1161             : 
    1162        1541 :         os_free(sm->identity);
    1163        1541 :         eapol_auth_free(sm);
    1164             : }
    1165             : 
    1166             : 
    1167             : #ifndef CONFIG_NO_RADIUS
    1168        3743 : static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
    1169             :                                           struct sta_info *sta)
    1170             : {
    1171             :         struct wpabuf *eap;
    1172             :         const struct eap_hdr *hdr;
    1173        3743 :         int eap_type = -1;
    1174             :         char buf[64];
    1175             :         struct radius_msg *msg;
    1176        3743 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1177             : 
    1178        3743 :         if (sm == NULL || sm->last_recv_radius == NULL) {
    1179           0 :                 if (sm)
    1180           0 :                         sm->eap_if->aaaEapNoReq = TRUE;
    1181          18 :                 return;
    1182             :         }
    1183             : 
    1184        3743 :         msg = sm->last_recv_radius;
    1185             : 
    1186        3743 :         eap = radius_msg_get_eap(msg);
    1187        3743 :         if (eap == NULL) {
    1188             :                 /* RFC 3579, Chap. 2.6.3:
    1189             :                  * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
    1190             :                  * attribute */
    1191          18 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1192             :                                HOSTAPD_LEVEL_WARNING, "could not extract "
    1193             :                                "EAP-Message from RADIUS message");
    1194          18 :                 sm->eap_if->aaaEapNoReq = TRUE;
    1195          18 :                 return;
    1196             :         }
    1197             : 
    1198        3725 :         if (wpabuf_len(eap) < sizeof(*hdr)) {
    1199           0 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1200             :                                HOSTAPD_LEVEL_WARNING, "too short EAP packet "
    1201             :                                "received from authentication server");
    1202           0 :                 wpabuf_free(eap);
    1203           0 :                 sm->eap_if->aaaEapNoReq = TRUE;
    1204           0 :                 return;
    1205             :         }
    1206             : 
    1207        3725 :         if (wpabuf_len(eap) > sizeof(*hdr))
    1208        2865 :                 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
    1209             : 
    1210        3725 :         hdr = wpabuf_head(eap);
    1211        3725 :         switch (hdr->code) {
    1212             :         case EAP_CODE_REQUEST:
    1213        2841 :                 if (eap_type >= 0)
    1214        2841 :                         sm->eap_type_authsrv = eap_type;
    1215        2841 :                 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
    1216             :                             eap_server_get_name(0, eap_type), eap_type);
    1217        2841 :                 break;
    1218             :         case EAP_CODE_RESPONSE:
    1219           6 :                 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
    1220             :                             eap_server_get_name(0, eap_type), eap_type);
    1221           6 :                 break;
    1222             :         case EAP_CODE_SUCCESS:
    1223         656 :                 os_strlcpy(buf, "EAP Success", sizeof(buf));
    1224         656 :                 break;
    1225             :         case EAP_CODE_FAILURE:
    1226         206 :                 os_strlcpy(buf, "EAP Failure", sizeof(buf));
    1227         206 :                 break;
    1228             :         default:
    1229          16 :                 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
    1230          16 :                 break;
    1231             :         }
    1232        3725 :         buf[sizeof(buf) - 1] = '\0';
    1233       11175 :         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1234             :                        HOSTAPD_LEVEL_DEBUG, "decapsulated EAP packet (code=%d "
    1235             :                        "id=%d len=%d) from RADIUS server: %s",
    1236       11175 :                        hdr->code, hdr->identifier, be_to_host16(hdr->length),
    1237             :                        buf);
    1238        3725 :         sm->eap_if->aaaEapReq = TRUE;
    1239             : 
    1240        3725 :         wpabuf_free(sm->eap_if->aaaEapReqData);
    1241        3725 :         sm->eap_if->aaaEapReqData = eap;
    1242             : }
    1243             : 
    1244             : 
    1245         667 : static void ieee802_1x_get_keys(struct hostapd_data *hapd,
    1246             :                                 struct sta_info *sta, struct radius_msg *msg,
    1247             :                                 struct radius_msg *req,
    1248             :                                 const u8 *shared_secret,
    1249             :                                 size_t shared_secret_len)
    1250             : {
    1251             :         struct radius_ms_mppe_keys *keys;
    1252         667 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1253         667 :         if (sm == NULL)
    1254         667 :                 return;
    1255             : 
    1256         667 :         keys = radius_msg_get_ms_keys(msg, req, shared_secret,
    1257             :                                       shared_secret_len);
    1258             : 
    1259        1321 :         if (keys && keys->send && keys->recv) {
    1260         654 :                 size_t len = keys->send_len + keys->recv_len;
    1261        1308 :                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
    1262         654 :                                 keys->send, keys->send_len);
    1263        1308 :                 wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
    1264         654 :                                 keys->recv, keys->recv_len);
    1265             : 
    1266         654 :                 os_free(sm->eap_if->aaaEapKeyData);
    1267         654 :                 sm->eap_if->aaaEapKeyData = os_malloc(len);
    1268         654 :                 if (sm->eap_if->aaaEapKeyData) {
    1269         653 :                         os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
    1270             :                                   keys->recv_len);
    1271         653 :                         os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
    1272             :                                   keys->send, keys->send_len);
    1273         653 :                         sm->eap_if->aaaEapKeyDataLen = len;
    1274         653 :                         sm->eap_if->aaaEapKeyAvailable = TRUE;
    1275             :                 }
    1276             :         } else {
    1277          13 :                 wpa_printf(MSG_DEBUG,
    1278             :                            "MS-MPPE: 1x_get_keys, could not get keys: %p  send: %p  recv: %p",
    1279             :                            keys, keys ? keys->send : NULL,
    1280             :                            keys ? keys->recv : NULL);
    1281             :         }
    1282             : 
    1283         667 :         if (keys) {
    1284         666 :                 os_free(keys->send);
    1285         666 :                 os_free(keys->recv);
    1286         666 :                 os_free(keys);
    1287             :         }
    1288             : }
    1289             : 
    1290             : 
    1291         667 : static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
    1292             :                                           struct sta_info *sta,
    1293             :                                           struct radius_msg *msg)
    1294             : {
    1295             :         u8 *attr_class;
    1296             :         size_t class_len;
    1297         667 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1298             :         int count, i;
    1299             :         struct radius_attr_data *nclass;
    1300             :         size_t nclass_count;
    1301             : 
    1302         667 :         if (!hapd->conf->radius->acct_server || hapd->radius == NULL ||
    1303             :             sm == NULL)
    1304        1104 :                 return;
    1305             : 
    1306         225 :         radius_free_class(&sm->radius_class);
    1307         225 :         count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
    1308         225 :         if (count <= 0)
    1309         220 :                 return;
    1310             : 
    1311           5 :         nclass = os_calloc(count, sizeof(struct radius_attr_data));
    1312           5 :         if (nclass == NULL)
    1313           0 :                 return;
    1314             : 
    1315           5 :         nclass_count = 0;
    1316             : 
    1317           5 :         attr_class = NULL;
    1318          10 :         for (i = 0; i < count; i++) {
    1319             :                 do {
    1320           5 :                         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
    1321             :                                                     &attr_class, &class_len,
    1322             :                                                     attr_class) < 0) {
    1323           0 :                                 i = count;
    1324           0 :                                 break;
    1325             :                         }
    1326           5 :                 } while (class_len < 1);
    1327             : 
    1328           5 :                 nclass[nclass_count].data = os_malloc(class_len);
    1329           5 :                 if (nclass[nclass_count].data == NULL)
    1330           0 :                         break;
    1331             : 
    1332           5 :                 os_memcpy(nclass[nclass_count].data, attr_class, class_len);
    1333           5 :                 nclass[nclass_count].len = class_len;
    1334           5 :                 nclass_count++;
    1335             :         }
    1336             : 
    1337           5 :         sm->radius_class.attr = nclass;
    1338           5 :         sm->radius_class.count = nclass_count;
    1339          30 :         wpa_printf(MSG_DEBUG, "IEEE 802.1X: Stored %lu RADIUS Class "
    1340             :                    "attributes for " MACSTR,
    1341             :                    (unsigned long) sm->radius_class.count,
    1342          30 :                    MAC2STR(sta->addr));
    1343             : }
    1344             : 
    1345             : 
    1346             : /* Update sta->identity based on User-Name attribute in Access-Accept */
    1347         667 : static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
    1348             :                                            struct sta_info *sta,
    1349             :                                            struct radius_msg *msg)
    1350             : {
    1351             :         u8 *buf, *identity;
    1352             :         size_t len;
    1353         667 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1354             : 
    1355         667 :         if (sm == NULL)
    1356         667 :                 return;
    1357             : 
    1358         667 :         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
    1359             :                                     NULL) < 0)
    1360         667 :                 return;
    1361             : 
    1362           0 :         identity = (u8 *) dup_binstr(buf, len);
    1363           0 :         if (identity == NULL)
    1364           0 :                 return;
    1365             : 
    1366           0 :         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1367             :                        HOSTAPD_LEVEL_DEBUG, "old identity '%s' updated with "
    1368             :                        "User-Name from Access-Accept '%s'",
    1369           0 :                        sm->identity ? (char *) sm->identity : "N/A",
    1370             :                        (char *) identity);
    1371             : 
    1372           0 :         os_free(sm->identity);
    1373           0 :         sm->identity = identity;
    1374           0 :         sm->identity_len = len;
    1375             : }
    1376             : 
    1377             : 
    1378             : /* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
    1379         667 : static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
    1380             :                                       struct sta_info *sta,
    1381             :                                       struct radius_msg *msg)
    1382             : {
    1383         667 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1384             :         struct wpabuf *cui;
    1385             :         u8 *buf;
    1386             :         size_t len;
    1387             : 
    1388         667 :         if (sm == NULL)
    1389         662 :                 return;
    1390             : 
    1391         667 :         if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
    1392             :                                     &buf, &len, NULL) < 0)
    1393         662 :                 return;
    1394             : 
    1395           5 :         cui = wpabuf_alloc_copy(buf, len);
    1396           5 :         if (cui == NULL)
    1397           0 :                 return;
    1398             : 
    1399           5 :         wpabuf_free(sm->radius_cui);
    1400           5 :         sm->radius_cui = cui;
    1401             : }
    1402             : 
    1403             : 
    1404             : #ifdef CONFIG_HS20
    1405             : 
    1406           2 : static void ieee802_1x_hs20_sub_rem(struct sta_info *sta, u8 *pos, size_t len)
    1407             : {
    1408           2 :         sta->remediation = 1;
    1409           2 :         os_free(sta->remediation_url);
    1410           2 :         if (len > 2) {
    1411           2 :                 sta->remediation_url = os_malloc(len);
    1412           2 :                 if (!sta->remediation_url)
    1413           2 :                         return;
    1414           2 :                 sta->remediation_method = pos[0];
    1415           2 :                 os_memcpy(sta->remediation_url, pos + 1, len - 1);
    1416           2 :                 sta->remediation_url[len - 1] = '\0';
    1417          16 :                 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
    1418             :                            "for " MACSTR " - server method %u URL %s",
    1419          14 :                            MAC2STR(sta->addr), sta->remediation_method,
    1420             :                            sta->remediation_url);
    1421             :         } else {
    1422           0 :                 sta->remediation_url = NULL;
    1423           0 :                 wpa_printf(MSG_DEBUG, "HS 2.0: Subscription remediation needed "
    1424           0 :                            "for " MACSTR, MAC2STR(sta->addr));
    1425             :         }
    1426             :         /* TODO: assign the STA into remediation VLAN or add filtering */
    1427             : }
    1428             : 
    1429             : 
    1430           1 : static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
    1431             :                                        struct sta_info *sta, u8 *pos,
    1432             :                                        size_t len)
    1433             : {
    1434           1 :         if (len < 3)
    1435           1 :                 return; /* Malformed information */
    1436           1 :         sta->hs20_deauth_requested = 1;
    1437           2 :         wpa_printf(MSG_DEBUG, "HS 2.0: Deauthentication request - Code %u  "
    1438             :                    "Re-auth Delay %u",
    1439           2 :                    *pos, WPA_GET_LE16(pos + 1));
    1440           1 :         wpabuf_free(sta->hs20_deauth_req);
    1441           1 :         sta->hs20_deauth_req = wpabuf_alloc(len + 1);
    1442           1 :         if (sta->hs20_deauth_req) {
    1443           1 :                 wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
    1444           1 :                 wpabuf_put_u8(sta->hs20_deauth_req, len - 3);
    1445           1 :                 wpabuf_put_data(sta->hs20_deauth_req, pos + 3, len - 3);
    1446             :         }
    1447           1 :         ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
    1448             : }
    1449             : 
    1450             : 
    1451           1 : static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
    1452             :                                          struct sta_info *sta, u8 *pos,
    1453             :                                          size_t len, int session_timeout)
    1454             : {
    1455             :         unsigned int swt;
    1456             :         int warning_time, beacon_int;
    1457             : 
    1458           1 :         if (len < 1)
    1459           0 :                 return; /* Malformed information */
    1460           1 :         os_free(sta->hs20_session_info_url);
    1461           1 :         sta->hs20_session_info_url = os_malloc(len);
    1462           1 :         if (sta->hs20_session_info_url == NULL)
    1463           0 :                 return;
    1464           1 :         swt = pos[0];
    1465           1 :         os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
    1466           1 :         sta->hs20_session_info_url[len - 1] = '\0';
    1467           1 :         wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
    1468             :                    "(session_timeout=%d)",
    1469             :                    sta->hs20_session_info_url, swt, session_timeout);
    1470           1 :         if (session_timeout < 0) {
    1471           0 :                 wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
    1472           0 :                 return;
    1473             :         }
    1474           1 :         if (swt == 255)
    1475           0 :                 swt = 1; /* Use one minute as the AP selected value */
    1476             : 
    1477           1 :         if ((unsigned int) session_timeout < swt * 60)
    1478           0 :                 warning_time = 0;
    1479             :         else
    1480           1 :                 warning_time = session_timeout - swt * 60;
    1481             : 
    1482           1 :         beacon_int = hapd->iconf->beacon_int;
    1483           1 :         if (beacon_int < 1)
    1484           0 :                 beacon_int = 100; /* best guess */
    1485           1 :         sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
    1486           1 :         if (sta->hs20_disassoc_timer > 65535)
    1487           0 :                 sta->hs20_disassoc_timer = 65535;
    1488             : 
    1489           1 :         ap_sta_session_warning_timeout(hapd, sta, warning_time);
    1490             : }
    1491             : 
    1492             : #endif /* CONFIG_HS20 */
    1493             : 
    1494             : 
    1495         667 : static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
    1496             :                                   struct sta_info *sta,
    1497             :                                   struct radius_msg *msg,
    1498             :                                   int session_timeout)
    1499             : {
    1500             : #ifdef CONFIG_HS20
    1501             :         u8 *buf, *pos, *end, type, sublen;
    1502             :         size_t len;
    1503             : 
    1504         667 :         buf = NULL;
    1505         667 :         sta->remediation = 0;
    1506         667 :         sta->hs20_deauth_requested = 0;
    1507             : 
    1508             :         for (;;) {
    1509        1997 :                 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
    1510             :                                             &buf, &len, buf) < 0)
    1511         667 :                         break;
    1512        1330 :                 if (len < 6)
    1513           0 :                         continue;
    1514        1330 :                 pos = buf;
    1515        1330 :                 end = buf + len;
    1516        1330 :                 if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
    1517        1326 :                         continue;
    1518           4 :                 pos += 4;
    1519             : 
    1520           4 :                 type = *pos++;
    1521           4 :                 sublen = *pos++;
    1522           4 :                 if (sublen < 2)
    1523           0 :                         continue; /* invalid length */
    1524           4 :                 sublen -= 2; /* skip header */
    1525           4 :                 if (pos + sublen > end)
    1526           0 :                         continue; /* invalid WFA VSA */
    1527             : 
    1528           4 :                 switch (type) {
    1529             :                 case RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION:
    1530           2 :                         ieee802_1x_hs20_sub_rem(sta, pos, sublen);
    1531           2 :                         break;
    1532             :                 case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
    1533           1 :                         ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
    1534           1 :                         break;
    1535             :                 case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
    1536           1 :                         ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
    1537             :                                                      session_timeout);
    1538           1 :                         break;
    1539             :                 }
    1540        1330 :         }
    1541             : #endif /* CONFIG_HS20 */
    1542         667 : }
    1543             : 
    1544             : 
    1545             : struct sta_id_search {
    1546             :         u8 identifier;
    1547             :         struct eapol_state_machine *sm;
    1548             : };
    1549             : 
    1550             : 
    1551        3784 : static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
    1552             :                                                struct sta_info *sta,
    1553             :                                                void *ctx)
    1554             : {
    1555        3784 :         struct sta_id_search *id_search = ctx;
    1556        3784 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1557             : 
    1558        7538 :         if (sm && sm->radius_identifier >= 0 &&
    1559        3754 :             sm->radius_identifier == id_search->identifier) {
    1560        3746 :                 id_search->sm = sm;
    1561        3746 :                 return 1;
    1562             :         }
    1563          38 :         return 0;
    1564             : }
    1565             : 
    1566             : 
    1567             : static struct eapol_state_machine *
    1568        3746 : ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
    1569             : {
    1570             :         struct sta_id_search id_search;
    1571        3746 :         id_search.identifier = identifier;
    1572        3746 :         id_search.sm = NULL;
    1573        3746 :         ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
    1574        3746 :         return id_search.sm;
    1575             : }
    1576             : 
    1577             : 
    1578             : /**
    1579             :  * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
    1580             :  * @msg: RADIUS response message
    1581             :  * @req: RADIUS request message
    1582             :  * @shared_secret: RADIUS shared secret
    1583             :  * @shared_secret_len: Length of shared_secret in octets
    1584             :  * @data: Context data (struct hostapd_data *)
    1585             :  * Returns: Processing status
    1586             :  */
    1587             : static RadiusRxResult
    1588        3746 : ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
    1589             :                         const u8 *shared_secret, size_t shared_secret_len,
    1590             :                         void *data)
    1591             : {
    1592        3746 :         struct hostapd_data *hapd = data;
    1593             :         struct sta_info *sta;
    1594        3746 :         u32 session_timeout = 0, termination_action, acct_interim_interval;
    1595        3746 :         int session_timeout_set, vlan_id = 0;
    1596             :         struct eapol_state_machine *sm;
    1597        3746 :         int override_eapReq = 0;
    1598        3746 :         struct radius_hdr *hdr = radius_msg_get_hdr(msg);
    1599             : 
    1600        3746 :         sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
    1601        3746 :         if (sm == NULL) {
    1602           0 :                 wpa_printf(MSG_DEBUG, "IEEE 802.1X: Could not find matching "
    1603             :                            "station for this RADIUS message");
    1604           0 :                 return RADIUS_RX_UNKNOWN;
    1605             :         }
    1606        3746 :         sta = sm->sta;
    1607             : 
    1608             :         /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
    1609             :          * present when packet contains an EAP-Message attribute */
    1610        3848 :         if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
    1611         102 :             radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
    1612           0 :                                 0) < 0 &&
    1613           0 :             radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
    1614           0 :                 wpa_printf(MSG_DEBUG, "Allowing RADIUS Access-Reject without "
    1615             :                            "Message-Authenticator since it does not include "
    1616             :                            "EAP-Message");
    1617        3746 :         } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
    1618             :                                      req, 1)) {
    1619           3 :                 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
    1620           3 :                 return RADIUS_RX_INVALID_AUTHENTICATOR;
    1621             :         }
    1622             : 
    1623        6818 :         if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
    1624        6048 :             hdr->code != RADIUS_CODE_ACCESS_REJECT &&
    1625        2973 :             hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
    1626           0 :                 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
    1627           0 :                 return RADIUS_RX_UNKNOWN;
    1628             :         }
    1629             : 
    1630        3743 :         sm->radius_identifier = -1;
    1631       22458 :         wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
    1632       22458 :                    MAC2STR(sta->addr));
    1633             : 
    1634        3743 :         radius_msg_free(sm->last_recv_radius);
    1635        3743 :         sm->last_recv_radius = msg;
    1636             : 
    1637        3743 :         session_timeout_set =
    1638        3743 :                 !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
    1639             :                                            &session_timeout);
    1640        3743 :         if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
    1641             :                                       &termination_action))
    1642        3743 :                 termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
    1643             : 
    1644        7480 :         if (hapd->conf->acct_interim_interval == 0 &&
    1645        4403 :             hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
    1646         666 :             radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
    1647             :                                       &acct_interim_interval) == 0) {
    1648           0 :                 if (acct_interim_interval < 60) {
    1649           0 :                         hostapd_logger(hapd, sta->addr,
    1650             :                                        HOSTAPD_MODULE_IEEE8021X,
    1651             :                                        HOSTAPD_LEVEL_INFO,
    1652             :                                        "ignored too small "
    1653             :                                        "Acct-Interim-Interval %d",
    1654             :                                        acct_interim_interval);
    1655             :                 } else
    1656           0 :                         sta->acct_interim_interval = acct_interim_interval;
    1657             :         }
    1658             : 
    1659             : 
    1660        3743 :         switch (hdr->code) {
    1661             :         case RADIUS_CODE_ACCESS_ACCEPT:
    1662         668 :                 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
    1663         652 :                         vlan_id = 0;
    1664             : #ifndef CONFIG_NO_VLAN
    1665             :                 else
    1666          16 :                         vlan_id = radius_msg_get_vlanid(msg);
    1667         680 :                 if (vlan_id > 0 &&
    1668          12 :                     hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
    1669          12 :                         hostapd_logger(hapd, sta->addr,
    1670             :                                        HOSTAPD_MODULE_RADIUS,
    1671             :                                        HOSTAPD_LEVEL_INFO,
    1672             :                                        "VLAN ID %d", vlan_id);
    1673         656 :                 } else if (vlan_id > 0) {
    1674           0 :                         sta->eapol_sm->authFail = TRUE;
    1675           0 :                         hostapd_logger(hapd, sta->addr,
    1676             :                                        HOSTAPD_MODULE_RADIUS,
    1677             :                                        HOSTAPD_LEVEL_INFO,
    1678             :                                        "Invalid VLAN ID %d received from RADIUS server",
    1679             :                                        vlan_id);
    1680           0 :                         break;
    1681         656 :                 } else if (hapd->conf->ssid.dynamic_vlan ==
    1682             :                            DYNAMIC_VLAN_REQUIRED) {
    1683           1 :                         sta->eapol_sm->authFail = TRUE;
    1684           1 :                         hostapd_logger(hapd, sta->addr,
    1685             :                                        HOSTAPD_MODULE_IEEE8021X,
    1686             :                                        HOSTAPD_LEVEL_INFO, "authentication "
    1687             :                                        "server did not include required VLAN "
    1688             :                                        "ID in Access-Accept");
    1689           1 :                         break;
    1690             :                 }
    1691             : #endif /* CONFIG_NO_VLAN */
    1692             : 
    1693         667 :                 sta->vlan_id = vlan_id;
    1694        1330 :                 if ((sta->flags & WLAN_STA_ASSOC) &&
    1695         663 :                     ap_sta_bind_vlan(hapd, sta) < 0)
    1696           0 :                         break;
    1697             : 
    1698         667 :                 sta->session_timeout_set = !!session_timeout_set;
    1699         667 :                 sta->session_timeout = session_timeout;
    1700             : 
    1701             :                 /* RFC 3580, Ch. 3.17 */
    1702         667 :                 if (session_timeout_set && termination_action ==
    1703             :                     RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
    1704           0 :                         sm->reAuthPeriod = session_timeout;
    1705         667 :                 } else if (session_timeout_set)
    1706           9 :                         ap_sta_session_timeout(hapd, sta, session_timeout);
    1707             : 
    1708         667 :                 sm->eap_if->aaaSuccess = TRUE;
    1709         667 :                 override_eapReq = 1;
    1710         667 :                 ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
    1711             :                                     shared_secret_len);
    1712         667 :                 ieee802_1x_store_radius_class(hapd, sta, msg);
    1713         667 :                 ieee802_1x_update_sta_identity(hapd, sta, msg);
    1714         667 :                 ieee802_1x_update_sta_cui(hapd, sta, msg);
    1715         676 :                 ieee802_1x_check_hs20(hapd, sta, msg,
    1716             :                                       session_timeout_set ?
    1717           9 :                                       (int) session_timeout : -1);
    1718         667 :                 if (sm->eap_if->eapKeyAvailable && !sta->remediation &&
    1719           0 :                     !sta->hs20_deauth_requested &&
    1720           0 :                     wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
    1721             :                                        session_timeout_set ?
    1722           0 :                                        (int) session_timeout : -1, sm) == 0) {
    1723           0 :                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
    1724             :                                        HOSTAPD_LEVEL_DEBUG,
    1725             :                                        "Added PMKSA cache entry");
    1726             :                 }
    1727         667 :                 break;
    1728             :         case RADIUS_CODE_ACCESS_REJECT:
    1729         102 :                 sm->eap_if->aaaFail = TRUE;
    1730         102 :                 override_eapReq = 1;
    1731         102 :                 break;
    1732             :         case RADIUS_CODE_ACCESS_CHALLENGE:
    1733        2973 :                 sm->eap_if->aaaEapReq = TRUE;
    1734        2973 :                 if (session_timeout_set) {
    1735             :                         /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
    1736           0 :                         sm->eap_if->aaaMethodTimeout = session_timeout;
    1737           0 :                         hostapd_logger(hapd, sm->addr,
    1738             :                                        HOSTAPD_MODULE_IEEE8021X,
    1739             :                                        HOSTAPD_LEVEL_DEBUG,
    1740             :                                        "using EAP timeout of %d seconds (from "
    1741             :                                        "RADIUS)",
    1742           0 :                                        sm->eap_if->aaaMethodTimeout);
    1743             :                 } else {
    1744             :                         /*
    1745             :                          * Use dynamic retransmission behavior per EAP
    1746             :                          * specification.
    1747             :                          */
    1748        2973 :                         sm->eap_if->aaaMethodTimeout = 0;
    1749             :                 }
    1750        2973 :                 break;
    1751             :         }
    1752             : 
    1753        3743 :         ieee802_1x_decapsulate_radius(hapd, sta);
    1754        3743 :         if (override_eapReq)
    1755         769 :                 sm->eap_if->aaaEapReq = FALSE;
    1756             : 
    1757        3743 :         eapol_auth_step(sm);
    1758             : 
    1759        3743 :         return RADIUS_RX_QUEUED;
    1760             : }
    1761             : #endif /* CONFIG_NO_RADIUS */
    1762             : 
    1763             : 
    1764        1574 : void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
    1765             : {
    1766        1574 :         struct eapol_state_machine *sm = sta->eapol_sm;
    1767        1574 :         if (sm == NULL)
    1768        3115 :                 return;
    1769             : 
    1770          33 :         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    1771             :                        HOSTAPD_LEVEL_DEBUG, "aborting authentication");
    1772             : 
    1773             : #ifndef CONFIG_NO_RADIUS
    1774          33 :         radius_msg_free(sm->last_recv_radius);
    1775          33 :         sm->last_recv_radius = NULL;
    1776             : #endif /* CONFIG_NO_RADIUS */
    1777             : 
    1778          33 :         if (sm->eap_if->eapTimeout) {
    1779             :                 /*
    1780             :                  * Disconnect the STA since it did not reply to the last EAP
    1781             :                  * request and we cannot continue EAP processing (EAP-Failure
    1782             :                  * could only be sent if the EAP peer actually replied).
    1783             :                  */
    1784           0 :                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
    1785             :                         MAC2STR(sta->addr));
    1786             : 
    1787           0 :                 sm->eap_if->portEnabled = FALSE;
    1788           0 :                 ap_sta_disconnect(hapd, sta, sta->addr,
    1789             :                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
    1790             :         }
    1791             : }
    1792             : 
    1793             : 
    1794           3 : static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
    1795             : {
    1796           3 :         struct eapol_authenticator *eapol = hapd->eapol_auth;
    1797             : 
    1798           3 :         if (hapd->conf->default_wep_key_len < 1)
    1799           0 :                 return 0;
    1800             : 
    1801           3 :         os_free(eapol->default_wep_key);
    1802           3 :         eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
    1803           6 :         if (eapol->default_wep_key == NULL ||
    1804           3 :             random_get_bytes(eapol->default_wep_key,
    1805             :                              hapd->conf->default_wep_key_len)) {
    1806           0 :                 wpa_printf(MSG_INFO, "Could not generate random WEP key");
    1807           0 :                 os_free(eapol->default_wep_key);
    1808           0 :                 eapol->default_wep_key = NULL;
    1809           0 :                 return -1;
    1810             :         }
    1811             : 
    1812           6 :         wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
    1813           3 :                         eapol->default_wep_key,
    1814           3 :                         hapd->conf->default_wep_key_len);
    1815             : 
    1816           3 :         return 0;
    1817             : }
    1818             : 
    1819             : 
    1820           0 : static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
    1821             :                                         struct sta_info *sta, void *ctx)
    1822             : {
    1823           0 :         if (sta->eapol_sm) {
    1824           0 :                 sta->eapol_sm->eap_if->eapKeyAvailable = TRUE;
    1825           0 :                 eapol_auth_step(sta->eapol_sm);
    1826             :         }
    1827           0 :         return 0;
    1828             : }
    1829             : 
    1830             : 
    1831           3 : static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
    1832             : {
    1833           3 :         struct hostapd_data *hapd = eloop_ctx;
    1834           3 :         struct eapol_authenticator *eapol = hapd->eapol_auth;
    1835             : 
    1836           3 :         if (eapol->default_wep_key_idx >= 3)
    1837           0 :                 eapol->default_wep_key_idx =
    1838           0 :                         hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
    1839             :         else
    1840           3 :                 eapol->default_wep_key_idx++;
    1841             : 
    1842           3 :         wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
    1843           3 :                    eapol->default_wep_key_idx);
    1844             :                       
    1845           3 :         if (ieee802_1x_rekey_broadcast(hapd)) {
    1846           0 :                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
    1847             :                                HOSTAPD_LEVEL_WARNING, "failed to generate a "
    1848             :                                "new broadcast key");
    1849           0 :                 os_free(eapol->default_wep_key);
    1850           0 :                 eapol->default_wep_key = NULL;
    1851           0 :                 return;
    1852             :         }
    1853             : 
    1854             :         /* TODO: Could setup key for RX here, but change default TX keyid only
    1855             :          * after new broadcast key has been sent to all stations. */
    1856           6 :         if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
    1857             :                                 broadcast_ether_addr,
    1858           3 :                                 eapol->default_wep_key_idx, 1, NULL, 0,
    1859           3 :                                 eapol->default_wep_key,
    1860           3 :                                 hapd->conf->default_wep_key_len)) {
    1861           0 :                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
    1862             :                                HOSTAPD_LEVEL_WARNING, "failed to configure a "
    1863             :                                "new broadcast key");
    1864           0 :                 os_free(eapol->default_wep_key);
    1865           0 :                 eapol->default_wep_key = NULL;
    1866           0 :                 return;
    1867             :         }
    1868             : 
    1869           3 :         ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
    1870             : 
    1871           3 :         if (hapd->conf->wep_rekeying_period > 0) {
    1872           3 :                 eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
    1873             :                                        ieee802_1x_rekey, hapd, NULL);
    1874             :         }
    1875             : }
    1876             : 
    1877             : 
    1878        8377 : static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
    1879             :                                   const u8 *data, size_t datalen)
    1880             : {
    1881             : #ifdef CONFIG_WPS
    1882        8377 :         struct sta_info *sta = sta_ctx;
    1883             : 
    1884        8377 :         if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
    1885             :             WLAN_STA_MAYBE_WPS) {
    1886             :                 const u8 *identity;
    1887             :                 size_t identity_len;
    1888           0 :                 struct eapol_state_machine *sm = sta->eapol_sm;
    1889             : 
    1890           0 :                 identity = eap_get_identity(sm->eap, &identity_len);
    1891           0 :                 if (identity &&
    1892           0 :                     ((identity_len == WSC_ID_ENROLLEE_LEN &&
    1893           0 :                       os_memcmp(identity, WSC_ID_ENROLLEE,
    1894           0 :                                 WSC_ID_ENROLLEE_LEN) == 0) ||
    1895           0 :                      (identity_len == WSC_ID_REGISTRAR_LEN &&
    1896           0 :                       os_memcmp(identity, WSC_ID_REGISTRAR,
    1897             :                                 WSC_ID_REGISTRAR_LEN) == 0))) {
    1898           0 :                         wpa_printf(MSG_DEBUG, "WPS: WLAN_STA_MAYBE_WPS -> "
    1899             :                                    "WLAN_STA_WPS");
    1900           0 :                         sta->flags |= WLAN_STA_WPS;
    1901             :                 }
    1902             :         }
    1903             : #endif /* CONFIG_WPS */
    1904             : 
    1905        8377 :         ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
    1906        8377 : }
    1907             : 
    1908             : 
    1909        3768 : static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
    1910             :                                 const u8 *data, size_t datalen)
    1911             : {
    1912             : #ifndef CONFIG_NO_RADIUS
    1913        3768 :         struct hostapd_data *hapd = ctx;
    1914        3768 :         struct sta_info *sta = sta_ctx;
    1915             : 
    1916        3768 :         ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
    1917             : #endif /* CONFIG_NO_RADIUS */
    1918        3768 : }
    1919             : 
    1920             : 
    1921        1318 : static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
    1922             :                                  int preauth, int remediation)
    1923             : {
    1924        1318 :         struct hostapd_data *hapd = ctx;
    1925        1318 :         struct sta_info *sta = sta_ctx;
    1926        1318 :         if (preauth)
    1927           4 :                 rsn_preauth_finished(hapd, sta, success);
    1928             :         else
    1929        1314 :                 ieee802_1x_finished(hapd, sta, success, remediation);
    1930        1318 : }
    1931             : 
    1932             : 
    1933         733 : static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
    1934             :                                    size_t identity_len, int phase2,
    1935             :                                    struct eap_user *user)
    1936             : {
    1937         733 :         struct hostapd_data *hapd = ctx;
    1938             :         const struct hostapd_eap_user *eap_user;
    1939             :         int i;
    1940         733 :         int rv = -1;
    1941             : 
    1942         733 :         eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
    1943         733 :         if (eap_user == NULL)
    1944           1 :                 goto out;
    1945             : 
    1946         732 :         os_memset(user, 0, sizeof(*user));
    1947         732 :         user->phase2 = phase2;
    1948        6588 :         for (i = 0; i < EAP_MAX_METHODS; i++) {
    1949        5856 :                 user->methods[i].vendor = eap_user->methods[i].vendor;
    1950        5856 :                 user->methods[i].method = eap_user->methods[i].method;
    1951             :         }
    1952             : 
    1953         732 :         if (eap_user->password) {
    1954         236 :                 user->password = os_malloc(eap_user->password_len);
    1955         236 :                 if (user->password == NULL)
    1956           3 :                         goto out;
    1957         233 :                 os_memcpy(user->password, eap_user->password,
    1958             :                           eap_user->password_len);
    1959         233 :                 user->password_len = eap_user->password_len;
    1960         233 :                 user->password_hash = eap_user->password_hash;
    1961             :         }
    1962         729 :         user->force_version = eap_user->force_version;
    1963         729 :         user->macacl = eap_user->macacl;
    1964         729 :         user->ttls_auth = eap_user->ttls_auth;
    1965         729 :         user->remediation = eap_user->remediation;
    1966         729 :         rv = 0;
    1967             : 
    1968             : out:
    1969         733 :         if (rv)
    1970           4 :                 wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
    1971             : 
    1972         733 :         return rv;
    1973             : }
    1974             : 
    1975             : 
    1976      330641 : static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
    1977             : {
    1978      330641 :         struct hostapd_data *hapd = ctx;
    1979             :         struct sta_info *sta;
    1980      330641 :         sta = ap_get_sta(hapd, addr);
    1981      330641 :         if (sta == NULL || sta->eapol_sm == NULL)
    1982        6164 :                 return 0;
    1983      324477 :         return 1;
    1984             : }
    1985             : 
    1986             : 
    1987       10142 : static void ieee802_1x_logger(void *ctx, const u8 *addr,
    1988             :                               eapol_logger_level level, const char *txt)
    1989             : {
    1990             : #ifndef CONFIG_NO_HOSTAPD_LOGGER
    1991       10142 :         struct hostapd_data *hapd = ctx;
    1992             :         int hlevel;
    1993             : 
    1994       10142 :         switch (level) {
    1995             :         case EAPOL_LOGGER_WARNING:
    1996         551 :                 hlevel = HOSTAPD_LEVEL_WARNING;
    1997         551 :                 break;
    1998             :         case EAPOL_LOGGER_INFO:
    1999        1215 :                 hlevel = HOSTAPD_LEVEL_INFO;
    2000        1215 :                 break;
    2001             :         case EAPOL_LOGGER_DEBUG:
    2002             :         default:
    2003        8376 :                 hlevel = HOSTAPD_LEVEL_DEBUG;
    2004        8376 :                 break;
    2005             :         }
    2006             : 
    2007       10142 :         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
    2008             :                        txt);
    2009             : #endif /* CONFIG_NO_HOSTAPD_LOGGER */
    2010       10142 : }
    2011             : 
    2012             : 
    2013        2928 : static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
    2014             :                                            int authorized)
    2015             : {
    2016        2928 :         struct hostapd_data *hapd = ctx;
    2017        2928 :         struct sta_info *sta = sta_ctx;
    2018        2928 :         ieee802_1x_set_sta_authorized(hapd, sta, authorized);
    2019        2928 : }
    2020             : 
    2021             : 
    2022        1574 : static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
    2023             : {
    2024        1574 :         struct hostapd_data *hapd = ctx;
    2025        1574 :         struct sta_info *sta = sta_ctx;
    2026        1574 :         ieee802_1x_abort_auth(hapd, sta);
    2027        1574 : }
    2028             : 
    2029             : 
    2030           4 : static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
    2031             : {
    2032             : #ifndef CONFIG_FIPS
    2033             : #ifndef CONFIG_NO_RC4
    2034           4 :         struct hostapd_data *hapd = ctx;
    2035           4 :         struct sta_info *sta = sta_ctx;
    2036           4 :         ieee802_1x_tx_key(hapd, sta);
    2037             : #endif /* CONFIG_NO_RC4 */
    2038             : #endif /* CONFIG_FIPS */
    2039           4 : }
    2040             : 
    2041             : 
    2042       19157 : static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
    2043             :                                    enum eapol_event type)
    2044             : {
    2045             :         /* struct hostapd_data *hapd = ctx; */
    2046       19157 :         struct sta_info *sta = sta_ctx;
    2047       19157 :         switch (type) {
    2048             :         case EAPOL_AUTH_SM_CHANGE:
    2049       19156 :                 wpa_auth_sm_notify(sta->wpa_sm);
    2050       19156 :                 break;
    2051             :         case EAPOL_AUTH_REAUTHENTICATE:
    2052           1 :                 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
    2053           1 :                 break;
    2054             :         }
    2055       19157 : }
    2056             : 
    2057             : 
    2058             : #ifdef CONFIG_ERP
    2059             : 
    2060             : static struct eap_server_erp_key *
    2061           5 : ieee802_1x_erp_get_key(void *ctx, const char *keyname)
    2062             : {
    2063           5 :         struct hostapd_data *hapd = ctx;
    2064             :         struct eap_server_erp_key *erp;
    2065             : 
    2066           5 :         dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
    2067             :                          list) {
    2068           4 :                 if (os_strcmp(erp->keyname_nai, keyname) == 0)
    2069           4 :                         return erp;
    2070             :         }
    2071             : 
    2072           1 :         return NULL;
    2073             : }
    2074             : 
    2075             : 
    2076           5 : static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
    2077             : {
    2078           5 :         struct hostapd_data *hapd = ctx;
    2079             : 
    2080           5 :         dl_list_add(&hapd->erp_keys, &erp->list);
    2081           5 :         return 0;
    2082             : }
    2083             : 
    2084             : #endif /* CONFIG_ERP */
    2085             : 
    2086             : 
    2087        2048 : int ieee802_1x_init(struct hostapd_data *hapd)
    2088             : {
    2089             :         int i;
    2090             :         struct eapol_auth_config conf;
    2091             :         struct eapol_auth_cb cb;
    2092             : 
    2093        2048 :         dl_list_init(&hapd->erp_keys);
    2094             : 
    2095        2048 :         os_memset(&conf, 0, sizeof(conf));
    2096        2048 :         conf.ctx = hapd;
    2097        2048 :         conf.eap_reauth_period = hapd->conf->eap_reauth_period;
    2098        2048 :         conf.wpa = hapd->conf->wpa;
    2099        2048 :         conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
    2100        2048 :         conf.eap_server = hapd->conf->eap_server;
    2101        2048 :         conf.ssl_ctx = hapd->ssl_ctx;
    2102        2048 :         conf.msg_ctx = hapd->msg_ctx;
    2103        2048 :         conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
    2104        2048 :         conf.eap_req_id_text = hapd->conf->eap_req_id_text;
    2105        2048 :         conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
    2106        2048 :         conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
    2107        2048 :         conf.erp_domain = hapd->conf->erp_domain;
    2108        2048 :         conf.erp = hapd->conf->eap_server_erp;
    2109        2048 :         conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
    2110        2048 :         conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
    2111        2048 :         conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
    2112        2048 :         conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
    2113        2048 :         conf.eap_fast_a_id_info = hapd->conf->eap_fast_a_id_info;
    2114        2048 :         conf.eap_fast_prov = hapd->conf->eap_fast_prov;
    2115        2048 :         conf.pac_key_lifetime = hapd->conf->pac_key_lifetime;
    2116        2048 :         conf.pac_key_refresh_time = hapd->conf->pac_key_refresh_time;
    2117        2048 :         conf.eap_sim_aka_result_ind = hapd->conf->eap_sim_aka_result_ind;
    2118        2048 :         conf.tnc = hapd->conf->tnc;
    2119        2048 :         conf.wps = hapd->wps;
    2120        2048 :         conf.fragment_size = hapd->conf->fragment_size;
    2121        2048 :         conf.pwd_group = hapd->conf->pwd_group;
    2122        2048 :         conf.pbc_in_m1 = hapd->conf->pbc_in_m1;
    2123        2048 :         if (hapd->conf->server_id) {
    2124          13 :                 conf.server_id = (const u8 *) hapd->conf->server_id;
    2125          13 :                 conf.server_id_len = os_strlen(hapd->conf->server_id);
    2126             :         } else {
    2127        2035 :                 conf.server_id = (const u8 *) "hostapd";
    2128        2035 :                 conf.server_id_len = 7;
    2129             :         }
    2130             : 
    2131        2048 :         os_memset(&cb, 0, sizeof(cb));
    2132        2048 :         cb.eapol_send = ieee802_1x_eapol_send;
    2133        2048 :         cb.aaa_send = ieee802_1x_aaa_send;
    2134        2048 :         cb.finished = _ieee802_1x_finished;
    2135        2048 :         cb.get_eap_user = ieee802_1x_get_eap_user;
    2136        2048 :         cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
    2137        2048 :         cb.logger = ieee802_1x_logger;
    2138        2048 :         cb.set_port_authorized = ieee802_1x_set_port_authorized;
    2139        2048 :         cb.abort_auth = _ieee802_1x_abort_auth;
    2140        2048 :         cb.tx_key = _ieee802_1x_tx_key;
    2141        2048 :         cb.eapol_event = ieee802_1x_eapol_event;
    2142             : #ifdef CONFIG_ERP
    2143        2048 :         cb.erp_get_key = ieee802_1x_erp_get_key;
    2144        2048 :         cb.erp_add_key = ieee802_1x_erp_add_key;
    2145             : #endif /* CONFIG_ERP */
    2146             : 
    2147        2048 :         hapd->eapol_auth = eapol_auth_init(&conf, &cb);
    2148        2048 :         if (hapd->eapol_auth == NULL)
    2149           0 :                 return -1;
    2150             : 
    2151        3318 :         if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
    2152        1270 :             hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
    2153           0 :                 return -1;
    2154             : 
    2155             : #ifndef CONFIG_NO_RADIUS
    2156        1675 :         if (radius_client_register(hapd->radius, RADIUS_AUTH,
    2157             :                                    ieee802_1x_receive_auth, hapd))
    2158           0 :                 return -1;
    2159             : #endif /* CONFIG_NO_RADIUS */
    2160             : 
    2161        2048 :         if (hapd->conf->default_wep_key_len) {
    2162          15 :                 for (i = 0; i < 4; i++)
    2163          12 :                         hostapd_drv_set_key(hapd->conf->iface, hapd,
    2164             :                                             WPA_ALG_NONE, NULL, i, 0, NULL, 0,
    2165             :                                             NULL, 0);
    2166             : 
    2167           3 :                 ieee802_1x_rekey(hapd, NULL);
    2168             : 
    2169           3 :                 if (hapd->eapol_auth->default_wep_key == NULL)
    2170           0 :                         return -1;
    2171             :         }
    2172             : 
    2173        2048 :         return 0;
    2174             : }
    2175             : 
    2176             : 
    2177        2056 : void ieee802_1x_erp_flush(struct hostapd_data *hapd)
    2178             : {
    2179             :         struct eap_server_erp_key *erp;
    2180             : 
    2181        4117 :         while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
    2182             :                                     list)) != NULL) {
    2183           5 :                 dl_list_del(&erp->list);
    2184           5 :                 bin_clear_free(erp, sizeof(*erp));
    2185             :         }
    2186        2056 : }
    2187             : 
    2188             : 
    2189        2055 : void ieee802_1x_deinit(struct hostapd_data *hapd)
    2190             : {
    2191        2055 :         eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
    2192             : 
    2193        4110 :         if (hapd->driver != NULL &&
    2194        3644 :             (hapd->conf->ieee802_1x || hapd->conf->wpa))
    2195        1303 :                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
    2196             : 
    2197        2055 :         eapol_auth_deinit(hapd->eapol_auth);
    2198        2055 :         hapd->eapol_auth = NULL;
    2199             : 
    2200        2055 :         ieee802_1x_erp_flush(hapd);
    2201        2055 : }
    2202             : 
    2203             : 
    2204        1580 : int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
    2205             :                          const u8 *buf, size_t len, int ack)
    2206             : {
    2207             :         struct ieee80211_hdr *hdr;
    2208             :         u8 *pos;
    2209        1580 :         const unsigned char rfc1042_hdr[ETH_ALEN] =
    2210             :                 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
    2211             : 
    2212        1580 :         if (sta == NULL)
    2213           0 :                 return -1;
    2214        1580 :         if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
    2215         389 :                 return 0;
    2216             : 
    2217        1191 :         hdr = (struct ieee80211_hdr *) buf;
    2218        1191 :         pos = (u8 *) (hdr + 1);
    2219        1191 :         if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
    2220        1191 :                 return 0;
    2221           0 :         pos += sizeof(rfc1042_hdr);
    2222           0 :         if (WPA_GET_BE16(pos) != ETH_P_PAE)
    2223           0 :                 return 0;
    2224           0 :         pos += 2;
    2225             : 
    2226           0 :         return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
    2227             :                                           ack);
    2228             : }
    2229             : 
    2230             : 
    2231        8980 : int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
    2232             :                                const u8 *buf, int len, int ack)
    2233             : {
    2234        8980 :         const struct ieee802_1x_hdr *xhdr =
    2235             :                 (const struct ieee802_1x_hdr *) buf;
    2236        8980 :         const u8 *pos = buf + sizeof(*xhdr);
    2237             :         struct ieee802_1x_eapol_key *key;
    2238             : 
    2239        8980 :         if (len < (int) sizeof(*xhdr))
    2240           0 :                 return 0;
    2241       80820 :         wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR " TX status - version=%d "
    2242             :                    "type=%d length=%d - ack=%d",
    2243       71840 :                    MAC2STR(sta->addr), xhdr->version, xhdr->type,
    2244        8980 :                    be_to_host16(xhdr->length), ack);
    2245             : 
    2246        8980 :         if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
    2247        6287 :                 return 0;
    2248             : 
    2249        2693 :         if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
    2250             :                 const struct wpa_eapol_key *wpa;
    2251        2687 :                 wpa = (const struct wpa_eapol_key *) pos;
    2252        2763 :                 if (wpa->type == EAPOL_KEY_TYPE_RSN ||
    2253          76 :                     wpa->type == EAPOL_KEY_TYPE_WPA)
    2254        2687 :                         wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
    2255             :                                                      sta->wpa_sm, ack);
    2256             :         }
    2257             : 
    2258             :         /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
    2259             :          * or Authenticator state machines, but EAPOL-Key packets are not
    2260             :          * retransmitted in case of failure. Try to re-send failed EAPOL-Key
    2261             :          * packets couple of times because otherwise STA keys become
    2262             :          * unsynchronized with AP. */
    2263        2693 :         if (!ack && pos + sizeof(*key) <= buf + len) {
    2264           0 :                 key = (struct ieee802_1x_eapol_key *) pos;
    2265           0 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
    2266             :                                HOSTAPD_LEVEL_DEBUG, "did not Ack EAPOL-Key "
    2267             :                                "frame (%scast index=%d)",
    2268           0 :                                key->key_index & BIT(7) ? "uni" : "broad",
    2269           0 :                                key->key_index & ~BIT(7));
    2270             :                 /* TODO: re-send EAPOL-Key couple of times (with short delay
    2271             :                  * between them?). If all attempt fail, report error and
    2272             :                  * deauthenticate STA so that it will get new keys when
    2273             :                  * authenticating again (e.g., after returning in range).
    2274             :                  * Separate limit/transmit state needed both for unicast and
    2275             :                  * broadcast keys(?) */
    2276             :         }
    2277             :         /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
    2278             :          * to here and change the key only if the EAPOL-Key packet was Acked.
    2279             :          */
    2280             : 
    2281        2693 :         return 1;
    2282             : }
    2283             : 
    2284             : 
    2285         448 : u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
    2286             : {
    2287         448 :         if (sm == NULL || sm->identity == NULL)
    2288           6 :                 return NULL;
    2289             : 
    2290         442 :         *len = sm->identity_len;
    2291         442 :         return sm->identity;
    2292             : }
    2293             : 
    2294             : 
    2295         453 : u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
    2296             :                                  int idx)
    2297             : {
    2298         477 :         if (sm == NULL || sm->radius_class.attr == NULL ||
    2299          24 :             idx >= (int) sm->radius_class.count)
    2300         441 :                 return NULL;
    2301             : 
    2302          12 :         *len = sm->radius_class.attr[idx].len;
    2303          12 :         return sm->radius_class.attr[idx].data;
    2304             : }
    2305             : 
    2306             : 
    2307         444 : struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
    2308             : {
    2309         444 :         if (sm == NULL)
    2310           6 :                 return NULL;
    2311         438 :         return sm->radius_cui;
    2312             : }
    2313             : 
    2314             : 
    2315        2052 : const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
    2316             : {
    2317        2052 :         *len = 0;
    2318        2052 :         if (sm == NULL)
    2319           0 :                 return NULL;
    2320             : 
    2321        2052 :         *len = sm->eap_if->eapKeyDataLen;
    2322        2052 :         return sm->eap_if->eapKeyData;
    2323             : }
    2324             : 
    2325             : 
    2326       12147 : void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
    2327             :                                     int enabled)
    2328             : {
    2329       12147 :         if (sm == NULL)
    2330       17299 :                 return;
    2331        6995 :         sm->eap_if->portEnabled = enabled ? TRUE : FALSE;
    2332        6995 :         eapol_auth_step(sm);
    2333             : }
    2334             : 
    2335             : 
    2336        5131 : void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm,
    2337             :                                   int valid)
    2338             : {
    2339        5131 :         if (sm == NULL)
    2340        7353 :                 return;
    2341        2909 :         sm->portValid = valid ? TRUE : FALSE;
    2342        2909 :         eapol_auth_step(sm);
    2343             : }
    2344             : 
    2345             : 
    2346        3599 : void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
    2347             : {
    2348        3599 :         if (sm == NULL)
    2349        7092 :                 return;
    2350         106 :         if (pre_auth)
    2351           0 :                 sm->flags |= EAPOL_SM_PREAUTH;
    2352             :         else
    2353         106 :                 sm->flags &= ~EAPOL_SM_PREAUTH;
    2354             : }
    2355             : 
    2356             : 
    2357          18 : static const char * bool_txt(Boolean val)
    2358             : {
    2359          18 :         return val ? "TRUE" : "FALSE";
    2360             : }
    2361             : 
    2362             : 
    2363           8 : int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
    2364             : {
    2365             :         /* TODO */
    2366           8 :         return 0;
    2367             : }
    2368             : 
    2369             : 
    2370         202 : int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
    2371             :                            char *buf, size_t buflen)
    2372             : {
    2373         202 :         int len = 0, ret;
    2374         202 :         struct eapol_state_machine *sm = sta->eapol_sm;
    2375             :         struct os_reltime diff;
    2376             :         const char *name1;
    2377             :         const char *name2;
    2378             : 
    2379         202 :         if (sm == NULL)
    2380         193 :                 return 0;
    2381             : 
    2382           9 :         ret = os_snprintf(buf + len, buflen - len,
    2383             :                           "dot1xPaePortNumber=%d\n"
    2384             :                           "dot1xPaePortProtocolVersion=%d\n"
    2385             :                           "dot1xPaePortCapabilities=1\n"
    2386             :                           "dot1xPaePortInitialize=%d\n"
    2387             :                           "dot1xPaePortReauthenticate=FALSE\n",
    2388           9 :                           sta->aid,
    2389             :                           EAPOL_VERSION,
    2390           9 :                           sm->initialize);
    2391           9 :         if (os_snprintf_error(buflen - len, ret))
    2392           0 :                 return len;
    2393           9 :         len += ret;
    2394             : 
    2395             :         /* dot1xAuthConfigTable */
    2396          27 :         ret = os_snprintf(buf + len, buflen - len,
    2397             :                           "dot1xAuthPaeState=%d\n"
    2398             :                           "dot1xAuthBackendAuthState=%d\n"
    2399             :                           "dot1xAuthAdminControlledDirections=%d\n"
    2400             :                           "dot1xAuthOperControlledDirections=%d\n"
    2401             :                           "dot1xAuthAuthControlledPortStatus=%d\n"
    2402             :                           "dot1xAuthAuthControlledPortControl=%d\n"
    2403             :                           "dot1xAuthQuietPeriod=%u\n"
    2404             :                           "dot1xAuthServerTimeout=%u\n"
    2405             :                           "dot1xAuthReAuthPeriod=%u\n"
    2406             :                           "dot1xAuthReAuthEnabled=%s\n"
    2407             :                           "dot1xAuthKeyTxEnabled=%s\n",
    2408           9 :                           sm->auth_pae_state + 1,
    2409           9 :                           sm->be_auth_state + 1,
    2410           9 :                           sm->adminControlledDirections,
    2411           9 :                           sm->operControlledDirections,
    2412           9 :                           sm->authPortStatus,
    2413           9 :                           sm->portControl,
    2414             :                           sm->quietPeriod,
    2415             :                           sm->serverTimeout,
    2416             :                           sm->reAuthPeriod,
    2417             :                           bool_txt(sm->reAuthEnabled),
    2418             :                           bool_txt(sm->keyTxEnabled));
    2419           9 :         if (os_snprintf_error(buflen - len, ret))
    2420           0 :                 return len;
    2421           9 :         len += ret;
    2422             : 
    2423             :         /* dot1xAuthStatsTable */
    2424          54 :         ret = os_snprintf(buf + len, buflen - len,
    2425             :                           "dot1xAuthEapolFramesRx=%u\n"
    2426             :                           "dot1xAuthEapolFramesTx=%u\n"
    2427             :                           "dot1xAuthEapolStartFramesRx=%u\n"
    2428             :                           "dot1xAuthEapolLogoffFramesRx=%u\n"
    2429             :                           "dot1xAuthEapolRespIdFramesRx=%u\n"
    2430             :                           "dot1xAuthEapolRespFramesRx=%u\n"
    2431             :                           "dot1xAuthEapolReqIdFramesTx=%u\n"
    2432             :                           "dot1xAuthEapolReqFramesTx=%u\n"
    2433             :                           "dot1xAuthInvalidEapolFramesRx=%u\n"
    2434             :                           "dot1xAuthEapLengthErrorFramesRx=%u\n"
    2435             :                           "dot1xAuthLastEapolFrameVersion=%u\n"
    2436             :                           "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
    2437             :                           sm->dot1xAuthEapolFramesRx,
    2438             :                           sm->dot1xAuthEapolFramesTx,
    2439             :                           sm->dot1xAuthEapolStartFramesRx,
    2440             :                           sm->dot1xAuthEapolLogoffFramesRx,
    2441             :                           sm->dot1xAuthEapolRespIdFramesRx,
    2442             :                           sm->dot1xAuthEapolRespFramesRx,
    2443             :                           sm->dot1xAuthEapolReqIdFramesTx,
    2444             :                           sm->dot1xAuthEapolReqFramesTx,
    2445             :                           sm->dot1xAuthInvalidEapolFramesRx,
    2446             :                           sm->dot1xAuthEapLengthErrorFramesRx,
    2447             :                           sm->dot1xAuthLastEapolFrameVersion,
    2448          54 :                           MAC2STR(sm->addr));
    2449           9 :         if (os_snprintf_error(buflen - len, ret))
    2450           0 :                 return len;
    2451           9 :         len += ret;
    2452             : 
    2453             :         /* dot1xAuthDiagTable */
    2454           9 :         ret = os_snprintf(buf + len, buflen - len,
    2455             :                           "dot1xAuthEntersConnecting=%u\n"
    2456             :                           "dot1xAuthEapLogoffsWhileConnecting=%u\n"
    2457             :                           "dot1xAuthEntersAuthenticating=%u\n"
    2458             :                           "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
    2459             :                           "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
    2460             :                           "dot1xAuthAuthFailWhileAuthenticating=%u\n"
    2461             :                           "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
    2462             :                           "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
    2463             :                           "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
    2464             :                           "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
    2465             :                           "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
    2466             :                           "dot1xAuthBackendResponses=%u\n"
    2467             :                           "dot1xAuthBackendAccessChallenges=%u\n"
    2468             :                           "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
    2469             :                           "dot1xAuthBackendAuthSuccesses=%u\n"
    2470             :                           "dot1xAuthBackendAuthFails=%u\n",
    2471             :                           sm->authEntersConnecting,
    2472             :                           sm->authEapLogoffsWhileConnecting,
    2473             :                           sm->authEntersAuthenticating,
    2474             :                           sm->authAuthSuccessesWhileAuthenticating,
    2475             :                           sm->authAuthTimeoutsWhileAuthenticating,
    2476             :                           sm->authAuthFailWhileAuthenticating,
    2477             :                           sm->authAuthEapStartsWhileAuthenticating,
    2478             :                           sm->authAuthEapLogoffWhileAuthenticating,
    2479             :                           sm->authAuthReauthsWhileAuthenticated,
    2480             :                           sm->authAuthEapStartsWhileAuthenticated,
    2481             :                           sm->authAuthEapLogoffWhileAuthenticated,
    2482             :                           sm->backendResponses,
    2483             :                           sm->backendAccessChallenges,
    2484             :                           sm->backendOtherRequestsToSupplicant,
    2485             :                           sm->backendAuthSuccesses,
    2486             :                           sm->backendAuthFails);
    2487           9 :         if (os_snprintf_error(buflen - len, ret))
    2488           0 :                 return len;
    2489           9 :         len += ret;
    2490             : 
    2491             :         /* dot1xAuthSessionStatsTable */
    2492           9 :         os_reltime_age(&sta->acct_session_start, &diff);
    2493          27 :         ret = os_snprintf(buf + len, buflen - len,
    2494             :                           /* TODO: dot1xAuthSessionOctetsRx */
    2495             :                           /* TODO: dot1xAuthSessionOctetsTx */
    2496             :                           /* TODO: dot1xAuthSessionFramesRx */
    2497             :                           /* TODO: dot1xAuthSessionFramesTx */
    2498             :                           "dot1xAuthSessionId=%08X-%08X\n"
    2499             :                           "dot1xAuthSessionAuthenticMethod=%d\n"
    2500             :                           "dot1xAuthSessionTime=%u\n"
    2501             :                           "dot1xAuthSessionTerminateCause=999\n"
    2502             :                           "dot1xAuthSessionUserName=%s\n",
    2503             :                           sta->acct_session_id_hi, sta->acct_session_id_lo,
    2504           9 :                           (wpa_key_mgmt_wpa_ieee8021x(
    2505             :                                    wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
    2506             :                           1 : 2,
    2507           9 :                           (unsigned int) diff.sec,
    2508             :                           sm->identity);
    2509           9 :         if (os_snprintf_error(buflen - len, ret))
    2510           0 :                 return len;
    2511           9 :         len += ret;
    2512             : 
    2513           9 :         if (sm->acct_multi_session_id_hi) {
    2514           9 :                 ret = os_snprintf(buf + len, buflen - len,
    2515             :                                   "authMultiSessionId=%08X+%08X\n",
    2516             :                                   sm->acct_multi_session_id_hi,
    2517             :                                   sm->acct_multi_session_id_lo);
    2518           9 :                 if (os_snprintf_error(buflen - len, ret))
    2519           0 :                         return len;
    2520           9 :                 len += ret;
    2521             :         }
    2522             : 
    2523           9 :         name1 = eap_server_get_name(0, sm->eap_type_authsrv);
    2524           9 :         name2 = eap_server_get_name(0, sm->eap_type_supp);
    2525          18 :         ret = os_snprintf(buf + len, buflen - len,
    2526             :                           "last_eap_type_as=%d (%s)\n"
    2527             :                           "last_eap_type_sta=%d (%s)\n",
    2528           9 :                           sm->eap_type_authsrv, name1,
    2529           9 :                           sm->eap_type_supp, name2);
    2530           9 :         if (os_snprintf_error(buflen - len, ret))
    2531           0 :                 return len;
    2532           9 :         len += ret;
    2533             : 
    2534           9 :         return len;
    2535             : }
    2536             : 
    2537             : 
    2538        1314 : static void ieee802_1x_finished(struct hostapd_data *hapd,
    2539             :                                 struct sta_info *sta, int success,
    2540             :                                 int remediation)
    2541             : {
    2542             :         const u8 *key;
    2543             :         size_t len;
    2544             :         /* TODO: get PMKLifetime from WPA parameters */
    2545             :         static const int dot11RSNAConfigPMKLifetime = 43200;
    2546             :         unsigned int session_timeout;
    2547             : 
    2548             : #ifdef CONFIG_HS20
    2549        1314 :         if (remediation && !sta->remediation) {
    2550           0 :                 sta->remediation = 1;
    2551           0 :                 os_free(sta->remediation_url);
    2552           0 :                 sta->remediation_url =
    2553           0 :                         os_strdup(hapd->conf->subscr_remediation_url);
    2554           0 :                 sta->remediation_method = 1; /* SOAP-XML SPP */
    2555             :         }
    2556             : 
    2557        1314 :         if (success) {
    2558         761 :                 if (sta->remediation) {
    2559          12 :                         wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
    2560             :                                    "to " MACSTR " to indicate Subscription "
    2561             :                                    "Remediation",
    2562          12 :                                    MAC2STR(sta->addr));
    2563           2 :                         hs20_send_wnm_notification(hapd, sta->addr,
    2564           2 :                                                    sta->remediation_method,
    2565           2 :                                                    sta->remediation_url);
    2566           2 :                         os_free(sta->remediation_url);
    2567           2 :                         sta->remediation_url = NULL;
    2568             :                 }
    2569             : 
    2570         761 :                 if (sta->hs20_deauth_req) {
    2571           6 :                         wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
    2572             :                                    "to " MACSTR " to indicate imminent "
    2573           6 :                                    "deauthentication", MAC2STR(sta->addr));
    2574           1 :                         hs20_send_wnm_notification_deauth_req(
    2575           1 :                                 hapd, sta->addr, sta->hs20_deauth_req);
    2576             :                 }
    2577             :         }
    2578             : #endif /* CONFIG_HS20 */
    2579             : 
    2580        1314 :         key = ieee802_1x_get_key(sta->eapol_sm, &len);
    2581        1314 :         if (sta->session_timeout_set)
    2582          11 :                 session_timeout = sta->session_timeout;
    2583             :         else
    2584        1303 :                 session_timeout = dot11RSNAConfigPMKLifetime;
    2585        2040 :         if (success && key && len >= PMK_LEN && !sta->remediation &&
    2586        1451 :             !sta->hs20_deauth_requested &&
    2587         725 :             wpa_auth_pmksa_add(sta->wpa_sm, key, session_timeout,
    2588             :                                sta->eapol_sm) == 0) {
    2589         663 :                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
    2590             :                                HOSTAPD_LEVEL_DEBUG,
    2591             :                                "Added PMKSA cache entry (IEEE 802.1X)");
    2592             :         }
    2593             : 
    2594        1314 :         if (!success) {
    2595             :                 /*
    2596             :                  * Many devices require deauthentication after WPS provisioning
    2597             :                  * and some may not be be able to do that themselves, so
    2598             :                  * disconnect the client here. In addition, this may also
    2599             :                  * benefit IEEE 802.1X/EAPOL authentication cases, too since
    2600             :                  * the EAPOL PAE state machine would remain in HELD state for
    2601             :                  * considerable amount of time and some EAP methods, like
    2602             :                  * EAP-FAST with anonymous provisioning, may require another
    2603             :                  * EAPOL authentication to be started to complete connection.
    2604             :                  */
    2605         553 :                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "IEEE 802.1X: Force "
    2606             :                         "disconnection after EAP-Failure");
    2607             :                 /* Add a small sleep to increase likelihood of previously
    2608             :                  * requested EAP-Failure TX getting out before this should the
    2609             :                  * driver reorder operations.
    2610             :                  */
    2611         553 :                 os_sleep(0, 10000);
    2612         553 :                 ap_sta_disconnect(hapd, sta, sta->addr,
    2613             :                                   WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
    2614         553 :                 hostapd_wps_eap_completed(hapd);
    2615             :         }
    2616        1314 : }

Generated by: LCOV version 1.10