RADIUS: Use more likely unique accounting Acct-{,Multi-}Session-Id
[mech_eap.git] / src / ap / ieee802_1x.c
index 3c7b1ed..0c25f49 100644 (file)
 #include "ieee802_1x.h"
 
 
+#ifdef CONFIG_HS20
+static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
+#endif /* CONFIG_HS20 */
 static void ieee802_1x_finished(struct hostapd_data *hapd,
-                               struct sta_info *sta, int success);
+                               struct sta_info *sta, int success,
+                               int remediation);
 
 
 static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
@@ -65,6 +69,20 @@ static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
 
        if (wpa_auth_pairwise_set(sta->wpa_sm))
                encrypt = 1;
+#ifdef CONFIG_TESTING_OPTIONS
+       if (hapd->ext_eapol_frame_io) {
+               size_t hex_len = 2 * len + 1;
+               char *hex = os_malloc(hex_len);
+
+               if (hex) {
+                       wpa_snprintf_hex(hex, hex_len, buf, len);
+                       wpa_msg(hapd->msg_ctx, MSG_INFO,
+                               "EAPOL-TX " MACSTR " %s",
+                               MAC2STR(sta->addr), hex);
+                       os_free(hex);
+               }
+       } else
+#endif /* CONFIG_TESTING_OPTIONS */
        if (sta->flags & WLAN_STA_PREAUTH) {
                rsn_preauth_send(hapd, sta, buf, len);
        } else {
@@ -110,6 +128,9 @@ void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
 }
 
 
+#ifndef CONFIG_FIPS
+#ifndef CONFIG_NO_RC4
+
 static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
                                  struct sta_info *sta,
                                  int idx, int broadcast,
@@ -189,7 +210,7 @@ static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
 }
 
 
-void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
+static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
 {
        struct eapol_authenticator *eapol = hapd->eapol_auth;
        struct eapol_state_machine *sm = sta->eapol_sm;
@@ -244,6 +265,9 @@ void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
        }
 }
 
+#endif /* CONFIG_NO_RC4 */
+#endif /* CONFIG_FIPS */
+
 
 const char *radius_mode_txt(struct hostapd_data *hapd)
 {
@@ -281,9 +305,15 @@ static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
 {
        const u8 *identity;
        size_t identity_len;
+       const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
 
        if (len <= sizeof(struct eap_hdr) ||
-           eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY)
+           (hdr->code == EAP_CODE_RESPONSE &&
+            eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
+           (hdr->code == EAP_CODE_INITIATE &&
+            eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
+           (hdr->code != EAP_CODE_RESPONSE &&
+            hdr->code != EAP_CODE_INITIATE))
                return;
 
        identity = eap_get_identity(sm->eap, &identity_len);
@@ -305,6 +335,68 @@ static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
 }
 
 
+static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
+                                         struct hostapd_radius_attr *req_attr,
+                                         struct sta_info *sta,
+                                         struct radius_msg *msg)
+{
+       u32 suite;
+       int ver, val;
+
+       ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
+       val = wpa_auth_get_pairwise(sta->wpa_sm);
+       suite = wpa_cipher_to_suite(ver, val);
+       if (val != -1 &&
+           !hostapd_config_get_radius_attr(req_attr,
+                                           RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
+                                      suite)) {
+               wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
+               return -1;
+       }
+
+       suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2) ||
+                                    hapd->conf->osen) ?
+                                   WPA_PROTO_RSN : WPA_PROTO_WPA,
+                                   hapd->conf->wpa_group);
+       if (!hostapd_config_get_radius_attr(req_attr,
+                                           RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
+                                      suite)) {
+               wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
+               return -1;
+       }
+
+       val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
+       suite = wpa_akm_to_suite(val);
+       if (val != -1 &&
+           !hostapd_config_get_radius_attr(req_attr,
+                                           RADIUS_ATTR_WLAN_AKM_SUITE) &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
+                                      suite)) {
+               wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
+               return -1;
+       }
+
+#ifdef CONFIG_IEEE80211W
+       if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
+               suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
+                                           hapd->conf->group_mgmt_cipher);
+               if (!hostapd_config_get_radius_attr(
+                           req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
+                   !radius_msg_add_attr_int32(
+                           msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
+                       wpa_printf(MSG_ERROR,
+                                  "Could not add WLAN-Group-Mgmt-Cipher");
+                       return -1;
+               }
+       }
+#endif /* CONFIG_IEEE80211W */
+
+       return 0;
+}
+
+
 static int add_common_radius_sta_attr(struct hostapd_data *hapd,
                                      struct hostapd_radius_attr *req_attr,
                                      struct sta_info *sta,
@@ -346,9 +438,9 @@ static int add_common_radius_sta_attr(struct hostapd_data *hapd,
                return -1;
        }
 
-       if (sta->acct_session_id_hi || sta->acct_session_id_lo) {
-               os_snprintf(buf, sizeof(buf), "%08X-%08X",
-                           sta->acct_session_id_hi, sta->acct_session_id_lo);
+       if (sta->acct_session_id) {
+               os_snprintf(buf, sizeof(buf), "%016lX",
+                           (long unsigned int) sta->acct_session_id);
                if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
                                         (u8 *) buf, os_strlen(buf))) {
                        wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
@@ -356,6 +448,25 @@ static int add_common_radius_sta_attr(struct hostapd_data *hapd,
                }
        }
 
+#ifdef CONFIG_IEEE80211R
+       if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
+           sta->wpa_sm &&
+           (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
+            sta->auth_alg == WLAN_AUTH_FT) &&
+           !hostapd_config_get_radius_attr(req_attr,
+                                           RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
+                                      WPA_GET_BE16(
+                                              hapd->conf->mobility_domain))) {
+               wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
+               return -1;
+       }
+#endif /* CONFIG_IEEE80211R */
+
+       if ((hapd->conf->wpa || hapd->conf->osen) && sta->wpa_sm &&
+           add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
+               return -1;
+
        return 0;
 }
 
