Use os_get_random() for Shared Key authentication challenge
[mech_eap.git] / src / ap / ieee802_11.c
index 89911b1..ca3065b 100644 (file)
@@ -23,6 +23,7 @@
 #include "radius/radius_client.h"
 #include "p2p/p2p.h"
 #include "wps/wps.h"
+#include "fst/fst.h"
 #include "hostapd.h"
 #include "beacon.h"
 #include "ieee802_11_auth.h"
@@ -38,6 +39,7 @@
 #include "p2p_hostapd.h"
 #include "ap_drv_ops.h"
 #include "wnm_ap.h"
+#include "hw_features.h"
 #include "ieee802_11.h"
 #include "dfs.h"
 
@@ -132,8 +134,7 @@ u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
 }
 
 
-u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
-                          int probe)
+u16 hostapd_own_capab_info(struct hostapd_data *hapd)
 {
        int capab = WLAN_CAPABILITY_ESS;
        int privacy;
@@ -166,20 +167,6 @@ u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
                privacy = 1;
 #endif /* CONFIG_HS20 */
 
-       if (sta) {
-               int policy, def_klen;
-               if (probe && sta->ssid_probe) {
-                       policy = sta->ssid_probe->security_policy;
-                       def_klen = sta->ssid_probe->wep.default_len;
-               } else {
-                       policy = sta->ssid->security_policy;
-                       def_klen = sta->ssid->wep.default_len;
-               }
-               privacy = policy != SECURITY_PLAINTEXT;
-               if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
-                       privacy = 0;
-       }
-
        if (privacy)
                capab |= WLAN_CAPABILITY_PRIVACY;
 
@@ -206,6 +193,7 @@ u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
 }
 
 
+#ifndef CONFIG_NO_RC4
 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
                           u16 auth_transaction, const u8 *challenge,
                           int iswep)
@@ -219,16 +207,17 @@ static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
                if (!sta->challenge) {
                        /* Generate a pseudo-random challenge */
                        u8 key[8];
-                       struct os_time now;
-                       int r;
+
                        sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
                        if (sta->challenge == NULL)
                                return WLAN_STATUS_UNSPECIFIED_FAILURE;
 
-                       os_get_time(&now);
-                       r = os_random();
-                       os_memcpy(key, &now.sec, 4);
-                       os_memcpy(key + 4, &r, 4);
+                       if (os_get_random(key, sizeof(key)) < 0) {
+                               os_free(sta->challenge);
+                               sta->challenge = NULL;
+                               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+                       }
+
                        rc4_skip(key, sizeof(key), 0,
                                 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
                }
@@ -259,6 +248,7 @@ static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
 
        return 0;
 }
+#endif /* CONFIG_NO_RC4 */
 
 
 static void send_auth_reply(struct hostapd_data *hapd,
@@ -328,7 +318,6 @@ static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
 
 #ifdef CONFIG_SAE
 
-#define dot11RSNASAERetransPeriod 40   /* msec */
 #define dot11RSNASAESync 5             /* attempts */
 
 
@@ -511,12 +500,14 @@ static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
        switch (sta->sae->state) {
        case SAE_COMMITTED:
                ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
-               eloop_register_timeout(0, dot11RSNASAERetransPeriod * 1000,
+               eloop_register_timeout(0,
+                                      hapd->dot11RSNASAERetransPeriod * 1000,
                                       auth_sae_retransmit_timer, hapd, sta);
                break;
        case SAE_CONFIRMED:
                ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
-               eloop_register_timeout(0, dot11RSNASAERetransPeriod * 1000,
+               eloop_register_timeout(0,
+                                      hapd->dot11RSNASAERetransPeriod * 1000,
                                       auth_sae_retransmit_timer, hapd, sta);
                break;
        default:
@@ -542,7 +533,7 @@ static void sae_set_retransmit_timer(struct hostapd_data *hapd,
                return;
 
        eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
-       eloop_register_timeout(0, dot11RSNASAERetransPeriod * 1000,
+       eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
                               auth_sae_retransmit_timer, hapd, sta);
 }
 
@@ -624,7 +615,7 @@ static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
                                return WLAN_STATUS_SUCCESS;
                        sta->sae->sync++;
 
-                       ret = auth_sae_send_commit(hapd, sta, bssid, 1);
+                       ret = auth_sae_send_commit(hapd, sta, bssid, 0);
                        if (ret)
                                return ret;
 
@@ -675,7 +666,7 @@ static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
                        wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
                        sta->sae->state = SAE_ACCEPTED;
                        wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
-                                              sta->sae->pmk);
+                                              sta->sae->pmk, sta->sae->pmkid);
                }
                break;
        case SAE_ACCEPTED:
@@ -784,6 +775,12 @@ static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
                                        ((const u8 *) mgmt) + len -
                                        mgmt->u.auth.variable, &token,
                                        &token_len, hapd->conf->sae_groups);
