blob: 8b84d534b19d8f23b90ba9ffa6561192ff395d37 [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
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050050static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
51{
Florian Westphal2ab60962020-05-04 10:06:09 +020052 return xfrm_output(sk, skb);
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050053}
54
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050055static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Stevensad0081e2010-12-17 11:42:42 +000056{
57 struct dst_entry *dst = skb_dst(skb);
58 struct xfrm_state *x = dst->xfrm;
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +020059 int mtu;
Herbert Xu93efac32015-09-04 13:21:06 +080060 bool toobig;
Hannes Frederic Sowa0ea9d5e2013-08-13 04:35:58 +020061
Steffen Klassert55967322014-04-07 08:08:52 +020062#ifdef CONFIG_NETFILTER
63 if (!x) {
64 IP6CB(skb)->flags |= IP6SKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -050065 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020066 }
67#endif
68
Herbert Xu93efac32015-09-04 13:21:06 +080069 if (x->props.mode != XFRM_MODE_TUNNEL)
70 goto skip_frag;
71
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +020072 if (skb->protocol == htons(ETH_P_IPV6))
73 mtu = ip6_skb_dst_mtu(skb);
74 else
75 mtu = dst_mtu(skb_dst(skb));
Steffen Klassertdd767852011-10-11 01:44:30 +000076
Herbert Xu93efac32015-09-04 13:21:06 +080077 toobig = skb->len > mtu && !skb_is_gso(skb);
78
Florian Westphalede64dd2020-05-04 10:06:07 +020079 if (toobig && xfrm6_local_dontfrag(skb->sk)) {
Steffen Klassertdd767852011-10-11 01:44:30 +000080 xfrm6_local_rxpmtu(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -030081 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000082 return -EMSGSIZE;
Herbert Xu93efac32015-09-04 13:21:06 +080083 } else if (!skb->ignore_df && toobig && skb->sk) {
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020084 xfrm_local_error(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -030085 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000086 return -EMSGSIZE;
87 }
David Stevensad0081e2010-12-17 11:42:42 +000088
Herbert Xu93efac32015-09-04 13:21:06 +080089 if (toobig || dst_allfrag(skb_dst(skb)))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050090 return ip6_fragment(net, sk, skb,
91 __xfrm6_output_finish);
Herbert Xu93efac32015-09-04 13:21:06 +080092
93skip_frag:
Florian Westphal2ab60962020-05-04 10:06:09 +020094 return xfrm_output(sk, skb);
David Stevensad0081e2010-12-17 11:42:42 +000095}
96
Eric W. Biedermanede20592015-10-07 16:48:47 -050097int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -080098{
Eric W. Biederman29a26a52015-09-15 20:04:16 -050099 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100100 net, sk, skb, skb->dev, skb_dst(skb)->dev,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500101 __xfrm6_output,
Steffen Klassert55967322014-04-07 08:08:52 +0200102 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -0800103}