blob: d0d280077721b89067115d5bbd6cd05b558c8047 [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
Florian Westphal3e50ddd2020-05-04 10:06:06 +020019void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
Steffen Klassertdd767852011-10-11 01:44:30 +000020{
21 struct flowi6 fl6;
22 struct sock *sk = skb->sk;
23
24 fl6.flowi6_oif = sk->sk_bound_dev_if;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +000025 fl6.daddr = ipv6_hdr(skb)->daddr;
Steffen Klassertdd767852011-10-11 01:44:30 +000026
27 ipv6_local_rxpmtu(sk, &fl6, mtu);
28}
29
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020030void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
Steffen Klassertdd767852011-10-11 01:44:30 +000031{
32 struct flowi6 fl6;
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020033 const struct ipv6hdr *hdr;
Steffen Klassertdd767852011-10-11 01:44:30 +000034 struct sock *sk = skb->sk;
35
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020036 hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000037 fl6.fl6_dport = inet_sk(sk)->inet_dport;
Hannes Frederic Sowa5d0ff542013-08-18 13:46:57 +020038 fl6.daddr = hdr->daddr;
Steffen Klassertdd767852011-10-11 01:44:30 +000039
40 ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
41}
42
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050043static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
44{
Florian Westphal2ab60962020-05-04 10:06:09 +020045 return xfrm_output(sk, skb);
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050046}
47
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050048static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Stevensad0081e2010-12-17 11:42:42 +000049{
50 struct dst_entry *dst = skb_dst(skb);
51 struct xfrm_state *x = dst->xfrm;
Vadim Fedorenko40fc3052021-07-02 02:47:00 +030052 unsigned int mtu;
Herbert Xu93efac32015-09-04 13:21:06 +080053 bool toobig;
Hannes Frederic Sowa0ea9d5e2013-08-13 04:35:58 +020054
Steffen Klassert55967322014-04-07 08:08:52 +020055#ifdef CONFIG_NETFILTER
56 if (!x) {
57 IP6CB(skb)->flags |= IP6SKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -050058 return dst_output(net, sk, skb);
Steffen Klassert55967322014-04-07 08:08:52 +020059 }
60#endif
61
Herbert Xu93efac32015-09-04 13:21:06 +080062 if (x->props.mode != XFRM_MODE_TUNNEL)
63 goto skip_frag;
64
Hannes Frederic Sowa5a25cf12013-08-26 12:31:19 +020065 if (skb->protocol == htons(ETH_P_IPV6))
66 mtu = ip6_skb_dst_mtu(skb);
67 else
68 mtu = dst_mtu(skb_dst(skb));
Steffen Klassertdd767852011-10-11 01:44:30 +000069
Herbert Xu93efac32015-09-04 13:21:06 +080070 toobig = skb->len > mtu && !skb_is_gso(skb);
71
Florian Westphalede64dd2020-05-04 10:06:07 +020072 if (toobig && xfrm6_local_dontfrag(skb->sk)) {
Steffen Klassertdd767852011-10-11 01:44:30 +000073 xfrm6_local_rxpmtu(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -030074 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000075 return -EMSGSIZE;
Herbert Xu93efac32015-09-04 13:21:06 +080076 } else if (!skb->ignore_df && toobig && skb->sk) {
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +020077 xfrm_local_error(skb, mtu);
Thadeu Lima de Souza Cascardo215ab0f2018-08-31 08:38:49 -030078 kfree_skb(skb);
Steffen Klassertdd767852011-10-11 01:44:30 +000079 return -EMSGSIZE;
80 }
David Stevensad0081e2010-12-17 11:42:42 +000081
Herbert Xu93efac32015-09-04 13:21:06 +080082 if (toobig || dst_allfrag(skb_dst(skb)))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050083 return ip6_fragment(net, sk, skb,
84 __xfrm6_output_finish);
Herbert Xu93efac32015-09-04 13:21:06 +080085
86skip_frag:
Florian Westphal2ab60962020-05-04 10:06:09 +020087 return xfrm_output(sk, skb);
David Stevensad0081e2010-12-17 11:42:42 +000088}
89
Eric W. Biedermanede20592015-10-07 16:48:47 -050090int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Patrick McHardy16a66772006-01-06 23:01:48 -080091{
Eric W. Biederman29a26a52015-09-15 20:04:16 -050092 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +010093 net, sk, skb, skb->dev, skb_dst(skb)->dev,
Eric W. Biederman29a26a52015-09-15 20:04:16 -050094 __xfrm6_output,
Steffen Klassert55967322014-04-07 08:08:52 +020095 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Patrick McHardy16a66772006-01-06 23:01:48 -080096}