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