[IPSEC]: Get rid of ipv6_{auth,esp,comp}_hdr
[safe/jmp/linux-2.6] / net / ipv4 / esp4.c
index a315d5d..66eb496 100644 (file)
@@ -8,6 +8,7 @@
 #include <linux/kernel.h>
 #include <linux/pfkeyv2.h>
 #include <linux/random.h>
+#include <linux/spinlock.h>
 #include <net/icmp.h>
 #include <net/protocol.h>
 #include <net/udp.h>
@@ -27,9 +28,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
        int alen;
        int nfrags;
 
-       /* Strip IP+ESP header. */
-       __skb_pull(skb, skb_transport_offset(skb));
-       /* Now skb is pure payload to encrypt */
+       /* skb is pure payload to encrypt */
 
        err = -ENOMEM;
 
@@ -59,12 +58,14 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
        tail[clen - skb->len - 2] = (clen - skb->len) - 2;
        pskb_put(skb, trailer, clen - skb->len);
 
-       __skb_push(skb, skb->data - skb_network_header(skb));
+       skb_push(skb, -skb_network_offset(skb));
        top_iph = ip_hdr(skb);
-       esph = (struct ip_esp_hdr *)(skb_network_header(skb) +
-                                    top_iph->ihl * 4);
+       esph = ip_esp_hdr(skb);
        top_iph->tot_len = htons(skb->len + alen);
-       *(skb_tail_pointer(trailer) - 1) = top_iph->protocol;
+       *(skb_tail_pointer(trailer) - 1) = *skb_mac_header(skb);
+       *skb_mac_header(skb) = IPPROTO_ESP;
+
+       spin_lock_bh(&x->lock);
 
        /* this is non-NULL only with UDP Encapsulation */
        if (x->encap) {
@@ -90,13 +91,11 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
                        break;
                }
 
-               top_iph->protocol = IPPROTO_UDP;
-       } else
-               top_iph->protocol = IPPROTO_ESP;
+               *skb_mac_header(skb) = IPPROTO_UDP;
+       }
 
        esph->spi = x->id.spi;
-       esph->seq_no = htonl(++x->replay.oseq);
-       xfrm_aevent_doreplay(x);
+       esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
 
        if (esp->conf.ivlen) {
                if (unlikely(!esp->conf.ivinitted)) {
@@ -112,7 +111,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
                if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
                        sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
                        if (!sg)
-                               goto error;
+                               goto unlock;
                }
                skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
                err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
@@ -121,7 +120,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
        } while (0);
 
        if (unlikely(err))
-               goto error;
+               goto unlock;
 
        if (esp->conf.ivlen) {
                memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
@@ -134,6 +133,9 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
                memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
        }
 
+unlock:
+       spin_unlock_bh(&x->lock);
+
        ip_send_check(top_iph);
 
 error:
@@ -155,7 +157,7 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
        struct sk_buff *trailer;
        int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
        int alen = esp->auth.icv_trunc_len;
-       int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
+       int elen = skb->len - sizeof(*esph) - esp->conf.ivlen - alen;
        int nfrags;
        int ihl;
        u8 nexthdr[2];
@@ -163,7 +165,7 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
        int padlen;
        int err;
 
-       if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
+       if (!pskb_may_pull(skb, sizeof(*esph)))
                goto out;
 
        if (elen <= 0 || (elen & (blksize-1)))
@@ -191,7 +193,7 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
 
        skb->ip_summed = CHECKSUM_NONE;
 
-       esph = (struct ip_esp_hdr*)skb->data;
+       esph = (struct ip_esp_hdr *)skb->data;
 
        /* Get ivec. This can be wrong, check against another impls. */
        if (esp->conf.ivlen)
@@ -204,7 +206,7 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
                if (!sg)
                        goto out;
        }
-       skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
+       skb_to_sgvec(skb, sg, sizeof(*esph) + esp->conf.ivlen, elen);
        err = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
        if (unlikely(sg != &esp->sgbuf[0]))
                kfree(sg);
@@ -256,8 +258,7 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
                 *    as per draft-ietf-ipsec-udp-encaps-06,
                 *    section 3.1.2
                 */
-               if (x->props.mode == XFRM_MODE_TRANSPORT ||
-                   x->props.mode == XFRM_MODE_BEET)
+               if (x->props.mode == XFRM_MODE_TRANSPORT)
                        skb->ip_summed = CHECKSUM_UNNECESSARY;
        }
 
@@ -294,7 +295,6 @@ static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
                break;
        case XFRM_MODE_BEET:
                /* The worst case. */
-               mtu -= IPV4_BEET_PHMAXLEN;
                mtu += min_t(u32, IPV4_BEET_PHMAXLEN, rem);
                break;
        }
@@ -344,11 +344,6 @@ static int esp_init_state(struct xfrm_state *x)
        struct crypto_blkcipher *tfm;
        u32 align;
 
-       /* null auth and encryption can have zero length keys */
-       if (x->aalg) {
-               if (x->aalg->alg_key_len > 512)
-                       goto error;
-       }
        if (x->ealg == NULL)
                goto error;
 
@@ -360,15 +355,14 @@ static int esp_init_state(struct xfrm_state *x)
                struct xfrm_algo_desc *aalg_desc;
                struct crypto_hash *hash;
 
-               esp->auth.key = x->aalg->alg_key;
-               esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
                hash = crypto_alloc_hash(x->aalg->alg_name, 0,
                                         CRYPTO_ALG_ASYNC);
                if (IS_ERR(hash))
                        goto error;
 
                esp->auth.tfm = hash;
-               if (crypto_hash_setkey(hash, esp->auth.key, esp->auth.key_len))
+               if (crypto_hash_setkey(hash, x->aalg->alg_key,
+                                      (x->aalg->alg_key_len + 7) / 8))
                        goto error;
 
                aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
@@ -390,8 +384,7 @@ static int esp_init_state(struct xfrm_state *x)
                if (!esp->auth.work_icv)
                        goto error;
        }
-       esp->conf.key = x->ealg->alg_key;
-       esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
+
        tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
        if (IS_ERR(tfm))
                goto error;
@@ -404,11 +397,14 @@ static int esp_init_state(struct xfrm_state *x)
                        goto error;
                esp->conf.ivinitted = 0;
        }
-       if (crypto_blkcipher_setkey(tfm, esp->conf.key, esp->conf.key_len))
+       if (crypto_blkcipher_setkey(tfm, x->ealg->alg_key,
+                                   (x->ealg->alg_key_len + 7) / 8))
                goto error;
        x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
        if (x->props.mode == XFRM_MODE_TUNNEL)
                x->props.header_len += sizeof(struct iphdr);
+       else if (x->props.mode == XFRM_MODE_BEET)
+               x->props.header_len += IPV4_BEET_PHMAXLEN;
        if (x->encap) {
                struct xfrm_encap_tmpl *encap = x->encap;
 
@@ -442,6 +438,7 @@ static struct xfrm_type esp_type =
        .description    = "ESP4",
        .owner          = THIS_MODULE,
        .proto          = IPPROTO_ESP,
+       .flags          = XFRM_TYPE_REPLAY_PROT,
        .init_state     = esp_init_state,
        .destructor     = esp_destroy,
        .get_mtu        = esp4_get_mtu,
@@ -480,3 +477,4 @@ static void __exit esp4_fini(void)
 module_init(esp4_init);
 module_exit(esp4_fini);
 MODULE_LICENSE("GPL");
+MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);