tunnels: fix netns vs proto registration ordering
[safe/jmp/linux-2.6] / net / ipv6 / ah6.c
index 60954fc..c2f300c 100644 (file)
 /*
  * 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.
  */
 
+#include <crypto/hash.h>
 #include <linux/module.h>
 #include <net/ip.h>
 #include <net/ah.h>
 #include <linux/crypto.h>
 #include <linux/pfkeyv2.h>
 #include <linux/string.h>
+#include <linux/scatterlist.h>
 #include <net/icmp.h>
 #include <net/ipv6.h>
 #include <net/protocol.h>
 #include <net/xfrm.h>
-#include <asm/scatterlist.h>
+
+#define IPV6HDR_BASELEN 8
+
+struct tmp_ext {
+#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
+               struct in6_addr saddr;
+#endif
+               struct in6_addr daddr;
+               char hdrs[0];
+};
+
+struct ah_skb_cb {
+       struct xfrm_skb_cb xfrm;
+       void *tmp;
+};
+
+#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
+
+static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
+                         unsigned int size)
+{
+       unsigned int len;
+
+       len = size + crypto_ahash_digestsize(ahash) +
+             (crypto_ahash_alignmask(ahash) &
+              ~(crypto_tfm_ctx_alignment() - 1));
+
+       len = ALIGN(len, crypto_tfm_ctx_alignment());
+
+       len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
+       len = ALIGN(len, __alignof__(struct scatterlist));
+
+       len += sizeof(struct scatterlist) * nfrags;
+
+       return kmalloc(len, GFP_ATOMIC);
+}
+
+static inline struct tmp_ext *ah_tmp_ext(void *base)
+{
+       return base + IPV6HDR_BASELEN;
+}
+
+static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
+{
+       return tmp + offset;
+}
+
+static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
+                            unsigned int offset)
+{
+       return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
+}
+
+static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
+                                              u8 *icv)
+{
+       struct ahash_request *req;
+
+       req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
+                               crypto_tfm_ctx_alignment());
+
+       ahash_request_set_tfm(req, ahash);
+
+       return req;
+}
+
+static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
+                                            struct ahash_request *req)
+{
+       return (void *)ALIGN((unsigned long)(req + 1) +
+                            crypto_ahash_reqsize(ahash),
+                            __alignof__(struct scatterlist));
+}
 
 static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
 {
@@ -54,7 +128,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 +148,68 @@ bad:
        return 0;
 }
 
+#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
+/**
+ *     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;
+}
+#else
+static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
+#endif
+
 /**
  *     ipv6_rearrange_rthdr - rearrange IPv6 routing header
  *     @iph: IPv6 header
@@ -92,7 +228,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 +249,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 +264,10 @@ static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len)
 
        while (exthdr.raw < end) {
                switch (nexthdr) {
-               case NEXTHDR_HOP:
                case NEXTHDR_DEST:
+                       if (dir == XFRM_POLICY_OUT)
+                               ipv6_rearrange_destopt(iph, exthdr.opth);
+               case NEXTHDR_HOP:
                        if (!zero_out_mutable_opts(exthdr.opth)) {
                                LIMIT_NETDEBUG(
                                        KERN_WARNING "overrun %sopts\n",
@@ -154,49 +292,110 @@ static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len)
        return 0;
 }
 
+static void ah6_output_done(struct crypto_async_request *base, int err)
+{
+       int extlen;
+       u8 *iph_base;
+       u8 *icv;
+       struct sk_buff *skb = base->data;
+       struct xfrm_state *x = skb_dst(skb)->xfrm;
+       struct ah_data *ahp = x->data;
+       struct ipv6hdr *top_iph = ipv6_hdr(skb);
+       struct ip_auth_hdr *ah = ip_auth_hdr(skb);
+       struct tmp_ext *iph_ext;
+
+       extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
+       if (extlen)
+               extlen += sizeof(*iph_ext);
+
+       iph_base = AH_SKB_CB(skb)->tmp;
+       iph_ext = ah_tmp_ext(iph_base);
+       icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
+
+       memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
+       memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
+
+       if (extlen) {
+#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
+               memcpy(&top_iph->saddr, iph_ext, extlen);
+#else
+               memcpy(&top_iph->daddr, iph_ext, extlen);
+#endif
+       }
+
+       err = ah->nexthdr;
+
+       kfree(AH_SKB_CB(skb)->tmp);
+       xfrm_output_resume(skb, err);
+}
+
 static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
 {
        int err;
+       int nfrags;
        int extlen;
+       u8 *iph_base;
+       u8 *icv;
+       u8 nexthdr;
+       struct sk_buff *trailer;
+       struct crypto_ahash *ahash;
+       struct ahash_request *req;
+       struct scatterlist *sg;
        struct ipv6hdr *top_iph;
        struct ip_auth_hdr *ah;
        struct ah_data *ahp;
-       u8 nexthdr;
-       char tmp_base[8];
-       struct {
-               struct in6_addr daddr;
-               char hdrs[0];
-       } *tmp_ext;
+       struct tmp_ext *iph_ext;
+
+       ahp = x->data;
+       ahash = ahp->ahash;
+
+       if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
+               goto out;
+       nfrags = err;
+
+       skb_push(skb, -skb_network_offset(skb));
+       extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
+       if (extlen)
+               extlen += sizeof(*iph_ext);
+
+       err = -ENOMEM;
+       iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen);
+       if (!iph_base)
+               goto out;
+
+       iph_ext = ah_tmp_ext(iph_base);
+       icv = ah_tmp_icv(ahash, iph_ext, extlen);
+       req = ah_tmp_req(ahash, icv);
+       sg = ah_req_sg(ahash, req);
+
+       ah = ip_auth_hdr(skb);
+       memset(ah->auth_data, 0, ahp->icv_trunc_len);
 
-       top_iph = (struct ipv6hdr *)skb->data;
+       top_iph = ipv6_hdr(skb);
        top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
 
-       nexthdr = *skb->nh.raw;
-       *skb->nh.raw = IPPROTO_AH;
+       nexthdr = *skb_mac_header(skb);
+       *skb_mac_header(skb) = IPPROTO_AH;
 
        /* When there are no extension headers, we only need to save the first
         * 8 bytes of the base IP header.
         */
