3 * This is the IPv4 packet segmentation and reassembly implementation.
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Jani Monoses <jani@iv.ro>
37 * original reassembly code by Adam Dunkels <adam@sics.se>
42 #include "lwip/ip_frag.h"
44 #include "lwip/inet.h"
45 #include "lwip/inet_chksum.h"
46 #include "lwip/netif.h"
47 #include "lwip/snmp.h"
48 #include "lwip/stats.h"
49 #include "lwip/icmp.h"
55 * The IP reassembly code currently has the following limitations:
56 * - IP header options are not supported
57 * - fragments must not overlap (e.g. due to different routes),
58 * currently, overlapping or duplicate fragments are thrown away
59 * if IP_REASS_CHECK_OVERLAP=1 (the default)!
61 * @todo: work with IP header options
64 /** Setting this to 0, you can turn off checking the fragments for overlapping
65 * regions. The code gets a little smaller. Only use this if you know that
66 * overlapping won't occur on your network! */
67 #ifndef IP_REASS_CHECK_OVERLAP
68 #define IP_REASS_CHECK_OVERLAP 1
69 #endif /* IP_REASS_CHECK_OVERLAP */
71 /** Set to 0 to prevent freeing the oldest datagram when the reassembly buffer is
72 * full (IP_REASS_MAX_PBUFS pbufs are enqueued). The code gets a little smaller.
73 * Datagrams will be freed by timeout only. Especially useful when MEMP_NUM_REASSDATA
74 * is set to 1, so one datagram can be reassembled at a time, only. */
75 #ifndef IP_REASS_FREE_OLDEST
76 #define IP_REASS_FREE_OLDEST 1
77 #endif /* IP_REASS_FREE_OLDEST */
79 #define IP_REASS_FLAG_LASTFRAG 0x01
81 /** This is a helper struct which holds the starting
82 * offset and the ending offset of this fragment to
83 * easily chain the fragments.
84 * It has to be packed since it has to fit inside the IP header.
86 #ifdef PACK_STRUCT_USE_INCLUDES
87 # include "arch/bpstruct.h"
90 struct ip_reass_helper
{
91 PACK_STRUCT_FIELD(struct pbuf
*next_pbuf
);
92 PACK_STRUCT_FIELD(u16_t start
);
93 PACK_STRUCT_FIELD(u16_t end
);
96 #ifdef PACK_STRUCT_USE_INCLUDES
97 # include "arch/epstruct.h"
100 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
101 (ip_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
102 ip_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
103 IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
105 /* global variables */
106 static struct ip_reassdata
*reassdatagrams
;
107 static u16_t ip_reass_pbufcount
;
109 /* function prototypes */
110 static void ip_reass_dequeue_datagram(struct ip_reassdata
*ipr
, struct ip_reassdata
*prev
);
111 static int ip_reass_free_complete_datagram(struct ip_reassdata
*ipr
, struct ip_reassdata
*prev
);
114 * Reassembly timer base function
115 * for both NO_SYS == 0 and 1 (!).
117 * Should be called every 1000 msec (defined by IP_TMR_INTERVAL).
122 struct ip_reassdata
*r
, *prev
= NULL
;
126 /* Decrement the timer. Once it reaches 0,
127 * clean up the incomplete fragment assembly */
130 LWIP_DEBUGF(IP_REASS_DEBUG
, ("ip_reass_tmr: timer dec %"U16_F
"\n",(u16_t
)r
->timer
));
134 /* reassembly timed out */
135 struct ip_reassdata
*tmp
;
136 LWIP_DEBUGF(IP_REASS_DEBUG
, ("ip_reass_tmr: timer timed out\n"));
138 /* get the next pointer before freeing */
140 /* free the helper struct and all enqueued pbufs */
141 ip_reass_free_complete_datagram(tmp
, prev
);
147 * Free a datagram (struct ip_reassdata) and all its pbufs.
148 * Updates the total count of enqueued pbufs (ip_reass_pbufcount),
149 * SNMP counters and sends an ICMP time exceeded packet.
151 * @param ipr datagram to free
152 * @param prev the previous datagram in the linked list
153 * @return the number of pbufs freed
156 ip_reass_free_complete_datagram(struct ip_reassdata
*ipr
, struct ip_reassdata
*prev
)
160 struct ip_reass_helper
*iprh
;
162 LWIP_ASSERT("prev != ipr", prev
!= ipr
);
164 LWIP_ASSERT("prev->next == ipr", prev
->next
== ipr
);
167 snmp_inc_ipreasmfails();
169 iprh
= (struct ip_reass_helper
*)ipr
->p
->payload
;
170 if (iprh
->start
== 0) {
171 /* The first fragment was received, send ICMP time exceeded. */
172 /* First, de-queue the first pbuf from r->p. */
174 ipr
->p
= iprh
->next_pbuf
;
175 /* Then, copy the original header into it. */
176 SMEMCPY(p
->payload
, &ipr
->iphdr
, IP_HLEN
);
177 icmp_time_exceeded(p
, ICMP_TE_FRAG
);
178 pbufs_freed
+= pbuf_clen(p
);
181 #endif /* LWIP_ICMP */
183 /* First, free all received pbufs. The individual pbufs need to be released
184 separately as they have not yet been chained */
188 iprh
= (struct ip_reass_helper
*)p
->payload
;
190 /* get the next pointer before freeing */
192 pbufs_freed
+= pbuf_clen(pcur
);
195 /* Then, unchain the struct ip_reassdata from the list and free it. */
196 ip_reass_dequeue_datagram(ipr
, prev
);
197 LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount
>= pbufs_freed
);
198 ip_reass_pbufcount
-= pbufs_freed
;
203 #if IP_REASS_FREE_OLDEST
205 * Free the oldest datagram to make room for enqueueing new fragments.
206 * The datagram 'fraghdr' belongs to is not freed!
208 * @param fraghdr IP header of the current fragment
209 * @param pbufs_needed number of pbufs needed to enqueue
210 * (used for freeing other datagrams if not enough space)
211 * @return the number of pbufs freed
214 ip_reass_remove_oldest_datagram(struct ip_hdr
*fraghdr
, int pbufs_needed
)
216 /* @todo Can't we simply remove the last datagram in the
217 * linked list behind reassdatagrams?
219 struct ip_reassdata
*r
, *oldest
, *prev
;
220 int pbufs_freed
= 0, pbufs_freed_current
;
223 /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
224 * but don't free the datagram that 'fraghdr' belongs to! */
231 if (!IP_ADDRESSES_AND_ID_MATCH(&r
->iphdr
, fraghdr
)) {
232 /* Not the same datagram as fraghdr */
234 if (oldest
== NULL
) {
236 } else if (r
->timer
<= oldest
->timer
) {
237 /* older than the previous oldest */
241 if (r
->next
!= NULL
) {
246 if (oldest
!= NULL
) {
247 pbufs_freed_current
= ip_reass_free_complete_datagram(oldest
, prev
);
248 pbufs_freed
+= pbufs_freed_current
;
250 } while ((pbufs_freed
< pbufs_needed
) && (other_datagrams
> 1));
253 #endif /* IP_REASS_FREE_OLDEST */
256 * Enqueues a new fragment into the fragment queue
257 * @param fraghdr points to the new fragments IP hdr
258 * @param clen number of pbufs needed to enqueue (used for freeing other datagrams if not enough space)
259 * @return A pointer to the queue location into which the fragment was enqueued
261 static struct ip_reassdata
*
262 ip_reass_enqueue_new_datagram(struct ip_hdr
*fraghdr
, int clen
)
264 struct ip_reassdata
* ipr
;
265 /* No matching previous fragment found, allocate a new reassdata struct */
266 ipr
= memp_malloc(MEMP_REASSDATA
);
268 #if IP_REASS_FREE_OLDEST
269 if (ip_reass_remove_oldest_datagram(fraghdr
, clen
) >= clen
) {
270 ipr
= memp_malloc(MEMP_REASSDATA
);
273 #endif /* IP_REASS_FREE_OLDEST */
275 IPFRAG_STATS_INC(ip_frag
.memerr
);
276 LWIP_DEBUGF(IP_REASS_DEBUG
,("Failed to alloc reassdata struct\n"));
280 memset(ipr
, 0, sizeof(struct ip_reassdata
));
281 ipr
->timer
= IP_REASS_MAXAGE
;
283 /* enqueue the new structure to the front of the list */
284 ipr
->next
= reassdatagrams
;
285 reassdatagrams
= ipr
;
286 /* copy the ip header for later tests and input */
287 /* @todo: no ip options supported? */
288 SMEMCPY(&(ipr
->iphdr
), fraghdr
, IP_HLEN
);
293 * Dequeues a datagram from the datagram queue. Doesn't deallocate the pbufs.
294 * @param ipr points to the queue entry to dequeue
297 ip_reass_dequeue_datagram(struct ip_reassdata
*ipr
, struct ip_reassdata
*prev
)
300 /* dequeue the reass struct */
301 if (reassdatagrams
== ipr
) {
302 /* it was the first in the list */
303 reassdatagrams
= ipr
->next
;
305 /* it wasn't the first, so it must have a valid 'prev' */
306 LWIP_ASSERT("sanity check linked list", prev
!= NULL
);
307 prev
->next
= ipr
->next
;
310 /* now we can free the ip_reass struct */
311 memp_free(MEMP_REASSDATA
, ipr
);
315 * Chain a new pbuf into the pbuf list that composes the datagram. The pbuf list
316 * will grow over time as new pbufs are rx.
317 * Also checks that the datagram passes basic continuity checks (if the last
318 * fragment was received at least once).
319 * @param root_p points to the 'root' pbuf for the current datagram being assembled.
320 * @param new_p points to the pbuf for the current fragment
321 * @return 0 if invalid, >0 otherwise
324 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata
*ipr
, struct pbuf
*new_p
)
326 struct ip_reass_helper
*iprh
, *iprh_tmp
, *iprh_prev
=NULL
;
329 struct ip_hdr
*fraghdr
;
332 /* Extract length and fragment offset from current fragment */
333 fraghdr
= (struct ip_hdr
*)new_p
->payload
;
334 len
= ntohs(IPH_LEN(fraghdr
)) - IPH_HL(fraghdr
) * 4;
335 offset
= (ntohs(IPH_OFFSET(fraghdr
)) & IP_OFFMASK
) * 8;
337 /* overwrite the fragment's ip header from the pbuf with our helper struct,
338 * and setup the embedded helper structure. */
339 /* make sure the struct ip_reass_helper fits into the IP header */
340 LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
341 sizeof(struct ip_reass_helper
) <= IP_HLEN
);
342 iprh
= (struct ip_reass_helper
*)new_p
->payload
;
343 iprh
->next_pbuf
= NULL
;
344 iprh
->start
= offset
;
345 iprh
->end
= offset
+ len
;
347 /* Iterate through until we either get to the end of the list (append),
348 * or we find on with a larger offset (insert). */
349 for (q
= ipr
->p
; q
!= NULL
;) {
350 iprh_tmp
= (struct ip_reass_helper
*)q
->payload
;
351 if (iprh
->start
< iprh_tmp
->start
) {
352 /* the new pbuf should be inserted before this */
354 if (iprh_prev
!= NULL
) {
355 /* not the fragment with the lowest offset */
356 #if IP_REASS_CHECK_OVERLAP
357 if ((iprh
->start
< iprh_prev
->end
) || (iprh
->end
> iprh_tmp
->start
)) {
358 /* fragment overlaps with previous or following, throw away */
361 #endif /* IP_REASS_CHECK_OVERLAP */
362 iprh_prev
->next_pbuf
= new_p
;
364 /* fragment with the lowest offset */
368 } else if(iprh
->start
== iprh_tmp
->start
) {
369 /* received the same datagram twice: no need to keep the datagram */
371 #if IP_REASS_CHECK_OVERLAP
372 } else if(iprh
->start
< iprh_tmp
->end
) {
373 /* overlap: no need to keep the new datagram */
375 #endif /* IP_REASS_CHECK_OVERLAP */
377 /* Check if the fragments received so far have no wholes. */
378 if (iprh_prev
!= NULL
) {
379 if (iprh_prev
->end
!= iprh_tmp
->start
) {
380 /* There is a fragment missing between the current
381 * and the previous fragment */
386 q
= iprh_tmp
->next_pbuf
;
387 iprh_prev
= iprh_tmp
;
390 /* If q is NULL, then we made it to the end of the list. Determine what to do now */
392 if (iprh_prev
!= NULL
) {
393 /* this is (for now), the fragment with the highest offset:
394 * chain it to the last fragment */
395 #if IP_REASS_CHECK_OVERLAP
396 LWIP_ASSERT("check fragments don't overlap", iprh_prev
->end
<= iprh
->start
);
397 #endif /* IP_REASS_CHECK_OVERLAP */
398 iprh_prev
->next_pbuf
= new_p
;
399 if (iprh_prev
->end
!= iprh
->start
) {
403 #if IP_REASS_CHECK_OVERLAP
404 LWIP_ASSERT("no previous fragment, this must be the first fragment!",
406 #endif /* IP_REASS_CHECK_OVERLAP */
407 /* this is the first fragment we ever received for this ip datagram */
412 /* At this point, the validation part begins: */
413 /* If we already received the last fragment */
414 if ((ipr
->flags
& IP_REASS_FLAG_LASTFRAG
) != 0) {
415 /* and had no wholes so far */
417 /* then check if the rest of the fragments is here */
418 /* Check if the queue starts with the first datagram */
419 if (((struct ip_reass_helper
*)ipr
->p
->payload
)->start
!= 0) {
422 /* and check that there are no wholes after this datagram */
426 iprh
= (struct ip_reass_helper
*)q
->payload
;
427 if (iprh_prev
->end
!= iprh
->start
) {
434 /* if still valid, all fragments are received
435 * (because to the MF==0 already arrived */
437 LWIP_ASSERT("sanity check", ipr
->p
!= NULL
);
438 LWIP_ASSERT("sanity check",
439 ((struct ip_reass_helper
*)ipr
->p
->payload
) != iprh
);
440 LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
441 iprh
->next_pbuf
== NULL
);
442 LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
443 iprh
->end
== ipr
->datagram_len
);
447 /* If valid is 0 here, there are some fragments missing in the middle
448 * (since MF == 0 has already arrived). Such datagrams simply time out if
449 * no more fragments are received... */
452 /* If we come here, not all fragments were received, yet! */
453 return 0; /* not yet valid! */
454 #if IP_REASS_CHECK_OVERLAP
456 ip_reass_pbufcount
-= pbuf_clen(new_p
);
459 #endif /* IP_REASS_CHECK_OVERLAP */
463 * Reassembles incoming IP fragments into an IP datagram.
465 * @param p points to a pbuf chain of the fragment
466 * @return NULL if reassembly is incomplete, ? otherwise
469 ip_reass(struct pbuf
*p
)
472 struct ip_hdr
*fraghdr
;
473 struct ip_reassdata
*ipr
;
474 struct ip_reass_helper
*iprh
;
477 struct ip_reassdata
*ipr_prev
= NULL
;
479 IPFRAG_STATS_INC(ip_frag
.recv
);
480 snmp_inc_ipreasmreqds();
482 fraghdr
= (struct ip_hdr
*)p
->payload
;
484 if ((IPH_HL(fraghdr
) * 4) != IP_HLEN
) {
485 LWIP_DEBUGF(IP_REASS_DEBUG
,("ip_reass: IP options currently not supported!\n"));
486 IPFRAG_STATS_INC(ip_frag
.err
);
490 offset
= (ntohs(IPH_OFFSET(fraghdr
)) & IP_OFFMASK
) * 8;
491 len
= ntohs(IPH_LEN(fraghdr
)) - IPH_HL(fraghdr
) * 4;
493 /* Check if we are allowed to enqueue more datagrams. */
495 if ((ip_reass_pbufcount
+ clen
) > IP_REASS_MAX_PBUFS
) {
496 #if IP_REASS_FREE_OLDEST
497 if (!ip_reass_remove_oldest_datagram(fraghdr
, clen
) ||
498 ((ip_reass_pbufcount
+ clen
) > IP_REASS_MAX_PBUFS
))
499 #endif /* IP_REASS_FREE_OLDEST */
501 /* No datagram could be freed and still too many pbufs enqueued */
502 LWIP_DEBUGF(IP_REASS_DEBUG
,("ip_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
503 ip_reass_pbufcount
, clen
, IP_REASS_MAX_PBUFS
));
504 IPFRAG_STATS_INC(ip_frag
.memerr
);
505 /* @todo: send ICMP time exceeded here? */
511 /* Look for the datagram the fragment belongs to in the current datagram queue,
512 * remembering the previous in the queue for later dequeueing. */
513 for (ipr
= reassdatagrams
; ipr
!= NULL
; ipr
= ipr
->next
) {
514 /* Check if the incoming fragment matches the one currently present
515 in the reassembly buffer. If so, we proceed with copying the
516 fragment into the buffer. */
517 if (IP_ADDRESSES_AND_ID_MATCH(&ipr
->iphdr
, fraghdr
)) {
518 LWIP_DEBUGF(IP_REASS_DEBUG
, ("ip_reass: matching previous fragment ID=%"X16_F
"\n",
519 ntohs(IPH_ID(fraghdr
))));
520 IPFRAG_STATS_INC(ip_frag
.cachehit
);
527 /* Enqueue a new datagram into the datagram queue */
528 ipr
= ip_reass_enqueue_new_datagram(fraghdr
, clen
);
529 /* Bail if unable to enqueue */
534 if (((ntohs(IPH_OFFSET(fraghdr
)) & IP_OFFMASK
) == 0) &&
535 ((ntohs(IPH_OFFSET(&ipr
->iphdr
)) & IP_OFFMASK
) != 0)) {
536 /* ipr->iphdr is not the header from the first fragment, but fraghdr is
537 * -> copy fraghdr into ipr->iphdr since we want to have the header
538 * of the first fragment (for ICMP time exceeded and later, for copying
539 * all options, if supported)*/
540 SMEMCPY(&ipr
->iphdr
, fraghdr
, IP_HLEN
);
543 /* Track the current number of pbufs current 'in-flight', in order to limit
544 the number of fragments that may be enqueued at any one time */
545 ip_reass_pbufcount
+= clen
;
547 /* At this point, we have either created a new entry or pointing
548 * to an existing one */
550 /* check for 'no more fragments', and update queue entry*/
551 if ((ntohs(IPH_OFFSET(fraghdr
)) & IP_MF
) == 0) {
552 ipr
->flags
|= IP_REASS_FLAG_LASTFRAG
;
553 ipr
->datagram_len
= offset
+ len
;
554 LWIP_DEBUGF(IP_REASS_DEBUG
,
555 ("ip_reass: last fragment seen, total len %"S16_F
"\n",
558 /* find the right place to insert this pbuf */
559 /* @todo: trim pbufs if fragments are overlapping */
560 if (ip_reass_chain_frag_into_datagram_and_validate(ipr
, p
)) {
561 /* the totally last fragment (flag more fragments = 0) was received at least
562 * once AND all fragments are received */
563 ipr
->datagram_len
+= IP_HLEN
;
565 /* save the second pbuf before copying the header over the pointer */
566 r
= ((struct ip_reass_helper
*)ipr
->p
->payload
)->next_pbuf
;
568 /* copy the original ip header back to the first pbuf */
569 fraghdr
= (struct ip_hdr
*)(ipr
->p
->payload
);
570 SMEMCPY(fraghdr
, &ipr
->iphdr
, IP_HLEN
);
571 IPH_LEN_SET(fraghdr
, htons(ipr
->datagram_len
));
572 IPH_OFFSET_SET(fraghdr
, 0);
573 IPH_CHKSUM_SET(fraghdr
, 0);
574 /* @todo: do we need to set calculate the correct checksum? */
575 IPH_CHKSUM_SET(fraghdr
, inet_chksum(fraghdr
, IP_HLEN
));
579 /* chain together the pbufs contained within the reass_data list. */
581 iprh
= (struct ip_reass_helper
*)r
->payload
;
583 /* hide the ip header for every succeding fragment */
584 pbuf_header(r
, -IP_HLEN
);
588 /* release the sources allocate for the fragment queue entry */
589 ip_reass_dequeue_datagram(ipr
, ipr_prev
);
591 /* and adjust the number of pbufs currently queued for reassembly. */
592 ip_reass_pbufcount
-= pbuf_clen(p
);
594 /* Return the pbuf chain */
597 /* the datagram is not (yet?) reassembled completely */
598 LWIP_DEBUGF(IP_REASS_DEBUG
,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount
));
602 LWIP_DEBUGF(IP_REASS_DEBUG
,("ip_reass: nullreturn\n"));
603 IPFRAG_STATS_INC(ip_frag
.drop
);
607 #endif /* IP_REASSEMBLY */
610 #if IP_FRAG_USES_STATIC_BUF
611 static u8_t buf
[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU
+ MEM_ALIGNMENT
- 1)];
612 #endif /* IP_FRAG_USES_STATIC_BUF */
615 * Fragment an IP datagram if too large for the netif.
617 * Chop the datagram in MTU sized chunks and send them in order
618 * by using a fixed size static memory buffer (PBUF_REF) or
619 * point PBUF_REFs into p (depending on IP_FRAG_USES_STATIC_BUF).
621 * @param p ip packet to send
622 * @param netif the netif on which to send
623 * @param dest destination ip address to which to send
625 * @return ERR_OK if sent successfully, err_t otherwise
628 ip_frag(struct pbuf
*p
, struct netif
*netif
, struct ip_addr
*dest
)
631 #if IP_FRAG_USES_STATIC_BUF
634 struct pbuf
*newpbuf
;
635 struct ip_hdr
*original_iphdr
;
637 struct ip_hdr
*iphdr
;
640 u16_t mtu
= netif
->mtu
;
643 u16_t poff
= IP_HLEN
;
645 #if !IP_FRAG_USES_STATIC_BUF
646 u16_t newpbuflen
= 0;
650 /* Get a RAM based MTU sized pbuf */
651 #if IP_FRAG_USES_STATIC_BUF
652 /* When using a static buffer, we use a PBUF_REF, which we will
653 * use to reference the packet (without link header).
654 * Layer and length is irrelevant.
656 rambuf
= pbuf_alloc(PBUF_LINK
, 0, PBUF_REF
);
657 if (rambuf
== NULL
) {
658 LWIP_DEBUGF(IP_REASS_DEBUG
, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
661 rambuf
->tot_len
= rambuf
->len
= mtu
;
662 rambuf
->payload
= LWIP_MEM_ALIGN((void *)buf
);
664 /* Copy the IP header in it */
665 iphdr
= rambuf
->payload
;
666 SMEMCPY(iphdr
, p
->payload
, IP_HLEN
);
667 #else /* IP_FRAG_USES_STATIC_BUF */
668 original_iphdr
= p
->payload
;
669 iphdr
= original_iphdr
;
670 #endif /* IP_FRAG_USES_STATIC_BUF */
672 /* Save original offset */
673 tmp
= ntohs(IPH_OFFSET(iphdr
));
674 ofo
= tmp
& IP_OFFMASK
;
677 left
= p
->tot_len
- IP_HLEN
;
679 nfb
= (mtu
- IP_HLEN
) / 8;
682 last
= (left
<= mtu
- IP_HLEN
);
684 /* Set new offset and MF flag */
685 tmp
= omf
| (IP_OFFMASK
& (ofo
));
689 /* Fill this fragment */
690 cop
= last
? left
: nfb
* 8;
692 #if IP_FRAG_USES_STATIC_BUF
693 poff
+= pbuf_copy_partial(p
, (u8_t
*)iphdr
+ IP_HLEN
, cop
, poff
);
694 #else /* IP_FRAG_USES_STATIC_BUF */
695 /* When not using a static buffer, create a chain of pbufs.
696 * The first will be a PBUF_RAM holding the link and IP header.
697 * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
698 * but limited to the size of an mtu.
700 rambuf
= pbuf_alloc(PBUF_LINK
, IP_HLEN
, PBUF_RAM
);
701 if (rambuf
== NULL
) {
704 LWIP_ASSERT("this needs a pbuf in one piece!",
705 (p
->len
>= (IP_HLEN
)));
706 SMEMCPY(rambuf
->payload
, original_iphdr
, IP_HLEN
);
707 iphdr
= rambuf
->payload
;
709 /* Can just adjust p directly for needed offset. */
710 p
->payload
= (u8_t
*)p
->payload
+ poff
;
714 while (left_to_copy
) {
715 newpbuflen
= (left_to_copy
< p
->len
) ? left_to_copy
: p
->len
;
716 /* Is this pbuf already empty? */
721 newpbuf
= pbuf_alloc(PBUF_RAW
, 0, PBUF_REF
);
722 if (newpbuf
== NULL
) {
726 /* Mirror this pbuf, although we might not need all of it. */
727 newpbuf
->payload
= p
->payload
;
728 newpbuf
->len
= newpbuf
->tot_len
= newpbuflen
;
729 /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
730 * so that it is removed when pbuf_dechain is later called on rambuf.
732 pbuf_cat(rambuf
, newpbuf
);
733 left_to_copy
-= newpbuflen
;
738 #endif /* IP_FRAG_USES_STATIC_BUF */
741 IPH_OFFSET_SET(iphdr
, htons(tmp
));
742 IPH_LEN_SET(iphdr
, htons(cop
+ IP_HLEN
));
743 IPH_CHKSUM_SET(iphdr
, 0);
744 IPH_CHKSUM_SET(iphdr
, inet_chksum(iphdr
, IP_HLEN
));
746 #if IP_FRAG_USES_STATIC_BUF
748 pbuf_realloc(rambuf
, left
+ IP_HLEN
);
750 /* This part is ugly: we alloc a RAM based pbuf for
751 * the link level header for each chunk and then
752 * free it.A PBUF_ROM style pbuf for which pbuf_header
753 * worked would make things simpler.
755 header
= pbuf_alloc(PBUF_LINK
, 0, PBUF_RAM
);
756 if (header
!= NULL
) {
757 pbuf_chain(header
, rambuf
);
758 netif
->output(netif
, header
, dest
);
759 IPFRAG_STATS_INC(ip_frag
.xmit
);
760 snmp_inc_ipfragcreates();
763 LWIP_DEBUGF(IP_REASS_DEBUG
, ("ip_frag: pbuf_alloc() for header failed\n"));
767 #else /* IP_FRAG_USES_STATIC_BUF */
768 /* No need for separate header pbuf - we allowed room for it in rambuf
771 netif
->output(netif
, rambuf
, dest
);
772 IPFRAG_STATS_INC(ip_frag
.xmit
);
774 /* Unfortunately we can't reuse rambuf - the hardware may still be
775 * using the buffer. Instead we free it (and the ensuing chain) and
776 * recreate it next time round the loop. If we're lucky the hardware
777 * will have already sent the packet, the free will really free, and
778 * there will be zero memory penalty.
782 #endif /* IP_FRAG_USES_STATIC_BUF */
786 #if IP_FRAG_USES_STATIC_BUF
788 #endif /* IP_FRAG_USES_STATIC_BUF */
789 snmp_inc_ipfragoks();