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