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 struct net_device *dev = skb->dev;
471 int err;
472 __be32 seq;
473 __be32 seq_hi;
474 struct xfrm_state *x = NULL;
475 xfrm_address_t *daddr;
476 u32 mark = skb->mark;
477 u8 xfrm_proto = nexthdr;
478 unsigned int family = AF_UNSPEC;
479 int decaps = 0;
480 int async = 0;
481 bool xfrm_gro = false;
482 bool crypto_done = false;
483 struct xfrm_offload *xo = xfrm_offload(skb);
484 struct sec_path *sp;
485
486 if (encap_type < 0 || (xo && (xo->flags & XFRM_GRO || encap_type == 0 ||
487 encap_type == UDP_ENCAP_ESPINUDP))) {
488 x = xfrm_input_state(skb);
489 xfrm_proto = x->type ? x->type->proto : nexthdr;
490
491 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
492 if (x->km.state == XFRM_STATE_ACQ)
493 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
494 else
495 XFRM_INC_STATS(net,
496 LINUX_MIB_XFRMINSTATEINVALID);
497
498 if (encap_type == -1)
499 dev_put(dev);
500 goto drop;
501 }
502
503 family = x->props.family;
504
505 /* An encap_type of -2 indicates reconstructed inner packet */
506 if (encap_type == -2)
507 goto resume_decapped;
508
509 /* An encap_type of -1 indicates async resumption. */
510 if (encap_type == -1) {
511 async = 1;
512 seq = XFRM_SKB_CB(skb)->seq.input.low;
513 spin_lock(&x->lock);
514 goto resume;
515 }
516 /* GRO call */
517 seq = XFRM_SPI_SKB_CB(skb)->seq;
518
519 if (xo && (xo->flags & CRYPTO_DONE)) {
520 crypto_done = true;
521 family = XFRM_SPI_SKB_CB(skb)->family;
522
523 if (!(xo->status & CRYPTO_SUCCESS)) {
524 if (xo->status &
525 (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
526 CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
527 CRYPTO_TUNNEL_AH_AUTH_FAILED |
528 CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
529
530 xfrm_audit_state_icvfail(x, skb,
531 x->type->proto);
532 x->stats.integrity_failed++;
533 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
534 goto drop;
535 }
536
537 if (xo->status & CRYPTO_INVALID_PROTOCOL) {
538 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
539 goto drop;
540 }
541
542 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
543 goto drop;
544 }
545
546 if (xfrm_parse_spi(skb, nexthdr, &spi, &seq)) {
547 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
548 goto drop;
549 }
550
551 nexthdr = x->type_offload->input_tail(x, skb);
552 }
553
554 goto process;
555 }
556
557 family = XFRM_SPI_SKB_CB(skb)->family;
558
559 /* if tunnel is present override skb->mark value with tunnel i_key */
560 switch (family) {
561 case AF_INET:
562 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
563 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
564 break;
565 case AF_INET6:
566 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
567 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
568 break;
569 }
570
571 sp = secpath_set(skb);
572 if (!sp) {
573 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
574 goto drop;
575 }
576
577 seq = 0;
578 if (!spi && xfrm_parse_spi(skb, nexthdr, &spi, &seq)) {
579 secpath_reset(skb);
580 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
581 goto drop;
582 }
583
584 daddr = (xfrm_address_t *)(skb_network_header(skb) +
585 XFRM_SPI_SKB_CB(skb)->daddroff);
586 do {
587 sp = skb_sec_path(skb);
588
589 if (sp->len == XFRM_MAX_DEPTH) {
590 secpath_reset(skb);
591 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
592 goto drop;
593 }
594
595 x = xfrm_input_state_lookup(net, mark, daddr, spi, nexthdr, family);
596 if (x == NULL) {
597 xfrm_proto = nexthdr;
598 secpath_reset(skb);
599 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
600 xfrm_audit_state_notfound(skb, family, spi, seq);
601 goto drop;
602 }
603 xfrm_proto = x->type ? x->type->proto : nexthdr;
604
605 if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) {
606 secpath_reset(skb);
607 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEDIRERROR);
608 xfrm_audit_state_notfound(skb, family, spi, seq);
609 xfrm_state_put(x);
610 x = NULL;
611 xfrm_proto = nexthdr;
612 goto drop;
613 }
614
615 skb->mark = xfrm_smark_get(skb->mark, x);
616
617 sp->xvec[sp->len++] = x;
618
619 skb_dst_force(skb);
620 if (!skb_dst(skb)) {
621 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
622 goto drop;
623 }
624
625 process:
626 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
627
628 XFRM_SKB_CB(skb)->seq.input.low = seq;
629 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
630
631 spin_lock(&x->lock);
632
633 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
634 if (x->km.state == XFRM_STATE_ACQ)
635 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
636 else
637 XFRM_INC_STATS(net,
638 LINUX_MIB_XFRMINSTATEINVALID);
639 goto drop_unlock;
640 }
641
642 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
643 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
644 goto drop_unlock;
645 }
646
647 if (xfrm_replay_check(x, skb, seq)) {
648 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
649 goto drop_unlock;
650 }
651
652 if (xfrm_state_check_expire(x)) {
653 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
654 goto drop_unlock;
655 }
656
657 if (xfrm_tunnel_check(skb, x, family)) {
658 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
659 goto drop_unlock;
660 }
661
662 if (!crypto_done) {
663 spin_unlock(&x->lock);
664 dev_hold(dev);
665
666 nexthdr = x->type->input(x, skb);
667 if (nexthdr == -EINPROGRESS) {
668 if (async)
669 dev_put(dev);
670 return 0;
671 }
672
673 dev_put(dev);
674 spin_lock(&x->lock);
675 }
676 resume:
677 if (nexthdr < 0) {
678 if (nexthdr == -EBADMSG) {
679 xfrm_audit_state_icvfail(x, skb,
680 x->type->proto);
681 x->stats.integrity_failed++;
682 }
683 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
684 goto drop_unlock;
685 }
686
687 /* only the first xfrm gets the encap type */
688 encap_type = 0;
689
690 if (!crypto_done && xfrm_replay_recheck(x, skb, seq)) {
691 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
692 goto drop_unlock;
693 }
694
695 xfrm_replay_advance(x, seq);
696
697 x->curlft.bytes += skb->len;
698 x->curlft.packets++;
699 x->lastused = ktime_get_real_seconds();
700
701 spin_unlock(&x->lock);
702
703 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
704
705 err = xfrm_inner_mode_input(x, skb);
706 if (err == -EINPROGRESS) {
707 if (async)
708 dev_put(dev);
709 return 0;
710 } else if (err) {
711 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
712 goto drop;
713 }
714 resume_decapped:
715 if (x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL) {
716 decaps = 1;
717 break;
718 }
719
720 /*
721 * We need the inner address. However, we only get here for
722 * transport mode so the outer address is identical.
723 */
724 daddr = &x->id.daddr;
725 family = x->props.family;
726
727 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
728 if (err < 0) {
729 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
730 goto drop;
731 }
732 crypto_done = false;
733 } while (!err);
734
735 rcu_read_lock();
736 err = xfrm_rcv_cb(skb, family, xfrm_proto, 0);
737 if (err) {
738 rcu_read_unlock();
739 goto drop;
740 }
741
742 nf_reset_ct(skb);
743
744 if (decaps) {
745 sp = skb_sec_path(skb);
746 if (sp)
747 sp->olen = 0;
748 if (skb_valid_dst(skb))
749 skb_dst_drop(skb);
750 if (async)
751 dev_put(dev);
752 gro_cells_receive(&gro_cells, skb);
753 rcu_read_unlock();
754 return 0;
755 } else {
756 xo = xfrm_offload(skb);
757 if (xo)
758 xfrm_gro = xo->flags & XFRM_GRO;
759
760 err = -EAFNOSUPPORT;
761 afinfo = xfrm_state_afinfo_get_rcu(family);
762 if (likely(afinfo))
763 err = afinfo->transport_finish(skb, xfrm_gro || async);
764 if (xfrm_gro) {
765 sp = skb_sec_path(skb);
766 if (sp)
767 sp->olen = 0;
768 if (skb_valid_dst(skb))
769 skb_dst_drop(skb);
770 gro_cells_receive(&gro_cells, skb);
771 }
772
773 if (async)
774 dev_put(dev);
775 rcu_read_unlock();
776 return err;
777 }
778
779 drop_unlock:
780 spin_unlock(&x->lock);
781 drop:
782 if (async)
783 dev_put(dev);
784 xfrm_rcv_cb(skb, family, xfrm_proto, -1);
785 kfree_skb(skb);
786 return 0;
787 }
788 EXPORT_SYMBOL(xfrm_input);
789
xfrm_input_resume(struct sk_buff * skb,int nexthdr)790 int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
791 {
792 return xfrm_input(skb, nexthdr, 0, -1);
793 }
794 EXPORT_SYMBOL(xfrm_input_resume);
795
xfrm_trans_reinject(struct work_struct * work)796 static void xfrm_trans_reinject(struct work_struct *work)
797 {
798 struct xfrm_trans_tasklet *trans = container_of(work, struct xfrm_trans_tasklet, work);
799 struct sk_buff_head queue;
800 struct sk_buff *skb;
801
802 __skb_queue_head_init(&queue);
803 spin_lock_bh(&trans->queue_lock);
804 skb_queue_splice_init(&trans->queue, &queue);
805 spin_unlock_bh(&trans->queue_lock);
806
807 local_bh_disable();
808 rcu_read_lock();
809 while ((skb = __skb_dequeue(&queue))) {
810 struct net *net = XFRM_TRANS_SKB_CB(skb)->net;
811 struct net_device *dev = skb->dev;
812
813 XFRM_TRANS_SKB_CB(skb)->finish(net, NULL, skb);
814 if (dev)
815 dev_put(dev);
816 put_net(net);
817 }
818 rcu_read_unlock();
819 local_bh_enable();
820 }
821
xfrm_trans_queue_net(struct net * net,struct sk_buff * skb,int (* finish)(struct net *,struct sock *,struct sk_buff *))822 int xfrm_trans_queue_net(struct net *net, struct sk_buff *skb,
823 int (*finish)(struct net *, struct sock *,
824 struct sk_buff *))
825 {
826 struct xfrm_trans_tasklet *trans;
827 struct net *hold_net;
828
829 trans = this_cpu_ptr(&xfrm_trans_tasklet);
830
831 if (skb_queue_len(&trans->queue) >= READ_ONCE(net_hotdata.max_backlog))
832 return -ENOBUFS;
833
834 if (skb_dst(skb) && !skb_dst_force(skb))
835 return -EHOSTUNREACH;
836
837 BUILD_BUG_ON(sizeof(struct xfrm_trans_cb) > sizeof(skb->cb));
838
839 hold_net = maybe_get_net(net);
840 if (!hold_net)
841 return -ENODEV;
842
843 if (skb->dev)
844 dev_hold(skb->dev);
845
846 XFRM_TRANS_SKB_CB(skb)->finish = finish;
847 XFRM_TRANS_SKB_CB(skb)->net = hold_net;
848 spin_lock_bh(&trans->queue_lock);
849 __skb_queue_tail(&trans->queue, skb);
850 spin_unlock_bh(&trans->queue_lock);
851 schedule_work(&trans->work);
852 return 0;
853 }
854 EXPORT_SYMBOL(xfrm_trans_queue_net);
855
xfrm_trans_queue(struct sk_buff * skb,int (* finish)(struct net *,struct sock *,struct sk_buff *))856 int xfrm_trans_queue(struct sk_buff *skb,
857 int (*finish)(struct net *, struct sock *,
858 struct sk_buff *))
859 {
860 return xfrm_trans_queue_net(dev_net(skb->dev), skb, finish);
861 }
862 EXPORT_SYMBOL(xfrm_trans_queue);
863
xfrm_input_init(void)864 void __init xfrm_input_init(void)
865 {
866 int err;
867 int i;
868
869 xfrm_napi_dev = alloc_netdev_dummy(0);
870 if (!xfrm_napi_dev)
871 panic("Failed to allocate XFRM dummy netdev\n");
872
873 err = gro_cells_init(&gro_cells, xfrm_napi_dev);
874 if (err)
875 gro_cells.cells = NULL;
876
877 for_each_possible_cpu(i) {
878 struct xfrm_trans_tasklet *trans;
879
880 trans = &per_cpu(xfrm_trans_tasklet, i);
881 spin_lock_init(&trans->queue_lock);
882 __skb_queue_head_init(&trans->queue);
883 INIT_WORK(&trans->work, xfrm_trans_reinject);
884 }
885 }
886