@@ -367,6 +478,7 @@ int add_common_radius_attr(struct hostapd_data *hapd,
 {
        char buf[128];
        struct hostapd_radius_attr *attr;
+       int len;
 
        if (!hostapd_config_get_radius_attr(req_attr,
                                            RADIUS_ATTR_NAS_IP_ADDRESS) &&
@@ -398,15 +510,15 @@ int add_common_radius_attr(struct hostapd_data *hapd,
                return -1;
        }
 
-       os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
-                   MAC2STR(hapd->own_addr),
-                   wpa_ssid_txt(hapd->conf->ssid.ssid,
-                                hapd->conf->ssid.ssid_len));
-       buf[sizeof(buf) - 1] = '\0';
+       len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
+                         MAC2STR(hapd->own_addr));
+       os_memcpy(&buf[len], hapd->conf->ssid.ssid,
+                 hapd->conf->ssid.ssid_len);
+       len += hapd->conf->ssid.ssid_len;
        if (!hostapd_config_get_radius_attr(req_attr,
                                            RADIUS_ATTR_CALLED_STATION_ID) &&
            !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
-                                (u8 *) buf, os_strlen(buf))) {
+                                (u8 *) buf, len)) {
                wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
                return -1;
        }
@@ -419,6 +531,22 @@ int add_common_radius_attr(struct hostapd_data *hapd,
                return -1;
        }
 
+#ifdef CONFIG_INTERWORKING
+       if (hapd->conf->interworking &&
+           !is_zero_ether_addr(hapd->conf->hessid)) {
+               os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
+                           MAC2STR(hapd->conf->hessid));
+               buf[sizeof(buf) - 1] = '\0';
+               if (!hostapd_config_get_radius_attr(req_attr,
+                                                   RADIUS_ATTR_WLAN_HESSID) &&
+                   !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
+                                        (u8 *) buf, os_strlen(buf))) {
+                       wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
+                       return -1;
+               }
+       }
+#endif /* CONFIG_INTERWORKING */
+
        if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
                return -1;
 
@@ -482,7 +610,7 @@ static void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
                goto fail;
        }
 
