Merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6.git/
[linux-2.6] / 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 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;
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, int *work)
199 {
200         if (work)
201                 *work -= skb->truesize;
202         atomic_sub(skb->truesize, &ip6_frag_mem);
203         kfree_skb(skb);
204 }
205
206 static inline void frag_free_queue(struct frag_queue *fq, int *work)
207 {
208         if (work)
209                 *work -= sizeof(struct frag_queue);
210         atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
211         kfree(fq);
212 }
213
214 static inline struct frag_queue *frag_alloc_queue(void)
215 {
216         struct frag_queue *fq = kmalloc(sizeof(struct frag_queue), GFP_ATOMIC);
217
218         if(!fq)
219                 return NULL;
220         atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
221         return fq;
222 }
223
224 /* Destruction primitives. */
225
226 /* Complete destruction of fq. */
227 static void ip6_frag_destroy(struct frag_queue *fq, int *work)
228 {
229         struct sk_buff *fp;
230
231         BUG_TRAP(fq->last_in&COMPLETE);
232         BUG_TRAP(del_timer(&fq->timer) == 0);
233
234         /* Release all fragment data. */
235         fp = fq->fragments;
236         while (fp) {
237                 struct sk_buff *xp = fp->next;
238
239                 frag_kfree_skb(fp, work);
240                 fp = xp;
241         }
242
243         frag_free_queue(fq, work);
244 }
245
246 static __inline__ void fq_put(struct frag_queue *fq, int *work)
247 {
248         if (atomic_dec_and_test(&fq->refcnt))
249                 ip6_frag_destroy(fq, work);
250 }
251
252 /* Kill fq entry. It is not destroyed immediately,
253  * because caller (and someone more) holds reference count.
254  */
255 static __inline__ void fq_kill(struct frag_queue *fq)
256 {
257         if (del_timer(&fq->timer))
258                 atomic_dec(&fq->refcnt);
259
260         if (!(fq->last_in & COMPLETE)) {
261                 fq_unlink(fq);
262                 atomic_dec(&fq->refcnt);
263                 fq->last_in |= COMPLETE;
264         }
265 }
266
267 static void ip6_evictor(void)
268 {
269         struct frag_queue *fq;
270         struct list_head *tmp;
271         int work;
272
273         work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
274         if (work <= 0)
275                 return;
276
277         while(work > 0) {
278                 read_lock(&ip6_frag_lock);
279                 if (list_empty(&ip6_frag_lru_list)) {
280                         read_unlock(&ip6_frag_lock);
281                         return;
282                 }
283                 tmp = ip6_frag_lru_list.next;
284                 fq = list_entry(tmp, struct frag_queue, lru_list);
285                 atomic_inc(&fq->refcnt);
286                 read_unlock(&ip6_frag_lock);
287
288                 spin_lock(&fq->lock);
289                 if (!(fq->last_in&COMPLETE))
290                         fq_kill(fq);
291                 spin_unlock(&fq->lock);
292
293                 fq_put(fq, &work);
294                 IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
295         }
296 }
297
298 static void ip6_frag_expire(unsigned long data)
299 {
300         struct frag_queue *fq = (struct frag_queue *) data;
301
302         spin_lock(&fq->lock);
303
304         if (fq->last_in & COMPLETE)
305                 goto out;
306
307         fq_kill(fq);
308
309         IP6_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
310         IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
311
312         /* Send error only if the first segment arrived. */
313         if (fq->last_in&FIRST_IN && fq->fragments) {
314                 struct net_device *dev = dev_get_by_index(fq->iif);
315
316                 /*
317                    But use as source device on which LAST ARRIVED
318                    segment was received. And do not use fq->dev
319                    pointer directly, device might already disappeared.
320                  */
321                 if (dev) {
322                         fq->fragments->dev = dev;
323                         icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0,
324                                     dev);
325                         dev_put(dev);
326                 }
327         }
328 out:
329         spin_unlock(&fq->lock);
330         fq_put(fq, NULL);
331 }
332
333 /* Creation primitives. */
334
335
336 static struct frag_queue *ip6_frag_intern(unsigned int hash,
337                                           struct frag_queue *fq_in)
338 {
339         struct frag_queue *fq;
340
341         write_lock(&ip6_frag_lock);
342 #ifdef CONFIG_SMP
343         for (fq = ip6_frag_hash[hash]; fq; fq = fq->next) {
344                 if (fq->id == fq_in->id && 
345                     ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
346                     ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
347                         atomic_inc(&fq->refcnt);
348                         write_unlock(&ip6_frag_lock);
349                         fq_in->last_in |= COMPLETE;
350                         fq_put(fq_in, NULL);
351                         return fq;
352                 }
353         }
354 #endif
355         fq = fq_in;
356
357         if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
358                 atomic_inc(&fq->refcnt);
359
360         atomic_inc(&fq->refcnt);
361         if((fq->next = ip6_frag_hash[hash]) != NULL)
362                 fq->next->pprev = &fq->next;
363         ip6_frag_hash[hash] = fq;
364         fq->pprev = &ip6_frag_hash[hash];
365         INIT_LIST_HEAD(&fq->lru_list);
366         list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
367         ip6_frag_nqueues++;
368         write_unlock(&ip6_frag_lock);
369         return fq;
370 }
371
372
373 static struct frag_queue *
374 ip6_frag_create(unsigned int hash, u32 id, struct in6_addr *src, struct in6_addr *dst)
375 {
376         struct frag_queue *fq;
377
378         if ((fq = frag_alloc_queue()) == NULL)
379                 goto oom;
380
381         memset(fq, 0, sizeof(struct frag_queue));
382
383         fq->id = id;
384         ipv6_addr_copy(&fq->saddr, src);
385         ipv6_addr_copy(&fq->daddr, dst);
386
387         init_timer(&fq->timer);
388         fq->timer.function = ip6_frag_expire;
389         fq->timer.data = (long) fq;
390         spin_lock_init(&fq->lock);
391         atomic_set(&fq->refcnt, 1);
392
393         return ip6_frag_intern(hash, fq);
394
395 oom:
396         IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
397         return NULL;
398 }
399
400 static __inline__ struct frag_queue *
401 fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst)
402 {
403         struct frag_queue *fq;
404         unsigned int hash = ip6qhashfn(id, src, dst);
405
406         read_lock(&ip6_frag_lock);
407         for(fq = ip6_frag_hash[hash]; fq; fq = fq->next) {
408                 if (fq->id == id && 
409                     ipv6_addr_equal(src, &fq->saddr) &&
410                     ipv6_addr_equal(dst, &fq->daddr)) {
411                         atomic_inc(&fq->refcnt);
412                         read_unlock(&ip6_frag_lock);
413                         return fq;
414                 }
415         }
416         read_unlock(&ip6_frag_lock);
417
418         return ip6_frag_create(hash, id, src, dst);
419 }
420
421
422 static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 
423                            struct frag_hdr *fhdr, int nhoff)
424 {
425         struct sk_buff *prev, *next;
426         int offset, end;
427
428         if (fq->last_in & COMPLETE)
429                 goto err;
430
431         offset = ntohs(fhdr->frag_off) & ~0x7;
432         end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
433                         ((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
434
435         if ((unsigned int)end > IPV6_MAXPLEN) {
436                 IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
437                 icmpv6_param_prob(skb,ICMPV6_HDR_FIELD, (u8*)&fhdr->frag_off - skb->nh.raw);
438                 return;
439         }
440
441         if (skb->ip_summed == CHECKSUM_HW)
442                 skb->csum = csum_sub(skb->csum,
443                                      csum_partial(skb->nh.raw, (u8*)(fhdr+1)-skb->nh.raw, 0));
444
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.
449                  */
450                 if (end < fq->len ||
451                     ((fq->last_in & LAST_IN) && end != fq->len))
452                         goto err;
453                 fq->last_in |= LAST_IN;
454                 fq->len = end;
455         } else {
456                 /* Check if the fragment is rounded to 8 bytes.
457                  * Required by the RFC.
458                  */
459                 if (end & 0x7) {
460                         /* RFC2460 says always send parameter problem in
461                          * this case. -DaveM
462                          */
463                         IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
464                         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 
465                                           offsetof(struct ipv6hdr, payload_len));
466                         return;
467                 }
468                 if (end > fq->len) {
469                         /* Some bits beyond end -> corruption. */
470                         if (fq->last_in & LAST_IN)
471                                 goto err;
472                         fq->len = end;
473                 }
474         }
475
476         if (end == offset)
477                 goto err;
478
479         /* Point into the IP datagram 'data' part. */
480         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
481                 goto err;
482         if (end-offset < skb->len) {
483                 if (pskb_trim(skb, end - offset))
484                         goto err;
485                 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
486                         skb->ip_summed = CHECKSUM_NONE;
487         }
488
489         /* Find out which fragments are in front and at the back of us
490          * in the chain of fragments so far.  We must know where to put
491          * this fragment, right?
492          */
493         prev = NULL;
494         for(next = fq->fragments; next != NULL; next = next->next) {
495                 if (FRAG6_CB(next)->offset >= offset)
496                         break;  /* bingo! */
497                 prev = next;
498         }
499
500         /* We found where to put this one.  Check for overlap with
501          * preceding fragment, and, if needed, align things so that
502          * any overlaps are eliminated.
503          */
504         if (prev) {
505                 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
506
507                 if (i > 0) {
508                         offset += i;
509                         if (end <= offset)
510                                 goto err;
511                         if (!pskb_pull(skb, i))
512                                 goto err;
513                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
514                                 skb->ip_summed = CHECKSUM_NONE;
515                 }
516         }
517
518         /* Look for overlap with succeeding segments.
519          * If we can merge fragments, do it.
520          */
521         while (next && FRAG6_CB(next)->offset < end) {
522                 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
523
524                 if (i < next->len) {
525                         /* Eat head of the next overlapped fragment
526                          * and leave the loop. The next ones cannot overlap.
527                          */
528                         if (!pskb_pull(next, i))
529                                 goto err;
530                         FRAG6_CB(next)->offset += i;    /* next fragment */
531                         fq->meat -= i;
532                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
533                                 next->ip_summed = CHECKSUM_NONE;
534                         break;
535                 } else {
536                         struct sk_buff *free_it = next;
537
538                         /* Old fragment is completely overridden with
539                          * new one drop it.
540                          */
541                         next = next->next;
542
543                         if (prev)
544                                 prev->next = next;
545                         else
546                                 fq->fragments = next;
547
548                         fq->meat -= free_it->len;
549                         frag_kfree_skb(free_it, NULL);
550                 }
551         }
552
553         FRAG6_CB(skb)->offset = offset;
554
555         /* Insert this fragment in the chain of fragments. */
556         skb->next = next;
557         if (prev)
558                 prev->next = skb;
559         else
560                 fq->fragments = skb;
561
562         if (skb->dev)
563                 fq->iif = skb->dev->ifindex;
564         skb->dev = NULL;
565         fq->stamp = skb->stamp;
566         fq->meat += skb->len;
567         atomic_add(skb->truesize, &ip6_frag_mem);
568
569         /* The first fragment.
570          * nhoffset is obtained from the first fragment, of course.
571          */
572         if (offset == 0) {
573                 fq->nhoffset = nhoff;
574                 fq->last_in |= FIRST_IN;
575         }
576         write_lock(&ip6_frag_lock);
577         list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
578         write_unlock(&ip6_frag_lock);
579         return;
580
581 err:
582         IP6_INC_STATS(IPSTATS_MIB_REASMFAILS);
583         kfree_skb(skb);
584 }
585
586 /*
587  *      Check if this packet is complete.
588  *      Returns NULL on failure by any reason, and pointer
589  *      to current nexthdr field in reassembled frame.
590  *
591  *      It is called with locked fq, and caller must check that
592  *      queue is eligible for reassembly i.e. it is not COMPLETE,
593  *      the last and the first frames arrived and all the bits are here.
594  */
595 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
596                           unsigned int *nhoffp,
597                           struct net_device *dev)
598 {
599         struct sk_buff *fp, *head = fq->fragments;
600         int    payload_len;
601         unsigned int nhoff;
602
603         fq_kill(fq);
604
605         BUG_TRAP(head != NULL);
606         BUG_TRAP(FRAG6_CB(head)->offset == 0);
607
608         /* Unfragmented part is taken from the first segment. */
609         payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
610         if (payload_len > IPV6_MAXPLEN)
611                 goto out_oversize;
612
613         /* Head of list must not be cloned. */
614         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
615                 goto out_oom;
616
617         /* If the first fragment is fragmented itself, we split
618          * it to two chunks: the first with data and paged part
619          * and the second, holding only fragments. */
620         if (skb_shinfo(head)->frag_list) {
621                 struct sk_buff *clone;
622                 int i, plen = 0;
623
624                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
625                         goto out_oom;
626                 clone->next = head->next;
627                 head->next = clone;
628                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
629                 skb_shinfo(head)->frag_list = NULL;
630                 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
631                         plen += skb_shinfo(head)->frags[i].size;
632                 clone->len = clone->data_len = head->data_len - plen;
633                 head->data_len -= clone->len;
634                 head->len -= clone->len;
635                 clone->csum = 0;
636                 clone->ip_summed = head->ip_summed;
637                 atomic_add(clone->truesize, &ip6_frag_mem);
638         }
639
640         /* We have to remove fragment header from datagram and to relocate
641          * header in order to calculate ICV correctly. */
642         nhoff = fq->nhoffset;
643         head->nh.raw[nhoff] = head->h.raw[0];
644         memmove(head->head + sizeof(struct frag_hdr), head->head, 
645                 (head->data - head->head) - sizeof(struct frag_hdr));
646         head->mac.raw += sizeof(struct frag_hdr);
647         head->nh.raw += sizeof(struct frag_hdr);
648
649         skb_shinfo(head)->frag_list = head->next;
650         head->h.raw = head->data;
651         skb_push(head, head->data - head->nh.raw);
652         atomic_sub(head->truesize, &ip6_frag_mem);
653
654         for (fp=head->next; fp; fp = fp->next) {
655                 head->data_len += fp->len;
656                 head->len += fp->len;
657                 if (head->ip_summed != fp->ip_summed)
658                         head->ip_summed = CHECKSUM_NONE;
659                 else if (head->ip_summed == CHECKSUM_HW)
660                         head->csum = csum_add(head->csum, fp->csum);
661                 head->truesize += fp->truesize;
662                 atomic_sub(fp->truesize, &ip6_frag_mem);
663         }
664
665         head->next = NULL;
666         head->dev = dev;
667         head->stamp = fq->stamp;
668         head->nh.ipv6h->payload_len = htons(payload_len);
669
670         *skb_in = head;
671
672         /* Yes, and fold redundant checksum back. 8) */
673         if (head->ip_summed == CHECKSUM_HW)
674                 head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
675
676         IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
677         fq->fragments = NULL;
678         *nhoffp = nhoff;
679         return 1;
680
681 out_oversize:
682         if (net_ratelimit())
683                 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
684         goto out_fail;
685 out_oom:
686         if (net_ratelimit())
687                 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
688 out_fail:
689         IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
690         return -1;
691 }
692
693 static int ipv6_frag_rcv(struct sk_buff **skbp, unsigned int *nhoffp)
694 {
695         struct sk_buff *skb = *skbp; 
696         struct net_device *dev = skb->dev;
697         struct frag_hdr *fhdr;
698         struct frag_queue *fq;
699         struct ipv6hdr *hdr;
700
701         hdr = skb->nh.ipv6h;
702
703         IP6_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
704
705         /* Jumbo payload inhibits frag. header */
706         if (hdr->payload_len==0) {
707                 IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
708                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
709                 return -1;
710         }
711         if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+sizeof(struct frag_hdr))) {
712                 IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
713                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
714                 return -1;
715         }
716
717         hdr = skb->nh.ipv6h;
718         fhdr = (struct frag_hdr *)skb->h.raw;
719
720         if (!(fhdr->frag_off & htons(0xFFF9))) {
721                 /* It is not a fragmented frame */
722                 skb->h.raw += sizeof(struct frag_hdr);
723                 IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
724
725                 *nhoffp = (u8*)fhdr - skb->nh.raw;
726                 return 1;
727         }
728
729         if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
730                 ip6_evictor();
731
732         if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr)) != NULL) {
733                 int ret = -1;
734
735                 spin_lock(&fq->lock);
736
737                 ip6_frag_queue(fq, skb, fhdr, *nhoffp);
738
739                 if (fq->last_in == (FIRST_IN|LAST_IN) &&
740                     fq->meat == fq->len)
741                         ret = ip6_frag_reasm(fq, skbp, nhoffp, dev);
742
743                 spin_unlock(&fq->lock);
744                 fq_put(fq, NULL);
745                 return ret;
746         }
747
748         IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
749         kfree_skb(skb);
750         return -1;
751 }
752
753 static struct inet6_protocol frag_protocol =
754 {
755         .handler        =       ipv6_frag_rcv,
756         .flags          =       INET6_PROTO_NOPOLICY,
757 };
758
759 void __init ipv6_frag_init(void)
760 {
761         if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
762                 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
763
764         ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
765                                    (jiffies ^ (jiffies >> 6)));
766
767         init_timer(&ip6_frag_secret_timer);
768         ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
769         ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
770         add_timer(&ip6_frag_secret_timer);
771 }