xref: /linux/net/ipv4/xfrm4_input.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * xfrm4_input.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Changes:
61da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
71da177e4SLinus Torvalds  *		Split up af-specific portion
81da177e4SLinus Torvalds  *	Derek Atkins <derek@ihtfp.com>
91da177e4SLinus Torvalds  *		Add Encapsulation support
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
135a0e3ad6STejun Heo #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/string.h>
16b05e1066SPatrick McHardy #include <linux/netfilter.h>
17b05e1066SPatrick McHardy #include <linux/netfilter_ipv4.h>
181da177e4SLinus Torvalds #include <net/ip.h>
191da177e4SLinus Torvalds #include <net/xfrm.h>
20172bf009SSteffen Klassert #include <net/protocol.h>
21172bf009SSteffen Klassert #include <net/gro.h>
221da177e4SLinus Torvalds 
xfrm4_rcv_encap_finish2(struct net * net,struct sock * sk,struct sk_buff * skb)23acf568eeSHerbert Xu static int xfrm4_rcv_encap_finish2(struct net *net, struct sock *sk,
24acf568eeSHerbert Xu 				   struct sk_buff *skb)
25acf568eeSHerbert Xu {
26acf568eeSHerbert Xu 	return dst_input(skb);
27acf568eeSHerbert Xu }
28acf568eeSHerbert Xu 
xfrm4_rcv_encap_finish(struct net * net,struct sock * sk,struct sk_buff * skb)290c4b51f0SEric W. Biederman static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk,
300c4b51f0SEric W. Biederman 					 struct sk_buff *skb)
31b05e1066SPatrick McHardy {
3251456b29SIan Morris 	if (!skb_dst(skb)) {
33eddc9ec5SArnaldo Carvalho de Melo 		const struct iphdr *iph = ip_hdr(skb);
34eddc9ec5SArnaldo Carvalho de Melo 
35c6cffba4SDavid S. Miller 		if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
36c10237e0SDavid S. Miller 					 iph->tos, skb->dev))
37b05e1066SPatrick McHardy 			goto drop;
38b05e1066SPatrick McHardy 	}
39acf568eeSHerbert Xu 
40acf568eeSHerbert Xu 	if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2))
41acf568eeSHerbert Xu 		goto drop;
42acf568eeSHerbert Xu 
43acf568eeSHerbert Xu 	return 0;
44b05e1066SPatrick McHardy drop:
45b05e1066SPatrick McHardy 	kfree_skb(skb);
46b05e1066SPatrick McHardy 	return NET_RX_DROP;
47b05e1066SPatrick McHardy }
48b05e1066SPatrick McHardy 
xfrm4_transport_finish(struct sk_buff * skb,int async)49716062fdSHerbert Xu int xfrm4_transport_finish(struct sk_buff *skb, int async)
50716062fdSHerbert Xu {
517785bba2SSteffen Klassert 	struct xfrm_offload *xo = xfrm_offload(skb);
5260d5fcfbSHerbert Xu 	struct iphdr *iph = ip_hdr(skb);
5360d5fcfbSHerbert Xu 
5460d5fcfbSHerbert Xu 	iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
5560d5fcfbSHerbert Xu 
560883ae0eSHerbert Xu #ifndef CONFIG_NETFILTER
570883ae0eSHerbert Xu 	if (!async)
580883ae0eSHerbert Xu 		return -iph->protocol;
590883ae0eSHerbert Xu #endif
600883ae0eSHerbert Xu 
6180bfab79SEric Dumazet 	__skb_push(skb, -skb_network_offset(skb));
6260d5fcfbSHerbert Xu 	iph->tot_len = htons(skb->len);
6360d5fcfbSHerbert Xu 	ip_send_check(iph);
64b05e1066SPatrick McHardy 
657785bba2SSteffen Klassert 	if (xo && (xo->flags & XFRM_GRO)) {
66*58fbfecaSPaul Davey 		/* The full l2 header needs to be preserved so that re-injecting the packet at l2
67*58fbfecaSPaul Davey 		 * works correctly in the presence of vlan tags.
68*58fbfecaSPaul Davey 		 */
69*58fbfecaSPaul Davey 		skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
70*58fbfecaSPaul Davey 		skb_reset_network_header(skb);
71bfc0698bSSowmini Varadhan 		skb_reset_transport_header(skb);
727785bba2SSteffen Klassert 		return 0;
737785bba2SSteffen Klassert 	}
747785bba2SSteffen Klassert 
7529a26a56SEric W. Biederman 	NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
7629a26a56SEric W. Biederman 		dev_net(skb->dev), NULL, skb, skb->dev, NULL,
77b05e1066SPatrick McHardy 		xfrm4_rcv_encap_finish);
78b05e1066SPatrick McHardy 	return 0;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
__xfrm4_udp_encap_rcv(struct sock * sk,struct sk_buff * skb,bool pull)81172bf009SSteffen Klassert static int __xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
82067b207bSJames Chapman {
83067b207bSJames Chapman 	struct udp_sock *up = udp_sk(sk);
84067b207bSJames Chapman 	struct udphdr *uh;
85067b207bSJames Chapman 	struct iphdr *iph;
86067b207bSJames Chapman 	int iphlen, len;
87067b207bSJames Chapman 	__u8 *udpdata;
88067b207bSJames Chapman 	__be32 *udpdata32;
8970a36f57SEric Dumazet 	u16 encap_type;
90067b207bSJames Chapman 
9170a36f57SEric Dumazet 	encap_type = READ_ONCE(up->encap_type);
92067b207bSJames Chapman 	/* if this is not encapsulated socket, then just return now */
93067b207bSJames Chapman 	if (!encap_type)
94067b207bSJames Chapman 		return 1;
95067b207bSJames Chapman 
96067b207bSJames Chapman 	/* If this is a paged skb, make sure we pull up
97067b207bSJames Chapman 	 * whatever data we need to look at. */
98067b207bSJames Chapman 	len = skb->len - sizeof(struct udphdr);
99067b207bSJames Chapman 	if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
100067b207bSJames Chapman 		return 1;
101067b207bSJames Chapman 
102067b207bSJames Chapman 	/* Now we can get the pointers */
103067b207bSJames Chapman 	uh = udp_hdr(skb);
104067b207bSJames Chapman 	udpdata = (__u8 *)uh + sizeof(struct udphdr);
105067b207bSJames Chapman 	udpdata32 = (__be32 *)udpdata;
106067b207bSJames Chapman 
107067b207bSJames Chapman 	switch (encap_type) {
108067b207bSJames Chapman 	default:
109067b207bSJames Chapman 	case UDP_ENCAP_ESPINUDP:
110067b207bSJames Chapman 		/* Check if this is a keepalive packet.  If so, eat it. */
111067b207bSJames Chapman 		if (len == 1 && udpdata[0] == 0xff) {
112172bf009SSteffen Klassert 			return -EINVAL;
113067b207bSJames Chapman 		} else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
114067b207bSJames Chapman 			/* ESP Packet without Non-ESP header */
115067b207bSJames Chapman 			len = sizeof(struct udphdr);
116067b207bSJames Chapman 		} else
117067b207bSJames Chapman 			/* Must be an IKE packet.. pass it through */
118067b207bSJames Chapman 			return 1;
119067b207bSJames Chapman 		break;
120067b207bSJames Chapman 	}
121067b207bSJames Chapman 
122067b207bSJames Chapman 	/* At this point we are sure that this is an ESPinUDP packet,
123067b207bSJames Chapman 	 * so we need to remove 'len' bytes from the packet (the UDP
124067b207bSJames Chapman 	 * header and optional ESP marker bytes) and then modify the
125067b207bSJames Chapman 	 * protocol to ESP, and then call into the transform receiver.
126067b207bSJames Chapman 	 */
12714bbd6a5SPravin B Shelar 	if (skb_unclone(skb, GFP_ATOMIC))
128172bf009SSteffen Klassert 		return -EINVAL;
129067b207bSJames Chapman 
130067b207bSJames Chapman 	/* Now we can update and verify the packet length... */
131067b207bSJames Chapman 	iph = ip_hdr(skb);
132067b207bSJames Chapman 	iphlen = iph->ihl << 2;
133067b207bSJames Chapman 	iph->tot_len = htons(ntohs(iph->tot_len) - len);
134067b207bSJames Chapman 	if (skb->len < iphlen + len) {
135067b207bSJames Chapman 		/* packet is too small!?! */
136172bf009SSteffen Klassert 		return -EINVAL;
137067b207bSJames Chapman 	}
138067b207bSJames Chapman 
139067b207bSJames Chapman 	/* pull the data buffer up to the ESP header and set the
140067b207bSJames Chapman 	 * transport header to point to ESP.  Keep UDP on the stack
141067b207bSJames Chapman 	 * for later.
142067b207bSJames Chapman 	 */
143172bf009SSteffen Klassert 	if (pull) {
144067b207bSJames Chapman 		__skb_pull(skb, len);
145067b207bSJames Chapman 		skb_reset_transport_header(skb);
146172bf009SSteffen Klassert 	} else {
147172bf009SSteffen Klassert 		skb_set_transport_header(skb, len);
148172bf009SSteffen Klassert 	}
149067b207bSJames Chapman 
150067b207bSJames Chapman 	/* process ESP */
151067b207bSJames Chapman 	return 0;
152067b207bSJames Chapman }
153067b207bSJames Chapman 
154172bf009SSteffen Klassert /* If it's a keepalive packet, then just eat it.
155172bf009SSteffen Klassert  * If it's an encapsulated packet, then pass it to the
156172bf009SSteffen Klassert  * IPsec xfrm input.
157172bf009SSteffen Klassert  * Returns 0 if skb passed to xfrm or was dropped.
158172bf009SSteffen Klassert  * Returns >0 if skb should be passed to UDP.
159172bf009SSteffen Klassert  * Returns <0 if skb should be resubmitted (-ret is protocol)
160172bf009SSteffen Klassert  */
xfrm4_udp_encap_rcv(struct sock * sk,struct sk_buff * skb)161172bf009SSteffen Klassert int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
162172bf009SSteffen Klassert {
163172bf009SSteffen Klassert 	int ret;
164172bf009SSteffen Klassert 
165172bf009SSteffen Klassert 	ret = __xfrm4_udp_encap_rcv(sk, skb, true);
166172bf009SSteffen Klassert 	if (!ret)
167172bf009SSteffen Klassert 		return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0,
168172bf009SSteffen Klassert 				       udp_sk(sk)->encap_type);
169172bf009SSteffen Klassert 
170172bf009SSteffen Klassert 	if (ret < 0) {
171172bf009SSteffen Klassert 		kfree_skb(skb);
172172bf009SSteffen Klassert 		return 0;
173172bf009SSteffen Klassert 	}
174172bf009SSteffen Klassert 
175172bf009SSteffen Klassert 	return ret;
176172bf009SSteffen Klassert }
17753a5b4f2SSteffen Klassert EXPORT_SYMBOL(xfrm4_udp_encap_rcv);
178172bf009SSteffen Klassert 
xfrm4_gro_udp_encap_rcv(struct sock * sk,struct list_head * head,struct sk_buff * skb)179172bf009SSteffen Klassert struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
180172bf009SSteffen Klassert 					struct sk_buff *skb)
181172bf009SSteffen Klassert {
182172bf009SSteffen Klassert 	int offset = skb_gro_offset(skb);
183172bf009SSteffen Klassert 	const struct net_offload *ops;
184172bf009SSteffen Klassert 	struct sk_buff *pp = NULL;
185172bf009SSteffen Klassert 	int ret;
186172bf009SSteffen Klassert 
187172bf009SSteffen Klassert 	offset = offset - sizeof(struct udphdr);
188172bf009SSteffen Klassert 
189172bf009SSteffen Klassert 	if (!pskb_pull(skb, offset))
190172bf009SSteffen Klassert 		return NULL;
191172bf009SSteffen Klassert 
192172bf009SSteffen Klassert 	rcu_read_lock();
193172bf009SSteffen Klassert 	ops = rcu_dereference(inet_offloads[IPPROTO_ESP]);
194172bf009SSteffen Klassert 	if (!ops || !ops->callbacks.gro_receive)
195172bf009SSteffen Klassert 		goto out;
196172bf009SSteffen Klassert 
197172bf009SSteffen Klassert 	ret = __xfrm4_udp_encap_rcv(sk, skb, false);
198172bf009SSteffen Klassert 	if (ret)
199172bf009SSteffen Klassert 		goto out;
200172bf009SSteffen Klassert 
201172bf009SSteffen Klassert 	skb_push(skb, offset);
202172bf009SSteffen Klassert 	NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
203172bf009SSteffen Klassert 
204172bf009SSteffen Klassert 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
205172bf009SSteffen Klassert 	rcu_read_unlock();
206172bf009SSteffen Klassert 
207172bf009SSteffen Klassert 	return pp;
208172bf009SSteffen Klassert 
209172bf009SSteffen Klassert out:
210172bf009SSteffen Klassert 	rcu_read_unlock();
211172bf009SSteffen Klassert 	skb_push(skb, offset);
212172bf009SSteffen Klassert 	NAPI_GRO_CB(skb)->same_flow = 0;
213172bf009SSteffen Klassert 	NAPI_GRO_CB(skb)->flush = 1;
214172bf009SSteffen Klassert 
215172bf009SSteffen Klassert 	return NULL;
216172bf009SSteffen Klassert }
21753a5b4f2SSteffen Klassert EXPORT_SYMBOL(xfrm4_gro_udp_encap_rcv);
218172bf009SSteffen Klassert 
xfrm4_rcv(struct sk_buff * skb)219067b207bSJames Chapman int xfrm4_rcv(struct sk_buff *skb)
220067b207bSJames Chapman {
221c4541b41SHerbert Xu 	return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
222067b207bSJames Chapman }
223067b207bSJames Chapman EXPORT_SYMBOL(xfrm4_rcv);
224