+               if (resp == SAE_SILENTLY_DISCARD) {
+                       wpa_printf(MSG_DEBUG,
+                                  "SAE: Drop commit message from " MACSTR " due to reflection attack",
+                                  MAC2STR(sta->addr));
+                       return;
+               }
                if (token && check_sae_token(hapd, sta->addr, token, token_len)
                    < 0) {
                        wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
@@ -890,7 +887,7 @@ static void handle_auth(struct hostapd_data *hapd,
        u16 fc;
        const u8 *challenge = NULL;
        u32 session_timeout, acct_interim_interval;
-       int vlan_id = 0;
+       struct vlan_description vlan_id;
        struct hostapd_sta_wpa_psk_short *psk = NULL;
        u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
        size_t resp_ies_len = 0;
@@ -898,6 +895,8 @@ static void handle_auth(struct hostapd_data *hapd,
        char *radius_cui = NULL;
        u16 seq_ctrl;
 
+       os_memset(&vlan_id, 0, sizeof(vlan_id));
+
        if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
                wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
                           (unsigned long) len);
@@ -934,6 +933,16 @@ static void handle_auth(struct hostapd_data *hapd,
                   challenge ? " challenge" : "",
                   seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
 
+#ifdef CONFIG_NO_RC4
+       if (auth_alg == WLAN_AUTH_SHARED_KEY) {
+               wpa_printf(MSG_INFO,
+                          "Unsupported authentication algorithm (%d)",
+                          auth_alg);
+               resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
+               goto fail;
+       }
+#endif /* CONFIG_NO_RC4 */
+
        if (hapd->tkip_countermeasures) {
                resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
                goto fail;
@@ -972,6 +981,61 @@ static void handle_auth(struct hostapd_data *hapd,
                goto fail;
        }
 
+       if (hapd->conf->no_auth_if_seen_on) {
+               struct hostapd_data *other;
+
+               other = sta_track_seen_on(hapd->iface, mgmt->sa,
+                                         hapd->conf->no_auth_if_seen_on);
+               if (other) {
+                       u8 *pos;
+                       u32 info;
+                       u8 op_class, channel, phytype;
+
+                       wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
+                                  MACSTR " since STA has been seen on %s",
+                                  hapd->conf->iface, MAC2STR(mgmt->sa),
+                                  hapd->conf->no_auth_if_seen_on);
+
+                       resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
+                       pos = &resp_ies[0];
+                       *pos++ = WLAN_EID_NEIGHBOR_REPORT;
+                       *pos++ = 13;
+                       os_memcpy(pos, other->own_addr, ETH_ALEN);
+                       pos += ETH_ALEN;
+                       info = 0; /* TODO: BSSID Information */
+                       WPA_PUT_LE32(pos, info);
+                       pos += 4;
+                       if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
+                               phytype = 8; /* dmg */
+                       else if (other->iconf->ieee80211ac)
+                               phytype = 9; /* vht */
+                       else if (other->iconf->ieee80211n)
+                               phytype = 7; /* ht */
+                       else if (other->iconf->hw_mode ==
+                                HOSTAPD_MODE_IEEE80211A)
+                               phytype = 4; /* ofdm */
+                       else if (other->iconf->hw_mode ==
+                                HOSTAPD_MODE_IEEE80211G)
+                               phytype = 6; /* erp */
+                       else
+                               phytype = 5; /* hrdsss */
+                       if (ieee80211_freq_to_channel_ext(
+                                   hostapd_hw_get_freq(other,
+                                                       other->iconf->channel),
+                                   other->iconf->secondary_channel,
+                                   other->iconf->ieee80211ac,
+                                   &op_class, &channel) == NUM_HOSTAPD_MODES) {
+                               op_class = 0;
+                               channel = other->iconf->channel;
+                       }
+                       *pos++ = op_class;
+                       *pos++ = channel;
+                       *pos++ = phytype;
+                       resp_ies_len = pos - &resp_ies[0];
+                       goto fail;
+               }
+       }
+
        res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
                                      &session_timeout,
                                      &acct_interim_interval, &vlan_id,
@@ -1006,13 +1070,22 @@ static void handle_auth(struct hostapd_data *hapd,
                                       seq_ctrl);
                        return;
                }
+#ifdef CONFIG_MESH
+               if ((hapd->conf->mesh & MESH_ENABLED) &&
+                   sta->plink_state == PLINK_BLOCKED) {
+                       wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
+                                  " is blocked - drop Authentication frame",
+                                  MAC2STR(mgmt->sa));
+                       return;
+               }
+#endif /* CONFIG_MESH */
        } else {
 #ifdef CONFIG_MESH
                if (hapd->conf->mesh & MESH_ENABLED) {
                        /* if the mesh peer is not available, we don't do auth.
                         */
                        wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
-                                  " not yet known - drop Authentiation frame",
+                                  " not yet known - drop Authentication frame",
                                   MAC2STR(mgmt->sa));
                        /*
                         * Save a copy of the frame so that it can be processed
@@ -1034,19 +1107,23 @@ static void handle_auth(struct hostapd_data *hapd,
        sta->last_seq_ctrl = seq_ctrl;
        sta->last_subtype = WLAN_FC_STYPE_AUTH;
 
-       if (vlan_id > 0) {
-               if (!hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
-                       hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
-                                      HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
-                                      "%d received from RADIUS server",
-                                      vlan_id);
-                       resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-                       goto fail;
-               }
-               sta->vlan_id = vlan_id;
+       if (vlan_id.notempty &&
+           !hostapd_vlan_valid(hapd->conf->vlan, &vlan_id)) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
-                              HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
+                              HOSTAPD_LEVEL_INFO,
+                              "Invalid VLAN %d%s received from RADIUS server",
+                              vlan_id.untagged,
+                              vlan_id.tagged[0] ? "+" : "");
+               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
+               goto fail;
        }
+       if (ap_sta_set_vlan(hapd, sta, &vlan_id) < 0) {
+               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
+               goto fail;
+       }
+       if (sta->vlan_id)
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
+                              HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
 
        hostapd_free_psk_list(sta->psk);
        if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
@@ -1081,6 +1158,7 @@ static void handle_auth(struct hostapd_data *hapd,
                sta->auth_alg = WLAN_AUTH_OPEN;
                mlme_authenticate_indication(hapd, sta);
                break;
+#ifndef CONFIG_NO_RC4
        case WLAN_AUTH_SHARED_KEY:
                resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
                                       fc & WLAN_FC_ISWEP);
@@ -1094,6 +1172,7 @@ static void handle_auth(struct hostapd_data *hapd,
                        resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
                }
                break;
+#endif /* CONFIG_NO_RC4 */
 #ifdef CONFIG_IEEE80211R
        case WLAN_AUTH_FT:
                sta->auth_alg = WLAN_AUTH_FT;
@@ -1147,7 +1226,7 @@ static void handle_auth(struct hostapd_data *hapd,
 }
 
 
-static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
+int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
 {
        int i, j = 32, aid;
 
@@ -1264,6 +1343,9 @@ static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
        }
 #endif /* CONFIG_INTERWORKING */
 
+       if (ext_capab_ie_len > 0)
+               sta->ecsa_supported = !!(ext_capab_ie[0] & BIT(2));
+
        return WLAN_STATUS_SUCCESS;
 }
 
