2 * Copyright (C)2002 USAGI/WIDE Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Mitsuru KANDA @USAGI : IPv6 Support
21 * Kazunori MIYAZAWA @USAGI :
24 * This file is derived from net/ipv4/ah.c.
27 #include <crypto/hash.h>
28 #include <linux/module.h>
31 #include <linux/crypto.h>
32 #include <linux/pfkeyv2.h>
33 #include <linux/string.h>
34 #include <linux/scatterlist.h>
37 #include <net/protocol.h>
40 #define IPV6HDR_BASELEN 8
43 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
44 struct in6_addr saddr;
46 struct in6_addr daddr;
51 struct xfrm_skb_cb xfrm;
55 #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
57 static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
62 len = size + crypto_ahash_digestsize(ahash) +
63 (crypto_ahash_alignmask(ahash) &
64 ~(crypto_tfm_ctx_alignment() - 1));
66 len = ALIGN(len, crypto_tfm_ctx_alignment());
68 len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
69 len = ALIGN(len, __alignof__(struct scatterlist));
71 len += sizeof(struct scatterlist) * nfrags;
73 return kmalloc(len, GFP_ATOMIC);
76 static inline struct tmp_ext *ah_tmp_ext(void *base)
78 return base + IPV6HDR_BASELEN;
81 static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
86 static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
89 return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
92 static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
95 struct ahash_request *req;
97 req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
98 crypto_tfm_ctx_alignment());
100 ahash_request_set_tfm(req, ahash);
105 static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
106 struct ahash_request *req)
108 return (void *)ALIGN((unsigned long)(req + 1) +
109 crypto_ahash_reqsize(ahash),
110 __alignof__(struct scatterlist));
113 static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
115 u8 *opt = (u8 *)opthdr;
116 int len = ipv6_optlen(opthdr);
133 optlen = opt[off+1]+2;
137 memset(&opt[off+2], 0, opt[off+1]);
151 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
153 * ipv6_rearrange_destopt - rearrange IPv6 destination options header
155 * @destopt: destionation options header
157 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
159 u8 *opt = (u8 *)destopt;
160 int len = ipv6_optlen(destopt);
177 optlen = opt[off+1]+2;
181 /* Rearrange the source address in @iph and the
182 * addresses in home address option for final source.
183 * See 11.3.2 of RFC 3775 for details.
185 if (opt[off] == IPV6_TLV_HAO) {
186 struct in6_addr final_addr;
187 struct ipv6_destopt_hao *hao;
189 hao = (struct ipv6_destopt_hao *)&opt[off];
190 if (hao->length != sizeof(hao->addr)) {
192 printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
195 ipv6_addr_copy(&final_addr, &hao->addr);
196 ipv6_addr_copy(&hao->addr, &iph->saddr);
197 ipv6_addr_copy(&iph->saddr, &final_addr);
205 /* Note: ok if len == 0 */
210 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
214 * ipv6_rearrange_rthdr - rearrange IPv6 routing header
216 * @rthdr: routing header
218 * Rearrange the destination address in @iph and the addresses in @rthdr
219 * so that they appear in the order they will at the final destination.
220 * See Appendix A2 of RFC 2402 for details.
222 static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
224 int segments, segments_left;
225 struct in6_addr *addrs;
226 struct in6_addr final_addr;
228 segments_left = rthdr->segments_left;
229 if (segments_left == 0)
231 rthdr->segments_left = 0;
233 /* The value of rthdr->hdrlen has been verified either by the system
234 * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
235 * packets. So we can assume that it is even and that segments is
236 * greater than or equal to segments_left.
238 * For the same reason we can assume that this option is of type 0.
240 segments = rthdr->hdrlen >> 1;
242 addrs = ((struct rt0_hdr *)rthdr)->addr;
243 ipv6_addr_copy(&final_addr, addrs + segments - 1);
245 addrs += segments - segments_left;
246 memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
248 ipv6_addr_copy(addrs, &iph->daddr);
249 ipv6_addr_copy(&iph->daddr, &final_addr);
252 static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
256 struct ipv6_opt_hdr *opth;
257 struct ipv6_rt_hdr *rth;
259 } exthdr = { .iph = iph };
260 char *end = exthdr.raw + len;
261 int nexthdr = iph->nexthdr;
265 while (exthdr.raw < end) {
268 if (dir == XFRM_POLICY_OUT)
269 ipv6_rearrange_destopt(iph, exthdr.opth);
271 if (!zero_out_mutable_opts(exthdr.opth)) {
273 KERN_WARNING "overrun %sopts\n",
274 nexthdr == NEXTHDR_HOP ?
280 case NEXTHDR_ROUTING:
281 ipv6_rearrange_rthdr(iph, exthdr.rth);
288 nexthdr = exthdr.opth->nexthdr;
289 exthdr.raw += ipv6_optlen(exthdr.opth);
295 static void ah6_output_done(struct crypto_async_request *base, int err)
300 struct sk_buff *skb = base->data;
301 struct xfrm_state *x = skb_dst(skb)->xfrm;
302 struct ah_data *ahp = x->data;
303 struct ipv6hdr *top_iph = ipv6_hdr(skb);
304 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
305 struct tmp_ext *iph_ext;
307 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
309 extlen += sizeof(*iph_ext);
311 iph_base = AH_SKB_CB(skb)->tmp;
312 iph_ext = ah_tmp_ext(iph_base);
313 icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
315 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
316 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
319 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
320 memcpy(&top_iph->saddr, iph_ext, extlen);
322 memcpy(&top_iph->daddr, iph_ext, extlen);
328 kfree(AH_SKB_CB(skb)->tmp);
329 xfrm_output_resume(skb, err);
332 static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
340 struct sk_buff *trailer;
341 struct crypto_ahash *ahash;
342 struct ahash_request *req;
343 struct scatterlist *sg;
344 struct ipv6hdr *top_iph;
345 struct ip_auth_hdr *ah;
347 struct tmp_ext *iph_ext;
352 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
356 skb_push(skb, -skb_network_offset(skb));
357 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
359 extlen += sizeof(*iph_ext);
362 iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen);
366 iph_ext = ah_tmp_ext(iph_base);
367 icv = ah_tmp_icv(ahash, iph_ext, extlen);
368 req = ah_tmp_req(ahash, icv);
369 sg = ah_req_sg(ahash, req);
371 ah = ip_auth_hdr(skb);
372 memset(ah->auth_data, 0, ahp->icv_trunc_len);
374 top_iph = ipv6_hdr(skb);
375 top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
377 nexthdr = *skb_mac_header(skb);
378 *skb_mac_header(skb) = IPPROTO_AH;
380 /* When there are no extension headers, we only need to save the first
381 * 8 bytes of the base IP header.
383 memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
386 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
387 memcpy(iph_ext, &top_iph->saddr, extlen);
389 memcpy(iph_ext, &top_iph->daddr, extlen);
391 err = ipv6_clear_mutable_options(top_iph,
392 extlen - sizeof(*iph_ext) +
399 ah->nexthdr = nexthdr;
401 top_iph->priority = 0;
402 top_iph->flow_lbl[0] = 0;
403 top_iph->flow_lbl[1] = 0;
404 top_iph->flow_lbl[2] = 0;
405 top_iph->hop_limit = 0;
407 ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
411 ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
413 sg_init_table(sg, nfrags);
414 skb_to_sgvec(skb, sg, 0, skb->len);
416 ahash_request_set_crypt(req, sg, icv, skb->len);
417 ahash_request_set_callback(req, 0, ah6_output_done, skb);
419 AH_SKB_CB(skb)->tmp = iph_base;
421 err = crypto_ahash_digest(req);
423 if (err == -EINPROGRESS)
431 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
432 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
435 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
436 memcpy(&top_iph->saddr, iph_ext, extlen);
438 memcpy(&top_iph->daddr, iph_ext, extlen);
448 static void ah6_input_done(struct crypto_async_request *base, int err)
453 struct sk_buff *skb = base->data;
454 struct xfrm_state *x = xfrm_input_state(skb);
455 struct ah_data *ahp = x->data;
456 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
457 int hdr_len = skb_network_header_len(skb);
458 int ah_hlen = (ah->hdrlen + 2) << 2;
460 work_iph = AH_SKB_CB(skb)->tmp;
461 auth_data = ah_tmp_auth(work_iph, hdr_len);
462 icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
464 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
468 skb->network_header += ah_hlen;
469 memcpy(skb_network_header(skb), work_iph, hdr_len);
470 __skb_pull(skb, ah_hlen + hdr_len);
471 skb_set_transport_header(skb, -hdr_len);
475 kfree(AH_SKB_CB(skb)->tmp);
476 xfrm_input_resume(skb, err);
481 static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
485 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
486 * |<-------------->| hdr_len
489 * Keeping copy of cleared headers. After AH processing,
490 * Moving the pointer of skb->network_header by using skb_pull as long
491 * as AH header length. Then copy back the copy as long as hdr_len
492 * If destination header following AH exists, copy it into after [Ext2].
494 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
495 * There is offset of AH before IPv6 header after the process.
501 struct sk_buff *trailer;
502 struct crypto_ahash *ahash;
503 struct ahash_request *req;
504 struct scatterlist *sg;
505 struct ip_auth_hdr *ah;
506 struct ipv6hdr *ip6h;
514 if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
517 /* We are going to _remove_ AH header to keep sockets happy,
518 * so... Later this can change. */
519 if (skb_cloned(skb) &&
520 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
523 skb->ip_summed = CHECKSUM_NONE;
525 hdr_len = skb_network_header_len(skb);
526 ah = (struct ip_auth_hdr *)skb->data;
530 nexthdr = ah->nexthdr;
531 ah_hlen = (ah->hdrlen + 2) << 2;
533 if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
534 ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
537 if (!pskb_may_pull(skb, ah_hlen))
540 ip6h = ipv6_hdr(skb);
542 skb_push(skb, hdr_len);
544 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
548 work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len);
552 auth_data = ah_tmp_auth(work_iph, hdr_len);
553 icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
554 req = ah_tmp_req(ahash, icv);
555 sg = ah_req_sg(ahash, req);
557 memcpy(work_iph, ip6h, hdr_len);
558 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
559 memset(ah->auth_data, 0, ahp->icv_trunc_len);
561 if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
565 ip6h->flow_lbl[0] = 0;
566 ip6h->flow_lbl[1] = 0;
567 ip6h->flow_lbl[2] = 0;
570 sg_init_table(sg, nfrags);
571 skb_to_sgvec(skb, sg, 0, skb->len);
573 ahash_request_set_crypt(req, sg, icv, skb->len);
574 ahash_request_set_callback(req, 0, ah6_input_done, skb);
576 AH_SKB_CB(skb)->tmp = work_iph;
578 err = crypto_ahash_digest(req);
580 if (err == -EINPROGRESS)
588 err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
592 skb->network_header += ah_hlen;
593 memcpy(skb_network_header(skb), work_iph, hdr_len);
594 skb->transport_header = skb->network_header;
595 __skb_pull(skb, ah_hlen + hdr_len);
605 static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
606 u8 type, u8 code, int offset, __be32 info)
608 struct net *net = dev_net(skb->dev);
609 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
610 struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
611 struct xfrm_state *x;
613 if (type != ICMPV6_DEST_UNREACH &&
614 type != ICMPV6_PKT_TOOBIG)
617 x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
621 NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n",
622 ntohl(ah->spi), &iph->daddr);
627 static int ah6_init_state(struct xfrm_state *x)
629 struct ah_data *ahp = NULL;
630 struct xfrm_algo_desc *aalg_desc;
631 struct crypto_ahash *ahash;
639 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
643 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
648 if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
649 (x->aalg->alg_key_len + 7) / 8))
653 * Lookup the algorithm description maintained by xfrm_algo,
654 * verify crypto transform properties, and store information
655 * we need for AH processing. This lookup cannot fail here
656 * after a successful crypto_alloc_hash().
658 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
661 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
662 crypto_ahash_digestsize(ahash)) {
663 printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
664 x->aalg->alg_name, crypto_ahash_digestsize(ahash),
665 aalg_desc->uinfo.auth.icv_fullbits/8);
669 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
670 ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
672 BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
674 x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
676 switch (x->props.mode) {
678 case XFRM_MODE_TRANSPORT:
680 case XFRM_MODE_TUNNEL:
681 x->props.header_len += sizeof(struct ipv6hdr);
692 crypto_free_ahash(ahp->ahash);
698 static void ah6_destroy(struct xfrm_state *x)
700 struct ah_data *ahp = x->data;
705 crypto_free_ahash(ahp->ahash);
709 static const struct xfrm_type ah6_type =
711 .description = "AH6",
712 .owner = THIS_MODULE,
714 .flags = XFRM_TYPE_REPLAY_PROT,
715 .init_state = ah6_init_state,
716 .destructor = ah6_destroy,
718 .output = ah6_output,
719 .hdr_offset = xfrm6_find_1stfragopt,
722 static const struct inet6_protocol ah6_protocol = {
723 .handler = xfrm6_rcv,
724 .err_handler = ah6_err,
725 .flags = INET6_PROTO_NOPOLICY,
728 static int __init ah6_init(void)
730 if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
731 printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
735 if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
736 printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
737 xfrm_unregister_type(&ah6_type, AF_INET6);
744 static void __exit ah6_fini(void)
746 if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
747 printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
749 if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
750 printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
754 module_init(ah6_init);
755 module_exit(ah6_fini);
757 MODULE_LICENSE("GPL");
758 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);