xref: /linux/net/xfrm/xfrm_input.c (revision a55f7f5f29b32c2c53cc291899cf9b0c25a07f7c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xfrm_input.c
4  *
5  * Changes:
6  * 	YOSHIFUJI Hideaki @USAGI
7  * 		Split up af-specific portion
8  *
9  */
10 
11 #include <linux/bottom_half.h>
12 #include <linux/cache.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/percpu.h>
18 #include <net/dst.h>
19 #include <net/ip.h>
20 #include <net/xfrm.h>
21 #include <net/ip_tunnels.h>
22 #include <net/ip6_tunnel.h>
23 #include <net/dst_metadata.h>
24 #include <net/hotdata.h>
25 
26 #include "xfrm_inout.h"
27 
28 struct xfrm_trans_tasklet {
29 	struct work_struct work;
30 	spinlock_t queue_lock;
31 	struct sk_buff_head queue;
32 };
33 
34 struct xfrm_trans_cb {
35 	union {
36 		struct inet_skb_parm	h4;
37 #if IS_ENABLED(CONFIG_IPV6)
38 		struct inet6_skb_parm	h6;
39 #endif
40 	} header;
41 	int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb);
42 	struct net *net;
43 };
44 
45 #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
46 
47 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
48 static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[2][AF_INET6 + 1];
49 
50 static struct gro_cells gro_cells;
51 static struct net_device *xfrm_napi_dev;
52 
53 static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet);
54 
xfrm_input_register_afinfo(const struct xfrm_input_afinfo * afinfo)55 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
56 {
57 	int err = 0;
58 
59 	if (WARN_ON(afinfo->family > AF_INET6))
60 		return -EAFNOSUPPORT;
61 
62 	spin_lock_bh(&xfrm_input_afinfo_lock);
63 	if (unlikely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family]))
64 		err = -EEXIST;
65 	else
66 		rcu_assign_pointer(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], afinfo);
67 	spin_unlock_bh(&xfrm_input_afinfo_lock);
68 	return err;
69 }
70 EXPORT_SYMBOL(xfrm_input_register_afinfo);
71 
xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo * afinfo)72 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
73 {
74 	int err = 0;
75 
76 	spin_lock_bh(&xfrm_input_afinfo_lock);
77 	if (likely(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family])) {
78 		const struct xfrm_input_afinfo *cur;
79 
80 		cur = rcu_access_pointer(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family]);
81 		if (unlikely(cur != afinfo))
82 			err = -EINVAL;
83 		else
84 			RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->is_ipip][afinfo->family], NULL);
85 	}
86 	spin_unlock_bh(&xfrm_input_afinfo_lock);
87 	synchronize_rcu();
88 	return err;
89 }
90 EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
91 
xfrm_input_get_afinfo(u8 family,bool is_ipip)92 static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(u8 family, bool is_ipip)
93 {
94 	const struct xfrm_input_afinfo *afinfo;
95 
96 	if (WARN_ON_ONCE(family > AF_INET6))
97 		return NULL;
98 
99 	rcu_read_lock();
100 	afinfo = rcu_dereference(xfrm_input_afinfo[is_ipip][family]);
101 	if (unlikely(!afinfo))
102 		rcu_read_unlock();
103 	return afinfo;
104 }
105 
xfrm_rcv_cb(struct sk_buff * skb,unsigned int family,u8 protocol,int err)106 static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
107 		       int err)
108 {
109 	bool is_ipip = (protocol == IPPROTO_IPIP || protocol == IPPROTO_IPV6);
110 	const struct xfrm_input_afinfo *afinfo;
111 	int ret;
112 
113 	afinfo = xfrm_input_get_afinfo(family, is_ipip);
114 	if (!afinfo)
115 		return -EAFNOSUPPORT;
116 
117 	ret = afinfo->callback(skb, protocol, err);
118 	rcu_read_unlock();
119 
120 	return ret;
121 }
122 
secpath_set(struct sk_buff * skb)123 struct sec_path *secpath_set(struct sk_buff *skb)
124 {
125 	struct sec_path *sp, *tmp = skb_ext_find(skb, SKB_EXT_SEC_PATH);
126 
127 	sp = skb_ext_add(skb, SKB_EXT_SEC_PATH);
128 	if (!sp)
129 		return NULL;
130 
131 	if (tmp) /* reused existing one (was COW'd if needed) */
132 		return sp;
133 
134 	/* allocated new secpath */
135 	memset(sp->ovec, 0, sizeof(sp->ovec));
136 	sp->olen = 0;
137 	sp->len = 0;
138 	sp->verified_cnt = 0;
139 
140 	return sp;
141 }
142 EXPORT_SYMBOL(secpath_set);
143 
144 /* Fetch spi and seq from ipsec header */
145 
xfrm_parse_spi(struct sk_buff * skb,u8 nexthdr,__be32 * spi,__be32 * seq)146 int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
147 {
148 	int offset, offset_seq;
149 	int hlen;
150 
151 	switch (nexthdr) {
152 	case IPPROTO_AH:
153 		hlen = sizeof(struct ip_auth_hdr);
154 		offset = offsetof(struct ip_auth_hdr, spi);
155 		offset_seq = offsetof(struct ip_auth_hdr, seq_no);
156 		break;
157 	case IPPROTO_ESP:
158 		hlen = sizeof(struct ip_esp_hdr);
159 		offset = offsetof(struct ip_esp_hdr, spi);
160 		offset_seq = offsetof(struct ip_esp_hdr, seq_no);
161 		break;
162 	case IPPROTO_COMP:
163 		if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
164 			return -EINVAL;
165 		*spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
166 		*seq = 0;
167 		return 0;
168 	default:
169 		return 1;
170 	}
171 
172 	if (!pskb_may_pull(skb, hlen))
173 		return -EINVAL;
174 
175 	*spi = *(__be32 *)(skb_transport_header(skb) + offset);
176 	*seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
177 	return 0;
178 }
179 EXPORT_SYMBOL(xfrm_parse_spi);
180 
xfrm4_remove_beet_encap(struct xfrm_state * x,struct sk_buff * skb)181 static int xfrm4_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
182 {
183 	struct iphdr *iph;
184 	int optlen = 0;
185 	int err = -EINVAL;
186 
187 	skb->protocol = htons(ETH_P_IP);
188 
189 	if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
190 		struct ip_beet_phdr *ph;
191 		int phlen;
192 
193 		if (!pskb_may_pull(skb, sizeof(*ph)))
194 			goto out;
195 
196 		ph = (struct ip_beet_phdr *)skb->data;
197 
198 		phlen = sizeof(*ph) + ph->padlen;
199 		optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
200 		if (optlen < 0 || optlen & 3 || optlen > 250)
201 			goto out;
202 
203 		XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
204 
205 		if (!pskb_may_pull(skb, phlen))
206 			goto out;
207 		__skb_pull(skb, phlen);
208 	}
209 
210 	skb_push(skb, sizeof(*iph));
211 	skb_reset_network_header(skb);
212 	skb_mac_header_rebuild(skb);
213 
214 	xfrm4_beet_make_header(skb);
215 
216 	iph = ip_hdr(skb);
217 
218 	iph->ihl += optlen / 4;
219 	iph->tot_len = htons(skb->len);
220 	iph->daddr = x->sel.daddr.a4;
221 	iph->saddr = x->sel.saddr.a4;
222 	iph->check = 0;
223 	iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
224 	err = 0;
225 out:
226 	return err;
227 }
228 
ipip_ecn_decapsulate(struct sk_buff * skb)229 static void ipip_ecn_decapsulate(struct sk_buff *skb)
230 {
231 	struct iphdr *inner_iph = ipip_hdr(skb);
232 
233 	if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
234 		IP_ECN_set_ce(inner_iph);
235 }
236 
xfrm4_remove_tunnel_encap(struct xfrm_state * x,struct sk_buff * skb)237 static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
238 {
239 	int err = -EINVAL;
240 
241 	skb->protocol = htons(ETH_P_IP);
242 
243 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
244 		goto out;
245 
246 	err = skb_unclone(skb, GFP_ATOMIC);
247 	if (err)
248 		goto out;
249 
250 	if (x->props.flags & XFRM_STATE_DECAP_DSCP)
251 		ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipip_hdr(skb));
252 	if (!(x->props.flags & XFRM_STATE_NOECN))
253 		ipip_ecn_decapsulate(skb);
254 
255 	skb_reset_network_header(skb);
256 	skb_mac_header_rebuild(skb);
257 	if (skb->mac_len)
258 		eth_hdr(skb)->h_proto = skb->protocol;
259 
260 	err = 0;
261 
262 out:
263 	return err;
264 }
265 
ipip6_ecn_decapsulate(struct sk_buff * skb)266 static void ipip6_ecn_decapsulate(struct sk_buff *skb)
267 {
268 	struct ipv6hdr *inner_iph = ipipv6_hdr(skb);
269 
270 	if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
271 		IP6_ECN_set_ce(skb, inner_iph);
272 }
273 
xfrm6_remove_tunnel_encap(struct xfrm_state * x,struct sk_buff * skb)274 static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
275 {
276 	int err = -EINVAL;
277 
278 	skb->protocol = htons(ETH_P_IPV6);
279 
280 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
281 		goto out;
282 
283 	err = skb_unclone(skb, GFP_ATOMIC);
284 	if (err)
285 		goto out;
286 
287 	if (x->props.flags & XFRM_STATE_DECAP_DSCP)
288 		ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, ipipv6_hdr(skb));
289 	if (!(x->props.flags & XFRM_STATE_NOECN))
290 		ipip6_ecn_decapsulate(skb);
291 
292 	skb_reset_network_header(skb);
293 	skb_mac_header_rebuild(skb);
294 	if (skb->mac_len)
295 		eth_hdr(skb)->h_proto = skb->protocol;
296 
297 	err = 0;
298 
299 out:
300 	return err;
301 }
302 
xfrm6_remove_beet_encap(struct xfrm_state * x,struct sk_buff * skb)303 static int xfrm6_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
304 {
305 	struct ipv6hdr *ip6h;
306 	int size = sizeof(struct ipv6hdr);
307 	int err;
308 
309 	skb->protocol = htons(ETH_P_IPV6);
310 
311 	err = skb_cow_head(skb, size + skb->mac_len);
312 	if (err)
313 		goto out;
314 
315 	__skb_push(skb, size);
316 	skb_reset_network_header(skb);
317 	skb_mac_header_rebuild(skb);
318 
319 	xfrm6_beet_make_header(skb);
320 
321 	ip6h = ipv6_hdr(skb);
322 	ip6h->payload_len = htons(skb->len - size);
323 	ip6h->daddr = x->sel.daddr.in6;
324 	ip6h->saddr = x->sel.saddr.in6;
325 	err = 0;
326 out:
327 	return err;
328 }
329 
330 /* Remove encapsulation header.
331  *
332  * The IP header will be moved over the top of the encapsulation
333  * header.
334  *
335  * On entry, the transport header shall point to where the IP header
336  * should be and the network header shall be set to where the IP
337  * header currently is.  skb->data shall point to the start of the
338  * payload.
339  */
340 static int
xfrm_inner_mode_encap_remove(struct xfrm_state * x,struct sk_buff * skb)341 xfrm_inner_mode_encap_remove(struct xfrm_state *x,
342 			     struct sk_buff *skb)
343 {
344 	switch (x->props.mode) {
345 	case XFRM_MODE_BEET:
346 		switch (x->sel.family) {
347 		case AF_INET:
348 			return xfrm4_remove_beet_encap(x, skb);
349 		case AF_INET6:
350 			return xfrm6_remove_beet_encap(x, skb);
351 		}
352 		break;
353 	case XFRM_MODE_TUNNEL:
354 		switch (XFRM_MODE_SKB_CB(skb)->protocol) {
355 		case IPPROTO_IPIP:
356 			return xfrm4_remove_tunnel_encap(x, skb);
357 		case IPPROTO_IPV6:
358 			return xfrm6_remove_tunnel_encap(x, skb);
359 		break;
360 		}
361 		return -EINVAL;
362 	}
363 
364 	WARN_ON_ONCE(1);
365 	return -EOPNOTSUPP;
366 }
367 
xfrm_prepare_input(struct xfrm_state * x,struct sk_buff * skb)368 static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
369 {
370 	switch (x->props.family) {
371 	case AF_INET:
372 		xfrm4_extract_header(skb);
373 		break;
374 	case AF_INET6:
375 		xfrm6_extract_header(skb);
376 		break;
377 	default:
378 		WARN_ON_ONCE(1);
379 		return -EAFNOSUPPORT;
380 	}
381 
382 	return xfrm_inner_mode_encap_remove(x, skb);
383 }
384 
385 /* Remove encapsulation header.
386  *
387  * The IP header will be moved over the top of the encapsulation header.
388  *
389  * On entry, skb_transport_header() shall point to where the IP header
390  * should be and skb_network_header() shall be set to where the IP header
391  * currently is.  skb->data shall point to the start of the payload.
392  */
xfrm4_transport_input(struct xfrm_state * x,struct sk_buff * skb)393 static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb)
394 {
395 	struct xfrm_offload *xo = xfrm_offload(skb);
396 	int ihl = skb->data - skb_transport_header(skb);
397 
398 	if (skb->transport_header != skb->network_header) {
399 		memmove(skb_transport_header(skb),
400 			skb_network_header(skb), ihl);
401 		if (xo)
402 			xo->orig_mac_len =
403 				skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0;
404 		skb->network_header = skb->transport_header;
405 	}
406 	ip_hdr(skb)->tot_len = htons(skb->len + ihl);
407 	skb_reset_transport_header(skb);
408 	return 0;
409 }
410 
xfrm6_transport_input(struct xfrm_state * x,struct sk_buff * skb)411 static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
412 {
413 #if IS_ENABLED(CONFIG_IPV6)
414 	struct xfrm_offload *xo = xfrm_offload(skb);
415 	int ihl = skb->data - skb_transport_header(skb);
416 
417 	if (skb->transport_header != skb->network_header) {
418 		memmove(skb_transport_header(skb),
419 			skb_network_header(skb), ihl);
420 		if (xo)
421 			xo->orig_mac_len =
422 				skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0;
423 		skb->network_header = skb->transport_header;
424 	}
425 	ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
426 					   sizeof(struct ipv6hdr));
427 	skb_reset_transport_header(skb);
428 	return 0;
429 #else
430 	WARN_ON_ONCE(1);
431 	return -EAFNOSUPPORT;
432 #endif
433 }
434 
xfrm_inner_mode_input(struct xfrm_state * x,struct sk_buff * skb)435 static int xfrm_inner_mode_input(struct xfrm_state *x,
436 				 struct sk_buff *skb)
437 {
438 	switch (x->props.mode) {
439 	case XFRM_MODE_BEET:
440 	case XFRM_MODE_TUNNEL:
441 		return xfrm_prepare_input(x, skb);
442 	case XFRM_MODE_TRANSPORT:
443 		if (x->props.family == AF_INET)
444 			return xfrm4_transport_input(x, skb);
445 		if (x->props.family == AF_INET6)
446 			return xfrm6_transport_input(x, skb);
447 		break;
448 	case XFRM_MODE_ROUTEOPTIMIZATION:
449 		WARN_ON_ONCE(1);
450 		break;
451 	default:
452 		if (x->mode_cbs && x->mode_cbs->input)
453 			return x->mode_cbs->input(x, skb);
454 
455 		WARN_ON_ONCE(1);
456 		break;
457 	}
458 
459 	return -EOPNOTSUPP;
460 }
461 
462 /* NOTE: encap_type - In addition to the normal (non-negative) values for
463  * encap_type, a negative value of -1 or -2 can be used to resume/restart this
464  * function after a previous invocation early terminated for async operation.
465  */
xfrm_input(struct sk_buff * skb,int nexthdr,__be32 spi,int encap_type)466 int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
467 {
468 	const struct xfrm_state_afinfo *afinfo;
469 	struct net *net = dev_net(skb->dev);
470 	int err;
471 	__be32 seq;
472 	__be32 seq_hi;
473 	struct xfrm_state *x = NULL;
474 	xfrm_address_t *daddr;
475 	u32 mark = skb->mark;
476 	unsigned int family = AF_UNSPEC;
477 	int decaps = 0;
478 	int async = 0;
479 	bool xfrm_gro = false;
480 	bool crypto_done = false;
481 	struct xfrm_offload *xo = xfrm_offload(skb);
482 	struct sec_path *sp;
483 
484 	if (encap_type < 0 || (xo && (xo->flags & XFRM_GRO || encap_type == 0 ||
485 				      encap_type == UDP_ENCAP_ESPINUDP))) {
486 		x = xfrm_input_state(skb);
487 
488 		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
489 			if (x->km.state == XFRM_STATE_ACQ)
490 				XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
491 			else
492 				XFRM_INC_STATS(net,
493 					       LINUX_MIB_XFRMINSTATEINVALID);
494 
495 			if (encap_type == -1)
496 				dev_put(skb->dev);
497 			goto drop;
498 		}
499 
500 		family = x->props.family;
501 
502 		/* An encap_type of -2 indicates reconstructed inner packet */
503 		if (encap_type == -2)
504 			goto resume_decapped;
505 
506 		/* An encap_type of -1 indicates async resumption. */
507 		if (encap_type == -1) {
508 			async = 1;
509 			seq = XFRM_SKB_CB(skb)->seq.input.low;
510 			spin_lock(&x->lock);
511 			goto resume;
512 		}
513 		/* GRO call */
514 		seq = XFRM_SPI_SKB_CB(skb)->seq;
515 
516 		if (xo && (xo->flags & CRYPTO_DONE)) {
517 			crypto_done = true;
518 			family = XFRM_SPI_SKB_CB(skb)->family;
519 
520 			if (!(xo->status & CRYPTO_SUCCESS)) {
521 				if (xo->status &
522 				    (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
523 				     CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
524 				     CRYPTO_TUNNEL_AH_AUTH_FAILED |
525 				     CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
526 
527 					xfrm_audit_state_icvfail(x, skb,
528 								 x->type->proto);
529 					x->stats.integrity_failed++;
530 					XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
531 					goto drop;
532 				}
533 
534 				if (xo->status & CRYPTO_INVALID_PROTOCOL) {
535 					XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
536 					goto drop;
537 				}
538 
539 				XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
540 				goto drop;
541 			}
542 
543 			if (xfrm_parse_spi(skb, nexthdr, &spi, &seq)) {
544 				XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
545 				goto drop;
546 			}
547 
548 			nexthdr = x->type_offload->input_tail(x, skb);
549 		}
550 
551 		goto process;
552 	}
553 
554 	family = XFRM_SPI_SKB_CB(skb)->family;
555 
556 	/* if tunnel is present override skb->mark value with tunnel i_key */
557 	switch (family) {
558 	case AF_INET:
559 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
560 			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
561 		break;
562 	case AF_INET6:
563 		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
564 			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
565 		break;
566 	}
567 
568 	sp = secpath_set(skb);
569 	if (!sp) {
570 		XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
571 		goto drop;
572 	}
573 
574 	seq = 0;
575 	if (!spi && xfrm_parse_spi(skb, nexthdr, &spi, &seq)) {
576 		secpath_reset(skb);
577 		XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
578 		goto drop;
579 	}
580 
581 	daddr = (xfrm_address_t *)(skb_network_header(skb) +
582 				   XFRM_SPI_SKB_CB(skb)->daddroff);
583 	do {
584 		sp = skb_sec_path(skb);
585 
586 		if (sp->len == XFRM_MAX_DEPTH) {
587 			secpath_reset(skb);
588 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
589 			goto drop;
590 		}
591 
592 		x = xfrm_input_state_lookup(net, mark, daddr, spi, nexthdr, family);
593 		if (x == NULL) {
594 			secpath_reset(skb);
595 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
596 			xfrm_audit_state_notfound(skb, family, spi, seq);
597 			goto drop;
598 		}
599 
600 		if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) {
601 			secpath_reset(skb);
602 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEDIRERROR);
603 			xfrm_audit_state_notfound(skb, family, spi, seq);
604 			xfrm_state_put(x);
605 			x = NULL;
606 			goto drop;
607 		}
608 
609 		skb->mark = xfrm_smark_get(skb->mark, x);
610 
611 		sp->xvec[sp->len++] = x;
612 
613 		skb_dst_force(skb);
614 		if (!skb_dst(skb)) {
615 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
616 			goto drop;
617 		}
618 
619 process:
620 		seq_hi = htonl(xfrm_replay_seqhi(x, seq));
621 
622 		XFRM_SKB_CB(skb)->seq.input.low = seq;
623 		XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
624 
625 		spin_lock(&x->lock);
626 
627 		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
628 			if (x->km.state == XFRM_STATE_ACQ)
629 				XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
630 			else
631 				XFRM_INC_STATS(net,
632 					       LINUX_MIB_XFRMINSTATEINVALID);
633 			goto drop_unlock;
634 		}
635 
636 		if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
637 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
638 			goto drop_unlock;
639 		}
640 
641 		if (xfrm_replay_check(x, skb, seq)) {
642 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
643 			goto drop_unlock;
644 		}
645 
646 		if (xfrm_state_check_expire(x)) {
647 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
648 			goto drop_unlock;
649 		}
650 
651 		if (xfrm_tunnel_check(skb, x, family)) {
652 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
653 			goto drop_unlock;
654 		}
655 
656 		if (!crypto_done) {
657 			spin_unlock(&x->lock);
658 			dev_hold(skb->dev);
659 
660 			nexthdr = x->type->input(x, skb);
661 			if (nexthdr == -EINPROGRESS) {
662 				if (async)
663 					dev_put(skb->dev);
664 				return 0;
665 			}
666 
667 			dev_put(skb->dev);
668 			spin_lock(&x->lock);
669 		}
670 resume:
671 		if (nexthdr < 0) {
672 			if (nexthdr == -EBADMSG) {
673 				xfrm_audit_state_icvfail(x, skb,
674 							 x->type->proto);
675 				x->stats.integrity_failed++;
676 			}
677 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
678 			goto drop_unlock;
679 		}
680 
681 		/* only the first xfrm gets the encap type */
682 		encap_type = 0;
683 
684 		if (!crypto_done && xfrm_replay_recheck(x, skb, seq)) {
685 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
686 			goto drop_unlock;
687 		}
688 
689 		xfrm_replay_advance(x, seq);
690 
691 		x->curlft.bytes += skb->len;
692 		x->curlft.packets++;
693 		x->lastused = ktime_get_real_seconds();
694 
695 		spin_unlock(&x->lock);
696 
697 		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
698 
699 		err = xfrm_inner_mode_input(x, skb);
700 		if (err == -EINPROGRESS) {
701 			if (async)
702 				dev_put(skb->dev);
703 			return 0;
704 		} else if (err) {
705 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
706 			goto drop;
707 		}
708 resume_decapped:
709 		if (x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL) {
710 			decaps = 1;
711 			break;
712 		}
713 
714 		/*
715 		 * We need the inner address.  However, we only get here for
716 		 * transport mode so the outer address is identical.
717 		 */
718 		daddr = &x->id.daddr;
719 		family = x->props.family;
720 
721 		err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
722 		if (err < 0) {
723 			XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
724 			goto drop;
725 		}
726 		crypto_done = false;
727 	} while (!err);
728 
729 	err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
730 	if (err)
731 		goto drop;
732 
733 	nf_reset_ct(skb);
734 
735 	if (decaps) {
736 		sp = skb_sec_path(skb);
737 		if (sp)
738 			sp->olen = 0;
739 		if (skb_valid_dst(skb))
740 			skb_dst_drop(skb);
741 		if (async)
742 			dev_put(skb->dev);
743 		gro_cells_receive(&gro_cells, skb);
744 		return 0;
745 	} else {
746 		xo = xfrm_offload(skb);
747 		if (xo)
748 			xfrm_gro = xo->flags & XFRM_GRO;
749 
750 		err = -EAFNOSUPPORT;
751 		rcu_read_lock();
752 		afinfo = xfrm_state_afinfo_get_rcu(x->props.family);
753 		if (likely(afinfo))
754 			err = afinfo->transport_finish(skb, xfrm_gro || async);
755 		rcu_read_unlock();
756 		if (xfrm_gro) {
757 			sp = skb_sec_path(skb);
758 			if (sp)
759 				sp->olen = 0;
760 			if (skb_valid_dst(skb))
761 				skb_dst_drop(skb);
762 			if (async)
763 				dev_put(skb->dev);
764 			gro_cells_receive(&gro_cells, skb);
765 			return err;
766 		}
767 
768 		return err;
769 	}
770 
771 drop_unlock:
772 	spin_unlock(&x->lock);
773 drop:
774 	if (async)
775 		dev_put(skb->dev);
776 	xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
777 	kfree_skb(skb);
778 	return 0;
779 }
780 EXPORT_SYMBOL(xfrm_input);
781 
xfrm_input_resume(struct sk_buff * skb,int nexthdr)782 int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
783 {
784 	return xfrm_input(skb, nexthdr, 0, -1);
785 }
786 EXPORT_SYMBOL(xfrm_input_resume);
787 
xfrm_trans_reinject(struct work_struct * work)788 static void xfrm_trans_reinject(struct work_struct *work)
789 {
790 	struct xfrm_trans_tasklet *trans = container_of(work, struct xfrm_trans_tasklet, work);
791 	struct sk_buff_head queue;
792 	struct sk_buff *skb;
793 
794 	__skb_queue_head_init(&queue);
795 	spin_lock_bh(&trans->queue_lock);
796 	skb_queue_splice_init(&trans->queue, &queue);
797 	spin_unlock_bh(&trans->queue_lock);
798 
799 	local_bh_disable();
800 	while ((skb = __skb_dequeue(&queue)))
801 		XFRM_TRANS_SKB_CB(skb)->finish(XFRM_TRANS_SKB_CB(skb)->net,
802 					       NULL, skb);
803 	local_bh_enable();
804 }
805 
xfrm_trans_queue_net(struct net * net,struct sk_buff * skb,int (* finish)(struct net *,struct sock *,struct sk_buff *))806 int xfrm_trans_queue_net(struct net *net, struct sk_buff *skb,
807 			 int (*finish)(struct net *, struct sock *,
808 				       struct sk_buff *))
809 {
810 	struct xfrm_trans_tasklet *trans;
811 
812 	trans = this_cpu_ptr(&xfrm_trans_tasklet);
813 
814 	if (skb_queue_len(&trans->queue) >= READ_ONCE(net_hotdata.max_backlog))
815 		return -ENOBUFS;
816 
817 	BUILD_BUG_ON(sizeof(struct xfrm_trans_cb) > sizeof(skb->cb));
818 
819 	XFRM_TRANS_SKB_CB(skb)->finish = finish;
820 	XFRM_TRANS_SKB_CB(skb)->net = net;
821 	spin_lock_bh(&trans->queue_lock);
822 	__skb_queue_tail(&trans->queue, skb);
823 	spin_unlock_bh(&trans->queue_lock);
824 	schedule_work(&trans->work);
825 	return 0;
826 }
827 EXPORT_SYMBOL(xfrm_trans_queue_net);
828 
xfrm_trans_queue(struct sk_buff * skb,int (* finish)(struct net *,struct sock *,struct sk_buff *))829 int xfrm_trans_queue(struct sk_buff *skb,
830 		     int (*finish)(struct net *, struct sock *,
831 				   struct sk_buff *))
832 {
833 	return xfrm_trans_queue_net(dev_net(skb->dev), skb, finish);
834 }
835 EXPORT_SYMBOL(xfrm_trans_queue);
836 
xfrm_input_init(void)837 void __init xfrm_input_init(void)
838 {
839 	int err;
840 	int i;
841 
842 	xfrm_napi_dev = alloc_netdev_dummy(0);
843 	if (!xfrm_napi_dev)
844 		panic("Failed to allocate XFRM dummy netdev\n");
845 
846 	err = gro_cells_init(&gro_cells, xfrm_napi_dev);
847 	if (err)
848 		gro_cells.cells = NULL;
849 
850 	for_each_possible_cpu(i) {
851 		struct xfrm_trans_tasklet *trans;
852 
853 		trans = &per_cpu(xfrm_trans_tasklet, i);
854 		spin_lock_init(&trans->queue_lock);
855 		__skb_queue_head_init(&trans->queue);
856 		INIT_WORK(&trans->work, xfrm_trans_reinject);
857 	}
858 }
859