fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / ah4.c
index 0d62398..67a5509 100644 (file)
@@ -1,12 +1,12 @@
-#include <linux/config.h>
+#include <linux/err.h>
 #include <linux/module.h>
-#include <net/inet_ecn.h>
 #include <net/ip.h>
 #include <net/xfrm.h>
 #include <net/ah.h>
 #include <linux/crypto.h>
 #include <linux/pfkeyv2.h>
 #include <net/icmp.h>
+#include <net/protocol.h>
 #include <asm/scatterlist.h>
 
 
@@ -14,7 +14,7 @@
  * into IP header for icv calculation. Options are already checked
  * for validity, so paranoia is not required. */
 
-static int ip_clear_mutable_options(struct iphdr *iph, u32 *daddr)
+static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
 {
        unsigned char * optptr = (unsigned char*)(iph+1);
        int  l = iph->ihl*4 - sizeof(struct iphdr);
@@ -35,7 +35,7 @@ static int ip_clear_mutable_options(struct iphdr *iph, u32 *daddr)
                switch (*optptr) {
                case IPOPT_SEC:
                case 0x85:      /* Some "Extended Security" crap. */
-               case 0x86:      /* Another "Commercial Security" crap. */
+               case IPOPT_CIPSO:
                case IPOPT_RA:
                case 0x80|21:   /* RFC1770 */
                        break;
@@ -54,11 +54,9 @@ static int ip_clear_mutable_options(struct iphdr *iph, u32 *daddr)
        return 0;
 }
 
-static int ah_output(struct sk_buff **pskb)
+static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
 {
        int err;
-       struct dst_entry *dst = (*pskb)->dst;
-       struct xfrm_state *x  = dst->xfrm;
        struct iphdr *iph, *top_iph;
        struct ip_auth_hdr *ah;
        struct ah_data *ahp;
@@ -67,53 +65,31 @@ static int ah_output(struct sk_buff **pskb)
                char            buf[60];
        } tmp_iph;
 
-       if ((*pskb)->ip_summed == CHECKSUM_HW) {
-               err = skb_checksum_help(pskb, 0);
+       top_iph = skb->nh.iph;
+       iph = &tmp_iph.iph;
+
+       iph->tos = top_iph->tos;
+       iph->ttl = top_iph->ttl;
+       iph->frag_off = top_iph->frag_off;
+
+       if (top_iph->ihl != 5) {
+               iph->daddr = top_iph->daddr;
+               memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
+               err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
                if (err)
-                       goto error_nolock;
+                       goto error;
        }
 
-       spin_lock_bh(&x->lock);
-       err = xfrm_check_output(x, *pskb, AF_INET);
-       if (err)
-               goto error;
+       ah = (struct ip_auth_hdr *)((char *)top_iph+top_iph->ihl*4);
+       ah->nexthdr = top_iph->protocol;
+
+       top_iph->tos = 0;
+       top_iph->tot_len = htons(skb->len);
+       top_iph->frag_off = 0;
+       top_iph->ttl = 0;
+       top_iph->protocol = IPPROTO_AH;
+       top_iph->check = 0;
 
-       iph = (*pskb)->nh.iph;
-       if (x->props.mode) {
-               top_iph = (struct iphdr*)skb_push(*pskb, x->props.header_len);
-               top_iph->ihl = 5;
-               top_iph->version = 4;
-               top_iph->tos = 0;
-               top_iph->tot_len = htons((*pskb)->len);
-               top_iph->frag_off = 0;
-               if (!(iph->frag_off&htons(IP_DF)))
-                       __ip_select_ident(top_iph, dst, 0);
-               top_iph->ttl = 0;
-               top_iph->protocol = IPPROTO_AH;
-               top_iph->check = 0;
-               top_iph->saddr = x->props.saddr.a4;
-               top_iph->daddr = x->id.daddr.a4;
-               ah = (struct ip_auth_hdr*)(top_iph+1);
-               ah->nexthdr = IPPROTO_IPIP;
-       } else {
-               memcpy(&tmp_iph, (*pskb)->data, iph->ihl*4);
-               top_iph = (struct iphdr*)skb_push(*pskb, x->props.header_len);
-               memcpy(top_iph, &tmp_iph, iph->ihl*4);
-               iph = &tmp_iph.iph;
-               top_iph->tos = 0;
-               top_iph->tot_len = htons((*pskb)->len);
-               top_iph->frag_off = 0;
-               top_iph->ttl = 0;
-               top_iph->protocol = IPPROTO_AH;
-               top_iph->check = 0;
-               if (top_iph->ihl != 5) {
-                       err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
-                       if (err)
-                               goto error;
-               }
-               ah = (struct ip_auth_hdr*)((char*)top_iph+iph->ihl*4);
-               ah->nexthdr = iph->protocol;
-       }
        ahp = x->data;
        ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + 
                                   ahp->icv_trunc_len) >> 2) - 2;
