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