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
xfrm6_local_rxpmtu(struct sk_buff * skb,u32 mtu)19 void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
20 {
21 struct flowi6 fl6;
22 struct sock *sk = skb_to_full_sk(skb);
23
24 if (!sk)
25 return;
26
27 fl6.flowi6_oif = sk->sk_bound_dev_if;
28 fl6.daddr = ipv6_hdr(skb)->daddr;
29
30 ipv6_local_rxpmtu(sk, &fl6, mtu);
31 }
32
xfrm6_local_error(struct sk_buff * skb,u32 mtu)33 void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
34 {
35 struct flowi6 fl6;
36 const struct ipv6hdr *hdr;
37 struct sock *sk = skb_to_full_sk(skb);
38
39 if (!sk)
40 return;
41
42 hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
43 fl6.fl6_dport = inet_sk(sk)->inet_dport;
44 fl6.daddr = hdr->daddr;
45
46 ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
47 }
48
__xfrm6_output_finish(struct net * net,struct sock * sk,struct sk_buff * skb)49 static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
50 {
51 return xfrm_output(sk, skb);
52 }
53
xfrm6_noneed_fragment(struct sk_buff * skb)54 static int xfrm6_noneed_fragment(struct sk_buff *skb)
55 {
56 struct frag_hdr *fh;
57 u8 prevhdr = ipv6_hdr(skb)->nexthdr;
58
59 if (prevhdr != NEXTHDR_FRAGMENT)
60 return 0;
61 fh = (struct frag_hdr *)(skb->data + sizeof(struct ipv6hdr));
62 if (fh->nexthdr == NEXTHDR_ESP || fh->nexthdr == NEXTHDR_AUTH)
63 return 1;
64 return 0;
65 }
66
__xfrm6_output(struct net * net,struct sock * sk,struct sk_buff * skb)67 static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
68 {
69 struct dst_entry *dst = skb_dst(skb);
70 struct xfrm_state *x = dst->xfrm;
71 unsigned int mtu;
72 bool toobig;
73
74 #ifdef CONFIG_NETFILTER
75 if (!x) {
76 IP6CB(skb)->flags |= IP6SKB_REROUTED;
77 return dst_output(net, sk, skb);
78 }
79 #endif
80
81 if (x->props.mode != XFRM_MODE_TUNNEL)
82 goto skip_frag;
83
84 if (skb->protocol == htons(ETH_P_IPV6))
85 mtu = ip6_skb_dst_mtu(skb);
86 else
87 mtu = dst_mtu(skb_dst(skb));
88
89 toobig = skb->len > mtu && !skb_is_gso(skb);
90
91 if (toobig && xfrm6_local_dontfrag(sk)) {
92 xfrm6_local_rxpmtu(skb, mtu);
93 kfree_skb(skb);
94 return -EMSGSIZE;
95 } else if (toobig && xfrm6_noneed_fragment(skb)) {
96 skb->ignore_df = 1;
97 goto skip_frag;
98 } else if (!skb->ignore_df && toobig && sk) {
99 xfrm_local_error(skb, mtu);
100 kfree_skb(skb);
101 return -EMSGSIZE;
102 }
103
104 if (toobig)
105 return ip6_fragment(net, sk, skb,
106 __xfrm6_output_finish);
107
108 skip_frag:
109 return xfrm_output(sk, skb);
110 }
111
xfrm6_output(struct net * net,struct sock * sk,struct sk_buff * skb)112 int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
113 {
114 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
115 net, sk, skb, skb->dev, skb_dst_dev(skb),
116 __xfrm6_output,
117 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
118 }
119