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