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