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 */
6
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>
16
xfrm4_tunnel_check_size(struct sk_buff * skb)17 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
18 {
19 int mtu, ret = 0;
20
21 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
22 goto out;
23
24 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
25 goto out;
26
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);
32
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;
39 }
40 out:
41 return ret;
42 }
43
xfrm4_extract_output(struct xfrm_state * x,struct sk_buff * skb)44 int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
45 {
46 int err;
47
48 err = xfrm4_tunnel_check_size(skb);
49 if (err)
50 return err;
51
52 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
53
54 return xfrm4_extract_header(skb);
55 }
56
xfrm4_output_finish(struct sock * sk,struct sk_buff * skb)57 int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb)
58 {
59 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
60
61 #ifdef CONFIG_NETFILTER
62 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
63 #endif
64
65 return xfrm_output(sk, skb);
66 }
67
__xfrm4_output(struct net * net,struct sock * sk,struct sk_buff * skb)68 static int __xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
69 {
70 struct xfrm_state *x = skb_dst(skb)->xfrm;
71 const struct xfrm_state_afinfo *afinfo;
72 int ret = -EAFNOSUPPORT;
73
74 #ifdef CONFIG_NETFILTER
75 if (!x) {
76 IPCB(skb)->flags |= IPSKB_REROUTED;
77 return dst_output(net, sk, skb);
78 }
79 #endif
80
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();
88
89 return ret;
90 }
91
xfrm4_output(struct net * net,struct sock * sk,struct sk_buff * skb)92 int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb)
93 {
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));
98 }
99
xfrm4_local_error(struct sk_buff * skb,u32 mtu)100 void xfrm4_local_error(struct sk_buff *skb, u32 mtu)
101 {
102 struct iphdr *hdr;
103
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);
107 }
108