Move hostapd configuration parser into separate file
[libeap.git] / hostapd / ieee802_11.c
index b8d0443..f3dc81a 100644 (file)
@@ -29,7 +29,6 @@
 #include "beacon.h"
 #include "hw_features.h"
 #include "ieee802_11_auth.h"
-#include "sta_flags.h"
 #include "sta_info.h"
 #include "ieee802_1x.h"
 #include "wpa.h"
@@ -197,7 +196,8 @@ void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
  * @addr: Address of the destination STA
  * @reason: Reason code for Deauthentication
  */
-void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
+void ieee802_11_send_deauth(struct hostapd_data *hapd, const u8 *addr,
+                           u16 reason)
 {
        struct ieee80211_mgmt mgmt;
 
@@ -211,14 +211,15 @@ void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
        os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
        os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
        mgmt.u.deauth.reason_code = host_to_le16(reason);
-       if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
-                                   sizeof(mgmt.u.deauth)) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
+                                      sizeof(mgmt.u.deauth)) < 0)
                perror("ieee802_11_send_deauth: send");
 }
 
 
 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
-                          u16 auth_transaction, u8 *challenge, int iswep)
+                          u16 auth_transaction, const u8 *challenge,
+                          int iswep)
 {
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_DEBUG,
@@ -307,7 +308,7 @@ static void send_auth_reply(struct hostapd_data *hapd,
                   " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
                   MAC2STR(dst), auth_alg, auth_transaction,
                   resp, (unsigned long) ies_len);
-       if (hostapd_send_mgmt_frame(hapd, reply, rlen) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, reply, rlen) < 0)
                perror("send_auth_reply: send");
 
        os_free(buf);
@@ -340,15 +341,15 @@ static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
 #endif /* CONFIG_IEEE80211R */
 
 
-static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
-                       size_t len)
+static void handle_auth(struct hostapd_data *hapd,
+                       const struct ieee80211_mgmt *mgmt, size_t len)
 {
        u16 auth_alg, auth_transaction, status_code;
        u16 resp = WLAN_STATUS_SUCCESS;
        struct sta_info *sta = NULL;
        int res;
        u16 fc;
-       u8 *challenge = NULL;
+       const u8 *challenge = NULL;
        u32 session_timeout, acct_interim_interval;
        int vlan_id = 0;
        u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
@@ -636,11 +637,11 @@ static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
 
 
 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
-                          u8 *ies, size_t ies_len, int reassoc)
+                          const u8 *ies, size_t ies_len, int reassoc)
 {
        struct ieee802_11_elems elems;
        u16 resp;
-       u8 *wpa_ie;
+       const u8 *wpa_ie;
        size_t wpa_ie_len;
 
        if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
@@ -815,14 +816,14 @@ static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
        send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
        reply.u.deauth.reason_code = host_to_le16(reason_code);
 
-       if (hostapd_send_mgmt_frame(hapd, &reply, send_len) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, &reply, send_len) < 0)
                wpa_printf(MSG_INFO, "Failed to send deauth: %s",
                           strerror(errno));
 }
 
 
 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
-                           u16 status_code, int reassoc, u8 *ies,
+                           u16 status_code, int reassoc, const u8 *ies,
                            size_t ies_len)
 {
        int send_len;
@@ -876,18 +877,19 @@ static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
 
        send_len += p - reply->u.assoc_resp.variable;
 
-       if (hostapd_send_mgmt_frame(hapd, reply, send_len) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, reply, send_len) < 0)
                wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
                           strerror(errno));
 }
 
 
 static void handle_assoc(struct hostapd_data *hapd,
-                        struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
+                        const struct ieee80211_mgmt *mgmt, size_t len,
+                        int reassoc)
 {
        u16 capab_info, listen_interval;
        u16 resp = WLAN_STATUS_SUCCESS;
-       u8 *pos;
+       const u8 *pos;
        int left, i;
        struct sta_info *sta;
 
@@ -1054,7 +1056,7 @@ static void handle_assoc(struct hostapd_data *hapd,
 
 
 static void handle_disassoc(struct hostapd_data *hapd,
-                           struct ieee80211_mgmt *mgmt, size_t len)
+                           const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct sta_info *sta;
 
@@ -1103,7 +1105,7 @@ static void handle_disassoc(struct hostapd_data *hapd,
 
 
 static void handle_deauth(struct hostapd_data *hapd,
-                         struct ieee80211_mgmt *mgmt, size_t len)
+                         const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct sta_info *sta;
 
@@ -1140,7 +1142,7 @@ static void handle_deauth(struct hostapd_data *hapd,
 
 
 static void handle_beacon(struct hostapd_data *hapd,
-                         struct ieee80211_mgmt *mgmt, size_t len,
+                         const struct ieee80211_mgmt *mgmt, size_t len,
                          struct hostapd_frame_info *fi)
 {
        struct ieee802_11_elems elems;
@@ -1185,16 +1187,17 @@ void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
        os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
                  WLAN_SA_QUERY_TR_ID_LEN);
        end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
-       if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
                perror("ieee802_11_send_sa_query_req: send");
 }
 
 
 static void hostapd_sa_query_action(struct hostapd_data *hapd,
-                                   struct ieee80211_mgmt *mgmt, size_t len)
+                                   const struct ieee80211_mgmt *mgmt,
+                                   size_t len)
 {
        struct sta_info *sta;
-       u8 *end;
+       const u8 *end;
        int i;
 
        end = mgmt->u.action.u.sa_query_resp.trans_id +
@@ -1256,7 +1259,7 @@ static int robust_action_frame(u8 category)
 
 
 static void handle_action(struct hostapd_data *hapd,
-                         struct ieee80211_mgmt *mgmt, size_t len)
+                         const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct sta_info *sta;
 
@@ -1316,6 +1319,8 @@ static void handle_action(struct hostapd_data *hapd,
                       mgmt->u.action.category);
        if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
            !(mgmt->sa[0] & 0x01)) {
+               struct ieee80211_mgmt *resp;
+
                /*
                 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
                 * Return the Action frame to the source without change
@@ -1323,12 +1328,17 @@ static void handle_action(struct hostapd_data *hapd,
                 */
                wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
                           "frame back to sender");
-               os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
-               os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
-               os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
-               mgmt->u.action.category |= 0x80;
+               resp = os_malloc(len);
+               if (resp == NULL)
+                       return;
+               os_memcpy(resp, mgmt, len);
+               os_memcpy(resp->da, resp->sa, ETH_ALEN);
+               os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
+               os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
+               resp->u.action.category |= 0x80;
 
-               hostapd_send_mgmt_frame(hapd, mgmt, len);
+               hapd->drv.send_mgmt_frame(hapd, resp, len);
+               os_free(resp);
        }
 }
 
