1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C)2002 USAGI/WIDE Project
7 * Mitsuru KANDA @USAGI : IPv6 Support
8 * Kazunori MIYAZAWA @USAGI :
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
11 * This file is derived from net/ipv4/ah.c.
14 #define pr_fmt(fmt) "IPv6: " fmt
16 #include <crypto/algapi.h>
17 #include <crypto/hash.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
22 #include <linux/crypto.h>
23 #include <linux/pfkeyv2.h>
24 #include <linux/string.h>
25 #include <linux/scatterlist.h>
26 #include <net/ip6_route.h>
29 #include <net/protocol.h>
32 #define IPV6HDR_BASELEN 8
35 #if IS_ENABLED(CONFIG_IPV6_MIP6)
36 struct in6_addr saddr;
38 struct in6_addr daddr;
43 struct xfrm_skb_cb xfrm;
47 #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
49 static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
54 len = size + crypto_ahash_digestsize(ahash) +
55 (crypto_ahash_alignmask(ahash) &
56 ~(crypto_tfm_ctx_alignment() - 1));
58 len = ALIGN(len, crypto_tfm_ctx_alignment());
60 len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
61 len = ALIGN(len, __alignof__(struct scatterlist));
63 len += sizeof(struct scatterlist) * nfrags;
65 return kmalloc(len, GFP_ATOMIC);
68 static inline struct tmp_ext *ah_tmp_ext(void *base)
70 return base + IPV6HDR_BASELEN;
73 static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
78 static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
81 return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
84 static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
87 struct ahash_request *req;
89 req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
90 crypto_tfm_ctx_alignment());
92 ahash_request_set_tfm(req, ahash);
97 static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
98 struct ahash_request *req)
100 return (void *)ALIGN((unsigned long)(req + 1) +
101 crypto_ahash_reqsize(ahash),
102 __alignof__(struct scatterlist));
105 static bool zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
107 u8 *opt = (u8 *)opthdr;
108 int len = ipv6_optlen(opthdr);
125 optlen = opt[off+1]+2;
129 memset(&opt[off+2], 0, opt[off+1]);
143 #if IS_ENABLED(CONFIG_IPV6_MIP6)
145 * ipv6_rearrange_destopt - rearrange IPv6 destination options header
147 * @destopt: destionation options header
149 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
151 u8 *opt = (u8 *)destopt;
152 int len = ipv6_optlen(destopt);
169 optlen = opt[off+1]+2;
173 /* Rearrange the source address in @iph and the
174 * addresses in home address option for final source.
175 * See 11.3.2 of RFC 3775 for details.
177 if (opt[off] == IPV6_TLV_HAO) {
178 struct ipv6_destopt_hao *hao;
180 hao = (struct ipv6_destopt_hao *)&opt[off];
181 if (hao->length != sizeof(hao->addr)) {
182 net_warn_ratelimited("destopt hao: invalid header length: %u\n",
186 swap(hao->addr, iph->saddr);
194 /* Note: ok if len == 0 */
199 static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
203 * ipv6_rearrange_rthdr - rearrange IPv6 routing header
205 * @rthdr: routing header
207 * Rearrange the destination address in @iph and the addresses in @rthdr
208 * so that they appear in the order they will at the final destination.
209 * See Appendix A2 of RFC 2402 for details.
211 static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
213 int segments, segments_left;
214 struct in6_addr *addrs;
215 struct in6_addr final_addr;
217 segments_left = rthdr->segments_left;
218 if (segments_left == 0)
220 rthdr->segments_left = 0;
222 /* The value of rthdr->hdrlen has been verified either by the system
223 * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
224 * packets. So we can assume that it is even and that segments is
225 * greater than or equal to segments_left.
227 * For the same reason we can assume that this option is of type 0.
229 segments = rthdr->hdrlen >> 1;
231 addrs = ((struct rt0_hdr *)rthdr)->addr;
232 final_addr = addrs[segments - 1];
234 addrs += segments - segments_left;
235 memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
237 addrs[0] = iph->daddr;
238 iph->daddr = final_addr;
241 static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
245 struct ipv6_opt_hdr *opth;
246 struct ipv6_rt_hdr *rth;
248 } exthdr = { .iph = iph };
249 char *end = exthdr.raw + len;
250 int nexthdr = iph->nexthdr;
254 while (exthdr.raw < end) {
257 if (dir == XFRM_POLICY_OUT)
258 ipv6_rearrange_destopt(iph, exthdr.opth);
261 if (!zero_out_mutable_opts(exthdr.opth)) {
262 net_dbg_ratelimited("overrun %sopts\n",
263 nexthdr == NEXTHDR_HOP ?
269 case NEXTHDR_ROUTING:
270 ipv6_rearrange_rthdr(iph, exthdr.rth);
277 nexthdr = exthdr.opth->nexthdr;
278 exthdr.raw += ipv6_optlen(exthdr.opth);
284 static void ah6_output_done(struct crypto_async_request *base, int err)
289 struct sk_buff *skb = base->data;
290 struct xfrm_state *x = skb_dst(skb)->xfrm;
291 struct ah_data *ahp = x->data;
292 struct ipv6hdr *top_iph = ipv6_hdr(skb);
293 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
294 struct tmp_ext *iph_ext;
296 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
298 extlen += sizeof(*iph_ext);
300 iph_base = AH_SKB_CB(skb)->tmp;
301 iph_ext = ah_tmp_ext(iph_base);
302 icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
304 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
305 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
308 #if IS_ENABLED(CONFIG_IPV6_MIP6)
309 memcpy(&top_iph->saddr, iph_ext, extlen);
311 memcpy(&top_iph->daddr, iph_ext, extlen);
315 kfree(AH_SKB_CB(skb)->tmp);
316 xfrm_output_resume(skb->sk, skb, err);
319 static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
327 struct sk_buff *trailer;
328 struct crypto_ahash *ahash;
329 struct ahash_request *req;
330 struct scatterlist *sg;
331 struct ipv6hdr *top_iph;
332 struct ip_auth_hdr *ah;
334 struct tmp_ext *iph_ext;
338 struct scatterlist *seqhisg;
343 err = skb_cow_data(skb, 0, &trailer);
348 skb_push(skb, -skb_network_offset(skb));
349 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
351 extlen += sizeof(*iph_ext);
353 if (x->props.flags & XFRM_STATE_ESN) {
355 seqhi_len = sizeof(*seqhi);
358 iph_base = ah_alloc_tmp(ahash, nfrags + sglists, IPV6HDR_BASELEN +
363 iph_ext = ah_tmp_ext(iph_base);
364 seqhi = (__be32 *)((char *)iph_ext + extlen);
365 icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
366 req = ah_tmp_req(ahash, icv);
367 sg = ah_req_sg(ahash, req);
368 seqhisg = sg + nfrags;
370 ah = ip_auth_hdr(skb);
371 memset(ah->auth_data, 0, ahp->icv_trunc_len);
373 top_iph = ipv6_hdr(skb);
374 top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
376 nexthdr = *skb_mac_header(skb);
377 *skb_mac_header(skb) = IPPROTO_AH;
379 /* When there are no extension headers, we only need to save the first
380 * 8 bytes of the base IP header.
382 memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
385 #if IS_ENABLED(CONFIG_IPV6_MIP6)
386 memcpy(iph_ext, &top_iph->saddr, extlen);
388 memcpy(iph_ext, &top_iph->daddr, extlen);
390 err = ipv6_clear_mutable_options(top_iph,
391 extlen - sizeof(*iph_ext) +
398 ah->nexthdr = nexthdr;
400 top_iph->priority = 0;
401 top_iph->flow_lbl[0] = 0;
402 top_iph->flow_lbl[1] = 0;
403 top_iph->flow_lbl[2] = 0;
404 top_iph->hop_limit = 0;
406 ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
410 ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
412 sg_init_table(sg, nfrags + sglists);
413 err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
414 if (unlikely(err < 0))
417 if (x->props.flags & XFRM_STATE_ESN) {
418 /* Attach seqhi sg right after packet payload */
419 *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
420 sg_set_buf(seqhisg, seqhi, seqhi_len);
422 ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
423 ahash_request_set_callback(req, 0, ah6_output_done, skb);
425 AH_SKB_CB(skb)->tmp = iph_base;
427 err = crypto_ahash_digest(req);
429 if (err == -EINPROGRESS)
437 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
438 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
441 #if IS_ENABLED(CONFIG_IPV6_MIP6)
442 memcpy(&top_iph->saddr, iph_ext, extlen);
444 memcpy(&top_iph->daddr, iph_ext, extlen);
454 static void ah6_input_done(struct crypto_async_request *base, int err)
459 struct sk_buff *skb = base->data;
460 struct xfrm_state *x = xfrm_input_state(skb);
461 struct ah_data *ahp = x->data;
462 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
463 int hdr_len = skb_network_header_len(skb);
464 int ah_hlen = ipv6_authlen(ah);
469 work_iph = AH_SKB_CB(skb)->tmp;
470 auth_data = ah_tmp_auth(work_iph, hdr_len);
471 icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
473 err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
479 skb->network_header += ah_hlen;
480 memcpy(skb_network_header(skb), work_iph, hdr_len);
481 __skb_pull(skb, ah_hlen + hdr_len);
482 if (x->props.mode == XFRM_MODE_TUNNEL)
483 skb_reset_transport_header(skb);
485 skb_set_transport_header(skb, -hdr_len);
487 kfree(AH_SKB_CB(skb)->tmp);
488 xfrm_input_resume(skb, err);
493 static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
497 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
498 * |<-------------->| hdr_len
501 * Keeping copy of cleared headers. After AH processing,
502 * Moving the pointer of skb->network_header by using skb_pull as long
503 * as AH header length. Then copy back the copy as long as hdr_len
504 * If destination header following AH exists, copy it into after [Ext2].
506 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
507 * There is offset of AH before IPv6 header after the process.
513 struct sk_buff *trailer;
514 struct crypto_ahash *ahash;
515 struct ahash_request *req;
516 struct scatterlist *sg;
517 struct ip_auth_hdr *ah;
518 struct ipv6hdr *ip6h;
528 struct scatterlist *seqhisg;
530 if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
533 /* We are going to _remove_ AH header to keep sockets happy,
534 * so... Later this can change. */
535 if (skb_unclone(skb, GFP_ATOMIC))
538 skb->ip_summed = CHECKSUM_NONE;
540 hdr_len = skb_network_header_len(skb);
541 ah = (struct ip_auth_hdr *)skb->data;
545 nexthdr = ah->nexthdr;
546 ah_hlen = ipv6_authlen(ah);
548 if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
549 ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
552 if (!pskb_may_pull(skb, ah_hlen))
555 err = skb_cow_data(skb, 0, &trailer);
560 ah = (struct ip_auth_hdr *)skb->data;
561 ip6h = ipv6_hdr(skb);
563 skb_push(skb, hdr_len);
565 if (x->props.flags & XFRM_STATE_ESN) {
567 seqhi_len = sizeof(*seqhi);
570 work_iph = ah_alloc_tmp(ahash, nfrags + sglists, hdr_len +
571 ahp->icv_trunc_len + seqhi_len);
577 auth_data = ah_tmp_auth((u8 *)work_iph, hdr_len);
578 seqhi = (__be32 *)(auth_data + ahp->icv_trunc_len);
579 icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
580 req = ah_tmp_req(ahash, icv);
581 sg = ah_req_sg(ahash, req);
582 seqhisg = sg + nfrags;
584 memcpy(work_iph, ip6h, hdr_len);
585 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
586 memset(ah->auth_data, 0, ahp->icv_trunc_len);
588 err = ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN);
593 ip6h->flow_lbl[0] = 0;
594 ip6h->flow_lbl[1] = 0;
595 ip6h->flow_lbl[2] = 0;
598 sg_init_table(sg, nfrags + sglists);
599 err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
600 if (unlikely(err < 0))
603 if (x->props.flags & XFRM_STATE_ESN) {
604 /* Attach seqhi sg right after packet payload */
605 *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
606 sg_set_buf(seqhisg, seqhi, seqhi_len);
609 ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
610 ahash_request_set_callback(req, 0, ah6_input_done, skb);
612 AH_SKB_CB(skb)->tmp = work_iph;
614 err = crypto_ahash_digest(req);
616 if (err == -EINPROGRESS)
622 err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
626 skb->network_header += ah_hlen;
627 memcpy(skb_network_header(skb), work_iph, hdr_len);
628 __skb_pull(skb, ah_hlen + hdr_len);
630 if (x->props.mode == XFRM_MODE_TUNNEL)
631 skb_reset_transport_header(skb);
633 skb_set_transport_header(skb, -hdr_len);
643 static int ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
644 u8 type, u8 code, int offset, __be32 info)
646 struct net *net = dev_net(skb->dev);
647 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
648 struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+offset);
649 struct xfrm_state *x;
651 if (type != ICMPV6_PKT_TOOBIG &&
652 type != NDISC_REDIRECT)
655 x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
659 if (type == NDISC_REDIRECT)
660 ip6_redirect(skb, net, skb->dev->ifindex, 0,
661 sock_net_uid(net, NULL));
663 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
669 static int ah6_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
671 struct ah_data *ahp = NULL;
672 struct xfrm_algo_desc *aalg_desc;
673 struct crypto_ahash *ahash;
676 NL_SET_ERR_MSG(extack, "AH requires a state with an AUTH algorithm");
681 NL_SET_ERR_MSG(extack, "AH is not compatible with encapsulation");
685 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
689 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
691 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
696 if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
697 (x->aalg->alg_key_len + 7) / 8)) {
698 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
703 * Lookup the algorithm description maintained by xfrm_algo,
704 * verify crypto transform properties, and store information
705 * we need for AH processing. This lookup cannot fail here
706 * after a successful crypto_alloc_hash().
708 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
711 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
712 crypto_ahash_digestsize(ahash)) {
713 NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
717 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
718 ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
720 x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
722 switch (x->props.mode) {
724 case XFRM_MODE_TRANSPORT:
726 case XFRM_MODE_TUNNEL:
727 x->props.header_len += sizeof(struct ipv6hdr);
730 NL_SET_ERR_MSG(extack, "Invalid mode requested for AH, must be one of TRANSPORT, TUNNEL, BEET");
739 crypto_free_ahash(ahp->ahash);
745 static void ah6_destroy(struct xfrm_state *x)
747 struct ah_data *ahp = x->data;
752 crypto_free_ahash(ahp->ahash);
756 static int ah6_rcv_cb(struct sk_buff *skb, int err)
761 static const struct xfrm_type ah6_type = {
762 .owner = THIS_MODULE,
764 .flags = XFRM_TYPE_REPLAY_PROT,
765 .init_state = ah6_init_state,
766 .destructor = ah6_destroy,
768 .output = ah6_output,
771 static struct xfrm6_protocol ah6_protocol = {
772 .handler = xfrm6_rcv,
773 .input_handler = xfrm_input,
774 .cb_handler = ah6_rcv_cb,
775 .err_handler = ah6_err,
779 static int __init ah6_init(void)
781 if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
782 pr_info("%s: can't add xfrm type\n", __func__);
786 if (xfrm6_protocol_register(&ah6_protocol, IPPROTO_AH) < 0) {
787 pr_info("%s: can't add protocol\n", __func__);
788 xfrm_unregister_type(&ah6_type, AF_INET6);
795 static void __exit ah6_fini(void)
797 if (xfrm6_protocol_deregister(&ah6_protocol, IPPROTO_AH) < 0)
798 pr_info("%s: can't remove protocol\n", __func__);
800 xfrm_unregister_type(&ah6_type, AF_INET6);
803 module_init(ah6_init);
804 module_exit(ah6_fini);
806 MODULE_LICENSE("GPL");
807 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);