blob: fbe51d40bd7e9f8b50bc3ba1c1d41710a162e29d [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
Steffen Klassertdd767852011-10-11 01:44:30 +000026static int xfrm6_local_dontfrag(struct sk_buff *skb)
27{
28 int proto;
29 struct sock *sk = skb->sk;
30
31 if (sk) {
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020032 if (sk->sk_family != AF_INET6)
33 return 0;
Steffen Klassertdd767852011-10-11 01:44:30 +000034
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020035 proto = sk->sk_protocol;
Steffen Klassertdd767852011-10-11 01:44:30 +000036 if (proto == IPPROTO_UDP || proto == IPPROTO_RAW)
37 return inet6_sk(sk)->dontfrag;
38 }
39
40 return 0;
41}
42
43static 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 Dobriyan4e3fd7a2011-11-21 03:39:03 +000049 fl6.daddr = ipv6_hdr(skb)->daddr;
Steffen Klassertdd767852011-10-11 01:44:30 +000050
51 ipv6_local_rxpmtu(sk, &fl6, mtu);
52}
53
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020054void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
Steffen Klassertdd767852011-10-11 01:44:30 +000055{
56 struct flowi6 fl6;
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020057 const struct ipv6hdr *hdr;
Steffen Klassertdd767852011-10-11 01:44:30 +000058 struct sock *sk = skb->sk;
59
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020060 hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000061 fl6.fl6_dport = inet_sk(sk)->inet_dport;
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020062 fl6.daddr = hdr->daddr;
Steffen Klassertdd767852011-10-11 01:44:30 +000063
64 ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int xfrm6_tunnel_check_size(struct sk_buff *skb)
68{
69 int mtu, ret = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +000070 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Steffen Klassertd77e38e2017-04-14 10:06:10 +020072 if (skb->ignore_df)
73 goto out;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 mtu = dst_mtu(dst);
76 if (mtu < IPV6_MIN_MTU)
77 mtu = IPV6_MIN_MTU;
78
Steffen Klassertd77e38e2017-04-14 10:06:10 +020079 if ((!skb_is_gso(skb) && skb->len > mtu) ||
80 (skb_is_gso(skb) &&
Daniel Axtens80f59742018-03-01 17:13:39 +110081 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
Herbert Xu180e4252005-05-23 13:11:07 -070082 skb->dev = dst->dev;
Steffen Klassertca064bd2015-10-19 10:30:05 +020083 skb->protocol = htons(ETH_P_IPV6);
Steffen Klassertdd767852011-10-11 01:44:30 +000084
85 if (xfrm6_local_dontfrag(skb))
86 xfrm6_local_rxpmtu(skb, mtu);
87 else if (skb->sk)
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020088 xfrm_local_error(skb, mtu);
Steffen Klassertdd767852011-10-11 01:44:30 +000089 else
90 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ret = -EMSGSIZE;
92 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +020093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return ret;
95}
96
Herbert Xu36cf9ac2007-11-13 21:40:52 -080097int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900100
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800101 err = xfrm6_tunnel_check_size(skb);
102 if (err)
103 return err;
104
Herbert Xu60d5fcf2007-11-19 18:47:58 -0800105 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
106
Herbert Xu36cf9ac2007-11-13 21:40:52 -0800107 return xfrm6_extract_header(skb);
108}
109
David Miller7026b1d2015-04-05 22:19:04 -0400110int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb)
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700111{
Steffen Klassert55967322014-04-07 08:08:52 +0200112 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
Steffen Klassert55967322014-04-07 08:08:52 +0200113
Herbert Xu862b82c2007-11-13 21:43:11 -0800114#ifdef CONFIG_NETFILTER
115 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
116#endif
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700117
David Miller7026b1d2015-04-05 22:19:04 -0400118 return xfrm_output(sk, skb);
Herbert Xu09b8f7a2006-06-22 03:08:03 -0700119}
120
Florian Westphal733a5fa2019-03-29 21:16:30 +0100121static 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 Westphalc9500d72019-03-29 21:16:32 +0100128 afinfo = xfrm_state_afinfo_get_rcu(x->outer_mode.family);
Florian Westphal733a5fa2019-03-29 21:16:30 +0100129 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. Biederman7d8c6e32015-06-12 22:12:04 -0500138static 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. Biedermanb1842ff2015-10-01 11:41:42 -0500141
Florian Westphal733a5fa2019-03-29 21:16:30 +0100142 return __xfrm6_output_state_finish(x, sk, skb);
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500143}
144
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500145static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Stevensad0081e2010-12-17 11:42:42 +0000146{
147 struct dst_entry *dst = skb_dst(skb);
148 struct xfrm_state *x = dst->xfrm;
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +0200149 int mtu;
Herbert Xu93efac32015-09-04 13:21:06 +0800150 bool toobig;
Hannes Frederic Sowa0ea9d5e2013-08-13 04:35:58 +0200151
Steffen Klassert55967322014-04-07 08:08:52 +0200152#ifdef CONFIG_NETFILTER
153 if (!x) {
154 IP6CB(skb)->flags |= IP6SKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -0500155 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +0200156 }
157#endif
158
Herbert Xu93efac32015-09-04 13:21:06 +0800159 if (x->props.mode != XFRM_MODE_TUNNEL)
160 goto skip_frag;
161
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +0200162 if (skb->protocol == htons(ETH_P_IPV6))
163 mtu = ip6_skb_dst_mtu(skb);
164 else
165 mtu = dst_mtu(skb_dst(skb));
Steffen Klassertdd767852011-10-11 01:44:30 +0000166
Herbert Xu93efac32015-09-04 13:21:06 +0800167 toobig = skb->len > mtu && !skb_is_gso(skb);
168
169 if (toobig && xfrm6_local_dontfrag(skb)) {
Steffen Klassertdd767852011-10-11 01:44:30 +0000170 xfrm6_local_rxpmtu(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -0300171 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +0000172 return -EMSGSIZE;
Herbert Xu93efac32015-09-04 13:21:06 +0800173 } else if (!skb->ignore_df && toobig && skb->sk) {
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200174 xfrm_local_error(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -0300175 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +0000176 return -EMSGSIZE;
177 }
David Stevensad0081e2010-12-17 11:42:42 +0000178
Herbert Xu93efac32015-09-04 13:21:06 +0800179 if (toobig || dst_allfrag(skb_dst(skb)))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500180 return ip6_fragment(net, sk, skb,
181 __xfrm6_output_finish);
Herbert Xu93efac32015-09-04 13:21:06 +0800182
183skip_frag:
Florian Westphal733a5fa2019-03-29 21:16:30 +0100184 return __xfrm6_output_state_finish(x, sk, skb);
David Stevensad0081e2010-12-17 11:42:42 +0000185}
186
Eric W. Biedermanede20592015-10-07 16:48:47 -0500187int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -0800188{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500189 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100190 net, sk, skb, skb->dev, skb_dst(skb)->dev,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500191 __xfrm6_output,
Steffen Klassert55967322014-04-07 08:08:52 +0200192 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800193}