2 * IPv6 fragment reassembly
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
10 * Based on: net/ipv4/ip_fragment.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen Make it work with multiple hosts.
21 * More RFC compliance.
23 * Horst von Brand Add missing #include <linux/string.h>
24 * Alexey Kuznetsov SMP races, threading, cleanup.
25 * Patrick McHardy LRU queue of frag heads for evictor.
26 * Mitsuru KANDA @USAGI Register inet6_protocol{}.
28 * YOSHIFUJI,H. @USAGI Always remove fragment header to
29 * calculate ICV correctly.
31 #include <linux/errno.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
36 #include <linux/jiffies.h>
37 #include <linux/net.h>
38 #include <linux/list.h>
39 #include <linux/netdevice.h>
40 #include <linux/in6.h>
41 #include <linux/ipv6.h>
42 #include <linux/icmpv6.h>
43 #include <linux/random.h>
44 #include <linux/jhash.h>
45 #include <linux/skbuff.h>
51 #include <net/ip6_route.h>
52 #include <net/protocol.h>
53 #include <net/transp_v6.h>
54 #include <net/rawv6.h>
55 #include <net/ndisc.h>
56 #include <net/addrconf.h>
57 #include <net/inet_frag.h>
59 int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
60 int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
62 int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
66 struct inet6_skb_parm h;
70 #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
74 * Equivalent of ipv4 struct ipq
79 struct inet_frag_queue q;
81 __be32 id; /* fragment id */
82 struct in6_addr saddr;
83 struct in6_addr daddr;
92 #define IP6Q_HASHSZ 64
94 static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
95 static DEFINE_RWLOCK(ip6_frag_lock);
96 static u32 ip6_frag_hash_rnd;
97 static LIST_HEAD(ip6_frag_lru_list);
98 int ip6_frag_nqueues = 0;
100 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
101 struct net_device *dev);
103 static __inline__ void __fq_unlink(struct frag_queue *fq)
105 hlist_del(&fq->q.list);
106 list_del(&fq->q.lru_list);
110 static __inline__ void fq_unlink(struct frag_queue *fq)
112 write_lock(&ip6_frag_lock);
114 write_unlock(&ip6_frag_lock);
118 * callers should be careful not to use the hash value outside the ipfrag_lock
119 * as doing so could race with ipfrag_hash_rnd being recalculated.
121 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
122 struct in6_addr *daddr)
126 a = (__force u32)saddr->s6_addr32[0];
127 b = (__force u32)saddr->s6_addr32[1];
128 c = (__force u32)saddr->s6_addr32[2];
130 a += JHASH_GOLDEN_RATIO;
131 b += JHASH_GOLDEN_RATIO;
132 c += ip6_frag_hash_rnd;
133 __jhash_mix(a, b, c);
135 a += (__force u32)saddr->s6_addr32[3];
136 b += (__force u32)daddr->s6_addr32[0];
137 c += (__force u32)daddr->s6_addr32[1];
138 __jhash_mix(a, b, c);
140 a += (__force u32)daddr->s6_addr32[2];
141 b += (__force u32)daddr->s6_addr32[3];
142 c += (__force u32)id;
143 __jhash_mix(a, b, c);
145 return c & (IP6Q_HASHSZ - 1);
148 static struct timer_list ip6_frag_secret_timer;
149 int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
151 static void ip6_frag_secret_rebuild(unsigned long dummy)
153 unsigned long now = jiffies;
156 write_lock(&ip6_frag_lock);
157 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
158 for (i = 0; i < IP6Q_HASHSZ; i++) {
159 struct frag_queue *q;
160 struct hlist_node *p, *n;
162 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], q.list) {
163 unsigned int hval = ip6qhashfn(q->id,
168 hlist_del(&q->q.list);
170 /* Relink to new hash chain. */
171 hlist_add_head(&q->q.list,
172 &ip6_frag_hash[hval]);
177 write_unlock(&ip6_frag_lock);
179 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
182 atomic_t ip6_frag_mem = ATOMIC_INIT(0);
184 /* Memory Tracking Functions. */
185 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
188 *work -= skb->truesize;
189 atomic_sub(skb->truesize, &ip6_frag_mem);
193 static inline void frag_free_queue(struct frag_queue *fq, int *work)
196 *work -= sizeof(struct frag_queue);
197 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
201 static inline struct frag_queue *frag_alloc_queue(void)
203 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
207 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
211 /* Destruction primitives. */
213 /* Complete destruction of fq. */
214 static void ip6_frag_destroy(struct frag_queue *fq, int *work)
218 BUG_TRAP(fq->q.last_in&COMPLETE);
219 BUG_TRAP(del_timer(&fq->q.timer) == 0);
221 /* Release all fragment data. */
222 fp = fq->q.fragments;
224 struct sk_buff *xp = fp->next;
226 frag_kfree_skb(fp, work);
230 frag_free_queue(fq, work);
233 static __inline__ void fq_put(struct frag_queue *fq, int *work)
235 if (atomic_dec_and_test(&fq->q.refcnt))
236 ip6_frag_destroy(fq, work);
239 /* Kill fq entry. It is not destroyed immediately,
240 * because caller (and someone more) holds reference count.
242 static __inline__ void fq_kill(struct frag_queue *fq)
244 if (del_timer(&fq->q.timer))
245 atomic_dec(&fq->q.refcnt);
247 if (!(fq->q.last_in & COMPLETE)) {
249 atomic_dec(&fq->q.refcnt);
250 fq->q.last_in |= COMPLETE;
254 static void ip6_evictor(struct inet6_dev *idev)
256 struct frag_queue *fq;
257 struct list_head *tmp;
260 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
265 read_lock(&ip6_frag_lock);
266 if (list_empty(&ip6_frag_lru_list)) {
267 read_unlock(&ip6_frag_lock);
270 tmp = ip6_frag_lru_list.next;
271 fq = list_entry(tmp, struct frag_queue, q.lru_list);
272 atomic_inc(&fq->q.refcnt);
273 read_unlock(&ip6_frag_lock);
275 spin_lock(&fq->q.lock);
276 if (!(fq->q.last_in&COMPLETE))
278 spin_unlock(&fq->q.lock);
281 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
285 static void ip6_frag_expire(unsigned long data)
287 struct frag_queue *fq = (struct frag_queue *) data;
288 struct net_device *dev = NULL;
290 spin_lock(&fq->q.lock);
292 if (fq->q.last_in & COMPLETE)
297 dev = dev_get_by_index(&init_net, fq->iif);
302 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
303 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
306 /* Don't send error if the first segment did not arrive. */
307 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
311 But use as source device on which LAST ARRIVED
312 segment was received. And do not use fq->dev
313 pointer directly, device might already disappeared.
315 fq->q.fragments->dev = dev;
316 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
320 spin_unlock(&fq->q.lock);
324 /* Creation primitives. */
327 static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
329 struct frag_queue *fq;
332 struct hlist_node *n;
335 write_lock(&ip6_frag_lock);
336 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
338 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) {
339 if (fq->id == fq_in->id &&
340 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
341 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
342 atomic_inc(&fq->q.refcnt);
343 write_unlock(&ip6_frag_lock);
344 fq_in->q.last_in |= COMPLETE;
352 if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time))
353 atomic_inc(&fq->q.refcnt);
355 atomic_inc(&fq->q.refcnt);
356 hlist_add_head(&fq->q.list, &ip6_frag_hash[hash]);
357 INIT_LIST_HEAD(&fq->q.lru_list);
358 list_add_tail(&fq->q.lru_list, &ip6_frag_lru_list);
360 write_unlock(&ip6_frag_lock);
365 static struct frag_queue *
366 ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
367 struct inet6_dev *idev)
369 struct frag_queue *fq;
371 if ((fq = frag_alloc_queue()) == NULL)
375 ipv6_addr_copy(&fq->saddr, src);
376 ipv6_addr_copy(&fq->daddr, dst);
378 init_timer(&fq->q.timer);
379 fq->q.timer.function = ip6_frag_expire;
380 fq->q.timer.data = (long) fq;
381 spin_lock_init(&fq->q.lock);
382 atomic_set(&fq->q.refcnt, 1);
384 return ip6_frag_intern(fq);
387 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
391 static __inline__ struct frag_queue *
392 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
393 struct inet6_dev *idev)
395 struct frag_queue *fq;
396 struct hlist_node *n;
399 read_lock(&ip6_frag_lock);
400 hash = ip6qhashfn(id, src, dst);
401 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) {
403 ipv6_addr_equal(src, &fq->saddr) &&
404 ipv6_addr_equal(dst, &fq->daddr)) {
405 atomic_inc(&fq->q.refcnt);
406 read_unlock(&ip6_frag_lock);
410 read_unlock(&ip6_frag_lock);
412 return ip6_frag_create(id, src, dst, idev);
416 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
417 struct frag_hdr *fhdr, int nhoff)
419 struct sk_buff *prev, *next;
420 struct net_device *dev;
423 if (fq->q.last_in & COMPLETE)
426 offset = ntohs(fhdr->frag_off) & ~0x7;
427 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
428 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
430 if ((unsigned int)end > IPV6_MAXPLEN) {
431 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
432 IPSTATS_MIB_INHDRERRORS);
433 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
434 ((u8 *)&fhdr->frag_off -
435 skb_network_header(skb)));
439 if (skb->ip_summed == CHECKSUM_COMPLETE) {
440 const unsigned char *nh = skb_network_header(skb);
441 skb->csum = csum_sub(skb->csum,
442 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
446 /* Is this the final fragment? */
447 if (!(fhdr->frag_off & htons(IP6_MF))) {
448 /* If we already have some bits beyond end
449 * or have different end, the segment is corrupted.
451 if (end < fq->q.len ||
452 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
454 fq->q.last_in |= LAST_IN;
457 /* Check if the fragment is rounded to 8 bytes.
458 * Required by the RFC.
461 /* RFC2460 says always send parameter problem in
464 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
465 IPSTATS_MIB_INHDRERRORS);
466 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
467 offsetof(struct ipv6hdr, payload_len));
470 if (end > fq->q.len) {
471 /* Some bits beyond end -> corruption. */
472 if (fq->q.last_in & LAST_IN)
481 /* Point into the IP datagram 'data' part. */
482 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
485 if (pskb_trim_rcsum(skb, end - offset))
488 /* Find out which fragments are in front and at the back of us
489 * in the chain of fragments so far. We must know where to put
490 * this fragment, right?
493 for(next = fq->q.fragments; next != NULL; next = next->next) {
494 if (FRAG6_CB(next)->offset >= offset)
499 /* We found where to put this one. Check for overlap with
500 * preceding fragment, and, if needed, align things so that
501 * any overlaps are eliminated.
504 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
510 if (!pskb_pull(skb, i))
512 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
513 skb->ip_summed = CHECKSUM_NONE;
517 /* Look for overlap with succeeding segments.
518 * If we can merge fragments, do it.
520 while (next && FRAG6_CB(next)->offset < end) {
521 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
524 /* Eat head of the next overlapped fragment
525 * and leave the loop. The next ones cannot overlap.
527 if (!pskb_pull(next, i))
529 FRAG6_CB(next)->offset += i; /* next fragment */
531 if (next->ip_summed != CHECKSUM_UNNECESSARY)
532 next->ip_summed = CHECKSUM_NONE;
535 struct sk_buff *free_it = next;
537 /* Old fragment is completely overridden with
545 fq->q.fragments = next;
547 fq->q.meat -= free_it->len;
548 frag_kfree_skb(free_it, NULL);
552 FRAG6_CB(skb)->offset = offset;
554 /* Insert this fragment in the chain of fragments. */
559 fq->q.fragments = skb;
563 fq->iif = dev->ifindex;
566 fq->q.stamp = skb->tstamp;
567 fq->q.meat += skb->len;
568 atomic_add(skb->truesize, &ip6_frag_mem);
570 /* The first fragment.
571 * nhoffset is obtained from the first fragment, of course.
574 fq->nhoffset = nhoff;
575 fq->q.last_in |= FIRST_IN;
578 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
579 return ip6_frag_reasm(fq, prev, dev);
581 write_lock(&ip6_frag_lock);
582 list_move_tail(&fq->q.lru_list, &ip6_frag_lru_list);
583 write_unlock(&ip6_frag_lock);
587 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
593 * Check if this packet is complete.
594 * Returns NULL on failure by any reason, and pointer
595 * to current nexthdr field in reassembled frame.
597 * It is called with locked fq, and caller must check that
598 * queue is eligible for reassembly i.e. it is not COMPLETE,
599 * the last and the first frames arrived and all the bits are here.
601 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
602 struct net_device *dev)
604 struct sk_buff *fp, *head = fq->q.fragments;
610 /* Make the one we just received the head. */
613 fp = skb_clone(head, GFP_ATOMIC);
618 fp->next = head->next;
621 skb_morph(head, fq->q.fragments);
622 head->next = fq->q.fragments->next;
624 kfree_skb(fq->q.fragments);
625 fq->q.fragments = head;
628 BUG_TRAP(head != NULL);
629 BUG_TRAP(FRAG6_CB(head)->offset == 0);
631 /* Unfragmented part is taken from the first segment. */
632 payload_len = ((head->data - skb_network_header(head)) -
633 sizeof(struct ipv6hdr) + fq->q.len -
634 sizeof(struct frag_hdr));
635 if (payload_len > IPV6_MAXPLEN)
638 /* Head of list must not be cloned. */
639 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
642 /* If the first fragment is fragmented itself, we split
643 * it to two chunks: the first with data and paged part
644 * and the second, holding only fragments. */
645 if (skb_shinfo(head)->frag_list) {
646 struct sk_buff *clone;
649 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
651 clone->next = head->next;
653 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
654 skb_shinfo(head)->frag_list = NULL;
655 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
656 plen += skb_shinfo(head)->frags[i].size;
657 clone->len = clone->data_len = head->data_len - plen;
658 head->data_len -= clone->len;
659 head->len -= clone->len;
661 clone->ip_summed = head->ip_summed;
662 atomic_add(clone->truesize, &ip6_frag_mem);
665 /* We have to remove fragment header from datagram and to relocate
666 * header in order to calculate ICV correctly. */
667 nhoff = fq->nhoffset;
668 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
669 memmove(head->head + sizeof(struct frag_hdr), head->head,
670 (head->data - head->head) - sizeof(struct frag_hdr));
671 head->mac_header += sizeof(struct frag_hdr);
672 head->network_header += sizeof(struct frag_hdr);
674 skb_shinfo(head)->frag_list = head->next;
675 skb_reset_transport_header(head);
676 skb_push(head, head->data - skb_network_header(head));
677 atomic_sub(head->truesize, &ip6_frag_mem);
679 for (fp=head->next; fp; fp = fp->next) {
680 head->data_len += fp->len;
681 head->len += fp->len;
682 if (head->ip_summed != fp->ip_summed)
683 head->ip_summed = CHECKSUM_NONE;
684 else if (head->ip_summed == CHECKSUM_COMPLETE)
685 head->csum = csum_add(head->csum, fp->csum);
686 head->truesize += fp->truesize;
687 atomic_sub(fp->truesize, &ip6_frag_mem);
692 head->tstamp = fq->q.stamp;
693 ipv6_hdr(head)->payload_len = htons(payload_len);
694 IP6CB(head)->nhoff = nhoff;
696 /* Yes, and fold redundant checksum back. 8) */
697 if (head->ip_summed == CHECKSUM_COMPLETE)
698 head->csum = csum_partial(skb_network_header(head),
699 skb_network_header_len(head),
703 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
705 fq->q.fragments = NULL;
710 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
714 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
717 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
722 static int ipv6_frag_rcv(struct sk_buff **skbp)
724 struct sk_buff *skb = *skbp;
725 struct frag_hdr *fhdr;
726 struct frag_queue *fq;
727 struct ipv6hdr *hdr = ipv6_hdr(skb);
729 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
731 /* Jumbo payload inhibits frag. header */
732 if (hdr->payload_len==0) {
733 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
734 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
735 skb_network_header_len(skb));
738 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
739 sizeof(struct frag_hdr)))) {
740 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
741 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
742 skb_network_header_len(skb));
747 fhdr = (struct frag_hdr *)skb_transport_header(skb);
749 if (!(fhdr->frag_off & htons(0xFFF9))) {
750 /* It is not a fragmented frame */
751 skb->transport_header += sizeof(struct frag_hdr);
752 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
754 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
758 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
759 ip6_evictor(ip6_dst_idev(skb->dst));
761 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
762 ip6_dst_idev(skb->dst))) != NULL) {
765 spin_lock(&fq->q.lock);
767 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
769 spin_unlock(&fq->q.lock);
774 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
779 static struct inet6_protocol frag_protocol =
781 .handler = ipv6_frag_rcv,
782 .flags = INET6_PROTO_NOPOLICY,
785 void __init ipv6_frag_init(void)
787 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
788 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
790 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
791 (jiffies ^ (jiffies >> 6)));
793 init_timer(&ip6_frag_secret_timer);
794 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
795 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
796 add_timer(&ip6_frag_secret_timer);