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>
54 struct nf_ct_frag6_skb_cb
56 struct inet6_skb_parm h;
61 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
63 static struct inet_frags nf_frags;
66 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
68 .procname = "nf_conntrack_frag6_timeout",
69 .data = &init_net.nf_frag.frags.timeout,
70 .maxlen = sizeof(unsigned int),
72 .proc_handler = proc_dointvec_jiffies,
75 .procname = "nf_conntrack_frag6_low_thresh",
76 .data = &init_net.nf_frag.frags.low_thresh,
77 .maxlen = sizeof(unsigned int),
79 .proc_handler = proc_dointvec,
82 .procname = "nf_conntrack_frag6_high_thresh",
83 .data = &init_net.nf_frag.frags.high_thresh,
84 .maxlen = sizeof(unsigned int),
86 .proc_handler = proc_dointvec,
91 static int nf_ct_frag6_sysctl_register(struct net *net)
93 struct ctl_table *table;
94 struct ctl_table_header *hdr;
96 table = nf_ct_frag6_sysctl_table;
97 if (!net_eq(net, &init_net)) {
98 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
103 table[0].data = &net->nf_frag.frags.timeout;
104 table[1].data = &net->nf_frag.frags.low_thresh;
105 table[2].data = &net->nf_frag.frags.high_thresh;
108 hdr = register_net_sysctl(net, "net/netfilter", table);
112 net->nf_frag.sysctl.frags_hdr = hdr;
116 if (!net_eq(net, &init_net))
122 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
124 struct ctl_table *table;
126 table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg;
127 unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr);
128 if (!net_eq(net, &init_net))
133 static int nf_ct_frag6_sysctl_register(struct net *net)
137 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
142 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
144 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
147 static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
148 const struct in6_addr *daddr)
150 net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
151 return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
152 (__force u32)id, nf_frags.rnd);
156 static unsigned int nf_hashfn(const struct inet_frag_queue *q)
158 const struct frag_queue *nq;
160 nq = container_of(q, struct frag_queue, q);
161 return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
164 static void nf_skb_free(struct sk_buff *skb)
166 if (NFCT_FRAG6_CB(skb)->orig)
167 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
170 static void nf_ct_frag6_expire(unsigned long data)
172 struct frag_queue *fq;
175 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
176 net = container_of(fq->q.net, struct net, nf_frag.frags);
178 ip6_expire_frag_queue(net, fq, &nf_frags);
181 /* Creation primitives. */
182 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
183 u32 user, struct in6_addr *src,
184 struct in6_addr *dst, u8 ecn)
186 struct inet_frag_queue *q;
187 struct ip6_create_arg arg;
196 read_lock_bh(&nf_frags.lock);
197 hash = nf_hash_frag(id, src, dst);
199 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
201 if (IS_ERR_OR_NULL(q)) {
202 inet_frag_maybe_warn_overflow(q, pr_fmt());
205 return container_of(q, struct frag_queue, q);
209 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
210 const struct frag_hdr *fhdr, int nhoff)
212 struct sk_buff *prev, *next;
213 unsigned int payload_len;
217 if (fq->q.last_in & INET_FRAG_COMPLETE) {
218 pr_debug("Already completed\n");
222 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
224 offset = ntohs(fhdr->frag_off) & ~0x7;
225 end = offset + (payload_len -
226 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
228 if ((unsigned int)end > IPV6_MAXPLEN) {
229 pr_debug("offset is too large.\n");
233 ecn = ip6_frag_ecn(ipv6_hdr(skb));
235 if (skb->ip_summed == CHECKSUM_COMPLETE) {
236 const unsigned char *nh = skb_network_header(skb);
237 skb->csum = csum_sub(skb->csum,
238 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
242 /* Is this the final fragment? */
243 if (!(fhdr->frag_off & htons(IP6_MF))) {
244 /* If we already have some bits beyond end
245 * or have different end, the segment is corrupted.
247 if (end < fq->q.len ||
248 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
249 pr_debug("already received last fragment\n");
252 fq->q.last_in |= INET_FRAG_LAST_IN;
255 /* Check if the fragment is rounded to 8 bytes.
256 * Required by the RFC.
259 /* RFC2460 says always send parameter problem in
262 pr_debug("end of fragment not rounded to 8 bytes.\n");
265 if (end > fq->q.len) {
266 /* Some bits beyond end -> corruption. */
267 if (fq->q.last_in & INET_FRAG_LAST_IN) {
268 pr_debug("last packet already reached.\n");
278 /* Point into the IP datagram 'data' part. */
279 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
280 pr_debug("queue: message is too short.\n");
283 if (pskb_trim_rcsum(skb, end - offset)) {
284 pr_debug("Can't trim\n");
288 /* Find out which fragments are in front and at the back of us
289 * in the chain of fragments so far. We must know where to put
290 * this fragment, right?
292 prev = fq->q.fragments_tail;
293 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
298 for (next = fq->q.fragments; next != NULL; next = next->next) {
299 if (NFCT_FRAG6_CB(next)->offset >= offset)
305 /* RFC5722, Section 4:
306 * When reassembling an IPv6 datagram, if
307 * one or more its constituent fragments is determined to be an
308 * overlapping fragment, the entire datagram (and any constituent
309 * fragments, including those not yet received) MUST be silently
313 /* Check for overlap with preceding fragment. */
315 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
318 /* Look for overlap with succeeding segment. */
319 if (next && NFCT_FRAG6_CB(next)->offset < end)
322 NFCT_FRAG6_CB(skb)->offset = offset;
324 /* Insert this fragment in the chain of fragments. */
327 fq->q.fragments_tail = skb;
331 fq->q.fragments = skb;
334 fq->iif = skb->dev->ifindex;
337 fq->q.stamp = skb->tstamp;
338 fq->q.meat += skb->len;
340 if (payload_len > fq->q.max_size)
341 fq->q.max_size = payload_len;
342 add_frag_mem_limit(&fq->q, skb->truesize);
344 /* The first fragment.
345 * nhoffset is obtained from the first fragment, of course.
348 fq->nhoffset = nhoff;
349 fq->q.last_in |= INET_FRAG_FIRST_IN;
352 inet_frag_lru_move(&fq->q);
356 inet_frag_kill(&fq->q, &nf_frags);
362 * Check if this packet is complete.
363 * Returns NULL on failure by any reason, and pointer
364 * to current nexthdr field in reassembled frame.
366 * It is called with locked fq, and caller must check that
367 * queue is eligible for reassembly i.e. it is not COMPLETE,
368 * the last and the first frames arrived and all the bits are here.
370 static struct sk_buff *
371 nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
373 struct sk_buff *fp, *op, *head = fq->q.fragments;
377 inet_frag_kill(&fq->q, &nf_frags);
379 WARN_ON(head == NULL);
380 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
382 ecn = ip_frag_ecn_table[fq->ecn];
383 if (unlikely(ecn == 0xff))
386 /* Unfragmented part is taken from the first segment. */
387 payload_len = ((head->data - skb_network_header(head)) -
388 sizeof(struct ipv6hdr) + fq->q.len -
389 sizeof(struct frag_hdr));
390 if (payload_len > IPV6_MAXPLEN) {
391 pr_debug("payload len is too large.\n");
395 /* Head of list must not be cloned. */
396 if (skb_unclone(head, GFP_ATOMIC)) {
397 pr_debug("skb is cloned but can't expand head");
401 /* If the first fragment is fragmented itself, we split
402 * it to two chunks: the first with data and paged part
403 * and the second, holding only fragments. */
404 if (skb_has_frag_list(head)) {
405 struct sk_buff *clone;
408 clone = alloc_skb(0, GFP_ATOMIC);
412 clone->next = head->next;
414 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
415 skb_frag_list_init(head);
416 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
417 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
418 clone->len = clone->data_len = head->data_len - plen;
419 head->data_len -= clone->len;
420 head->len -= clone->len;
422 clone->ip_summed = head->ip_summed;
424 NFCT_FRAG6_CB(clone)->orig = NULL;
425 add_frag_mem_limit(&fq->q, clone->truesize);
428 /* We have to remove fragment header from datagram and to relocate
429 * header in order to calculate ICV correctly. */
430 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
431 memmove(head->head + sizeof(struct frag_hdr), head->head,
432 (head->data - head->head) - sizeof(struct frag_hdr));
433 head->mac_header += sizeof(struct frag_hdr);
434 head->network_header += sizeof(struct frag_hdr);
436 skb_shinfo(head)->frag_list = head->next;
437 skb_reset_transport_header(head);
438 skb_push(head, head->data - skb_network_header(head));
440 for (fp=head->next; fp; fp = fp->next) {
441 head->data_len += fp->len;
442 head->len += fp->len;
443 if (head->ip_summed != fp->ip_summed)
444 head->ip_summed = CHECKSUM_NONE;
445 else if (head->ip_summed == CHECKSUM_COMPLETE)
446 head->csum = csum_add(head->csum, fp->csum);
447 head->truesize += fp->truesize;
449 sub_frag_mem_limit(&fq->q, head->truesize);
454 head->tstamp = fq->q.stamp;
455 ipv6_hdr(head)->payload_len = htons(payload_len);
456 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
457 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
459 /* Yes, and fold redundant checksum back. 8) */
460 if (head->ip_summed == CHECKSUM_COMPLETE)
461 head->csum = csum_partial(skb_network_header(head),
462 skb_network_header_len(head),
465 fq->q.fragments = NULL;
466 fq->q.fragments_tail = NULL;
468 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
469 fp = skb_shinfo(head)->frag_list;
470 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
471 /* at above code, head skb is divided into two skbs. */
474 op = NFCT_FRAG6_CB(head)->orig;
475 for (; fp; fp = fp->next) {
476 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
480 NFCT_FRAG6_CB(fp)->orig = NULL;
486 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
490 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
496 * find the header just before Fragment Header.
498 * if success return 0 and set ...
499 * (*prevhdrp): the value of "Next Header Field" in the header
500 * just before Fragment Header.
501 * (*prevhoff): the offset of "Next Header Field" in the header
502 * just before Fragment Header.
503 * (*fhoff) : the offset of Fragment Header.
505 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
509 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
511 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
512 const int netoff = skb_network_offset(skb);
513 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
514 int start = netoff + sizeof(struct ipv6hdr);
515 int len = skb->len - start;
516 u8 prevhdr = NEXTHDR_IPV6;
518 while (nexthdr != NEXTHDR_FRAGMENT) {
519 struct ipv6_opt_hdr hdr;
522 if (!ipv6_ext_hdr(nexthdr)) {
525 if (nexthdr == NEXTHDR_NONE) {
526 pr_debug("next header is none\n");
529 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
530 pr_debug("too short\n");
533 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
535 if (nexthdr == NEXTHDR_AUTH)
536 hdrlen = (hdr.hdrlen+2)<<2;
538 hdrlen = ipv6_optlen(&hdr);
543 nexthdr = hdr.nexthdr;
552 *prevhoff = prev_nhoff;
558 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
560 struct sk_buff *clone;
561 struct net_device *dev = skb->dev;
562 struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev)
564 struct frag_hdr *fhdr;
565 struct frag_queue *fq;
569 struct sk_buff *ret_skb = NULL;
571 /* Jumbo payload inhibits frag. header */
572 if (ipv6_hdr(skb)->payload_len == 0) {
573 pr_debug("payload len = 0\n");
577 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
580 clone = skb_clone(skb, GFP_ATOMIC);
582 pr_debug("Can't clone skb\n");
586 NFCT_FRAG6_CB(clone)->orig = skb;
588 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
589 pr_debug("message is too short.\n");
593 skb_set_transport_header(clone, fhoff);
594 hdr = ipv6_hdr(clone);
595 fhdr = (struct frag_hdr *)skb_transport_header(clone);
597 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
600 pr_debug("Can't find and can't create new queue\n");
604 spin_lock_bh(&fq->q.lock);
606 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
607 spin_unlock_bh(&fq->q.lock);
608 pr_debug("Can't insert skb to queue\n");
609 inet_frag_put(&fq->q, &nf_frags);
613 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
614 fq->q.meat == fq->q.len) {
615 ret_skb = nf_ct_frag6_reasm(fq, dev);
617 pr_debug("Can't reassemble fragmented packets\n");
619 spin_unlock_bh(&fq->q.lock);
621 inet_frag_put(&fq->q, &nf_frags);
629 void nf_ct_frag6_consume_orig(struct sk_buff *skb)
631 struct sk_buff *s, *s2;
633 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
641 static int nf_ct_net_init(struct net *net)
643 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
644 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
645 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
646 inet_frags_init_net(&net->nf_frag.frags);
648 return nf_ct_frag6_sysctl_register(net);
651 static void nf_ct_net_exit(struct net *net)
653 nf_ct_frags6_sysctl_unregister(net);
654 inet_frags_exit_net(&net->nf_frag.frags, &nf_frags);
657 static struct pernet_operations nf_ct_net_ops = {
658 .init = nf_ct_net_init,
659 .exit = nf_ct_net_exit,
662 int nf_ct_frag6_init(void)
666 nf_frags.hashfn = nf_hashfn;
667 nf_frags.constructor = ip6_frag_init;
668 nf_frags.destructor = NULL;
669 nf_frags.skb_free = nf_skb_free;
670 nf_frags.qsize = sizeof(struct frag_queue);
671 nf_frags.match = ip6_frag_match;
672 nf_frags.frag_expire = nf_ct_frag6_expire;
673 nf_frags.secret_interval = 10 * 60 * HZ;
674 inet_frags_init(&nf_frags);
676 ret = register_pernet_subsys(&nf_ct_net_ops);
678 inet_frags_fini(&nf_frags);
683 void nf_ct_frag6_cleanup(void)
685 unregister_pernet_subsys(&nf_ct_net_ops);
686 inet_frags_fini(&nf_frags);