]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/netfilter/nf_conntrack_reasm.c
inet: frags: break the 2GB limit for frags storage
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
1 /*
2 * IPv6 fragment reassembly for connection tracking
3 *
4 * Copyright (C)2004 USAGI/WIDE Project
5 *
6 * Author:
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8 *
9 * Based on: net/ipv6/reassembly.c
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17 #define pr_fmt(fmt) "IPv6-nf: " fmt
18
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/jiffies.h>
25 #include <linux/net.h>
26 #include <linux/list.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/ipv6.h>
30 #include <linux/icmpv6.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
33
34 #include <net/sock.h>
35 #include <net/snmp.h>
36 #include <net/inet_frag.h>
37
38 #include <net/ipv6.h>
39 #include <net/protocol.h>
40 #include <net/transp_v6.h>
41 #include <net/rawv6.h>
42 #include <net/ndisc.h>
43 #include <net/addrconf.h>
44 #include <net/inet_ecn.h>
45 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
46 #include <linux/sysctl.h>
47 #include <linux/netfilter.h>
48 #include <linux/netfilter_ipv6.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
52
53 static const char nf_frags_cache_name[] = "nf-frags";
54
55 struct nf_ct_frag6_skb_cb
56 {
57 struct inet6_skb_parm h;
58 int offset;
59 };
60
61 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb *)((skb)->cb))
62
63 static struct inet_frags nf_frags;
64
65 #ifdef CONFIG_SYSCTL
66 static long zero;
67
68 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
69 {
70 .procname = "nf_conntrack_frag6_timeout",
71 .data = &init_net.nf_frag.frags.timeout,
72 .maxlen = sizeof(unsigned int),
73 .mode = 0644,
74 .proc_handler = proc_dointvec_jiffies,
75 },
76 {
77 .procname = "nf_conntrack_frag6_low_thresh",
78 .data = &init_net.nf_frag.frags.low_thresh,
79 .maxlen = sizeof(unsigned long),
80 .mode = 0644,
81 .proc_handler = proc_doulongvec_minmax,
82 .extra1 = &zero,
83 .extra2 = &init_net.nf_frag.frags.high_thresh
84 },
85 {
86 .procname = "nf_conntrack_frag6_high_thresh",
87 .data = &init_net.nf_frag.frags.high_thresh,
88 .maxlen = sizeof(unsigned long),
89 .mode = 0644,
90 .proc_handler = proc_doulongvec_minmax,
91 .extra1 = &init_net.nf_frag.frags.low_thresh
92 },
93 { }
94 };
95
96 static int nf_ct_frag6_sysctl_register(struct net *net)
97 {
98 struct ctl_table *table;
99 struct ctl_table_header *hdr;
100
101 table = nf_ct_frag6_sysctl_table;
102 if (!net_eq(net, &init_net)) {
103 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
104 GFP_KERNEL);
105 if (table == NULL)
106 goto err_alloc;
107
108 table[0].data = &net->nf_frag.frags.timeout;
109 table[1].data = &net->nf_frag.frags.low_thresh;
110 table[1].extra2 = &net->nf_frag.frags.high_thresh;
111 table[2].data = &net->nf_frag.frags.high_thresh;
112 table[2].extra1 = &net->nf_frag.frags.low_thresh;
113 table[2].extra2 = &init_net.nf_frag.frags.high_thresh;
114 }
115
116 hdr = register_net_sysctl(net, "net/netfilter", table);
117 if (hdr == NULL)
118 goto err_reg;
119
120 net->nf_frag_frags_hdr = hdr;
121 return 0;
122
123 err_reg:
124 if (!net_eq(net, &init_net))
125 kfree(table);
126 err_alloc:
127 return -ENOMEM;
128 }
129
130 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
131 {
132 struct ctl_table *table;
133
134 table = net->nf_frag_frags_hdr->ctl_table_arg;
135 unregister_net_sysctl_table(net->nf_frag_frags_hdr);
136 if (!net_eq(net, &init_net))
137 kfree(table);
138 }
139
140 #else
141 static int nf_ct_frag6_sysctl_register(struct net *net)
142 {
143 return 0;
144 }
145 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
146 {
147 }
148 #endif
149
150 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
151 {
152 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
153 }
154
155 static void nf_ct_frag6_expire(struct timer_list *t)
156 {
157 struct inet_frag_queue *frag = from_timer(frag, t, timer);
158 struct frag_queue *fq;
159 struct net *net;
160
161 fq = container_of(frag, struct frag_queue, q);
162 net = container_of(fq->q.net, struct net, nf_frag.frags);
163
164 ip6_expire_frag_queue(net, fq);
165 }
166
167 /* Creation primitives. */
168 static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
169 const struct ipv6hdr *hdr, int iif)
170 {
171 struct frag_v6_compare_key key = {
172 .id = id,
173 .saddr = hdr->saddr,
174 .daddr = hdr->daddr,
175 .user = user,
176 .iif = iif,
177 };
178 struct inet_frag_queue *q;
179
180 q = inet_frag_find(&net->nf_frag.frags, &key);
181 if (!q)
182 return NULL;
183
184 return container_of(q, struct frag_queue, q);
185 }
186
187
188 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
189 const struct frag_hdr *fhdr, int nhoff)
190 {
191 struct sk_buff *prev, *next;
192 unsigned int payload_len;
193 int offset, end;
194 u8 ecn;
195
196 if (fq->q.flags & INET_FRAG_COMPLETE) {
197 pr_debug("Already completed\n");
198 goto err;
199 }
200
201 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
202
203 offset = ntohs(fhdr->frag_off) & ~0x7;
204 end = offset + (payload_len -
205 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
206
207 if ((unsigned int)end > IPV6_MAXPLEN) {
208 pr_debug("offset is too large.\n");
209 return -EINVAL;
210 }
211
212 ecn = ip6_frag_ecn(ipv6_hdr(skb));
213
214 if (skb->ip_summed == CHECKSUM_COMPLETE) {
215 const unsigned char *nh = skb_network_header(skb);
216 skb->csum = csum_sub(skb->csum,
217 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
218 0));
219 }
220
221 /* Is this the final fragment? */
222 if (!(fhdr->frag_off & htons(IP6_MF))) {
223 /* If we already have some bits beyond end
224 * or have different end, the segment is corrupted.
225 */
226 if (end < fq->q.len ||
227 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
228 pr_debug("already received last fragment\n");
229 goto err;
230 }
231 fq->q.flags |= INET_FRAG_LAST_IN;
232 fq->q.len = end;
233 } else {
234 /* Check if the fragment is rounded to 8 bytes.
235 * Required by the RFC.
236 */
237 if (end & 0x7) {
238 /* RFC2460 says always send parameter problem in
239 * this case. -DaveM
240 */
241 pr_debug("end of fragment not rounded to 8 bytes.\n");
242 inet_frag_kill(&fq->q);
243 return -EPROTO;
244 }
245 if (end > fq->q.len) {
246 /* Some bits beyond end -> corruption. */
247 if (fq->q.flags & INET_FRAG_LAST_IN) {
248 pr_debug("last packet already reached.\n");
249 goto err;
250 }
251 fq->q.len = end;
252 }
253 }
254
255 if (end == offset)
256 goto err;
257
258 /* Point into the IP datagram 'data' part. */
259 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
260 pr_debug("queue: message is too short.\n");
261 goto err;
262 }
263 if (pskb_trim_rcsum(skb, end - offset)) {
264 pr_debug("Can't trim\n");
265 goto err;
266 }
267
268 /* Find out which fragments are in front and at the back of us
269 * in the chain of fragments so far. We must know where to put
270 * this fragment, right?
271 */
272 prev = fq->q.fragments_tail;
273 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
274 next = NULL;
275 goto found;
276 }
277 prev = NULL;
278 for (next = fq->q.fragments; next != NULL; next = next->next) {
279 if (NFCT_FRAG6_CB(next)->offset >= offset)
280 break; /* bingo! */
281 prev = next;
282 }
283
284 found:
285 /* RFC5722, Section 4:
286 * When reassembling an IPv6 datagram, if
287 * one or more its constituent fragments is determined to be an
288 * overlapping fragment, the entire datagram (and any constituent
289 * fragments, including those not yet received) MUST be silently
290 * discarded.
291 */
292
293 /* Check for overlap with preceding fragment. */
294 if (prev &&
295 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
296 goto discard_fq;
297
298 /* Look for overlap with succeeding segment. */
299 if (next && NFCT_FRAG6_CB(next)->offset < end)
300 goto discard_fq;
301
302 NFCT_FRAG6_CB(skb)->offset = offset;
303
304 /* Insert this fragment in the chain of fragments. */
305 skb->next = next;
306 if (!next)
307 fq->q.fragments_tail = skb;
308 if (prev)
309 prev->next = skb;
310 else
311 fq->q.fragments = skb;
312
313 if (skb->dev) {
314 fq->iif = skb->dev->ifindex;
315 skb->dev = NULL;
316 }
317 fq->q.stamp = skb->tstamp;
318 fq->q.meat += skb->len;
319 fq->ecn |= ecn;
320 if (payload_len > fq->q.max_size)
321 fq->q.max_size = payload_len;
322 add_frag_mem_limit(fq->q.net, skb->truesize);
323
324 /* The first fragment.
325 * nhoffset is obtained from the first fragment, of course.
326 */
327 if (offset == 0) {
328 fq->nhoffset = nhoff;
329 fq->q.flags |= INET_FRAG_FIRST_IN;
330 }
331
332 return 0;
333
334 discard_fq:
335 inet_frag_kill(&fq->q);
336 err:
337 return -EINVAL;
338 }
339
340 /*
341 * Check if this packet is complete.
342 *
343 * It is called with locked fq, and caller must check that
344 * queue is eligible for reassembly i.e. it is not COMPLETE,
345 * the last and the first frames arrived and all the bits are here.
346 *
347 * returns true if *prev skb has been transformed into the reassembled
348 * skb, false otherwise.
349 */
350 static bool
351 nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev, struct net_device *dev)
352 {
353 struct sk_buff *fp, *head = fq->q.fragments;
354 int payload_len;
355 u8 ecn;
356
357 inet_frag_kill(&fq->q);
358
359 WARN_ON(head == NULL);
360 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
361
362 ecn = ip_frag_ecn_table[fq->ecn];
363 if (unlikely(ecn == 0xff))
364 return false;
365
366 /* Unfragmented part is taken from the first segment. */
367 payload_len = ((head->data - skb_network_header(head)) -
368 sizeof(struct ipv6hdr) + fq->q.len -
369 sizeof(struct frag_hdr));
370 if (payload_len > IPV6_MAXPLEN) {
371 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
372 payload_len);
373 return false;
374 }
375
376 /* Head of list must not be cloned. */
377 if (skb_unclone(head, GFP_ATOMIC))
378 return false;
379
380 /* If the first fragment is fragmented itself, we split
381 * it to two chunks: the first with data and paged part
382 * and the second, holding only fragments. */
383 if (skb_has_frag_list(head)) {
384 struct sk_buff *clone;
385 int i, plen = 0;
386
387 clone = alloc_skb(0, GFP_ATOMIC);
388 if (clone == NULL)
389 return false;
390
391 clone->next = head->next;
392 head->next = clone;
393 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
394 skb_frag_list_init(head);
395 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
396 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
397 clone->len = clone->data_len = head->data_len - plen;
398 head->data_len -= clone->len;
399 head->len -= clone->len;
400 clone->csum = 0;
401 clone->ip_summed = head->ip_summed;
402
403 add_frag_mem_limit(fq->q.net, clone->truesize);
404 }
405
406 /* morph head into last received skb: prev.
407 *
408 * This allows callers of ipv6 conntrack defrag to continue
409 * to use the last skb(frag) passed into the reasm engine.
410 * The last skb frag 'silently' turns into the full reassembled skb.
411 *
412 * Since prev is also part of q->fragments we have to clone it first.
413 */
414 if (head != prev) {
415 struct sk_buff *iter;
416
417 fp = skb_clone(prev, GFP_ATOMIC);
418 if (!fp)
419 return false;
420
421 fp->next = prev->next;
422
423 iter = head;
424 while (iter) {
425 if (iter->next == prev) {
426 iter->next = fp;
427 break;
428 }
429 iter = iter->next;
430 }
431
432 skb_morph(prev, head);
433 prev->next = head->next;
434 consume_skb(head);
435 head = prev;
436 }
437
438 /* We have to remove fragment header from datagram and to relocate
439 * header in order to calculate ICV correctly. */
440 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
441 memmove(head->head + sizeof(struct frag_hdr), head->head,
442 (head->data - head->head) - sizeof(struct frag_hdr));
443 head->mac_header += sizeof(struct frag_hdr);
444 head->network_header += sizeof(struct frag_hdr);
445
446 skb_shinfo(head)->frag_list = head->next;
447 skb_reset_transport_header(head);
448 skb_push(head, head->data - skb_network_header(head));
449
450 for (fp = head->next; fp; fp = fp->next) {
451 head->data_len += fp->len;
452 head->len += fp->len;
453 if (head->ip_summed != fp->ip_summed)
454 head->ip_summed = CHECKSUM_NONE;
455 else if (head->ip_summed == CHECKSUM_COMPLETE)
456 head->csum = csum_add(head->csum, fp->csum);
457 head->truesize += fp->truesize;
458 }
459 sub_frag_mem_limit(fq->q.net, head->truesize);
460
461 head->ignore_df = 1;
462 head->next = NULL;
463 head->dev = dev;
464 head->tstamp = fq->q.stamp;
465 ipv6_hdr(head)->payload_len = htons(payload_len);
466 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
467 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
468
469 /* Yes, and fold redundant checksum back. 8) */
470 if (head->ip_summed == CHECKSUM_COMPLETE)
471 head->csum = csum_partial(skb_network_header(head),
472 skb_network_header_len(head),
473 head->csum);
474
475 fq->q.fragments = NULL;
476 fq->q.fragments_tail = NULL;
477
478 return true;
479 }
480
481 /*
482 * find the header just before Fragment Header.
483 *
484 * if success return 0 and set ...
485 * (*prevhdrp): the value of "Next Header Field" in the header
486 * just before Fragment Header.
487 * (*prevhoff): the offset of "Next Header Field" in the header
488 * just before Fragment Header.
489 * (*fhoff) : the offset of Fragment Header.
490 *
491 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
492 *
493 */
494 static int
495 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
496 {
497 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
498 const int netoff = skb_network_offset(skb);
499 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
500 int start = netoff + sizeof(struct ipv6hdr);
501 int len = skb->len - start;
502 u8 prevhdr = NEXTHDR_IPV6;
503
504 while (nexthdr != NEXTHDR_FRAGMENT) {
505 struct ipv6_opt_hdr hdr;
506 int hdrlen;
507
508 if (!ipv6_ext_hdr(nexthdr)) {
509 return -1;
510 }
511 if (nexthdr == NEXTHDR_NONE) {
512 pr_debug("next header is none\n");
513 return -1;
514 }
515 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
516 pr_debug("too short\n");
517 return -1;
518 }
519 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
520 BUG();
521 if (nexthdr == NEXTHDR_AUTH)
522 hdrlen = (hdr.hdrlen+2)<<2;
523 else
524 hdrlen = ipv6_optlen(&hdr);
525
526 prevhdr = nexthdr;
527 prev_nhoff = start;
528
529 nexthdr = hdr.nexthdr;
530 len -= hdrlen;
531 start += hdrlen;
532 }
533
534 if (len < 0)
535 return -1;
536
537 *prevhdrp = prevhdr;
538 *prevhoff = prev_nhoff;
539 *fhoff = start;
540
541 return 0;
542 }
543
544 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
545 {
546 u16 savethdr = skb->transport_header;
547 struct net_device *dev = skb->dev;
548 int fhoff, nhoff, ret;
549 struct frag_hdr *fhdr;
550 struct frag_queue *fq;
551 struct ipv6hdr *hdr;
552 u8 prevhdr;
553
554 /* Jumbo payload inhibits frag. header */
555 if (ipv6_hdr(skb)->payload_len == 0) {
556 pr_debug("payload len = 0\n");
557 return 0;
558 }
559
560 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
561 return 0;
562
563 if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
564 return -ENOMEM;
565
566 skb_set_transport_header(skb, fhoff);
567 hdr = ipv6_hdr(skb);
568 fhdr = (struct frag_hdr *)skb_transport_header(skb);
569
570 skb_orphan(skb);
571 fq = fq_find(net, fhdr->identification, user, hdr,
572 skb->dev ? skb->dev->ifindex : 0);
573 if (fq == NULL) {
574 pr_debug("Can't find and can't create new queue\n");
575 return -ENOMEM;
576 }
577
578 spin_lock_bh(&fq->q.lock);
579
580 ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
581 if (ret < 0) {
582 if (ret == -EPROTO) {
583 skb->transport_header = savethdr;
584 ret = 0;
585 }
586 goto out_unlock;
587 }
588
589 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
590 * must be returned.
591 */
592 ret = -EINPROGRESS;
593 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
594 fq->q.meat == fq->q.len &&
595 nf_ct_frag6_reasm(fq, skb, dev))
596 ret = 0;
597 else
598 skb_dst_drop(skb);
599
600 out_unlock:
601 spin_unlock_bh(&fq->q.lock);
602 inet_frag_put(&fq->q);
603 return ret;
604 }
605 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
606
607 static int nf_ct_net_init(struct net *net)
608 {
609 int res;
610
611 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
612 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
613 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
614 net->nf_frag.frags.f = &nf_frags;
615
616 res = inet_frags_init_net(&net->nf_frag.frags);
617 if (res < 0)
618 return res;
619 res = nf_ct_frag6_sysctl_register(net);
620 if (res < 0)
621 inet_frags_exit_net(&net->nf_frag.frags);
622 return res;
623 }
624
625 static void nf_ct_net_exit(struct net *net)
626 {
627 nf_ct_frags6_sysctl_unregister(net);
628 inet_frags_exit_net(&net->nf_frag.frags);
629 }
630
631 static struct pernet_operations nf_ct_net_ops = {
632 .init = nf_ct_net_init,
633 .exit = nf_ct_net_exit,
634 };
635
636 int nf_ct_frag6_init(void)
637 {
638 int ret = 0;
639
640 nf_frags.constructor = ip6_frag_init;
641 nf_frags.destructor = NULL;
642 nf_frags.qsize = sizeof(struct frag_queue);
643 nf_frags.frag_expire = nf_ct_frag6_expire;
644 nf_frags.frags_cache_name = nf_frags_cache_name;
645 nf_frags.rhash_params = ip6_rhash_params;
646 ret = inet_frags_init(&nf_frags);
647 if (ret)
648 goto out;
649 ret = register_pernet_subsys(&nf_ct_net_ops);
650 if (ret)
651 inet_frags_fini(&nf_frags);
652
653 out:
654 return ret;
655 }
656
657 void nf_ct_frag6_cleanup(void)
658 {
659 unregister_pernet_subsys(&nf_ct_net_ops);
660 inet_frags_fini(&nf_frags);
661 }