[NETNS][FRAGS]: Isolate the secret interval from namespaces.
[linux-2.6/btrfs-unstable.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
blob6eed991a4a3f5c6056da7480a55904aa807c215b
1 /*
2 * IPv6 fragment reassembly for connection tracking
4 * Copyright (C)2004 USAGI/WIDE Project
6 * Author:
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * Based on: net/ipv6/reassembly.c
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.
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>
32 #include <net/sock.h>
33 #include <net/snmp.h>
34 #include <net/inet_frag.h>
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>
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
52 struct nf_ct_frag6_skb_cb
54 struct inet6_skb_parm h;
55 int offset;
56 struct sk_buff *orig;
59 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
61 struct nf_ct_frag6_queue
63 struct inet_frag_queue q;
65 __be32 id; /* fragment id */
66 struct in6_addr saddr;
67 struct in6_addr daddr;
69 unsigned int csum;
70 __u16 nhoffset;
73 static struct inet_frags nf_frags;
74 static struct netns_frags nf_init_frags;
76 #ifdef CONFIG_SYSCTL
77 struct ctl_table nf_ct_ipv6_sysctl_table[] = {
79 .procname = "nf_conntrack_frag6_timeout",
80 .data = &nf_init_frags.timeout,
81 .maxlen = sizeof(unsigned int),
82 .mode = 0644,
83 .proc_handler = &proc_dointvec_jiffies,
86 .ctl_name = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
87 .procname = "nf_conntrack_frag6_low_thresh",
88 .data = &nf_init_frags.low_thresh,
89 .maxlen = sizeof(unsigned int),
90 .mode = 0644,
91 .proc_handler = &proc_dointvec,
94 .ctl_name = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
95 .procname = "nf_conntrack_frag6_high_thresh",
96 .data = &nf_init_frags.high_thresh,
97 .maxlen = sizeof(unsigned int),
98 .mode = 0644,
99 .proc_handler = &proc_dointvec,
101 { .ctl_name = 0 }
103 #endif
105 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
106 struct in6_addr *daddr)
108 u32 a, b, c;
110 a = (__force u32)saddr->s6_addr32[0];
111 b = (__force u32)saddr->s6_addr32[1];
112 c = (__force u32)saddr->s6_addr32[2];
114 a += JHASH_GOLDEN_RATIO;
115 b += JHASH_GOLDEN_RATIO;
116 c += nf_frags.rnd;
117 __jhash_mix(a, b, c);
119 a += (__force u32)saddr->s6_addr32[3];
120 b += (__force u32)daddr->s6_addr32[0];
121 c += (__force u32)daddr->s6_addr32[1];
122 __jhash_mix(a, b, c);
124 a += (__force u32)daddr->s6_addr32[2];
125 b += (__force u32)daddr->s6_addr32[3];
126 c += (__force u32)id;
127 __jhash_mix(a, b, c);
129 return c & (INETFRAGS_HASHSZ - 1);
132 static unsigned int nf_hashfn(struct inet_frag_queue *q)
134 struct nf_ct_frag6_queue *nq;
136 nq = container_of(q, struct nf_ct_frag6_queue, q);
137 return ip6qhashfn(nq->id, &nq->saddr, &nq->daddr);
140 static void nf_skb_free(struct sk_buff *skb)
142 if (NFCT_FRAG6_CB(skb)->orig)
143 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
146 /* Memory Tracking Functions. */
147 static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
149 if (work)
150 *work -= skb->truesize;
151 atomic_sub(skb->truesize, &nf_init_frags.mem);
152 nf_skb_free(skb);
153 kfree_skb(skb);
156 /* Destruction primitives. */
158 static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
160 inet_frag_put(&fq->q, &nf_frags);
163 /* Kill fq entry. It is not destroyed immediately,
164 * because caller (and someone more) holds reference count.
166 static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
168 inet_frag_kill(&fq->q, &nf_frags);
171 static void nf_ct_frag6_evictor(void)
173 inet_frag_evictor(&nf_init_frags, &nf_frags);
176 static void nf_ct_frag6_expire(unsigned long data)
178 struct nf_ct_frag6_queue *fq;
180 fq = container_of((struct inet_frag_queue *)data,
181 struct nf_ct_frag6_queue, q);
183 spin_lock(&fq->q.lock);
185 if (fq->q.last_in & COMPLETE)
186 goto out;
188 fq_kill(fq);
190 out:
191 spin_unlock(&fq->q.lock);
192 fq_put(fq);
195 /* Creation primitives. */
197 static __inline__ struct nf_ct_frag6_queue *
198 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
200 struct inet_frag_queue *q;
201 struct ip6_create_arg arg;
202 unsigned int hash;
204 arg.id = id;
205 arg.src = src;
206 arg.dst = dst;
207 hash = ip6qhashfn(id, src, dst);
209 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
210 if (q == NULL)
211 goto oom;
213 return container_of(q, struct nf_ct_frag6_queue, q);
215 oom:
216 pr_debug("Can't alloc new queue\n");
217 return NULL;
221 static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
222 struct frag_hdr *fhdr, int nhoff)
224 struct sk_buff *prev, *next;
225 int offset, end;
227 if (fq->q.last_in & COMPLETE) {
228 pr_debug("Allready completed\n");
229 goto err;
232 offset = ntohs(fhdr->frag_off) & ~0x7;
233 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
234 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
236 if ((unsigned int)end > IPV6_MAXPLEN) {
237 pr_debug("offset is too large.\n");
238 return -1;
241 if (skb->ip_summed == CHECKSUM_COMPLETE) {
242 const unsigned char *nh = skb_network_header(skb);
243 skb->csum = csum_sub(skb->csum,
244 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
245 0));
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.
253 if (end < fq->q.len ||
254 ((fq->q.last_in & LAST_IN) && end != fq->q.len)) {
255 pr_debug("already received last fragment\n");
256 goto err;
258 fq->q.last_in |= LAST_IN;
259 fq->q.len = end;
260 } else {
261 /* Check if the fragment is rounded to 8 bytes.
262 * Required by the RFC.
264 if (end & 0x7) {
265 /* RFC2460 says always send parameter problem in
266 * this case. -DaveM
268 pr_debug("end of fragment not rounded to 8 bytes.\n");
269 return -1;
271 if (end > fq->q.len) {
272 /* Some bits beyond end -> corruption. */
273 if (fq->q.last_in & LAST_IN) {
274 pr_debug("last packet already reached.\n");
275 goto err;
277 fq->q.len = end;
281 if (end == offset)
282 goto err;
284 /* Point into the IP datagram 'data' part. */
285 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
286 pr_debug("queue: message is too short.\n");
287 goto err;
289 if (pskb_trim_rcsum(skb, end - offset)) {
290 pr_debug("Can't trim\n");
291 goto err;
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?
298 prev = NULL;
299 for (next = fq->q.fragments; next != NULL; next = next->next) {
300 if (NFCT_FRAG6_CB(next)->offset >= offset)
301 break; /* bingo! */
302 prev = next;
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.
309 if (prev) {
310 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
312 if (i > 0) {
313 offset += i;
314 if (end <= offset) {
315 pr_debug("overlap\n");
316 goto err;
318 if (!pskb_pull(skb, i)) {
319 pr_debug("Can't pull\n");
320 goto err;
322 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
323 skb->ip_summed = CHECKSUM_NONE;
327 /* Look for overlap with succeeding segments.
328 * If we can merge fragments, do it.
330 while (next && NFCT_FRAG6_CB(next)->offset < end) {
331 /* overlap is 'i' bytes */
332 int i = end - NFCT_FRAG6_CB(next)->offset;
334 if (i < next->len) {
335 /* Eat head of the next overlapped fragment
336 * and leave the loop. The next ones cannot overlap.
338 pr_debug("Eat head of the overlapped parts.: %d", i);
339 if (!pskb_pull(next, i))
340 goto err;
342 /* next fragment */
343 NFCT_FRAG6_CB(next)->offset += i;
344 fq->q.meat -= i;
345 if (next->ip_summed != CHECKSUM_UNNECESSARY)
346 next->ip_summed = CHECKSUM_NONE;
347 break;
348 } else {
349 struct sk_buff *free_it = next;
351 /* Old fragmnet is completely overridden with
352 * new one drop it.
354 next = next->next;
356 if (prev)
357 prev->next = next;
358 else
359 fq->q.fragments = next;
361 fq->q.meat -= free_it->len;
362 frag_kfree_skb(free_it, NULL);
366 NFCT_FRAG6_CB(skb)->offset = offset;
368 /* Insert this fragment in the chain of fragments. */
369 skb->next = next;
370 if (prev)
371 prev->next = skb;
372 else
373 fq->q.fragments = skb;
375 skb->dev = NULL;
376 fq->q.stamp = skb->tstamp;
377 fq->q.meat += skb->len;
378 atomic_add(skb->truesize, &nf_init_frags.mem);
380 /* The first fragment.
381 * nhoffset is obtained from the first fragment, of course.
383 if (offset == 0) {
384 fq->nhoffset = nhoff;
385 fq->q.last_in |= FIRST_IN;
387 write_lock(&nf_frags.lock);
388 list_move_tail(&fq->q.lru_list, &nf_frags.lru_list);
389 write_unlock(&nf_frags.lock);
390 return 0;
392 err:
393 return -1;
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.
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.
405 static struct sk_buff *
406 nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
408 struct sk_buff *fp, *op, *head = fq->q.fragments;
409 int payload_len;
411 fq_kill(fq);
413 BUG_TRAP(head != NULL);
414 BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0);
416 /* Unfragmented part is taken from the first segment. */
417 payload_len = ((head->data - skb_network_header(head)) -
418 sizeof(struct ipv6hdr) + fq->q.len -
419 sizeof(struct frag_hdr));
420 if (payload_len > IPV6_MAXPLEN) {
421 pr_debug("payload len is too large.\n");
422 goto out_oversize;
425 /* Head of list must not be cloned. */
426 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
427 pr_debug("skb is cloned but can't expand head");
428 goto out_oom;
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;
438 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
439 pr_debug("Can't alloc skb\n");
440 goto out_oom;
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;
454 NFCT_FRAG6_CB(clone)->orig = NULL;
455 atomic_add(clone->truesize, &nf_init_frags.mem);
458 /* We have to remove fragment header from datagram and to relocate
459 * header in order to calculate ICV correctly. */
460 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
461 memmove(head->head + sizeof(struct frag_hdr), head->head,
462 (head->data - head->head) - sizeof(struct frag_hdr));
463 head->mac_header += sizeof(struct frag_hdr);
464 head->network_header += sizeof(struct frag_hdr);
466 skb_shinfo(head)->frag_list = head->next;
467 skb_reset_transport_header(head);
468 skb_push(head, head->data - skb_network_header(head));
469 atomic_sub(head->truesize, &nf_init_frags.mem);
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;
476 else if (head->ip_summed == CHECKSUM_COMPLETE)
477 head->csum = csum_add(head->csum, fp->csum);
478 head->truesize += fp->truesize;
479 atomic_sub(fp->truesize, &nf_init_frags.mem);
482 head->next = NULL;
483 head->dev = dev;
484 head->tstamp = fq->q.stamp;
485 ipv6_hdr(head)->payload_len = htons(payload_len);
487 /* Yes, and fold redundant checksum back. 8) */
488 if (head->ip_summed == CHECKSUM_COMPLETE)
489 head->csum = csum_partial(skb_network_header(head),
490 skb_network_header_len(head),
491 head->csum);
493 fq->q.fragments = NULL;
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;
501 op = NFCT_FRAG6_CB(head)->orig;
502 for (; fp; fp = fp->next) {
503 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
505 op->next = orig;
506 op = orig;
507 NFCT_FRAG6_CB(fp)->orig = NULL;
510 return head;
512 out_oversize:
513 if (net_ratelimit())
514 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
515 goto out_fail;
516 out_oom:
517 if (net_ratelimit())
518 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
519 out_fail:
520 return NULL;
524 * find the header just before Fragment Header.
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.
533 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
536 static int
537 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
539 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
540 const int netoff = skb_network_offset(skb);
541 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
542 int start = netoff + sizeof(struct ipv6hdr);
543 int len = skb->len - start;
544 u8 prevhdr = NEXTHDR_IPV6;
546 while (nexthdr != NEXTHDR_FRAGMENT) {
547 struct ipv6_opt_hdr hdr;
548 int hdrlen;
550 if (!ipv6_ext_hdr(nexthdr)) {
551 return -1;
553 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
554 pr_debug("too short\n");
555 return -1;
557 if (nexthdr == NEXTHDR_NONE) {
558 pr_debug("next header is none\n");
559 return -1;
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);
568 prevhdr = nexthdr;
569 prev_nhoff = start;
571 nexthdr = hdr.nexthdr;
572 len -= hdrlen;
573 start += hdrlen;
576 if (len < 0)
577 return -1;
579 *prevhdrp = prevhdr;
580 *prevhoff = prev_nhoff;
581 *fhoff = start;
583 return 0;
586 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
588 struct sk_buff *clone;
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;
597 /* Jumbo payload inhibits frag. header */
598 if (ipv6_hdr(skb)->payload_len == 0) {
599 pr_debug("payload len = 0\n");
600 return skb;
603 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
604 return skb;
606 clone = skb_clone(skb, GFP_ATOMIC);
607 if (clone == NULL) {
608 pr_debug("Can't clone skb\n");
609 return skb;
612 NFCT_FRAG6_CB(clone)->orig = skb;
614 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
615 pr_debug("message is too short.\n");
616 goto ret_orig;
619 skb_set_transport_header(clone, fhoff);
620 hdr = ipv6_hdr(clone);
621 fhdr = (struct frag_hdr *)skb_transport_header(clone);
623 if (!(fhdr->frag_off & htons(0xFFF9))) {
624 pr_debug("Invalid fragment offset\n");
625 /* It is not a fragmented frame */
626 goto ret_orig;
629 if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh)
630 nf_ct_frag6_evictor();
632 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
633 if (fq == NULL) {
634 pr_debug("Can't find and can't create new queue\n");
635 goto ret_orig;
638 spin_lock(&fq->q.lock);
640 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
641 spin_unlock(&fq->q.lock);
642 pr_debug("Can't insert skb to queue\n");
643 fq_put(fq);
644 goto ret_orig;
647 if (fq->q.last_in == (FIRST_IN|LAST_IN) && fq->q.meat == fq->q.len) {
648 ret_skb = nf_ct_frag6_reasm(fq, dev);
649 if (ret_skb == NULL)
650 pr_debug("Can't reassemble fragmented packets\n");
652 spin_unlock(&fq->q.lock);
654 fq_put(fq);
655 return ret_skb;
657 ret_orig:
658 kfree_skb(clone);
659 return skb;
662 void 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 *))
666 struct sk_buff *s, *s2;
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;
673 s2 = s->next;
674 s->next = NULL;
676 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
677 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
678 s = s2;
680 nf_conntrack_put_reasm(skb);
683 int nf_ct_frag6_kfree_frags(struct sk_buff *skb)
685 struct sk_buff *s, *s2;
687 for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) {
689 s2 = s->next;
690 kfree_skb(s);
693 kfree_skb(skb);
695 return 0;
698 int nf_ct_frag6_init(void)
700 nf_frags.hashfn = nf_hashfn;
701 nf_frags.constructor = ip6_frag_init;
702 nf_frags.destructor = NULL;
703 nf_frags.skb_free = nf_skb_free;
704 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
705 nf_frags.match = ip6_frag_match;
706 nf_frags.frag_expire = nf_ct_frag6_expire;
707 nf_frags.secret_interval = 10 * 60 * HZ;
708 nf_init_frags.timeout = IPV6_FRAG_TIMEOUT;
709 nf_init_frags.high_thresh = 256 * 1024;
710 nf_init_frags.low_thresh = 192 * 1024;
711 inet_frags_init_net(&nf_init_frags);
712 inet_frags_init(&nf_frags);
714 return 0;
717 void nf_ct_frag6_cleanup(void)
719 inet_frags_fini(&nf_frags);
721 nf_init_frags.low_thresh = 0;
722 nf_ct_frag6_evictor();