]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/netfilter/nf_conntrack_reasm.c
inet: frags: add a pointer to struct netns_frags
[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 int 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 int),
80 .mode = 0644,
81 .proc_handler = proc_dointvec_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 int),
89 .mode = 0644,
90 .proc_handler = proc_dointvec_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 unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
156 const struct in6_addr *daddr)
157 {
158 net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
159 return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
160 (__force u32)id, nf_frags.rnd);
161 }
162
163
164 static unsigned int nf_hashfn(const struct inet_frag_queue *q)
165 {
166 const struct frag_queue *nq;
167
168 nq = container_of(q, struct frag_queue, q);
169 return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
170 }
171
172 static void nf_ct_frag6_expire(struct timer_list *t)
173 {
174 struct inet_frag_queue *frag = from_timer(frag, t, timer);
175 struct frag_queue *fq;
176 struct net *net;
177
178 fq = container_of(frag, struct frag_queue, q);
179 net = container_of(fq->q.net, struct net, nf_frag.frags);
180
181 ip6_expire_frag_queue(net, fq);
182 }
183
184 /* Creation primitives. */
185 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
186 u32 user, struct in6_addr *src,
187 struct in6_addr *dst, int iif, u8 ecn)
188 {
189 struct inet_frag_queue *q;
190 struct ip6_create_arg arg;
191 unsigned int hash;
192
193 arg.id = id;
194 arg.user = user;
195 arg.src = src;
196 arg.dst = dst;
197 arg.iif = iif;
198 arg.ecn = ecn;
199
200 local_bh_disable();
201 hash = nf_hash_frag(id, src, dst);
202
203 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
204 local_bh_enable();
205 if (IS_ERR_OR_NULL(q)) {
206 inet_frag_maybe_warn_overflow(q, pr_fmt());
207 return NULL;
208 }
209 return container_of(q, struct frag_queue, q);
210 }
211
212
213 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
214 const struct frag_hdr *fhdr, int nhoff)
215 {
216 struct sk_buff *prev, *next;
217 unsigned int payload_len;
218 int offset, end;
219 u8 ecn;
220
221 if (fq->q.flags & INET_FRAG_COMPLETE) {
222 pr_debug("Already completed\n");
223 goto err;
224 }
225
226 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
227
228 offset = ntohs(fhdr->frag_off) & ~0x7;
229 end = offset + (payload_len -
230 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
231
232 if ((unsigned int)end > IPV6_MAXPLEN) {
233 pr_debug("offset is too large.\n");
234 return -EINVAL;
235 }
236
237 ecn = ip6_frag_ecn(ipv6_hdr(skb));
238
239 if (skb->ip_summed == CHECKSUM_COMPLETE) {
240 const unsigned char *nh = skb_network_header(skb);
241 skb->csum = csum_sub(skb->csum,
242 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
243 0));
244 }
245
246 /* Is this the final fragment? */
247 if (!(fhdr->frag_off & htons(IP6_MF))) {
248 /* If we already have some bits beyond end
249 * or have different end, the segment is corrupted.
250 */
251 if (end < fq->q.len ||
252 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
253 pr_debug("already received last fragment\n");
254 goto err;
255 }
256 fq->q.flags |= INET_FRAG_LAST_IN;
257 fq->q.len = end;
258 } else {
259 /* Check if the fragment is rounded to 8 bytes.
260 * Required by the RFC.
261 */
262 if (end & 0x7) {
263 /* RFC2460 says always send parameter problem in
264 * this case. -DaveM
265 */
266 pr_debug("end of fragment not rounded to 8 bytes.\n");
267 inet_frag_kill(&fq->q);
268 return -EPROTO;
269 }
270 if (end > fq->q.len) {
271 /* Some bits beyond end -> corruption. */
272 if (fq->q.flags & INET_FRAG_LAST_IN) {
273 pr_debug("last packet already reached.\n");
274 goto err;
275 }
276 fq->q.len = end;
277 }
278 }
279
280 if (end == offset)
281 goto err;
282
283 /* Point into the IP datagram 'data' part. */
284 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
285 pr_debug("queue: message is too short.\n");
286 goto err;
287 }
288 if (pskb_trim_rcsum(skb, end - offset)) {
289 pr_debug("Can't trim\n");
290 goto err;
291 }
292
293 /* Find out which fragments are in front and at the back of us
294 * in the chain of fragments so far. We must know where to put
295 * this fragment, right?
296 */
297 prev = fq->q.fragments_tail;
298 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
299 next = NULL;
300 goto found;
301 }
302 prev = NULL;
303 for (next = fq->q.fragments; next != NULL; next = next->next) {
304 if (NFCT_FRAG6_CB(next)->offset >= offset)
305 break; /* bingo! */
306 prev = next;
307 }
308
309 found:
310 /* RFC5722, Section 4:
311 * When reassembling an IPv6 datagram, if
312 * one or more its constituent fragments is determined to be an
313 * overlapping fragment, the entire datagram (and any constituent
314 * fragments, including those not yet received) MUST be silently
315 * discarded.
316 */
317
318 /* Check for overlap with preceding fragment. */
319 if (prev &&
320 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
321 goto discard_fq;
322
323 /* Look for overlap with succeeding segment. */
324 if (next && NFCT_FRAG6_CB(next)->offset < end)
325 goto discard_fq;
326
327 NFCT_FRAG6_CB(skb)->offset = offset;
328
329 /* Insert this fragment in the chain of fragments. */
330 skb->next = next;
331 if (!next)
332 fq->q.fragments_tail = skb;
333 if (prev)
334 prev->next = skb;
335 else
336 fq->q.fragments = skb;
337
338 if (skb->dev) {
339 fq->iif = skb->dev->ifindex;
340 skb->dev = NULL;
341 }
342 fq->q.stamp = skb->tstamp;
343 fq->q.meat += skb->len;
344 fq->ecn |= ecn;
345 if (payload_len > fq->q.max_size)
346 fq->q.max_size = payload_len;
347 add_frag_mem_limit(fq->q.net, skb->truesize);
348
349 /* The first fragment.
350 * nhoffset is obtained from the first fragment, of course.
351 */
352 if (offset == 0) {
353 fq->nhoffset = nhoff;
354 fq->q.flags |= INET_FRAG_FIRST_IN;
355 }
356
357 return 0;
358
359 discard_fq:
360 inet_frag_kill(&fq->q);
361 err:
362 return -EINVAL;
363 }
364
365 /*
366 * Check if this packet is complete.
367 *
368 * It is called with locked fq, and caller must check that
369 * queue is eligible for reassembly i.e. it is not COMPLETE,
370 * the last and the first frames arrived and all the bits are here.
371 *
372 * returns true if *prev skb has been transformed into the reassembled
373 * skb, false otherwise.
374 */
375 static bool
376 nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev, struct net_device *dev)
377 {
378 struct sk_buff *fp, *head = fq->q.fragments;
379 int payload_len;
380 u8 ecn;
381
382 inet_frag_kill(&fq->q);
383
384 WARN_ON(head == NULL);
385 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
386
387 ecn = ip_frag_ecn_table[fq->ecn];
388 if (unlikely(ecn == 0xff))
389 return false;
390
391 /* Unfragmented part is taken from the first segment. */
392 payload_len = ((head->data - skb_network_header(head)) -
393 sizeof(struct ipv6hdr) + fq->q.len -
394 sizeof(struct frag_hdr));
395 if (payload_len > IPV6_MAXPLEN) {
396 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
397 payload_len);
398 return false;
399 }
400
401 /* Head of list must not be cloned. */
402 if (skb_unclone(head, GFP_ATOMIC))
403 return false;
404
405 /* If the first fragment is fragmented itself, we split
406 * it to two chunks: the first with data and paged part
407 * and the second, holding only fragments. */
408 if (skb_has_frag_list(head)) {
409 struct sk_buff *clone;
410 int i, plen = 0;
411
412 clone = alloc_skb(0, GFP_ATOMIC);
413 if (clone == NULL)
414 return false;
415
416 clone->next = head->next;
417 head->next = clone;
418 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
419 skb_frag_list_init(head);
420 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
421 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
422 clone->len = clone->data_len = head->data_len - plen;
423 head->data_len -= clone->len;
424 head->len -= clone->len;
425 clone->csum = 0;
426 clone->ip_summed = head->ip_summed;
427
428 add_frag_mem_limit(fq->q.net, clone->truesize);
429 }
430
431 /* morph head into last received skb: prev.
432 *
433 * This allows callers of ipv6 conntrack defrag to continue
434 * to use the last skb(frag) passed into the reasm engine.
435 * The last skb frag 'silently' turns into the full reassembled skb.
436 *
437 * Since prev is also part of q->fragments we have to clone it first.
438 */
439 if (head != prev) {
440 struct sk_buff *iter;
441
442 fp = skb_clone(prev, GFP_ATOMIC);
443 if (!fp)
444 return false;
445
446 fp->next = prev->next;
447
448 iter = head;
449 while (iter) {
450 if (iter->next == prev) {
451 iter->next = fp;
452 break;
453 }
454 iter = iter->next;
455 }
456
457 skb_morph(prev, head);
458 prev->next = head->next;
459 consume_skb(head);
460 head = prev;
461 }
462
463 /* We have to remove fragment header from datagram and to relocate
464 * header in order to calculate ICV correctly. */
465 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
466 memmove(head->head + sizeof(struct frag_hdr), head->head,
467 (head->data - head->head) - sizeof(struct frag_hdr));
468 head->mac_header += sizeof(struct frag_hdr);
469 head->network_header += sizeof(struct frag_hdr);
470
471 skb_shinfo(head)->frag_list = head->next;
472 skb_reset_transport_header(head);
473 skb_push(head, head->data - skb_network_header(head));
474
475 for (fp = head->next; fp; fp = fp->next) {
476 head->data_len += fp->len;
477 head->len += fp->len;
478 if (head->ip_summed != fp->ip_summed)
479 head->ip_summed = CHECKSUM_NONE;
480 else if (head->ip_summed == CHECKSUM_COMPLETE)
481 head->csum = csum_add(head->csum, fp->csum);
482 head->truesize += fp->truesize;
483 }
484 sub_frag_mem_limit(fq->q.net, head->truesize);
485
486 head->ignore_df = 1;
487 head->next = NULL;
488 head->dev = dev;
489 head->tstamp = fq->q.stamp;
490 ipv6_hdr(head)->payload_len = htons(payload_len);
491 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
492 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
493
494 /* Yes, and fold redundant checksum back. 8) */
495 if (head->ip_summed == CHECKSUM_COMPLETE)
496 head->csum = csum_partial(skb_network_header(head),
497 skb_network_header_len(head),
498 head->csum);
499
500 fq->q.fragments = NULL;
501 fq->q.fragments_tail = NULL;
502
503 return true;
504 }
505
506 /*
507 * find the header just before Fragment Header.
508 *
509 * if success return 0 and set ...
510 * (*prevhdrp): the value of "Next Header Field" in the header
511 * just before Fragment Header.
512 * (*prevhoff): the offset of "Next Header Field" in the header
513 * just before Fragment Header.
514 * (*fhoff) : the offset of Fragment Header.
515 *
516 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
517 *
518 */
519 static int
520 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
521 {
522 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
523 const int netoff = skb_network_offset(skb);
524 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
525 int start = netoff + sizeof(struct ipv6hdr);
526 int len = skb->len - start;
527 u8 prevhdr = NEXTHDR_IPV6;
528
529 while (nexthdr != NEXTHDR_FRAGMENT) {
530 struct ipv6_opt_hdr hdr;
531 int hdrlen;
532
533 if (!ipv6_ext_hdr(nexthdr)) {
534 return -1;
535 }
536 if (nexthdr == NEXTHDR_NONE) {
537 pr_debug("next header is none\n");
538 return -1;
539 }
540 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
541 pr_debug("too short\n");
542 return -1;
543 }
544 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
545 BUG();
546 if (nexthdr == NEXTHDR_AUTH)
547 hdrlen = (hdr.hdrlen+2)<<2;
548 else
549 hdrlen = ipv6_optlen(&hdr);
550
551 prevhdr = nexthdr;
552 prev_nhoff = start;
553
554 nexthdr = hdr.nexthdr;
555 len -= hdrlen;
556 start += hdrlen;
557 }
558
559 if (len < 0)
560 return -1;
561
562 *prevhdrp = prevhdr;
563 *prevhoff = prev_nhoff;
564 *fhoff = start;
565
566 return 0;
567 }
568
569 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
570 {
571 u16 savethdr = skb->transport_header;
572 struct net_device *dev = skb->dev;
573 int fhoff, nhoff, ret;
574 struct frag_hdr *fhdr;
575 struct frag_queue *fq;
576 struct ipv6hdr *hdr;
577 u8 prevhdr;
578
579 /* Jumbo payload inhibits frag. header */
580 if (ipv6_hdr(skb)->payload_len == 0) {
581 pr_debug("payload len = 0\n");
582 return 0;
583 }
584
585 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
586 return 0;
587
588 if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
589 return -ENOMEM;
590
591 skb_set_transport_header(skb, fhoff);
592 hdr = ipv6_hdr(skb);
593 fhdr = (struct frag_hdr *)skb_transport_header(skb);
594
595 skb_orphan(skb);
596 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
597 skb->dev ? skb->dev->ifindex : 0, ip6_frag_ecn(hdr));
598 if (fq == NULL) {
599 pr_debug("Can't find and can't create new queue\n");
600 return -ENOMEM;
601 }
602
603 spin_lock_bh(&fq->q.lock);
604
605 ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
606 if (ret < 0) {
607 if (ret == -EPROTO) {
608 skb->transport_header = savethdr;
609 ret = 0;
610 }
611 goto out_unlock;
612 }
613
614 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
615 * must be returned.
616 */
617 ret = -EINPROGRESS;
618 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
619 fq->q.meat == fq->q.len &&
620 nf_ct_frag6_reasm(fq, skb, dev))
621 ret = 0;
622 else
623 skb_dst_drop(skb);
624
625 out_unlock:
626 spin_unlock_bh(&fq->q.lock);
627 inet_frag_put(&fq->q);
628 return ret;
629 }
630 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
631
632 static int nf_ct_net_init(struct net *net)
633 {
634 int res;
635
636 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
637 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
638 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
639 net->nf_frag.frags.f = &nf_frags;
640
641 res = inet_frags_init_net(&net->nf_frag.frags);
642 if (res < 0)
643 return res;
644 res = nf_ct_frag6_sysctl_register(net);
645 if (res < 0)
646 inet_frags_exit_net(&net->nf_frag.frags);
647 return res;
648 }
649
650 static void nf_ct_net_exit(struct net *net)
651 {
652 nf_ct_frags6_sysctl_unregister(net);
653 inet_frags_exit_net(&net->nf_frag.frags);
654 }
655
656 static struct pernet_operations nf_ct_net_ops = {
657 .init = nf_ct_net_init,
658 .exit = nf_ct_net_exit,
659 };
660
661 int nf_ct_frag6_init(void)
662 {
663 int ret = 0;
664
665 nf_frags.hashfn = nf_hashfn;
666 nf_frags.constructor = ip6_frag_init;
667 nf_frags.destructor = NULL;
668 nf_frags.qsize = sizeof(struct frag_queue);
669 nf_frags.match = ip6_frag_match;
670 nf_frags.frag_expire = nf_ct_frag6_expire;
671 nf_frags.frags_cache_name = nf_frags_cache_name;
672 ret = inet_frags_init(&nf_frags);
673 if (ret)
674 goto out;
675 ret = register_pernet_subsys(&nf_ct_net_ops);
676 if (ret)
677 inet_frags_fini(&nf_frags);
678
679 out:
680 return ret;
681 }
682
683 void nf_ct_frag6_cleanup(void)
684 {
685 unregister_pernet_subsys(&nf_ct_net_ops);
686 inet_frags_fini(&nf_frags);
687 }