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>
50 #include <net/ip6_route.h>
51 #include <net/protocol.h>
52 #include <net/transp_v6.h>
53 #include <net/rawv6.h>
54 #include <net/ndisc.h>
55 #include <net/addrconf.h>
57 int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
58 int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
60 int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
64 struct inet6_skb_parm h;
68 #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
72 * Equivalent of ipv4 struct ipq
77 struct hlist_node list;
78 struct list_head lru_list; /* lru list member */
80 __be32 id; /* fragment id */
81 struct in6_addr saddr;
82 struct in6_addr daddr;
86 struct timer_list timer; /* expire timer */
87 struct sk_buff *fragments;
93 __u8 last_in; /* has first/last segment arrived? */
102 #define IP6Q_HASHSZ 64
104 static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
105 static DEFINE_RWLOCK(ip6_frag_lock);
106 static u32 ip6_frag_hash_rnd;
107 static LIST_HEAD(ip6_frag_lru_list);
108 int ip6_frag_nqueues = 0;
110 static __inline__ void __fq_unlink(struct frag_queue *fq)
112 hlist_del(&fq->list);
113 list_del(&fq->lru_list);
117 static __inline__ void fq_unlink(struct frag_queue *fq)
119 write_lock(&ip6_frag_lock);
121 write_unlock(&ip6_frag_lock);
125 * callers should be careful not to use the hash value outside the ipfrag_lock
126 * as doing so could race with ipfrag_hash_rnd being recalculated.
128 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
129 struct in6_addr *daddr)
133 a = (__force u32)saddr->s6_addr32[0];
134 b = (__force u32)saddr->s6_addr32[1];
135 c = (__force u32)saddr->s6_addr32[2];
137 a += JHASH_GOLDEN_RATIO;
138 b += JHASH_GOLDEN_RATIO;
139 c += ip6_frag_hash_rnd;
140 __jhash_mix(a, b, c);
142 a += (__force u32)saddr->s6_addr32[3];
143 b += (__force u32)daddr->s6_addr32[0];
144 c += (__force u32)daddr->s6_addr32[1];
145 __jhash_mix(a, b, c);
147 a += (__force u32)daddr->s6_addr32[2];
148 b += (__force u32)daddr->s6_addr32[3];
149 c += (__force u32)id;
150 __jhash_mix(a, b, c);
152 return c & (IP6Q_HASHSZ - 1);
155 static struct timer_list ip6_frag_secret_timer;
156 int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
158 static void ip6_frag_secret_rebuild(unsigned long dummy)
160 unsigned long now = jiffies;
163 write_lock(&ip6_frag_lock);
164 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
165 for (i = 0; i < IP6Q_HASHSZ; i++) {
166 struct frag_queue *q;
167 struct hlist_node *p, *n;
169 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
170 unsigned int hval = ip6qhashfn(q->id,
177 /* Relink to new hash chain. */
178 hlist_add_head(&q->list,
179 &ip6_frag_hash[hval]);
184 write_unlock(&ip6_frag_lock);
186 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
189 atomic_t ip6_frag_mem = ATOMIC_INIT(0);
191 /* Memory Tracking Functions. */
192 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
195 *work -= skb->truesize;
196 atomic_sub(skb->truesize, &ip6_frag_mem);
200 static inline void frag_free_queue(struct frag_queue *fq, int *work)
203 *work -= sizeof(struct frag_queue);
204 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
208 static inline struct frag_queue *frag_alloc_queue(void)
210 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
214 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
218 /* Destruction primitives. */
220 /* Complete destruction of fq. */
221 static void ip6_frag_destroy(struct frag_queue *fq, int *work)
225 BUG_TRAP(fq->last_in&COMPLETE);
226 BUG_TRAP(del_timer(&fq->timer) == 0);
228 /* Release all fragment data. */
231 struct sk_buff *xp = fp->next;
233 frag_kfree_skb(fp, work);
237 frag_free_queue(fq, work);
240 static __inline__ void fq_put(struct frag_queue *fq, int *work)
242 if (atomic_dec_and_test(&fq->refcnt))
243 ip6_frag_destroy(fq, work);
246 /* Kill fq entry. It is not destroyed immediately,
247 * because caller (and someone more) holds reference count.
249 static __inline__ void fq_kill(struct frag_queue *fq)
251 if (del_timer(&fq->timer))
252 atomic_dec(&fq->refcnt);
254 if (!(fq->last_in & COMPLETE)) {
256 atomic_dec(&fq->refcnt);
257 fq->last_in |= COMPLETE;
261 static void ip6_evictor(struct inet6_dev *idev)
263 struct frag_queue *fq;
264 struct list_head *tmp;
267 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
272 read_lock(&ip6_frag_lock);
273 if (list_empty(&ip6_frag_lru_list)) {
274 read_unlock(&ip6_frag_lock);
277 tmp = ip6_frag_lru_list.next;
278 fq = list_entry(tmp, struct frag_queue, lru_list);
279 atomic_inc(&fq->refcnt);
280 read_unlock(&ip6_frag_lock);
282 spin_lock(&fq->lock);
283 if (!(fq->last_in&COMPLETE))
285 spin_unlock(&fq->lock);
288 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
292 static void ip6_frag_expire(unsigned long data)
294 struct frag_queue *fq = (struct frag_queue *) data;
295 struct net_device *dev = NULL;
297 spin_lock(&fq->lock);
299 if (fq->last_in & COMPLETE)
304 dev = dev_get_by_index(fq->iif);
309 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
310 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
313 /* Don't send error if the first segment did not arrive. */
314 if (!(fq->last_in&FIRST_IN) || !fq->fragments)
318 But use as source device on which LAST ARRIVED
319 segment was received. And do not use fq->dev
320 pointer directly, device might already disappeared.
322 fq->fragments->dev = dev;
323 icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
327 spin_unlock(&fq->lock);
331 /* Creation primitives. */
334 static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
336 struct frag_queue *fq;
339 struct hlist_node *n;
342 write_lock(&ip6_frag_lock);
343 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
345 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
346 if (fq->id == fq_in->id &&
347 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
348 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
349 atomic_inc(&fq->refcnt);
350 write_unlock(&ip6_frag_lock);
351 fq_in->last_in |= COMPLETE;
359 if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
360 atomic_inc(&fq->refcnt);
362 atomic_inc(&fq->refcnt);
363 hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
364 INIT_LIST_HEAD(&fq->lru_list);
365 list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
367 write_unlock(&ip6_frag_lock);
372 static struct frag_queue *
373 ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
374 struct inet6_dev *idev)
376 struct frag_queue *fq;
378 if ((fq = frag_alloc_queue()) == NULL)
382 ipv6_addr_copy(&fq->saddr, src);
383 ipv6_addr_copy(&fq->daddr, dst);
385 init_timer(&fq->timer);
386 fq->timer.function = ip6_frag_expire;
387 fq->timer.data = (long) fq;
388 spin_lock_init(&fq->lock);
389 atomic_set(&fq->refcnt, 1);
391 return ip6_frag_intern(fq);
394 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
398 static __inline__ struct frag_queue *
399 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
400 struct inet6_dev *idev)
402 struct frag_queue *fq;
403 struct hlist_node *n;
406 read_lock(&ip6_frag_lock);
407 hash = ip6qhashfn(id, src, dst);
408 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
410 ipv6_addr_equal(src, &fq->saddr) &&
411 ipv6_addr_equal(dst, &fq->daddr)) {
412 atomic_inc(&fq->refcnt);
413 read_unlock(&ip6_frag_lock);
417 read_unlock(&ip6_frag_lock);
419 return ip6_frag_create(id, src, dst, idev);
423 static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
424 struct frag_hdr *fhdr, int nhoff)
426 struct sk_buff *prev, *next;
429 if (fq->last_in & COMPLETE)
432 offset = ntohs(fhdr->frag_off) & ~0x7;
433 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
434 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
436 if ((unsigned int)end > IPV6_MAXPLEN) {
437 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
438 IPSTATS_MIB_INHDRERRORS);
439 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
440 ((u8 *)&fhdr->frag_off -
441 skb_network_header(skb)));
445 if (skb->ip_summed == CHECKSUM_COMPLETE) {
446 const unsigned char *nh = skb_network_header(skb);
447 skb->csum = csum_sub(skb->csum,
448 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
452 /* Is this the final fragment? */
453 if (!(fhdr->frag_off & htons(IP6_MF))) {
454 /* If we already have some bits beyond end
455 * or have different end, the segment is corrupted.
458 ((fq->last_in & LAST_IN) && end != fq->len))
460 fq->last_in |= LAST_IN;
463 /* Check if the fragment is rounded to 8 bytes.
464 * Required by the RFC.
467 /* RFC2460 says always send parameter problem in
470 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
471 IPSTATS_MIB_INHDRERRORS);
472 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
473 offsetof(struct ipv6hdr, payload_len));
477 /* Some bits beyond end -> corruption. */
478 if (fq->last_in & LAST_IN)
487 /* Point into the IP datagram 'data' part. */
488 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
491 if (pskb_trim_rcsum(skb, end - offset))
494 /* Find out which fragments are in front and at the back of us
495 * in the chain of fragments so far. We must know where to put
496 * this fragment, right?
499 for(next = fq->fragments; next != NULL; next = next->next) {
500 if (FRAG6_CB(next)->offset >= offset)
505 /* We found where to put this one. Check for overlap with
506 * preceding fragment, and, if needed, align things so that
507 * any overlaps are eliminated.
510 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
516 if (!pskb_pull(skb, i))
518 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
519 skb->ip_summed = CHECKSUM_NONE;
523 /* Look for overlap with succeeding segments.
524 * If we can merge fragments, do it.
526 while (next && FRAG6_CB(next)->offset < end) {
527 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
530 /* Eat head of the next overlapped fragment
531 * and leave the loop. The next ones cannot overlap.
533 if (!pskb_pull(next, i))
535 FRAG6_CB(next)->offset += i; /* next fragment */
537 if (next->ip_summed != CHECKSUM_UNNECESSARY)
538 next->ip_summed = CHECKSUM_NONE;
541 struct sk_buff *free_it = next;
543 /* Old fragment is completely overridden with
551 fq->fragments = next;
553 fq->meat -= free_it->len;
554 frag_kfree_skb(free_it, NULL);
558 FRAG6_CB(skb)->offset = offset;
560 /* Insert this fragment in the chain of fragments. */
568 fq->iif = skb->dev->ifindex;
570 fq->stamp = skb->tstamp;
571 fq->meat += skb->len;
572 atomic_add(skb->truesize, &ip6_frag_mem);
574 /* The first fragment.
575 * nhoffset is obtained from the first fragment, of course.
578 fq->nhoffset = nhoff;
579 fq->last_in |= FIRST_IN;
581 write_lock(&ip6_frag_lock);
582 list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
583 write_unlock(&ip6_frag_lock);
587 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 **skb_in,
601 struct net_device *dev)
603 struct sk_buff *fp, *head = fq->fragments;
609 BUG_TRAP(head != NULL);
610 BUG_TRAP(FRAG6_CB(head)->offset == 0);
612 /* Unfragmented part is taken from the first segment. */
613 payload_len = ((head->data - skb_network_header(head)) -
614 sizeof(struct ipv6hdr) + fq->len -
615 sizeof(struct frag_hdr));
616 if (payload_len > IPV6_MAXPLEN)
619 /* Head of list must not be cloned. */
620 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
623 /* If the first fragment is fragmented itself, we split
624 * it to two chunks: the first with data and paged part
625 * and the second, holding only fragments. */
626 if (skb_shinfo(head)->frag_list) {
627 struct sk_buff *clone;
630 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
632 clone->next = head->next;
634 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
635 skb_shinfo(head)->frag_list = NULL;
636 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
637 plen += skb_shinfo(head)->frags[i].size;
638 clone->len = clone->data_len = head->data_len - plen;
639 head->data_len -= clone->len;
640 head->len -= clone->len;
642 clone->ip_summed = head->ip_summed;
643 atomic_add(clone->truesize, &ip6_frag_mem);
646 /* We have to remove fragment header from datagram and to relocate
647 * header in order to calculate ICV correctly. */
648 nhoff = fq->nhoffset;
649 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
650 memmove(head->head + sizeof(struct frag_hdr), head->head,
651 (head->data - head->head) - sizeof(struct frag_hdr));
652 head->mac_header += sizeof(struct frag_hdr);
653 head->network_header += sizeof(struct frag_hdr);
655 skb_shinfo(head)->frag_list = head->next;
656 skb_reset_transport_header(head);
657 skb_push(head, head->data - skb_network_header(head));
658 atomic_sub(head->truesize, &ip6_frag_mem);
660 for (fp=head->next; fp; fp = fp->next) {
661 head->data_len += fp->len;
662 head->len += fp->len;
663 if (head->ip_summed != fp->ip_summed)
664 head->ip_summed = CHECKSUM_NONE;
665 else if (head->ip_summed == CHECKSUM_COMPLETE)
666 head->csum = csum_add(head->csum, fp->csum);
667 head->truesize += fp->truesize;
668 atomic_sub(fp->truesize, &ip6_frag_mem);
673 head->tstamp = fq->stamp;
674 ipv6_hdr(head)->payload_len = htons(payload_len);
675 IP6CB(head)->nhoff = nhoff;
679 /* Yes, and fold redundant checksum back. 8) */
680 if (head->ip_summed == CHECKSUM_COMPLETE)
681 head->csum = csum_partial(skb_network_header(head),
682 skb_network_header_len(head),
686 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
688 fq->fragments = NULL;
693 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
697 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
700 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
705 static int ipv6_frag_rcv(struct sk_buff **skbp)
707 struct sk_buff *skb = *skbp;
708 struct net_device *dev = skb->dev;
709 struct frag_hdr *fhdr;
710 struct frag_queue *fq;
711 struct ipv6hdr *hdr = ipv6_hdr(skb);
713 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
715 /* Jumbo payload inhibits frag. header */
716 if (hdr->payload_len==0) {
717 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
718 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
719 skb_network_header_len(skb));
722 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
723 sizeof(struct frag_hdr)))) {
724 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
725 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
726 skb_network_header_len(skb));
731 fhdr = (struct frag_hdr *)skb_transport_header(skb);
733 if (!(fhdr->frag_off & htons(0xFFF9))) {
734 /* It is not a fragmented frame */
735 skb->transport_header += sizeof(struct frag_hdr);
736 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
738 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
742 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
743 ip6_evictor(ip6_dst_idev(skb->dst));
745 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
746 ip6_dst_idev(skb->dst))) != NULL) {
749 spin_lock(&fq->lock);
751 ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
753 if (fq->last_in == (FIRST_IN|LAST_IN) &&
755 ret = ip6_frag_reasm(fq, skbp, dev);
757 spin_unlock(&fq->lock);
762 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
767 static struct inet6_protocol frag_protocol =
769 .handler = ipv6_frag_rcv,
770 .flags = INET6_PROTO_NOPOLICY,
773 void __init ipv6_frag_init(void)
775 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
776 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
778 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
779 (jiffies ^ (jiffies >> 6)));
781 init_timer(&ip6_frag_secret_timer);
782 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
783 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
784 add_timer(&ip6_frag_secret_timer);