GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / ipv6 / reassembly.c
1 /*
2  *      IPv6 fragment reassembly
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      Based on: net/ipv4/ip_fragment.c
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 /*
17  *      Fixes:
18  *      Andi Kleen      Make it work with multiple hosts.
19  *                      More RFC compliance.
20  *
21  *      Horst von Brand Add missing #include <linux/string.h>
22  *      Alexey Kuznetsov        SMP races, threading, cleanup.
23  *      Patrick McHardy         LRU queue of frag heads for evictor.
24  *      Mitsuru KANDA @USAGI    Register inet6_protocol{}.
25  *      David Stevens and
26  *      YOSHIFUJI,H. @USAGI     Always remove fragment header to
27  *                              calculate ICV correctly.
28  */
29
30 #define pr_fmt(fmt) "IPv6: " fmt
31
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/net.h>
39 #include <linux/list.h>
40 #include <linux/netdevice.h>
41 #include <linux/in6.h>
42 #include <linux/ipv6.h>
43 #include <linux/icmpv6.h>
44 #include <linux/random.h>
45 #include <linux/jhash.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/export.h>
49
50 #include <net/sock.h>
51 #include <net/snmp.h>
52
53 #include <net/ipv6.h>
54 #include <net/ip6_route.h>
55 #include <net/protocol.h>
56 #include <net/transp_v6.h>
57 #include <net/rawv6.h>
58 #include <net/ndisc.h>
59 #include <net/addrconf.h>
60 #include <net/ipv6_frag.h>
61 #include <net/inet_ecn.h>
62
63 static const char ip6_frag_cache_name[] = "ip6-frags";
64
65 static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
66 {
67         return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
68 }
69
70 static struct inet_frags ip6_frags;
71
72 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
73                           struct sk_buff *prev_tail, struct net_device *dev);
74
75 static void ip6_frag_expire(struct timer_list *t)
76 {
77         struct inet_frag_queue *frag = from_timer(frag, t, timer);
78         struct frag_queue *fq;
79         struct net *net;
80
81         fq = container_of(frag, struct frag_queue, q);
82         net = container_of(fq->q.net, struct net, ipv6.frags);
83
84         ip6frag_expire_frag_queue(net, fq);
85 }
86
87 static struct frag_queue *
88 fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
89 {
90         struct frag_v6_compare_key key = {
91                 .id = id,
92                 .saddr = hdr->saddr,
93                 .daddr = hdr->daddr,
94                 .user = IP6_DEFRAG_LOCAL_DELIVER,
95                 .iif = iif,
96         };
97         struct inet_frag_queue *q;
98
99         if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
100                                             IPV6_ADDR_LINKLOCAL)))
101                 key.iif = 0;
102
103         q = inet_frag_find(&net->ipv6.frags, &key);
104         if (!q)
105                 return NULL;
106
107         return container_of(q, struct frag_queue, q);
108 }
109
110 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
111                           struct frag_hdr *fhdr, int nhoff,
112                           u32 *prob_offset)
113 {
114         struct net *net = dev_net(skb_dst(skb)->dev);
115         int offset, end, fragsize;
116         struct sk_buff *prev_tail;
117         struct net_device *dev;
118         int err = -ENOENT;
119         u8 ecn;
120
121         if (fq->q.flags & INET_FRAG_COMPLETE)
122                 goto err;
123
124         err = -EINVAL;
125         offset = ntohs(fhdr->frag_off) & ~0x7;
126         end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
127                         ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
128
129         if ((unsigned int)end > IPV6_MAXPLEN) {
130                 *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
131                 /* note that if prob_offset is set, the skb is freed elsewhere,
132                  * we do not free it here.
133                  */
134                 return -1;
135         }
136
137         ecn = ip6_frag_ecn(ipv6_hdr(skb));
138
139         if (skb->ip_summed == CHECKSUM_COMPLETE) {
140                 const unsigned char *nh = skb_network_header(skb);
141                 skb->csum = csum_sub(skb->csum,
142                                      csum_partial(nh, (u8 *)(fhdr + 1) - nh,
143                                                   0));
144         }
145
146         /* Is this the final fragment? */
147         if (!(fhdr->frag_off & htons(IP6_MF))) {
148                 /* If we already have some bits beyond end
149                  * or have different end, the segment is corrupted.
150                  */
151                 if (end < fq->q.len ||
152                     ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
153                         goto discard_fq;
154                 fq->q.flags |= INET_FRAG_LAST_IN;
155                 fq->q.len = end;
156         } else {
157                 /* Check if the fragment is rounded to 8 bytes.
158                  * Required by the RFC.
159                  */
160                 if (end & 0x7) {
161                         /* RFC2460 says always send parameter problem in
162                          * this case. -DaveM
163                          */
164                         *prob_offset = offsetof(struct ipv6hdr, payload_len);
165                         return -1;
166                 }
167                 if (end > fq->q.len) {
168                         /* Some bits beyond end -> corruption. */
169                         if (fq->q.flags & INET_FRAG_LAST_IN)
170                                 goto discard_fq;
171                         fq->q.len = end;
172                 }
173         }
174
175         if (end == offset)
176                 goto discard_fq;
177
178         err = -ENOMEM;
179         /* Point into the IP datagram 'data' part. */
180         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
181                 goto discard_fq;
182
183         err = pskb_trim_rcsum(skb, end - offset);
184         if (err)
185                 goto discard_fq;
186
187         /* Note : skb->rbnode and skb->dev share the same location. */
188         dev = skb->dev;
189         /* Makes sure compiler wont do silly aliasing games */
190         barrier();
191
192         prev_tail = fq->q.fragments_tail;
193         err = inet_frag_queue_insert(&fq->q, skb, offset, end);
194         if (err)
195                 goto insert_error;
196
197         if (dev)
198                 fq->iif = dev->ifindex;
199
200         fq->q.stamp = skb->tstamp;
201         fq->q.meat += skb->len;
202         fq->ecn |= ecn;
203         add_frag_mem_limit(fq->q.net, skb->truesize);
204
205         fragsize = -skb_network_offset(skb) + skb->len;
206         if (fragsize > fq->q.max_size)
207                 fq->q.max_size = fragsize;
208
209         /* The first fragment.
210          * nhoffset is obtained from the first fragment, of course.
211          */
212         if (offset == 0) {
213                 fq->nhoffset = nhoff;
214                 fq->q.flags |= INET_FRAG_FIRST_IN;
215         }
216
217         if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
218             fq->q.meat == fq->q.len) {
219                 unsigned long orefdst = skb->_skb_refdst;
220
221                 skb->_skb_refdst = 0UL;
222                 err = ip6_frag_reasm(fq, skb, prev_tail, dev);
223                 skb->_skb_refdst = orefdst;
224                 return err;
225         }
226
227         skb_dst_drop(skb);
228         return -EINPROGRESS;
229
230 insert_error:
231         if (err == IPFRAG_DUP) {
232                 kfree_skb(skb);
233                 return -EINVAL;
234         }
235         err = -EINVAL;
236         __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
237                         IPSTATS_MIB_REASM_OVERLAPS);
238 discard_fq:
239         inet_frag_kill(&fq->q);
240         __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
241                         IPSTATS_MIB_REASMFAILS);
242 err:
243         kfree_skb(skb);
244         return err;
245 }
246
247 /*
248  *      Check if this packet is complete.
249  *
250  *      It is called with locked fq, and caller must check that
251  *      queue is eligible for reassembly i.e. it is not COMPLETE,
252  *      the last and the first frames arrived and all the bits are here.
253  */
254 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
255                           struct sk_buff *prev_tail, struct net_device *dev)
256 {
257         struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
258         unsigned int nhoff;
259         void *reasm_data;
260         int payload_len;
261         u8 ecn;
262
263         inet_frag_kill(&fq->q);
264
265         ecn = ip_frag_ecn_table[fq->ecn];
266         if (unlikely(ecn == 0xff))
267                 goto out_fail;
268
269         reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
270         if (!reasm_data)
271                 goto out_oom;
272
273         payload_len = ((skb->data - skb_network_header(skb)) -
274                        sizeof(struct ipv6hdr) + fq->q.len -
275                        sizeof(struct frag_hdr));
276         if (payload_len > IPV6_MAXPLEN)
277                 goto out_oversize;
278
279         /* We have to remove fragment header from datagram and to relocate
280          * header in order to calculate ICV correctly. */
281         nhoff = fq->nhoffset;
282         skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
283         memmove(skb->head + sizeof(struct frag_hdr), skb->head,
284                 (skb->data - skb->head) - sizeof(struct frag_hdr));
285         if (skb_mac_header_was_set(skb))
286                 skb->mac_header += sizeof(struct frag_hdr);
287         skb->network_header += sizeof(struct frag_hdr);
288
289         skb_reset_transport_header(skb);
290
291         inet_frag_reasm_finish(&fq->q, skb, reasm_data);
292
293         skb->dev = dev;
294         ipv6_hdr(skb)->payload_len = htons(payload_len);
295         ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
296         IP6CB(skb)->nhoff = nhoff;
297         IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
298         IP6CB(skb)->frag_max_size = fq->q.max_size;
299
300         /* Yes, and fold redundant checksum back. 8) */
301         skb_postpush_rcsum(skb, skb_network_header(skb),
302                            skb_network_header_len(skb));
303
304         rcu_read_lock();
305         __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
306         rcu_read_unlock();
307         fq->q.fragments = NULL;
308         fq->q.rb_fragments = RB_ROOT;
309         fq->q.fragments_tail = NULL;
310         fq->q.last_run_head = NULL;
311         return 1;
312
313 out_oversize:
314         net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
315         goto out_fail;
316 out_oom:
317         net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
318 out_fail:
319         rcu_read_lock();
320         __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
321         rcu_read_unlock();
322         inet_frag_kill(&fq->q);
323         return -1;
324 }
325
326 static int ipv6_frag_rcv(struct sk_buff *skb)
327 {
328         struct frag_hdr *fhdr;
329         struct frag_queue *fq;
330         const struct ipv6hdr *hdr = ipv6_hdr(skb);
331         struct net *net = dev_net(skb_dst(skb)->dev);
332         int iif;
333
334         if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
335                 goto fail_hdr;
336
337         __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
338
339         /* Jumbo payload inhibits frag. header */
340         if (hdr->payload_len == 0)
341                 goto fail_hdr;
342
343         if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
344                                  sizeof(struct frag_hdr))))
345                 goto fail_hdr;
346
347         hdr = ipv6_hdr(skb);
348         fhdr = (struct frag_hdr *)skb_transport_header(skb);
349
350         if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) {
351                 /* It is not a fragmented frame */
352                 skb->transport_header += sizeof(struct frag_hdr);
353                 __IP6_INC_STATS(net,
354                                 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
355
356                 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
357                 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
358                 IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) +
359                                             sizeof(struct ipv6hdr);
360                 return 1;
361         }
362
363         iif = skb->dev ? skb->dev->ifindex : 0;
364         fq = fq_find(net, fhdr->identification, hdr, iif);
365         if (fq) {
366                 u32 prob_offset = 0;
367                 int ret;
368
369                 spin_lock(&fq->q.lock);
370
371                 fq->iif = iif;
372                 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
373                                      &prob_offset);
374
375                 spin_unlock(&fq->q.lock);
376                 inet_frag_put(&fq->q);
377                 if (prob_offset) {
378                         __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
379                                         IPSTATS_MIB_INHDRERRORS);
380                         /* icmpv6_param_prob() calls kfree_skb(skb) */
381                         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
382                 }
383                 return ret;
384         }
385
386         __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
387         kfree_skb(skb);
388         return -1;
389
390 fail_hdr:
391         __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
392                         IPSTATS_MIB_INHDRERRORS);
393         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
394         return -1;
395 }
396
397 static const struct inet6_protocol frag_protocol = {
398         .handler        =       ipv6_frag_rcv,
399         .flags          =       INET6_PROTO_NOPOLICY,
400 };
401
402 #ifdef CONFIG_SYSCTL
403
404 static struct ctl_table ip6_frags_ns_ctl_table[] = {
405         {
406                 .procname       = "ip6frag_high_thresh",
407                 .data           = &init_net.ipv6.frags.high_thresh,
408                 .maxlen         = sizeof(unsigned long),
409                 .mode           = 0644,
410                 .proc_handler   = proc_doulongvec_minmax,
411                 .extra1         = &init_net.ipv6.frags.low_thresh
412         },
413         {
414                 .procname       = "ip6frag_low_thresh",
415                 .data           = &init_net.ipv6.frags.low_thresh,
416                 .maxlen         = sizeof(unsigned long),
417                 .mode           = 0644,
418                 .proc_handler   = proc_doulongvec_minmax,
419                 .extra2         = &init_net.ipv6.frags.high_thresh
420         },
421         {
422                 .procname       = "ip6frag_time",
423                 .data           = &init_net.ipv6.frags.timeout,
424                 .maxlen         = sizeof(int),
425                 .mode           = 0644,
426                 .proc_handler   = proc_dointvec_jiffies,
427         },
428         { }
429 };
430
431 /* secret interval has been deprecated */
432 static int ip6_frags_secret_interval_unused;
433 static struct ctl_table ip6_frags_ctl_table[] = {
434         {
435                 .procname       = "ip6frag_secret_interval",
436                 .data           = &ip6_frags_secret_interval_unused,
437                 .maxlen         = sizeof(int),
438                 .mode           = 0644,
439                 .proc_handler   = proc_dointvec_jiffies,
440         },
441         { }
442 };
443
444 static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
445 {
446         struct ctl_table *table;
447         struct ctl_table_header *hdr;
448
449         table = ip6_frags_ns_ctl_table;
450         if (!net_eq(net, &init_net)) {
451                 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
452                 if (!table)
453                         goto err_alloc;
454
455                 table[0].data = &net->ipv6.frags.high_thresh;
456                 table[0].extra1 = &net->ipv6.frags.low_thresh;
457                 table[0].extra2 = &init_net.ipv6.frags.high_thresh;
458                 table[1].data = &net->ipv6.frags.low_thresh;
459                 table[1].extra2 = &net->ipv6.frags.high_thresh;
460                 table[2].data = &net->ipv6.frags.timeout;
461         }
462
463         hdr = register_net_sysctl(net, "net/ipv6", table);
464         if (!hdr)
465                 goto err_reg;
466
467         net->ipv6.sysctl.frags_hdr = hdr;
468         return 0;
469
470 err_reg:
471         if (!net_eq(net, &init_net))
472                 kfree(table);
473 err_alloc:
474         return -ENOMEM;
475 }
476
477 static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
478 {
479         struct ctl_table *table;
480
481         table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
482         unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
483         if (!net_eq(net, &init_net))
484                 kfree(table);
485 }
486
487 static struct ctl_table_header *ip6_ctl_header;
488
489 static int ip6_frags_sysctl_register(void)
490 {
491         ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
492                         ip6_frags_ctl_table);
493         return ip6_ctl_header == NULL ? -ENOMEM : 0;
494 }
495
496 static void ip6_frags_sysctl_unregister(void)
497 {
498         unregister_net_sysctl_table(ip6_ctl_header);
499 }
500 #else
501 static int ip6_frags_ns_sysctl_register(struct net *net)
502 {
503         return 0;
504 }
505
506 static void ip6_frags_ns_sysctl_unregister(struct net *net)
507 {
508 }
509
510 static int ip6_frags_sysctl_register(void)
511 {
512         return 0;
513 }
514
515 static void ip6_frags_sysctl_unregister(void)
516 {
517 }
518 #endif
519
520 static int __net_init ipv6_frags_init_net(struct net *net)
521 {
522         int res;
523
524         net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
525         net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
526         net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
527         net->ipv6.frags.f = &ip6_frags;
528
529         res = inet_frags_init_net(&net->ipv6.frags);
530         if (res < 0)
531                 return res;
532
533         res = ip6_frags_ns_sysctl_register(net);
534         if (res < 0)
535                 inet_frags_exit_net(&net->ipv6.frags);
536         return res;
537 }
538
539 static void __net_exit ipv6_frags_exit_net(struct net *net)
540 {
541         ip6_frags_ns_sysctl_unregister(net);
542         inet_frags_exit_net(&net->ipv6.frags);
543 }
544
545 static struct pernet_operations ip6_frags_ops = {
546         .init = ipv6_frags_init_net,
547         .exit = ipv6_frags_exit_net,
548 };
549
550 static const struct rhashtable_params ip6_rhash_params = {
551         .head_offset            = offsetof(struct inet_frag_queue, node),
552         .hashfn                 = ip6frag_key_hashfn,
553         .obj_hashfn             = ip6frag_obj_hashfn,
554         .obj_cmpfn              = ip6frag_obj_cmpfn,
555         .automatic_shrinking    = true,
556 };
557
558 int __init ipv6_frag_init(void)
559 {
560         int ret;
561
562         ip6_frags.constructor = ip6frag_init;
563         ip6_frags.destructor = NULL;
564         ip6_frags.qsize = sizeof(struct frag_queue);
565         ip6_frags.frag_expire = ip6_frag_expire;
566         ip6_frags.frags_cache_name = ip6_frag_cache_name;
567         ip6_frags.rhash_params = ip6_rhash_params;
568         ret = inet_frags_init(&ip6_frags);
569         if (ret)
570                 goto out;
571
572         ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
573         if (ret)
574                 goto err_protocol;
575
576         ret = ip6_frags_sysctl_register();
577         if (ret)
578                 goto err_sysctl;
579
580         ret = register_pernet_subsys(&ip6_frags_ops);
581         if (ret)
582                 goto err_pernet;
583
584 out:
585         return ret;
586
587 err_pernet:
588         ip6_frags_sysctl_unregister();
589 err_sysctl:
590         inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
591 err_protocol:
592         inet_frags_fini(&ip6_frags);
593         goto out;
594 }
595
596 void ipv6_frag_exit(void)
597 {
598         ip6_frags_sysctl_unregister();
599         unregister_pernet_subsys(&ip6_frags_ops);
600         inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
601         inet_frags_fini(&ip6_frags);
602 }