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 1401264779 Lines: 904 1151 78.5 %
Date: 2014-05-28 Functions: 56 57 98.2 %

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

Generated by: LCOV version 1.10