2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * The IP fragmentation functionality.
8 * Version: $Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
10 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
11 * Alan Cox <Alan.Cox@linux.org>
14 * Alan Cox : Split from ip.c , see ip_input.c for history.
15 * David S. Miller : Begin massive cleanup...
16 * Andi Kleen : Add sysctls.
17 * xxxx : Overlapfrag bug.
18 * Ultima : ip_expire() kernel panic.
19 * Bill Hawes : Frag accounting and evictor fixes.
20 * John McDonald : 0 length frag bug.
21 * Alexey Kuznetsov: SMP races, threading, cleanup.
22 * Patrick McHardy : LRU queue of frag heads for evictor.
25 #include <linux/compiler.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
29 #include <linux/jiffies.h>
30 #include <linux/skbuff.h>
31 #include <linux/list.h>
33 #include <linux/icmp.h>
34 #include <linux/netdevice.h>
35 #include <linux/jhash.h>
36 #include <linux/random.h>
40 #include <net/checksum.h>
41 #include <net/inetpeer.h>
42 #include <net/inet_frag.h>
43 #include <linux/tcp.h>
44 #include <linux/udp.h>
45 #include <linux/inet.h>
46 #include <linux/netfilter_ipv4.h>
48 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
49 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
50 * as well. Or notify me, at least. --ANK
53 /* Fragment cache limits. We will commit 256K at one time. Should we
54 * cross that limit we will prune down to 192K. This should cope with
55 * even the most extreme cases without allowing an attacker to measurably
56 * harm machine performance.
58 int sysctl_ipfrag_high_thresh __read_mostly = 256*1024;
59 int sysctl_ipfrag_low_thresh __read_mostly = 192*1024;
61 int sysctl_ipfrag_max_dist __read_mostly = 64;
63 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
64 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
66 int sysctl_ipfrag_time __read_mostly = IP_FRAG_TIME;
70 struct inet_skb_parm h;
74 #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
76 /* Describe an entry in the "incomplete datagrams" queue. */
78 struct inet_frag_queue q;
87 struct inet_peer *peer;
94 /* Per-bucket lock is easy to add now. */
95 static struct hlist_head ipq_hash[IPQ_HASHSZ];
96 static DEFINE_RWLOCK(ipfrag_lock);
97 static u32 ipfrag_hash_rnd;
98 static LIST_HEAD(ipq_lru_list);
99 int ip_frag_nqueues = 0;
101 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
102 struct net_device *dev);
104 static __inline__ void __ipq_unlink(struct ipq *qp)
106 hlist_del(&qp->q.list);
107 list_del(&qp->q.lru_list);
111 static __inline__ void ipq_unlink(struct ipq *ipq)
113 write_lock(&ipfrag_lock);
115 write_unlock(&ipfrag_lock);
118 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
120 return jhash_3words((__force u32)id << 16 | prot,
121 (__force u32)saddr, (__force u32)daddr,
122 ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
125 static struct timer_list ipfrag_secret_timer;
126 int sysctl_ipfrag_secret_interval __read_mostly = 10 * 60 * HZ;
128 static void ipfrag_secret_rebuild(unsigned long dummy)
130 unsigned long now = jiffies;
133 write_lock(&ipfrag_lock);
134 get_random_bytes(&ipfrag_hash_rnd, sizeof(u32));
135 for (i = 0; i < IPQ_HASHSZ; i++) {
137 struct hlist_node *p, *n;
139 hlist_for_each_entry_safe(q, p, n, &ipq_hash[i], q.list) {
140 unsigned int hval = ipqhashfn(q->id, q->saddr,
141 q->daddr, q->protocol);
144 hlist_del(&q->q.list);
146 /* Relink to new hash chain. */
147 hlist_add_head(&q->q.list, &ipq_hash[hval]);
151 write_unlock(&ipfrag_lock);
153 mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval);
156 atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */
158 /* Memory Tracking Functions. */
159 static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work)
162 *work -= skb->truesize;
163 atomic_sub(skb->truesize, &ip_frag_mem);
167 static __inline__ void frag_free_queue(struct ipq *qp, int *work)
170 *work -= sizeof(struct ipq);
171 atomic_sub(sizeof(struct ipq), &ip_frag_mem);
175 static __inline__ struct ipq *frag_alloc_queue(void)
177 struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
181 atomic_add(sizeof(struct ipq), &ip_frag_mem);
186 /* Destruction primitives. */
188 /* Complete destruction of ipq. */
189 static void ip_frag_destroy(struct ipq *qp, int *work)
193 BUG_TRAP(qp->q.last_in&COMPLETE);
194 BUG_TRAP(del_timer(&qp->q.timer) == 0);
197 inet_putpeer(qp->peer);
199 /* Release all fragment data. */
200 fp = qp->q.fragments;
202 struct sk_buff *xp = fp->next;
204 frag_kfree_skb(fp, work);
208 /* Finally, release the queue descriptor itself. */
209 frag_free_queue(qp, work);
212 static __inline__ void ipq_put(struct ipq *ipq, int *work)
214 if (atomic_dec_and_test(&ipq->q.refcnt))
215 ip_frag_destroy(ipq, work);
218 /* Kill ipq entry. It is not destroyed immediately,
219 * because caller (and someone more) holds reference count.
221 static void ipq_kill(struct ipq *ipq)
223 if (del_timer(&ipq->q.timer))
224 atomic_dec(&ipq->q.refcnt);
226 if (!(ipq->q.last_in & COMPLETE)) {
228 atomic_dec(&ipq->q.refcnt);
229 ipq->q.last_in |= COMPLETE;
233 /* Memory limiting on fragments. Evictor trashes the oldest
234 * fragment queue until we are back under the threshold.
236 static void ip_evictor(void)
239 struct list_head *tmp;
242 work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh;
247 read_lock(&ipfrag_lock);
248 if (list_empty(&ipq_lru_list)) {
249 read_unlock(&ipfrag_lock);
252 tmp = ipq_lru_list.next;
253 qp = list_entry(tmp, struct ipq, q.lru_list);
254 atomic_inc(&qp->q.refcnt);
255 read_unlock(&ipfrag_lock);
257 spin_lock(&qp->q.lock);
258 if (!(qp->q.last_in&COMPLETE))
260 spin_unlock(&qp->q.lock);
263 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
268 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
270 static void ip_expire(unsigned long arg)
272 struct ipq *qp = (struct ipq *) arg;
274 spin_lock(&qp->q.lock);
276 if (qp->q.last_in & COMPLETE)
281 IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
282 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
284 if ((qp->q.last_in&FIRST_IN) && qp->q.fragments != NULL) {
285 struct sk_buff *head = qp->q.fragments;
286 /* Send an ICMP "Fragment Reassembly Timeout" message. */
287 if ((head->dev = dev_get_by_index(&init_net, qp->iif)) != NULL) {
288 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
293 spin_unlock(&qp->q.lock);
297 /* Creation primitives. */
299 static struct ipq *ip_frag_intern(struct ipq *qp_in)
303 struct hlist_node *n;
307 write_lock(&ipfrag_lock);
308 hash = ipqhashfn(qp_in->id, qp_in->saddr, qp_in->daddr,
311 /* With SMP race we have to recheck hash table, because
312 * such entry could be created on other cpu, while we
313 * promoted read lock to write lock.
315 hlist_for_each_entry(qp, n, &ipq_hash[hash], q.list) {
316 if (qp->id == qp_in->id &&
317 qp->saddr == qp_in->saddr &&
318 qp->daddr == qp_in->daddr &&
319 qp->protocol == qp_in->protocol &&
320 qp->user == qp_in->user) {
321 atomic_inc(&qp->q.refcnt);
322 write_unlock(&ipfrag_lock);
323 qp_in->q.last_in |= COMPLETE;
324 ipq_put(qp_in, NULL);
331 if (!mod_timer(&qp->q.timer, jiffies + sysctl_ipfrag_time))
332 atomic_inc(&qp->q.refcnt);
334 atomic_inc(&qp->q.refcnt);
335 hlist_add_head(&qp->q.list, &ipq_hash[hash]);
336 INIT_LIST_HEAD(&qp->q.lru_list);
337 list_add_tail(&qp->q.lru_list, &ipq_lru_list);
339 write_unlock(&ipfrag_lock);
343 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
344 static struct ipq *ip_frag_create(struct iphdr *iph, u32 user)
348 if ((qp = frag_alloc_queue()) == NULL)
351 qp->protocol = iph->protocol;
354 qp->saddr = iph->saddr;
355 qp->daddr = iph->daddr;
359 qp->q.fragments = NULL;
361 qp->peer = sysctl_ipfrag_max_dist ? inet_getpeer(iph->saddr, 1) : NULL;
363 /* Initialize a timer for this entry. */
364 init_timer(&qp->q.timer);
365 qp->q.timer.data = (unsigned long) qp; /* pointer to queue */
366 qp->q.timer.function = ip_expire; /* expire function */
367 spin_lock_init(&qp->q.lock);
368 atomic_set(&qp->q.refcnt, 1);
370 return ip_frag_intern(qp);
373 LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
377 /* Find the correct entry in the "incomplete datagrams" queue for
378 * this IP datagram, and create new one, if nothing is found.
380 static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
383 __be32 saddr = iph->saddr;
384 __be32 daddr = iph->daddr;
385 __u8 protocol = iph->protocol;
388 struct hlist_node *n;
390 read_lock(&ipfrag_lock);
391 hash = ipqhashfn(id, saddr, daddr, protocol);
392 hlist_for_each_entry(qp, n, &ipq_hash[hash], q.list) {
394 qp->saddr == saddr &&
395 qp->daddr == daddr &&
396 qp->protocol == protocol &&
398 atomic_inc(&qp->q.refcnt);
399 read_unlock(&ipfrag_lock);
403 read_unlock(&ipfrag_lock);
405 return ip_frag_create(iph, user);
408 /* Is the fragment too far ahead to be part of ipq? */
409 static inline int ip_frag_too_far(struct ipq *qp)
411 struct inet_peer *peer = qp->peer;
412 unsigned int max = sysctl_ipfrag_max_dist;
413 unsigned int start, end;
421 end = atomic_inc_return(&peer->rid);
424 rc = qp->q.fragments && (end - start) > max;
427 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
433 static int ip_frag_reinit(struct ipq *qp)
437 if (!mod_timer(&qp->q.timer, jiffies + sysctl_ipfrag_time)) {
438 atomic_inc(&qp->q.refcnt);
442 fp = qp->q.fragments;
444 struct sk_buff *xp = fp->next;
445 frag_kfree_skb(fp, NULL);
452 qp->q.fragments = NULL;
458 /* Add new segment to existing queue. */
459 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
461 struct sk_buff *prev, *next;
462 struct net_device *dev;
467 if (qp->q.last_in & COMPLETE)
470 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
471 unlikely(ip_frag_too_far(qp)) &&
472 unlikely(err = ip_frag_reinit(qp))) {
477 offset = ntohs(ip_hdr(skb)->frag_off);
478 flags = offset & ~IP_OFFSET;
480 offset <<= 3; /* offset is in 8-byte chunks */
481 ihl = ip_hdrlen(skb);
483 /* Determine the position of this fragment. */
484 end = offset + skb->len - ihl;
487 /* Is this the final fragment? */
488 if ((flags & IP_MF) == 0) {
489 /* If we already have some bits beyond end
490 * or have different end, the segment is corrrupted.
492 if (end < qp->q.len ||
493 ((qp->q.last_in & LAST_IN) && end != qp->q.len))
495 qp->q.last_in |= LAST_IN;
500 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
501 skb->ip_summed = CHECKSUM_NONE;
503 if (end > qp->q.len) {
504 /* Some bits beyond end -> corruption. */
505 if (qp->q.last_in & LAST_IN)
514 if (pskb_pull(skb, ihl) == NULL)
517 err = pskb_trim_rcsum(skb, end - offset);
521 /* Find out which fragments are in front and at the back of us
522 * in the chain of fragments so far. We must know where to put
523 * this fragment, right?
526 for (next = qp->q.fragments; next != NULL; next = next->next) {
527 if (FRAG_CB(next)->offset >= offset)
532 /* We found where to put this one. Check for overlap with
533 * preceding fragment, and, if needed, align things so that
534 * any overlaps are eliminated.
537 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
545 if (!pskb_pull(skb, i))
547 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
548 skb->ip_summed = CHECKSUM_NONE;
554 while (next && FRAG_CB(next)->offset < end) {
555 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
558 /* Eat head of the next overlapped fragment
559 * and leave the loop. The next ones cannot overlap.
561 if (!pskb_pull(next, i))
563 FRAG_CB(next)->offset += i;
565 if (next->ip_summed != CHECKSUM_UNNECESSARY)
566 next->ip_summed = CHECKSUM_NONE;
569 struct sk_buff *free_it = next;
571 /* Old fragment is completely overridden with
579 qp->q.fragments = next;
581 qp->q.meat -= free_it->len;
582 frag_kfree_skb(free_it, NULL);
586 FRAG_CB(skb)->offset = offset;
588 /* Insert this fragment in the chain of fragments. */
593 qp->q.fragments = skb;
597 qp->iif = dev->ifindex;
600 qp->q.stamp = skb->tstamp;
601 qp->q.meat += skb->len;
602 atomic_add(skb->truesize, &ip_frag_mem);
604 qp->q.last_in |= FIRST_IN;
606 if (qp->q.last_in == (FIRST_IN | LAST_IN) && qp->q.meat == qp->q.len)
607 return ip_frag_reasm(qp, prev, dev);
609 write_lock(&ipfrag_lock);
610 list_move_tail(&qp->q.lru_list, &ipq_lru_list);
611 write_unlock(&ipfrag_lock);
620 /* Build a new IP datagram from all its fragments. */
622 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
623 struct net_device *dev)
626 struct sk_buff *fp, *head = qp->q.fragments;
633 /* Make the one we just received the head. */
636 fp = skb_clone(head, GFP_ATOMIC);
641 fp->next = head->next;
644 skb_morph(head, qp->q.fragments);
645 head->next = qp->q.fragments->next;
647 kfree_skb(qp->q.fragments);
648 qp->q.fragments = head;
651 BUG_TRAP(head != NULL);
652 BUG_TRAP(FRAG_CB(head)->offset == 0);
654 /* Allocate a new buffer for the datagram. */
655 ihlen = ip_hdrlen(head);
656 len = ihlen + qp->q.len;
662 /* Head of list must not be cloned. */
664 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
667 /* If the first fragment is fragmented itself, we split
668 * it to two chunks: the first with data and paged part
669 * and the second, holding only fragments. */
670 if (skb_shinfo(head)->frag_list) {
671 struct sk_buff *clone;
674 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
676 clone->next = head->next;
678 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
679 skb_shinfo(head)->frag_list = NULL;
680 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
681 plen += skb_shinfo(head)->frags[i].size;
682 clone->len = clone->data_len = head->data_len - plen;
683 head->data_len -= clone->len;
684 head->len -= clone->len;
686 clone->ip_summed = head->ip_summed;
687 atomic_add(clone->truesize, &ip_frag_mem);
690 skb_shinfo(head)->frag_list = head->next;
691 skb_push(head, head->data - skb_network_header(head));
692 atomic_sub(head->truesize, &ip_frag_mem);
694 for (fp=head->next; fp; fp = fp->next) {
695 head->data_len += fp->len;
696 head->len += fp->len;
697 if (head->ip_summed != fp->ip_summed)
698 head->ip_summed = CHECKSUM_NONE;
699 else if (head->ip_summed == CHECKSUM_COMPLETE)
700 head->csum = csum_add(head->csum, fp->csum);
701 head->truesize += fp->truesize;
702 atomic_sub(fp->truesize, &ip_frag_mem);
707 head->tstamp = qp->q.stamp;
711 iph->tot_len = htons(len);
712 IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
713 qp->q.fragments = NULL;
717 LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
723 "Oversized IP packet from %d.%d.%d.%d.\n",
726 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
730 /* Process an incoming IP datagram fragment. */
731 int ip_defrag(struct sk_buff *skb, u32 user)
735 IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
737 /* Start by cleaning up the memory. */
738 if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
741 /* Lookup (or create) queue header */
742 if ((qp = ip_find(ip_hdr(skb), user)) != NULL) {
745 spin_lock(&qp->q.lock);
747 ret = ip_frag_queue(qp, skb);
749 spin_unlock(&qp->q.lock);
754 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
759 void __init ipfrag_init(void)
761 ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
762 (jiffies ^ (jiffies >> 6)));
764 init_timer(&ipfrag_secret_timer);
765 ipfrag_secret_timer.function = ipfrag_secret_rebuild;
766 ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
767 add_timer(&ipfrag_secret_timer);
770 EXPORT_SYMBOL(ip_defrag);