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 | |
Masahide NAKAMURA | aee5adb | 2006-08-23 17:57:28 -0700 | [diff] [blame] | 19 | int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb, |
| 20 | u8 **prevhdr) |
| 21 | { |
| 22 | return ip6_find_1stfragopt(skb, prevhdr); |
| 23 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 24 | EXPORT_SYMBOL(xfrm6_find_1stfragopt); |
| 25 | |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 26 | static int xfrm6_local_dontfrag(struct sk_buff *skb) |
| 27 | { |
| 28 | int proto; |
| 29 | struct sock *sk = skb->sk; |
| 30 | |
| 31 | if (sk) { |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 32 | if (sk->sk_family != AF_INET6) |
| 33 | return 0; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 34 | |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 35 | proto = sk->sk_protocol; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 36 | if (proto == IPPROTO_UDP || proto == IPPROTO_RAW) |
| 37 | return inet6_sk(sk)->dontfrag; |
| 38 | } |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu) |
| 44 | { |
| 45 | struct flowi6 fl6; |
| 46 | struct sock *sk = skb->sk; |
| 47 | |
| 48 | fl6.flowi6_oif = sk->sk_bound_dev_if; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 49 | fl6.daddr = ipv6_hdr(skb)->daddr; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 50 | |
| 51 | ipv6_local_rxpmtu(sk, &fl6, mtu); |
| 52 | } |
| 53 | |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 54 | void xfrm6_local_error(struct sk_buff *skb, u32 mtu) |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 55 | { |
| 56 | struct flowi6 fl6; |
Hannes Frederic Sowa | 5d0ff54 | 2013-08-18 13:46:57 +0200 | [diff] [blame] | 57 | const struct ipv6hdr *hdr; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 58 | struct sock *sk = skb->sk; |
| 59 | |
Hannes Frederic Sowa | 5d0ff54 | 2013-08-18 13:46:57 +0200 | [diff] [blame] | 60 | hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 61 | fl6.fl6_dport = inet_sk(sk)->inet_dport; |
Hannes Frederic Sowa | 5d0ff54 | 2013-08-18 13:46:57 +0200 | [diff] [blame] | 62 | fl6.daddr = hdr->daddr; |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 63 | |
| 64 | ipv6_local_error(sk, EMSGSIZE, &fl6, mtu); |
| 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) |
| 68 | { |
| 69 | int mtu, ret = 0; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 70 | struct dst_entry *dst = skb_dst(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 72 | if (skb->ignore_df) |
| 73 | goto out; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | mtu = dst_mtu(dst); |
| 76 | if (mtu < IPV6_MIN_MTU) |
| 77 | mtu = IPV6_MIN_MTU; |
| 78 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 79 | if ((!skb_is_gso(skb) && skb->len > mtu) || |
| 80 | (skb_is_gso(skb) && |
Daniel Axtens | 80f5974 | 2018-03-01 17:13:39 +1100 | [diff] [blame] | 81 | !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) { |
Herbert Xu | 180e425 | 2005-05-23 13:11:07 -0700 | [diff] [blame] | 82 | skb->dev = dst->dev; |
Steffen Klassert | ca064bd | 2015-10-19 10:30:05 +0200 | [diff] [blame] | 83 | skb->protocol = htons(ETH_P_IPV6); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 84 | |
| 85 | if (xfrm6_local_dontfrag(skb)) |
| 86 | xfrm6_local_rxpmtu(skb, mtu); |
| 87 | else if (skb->sk) |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 88 | xfrm_local_error(skb, mtu); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 89 | else |
| 90 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | ret = -EMSGSIZE; |
| 92 | } |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 93 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return ret; |
| 95 | } |
| 96 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 97 | int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 100 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 101 | err = xfrm6_tunnel_check_size(skb); |
| 102 | if (err) |
| 103 | return err; |
| 104 | |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 105 | XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr; |
| 106 | |
Herbert Xu | 36cf9ac | 2007-11-13 21:40:52 -0800 | [diff] [blame] | 107 | return xfrm6_extract_header(skb); |
| 108 | } |
| 109 | |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 110 | int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb) |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 111 | { |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 112 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 113 | |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 114 | #ifdef CONFIG_NETFILTER |
| 115 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
| 116 | #endif |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 117 | |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 118 | return xfrm_output(sk, skb); |
Herbert Xu | 09b8f7a | 2006-06-22 03:08:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 121 | static int __xfrm6_output_state_finish(struct xfrm_state *x, struct sock *sk, |
| 122 | struct sk_buff *skb) |
| 123 | { |
| 124 | const struct xfrm_state_afinfo *afinfo; |
| 125 | int ret = -EAFNOSUPPORT; |
| 126 | |
| 127 | rcu_read_lock(); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 128 | afinfo = xfrm_state_afinfo_get_rcu(x->outer_mode.family); |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 129 | if (likely(afinfo)) |
| 130 | ret = afinfo->output_finish(sk, skb); |
| 131 | else |
| 132 | kfree_skb(skb); |
| 133 | rcu_read_unlock(); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 138 | static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 139 | { |
| 140 | struct xfrm_state *x = skb_dst(skb)->xfrm; |
Eric W. Biederman | b1842ff | 2015-10-01 11:41:42 -0500 | [diff] [blame] | 141 | |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 142 | return __xfrm6_output_state_finish(x, sk, skb); |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | struct dst_entry *dst = skb_dst(skb); |
| 148 | struct xfrm_state *x = dst->xfrm; |
Hannes Frederic Sowa | 5a25cf1 | 2013-08-26 12:31:19 +0200 | [diff] [blame] | 149 | int mtu; |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 150 | bool toobig; |
Hannes Frederic Sowa | 0ea9d5e | 2013-08-13 04:35:58 +0200 | [diff] [blame] | 151 | |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 152 | #ifdef CONFIG_NETFILTER |
| 153 | if (!x) { |
| 154 | IP6CB(skb)->flags |= IP6SKB_REROUTED; |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 155 | return dst_output(net, sk, skb); |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 156 | } |
| 157 | #endif |
| 158 | |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 159 | if (x->props.mode != XFRM_MODE_TUNNEL) |
| 160 | goto skip_frag; |
| 161 | |
Hannes Frederic Sowa | 5a25cf1 | 2013-08-26 12:31:19 +0200 | [diff] [blame] | 162 | if (skb->protocol == htons(ETH_P_IPV6)) |
| 163 | mtu = ip6_skb_dst_mtu(skb); |
| 164 | else |
| 165 | mtu = dst_mtu(skb_dst(skb)); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 166 | |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 167 | toobig = skb->len > mtu && !skb_is_gso(skb); |
| 168 | |
| 169 | if (toobig && xfrm6_local_dontfrag(skb)) { |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 170 | xfrm6_local_rxpmtu(skb, mtu); |
Thadeu Lima de Souza Cascardo | 215ab0f | 2018-08-31 08:38:49 -0300 | [diff] [blame] | 171 | kfree_skb(skb); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 172 | return -EMSGSIZE; |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 173 | } else if (!skb->ignore_df && toobig && skb->sk) { |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 174 | xfrm_local_error(skb, mtu); |
Thadeu Lima de Souza Cascardo | 215ab0f | 2018-08-31 08:38:49 -0300 | [diff] [blame] | 175 | kfree_skb(skb); |
Steffen Klassert | dd76785 | 2011-10-11 01:44:30 +0000 | [diff] [blame] | 176 | return -EMSGSIZE; |
| 177 | } |
David Stevens | ad0081e | 2010-12-17 11:42:42 +0000 | [diff] [blame] | 178 | |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 179 | if (toobig || dst_allfrag(skb_dst(skb))) |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 180 | return ip6_fragment(net, sk, skb, |
| 181 | __xfrm6_output_finish); |
Herbert Xu | 93efac3 | 2015-09-04 13:21:06 +0800 | [diff] [blame] | 182 | |
| 183 | skip_frag: |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 184 | return __xfrm6_output_state_finish(x, sk, skb); |
David Stevens | ad0081e | 2010-12-17 11:42:42 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 187 | 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] | 188 | { |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 189 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, |
Phil Sutter | 28f8bfd | 2019-11-12 17:14:37 +0100 | [diff] [blame] | 190 | net, sk, skb, skb->dev, skb_dst(skb)->dev, |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 191 | __xfrm6_output, |
Steffen Klassert | 5596732 | 2014-04-07 08:08:52 +0200 | [diff] [blame] | 192 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 193 | } |