-       if (eap && !radius_msg_add_eap(msg, eap, len)) {
+       if (!radius_msg_add_eap(msg, eap, len)) {
                wpa_printf(MSG_INFO, "Could not add EAP-Message");
                goto fail;
        }
@@ -600,6 +728,39 @@ static void handle_eap_response(struct hostapd_data *hapd,
 }
 
 
+static void handle_eap_initiate(struct hostapd_data *hapd,
+                               struct sta_info *sta, struct eap_hdr *eap,
+                               size_t len)
+{
+#ifdef CONFIG_ERP
+       u8 type, *data;
+       struct eapol_state_machine *sm = sta->eapol_sm;
+
+       if (sm == NULL)
+               return;
+
+       if (len < sizeof(*eap) + 1) {
+               wpa_printf(MSG_INFO,
+                          "handle_eap_initiate: too short response data");
+               return;
+       }
+
+       data = (u8 *) (eap + 1);
+       type = data[0];
+
+       hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
+                      HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
+                      "id=%d len=%d) from STA: EAP Initiate type %u",
+                      eap->code, eap->identifier, be_to_host16(eap->length),
+                      type);
+
+       wpabuf_free(sm->eap_if->eapRespData);
+       sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
+       sm->eapolEap = TRUE;
+#endif /* CONFIG_ERP */
+}
+
+
 /* Process incoming EAP packet from Supplicant */
 static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
                       u8 *buf, size_t len)
@@ -643,6 +804,13 @@ static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
        case EAP_CODE_FAILURE:
                wpa_printf(MSG_DEBUG, " (failure)");
                return;
+       case EAP_CODE_INITIATE:
+               wpa_printf(MSG_DEBUG, " (initiate)");
+               handle_eap_initiate(hapd, sta, eap, eap_len);
+               break;
+       case EAP_CODE_FINISH:
+               wpa_printf(MSG_DEBUG, " (finish)");
+               break;
        default:
                wpa_printf(MSG_DEBUG, " (unknown code)");
                return;
@@ -864,8 +1032,9 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
        int key_mgmt;
 
 #ifdef CONFIG_WPS
-       if (hapd->conf->wps_state && hapd->conf->wpa &&
-           (sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
+       if (hapd->conf->wps_state &&
+           ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
+            (sta->flags & WLAN_STA_WPS))) {
                /*
                 * Need to enable IEEE 802.1X/EAPOL state machines for possible
                 * WPS handshake even if IEEE 802.1X/EAPOL is not used for
@@ -882,7 +1051,7 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
                 * Clear any possible EAPOL authenticator state to support
                 * reassociation change from WPS to PSK.
                 */
-               ieee802_1x_free_station(sta);
+               ieee802_1x_free_station(hapd, sta);
                return;
        }
 
@@ -893,7 +1062,7 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
                 * Clear any possible EAPOL authenticator state to support
                 * reassociation change from WPA-EAP to PSK.
                 */
-               ieee802_1x_free_station(sta);
+               ieee802_1x_free_station(hapd, sta);
                return;
        }
 
@@ -941,6 +1110,7 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
                sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
                sta->eapol_sm->authSuccess = TRUE;
                sta->eapol_sm->authFail = FALSE;
+               sta->eapol_sm->portValid = TRUE;
                if (sta->eapol_sm->eap)
                        eap_sm_notify_cached(sta->eapol_sm->eap);
                /* TODO: get vlan_id from R0KH using RRB message */
