My fork of the HTTPServer (working)

Dependents:   DGWWebServer LAN2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip_frag.c Source File

ip_frag.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * This is the IPv4 packet segmentation and reassembly implementation.
00004  *
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without modification, 
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission. 
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  * 
00035  * Author: Jani Monoses <jani@iv.ro> 
00036  *         Simon Goldschmidt
00037  * original reassembly code by Adam Dunkels <adam@sics.se>
00038  * 
00039  */
00040 
00041 #include "lwip/opt.h"
00042 #include "lwip/ip_frag.h"
00043 #include "lwip/ip.h"
00044 #include "lwip/inet.h"
00045 #include "lwip/inet_chksum.h"
00046 #include "lwip/netif.h"
00047 #include "lwip/snmp.h"
00048 #include "lwip/stats.h"
00049 #include "lwip/icmp.h"
00050 
00051 #include <string.h>
00052 
00053 #if IP_REASSEMBLY
00054 /**
00055  * The IP reassembly code currently has the following limitations:
00056  * - IP header options are not supported
00057  * - fragments must not overlap (e.g. due to different routes),
00058  *   currently, overlapping or duplicate fragments are thrown away
00059  *   if IP_REASS_CHECK_OVERLAP=1 (the default)!
00060  *
00061  * @todo: work with IP header options
00062  */
00063 
00064 /** Setting this to 0, you can turn off checking the fragments for overlapping
00065  * regions. The code gets a little smaller. Only use this if you know that
00066  * overlapping won't occur on your network! */
00067 #ifndef IP_REASS_CHECK_OVERLAP
00068 #define IP_REASS_CHECK_OVERLAP 1
00069 #endif /* IP_REASS_CHECK_OVERLAP */
00070 
00071 /** Set to 0 to prevent freeing the oldest datagram when the reassembly buffer is
00072  * full (IP_REASS_MAX_PBUFS pbufs are enqueued). The code gets a little smaller.
00073  * Datagrams will be freed by timeout only. Especially useful when MEMP_NUM_REASSDATA
00074  * is set to 1, so one datagram can be reassembled at a time, only. */
00075 #ifndef IP_REASS_FREE_OLDEST
00076 #define IP_REASS_FREE_OLDEST 1
00077 #endif /* IP_REASS_FREE_OLDEST */
00078 
00079 #define IP_REASS_FLAG_LASTFRAG 0x01
00080 
00081 /** This is a helper struct which holds the starting
00082  * offset and the ending offset of this fragment to
00083  * easily chain the fragments.
00084  * It has to be packed since it has to fit inside the IP header.
00085  */
00086 
00087 // XXX: For armcc
00088 struct ip_reass_helper {
00089   struct pbuf *next_pbuf;
00090   u16_t start;
00091   u16_t end;
00092 };
00093 
00094 /* // Will not work with armcc
00095 #ifdef PACK_STRUCT_USE_INCLUDES
00096 #  include "arch/bpstruct.h"
00097 #endif
00098 PACK_STRUCT_BEGIN
00099 struct ip_reass_helper {
00100   PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
00101   PACK_STRUCT_FIELD(u16_t start);
00102   PACK_STRUCT_FIELD(u16_t end);
00103 } PACK_STRUCT_STRUCT;
00104 PACK_STRUCT_END
00105 #ifdef PACK_STRUCT_USE_INCLUDES
00106 #  include "arch/epstruct.h"
00107 #endif
00108 */
00109 
00110 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB)  \
00111   (ip_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
00112    ip_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
00113    IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
00114 
00115 /* global variables */
00116 static struct ip_reassdata *reassdatagrams;
00117 static u16_t ip_reass_pbufcount;
00118 
00119 /* function prototypes */
00120 static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
00121 static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
00122 
00123 /**
00124  * Reassembly timer base function
00125  * for both NO_SYS == 0 and 1 (!).
00126  *
00127  * Should be called every 1000 msec (defined by IP_TMR_INTERVAL).
00128  */
00129 void
00130 ip_reass_tmr(void)
00131 {
00132   struct ip_reassdata *r, *prev = NULL;
00133 
00134   r = reassdatagrams;
00135   while (r != NULL) {
00136     /* Decrement the timer. Once it reaches 0,
00137      * clean up the incomplete fragment assembly */
00138     if (r->timer > 0) {
00139       r->timer--;
00140       LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)r->timer));
00141       prev = r;
00142       r = r->next;
00143     } else {
00144       /* reassembly timed out */
00145       struct ip_reassdata *tmp;
00146       LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
00147       tmp = r;
00148       /* get the next pointer before freeing */
00149       r = r->next;
00150       /* free the helper struct and all enqueued pbufs */
00151       ip_reass_free_complete_datagram(tmp, prev);
00152      }
00153    }
00154 }
00155 
00156 /**
00157  * Free a datagram (struct ip_reassdata) and all its pbufs.
00158  * Updates the total count of enqueued pbufs (ip_reass_pbufcount),
00159  * SNMP counters and sends an ICMP time exceeded packet.
00160  *
00161  * @param ipr datagram to free
00162  * @param prev the previous datagram in the linked list
00163  * @return the number of pbufs freed
00164  */
00165 static int
00166 ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
00167 {
00168   int pbufs_freed = 0;
00169   struct pbuf *p;
00170   struct ip_reass_helper *iprh;
00171 
00172   LWIP_ASSERT("prev != ipr", prev != ipr);
00173   if (prev != NULL) {
00174     LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
00175   }
00176 
00177   snmp_inc_ipreasmfails();
00178 #if LWIP_ICMP
00179   iprh = (struct ip_reass_helper *)ipr->p->payload;
00180   if (iprh->start == 0) {
00181     /* The first fragment was received, send ICMP time exceeded. */
00182     /* First, de-queue the first pbuf from r->p. */
00183     p = ipr->p;
00184     ipr->p = iprh->next_pbuf;
00185     /* Then, copy the original header into it. */
00186     SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
00187     icmp_time_exceeded(p, ICMP_TE_FRAG);
00188     pbufs_freed += pbuf_clen(p);
00189     pbuf_free(p);
00190   }
00191 #endif /* LWIP_ICMP */
00192 
00193   /* First, free all received pbufs.  The individual pbufs need to be released 
00194      separately as they have not yet been chained */
00195   p = ipr->p;
00196   while (p != NULL) {
00197     struct pbuf *pcur;
00198     iprh = (struct ip_reass_helper *)p->payload;
00199     pcur = p;
00200     /* get the next pointer before freeing */
00201     p = iprh->next_pbuf;
00202     pbufs_freed += pbuf_clen(pcur);
00203     pbuf_free(pcur);    
00204   }
00205   /* Then, unchain the struct ip_reassdata from the list and free it. */
00206   ip_reass_dequeue_datagram(ipr, prev);
00207   LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= pbufs_freed);
00208   ip_reass_pbufcount -= pbufs_freed;
00209 
00210   return pbufs_freed;
00211 }
00212 
00213 #if IP_REASS_FREE_OLDEST
00214 /**
00215  * Free the oldest datagram to make room for enqueueing new fragments.
00216  * The datagram 'fraghdr' belongs to is not freed!
00217  *
00218  * @param fraghdr IP header of the current fragment
00219  * @param pbufs_needed number of pbufs needed to enqueue
00220  *        (used for freeing other datagrams if not enough space)
00221  * @return the number of pbufs freed
00222  */
00223 static int
00224 ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
00225 {
00226   /* @todo Can't we simply remove the last datagram in the
00227    *       linked list behind reassdatagrams?
00228    */
00229   struct ip_reassdata *r, *oldest, *prev;
00230   int pbufs_freed = 0, pbufs_freed_current;
00231   int other_datagrams;
00232 
00233   /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
00234    * but don't free the datagram that 'fraghdr' belongs to! */
00235   do {
00236     oldest = NULL;
00237     prev = NULL;
00238     other_datagrams = 0;
00239     r = reassdatagrams;
00240     while (r != NULL) {
00241       if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
00242         /* Not the same datagram as fraghdr */
00243         other_datagrams++;
00244         if (oldest == NULL) {
00245           oldest = r;
00246         } else if (r->timer <= oldest->timer) {
00247           /* older than the previous oldest */
00248           oldest = r;
00249         }
00250       }
00251       if (r->next != NULL) {
00252         prev = r;
00253       }
00254       r = r->next;
00255     }
00256     if (oldest != NULL) {
00257       pbufs_freed_current = ip_reass_free_complete_datagram(oldest, prev);
00258       pbufs_freed += pbufs_freed_current;
00259     }
00260   } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
00261   return pbufs_freed;
00262 }
00263 #endif /* IP_REASS_FREE_OLDEST */
00264 
00265 /**
00266  * Enqueues a new fragment into the fragment queue
00267  * @param fraghdr points to the new fragments IP hdr
00268  * @param clen number of pbufs needed to enqueue (used for freeing other datagrams if not enough space)
00269  * @return A pointer to the queue location into which the fragment was enqueued
00270  */
00271 static struct ip_reassdata*
00272 ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
00273 {
00274   struct ip_reassdata* ipr;
00275   /* No matching previous fragment found, allocate a new reassdata struct */
00276   ipr = (struct ip_reassdata *)(memp_malloc(MEMP_REASSDATA)); // static_cast<struct ip_reassdata *>(x)
00277   if (ipr == NULL) {
00278 #if IP_REASS_FREE_OLDEST
00279     if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
00280       ipr = (struct ip_reassdata *)(memp_malloc(MEMP_REASSDATA)); // static_cast<struct ip_reassdata *>(x)
00281     }
00282     if (ipr == NULL)
00283 #endif /* IP_REASS_FREE_OLDEST */
00284     {
00285       IPFRAG_STATS_INC(ip_frag.memerr);
00286       LWIP_DEBUGF(IP_REASS_DEBUG,("Failed to alloc reassdata struct\n"));
00287       return NULL;
00288     }
00289   }
00290   memset(ipr, 0, sizeof(struct ip_reassdata));
00291   ipr->timer = IP_REASS_MAXAGE;
00292 
00293   /* enqueue the new structure to the front of the list */
00294   ipr->next = reassdatagrams;
00295   reassdatagrams = ipr;
00296   /* copy the ip header for later tests and input */
00297   /* @todo: no ip options supported? */
00298   SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
00299   return ipr;
00300 }
00301 
00302 /**
00303  * Dequeues a datagram from the datagram queue. Doesn't deallocate the pbufs.
00304  * @param ipr points to the queue entry to dequeue
00305  */
00306 static void
00307 ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
00308 {
00309   
00310   /* dequeue the reass struct  */
00311   if (reassdatagrams == ipr) {
00312     /* it was the first in the list */
00313     reassdatagrams = ipr->next;
00314   } else {
00315     /* it wasn't the first, so it must have a valid 'prev' */
00316     LWIP_ASSERT("sanity check linked list", prev != NULL);
00317     prev->next = ipr->next;
00318   }
00319 
00320   /* now we can free the ip_reass struct */
00321   memp_free(MEMP_REASSDATA, ipr);
00322 }
00323 
00324 /**
00325  * Chain a new pbuf into the pbuf list that composes the datagram.  The pbuf list
00326  * will grow over time as  new pbufs are rx.
00327  * Also checks that the datagram passes basic continuity checks (if the last
00328  * fragment was received at least once).
00329  * @param root_p points to the 'root' pbuf for the current datagram being assembled.
00330  * @param new_p points to the pbuf for the current fragment
00331  * @return 0 if invalid, >0 otherwise
00332  */
00333 static int
00334 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p)
00335 {
00336   struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
00337   struct pbuf *q;
00338   u16_t offset,len;
00339   struct ip_hdr *fraghdr;
00340   int valid = 1;
00341 
00342   /* Extract length and fragment offset from current fragment */
00343   fraghdr = (struct ip_hdr*)new_p->payload; 
00344   len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
00345   offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
00346 
00347   /* overwrite the fragment's ip header from the pbuf with our helper struct,
00348    * and setup the embedded helper structure. */
00349   /* make sure the struct ip_reass_helper fits into the IP header */
00350   LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
00351               sizeof(struct ip_reass_helper) <= IP_HLEN);
00352   iprh = (struct ip_reass_helper*)new_p->payload;
00353   iprh->next_pbuf = NULL;
00354   iprh->start = offset;
00355   iprh->end = offset + len;
00356 
00357   /* Iterate through until we either get to the end of the list (append),
00358    * or we find on with a larger offset (insert). */
00359   for (q = ipr->p; q != NULL;) {
00360     iprh_tmp = (struct ip_reass_helper*)q->payload;
00361     if (iprh->start < iprh_tmp->start) {
00362       /* the new pbuf should be inserted before this */
00363       iprh->next_pbuf = q;
00364       if (iprh_prev != NULL) {
00365         /* not the fragment with the lowest offset */
00366 #if IP_REASS_CHECK_OVERLAP
00367         if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
00368           /* fragment overlaps with previous or following, throw away */
00369           goto freepbuf;
00370         }
00371 #endif /* IP_REASS_CHECK_OVERLAP */
00372         iprh_prev->next_pbuf = new_p;
00373       } else {
00374         /* fragment with the lowest offset */
00375         ipr->p = new_p;
00376       }
00377       break;
00378     } else if(iprh->start == iprh_tmp->start) {
00379       /* received the same datagram twice: no need to keep the datagram */
00380       goto freepbuf;
00381 #if IP_REASS_CHECK_OVERLAP
00382     } else if(iprh->start < iprh_tmp->end) {
00383       /* overlap: no need to keep the new datagram */
00384       goto freepbuf;
00385 #endif /* IP_REASS_CHECK_OVERLAP */
00386     } else {
00387       /* Check if the fragments received so far have no wholes. */
00388       if (iprh_prev != NULL) {
00389         if (iprh_prev->end != iprh_tmp->start) {
00390           /* There is a fragment missing between the current
00391            * and the previous fragment */
00392           valid = 0;
00393         }
00394       }
00395     }
00396     q = iprh_tmp->next_pbuf;
00397     iprh_prev = iprh_tmp;
00398   }
00399 
00400   /* If q is NULL, then we made it to the end of the list. Determine what to do now */
00401   if (q == NULL) {
00402     if (iprh_prev != NULL) {
00403       /* this is (for now), the fragment with the highest offset:
00404        * chain it to the last fragment */
00405 #if IP_REASS_CHECK_OVERLAP
00406       LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
00407 #endif /* IP_REASS_CHECK_OVERLAP */
00408       iprh_prev->next_pbuf = new_p;
00409       if (iprh_prev->end != iprh->start) {
00410         valid = 0;
00411       }
00412     } else {
00413 #if IP_REASS_CHECK_OVERLAP
00414       LWIP_ASSERT("no previous fragment, this must be the first fragment!",
00415         ipr->p == NULL);
00416 #endif /* IP_REASS_CHECK_OVERLAP */
00417       /* this is the first fragment we ever received for this ip datagram */
00418       ipr->p = new_p;
00419     }
00420   }
00421 
00422   /* At this point, the validation part begins: */
00423   /* If we already received the last fragment */
00424   if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {
00425     /* and had no wholes so far */
00426     if (valid) {
00427       /* then check if the rest of the fragments is here */
00428       /* Check if the queue starts with the first datagram */
00429       if (((struct ip_reass_helper*)ipr->p->payload)->start != 0) {
00430         valid = 0;
00431       } else {
00432         /* and check that there are no wholes after this datagram */
00433         iprh_prev = iprh;
00434         q = iprh->next_pbuf;
00435         while (q != NULL) {
00436           iprh = (struct ip_reass_helper*)q->payload;
00437           if (iprh_prev->end != iprh->start) {
00438             valid = 0;
00439             break;
00440           }
00441           iprh_prev = iprh;
00442           q = iprh->next_pbuf;
00443         }
00444         /* if still valid, all fragments are received
00445          * (because to the MF==0 already arrived */
00446         if (valid) {
00447           LWIP_ASSERT("sanity check", ipr->p != NULL);
00448           LWIP_ASSERT("sanity check",
00449             ((struct ip_reass_helper*)ipr->p->payload) != iprh);
00450           LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
00451             iprh->next_pbuf == NULL);
00452           LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
00453             iprh->end == ipr->datagram_len);
00454         }
00455       }
00456     }
00457     /* If valid is 0 here, there are some fragments missing in the middle
00458      * (since MF == 0 has already arrived). Such datagrams simply time out if
00459      * no more fragments are received... */
00460     return valid;
00461   }
00462   /* If we come here, not all fragments were received, yet! */
00463   return 0; /* not yet valid! */
00464 #if IP_REASS_CHECK_OVERLAP
00465 freepbuf:
00466   ip_reass_pbufcount -= pbuf_clen(new_p);
00467   pbuf_free(new_p);
00468   return 0;
00469 #endif /* IP_REASS_CHECK_OVERLAP */
00470 }
00471 
00472 /**
00473  * Reassembles incoming IP fragments into an IP datagram.
00474  *
00475  * @param p points to a pbuf chain of the fragment
00476  * @return NULL if reassembly is incomplete, ? otherwise
00477  */
00478 struct pbuf *
00479 ip_reass(struct pbuf *p)
00480 {
00481   struct pbuf *r;
00482   struct ip_hdr *fraghdr;
00483   struct ip_reassdata *ipr;
00484   struct ip_reass_helper *iprh;
00485   u16_t offset, len;
00486   u8_t clen;
00487   struct ip_reassdata *ipr_prev = NULL;
00488 
00489   IPFRAG_STATS_INC(ip_frag.recv);
00490   snmp_inc_ipreasmreqds();
00491 
00492   fraghdr = (struct ip_hdr*)p->payload;
00493 
00494   if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {
00495     LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: IP options currently not supported!\n"));
00496     IPFRAG_STATS_INC(ip_frag.err);
00497     goto nullreturn;
00498   }
00499 
00500   offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
00501   len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
00502 
00503   /* Check if we are allowed to enqueue more datagrams. */
00504   clen = pbuf_clen(p);
00505   if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
00506 #if IP_REASS_FREE_OLDEST
00507     if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
00508         ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
00509 #endif /* IP_REASS_FREE_OLDEST */
00510     {
00511       /* No datagram could be freed and still too many pbufs enqueued */
00512       LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
00513         ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
00514       IPFRAG_STATS_INC(ip_frag.memerr);
00515       /* @todo: send ICMP time exceeded here? */
00516       /* drop this pbuf */
00517       goto nullreturn;
00518     }
00519   }
00520 
00521   /* Look for the datagram the fragment belongs to in the current datagram queue,
00522    * remembering the previous in the queue for later dequeueing. */
00523   for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
00524     /* Check if the incoming fragment matches the one currently present
00525        in the reassembly buffer. If so, we proceed with copying the
00526        fragment into the buffer. */
00527     if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
00528       LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching previous fragment ID=%"X16_F"\n",
00529         ntohs(IPH_ID(fraghdr))));
00530       IPFRAG_STATS_INC(ip_frag.cachehit);
00531       break;
00532     }
00533     ipr_prev = ipr;
00534   }
00535 
00536   if (ipr == NULL) {
00537   /* Enqueue a new datagram into the datagram queue */
00538     ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
00539     /* Bail if unable to enqueue */
00540     if(ipr == NULL) {
00541       goto nullreturn;
00542     }
00543   } else {
00544     if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) && 
00545       ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
00546       /* ipr->iphdr is not the header from the first fragment, but fraghdr is
00547        * -> copy fraghdr into ipr->iphdr since we want to have the header
00548        * of the first fragment (for ICMP time exceeded and later, for copying
00549        * all options, if supported)*/
00550       SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
00551     }
00552   }
00553   /* Track the current number of pbufs current 'in-flight', in order to limit 
00554   the number of fragments that may be enqueued at any one time */
00555   ip_reass_pbufcount += clen;
00556 
00557   /* At this point, we have either created a new entry or pointing 
00558    * to an existing one */
00559 
00560   /* check for 'no more fragments', and update queue entry*/
00561   if ((ntohs(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {
00562     ipr->flags |= IP_REASS_FLAG_LASTFRAG;
00563     ipr->datagram_len = offset + len;
00564     LWIP_DEBUGF(IP_REASS_DEBUG,
00565      ("ip_reass: last fragment seen, total len %"S16_F"\n",
00566       ipr->datagram_len));
00567   }
00568   /* find the right place to insert this pbuf */
00569   /* @todo: trim pbufs if fragments are overlapping */
00570   if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {
00571     /* the totally last fragment (flag more fragments = 0) was received at least
00572      * once AND all fragments are received */
00573     ipr->datagram_len += IP_HLEN;
00574 
00575     /* save the second pbuf before copying the header over the pointer */
00576     r = ((struct ip_reass_helper*)ipr->p->payload)->next_pbuf;
00577 
00578     /* copy the original ip header back to the first pbuf */
00579     fraghdr = (struct ip_hdr*)(ipr->p->payload);
00580     SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
00581     IPH_LEN_SET(fraghdr, htons(ipr->datagram_len));
00582     IPH_OFFSET_SET(fraghdr, 0);
00583     IPH_CHKSUM_SET(fraghdr, 0);
00584     /* @todo: do we need to set calculate the correct checksum? */
00585     IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
00586 
00587     p = ipr->p;
00588 
00589     /* chain together the pbufs contained within the reass_data list. */
00590     while(r != NULL) {
00591       iprh = (struct ip_reass_helper*)r->payload;
00592 
00593       /* hide the ip header for every succeding fragment */
00594       pbuf_header(r, -IP_HLEN);
00595       pbuf_cat(p, r);
00596       r = iprh->next_pbuf;
00597     }
00598     /* release the sources allocate for the fragment queue entry */
00599     ip_reass_dequeue_datagram(ipr, ipr_prev);
00600 
00601     /* and adjust the number of pbufs currently queued for reassembly. */
00602     ip_reass_pbufcount -= pbuf_clen(p);
00603 
00604     /* Return the pbuf chain */
00605     return p;
00606   }
00607   /* the datagram is not (yet?) reassembled completely */
00608   LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
00609   return NULL;
00610 
00611 nullreturn:
00612   LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: nullreturn\n"));
00613   IPFRAG_STATS_INC(ip_frag.drop);
00614   pbuf_free(p);
00615   return NULL;
00616 }
00617 #endif /* IP_REASSEMBLY */
00618 
00619 #if IP_FRAG
00620 #if IP_FRAG_USES_STATIC_BUF
00621 static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)];
00622 #endif /* IP_FRAG_USES_STATIC_BUF */
00623 
00624 /**
00625  * Fragment an IP datagram if too large for the netif.
00626  *
00627  * Chop the datagram in MTU sized chunks and send them in order
00628  * by using a fixed size static memory buffer (PBUF_REF) or
00629  * point PBUF_REFs into p (depending on IP_FRAG_USES_STATIC_BUF).
00630  *
00631  * @param p ip packet to send
00632  * @param netif the netif on which to send
00633  * @param dest destination ip address to which to send
00634  *
00635  * @return ERR_OK if sent successfully, err_t otherwise
00636  */
00637 err_t 
00638 ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest)
00639 {
00640   struct pbuf *rambuf;
00641 #if IP_FRAG_USES_STATIC_BUF
00642   struct pbuf *header;
00643 #else
00644   struct pbuf *newpbuf;
00645   struct ip_hdr *original_iphdr;
00646 #endif
00647   struct ip_hdr *iphdr;
00648   u16_t nfb;
00649   u16_t left, cop;
00650   u16_t mtu = netif->mtu;
00651   u16_t ofo, omf;
00652   u16_t last;
00653   u16_t poff = IP_HLEN;
00654   u16_t tmp;
00655 #if !IP_FRAG_USES_STATIC_BUF
00656   u16_t newpbuflen = 0;
00657   u16_t left_to_copy;
00658 #endif
00659 
00660   /* Get a RAM based MTU sized pbuf */
00661 #if IP_FRAG_USES_STATIC_BUF
00662   /* When using a static buffer, we use a PBUF_REF, which we will
00663    * use to reference the packet (without link header).
00664    * Layer and length is irrelevant.
00665    */
00666   rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
00667   if (rambuf == NULL) {
00668     LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
00669     return ERR_MEM;
00670   }
00671   rambuf->tot_len = rambuf->len = mtu;
00672   rambuf->payload = LWIP_MEM_ALIGN((void *)buf);
00673 
00674   /* Copy the IP header in it */
00675   iphdr = (struct ip_hdr *)(rambuf->payload); // static_cast<struct ip_hdr *>(x)
00676   SMEMCPY(iphdr, p->payload, IP_HLEN);
00677 #else /* IP_FRAG_USES_STATIC_BUF */
00678   original_iphdr = (struct ip_hdr *)p->payload;
00679   iphdr = original_iphdr;
00680 #endif /* IP_FRAG_USES_STATIC_BUF */
00681 
00682   /* Save original offset */
00683   tmp = ntohs(IPH_OFFSET(iphdr));
00684   ofo = tmp & IP_OFFMASK;
00685   omf = tmp & IP_MF;
00686 
00687   left = p->tot_len - IP_HLEN;
00688 
00689   nfb = (mtu - IP_HLEN) / 8;
00690 
00691   while (left) {
00692     last = (left <= mtu - IP_HLEN);
00693 
00694     /* Set new offset and MF flag */
00695     tmp = omf | (IP_OFFMASK & (ofo));
00696     if (!last)
00697       tmp = tmp | IP_MF;
00698 
00699     /* Fill this fragment */
00700     cop = last ? left : nfb * 8;
00701 
00702 #if IP_FRAG_USES_STATIC_BUF
00703     poff += pbuf_copy_partial(p, (u8_t*)iphdr + IP_HLEN, cop, poff);
00704 #else /* IP_FRAG_USES_STATIC_BUF */
00705     /* When not using a static buffer, create a chain of pbufs.
00706      * The first will be a PBUF_RAM holding the link and IP header.
00707      * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
00708      * but limited to the size of an mtu.
00709      */
00710     rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
00711     if (rambuf == NULL) {
00712       return ERR_MEM;
00713     }
00714     LWIP_ASSERT("this needs a pbuf in one piece!",
00715                 (p->len >= (IP_HLEN)));
00716     SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
00717     iphdr = (struct ip_hdr *)rambuf->payload;
00718 
00719     /* Can just adjust p directly for needed offset. */
00720     p->payload = (u8_t *)p->payload + poff;
00721     p->len -= poff;
00722 
00723     left_to_copy = cop;
00724     while (left_to_copy) {
00725       newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
00726       /* Is this pbuf already empty? */
00727       if (!newpbuflen) {
00728         p = p->next;
00729         continue;
00730       }
00731       newpbuf = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
00732       if (newpbuf == NULL) {
00733         pbuf_free(rambuf);
00734         return ERR_MEM;
00735       }
00736       /* Mirror this pbuf, although we might not need all of it. */
00737       newpbuf->payload = p->payload;
00738       newpbuf->len = newpbuf->tot_len = newpbuflen;
00739       /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
00740        * so that it is removed when pbuf_dechain is later called on rambuf.
00741        */
00742       pbuf_cat(rambuf, newpbuf);
00743       left_to_copy -= newpbuflen;
00744       if (left_to_copy)
00745         p = p->next;
00746     }
00747     poff = newpbuflen;
00748 #endif /* IP_FRAG_USES_STATIC_BUF */
00749 
00750     /* Correct header */
00751     IPH_OFFSET_SET(iphdr, htons(tmp));
00752     IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
00753     IPH_CHKSUM_SET(iphdr, 0);
00754     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
00755 
00756 #if IP_FRAG_USES_STATIC_BUF
00757     if (last)
00758       pbuf_realloc(rambuf, left + IP_HLEN);
00759 
00760     /* This part is ugly: we alloc a RAM based pbuf for 
00761      * the link level header for each chunk and then 
00762      * free it.A PBUF_ROM style pbuf for which pbuf_header
00763      * worked would make things simpler.
00764      */
00765     header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
00766     if (header != NULL) {
00767       pbuf_chain(header, rambuf);
00768       netif->output(netif, header, dest);
00769       IPFRAG_STATS_INC(ip_frag.xmit);
00770       snmp_inc_ipfragcreates();
00771       pbuf_free(header);
00772     } else {
00773       LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc() for header failed\n"));
00774       pbuf_free(rambuf);
00775       return ERR_MEM;
00776     }
00777 #else /* IP_FRAG_USES_STATIC_BUF */
00778     /* No need for separate header pbuf - we allowed room for it in rambuf
00779      * when allocated.
00780      */
00781     netif->output(netif, rambuf, dest);
00782     IPFRAG_STATS_INC(ip_frag.xmit);
00783 
00784     /* Unfortunately we can't reuse rambuf - the hardware may still be
00785      * using the buffer. Instead we free it (and the ensuing chain) and
00786      * recreate it next time round the loop. If we're lucky the hardware
00787      * will have already sent the packet, the free will really free, and
00788      * there will be zero memory penalty.
00789      */
00790     
00791     pbuf_free(rambuf);
00792 #endif /* IP_FRAG_USES_STATIC_BUF */
00793     left -= cop;
00794     ofo += nfb;
00795   }
00796 #if IP_FRAG_USES_STATIC_BUF
00797   pbuf_free(rambuf);
00798 #endif /* IP_FRAG_USES_STATIC_BUF */
00799   snmp_inc_ipfragoks();
00800   return ERR_OK;
00801 }
00802 #endif /* IP_FRAG */