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