Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C)2002 USAGI/WIDE Project
0004  *
0005  * Authors
0006  *
0007  *  Mitsuru KANDA @USAGI       : IPv6 Support
0008  *  Kazunori MIYAZAWA @USAGI   :
0009  *  Kunihiro Ishiguro <kunihiro@ipinfusion.com>
0010  *
0011  *  This file is derived from net/ipv4/ah.c.
0012  */
0013 
0014 #define pr_fmt(fmt) "IPv6: " fmt
0015 
0016 #include <crypto/algapi.h>
0017 #include <crypto/hash.h>
0018 #include <linux/module.h>
0019 #include <linux/slab.h>
0020 #include <net/ip.h>
0021 #include <net/ah.h>
0022 #include <linux/crypto.h>
0023 #include <linux/pfkeyv2.h>
0024 #include <linux/string.h>
0025 #include <linux/scatterlist.h>
0026 #include <net/ip6_route.h>
0027 #include <net/icmp.h>
0028 #include <net/ipv6.h>
0029 #include <net/protocol.h>
0030 #include <net/xfrm.h>
0031 
0032 #define IPV6HDR_BASELEN 8
0033 
0034 struct tmp_ext {
0035 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0036         struct in6_addr saddr;
0037 #endif
0038         struct in6_addr daddr;
0039         char hdrs[];
0040 };
0041 
0042 struct ah_skb_cb {
0043     struct xfrm_skb_cb xfrm;
0044     void *tmp;
0045 };
0046 
0047 #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
0048 
0049 static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
0050               unsigned int size)
0051 {
0052     unsigned int len;
0053 
0054     len = size + crypto_ahash_digestsize(ahash) +
0055           (crypto_ahash_alignmask(ahash) &
0056            ~(crypto_tfm_ctx_alignment() - 1));
0057 
0058     len = ALIGN(len, crypto_tfm_ctx_alignment());
0059 
0060     len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
0061     len = ALIGN(len, __alignof__(struct scatterlist));
0062 
0063     len += sizeof(struct scatterlist) * nfrags;
0064 
0065     return kmalloc(len, GFP_ATOMIC);
0066 }
0067 
0068 static inline struct tmp_ext *ah_tmp_ext(void *base)
0069 {
0070     return base + IPV6HDR_BASELEN;
0071 }
0072 
0073 static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
0074 {
0075     return tmp + offset;
0076 }
0077 
0078 static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
0079                  unsigned int offset)
0080 {
0081     return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
0082 }
0083 
0084 static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
0085                            u8 *icv)
0086 {
0087     struct ahash_request *req;
0088 
0089     req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
0090                 crypto_tfm_ctx_alignment());
0091 
0092     ahash_request_set_tfm(req, ahash);
0093 
0094     return req;
0095 }
0096 
0097 static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
0098                          struct ahash_request *req)
0099 {
0100     return (void *)ALIGN((unsigned long)(req + 1) +
0101                  crypto_ahash_reqsize(ahash),
0102                  __alignof__(struct scatterlist));
0103 }
0104 
0105 static bool zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
0106 {
0107     u8 *opt = (u8 *)opthdr;
0108     int len = ipv6_optlen(opthdr);
0109     int off = 0;
0110     int optlen = 0;
0111 
0112     off += 2;
0113     len -= 2;
0114 
0115     while (len > 0) {
0116 
0117         switch (opt[off]) {
0118 
0119         case IPV6_TLV_PAD1:
0120             optlen = 1;
0121             break;
0122         default:
0123             if (len < 2)
0124                 goto bad;
0125             optlen = opt[off+1]+2;
0126             if (len < optlen)
0127                 goto bad;
0128             if (opt[off] & 0x20)
0129                 memset(&opt[off+2], 0, opt[off+1]);
0130             break;
0131         }
0132 
0133         off += optlen;
0134         len -= optlen;
0135     }
0136     if (len == 0)
0137         return true;
0138 
0139 bad:
0140     return false;
0141 }
0142 
0143 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0144 /**
0145  *  ipv6_rearrange_destopt - rearrange IPv6 destination options header
0146  *  @iph: IPv6 header
0147  *  @destopt: destionation options header
0148  */
0149 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
0150 {
0151     u8 *opt = (u8 *)destopt;
0152     int len = ipv6_optlen(destopt);
0153     int off = 0;
0154     int optlen = 0;
0155 
0156     off += 2;
0157     len -= 2;
0158 
0159     while (len > 0) {
0160 
0161         switch (opt[off]) {
0162 
0163         case IPV6_TLV_PAD1:
0164             optlen = 1;
0165             break;
0166         default:
0167             if (len < 2)
0168                 goto bad;
0169             optlen = opt[off+1]+2;
0170             if (len < optlen)
0171                 goto bad;
0172 
0173             /* Rearrange the source address in @iph and the
0174              * addresses in home address option for final source.
0175              * See 11.3.2 of RFC 3775 for details.
0176              */
0177             if (opt[off] == IPV6_TLV_HAO) {
0178                 struct ipv6_destopt_hao *hao;
0179 
0180                 hao = (struct ipv6_destopt_hao *)&opt[off];
0181                 if (hao->length != sizeof(hao->addr)) {
0182                     net_warn_ratelimited("destopt hao: invalid header length: %u\n",
0183                                  hao->length);
0184                     goto bad;
0185                 }
0186                 swap(hao->addr, iph->saddr);
0187             }
0188             break;
0189         }
0190 
0191         off += optlen;
0192         len -= optlen;
0193     }
0194     /* Note: ok if len == 0 */
0195 bad:
0196     return;
0197 }
0198 #else
0199 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
0200 #endif
0201 
0202 /**
0203  *  ipv6_rearrange_rthdr - rearrange IPv6 routing header
0204  *  @iph: IPv6 header
0205  *  @rthdr: routing header
0206  *
0207  *  Rearrange the destination address in @iph and the addresses in @rthdr
0208  *  so that they appear in the order they will at the final destination.
0209  *  See Appendix A2 of RFC 2402 for details.
0210  */
0211 static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
0212 {
0213     int segments, segments_left;
0214     struct in6_addr *addrs;
0215     struct in6_addr final_addr;
0216 
0217     segments_left = rthdr->segments_left;
0218     if (segments_left == 0)
0219         return;
0220     rthdr->segments_left = 0;
0221 
0222     /* The value of rthdr->hdrlen has been verified either by the system
0223      * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
0224      * packets.  So we can assume that it is even and that segments is
0225      * greater than or equal to segments_left.
0226      *
0227      * For the same reason we can assume that this option is of type 0.
0228      */
0229     segments = rthdr->hdrlen >> 1;
0230 
0231     addrs = ((struct rt0_hdr *)rthdr)->addr;
0232     final_addr = addrs[segments - 1];
0233 
0234     addrs += segments - segments_left;
0235     memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
0236 
0237     addrs[0] = iph->daddr;
0238     iph->daddr = final_addr;
0239 }
0240 
0241 static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
0242 {
0243     union {
0244         struct ipv6hdr *iph;
0245         struct ipv6_opt_hdr *opth;
0246         struct ipv6_rt_hdr *rth;
0247         char *raw;
0248     } exthdr = { .iph = iph };
0249     char *end = exthdr.raw + len;
0250     int nexthdr = iph->nexthdr;
0251 
0252     exthdr.iph++;
0253 
0254     while (exthdr.raw < end) {
0255         switch (nexthdr) {
0256         case NEXTHDR_DEST:
0257             if (dir == XFRM_POLICY_OUT)
0258                 ipv6_rearrange_destopt(iph, exthdr.opth);
0259             fallthrough;
0260         case NEXTHDR_HOP:
0261             if (!zero_out_mutable_opts(exthdr.opth)) {
0262                 net_dbg_ratelimited("overrun %sopts\n",
0263                             nexthdr == NEXTHDR_HOP ?
0264                             "hop" : "dest");
0265                 return -EINVAL;
0266             }
0267             break;
0268 
0269         case NEXTHDR_ROUTING:
0270             ipv6_rearrange_rthdr(iph, exthdr.rth);
0271             break;
0272 
0273         default:
0274             return 0;
0275         }
0276 
0277         nexthdr = exthdr.opth->nexthdr;
0278         exthdr.raw += ipv6_optlen(exthdr.opth);
0279     }
0280 
0281     return 0;
0282 }
0283 
0284 static void ah6_output_done(struct crypto_async_request *base, int err)
0285 {
0286     int extlen;
0287     u8 *iph_base;
0288     u8 *icv;
0289     struct sk_buff *skb = base->data;
0290     struct xfrm_state *x = skb_dst(skb)->xfrm;
0291     struct ah_data *ahp = x->data;
0292     struct ipv6hdr *top_iph = ipv6_hdr(skb);
0293     struct ip_auth_hdr *ah = ip_auth_hdr(skb);
0294     struct tmp_ext *iph_ext;
0295 
0296     extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
0297     if (extlen)
0298         extlen += sizeof(*iph_ext);
0299 
0300     iph_base = AH_SKB_CB(skb)->tmp;
0301     iph_ext = ah_tmp_ext(iph_base);
0302     icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
0303 
0304     memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
0305     memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
0306 
0307     if (extlen) {
0308 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0309         memcpy(&top_iph->saddr, iph_ext, extlen);
0310 #else
0311         memcpy(&top_iph->daddr, iph_ext, extlen);
0312 #endif
0313     }
0314 
0315     kfree(AH_SKB_CB(skb)->tmp);
0316     xfrm_output_resume(skb->sk, skb, err);
0317 }
0318 
0319 static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
0320 {
0321     int err;
0322     int nfrags;
0323     int extlen;
0324     u8 *iph_base;
0325     u8 *icv;
0326     u8 nexthdr;
0327     struct sk_buff *trailer;
0328     struct crypto_ahash *ahash;
0329     struct ahash_request *req;
0330     struct scatterlist *sg;
0331     struct ipv6hdr *top_iph;
0332     struct ip_auth_hdr *ah;
0333     struct ah_data *ahp;
0334     struct tmp_ext *iph_ext;
0335     int seqhi_len = 0;
0336     __be32 *seqhi;
0337     int sglists = 0;
0338     struct scatterlist *seqhisg;
0339 
0340     ahp = x->data;
0341     ahash = ahp->ahash;
0342 
0343     err = skb_cow_data(skb, 0, &trailer);
0344     if (err < 0)
0345         goto out;
0346     nfrags = err;
0347 
0348     skb_push(skb, -skb_network_offset(skb));
0349     extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
0350     if (extlen)
0351         extlen += sizeof(*iph_ext);
0352 
0353     if (x->props.flags & XFRM_STATE_ESN) {
0354         sglists = 1;
0355         seqhi_len = sizeof(*seqhi);
0356     }
0357     err = -ENOMEM;
0358     iph_base = ah_alloc_tmp(ahash, nfrags + sglists, IPV6HDR_BASELEN +
0359                 extlen + seqhi_len);
0360     if (!iph_base)
0361         goto out;
0362 
0363     iph_ext = ah_tmp_ext(iph_base);
0364     seqhi = (__be32 *)((char *)iph_ext + extlen);
0365     icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
0366     req = ah_tmp_req(ahash, icv);
0367     sg = ah_req_sg(ahash, req);
0368     seqhisg = sg + nfrags;
0369 
0370     ah = ip_auth_hdr(skb);
0371     memset(ah->auth_data, 0, ahp->icv_trunc_len);
0372 
0373     top_iph = ipv6_hdr(skb);
0374     top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
0375 
0376     nexthdr = *skb_mac_header(skb);
0377     *skb_mac_header(skb) = IPPROTO_AH;
0378 
0379     /* When there are no extension headers, we only need to save the first
0380      * 8 bytes of the base IP header.
0381      */
0382     memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
0383 
0384     if (extlen) {
0385 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0386         memcpy(iph_ext, &top_iph->saddr, extlen);
0387 #else
0388         memcpy(iph_ext, &top_iph->daddr, extlen);
0389 #endif
0390         err = ipv6_clear_mutable_options(top_iph,
0391                          extlen - sizeof(*iph_ext) +
0392                          sizeof(*top_iph),
0393                          XFRM_POLICY_OUT);
0394         if (err)
0395             goto out_free;
0396     }
0397 
0398     ah->nexthdr = nexthdr;
0399 
0400     top_iph->priority    = 0;
0401     top_iph->flow_lbl[0] = 0;
0402     top_iph->flow_lbl[1] = 0;
0403     top_iph->flow_lbl[2] = 0;
0404     top_iph->hop_limit   = 0;
0405 
0406     ah->hdrlen  = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
0407 
0408     ah->reserved = 0;
0409     ah->spi = x->id.spi;
0410     ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
0411 
0412     sg_init_table(sg, nfrags + sglists);
0413     err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
0414     if (unlikely(err < 0))
0415         goto out_free;
0416 
0417     if (x->props.flags & XFRM_STATE_ESN) {
0418         /* Attach seqhi sg right after packet payload */
0419         *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
0420         sg_set_buf(seqhisg, seqhi, seqhi_len);
0421     }
0422     ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
0423     ahash_request_set_callback(req, 0, ah6_output_done, skb);
0424 
0425     AH_SKB_CB(skb)->tmp = iph_base;
0426 
0427     err = crypto_ahash_digest(req);
0428     if (err) {
0429         if (err == -EINPROGRESS)
0430             goto out;
0431 
0432         if (err == -ENOSPC)
0433             err = NET_XMIT_DROP;
0434         goto out_free;
0435     }
0436 
0437     memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
0438     memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
0439 
0440     if (extlen) {
0441 #if IS_ENABLED(CONFIG_IPV6_MIP6)
0442         memcpy(&top_iph->saddr, iph_ext, extlen);
0443 #else
0444         memcpy(&top_iph->daddr, iph_ext, extlen);
0445 #endif
0446     }
0447 
0448 out_free:
0449     kfree(iph_base);
0450 out:
0451     return err;
0452 }
0453 
0454 static void ah6_input_done(struct crypto_async_request *base, int err)
0455 {
0456     u8 *auth_data;
0457     u8 *icv;
0458     u8 *work_iph;
0459     struct sk_buff *skb = base->data;
0460     struct xfrm_state *x = xfrm_input_state(skb);
0461     struct ah_data *ahp = x->data;
0462     struct ip_auth_hdr *ah = ip_auth_hdr(skb);
0463     int hdr_len = skb_network_header_len(skb);
0464     int ah_hlen = ipv6_authlen(ah);
0465 
0466     if (err)
0467         goto out;
0468 
0469     work_iph = AH_SKB_CB(skb)->tmp;
0470     auth_data = ah_tmp_auth(work_iph, hdr_len);
0471     icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
0472 
0473     err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
0474     if (err)
0475         goto out;
0476 
0477     err = ah->nexthdr;
0478 
0479     skb->network_header += ah_hlen;
0480     memcpy(skb_network_header(skb), work_iph, hdr_len);
0481     __skb_pull(skb, ah_hlen + hdr_len);
0482     if (x->props.mode == XFRM_MODE_TUNNEL)
0483         skb_reset_transport_header(skb);
0484     else
0485         skb_set_transport_header(skb, -hdr_len);
0486 out:
0487     kfree(AH_SKB_CB(skb)->tmp);
0488     xfrm_input_resume(skb, err);
0489 }
0490 
0491 
0492 
0493 static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
0494 {
0495     /*
0496      * Before process AH
0497      * [IPv6][Ext1][Ext2][AH][Dest][Payload]
0498      * |<-------------->| hdr_len
0499      *
0500      * To erase AH:
0501      * Keeping copy of cleared headers. After AH processing,
0502      * Moving the pointer of skb->network_header by using skb_pull as long
0503      * as AH header length. Then copy back the copy as long as hdr_len
0504      * If destination header following AH exists, copy it into after [Ext2].
0505      *
0506      * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
0507      * There is offset of AH before IPv6 header after the process.
0508      */
0509 
0510     u8 *auth_data;
0511     u8 *icv;
0512     u8 *work_iph;
0513     struct sk_buff *trailer;
0514     struct crypto_ahash *ahash;
0515     struct ahash_request *req;
0516     struct scatterlist *sg;
0517     struct ip_auth_hdr *ah;
0518     struct ipv6hdr *ip6h;
0519     struct ah_data *ahp;
0520     u16 hdr_len;
0521     u16 ah_hlen;
0522     int nexthdr;
0523     int nfrags;
0524     int err = -ENOMEM;
0525     int seqhi_len = 0;
0526     __be32 *seqhi;
0527     int sglists = 0;
0528     struct scatterlist *seqhisg;
0529 
0530     if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
0531         goto out;
0532 
0533     /* We are going to _remove_ AH header to keep sockets happy,
0534      * so... Later this can change. */
0535     if (skb_unclone(skb, GFP_ATOMIC))
0536         goto out;
0537 
0538     skb->ip_summed = CHECKSUM_NONE;
0539 
0540     hdr_len = skb_network_header_len(skb);
0541     ah = (struct ip_auth_hdr *)skb->data;
0542     ahp = x->data;
0543     ahash = ahp->ahash;
0544 
0545     nexthdr = ah->nexthdr;
0546     ah_hlen = ipv6_authlen(ah);
0547 
0548     if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
0549         ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
0550         goto out;
0551 
0552     if (!pskb_may_pull(skb, ah_hlen))
0553         goto out;
0554 
0555     err = skb_cow_data(skb, 0, &trailer);
0556     if (err < 0)
0557         goto out;
0558     nfrags = err;
0559 
0560     ah = (struct ip_auth_hdr *)skb->data;
0561     ip6h = ipv6_hdr(skb);
0562 
0563     skb_push(skb, hdr_len);
0564 
0565     if (x->props.flags & XFRM_STATE_ESN) {
0566         sglists = 1;
0567         seqhi_len = sizeof(*seqhi);
0568     }
0569 
0570     work_iph = ah_alloc_tmp(ahash, nfrags + sglists, hdr_len +
0571                 ahp->icv_trunc_len + seqhi_len);
0572     if (!work_iph) {
0573         err = -ENOMEM;
0574         goto out;
0575     }
0576 
0577     auth_data = ah_tmp_auth((u8 *)work_iph, hdr_len);
0578     seqhi = (__be32 *)(auth_data + ahp->icv_trunc_len);
0579     icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
0580     req = ah_tmp_req(ahash, icv);
0581     sg = ah_req_sg(ahash, req);
0582     seqhisg = sg + nfrags;
0583 
0584     memcpy(work_iph, ip6h, hdr_len);
0585     memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
0586     memset(ah->auth_data, 0, ahp->icv_trunc_len);
0587 
0588     err = ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN);
0589     if (err)
0590         goto out_free;
0591 
0592     ip6h->priority    = 0;
0593     ip6h->flow_lbl[0] = 0;
0594     ip6h->flow_lbl[1] = 0;
0595     ip6h->flow_lbl[2] = 0;
0596     ip6h->hop_limit   = 0;
0597 
0598     sg_init_table(sg, nfrags + sglists);
0599     err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
0600     if (unlikely(err < 0))
0601         goto out_free;
0602 
0603     if (x->props.flags & XFRM_STATE_ESN) {
0604         /* Attach seqhi sg right after packet payload */
0605         *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
0606         sg_set_buf(seqhisg, seqhi, seqhi_len);
0607     }
0608 
0609     ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
0610     ahash_request_set_callback(req, 0, ah6_input_done, skb);
0611 
0612     AH_SKB_CB(skb)->tmp = work_iph;
0613 
0614     err = crypto_ahash_digest(req);
0615     if (err) {
0616         if (err == -EINPROGRESS)
0617             goto out;
0618 
0619         goto out_free;
0620     }
0621 
0622     err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
0623     if (err)
0624         goto out_free;
0625 
0626     skb->network_header += ah_hlen;
0627     memcpy(skb_network_header(skb), work_iph, hdr_len);
0628     __skb_pull(skb, ah_hlen + hdr_len);
0629 
0630     if (x->props.mode == XFRM_MODE_TUNNEL)
0631         skb_reset_transport_header(skb);
0632     else
0633         skb_set_transport_header(skb, -hdr_len);
0634 
0635     err = nexthdr;
0636 
0637 out_free:
0638     kfree(work_iph);
0639 out:
0640     return err;
0641 }
0642 
0643 static int ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
0644            u8 type, u8 code, int offset, __be32 info)
0645 {
0646     struct net *net = dev_net(skb->dev);
0647     struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
0648     struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+offset);
0649     struct xfrm_state *x;
0650 
0651     if (type != ICMPV6_PKT_TOOBIG &&
0652         type != NDISC_REDIRECT)
0653         return 0;
0654 
0655     x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
0656     if (!x)
0657         return 0;
0658 
0659     if (type == NDISC_REDIRECT)
0660         ip6_redirect(skb, net, skb->dev->ifindex, 0,
0661                  sock_net_uid(net, NULL));
0662     else
0663         ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
0664     xfrm_state_put(x);
0665 
0666     return 0;
0667 }
0668 
0669 static int ah6_init_state(struct xfrm_state *x)
0670 {
0671     struct ah_data *ahp = NULL;
0672     struct xfrm_algo_desc *aalg_desc;
0673     struct crypto_ahash *ahash;
0674 
0675     if (!x->aalg)
0676         goto error;
0677 
0678     if (x->encap)
0679         goto error;
0680 
0681     ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
0682     if (!ahp)
0683         return -ENOMEM;
0684 
0685     ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
0686     if (IS_ERR(ahash))
0687         goto error;
0688 
0689     ahp->ahash = ahash;
0690     if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
0691                    (x->aalg->alg_key_len + 7) / 8))
0692         goto error;
0693 
0694     /*
0695      * Lookup the algorithm description maintained by xfrm_algo,
0696      * verify crypto transform properties, and store information
0697      * we need for AH processing.  This lookup cannot fail here
0698      * after a successful crypto_alloc_hash().
0699      */
0700     aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
0701     BUG_ON(!aalg_desc);
0702 
0703     if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
0704         crypto_ahash_digestsize(ahash)) {
0705         pr_info("AH: %s digestsize %u != %u\n",
0706             x->aalg->alg_name, crypto_ahash_digestsize(ahash),
0707             aalg_desc->uinfo.auth.icv_fullbits/8);
0708         goto error;
0709     }
0710 
0711     ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
0712     ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
0713 
0714     x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
0715                       ahp->icv_trunc_len);
0716     switch (x->props.mode) {
0717     case XFRM_MODE_BEET:
0718     case XFRM_MODE_TRANSPORT:
0719         break;
0720     case XFRM_MODE_TUNNEL:
0721         x->props.header_len += sizeof(struct ipv6hdr);
0722         break;
0723     default:
0724         goto error;
0725     }
0726     x->data = ahp;
0727 
0728     return 0;
0729 
0730 error:
0731     if (ahp) {
0732         crypto_free_ahash(ahp->ahash);
0733         kfree(ahp);
0734     }
0735     return -EINVAL;
0736 }
0737 
0738 static void ah6_destroy(struct xfrm_state *x)
0739 {
0740     struct ah_data *ahp = x->data;
0741 
0742     if (!ahp)
0743         return;
0744 
0745     crypto_free_ahash(ahp->ahash);
0746     kfree(ahp);
0747 }
0748 
0749 static int ah6_rcv_cb(struct sk_buff *skb, int err)
0750 {
0751     return 0;
0752 }
0753 
0754 static const struct xfrm_type ah6_type = {
0755     .owner      = THIS_MODULE,
0756     .proto      = IPPROTO_AH,
0757     .flags      = XFRM_TYPE_REPLAY_PROT,
0758     .init_state = ah6_init_state,
0759     .destructor = ah6_destroy,
0760     .input      = ah6_input,
0761     .output     = ah6_output,
0762 };
0763 
0764 static struct xfrm6_protocol ah6_protocol = {
0765     .handler    =   xfrm6_rcv,
0766     .input_handler  =   xfrm_input,
0767     .cb_handler =   ah6_rcv_cb,
0768     .err_handler    =   ah6_err,
0769     .priority   =   0,
0770 };
0771 
0772 static int __init ah6_init(void)
0773 {
0774     if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
0775         pr_info("%s: can't add xfrm type\n", __func__);
0776         return -EAGAIN;
0777     }
0778 
0779     if (xfrm6_protocol_register(&ah6_protocol, IPPROTO_AH) < 0) {
0780         pr_info("%s: can't add protocol\n", __func__);
0781         xfrm_unregister_type(&ah6_type, AF_INET6);
0782         return -EAGAIN;
0783     }
0784 
0785     return 0;
0786 }
0787 
0788 static void __exit ah6_fini(void)
0789 {
0790     if (xfrm6_protocol_deregister(&ah6_protocol, IPPROTO_AH) < 0)
0791         pr_info("%s: can't remove protocol\n", __func__);
0792 
0793     xfrm_unregister_type(&ah6_type, AF_INET6);
0794 }
0795 
0796 module_init(ah6_init);
0797 module_exit(ah6_fini);
0798 
0799 MODULE_LICENSE("GPL");
0800 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);