[SK_BUFF]: Introduce ip_hdr(), remove skb->nh.iph
[safe/jmp/linux-2.6] / net / ieee80211 / ieee80211_tx.c
index 42c5061..62a8a2b 100644 (file)
@@ -24,7 +24,6 @@
 
 ******************************************************************************/
 #include <linux/compiler.h>
-#include <linux/config.h>
 #include <linux/errno.h>
 #include <linux/if_arp.h>
 #include <linux/in6.h>
@@ -38,7 +37,6 @@
 #include <linux/slab.h>
 #include <linux/tcp.h>
 #include <linux/types.h>
-#include <linux/version.h>
 #include <linux/wireless.h>
 #include <linux/etherdevice.h>
 #include <asm/uaccess.h>
@@ -56,8 +54,19 @@ Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  Frame  |  fcs |
       |      | tion | (BSSID) |         |         | ence |  data   |      |
       `--------------------------------------------------|         |------'
 Total: 28 non-data bytes                                 `----.----'
-                                                              |
-       .- 'Frame data' expands to <---------------------------'
+                                                             |
+       .- 'Frame data' expands, if WEP enabled, to <----------'
+       |
+       V
+      ,-----------------------.
+Bytes |  4  |   0-2296  |  4  |
+      |-----|-----------|-----|
+Desc. | IV  | Encrypted | ICV |
+      |     | Packet    |     |
+      `-----|           |-----'
+           `-----.-----'
+                 |
+       .- 'Encrypted Packet' expands to
        |
        V
       ,---------------------------------------------------.
@@ -66,18 +75,7 @@ Bytes |  1   |  1   |    1    |    3     |  2   |  0-2304 |
 Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP      |
       | DSAP | SSAP |         |          |      | Packet  |
       | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8|      |         |
-      `-----------------------------------------|         |
-Total: 8 non-data bytes                         `----.----'
-                                                     |
-       .- 'IP Packet' expands, if WEP enabled, to <--'
-       |
-       V
-      ,-----------------------.
-Bytes |  4  |   0-2296  |  4  |
-      |-----|-----------|-----|
-Desc. | IV  | Encrypted | ICV |
-      |     | IP Packet |     |
-      `-----------------------'
+      `----------------------------------------------------
 Total: 8 non-data bytes
 
 802.3 Ethernet Data Frame
@@ -128,7 +126,7 @@ payload of each frame is reduced to 492 bytes.
 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
 
-static inline int ieee80211_copy_snap(u8 * data, u16 h_proto)
+static int ieee80211_copy_snap(u8 * data, u16 h_proto)
 {
        struct ieee80211_snap_hdr *snap;
        u8 *oui;
@@ -151,17 +149,20 @@ static inline int ieee80211_copy_snap(u8 * data, u16 h_proto)
        return SNAP_SIZE + sizeof(u16);
 }
 