@@ -1339,7 +1349,6 @@ static void handle_action(struct hostapd_data *hapd,
  * sent to)
  * @buf: management frame data (starting from IEEE 802.11 header)
  * @len: length of frame data in octets
- * @stype: management frame subtype from frame control field
  * @fi: meta data about received frame (signal level, etc.)
  *
  * Process all incoming IEEE 802.11 management frames. This will be called for
@@ -1347,11 +1356,16 @@ static void handle_action(struct hostapd_data *hapd,
  * addition, it can be called to re-inserted pending frames (e.g., when using
  * external RADIUS server as an MAC ACL).
  */
-void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
+void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
                     struct hostapd_frame_info *fi)
 {
-       struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
+       struct ieee80211_mgmt *mgmt;
        int broadcast;
+       u16 fc, stype;
+
+       mgmt = (struct ieee80211_mgmt *) buf;
+       fc = le_to_host16(mgmt->frame_control);
+       stype = WLAN_FC_GET_STYPE(fc);
 
        if (stype == WLAN_FC_STYPE_BEACON) {
                handle_beacon(hapd, mgmt, len, fi);
@@ -1418,7 +1432,7 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
 
 
 static void handle_auth_cb(struct hostapd_data *hapd,
-                          struct ieee80211_mgmt *mgmt,
+                          const struct ieee80211_mgmt *mgmt,
                           size_t len, int ok)
 {
        u16 auth_alg, auth_transaction, status_code;
@@ -1459,7 +1473,7 @@ static void handle_auth_cb(struct hostapd_data *hapd,
 
 
 static void handle_assoc_cb(struct hostapd_data *hapd,
-                           struct ieee80211_mgmt *mgmt,
+                           const struct ieee80211_mgmt *mgmt,
                            size_t len, int reassoc, int ok)
 {
        u16 status;
@@ -1595,10 +1609,11 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
  * @stype: management frame subtype from frame control field
  * @ok: Whether the frame was ACK'ed
  */
-void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
+void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
                        u16 stype, int ok)
 {
-       struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
+       const struct ieee80211_mgmt *mgmt;
+       mgmt = (const struct ieee80211_mgmt *) buf;
 
        switch (stype) {
        case WLAN_FC_STYPE_AUTH:
@@ -1643,4 +1658,65 @@ int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
        return 0;
 }
 
+
+void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
+                      const u8 *buf, size_t len, int ack)
+{
+       struct sta_info *sta;
+       struct hostapd_iface *iface = hapd->iface;
+
+       sta = ap_get_sta(hapd, addr);
+       if (sta == NULL && iface->num_bss > 1) {
+               size_t j;
+               for (j = 0; j < iface->num_bss; j++) {
+                       hapd = iface->bss[j];
+                       sta = ap_get_sta(hapd, addr);
+                       if (sta)
+                               break;
+               }
+       }
+       if (sta == NULL)
+               return;
+       if (sta->flags & WLAN_STA_PENDING_POLL) {
+               wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
+                          "activity poll", MAC2STR(sta->addr),
+                          ack ? "ACKed" : "did not ACK");
+               if (ack)
+                       sta->flags &= ~WLAN_STA_PENDING_POLL;
+       }
+
+       ieee802_1x_tx_status(hapd, sta, buf, len, ack);
+}
+
+
+void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
+                               int wds)
+{
+       struct sta_info *sta;
+
+       sta = ap_get_sta(hapd, src);
+       if (sta && (sta->flags & WLAN_STA_ASSOC)) {
+               if (wds && !(sta->flags & WLAN_STA_WDS)) {
+                       wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
+                                  "STA " MACSTR " (aid %u)",
+                                  MAC2STR(sta->addr), sta->aid);
+                       sta->flags |= WLAN_STA_WDS;
+                       hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
+               }
+               return;
+       }
+
+       wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
+                  MACSTR, MAC2STR(src));
+       if (sta && (sta->flags & WLAN_STA_AUTH))
+               hostapd_sta_disassoc(
+                       hapd, src,
+                       WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
+       else
+               hostapd_sta_deauth(
+                       hapd, src,
+                       WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
+}
+
+
 #endif /* CONFIG_NATIVE_WINDOWS */