Linux 5.4.12
[linux/fpc-iii.git] / net / ipv4 / xfrm4_output.c
blobecff3fce9807365123a07f78322ae3d28340514a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * xfrm4_output.c - Common IPsec encapsulation code for IPv4.
4 * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
5 */
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>
12 #include <net/dst.h>
13 #include <net/ip.h>
14 #include <net/xfrm.h>
15 #include <net/icmp.h>
17 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
19 int mtu, ret = 0;
21 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
22 goto out;
24 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
25 goto out;
27 mtu = dst_mtu(skb_dst(skb));
28 if ((!skb_is_gso(skb) && skb->len > mtu) ||
29 (skb_is_gso(skb) &&
30 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
31 skb->protocol = htons(ETH_P_IP);
33 if (skb->sk)
34 xfrm_local_error(skb, mtu);
35 else
36 icmp_send(skb, ICMP_DEST_UNREACH,
37 ICMP_FRAG_NEEDED, htonl(mtu));
38 ret = -EMSGSIZE;
40 out:
41 return ret;
44 int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
46 int err;
48 err = xfrm4_tunnel_check_size(skb);
49 if (err)
50 return err;
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;
63 #endif
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
75 if (!x) {
76 IPCB(skb)->flags |= IPSKB_REROUTED;
77 return dst_output(net, sk, skb);
79 #endif
81 rcu_read_lock();
82 afinfo = xfrm_state_afinfo_get_rcu(x->outer_mode.family);
83 if (likely(afinfo))
84 ret = afinfo->output_finish(sk, skb);
85 else
86 kfree_skb(skb);
87 rcu_read_unlock();
89 return ret;
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,
96 __xfrm4_output,
97 !(IPCB(skb)->flags & IPSKB_REROUTED));
100 void xfrm4_local_error(struct sk_buff *skb, u32 mtu)
102 struct iphdr *hdr;
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);