[SKBUFF]: add skb_morph & co.
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv6 / reassembly.c
blobc77f4a33c1b1d76b10d797ed44a88cb3ef338747
1 /*
2 * IPv6 fragment reassembly
3 * Linux INET6 implementation
5 * Authors:
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.
19 * Fixes:
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{}.
27 * David Stevens and
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>
47 #include <net/sock.h>
48 #include <net/snmp.h>
50 #include <net/ipv6.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>
58 int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
59 int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
61 int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
63 struct ip6frag_skb_cb
65 struct inet6_skb_parm h;
66 int offset;
69 #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
73 * Equivalent of ipv4 struct ipq
76 struct frag_queue
78 struct hlist_node list;
79 struct list_head lru_list; /* lru list member */
81 __be32 id; /* fragment id */
82 struct in6_addr saddr;
83 struct in6_addr daddr;
85 spinlock_t lock;
86 atomic_t refcnt;
87 struct timer_list timer; /* expire timer */
88 struct sk_buff *fragments;
89 int len;
90 int meat;
91 int iif;
92 ktime_t stamp;
93 unsigned int csum;
94 __u8 last_in; /* has first/last segment arrived? */
95 #define COMPLETE 4
96 #define FIRST_IN 2
97 #define LAST_IN 1
98 __u16 nhoffset;
101 /* Hash table. */
103 #define IP6Q_HASHSZ 64
105 static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
106 static DEFINE_RWLOCK(ip6_frag_lock);
107 static u32 ip6_frag_hash_rnd;
108 static LIST_HEAD(ip6_frag_lru_list);
109 int ip6_frag_nqueues = 0;
111 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
112 struct net_device *dev);
114 static __inline__ void __fq_unlink(struct frag_queue *fq)
116 hlist_del(&fq->list);
117 list_del(&fq->lru_list);
118 ip6_frag_nqueues--;
121 static __inline__ void fq_unlink(struct frag_queue *fq)
123 write_lock(&ip6_frag_lock);
124 __fq_unlink(fq);
125 write_unlock(&ip6_frag_lock);
129 * callers should be careful not to use the hash value outside the ipfrag_lock
130 * as doing so could race with ipfrag_hash_rnd being recalculated.
132 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
133 struct in6_addr *daddr)
135 /* SpeedMod */
136 #if 0
137 u32 a, b, c;
139 a = (__force u32)saddr->s6_addr32[0];
140 b = (__force u32)saddr->s6_addr32[1];
141 c = (__force u32)saddr->s6_addr32[2];
143 a += JHASH_GOLDEN_RATIO;
144 b += JHASH_GOLDEN_RATIO;
145 c += ip6_frag_hash_rnd;
146 __jhash_mix(a, b, c);
148 a += (__force u32)saddr->s6_addr32[3];
149 b += (__force u32)daddr->s6_addr32[0];
150 c += (__force u32)daddr->s6_addr32[1];
151 __jhash_mix(a, b, c);
153 a += (__force u32)daddr->s6_addr32[2];
154 b += (__force u32)daddr->s6_addr32[3];
155 c += (__force u32)id;
156 __jhash_mix(a, b, c);
157 #else
158 u32 c;
159 u32 key[9] = {
160 (__force u32)saddr->s6_addr32[0],
161 (__force u32)saddr->s6_addr32[1],
162 (__force u32)saddr->s6_addr32[2],
163 (__force u32)saddr->s6_addr32[3],
164 (__force u32)daddr->s6_addr32[0],
165 (__force u32)daddr->s6_addr32[1],
166 (__force u32)daddr->s6_addr32[2],
167 (__force u32)daddr->s6_addr32[3],
168 (__force u32)id
171 c = jhash2(key, 9, ip6_frag_hash_rnd);
172 #endif
173 return c & (IP6Q_HASHSZ - 1);
176 static struct timer_list ip6_frag_secret_timer;
177 int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
179 static void ip6_frag_secret_rebuild(unsigned long dummy)
181 unsigned long now = jiffies;
182 int i;
184 write_lock(&ip6_frag_lock);
185 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
186 for (i = 0; i < IP6Q_HASHSZ; i++) {
187 struct frag_queue *q;
188 struct hlist_node *p, *n;
190 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
191 unsigned int hval = ip6qhashfn(q->id,
192 &q->saddr,
193 &q->daddr);
195 if (hval != i) {
196 hlist_del(&q->list);
198 /* Relink to new hash chain. */
199 hlist_add_head(&q->list,
200 &ip6_frag_hash[hval]);
205 write_unlock(&ip6_frag_lock);
207 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
210 atomic_t ip6_frag_mem = ATOMIC_INIT(0);
212 /* Memory Tracking Functions. */
213 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
215 if (work)
216 *work -= skb->truesize;
217 atomic_sub(skb->truesize, &ip6_frag_mem);
218 kfree_skb(skb);
221 static inline void frag_free_queue(struct frag_queue *fq, int *work)
223 if (work)
224 *work -= sizeof(struct frag_queue);
225 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
226 kfree(fq);
229 static inline struct frag_queue *frag_alloc_queue(void)
231 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
233 if(!fq)
234 return NULL;
235 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
236 return fq;
239 /* Destruction primitives. */
241 /* Complete destruction of fq. */
242 static void ip6_frag_destroy(struct frag_queue *fq, int *work)
244 struct sk_buff *fp;
246 BUG_TRAP(fq->last_in&COMPLETE);
247 BUG_TRAP(del_timer(&fq->timer) == 0);
249 /* Release all fragment data. */
250 fp = fq->fragments;
251 while (fp) {
252 struct sk_buff *xp = fp->next;
254 frag_kfree_skb(fp, work);
255 fp = xp;
258 frag_free_queue(fq, work);
261 static __inline__ void fq_put(struct frag_queue *fq, int *work)
263 if (atomic_dec_and_test(&fq->refcnt))
264 ip6_frag_destroy(fq, work);
267 /* Kill fq entry. It is not destroyed immediately,
268 * because caller (and someone more) holds reference count.
270 static __inline__ void fq_kill(struct frag_queue *fq)
272 if (del_timer(&fq->timer))
273 atomic_dec(&fq->refcnt);
275 if (!(fq->last_in & COMPLETE)) {
276 fq_unlink(fq);
277 atomic_dec(&fq->refcnt);
278 fq->last_in |= COMPLETE;
282 static void ip6_evictor(struct inet6_dev *idev)
284 struct frag_queue *fq;
285 struct list_head *tmp;
286 int work;
288 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
289 if (work <= 0)
290 return;
292 while(work > 0) {
293 read_lock(&ip6_frag_lock);
294 if (list_empty(&ip6_frag_lru_list)) {
295 read_unlock(&ip6_frag_lock);
296 return;
298 tmp = ip6_frag_lru_list.next;
299 fq = list_entry(tmp, struct frag_queue, lru_list);
300 atomic_inc(&fq->refcnt);
301 read_unlock(&ip6_frag_lock);
303 spin_lock(&fq->lock);
304 if (!(fq->last_in&COMPLETE))
305 fq_kill(fq);
306 spin_unlock(&fq->lock);
308 fq_put(fq, &work);
309 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
313 static void ip6_frag_expire(unsigned long data)
315 struct frag_queue *fq = (struct frag_queue *) data;
316 struct net_device *dev = NULL;
318 spin_lock(&fq->lock);
320 if (fq->last_in & COMPLETE)
321 goto out;
323 fq_kill(fq);
325 dev = dev_get_by_index(fq->iif);
326 if (!dev)
327 goto out;
329 rcu_read_lock();
330 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
331 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
332 rcu_read_unlock();
334 /* Don't send error if the first segment did not arrive. */
335 if (!(fq->last_in&FIRST_IN) || !fq->fragments)
336 goto out;
339 But use as source device on which LAST ARRIVED
340 segment was received. And do not use fq->dev
341 pointer directly, device might already disappeared.
343 fq->fragments->dev = dev;
344 icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
345 out:
346 if (dev)
347 dev_put(dev);
348 spin_unlock(&fq->lock);
349 fq_put(fq, NULL);
352 /* Creation primitives. */
355 static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
357 struct frag_queue *fq;
358 unsigned int hash;
359 #ifdef CONFIG_SMP
360 struct hlist_node *n;
361 #endif
363 write_lock(&ip6_frag_lock);
364 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
365 #ifdef CONFIG_SMP
366 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
367 if (fq->id == fq_in->id &&
368 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
369 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
370 atomic_inc(&fq->refcnt);
371 write_unlock(&ip6_frag_lock);
372 fq_in->last_in |= COMPLETE;
373 fq_put(fq_in, NULL);
374 return fq;
377 #endif
378 fq = fq_in;
380 if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
381 atomic_inc(&fq->refcnt);
383 atomic_inc(&fq->refcnt);
384 hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
385 INIT_LIST_HEAD(&fq->lru_list);
386 list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
387 ip6_frag_nqueues++;
388 write_unlock(&ip6_frag_lock);
389 return fq;
393 static struct frag_queue *
394 ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
395 struct inet6_dev *idev)
397 struct frag_queue *fq;
399 if ((fq = frag_alloc_queue()) == NULL)
400 goto oom;
402 fq->id = id;
403 ipv6_addr_copy(&fq->saddr, src);
404 ipv6_addr_copy(&fq->daddr, dst);
406 init_timer(&fq->timer);
407 fq->timer.function = ip6_frag_expire;
408 fq->timer.data = (long) fq;
409 spin_lock_init(&fq->lock);
410 atomic_set(&fq->refcnt, 1);
412 return ip6_frag_intern(fq);
414 oom:
415 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
416 return NULL;
419 static __inline__ struct frag_queue *
420 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
421 struct inet6_dev *idev)
423 struct frag_queue *fq;
424 struct hlist_node *n;
425 unsigned int hash;
427 read_lock(&ip6_frag_lock);
428 hash = ip6qhashfn(id, src, dst);
429 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
430 if (fq->id == id &&
431 ipv6_addr_equal(src, &fq->saddr) &&
432 ipv6_addr_equal(dst, &fq->daddr)) {
433 atomic_inc(&fq->refcnt);
434 read_unlock(&ip6_frag_lock);
435 return fq;
438 read_unlock(&ip6_frag_lock);
440 return ip6_frag_create(id, src, dst, idev);
444 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
445 struct frag_hdr *fhdr, int nhoff)
447 struct sk_buff *prev, *next;
448 struct net_device *dev;
449 int offset, end;
451 if (fq->last_in & COMPLETE)
452 goto err;
454 offset = ntohs(fhdr->frag_off) & ~0x7;
455 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
456 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
458 if ((unsigned int)end > IPV6_MAXPLEN) {
459 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
460 IPSTATS_MIB_INHDRERRORS);
461 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
462 ((u8 *)&fhdr->frag_off -
463 skb_network_header(skb)));
464 return -1;
467 if (skb->ip_summed == CHECKSUM_COMPLETE) {
468 const unsigned char *nh = skb_network_header(skb);
469 skb->csum = csum_sub(skb->csum,
470 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
471 0));
474 /* Is this the final fragment? */
475 if (!(fhdr->frag_off & htons(IP6_MF))) {
476 /* If we already have some bits beyond end
477 * or have different end, the segment is corrupted.
479 if (end < fq->len ||
480 ((fq->last_in & LAST_IN) && end != fq->len))
481 goto err;
482 fq->last_in |= LAST_IN;
483 fq->len = end;
484 } else {
485 /* Check if the fragment is rounded to 8 bytes.
486 * Required by the RFC.
488 if (end & 0x7) {
489 /* RFC2460 says always send parameter problem in
490 * this case. -DaveM
492 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
493 IPSTATS_MIB_INHDRERRORS);
494 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
495 offsetof(struct ipv6hdr, payload_len));
496 return -1;
498 if (end > fq->len) {
499 /* Some bits beyond end -> corruption. */
500 if (fq->last_in & LAST_IN)
501 goto err;
502 fq->len = end;
506 if (end == offset)
507 goto err;
509 /* Point into the IP datagram 'data' part. */
510 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
511 goto err;
513 if (pskb_trim_rcsum(skb, end - offset))
514 goto err;
516 /* Find out which fragments are in front and at the back of us
517 * in the chain of fragments so far. We must know where to put
518 * this fragment, right?
520 prev = NULL;
521 for(next = fq->fragments; next != NULL; next = next->next) {
522 if (FRAG6_CB(next)->offset >= offset)
523 break; /* bingo! */
524 prev = next;
527 /* We found where to put this one. Check for overlap with
528 * preceding fragment, and, if needed, align things so that
529 * any overlaps are eliminated.
531 if (prev) {
532 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
534 if (i > 0) {
535 offset += i;
536 if (end <= offset)
537 goto err;
538 if (!pskb_pull(skb, i))
539 goto err;
540 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
541 skb->ip_summed = CHECKSUM_NONE;
545 /* Look for overlap with succeeding segments.
546 * If we can merge fragments, do it.
548 while (next && FRAG6_CB(next)->offset < end) {
549 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
551 if (i < next->len) {
552 /* Eat head of the next overlapped fragment
553 * and leave the loop. The next ones cannot overlap.
555 if (!pskb_pull(next, i))
556 goto err;
557 FRAG6_CB(next)->offset += i; /* next fragment */
558 fq->meat -= i;
559 if (next->ip_summed != CHECKSUM_UNNECESSARY)
560 next->ip_summed = CHECKSUM_NONE;
561 break;
562 } else {
563 struct sk_buff *free_it = next;
565 /* Old fragment is completely overridden with
566 * new one drop it.
568 next = next->next;
570 if (prev)
571 prev->next = next;
572 else
573 fq->fragments = next;
575 fq->meat -= free_it->len;
576 frag_kfree_skb(free_it, NULL);
580 FRAG6_CB(skb)->offset = offset;
582 /* Insert this fragment in the chain of fragments. */
583 skb->next = next;
584 if (prev)
585 prev->next = skb;
586 else
587 fq->fragments = skb;
589 dev = skb->dev;
590 if (dev) {
591 fq->iif = dev->ifindex;
592 skb->dev = NULL;
594 fq->stamp = skb->tstamp;
595 fq->meat += skb->len;
596 atomic_add(skb->truesize, &ip6_frag_mem);
598 /* The first fragment.
599 * nhoffset is obtained from the first fragment, of course.
601 if (offset == 0) {
602 fq->nhoffset = nhoff;
603 fq->last_in |= FIRST_IN;
606 if (fq->last_in == (FIRST_IN | LAST_IN) && fq->meat == fq->len)
607 return ip6_frag_reasm(fq, prev, dev);
609 write_lock(&ip6_frag_lock);
610 list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
611 write_unlock(&ip6_frag_lock);
612 return -1;
614 err:
615 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
616 kfree_skb(skb);
617 return -1;
621 * Check if this packet is complete.
622 * Returns NULL on failure by any reason, and pointer
623 * to current nexthdr field in reassembled frame.
625 * It is called with locked fq, and caller must check that
626 * queue is eligible for reassembly i.e. it is not COMPLETE,
627 * the last and the first frames arrived and all the bits are here.
629 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
630 struct net_device *dev)
632 struct sk_buff *fp, *head = fq->fragments;
633 int payload_len;
634 unsigned int nhoff;
636 fq_kill(fq);
638 /* Make the one we just received the head. */
639 if (prev) {
640 head = prev->next;
641 fp = skb_clone(head, GFP_ATOMIC);
643 if (!fp)
644 goto out_oom;
646 fp->next = head->next;
647 prev->next = fp;
649 skb_morph(head, fq->fragments);
650 head->next = fq->fragments->next;
652 kfree_skb(fq->fragments);
653 fq->fragments = head;
656 BUG_TRAP(head != NULL);
657 BUG_TRAP(FRAG6_CB(head)->offset == 0);
659 /* Unfragmented part is taken from the first segment. */
660 payload_len = ((head->data - skb_network_header(head)) -
661 sizeof(struct ipv6hdr) + fq->len -
662 sizeof(struct frag_hdr));
663 if (payload_len > IPV6_MAXPLEN)
664 goto out_oversize;
666 /* Head of list must not be cloned. */
667 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
668 goto out_oom;
670 /* If the first fragment is fragmented itself, we split
671 * it to two chunks: the first with data and paged part
672 * and the second, holding only fragments. */
673 if (skb_shinfo(head)->frag_list) {
674 struct sk_buff *clone;
675 int i, plen = 0;
677 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
678 goto out_oom;
679 clone->next = head->next;
680 head->next = clone;
681 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
682 skb_shinfo(head)->frag_list = NULL;
683 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
684 plen += skb_shinfo(head)->frags[i].size;
685 clone->len = clone->data_len = head->data_len - plen;
686 head->data_len -= clone->len;
687 head->len -= clone->len;
688 clone->csum = 0;
689 clone->ip_summed = head->ip_summed;
690 atomic_add(clone->truesize, &ip6_frag_mem);
693 /* We have to remove fragment header from datagram and to relocate
694 * header in order to calculate ICV correctly. */
695 nhoff = fq->nhoffset;
696 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
697 memmove(head->head + sizeof(struct frag_hdr), head->head,
698 (head->data - head->head) - sizeof(struct frag_hdr));
699 head->mac_header += sizeof(struct frag_hdr);
700 head->network_header += sizeof(struct frag_hdr);
702 skb_shinfo(head)->frag_list = head->next;
703 skb_reset_transport_header(head);
704 skb_push(head, head->data - skb_network_header(head));
705 atomic_sub(head->truesize, &ip6_frag_mem);
707 for (fp=head->next; fp; fp = fp->next) {
708 head->data_len += fp->len;
709 head->len += fp->len;
710 if (head->ip_summed != fp->ip_summed)
711 head->ip_summed = CHECKSUM_NONE;
712 else if (head->ip_summed == CHECKSUM_COMPLETE)
713 head->csum = csum_add(head->csum, fp->csum);
714 head->truesize += fp->truesize;
715 atomic_sub(fp->truesize, &ip6_frag_mem);
718 head->next = NULL;
719 head->dev = dev;
720 head->tstamp = fq->stamp;
721 ipv6_hdr(head)->payload_len = htons(payload_len);
722 IP6CB(head)->nhoff = nhoff;
724 /* Yes, and fold redundant checksum back. 8) */
725 if (head->ip_summed == CHECKSUM_COMPLETE)
726 head->csum = csum_partial(skb_network_header(head),
727 skb_network_header_len(head),
728 head->csum);
730 rcu_read_lock();
731 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
732 rcu_read_unlock();
733 fq->fragments = NULL;
734 return 1;
736 out_oversize:
737 if (net_ratelimit())
738 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
739 goto out_fail;
740 out_oom:
741 if (net_ratelimit())
742 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
743 out_fail:
744 rcu_read_lock();
745 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
746 rcu_read_unlock();
747 return -1;
750 static int ipv6_frag_rcv(struct sk_buff **skbp)
752 struct sk_buff *skb = *skbp;
753 struct frag_hdr *fhdr;
754 struct frag_queue *fq;
755 struct ipv6hdr *hdr = ipv6_hdr(skb);
757 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
759 /* Jumbo payload inhibits frag. header */
760 if (hdr->payload_len==0) {
761 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
762 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
763 skb_network_header_len(skb));
764 return -1;
766 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
767 sizeof(struct frag_hdr)))) {
768 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
769 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
770 skb_network_header_len(skb));
771 return -1;
774 hdr = ipv6_hdr(skb);
775 fhdr = (struct frag_hdr *)skb_transport_header(skb);
777 if (!(fhdr->frag_off & htons(0xFFF9))) {
778 /* It is not a fragmented frame */
779 skb->transport_header += sizeof(struct frag_hdr);
780 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
782 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
783 return 1;
786 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
787 ip6_evictor(ip6_dst_idev(skb->dst));
789 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
790 ip6_dst_idev(skb->dst))) != NULL) {
791 int ret;
793 spin_lock(&fq->lock);
795 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
797 spin_unlock(&fq->lock);
798 fq_put(fq, NULL);
799 return ret;
802 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
803 kfree_skb(skb);
804 return -1;
807 static struct inet6_protocol frag_protocol =
809 .handler = ipv6_frag_rcv,
810 .flags = INET6_PROTO_NOPOLICY,
813 void __init ipv6_frag_init(void)
815 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
816 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
818 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
819 (jiffies ^ (jiffies >> 6)));
821 init_timer(&ip6_frag_secret_timer);
822 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
823 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
824 add_timer(&ip6_frag_secret_timer);