1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
16 #include <net/ip6_route.h>
19 int xfrm6_find_1stfragopt(struct xfrm_state
*x
, struct sk_buff
*skb
,
22 return ip6_find_1stfragopt(skb
, prevhdr
);
24 EXPORT_SYMBOL(xfrm6_find_1stfragopt
);
26 static int xfrm6_local_dontfrag(struct sk_buff
*skb
)
29 struct sock
*sk
= skb
->sk
;
32 if (sk
->sk_family
!= AF_INET6
)
35 proto
= sk
->sk_protocol
;
36 if (proto
== IPPROTO_UDP
|| proto
== IPPROTO_RAW
)
37 return inet6_sk(sk
)->dontfrag
;
43 static void xfrm6_local_rxpmtu(struct sk_buff
*skb
, u32 mtu
)
46 struct sock
*sk
= skb
->sk
;
48 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
49 fl6
.daddr
= ipv6_hdr(skb
)->daddr
;
51 ipv6_local_rxpmtu(sk
, &fl6
, mtu
);
54 void xfrm6_local_error(struct sk_buff
*skb
, u32 mtu
)
57 const struct ipv6hdr
*hdr
;
58 struct sock
*sk
= skb
->sk
;
60 hdr
= skb
->encapsulation
? inner_ipv6_hdr(skb
) : ipv6_hdr(skb
);
61 fl6
.fl6_dport
= inet_sk(sk
)->inet_dport
;
62 fl6
.daddr
= hdr
->daddr
;
64 ipv6_local_error(sk
, EMSGSIZE
, &fl6
, mtu
);
67 static int xfrm6_tunnel_check_size(struct sk_buff
*skb
)
70 struct dst_entry
*dst
= skb_dst(skb
);
76 if (mtu
< IPV6_MIN_MTU
)
79 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
81 !skb_gso_validate_network_len(skb
, ip6_skb_dst_mtu(skb
)))) {
83 skb
->protocol
= htons(ETH_P_IPV6
);
85 if (xfrm6_local_dontfrag(skb
))
86 xfrm6_local_rxpmtu(skb
, mtu
);
88 xfrm_local_error(skb
, mtu
);
90 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
97 int xfrm6_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
101 err
= xfrm6_tunnel_check_size(skb
);
105 XFRM_MODE_SKB_CB(skb
)->protocol
= ipv6_hdr(skb
)->nexthdr
;
107 return xfrm6_extract_header(skb
);
110 int xfrm6_output_finish(struct sock
*sk
, struct sk_buff
*skb
)
112 memset(IP6CB(skb
), 0, sizeof(*IP6CB(skb
)));
114 #ifdef CONFIG_NETFILTER
115 IP6CB(skb
)->flags
|= IP6SKB_XFRM_TRANSFORMED
;
118 return xfrm_output(sk
, skb
);
121 static int __xfrm6_output_state_finish(struct xfrm_state
*x
, struct sock
*sk
,
124 const struct xfrm_state_afinfo
*afinfo
;
125 int ret
= -EAFNOSUPPORT
;
128 afinfo
= xfrm_state_afinfo_get_rcu(x
->outer_mode
.family
);
130 ret
= afinfo
->output_finish(sk
, skb
);
138 static int __xfrm6_output_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
140 struct xfrm_state
*x
= skb_dst(skb
)->xfrm
;
142 return __xfrm6_output_state_finish(x
, sk
, skb
);
145 static int __xfrm6_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
147 struct dst_entry
*dst
= skb_dst(skb
);
148 struct xfrm_state
*x
= dst
->xfrm
;
152 #ifdef CONFIG_NETFILTER
154 IP6CB(skb
)->flags
|= IP6SKB_REROUTED
;
155 return dst_output(net
, sk
, skb
);
159 if (x
->props
.mode
!= XFRM_MODE_TUNNEL
)
162 if (skb
->protocol
== htons(ETH_P_IPV6
))
163 mtu
= ip6_skb_dst_mtu(skb
);
165 mtu
= dst_mtu(skb_dst(skb
));
167 toobig
= skb
->len
> mtu
&& !skb_is_gso(skb
);
169 if (toobig
&& xfrm6_local_dontfrag(skb
)) {
170 xfrm6_local_rxpmtu(skb
, mtu
);
173 } else if (!skb
->ignore_df
&& toobig
&& skb
->sk
) {
174 xfrm_local_error(skb
, mtu
);
179 if (toobig
|| dst_allfrag(skb_dst(skb
)))
180 return ip6_fragment(net
, sk
, skb
,
181 __xfrm6_output_finish
);
184 return __xfrm6_output_state_finish(x
, sk
, skb
);
187 int xfrm6_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
189 return NF_HOOK_COND(NFPROTO_IPV6
, NF_INET_POST_ROUTING
,
190 net
, sk
, skb
, skb
->dev
, skb_dst(skb
)->dev
,
192 !(IP6CB(skb
)->flags
& IP6SKB_REROUTED
));