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