@@ -950,8 +1120,6 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
 
        pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
        if (pmksa) {
-               int old_vlanid;
-
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
                               HOSTAPD_LEVEL_DEBUG,
                               "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
@@ -965,11 +1133,8 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
                sta->eapol_sm->authFail = FALSE;
                if (sta->eapol_sm->eap)
                        eap_sm_notify_cached(sta->eapol_sm->eap);
-               old_vlanid = sta->vlan_id;
                pmksa_cache_to_eapol_data(pmksa, sta->eapol_sm);
-               if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
-                       sta->vlan_id = 0;
-               ap_sta_bind_vlan(hapd, sta, old_vlanid);
+               ap_sta_bind_vlan(hapd, sta);
        } else {
                if (reassoc) {
                        /*
@@ -984,10 +1149,14 @@ void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
 }
 
 
-void ieee802_1x_free_station(struct sta_info *sta)
+void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
 {
        struct eapol_state_machine *sm = sta->eapol_sm;
 
+#ifdef CONFIG_HS20
+       eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
+#endif /* CONFIG_HS20 */
+
        if (sm == NULL)
                return;
 
@@ -996,10 +1165,8 @@ void ieee802_1x_free_station(struct sta_info *sta)
 #ifndef CONFIG_NO_RADIUS
        radius_msg_free(sm->last_recv_radius);
        radius_free_class(&sm->radius_class);
-       wpabuf_free(sm->radius_cui);
 #endif /* CONFIG_NO_RADIUS */
 
-       os_free(sm->identity);
        eapol_auth_free(sm);
 }
 
@@ -1053,15 +1220,11 @@ static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
                if (eap_type >= 0)
                        sm->eap_type_authsrv = eap_type;
                os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
-                           eap_type >= 0 ? eap_server_get_name(0, eap_type) :
-                           "??",
-                           eap_type);
+                           eap_server_get_name(0, eap_type), eap_type);
                break;
        case EAP_CODE_RESPONSE:
                os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
-                           eap_type >= 0 ? eap_server_get_name(0, eap_type) :
-                           "??",
-                           eap_type);
+                           eap_server_get_name(0, eap_type), eap_type);
                break;
        case EAP_CODE_SUCCESS:
                os_strlcpy(buf, "EAP Success", sizeof(buf));
@@ -1117,6 +1280,11 @@ static void ieee802_1x_get_keys(struct hostapd_data *hapd,
                        sm->eap_if->aaaEapKeyDataLen = len;
                        sm->eap_if->aaaEapKeyAvailable = TRUE;
                }
+       } else {
+               wpa_printf(MSG_DEBUG,
+                          "MS-MPPE: 1x_get_keys, could not get keys: %p  send: %p  recv: %p",
+                          keys, keys ? keys->send : NULL,
+                          keys ? keys->recv : NULL);
        }
 
        if (keys) {
@@ -1131,7 +1299,7 @@ static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
                                          struct sta_info *sta,
                                          struct radius_msg *msg)
 {
-       u8 *class;
+       u8 *attr_class;
        size_t class_len;
        struct eapol_state_machine *sm = sta->eapol_sm;
        int count, i;
@@ -1153,12 +1321,12 @@ static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
 
        nclass_count = 0;
 
-       class = NULL;
+       attr_class = NULL;
        for (i = 0; i < count; i++) {
                do {
                        if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
-                                                   &class, &class_len,
-                                                   class) < 0) {
+                                                   &attr_class, &class_len,
+                                                   attr_class) < 0) {
                                i = count;
                                break;
                        }
@@ -1168,7 +1336,7 @@ static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
                if (nclass[nclass_count].data == NULL)
                        break;
 
-               os_memcpy(nclass[nclass_count].data, class, class_len);
+               os_memcpy(nclass[nclass_count].data, attr_class, class_len);
                nclass[nclass_count].len = class_len;
                nclass_count++;
        }
@@ -1286,12 +1454,55 @@ static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
        ap_sta_session_timeout(hapd, sta, hapd->conf->hs20_deauth_req_timeout);
 }
 
+
+static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
+                                        struct sta_info *sta, u8 *pos,
+                                        size_t len, int session_timeout)
+{
+       unsigned int swt;
+       int warning_time, beacon_int;
+
+       if (len < 1)
+               return; /* Malformed information */
+       os_free(sta->hs20_session_info_url);
+       sta->hs20_session_info_url = os_malloc(len);
+       if (sta->hs20_session_info_url == NULL)
+               return;
+       swt = pos[0];
+       os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
+       sta->hs20_session_info_url[len - 1] = '\0';
+       wpa_printf(MSG_DEBUG, "HS 2.0: Session Information URL='%s' SWT=%u "
+                  "(session_timeout=%d)",
+                  sta->hs20_session_info_url, swt, session_timeout);
+       if (session_timeout < 0) {
+               wpa_printf(MSG_DEBUG, "HS 2.0: No Session-Timeout set - ignore session info URL");
+               return;
+       }
+       if (swt == 255)
+               swt = 1; /* Use one minute as the AP selected value */
+
+       if ((unsigned int) session_timeout < swt * 60)
+               warning_time = 0;
+       else
+               warning_time = session_timeout - swt * 60;
+
+       beacon_int = hapd->iconf->beacon_int;
+       if (beacon_int < 1)
+               beacon_int = 100; /* best guess */
+       sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
+       if (sta->hs20_disassoc_timer > 65535)
+               sta->hs20_disassoc_timer = 65535;
+
+       ap_sta_session_warning_timeout(hapd, sta, warning_time);
+}
+
 #endif /* CONFIG_HS20 */
 
 
 static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
                                  struct sta_info *sta,
