~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/net/ipv4/ip_fragment.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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.
  5  *
  6  *              The IP fragmentation functionality.
  7  *              
  8  * Version:     $Id: ip_fragment.c,v 1.53 2000/12/08 17:15:53 davem Exp $
  9  *
 10  * Authors:     Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
 11  *              Alan Cox <Alan.Cox@linux.org>
 12  *
 13  * Fixes:
 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  */
 23 
 24 #include <linux/config.h>
 25 #include <linux/types.h>
 26 #include <linux/mm.h>
 27 #include <linux/sched.h>
 28 #include <linux/skbuff.h>
 29 #include <linux/ip.h>
 30 #include <linux/icmp.h>
 31 #include <linux/netdevice.h>
 32 #include <net/sock.h>
 33 #include <net/ip.h>
 34 #include <net/icmp.h>
 35 #include <net/checksum.h>
 36 #include <linux/tcp.h>
 37 #include <linux/udp.h>
 38 #include <linux/inet.h>
 39 #include <linux/netfilter_ipv4.h>
 40 
 41 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
 42  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
 43  * as well. Or notify me, at least. --ANK
 44  */
 45 
 46 /* Fragment cache limits. We will commit 256K at one time. Should we
 47  * cross that limit we will prune down to 192K. This should cope with
 48  * even the most extreme cases without allowing an attacker to measurably
 49  * harm machine performance.
 50  */
 51 int sysctl_ipfrag_high_thresh = 256*1024;
 52 int sysctl_ipfrag_low_thresh = 192*1024;
 53 
 54 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
 55  * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
 56  */
 57 int sysctl_ipfrag_time = IP_FRAG_TIME;
 58 
 59 struct ipfrag_skb_cb
 60 {
 61         struct inet_skb_parm    h;
 62         int                     offset;
 63 };
 64 
 65 #define FRAG_CB(skb)    ((struct ipfrag_skb_cb*)((skb)->cb))
 66 
 67 /* Describe an entry in the "incomplete datagrams" queue. */
 68 struct ipq {
 69         struct ipq      *next;          /* linked list pointers                 */
 70         u32             saddr;
 71         u32             daddr;
 72         u16             id;
 73         u8              protocol;
 74         u8              last_in;
 75 #define COMPLETE                4
 76 #define FIRST_IN                2
 77 #define LAST_IN                 1
 78 
 79         struct sk_buff  *fragments;     /* linked list of received fragments    */
 80         int             len;            /* total length of original datagram    */
 81         int             meat;
 82         spinlock_t      lock;
 83         atomic_t        refcnt;
 84         struct timer_list timer;        /* when will this queue expire?         */
 85         struct ipq      **pprev;
 86         int             iif;            /* Device index - for icmp replies      */
 87 };
 88 
 89 /* Hash table. */
 90 
 91 #define IPQ_HASHSZ      64
 92 
 93 /* Per-bucket lock is easy to add now. */
 94 static struct ipq *ipq_hash[IPQ_HASHSZ];
 95 static rwlock_t ipfrag_lock = RW_LOCK_UNLOCKED;
 96 int ip_frag_nqueues = 0;
 97 
 98 static __inline__ void __ipq_unlink(struct ipq *qp)
 99 {
100         if(qp->next)
101                 qp->next->pprev = qp->pprev;
102         *qp->pprev = qp->next;
103         ip_frag_nqueues--;
104 }
105 
106 static __inline__ void ipq_unlink(struct ipq *ipq)
107 {
108         write_lock(&ipfrag_lock);
109         __ipq_unlink(ipq);
110         write_unlock(&ipfrag_lock);
111 }
112 
113 /*
114  * Was: ((((id) >> 1) ^ (saddr) ^ (daddr) ^ (prot)) & (IPQ_HASHSZ - 1))
115  *
116  * I see, I see evil hand of bigendian mafia. On Intel all the packets hit
117  * one hash bucket with this hash function. 8)
118  */
119 static __inline__ unsigned int ipqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot)
120 {
121         unsigned int h = saddr ^ daddr;
122 
123         h ^= (h>>16)^id;
124         h ^= (h>>8)^prot;
125         return h & (IPQ_HASHSZ - 1);
126 }
127 
128 
129 atomic_t ip_frag_mem = ATOMIC_INIT(0);  /* Memory used for fragments */
130 
131 /* Memory Tracking Functions. */
132 extern __inline__ void frag_kfree_skb(struct sk_buff *skb)
133 {
134         atomic_sub(skb->truesize, &ip_frag_mem);
135         kfree_skb(skb);
136 }
137 
138 extern __inline__ void frag_free_queue(struct ipq *qp)
139 {
140         atomic_sub(sizeof(struct ipq), &ip_frag_mem);
141         kfree(qp);
142 }
143 
144 extern __inline__ struct ipq *frag_alloc_queue(void)
145 {
146         struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
147 
148         if(!qp)
149                 return NULL;
150         atomic_add(sizeof(struct ipq), &ip_frag_mem);
151         return qp;
152 }
153 
154 
155 /* Destruction primitives. */
156 
157 /* Complete destruction of ipq. */
158 static void ip_frag_destroy(struct ipq *qp)
159 {
160         struct sk_buff *fp;
161 
162         BUG_TRAP(qp->last_in&COMPLETE);
163         BUG_TRAP(del_timer(&qp->timer) == 0);
164 
165         /* Release all fragment data. */
166         fp = qp->fragments;
167         while (fp) {
168                 struct sk_buff *xp = fp->next;
169 
170                 frag_kfree_skb(fp);
171                 fp = xp;
172         }
173 
174         /* Finally, release the queue descriptor itself. */
175         frag_free_queue(qp);
176 }
177 
178 static __inline__ void ipq_put(struct ipq *ipq)
179 {
180         if (atomic_dec_and_test(&ipq->refcnt))
181                 ip_frag_destroy(ipq);
182 }
183 
184 /* Kill ipq entry. It is not destroyed immediately,
185  * because caller (and someone more) holds reference count.
186  */
187 static __inline__ void ipq_kill(struct ipq *ipq)
188 {
189         if (del_timer(&ipq->timer))
190                 atomic_dec(&ipq->refcnt);
191 
192         if (!(ipq->last_in & COMPLETE)) {
193                 ipq_unlink(ipq);
194                 atomic_dec(&ipq->refcnt);
195                 ipq->last_in |= COMPLETE;
196         }
197 }
198 
199 /* Memory limiting on fragments.  Evictor trashes the oldest 
200  * fragment queue until we are back under the low threshold.
201  */
202 static void ip_evictor(void)
203 {
204         int i, progress;
205 
206         do {
207                 if (atomic_read(&ip_frag_mem) <= sysctl_ipfrag_low_thresh)
208                         return;
209                 progress = 0;
210                 /* FIXME: Make LRU queue of frag heads. -DaveM */
211                 for (i = 0; i < IPQ_HASHSZ; i++) {
212                         struct ipq *qp;
213                         if (ipq_hash[i] == NULL)
214                                 continue;
215 
216                         write_lock(&ipfrag_lock);
217                         if ((qp = ipq_hash[i]) != NULL) {
218                                 /* find the oldest queue for this hash bucket */
219                                 while (qp->next)
220                                         qp = qp->next;
221                                 __ipq_unlink(qp);
222                                 write_unlock(&ipfrag_lock);
223 
224                                 spin_lock(&qp->lock);
225                                 if (del_timer(&qp->timer))
226                                         atomic_dec(&qp->refcnt);
227                                 qp->last_in |= COMPLETE;
228                                 spin_unlock(&qp->lock);
229 
230                                 ipq_put(qp);
231                                 IP_INC_STATS_BH(IpReasmFails);
232                                 progress = 1;
233                                 continue;
234                         }
235                         write_unlock(&ipfrag_lock);
236                 }
237         } while (progress);
238 }
239 
240 /*
241  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
242  */
243 static void ip_expire(unsigned long arg)
244 {
245         struct ipq *qp = (struct ipq *) arg;
246 
247         spin_lock(&qp->lock);
248 
249         if (qp->last_in & COMPLETE)
250                 goto out;
251 
252         ipq_kill(qp);
253 
254         IP_INC_STATS_BH(IpReasmTimeout);
255         IP_INC_STATS_BH(IpReasmFails);
256 
257         if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) {
258                 struct sk_buff *head = qp->fragments;
259 
260                 /* Send an ICMP "Fragment Reassembly Timeout" message. */
261                 if ((head->dev = dev_get_by_index(qp->iif)) != NULL) {
262                         icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
263                         dev_put(head->dev);
264                 }
265         }
266 out:
267         spin_unlock(&qp->lock);
268         ipq_put(qp);
269 }
270 
271 /* Creation primitives. */
272 
273 static struct ipq *ip_frag_intern(unsigned int hash, struct ipq *qp_in)
274 {
275         struct ipq *qp;
276 
277         write_lock(&ipfrag_lock);
278 #ifdef CONFIG_SMP
279         /* With SMP race we have to recheck hash table, because
280          * such entry could be created on other cpu, while we
281          * promoted read lock to write lock.
282          */
283         for(qp = ipq_hash[hash]; qp; qp = qp->next) {
284                 if(qp->id == qp_in->id          &&
285                    qp->saddr == qp_in->saddr    &&
286                    qp->daddr == qp_in->daddr    &&
287                    qp->protocol == qp_in->protocol) {
288                         atomic_inc(&qp->refcnt);
289                         write_unlock(&ipfrag_lock);
290                         qp_in->last_in |= COMPLETE;
291                         ipq_put(qp_in);
292                         return qp;
293                 }
294         }
295 #endif
296         qp = qp_in;
297 
298         if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time))
299                 atomic_inc(&qp->refcnt);
300 
301         atomic_inc(&qp->refcnt);
302         if((qp->next = ipq_hash[hash]) != NULL)
303                 qp->next->pprev = &qp->next;
304         ipq_hash[hash] = qp;
305         qp->pprev = &ipq_hash[hash];
306         ip_frag_nqueues++;
307         write_unlock(&ipfrag_lock);
308         return qp;
309 }
310 
311 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
312 static struct ipq *ip_frag_create(unsigned hash, struct iphdr *iph)
313 {
314         struct ipq *qp;
315 
316         if ((qp = frag_alloc_queue()) == NULL)
317                 goto out_nomem;
318 
319         qp->protocol = iph->protocol;
320         qp->last_in = 0;
321         qp->id = iph->id;
322         qp->saddr = iph->saddr;
323         qp->daddr = iph->daddr;
324         qp->len = 0;
325         qp->meat = 0;
326         qp->fragments = NULL;
327         qp->iif = 0;
328 
329         /* Initialize a timer for this entry. */
330         init_timer(&qp->timer);
331         qp->timer.data = (unsigned long) qp;    /* pointer to queue     */
332         qp->timer.function = ip_expire;         /* expire function      */
333         qp->lock = SPIN_LOCK_UNLOCKED;
334         atomic_set(&qp->refcnt, 1);
335 
336         return ip_frag_intern(hash, qp);
337 
338 out_nomem:
339         NETDEBUG(printk(KERN_ERR "ip_frag_create: no memory left !\n"));
340         return NULL;
341 }
342 
343 /* Find the correct entry in the "incomplete datagrams" queue for
344  * this IP datagram, and create new one, if nothing is found.
345  */
346 static inline struct ipq *ip_find(struct iphdr *iph)
347 {
348         __u16 id = iph->id;
349         __u32 saddr = iph->saddr;
350         __u32 daddr = iph->daddr;
351         __u8 protocol = iph->protocol;
352         unsigned int hash = ipqhashfn(id, saddr, daddr, protocol);
353         struct ipq *qp;
354 
355         read_lock(&ipfrag_lock);
356         for(qp = ipq_hash[hash]; qp; qp = qp->next) {
357                 if(qp->id == id         &&
358                    qp->saddr == saddr   &&
359                    qp->daddr == daddr   &&
360                    qp->protocol == protocol) {
361                         atomic_inc(&qp->refcnt);
362                         read_unlock(&ipfrag_lock);
363                         return qp;
364                 }
365         }
366         read_unlock(&ipfrag_lock);
367 
368         return ip_frag_create(hash, iph);
369 }
370 
371 /* Add new segment to existing queue. */
372 static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
373 {
374         struct iphdr *iph = skb->nh.iph;
375         struct sk_buff *prev, *next;
376         int flags, offset;
377         int ihl, end;
378 
379         if (qp->last_in & COMPLETE)
380                 goto err;
381 
382         offset = ntohs(iph->frag_off);
383         flags = offset & ~IP_OFFSET;
384         offset &= IP_OFFSET;
385         offset <<= 3;           /* offset is in 8-byte chunks */
386         ihl = iph->ihl * 4;
387 
388         /* Determine the position of this fragment. */
389         end = offset + (ntohs(iph->tot_len) - ihl);
390 
391         /* Is this the final fragment? */
392         if ((flags & IP_MF) == 0) {
393                 /* If we already have some bits beyond end
394                  * or have different end, the segment is corrrupted.
395                  */
396                 if (end < qp->len ||
397                     ((qp->last_in & LAST_IN) && end != qp->len))
398                         goto err;
399                 qp->last_in |= LAST_IN;
400                 qp->len = end;
401         } else {
402                 if (end&7) {
403                         end &= ~7;
404                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
405                                 skb->ip_summed = CHECKSUM_NONE;
406                 }
407                 if (end > qp->len) {
408                         /* Some bits beyond end -> corruption. */
409                         if (qp->last_in & LAST_IN)
410                                 goto err;
411                         qp->len = end;
412                 }
413         }
414         if (end == offset)
415                 goto err;
416 
417         /* Point into the IP datagram 'data' part. */
418         skb_pull(skb, (skb->nh.raw+ihl) - skb->data);
419         skb_trim(skb, end - offset);
420 
421         /* Find out which fragments are in front and at the back of us
422          * in the chain of fragments so far.  We must know where to put
423          * this fragment, right?
424          */
425         prev = NULL;
426         for(next = qp->fragments; next != NULL; next = next->next) {
427                 if (FRAG_CB(next)->offset >= offset)
428                         break;  /* bingo! */
429                 prev = next;
430         }
431 
432         /* We found where to put this one.  Check for overlap with
433          * preceding fragment, and, if needed, align things so that
434          * any overlaps are eliminated.
435          */
436         if (prev) {
437                 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
438 
439                 if (i > 0) {
440                         offset += i;
441                         if (end <= offset)
442                                 goto err;
443                         skb_pull(skb, i);
444                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
445                                 skb->ip_summed = CHECKSUM_NONE;
446                 }
447         }
448 
449         while (next && FRAG_CB(next)->offset < end) {
450                 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
451 
452                 if (i < next->len) {
453                         /* Eat head of the next overlapped fragment
454                          * and leave the loop. The next ones cannot overlap.
455                          */
456                         FRAG_CB(next)->offset += i;
457                         skb_pull(next, i);
458                         qp->meat -= i;
459                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
460                                 next->ip_summed = CHECKSUM_NONE;
461                         break;
462                 } else {
463                         struct sk_buff *free_it = next;
464 
465                         /* Old fragmnet is completely overridden with
466                          * new one drop it.
467                          */
468                         next = next->next;
469 
470                         if (prev)
471                                 prev->next = next;
472                         else
473                                 qp->fragments = next;
474 
475                         qp->meat -= free_it->len;
476                         frag_kfree_skb(free_it);
477                 }
478         }
479 
480         FRAG_CB(skb)->offset = offset;
481 
482         /* Insert this fragment in the chain of fragments. */
483         skb->next = next;
484         if (prev)
485                 prev->next = skb;
486         else
487                 qp->fragments = skb;
488 
489         if (skb->dev)
490                 qp->iif = skb->dev->ifindex;
491         skb->dev = NULL;
492         qp->meat += skb->len;
493         atomic_add(skb->truesize, &ip_frag_mem);
494         if (offset == 0)
495                 qp->last_in |= FIRST_IN;
496 
497         return;
498 
499 err:
500         kfree_skb(skb);
501 }
502 
503 
504 /* Build a new IP datagram from all its fragments.
505  *
506  * FIXME: We copy here because we lack an effective way of handling lists
507  * of bits on input. Until the new skb data handling is in I'm not going
508  * to touch this with a bargepole. 
509  */
510 static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev)
511 {
512         struct sk_buff *skb;
513         struct iphdr *iph;
514         struct sk_buff *fp, *head = qp->fragments;
515         int len;
516         int ihlen;
517 
518         ipq_kill(qp);
519 
520         BUG_TRAP(head != NULL);
521         BUG_TRAP(FRAG_CB(head)->offset == 0);
522 
523         /* Allocate a new buffer for the datagram. */
524         ihlen = head->nh.iph->ihl*4;
525         len = ihlen + qp->len;
526 
527         if(len > 65535)
528                 goto out_oversize;
529 
530         skb = dev_alloc_skb(len);
531         if (!skb)
532                 goto out_nomem;
533 
534         /* Fill in the basic details. */
535         skb->mac.raw = skb->data;
536         skb->nh.raw = skb->data;
537         FRAG_CB(skb)->h = FRAG_CB(head)->h;
538         skb->ip_summed = head->ip_summed;
539         skb->csum = 0;
540 
541         /* Copy the original IP headers into the new buffer. */
542         memcpy(skb_put(skb, ihlen), head->nh.iph, ihlen);
543 
544         /* Copy the data portions of all fragments into the new buffer. */
545         for (fp=head; fp; fp = fp->next) {
546                 memcpy(skb_put(skb, fp->len), fp->data, fp->len);
547 
548                 if (skb->ip_summed != fp->ip_summed)
549                         skb->ip_summed = CHECKSUM_NONE;
550                 else if (skb->ip_summed == CHECKSUM_HW)
551                         skb->csum = csum_add(skb->csum, fp->csum);
552         }
553 
554         skb->dst = dst_clone(head->dst);
555         skb->pkt_type = head->pkt_type;
556         skb->protocol = head->protocol;
557         skb->dev = dev;
558 
559         /*
560         *  Clearly bogus, because security markings of the individual
561         *  fragments should have been checked for consistency before
562         *  gluing, and intermediate coalescing of fragments may have
563         *  taken place in ip_defrag() before ip_glue() ever got called.
564         *  If we're not going to do the consistency checking, we might
565         *  as well take the value associated with the first fragment.
566         *       --rct
567         */
568         skb->security = head->security;
569 
570 #ifdef CONFIG_NETFILTER
571         /* Connection association is same as fragment (if any). */
572         skb->nfct = head->nfct;
573         nf_conntrack_get(skb->nfct);
574 #ifdef CONFIG_NETFILTER_DEBUG
575         skb->nf_debug = head->nf_debug;
576 #endif
577 #endif
578 
579         /* Done with all fragments. Fixup the new IP header. */
580         iph = skb->nh.iph;
581         iph->frag_off = 0;
582         iph->tot_len = htons(len);
583         IP_INC_STATS_BH(IpReasmOKs);
584         return skb;
585 
586 out_nomem:
587         NETDEBUG(printk(KERN_ERR 
588                         "IP: queue_glue: no memory for gluing queue %p\n",
589                         qp));
590         goto out_fail;
591 out_oversize:
592         if (net_ratelimit())
593                 printk(KERN_INFO
594                         "Oversized IP packet from %d.%d.%d.%d.\n",
595                         NIPQUAD(qp->saddr));
596 out_fail:
597         IP_INC_STATS_BH(IpReasmFails);
598         return NULL;
599 }
600 
601 /* Process an incoming IP datagram fragment. */
602 struct sk_buff *ip_defrag(struct sk_buff *skb)
603 {
604         struct iphdr *iph = skb->nh.iph;
605         struct ipq *qp;
606         struct net_device *dev;
607         
608         IP_INC_STATS_BH(IpReasmReqds);
609 
610         /* Start by cleaning up the memory. */
611         if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
612                 ip_evictor();
613 
614         dev = skb->dev;
615 
616         /* Lookup (or create) queue header */
617         if ((qp = ip_find(iph)) != NULL) {
618                 struct sk_buff *ret = NULL;
619 
620                 spin_lock(&qp->lock);
621 
622                 ip_frag_queue(qp, skb);
623 
624                 if (qp->last_in == (FIRST_IN|LAST_IN) &&
625                     qp->meat == qp->len)
626                         ret = ip_frag_reasm(qp, dev);
627 
628                 spin_unlock(&qp->lock);
629                 ipq_put(qp);
630                 return ret;
631         }
632 
633         IP_INC_STATS_BH(IpReasmFails);
634         kfree_skb(skb);
635         return NULL;
636 }
637 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.