GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / ipv4 / ip_fragment.c
blobc464efad85be3d1bc23f432cc1cb25cdbdeedcb9
3 #include <linux/compiler.h>
4 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/mm.h>
7 #include <linux/jiffies.h>
8 #include <linux/skbuff.h>
9 #include <linux/list.h>
10 #include <linux/ip.h>
11 #include <linux/icmp.h>
12 #include <linux/netdevice.h>
13 #include <linux/jhash.h>
14 #include <linux/random.h>
15 #include <linux/slab.h>
16 #include <net/route.h>
17 #include <net/dst.h>
18 #include <net/sock.h>
19 #include <net/ip.h>
20 #include <net/icmp.h>
21 #include <net/checksum.h>
22 #include <net/inetpeer.h>
23 #include <net/inet_frag.h>
24 #include <linux/tcp.h>
25 #include <linux/udp.h>
26 #include <linux/inet.h>
27 #include <linux/netfilter_ipv4.h>
29 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
30 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
31 * as well. Or notify me, at least. --ANK
34 static int sysctl_ipfrag_max_dist __read_mostly = 64;
36 struct ipfrag_skb_cb
38 struct inet_skb_parm h;
39 int offset;
42 #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
44 /* Describe an entry in the "incomplete datagrams" queue. */
45 struct ipq {
46 struct inet_frag_queue q;
48 u32 user;
49 __be32 saddr;
50 __be32 daddr;
51 __be16 id;
52 u8 protocol;
53 int iif;
54 unsigned int rid;
55 struct inet_peer *peer;
58 static struct inet_frags ip4_frags;
60 int ip_frag_nqueues(struct net *net)
62 return net->ipv4.frags.nqueues;
65 int ip_frag_mem(struct net *net)
67 return atomic_read(&net->ipv4.frags.mem);
70 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
71 struct net_device *dev);
73 struct ip4_create_arg {
74 struct iphdr *iph;
75 u32 user;
78 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
80 return jhash_3words((__force u32)id << 16 | prot,
81 (__force u32)saddr, (__force u32)daddr,
82 ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
85 static unsigned int ip4_hashfn(struct inet_frag_queue *q)
87 struct ipq *ipq;
89 ipq = container_of(q, struct ipq, q);
90 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
93 static int ip4_frag_match(struct inet_frag_queue *q, void *a)
95 struct ipq *qp;
96 struct ip4_create_arg *arg = a;
98 qp = container_of(q, struct ipq, q);
99 return (qp->id == arg->iph->id &&
100 qp->saddr == arg->iph->saddr &&
101 qp->daddr == arg->iph->daddr &&
102 qp->protocol == arg->iph->protocol &&
103 qp->user == arg->user);
106 /* Memory Tracking Functions. */
107 static void frag_kfree_skb(struct netns_frags *nf, struct sk_buff *skb)
109 atomic_sub(skb->truesize, &nf->mem);
110 kfree_skb(skb);
113 static void ip4_frag_init(struct inet_frag_queue *q, void *a)
115 struct ipq *qp = container_of(q, struct ipq, q);
116 struct ip4_create_arg *arg = a;
118 qp->protocol = arg->iph->protocol;
119 qp->id = arg->iph->id;
120 qp->saddr = arg->iph->saddr;
121 qp->daddr = arg->iph->daddr;
122 qp->user = arg->user;
123 qp->peer = sysctl_ipfrag_max_dist ?
124 inet_getpeer(arg->iph->saddr, 1) : NULL;
127 static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
129 struct ipq *qp;
131 qp = container_of(q, struct ipq, q);
132 if (qp->peer)
133 inet_putpeer(qp->peer);
137 /* Destruction primitives. */
139 static __inline__ void ipq_put(struct ipq *ipq)
141 inet_frag_put(&ipq->q, &ip4_frags);
144 /* Kill ipq entry. It is not destroyed immediately,
145 * because caller (and someone more) holds reference count.
147 static void ipq_kill(struct ipq *ipq)
149 inet_frag_kill(&ipq->q, &ip4_frags);
152 /* Memory limiting on fragments. Evictor trashes the oldest
153 * fragment queue until we are back under the threshold.
155 static void ip_evictor(struct net *net)
157 int evicted;
159 evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags);
160 if (evicted)
161 IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted);
165 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
167 static void ip_expire(unsigned long arg)
169 struct ipq *qp;
170 struct net *net;
172 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
173 net = container_of(qp->q.net, struct net, ipv4.frags);
175 spin_lock(&qp->q.lock);
177 if (qp->q.last_in & INET_FRAG_COMPLETE)
178 goto out;
180 ipq_kill(qp);
182 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
183 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
185 if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
186 struct sk_buff *head = qp->q.fragments;
188 rcu_read_lock();
189 head->dev = dev_get_by_index_rcu(net, qp->iif);
190 if (!head->dev)
191 goto out_rcu_unlock;
194 * Only search router table for the head fragment,
195 * when defraging timeout at PRE_ROUTING HOOK.
197 if (qp->user == IP_DEFRAG_CONNTRACK_IN && !skb_dst(head)) {
198 const struct iphdr *iph = ip_hdr(head);
199 int err = ip_route_input(head, iph->daddr, iph->saddr,
200 iph->tos, head->dev);
201 if (unlikely(err))
202 goto out_rcu_unlock;
205 * Only an end host needs to send an ICMP
206 * "Fragment Reassembly Timeout" message, per RFC792.
208 if (skb_rtable(head)->rt_type != RTN_LOCAL)
209 goto out_rcu_unlock;
213 /* Send an ICMP "Fragment Reassembly Timeout" message. */
214 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
215 out_rcu_unlock:
216 rcu_read_unlock();
218 out:
219 spin_unlock(&qp->q.lock);
220 ipq_put(qp);
223 /* Find the correct entry in the "incomplete datagrams" queue for
224 * this IP datagram, and create new one, if nothing is found.
226 static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
228 struct inet_frag_queue *q;
229 struct ip4_create_arg arg;
230 unsigned int hash;
232 arg.iph = iph;
233 arg.user = user;
235 read_lock(&ip4_frags.lock);
236 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
238 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
239 if (q == NULL)
240 goto out_nomem;
242 return container_of(q, struct ipq, q);
244 out_nomem:
245 LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
246 return NULL;
249 /* Is the fragment too far ahead to be part of ipq? */
250 static inline int ip_frag_too_far(struct ipq *qp)
252 struct inet_peer *peer = qp->peer;
253 unsigned int max = sysctl_ipfrag_max_dist;
254 unsigned int start, end;
256 int rc;
258 if (!peer || !max)
259 return 0;
261 start = qp->rid;
262 end = atomic_inc_return(&peer->rid);
263 qp->rid = end;
265 rc = qp->q.fragments && (end - start) > max;
267 if (rc) {
268 struct net *net;
270 net = container_of(qp->q.net, struct net, ipv4.frags);
271 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
274 return rc;
277 static int ip_frag_reinit(struct ipq *qp)
279 struct sk_buff *fp;
281 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
282 atomic_inc(&qp->q.refcnt);
283 return -ETIMEDOUT;
286 fp = qp->q.fragments;
287 do {
288 struct sk_buff *xp = fp->next;
289 frag_kfree_skb(qp->q.net, fp);
290 fp = xp;
291 } while (fp);
293 qp->q.last_in = 0;
294 qp->q.len = 0;
295 qp->q.meat = 0;
296 qp->q.fragments = NULL;
297 qp->q.fragments_tail = NULL;
298 qp->iif = 0;
300 return 0;
303 /* Add new segment to existing queue. */
304 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
306 struct sk_buff *prev, *next;
307 struct net_device *dev;
308 int flags, offset;
309 int ihl, end;
310 int err = -ENOENT;
312 if (qp->q.last_in & INET_FRAG_COMPLETE)
313 goto err;
315 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
316 unlikely(ip_frag_too_far(qp)) &&
317 unlikely(err = ip_frag_reinit(qp))) {
318 ipq_kill(qp);
319 goto err;
322 offset = ntohs(ip_hdr(skb)->frag_off);
323 flags = offset & ~IP_OFFSET;
324 offset &= IP_OFFSET;
325 offset <<= 3; /* offset is in 8-byte chunks */
326 ihl = ip_hdrlen(skb);
328 /* Determine the position of this fragment. */
329 end = offset + skb->len - ihl;
330 err = -EINVAL;
332 /* Is this the final fragment? */
333 if ((flags & IP_MF) == 0) {
334 /* If we already have some bits beyond end
335 * or have different end, the segment is corrrupted.
337 if (end < qp->q.len ||
338 ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
339 goto err;
340 qp->q.last_in |= INET_FRAG_LAST_IN;
341 qp->q.len = end;
342 } else {
343 if (end&7) {
344 end &= ~7;
345 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
346 skb->ip_summed = CHECKSUM_NONE;
348 if (end > qp->q.len) {
349 /* Some bits beyond end -> corruption. */
350 if (qp->q.last_in & INET_FRAG_LAST_IN)
351 goto err;
352 qp->q.len = end;
355 if (end == offset)
356 goto err;
358 err = -ENOMEM;
359 if (pskb_pull(skb, ihl) == NULL)
360 goto err;
362 err = pskb_trim_rcsum(skb, end - offset);
363 if (err)
364 goto err;
366 /* Find out which fragments are in front and at the back of us
367 * in the chain of fragments so far. We must know where to put
368 * this fragment, right?
370 prev = qp->q.fragments_tail;
371 if (!prev || FRAG_CB(prev)->offset < offset) {
372 next = NULL;
373 goto found;
375 prev = NULL;
376 for (next = qp->q.fragments; next != NULL; next = next->next) {
377 if (FRAG_CB(next)->offset >= offset)
378 break; /* bingo! */
379 prev = next;
382 found:
383 /* We found where to put this one. Check for overlap with
384 * preceding fragment, and, if needed, align things so that
385 * any overlaps are eliminated.
387 if (prev) {
388 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
390 if (i > 0) {
391 offset += i;
392 err = -EINVAL;
393 if (end <= offset)
394 goto err;
395 err = -ENOMEM;
396 if (!pskb_pull(skb, i))
397 goto err;
398 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
399 skb->ip_summed = CHECKSUM_NONE;
403 err = -ENOMEM;
405 while (next && FRAG_CB(next)->offset < end) {
406 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
408 if (i < next->len) {
409 /* Eat head of the next overlapped fragment
410 * and leave the loop. The next ones cannot overlap.
412 if (!pskb_pull(next, i))
413 goto err;
414 FRAG_CB(next)->offset += i;
415 qp->q.meat -= i;
416 if (next->ip_summed != CHECKSUM_UNNECESSARY)
417 next->ip_summed = CHECKSUM_NONE;
418 break;
419 } else {
420 struct sk_buff *free_it = next;
422 /* Old fragment is completely overridden with
423 * new one drop it.
425 next = next->next;
427 if (prev)
428 prev->next = next;
429 else
430 qp->q.fragments = next;
432 qp->q.meat -= free_it->len;
433 frag_kfree_skb(qp->q.net, free_it);
437 FRAG_CB(skb)->offset = offset;
439 /* Insert this fragment in the chain of fragments. */
440 skb->next = next;
441 if (!next)
442 qp->q.fragments_tail = skb;
443 if (prev)
444 prev->next = skb;
445 else
446 qp->q.fragments = skb;
448 dev = skb->dev;
449 if (dev) {
450 qp->iif = dev->ifindex;
451 skb->dev = NULL;
453 qp->q.stamp = skb->tstamp;
454 qp->q.meat += skb->len;
455 atomic_add(skb->truesize, &qp->q.net->mem);
456 if (offset == 0)
457 qp->q.last_in |= INET_FRAG_FIRST_IN;
459 if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
460 qp->q.meat == qp->q.len)
461 return ip_frag_reasm(qp, prev, dev);
463 write_lock(&ip4_frags.lock);
464 list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
465 write_unlock(&ip4_frags.lock);
466 return -EINPROGRESS;
468 err:
469 kfree_skb(skb);
470 return err;
474 /* Build a new IP datagram from all its fragments. */
476 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
477 struct net_device *dev)
479 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
480 struct iphdr *iph;
481 struct sk_buff *fp, *head = qp->q.fragments;
482 int len;
483 int ihlen;
484 int err;
486 ipq_kill(qp);
488 /* Make the one we just received the head. */
489 if (prev) {
490 head = prev->next;
491 fp = skb_clone(head, GFP_ATOMIC);
492 if (!fp)
493 goto out_nomem;
495 fp->next = head->next;
496 if (!fp->next)
497 qp->q.fragments_tail = fp;
498 prev->next = fp;
500 skb_morph(head, qp->q.fragments);
501 head->next = qp->q.fragments->next;
503 kfree_skb(qp->q.fragments);
504 qp->q.fragments = head;
507 WARN_ON(head == NULL);
508 WARN_ON(FRAG_CB(head)->offset != 0);
510 /* Allocate a new buffer for the datagram. */
511 ihlen = ip_hdrlen(head);
512 len = ihlen + qp->q.len;
514 err = -E2BIG;
515 if (len > 65535)
516 goto out_oversize;
518 /* Head of list must not be cloned. */
519 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
520 goto out_nomem;
522 /* If the first fragment is fragmented itself, we split
523 * it to two chunks: the first with data and paged part
524 * and the second, holding only fragments. */
525 if (skb_has_frags(head)) {
526 struct sk_buff *clone;
527 int i, plen = 0;
529 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
530 goto out_nomem;
531 clone->next = head->next;
532 head->next = clone;
533 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
534 skb_frag_list_init(head);
535 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
536 plen += skb_shinfo(head)->frags[i].size;
537 clone->len = clone->data_len = head->data_len - plen;
538 head->data_len -= clone->len;
539 head->len -= clone->len;
540 clone->csum = 0;
541 clone->ip_summed = head->ip_summed;
542 atomic_add(clone->truesize, &qp->q.net->mem);
545 skb_shinfo(head)->frag_list = head->next;
546 skb_push(head, head->data - skb_network_header(head));
548 for (fp=head->next; fp; fp = fp->next) {
549 head->data_len += fp->len;
550 head->len += fp->len;
551 if (head->ip_summed != fp->ip_summed)
552 head->ip_summed = CHECKSUM_NONE;
553 else if (head->ip_summed == CHECKSUM_COMPLETE)
554 head->csum = csum_add(head->csum, fp->csum);
555 head->truesize += fp->truesize;
557 atomic_sub(head->truesize, &qp->q.net->mem);
559 head->next = NULL;
560 head->dev = dev;
561 head->tstamp = qp->q.stamp;
563 iph = ip_hdr(head);
564 iph->frag_off = 0;
565 iph->tot_len = htons(len);
566 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
567 qp->q.fragments = NULL;
568 qp->q.fragments_tail = NULL;
569 return 0;
571 out_nomem:
572 LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
573 "queue %p\n", qp);
574 err = -ENOMEM;
575 goto out_fail;
576 out_oversize:
577 if (net_ratelimit())
578 printk(KERN_INFO "Oversized IP packet from %pI4.\n",
579 &qp->saddr);
580 out_fail:
581 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
582 return err;
585 /* Process an incoming IP datagram fragment. */
586 int ip_defrag(struct sk_buff *skb, u32 user)
588 struct ipq *qp;
589 struct net *net;
591 net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
592 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
594 /* Start by cleaning up the memory. */
595 if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
596 ip_evictor(net);
598 /* Lookup (or create) queue header */
599 if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
600 int ret;
602 spin_lock(&qp->q.lock);
604 ret = ip_frag_queue(qp, skb);
606 spin_unlock(&qp->q.lock);
607 ipq_put(qp);
608 return ret;
611 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
612 kfree_skb(skb);
613 return -ENOMEM;
615 EXPORT_SYMBOL(ip_defrag);
617 #ifdef CONFIG_SYSCTL
618 static int zero;
620 static struct ctl_table ip4_frags_ns_ctl_table[] = {
622 .procname = "ipfrag_high_thresh",
623 .data = &init_net.ipv4.frags.high_thresh,
624 .maxlen = sizeof(int),
625 .mode = 0644,
626 .proc_handler = proc_dointvec
629 .procname = "ipfrag_low_thresh",
630 .data = &init_net.ipv4.frags.low_thresh,
631 .maxlen = sizeof(int),
632 .mode = 0644,
633 .proc_handler = proc_dointvec
636 .procname = "ipfrag_time",
637 .data = &init_net.ipv4.frags.timeout,
638 .maxlen = sizeof(int),
639 .mode = 0644,
640 .proc_handler = proc_dointvec_jiffies,
645 static struct ctl_table ip4_frags_ctl_table[] = {
647 .procname = "ipfrag_secret_interval",
648 .data = &ip4_frags.secret_interval,
649 .maxlen = sizeof(int),
650 .mode = 0644,
651 .proc_handler = proc_dointvec_jiffies,
654 .procname = "ipfrag_max_dist",
655 .data = &sysctl_ipfrag_max_dist,
656 .maxlen = sizeof(int),
657 .mode = 0644,
658 .proc_handler = proc_dointvec_minmax,
659 .extra1 = &zero
664 static int __net_init ip4_frags_ns_ctl_register(struct net *net)
666 struct ctl_table *table;
667 struct ctl_table_header *hdr;
669 table = ip4_frags_ns_ctl_table;
670 if (!net_eq(net, &init_net)) {
671 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
672 if (table == NULL)
673 goto err_alloc;
675 table[0].data = &net->ipv4.frags.high_thresh;
676 table[1].data = &net->ipv4.frags.low_thresh;
677 table[2].data = &net->ipv4.frags.timeout;
680 hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
681 if (hdr == NULL)
682 goto err_reg;
684 net->ipv4.frags_hdr = hdr;
685 return 0;
687 err_reg:
688 if (!net_eq(net, &init_net))
689 kfree(table);
690 err_alloc:
691 return -ENOMEM;
694 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
696 struct ctl_table *table;
698 table = net->ipv4.frags_hdr->ctl_table_arg;
699 unregister_net_sysctl_table(net->ipv4.frags_hdr);
700 kfree(table);
703 static void ip4_frags_ctl_register(void)
705 register_net_sysctl_rotable(net_ipv4_ctl_path, ip4_frags_ctl_table);
707 #else
708 static inline int ip4_frags_ns_ctl_register(struct net *net)
710 return 0;
713 static inline void ip4_frags_ns_ctl_unregister(struct net *net)
717 static inline void ip4_frags_ctl_register(void)
720 #endif
722 static int __net_init ipv4_frags_init_net(struct net *net)
725 * Fragment cache limits. We will commit 256K at one time. Should we
726 * cross that limit we will prune down to 192K. This should cope with
727 * even the most extreme cases without allowing an attacker to
728 * measurably harm machine performance.
730 net->ipv4.frags.high_thresh = 256 * 1024;
731 net->ipv4.frags.low_thresh = 192 * 1024;
733 * Important NOTE! Fragment queue must be destroyed before MSL expires.
734 * RFC791 is wrong proposing to prolongate timer each fragment arrival
735 * by TTL.
737 net->ipv4.frags.timeout = IP_FRAG_TIME;
739 inet_frags_init_net(&net->ipv4.frags);
741 return ip4_frags_ns_ctl_register(net);
744 static void __net_exit ipv4_frags_exit_net(struct net *net)
746 ip4_frags_ns_ctl_unregister(net);
747 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
750 static struct pernet_operations ip4_frags_ops = {
751 .init = ipv4_frags_init_net,
752 .exit = ipv4_frags_exit_net,
755 void __init ipfrag_init(void)
757 ip4_frags_ctl_register();
758 register_pernet_subsys(&ip4_frags_ops);
759 ip4_frags.hashfn = ip4_hashfn;
760 ip4_frags.constructor = ip4_frag_init;
761 ip4_frags.destructor = ip4_frag_free;
762 ip4_frags.skb_free = NULL;
763 ip4_frags.qsize = sizeof(struct ipq);
764 ip4_frags.match = ip4_frag_match;
765 ip4_frags.frag_expire = ip_expire;
766 ip4_frags.secret_interval = 10 * 60 * HZ;
767 inet_frags_init(&ip4_frags);