-       memcpy(tmp_base, top_iph, sizeof(tmp_base));
+       memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
 
-       tmp_ext = NULL;
-       extlen = skb->h.raw - (unsigned char *)(top_iph + 1);
        if (extlen) {
-               extlen += sizeof(*tmp_ext);
-               tmp_ext = kmalloc(extlen, GFP_ATOMIC);
-               if (!tmp_ext) {
-                       err = -ENOMEM;
-                       goto error;
-               }
-               memcpy(tmp_ext, &top_iph->daddr, extlen);
+#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
+               memcpy(iph_ext, &top_iph->saddr, extlen);
+#else
+               memcpy(iph_ext, &top_iph->daddr, extlen);
+#endif
                err = ipv6_clear_mutable_options(top_iph,
-                                                extlen - sizeof(*tmp_ext) +
-                                                sizeof(*top_iph));
+                                                extlen - sizeof(*iph_ext) +
+                                                sizeof(*top_iph),
+                                                XFRM_POLICY_OUT);
                if (err)
-                       goto error_free_iph;
+                       goto out_free;
        }
 
-       ah = (struct ip_auth_hdr *)skb->h.raw;
        ah->nexthdr = nexthdr;
 
        top_iph->priority    = 0;
@@ -205,32 +404,80 @@ static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
        top_iph->flow_lbl[2] = 0;
        top_iph->hop_limit   = 0;
 
-       ahp = x->data;
-       ah->hdrlen  = (XFRM_ALIGN8(sizeof(struct ipv6_auth_hdr) + 
-                                  ahp->icv_trunc_len) >> 2) - 2;
+       ah->hdrlen  = (XFRM_ALIGN8(sizeof(*ah) + 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);
-       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);
+       ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
+
+       sg_init_table(sg, nfrags);
+       skb_to_sgvec(skb, sg, 0, skb->len);
+
+       ahash_request_set_crypt(req, sg, icv, skb->len);
+       ahash_request_set_callback(req, 0, ah6_output_done, skb);
 
-       err = 0;
+       AH_SKB_CB(skb)->tmp = iph_base;
 
-       memcpy(top_iph, tmp_base, sizeof(tmp_base));
-       if (tmp_ext) {
-               memcpy(&top_iph->daddr, tmp_ext, extlen);
-error_free_iph:
-               kfree(tmp_ext);
+       err = crypto_ahash_digest(req);
+       if (err) {
+               if (err == -EINPROGRESS)
+                       goto out;
+
+               if (err == -EBUSY)
+                       err = NET_XMIT_DROP;
+               goto out_free;
        }
 
-error:
+       memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
+       memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
+
+       if (extlen) {
+#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
+               memcpy(&top_iph->saddr, iph_ext, extlen);
+#else
+               memcpy(&top_iph->daddr, iph_ext, extlen);
+#endif
+       }
+
+out_free:
+       kfree(iph_base);
+out:
        return err;
 }
 
