Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-drm-fsl-dcu.git] / net / ipv6 / ah6.c
index 9d4831bd43355fd6d8ca1ff343fde85092b4de8d..dc68b7269c3c90de00a54aacc072ad7faaca567e 100644 (file)
@@ -1,26 +1,26 @@
 /*
  * Copyright (C)2002 USAGI/WIDE Project
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Authors
  *
- *     Mitsuru KANDA @USAGI       : IPv6 Support 
+ *     Mitsuru KANDA @USAGI       : IPv6 Support
  *     Kazunori MIYAZAWA @USAGI   :
  *     Kunihiro Ishiguro <kunihiro@ipinfusion.com>
- *     
+ *
  *     This file is derived from net/ipv4/ah.c.
  */
 
@@ -54,7 +54,7 @@ static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
                        optlen = 1;
                        break;
                default:
-                       if (len < 2) 
+                       if (len < 2)
                                goto bad;
                        optlen = opt[off+1]+2;
                        if (len < optlen)
@@ -74,6 +74,66 @@ bad:
        return 0;
 }
 
+#ifdef CONFIG_IPV6_MIP6
+/**
+ *     ipv6_rearrange_destopt - rearrange IPv6 destination options header
+ *     @iph: IPv6 header
+ *     @destopt: destionation options header
+ */
+static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
+{
+       u8 *opt = (u8 *)destopt;
+       int len = ipv6_optlen(destopt);
+       int off = 0;
+       int optlen = 0;
+
+       off += 2;
+       len -= 2;
+
+       while (len > 0) {
+
+               switch (opt[off]) {
+
+               case IPV6_TLV_PAD0:
+                       optlen = 1;
+                       break;
+               default:
+                       if (len < 2)
+                               goto bad;
+                       optlen = opt[off+1]+2;
+                       if (len < optlen)
+                               goto bad;
+
+                       /* Rearrange the source address in @iph and the
+                        * addresses in home address option for final source.
+                        * See 11.3.2 of RFC 3775 for details.
+                        */
+                       if (opt[off] == IPV6_TLV_HAO) {
+                               struct in6_addr final_addr;
+                               struct ipv6_destopt_hao *hao;
+
+                               hao = (struct ipv6_destopt_hao *)&opt[off];
+                               if (hao->length != sizeof(hao->addr)) {
+                                       if (net_ratelimit())
+                                               printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
+                                       goto bad;
+                               }
+                               ipv6_addr_copy(&final_addr, &hao->addr);
+                               ipv6_addr_copy(&hao->addr, &iph->saddr);
+                               ipv6_addr_copy(&iph->saddr, &final_addr);
+                       }
+                       break;
+               }
+
+               off += optlen;
+               len -= optlen;
+       }
+       /* Note: ok if len == 0 */
+bad:
+       return;
+}
+#endif
+
 /**
  *     ipv6_rearrange_rthdr - rearrange IPv6 routing header
  *     @iph: IPv6 header
@@ -92,7 +152,7 @@ static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
        segments_left = rthdr->segments_left;
        if (segments_left == 0)
                return;
-       rthdr->segments_left = 0; 
+       rthdr->segments_left = 0;
 
        /* The value of rthdr->hdrlen has been verified either by the system
         * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
@@ -113,7 +173,7 @@ static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
        ipv6_addr_copy(&iph->daddr, &final_addr);
 }
 
-static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len)
+static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
 {
        union {
                struct ipv6hdr *iph;
@@ -128,8 +188,12 @@ static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len)
 
        while (exthdr.raw < end) {
                switch (nexthdr) {
-               case NEXTHDR_HOP:
                case NEXTHDR_DEST:
+#ifdef CONFIG_IPV6_MIP6
+                       if (dir == XFRM_POLICY_OUT)
+                               ipv6_rearrange_destopt(iph, exthdr.opth);
+#endif
+               case NEXTHDR_HOP:
                        if (!zero_out_mutable_opts(exthdr.opth)) {
                                LIMIT_NETDEBUG(
                                        KERN_WARNING "overrun %sopts\n",
@@ -164,6 +228,9 @@ static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
        u8 nexthdr;
        char tmp_base[8];
        struct {
+#ifdef CONFIG_IPV6_MIP6
+               struct in6_addr saddr;
+#endif
                struct in6_addr daddr;
                char hdrs[0];
        } *tmp_ext;
@@ -188,10 +255,15 @@ static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
                        err = -ENOMEM;
                        goto error;
                }
+#ifdef CONFIG_IPV6_MIP6
+               memcpy(tmp_ext, &top_iph->saddr, extlen);
+#else
                memcpy(tmp_ext, &top_iph->daddr, extlen);
+#endif
                err = ipv6_clear_mutable_options(top_iph,
                                                 extlen - sizeof(*tmp_ext) +
-                                                sizeof(*top_iph));
+                                                sizeof(*top_iph),
+                                                XFRM_POLICY_OUT);
                if (err)
                        goto error_free_iph;
        }
@@ -206,20 +278,27 @@ static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
        top_iph->hop_limit   = 0;
 
        ahp = x->data;
-       ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + 
+       ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) +
                                   ahp->icv_trunc_len) >> 2) - 2;
 
        ah->reserved = 0;
        ah->spi = x->id.spi;
        ah->seq_no = htonl(++x->replay.oseq);
        xfrm_aevent_doreplay(x);
-       ahp->icv(ahp, skb, ah->auth_data);
+       err = ah_mac_digest(ahp, skb, ah->auth_data);
+       if (err)
+               goto error_free_iph;
+       memcpy(ah->auth_data, ahp->work_icv, ahp->icv_trunc_len);
 
        err = 0;
 
        memcpy(top_iph, tmp_base, sizeof(tmp_base));
        if (tmp_ext) {
+#ifdef CONFIG_IPV6_MIP6
+               memcpy(&top_iph->saddr, tmp_ext, extlen);
+#else
                memcpy(&top_iph->daddr, tmp_ext, extlen);
+#endif
 error_free_iph:
                kfree(tmp_ext);
        }
@@ -240,7 +319,7 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
         * Moving the pointer of skb->nh.raw by using skb_pull as long as AH
         * header length. Then copy back the copy as long as hdr_len
         * If destination header following AH exists, copy it into after [Ext2].
-        * 
+        *
         * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
         * There is offset of AH before IPv6 header after the process.
         */
