patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / ipv6 / reassembly.c
1 /*
2  *      IPv6 fragment reassembly
3  *      Linux INET6 implementation 
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
7  *
8  *      $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
9  *
10  *      Based on: net/ipv4/ip_fragment.c
11  *
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.
16  */
17
18 /* 
19  *      Fixes:  
20  *      Andi Kleen      Make it work with multiple hosts.
21  *                      More RFC compliance.
22  *
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.
30  */
31 #include <linux/config.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/net.h>
39 #include <linux/list.h>
40 #include <linux/netdevice.h>
41 #include <linux/in6.h>
42 #include <linux/ipv6.h>
43 #include <linux/icmpv6.h>
44 #include <linux/random.h>
45 #include <linux/jhash.h>
46
47 #include <net/sock.h>
48 #include <net/snmp.h>
49
50 #include <net/ipv6.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>
56
57 int sysctl_ip6frag_high_thresh = 256*1024;
58 int sysctl_ip6frag_low_thresh = 192*1024;
59
60 int sysctl_ip6frag_time = IPV6_FRAG_TIMEOUT;
61
62 struct ip6frag_skb_cb
63 {
64         struct inet6_skb_parm   h;
65         int                     offset;
66 };
67
68 #define FRAG6_CB(skb)   ((struct ip6frag_skb_cb*)((skb)->cb))
69
70
71 /*
72  *      Equivalent of ipv4 struct ipq
73  */
74
75 struct frag_queue
76 {
77         struct frag_queue       *next;
78         struct list_head lru_list;              /* lru list member      */
79
80         __u32                   id;             /* fragment id          */
81         struct in6_addr         saddr;
82         struct in6_addr         daddr;
83
84         spinlock_t              lock;
85         atomic_t                refcnt;
86         struct timer_list       timer;          /* expire timer         */
87         struct sk_buff          *fragments;
88         int                     len;
89         int                     meat;
90         int                     iif;
91         struct timeval          stamp;
92         unsigned int            csum;
93         __u8                    last_in;        /* has first/last segment arrived? */
94 #define COMPLETE                4
95 #define FIRST_IN                2
96 #define LAST_IN                 1
97         __u16                   nhoffset;
98         struct frag_queue       **pprev;
99 };
100
101 /* Hash table. */
102
103 #define IP6Q_HASHSZ     64
104
105 static struct frag_queue *ip6_frag_hash[IP6Q_HASHSZ];
106 static rwlock_t ip6_frag_lock = RW_LOCK_UNLOCKED;
107 static u32 ip6_frag_hash_rnd;
108 static LIST_HEAD(ip6_frag_lru_list);
109 int ip6_frag_nqueues = 0;
110
111 static __inline__ void __fq_unlink(struct frag_queue *fq)
112 {
113         if(fq->next)
114                 fq->next->pprev = fq->pprev;
115         *fq->pprev = fq->next;
116         list_del(&fq->lru_list);
117         ip6_frag_nqueues--;
118 }
119
120 static __inline__ void fq_unlink(struct frag_queue *fq)
121 {
122         write_lock(&ip6_frag_lock);
123         __fq_unlink(fq);
124         write_unlock(&ip6_frag_lock);
125 }
126
127 static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr,
128                                struct in6_addr *daddr)
129 {
130         u32 a, b, c;
131
132         a = saddr->s6_addr32[0];
133         b = saddr->s6_addr32[1];
134         c = saddr->s6_addr32[2];
135
136         a += JHASH_GOLDEN_RATIO;
137         b += JHASH_GOLDEN_RATIO;
138         c += ip6_frag_hash_rnd;
139         __jhash_mix(a, b, c);
140
141         a += saddr->s6_addr32[3];
142         b += daddr->s6_addr32[0];
143         c += daddr->s6_addr32[1];
144         __jhash_mix(a, b, c);
145
146         a += daddr->s6_addr32[2];
147         b += daddr->s6_addr32[3];
148         c += id;
149         __jhash_mix(a, b, c);
150
151         return c & (IP6Q_HASHSZ - 1);
152 }
153
154 static struct timer_list ip6_frag_secret_timer;
155 int sysctl_ip6frag_secret_interval = 10 * 60 * HZ;
156
157 static void ip6_frag_secret_rebuild(unsigned long dummy)
158 {
159         unsigned long now = jiffies;
160         int i;
161
162         write_lock(&ip6_frag_lock);
163         get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
164         for (i = 0; i < IP6Q_HASHSZ; i++) {
165                 struct frag_queue *q;
166
167                 q = ip6_frag_hash[i];
168                 while (q) {
169                         struct frag_queue *next = q->next;
170                         unsigned int hval = ip6qhashfn(q->id,
171                                                        &q->saddr,
172                                                        &q->daddr);
173
174                         if (hval != i) {
175                                 /* Unlink. */
176                                 if (q->next)
177                                         q->next->pprev = q->pprev;
178                                 *q->pprev = q->next;
179
180                                 /* Relink to new hash chain. */
181                                 if ((q->next = ip6_frag_hash[hval]) != NULL)
182                                         q->next->pprev = &q->next;
183                                 ip6_frag_hash[hval] = q;
184                                 q->pprev = &ip6_frag_hash[hval];
185                         }
186
187                         q = next;
188                 }
189         }
190         write_unlock(&ip6_frag_lock);
191
192         mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
193 }
194
195 atomic_t ip6_frag_mem = ATOMIC_INIT(0);
196
197 /* Memory Tracking Functions. */
198 static inline void frag_kfree_skb(struct sk_buff *skb)
199 {
200         atomic_sub(skb->truesize, &ip6_frag_mem);
201         kfree_skb(skb);
202 }
203
204 static inline void frag_free_queue(struct frag_queue *fq)
205 {
206         atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
207         kfree(fq);
208 }
209
210 static inline struct frag_queue *frag_alloc_queue(void)
211 {
212         struct frag_queue *fq = kmalloc(sizeof(struct frag_queue), GFP_ATOMIC);
213
214         if(!fq)
215                 return NULL;
216         atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
217         return fq;
218 }
219
220 /* Destruction primitives. */
221
222 /* Complete destruction of fq. */
223 static void ip6_frag_destroy(struct frag_queue *fq)
224 {
225         struct sk_buff *fp;
226
227         BUG_TRAP(fq->last_in&COMPLETE);
228         BUG_TRAP(del_timer(&fq->timer) == 0);
229
230         /* Release all fragment data. */
231         fp = fq->fragments;
232         while (fp) {
233                 struct sk_buff *xp = fp->next;
234
235                 frag_kfree_skb(fp);
236                 fp = xp;
237         }
238
239         frag_free_queue(fq);
240 }
241
242 static __inline__ void fq_put(struct frag_queue *fq)
243 {
244         if (atomic_dec_and_test(&fq->refcnt))
245                 ip6_frag_destroy(fq);
246 }
247
248 /* Kill fq entry. It is not destroyed immediately,
249  * because caller (and someone more) holds reference count.
250  */
251 static __inline__ void fq_kill(struct frag_queue *fq)
252 {
253         if (del_timer(&fq->timer))
254                 atomic_dec(&fq->refcnt);
255
256         if (!(fq->last_in & COMPLETE)) {
257                 fq_unlink(fq);
258                 atomic_dec(&fq->refcnt);
259                 fq->last_in |= COMPLETE;
260         }
261 }
262
263 static void ip6_evictor(void)
264 {
265         struct frag_queue *fq;
266         struct list_head *tmp;
267
268         for(;;) {
269                 if (atomic_read(&ip6_frag_mem) <= sysctl_ip6frag_low_thresh)
270                         return;
271                 read_lock(&ip6_frag_lock);
272                 if (list_empty(&ip6_frag_lru_list)) {
273                         read_unlock(&ip6_frag_lock);
274                         return;
275                 }
276                 tmp = ip6_frag_lru_list.next;
277                 fq = list_entry(tmp, struct frag_queue, lru_list);
278                 atomic_inc(&fq->refcnt);
279                 read_unlock(&ip6_frag_lock);
280
281                 spin_lock(&fq->lock);
282                 if (!(fq->last_in&COMPLETE))
283                         fq_kill(fq);
284                 spin_unlock(&fq->lock);
285
286                 fq_put(fq);
287                 IP6_INC_STATS_BH(ReasmFails);
288         }
289 }
290
291 static void ip6_frag_expire(unsigned long data)
292 {
293         struct frag_queue *fq = (struct frag_queue *) data;
294
295         spin_lock(&fq->lock);
296
297         if (fq->last_in & COMPLETE)
298                 goto out;
299
300         fq_kill(fq);
301
302         IP6_INC_STATS_BH(ReasmTimeout);
303         IP6_INC_STATS_BH(ReasmFails);
304
305         /* Send error only if the first segment arrived. */
306         if (fq->last_in&FIRST_IN && fq->fragments) {
307                 struct net_device *dev = dev_get_by_index(fq->iif);
308
309                 /*
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.
313                  */
314                 if (dev) {
315                         fq->fragments->dev = dev;
316                         icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0,
317                                     dev);
318                         dev_put(dev);
319                 }
320         }
321 out:
322         spin_unlock(&fq->lock);
323         fq_put(fq);
324 }
325
326 /* Creation primitives. */
327
328
329 static struct frag_queue *ip6_frag_intern(unsigned int hash,
330                                           struct frag_queue *fq_in)
331 {
332         struct frag_queue *fq;
333
334         write_lock(&ip6_frag_lock);
335 #ifdef CONFIG_SMP
336         for (fq = ip6_frag_hash[hash]; fq; fq = fq->next) {
337                 if (fq->id == fq_in->id && 
338                     !ipv6_addr_cmp(&fq_in->saddr, &fq->saddr) &&
339                     !ipv6_addr_cmp(&fq_in->daddr, &fq->daddr)) {
340                         atomic_inc(&fq->refcnt);
341                         write_unlock(&ip6_frag_lock);
342                         fq_in->last_in |= COMPLETE;
343                         fq_put(fq_in);
344                         return fq;
345                 }
346         }
347 #endif
348         fq = fq_in;
349
350         if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
351                 atomic_inc(&fq->refcnt);
352
353         atomic_inc(&fq->refcnt);
354         if((fq->next = ip6_frag_hash[hash]) != NULL)
355                 fq->next->pprev = &fq->next;
356         ip6_frag_hash[hash] = fq;
357         fq->pprev = &ip6_frag_hash[hash];
358         INIT_LIST_HEAD(&fq->lru_list);
359         list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
360         ip6_frag_nqueues++;
361         write_unlock(&ip6_frag_lock);
362         return fq;
363 }
364
365
366 static struct frag_queue *
367 ip6_frag_create(unsigned int hash, u32 id, struct in6_addr *src, struct in6_addr *dst)
368 {
369         struct frag_queue *fq;
370
371         if ((fq = frag_alloc_queue()) == NULL)
372                 goto oom;
373
374         memset(fq, 0, sizeof(struct frag_queue));
375
376         fq->id = id;
377         ipv6_addr_copy(&fq->saddr, src);
378         ipv6_addr_copy(&fq->daddr, dst);
379
380         init_timer(&fq->timer);
381         fq->timer.function = ip6_frag_expire;
382         fq->timer.data = (long) fq;
383         fq->lock = SPIN_LOCK_UNLOCKED;
384         atomic_set(&fq->refcnt, 1);
385
386         return ip6_frag_intern(hash, fq);
387
388 oom:
389         IP6_INC_STATS_BH(ReasmFails);
390         return NULL;
391 }
392
393 static __inline__ struct frag_queue *
394 fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst)
395 {
396         struct frag_queue *fq;
397         unsigned int hash = ip6qhashfn(id, src, dst);
398
399         read_lock(&ip6_frag_lock);
400         for(fq = ip6_frag_hash[hash]; fq; fq = fq->next) {
401                 if (fq->id == id && 
402                     !ipv6_addr_cmp(src, &fq->saddr) &&
403                     !ipv6_addr_cmp(dst, &fq->daddr)) {
404                         atomic_inc(&fq->refcnt);
405                         read_unlock(&ip6_frag_lock);
406                         return fq;
407                 }
408         }
409         read_unlock(&ip6_frag_lock);
410
411         return ip6_frag_create(hash, id, src, dst);
412 }
413
414
415 static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 
416                            struct frag_hdr *fhdr, int nhoff)
417 {
418         struct sk_buff *prev, *next;
419         int offset, end;
420
421         if (fq->last_in & COMPLETE)
422                 goto err;
423
424         offset = ntohs(fhdr->frag_off) & ~0x7;
425         end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
426                         ((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
427
428         if ((unsigned int)end > IPV6_MAXPLEN) {
429                 IP6_INC_STATS_BH(InHdrErrors);
430                 icmpv6_param_prob(skb,ICMPV6_HDR_FIELD, (u8*)&fhdr->frag_off - skb->nh.raw);
431                 return;
432         }
433
434         if (skb->ip_summed == CHECKSUM_HW)
435                 skb->csum = csum_sub(skb->csum,
436                                      csum_partial(skb->nh.raw, (u8*)(fhdr+1)-skb->nh.raw, 0));
437
438         /* Is this the final fragment? */
439         if (!(fhdr->frag_off & htons(IP6_MF))) {
440                 /* If we already have some bits beyond end
441                  * or have different end, the segment is corrupted.
442                  */
443                 if (end < fq->len ||
444                     ((fq->last_in & LAST_IN) && end != fq->len))
445                         goto err;
446                 fq->last_in |= LAST_IN;
447                 fq->len = end;
448         } else {
449                 /* Check if the fragment is rounded to 8 bytes.
450                  * Required by the RFC.
451                  */
452                 if (end & 0x7) {
453                         /* RFC2460 says always send parameter problem in
454                          * this case. -DaveM
455                          */
456                         IP6_INC_STATS_BH(InHdrErrors);
457                         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 
458                                           offsetof(struct ipv6hdr, payload_len));
459                         return;
460                 }
461                 if (end > fq->len) {
462                         /* Some bits beyond end -> corruption. */
463                         if (fq->last_in & LAST_IN)
464                                 goto err;
465                         fq->len = end;
466                 }
467         }
468
469         if (end == offset)
470                 goto err;
471
472         /* Point into the IP datagram 'data' part. */
473         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
474                 goto err;
475         if (end-offset < skb->len) {
476                 if (pskb_trim(skb, end - offset))
477                         goto err;
478                 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
479                         skb->ip_summed = CHECKSUM_NONE;
480         }
481
482         /* Find out which fragments are in front and at the back of us
483          * in the chain of fragments so far.  We must know where to put
484          * this fragment, right?
485          */
486         prev = NULL;
487         for(next = fq->fragments; next != NULL; next = next->next) {
488                 if (FRAG6_CB(next)->offset >= offset)
489                         break;  /* bingo! */
490                 prev = next;
491         }
492
493         /* We found where to put this one.  Check for overlap with
494          * preceding fragment, and, if needed, align things so that
495          * any overlaps are eliminated.
496          */
497         if (prev) {
498                 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
499
500                 if (i > 0) {
501                         offset += i;
502                         if (end <= offset)
503                                 goto err;
504                         if (!pskb_pull(skb, i))
505                                 goto err;
506                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
507                                 skb->ip_summed = CHECKSUM_NONE;
508                 }
509         }
510
511         /* Look for overlap with succeeding segments.
512          * If we can merge fragments, do it.
513          */
514         while (next && FRAG6_CB(next)->offset < end) {
515                 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
516
517                 if (i < next->len) {
518                         /* Eat head of the next overlapped fragment
519                          * and leave the loop. The next ones cannot overlap.
520                          */
521                         if (!pskb_pull(next, i))
522                                 goto err;
523                         FRAG6_CB(next)->offset += i;    /* next fragment */
524                         fq->meat -= i;
525                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
526                                 next->ip_summed = CHECKSUM_NONE;
527                         break;
528                 } else {
529                         struct sk_buff *free_it = next;
530
531                         /* Old fragment is completely overridden with
532                          * new one drop it.
533                          */
534                         next = next->next;
535
536                         if (prev)
537                                 prev->next = next;
538                         else
539                                 fq->fragments = next;
540
541                         fq->meat -= free_it->len;
542                         frag_kfree_skb(free_it);
543                 }
544         }
545
546         FRAG6_CB(skb)->offset = offset;
547
548         /* Insert this fragment in the chain of fragments. */
549         skb->next = next;
550         if (prev)
551                 prev->next = skb;
552         else
553                 fq->fragments = skb;
554
555         if (skb->dev)
556                 fq->iif = skb->dev->ifindex;
557         skb->dev = NULL;
558         fq->stamp = skb->stamp;
559         fq->meat += skb->len;
560         atomic_add(skb->truesize, &ip6_frag_mem);
561
562         /* The first fragment.
563          * nhoffset is obtained from the first fragment, of course.
564          */
565         if (offset == 0) {
566                 fq->nhoffset = nhoff;
567                 fq->last_in |= FIRST_IN;
568         }
569         write_lock(&ip6_frag_lock);
570         list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
571         write_unlock(&ip6_frag_lock);
572         return;
573
574 err:
575         IP6_INC_STATS(ReasmFails);
576         kfree_skb(skb);
577 }
578
579 /*
580  *      Check if this packet is complete.
581  *      Returns NULL on failure by any reason, and pointer
582  *      to current nexthdr field in reassembled frame.
583  *
584  *      It is called with locked fq, and caller must check that
585  *      queue is eligible for reassembly i.e. it is not COMPLETE,
586  *      the last and the first frames arrived and all the bits are here.
587  */
588 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
589                           unsigned int *nhoffp,
590                           struct net_device *dev)
591 {
592         struct sk_buff *fp, *head = fq->fragments;
593         int    payload_len;
594         unsigned int nhoff;
595
596         fq_kill(fq);
597
598         BUG_TRAP(head != NULL);
599         BUG_TRAP(FRAG6_CB(head)->offset == 0);
600
601         /* Unfragmented part is taken from the first segment. */
602         payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
603         if (payload_len > IPV6_MAXPLEN)
604                 goto out_oversize;
605
606         /* Head of list must not be cloned. */
607         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
608                 goto out_oom;
609
610         /* If the first fragment is fragmented itself, we split
611          * it to two chunks: the first with data and paged part
612          * and the second, holding only fragments. */
613         if (skb_shinfo(head)->frag_list) {
614                 struct sk_buff *clone;
615                 int i, plen = 0;
616
617                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
618                         goto out_oom;
619                 clone->next = head->next;
620                 head->next = clone;
621                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
622                 skb_shinfo(head)->frag_list = NULL;
623                 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
624                         plen += skb_shinfo(head)->frags[i].size;
625                 clone->len = clone->data_len = head->data_len - plen;
626                 head->data_len -= clone->len;
627                 head->len -= clone->len;
628                 clone->csum = 0;
629                 clone->ip_summed = head->ip_summed;
630                 atomic_add(clone->truesize, &ip6_frag_mem);
631         }
632
633         /* We have to remove fragment header from datagram and to relocate
634          * header in order to calculate ICV correctly. */
635         nhoff = fq->nhoffset;
636         head->nh.raw[nhoff] = head->h.raw[0];
637         memmove(head->head + sizeof(struct frag_hdr), head->head, 
638                 (head->data - head->head) - sizeof(struct frag_hdr));
639         head->mac.raw += sizeof(struct frag_hdr);
640         head->nh.raw += sizeof(struct frag_hdr);
641
642         skb_shinfo(head)->frag_list = head->next;
643         head->h.raw = head->data;
644         skb_push(head, head->data - head->nh.raw);
645         atomic_sub(head->truesize, &ip6_frag_mem);
646
647         for (fp=head->next; fp; fp = fp->next) {
648                 head->data_len += fp->len;
649                 head->len += fp->len;
650                 if (head->ip_summed != fp->ip_summed)
651                         head->ip_summed = CHECKSUM_NONE;
652                 else if (head->ip_summed == CHECKSUM_HW)
653                         head->csum = csum_add(head->csum, fp->csum);
654                 head->truesize += fp->truesize;
655                 atomic_sub(fp->truesize, &ip6_frag_mem);
656         }
657
658         head->next = NULL;
659         head->dev = dev;
660         head->stamp = fq->stamp;
661         head->nh.ipv6h->payload_len = ntohs(payload_len);
662
663         *skb_in = head;
664
665         /* Yes, and fold redundant checksum back. 8) */
666         if (head->ip_summed == CHECKSUM_HW)
667                 head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
668
669         IP6_INC_STATS_BH(ReasmOKs);
670         fq->fragments = NULL;
671         *nhoffp = nhoff;
672         return 1;
673
674 out_oversize:
675         if (net_ratelimit())
676                 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
677         goto out_fail;
678 out_oom:
679         if (net_ratelimit())
680                 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
681 out_fail:
682         IP6_INC_STATS_BH(ReasmFails);
683         return -1;
684 }
685
686 static int ipv6_frag_rcv(struct sk_buff **skbp, unsigned int *nhoffp)
687 {
688         struct sk_buff *skb = *skbp; 
689         struct net_device *dev = skb->dev;
690         struct frag_hdr *fhdr;
691         struct frag_queue *fq;
692         struct ipv6hdr *hdr;
693
694         hdr = skb->nh.ipv6h;
695
696         IP6_INC_STATS_BH(ReasmReqds);
697
698         /* Jumbo payload inhibits frag. header */
699         if (hdr->payload_len==0) {
700                 IP6_INC_STATS(InHdrErrors);
701                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
702                 return -1;
703         }
704         if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+sizeof(struct frag_hdr))) {
705                 IP6_INC_STATS(InHdrErrors);
706                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
707                 return -1;
708         }
709
710         hdr = skb->nh.ipv6h;
711         fhdr = (struct frag_hdr *)skb->h.raw;
712
713         if (!(fhdr->frag_off & htons(0xFFF9))) {
714                 /* It is not a fragmented frame */
715                 skb->h.raw += sizeof(struct frag_hdr);
716                 IP6_INC_STATS_BH(ReasmOKs);
717
718                 *nhoffp = (u8*)fhdr - skb->nh.raw;
719                 return 1;
720         }
721
722         if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
723                 ip6_evictor();
724
725         if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr)) != NULL) {
726                 int ret = -1;
727
728                 spin_lock(&fq->lock);
729
730                 ip6_frag_queue(fq, skb, fhdr, *nhoffp);
731
732                 if (fq->last_in == (FIRST_IN|LAST_IN) &&
733                     fq->meat == fq->len)
734                         ret = ip6_frag_reasm(fq, skbp, nhoffp, dev);
735
736                 spin_unlock(&fq->lock);
737                 fq_put(fq);
738                 return ret;
739         }
740
741         IP6_INC_STATS_BH(ReasmFails);
742         kfree_skb(skb);
743         return -1;
744 }
745
746 static struct inet6_protocol frag_protocol =
747 {
748         .handler        =       ipv6_frag_rcv,
749         .flags          =       INET6_PROTO_NOPOLICY,
750 };
751
752 void __init ipv6_frag_init(void)
753 {
754         if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
755                 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
756
757         ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
758                                    (jiffies ^ (jiffies >> 6)));
759
760         init_timer(&ip6_frag_secret_timer);
761         ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
762         ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
763         add_timer(&ip6_frag_secret_timer);
764 }