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 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/jiffies.h>
23 #include <linux/net.h>
24 #include <linux/list.h>
25 #include <linux/netdevice.h>
26 #include <linux/in6.h>
27 #include <linux/ipv6.h>
28 #include <linux/icmpv6.h>
29 #include <linux/random.h>
30 #include <linux/jhash.h>
34 #include <net/inet_frag.h>
37 #include <net/protocol.h>
38 #include <net/transp_v6.h>
39 #include <net/rawv6.h>
40 #include <net/ndisc.h>
41 #include <net/addrconf.h>
42 #include <linux/sysctl.h>
43 #include <linux/netfilter.h>
44 #include <linux/netfilter_ipv6.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
48 #define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */
49 #define NF_CT_FRAG6_LOW_THRESH 196608 /* == 192*1024 */
50 #define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUT
52 struct nf_ct_frag6_skb_cb
54 struct inet6_skb_parm h;
59 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
61 struct nf_ct_frag6_queue
63 struct inet_frag_queue q;
65 __be32 id; /* fragment id */
66 struct in6_addr saddr;
67 struct in6_addr daddr;
73 struct inet_frags_ctl nf_frags_ctl __read_mostly = {
74 .high_thresh = 256 * 1024,
75 .low_thresh = 192 * 1024,
76 .timeout = IPV6_FRAG_TIMEOUT,
77 .secret_interval = 10 * 60 * HZ,
80 static struct inet_frags nf_frags;
82 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
83 struct in6_addr *daddr)
87 a = (__force u32)saddr->s6_addr32[0];
88 b = (__force u32)saddr->s6_addr32[1];
89 c = (__force u32)saddr->s6_addr32[2];
91 a += JHASH_GOLDEN_RATIO;
92 b += JHASH_GOLDEN_RATIO;
96 a += (__force u32)saddr->s6_addr32[3];
97 b += (__force u32)daddr->s6_addr32[0];
98 c += (__force u32)daddr->s6_addr32[1];
101 a += (__force u32)daddr->s6_addr32[2];
102 b += (__force u32)daddr->s6_addr32[3];
103 c += (__force u32)id;
104 __jhash_mix(a, b, c);
106 return c & (INETFRAGS_HASHSZ - 1);
109 static unsigned int nf_hashfn(struct inet_frag_queue *q)
111 struct nf_ct_frag6_queue *nq;
113 nq = container_of(q, struct nf_ct_frag6_queue, q);
114 return ip6qhashfn(nq->id, &nq->saddr, &nq->daddr);
117 static void nf_skb_free(struct sk_buff *skb)
119 if (NFCT_FRAG6_CB(skb)->orig)
120 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
123 /* Memory Tracking Functions. */
124 static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
127 *work -= skb->truesize;
128 atomic_sub(skb->truesize, &nf_frags.mem);
133 static void nf_frag_free(struct inet_frag_queue *q)
135 kfree(container_of(q, struct nf_ct_frag6_queue, q));
138 static inline struct nf_ct_frag6_queue *frag_alloc_queue(void)
140 struct nf_ct_frag6_queue *fq;
142 fq = kzalloc(sizeof(struct nf_ct_frag6_queue), GFP_ATOMIC);
145 atomic_add(sizeof(struct nf_ct_frag6_queue), &nf_frags.mem);
149 /* Destruction primitives. */
151 static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
153 inet_frag_put(&fq->q, &nf_frags);
156 /* Kill fq entry. It is not destroyed immediately,
157 * because caller (and someone more) holds reference count.
159 static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
161 inet_frag_kill(&fq->q, &nf_frags);
164 static void nf_ct_frag6_evictor(void)
166 inet_frag_evictor(&nf_frags);
169 static void nf_ct_frag6_expire(unsigned long data)
171 struct nf_ct_frag6_queue *fq = (struct nf_ct_frag6_queue *) data;
173 spin_lock(&fq->q.lock);
175 if (fq->q.last_in & COMPLETE)
181 spin_unlock(&fq->q.lock);
185 /* Creation primitives. */
187 static struct nf_ct_frag6_queue *nf_ct_frag6_intern(unsigned int hash,
188 struct nf_ct_frag6_queue *fq_in)
190 struct nf_ct_frag6_queue *fq;
192 struct hlist_node *n;
195 write_lock(&nf_frags.lock);
197 hlist_for_each_entry(fq, n, &nf_frags.hash[hash], q.list) {
198 if (fq->id == fq_in->id &&
199 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
200 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
201 atomic_inc(&fq->q.refcnt);
202 write_unlock(&nf_frags.lock);
203 fq_in->q.last_in |= COMPLETE;
211 if (!mod_timer(&fq->q.timer, jiffies + nf_frags_ctl.timeout))
212 atomic_inc(&fq->q.refcnt);
214 atomic_inc(&fq->q.refcnt);
215 hlist_add_head(&fq->q.list, &nf_frags.hash[hash]);
216 INIT_LIST_HEAD(&fq->q.lru_list);
217 list_add_tail(&fq->q.lru_list, &nf_frags.lru_list);
219 write_unlock(&nf_frags.lock);
224 static struct nf_ct_frag6_queue *
225 nf_ct_frag6_create(unsigned int hash, __be32 id, struct in6_addr *src, struct in6_addr *dst)
227 struct nf_ct_frag6_queue *fq;
229 if ((fq = frag_alloc_queue()) == NULL) {
230 pr_debug("Can't alloc new queue\n");
235 ipv6_addr_copy(&fq->saddr, src);
236 ipv6_addr_copy(&fq->daddr, dst);
238 setup_timer(&fq->q.timer, nf_ct_frag6_expire, (unsigned long)fq);
239 spin_lock_init(&fq->q.lock);
240 atomic_set(&fq->q.refcnt, 1);
242 return nf_ct_frag6_intern(hash, fq);
248 static __inline__ struct nf_ct_frag6_queue *
249 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
251 struct nf_ct_frag6_queue *fq;
252 struct hlist_node *n;
253 unsigned int hash = ip6qhashfn(id, src, dst);
255 read_lock(&nf_frags.lock);
256 hlist_for_each_entry(fq, n, &nf_frags.hash[hash], q.list) {
258 ipv6_addr_equal(src, &fq->saddr) &&
259 ipv6_addr_equal(dst, &fq->daddr)) {
260 atomic_inc(&fq->q.refcnt);
261 read_unlock(&nf_frags.lock);
265 read_unlock(&nf_frags.lock);
267 return nf_ct_frag6_create(hash, id, src, dst);
271 static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
272 struct frag_hdr *fhdr, int nhoff)
274 struct sk_buff *prev, *next;
277 if (fq->q.last_in & COMPLETE) {
278 pr_debug("Allready completed\n");
282 offset = ntohs(fhdr->frag_off) & ~0x7;
283 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
284 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
286 if ((unsigned int)end > IPV6_MAXPLEN) {
287 pr_debug("offset is too large.\n");
291 if (skb->ip_summed == CHECKSUM_COMPLETE) {
292 const unsigned char *nh = skb_network_header(skb);
293 skb->csum = csum_sub(skb->csum,
294 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
298 /* Is this the final fragment? */
299 if (!(fhdr->frag_off & htons(IP6_MF))) {
300 /* If we already have some bits beyond end
301 * or have different end, the segment is corrupted.
303 if (end < fq->q.len ||
304 ((fq->q.last_in & LAST_IN) && end != fq->q.len)) {
305 pr_debug("already received last fragment\n");
308 fq->q.last_in |= LAST_IN;
311 /* Check if the fragment is rounded to 8 bytes.
312 * Required by the RFC.
315 /* RFC2460 says always send parameter problem in
318 pr_debug("end of fragment not rounded to 8 bytes.\n");
321 if (end > fq->q.len) {
322 /* Some bits beyond end -> corruption. */
323 if (fq->q.last_in & LAST_IN) {
324 pr_debug("last packet already reached.\n");
334 /* Point into the IP datagram 'data' part. */
335 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
336 pr_debug("queue: message is too short.\n");
339 if (pskb_trim_rcsum(skb, end - offset)) {
340 pr_debug("Can't trim\n");
344 /* Find out which fragments are in front and at the back of us
345 * in the chain of fragments so far. We must know where to put
346 * this fragment, right?
349 for (next = fq->q.fragments; next != NULL; next = next->next) {
350 if (NFCT_FRAG6_CB(next)->offset >= offset)
355 /* We found where to put this one. Check for overlap with
356 * preceding fragment, and, if needed, align things so that
357 * any overlaps are eliminated.
360 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
365 pr_debug("overlap\n");
368 if (!pskb_pull(skb, i)) {
369 pr_debug("Can't pull\n");
372 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
373 skb->ip_summed = CHECKSUM_NONE;
377 /* Look for overlap with succeeding segments.
378 * If we can merge fragments, do it.
380 while (next && NFCT_FRAG6_CB(next)->offset < end) {
381 /* overlap is 'i' bytes */
382 int i = end - NFCT_FRAG6_CB(next)->offset;
385 /* Eat head of the next overlapped fragment
386 * and leave the loop. The next ones cannot overlap.
388 pr_debug("Eat head of the overlapped parts.: %d", i);
389 if (!pskb_pull(next, i))
393 NFCT_FRAG6_CB(next)->offset += i;
395 if (next->ip_summed != CHECKSUM_UNNECESSARY)
396 next->ip_summed = CHECKSUM_NONE;
399 struct sk_buff *free_it = next;
401 /* Old fragmnet is completely overridden with
409 fq->q.fragments = next;
411 fq->q.meat -= free_it->len;
412 frag_kfree_skb(free_it, NULL);
416 NFCT_FRAG6_CB(skb)->offset = offset;
418 /* Insert this fragment in the chain of fragments. */
423 fq->q.fragments = skb;
426 fq->q.stamp = skb->tstamp;
427 fq->q.meat += skb->len;
428 atomic_add(skb->truesize, &nf_frags.mem);
430 /* The first fragment.
431 * nhoffset is obtained from the first fragment, of course.
434 fq->nhoffset = nhoff;
435 fq->q.last_in |= FIRST_IN;
437 write_lock(&nf_frags.lock);
438 list_move_tail(&fq->q.lru_list, &nf_frags.lru_list);
439 write_unlock(&nf_frags.lock);
447 * Check if this packet is complete.
448 * Returns NULL on failure by any reason, and pointer
449 * to current nexthdr field in reassembled frame.
451 * It is called with locked fq, and caller must check that
452 * queue is eligible for reassembly i.e. it is not COMPLETE,
453 * the last and the first frames arrived and all the bits are here.
455 static struct sk_buff *
456 nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
458 struct sk_buff *fp, *op, *head = fq->q.fragments;
463 BUG_TRAP(head != NULL);
464 BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0);
466 /* Unfragmented part is taken from the first segment. */
467 payload_len = ((head->data - skb_network_header(head)) -
468 sizeof(struct ipv6hdr) + fq->q.len -
469 sizeof(struct frag_hdr));
470 if (payload_len > IPV6_MAXPLEN) {
471 pr_debug("payload len is too large.\n");
475 /* Head of list must not be cloned. */
476 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
477 pr_debug("skb is cloned but can't expand head");
481 /* If the first fragment is fragmented itself, we split
482 * it to two chunks: the first with data and paged part
483 * and the second, holding only fragments. */
484 if (skb_shinfo(head)->frag_list) {
485 struct sk_buff *clone;
488 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
489 pr_debug("Can't alloc skb\n");
492 clone->next = head->next;
494 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
495 skb_shinfo(head)->frag_list = NULL;
496 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
497 plen += skb_shinfo(head)->frags[i].size;
498 clone->len = clone->data_len = head->data_len - plen;
499 head->data_len -= clone->len;
500 head->len -= clone->len;
502 clone->ip_summed = head->ip_summed;
504 NFCT_FRAG6_CB(clone)->orig = NULL;
505 atomic_add(clone->truesize, &nf_frags.mem);
508 /* We have to remove fragment header from datagram and to relocate
509 * header in order to calculate ICV correctly. */
510 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
511 memmove(head->head + sizeof(struct frag_hdr), head->head,
512 (head->data - head->head) - sizeof(struct frag_hdr));
513 head->mac_header += sizeof(struct frag_hdr);
514 head->network_header += sizeof(struct frag_hdr);
516 skb_shinfo(head)->frag_list = head->next;
517 skb_reset_transport_header(head);
518 skb_push(head, head->data - skb_network_header(head));
519 atomic_sub(head->truesize, &nf_frags.mem);
521 for (fp=head->next; fp; fp = fp->next) {
522 head->data_len += fp->len;
523 head->len += fp->len;
524 if (head->ip_summed != fp->ip_summed)
525 head->ip_summed = CHECKSUM_NONE;
526 else if (head->ip_summed == CHECKSUM_COMPLETE)
527 head->csum = csum_add(head->csum, fp->csum);
528 head->truesize += fp->truesize;
529 atomic_sub(fp->truesize, &nf_frags.mem);
534 head->tstamp = fq->q.stamp;
535 ipv6_hdr(head)->payload_len = htons(payload_len);
537 /* Yes, and fold redundant checksum back. 8) */
538 if (head->ip_summed == CHECKSUM_COMPLETE)
539 head->csum = csum_partial(skb_network_header(head),
540 skb_network_header_len(head),
543 fq->q.fragments = NULL;
545 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
546 fp = skb_shinfo(head)->frag_list;
547 if (NFCT_FRAG6_CB(fp)->orig == NULL)
548 /* at above code, head skb is divided into two skbs. */
551 op = NFCT_FRAG6_CB(head)->orig;
552 for (; fp; fp = fp->next) {
553 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
557 NFCT_FRAG6_CB(fp)->orig = NULL;
564 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
568 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
574 * find the header just before Fragment Header.
576 * if success return 0 and set ...
577 * (*prevhdrp): the value of "Next Header Field" in the header
578 * just before Fragment Header.
579 * (*prevhoff): the offset of "Next Header Field" in the header
580 * just before Fragment Header.
581 * (*fhoff) : the offset of Fragment Header.
583 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
587 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
589 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
590 const int netoff = skb_network_offset(skb);
591 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
592 int start = netoff + sizeof(struct ipv6hdr);
593 int len = skb->len - start;
594 u8 prevhdr = NEXTHDR_IPV6;
596 while (nexthdr != NEXTHDR_FRAGMENT) {
597 struct ipv6_opt_hdr hdr;
600 if (!ipv6_ext_hdr(nexthdr)) {
603 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
604 pr_debug("too short\n");
607 if (nexthdr == NEXTHDR_NONE) {
608 pr_debug("next header is none\n");
611 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
613 if (nexthdr == NEXTHDR_AUTH)
614 hdrlen = (hdr.hdrlen+2)<<2;
616 hdrlen = ipv6_optlen(&hdr);
621 nexthdr = hdr.nexthdr;
630 *prevhoff = prev_nhoff;
636 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
638 struct sk_buff *clone;
639 struct net_device *dev = skb->dev;
640 struct frag_hdr *fhdr;
641 struct nf_ct_frag6_queue *fq;
645 struct sk_buff *ret_skb = NULL;
647 /* Jumbo payload inhibits frag. header */
648 if (ipv6_hdr(skb)->payload_len == 0) {
649 pr_debug("payload len = 0\n");
653 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
656 clone = skb_clone(skb, GFP_ATOMIC);
658 pr_debug("Can't clone skb\n");
662 NFCT_FRAG6_CB(clone)->orig = skb;
664 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
665 pr_debug("message is too short.\n");
669 skb_set_transport_header(clone, fhoff);
670 hdr = ipv6_hdr(clone);
671 fhdr = (struct frag_hdr *)skb_transport_header(clone);
673 if (!(fhdr->frag_off & htons(0xFFF9))) {
674 pr_debug("Invalid fragment offset\n");
675 /* It is not a fragmented frame */
679 if (atomic_read(&nf_frags.mem) > nf_frags_ctl.high_thresh)
680 nf_ct_frag6_evictor();
682 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
684 pr_debug("Can't find and can't create new queue\n");
688 spin_lock(&fq->q.lock);
690 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
691 spin_unlock(&fq->q.lock);
692 pr_debug("Can't insert skb to queue\n");
697 if (fq->q.last_in == (FIRST_IN|LAST_IN) && fq->q.meat == fq->q.len) {
698 ret_skb = nf_ct_frag6_reasm(fq, dev);
700 pr_debug("Can't reassemble fragmented packets\n");
702 spin_unlock(&fq->q.lock);
712 void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
713 struct net_device *in, struct net_device *out,
714 int (*okfn)(struct sk_buff *))
716 struct sk_buff *s, *s2;
718 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
719 nf_conntrack_put_reasm(s->nfct_reasm);
720 nf_conntrack_get_reasm(skb);
726 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
727 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
730 nf_conntrack_put_reasm(skb);
733 int nf_ct_frag6_kfree_frags(struct sk_buff *skb)
735 struct sk_buff *s, *s2;
737 for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) {
748 int nf_ct_frag6_init(void)
750 nf_frags.ctl = &nf_frags_ctl;
751 nf_frags.hashfn = nf_hashfn;
752 nf_frags.destructor = nf_frag_free;
753 nf_frags.skb_free = nf_skb_free;
754 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
755 inet_frags_init(&nf_frags);
760 void nf_ct_frag6_cleanup(void)
762 inet_frags_fini(&nf_frags);
764 nf_frags_ctl.low_thresh = 0;
765 nf_ct_frag6_evictor();