Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xfrm6_output.c - Common IPsec encapsulation code for IPv6. |
| 4 | * Copyright (C) 2002 USAGI/WIDE Project |
| 5 | * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 8 | #include <linux/if_ether.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/icmpv6.h> |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 13 | #include <linux/netfilter_ipv6.h> |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 14 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <net/ipv6.h> |
David Stevens | ad0081e | 2010-12-17 11:42:42 +0000 | [diff] [blame] | 16 | #include <net/ip6_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/xfrm.h> |
| 18 | |
Florian Westphal | 3e50ddd | 2020-05-04 10:06:06 +0200 | [diff] [blame] | 19 | void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu) |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 20 | { |
| 21 | struct flowi6 fl6; |
| 22 | struct sock *sk = skb->sk; |
| 23 | |
| 24 | fl6.flowi6_oif = sk->sk_bound_dev_if; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 25 | fl6.daddr = ipv6_hdr(skb)->daddr; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 26 | |
| 27 | ipv6_local_rxpmtu(sk, &fl6, mtu); |
| 28 | } |
| 29 | |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 30 | void xfrm6_local_error(struct sk_buff *skb, u32 mtu) |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 31 | { |
| 32 | struct flowi6 fl6; |
Hannes Frederic Sowa | 5d0ff54 | 2013-08-18 13:46:57 +0200 | [diff] [blame] | 33 | const struct ipv6hdr *hdr; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 34 | struct sock *sk = skb->sk; |
| 35 | |
Hannes Frederic Sowa | 5d0ff54 | 2013-08-18 13:46:57 +0200 | [diff] [blame] | 36 | hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 37 | fl6.fl6_dport = inet_sk(sk)->inet_dport; |
Hannes Frederic Sowa | 5d0ff54 | 2013-08-18 13:46:57 +0200 | [diff] [blame] | 38 | fl6.daddr = hdr->daddr; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 39 | |
| 40 | ipv6_local_error(sk, EMSGSIZE, &fl6, mtu); |
| 41 | } |
| 42 | |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 43 | static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 44 | { |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 45 | return xfrm_output(sk, skb); |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 48 | static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
David Stevens | ad0081e | 2010-12-17 11:42:42 +0000 | [diff] [blame] | 49 | { |
| 50 | struct dst_entry *dst = skb_dst(skb); |
| 51 | struct xfrm_state *x = dst->xfrm; |
Vadim Fedorenko | 40fc305 | 2021-07-02 02:47:00 +0300 | [diff] [blame] | 52 | unsigned int mtu; |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 53 | bool toobig; |
Hannes Frederic Sowa | 0ea9d5e | 2013-08-13 04:35:58 +0200 | [diff] [blame] | 54 | |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 55 | #ifdef CONFIG_NETFILTER |
| 56 | if (!x) { |
| 57 | IP6CB(skb)->flags |= IP6SKB_REROUTED; |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 58 | return dst_output(net, sk, skb); |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 59 | } |
| 60 | #endif |
| 61 | |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 62 | if (x->props.mode != XFRM_MODE_TUNNEL) |
| 63 | goto skip_frag; |
| 64 | |
Hannes Frederic Sowa | 5a25cf1 | 2013-08-26 12:31:19 +0200 | [diff] [blame] | 65 | if (skb->protocol == htons(ETH_P_IPV6)) |
| 66 | mtu = ip6_skb_dst_mtu(skb); |
| 67 | else |
| 68 | mtu = dst_mtu(skb_dst(skb)); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 69 | |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 70 | toobig = skb->len > mtu && !skb_is_gso(skb); |
| 71 | |
Florian Westphal | ede64dd | 2020-05-04 10:06:07 +0200 | [diff] [blame] | 72 | if (toobig && xfrm6_local_dontfrag(skb->sk)) { |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 73 | xfrm6_local_rxpmtu(skb, mtu); |
Thadeu Lima de Souza Cascardo | 215ab0f | 2018-08-31 08:38:49 -0300 | [diff] [blame] | 74 | kfree_skb(skb); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 75 | return -EMSGSIZE; |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 76 | } else if (!skb->ignore_df && toobig && skb->sk) { |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 77 | xfrm_local_error(skb, mtu); |
Thadeu Lima de Souza Cascardo | 215ab0f | 2018-08-31 08:38:49 -0300 | [diff] [blame] | 78 | kfree_skb(skb); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 79 | return -EMSGSIZE; |
| 80 | } |
David Stevens | ad0081e | 2010-12-17 11:42:42 +0000 | [diff] [blame] | 81 | |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 82 | if (toobig || dst_allfrag(skb_dst(skb))) |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 83 | return ip6_fragment(net, sk, skb, |
| 84 | __xfrm6_output_finish); |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 85 | |
| 86 | skip_frag: |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 87 | return xfrm_output(sk, skb); |
David Stevens | ad0081e | 2010-12-17 11:42:42 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 90 | int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 91 | { |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 92 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, |
Phil Sutter | 28f8bfd | 2019-11-12 17:14:37 +0100 | [diff] [blame] | 93 | net, sk, skb, skb->dev, skb_dst(skb)->dev, |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 94 | __xfrm6_output, |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 95 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 96 | } |