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.58.2.1 2002/01/12 07:53:15 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/types.h>
28 #include <linux/sched.h>
29 #include <linux/skbuff.h>
30 #include <linux/list.h>
32 #include <linux/icmp.h>
33 #include <linux/netdevice.h>
34 #include <linux/jhash.h>
35 #include <linux/random.h>
39 #include <net/checksum.h>
40 #include <linux/tcp.h>
41 #include <linux/udp.h>
42 #include <linux/inet.h>
43 #include <linux/netfilter_ipv4.h>
45 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
46 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
47 * as well. Or notify me, at least. --ANK
50 /* Fragment cache limits. We will commit 256K at one time. Should we
51 * cross that limit we will prune down to 192K. This should cope with
52 * even the most extreme cases without allowing an attacker to measurably
53 * harm machine performance.
55 int sysctl_ipfrag_high_thresh = 256*1024;
56 int sysctl_ipfrag_low_thresh = 192*1024;
58 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
59 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
61 int sysctl_ipfrag_time = IP_FRAG_TIME;
65 struct inet_skb_parm h;
69 #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
71 /* Describe an entry in the "incomplete datagrams" queue. */
73 struct ipq *next; /* linked list pointers */
74 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, int *work)
175 *work -= skb->truesize;
176 atomic_sub(skb->truesize, &ip_frag_mem);
180 static __inline__ void frag_free_queue(struct ipq *qp, int *work)
183 *work -= sizeof(struct ipq);
184 atomic_sub(sizeof(struct ipq), &ip_frag_mem);
188 static __inline__ struct ipq *frag_alloc_queue(void)
190 struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
194 atomic_add(sizeof(struct ipq), &ip_frag_mem);
199 /* Destruction primitives. */
201 /* Complete destruction of ipq. */
202 static void ip_frag_destroy(struct ipq *qp, int *work)
206 BUG_TRAP(qp->last_in&COMPLETE);
207 BUG_TRAP(del_timer(&qp->timer) == 0);
209 /* Release all fragment data. */
212 struct sk_buff *xp = fp->next;
214 frag_kfree_skb(fp, work);
218 /* Finally, release the queue descriptor itself. */
219 frag_free_queue(qp, work);
222 static __inline__ void ipq_put(struct ipq *ipq, int *work)
224 if (atomic_dec_and_test(&ipq->refcnt))
225 ip_frag_destroy(ipq, work);
228 /* Kill ipq entry. It is not destroyed immediately,
229 * because caller (and someone more) holds reference count.
231 static __inline__ void ipq_kill(struct ipq *ipq)
233 if (del_timer(&ipq->timer))
234 atomic_dec(&ipq->refcnt);
236 if (!(ipq->last_in & COMPLETE)) {
238 atomic_dec(&ipq->refcnt);
239 ipq->last_in |= COMPLETE;
243 /* Memory limiting on fragments. Evictor trashes the oldest
244 * fragment queue until we are back under the threshold.
246 static void ip_evictor(void)
249 struct list_head *tmp;
252 work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh;
257 read_lock(&ipfrag_lock);
258 if (list_empty(&ipq_lru_list)) {
259 read_unlock(&ipfrag_lock);
262 tmp = ipq_lru_list.next;
263 qp = list_entry(tmp, struct ipq, lru_list);
264 atomic_inc(&qp->refcnt);
265 read_unlock(&ipfrag_lock);
267 spin_lock(&qp->lock);
268 if (!(qp->last_in&COMPLETE))
270 spin_unlock(&qp->lock);
273 IP_INC_STATS_BH(IpReasmFails);
278 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
280 static void ip_expire(unsigned long arg)
282 struct ipq *qp = (struct ipq *) arg;
284 spin_lock(&qp->lock);
286 if (qp->last_in & COMPLETE)
291 IP_INC_STATS_BH(IpReasmTimeout);
292 IP_INC_STATS_BH(IpReasmFails);
294 if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) {
295 struct sk_buff *head = qp->fragments;
296 /* Send an ICMP "Fragment Reassembly Timeout" message. */
297 if ((head->dev = dev_get_by_index(qp->iif)) != NULL) {
298 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
303 spin_unlock(&qp->lock);
307 /* Creation primitives. */
309 static struct ipq *ip_frag_intern(unsigned int hash, struct ipq *qp_in)
313 write_lock(&ipfrag_lock);
315 /* With SMP race we have to recheck hash table, because
316 * such entry could be created on other cpu, while we
317 * promoted read lock to write lock.
319 for(qp = ipq_hash[hash]; qp; qp = qp->next) {
320 if(qp->id == qp_in->id &&
321 qp->saddr == qp_in->saddr &&
322 qp->daddr == qp_in->daddr &&
323 qp->protocol == qp_in->protocol &&
324 qp->user == qp_in->user) {
325 atomic_inc(&qp->refcnt);
326 write_unlock(&ipfrag_lock);
327 qp_in->last_in |= COMPLETE;
328 ipq_put(qp_in, NULL);
335 if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
336 atomic_inc(&qp->refcnt);
338 atomic_inc(&qp->refcnt);
339 if((qp->next = ipq_hash[hash]) != NULL)
340 qp->next->pprev = &qp->next;
342 qp->pprev = &ipq_hash[hash];
343 INIT_LIST_HEAD(&qp->lru_list);
344 list_add_tail(&qp->lru_list, &ipq_lru_list);
346 write_unlock(&ipfrag_lock);
350 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
351 static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph, u32 user)
355 if ((qp = frag_alloc_queue()) == NULL)
358 qp->protocol = iph->protocol;
361 qp->saddr = iph->saddr;
362 qp->daddr = iph->daddr;
366 qp->fragments = NULL;
369 /* Initialize a timer for this entry. */
370 init_timer(&qp->timer);
371 qp->timer.data = (unsigned long) qp; /* pointer to queue */
372 qp->timer.function = ip_expire; /* expire function */
373 qp->lock = SPIN_LOCK_UNLOCKED;
374 atomic_set(&qp->refcnt, 1);
376 return ip_frag_intern(hash, qp);
379 NETDEBUG(if (net_ratelimit()) printk(KERN_ERR "ip_frag_create: no memory left !\n"));
383 /* Find the correct entry in the "incomplete datagrams" queue for
384 * this IP datagram, and create new one, if nothing is found.
386 static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
389 __u32 saddr = iph->saddr;
390 __u32 daddr = iph->daddr;
391 __u8 protocol = iph->protocol;
392 unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
395 read_lock(&ipfrag_lock);
396 for(qp = ipq_hash[hash]; qp; qp = qp->next) {
398 qp->saddr == saddr &&
399 qp->daddr == daddr &&
400 qp->protocol == protocol &&
402 atomic_inc(&qp->refcnt);
403 read_unlock(&ipfrag_lock);
407 read_unlock(&ipfrag_lock);
409 return ip_frag_create(hash, iph, user);
412 /* Add new segment to existing queue. */
413 static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
415 struct sk_buff *prev, *next;
419 if (qp->last_in & COMPLETE)
422 offset = ntohs(skb->nh.iph->frag_off);
423 flags = offset & ~IP_OFFSET;
425 offset <<= 3; /* offset is in 8-byte chunks */
426 ihl = skb->nh.iph->ihl * 4;
428 /* Determine the position of this fragment. */
429 end = offset + skb->len - ihl;
431 /* Is this the final fragment? */
432 if ((flags & IP_MF) == 0) {
433 /* If we already have some bits beyond end
434 * or have different end, the segment is corrrupted.
437 ((qp->last_in & LAST_IN) && end != qp->len))
439 qp->last_in |= LAST_IN;
444 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
445 skb->ip_summed = CHECKSUM_NONE;
448 /* Some bits beyond end -> corruption. */
449 if (qp->last_in & LAST_IN)
457 if (pskb_pull(skb, ihl) == NULL)
459 if (pskb_trim(skb, end-offset))
462 /* Find out which fragments are in front and at the back of us
463 * in the chain of fragments so far. We must know where to put
464 * this fragment, right?
467 for(next = qp->fragments; next != NULL; next = next->next) {
468 if (FRAG_CB(next)->offset >= offset)
473 /* We found where to put this one. Check for overlap with
474 * preceding fragment, and, if needed, align things so that
475 * any overlaps are eliminated.
478 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
484 if (!pskb_pull(skb, i))
486 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
487 skb->ip_summed = CHECKSUM_NONE;
491 while (next && FRAG_CB(next)->offset < end) {
492 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
495 /* Eat head of the next overlapped fragment
496 * and leave the loop. The next ones cannot overlap.
498 if (!pskb_pull(next, i))
500 FRAG_CB(next)->offset += i;
502 if (next->ip_summed != CHECKSUM_UNNECESSARY)
503 next->ip_summed = CHECKSUM_NONE;
506 struct sk_buff *free_it = next;
508 /* Old fragmnet is completely overridden with
516 qp->fragments = next;
518 qp->meat -= free_it->len;
519 frag_kfree_skb(free_it, NULL);
523 FRAG_CB(skb)->offset = offset;
525 /* Insert this fragment in the chain of fragments. */
533 qp->iif = skb->dev->ifindex;
535 qp->stamp = skb->stamp;
536 qp->meat += skb->len;
537 atomic_add(skb->truesize, &ip_frag_mem);
539 qp->last_in |= FIRST_IN;
541 write_lock(&ipfrag_lock);
542 list_move_tail(&qp->lru_list, &ipq_lru_list);
543 write_unlock(&ipfrag_lock);
552 /* Build a new IP datagram from all its fragments. */
554 static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
557 struct sk_buff *fp, *head = qp->fragments;
563 BUG_TRAP(head != NULL);
564 BUG_TRAP(FRAG_CB(head)->offset == 0);
566 /* Allocate a new buffer for the datagram. */
567 ihlen = head->nh.iph->ihl*4;
568 len = ihlen + qp->len;
573 /* Head of list must not be cloned. */
574 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
577 /* If the first fragment is fragmented itself, we split
578 * it to two chunks: the first with data and paged part
579 * and the second, holding only fragments. */
580 if (skb_shinfo(head)->frag_list) {
581 struct sk_buff *clone;
584 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
586 clone->next = head->next;
588 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
589 skb_shinfo(head)->frag_list = NULL;
590 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
591 plen += skb_shinfo(head)->frags[i].size;
592 clone->len = clone->data_len = head->data_len - plen;
593 head->data_len -= clone->len;
594 head->len -= clone->len;
596 clone->ip_summed = head->ip_summed;
597 atomic_add(clone->truesize, &ip_frag_mem);
600 skb_shinfo(head)->frag_list = head->next;
601 skb_push(head, head->data - head->nh.raw);
602 atomic_sub(head->truesize, &ip_frag_mem);
604 for (fp=head->next; fp; fp = fp->next) {
605 head->data_len += fp->len;
606 head->len += fp->len;
607 if (head->ip_summed != fp->ip_summed)
608 head->ip_summed = CHECKSUM_NONE;
609 else if (head->ip_summed == CHECKSUM_HW)
610 head->csum = csum_add(head->csum, fp->csum);
611 head->truesize += fp->truesize;
612 atomic_sub(fp->truesize, &ip_frag_mem);
617 head->stamp = qp->stamp;
621 iph->tot_len = htons(len);
622 IP_INC_STATS_BH(IpReasmOKs);
623 qp->fragments = NULL;
627 NETDEBUG(if (net_ratelimit())
629 "IP: queue_glue: no memory for gluing queue %p\n",
635 "Oversized IP packet from %d.%d.%d.%d.\n",
638 IP_INC_STATS_BH(IpReasmFails);
642 /* Process an incoming IP datagram fragment. */
643 struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user)
645 struct iphdr *iph = skb->nh.iph;
647 struct net_device *dev;
649 IP_INC_STATS_BH(IpReasmReqds);
651 /* Start by cleaning up the memory. */
652 if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
657 /* Lookup (or create) queue header */
658 if ((qp = ip_find(iph, user)) != NULL) {
659 struct sk_buff *ret = NULL;
661 spin_lock(&qp->lock);
663 ip_frag_queue(qp, skb);
665 if (qp->last_in == (FIRST_IN|LAST_IN) &&
667 ret = ip_frag_reasm(qp, dev);
669 spin_unlock(&qp->lock);
674 IP_INC_STATS_BH(IpReasmFails);
679 void ipfrag_init(void)
681 ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
682 (jiffies ^ (jiffies >> 6)));
684 init_timer(&ipfrag_secret_timer);
685 ipfrag_secret_timer.function = ipfrag_secret_rebuild;
686 ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
687 add_timer(&ipfrag_secret_timer);