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