[IPSEC]: Kill afinfo->nf_post_routing
[safe/jmp/linux-2.6] / net / ipv6 / esp6.c
index 9fc1940..5bd5292 100644 (file)
 #include <net/ip.h>
 #include <net/xfrm.h>
 #include <net/esp.h>
-#include <asm/scatterlist.h>
+#include <linux/scatterlist.h>
 #include <linux/crypto.h>
 #include <linux/kernel.h>
 #include <linux/pfkeyv2.h>
 #include <linux/random.h>
+#include <linux/spinlock.h>
 #include <net/icmp.h>
 #include <net/ipv6.h>
 #include <net/protocol.h>
@@ -42,8 +43,7 @@
 static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
 {
        int err;
-       struct ipv6hdr *top_iph;
-       struct ipv6_esp_hdr *esph;
+       struct ip_esp_hdr *esph;
        struct crypto_blkcipher *tfm;
        struct blkcipher_desc desc;
        struct sk_buff *trailer;
@@ -53,13 +53,8 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
        int nfrags;
        u8 *tail;
        struct esp_data *esp = x->data;
-       int hdr_len = (skb_transport_offset(skb) +
-                      sizeof(*esph) + esp->conf.ivlen);
 
-       /* Strip IP+ESP header. */
-       __skb_pull(skb, hdr_len);
-
-       /* Now skb is pure payload to encrypt */
+       /* skb is pure payload to encrypt */
        err = -ENOMEM;
 
        /* Round to block size */
@@ -88,16 +83,16 @@ static int esp6_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_network_offset(skb));
-       top_iph = ipv6_hdr(skb);
-       esph = (struct ipv6_esp_hdr *)skb_transport_header(skb);
-       top_iph->payload_len = htons(skb->len + alen - sizeof(*top_iph));
+       skb_push(skb, -skb_network_offset(skb));
+       esph = ip_esp_hdr(skb);
        *(skb_tail_pointer(trailer) - 1) = *skb_mac_header(skb);
        *skb_mac_header(skb) = IPPROTO_ESP;
 
        esph->spi = x->id.spi;
        esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
 
+       spin_lock_bh(&x->lock);
+
        if (esp->conf.ivlen) {
                if (unlikely(!esp->conf.ivinitted)) {
                        get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
@@ -112,16 +107,20 @@ static int esp6_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);
+               sg_init_table(sg, nfrags);
+               skb_to_sgvec(skb, sg,
+                            esph->enc_data +
+                            esp->conf.ivlen -
+                            skb->data, clen);
                err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
                if (unlikely(sg != &esp->sgbuf[0]))
                        kfree(sg);
        } 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 esp6_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);
+
 error:
        return err;
 }
@@ -141,19 +143,19 @@ error:
 static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
 {
        struct ipv6hdr *iph;
-       struct ipv6_esp_hdr *esph;
+       struct ip_esp_hdr *esph;
        struct esp_data *esp = x->data;
        struct crypto_blkcipher *tfm = esp->conf.tfm;
        struct blkcipher_desc desc = { .tfm = tfm };
        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 ipv6_esp_hdr) - esp->conf.ivlen - alen;
+       int elen = skb->len - sizeof(*esph) - esp->conf.ivlen - alen;
        int hdr_len = skb_network_header_len(skb);
        int nfrags;
        int ret = 0;
 
-       if (!pskb_may_pull(skb, sizeof(struct ipv6_esp_hdr))) {
+       if (!pskb_may_pull(skb, sizeof(*esph))) {
                ret = -EINVAL;
                goto out;
        }
@@ -163,32 +165,33 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
                goto out;
        }
 
+       if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       skb->ip_summed = CHECKSUM_NONE;
+
+       spin_lock(&x->lock);
+
        /* If integrity check is required, do this. */
        if (esp->auth.icv_full_len) {
                u8 sum[alen];
 
                ret = esp_mac_digest(esp, skb, 0, skb->len - alen);
                if (ret)
-                       goto out;
+                       goto unlock;
 
                if (skb_copy_bits(skb, skb->len - alen, sum, alen))
                        BUG();
 
                if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
-                       x->stats.integrity_failed++;
-                       ret = -EINVAL;
-                       goto out;
+                       ret = -EBADMSG;
+                       goto unlock;
                }
        }
 
-       if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
-               ret = -EINVAL;
-               goto out;
-       }
-
-       skb->ip_summed = CHECKSUM_NONE;
-
-       esph = (struct ipv6_esp_hdr*)skb->data;
+       esph = (struct ip_esp_hdr *)skb->data;
        iph = ipv6_hdr(skb);
 
        /* Get ivec. This can be wrong, check against another impls. */
@@ -196,23 +199,33 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
                crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
 
        {
-               u8 nexthdr[2];
                struct scatterlist *sg = &esp->sgbuf[0];
-               u8 padlen;
 
                if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
                        sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
                        if (!sg) {
                                ret = -ENOMEM;
-                               goto out;
+                               goto unlock;
                        }
                }
-               skb_to_sgvec(skb, sg, sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen, elen);
+               sg_init_table(sg, nfrags);
+               skb_to_sgvec(skb, sg,
+                            sizeof(*esph) + esp->conf.ivlen,
+                            elen);
                ret = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
                if (unlikely(sg != &esp->sgbuf[0]))
                        kfree(sg);
-               if (unlikely(ret))
-                       goto out;
+       }
+
+unlock:
+       spin_unlock(&x->lock);
+
+       if (unlikely(ret))
+               goto out;
+
+       {
+               u8 nexthdr[2];
+               u8 padlen;
 
                if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
                        BUG();
@@ -225,6 +238,12 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
                }
                /* ... check padding bits here. Silly. :-) */
 
+               /* RFC4303: Drop dummy packets without any error */
+               if (nexthdr[1] == IPPROTO_NONE) {
+                       ret = -EINVAL;
+                       goto out;
+               }
+
                pskb_trim(skb, skb->len - alen - padlen - 2);
                ret = nexthdr[1];
        }
@@ -259,7 +278,7 @@ static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
                     int type, int code, int offset, __be32 info)
 {
        struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
-       struct ipv6_esp_hdr *esph = (struct ipv6_esp_hdr*)(skb->data+offset);
+       struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
        struct xfrm_state *x;
 
        if (type != ICMPV6_DEST_UNREACH &&
@@ -355,9 +374,17 @@ static int esp6_init_state(struct xfrm_state *x)
        if (crypto_blkcipher_setkey(tfm, x->ealg->alg_key,
                                    (x->ealg->alg_key_len + 7) / 8))
                goto error;
-       x->props.header_len = sizeof(struct ipv6_esp_hdr) + esp->conf.ivlen;
-       if (x->props.mode == XFRM_MODE_TUNNEL)
+       x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
+       switch (x->props.mode) {
+       case XFRM_MODE_BEET:
+       case XFRM_MODE_TRANSPORT:
+               break;
+       case XFRM_MODE_TUNNEL:
                x->props.header_len += sizeof(struct ipv6hdr);
+               break;
+       default:
+               goto error;
+       }
        x->data = esp;
        return 0;