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/config.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 <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/inet.h>
44 #include <linux/netfilter_ipv4.h>
46 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
47 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
48 * as well. Or notify me, at least. --ANK
51 /* Fragment cache limits. We will commit 256K at one time. Should we
52 * cross that limit we will prune down to 192K. This should cope with
53 * even the most extreme cases without allowing an attacker to measurably
54 * harm machine performance.
56 int sysctl_ipfrag_high_thresh = 256*1024;
57 int sysctl_ipfrag_low_thresh = 192*1024;
59 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
60 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
62 int sysctl_ipfrag_time = IP_FRAG_TIME;
66 struct inet_skb_parm h;
70 #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
72 /* Describe an entry in the "incomplete datagrams" queue. */
74 struct ipq *next; /* linked list pointers */
75 struct list_head lru_list; /* lru list member */
85 struct sk_buff *fragments; /* linked list of received fragments */
86 int len; /* total length of original datagram */
90 struct timer_list timer; /* when will this queue expire? */
100 /* Per-bucket lock is easy to add now. */
101 static struct ipq *ipq_hash[IPQ_HASHSZ];
102 static rwlock_t ipfrag_lock = RW_LOCK_UNLOCKED;
103 static u32 ipfrag_hash_rnd;
104 static LIST_HEAD(ipq_lru_list);
105 int ip_frag_nqueues = 0;
107 static __inline__ void __ipq_unlink(struct ipq *qp)
110 qp->next->pprev = qp->pprev;
111 *qp->pprev = qp->next;
112 list_del(&qp->lru_list);
116 static __inline__ void ipq_unlink(struct ipq *ipq)
118 write_lock(&ipfrag_lock);
120 write_unlock(&ipfrag_lock);
123 static unsigned int ipqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot)
125 return jhash_3words((u32)id << 16 | prot, saddr, daddr,
126 ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
129 static struct timer_list ipfrag_secret_timer;
130 int sysctl_ipfrag_secret_interval = 10 * 60 * HZ;
132 static void ipfrag_secret_rebuild(unsigned long dummy)
134 unsigned long now = jiffies;
137 write_lock(&ipfrag_lock);
138 get_random_bytes(&ipfrag_hash_rnd, sizeof(u32));
139 for (i = 0; i < IPQ_HASHSZ; i++) {
144 struct ipq *next = q->next;
145 unsigned int hval = ipqhashfn(q->id, q->saddr,
146 q->daddr, q->protocol);
151 q->next->pprev = q->pprev;
154 /* Relink to new hash chain. */
155 if ((q->next = ipq_hash[hval]) != NULL)
156 q->next->pprev = &q->next;
158 q->pprev = &ipq_hash[hval];
164 write_unlock(&ipfrag_lock);
166 mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval);
169 atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */
171 /* Memory Tracking Functions. */
172 static __inline__ void frag_kfree_skb(struct sk_buff *skb)
174 atomic_sub(skb->truesize, &ip_frag_mem);
178 static __inline__ void frag_free_queue(struct ipq *qp)
180 atomic_sub(sizeof(struct ipq), &ip_frag_mem);
184 static __inline__ struct ipq *frag_alloc_queue(void)
186 struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
190 atomic_add(sizeof(struct ipq), &ip_frag_mem);
195 /* Destruction primitives. */
197 /* Complete destruction of ipq. */
198 static void ip_frag_destroy(struct ipq *qp)
202 BUG_TRAP(qp->last_in&COMPLETE);
203 BUG_TRAP(del_timer(&qp->timer) == 0);
205 /* Release all fragment data. */
208 struct sk_buff *xp = fp->next;
214 /* Finally, release the queue descriptor itself. */
218 static __inline__ void ipq_put(struct ipq *ipq)
220 if (atomic_dec_and_test(&ipq->refcnt))
221 ip_frag_destroy(ipq);
224 /* Kill ipq entry. It is not destroyed immediately,
225 * because caller (and someone more) holds reference count.
227 static void ipq_kill(struct ipq *ipq)
229 if (del_timer(&ipq->timer))
230 atomic_dec(&ipq->refcnt);
232 if (!(ipq->last_in & COMPLETE)) {
234 atomic_dec(&ipq->refcnt);
235 ipq->last_in |= COMPLETE;
239 /* Memory limiting on fragments. Evictor trashes the oldest
240 * fragment queue until we are back under the low threshold.
242 static void ip_evictor(void)
245 struct list_head *tmp;
248 if (atomic_read(&ip_frag_mem) <= sysctl_ipfrag_low_thresh)
250 read_lock(&ipfrag_lock);
251 if (list_empty(&ipq_lru_list)) {
252 read_unlock(&ipfrag_lock);
255 tmp = ipq_lru_list.next;
256 qp = list_entry(tmp, struct ipq, lru_list);
257 atomic_inc(&qp->refcnt);
258 read_unlock(&ipfrag_lock);
260 spin_lock(&qp->lock);
261 if (!(qp->last_in&COMPLETE))
263 spin_unlock(&qp->lock);
266 IP_INC_STATS_BH(ReasmFails);
271 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
273 static void ip_expire(unsigned long arg)
275 struct ipq *qp = (struct ipq *) arg;
277 spin_lock(&qp->lock);
279 if (qp->last_in & COMPLETE)
284 IP_INC_STATS_BH(ReasmTimeout);
285 IP_INC_STATS_BH(ReasmFails);
287 if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) {
288 struct sk_buff *head = qp->fragments;
289 /* Send an ICMP "Fragment Reassembly Timeout" message. */
290 if ((head->dev = dev_get_by_index(qp->iif)) != NULL) {
291 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
296 spin_unlock(&qp->lock);
300 /* Creation primitives. */
302 static struct ipq *ip_frag_intern(unsigned int hash, struct ipq *qp_in)
306 write_lock(&ipfrag_lock);
308 /* With SMP race we have to recheck hash table, because
309 * such entry could be created on other cpu, while we
310 * promoted read lock to write lock.
312 for(qp = ipq_hash[hash]; qp; qp = qp->next) {
313 if(qp->id == qp_in->id &&
314 qp->saddr == qp_in->saddr &&
315 qp->daddr == qp_in->daddr &&
316 qp->protocol == qp_in->protocol) {
317 atomic_inc(&qp->refcnt);
318 write_unlock(&ipfrag_lock);
319 qp_in->last_in |= COMPLETE;
327 if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
328 atomic_inc(&qp->refcnt);
330 atomic_inc(&qp->refcnt);
331 if((qp->next = ipq_hash[hash]) != NULL)
332 qp->next->pprev = &qp->next;
334 qp->pprev = &ipq_hash[hash];
335 INIT_LIST_HEAD(&qp->lru_list);
336 list_add_tail(&qp->lru_list, &ipq_lru_list);
338 write_unlock(&ipfrag_lock);
342 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
343 static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph)
347 if ((qp = frag_alloc_queue()) == NULL)
350 qp->protocol = iph->protocol;
353 qp->saddr = iph->saddr;
354 qp->daddr = iph->daddr;
357 qp->fragments = NULL;
360 /* Initialize a timer for this entry. */
361 init_timer(&qp->timer);
362 qp->timer.data = (unsigned long) qp; /* pointer to queue */
363 qp->timer.function = ip_expire; /* expire function */
364 qp->lock = SPIN_LOCK_UNLOCKED;
365 atomic_set(&qp->refcnt, 1);
367 return ip_frag_intern(hash, qp);
370 NETDEBUG(if (net_ratelimit()) printk(KERN_ERR "ip_frag_create: no memory left !\n"));
374 /* Find the correct entry in the "incomplete datagrams" queue for
375 * this IP datagram, and create new one, if nothing is found.
377 static inline struct ipq *ip_find(struct iphdr *iph)
380 __u32 saddr = iph->saddr;
381 __u32 daddr = iph->daddr;
382 __u8 protocol = iph->protocol;
383 unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
386 read_lock(&ipfrag_lock);
387 for(qp = ipq_hash[hash]; qp; qp = qp->next) {
389 qp->saddr == saddr &&
390 qp->daddr == daddr &&
391 qp->protocol == protocol) {
392 atomic_inc(&qp->refcnt);
393 read_unlock(&ipfrag_lock);
397 read_unlock(&ipfrag_lock);
399 return ip_frag_create(hash, iph);
402 /* Add new segment to existing queue. */
403 static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
405 struct sk_buff *prev, *next;
409 if (qp->last_in & COMPLETE)
412 offset = ntohs(skb->nh.iph->frag_off);
413 flags = offset & ~IP_OFFSET;
415 offset <<= 3; /* offset is in 8-byte chunks */
416 ihl = skb->nh.iph->ihl * 4;
418 /* Determine the position of this fragment. */
419 end = offset + skb->len - ihl;
421 /* Is this the final fragment? */
422 if ((flags & IP_MF) == 0) {
423 /* If we already have some bits beyond end
424 * or have different end, the segment is corrrupted.
427 ((qp->last_in & LAST_IN) && end != qp->len))
429 qp->last_in |= LAST_IN;
434 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
435 skb->ip_summed = CHECKSUM_NONE;
438 /* Some bits beyond end -> corruption. */
439 if (qp->last_in & LAST_IN)
447 if (pskb_pull(skb, ihl) == NULL)
449 if (pskb_trim(skb, end-offset))
452 /* Find out which fragments are in front and at the back of us
453 * in the chain of fragments so far. We must know where to put
454 * this fragment, right?
457 for(next = qp->fragments; next != NULL; next = next->next) {
458 if (FRAG_CB(next)->offset >= offset)
463 /* We found where to put this one. Check for overlap with
464 * preceding fragment, and, if needed, align things so that
465 * any overlaps are eliminated.
468 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
474 if (!pskb_pull(skb, i))
476 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
477 skb->ip_summed = CHECKSUM_NONE;
481 while (next && FRAG_CB(next)->offset < end) {
482 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
485 /* Eat head of the next overlapped fragment
486 * and leave the loop. The next ones cannot overlap.
488 if (!pskb_pull(next, i))
490 FRAG_CB(next)->offset += i;
492 if (next->ip_summed != CHECKSUM_UNNECESSARY)
493 next->ip_summed = CHECKSUM_NONE;
496 struct sk_buff *free_it = next;
498 /* Old fragmnet is completely overridden with
506 qp->fragments = next;
508 qp->meat -= free_it->len;
509 frag_kfree_skb(free_it);
513 FRAG_CB(skb)->offset = offset;
515 /* Insert this fragment in the chain of fragments. */
523 qp->iif = skb->dev->ifindex;
525 qp->stamp = skb->stamp;
526 qp->meat += skb->len;
527 atomic_add(skb->truesize, &ip_frag_mem);
529 qp->last_in |= FIRST_IN;
531 write_lock(&ipfrag_lock);
532 list_move_tail(&qp->lru_list, &ipq_lru_list);
533 write_unlock(&ipfrag_lock);
542 /* Build a new IP datagram from all its fragments. */
544 static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
547 struct sk_buff *fp, *head = qp->fragments;
553 BUG_TRAP(head != NULL);
554 BUG_TRAP(FRAG_CB(head)->offset == 0);
556 /* Allocate a new buffer for the datagram. */
557 ihlen = head->nh.iph->ihl*4;
558 len = ihlen + qp->len;
563 /* Head of list must not be cloned. */
564 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
567 /* If the first fragment is fragmented itself, we split
568 * it to two chunks: the first with data and paged part
569 * and the second, holding only fragments. */
570 if (skb_shinfo(head)->frag_list) {
571 struct sk_buff *clone;
574 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
576 clone->next = head->next;
578 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
579 skb_shinfo(head)->frag_list = NULL;
580 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
581 plen += skb_shinfo(head)->frags[i].size;
582 clone->len = clone->data_len = head->data_len - plen;
583 head->data_len -= clone->len;
584 head->len -= clone->len;
586 clone->ip_summed = head->ip_summed;
587 atomic_add(clone->truesize, &ip_frag_mem);
590 skb_shinfo(head)->frag_list = head->next;
591 skb_push(head, head->data - head->nh.raw);
592 atomic_sub(head->truesize, &ip_frag_mem);
594 for (fp=head->next; fp; fp = fp->next) {
595 head->data_len += fp->len;
596 head->len += fp->len;
597 if (head->ip_summed != fp->ip_summed)
598 head->ip_summed = CHECKSUM_NONE;
599 else if (head->ip_summed == CHECKSUM_HW)
600 head->csum = csum_add(head->csum, fp->csum);
601 head->truesize += fp->truesize;
602 atomic_sub(fp->truesize, &ip_frag_mem);
607 head->stamp = qp->stamp;
611 iph->tot_len = htons(len);
612 IP_INC_STATS_BH(ReasmOKs);
613 qp->fragments = NULL;
617 NETDEBUG(if (net_ratelimit())
619 "IP: queue_glue: no memory for gluing queue %p\n",
625 "Oversized IP packet from %d.%d.%d.%d.\n",
628 IP_INC_STATS_BH(ReasmFails);
632 /* Process an incoming IP datagram fragment. */
633 struct sk_buff *ip_defrag(struct sk_buff *skb)
635 struct iphdr *iph = skb->nh.iph;
637 struct net_device *dev;
639 IP_INC_STATS_BH(ReasmReqds);
641 /* Start by cleaning up the memory. */
642 if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
647 /* Lookup (or create) queue header */
648 if ((qp = ip_find(iph)) != NULL) {
649 struct sk_buff *ret = NULL;
651 spin_lock(&qp->lock);
653 ip_frag_queue(qp, skb);
655 if (qp->last_in == (FIRST_IN|LAST_IN) &&
657 ret = ip_frag_reasm(qp, dev);
659 spin_unlock(&qp->lock);
664 IP_INC_STATS_BH(ReasmFails);
669 void ipfrag_init(void)
671 ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
672 (jiffies ^ (jiffies >> 6)));
674 init_timer(&ipfrag_secret_timer);
675 ipfrag_secret_timer.function = ipfrag_secret_rebuild;
676 ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
677 add_timer(&ipfrag_secret_timer);
680 EXPORT_SYMBOL(ip_defrag);