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