-                                 struct radius_msg *msg)
+                                 struct radius_msg *msg,
+                                 int session_timeout)
 {
 #ifdef CONFIG_HS20
        u8 *buf, *pos, *end, type, sublen;
@@ -1328,6 +1539,10 @@ static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
                case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
                        ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
                        break;
+               case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
+                       ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
+                                                    session_timeout);
+                       break;
                }
        }
 #endif /* CONFIG_HS20 */
@@ -1384,7 +1599,7 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
        struct hostapd_data *hapd = data;
        struct sta_info *sta;
        u32 session_timeout = 0, termination_action, acct_interim_interval;
-       int session_timeout_set, old_vlanid = 0;
+       int session_timeout_set, vlan_id = 0;
        struct eapol_state_machine *sm;
        int override_eapReq = 0;
        struct radius_hdr *hdr = radius_msg_get_hdr(msg);
@@ -1451,20 +1666,27 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
 
        switch (hdr->code) {
        case RADIUS_CODE_ACCESS_ACCEPT:
-               if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
-                       sta->vlan_id = 0;
+               if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
+                       vlan_id = 0;
 #ifndef CONFIG_NO_VLAN
-               else {
-                       old_vlanid = sta->vlan_id;
-                       sta->vlan_id = radius_msg_get_vlanid(msg);
-               }
-               if (sta->vlan_id > 0 &&
-                   hostapd_vlan_id_valid(hapd->conf->vlan, sta->vlan_id)) {
+               else
+                       vlan_id = radius_msg_get_vlanid(msg);
+               if (vlan_id > 0 &&
+                   hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
                        hostapd_logger(hapd, sta->addr,
                                       HOSTAPD_MODULE_RADIUS,
                                       HOSTAPD_LEVEL_INFO,
-                                      "VLAN ID %d", sta->vlan_id);
-               } else if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_REQUIRED) {
+                                      "VLAN ID %d", vlan_id);
+               } else if (vlan_id > 0) {
+                       sta->eapol_sm->authFail = TRUE;
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_RADIUS,
+                                      HOSTAPD_LEVEL_INFO,
+                                      "Invalid VLAN ID %d received from RADIUS server",
+                                      vlan_id);
+                       break;
+               } else if (hapd->conf->ssid.dynamic_vlan ==
+                          DYNAMIC_VLAN_REQUIRED) {
                        sta->eapol_sm->authFail = TRUE;
                        hostapd_logger(hapd, sta->addr,
                                       HOSTAPD_MODULE_IEEE8021X,
@@ -1475,9 +1697,14 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
                }
 #endif /* CONFIG_NO_VLAN */
 
-               if (ap_sta_bind_vlan(hapd, sta, old_vlanid) < 0)
+               sta->vlan_id = vlan_id;
+               if ((sta->flags & WLAN_STA_ASSOC) &&
+                   ap_sta_bind_vlan(hapd, sta) < 0)
                        break;
 
+               sta->session_timeout_set = !!session_timeout_set;
+               sta->session_timeout = session_timeout;
+
                /* RFC 3580, Ch. 3.17 */
                if (session_timeout_set && termination_action ==
                    RADIUS_TERMINATION_ACTION_RADIUS_REQUEST) {
@@ -1492,16 +1719,9 @@ ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
                ieee802_1x_store_radius_class(hapd, sta, msg);
                ieee802_1x_update_sta_identity(hapd, sta, msg);
                ieee802_1x_update_sta_cui(hapd, sta, msg);
-               ieee802_1x_check_hs20(hapd, sta, msg);
-               if (sm->eap_if->eapKeyAvailable && !sta->remediation &&
-                   !sta->hs20_deauth_requested &&
-                   wpa_auth_pmksa_add(sta->wpa_sm, sm->eapol_key_crypt,
-                                      session_timeout_set ?
-                                      (int) session_timeout : -1, sm) == 0) {
-                       hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
-                                      HOSTAPD_LEVEL_DEBUG,
-                                      "Added PMKSA cache entry");
-               }
+               ieee802_1x_check_hs20(hapd, sta, msg,
+                                     session_timeout_set ?
+                                     (int) session_timeout : -1);
                break;
        case RADIUS_CODE_ACCESS_REJECT:
                sm->eap_if->aaaFail = TRUE;
@@ -1697,14 +1917,14 @@ static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
 
 
 static void _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
-                                int preauth)
+                                int preauth, int remediation)
 {
        struct hostapd_data *hapd = ctx;
        struct sta_info *sta = sta_ctx;
        if (preauth)
                rsn_preauth_finished(hapd, sta, success);
        else
-               ieee802_1x_finished(hapd, sta, success);
+               ieee802_1x_finished(hapd, sta, success, remediation);
 }
 
 