@@ -1297,8 +1379,7 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
        if (resp != WLAN_STATUS_SUCCESS)
                return resp;
 #ifdef CONFIG_IEEE80211N
-       resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
-                                elems.ht_capabilities_len);
+       resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
        if (resp != WLAN_STATUS_SUCCESS)
                return resp;
        if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
@@ -1311,14 +1392,15 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
 #endif /* CONFIG_IEEE80211N */
 
 #ifdef CONFIG_IEEE80211AC
-       resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
-                                 elems.vht_capabilities_len);
-       if (resp != WLAN_STATUS_SUCCESS)
-               return resp;
+       if (hapd->iconf->ieee80211ac) {
+               resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
+               if (resp != WLAN_STATUS_SUCCESS)
+                       return resp;
 
-       resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
-       if (resp != WLAN_STATUS_SUCCESS)
-               return resp;
+               resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
+               if (resp != WLAN_STATUS_SUCCESS)
+                       return resp;
+       }
 
        if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
            !(sta->flags & WLAN_STA_VHT)) {
@@ -1546,6 +1628,14 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
                sta->hs20_ie = NULL;
 #endif /* CONFIG_HS20 */
 
+#ifdef CONFIG_FST
+       wpabuf_free(sta->mb_ies);
+       if (hapd->iface->fst)
+               sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
+       else
+               sta->mb_ies = NULL;
+#endif /* CONFIG_FST */
+
        return WLAN_STATUS_SUCCESS;
 }
 