@@ -251,6 +330,7 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
        u16 hdr_len;
        u16 ah_hlen;
        int nexthdr;
+       int err = -EINVAL;
 
        if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
                goto out;
@@ -267,18 +347,17 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
        nexthdr = ah->nexthdr;
        ah_hlen = (ah->hdrlen + 2) << 2;
 
-        if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) &&
-            ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len))
-                goto out;
+       if (ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_full_len) &&
+           ah_hlen != XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len))
+               goto out;
 
        if (!pskb_may_pull(skb, ah_hlen))
                goto out;
 
-       tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
+       tmp_hdr = kmemdup(skb->nh.raw, hdr_len, GFP_ATOMIC);
        if (!tmp_hdr)
                goto out;
-       memcpy(tmp_hdr, skb->nh.raw, hdr_len);
-       if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len))
+       if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len, XFRM_POLICY_IN))
                goto free_out;
        skb->nh.ipv6h->priority    = 0;
        skb->nh.ipv6h->flow_lbl[0] = 0;
@@ -286,14 +365,17 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
        skb->nh.ipv6h->flow_lbl[2] = 0;
        skb->nh.ipv6h->hop_limit   = 0;
 
-        {
+       {
                u8 auth_data[MAX_AH_AUTH_LEN];
 
                memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
                memset(ah->auth_data, 0, ahp->icv_trunc_len);
                skb_push(skb, hdr_len);
-               ahp->icv(ahp, skb, ah->auth_data);
-               if (memcmp(ah->auth_data, auth_data, ahp->icv_trunc_len)) {
+               err = ah_mac_digest(ahp, skb, ah->auth_data);
+               if (err)
+                       goto free_out;
+               err = -EINVAL;
+               if (memcmp(ahp->work_icv, auth_data, ahp->icv_trunc_len)) {
                        LIMIT_NETDEBUG(KERN_WARNING "ipsec ah authentication error\n");
                        x->stats.integrity_failed++;
                        goto free_out;
@@ -310,11 +392,11 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
 free_out:
        kfree(tmp_hdr);
 out:
-       return -EINVAL;
+       return err;
 }
 
-static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 
-                    int type, int code, int offset, __u32 info)
+static void ah6_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 ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
@@ -338,6 +420,7 @@ static int ah6_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;
@@ -355,39 +438,42 @@ static int ah6_init_state(struct xfrm_state *x)
 
        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->icv = ah_hmac_digest;
-       
+
+       ahp->tfm = tfm;
+       if (crypto_hash_setkey(tfm, ahp->key, ahp->key_len))
+               goto error;
+
        /*
         * 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, 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;
        }
-       
+
        ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
        ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
-       
+
        BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
-       
+
        ahp->work_icv = kmalloc(ahp->icv_full_len, GFP_KERNEL);
        if (!ahp->work_icv)
                goto error;
-       
+
        x->props.header_len = XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + ahp->icv_trunc_len);
-       if (x->props.mode)
+       if (x->props.mode == XFRM_MODE_TUNNEL)
                x->props.header_len += sizeof(struct ipv6hdr);
        x->data = ahp;
 
@@ -396,7 +482,7 @@ static int ah6_init_state(struct xfrm_state *x)
 error:
        if (ahp) {
                kfree(ahp->work_icv);
-               crypto_free_tfm(ahp->tfm);
+               crypto_free_hash(ahp->tfm);
                kfree(ahp);
        }
        return -EINVAL;
@@ -411,7 +497,7 @@ static void ah6_destroy(struct xfrm_state *x)
 
        kfree(ahp->work_icv);
        ahp->work_icv = NULL;
-       crypto_free_tfm(ahp->tfm);
+       crypto_free_hash(ahp->tfm);
        ahp->tfm = NULL;
        kfree(ahp);
 }
@@ -424,7 +510,8 @@ static struct xfrm_type ah6_type =
        .init_state     = ah6_init_state,
        .destructor     = ah6_destroy,
        .input          = ah6_input,
-       .output         = ah6_output
+       .output         = ah6_output,
+       .hdr_offset     = xfrm6_find_1stfragopt,
 };
 
 static struct inet6_protocol ah6_protocol = {