@@ -1715,10 +1935,11 @@ static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
        struct hostapd_data *hapd = ctx;
        const struct hostapd_eap_user *eap_user;
        int i;
+       int rv = -1;
 
        eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
        if (eap_user == NULL)
-               return -1;
+               goto out;
 
        os_memset(user, 0, sizeof(*user));
        user->phase2 = phase2;
@@ -1730,16 +1951,23 @@ static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
        if (eap_user->password) {
                user->password = os_malloc(eap_user->password_len);
                if (user->password == NULL)
-                       return -1;
+                       goto out;
                os_memcpy(user->password, eap_user->password,
                          eap_user->password_len);
                user->password_len = eap_user->password_len;
                user->password_hash = eap_user->password_hash;
        }
        user->force_version = eap_user->force_version;
+       user->macacl = eap_user->macacl;
        user->ttls_auth = eap_user->ttls_auth;
+       user->remediation = eap_user->remediation;
+       rv = 0;
 
-       return 0;
+out:
+       if (rv)
+               wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
+
+       return rv;
 }
 
 
@@ -1799,9 +2027,13 @@ static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
 
 static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
 {
+#ifndef CONFIG_FIPS
+#ifndef CONFIG_NO_RC4
        struct hostapd_data *hapd = ctx;
        struct sta_info *sta = sta_ctx;
        ieee802_1x_tx_key(hapd, sta);
+#endif /* CONFIG_NO_RC4 */
+#endif /* CONFIG_FIPS */
 }
 
 
@@ -1821,12 +2053,43 @@ static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
 }
 
 
+#ifdef CONFIG_ERP
+
+static struct eap_server_erp_key *
+ieee802_1x_erp_get_key(void *ctx, const char *keyname)
+{
+       struct hostapd_data *hapd = ctx;
+       struct eap_server_erp_key *erp;
+
+       dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
+                        list) {
+               if (os_strcmp(erp->keyname_nai, keyname) == 0)
+                       return erp;
+       }
+
+       return NULL;
+}
+
+
+static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
+{
+       struct hostapd_data *hapd = ctx;
+
+       dl_list_add(&hapd->erp_keys, &erp->list);
+       return 0;
+}
+
+#endif /* CONFIG_ERP */
+
+
 int ieee802_1x_init(struct hostapd_data *hapd)
 {
        int i;
        struct eapol_auth_config conf;
        struct eapol_auth_cb cb;
 
+       dl_list_init(&hapd->erp_keys);
+
        os_memset(&conf, 0, sizeof(conf));
        conf.ctx = hapd;
        conf.eap_reauth_period = hapd->conf->eap_reauth_period;
@@ -1838,6 +2101,10 @@ int ieee802_1x_init(struct hostapd_data *hapd)
        conf.eap_sim_db_priv = hapd->eap_sim_db_priv;
        conf.eap_req_id_text = hapd->conf->eap_req_id_text;
        conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
+       conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
+       conf.erp_domain = hapd->conf->erp_domain;
+       conf.erp = hapd->conf->eap_server_erp;
+       conf.tls_session_lifetime = hapd->conf->tls_session_lifetime;
        conf.pac_opaque_encr_key = hapd->conf->pac_opaque_encr_key;
        conf.eap_fast_a_id = hapd->conf->eap_fast_a_id;
        conf.eap_fast_a_id_len = hapd->conf->eap_fast_a_id_len;
@@ -1870,6 +2137,10 @@ int ieee802_1x_init(struct hostapd_data *hapd)
        cb.abort_auth = _ieee802_1x_abort_auth;
        cb.tx_key = _ieee802_1x_tx_key;
        cb.eapol_event = ieee802_1x_eapol_event;
+#ifdef CONFIG_ERP
+       cb.erp_get_key = ieee802_1x_erp_get_key;
+       cb.erp_add_key = ieee802_1x_erp_add_key;
+#endif /* CONFIG_ERP */
 
        hapd->eapol_auth = eapol_auth_init(&conf, &cb);
        if (hapd->eapol_auth == NULL)
@@ -1901,16 +2172,30 @@ int ieee802_1x_init(struct hostapd_data *hapd)
 }
 
 
