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 
xfrm4_rcv_encap_finish2(struct net * net,struct sock * sk,struct sk_buff * skb)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 
xfrm4_rcv_encap_finish(struct net * net,struct sock * sk,struct sk_buff * skb)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 					 iph->tos, 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 
xfrm4_transport_finish(struct sk_buff * skb,int async)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 
54 	iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
55 
56 #ifndef CONFIG_NETFILTER
57 	if (!async)
58 		return -iph->protocol;
59 #endif
60 
61 	__skb_push(skb, -skb_network_offset(skb));
62 	iph->tot_len = htons(skb->len);
63 	ip_send_check(iph);
64 
65 	if (xo && (xo->flags & XFRM_GRO)) {
66 		/* The full l2 header needs to be preserved so that re-injecting the packet at l2
67 		 * works correctly in the presence of vlan tags.
68 		 */
69 		skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
70 		skb_reset_network_header(skb);
71 		skb_reset_transport_header(skb);
72 		return 0;
73 	}
74 
75 	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
76 		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
77 		xfrm4_rcv_encap_finish);
78 	return 0;
79 }
80 
__xfrm4_udp_encap_rcv(struct sock * sk,struct sk_buff * skb,bool pull)81 static int __xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
82 {
83 	struct udp_sock *up = udp_sk(sk);
84 	struct udphdr *uh;
85 	struct iphdr *iph;
86 	int iphlen, len;
87 	__u8 *udpdata;
88 	__be32 *udpdata32;
89 	u16 encap_type;
90 
91 	encap_type = READ_ONCE(up->encap_type);
92 	/* if this is not encapsulated socket, then just return now */
93 	if (!encap_type)
94 		return 1;
95 
96 	/* If this is a paged skb, make sure we pull up
97 	 * whatever data we need to look at. */
98 	len = skb->len - sizeof(struct udphdr);
99 	if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
100 		return 1;
101 
102 	/* Now we can get the pointers */
103 	uh = udp_hdr(skb);
104 	udpdata = (__u8 *)uh + sizeof(struct udphdr);
105 	udpdata32 = (__be32 *)udpdata;
106 
107 	switch (encap_type) {
108 	default:
109 	case UDP_ENCAP_ESPINUDP:
110 		/* Check if this is a keepalive packet.  If so, eat it. */
111 		if (len == 1 && udpdata[0] == 0xff) {
112 			return -EINVAL;
113 		} else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
114 			/* ESP Packet without Non-ESP header */
115 			len = sizeof(struct udphdr);
116 		} else
117 			/* Must be an IKE packet.. pass it through */
118 			return 1;
119 		break;
120 	}
121 
122 	/* At this point we are sure that this is an ESPinUDP packet,
123 	 * so we need to remove 'len' bytes from the packet (the UDP
124 	 * header and optional ESP marker bytes) and then modify the
125 	 * protocol to ESP, and then call into the transform receiver.
126 	 */
127 	if (skb_unclone(skb, GFP_ATOMIC))
128 		return -EINVAL;
129 
130 	/* Now we can update and verify the packet length... */
131 	iph = ip_hdr(skb);
132 	iphlen = iph->ihl << 2;
133 	iph->tot_len = htons(ntohs(iph->tot_len) - len);
134 	if (skb->len < iphlen + len) {
135 		/* packet is too small!?! */
136 		return -EINVAL;
137 	}
138 
139 	/* pull the data buffer up to the ESP header and set the
140 	 * transport header to point to ESP.  Keep UDP on the stack
141 	 * for later.
142 	 */
143 	if (pull) {
144 		__skb_pull(skb, len);
145 		skb_reset_transport_header(skb);
146 	} else {
147 		skb_set_transport_header(skb, len);
148 	}
149 
150 	/* process ESP */
151 	return 0;
152 }
153 
154 /* If it's a keepalive packet, then just eat it.
155  * If it's an encapsulated packet, then pass it to the
156  * IPsec xfrm input.
157  * Returns 0 if skb passed to xfrm or was dropped.
158  * Returns >0 if skb should be passed to UDP.
159  * Returns <0 if skb should be resubmitted (-ret is protocol)
160  */
xfrm4_udp_encap_rcv(struct sock * sk,struct sk_buff * skb)161 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
162 {
163 	int ret;
164 
165 	ret = __xfrm4_udp_encap_rcv(sk, skb, true);
166 	if (!ret)
167 		return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0,
168 				       udp_sk(sk)->encap_type);
169 
170 	if (ret < 0) {
171 		kfree_skb(skb);
172 		return 0;
173 	}
174 
175 	return ret;
176 }
177 EXPORT_SYMBOL(xfrm4_udp_encap_rcv);
178 
xfrm4_gro_udp_encap_rcv(struct sock * sk,struct list_head * head,struct sk_buff * skb)179 struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
180 					struct sk_buff *skb)
181 {
182 	int offset = skb_gro_offset(skb);
183 	const struct net_offload *ops;
184 	struct sk_buff *pp = NULL;
185 	int ret;
186 
187 	offset = offset - sizeof(struct udphdr);
188 
189 	if (!pskb_pull(skb, offset))
190 		return NULL;
191 
192 	rcu_read_lock();
193 	ops = rcu_dereference(inet_offloads[IPPROTO_ESP]);
194 	if (!ops || !ops->callbacks.gro_receive)
195 		goto out;
196 
197 	ret = __xfrm4_udp_encap_rcv(sk, skb, false);
198 	if (ret)
199 		goto out;
200 
201 	skb_push(skb, offset);
202 	NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
203 
204 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
205 	rcu_read_unlock();
206 
207 	return pp;
208 
209 out:
210 	rcu_read_unlock();
211 	skb_push(skb, offset);
212 	NAPI_GRO_CB(skb)->same_flow = 0;
213 	NAPI_GRO_CB(skb)->flush = 1;
214 
215 	return NULL;
216 }
217 EXPORT_SYMBOL(xfrm4_gro_udp_encap_rcv);
218 
xfrm4_rcv(struct sk_buff * skb)219 int xfrm4_rcv(struct sk_buff *skb)
220 {
221 	return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
222 }
223 EXPORT_SYMBOL(xfrm4_rcv);
224