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