@@ -121,43 +97,33 @@ static int ah_output(struct sk_buff **pskb)
        ah->reserved = 0;
        ah->spi = x->id.spi;
        ah->seq_no = htonl(++x->replay.oseq);
-       ahp->icv(ahp, *pskb, ah->auth_data);
+       xfrm_aevent_doreplay(x);
+       err = ah_mac_digest(ahp, skb, ah->auth_data);
+       if (err)
+               goto error;
+       memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
+
        top_iph->tos = iph->tos;
        top_iph->ttl = iph->ttl;
-       if (x->props.mode) {
-               if (x->props.flags & XFRM_STATE_NOECN)
-                       IP_ECN_clear(top_iph);
-               top_iph->frag_off = iph->frag_off&~htons(IP_MF|IP_OFFSET);
-               memset(&(IPCB(*pskb)->opt), 0, sizeof(struct ip_options));
-       } else {
-               top_iph->frag_off = iph->frag_off;
+       top_iph->frag_off = iph->frag_off;
+       if (top_iph->ihl != 5) {
                top_iph->daddr = iph->daddr;
-               if (iph->ihl != 5)
-                       memcpy(top_iph+1, iph+1, iph->ihl*4 - sizeof(struct iphdr));
+               memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
        }
-       ip_send_check(top_iph);
 
-       (*pskb)->nh.raw = (*pskb)->data;
+       ip_send_check(top_iph);
 
-       x->curlft.bytes += (*pskb)->len;
-       x->curlft.packets++;
-       spin_unlock_bh(&x->lock);
-       if (((*pskb)->dst = dst_pop(dst)) == NULL) {
-               err = -EHOSTUNREACH;
-               goto error_nolock;
-       }
-       return NET_XMIT_BYPASS;
+       err = 0;
 
 error:
-       spin_unlock_bh(&x->lock);
-error_nolock:
-       kfree_skb(*pskb);
        return err;
 }
 
-int ah_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
+static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
 {
        int ah_hlen;
+       int ihl;
+       int err = -EINVAL;
        struct iphdr *iph;
        struct ip_auth_hdr *ah;
        struct ah_data *ahp;
@@ -188,14 +154,15 @@ int ah_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buf
        ah = (struct ip_auth_hdr*)skb->data;
        iph = skb->nh.iph;
 
-       memcpy(work_buf, iph, iph->ihl*4);
+       ihl = skb->data - skb->nh.raw;
+       memcpy(work_buf, iph, ihl);
 
        iph->ttl = 0;
        iph->tos = 0;
        iph->frag_off = 0;
        iph->check = 0;
-       if (iph->ihl != 5) {
-               u32 dummy;
+       if (ihl > sizeof(*iph)) {
+               __be32 dummy;
                if (ip_clear_mutable_options(iph, &dummy))
                        goto out;
        }
@@ -203,27 +170,27 @@ int ah_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buf
                u8 auth_data[MAX_AH_AUTH_LEN];
                
                memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
-               skb_push(skb, skb->data - skb->nh.raw);
-               ahp->icv(ahp, skb, ah->auth_data);
-               if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) {
+               skb_push(skb, ihl);
+               err = ah_mac_digest(ahp, skb, ah->auth_data);
+               if (err)
+                       goto out;
+               err = -EINVAL;
+               if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
                        x->stats.integrity_failed++;
                        goto out;
                }
        }
        ((struct iphdr*)work_buf)->protocol = ah->nexthdr;
-       skb->nh.raw = skb_pull(skb, ah_hlen);
-       memcpy(skb->nh.raw, work_buf, iph->ihl*4);
-       skb->nh.iph->tot_len = htons(skb->len);
-       skb_pull(skb, skb->nh.iph->ihl*4);
-       skb->h.raw = skb->data;
+       skb->h.raw = memcpy(skb->nh.raw += ah_hlen, work_buf, ihl);
+       __skb_pull(skb, ah_hlen + ihl);
 
        return 0;
 
 out:
