00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "lwip/opt.h"
00042 #include "lwip/ip_frag.h"
00043 #include "lwip/def.h"
00044 #include "lwip/inet_chksum.h"
00045 #include "lwip/netif.h"
00046 #include "lwip/snmp.h"
00047 #include "lwip/stats.h"
00048 #include "lwip/icmp.h"
00049
00050 #include <string.h>
00051
00052 #if IP_REASSEMBLY
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef IP_REASS_CHECK_OVERLAP
00067 #define IP_REASS_CHECK_OVERLAP 1
00068 #endif
00069
00070
00071
00072
00073
00074 #ifndef IP_REASS_FREE_OLDEST
00075 #define IP_REASS_FREE_OLDEST 1
00076 #endif
00077
00078 #define IP_REASS_FLAG_LASTFRAG 0x01
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 #ifdef PACK_STRUCT_USE_INCLUDES
00089 # include "arch/bpstruct.h"
00090 #endif
00091 PACK_STRUCT_BEGIN
00092 struct ip_reass_helper {
00093 PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
00094 PACK_STRUCT_FIELD(u16_t start);
00095 PACK_STRUCT_FIELD(u16_t end);
00096 } PACK_STRUCT_STRUCT;
00097 PACK_STRUCT_END
00098 #ifdef PACK_STRUCT_USE_INCLUDES
00099 # include "arch/epstruct.h"
00100 #endif
00101
00102 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
00103 (ip_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
00104 ip_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
00105 IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
00106
00107
00108 static struct ip_reassdata *reassdatagrams;
00109 static u16_t ip_reass_pbufcount;
00110
00111
00112 static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
00113 static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
00114
00115
00116
00117
00118
00119
00120
00121 void
00122 ip_reass_tmr(void)
00123 {
00124 struct ip_reassdata *r, *prev = NULL;
00125
00126 r = reassdatagrams;
00127 while (r != NULL) {
00128
00129
00130 if (r->timer > 0) {
00131 r->timer--;
00132 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)r->timer));
00133 prev = r;
00134 r = r->next;
00135 } else {
00136
00137 struct ip_reassdata *tmp;
00138 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
00139 tmp = r;
00140
00141 r = r->next;
00142
00143 ip_reass_free_complete_datagram(tmp, prev);
00144 }
00145 }
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 static int
00158 ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
00159 {
00160 u16_t pbufs_freed = 0;
00161 u8_t clen;
00162 struct pbuf *p;
00163 struct ip_reass_helper *iprh;
00164
00165 LWIP_ASSERT("prev != ipr", prev != ipr);
00166 if (prev != NULL) {
00167 LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
00168 }
00169
00170 snmp_inc_ipreasmfails();
00171 #if LWIP_ICMP
00172 iprh = (struct ip_reass_helper *)ipr->p->payload;
00173 if (iprh->start == 0) {
00174
00175
00176 p = ipr->p;
00177 ipr->p = iprh->next_pbuf;
00178
00179 SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
00180 icmp_time_exceeded(p, ICMP_TE_FRAG);
00181 clen = pbuf_clen(p);
00182 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
00183 pbufs_freed += clen;
00184 pbuf_free(p);
00185 }
00186 #endif
00187
00188
00189
00190 p = ipr->p;
00191 while (p != NULL) {
00192 struct pbuf *pcur;
00193 iprh = (struct ip_reass_helper *)p->payload;
00194 pcur = p;
00195
00196 p = iprh->next_pbuf;
00197 clen = pbuf_clen(pcur);
00198 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
00199 pbufs_freed += clen;
00200 pbuf_free(pcur);
00201 }
00202
00203 ip_reass_dequeue_datagram(ipr, prev);
00204 LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= pbufs_freed);
00205 ip_reass_pbufcount -= pbufs_freed;
00206
00207 return pbufs_freed;
00208 }
00209
00210 #if IP_REASS_FREE_OLDEST
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 static int
00221 ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
00222 {
00223
00224
00225
00226 struct ip_reassdata *r, *oldest, *prev;
00227 int pbufs_freed = 0, pbufs_freed_current;
00228 int other_datagrams;
00229
00230
00231
00232 do {
00233 oldest = NULL;
00234 prev = NULL;
00235 other_datagrams = 0;
00236 r = reassdatagrams;
00237 while (r != NULL) {
00238 if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
00239
00240 other_datagrams++;
00241 if (oldest == NULL) {
00242 oldest = r;
00243 } else if (r->timer <= oldest->timer) {
00244
00245 oldest = r;
00246 }
00247 }
00248 if (r->next != NULL) {
00249 prev = r;
00250 }
00251 r = r->next;
00252 }
00253 if (oldest != NULL) {
00254 pbufs_freed_current = ip_reass_free_complete_datagram(oldest, prev);
00255 pbufs_freed += pbufs_freed_current;
00256 }
00257 } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
00258 return pbufs_freed;
00259 }
00260 #endif
00261
00262
00263
00264
00265
00266
00267
00268 static struct ip_reassdata*
00269 ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
00270 {
00271 struct ip_reassdata* ipr;
00272
00273 ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
00274 if (ipr == NULL) {
00275 #if IP_REASS_FREE_OLDEST
00276 if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
00277 ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
00278 }
00279 if (ipr == NULL)
00280 #endif
00281 {
00282 IPFRAG_STATS_INC(ip_frag.memerr);
00283 LWIP_DEBUGF(IP_REASS_DEBUG,("Failed to alloc reassdata struct\n"));
00284 return NULL;
00285 }
00286 }
00287 memset(ipr, 0, sizeof(struct ip_reassdata));
00288 ipr->timer = IP_REASS_MAXAGE;
00289
00290
00291 ipr->next = reassdatagrams;
00292 reassdatagrams = ipr;
00293
00294
00295 SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
00296 return ipr;
00297 }
00298
00299
00300
00301
00302
00303 static void
00304 ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
00305 {
00306
00307
00308 if (reassdatagrams == ipr) {
00309
00310 reassdatagrams = ipr->next;
00311 } else {
00312
00313 LWIP_ASSERT("sanity check linked list", prev != NULL);
00314 prev->next = ipr->next;
00315 }
00316
00317
00318 memp_free(MEMP_REASSDATA, ipr);
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 static int
00331 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p)
00332 {
00333 struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
00334 struct pbuf *q;
00335 u16_t offset,len;
00336 struct ip_hdr *fraghdr;
00337 int valid = 1;
00338
00339
00340 fraghdr = (struct ip_hdr*)new_p->payload;
00341 len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
00342 offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
00343
00344
00345
00346
00347 LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
00348 sizeof(struct ip_reass_helper) <= IP_HLEN);
00349 iprh = (struct ip_reass_helper*)new_p->payload;
00350 iprh->next_pbuf = NULL;
00351 iprh->start = offset;
00352 iprh->end = offset + len;
00353
00354
00355
00356 for (q = ipr->p; q != NULL;) {
00357 iprh_tmp = (struct ip_reass_helper*)q->payload;
00358 if (iprh->start < iprh_tmp->start) {
00359
00360 iprh->next_pbuf = q;
00361 if (iprh_prev != NULL) {
00362
00363 #if IP_REASS_CHECK_OVERLAP
00364 if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
00365
00366 goto freepbuf;
00367 }
00368 #endif
00369 iprh_prev->next_pbuf = new_p;
00370 } else {
00371
00372 ipr->p = new_p;
00373 }
00374 break;
00375 } else if(iprh->start == iprh_tmp->start) {
00376
00377 goto freepbuf;
00378 #if IP_REASS_CHECK_OVERLAP
00379 } else if(iprh->start < iprh_tmp->end) {
00380
00381 goto freepbuf;
00382 #endif
00383 } else {
00384
00385 if (iprh_prev != NULL) {
00386 if (iprh_prev->end != iprh_tmp->start) {
00387
00388
00389 valid = 0;
00390 }
00391 }
00392 }
00393 q = iprh_tmp->next_pbuf;
00394 iprh_prev = iprh_tmp;
00395 }
00396
00397
00398 if (q == NULL) {
00399 if (iprh_prev != NULL) {
00400
00401
00402 #if IP_REASS_CHECK_OVERLAP
00403 LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
00404 #endif
00405 iprh_prev->next_pbuf = new_p;
00406 if (iprh_prev->end != iprh->start) {
00407 valid = 0;
00408 }
00409 } else {
00410 #if IP_REASS_CHECK_OVERLAP
00411 LWIP_ASSERT("no previous fragment, this must be the first fragment!",
00412 ipr->p == NULL);
00413 #endif
00414
00415 ipr->p = new_p;
00416 }
00417 }
00418
00419
00420
00421 if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {
00422
00423 if (valid) {
00424
00425
00426 if (((struct ip_reass_helper*)ipr->p->payload)->start != 0) {
00427 valid = 0;
00428 } else {
00429
00430 iprh_prev = iprh;
00431 q = iprh->next_pbuf;
00432 while (q != NULL) {
00433 iprh = (struct ip_reass_helper*)q->payload;
00434 if (iprh_prev->end != iprh->start) {
00435 valid = 0;
00436 break;
00437 }
00438 iprh_prev = iprh;
00439 q = iprh->next_pbuf;
00440 }
00441
00442
00443 if (valid) {
00444 LWIP_ASSERT("sanity check", ipr->p != NULL);
00445 LWIP_ASSERT("sanity check",
00446 ((struct ip_reass_helper*)ipr->p->payload) != iprh);
00447 LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
00448 iprh->next_pbuf == NULL);
00449 LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
00450 iprh->end == ipr->datagram_len);
00451 }
00452 }
00453 }
00454
00455
00456
00457 return valid;
00458 }
00459
00460 return 0;
00461 #if IP_REASS_CHECK_OVERLAP
00462 freepbuf:
00463 ip_reass_pbufcount -= pbuf_clen(new_p);
00464 pbuf_free(new_p);
00465 return 0;
00466 #endif
00467 }
00468
00469
00470
00471
00472
00473
00474
00475 struct pbuf *
00476 ip_reass(struct pbuf *p)
00477 {
00478 struct pbuf *r;
00479 struct ip_hdr *fraghdr;
00480 struct ip_reassdata *ipr;
00481 struct ip_reass_helper *iprh;
00482 u16_t offset, len;
00483 u8_t clen;
00484 struct ip_reassdata *ipr_prev = NULL;
00485
00486 IPFRAG_STATS_INC(ip_frag.recv);
00487 snmp_inc_ipreasmreqds();
00488
00489 fraghdr = (struct ip_hdr*)p->payload;
00490
00491 if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {
00492 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: IP options currently not supported!\n"));
00493 IPFRAG_STATS_INC(ip_frag.err);
00494 goto nullreturn;
00495 }
00496
00497 offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
00498 len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
00499
00500
00501 clen = pbuf_clen(p);
00502 if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
00503 #if IP_REASS_FREE_OLDEST
00504 if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
00505 ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
00506 #endif
00507 {
00508
00509 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
00510 ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
00511 IPFRAG_STATS_INC(ip_frag.memerr);
00512
00513
00514 goto nullreturn;
00515 }
00516 }
00517
00518
00519
00520 for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
00521
00522
00523
00524 if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
00525 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching previous fragment ID=%"X16_F"\n",
00526 ntohs(IPH_ID(fraghdr))));
00527 IPFRAG_STATS_INC(ip_frag.cachehit);
00528 break;
00529 }
00530 ipr_prev = ipr;
00531 }
00532
00533 if (ipr == NULL) {
00534
00535 ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
00536
00537 if(ipr == NULL) {
00538 goto nullreturn;
00539 }
00540 } else {
00541 if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
00542 ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
00543
00544
00545
00546
00547 SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
00548 }
00549 }
00550
00551
00552 ip_reass_pbufcount += clen;
00553
00554
00555
00556
00557
00558 if ((IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0) {
00559 ipr->flags |= IP_REASS_FLAG_LASTFRAG;
00560 ipr->datagram_len = offset + len;
00561 LWIP_DEBUGF(IP_REASS_DEBUG,
00562 ("ip_reass: last fragment seen, total len %"S16_F"\n",
00563 ipr->datagram_len));
00564 }
00565
00566
00567 if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {
00568
00569
00570 ipr->datagram_len += IP_HLEN;
00571
00572
00573 r = ((struct ip_reass_helper*)ipr->p->payload)->next_pbuf;
00574
00575
00576 fraghdr = (struct ip_hdr*)(ipr->p->payload);
00577 SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
00578 IPH_LEN_SET(fraghdr, htons(ipr->datagram_len));
00579 IPH_OFFSET_SET(fraghdr, 0);
00580 IPH_CHKSUM_SET(fraghdr, 0);
00581
00582 IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
00583
00584 p = ipr->p;
00585
00586
00587 while(r != NULL) {
00588 iprh = (struct ip_reass_helper*)r->payload;
00589
00590
00591 pbuf_header(r, -IP_HLEN);
00592 pbuf_cat(p, r);
00593 r = iprh->next_pbuf;
00594 }
00595
00596 ip_reass_dequeue_datagram(ipr, ipr_prev);
00597
00598
00599 ip_reass_pbufcount -= pbuf_clen(p);
00600
00601
00602 return p;
00603 }
00604
00605 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
00606 return NULL;
00607
00608 nullreturn:
00609 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: nullreturn\n"));
00610 IPFRAG_STATS_INC(ip_frag.drop);
00611 pbuf_free(p);
00612 return NULL;
00613 }
00614 #endif
00615
00616 #if IP_FRAG
00617 #if IP_FRAG_USES_STATIC_BUF
00618 static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)];
00619 #else
00620
00621 #if !LWIP_NETIF_TX_SINGLE_PBUF
00622
00623 static struct pbuf_custom_ref*
00624 ip_frag_alloc_pbuf_custom_ref(void)
00625 {
00626 return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
00627 }
00628
00629
00630 static void
00631 ip_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
00632 {
00633 LWIP_ASSERT("p != NULL", p != NULL);
00634 memp_free(MEMP_FRAG_PBUF, p);
00635 }
00636
00637
00638
00639 static void
00640 ipfrag_free_pbuf_custom(struct pbuf *p)
00641 {
00642 struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
00643 LWIP_ASSERT("pcr != NULL", pcr != NULL);
00644 LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
00645 if (pcr->original != NULL) {
00646 pbuf_free(pcr->original);
00647 }
00648 ip_frag_free_pbuf_custom_ref(pcr);
00649 }
00650 #endif
00651 #endif
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666 err_t
00667 ip_frag(struct pbuf *p, struct netif *netif, ip_addr_t *dest)
00668 {
00669 struct pbuf *rambuf;
00670 #if IP_FRAG_USES_STATIC_BUF
00671 struct pbuf *header;
00672 #else
00673 #if !LWIP_NETIF_TX_SINGLE_PBUF
00674 struct pbuf *newpbuf;
00675 #endif
00676 struct ip_hdr *original_iphdr;
00677 #endif
00678 struct ip_hdr *iphdr;
00679 u16_t nfb;
00680 u16_t left, cop;
00681 u16_t mtu = netif->mtu;
00682 u16_t ofo, omf;
00683 u16_t last;
00684 u16_t poff = IP_HLEN;
00685 u16_t tmp;
00686 #if !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF
00687 u16_t newpbuflen = 0;
00688 u16_t left_to_copy;
00689 #endif
00690
00691
00692 #if IP_FRAG_USES_STATIC_BUF
00693
00694
00695
00696
00697 rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
00698 if (rambuf == NULL) {
00699 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
00700 return ERR_MEM;
00701 }
00702 rambuf->tot_len = rambuf->len = mtu;
00703 rambuf->payload = LWIP_MEM_ALIGN((void *)buf);
00704
00705
00706 iphdr = (struct ip_hdr *)rambuf->payload;
00707 SMEMCPY(iphdr, p->payload, IP_HLEN);
00708 #else
00709 original_iphdr = (struct ip_hdr *)p->payload;
00710 iphdr = original_iphdr;
00711 #endif
00712
00713
00714 tmp = ntohs(IPH_OFFSET(iphdr));
00715 ofo = tmp & IP_OFFMASK;
00716 omf = tmp & IP_MF;
00717
00718 left = p->tot_len - IP_HLEN;
00719
00720 nfb = (mtu - IP_HLEN) / 8;
00721
00722 while (left) {
00723 last = (left <= mtu - IP_HLEN);
00724
00725
00726 tmp = omf | (IP_OFFMASK & (ofo));
00727 if (!last) {
00728 tmp = tmp | IP_MF;
00729 }
00730
00731
00732 cop = last ? left : nfb * 8;
00733
00734 #if IP_FRAG_USES_STATIC_BUF
00735 poff += pbuf_copy_partial(p, (u8_t*)iphdr + IP_HLEN, cop, poff);
00736 #else
00737 #if LWIP_NETIF_TX_SINGLE_PBUF
00738 rambuf = pbuf_alloc(PBUF_IP, cop, PBUF_RAM);
00739 if (rambuf == NULL) {
00740 return ERR_MEM;
00741 }
00742 LWIP_ASSERT("this needs a pbuf in one piece!",
00743 (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
00744 poff += pbuf_copy_partial(p, rambuf->payload, cop, poff);
00745
00746 if(pbuf_header(rambuf, IP_HLEN)) {
00747 pbuf_free(rambuf);
00748 return ERR_MEM;
00749 }
00750
00751 SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
00752 iphdr = rambuf->payload;
00753 #else
00754
00755
00756
00757
00758
00759 rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
00760 if (rambuf == NULL) {
00761 return ERR_MEM;
00762 }
00763 LWIP_ASSERT("this needs a pbuf in one piece!",
00764 (p->len >= (IP_HLEN)));
00765 SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
00766 iphdr = (struct ip_hdr *)rambuf->payload;
00767
00768
00769 p->payload = (u8_t *)p->payload + poff;
00770 p->len -= poff;
00771
00772 left_to_copy = cop;
00773 while (left_to_copy) {
00774 struct pbuf_custom_ref *pcr;
00775 newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
00776
00777 if (!newpbuflen) {
00778 p = p->next;
00779 continue;
00780 }
00781 pcr = ip_frag_alloc_pbuf_custom_ref();
00782 if (pcr == NULL) {
00783 pbuf_free(rambuf);
00784 return ERR_MEM;
00785 }
00786
00787 newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
00788 if (newpbuf == NULL) {
00789 ip_frag_free_pbuf_custom_ref(pcr);
00790 pbuf_free(rambuf);
00791 return ERR_MEM;
00792 }
00793 pbuf_ref(p);
00794 pcr->original = p;
00795 pcr->pc.custom_free_function = ipfrag_free_pbuf_custom;
00796
00797
00798
00799
00800 pbuf_cat(rambuf, newpbuf);
00801 left_to_copy -= newpbuflen;
00802 if (left_to_copy) {
00803 p = p->next;
00804 }
00805 }
00806 poff = newpbuflen;
00807 #endif
00808 #endif
00809
00810
00811 IPH_OFFSET_SET(iphdr, htons(tmp));
00812 IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
00813 IPH_CHKSUM_SET(iphdr, 0);
00814 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
00815
00816 #if IP_FRAG_USES_STATIC_BUF
00817 if (last) {
00818 pbuf_realloc(rambuf, left + IP_HLEN);
00819 }
00820
00821
00822
00823
00824
00825
00826 header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
00827 if (header != NULL) {
00828 pbuf_chain(header, rambuf);
00829 netif->output(netif, header, dest);
00830 IPFRAG_STATS_INC(ip_frag.xmit);
00831 snmp_inc_ipfragcreates();
00832 pbuf_free(header);
00833 } else {
00834 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc() for header failed\n"));
00835 pbuf_free(rambuf);
00836 return ERR_MEM;
00837 }
00838 #else
00839
00840
00841
00842 netif->output(netif, rambuf, dest);
00843 IPFRAG_STATS_INC(ip_frag.xmit);
00844
00845
00846
00847
00848
00849
00850
00851
00852 pbuf_free(rambuf);
00853 #endif
00854 left -= cop;
00855 ofo += nfb;
00856 }
00857 #if IP_FRAG_USES_STATIC_BUF
00858 pbuf_free(rambuf);
00859 #endif
00860 snmp_inc_ipfragoks();
00861 return ERR_OK;
00862 }
00863 #endif