xref: /linux/net/ipv4/xfrm4_input.c (revision 6fa6b5cb60490db2591bb93872b95f72315e5f53)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xfrm4_input.c
4  *
5  * Changes:
6  *	YOSHIFUJI Hideaki @USAGI
7  *		Split up af-specific portion
8  *	Derek Atkins <derek@ihtfp.com>
9  *		Add Encapsulation support
10  *
11  */
12 
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <net/ip.h>
19 #include <net/xfrm.h>
20 #include <net/protocol.h>
21 #include <net/gro.h>
22 
23 static int xfrm4_rcv_encap_finish2(struct net *net, struct sock *sk,
24 				   struct sk_buff *skb)
25 {
26 	return dst_input(skb);
27 }
28 
29 static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk,
30 					 struct sk_buff *skb)
31 {
32 	if (!skb_dst(skb)) {
33 		const struct iphdr *iph = ip_hdr(skb);
34 
35 		if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
36 					 ip4h_dscp(iph), skb->dev))
37 			goto drop;
38 	}
39 
40 	if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2))
41 		goto drop;
42 
43 	return 0;
44 drop:
45 	kfree_skb(skb);
46 	return NET_RX_DROP;
47 }
48 
49 int xfrm4_transport_finish(struct sk_buff *skb, int async)
50 {
51 	struct xfrm_offload *xo = xfrm_offload(skb);
52 	struct iphdr *iph = ip_hdr(skb);
53 	struct net_device *dev = skb->dev;
54 
55 	iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
56 
57 #ifndef CONFIG_NETFILTER
58 	if (!async)
59 		return -iph->protocol;
60 #endif
61 
62 	__skb_push(skb, -skb_network_offset(skb));
63 	iph->tot_len = htons(skb->len);
64 	ip_send_check(iph);
65 
66 	if (xo && (xo->flags & XFRM_GRO)) {
67 		/* The full l2 header needs to be preserved so that re-injecting the packet at l2
68 		 * works correctly in the presence of vlan tags.
69 		 */
70 		skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
71 		skb_reset_network_header(skb);
72 		skb_reset_transport_header(skb);
73 		return 0;
74 	}
75 
76 	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
77 		dev_net(dev), NULL, skb, dev, NULL,
78 		xfrm4_rcv_encap_finish);
79 	if (async)
80 		dev_put(dev);
81 	return 0;
82 }
83 
84 static int __xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
85 {
86 	struct udp_sock *up = udp_sk(sk);
87 	struct udphdr *uh;
88 	struct iphdr *iph;
89 	int iphlen, len;
90 	__u8 *udpdata;
91 	__be32 *udpdata32;
92 	u16 encap_type;
93 
94 	encap_type = READ_ONCE(up->encap_type);
95 	/* if this is not encapsulated socket, then just return now */
96 	if (!encap_type)
97 		return 1;
98 
99 	/* If this is a paged skb, make sure we pull up
100 	 * whatever data we need to look at. */
101 	len = skb->len - sizeof(struct udphdr);
102 	if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
103 		return 1;
104 
105 	/* Now we can get the pointers */
106 	uh = udp_hdr(skb);
107 	udpdata = (__u8 *)uh + sizeof(struct udphdr);
108 	udpdata32 = (__be32 *)udpdata;
109 
110 	switch (encap_type) {
111 	default:
112 	case UDP_ENCAP_ESPINUDP:
113 		/* Check if this is a keepalive packet.  If so, eat it. */
114 		if (len == 1 && udpdata[0] == 0xff) {
115 			return -EINVAL;
116 		} else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
117 			/* ESP Packet without Non-ESP header */
118 			len = sizeof(struct udphdr);
119 		} else
120 			/* Must be an IKE packet.. pass it through */
121 			return 1;
122 		break;
123 	}
124 
125 	/* At this point we are sure that this is an ESPinUDP packet,
126 	 * so we need to remove 'len' bytes from the packet (the UDP
127 	 * header and optional ESP marker bytes) and then modify the
128 	 * protocol to ESP, and then call into the transform receiver.
129 	 */
130 	if (skb_unclone(skb, GFP_ATOMIC))
131 		return -EINVAL;
132 
133 	/* Now we can update and verify the packet length... */
134 	iph = ip_hdr(skb);
135 	iphlen = iph->ihl << 2;
136 	iph->tot_len = htons(ntohs(iph->tot_len) - len);
137 	if (skb->len < iphlen + len) {
138 		/* packet is too small!?! */
139 		return -EINVAL;
140 	}
141 
142 	/* pull the data buffer up to the ESP header and set the
143 	 * transport header to point to ESP.  Keep UDP on the stack
144 	 * for later.
145 	 */
146 	if (pull) {
147 		__skb_pull(skb, len);
148 		skb_reset_transport_header(skb);
149 	} else {
150 		skb_set_transport_header(skb, len);
151 	}
152 
153 	/* process ESP */
154 	return 0;
155 }
156 
157 /* If it's a keepalive packet, then just eat it.
158  * If it's an encapsulated packet, then pass it to the
159  * IPsec xfrm input.
160  * Returns 0 if skb passed to xfrm or was dropped.
161  * Returns >0 if skb should be passed to UDP.
162  * Returns <0 if skb should be resubmitted (-ret is protocol)
163  */
164 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
165 {
166 	int ret;
167 
168 	ret = __xfrm4_udp_encap_rcv(sk, skb, true);
169 	if (!ret)
170 		return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0,
171 				       udp_sk(sk)->encap_type);
172 
173 	if (ret < 0) {
174 		kfree_skb(skb);
175 		return 0;
176 	}
177 
178 	return ret;
179 }
180 EXPORT_SYMBOL(xfrm4_udp_encap_rcv);
181 
182 struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
183 					struct sk_buff *skb)
184 {
185 	int offset = skb_gro_offset(skb);
186 	const struct net_offload *ops;
187 	struct sk_buff *pp = NULL;
188 	int len, dlen;
189 	__u8 *udpdata;
190 	__be32 *udpdata32;
191 
192 	len = skb->len - offset;
193 	dlen = offset + min(len, 8);
194 	udpdata = skb_gro_header(skb, dlen, offset);
195 	udpdata32 = (__be32 *)udpdata;
196 	if (unlikely(!udpdata))
197 		return NULL;
198 
199 	rcu_read_lock();
200 	ops = rcu_dereference(inet_offloads[IPPROTO_ESP]);
201 	if (!ops || !ops->callbacks.gro_receive)
202 		goto out;
203 
204 	/* check if it is a keepalive or IKE packet */
205 	if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
206 		goto out;
207 
208 	/* set the transport header to ESP */
209 	skb_set_transport_header(skb, offset);
210 
211 	NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
212 
213 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
214 	rcu_read_unlock();
215 
216 	return pp;
217 
218 out:
219 	rcu_read_unlock();
220 	NAPI_GRO_CB(skb)->same_flow = 0;
221 	NAPI_GRO_CB(skb)->flush = 1;
222 
223 	return NULL;
224 }
225 EXPORT_SYMBOL(xfrm4_gro_udp_encap_rcv);
226 
227 int xfrm4_rcv(struct sk_buff *skb)
228 {
229 	return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
230 }
231 EXPORT_SYMBOL(xfrm4_rcv);
232