-static inline int ieee80211_encrypt_fragment(struct ieee80211_device *ieee,
+static int ieee80211_encrypt_fragment(struct ieee80211_device *ieee,
                                             struct sk_buff *frag, int hdr_len)
 {
        struct ieee80211_crypt_data *crypt = ieee->crypt[ieee->tx_keyidx];
        int res;
 
+       if (crypt == NULL)
+               return -1;
+
        /* To encrypt, frame format is:
         * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
        atomic_inc(&crypt->refcnt);
        res = 0;
-       if (crypt->ops->encrypt_mpdu)
+       if (crypt->ops && crypt->ops->encrypt_mpdu)
                res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
 
        atomic_dec(&crypt->refcnt);
@@ -187,7 +188,7 @@ void ieee80211_txb_free(struct ieee80211_txb *txb)
 }
 
 static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
-                                                gfp_t gfp_mask)
+                                                int headroom, gfp_t gfp_mask)
 {
        struct ieee80211_txb *txb;
        int i;
@@ -201,11 +202,13 @@ static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
        txb->frag_size = txb_size;
 
        for (i = 0; i < nr_frags; i++) {
-               txb->fragments[i] = dev_alloc_skb(txb_size);
+               txb->fragments[i] = __dev_alloc_skb(txb_size + headroom,
+                                                   gfp_mask);
                if (unlikely(!txb->fragments[i])) {
                        i--;
                        break;
                }
+               skb_reserve(txb->fragments[i], headroom);
        }
        if (unlikely(i != nr_frags)) {
                while (i >= 0)
@@ -216,13 +219,43 @@ static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
        return txb;
 }
 
+static int ieee80211_classify(struct sk_buff *skb)
+{
+       struct ethhdr *eth;
+       struct iphdr *ip;
+
+       eth = (struct ethhdr *)skb->data;
+       if (eth->h_proto != htons(ETH_P_IP))
+               return 0;
+
+       ip = ip_hdr(skb);
+       switch (ip->tos & 0xfc) {
+       case 0x20:
+               return 2;
+       case 0x40:
+               return 1;
+       case 0x60:
+               return 3;
+       case 0x80:
+               return 4;
+       case 0xa0:
+               return 5;
+       case 0xc0:
+               return 6;
+       case 0xe0:
+               return 7;
+       default:
+               return 0;
+       }
+}
+
 /* Incoming skb is converted to a txb which consists of
  * a block of 802.11 fragment packets (stored as skbs) */
 int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct ieee80211_device *ieee = netdev_priv(dev);
        struct ieee80211_txb *txb = NULL;
-       struct ieee80211_hdr_3addr *frag_hdr;
+       struct ieee80211_hdr_3addrqos *frag_hdr;
        int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size,
            rts_required;
        unsigned long flags;
@@ -230,9 +263,10 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        int ether_type, encrypt, host_encrypt, host_encrypt_msdu, host_build_iv;
        int bytes, fc, hdr_len;
        struct sk_buff *skb_frag;
-       struct ieee80211_hdr_3addr header = {   /* Ensure zero initialized */
+       struct ieee80211_hdr_3addrqos header = {/* Ensure zero initialized */
                .duration_id = 0,
-               .seq_ctl = 0
+               .seq_ctl = 0,
+               .qos_ctl = 0
        };
        u8 dest[ETH_ALEN], src[ETH_ALEN];
        struct ieee80211_crypt_data *crypt;
@@ -264,9 +298,9 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
            ieee->sec.encrypt;
 
-       host_encrypt = ieee->host_encrypt && encrypt;
-       host_encrypt_msdu = ieee->host_encrypt_msdu && encrypt;
-       host_build_iv = ieee->host_build_iv && encrypt;
+       host_encrypt = ieee->host_encrypt && encrypt && crypt;
+       host_encrypt_msdu = ieee->host_encrypt_msdu && encrypt && crypt;
+       host_build_iv = ieee->host_build_iv && encrypt && crypt;
 
        if (!encrypt && ieee->ieee802_1x &&
            ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
@@ -278,13 +312,7 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        memcpy(dest, skb->data, ETH_ALEN);
        memcpy(src, skb->data + ETH_ALEN, ETH_ALEN);
 
-       /* Advance the SKB to the start of the payload */
-       skb_pull(skb, sizeof(struct ethhdr));
-
-       /* Determine total amount of storage required for TXB packets */
-       bytes = skb->len + SNAP_SIZE + sizeof(u16);
-
-       if (host_encrypt)
+       if (host_encrypt || host_build_iv)
                fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA |
                    IEEE80211_FCTL_PROTECTED;
        else
@@ -302,9 +330,23 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
                memcpy(header.addr2, src, ETH_ALEN);
                memcpy(header.addr3, ieee->bssid, ETH_ALEN);
        }
-       header.frame_ctl = cpu_to_le16(fc);
        hdr_len = IEEE80211_3ADDR_LEN;
 
+       if (ieee->is_qos_active && ieee->is_qos_active(dev, skb)) {
+               fc |= IEEE80211_STYPE_QOS_DATA;
+               hdr_len += 2;
+
+               skb->priority = ieee80211_classify(skb);
+               header.qos_ctl |= cpu_to_le16(skb->priority & IEEE80211_QCTL_TID);
+       }
+       header.frame_ctl = cpu_to_le16(fc);
+
+       /* Advance the SKB to the start of the payload */
+       skb_pull(skb, sizeof(struct ethhdr));
+
+       /* Determine total amount of storage required for TXB packets */
+       bytes = skb->len + SNAP_SIZE + sizeof(u16);
+
        /* Encrypt msdu first on the whole data packet. */
        if ((host_encrypt || host_encrypt_msdu) &&
            crypt && crypt->ops && crypt->ops->encrypt_msdu) {
@@ -338,7 +380,8 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        if (host_encrypt || ieee->host_open_frag) {
                /* Determine fragmentation size based on destination (multicast
                 * and broadcast are not fragmented) */
-               if (is_multicast_ether_addr(dest))
+               if (is_multicast_ether_addr(dest) ||
+                   is_broadcast_ether_addr(dest))
                        frag_size = MAX_FRAG_THRESHOLD;
                else
                        frag_size = ieee->fts;
@@ -347,7 +390,7 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
                 * this stack is providing the full 802.11 header, one will
                 * eventually be affixed to this fragment -- so we must account
                 * for it when determining the amount of payload space. */
-               bytes_per_frag = frag_size - IEEE80211_3ADDR_LEN;
+               bytes_per_frag = frag_size - hdr_len;
                if (ieee->config &
                    (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
                        bytes_per_frag -= IEEE80211_FCS_LEN;
@@ -369,7 +412,7 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        } else {
                nr_frags = 1;
                bytes_per_frag = bytes_last_frag = bytes;
-               frag_size = bytes + IEEE80211_3ADDR_LEN;
+               frag_size = bytes + hdr_len;
        }
 
        rts_required = (frag_size > ieee->rts
@@ -380,7 +423,8 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        /* When we allocate the TXB we allocate enough space for the reserve
         * and full fragment bytes (bytes_per_frag doesn't include prefix,
         * postfix, header, FCS, etc.) */
-       txb = ieee80211_alloc_txb(nr_frags, frag_size, GFP_ATOMIC);
+       txb = ieee80211_alloc_txb(nr_frags, frag_size,
+                                 ieee->tx_headroom, GFP_ATOMIC);
        if (unlikely(!txb)) {
                printk(KERN_WARNING "%s: Could not allocate TXB\n",
                       ieee->dev->name);
@@ -396,7 +440,7 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
        if (rts_required) {
                skb_frag = txb->fragments[0];
                frag_hdr =
-                   (struct ieee80211_hdr_3addr *)skb_put(skb_frag, hdr_len);
+                   (struct ieee80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
 
                /*
                 * Set header frame_ctl to the RTS.
@@ -427,7 +471,7 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
                                    crypt->ops->extra_mpdu_prefix_len);
 
                frag_hdr =
-                   (struct ieee80211_hdr_3addr *)skb_put(skb_frag, hdr_len);
+                   (struct ieee80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
                memcpy(frag_hdr, &header, hdr_len);
 
                /* If this is not the last fragment, then add the MOREFRAGS
@@ -458,13 +502,12 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
                if (host_encrypt)
                        ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
                else if (host_build_iv) {
-                       struct ieee80211_crypt_data *crypt;
-
-                       crypt = ieee->crypt[ieee->tx_keyidx];
                        atomic_inc(&crypt->refcnt);
                        if (crypt->ops->build_iv)
                                crypt->ops->build_iv(skb_frag, hdr_len,
-                                                    crypt->priv);
+                                     ieee->sec.keys[ieee->sec.active_key],
+                                     ieee->sec.key_sizes[ieee->sec.active_key],
+                                     crypt->priv);
                        atomic_dec(&crypt->refcnt);
                }
 
@@ -486,13 +529,6 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
                        return 0;
                }
 
-               if (ret == NETDEV_TX_BUSY) {
-                       printk(KERN_ERR "%s: NETDEV_TX_BUSY returned; "
-                              "driver should report queue full via "
-                              "ieee_device->is_queue_full.\n",
-                              ieee->dev->name);
-               }
-
                ieee80211_txb_free(txb);
        }
 
@@ -508,16 +544,23 @@ int ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
 /* Incoming 802.11 strucure is converted to a TXB
  * a block of 802.11 fragment packets (stored as skbs) */
 int ieee80211_tx_frame(struct ieee80211_device *ieee,
-                      struct ieee80211_hdr *frame, int len)
+                      struct ieee80211_hdr *frame, int hdr_len, int total_len,
+                      int encrypt_mpdu)
 {
        struct ieee80211_txb *txb = NULL;
        unsigned long flags;
        struct net_device_stats *stats = &ieee->stats;
        struct sk_buff *skb_frag;
        int priority = -1;
+       int fraglen = total_len;
+       int headroom = ieee->tx_headroom;
+       struct ieee80211_crypt_data *crypt = ieee->crypt[ieee->tx_keyidx];
 
        spin_lock_irqsave(&ieee->lock, flags);
 
+       if (encrypt_mpdu && (!ieee->sec.encrypt || !crypt))
+               encrypt_mpdu = 0;
+
        /* If there is no driver handler to take the TXB, dont' bother
         * creating it... */
        if (!ieee->hard_start_xmit) {
@@ -525,32 +568,45 @@ int ieee80211_tx_frame(struct ieee80211_device *ieee,
                goto success;
        }
 
-       if (unlikely(len < 24)) {
+       if (unlikely(total_len < 24)) {
                printk(KERN_WARNING "%s: skb too small (%d).\n",
-                      ieee->dev->name, len);
+                      ieee->dev->name, total_len);
                goto success;
        }
 
+       if (encrypt_mpdu) {
+               frame->frame_ctl |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
+               fraglen += crypt->ops->extra_mpdu_prefix_len +
+                          crypt->ops->extra_mpdu_postfix_len;
+               headroom += crypt->ops->extra_mpdu_prefix_len;
+       }
+
        /* When we allocate the TXB we allocate enough space for the reserve
         * and full fragment bytes (bytes_per_frag doesn't include prefix,
         * postfix, header, FCS, etc.) */
-       txb = ieee80211_alloc_txb(1, len, GFP_ATOMIC);
+       txb = ieee80211_alloc_txb(1, fraglen, headroom, GFP_ATOMIC);
        if (unlikely(!txb)) {
                printk(KERN_WARNING "%s: Could not allocate TXB\n",
                       ieee->dev->name);
                goto failed;
        }
        txb->encrypted = 0;
-       txb->payload_size = len;
+       txb->payload_size = fraglen;
 
        skb_frag = txb->fragments[0];
 
-       memcpy(skb_put(skb_frag, len), frame, len);
+       memcpy(skb_put(skb_frag, total_len), frame, total_len);
 
        if (ieee->config &
            (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
                skb_put(skb_frag, 4);
 
+       /* To avoid overcomplicating things, we do the corner-case frame
+        * encryption in software. The only real situation where encryption is
+        * needed here is during software-based shared key authentication. */
+       if (encrypt_mpdu)
+               ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
+
       success:
        spin_unlock_irqrestore(&ieee->lock, flags);