+void ieee802_1x_erp_flush(struct hostapd_data *hapd)
+{
+       struct eap_server_erp_key *erp;
+
+       while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
+                                   list)) != NULL) {
+               dl_list_del(&erp->list);
+               bin_clear_free(erp, sizeof(*erp));
+       }
+}
+
+
 void ieee802_1x_deinit(struct hostapd_data *hapd)
 {
        eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
 
-       if (hapd->driver != NULL &&
+       if (hapd->driver && hapd->drv_priv &&
            (hapd->conf->ieee802_1x || hapd->conf->wpa))
                hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
 
        eapol_auth_deinit(hapd->eapol_auth);
        hapd->eapol_auth = NULL;
+
+       ieee802_1x_erp_flush(hapd);
 }
 
 
@@ -2067,9 +2352,9 @@ void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, int pre_auth)
 }
 
 
-static const char * bool_txt(Boolean bool)
+static const char * bool_txt(Boolean val)
 {
-       return bool ? "TRUE" : "FALSE";
+       return val ? "TRUE" : "FALSE";
 }
 
 
@@ -2101,7 +2386,7 @@ int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
                          sta->aid,
                          EAPOL_VERSION,
                          sm->initialize);
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -2129,7 +2414,7 @@ int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
                          sm->reAuthPeriod,
                          bool_txt(sm->reAuthEnabled),
                          bool_txt(sm->keyTxEnabled));
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -2159,7 +2444,7 @@ int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
                          sm->dot1xAuthEapLengthErrorFramesRx,
                          sm->dot1xAuthLastEapolFrameVersion,
                          MAC2STR(sm->addr));
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -2197,7 +2482,7 @@ int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
                          sm->backendOtherRequestsToSupplicant,
                          sm->backendAuthSuccesses,
                          sm->backendAuthFails);
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -2208,31 +2493,39 @@ int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
                          /* TODO: dot1xAuthSessionOctetsTx */
                          /* TODO: dot1xAuthSessionFramesRx */
                          /* TODO: dot1xAuthSessionFramesTx */
-                         "dot1xAuthSessionId=%08X-%08X\n"
+                         "dot1xAuthSessionId=%016lX\n"
                          "dot1xAuthSessionAuthenticMethod=%d\n"
                          "dot1xAuthSessionTime=%u\n"
                          "dot1xAuthSessionTerminateCause=999\n"
                          "dot1xAuthSessionUserName=%s\n",
