xref: /openbmc/linux/net/ipv6/xfrm6_output.c (revision 91b0383f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
6  */
7 
8 #include <linux/if_ether.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/icmpv6.h>
13 #include <linux/netfilter_ipv6.h>
14 #include <net/dst.h>
15 #include <net/ipv6.h>
16 #include <net/ip6_route.h>
17 #include <net/xfrm.h>
18 
19 void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
20 {
21 	struct flowi6 fl6;
22 	struct sock *sk = skb->sk;
23 
24 	fl6.flowi6_oif = sk->sk_bound_dev_if;
25 	fl6.daddr = ipv6_hdr(skb)->daddr;
26 
27 	ipv6_local_rxpmtu(sk, &fl6, mtu);
28 }
29 
30 void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
31 {
32 	struct flowi6 fl6;
33 	const struct ipv6hdr *hdr;
34 	struct sock *sk = skb->sk;
35 
36 	hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
37 	fl6.fl6_dport = inet_sk(sk)->inet_dport;
38 	fl6.daddr = hdr->daddr;
39 
40 	ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
41 }
42 
43 static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
44 {
45 	return xfrm_output(sk, skb);
46 }
47 
48 static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
49 {
50 	struct dst_entry *dst = skb_dst(skb);
51 	struct xfrm_state *x = dst->xfrm;
52 	unsigned int mtu;
53 	bool toobig;
54 
55 #ifdef CONFIG_NETFILTER
56 	if (!x) {
57 		IP6CB(skb)->flags |= IP6SKB_REROUTED;
58 		return dst_output(net, sk, skb);
59 	}
60 #endif
61 
62 	if (x->props.mode != XFRM_MODE_TUNNEL)
63 		goto skip_frag;
64 
65 	if (skb->protocol == htons(ETH_P_IPV6))
66 		mtu = ip6_skb_dst_mtu(skb);
67 	else
68 		mtu = dst_mtu(skb_dst(skb));
69 
70 	toobig = skb->len > mtu && !skb_is_gso(skb);
71 
72 	if (toobig && xfrm6_local_dontfrag(skb->sk)) {
73 		xfrm6_local_rxpmtu(skb, mtu);
74 		kfree_skb(skb);
75 		return -EMSGSIZE;
76 	} else if (!skb->ignore_df && toobig && skb->sk) {
77 		xfrm_local_error(skb, mtu);
78 		kfree_skb(skb);
79 		return -EMSGSIZE;
80 	}
81 
82 	if (toobig || dst_allfrag(skb_dst(skb)))
83 		return ip6_fragment(net, sk, skb,
84 				    __xfrm6_output_finish);
85 
86 skip_frag:
87 	return xfrm_output(sk, skb);
88 }
89 
90 int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
91 {
92 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
93 			    net, sk, skb,  skb->dev, skb_dst(skb)->dev,
94 			    __xfrm6_output,
95 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
96 }
97