+static void ah6_input_done(struct crypto_async_request *base, int err)
+{
+       u8 *auth_data;
+       u8 *icv;
+       u8 *work_iph;
+       struct sk_buff *skb = base->data;
+       struct xfrm_state *x = xfrm_input_state(skb);
+       struct ah_data *ahp = x->data;
+       struct ip_auth_hdr *ah = ip_auth_hdr(skb);
+       int hdr_len = skb_network_header_len(skb);
+       int ah_hlen = (ah->hdrlen + 2) << 2;
+
+       work_iph = AH_SKB_CB(skb)->tmp;
+       auth_data = ah_tmp_auth(work_iph, hdr_len);
+       icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
+
+       err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
+       if (err)
+               goto out;
+
+       skb->network_header += ah_hlen;
+       memcpy(skb_network_header(skb), work_iph, hdr_len);
+       __skb_pull(skb, ah_hlen + hdr_len);
+       skb_set_transport_header(skb, -hdr_len);
+
+       err = ah->nexthdr;
+out:
+       kfree(AH_SKB_CB(skb)->tmp);
+       xfrm_input_resume(skb, err);
+}
+
+
+
 static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
 {
        /*
@@ -240,21 +487,29 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
         *
         * To erase AH:
         * Keeping copy of cleared headers. After AH processing,
-        * 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
+        * Moving the pointer of skb->network_header 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.
         */
 
-       struct ipv6_auth_hdr *ah;
+       u8 *auth_data;
+       u8 *icv;
+       u8 *work_iph;
+       struct sk_buff *trailer;
+       struct crypto_ahash *ahash;
+       struct ahash_request *req;
+       struct scatterlist *sg;
+       struct ip_auth_hdr *ah;
+       struct ipv6hdr *ip6h;
        struct ah_data *ahp;
-       unsigned char *tmp_hdr = NULL;
        u16 hdr_len;
        u16 ah_hlen;
        int nexthdr;
-       int err = -EINVAL;
+       int nfrags;
+       int err = -ENOMEM;
 
        if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
                goto out;
@@ -265,64 +520,92 @@ static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
            pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
                goto out;
 
-       hdr_len = skb->data - skb->nh.raw;
-       ah = (struct ipv6_auth_hdr*)skb->data;
+       skb->ip_summed = CHECKSUM_NONE;
+
+       hdr_len = skb_network_header_len(skb);
+       ah = (struct ip_auth_hdr *)skb->data;
        ahp = x->data;
+       ahash = ahp->ahash;
+
        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(*ah) + ahp->icv_full_len) &&
+           ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
+               goto out;
 
        if (!pskb_may_pull(skb, ah_hlen))
                goto out;
 
-       tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
-       if (!tmp_hdr)
+       ip6h = ipv6_hdr(skb);
+
+       skb_push(skb, hdr_len);
+
+       if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
                goto out;
-       memcpy(tmp_hdr, skb->nh.raw, hdr_len);
-       if (ipv6_clear_mutable_options(skb->nh.ipv6h, hdr_len))
-               goto free_out;
-       skb->nh.ipv6h->priority    = 0;
-       skb->nh.ipv6h->flow_lbl[0] = 0;
-       skb->nh.ipv6h->flow_lbl[1] = 0;
-       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);
-               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;
-               }
+       nfrags = err;
+
+       work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len);
+       if (!work_iph)
+               goto out;
+
+       auth_data = ah_tmp_auth(work_iph, hdr_len);
+       icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
+       req = ah_tmp_req(ahash, icv);
+       sg = ah_req_sg(ahash, req);
+
+       memcpy(work_iph, ip6h, hdr_len);
+       memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
+       memset(ah->auth_data, 0, ahp->icv_trunc_len);
+
+       if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
+               goto out_free;
+
+       ip6h->priority    = 0;
+       ip6h->flow_lbl[0] = 0;
+       ip6h->flow_lbl[1] = 0;
+       ip6h->flow_lbl[2] = 0;
+       ip6h->hop_limit   = 0;
+
+       sg_init_table(sg, nfrags);
+       skb_to_sgvec(skb, sg, 0, skb->len);
+
+       ahash_request_set_crypt(req, sg, icv, skb->len);
+       ahash_request_set_callback(req, 0, ah6_input_done, skb);
+
+       AH_SKB_CB(skb)->tmp = work_iph;
+
+       err = crypto_ahash_digest(req);
+       if (err) {
+               if (err == -EINPROGRESS)
+                       goto out;
+
+               if (err == -EBUSY)
+                       err = NET_XMIT_DROP;
+               goto out_free;
        }
 