-                         sta->acct_session_id_hi, sta->acct_session_id_lo,
+                         (long unsigned int) sta->acct_session_id,
                          (wpa_key_mgmt_wpa_ieee8021x(
                                   wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
                          1 : 2,
                          (unsigned int) diff.sec,
                          sm->identity);
-       if (ret < 0 || (size_t) ret >= buflen - len)
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
+       if (sm->acct_multi_session_id) {
+               ret = os_snprintf(buf + len, buflen - len,
+                                 "authMultiSessionId=%016lX\n",
+                                 (long unsigned int)
+                                 sm->acct_multi_session_id);
+               if (os_snprintf_error(buflen - len, ret))
+                       return len;
+               len += ret;
+       }
+
        name1 = eap_server_get_name(0, sm->eap_type_authsrv);
        name2 = eap_server_get_name(0, sm->eap_type_supp);
        ret = os_snprintf(buf + len, buflen - len,
                          "last_eap_type_as=%d (%s)\n"
                          "last_eap_type_sta=%d (%s)\n",
-                         sm->eap_type_authsrv,
-                         name1 ? name1 : "",
-                         sm->eap_type_supp,
-                         name2 ? name2 : "");
-       if (ret < 0 || (size_t) ret >= buflen - len)
+                         sm->eap_type_authsrv, name1,
+                         sm->eap_type_supp, name2);
+       if (os_snprintf_error(buflen - len, ret))
                return len;
        len += ret;
 
@@ -2240,42 +2533,70 @@ int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
 }
 
 
+#ifdef CONFIG_HS20
+static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
+{
+       struct hostapd_data *hapd = eloop_ctx;
+       struct sta_info *sta = timeout_ctx;
+
+       if (sta->remediation) {
+               wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
+                          MACSTR " to indicate Subscription Remediation",
+                          MAC2STR(sta->addr));
+               hs20_send_wnm_notification(hapd, sta->addr,
+                                          sta->remediation_method,
+                                          sta->remediation_url);
+               os_free(sta->remediation_url);
+               sta->remediation_url = NULL;
+       }
+
+       if (sta->hs20_deauth_req) {
+               wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
+                          MACSTR " to indicate imminent deauthentication",
+                          MAC2STR(sta->addr));
+               hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
+                                                     sta->hs20_deauth_req);
+       }
+}
+#endif /* CONFIG_HS20 */
+
+
 static void ieee802_1x_finished(struct hostapd_data *hapd,
-                               struct sta_info *sta, int success)
+                               struct sta_info *sta, int success,
+                               int remediation)
 {
        const u8 *key;
        size_t len;
        /* TODO: get PMKLifetime from WPA parameters */
        static const int dot11RSNAConfigPMKLifetime = 43200;
+       unsigned int session_timeout;
 
 #ifdef CONFIG_HS20
-       if (success) {
-               if (sta->remediation) {
-                       wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
-                                  "to " MACSTR " to indicate Subscription "
-                                  "Remediation",
-                                  MAC2STR(sta->addr));
-                       hs20_send_wnm_notification(hapd, sta->addr,
-                                                  sta->remediation_method,
-                                                  sta->remediation_url);
-                       os_free(sta->remediation_url);
-                       sta->remediation_url = NULL;
-               }
+       if (remediation && !sta->remediation) {
+               sta->remediation = 1;
+               os_free(sta->remediation_url);
+               sta->remediation_url =
+                       os_strdup(hapd->conf->subscr_remediation_url);
+               sta->remediation_method = 1; /* SOAP-XML SPP */
+       }
 
-               if (sta->hs20_deauth_req) {
-                       wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification "
-                                  "to " MACSTR " to indicate imminent "
-                                  "deauthentication", MAC2STR(sta->addr));
-                       hs20_send_wnm_notification_deauth_req(
-                               hapd, sta->addr, sta->hs20_deauth_req);
-               }
+       if (success && (sta->remediation || sta->hs20_deauth_req)) {
+               wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
+                          MACSTR " in 100 ms", MAC2STR(sta->addr));
+               eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
+               eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
+                                      hapd, sta);
        }
 #endif /* CONFIG_HS20 */
 
        key = ieee802_1x_get_key(sta->eapol_sm, &len);
+       if (sta->session_timeout_set)
+               session_timeout = sta->session_timeout;
+       else
+               session_timeout = dot11RSNAConfigPMKLifetime;
        if (success && key && len >= PMK_LEN && !sta->remediation &&
            !sta->hs20_deauth_requested &&
-           wpa_auth_pmksa_add(sta->wpa_sm, key, dot11RSNAConfigPMKLifetime,
+           wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
                               sta->eapol_sm) == 0) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
                               HOSTAPD_LEVEL_DEBUG,