@@ -1594,7 +1684,7 @@ static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
        send_len = IEEE80211_HDRLEN;
        send_len += sizeof(reply->u.assoc_resp);
        reply->u.assoc_resp.capab_info =
-               host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
+               host_to_le16(hostapd_own_capab_info(hapd));
        reply->u.assoc_resp.status_code = host_to_le16(status_code);
        reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
        /* Supported rates */
@@ -1634,6 +1724,14 @@ static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
        if (sta->qos_map_enabled)
                p = hostapd_eid_qos_map_set(hapd, p);
 
+#ifdef CONFIG_FST
+       if (hapd->iface->fst_ies) {
+               os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
+                         wpabuf_len(hapd->iface->fst_ies));
+               p += wpabuf_len(hapd->iface->fst_ies);
+       }
+#endif /* CONFIG_FST */
+
 #ifdef CONFIG_IEEE80211AC
        if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
                p = hostapd_eid_vendor_vht(hapd, p);
@@ -1655,7 +1753,7 @@ static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
 #endif /* CONFIG_WPS */
 
 #ifdef CONFIG_P2P
-       if (sta->p2p_ie) {
+       if (sta->p2p_ie && hapd->p2p_group) {
                struct wpabuf *p2p_resp_ie;
                enum p2p_status_code status;
                switch (status_code) {
@@ -1928,7 +2026,7 @@ static void handle_disassoc(struct hostapd_data *hapd,
        /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
         * authenticated. */
        accounting_sta_stop(hapd, sta);
-       ieee802_1x_free_station(sta);
+       ieee802_1x_free_station(hapd, sta);
        if (sta->ipaddr)
                hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
        ap_sta_ip6addr_del(hapd, sta);
@@ -2112,10 +2210,20 @@ static int handle_action(struct hostapd_data *hapd,
                ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
                return 1;
 #endif /* CONFIG_WNM */
+#ifdef CONFIG_FST
+       case WLAN_ACTION_FST:
+               if (hapd->iface->fst)
+                       fst_rx_action(hapd->iface->fst, mgmt, len);
+               else
+                       wpa_printf(MSG_DEBUG,
+                                  "FST: Ignore FST Action frame - no FST attached");
+               return 1;
+#endif /* CONFIG_FST */
        case WLAN_ACTION_PUBLIC:
        case WLAN_ACTION_PROTECTED_DUAL:
 #ifdef CONFIG_IEEE80211N
-               if (mgmt->u.action.u.public_action.action ==
+               if (len >= IEEE80211_HDRLEN + 2 &&
+                   mgmt->u.action.u.public_action.action ==
                    WLAN_PA_20_40_BSS_COEX) {
                        wpa_printf(MSG_DEBUG,
                                   "HT20/40 coex mgmt frame received from STA "
@@ -2248,6 +2356,9 @@ int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
                return 0;
        }
 
+       if (hapd->iconf->track_sta_max_num)
+               sta_track_add(hapd->iface, mgmt->sa);
+
        switch (stype) {
        case WLAN_FC_STYPE_AUTH:
                wpa_printf(MSG_DEBUG, "mgmt::auth");
@@ -2335,7 +2446,7 @@ static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
                                       char *ifname_wds)
 {
        int i;
-       struct hostapd_ssid *ssid = sta->ssid;
+       struct hostapd_ssid *ssid = &hapd->conf->ssid;
 
        if (hapd->conf->ieee802_1x || hapd->conf->wpa)
                return;
@@ -2473,11 +2584,11 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                 * so bind it to the selected VLAN interface now, since the
                 * interface selection is not going to change anymore.
                 */
-               if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
+               if (ap_sta_bind_vlan(hapd, sta) < 0)
                        return;
        } else if (sta->vlan_id) {
                /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
-               if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
+               if (ap_sta_bind_vlan(hapd, sta) < 0)
                        return;
        }