1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
4 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
7 #include <linux/if_ether.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <linux/netfilter_ipv4.h>
17 static int xfrm4_tunnel_check_size(struct sk_buff
*skb
)
21 if (IPCB(skb
)->flags
& IPSKB_XFRM_TUNNEL_SIZE
)
24 if (!(ip_hdr(skb
)->frag_off
& htons(IP_DF
)) || skb
->ignore_df
)
27 mtu
= dst_mtu(skb_dst(skb
));
28 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
30 !skb_gso_validate_network_len(skb
, ip_skb_dst_mtu(skb
->sk
, skb
)))) {
31 skb
->protocol
= htons(ETH_P_IP
);
34 xfrm_local_error(skb
, mtu
);
36 icmp_send(skb
, ICMP_DEST_UNREACH
,
37 ICMP_FRAG_NEEDED
, htonl(mtu
));
44 int xfrm4_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
48 err
= xfrm4_tunnel_check_size(skb
);
52 XFRM_MODE_SKB_CB(skb
)->protocol
= ip_hdr(skb
)->protocol
;
54 return xfrm4_extract_header(skb
);
57 int xfrm4_output_finish(struct sock
*sk
, struct sk_buff
*skb
)
59 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
61 #ifdef CONFIG_NETFILTER
62 IPCB(skb
)->flags
|= IPSKB_XFRM_TRANSFORMED
;
65 return xfrm_output(sk
, skb
);
68 static int __xfrm4_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
70 struct xfrm_state
*x
= skb_dst(skb
)->xfrm
;
71 const struct xfrm_state_afinfo
*afinfo
;
72 int ret
= -EAFNOSUPPORT
;
74 #ifdef CONFIG_NETFILTER
76 IPCB(skb
)->flags
|= IPSKB_REROUTED
;
77 return dst_output(net
, sk
, skb
);
82 afinfo
= xfrm_state_afinfo_get_rcu(x
->outer_mode
.family
);
84 ret
= afinfo
->output_finish(sk
, skb
);
92 int xfrm4_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
94 return NF_HOOK_COND(NFPROTO_IPV4
, NF_INET_POST_ROUTING
,
95 net
, sk
, skb
, NULL
, skb_dst(skb
)->dev
,
97 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));
100 void xfrm4_local_error(struct sk_buff
*skb
, u32 mtu
)
104 hdr
= skb
->encapsulation
? inner_ip_hdr(skb
) : ip_hdr(skb
);
105 ip_local_error(skb
->sk
, EMSGSIZE
, hdr
->daddr
,
106 inet_sk(skb
->sk
)->inet_dport
, mtu
);