2 * IPv6 fragment reassembly for connection tracking
4 * Copyright (C)2004 USAGI/WIDE Project
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * Based on: net/ipv6/reassembly.c
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.
17 #define pr_fmt(fmt) "IPv6-nf: " fmt
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>
36 #include <net/inet_frag.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>
53 static const char nf_frags_cache_name[] = "nf-frags";
55 static struct inet_frags nf_frags;
59 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
61 .procname = "nf_conntrack_frag6_timeout",
62 .data = &init_net.nf_frag.frags.timeout,
63 .maxlen = sizeof(unsigned int),
65 .proc_handler = proc_dointvec_jiffies,
68 .procname = "nf_conntrack_frag6_low_thresh",
69 .data = &init_net.nf_frag.frags.low_thresh,
70 .maxlen = sizeof(unsigned long),
72 .proc_handler = proc_doulongvec_minmax,
73 .extra2 = &init_net.nf_frag.frags.high_thresh
76 .procname = "nf_conntrack_frag6_high_thresh",
77 .data = &init_net.nf_frag.frags.high_thresh,
78 .maxlen = sizeof(unsigned long),
80 .proc_handler = proc_doulongvec_minmax,
81 .extra1 = &init_net.nf_frag.frags.low_thresh
86 static int nf_ct_frag6_sysctl_register(struct net *net)
88 struct ctl_table *table;
89 struct ctl_table_header *hdr;
91 table = nf_ct_frag6_sysctl_table;
92 if (!net_eq(net, &init_net)) {
93 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
98 table[0].data = &net->nf_frag.frags.timeout;
99 table[1].data = &net->nf_frag.frags.low_thresh;
100 table[1].extra2 = &net->nf_frag.frags.high_thresh;
101 table[2].data = &net->nf_frag.frags.high_thresh;
102 table[2].extra1 = &net->nf_frag.frags.low_thresh;
103 table[2].extra2 = &init_net.nf_frag.frags.high_thresh;
106 hdr = register_net_sysctl(net, "net/netfilter", table);
110 net->nf_frag_frags_hdr = hdr;
114 if (!net_eq(net, &init_net))
120 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
122 struct ctl_table *table;
124 table = net->nf_frag_frags_hdr->ctl_table_arg;
125 unregister_net_sysctl_table(net->nf_frag_frags_hdr);
126 if (!net_eq(net, &init_net))
131 static int nf_ct_frag6_sysctl_register(struct net *net)
135 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
140 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
142 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
145 static void nf_ct_frag6_expire(struct timer_list *t)
147 struct inet_frag_queue *frag = from_timer(frag, t, timer);
148 struct frag_queue *fq;
151 fq = container_of(frag, struct frag_queue, q);
152 net = container_of(fq->q.net, struct net, nf_frag.frags);
154 ip6_expire_frag_queue(net, fq);
157 /* Creation primitives. */
158 static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
159 const struct ipv6hdr *hdr, int iif)
161 struct frag_v6_compare_key key = {
168 struct inet_frag_queue *q;
170 q = inet_frag_find(&net->nf_frag.frags, &key);
174 return container_of(q, struct frag_queue, q);
178 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
179 const struct frag_hdr *fhdr, int nhoff)
181 struct sk_buff *prev, *next;
182 unsigned int payload_len;
186 if (fq->q.flags & INET_FRAG_COMPLETE) {
187 pr_debug("Already completed\n");
191 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
193 offset = ntohs(fhdr->frag_off) & ~0x7;
194 end = offset + (payload_len -
195 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
197 if ((unsigned int)end > IPV6_MAXPLEN) {
198 pr_debug("offset is too large.\n");
202 ecn = ip6_frag_ecn(ipv6_hdr(skb));
204 if (skb->ip_summed == CHECKSUM_COMPLETE) {
205 const unsigned char *nh = skb_network_header(skb);
206 skb->csum = csum_sub(skb->csum,
207 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
211 /* Is this the final fragment? */
212 if (!(fhdr->frag_off & htons(IP6_MF))) {
213 /* If we already have some bits beyond end
214 * or have different end, the segment is corrupted.
216 if (end < fq->q.len ||
217 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
218 pr_debug("already received last fragment\n");
221 fq->q.flags |= INET_FRAG_LAST_IN;
224 /* Check if the fragment is rounded to 8 bytes.
225 * Required by the RFC.
228 /* RFC2460 says always send parameter problem in
231 pr_debug("end of fragment not rounded to 8 bytes.\n");
232 inet_frag_kill(&fq->q);
235 if (end > fq->q.len) {
236 /* Some bits beyond end -> corruption. */
237 if (fq->q.flags & INET_FRAG_LAST_IN) {
238 pr_debug("last packet already reached.\n");
248 /* Point into the IP datagram 'data' part. */
249 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
250 pr_debug("queue: message is too short.\n");
253 if (pskb_trim_rcsum(skb, end - offset)) {
254 pr_debug("Can't trim\n");
258 /* Find out which fragments are in front and at the back of us
259 * in the chain of fragments so far. We must know where to put
260 * this fragment, right?
262 prev = fq->q.fragments_tail;
263 if (!prev || prev->ip_defrag_offset < offset) {
268 for (next = fq->q.fragments; next != NULL; next = next->next) {
269 if (next->ip_defrag_offset >= offset)
275 /* RFC5722, Section 4:
276 * When reassembling an IPv6 datagram, if
277 * one or more its constituent fragments is determined to be an
278 * overlapping fragment, the entire datagram (and any constituent
279 * fragments, including those not yet received) MUST be silently
283 /* Check for overlap with preceding fragment. */
285 (prev->ip_defrag_offset + prev->len) > offset)
288 /* Look for overlap with succeeding segment. */
289 if (next && next->ip_defrag_offset < end)
292 /* Note : skb->ip_defrag_offset and skb->dev share the same location */
294 fq->iif = skb->dev->ifindex;
295 /* Makes sure compiler wont do silly aliasing games */
297 skb->ip_defrag_offset = offset;
299 /* Insert this fragment in the chain of fragments. */
302 fq->q.fragments_tail = skb;
306 fq->q.fragments = skb;
308 fq->q.stamp = skb->tstamp;
309 fq->q.meat += skb->len;
311 if (payload_len > fq->q.max_size)
312 fq->q.max_size = payload_len;
313 add_frag_mem_limit(fq->q.net, skb->truesize);
315 /* The first fragment.
316 * nhoffset is obtained from the first fragment, of course.
319 fq->nhoffset = nhoff;
320 fq->q.flags |= INET_FRAG_FIRST_IN;
326 inet_frag_kill(&fq->q);
332 * Check if this packet is complete.
334 * It is called with locked fq, and caller must check that
335 * queue is eligible for reassembly i.e. it is not COMPLETE,
336 * the last and the first frames arrived and all the bits are here.
338 * returns true if *prev skb has been transformed into the reassembled
339 * skb, false otherwise.
342 nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev, struct net_device *dev)
344 struct sk_buff *fp, *head = fq->q.fragments;
348 inet_frag_kill(&fq->q);
350 WARN_ON(head == NULL);
351 WARN_ON(head->ip_defrag_offset != 0);
353 ecn = ip_frag_ecn_table[fq->ecn];
354 if (unlikely(ecn == 0xff))
357 /* Unfragmented part is taken from the first segment. */
358 payload_len = ((head->data - skb_network_header(head)) -
359 sizeof(struct ipv6hdr) + fq->q.len -
360 sizeof(struct frag_hdr));
361 if (payload_len > IPV6_MAXPLEN) {
362 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
367 /* Head of list must not be cloned. */
368 if (skb_unclone(head, GFP_ATOMIC))
371 /* If the first fragment is fragmented itself, we split
372 * it to two chunks: the first with data and paged part
373 * and the second, holding only fragments. */
374 if (skb_has_frag_list(head)) {
375 struct sk_buff *clone;
378 clone = alloc_skb(0, GFP_ATOMIC);
382 clone->next = head->next;
384 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
385 skb_frag_list_init(head);
386 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
387 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
388 clone->len = clone->data_len = head->data_len - plen;
389 head->data_len -= clone->len;
390 head->len -= clone->len;
392 clone->ip_summed = head->ip_summed;
394 add_frag_mem_limit(fq->q.net, clone->truesize);
397 /* morph head into last received skb: prev.
399 * This allows callers of ipv6 conntrack defrag to continue
400 * to use the last skb(frag) passed into the reasm engine.
401 * The last skb frag 'silently' turns into the full reassembled skb.
403 * Since prev is also part of q->fragments we have to clone it first.
406 struct sk_buff *iter;
408 fp = skb_clone(prev, GFP_ATOMIC);
412 fp->next = prev->next;
416 if (iter->next == prev) {
423 skb_morph(prev, head);
424 prev->next = head->next;
429 /* We have to remove fragment header from datagram and to relocate
430 * header in order to calculate ICV correctly. */
431 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
432 memmove(head->head + sizeof(struct frag_hdr), head->head,
433 (head->data - head->head) - sizeof(struct frag_hdr));
434 head->mac_header += sizeof(struct frag_hdr);
435 head->network_header += sizeof(struct frag_hdr);
437 skb_shinfo(head)->frag_list = head->next;
438 skb_reset_transport_header(head);
439 skb_push(head, head->data - skb_network_header(head));
441 for (fp = head->next; fp; fp = fp->next) {
442 head->data_len += fp->len;
443 head->len += fp->len;
444 if (head->ip_summed != fp->ip_summed)
445 head->ip_summed = CHECKSUM_NONE;
446 else if (head->ip_summed == CHECKSUM_COMPLETE)
447 head->csum = csum_add(head->csum, fp->csum);
448 head->truesize += fp->truesize;
450 sub_frag_mem_limit(fq->q.net, head->truesize);
455 head->tstamp = fq->q.stamp;
456 ipv6_hdr(head)->payload_len = htons(payload_len);
457 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
458 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
460 /* Yes, and fold redundant checksum back. 8) */
461 if (head->ip_summed == CHECKSUM_COMPLETE)
462 head->csum = csum_partial(skb_network_header(head),
463 skb_network_header_len(head),
466 fq->q.fragments = NULL;
467 fq->q.fragments_tail = NULL;
473 * find the header just before Fragment Header.
475 * if success return 0 and set ...
476 * (*prevhdrp): the value of "Next Header Field" in the header
477 * just before Fragment Header.
478 * (*prevhoff): the offset of "Next Header Field" in the header
479 * just before Fragment Header.
480 * (*fhoff) : the offset of Fragment Header.
482 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
486 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
488 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
489 const int netoff = skb_network_offset(skb);
490 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
491 int start = netoff + sizeof(struct ipv6hdr);
492 int len = skb->len - start;
493 u8 prevhdr = NEXTHDR_IPV6;
495 while (nexthdr != NEXTHDR_FRAGMENT) {
496 struct ipv6_opt_hdr hdr;
499 if (!ipv6_ext_hdr(nexthdr)) {
502 if (nexthdr == NEXTHDR_NONE) {
503 pr_debug("next header is none\n");
506 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
507 pr_debug("too short\n");
510 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
512 if (nexthdr == NEXTHDR_AUTH)
513 hdrlen = (hdr.hdrlen+2)<<2;
515 hdrlen = ipv6_optlen(&hdr);
520 nexthdr = hdr.nexthdr;
529 *prevhoff = prev_nhoff;
535 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
537 u16 savethdr = skb->transport_header;
538 struct net_device *dev = skb->dev;
539 int fhoff, nhoff, ret;
540 struct frag_hdr *fhdr;
541 struct frag_queue *fq;
545 /* Jumbo payload inhibits frag. header */
546 if (ipv6_hdr(skb)->payload_len == 0) {
547 pr_debug("payload len = 0\n");
551 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
554 if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
557 skb_set_transport_header(skb, fhoff);
559 fhdr = (struct frag_hdr *)skb_transport_header(skb);
562 fq = fq_find(net, fhdr->identification, user, hdr,
563 skb->dev ? skb->dev->ifindex : 0);
565 pr_debug("Can't find and can't create new queue\n");
569 spin_lock_bh(&fq->q.lock);
571 ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
573 if (ret == -EPROTO) {
574 skb->transport_header = savethdr;
580 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
584 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
585 fq->q.meat == fq->q.len &&
586 nf_ct_frag6_reasm(fq, skb, dev))
592 spin_unlock_bh(&fq->q.lock);
593 inet_frag_put(&fq->q);
596 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
598 static int nf_ct_net_init(struct net *net)
602 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
603 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
604 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
605 net->nf_frag.frags.f = &nf_frags;
607 res = inet_frags_init_net(&net->nf_frag.frags);
610 res = nf_ct_frag6_sysctl_register(net);
612 inet_frags_exit_net(&net->nf_frag.frags);
616 static void nf_ct_net_exit(struct net *net)
618 nf_ct_frags6_sysctl_unregister(net);
619 inet_frags_exit_net(&net->nf_frag.frags);
622 static struct pernet_operations nf_ct_net_ops = {
623 .init = nf_ct_net_init,
624 .exit = nf_ct_net_exit,
627 int nf_ct_frag6_init(void)
631 nf_frags.constructor = ip6_frag_init;
632 nf_frags.destructor = NULL;
633 nf_frags.qsize = sizeof(struct frag_queue);
634 nf_frags.frag_expire = nf_ct_frag6_expire;
635 nf_frags.frags_cache_name = nf_frags_cache_name;
636 nf_frags.rhash_params = ip6_rhash_params;
637 ret = inet_frags_init(&nf_frags);
640 ret = register_pernet_subsys(&nf_ct_net_ops);
642 inet_frags_fini(&nf_frags);
648 void nf_ct_frag6_cleanup(void)
650 unregister_pernet_subsys(&nf_ct_net_ops);
651 inet_frags_fini(&nf_frags);