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