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