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