This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
1 /*
2  * IPv6 fragment reassembly for connection tracking
3  *
4  * Copyright (C)2004 USAGI/WIDE Project
5  *
6  * Author:
7  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8  *
9  * Based on: net/ipv6/reassembly.c
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  */
16
17 #include <linux/config.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/jiffies.h>
24 #include <linux/net.h>
25 #include <linux/list.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/ipv6.h>
29 #include <linux/icmpv6.h>
30 #include <linux/random.h>
31 #include <linux/jhash.h>
32
33 #include <net/sock.h>
34 #include <net/snmp.h>
35
36 #include <net/ipv6.h>
37 #include <net/protocol.h>
38 #include <net/transp_v6.h>
39 #include <net/rawv6.h>
40 #include <net/ndisc.h>
41 #include <net/addrconf.h>
42 #include <linux/sysctl.h>
43 #include <linux/netfilter.h>
44 #include <linux/netfilter_ipv6.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47
48 #if 0
49 #define DEBUGP printk
50 #else
51 #define DEBUGP(format, args...)
52 #endif
53
54 #define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */
55 #define NF_CT_FRAG6_LOW_THRESH 196608  /* == 192*1024 */
56 #define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUT
57
58 unsigned int nf_ct_frag6_high_thresh = 256*1024;
59 unsigned int nf_ct_frag6_low_thresh = 192*1024;
60 unsigned long nf_ct_frag6_timeout = IPV6_FRAG_TIMEOUT;
61
62 struct nf_ct_frag6_skb_cb
63 {
64         struct inet6_skb_parm   h;
65         int                     offset;
66         struct sk_buff          *orig;
67 };
68
69 #define NFCT_FRAG6_CB(skb)      ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
70
71 struct nf_ct_frag6_queue
72 {
73         struct hlist_node       list;
74         struct list_head        lru_list;       /* lru list member      */
75
76         __u32                   id;             /* fragment id          */
77         struct in6_addr         saddr;
78         struct in6_addr         daddr;
79
80         spinlock_t              lock;
81         atomic_t                refcnt;
82         struct timer_list       timer;          /* expire timer         */
83         struct sk_buff          *fragments;
84         int                     len;
85         int                     meat;
86         struct timeval          stamp;
87         unsigned int            csum;
88         __u8                    last_in;        /* has first/last segment arrived? */
89 #define COMPLETE                4
90 #define FIRST_IN                2
91 #define LAST_IN                 1
92         __u16                   nhoffset;
93 };
94
95 /* Hash table. */
96
97 #define FRAG6Q_HASHSZ   64
98
99 static struct hlist_head nf_ct_frag6_hash[FRAG6Q_HASHSZ];
100 static DEFINE_RWLOCK(nf_ct_frag6_lock);
101 static u32 nf_ct_frag6_hash_rnd;
102 static LIST_HEAD(nf_ct_frag6_lru_list);
103 int nf_ct_frag6_nqueues = 0;
104
105 static __inline__ void __fq_unlink(struct nf_ct_frag6_queue *fq)
106 {
107         hlist_del(&fq->list);
108         list_del(&fq->lru_list);
109         nf_ct_frag6_nqueues--;
110 }
111
112 static __inline__ void fq_unlink(struct nf_ct_frag6_queue *fq)
113 {
114         write_lock(&nf_ct_frag6_lock);
115         __fq_unlink(fq);
116         write_unlock(&nf_ct_frag6_lock);
117 }
118
119 static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr,
120                                struct in6_addr *daddr)
121 {
122         u32 a, b, c;
123
124         a = saddr->s6_addr32[0];
125         b = saddr->s6_addr32[1];
126         c = saddr->s6_addr32[2];
127
128         a += JHASH_GOLDEN_RATIO;
129         b += JHASH_GOLDEN_RATIO;
130         c += nf_ct_frag6_hash_rnd;
131         __jhash_mix(a, b, c);
132
133         a += saddr->s6_addr32[3];
134         b += daddr->s6_addr32[0];
135         c += daddr->s6_addr32[1];
136         __jhash_mix(a, b, c);
137
138         a += daddr->s6_addr32[2];
139         b += daddr->s6_addr32[3];
140         c += id;
141         __jhash_mix(a, b, c);
142
143         return c & (FRAG6Q_HASHSZ - 1);
144 }
145
146 static struct timer_list nf_ct_frag6_secret_timer;
147 int nf_ct_frag6_secret_interval = 10 * 60 * HZ;
148
149 static void nf_ct_frag6_secret_rebuild(unsigned long dummy)
150 {
151         unsigned long now = jiffies;
152         int i;
153
154         write_lock(&nf_ct_frag6_lock);
155         get_random_bytes(&nf_ct_frag6_hash_rnd, sizeof(u32));
156         for (i = 0; i < FRAG6Q_HASHSZ; i++) {
157                 struct nf_ct_frag6_queue *q;
158                 struct hlist_node *p, *n;
159
160                 hlist_for_each_entry_safe(q, p, n, &nf_ct_frag6_hash[i], list) {
161                         unsigned int hval = ip6qhashfn(q->id,
162                                                        &q->saddr,
163                                                        &q->daddr);
164                         if (hval != i) {
165                                 hlist_del(&q->list);
166                                 /* Relink to new hash chain. */
167                                 hlist_add_head(&q->list,
168                                                &nf_ct_frag6_hash[hval]);
169                         }
170                 }
171         }
172         write_unlock(&nf_ct_frag6_lock);
173
174         mod_timer(&nf_ct_frag6_secret_timer, now + nf_ct_frag6_secret_interval);
175 }
176
177 atomic_t nf_ct_frag6_mem = ATOMIC_INIT(0);
178
179 /* Memory Tracking Functions. */
180 static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
181 {
182         if (work)
183                 *work -= skb->truesize;
184         atomic_sub(skb->truesize, &nf_ct_frag6_mem);
185         if (NFCT_FRAG6_CB(skb)->orig)
186                 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
187
188         kfree_skb(skb);
189 }
190
191 static inline void frag_free_queue(struct nf_ct_frag6_queue *fq,
192                                    unsigned int *work)
193 {
194         if (work)
195                 *work -= sizeof(struct nf_ct_frag6_queue);
196         atomic_sub(sizeof(struct nf_ct_frag6_queue), &nf_ct_frag6_mem);
197         kfree(fq);
198 }
199
200 static inline struct nf_ct_frag6_queue *frag_alloc_queue(void)
201 {
202         struct nf_ct_frag6_queue *fq = kmalloc(sizeof(struct nf_ct_frag6_queue), GFP_ATOMIC);
203
204         if (!fq)
205                 return NULL;
206         atomic_add(sizeof(struct nf_ct_frag6_queue), &nf_ct_frag6_mem);
207         return fq;
208 }
209
210 /* Destruction primitives. */
211
212 /* Complete destruction of fq. */
213 static void nf_ct_frag6_destroy(struct nf_ct_frag6_queue *fq,
214                                 unsigned int *work)
215 {
216         struct sk_buff *fp;
217
218         BUG_TRAP(fq->last_in&COMPLETE);
219         BUG_TRAP(del_timer(&fq->timer) == 0);
220
221         /* Release all fragment data. */
222         fp = fq->fragments;
223         while (fp) {
224                 struct sk_buff *xp = fp->next;
225
226                 frag_kfree_skb(fp, work);
227                 fp = xp;
228         }
229
230         frag_free_queue(fq, work);
231 }
232
233 static __inline__ void fq_put(struct nf_ct_frag6_queue *fq, unsigned int *work)
234 {
235         if (atomic_dec_and_test(&fq->refcnt))
236                 nf_ct_frag6_destroy(fq, work);
237 }
238
239 /* Kill fq entry. It is not destroyed immediately,
240  * because caller (and someone more) holds reference count.
241  */
242 static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
243 {
244         if (del_timer(&fq->timer))
245                 atomic_dec(&fq->refcnt);
246
247         if (!(fq->last_in & COMPLETE)) {
248                 fq_unlink(fq);
249                 atomic_dec(&fq->refcnt);
250                 fq->last_in |= COMPLETE;
251         }
252 }
253
254 static void nf_ct_frag6_evictor(void)
255 {
256         struct nf_ct_frag6_queue *fq;
257         struct list_head *tmp;
258         unsigned int work;
259
260         work = atomic_read(&nf_ct_frag6_mem);
261         if (work <= nf_ct_frag6_low_thresh)
262                 return;
263
264         work -= nf_ct_frag6_low_thresh;
265         while (work > 0) {
266                 read_lock(&nf_ct_frag6_lock);
267                 if (list_empty(&nf_ct_frag6_lru_list)) {
268                         read_unlock(&nf_ct_frag6_lock);
269                         return;
270                 }
271                 tmp = nf_ct_frag6_lru_list.next;
272                 BUG_ON(tmp == NULL);
273                 fq = list_entry(tmp, struct nf_ct_frag6_queue, lru_list);
274                 atomic_inc(&fq->refcnt);
275                 read_unlock(&nf_ct_frag6_lock);
276
277                 spin_lock(&fq->lock);
278                 if (!(fq->last_in&COMPLETE))
279                         fq_kill(fq);
280                 spin_unlock(&fq->lock);
281
282                 fq_put(fq, &work);
283         }
284 }
285
286 static void nf_ct_frag6_expire(unsigned long data)
287 {
288         struct nf_ct_frag6_queue *fq = (struct nf_ct_frag6_queue *) data;
289
290         spin_lock(&fq->lock);
291
292         if (fq->last_in & COMPLETE)
293                 goto out;
294
295         fq_kill(fq);
296
297 out:
298         spin_unlock(&fq->lock);
299         fq_put(fq, NULL);
300 }
301
302 /* Creation primitives. */
303
304 static struct nf_ct_frag6_queue *nf_ct_frag6_intern(unsigned int hash,
305                                           struct nf_ct_frag6_queue *fq_in)
306 {
307         struct nf_ct_frag6_queue *fq;
308 #ifdef CONFIG_SMP
309         struct hlist_node *n;
310 #endif
311
312         write_lock(&nf_ct_frag6_lock);
313 #ifdef CONFIG_SMP
314         hlist_for_each_entry(fq, n, &nf_ct_frag6_hash[hash], list) {
315                 if (fq->id == fq_in->id && 
316                     ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
317                     ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
318                         atomic_inc(&fq->refcnt);
319                         write_unlock(&nf_ct_frag6_lock);
320                         fq_in->last_in |= COMPLETE;
321                         fq_put(fq_in, NULL);
322                         return fq;
323                 }
324         }
325 #endif
326         fq = fq_in;
327
328         if (!mod_timer(&fq->timer, jiffies + nf_ct_frag6_timeout))
329                 atomic_inc(&fq->refcnt);
330
331         atomic_inc(&fq->refcnt);
332         hlist_add_head(&fq->list, &nf_ct_frag6_hash[hash]);
333         INIT_LIST_HEAD(&fq->lru_list);
334         list_add_tail(&fq->lru_list, &nf_ct_frag6_lru_list);
335         nf_ct_frag6_nqueues++;
336         write_unlock(&nf_ct_frag6_lock);
337         return fq;
338 }
339
340
341 static struct nf_ct_frag6_queue *
342 nf_ct_frag6_create(unsigned int hash, u32 id, struct in6_addr *src,                                struct in6_addr *dst)
343 {
344         struct nf_ct_frag6_queue *fq;
345
346         if ((fq = frag_alloc_queue()) == NULL) {
347                 DEBUGP("Can't alloc new queue\n");
348                 goto oom;
349         }
350
351         memset(fq, 0, sizeof(struct nf_ct_frag6_queue));
352
353         fq->id = id;
354         ipv6_addr_copy(&fq->saddr, src);
355         ipv6_addr_copy(&fq->daddr, dst);
356
357         init_timer(&fq->timer);
358         fq->timer.function = nf_ct_frag6_expire;
359         fq->timer.data = (long) fq;
360         spin_lock_init(&fq->lock);
361         atomic_set(&fq->refcnt, 1);
362
363         return nf_ct_frag6_intern(hash, fq);
364
365 oom:
366         return NULL;
367 }
368
369 static __inline__ struct nf_ct_frag6_queue *
370 fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst)
371 {
372         struct nf_ct_frag6_queue *fq;
373         struct hlist_node *n;
374         unsigned int hash = ip6qhashfn(id, src, dst);
375
376         read_lock(&nf_ct_frag6_lock);
377         hlist_for_each_entry(fq, n, &nf_ct_frag6_hash[hash], list) {
378                 if (fq->id == id && 
379                     ipv6_addr_equal(src, &fq->saddr) &&
380                     ipv6_addr_equal(dst, &fq->daddr)) {
381                         atomic_inc(&fq->refcnt);
382                         read_unlock(&nf_ct_frag6_lock);
383                         return fq;
384                 }
385         }
386         read_unlock(&nf_ct_frag6_lock);
387
388         return nf_ct_frag6_create(hash, id, src, dst);
389 }
390
391
392 static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb, 
393                              struct frag_hdr *fhdr, int nhoff)
394 {
395         struct sk_buff *prev, *next;
396         int offset, end;
397
398         if (fq->last_in & COMPLETE) {
399                 DEBUGP("Allready completed\n");
400                 goto err;
401         }
402
403         offset = ntohs(fhdr->frag_off) & ~0x7;
404         end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
405                         ((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
406
407         if ((unsigned int)end > IPV6_MAXPLEN) {
408                 DEBUGP("offset is too large.\n");
409                 return -1;
410         }
411
412         if (skb->ip_summed == CHECKSUM_HW)
413                 skb->csum = csum_sub(skb->csum,
414                                      csum_partial(skb->nh.raw,
415                                                   (u8*)(fhdr + 1) - skb->nh.raw,
416                                                   0));
417
418         /* Is this the final fragment? */
419         if (!(fhdr->frag_off & htons(IP6_MF))) {
420                 /* If we already have some bits beyond end
421                  * or have different end, the segment is corrupted.
422                  */
423                 if (end < fq->len ||
424                     ((fq->last_in & LAST_IN) && end != fq->len)) {
425                         DEBUGP("already received last fragment\n");
426                         goto err;
427                 }
428                 fq->last_in |= LAST_IN;
429                 fq->len = end;
430         } else {
431                 /* Check if the fragment is rounded to 8 bytes.
432                  * Required by the RFC.
433                  */
434                 if (end & 0x7) {
435                         /* RFC2460 says always send parameter problem in
436                          * this case. -DaveM
437                          */
438                         DEBUGP("the end of this fragment is not rounded to 8 bytes.\n");
439                         return -1;
440                 }
441                 if (end > fq->len) {
442                         /* Some bits beyond end -> corruption. */
443                         if (fq->last_in & LAST_IN) {
444                                 DEBUGP("last packet already reached.\n");
445                                 goto err;
446                         }
447                         fq->len = end;
448                 }
449         }
450
451         if (end == offset)
452                 goto err;
453
454         /* Point into the IP datagram 'data' part. */
455         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
456                 DEBUGP("queue: message is too short.\n");
457                 goto err;
458         }
459         if (end-offset < skb->len) {
460                 if (pskb_trim(skb, end - offset)) {
461                         DEBUGP("Can't trim\n");
462                         goto err;
463                 }
464                 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
465                         skb->ip_summed = CHECKSUM_NONE;
466         }
467
468         /* Find out which fragments are in front and at the back of us
469          * in the chain of fragments so far.  We must know where to put
470          * this fragment, right?
471          */
472         prev = NULL;
473         for (next = fq->fragments; next != NULL; next = next->next) {
474                 if (NFCT_FRAG6_CB(next)->offset >= offset)
475                         break;  /* bingo! */
476                 prev = next;
477         }
478
479         /* We found where to put this one.  Check for overlap with
480          * preceding fragment, and, if needed, align things so that
481          * any overlaps are eliminated.
482          */
483         if (prev) {
484                 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
485
486                 if (i > 0) {
487                         offset += i;
488                         if (end <= offset) {
489                                 DEBUGP("overlap\n");
490                                 goto err;
491                         }
492                         if (!pskb_pull(skb, i)) {
493                                 DEBUGP("Can't pull\n");
494                                 goto err;
495                         }
496                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
497                                 skb->ip_summed = CHECKSUM_NONE;
498                 }
499         }
500
501         /* Look for overlap with succeeding segments.
502          * If we can merge fragments, do it.
503          */
504         while (next && NFCT_FRAG6_CB(next)->offset < end) {
505                 /* overlap is 'i' bytes */
506                 int i = end - NFCT_FRAG6_CB(next)->offset;
507
508                 if (i < next->len) {
509                         /* Eat head of the next overlapped fragment
510                          * and leave the loop. The next ones cannot overlap.
511                          */
512                         DEBUGP("Eat head of the overlapped parts.: %d", i);
513                         if (!pskb_pull(next, i))
514                                 goto err;
515
516                         /* next fragment */
517                         NFCT_FRAG6_CB(next)->offset += i;
518                         fq->meat -= i;
519                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
520                                 next->ip_summed = CHECKSUM_NONE;
521                         break;
522                 } else {
523                         struct sk_buff *free_it = next;
524
525                         /* Old fragmnet is completely overridden with
526                          * new one drop it.
527                          */
528                         next = next->next;
529
530                         if (prev)
531                                 prev->next = next;
532                         else
533                                 fq->fragments = next;
534
535                         fq->meat -= free_it->len;
536                         frag_kfree_skb(free_it, NULL);
537                 }
538         }
539
540         NFCT_FRAG6_CB(skb)->offset = offset;
541
542         /* Insert this fragment in the chain of fragments. */
543         skb->next = next;
544         if (prev)
545                 prev->next = skb;
546         else
547                 fq->fragments = skb;
548
549         skb->dev = NULL;
550         skb_get_timestamp(skb, &fq->stamp);
551         fq->meat += skb->len;
552         atomic_add(skb->truesize, &nf_ct_frag6_mem);
553
554         /* The first fragment.
555          * nhoffset is obtained from the first fragment, of course.
556          */
557         if (offset == 0) {
558                 fq->nhoffset = nhoff;
559                 fq->last_in |= FIRST_IN;
560         }
561         write_lock(&nf_ct_frag6_lock);
562         list_move_tail(&fq->lru_list, &nf_ct_frag6_lru_list);
563         write_unlock(&nf_ct_frag6_lock);
564         return 0;
565
566 err:
567         return -1;
568 }
569
570 /*
571  *      Check if this packet is complete.
572  *      Returns NULL on failure by any reason, and pointer
573  *      to current nexthdr field in reassembled frame.
574  *
575  *      It is called with locked fq, and caller must check that
576  *      queue is eligible for reassembly i.e. it is not COMPLETE,
577  *      the last and the first frames arrived and all the bits are here.
578  */
579 static struct sk_buff *
580 nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
581 {
582         struct sk_buff *fp, *op, *head = fq->fragments;
583         int    payload_len;
584
585         fq_kill(fq);
586
587         BUG_TRAP(head != NULL);
588         BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0);
589
590         /* Unfragmented part is taken from the first segment. */
591         payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
592         if (payload_len > IPV6_MAXPLEN) {
593                 DEBUGP("payload len is too large.\n");
594                 goto out_oversize;
595         }
596
597         /* Head of list must not be cloned. */
598         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
599                 DEBUGP("skb is cloned but can't expand head");
600                 goto out_oom;
601         }
602
603         /* If the first fragment is fragmented itself, we split
604          * it to two chunks: the first with data and paged part
605          * and the second, holding only fragments. */
606         if (skb_shinfo(head)->frag_list) {
607                 struct sk_buff *clone;
608                 int i, plen = 0;
609
610                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
611                         DEBUGP("Can't alloc skb\n");
612                         goto out_oom;
613                 }
614                 clone->next = head->next;
615                 head->next = clone;
616                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
617                 skb_shinfo(head)->frag_list = NULL;
618                 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
619                         plen += skb_shinfo(head)->frags[i].size;
620                 clone->len = clone->data_len = head->data_len - plen;
621                 head->data_len -= clone->len;
622                 head->len -= clone->len;
623                 clone->csum = 0;
624                 clone->ip_summed = head->ip_summed;
625
626                 NFCT_FRAG6_CB(clone)->orig = NULL;
627                 atomic_add(clone->truesize, &nf_ct_frag6_mem);
628         }
629
630         /* We have to remove fragment header from datagram and to relocate
631          * header in order to calculate ICV correctly. */
632         head->nh.raw[fq->nhoffset] = head->h.raw[0];
633         memmove(head->head + sizeof(struct frag_hdr), head->head, 
634                 (head->data - head->head) - sizeof(struct frag_hdr));
635         head->mac.raw += sizeof(struct frag_hdr);
636         head->nh.raw += sizeof(struct frag_hdr);
637
638         skb_shinfo(head)->frag_list = head->next;
639         head->h.raw = head->data;
640         skb_push(head, head->data - head->nh.raw);
641         atomic_sub(head->truesize, &nf_ct_frag6_mem);
642
643         for (fp=head->next; fp; fp = fp->next) {
644                 head->data_len += fp->len;
645                 head->len += fp->len;
646                 if (head->ip_summed != fp->ip_summed)
647                         head->ip_summed = CHECKSUM_NONE;
648                 else if (head->ip_summed == CHECKSUM_HW)
649                         head->csum = csum_add(head->csum, fp->csum);
650                 head->truesize += fp->truesize;
651                 atomic_sub(fp->truesize, &nf_ct_frag6_mem);
652         }
653
654         head->next = NULL;
655         head->dev = dev;
656         skb_set_timestamp(head, &fq->stamp);
657         head->nh.ipv6h->payload_len = htons(payload_len);
658
659         /* Yes, and fold redundant checksum back. 8) */
660         if (head->ip_summed == CHECKSUM_HW)
661                 head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
662
663         fq->fragments = NULL;
664
665         /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
666         fp = skb_shinfo(head)->frag_list;
667         if (NFCT_FRAG6_CB(fp)->orig == NULL)
668                 /* at above code, head skb is divided into two skbs. */
669                 fp = fp->next;
670
671         op = NFCT_FRAG6_CB(head)->orig;
672         for (; fp; fp = fp->next) {
673                 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
674
675                 op->next = orig;
676                 op = orig;
677                 NFCT_FRAG6_CB(fp)->orig = NULL;
678         }
679
680         return head;
681
682 out_oversize:
683         if (net_ratelimit())
684                 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
685         goto out_fail;
686 out_oom:
687         if (net_ratelimit())
688                 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
689 out_fail:
690         return NULL;
691 }
692
693 /*
694  * find the header just before Fragment Header.
695  *
696  * if success return 0 and set ...
697  * (*prevhdrp): the value of "Next Header Field" in the header
698  *              just before Fragment Header.
699  * (*prevhoff): the offset of "Next Header Field" in the header
700  *              just before Fragment Header.
701  * (*fhoff)   : the offset of Fragment Header.
702  *
703  * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
704  *
705  */
706 static int
707 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
708 {
709         u8 nexthdr = skb->nh.ipv6h->nexthdr;
710         u8 prev_nhoff = (u8 *)&skb->nh.ipv6h->nexthdr - skb->data;
711         int start = (u8 *)(skb->nh.ipv6h+1) - skb->data;
712         int len = skb->len - start;
713         u8 prevhdr = NEXTHDR_IPV6;
714
715         while (nexthdr != NEXTHDR_FRAGMENT) {
716                 struct ipv6_opt_hdr hdr;
717                 int hdrlen;
718
719                 if (!ipv6_ext_hdr(nexthdr)) {
720                         return -1;
721                 }
722                 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
723                         DEBUGP("too short\n");
724                         return -1;
725                 }
726                 if (nexthdr == NEXTHDR_NONE) {
727                         DEBUGP("next header is none\n");
728                         return -1;
729                 }
730                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
731                         BUG();
732                 if (nexthdr == NEXTHDR_AUTH)
733                         hdrlen = (hdr.hdrlen+2)<<2;
734                 else
735                         hdrlen = ipv6_optlen(&hdr);
736
737                 prevhdr = nexthdr;
738                 prev_nhoff = start;
739
740                 nexthdr = hdr.nexthdr;
741                 len -= hdrlen;
742                 start += hdrlen;
743         }
744
745         if (len < 0)
746                 return -1;
747
748         *prevhdrp = prevhdr;
749         *prevhoff = prev_nhoff;
750         *fhoff = start;
751
752         return 0;
753 }
754
755 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
756 {
757         struct sk_buff *clone; 
758         struct net_device *dev = skb->dev;
759         struct frag_hdr *fhdr;
760         struct nf_ct_frag6_queue *fq;
761         struct ipv6hdr *hdr;
762         int fhoff, nhoff;
763         u8 prevhdr;
764         struct sk_buff *ret_skb = NULL;
765
766         /* Jumbo payload inhibits frag. header */
767         if (skb->nh.ipv6h->payload_len == 0) {
768                 DEBUGP("payload len = 0\n");
769                 return skb;
770         }
771
772         if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
773                 return skb;
774
775         clone = skb_clone(skb, GFP_ATOMIC);
776         if (clone == NULL) {
777                 DEBUGP("Can't clone skb\n");
778                 return skb;
779         }
780
781         NFCT_FRAG6_CB(clone)->orig = skb;
782
783         if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
784                 DEBUGP("message is too short.\n");
785                 goto ret_orig;
786         }
787
788         clone->h.raw = clone->data + fhoff;
789         hdr = clone->nh.ipv6h;
790         fhdr = (struct frag_hdr *)clone->h.raw;
791
792         if (!(fhdr->frag_off & htons(0xFFF9))) {
793                 DEBUGP("Invalid fragment offset\n");
794                 /* It is not a fragmented frame */
795                 goto ret_orig;
796         }
797
798         if (atomic_read(&nf_ct_frag6_mem) > nf_ct_frag6_high_thresh)
799                 nf_ct_frag6_evictor();
800
801         fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
802         if (fq == NULL) {
803                 DEBUGP("Can't find and can't create new queue\n");
804                 goto ret_orig;
805         }
806
807         spin_lock(&fq->lock);
808
809         if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
810                 spin_unlock(&fq->lock);
811                 DEBUGP("Can't insert skb to queue\n");
812                 fq_put(fq, NULL);
813                 goto ret_orig;
814         }
815
816         if (fq->last_in == (FIRST_IN|LAST_IN) && fq->meat == fq->len) {
817                 ret_skb = nf_ct_frag6_reasm(fq, dev);
818                 if (ret_skb == NULL)
819                         DEBUGP("Can't reassemble fragmented packets\n");
820         }
821         spin_unlock(&fq->lock);
822
823         fq_put(fq, NULL);
824         return ret_skb;
825
826 ret_orig:
827         kfree_skb(clone);
828         return skb;
829 }
830
831 void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
832                         struct net_device *in, struct net_device *out,
833                         int (*okfn)(struct sk_buff *))
834 {
835         struct sk_buff *s, *s2;
836
837         for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
838                 nf_conntrack_put_reasm(s->nfct_reasm);
839                 nf_conntrack_get_reasm(skb);
840                 s->nfct_reasm = skb;
841
842                 s2 = s->next;
843                 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
844                                NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
845                 s = s2;
846         }
847         nf_conntrack_put_reasm(skb);
848 }
849
850 int nf_ct_frag6_kfree_frags(struct sk_buff *skb)
851 {
852         struct sk_buff *s, *s2;
853
854         for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) {
855
856                 s2 = s->next;
857                 kfree_skb(s);
858         }
859
860         kfree_skb(skb);
861
862         return 0;
863 }
864
865 int nf_ct_frag6_init(void)
866 {
867         nf_ct_frag6_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
868                                    (jiffies ^ (jiffies >> 6)));
869
870         init_timer(&nf_ct_frag6_secret_timer);
871         nf_ct_frag6_secret_timer.function = nf_ct_frag6_secret_rebuild;
872         nf_ct_frag6_secret_timer.expires = jiffies
873                                            + nf_ct_frag6_secret_interval;
874         add_timer(&nf_ct_frag6_secret_timer);
875
876         return 0;
877 }
878
879 void nf_ct_frag6_cleanup(void)
880 {
881         del_timer(&nf_ct_frag6_secret_timer);
882         nf_ct_frag6_low_thresh = 0;
883         nf_ct_frag6_evictor();
884 }