blob: be64f280510c95b8a53a1899a1bb294d8bc1ff72 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Herbert Xu406ef772007-10-08 17:16:30 -07008#include <linux/if_ether.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -08009#include <linux/kernel.h>
10#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/icmpv6.h>
Patrick McHardy16a66772006-01-06 23:01:48 -080013#include <linux/netfilter_ipv6.h>
Herbert Xu36cf9ac2007-11-13 21:40:52 -080014#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/ipv6.h>
David Stevensad0081e2010-12-17 11:42:42 +000016#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/xfrm.h>
18
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -070019int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
20 u8 **prevhdr)
21{
22 return ip6_find_1stfragopt(skb, prevhdr);
23}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090024EXPORT_SYMBOL(xfrm6_find_1stfragopt);
25
Florian Westphal3e50ddd2020-05-04 10:06:06 +020026void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
Steffen Klassertdd767852011-10-11 01:44:30 +000027{
28 struct flowi6 fl6;
29 struct sock *sk = skb->sk;
30
31 fl6.flowi6_oif = sk->sk_bound_dev_if;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +000032 fl6.daddr = ipv6_hdr(skb)->daddr;
Steffen Klassertdd767852011-10-11 01:44:30 +000033
34 ipv6_local_rxpmtu(sk, &fl6, mtu);
35}
36
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020037void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
Steffen Klassertdd767852011-10-11 01:44:30 +000038{
39 struct flowi6 fl6;
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020040 const struct ipv6hdr *hdr;
Steffen Klassertdd767852011-10-11 01:44:30 +000041 struct sock *sk = skb->sk;
42
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020043 hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000044 fl6.fl6_dport = inet_sk(sk)->inet_dport;
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020045 fl6.daddr = hdr->daddr;
Steffen Klassertdd767852011-10-11 01:44:30 +000046
47 ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int xfrm6_tunnel_check_size(struct sk_buff *skb)
51{
52 int mtu, ret = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +000053 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Steffen Klassertd77e38e2017-04-14 10:06:10 +020055 if (skb->ignore_df)
56 goto out;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mtu = dst_mtu(dst);
59 if (mtu < IPV6_MIN_MTU)
60 mtu = IPV6_MIN_MTU;
61
Steffen Klassertd77e38e2017-04-14 10:06:10 +020062 if ((!skb_is_gso(skb) && skb->len > mtu) ||
63 (skb_is_gso(skb) &&
Daniel Axtens80f59742018-03-01 17:13:39 +110064 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
Herbert Xu180e4252005-05-23 13:11:07 -070065 skb->dev = dst->dev;
Steffen Klassertca064bd2015-10-19 10:30:05 +020066 skb->protocol = htons(ETH_P_IPV6);
Steffen Klassertdd767852011-10-11 01:44:30 +000067
Florian Westphalede64dd2020-05-04 10:06:07 +020068 if (xfrm6_local_dontfrag(skb->sk))
Steffen Klassertdd767852011-10-11 01:44:30 +000069 xfrm6_local_rxpmtu(skb, mtu);
70 else if (skb->sk)
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020071 xfrm_local_error(skb, mtu);
Steffen Klassertdd767852011-10-11 01:44:30 +000072 else
73 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 ret = -EMSGSIZE;
75 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +020076out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return ret;
78}
79
Florian Westphala269fbf2020-05-04 10:06:04 +020080static 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 Xu36cf9ac2007-11-13 21:40:52 -080094int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090097
Herbert Xu36cf9ac2007-11-13 21:40:52 -080098 err = xfrm6_tunnel_check_size(skb);
99 if (err)
100 return err;
101
Herbert Xu60d5fcf2007-11-19 18:47:58 -0800102 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
103
Florian Westphala269fbf2020-05-04 10:06:04 +0200104 __xfrm6_extract_header(skb);
105 return 0;
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800106}
107
David Miller7026b1d2015-04-05 22:19:04 -0400108int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb)
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700109{
Steffen Klassert55967322014-04-07 08:08:52 +0200110 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
Steffen Klassert55967322014-04-07 08:08:52 +0200111
Herbert Xu862b82c2007-11-13 21:43:11 -0800112#ifdef CONFIG_NETFILTER
113 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
114#endif
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700115
David Miller7026b1d2015-04-05 22:19:04 -0400116 return xfrm_output(sk, skb);
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700117}
118
Florian Westphal733a5fa2019-03-29 21:16:30 +0100119static 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 Westphalc9500d72019-03-29 21:16:32 +0100126 afinfo = xfrm_state_afinfo_get_rcu(x->outer_mode.family);
Florian Westphal733a5fa2019-03-29 21:16:30 +0100127 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. Biederman7d8c6e32015-06-12 22:12:04 -0500136static 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. Biedermanb1842ff2015-10-01 11:41:42 -0500139
Florian Westphal733a5fa2019-03-29 21:16:30 +0100140 return __xfrm6_output_state_finish(x, sk, skb);
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500141}
142
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500143static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Stevensad0081e2010-12-17 11:42:42 +0000144{
145 struct dst_entry *dst = skb_dst(skb);
146 struct xfrm_state *x = dst->xfrm;
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +0200147 int mtu;
Herbert Xu93efac32015-09-04 13:21:06 +0800148 bool toobig;
Hannes Frederic Sowa0ea9d5e2013-08-13 04:35:58 +0200149
Steffen Klassert55967322014-04-07 08:08:52 +0200150#ifdef CONFIG_NETFILTER
151 if (!x) {
152 IP6CB(skb)->flags |= IP6SKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -0500153 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +0200154 }
155#endif
156
Herbert Xu93efac32015-09-04 13:21:06 +0800157 if (x->props.mode != XFRM_MODE_TUNNEL)
158 goto skip_frag;
159
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +0200160 if (skb->protocol == htons(ETH_P_IPV6))
161 mtu = ip6_skb_dst_mtu(skb);
162 else
163 mtu = dst_mtu(skb_dst(skb));
Steffen Klassertdd767852011-10-11 01:44:30 +0000164
Herbert Xu93efac32015-09-04 13:21:06 +0800165 toobig = skb->len > mtu && !skb_is_gso(skb);
166
Florian Westphalede64dd2020-05-04 10:06:07 +0200167 if (toobig && xfrm6_local_dontfrag(skb->sk)) {
Steffen Klassertdd767852011-10-11 01:44:30 +0000168 xfrm6_local_rxpmtu(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -0300169 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +0000170 return -EMSGSIZE;
Herbert Xu93efac32015-09-04 13:21:06 +0800171 } else if (!skb->ignore_df && toobig && skb->sk) {
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200172 xfrm_local_error(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -0300173 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +0000174 return -EMSGSIZE;
175 }
David Stevensad0081e2010-12-17 11:42:42 +0000176
Herbert Xu93efac32015-09-04 13:21:06 +0800177 if (toobig || dst_allfrag(skb_dst(skb)))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500178 return ip6_fragment(net, sk, skb,
179 __xfrm6_output_finish);
Herbert Xu93efac32015-09-04 13:21:06 +0800180
181skip_frag:
Florian Westphal733a5fa2019-03-29 21:16:30 +0100182 return __xfrm6_output_state_finish(x, sk, skb);
David Stevensad0081e2010-12-17 11:42:42 +0000183}
184
Eric W. Biedermanede20592015-10-07 16:48:47 -0500185int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800186{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500187 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100188 net, sk, skb, skb->dev, skb_dst(skb)->dev,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500189 __xfrm6_output,
Steffen Klassert55967322014-04-07 08:08:52 +0200190 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800191}