Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/kaber/nf-next-2.6
[safe/jmp/linux-2.6] / net / mac80211 / wpa.c
index aff46ad..f4971cd 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2002-2004, Instant802 Networks, Inc.
+ * Copyright 2008, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -19,6 +20,7 @@
 #include "michael.h"
 #include "tkip.h"
 #include "aes_ccm.h"
+#include "aes_cmac.h"
 #include "wpa.h"
 
 ieee80211_tx_result
@@ -29,8 +31,8 @@ ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
        unsigned int hdrlen;
        struct ieee80211_hdr *hdr;
        struct sk_buff *skb = tx->skb;
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
        int authenticator;
-       int wpa_test = 0;
        int tail;
 
        hdr = (struct ieee80211_hdr *)skb->data;
@@ -45,16 +47,15 @@ ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
        data = skb->data + hdrlen;
        data_len = skb->len - hdrlen;
 
-       if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
+       if (info->control.hw_key &&
            !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
-           !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) &&
-           !wpa_test) {
-               /* hwaccel - with no need for preallocated room for MMIC */
+           !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
+               /* hwaccel - with no need for SW-generated MMIC */
                return TX_CONTINUE;
        }
 
        tail = MICHAEL_MIC_LEN;
-       if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
+       if (!info->control.hw_key)
                tail += TKIP_ICV_LEN;
 
        if (WARN_ON(skb_tailroom(skb) < tail ||
@@ -83,16 +84,16 @@ ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
        u8 *data, *key = NULL, key_offset;
        size_t data_len;
        unsigned int hdrlen;
-       struct ieee80211_hdr *hdr;
        u8 mic[MICHAEL_MIC_LEN];
        struct sk_buff *skb = rx->skb;
+       struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
        int authenticator = 1, wpa_test = 0;
 
        /* No way to verify the MIC if the hardware stripped it */
-       if (rx->status->flag & RX_FLAG_MMIC_STRIPPED)
+       if (status->flag & RX_FLAG_MMIC_STRIPPED)
                return RX_CONTINUE;
 
-       hdr = (struct ieee80211_hdr *)skb->data;
        if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
            !ieee80211_has_protected(hdr->frame_control) ||
            !ieee80211_is_data_present(hdr->frame_control))
@@ -120,7 +121,8 @@ ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
                        return RX_DROP_UNUSABLE;
 
                mac80211_ev_michael_mic_failure(rx->sdata, rx->key->conf.keyidx,
-                                               (void *) skb->data);
+                                               (void *) skb->data, NULL,
+                                               GFP_ATOMIC);
                return RX_DROP_UNUSABLE;
        }
 
@@ -144,17 +146,16 @@ static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
        int len, tail;
        u8 *pos;
 
-       if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
-           !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
-               /* hwaccel - with no need for preallocated room for IV/ICV */
-               info->control.hw_key = &tx->key->conf;
+       if (info->control.hw_key &&
+           !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
+               /* hwaccel - with no need for software-generated IV */
                return 0;
        }
 
        hdrlen = ieee80211_hdrlen(hdr->frame_control);
        len = skb->len - hdrlen;
 
-       if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
+       if (info->control.hw_key)
                tail = 0;
        else
                tail = TKIP_ICV_LEN;
@@ -172,13 +173,11 @@ static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
        if (key->u.tkip.tx.iv16 == 0)
                key->u.tkip.tx.iv32++;
 
-       if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
-               /* hwaccel - with preallocated room for IV */
-               ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
+       pos = ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
 
-               info->control.hw_key = &tx->key->conf;
+       /* hwaccel - with software IV */
+       if (info->control.hw_key)
                return 0;
-       }
 
        /* Add room for ICV */
        skb_put(skb, TKIP_ICV_LEN);
@@ -194,19 +193,13 @@ ieee80211_tx_result
 ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
 {
        struct sk_buff *skb = tx->skb;
-       int i;
 
        ieee80211_tx_set_protected(tx);
 
-       if (tkip_encrypt_skb(tx, skb) < 0)
-               return TX_DROP;
-
-       if (tx->extra_frag) {
-               for (i = 0; i < tx->num_extra_frag; i++) {
-                       if (tkip_encrypt_skb(tx, tx->extra_frag[i]))
-                               return TX_DROP;
-               }
-       }
+       do {
+               if (tkip_encrypt_skb(tx, skb) < 0)
+                       return TX_DROP;
+       } while ((skb = skb->next));
 
        return TX_CONTINUE;
 }