-       return -EINVAL;
+       return err;
 }
 
-void ah4_err(struct sk_buff *skb, u32 info)
+static void ah4_err(struct sk_buff *skb, u32 info)
 {
        struct iphdr *iph = (struct iphdr*)skb->data;
        struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2));
@@ -241,10 +208,11 @@ void ah4_err(struct sk_buff *skb, u32 info)
        xfrm_state_put(x);
 }
 
-static int ah_init_state(struct xfrm_state *x, void *args)
+static int ah_init_state(struct xfrm_state *x)
 {
        struct ah_data *ahp = NULL;
        struct xfrm_algo_desc *aalg_desc;
+       struct crypto_hash *tfm;
 
        if (!x->aalg)
                goto error;
@@ -253,32 +221,36 @@ static int ah_init_state(struct xfrm_state *x, void *args)
        if (x->aalg->alg_key_len > 512)
                goto error;
 
-       ahp = kmalloc(sizeof(*ahp), GFP_KERNEL);
+       if (x->encap)
+               goto error;
+
+       ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
        if (ahp == NULL)
                return -ENOMEM;
 
-       memset(ahp, 0, sizeof(*ahp));
-
        ahp->key = x->aalg->alg_key;
        ahp->key_len = (x->aalg->alg_key_len+7)/8;
-       ahp->tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
-       if (!ahp->tfm)
+       tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
+       if (IS_ERR(tfm))
+               goto error;
+
+       ahp->tfm = tfm;
+       if (crypto_hash_setkey(tfm, ahp->key, ahp->key_len))
                goto error;
-       ahp->icv = ah_hmac_digest;
        
        /*
         * Lookup the algorithm description maintained by xfrm_algo,
         * verify crypto transform properties, and store information
         * we need for AH processing.  This lookup cannot fail here
-        * after a successful crypto_alloc_tfm().
+        * after a successful crypto_alloc_hash().
         */
-       aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
+       aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
        BUG_ON(!aalg_desc);
 
        if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
-           crypto_tfm_alg_digestsize(ahp->tfm)) {
+           crypto_hash_digestsize(tfm)) {
                printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
-                      x->aalg->alg_name, crypto_tfm_alg_digestsize(ahp->tfm),
+                      x->aalg->alg_name, crypto_hash_digestsize(tfm),
                       aalg_desc->uinfo.auth.icv_fullbits/8);
                goto error;
        }
@@ -293,7 +265,7 @@ static int ah_init_state(struct xfrm_state *x, void *args)
                goto error;
        
        x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + ahp->icv_trunc_len);
-       if (x->props.mode)
+       if (x->props.mode == XFRM_MODE_TUNNEL)
                x->props.header_len += sizeof(struct iphdr);
        x->data = ahp;
 
@@ -301,10 +273,8 @@ static int ah_init_state(struct xfrm_state *x, void *args)
 
 error:
        if (ahp) {
-               if (ahp->work_icv)
-                       kfree(ahp->work_icv);
-               if (ahp->tfm)
-                       crypto_free_tfm(ahp->tfm);
+               kfree(ahp->work_icv);
+               crypto_free_hash(ahp->tfm);
                kfree(ahp);
        }
        return -EINVAL;
@@ -317,14 +287,10 @@ static void ah_destroy(struct xfrm_state *x)
        if (!ahp)
                return;
 
-       if (ahp->work_icv) {
-               kfree(ahp->work_icv);
-               ahp->work_icv = NULL;
-       }
-       if (ahp->tfm) {
-               crypto_free_tfm(ahp->tfm);
-               ahp->tfm = NULL;
-       }
+       kfree(ahp->work_icv);
+       ahp->work_icv = NULL;
+       crypto_free_hash(ahp->tfm);
+       ahp->tfm = NULL;
        kfree(ahp);
 }
 
@@ -340,7 +306,7 @@ static struct xfrm_type ah_type =
        .output         = ah_output
 };
 
-static struct inet_protocol ah4_protocol = {
+static struct net_protocol ah4_protocol = {
        .handler        =       xfrm4_rcv,
        .err_handler    =       ah4_err,
        .no_policy      =       1,