xfrm: change secpath_set to return secpath struct, not error value
[linux-2.6-block.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 struct xfrm_trans_tasklet {
25         struct tasklet_struct tasklet;
26         struct sk_buff_head queue;
27 };
28
29 struct xfrm_trans_cb {
30         union {
31                 struct inet_skb_parm    h4;
32 #if IS_ENABLED(CONFIG_IPV6)
33                 struct inet6_skb_parm   h6;
34 #endif
35         } header;
36         int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb);
37 };
38
39 #define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
40
41 static struct kmem_cache *secpath_cachep __ro_after_init;
42
43 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
44 static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[AF_INET6 + 1];
45
46 static struct gro_cells gro_cells;
47 static struct net_device xfrm_napi_dev;
48
49 static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet);
50
51 int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
52 {
53         int err = 0;
54
55         if (WARN_ON(afinfo->family >= ARRAY_SIZE(xfrm_input_afinfo)))
56                 return -EAFNOSUPPORT;
57
58         spin_lock_bh(&xfrm_input_afinfo_lock);
59         if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
60                 err = -EEXIST;
61         else
62                 rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
63         spin_unlock_bh(&xfrm_input_afinfo_lock);
64         return err;
65 }
66 EXPORT_SYMBOL(xfrm_input_register_afinfo);
67
68 int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
69 {
70         int err = 0;
71
72         spin_lock_bh(&xfrm_input_afinfo_lock);
73         if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
74                 if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
75                         err = -EINVAL;
76                 else
77                         RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
78         }
79         spin_unlock_bh(&xfrm_input_afinfo_lock);
80         synchronize_rcu();
81         return err;
82 }
83 EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
84
85 static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
86 {
87         const struct xfrm_input_afinfo *afinfo;
88
89         if (WARN_ON_ONCE(family >= ARRAY_SIZE(xfrm_input_afinfo)))
90                 return NULL;
91
92         rcu_read_lock();
93         afinfo = rcu_dereference(xfrm_input_afinfo[family]);
94         if (unlikely(!afinfo))
95                 rcu_read_unlock();
96         return afinfo;
97 }
98
99 static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
100                        int err)
101 {
102         int ret;
103         const struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
104
105         if (!afinfo)
106                 return -EAFNOSUPPORT;
107
108         ret = afinfo->callback(skb, protocol, err);
109         rcu_read_unlock();
110
111         return ret;
112 }
113
114 void __secpath_destroy(struct sec_path *sp)
115 {
116         int i;
117         for (i = 0; i < sp->len; i++)
118                 xfrm_state_put(sp->xvec[i]);
119         kmem_cache_free(secpath_cachep, sp);
120 }
121 EXPORT_SYMBOL(__secpath_destroy);
122
123 struct sec_path *secpath_dup(struct sec_path *src)
124 {
125         struct sec_path *sp;
126
127         sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
128         if (!sp)
129                 return NULL;
130
131         sp->len = 0;
132         sp->olen = 0;
133
134         memset(sp->ovec, 0, sizeof(sp->ovec));
135
136         if (src) {
137                 int i;
138
139                 memcpy(sp, src, sizeof(*sp));
140                 for (i = 0; i < sp->len; i++)
141                         xfrm_state_hold(sp->xvec[i]);
142         }
143         refcount_set(&sp->refcnt, 1);
144         return sp;
145 }
146 EXPORT_SYMBOL(secpath_dup);
147
148 struct sec_path *secpath_set(struct sk_buff *skb)
149 {
150         struct sec_path *sp = skb->sp;
151
152         /* Allocate new secpath or COW existing one. */
153         if (!sp || refcount_read(&sp->refcnt) != 1) {
154                 sp = secpath_dup(skb->sp);
155                 if (!sp)
156                         return NULL;
157
158                 if (skb->sp)
159                         secpath_put(skb->sp);
160                 skb->sp = sp;
161         }
162
163         return sp;
164 }
165 EXPORT_SYMBOL(secpath_set);
166
167 /* Fetch spi and seq from ipsec header */
168
169 int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
170 {
171         int offset, offset_seq;
172         int hlen;
173
174         switch (nexthdr) {
175         case IPPROTO_AH:
176                 hlen = sizeof(struct ip_auth_hdr);
177                 offset = offsetof(struct ip_auth_hdr, spi);
178                 offset_seq = offsetof(struct ip_auth_hdr, seq_no);
179                 break;
180         case IPPROTO_ESP:
181                 hlen = sizeof(struct ip_esp_hdr);
182                 offset = offsetof(struct ip_esp_hdr, spi);
183                 offset_seq = offsetof(struct ip_esp_hdr, seq_no);
184                 break;
185         case IPPROTO_COMP:
186                 if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
187                         return -EINVAL;
188                 *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
189                 *seq = 0;
190                 return 0;
191         default:
192                 return 1;
193         }
194
195         if (!pskb_may_pull(skb, hlen))
196                 return -EINVAL;
197
198         *spi = *(__be32 *)(skb_transport_header(skb) + offset);
199         *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
200         return 0;
201 }
202 EXPORT_SYMBOL(xfrm_parse_spi);
203
204 int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
205 {
206         struct xfrm_mode *inner_mode = x->inner_mode;
207         int err;
208
209         err = x->outer_mode->afinfo->extract_input(x, skb);
210         if (err)
211                 return err;
212
213         if (x->sel.family == AF_UNSPEC) {
214                 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
215                 if (inner_mode == NULL)
216                         return -EAFNOSUPPORT;
217         }
218
219         skb->protocol = inner_mode->afinfo->eth_proto;
220         return inner_mode->input2(x, skb);
221 }
222 EXPORT_SYMBOL(xfrm_prepare_input);
223
224 int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
225 {
226         struct net *net = dev_net(skb->dev);
227         int err;
228         __be32 seq;
229         __be32 seq_hi;
230         struct xfrm_state *x = NULL;
231         xfrm_address_t *daddr;
232         struct xfrm_mode *inner_mode;
233         u32 mark = skb->mark;
234         unsigned int family = AF_UNSPEC;
235         int decaps = 0;
236         int async = 0;
237         bool xfrm_gro = false;
238         bool crypto_done = false;
239         struct xfrm_offload *xo = xfrm_offload(skb);
240         struct sec_path *sp;
241
242         if (encap_type < 0) {
243                 x = xfrm_input_state(skb);
244
245                 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
246                         if (x->km.state == XFRM_STATE_ACQ)
247                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
248                         else
249                                 XFRM_INC_STATS(net,
250                                                LINUX_MIB_XFRMINSTATEINVALID);
251                         goto drop;
252                 }
253
254                 family = x->outer_mode->afinfo->family;
255
256                 /* An encap_type of -1 indicates async resumption. */
257                 if (encap_type == -1) {
258                         async = 1;
259                         seq = XFRM_SKB_CB(skb)->seq.input.low;
260                         goto resume;
261                 }
262
263                 /* encap_type < -1 indicates a GRO call. */
264                 encap_type = 0;
265                 seq = XFRM_SPI_SKB_CB(skb)->seq;
266
267                 if (xo && (xo->flags & CRYPTO_DONE)) {
268                         crypto_done = true;
269                         family = XFRM_SPI_SKB_CB(skb)->family;
270
271                         if (!(xo->status & CRYPTO_SUCCESS)) {
272                                 if (xo->status &
273                                     (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
274                                      CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
275                                      CRYPTO_TUNNEL_AH_AUTH_FAILED |
276                                      CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
277
278                                         xfrm_audit_state_icvfail(x, skb,
279                                                                  x->type->proto);
280                                         x->stats.integrity_failed++;
281                                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
282                                         goto drop;
283                                 }
284
285                                 if (xo->status & CRYPTO_INVALID_PROTOCOL) {
286                                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
287                                         goto drop;
288                                 }
289
290                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
291                                 goto drop;
292                         }
293
294                         if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
295                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
296                                 goto drop;
297                         }
298                 }
299
300                 goto lock;
301         }
302
303         family = XFRM_SPI_SKB_CB(skb)->family;
304
305         /* if tunnel is present override skb->mark value with tunnel i_key */
306         switch (family) {
307         case AF_INET:
308                 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
309                         mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
310                 break;
311         case AF_INET6:
312                 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
313                         mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
314                 break;
315         }
316
317         sp = secpath_set(skb);
318         if (!sp) {
319                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
320                 goto drop;
321         }
322
323         seq = 0;
324         if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
325                 secpath_reset(skb);
326                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
327                 goto drop;
328         }
329
330         daddr = (xfrm_address_t *)(skb_network_header(skb) +
331                                    XFRM_SPI_SKB_CB(skb)->daddroff);
332         do {
333                 if (skb->sp->len == XFRM_MAX_DEPTH) {
334                         secpath_reset(skb);
335                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
336                         goto drop;
337                 }
338
339                 x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
340                 if (x == NULL) {
341                         secpath_reset(skb);
342                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
343                         xfrm_audit_state_notfound(skb, family, spi, seq);
344                         goto drop;
345                 }
346
347                 skb->mark = xfrm_smark_get(skb->mark, x);
348
349                 skb->sp->xvec[skb->sp->len++] = x;
350
351 lock:
352                 spin_lock(&x->lock);
353
354                 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
355                         if (x->km.state == XFRM_STATE_ACQ)
356                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
357                         else
358                                 XFRM_INC_STATS(net,
359                                                LINUX_MIB_XFRMINSTATEINVALID);
360                         goto drop_unlock;
361                 }
362
363                 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
364                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
365                         goto drop_unlock;
366                 }
367
368                 if (x->repl->check(x, skb, seq)) {
369                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
370                         goto drop_unlock;
371                 }
372
373                 if (xfrm_state_check_expire(x)) {
374                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
375                         goto drop_unlock;
376                 }
377
378                 spin_unlock(&x->lock);
379
380                 if (xfrm_tunnel_check(skb, x, family)) {
381                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
382                         goto drop;
383                 }
384
385                 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
386
387                 XFRM_SKB_CB(skb)->seq.input.low = seq;
388                 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
389
390                 skb_dst_force(skb);
391                 dev_hold(skb->dev);
392
393                 if (crypto_done)
394                         nexthdr = x->type_offload->input_tail(x, skb);
395                 else
396                         nexthdr = x->type->input(x, skb);
397
398                 if (nexthdr == -EINPROGRESS)
399                         return 0;
400 resume:
401                 dev_put(skb->dev);
402
403                 spin_lock(&x->lock);
404                 if (nexthdr <= 0) {
405                         if (nexthdr == -EBADMSG) {
406                                 xfrm_audit_state_icvfail(x, skb,
407                                                          x->type->proto);
408                                 x->stats.integrity_failed++;
409                         }
410                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
411                         goto drop_unlock;
412                 }
413
414                 /* only the first xfrm gets the encap type */
415                 encap_type = 0;
416
417                 if (async && x->repl->recheck(x, skb, seq)) {
418                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
419                         goto drop_unlock;
420                 }
421
422                 x->repl->advance(x, seq);
423
424                 x->curlft.bytes += skb->len;
425                 x->curlft.packets++;
426
427                 spin_unlock(&x->lock);
428
429                 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
430
431                 inner_mode = x->inner_mode;
432
433                 if (x->sel.family == AF_UNSPEC) {
434                         inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
435                         if (inner_mode == NULL) {
436                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
437                                 goto drop;
438                         }
439                 }
440
441                 if (inner_mode->input(x, skb)) {
442                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
443                         goto drop;
444                 }
445
446                 if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
447                         decaps = 1;
448                         break;
449                 }
450
451                 /*
452                  * We need the inner address.  However, we only get here for
453                  * transport mode so the outer address is identical.
454                  */
455                 daddr = &x->id.daddr;
456                 family = x->outer_mode->afinfo->family;
457
458                 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
459                 if (err < 0) {
460                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
461                         goto drop;
462                 }
463                 crypto_done = false;
464         } while (!err);
465
466         err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
467         if (err)
468                 goto drop;
469
470         nf_reset(skb);
471
472         if (decaps) {
473                 if (skb->sp)
474                         skb->sp->olen = 0;
475                 skb_dst_drop(skb);
476                 gro_cells_receive(&gro_cells, skb);
477                 return 0;
478         } else {
479                 xo = xfrm_offload(skb);
480                 if (xo)
481                         xfrm_gro = xo->flags & XFRM_GRO;
482
483                 err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
484                 if (xfrm_gro) {
485                         if (skb->sp)
486                                 skb->sp->olen = 0;
487                         skb_dst_drop(skb);
488                         gro_cells_receive(&gro_cells, skb);
489                         return err;
490                 }
491
492                 return err;
493         }
494
495 drop_unlock:
496         spin_unlock(&x->lock);
497 drop:
498         xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
499         kfree_skb(skb);
500         return 0;
501 }
502 EXPORT_SYMBOL(xfrm_input);
503
504 int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
505 {
506         return xfrm_input(skb, nexthdr, 0, -1);
507 }
508 EXPORT_SYMBOL(xfrm_input_resume);
509
510 static void xfrm_trans_reinject(unsigned long data)
511 {
512         struct xfrm_trans_tasklet *trans = (void *)data;
513         struct sk_buff_head queue;
514         struct sk_buff *skb;
515
516         __skb_queue_head_init(&queue);
517         skb_queue_splice_init(&trans->queue, &queue);
518
519         while ((skb = __skb_dequeue(&queue)))
520                 XFRM_TRANS_SKB_CB(skb)->finish(dev_net(skb->dev), NULL, skb);
521 }
522
523 int xfrm_trans_queue(struct sk_buff *skb,
524                      int (*finish)(struct net *, struct sock *,
525                                    struct sk_buff *))
526 {
527         struct xfrm_trans_tasklet *trans;
528
529         trans = this_cpu_ptr(&xfrm_trans_tasklet);
530
531         if (skb_queue_len(&trans->queue) >= netdev_max_backlog)
532                 return -ENOBUFS;
533
534         XFRM_TRANS_SKB_CB(skb)->finish = finish;
535         __skb_queue_tail(&trans->queue, skb);
536         tasklet_schedule(&trans->tasklet);
537         return 0;
538 }
539 EXPORT_SYMBOL(xfrm_trans_queue);
540
541 void __init xfrm_input_init(void)
542 {
543         int err;
544         int i;
545
546         init_dummy_netdev(&xfrm_napi_dev);
547         err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
548         if (err)
549                 gro_cells.cells = NULL;
550
551         secpath_cachep = kmem_cache_create("secpath_cache",
552                                            sizeof(struct sec_path),
553                                            0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
554                                            NULL);
555
556         for_each_possible_cpu(i) {
557                 struct xfrm_trans_tasklet *trans;
558
559                 trans = &per_cpu(xfrm_trans_tasklet, i);
560                 __skb_queue_head_init(&trans->queue);
561                 tasklet_init(&trans->tasklet, xfrm_trans_reinject,
562                              (unsigned long)trans);
563         }
564 }