sysctl net: Remove unused binary sysctl code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
blobe0b9424fa1b2b3457cc99051b49c22105f5f3aff
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>
31 #include <net/sock.h>
32 #include <net/snmp.h>
33 #include <net/inet_frag.h>
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>
41 #include <net/netfilter/ipv6/nf_conntrack_ipv6.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 .procname = "nf_conntrack_frag6_low_thresh",
87 .data = &nf_init_frags.low_thresh,
88 .maxlen = sizeof(unsigned int),
89 .mode = 0644,
90 .proc_handler = proc_dointvec,
93 .procname = "nf_conntrack_frag6_high_thresh",
94 .data = &nf_init_frags.high_thresh,
95 .maxlen = sizeof(unsigned int),
96 .mode = 0644,
97 .proc_handler = proc_dointvec,
99 { }
101 #endif
103 static unsigned int nf_hashfn(struct inet_frag_queue *q)
105 const struct nf_ct_frag6_queue *nq;
107 nq = container_of(q, struct nf_ct_frag6_queue, q);
108 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd);
111 static void nf_skb_free(struct sk_buff *skb)
113 if (NFCT_FRAG6_CB(skb)->orig)
114 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
117 /* Memory Tracking Functions. */
118 static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
120 if (work)
121 *work -= skb->truesize;
122 atomic_sub(skb->truesize, &nf_init_frags.mem);
123 nf_skb_free(skb);
124 kfree_skb(skb);
127 /* Destruction primitives. */
129 static __inline__ void fq_put(struct nf_ct_frag6_queue *fq)
131 inet_frag_put(&fq->q, &nf_frags);
134 /* Kill fq entry. It is not destroyed immediately,
135 * because caller (and someone more) holds reference count.
137 static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
139 inet_frag_kill(&fq->q, &nf_frags);
142 static void nf_ct_frag6_evictor(void)
144 local_bh_disable();
145 inet_frag_evictor(&nf_init_frags, &nf_frags);
146 local_bh_enable();
149 static void nf_ct_frag6_expire(unsigned long data)
151 struct nf_ct_frag6_queue *fq;
153 fq = container_of((struct inet_frag_queue *)data,
154 struct nf_ct_frag6_queue, q);
156 spin_lock(&fq->q.lock);
158 if (fq->q.last_in & INET_FRAG_COMPLETE)
159 goto out;
161 fq_kill(fq);
163 out:
164 spin_unlock(&fq->q.lock);
165 fq_put(fq);
168 /* Creation primitives. */
170 static __inline__ struct nf_ct_frag6_queue *
171 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
173 struct inet_frag_queue *q;
174 struct ip6_create_arg arg;
175 unsigned int hash;
177 arg.id = id;
178 arg.src = src;
179 arg.dst = dst;
181 read_lock_bh(&nf_frags.lock);
182 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd);
184 q = inet_frag_find(&nf_init_frags, &nf_frags, &arg, hash);
185 local_bh_enable();
186 if (q == NULL)
187 goto oom;
189 return container_of(q, struct nf_ct_frag6_queue, q);
191 oom:
192 pr_debug("Can't alloc new queue\n");
193 return NULL;
197 static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
198 const struct frag_hdr *fhdr, int nhoff)
200 struct sk_buff *prev, *next;
201 int offset, end;
203 if (fq->q.last_in & INET_FRAG_COMPLETE) {
204 pr_debug("Allready completed\n");
205 goto err;
208 offset = ntohs(fhdr->frag_off) & ~0x7;
209 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
210 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
212 if ((unsigned int)end > IPV6_MAXPLEN) {
213 pr_debug("offset is too large.\n");
214 return -1;
217 if (skb->ip_summed == CHECKSUM_COMPLETE) {
218 const unsigned char *nh = skb_network_header(skb);
219 skb->csum = csum_sub(skb->csum,
220 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
221 0));
224 /* Is this the final fragment? */
225 if (!(fhdr->frag_off & htons(IP6_MF))) {
226 /* If we already have some bits beyond end
227 * or have different end, the segment is corrupted.
229 if (end < fq->q.len ||
230 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
231 pr_debug("already received last fragment\n");
232 goto err;
234 fq->q.last_in |= INET_FRAG_LAST_IN;
235 fq->q.len = end;
236 } else {
237 /* Check if the fragment is rounded to 8 bytes.
238 * Required by the RFC.
240 if (end & 0x7) {
241 /* RFC2460 says always send parameter problem in
242 * this case. -DaveM
244 pr_debug("end of fragment not rounded to 8 bytes.\n");
245 return -1;
247 if (end > fq->q.len) {
248 /* Some bits beyond end -> corruption. */
249 if (fq->q.last_in & INET_FRAG_LAST_IN) {
250 pr_debug("last packet already reached.\n");
251 goto err;
253 fq->q.len = end;
257 if (end == offset)
258 goto err;
260 /* Point into the IP datagram 'data' part. */
261 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
262 pr_debug("queue: message is too short.\n");
263 goto err;
265 if (pskb_trim_rcsum(skb, end - offset)) {
266 pr_debug("Can't trim\n");
267 goto err;
270 /* Find out which fragments are in front and at the back of us
271 * in the chain of fragments so far. We must know where to put
272 * this fragment, right?
274 prev = NULL;
275 for (next = fq->q.fragments; next != NULL; next = next->next) {
276 if (NFCT_FRAG6_CB(next)->offset >= offset)
277 break; /* bingo! */
278 prev = next;
281 /* We found where to put this one. Check for overlap with
282 * preceding fragment, and, if needed, align things so that
283 * any overlaps are eliminated.
285 if (prev) {
286 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
288 if (i > 0) {
289 offset += i;
290 if (end <= offset) {
291 pr_debug("overlap\n");
292 goto err;
294 if (!pskb_pull(skb, i)) {
295 pr_debug("Can't pull\n");
296 goto err;
298 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
299 skb->ip_summed = CHECKSUM_NONE;
303 /* Look for overlap with succeeding segments.
304 * If we can merge fragments, do it.
306 while (next && NFCT_FRAG6_CB(next)->offset < end) {
307 /* overlap is 'i' bytes */
308 int i = end - NFCT_FRAG6_CB(next)->offset;
310 if (i < next->len) {
311 /* Eat head of the next overlapped fragment
312 * and leave the loop. The next ones cannot overlap.
314 pr_debug("Eat head of the overlapped parts.: %d", i);
315 if (!pskb_pull(next, i))
316 goto err;
318 /* next fragment */
319 NFCT_FRAG6_CB(next)->offset += i;
320 fq->q.meat -= i;
321 if (next->ip_summed != CHECKSUM_UNNECESSARY)
322 next->ip_summed = CHECKSUM_NONE;
323 break;
324 } else {
325 struct sk_buff *free_it = next;
327 /* Old fragmnet is completely overridden with
328 * new one drop it.
330 next = next->next;
332 if (prev)
333 prev->next = next;
334 else
335 fq->q.fragments = next;
337 fq->q.meat -= free_it->len;
338 frag_kfree_skb(free_it, NULL);
342 NFCT_FRAG6_CB(skb)->offset = offset;
344 /* Insert this fragment in the chain of fragments. */
345 skb->next = next;
346 if (prev)
347 prev->next = skb;
348 else
349 fq->q.fragments = skb;
351 skb->dev = NULL;
352 fq->q.stamp = skb->tstamp;
353 fq->q.meat += skb->len;
354 atomic_add(skb->truesize, &nf_init_frags.mem);
356 /* The first fragment.
357 * nhoffset is obtained from the first fragment, of course.
359 if (offset == 0) {
360 fq->nhoffset = nhoff;
361 fq->q.last_in |= INET_FRAG_FIRST_IN;
363 write_lock(&nf_frags.lock);
364 list_move_tail(&fq->q.lru_list, &nf_init_frags.lru_list);
365 write_unlock(&nf_frags.lock);
366 return 0;
368 err:
369 return -1;
373 * Check if this packet is complete.
374 * Returns NULL on failure by any reason, and pointer
375 * to current nexthdr field in reassembled frame.
377 * It is called with locked fq, and caller must check that
378 * queue is eligible for reassembly i.e. it is not COMPLETE,
379 * the last and the first frames arrived and all the bits are here.
381 static struct sk_buff *
382 nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
384 struct sk_buff *fp, *op, *head = fq->q.fragments;
385 int payload_len;
387 fq_kill(fq);
389 WARN_ON(head == NULL);
390 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
392 /* Unfragmented part is taken from the first segment. */
393 payload_len = ((head->data - skb_network_header(head)) -
394 sizeof(struct ipv6hdr) + fq->q.len -
395 sizeof(struct frag_hdr));
396 if (payload_len > IPV6_MAXPLEN) {
397 pr_debug("payload len is too large.\n");
398 goto out_oversize;
401 /* Head of list must not be cloned. */
402 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
403 pr_debug("skb is cloned but can't expand head");
404 goto out_oom;
407 /* If the first fragment is fragmented itself, we split
408 * it to two chunks: the first with data and paged part
409 * and the second, holding only fragments. */
410 if (skb_has_frags(head)) {
411 struct sk_buff *clone;
412 int i, plen = 0;
414 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
415 pr_debug("Can't alloc skb\n");
416 goto out_oom;
418 clone->next = head->next;
419 head->next = clone;
420 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
421 skb_frag_list_init(head);
422 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
423 plen += skb_shinfo(head)->frags[i].size;
424 clone->len = clone->data_len = head->data_len - plen;
425 head->data_len -= clone->len;
426 head->len -= clone->len;
427 clone->csum = 0;
428 clone->ip_summed = head->ip_summed;
430 NFCT_FRAG6_CB(clone)->orig = NULL;
431 atomic_add(clone->truesize, &nf_init_frags.mem);
434 /* We have to remove fragment header from datagram and to relocate
435 * header in order to calculate ICV correctly. */
436 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
437 memmove(head->head + sizeof(struct frag_hdr), head->head,
438 (head->data - head->head) - sizeof(struct frag_hdr));
439 head->mac_header += sizeof(struct frag_hdr);
440 head->network_header += sizeof(struct frag_hdr);
442 skb_shinfo(head)->frag_list = head->next;
443 skb_reset_transport_header(head);
444 skb_push(head, head->data - skb_network_header(head));
445 atomic_sub(head->truesize, &nf_init_frags.mem);
447 for (fp=head->next; fp; fp = fp->next) {
448 head->data_len += fp->len;
449 head->len += fp->len;
450 if (head->ip_summed != fp->ip_summed)
451 head->ip_summed = CHECKSUM_NONE;
452 else if (head->ip_summed == CHECKSUM_COMPLETE)
453 head->csum = csum_add(head->csum, fp->csum);
454 head->truesize += fp->truesize;
455 atomic_sub(fp->truesize, &nf_init_frags.mem);
458 head->next = NULL;
459 head->dev = dev;
460 head->tstamp = fq->q.stamp;
461 ipv6_hdr(head)->payload_len = htons(payload_len);
463 /* Yes, and fold redundant checksum back. 8) */
464 if (head->ip_summed == CHECKSUM_COMPLETE)
465 head->csum = csum_partial(skb_network_header(head),
466 skb_network_header_len(head),
467 head->csum);
469 fq->q.fragments = NULL;
471 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
472 fp = skb_shinfo(head)->frag_list;
473 if (NFCT_FRAG6_CB(fp)->orig == NULL)
474 /* at above code, head skb is divided into two skbs. */
475 fp = fp->next;
477 op = NFCT_FRAG6_CB(head)->orig;
478 for (; fp; fp = fp->next) {
479 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
481 op->next = orig;
482 op = orig;
483 NFCT_FRAG6_CB(fp)->orig = NULL;
486 return head;
488 out_oversize:
489 if (net_ratelimit())
490 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
491 goto out_fail;
492 out_oom:
493 if (net_ratelimit())
494 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
495 out_fail:
496 return NULL;
500 * find the header just before Fragment Header.
502 * if success return 0 and set ...
503 * (*prevhdrp): the value of "Next Header Field" in the header
504 * just before Fragment Header.
505 * (*prevhoff): the offset of "Next Header Field" in the header
506 * just before Fragment Header.
507 * (*fhoff) : the offset of Fragment Header.
509 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
512 static int
513 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
515 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
516 const int netoff = skb_network_offset(skb);
517 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
518 int start = netoff + sizeof(struct ipv6hdr);
519 int len = skb->len - start;
520 u8 prevhdr = NEXTHDR_IPV6;
522 while (nexthdr != NEXTHDR_FRAGMENT) {
523 struct ipv6_opt_hdr hdr;
524 int hdrlen;
526 if (!ipv6_ext_hdr(nexthdr)) {
527 return -1;
529 if (nexthdr == NEXTHDR_NONE) {
530 pr_debug("next header is none\n");
531 return -1;
533 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
534 pr_debug("too short\n");
535 return -1;
537 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
538 BUG();
539 if (nexthdr == NEXTHDR_AUTH)
540 hdrlen = (hdr.hdrlen+2)<<2;
541 else
542 hdrlen = ipv6_optlen(&hdr);
544 prevhdr = nexthdr;
545 prev_nhoff = start;
547 nexthdr = hdr.nexthdr;
548 len -= hdrlen;
549 start += hdrlen;
552 if (len < 0)
553 return -1;
555 *prevhdrp = prevhdr;
556 *prevhoff = prev_nhoff;
557 *fhoff = start;
559 return 0;
562 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
564 struct sk_buff *clone;
565 struct net_device *dev = skb->dev;
566 struct frag_hdr *fhdr;
567 struct nf_ct_frag6_queue *fq;
568 struct ipv6hdr *hdr;
569 int fhoff, nhoff;
570 u8 prevhdr;
571 struct sk_buff *ret_skb = NULL;
573 /* Jumbo payload inhibits frag. header */
574 if (ipv6_hdr(skb)->payload_len == 0) {
575 pr_debug("payload len = 0\n");
576 return skb;
579 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
580 return skb;
582 clone = skb_clone(skb, GFP_ATOMIC);
583 if (clone == NULL) {
584 pr_debug("Can't clone skb\n");
585 return skb;
588 NFCT_FRAG6_CB(clone)->orig = skb;
590 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
591 pr_debug("message is too short.\n");
592 goto ret_orig;
595 skb_set_transport_header(clone, fhoff);
596 hdr = ipv6_hdr(clone);
597 fhdr = (struct frag_hdr *)skb_transport_header(clone);
599 if (!(fhdr->frag_off & htons(0xFFF9))) {
600 pr_debug("Invalid fragment offset\n");
601 /* It is not a fragmented frame */
602 goto ret_orig;
605 if (atomic_read(&nf_init_frags.mem) > nf_init_frags.high_thresh)
606 nf_ct_frag6_evictor();
608 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
609 if (fq == NULL) {
610 pr_debug("Can't find and can't create new queue\n");
611 goto ret_orig;
614 spin_lock_bh(&fq->q.lock);
616 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
617 spin_unlock_bh(&fq->q.lock);
618 pr_debug("Can't insert skb to queue\n");
619 fq_put(fq);
620 goto ret_orig;
623 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
624 fq->q.meat == fq->q.len) {
625 ret_skb = nf_ct_frag6_reasm(fq, dev);
626 if (ret_skb == NULL)
627 pr_debug("Can't reassemble fragmented packets\n");
629 spin_unlock_bh(&fq->q.lock);
631 fq_put(fq);
632 return ret_skb;
634 ret_orig:
635 kfree_skb(clone);
636 return skb;
639 void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
640 struct net_device *in, struct net_device *out,
641 int (*okfn)(struct sk_buff *))
643 struct sk_buff *s, *s2;
645 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
646 nf_conntrack_put_reasm(s->nfct_reasm);
647 nf_conntrack_get_reasm(skb);
648 s->nfct_reasm = skb;
650 s2 = s->next;
651 s->next = NULL;
653 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
654 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
655 s = s2;
657 nf_conntrack_put_reasm(skb);
660 int nf_ct_frag6_init(void)
662 nf_frags.hashfn = nf_hashfn;
663 nf_frags.constructor = ip6_frag_init;
664 nf_frags.destructor = NULL;
665 nf_frags.skb_free = nf_skb_free;
666 nf_frags.qsize = sizeof(struct nf_ct_frag6_queue);
667 nf_frags.match = ip6_frag_match;
668 nf_frags.frag_expire = nf_ct_frag6_expire;
669 nf_frags.secret_interval = 10 * 60 * HZ;
670 nf_init_frags.timeout = IPV6_FRAG_TIMEOUT;
671 nf_init_frags.high_thresh = 256 * 1024;
672 nf_init_frags.low_thresh = 192 * 1024;
673 inet_frags_init_net(&nf_init_frags);
674 inet_frags_init(&nf_frags);
676 return 0;
679 void nf_ct_frag6_cleanup(void)
681 inet_frags_fini(&nf_frags);
683 nf_init_frags.low_thresh = 0;
684 nf_ct_frag6_evictor();