@@ -219,6 +212,7 @@ ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
        int hdrlen, res, hwaccel = 0, wpa_test = 0;
        struct ieee80211_key *key = rx->key;
        struct sk_buff *skb = rx->skb;
+       struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
 
        hdrlen = ieee80211_hdrlen(hdr->frame_control);
 
@@ -228,8 +222,8 @@ ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
        if (!rx->sta || skb->len - hdrlen < 12)
                return RX_DROP_UNUSABLE;
 
-       if (rx->status->flag & RX_FLAG_DECRYPTED) {
-               if (rx->status->flag & RX_FLAG_IV_STRIPPED) {
+       if (status->flag & RX_FLAG_DECRYPTED) {
+               if (status->flag & RX_FLAG_IV_STRIPPED) {
                        /*
                         * Hardware took care of all processing, including
                         * replay protection, and stripped the ICV/IV so
@@ -366,18 +360,19 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
        u8 *pos, *pn;
        int i;
 
-       if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
-           !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
-               /* hwaccel - with no need for preallocated room for CCMP
-                * header or MIC fields */
-               info->control.hw_key = &tx->key->conf;
+       if (info->control.hw_key &&
+           !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
+               /*
+                * hwaccel has no need for preallocated room for CCMP
+                * header or MIC fields
+                */
                return 0;
        }
 
        hdrlen = ieee80211_hdrlen(hdr->frame_control);
        len = skb->len - hdrlen;
 
-       if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
+       if (info->control.hw_key)
                tail = 0;
        else
                tail = CCMP_MIC_LEN;
@@ -402,11 +397,9 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
 
        ccmp_pn2hdr(pos, pn, key->conf.keyidx);
 
-       if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
-               /* hwaccel - with preallocated room for CCMP header */
-               info->control.hw_key = &tx->key->conf;
+       /* hwaccel - with software CCMP header */
+       if (info->control.hw_key)
                return 0;
-       }
 
        pos += CCMP_HDR_LEN;
        ccmp_special_blocks(skb, pn, key->u.ccmp.tx_crypto_buf, 0);
@@ -421,19 +414,13 @@ ieee80211_tx_result
 ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
 {
        struct sk_buff *skb = tx->skb;
-       int i;
 
        ieee80211_tx_set_protected(tx);
 
-       if (ccmp_encrypt_skb(tx, skb) < 0)
-               return TX_DROP;
-
-       if (tx->extra_frag) {
-               for (i = 0; i < tx->num_extra_frag; i++) {
-                       if (ccmp_encrypt_skb(tx, tx->extra_frag[i]))
-                               return TX_DROP;
-               }
-       }
+       do {
+               if (ccmp_encrypt_skb(tx, skb) < 0)
+                       return TX_DROP;
+       } while ((skb = skb->next));
 
        return TX_CONTINUE;
 }
@@ -446,6 +433,7 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
        int hdrlen;
        struct ieee80211_key *key = rx->key;
        struct sk_buff *skb = rx->skb;
+       struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
        u8 pn[CCMP_PN_LEN];
        int data_len;
 
@@ -459,8 +447,8 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
        if (!rx->sta || data_len < 0)
                return RX_DROP_UNUSABLE;
 
-       if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
-           (rx->status->flag & RX_FLAG_IV_STRIPPED))
+       if ((status->flag & RX_FLAG_DECRYPTED) &&
+           (status->flag & RX_FLAG_IV_STRIPPED))
                return RX_CONTINUE;
 
        ccmp_hdr2pn(pn, skb->data + hdrlen);
@@ -470,7 +458,7 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
                return RX_DROP_UNUSABLE;
        }
 
-       if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
+       if (!(status->flag & RX_FLAG_DECRYPTED)) {
                /* hardware didn't decrypt/verify MIC */
                ccmp_special_blocks(skb, pn, key->u.ccmp.rx_crypto_buf, 1);
 
@@ -491,3 +479,124 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
 
        return RX_CONTINUE;
 }
+
+
+static void bip_aad(struct sk_buff *skb, u8 *aad)
+{
+       /* BIP AAD: FC(masked) || A1 || A2 || A3 */
+
+       /* FC type/subtype */
+       aad[0] = skb->data[0];
+       /* Mask FC Retry, PwrMgt, MoreData flags to zero */
+       aad[1] = skb->data[1] & ~(BIT(4) | BIT(5) | BIT(6));
+       /* A1 || A2 || A3 */
+       memcpy(aad + 2, skb->data + 4, 3 * ETH_ALEN);
+}
+
+
+static inline void bip_ipn_swap(u8 *d, const u8 *s)
+{
+       *d++ = s[5];
+       *d++ = s[4];
+       *d++ = s[3];
+       *d++ = s[2];
+       *d++ = s[1];
+       *d = s[0];
+}
+
+
+ieee80211_tx_result
+ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
+{
+       struct sk_buff *skb = tx->skb;
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+       struct ieee80211_key *key = tx->key;
+       struct ieee80211_mmie *mmie;
+       u8 *pn, aad[20];
+       int i;
+
+       if (info->control.hw_key)
+               return 0;
+
+       if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
+               return TX_DROP;
+
+       mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
+       mmie->element_id = WLAN_EID_MMIE;
+       mmie->length = sizeof(*mmie) - 2;
+       mmie->key_id = cpu_to_le16(key->conf.keyidx);
+
+       /* PN = PN + 1 */
+       pn = key->u.aes_cmac.tx_pn;
+
+       for (i = sizeof(key->u.aes_cmac.tx_pn) - 1; i >= 0; i--) {
+               pn[i]++;
+               if (pn[i])
+                       break;
+       }
+       bip_ipn_swap(mmie->sequence_number, pn);
+
+       bip_aad(skb, aad);
+
+       /*
+        * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
+        */
+       ieee80211_aes_cmac(key->u.aes_cmac.tfm, key->u.aes_cmac.tx_crypto_buf,
+                          aad, skb->data + 24, skb->len - 24, mmie->mic);
+
+       return TX_CONTINUE;
+}
+
+
+ieee80211_rx_result
+ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
+{
+       struct sk_buff *skb = rx->skb;
+       struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
+       struct ieee80211_key *key = rx->key;
+       struct ieee80211_mmie *mmie;
+       u8 aad[20], mic[8], ipn[6];
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+
+       if (!ieee80211_is_mgmt(hdr->frame_control))
+               return RX_CONTINUE;
+
+       if ((status->flag & RX_FLAG_DECRYPTED) &&
+           (status->flag & RX_FLAG_IV_STRIPPED))
+               return RX_CONTINUE;
+
+       if (skb->len < 24 + sizeof(*mmie))
+               return RX_DROP_UNUSABLE;
+
+       mmie = (struct ieee80211_mmie *)
+               (skb->data + skb->len - sizeof(*mmie));
+       if (mmie->element_id != WLAN_EID_MMIE ||
+           mmie->length != sizeof(*mmie) - 2)
+               return RX_DROP_UNUSABLE; /* Invalid MMIE */
+
+       bip_ipn_swap(ipn, mmie->sequence_number);
+
+       if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
+               key->u.aes_cmac.replays++;
+               return RX_DROP_UNUSABLE;
+       }
+
+       if (!(status->flag & RX_FLAG_DECRYPTED)) {
+               /* hardware didn't decrypt/verify MIC */
+               bip_aad(skb, aad);
+               ieee80211_aes_cmac(key->u.aes_cmac.tfm,
+                                  key->u.aes_cmac.rx_crypto_buf, aad,
+                                  skb->data + 24, skb->len - 24, mic);
+               if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
+                       key->u.aes_cmac.icverrors++;
+                       return RX_DROP_UNUSABLE;
+               }
+       }
+
+       memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
+
+       /* Remove MMIE */
+       skb_trim(skb, skb->len - sizeof(*mmie));
+
+       return RX_CONTINUE;
+}