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;
90 static struct inet_frags ip6_frags;
92 int ip6_frag_nqueues(void)
94 return ip6_frags.nqueues;
97 int ip6_frag_mem(void)
99 return atomic_read(&ip6_frags.mem);
102 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
103 struct net_device *dev);
105 static __inline__ void __fq_unlink(struct frag_queue *fq)
107 hlist_del(&fq->q.list);
108 list_del(&fq->q.lru_list);
112 static __inline__ void fq_unlink(struct frag_queue *fq)
114 write_lock(&ip6_frags.lock);
116 write_unlock(&ip6_frags.lock);
120 * callers should be careful not to use the hash value outside the ipfrag_lock
121 * as doing so could race with ipfrag_hash_rnd being recalculated.
123 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
124 struct in6_addr *daddr)
128 a = (__force u32)saddr->s6_addr32[0];
129 b = (__force u32)saddr->s6_addr32[1];
130 c = (__force u32)saddr->s6_addr32[2];
132 a += JHASH_GOLDEN_RATIO;
133 b += JHASH_GOLDEN_RATIO;
135 __jhash_mix(a, b, c);
137 a += (__force u32)saddr->s6_addr32[3];
138 b += (__force u32)daddr->s6_addr32[0];
139 c += (__force u32)daddr->s6_addr32[1];
140 __jhash_mix(a, b, c);
142 a += (__force u32)daddr->s6_addr32[2];
143 b += (__force u32)daddr->s6_addr32[3];
144 c += (__force u32)id;
145 __jhash_mix(a, b, c);
147 return c & (INETFRAGS_HASHSZ - 1);
150 int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
152 static void ip6_frag_secret_rebuild(unsigned long dummy)
154 unsigned long now = jiffies;
157 write_lock(&ip6_frags.lock);
158 get_random_bytes(&ip6_frags.rnd, sizeof(u32));
159 for (i = 0; i < INETFRAGS_HASHSZ; i++) {
160 struct frag_queue *q;
161 struct hlist_node *p, *n;
163 hlist_for_each_entry_safe(q, p, n, &ip6_frags.hash[i], q.list) {
164 unsigned int hval = ip6qhashfn(q->id,
169 hlist_del(&q->q.list);
171 /* Relink to new hash chain. */
172 hlist_add_head(&q->q.list,
173 &ip6_frags.hash[hval]);
178 write_unlock(&ip6_frags.lock);
180 mod_timer(&ip6_frags.secret_timer, now + sysctl_ip6frag_secret_interval);
183 /* Memory Tracking Functions. */
184 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
187 *work -= skb->truesize;
188 atomic_sub(skb->truesize, &ip6_frags.mem);
192 static inline void frag_free_queue(struct frag_queue *fq, int *work)
195 *work -= sizeof(struct frag_queue);
196 atomic_sub(sizeof(struct frag_queue), &ip6_frags.mem);
200 static inline struct frag_queue *frag_alloc_queue(void)
202 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
206 atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
210 /* Destruction primitives. */
212 /* Complete destruction of fq. */
213 static void ip6_frag_destroy(struct frag_queue *fq, int *work)
217 BUG_TRAP(fq->q.last_in&COMPLETE);
218 BUG_TRAP(del_timer(&fq->q.timer) == 0);
220 /* Release all fragment data. */
221 fp = fq->q.fragments;
223 struct sk_buff *xp = fp->next;
225 frag_kfree_skb(fp, work);
229 frag_free_queue(fq, work);
232 static __inline__ void fq_put(struct frag_queue *fq, int *work)
234 if (atomic_dec_and_test(&fq->q.refcnt))
235 ip6_frag_destroy(fq, work);
238 /* Kill fq entry. It is not destroyed immediately,
239 * because caller (and someone more) holds reference count.
241 static __inline__ void fq_kill(struct frag_queue *fq)
243 if (del_timer(&fq->q.timer))
244 atomic_dec(&fq->q.refcnt);
246 if (!(fq->q.last_in & COMPLETE)) {
248 atomic_dec(&fq->q.refcnt);
249 fq->q.last_in |= COMPLETE;
253 static void ip6_evictor(struct inet6_dev *idev)
255 struct frag_queue *fq;
256 struct list_head *tmp;
259 work = atomic_read(&ip6_frags.mem) - sysctl_ip6frag_low_thresh;
264 read_lock(&ip6_frags.lock);
265 if (list_empty(&ip6_frags.lru_list)) {
266 read_unlock(&ip6_frags.lock);
269 tmp = ip6_frags.lru_list.next;
270 fq = list_entry(tmp, struct frag_queue, q.lru_list);
271 atomic_inc(&fq->q.refcnt);
272 read_unlock(&ip6_frags.lock);
274 spin_lock(&fq->q.lock);
275 if (!(fq->q.last_in&COMPLETE))
277 spin_unlock(&fq->q.lock);
280 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
284 static void ip6_frag_expire(unsigned long data)
286 struct frag_queue *fq = (struct frag_queue *) data;
287 struct net_device *dev = NULL;
289 spin_lock(&fq->q.lock);
291 if (fq->q.last_in & COMPLETE)
296 dev = dev_get_by_index(&init_net, fq->iif);
301 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
302 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
305 /* Don't send error if the first segment did not arrive. */
306 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
310 But use as source device on which LAST ARRIVED
311 segment was received. And do not use fq->dev
312 pointer directly, device might already disappeared.
314 fq->q.fragments->dev = dev;
315 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
319 spin_unlock(&fq->q.lock);
323 /* Creation primitives. */
326 static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
328 struct frag_queue *fq;
331 struct hlist_node *n;
334 write_lock(&ip6_frags.lock);
335 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
337 hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
338 if (fq->id == fq_in->id &&
339 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
340 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
341 atomic_inc(&fq->q.refcnt);
342 write_unlock(&ip6_frags.lock);
343 fq_in->q.last_in |= COMPLETE;
351 if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time))
352 atomic_inc(&fq->q.refcnt);
354 atomic_inc(&fq->q.refcnt);
355 hlist_add_head(&fq->q.list, &ip6_frags.hash[hash]);
356 INIT_LIST_HEAD(&fq->q.lru_list);
357 list_add_tail(&fq->q.lru_list, &ip6_frags.lru_list);
359 write_unlock(&ip6_frags.lock);
364 static struct frag_queue *
365 ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
366 struct inet6_dev *idev)
368 struct frag_queue *fq;
370 if ((fq = frag_alloc_queue()) == NULL)
374 ipv6_addr_copy(&fq->saddr, src);
375 ipv6_addr_copy(&fq->daddr, dst);
377 init_timer(&fq->q.timer);
378 fq->q.timer.function = ip6_frag_expire;
379 fq->q.timer.data = (long) fq;
380 spin_lock_init(&fq->q.lock);
381 atomic_set(&fq->q.refcnt, 1);
383 return ip6_frag_intern(fq);
386 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
390 static __inline__ struct frag_queue *
391 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
392 struct inet6_dev *idev)
394 struct frag_queue *fq;
395 struct hlist_node *n;
398 read_lock(&ip6_frags.lock);
399 hash = ip6qhashfn(id, src, dst);
400 hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
402 ipv6_addr_equal(src, &fq->saddr) &&
403 ipv6_addr_equal(dst, &fq->daddr)) {
404 atomic_inc(&fq->q.refcnt);
405 read_unlock(&ip6_frags.lock);
409 read_unlock(&ip6_frags.lock);
411 return ip6_frag_create(id, src, dst, idev);
415 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
416 struct frag_hdr *fhdr, int nhoff)
418 struct sk_buff *prev, *next;
419 struct net_device *dev;
422 if (fq->q.last_in & COMPLETE)
425 offset = ntohs(fhdr->frag_off) & ~0x7;
426 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
427 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
429 if ((unsigned int)end > IPV6_MAXPLEN) {
430 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
431 IPSTATS_MIB_INHDRERRORS);
432 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
433 ((u8 *)&fhdr->frag_off -
434 skb_network_header(skb)));
438 if (skb->ip_summed == CHECKSUM_COMPLETE) {
439 const unsigned char *nh = skb_network_header(skb);
440 skb->csum = csum_sub(skb->csum,
441 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
445 /* Is this the final fragment? */
446 if (!(fhdr->frag_off & htons(IP6_MF))) {
447 /* If we already have some bits beyond end
448 * or have different end, the segment is corrupted.
450 if (end < fq->q.len ||
451 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
453 fq->q.last_in |= LAST_IN;
456 /* Check if the fragment is rounded to 8 bytes.
457 * Required by the RFC.
460 /* RFC2460 says always send parameter problem in
463 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
464 IPSTATS_MIB_INHDRERRORS);
465 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
466 offsetof(struct ipv6hdr, payload_len));
469 if (end > fq->q.len) {
470 /* Some bits beyond end -> corruption. */
471 if (fq->q.last_in & LAST_IN)
480 /* Point into the IP datagram 'data' part. */
481 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
484 if (pskb_trim_rcsum(skb, end - offset))
487 /* Find out which fragments are in front and at the back of us
488 * in the chain of fragments so far. We must know where to put
489 * this fragment, right?
492 for(next = fq->q.fragments; next != NULL; next = next->next) {
493 if (FRAG6_CB(next)->offset >= offset)
498 /* We found where to put this one. Check for overlap with
499 * preceding fragment, and, if needed, align things so that
500 * any overlaps are eliminated.
503 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
509 if (!pskb_pull(skb, i))
511 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
512 skb->ip_summed = CHECKSUM_NONE;
516 /* Look for overlap with succeeding segments.
517 * If we can merge fragments, do it.
519 while (next && FRAG6_CB(next)->offset < end) {
520 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
523 /* Eat head of the next overlapped fragment
524 * and leave the loop. The next ones cannot overlap.
526 if (!pskb_pull(next, i))
528 FRAG6_CB(next)->offset += i; /* next fragment */
530 if (next->ip_summed != CHECKSUM_UNNECESSARY)
531 next->ip_summed = CHECKSUM_NONE;
534 struct sk_buff *free_it = next;
536 /* Old fragment is completely overridden with
544 fq->q.fragments = next;
546 fq->q.meat -= free_it->len;
547 frag_kfree_skb(free_it, NULL);
551 FRAG6_CB(skb)->offset = offset;
553 /* Insert this fragment in the chain of fragments. */
558 fq->q.fragments = skb;
562 fq->iif = dev->ifindex;
565 fq->q.stamp = skb->tstamp;
566 fq->q.meat += skb->len;
567 atomic_add(skb->truesize, &ip6_frags.mem);
569 /* The first fragment.
570 * nhoffset is obtained from the first fragment, of course.
573 fq->nhoffset = nhoff;
574 fq->q.last_in |= FIRST_IN;
577 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
578 return ip6_frag_reasm(fq, prev, dev);
580 write_lock(&ip6_frags.lock);
581 list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
582 write_unlock(&ip6_frags.lock);
586 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
592 * Check if this packet is complete.
593 * Returns NULL on failure by any reason, and pointer
594 * to current nexthdr field in reassembled frame.
596 * It is called with locked fq, and caller must check that
597 * queue is eligible for reassembly i.e. it is not COMPLETE,
598 * the last and the first frames arrived and all the bits are here.
600 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
601 struct net_device *dev)
603 struct sk_buff *fp, *head = fq->q.fragments;
609 /* Make the one we just received the head. */
612 fp = skb_clone(head, GFP_ATOMIC);
617 fp->next = head->next;
620 skb_morph(head, fq->q.fragments);
621 head->next = fq->q.fragments->next;
623 kfree_skb(fq->q.fragments);
624 fq->q.fragments = head;
627 BUG_TRAP(head != NULL);
628 BUG_TRAP(FRAG6_CB(head)->offset == 0);
630 /* Unfragmented part is taken from the first segment. */
631 payload_len = ((head->data - skb_network_header(head)) -
632 sizeof(struct ipv6hdr) + fq->q.len -
633 sizeof(struct frag_hdr));
634 if (payload_len > IPV6_MAXPLEN)
637 /* Head of list must not be cloned. */
638 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
641 /* If the first fragment is fragmented itself, we split
642 * it to two chunks: the first with data and paged part
643 * and the second, holding only fragments. */
644 if (skb_shinfo(head)->frag_list) {
645 struct sk_buff *clone;
648 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
650 clone->next = head->next;
652 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
653 skb_shinfo(head)->frag_list = NULL;
654 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
655 plen += skb_shinfo(head)->frags[i].size;
656 clone->len = clone->data_len = head->data_len - plen;
657 head->data_len -= clone->len;
658 head->len -= clone->len;
660 clone->ip_summed = head->ip_summed;
661 atomic_add(clone->truesize, &ip6_frags.mem);
664 /* We have to remove fragment header from datagram and to relocate
665 * header in order to calculate ICV correctly. */
666 nhoff = fq->nhoffset;
667 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
668 memmove(head->head + sizeof(struct frag_hdr), head->head,
669 (head->data - head->head) - sizeof(struct frag_hdr));
670 head->mac_header += sizeof(struct frag_hdr);
671 head->network_header += sizeof(struct frag_hdr);
673 skb_shinfo(head)->frag_list = head->next;
674 skb_reset_transport_header(head);
675 skb_push(head, head->data - skb_network_header(head));
676 atomic_sub(head->truesize, &ip6_frags.mem);
678 for (fp=head->next; fp; fp = fp->next) {
679 head->data_len += fp->len;
680 head->len += fp->len;
681 if (head->ip_summed != fp->ip_summed)
682 head->ip_summed = CHECKSUM_NONE;
683 else if (head->ip_summed == CHECKSUM_COMPLETE)
684 head->csum = csum_add(head->csum, fp->csum);
685 head->truesize += fp->truesize;
686 atomic_sub(fp->truesize, &ip6_frags.mem);
691 head->tstamp = fq->q.stamp;
692 ipv6_hdr(head)->payload_len = htons(payload_len);
693 IP6CB(head)->nhoff = nhoff;
695 /* Yes, and fold redundant checksum back. 8) */
696 if (head->ip_summed == CHECKSUM_COMPLETE)
697 head->csum = csum_partial(skb_network_header(head),
698 skb_network_header_len(head),
702 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
704 fq->q.fragments = NULL;
709 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
713 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
716 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
721 static int ipv6_frag_rcv(struct sk_buff **skbp)
723 struct sk_buff *skb = *skbp;
724 struct frag_hdr *fhdr;
725 struct frag_queue *fq;
726 struct ipv6hdr *hdr = ipv6_hdr(skb);
728 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
730 /* Jumbo payload inhibits frag. header */
731 if (hdr->payload_len==0) {
732 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
733 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
734 skb_network_header_len(skb));
737 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
738 sizeof(struct frag_hdr)))) {
739 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
740 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
741 skb_network_header_len(skb));
746 fhdr = (struct frag_hdr *)skb_transport_header(skb);
748 if (!(fhdr->frag_off & htons(0xFFF9))) {
749 /* It is not a fragmented frame */
750 skb->transport_header += sizeof(struct frag_hdr);
751 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
753 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
757 if (atomic_read(&ip6_frags.mem) > sysctl_ip6frag_high_thresh)
758 ip6_evictor(ip6_dst_idev(skb->dst));
760 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
761 ip6_dst_idev(skb->dst))) != NULL) {
764 spin_lock(&fq->q.lock);
766 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
768 spin_unlock(&fq->q.lock);
773 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
778 static struct inet6_protocol frag_protocol =
780 .handler = ipv6_frag_rcv,
781 .flags = INET6_PROTO_NOPOLICY,
784 void __init ipv6_frag_init(void)
786 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
787 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
789 init_timer(&ip6_frags.secret_timer);
790 ip6_frags.secret_timer.function = ip6_frag_secret_rebuild;
791 ip6_frags.secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
792 add_timer(&ip6_frags.secret_timer);
794 inet_frags_init(&ip6_frags);