-       skb->h.raw = memcpy(skb->nh.raw += ah_hlen, tmp_hdr, hdr_len);
-       __skb_pull(skb, ah_hlen + hdr_len);
+       err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
+       if (err)
+               goto out_free;
 
-       kfree(tmp_hdr);
+       skb->network_header += ah_hlen;
+       memcpy(skb_network_header(skb), work_iph, hdr_len);
+       skb->transport_header = skb->network_header;
+       __skb_pull(skb, ah_hlen + hdr_len);
 
-       return nexthdr;
+       err = nexthdr;
 
-free_out:
-       kfree(tmp_hdr);
+out_free:
+       kfree(work_iph);
 out:
        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,
+                   u8 type, u8 code, int offset, __be32 info)
 {
+       struct net *net = dev_net(skb->dev);
        struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
        struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
        struct xfrm_state *x;
@@ -331,12 +614,12 @@ static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
            type != ICMPV6_PKT_TOOBIG)
                return;
 
-       x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
+       x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
        if (!x)
                return;
 
-       NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/" NIP6_FMT "\n",
-                ntohl(ah->spi), NIP6(iph->daddr));
+       NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n",
+                ntohl(ah->spi), &iph->daddr);
 
        xfrm_state_put(x);
 }
@@ -345,15 +628,11 @@ static int ah6_init_state(struct xfrm_state *x)
 {
        struct ah_data *ahp = NULL;
        struct xfrm_algo_desc *aalg_desc;
-       struct crypto_hash *tfm;
+       struct crypto_ahash *ahash;
 
        if (!x->aalg)
                goto error;
 
-       /* null auth can use a zero length key */
-       if (x->aalg->alg_key_len > 512)
-               goto error;
-
        if (x->encap)
                goto error;
 
@@ -361,16 +640,15 @@ static int ah6_init_state(struct xfrm_state *x)
        if (ahp == NULL)
                return -ENOMEM;
 
-       ahp->key = x->aalg->alg_key;
-       ahp->key_len = (x->aalg->alg_key_len+7)/8;
-       tfm = crypto_alloc_hash(x->aalg->alg_name, 0, CRYPTO_ALG_ASYNC);
-       if (IS_ERR(tfm))
+       ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
+       if (IS_ERR(ahash))
                goto error;
 
-       ahp->tfm = tfm;
-       if (crypto_hash_setkey(tfm, ahp->key, ahp->key_len))
+       ahp->ahash = ahash;
+       if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
+                              (x->aalg->alg_key_len + 7) / 8))
                goto error;
-       
+
        /*
         * Lookup the algorithm description maintained by xfrm_algo,
         * verify crypto transform properties, and store information
@@ -381,33 +659,37 @@ static int ah6_init_state(struct xfrm_state *x)
        BUG_ON(!aalg_desc);
 
        if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
-           crypto_hash_digestsize(tfm)) {
+           crypto_ahash_digestsize(ahash)) {
                printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
-                      x->aalg->alg_name, crypto_hash_digestsize(tfm),
+                      x->aalg->alg_name, crypto_ahash_digestsize(ahash),
                       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;
-       
+       ahp->icv_trunc_len = x->aalg->alg_trunc_len/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 == XFRM_MODE_TUNNEL)
+
+       x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
+                                         ahp->icv_trunc_len);
+       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 = ahp;
 
        return 0;
 
 error:
        if (ahp) {
-               kfree(ahp->work_icv);
-               crypto_free_hash(ahp->tfm);
+               crypto_free_ahash(ahp->ahash);
                kfree(ahp);
        }
        return -EINVAL;
@@ -420,25 +702,24 @@ static void ah6_destroy(struct xfrm_state *x)
        if (!ahp)
                return;
 
-       kfree(ahp->work_icv);
-       ahp->work_icv = NULL;
-       crypto_free_hash(ahp->tfm);
-       ahp->tfm = NULL;
+       crypto_free_ahash(ahp->ahash);
        kfree(ahp);
 }
 
-static struct xfrm_type ah6_type =
+static const struct xfrm_type ah6_type =
 {
        .description    = "AH6",
        .owner          = THIS_MODULE,
        .proto          = IPPROTO_AH,
+       .flags          = XFRM_TYPE_REPLAY_PROT,
        .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 = {
+static const struct inet6_protocol ah6_protocol = {
        .handler        =       xfrm6_rcv,
        .err_handler    =       ah6_err,
        .flags          =       INET6_PROTO_NOPOLICY,
@@ -474,3 +755,4 @@ module_init(ah6_init);
 module_exit(ah6_fini);
 
 MODULE_LICENSE("GPL");
+MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);