[NETNS][FRAGS]: Make the mem counter per-namespace.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
blobcb826bea4b1b178859e8df80dc9ed16a2751e3b5
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_ctl nf_frags_ctl __read_mostly = {
74 .high_thresh = 256 * 1024,
75 .low_thresh = 192 * 1024,
76 .timeout = IPV6_FRAG_TIMEOUT,
77 .secret_interval = 10 * 60 * HZ,
80 static struct inet_frags nf_frags;
81 static struct netns_frags nf_init_frags;
83 #ifdef CONFIG_SYSCTL
84 struct ctl_table nf_ct_ipv6_sysctl_table[] = {
86 .procname = "nf_conntrack_frag6_timeout",
87 .data = &nf_frags_ctl.timeout,
88 .maxlen = sizeof(unsigned int),
89 .mode = 0644,
90 .proc_handler = &proc_dointvec_jiffies,
93 .ctl_name = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
94 .procname = "nf_conntrack_frag6_low_thresh",
95 .data = &nf_frags_ctl.low_thresh,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec,
101 .ctl_name = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
102 .procname = "nf_conntrack_frag6_high_thresh",
103 .data = &nf_frags_ctl.high_thresh,
104 .maxlen = sizeof(unsigned int),
105 .mode = 0644,
106 .proc_handler = &proc_dointvec,
108 { .ctl_name = 0 }
110 #endif
112 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
113 struct in6_addr *daddr)
115 u32 a, b, c;
117 a = (__force u32)saddr->s6_addr32[0];
118 b = (__force u32)saddr->s6_addr32[1];
119 c = (__force u32)saddr->s6_addr32[2];
121 a += JHASH_GOLDEN_RATIO;
122 b += JHASH_GOLDEN_RATIO;
123 c += nf_frags.rnd;
124 __jhash_mix(a, b, c);
126 a += (__force u32)saddr->s6_addr32[3];
127 b += (__force u32)daddr->s6_addr32[0];
128 c += (__force u32)daddr->s6_addr32[1];
129 __jhash_mix(a, b, c);
131 a += (__force u32)daddr->s6_addr32[2];
132 b += (__force u32)daddr->s6_addr32[3];
133 c += (__force u32)id;
134 __jhash_mix(a, b, c);
136 return c & (INETFRAGS_HASHSZ - 1);
139 static unsigned int nf_hashfn(struct inet_frag_queue *q)
141 struct nf_ct_frag6_queue *nq;
143 nq = container_of(q, struct nf_ct_frag6_queue, q);
144 return ip6qhashfn(nq->id, &nq->saddr, &nq->daddr);
147 static void nf_skb_free(struct sk_buff *skb)
149 if (NFCT_FRAG6_CB(skb)->orig)
150 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
153 /* Memory Tracking Functions. */
154 static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
156 if (work)
157 *work -= skb->truesize;
158 atomic_sub(skb->truesize, &nf_init_frags.mem);
159 nf_skb_free(skb);
160 kfree_skb(skb);
163 /* Destruction primitives. */
165 static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
167 inet_frag_put(&fq->q, &nf_frags);
170 /* Kill fq entry. It is not destroyed immediately,
171 * because caller (and someone more) holds reference count.
173 static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
175 inet_frag_kill(&fq->q, &nf_frags);
178 static void nf_ct_frag6_evictor(void)
180 inet_frag_evictor(&nf_init_frags, &nf_frags);
183 static void nf_ct_frag6_expire(unsigned long data)
185 struct nf_ct_frag6_queue *fq;
187 fq = container_of((struct inet_frag_queue *)data,
188 struct nf_ct_frag6_queue, q);
190 spin_lock(&fq->q.lock);
192 if (fq->q.last_in & COMPLETE)
193 goto out;
195 fq_kill(fq);
197 out:
198 spin_unlock(&fq->q.lock);
199 fq_put(fq);
202 /* Creation primitives. */
204 static __inline__ struct nf_ct_frag6_queue *
205 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
207 struct inet_frag_queue *q;
208 struct ip6_create_arg arg;
209 unsigned int hash;
211 arg.id = id;
212 arg.src = src;
213 arg.dst = dst;
214 hash = ip6qhashfn(id, src, dst);
216 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
217 if (q == NULL)
218 goto oom;
220 return container_of(q, struct nf_ct_frag6_queue, q);
222 oom:
223 pr_debug("Can't alloc new queue\n");
224 return NULL;
228 static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
229 struct frag_hdr *fhdr, int nhoff)
231 struct sk_buff *prev, *next;
232 int offset, end;
234 if (fq->q.last_in & COMPLETE) {
235 pr_debug("Allready completed\n");
236 goto err;
239 offset = ntohs(fhdr->frag_off) & ~0x7;
240 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
241 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
243 if ((unsigned int)end > IPV6_MAXPLEN) {
244 pr_debug("offset is too large.\n");
245 return -1;
248 if (skb->ip_summed == CHECKSUM_COMPLETE) {
249 const unsigned char *nh = skb_network_header(skb);
250 skb->csum = csum_sub(skb->csum,
251 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
252 0));
255 /* Is this the final fragment? */
256 if (!(fhdr->frag_off & htons(IP6_MF))) {
257 /* If we already have some bits beyond end
258 * or have different end, the segment is corrupted.
260 if (end < fq->q.len ||
261 ((fq->q.last_in & LAST_IN) && end != fq->q.len)) {
262 pr_debug("already received last fragment\n");
263 goto err;
265 fq->q.last_in |= LAST_IN;
266 fq->q.len = end;
267 } else {
268 /* Check if the fragment is rounded to 8 bytes.
269 * Required by the RFC.
271 if (end & 0x7) {
272 /* RFC2460 says always send parameter problem in
273 * this case. -DaveM
275 pr_debug("end of fragment not rounded to 8 bytes.\n");
276 return -1;
278 if (end > fq->q.len) {
279 /* Some bits beyond end -> corruption. */
280 if (fq->q.last_in & LAST_IN) {
281 pr_debug("last packet already reached.\n");
282 goto err;
284 fq->q.len = end;
288 if (end == offset)
289 goto err;
291 /* Point into the IP datagram 'data' part. */
292 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
293 pr_debug("queue: message is too short.\n");
294 goto err;
296 if (pskb_trim_rcsum(skb, end - offset)) {
297 pr_debug("Can't trim\n");
298 goto err;
301 /* Find out which fragments are in front and at the back of us
302 * in the chain of fragments so far. We must know where to put
303 * this fragment, right?
305 prev = NULL;
306 for (next = fq->q.fragments; next != NULL; next = next->next) {
307 if (NFCT_FRAG6_CB(next)->offset >= offset)
308 break; /* bingo! */
309 prev = next;
312 /* We found where to put this one. Check for overlap with
313 * preceding fragment, and, if needed, align things so that
314 * any overlaps are eliminated.
316 if (prev) {
317 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
319 if (i > 0) {
320 offset += i;
321 if (end <= offset) {
322 pr_debug("overlap\n");
323 goto err;
325 if (!pskb_pull(skb, i)) {
326 pr_debug("Can't pull\n");
327 goto err;
329 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
330 skb->ip_summed = CHECKSUM_NONE;
334 /* Look for overlap with succeeding segments.
335 * If we can merge fragments, do it.
337 while (next && NFCT_FRAG6_CB(next)->offset < end) {
338 /* overlap is 'i' bytes */
339 int i = end - NFCT_FRAG6_CB(next)->offset;
341 if (i < next->len) {
342 /* Eat head of the next overlapped fragment
343 * and leave the loop. The next ones cannot overlap.
345 pr_debug("Eat head of the overlapped parts.: %d", i);
346 if (!pskb_pull(next, i))
347 goto err;
349 /* next fragment */
350 NFCT_FRAG6_CB(next)->offset += i;
351 fq->q.meat -= i;
352 if (next->ip_summed != CHECKSUM_UNNECESSARY)
353 next->ip_summed = CHECKSUM_NONE;
354 break;
355 } else {
356 struct sk_buff *free_it = next;
358 /* Old fragmnet is completely overridden with
359 * new one drop it.
361 next = next->next;
363 if (prev)
364 prev->next = next;
365 else
366 fq->q.fragments = next;
368 fq->q.meat -= free_it->len;
369 frag_kfree_skb(free_it, NULL);
373 NFCT_FRAG6_CB(skb)->offset = offset;
375 /* Insert this fragment in the chain of fragments. */
376 skb->next = next;
377 if (prev)
378 prev->next = skb;
379 else
380 fq->q.fragments = skb;
382 skb->dev = NULL;
383 fq->q.stamp = skb->tstamp;
384 fq->q.meat += skb->len;
385 atomic_add(skb->truesize, &nf_init_frags.mem);
387 /* The first fragment.
388 * nhoffset is obtained from the first fragment, of course.
390 if (offset == 0) {
391 fq->nhoffset = nhoff;
392 fq->q.last_in |= FIRST_IN;
394 write_lock(&nf_frags.lock);
395 list_move_tail(&fq->q.lru_list, &nf_frags.lru_list);
396 write_unlock(&nf_frags.lock);
397 return 0;
399 err:
400 return -1;
404 * Check if this packet is complete.
405 * Returns NULL on failure by any reason, and pointer
406 * to current nexthdr field in reassembled frame.
408 * It is called with locked fq, and caller must check that
409 * queue is eligible for reassembly i.e. it is not COMPLETE,
410 * the last and the first frames arrived and all the bits are here.
412 static struct sk_buff *
413 nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
415 struct sk_buff *fp, *op, *head = fq->q.fragments;
416 int payload_len;
418 fq_kill(fq);
420 BUG_TRAP(head != NULL);
421 BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0);
423 /* Unfragmented part is taken from the first segment. */
424 payload_len = ((head->data - skb_network_header(head)) -
425 sizeof(struct ipv6hdr) + fq->q.len -
426 sizeof(struct frag_hdr));
427 if (payload_len > IPV6_MAXPLEN) {
428 pr_debug("payload len is too large.\n");
429 goto out_oversize;
432 /* Head of list must not be cloned. */
433 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
434 pr_debug("skb is cloned but can't expand head");
435 goto out_oom;
438 /* If the first fragment is fragmented itself, we split
439 * it to two chunks: the first with data and paged part
440 * and the second, holding only fragments. */
441 if (skb_shinfo(head)->frag_list) {
442 struct sk_buff *clone;
443 int i, plen = 0;
445 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
446 pr_debug("Can't alloc skb\n");
447 goto out_oom;
449 clone->next = head->next;
450 head->next = clone;
451 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
452 skb_shinfo(head)->frag_list = NULL;
453 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
454 plen += skb_shinfo(head)->frags[i].size;
455 clone->len = clone->data_len = head->data_len - plen;
456 head->data_len -= clone->len;
457 head->len -= clone->len;
458 clone->csum = 0;
459 clone->ip_summed = head->ip_summed;
461 NFCT_FRAG6_CB(clone)->orig = NULL;
462 atomic_add(clone->truesize, &nf_init_frags.mem);
465 /* We have to remove fragment header from datagram and to relocate
466 * header in order to calculate ICV correctly. */
467 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
468 memmove(head->head + sizeof(struct frag_hdr), head->head,
469 (head->data - head->head) - sizeof(struct frag_hdr));
470 head->mac_header += sizeof(struct frag_hdr);
471 head->network_header += sizeof(struct frag_hdr);
473 skb_shinfo(head)->frag_list = head->next;
474 skb_reset_transport_header(head);
475 skb_push(head, head->data - skb_network_header(head));
476 atomic_sub(head->truesize, &nf_init_frags.mem);
478 for (fp=head->next; fp; fp = fp->next) {
479 head->data_len += fp->len;
480 head->len += fp->len;
481 if (head->ip_summed != fp->ip_summed)
482 head->ip_summed = CHECKSUM_NONE;
483 else if (head->ip_summed == CHECKSUM_COMPLETE)
484 head->csum = csum_add(head->csum, fp->csum);
485 head->truesize += fp->truesize;
486 atomic_sub(fp->truesize, &nf_init_frags.mem);
489 head->next = NULL;
490 head->dev = dev;
491 head->tstamp = fq->q.stamp;
492 ipv6_hdr(head)->payload_len = htons(payload_len);
494 /* Yes, and fold redundant checksum back. 8) */
495 if (head->ip_summed == CHECKSUM_COMPLETE)
496 head->csum = csum_partial(skb_network_header(head),
497 skb_network_header_len(head),
498 head->csum);
500 fq->q.fragments = NULL;
502 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
503 fp = skb_shinfo(head)->frag_list;
504 if (NFCT_FRAG6_CB(fp)->orig == NULL)
505 /* at above code, head skb is divided into two skbs. */
506 fp = fp->next;
508 op = NFCT_FRAG6_CB(head)->orig;
509 for (; fp; fp = fp->next) {
510 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
512 op->next = orig;
513 op = orig;
514 NFCT_FRAG6_CB(fp)->orig = NULL;
517 return head;
519 out_oversize:
520 if (net_ratelimit())
521 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
522 goto out_fail;
523 out_oom:
524 if (net_ratelimit())
525 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
526 out_fail:
527 return NULL;
531 * find the header just before Fragment Header.
533 * if success return 0 and set ...
534 * (*prevhdrp): the value of "Next Header Field" in the header
535 * just before Fragment Header.
536 * (*prevhoff): the offset of "Next Header Field" in the header
537 * just before Fragment Header.
538 * (*fhoff) : the offset of Fragment Header.
540 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
543 static int
544 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
546 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
547 const int netoff = skb_network_offset(skb);
548 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
549 int start = netoff + sizeof(struct ipv6hdr);
550 int len = skb->len - start;
551 u8 prevhdr = NEXTHDR_IPV6;
553 while (nexthdr != NEXTHDR_FRAGMENT) {
554 struct ipv6_opt_hdr hdr;
555 int hdrlen;
557 if (!ipv6_ext_hdr(nexthdr)) {
558 return -1;
560 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
561 pr_debug("too short\n");
562 return -1;
564 if (nexthdr == NEXTHDR_NONE) {
565 pr_debug("next header is none\n");
566 return -1;
568 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
569 BUG();
570 if (nexthdr == NEXTHDR_AUTH)
571 hdrlen = (hdr.hdrlen+2)<<2;
572 else
573 hdrlen = ipv6_optlen(&hdr);
575 prevhdr = nexthdr;
576 prev_nhoff = start;
578 nexthdr = hdr.nexthdr;
579 len -= hdrlen;
580 start += hdrlen;
583 if (len < 0)
584 return -1;
586 *prevhdrp = prevhdr;
587 *prevhoff = prev_nhoff;
588 *fhoff = start;
590 return 0;
593 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
595 struct sk_buff *clone;
596 struct net_device *dev = skb->dev;
597 struct frag_hdr *fhdr;
598 struct nf_ct_frag6_queue *fq;
599 struct ipv6hdr *hdr;
600 int fhoff, nhoff;
601 u8 prevhdr;
602 struct sk_buff *ret_skb = NULL;
604 /* Jumbo payload inhibits frag. header */
605 if (ipv6_hdr(skb)->payload_len == 0) {
606 pr_debug("payload len = 0\n");
607 return skb;
610 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
611 return skb;
613 clone = skb_clone(skb, GFP_ATOMIC);
614 if (clone == NULL) {
615 pr_debug("Can't clone skb\n");
616 return skb;
619 NFCT_FRAG6_CB(clone)->orig = skb;
621 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
622 pr_debug("message is too short.\n");
623 goto ret_orig;
626 skb_set_transport_header(clone, fhoff);
627 hdr = ipv6_hdr(clone);
628 fhdr = (struct frag_hdr *)skb_transport_header(clone);
630 if (!(fhdr->frag_off & htons(0xFFF9))) {
631 pr_debug("Invalid fragment offset\n");
632 /* It is not a fragmented frame */
633 goto ret_orig;
636 if (atomic_read(&nf_init_frags.mem) > nf_frags_ctl.high_thresh)
637 nf_ct_frag6_evictor();
639 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
640 if (fq == NULL) {
641 pr_debug("Can't find and can't create new queue\n");
642 goto ret_orig;
645 spin_lock(&fq->q.lock);
647 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
648 spin_unlock(&fq->q.lock);
649 pr_debug("Can't insert skb to queue\n");
650 fq_put(fq);
651 goto ret_orig;
654 if (fq->q.last_in == (FIRST_IN|LAST_IN) && fq->q.meat == fq->q.len) {
655 ret_skb = nf_ct_frag6_reasm(fq, dev);
656 if (ret_skb == NULL)
657 pr_debug("Can't reassemble fragmented packets\n");
659 spin_unlock(&fq->q.lock);
661 fq_put(fq);
662 return ret_skb;
664 ret_orig:
665 kfree_skb(clone);
666 return skb;
669 void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
670 struct net_device *in, struct net_device *out,
671 int (*okfn)(struct sk_buff *))
673 struct sk_buff *s, *s2;
675 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
676 nf_conntrack_put_reasm(s->nfct_reasm);
677 nf_conntrack_get_reasm(skb);
678 s->nfct_reasm = skb;
680 s2 = s->next;
681 s->next = NULL;
683 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
684 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
685 s = s2;
687 nf_conntrack_put_reasm(skb);
690 int nf_ct_frag6_kfree_frags(struct sk_buff *skb)
692 struct sk_buff *s, *s2;
694 for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) {
696 s2 = s->next;
697 kfree_skb(s);
700 kfree_skb(skb);
702 return 0;
705 int nf_ct_frag6_init(void)
707 nf_frags.ctl = &nf_frags_ctl;
708 nf_frags.hashfn = nf_hashfn;
709 nf_frags.constructor = ip6_frag_init;
710 nf_frags.destructor = NULL;
711 nf_frags.skb_free = nf_skb_free;
712 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
713 nf_frags.match = ip6_frag_match;
714 nf_frags.frag_expire = nf_ct_frag6_expire;
715 inet_frags_init_net(&nf_init_frags);
716 inet_frags_init(&nf_frags);
718 return 0;
721 void nf_ct_frag6_cleanup(void)
723 inet_frags_fini(&nf_frags);
725 nf_frags_ctl.low_thresh = 0;
726 nf_ct_frag6_evictor();