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