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_chksum.h"
45 #include "lwip/netif.h"
46 #include "lwip/snmp.h"
47 #include "lwip/stats.h"
48 #include "lwip/icmp.h"
54 * The IP reassembly code currently has the following limitations:
55 * - IP header options are not supported
56 * - fragments must not overlap (e.g. due to different routes),
57 * currently, overlapping or duplicate fragments are thrown away
58 * if IP_REASS_CHECK_OVERLAP=1 (the default)!
60 * @todo: work with IP header options
63 /** Setting this to 0, you can turn off checking the fragments for overlapping
64 * regions. The code gets a little smaller. Only use this if you know that
65 * overlapping won't occur on your network! */
66 #ifndef IP_REASS_CHECK_OVERLAP
67 #define IP_REASS_CHECK_OVERLAP 1
68 #endif /* IP_REASS_CHECK_OVERLAP */
70 /** Set to 0 to prevent freeing the oldest datagram when the reassembly buffer is
71 * full (IP_REASS_MAX_PBUFS pbufs are enqueued). The code gets a little smaller.
72 * Datagrams will be freed by timeout only. Especially useful when MEMP_NUM_REASSDATA
73 * is set to 1, so one datagram can be reassembled at a time, only. */
74 #ifndef IP_REASS_FREE_OLDEST
75 #define IP_REASS_FREE_OLDEST 1
76 #endif /* IP_REASS_FREE_OLDEST */
78 #define IP_REASS_FLAG_LASTFRAG 0x01
80 /** This is a helper struct which holds the starting
81 * offset and the ending offset of this fragment to
82 * easily chain the fragments.
83 * It has the same packing requirements as the IP header, since it replaces
84 * the IP header in memory in incoming fragments (after copying it) to keep
85 * track of the various fragments. (-> If the IP header doesn't need packing,
86 * this struct doesn't need packing, too.)
88 #ifdef PACK_STRUCT_USE_INCLUDES
89 # include "arch/bpstruct.h"
92 struct ip_reass_helper {
93 PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
94 PACK_STRUCT_FIELD(u16_t start);
95 PACK_STRUCT_FIELD(u16_t end);
98 #ifdef PACK_STRUCT_USE_INCLUDES
99 # include "arch/epstruct.h"
102 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
103 (ip_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
104 ip_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
105 IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
107 /* global variables */
108 static struct ip_reassdata *reassdatagrams;
109 static u16_t ip_reass_pbufcount;
111 /* function prototypes */
112 static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
113 static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
116 * Reassembly timer base function
117 * for both NO_SYS == 0 and 1 (!).
119 * Should be called every 1000 msec (defined by IP_TMR_INTERVAL).
124 struct ip_reassdata *r, *prev = NULL;
128 /* Decrement the timer. Once it reaches 0,
129 * clean up the incomplete fragment assembly */
132 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)r->timer));
136 /* reassembly timed out */
137 struct ip_reassdata *tmp;
138 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
140 /* get the next pointer before freeing */
142 /* free the helper struct and all enqueued pbufs */
143 ip_reass_free_complete_datagram(tmp, prev);
149 * Free a datagram (struct ip_reassdata) and all its pbufs.
150 * Updates the total count of enqueued pbufs (ip_reass_pbufcount),
151 * SNMP counters and sends an ICMP time exceeded packet.
153 * @param ipr datagram to free
154 * @param prev the previous datagram in the linked list
155 * @return the number of pbufs freed
158 ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
160 u16_t pbufs_freed = 0;
163 struct ip_reass_helper *iprh;
165 LWIP_ASSERT("prev != ipr", prev != ipr);
167 LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
170 snmp_inc_ipreasmfails();
172 iprh = (struct ip_reass_helper *)ipr->p->payload;
173 if (iprh->start == 0) {
174 /* The first fragment was received, send ICMP time exceeded. */
175 /* First, de-queue the first pbuf from r->p. */
177 ipr->p = iprh->next_pbuf;
178 /* Then, copy the original header into it. */
179 SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
180 icmp_time_exceeded(p, ICMP_TE_FRAG);
182 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
186 #endif /* LWIP_ICMP */
188 /* First, free all received pbufs. The individual pbufs need to be released
189 separately as they have not yet been chained */
193 iprh = (struct ip_reass_helper *)p->payload;
195 /* get the next pointer before freeing */
197 clen = pbuf_clen(pcur);
198 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
202 /* Then, unchain the struct ip_reassdata from the list and free it. */
203 ip_reass_dequeue_datagram(ipr, prev);
204 LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= pbufs_freed);
205 ip_reass_pbufcount -= pbufs_freed;
210 #if IP_REASS_FREE_OLDEST
212 * Free the oldest datagram to make room for enqueueing new fragments.
213 * The datagram 'fraghdr' belongs to is not freed!
215 * @param fraghdr IP header of the current fragment
216 * @param pbufs_needed number of pbufs needed to enqueue
217 * (used for freeing other datagrams if not enough space)
218 * @return the number of pbufs freed
221 ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
223 /* @todo Can't we simply remove the last datagram in the
224 * linked list behind reassdatagrams?
226 struct ip_reassdata *r, *oldest, *prev;
227 int pbufs_freed = 0, pbufs_freed_current;
230 /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
231 * but don't free the datagram that 'fraghdr' belongs to! */
238 if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
239 /* Not the same datagram as fraghdr */
241 if (oldest == NULL) {
243 } else if (r->timer <= oldest->timer) {
244 /* older than the previous oldest */
248 if (r->next != NULL) {
253 if (oldest != NULL) {
254 pbufs_freed_current = ip_reass_free_complete_datagram(oldest, prev);
255 pbufs_freed += pbufs_freed_current;
257 } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
260 #endif /* IP_REASS_FREE_OLDEST */
263 * Enqueues a new fragment into the fragment queue
264 * @param fraghdr points to the new fragments IP hdr
265 * @param clen number of pbufs needed to enqueue (used for freeing other datagrams if not enough space)
266 * @return A pointer to the queue location into which the fragment was enqueued
268 static struct ip_reassdata*
269 ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
271 struct ip_reassdata* ipr;
272 /* No matching previous fragment found, allocate a new reassdata struct */
273 ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
275 #if IP_REASS_FREE_OLDEST
276 if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
277 ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
280 #endif /* IP_REASS_FREE_OLDEST */
282 IPFRAG_STATS_INC(ip_frag.memerr);
283 LWIP_DEBUGF(IP_REASS_DEBUG,("Failed to alloc reassdata struct\n"));
287 memset(ipr, 0, sizeof(struct ip_reassdata));
288 ipr->timer = IP_REASS_MAXAGE;
290 /* enqueue the new structure to the front of the list */
291 ipr->next = reassdatagrams;
292 reassdatagrams = ipr;
293 /* copy the ip header for later tests and input */
294 /* @todo: no ip options supported? */
295 SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
300 * Dequeues a datagram from the datagram queue. Doesn't deallocate the pbufs.
301 * @param ipr points to the queue entry to dequeue
304 ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
307 /* dequeue the reass struct */
308 if (reassdatagrams == ipr) {
309 /* it was the first in the list */
310 reassdatagrams = ipr->next;
312 /* it wasn't the first, so it must have a valid 'prev' */
313 LWIP_ASSERT("sanity check linked list", prev != NULL);
314 prev->next = ipr->next;
317 /* now we can free the ip_reass struct */
318 memp_free(MEMP_REASSDATA, ipr);
322 * Chain a new pbuf into the pbuf list that composes the datagram. The pbuf list
323 * will grow over time as new pbufs are rx.
324 * Also checks that the datagram passes basic continuity checks (if the last
325 * fragment was received at least once).
326 * @param root_p points to the 'root' pbuf for the current datagram being assembled.
327 * @param new_p points to the pbuf for the current fragment
328 * @return 0 if invalid, >0 otherwise
331 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p)
333 struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
336 struct ip_hdr *fraghdr;
339 /* Extract length and fragment offset from current fragment */
340 fraghdr = (struct ip_hdr*)new_p->payload;
341 len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
342 offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
344 /* overwrite the fragment's ip header from the pbuf with our helper struct,
345 * and setup the embedded helper structure. */
346 /* make sure the struct ip_reass_helper fits into the IP header */
347 LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
348 sizeof(struct ip_reass_helper) <= IP_HLEN);
349 iprh = (struct ip_reass_helper*)new_p->payload;
350 iprh->next_pbuf = NULL;
351 iprh->start = offset;
352 iprh->end = offset + len;
354 /* Iterate through until we either get to the end of the list (append),
355 * or we find on with a larger offset (insert). */
356 for (q = ipr->p; q != NULL;) {
357 iprh_tmp = (struct ip_reass_helper*)q->payload;
358 if (iprh->start < iprh_tmp->start) {
359 /* the new pbuf should be inserted before this */
361 if (iprh_prev != NULL) {
362 /* not the fragment with the lowest offset */
363 #if IP_REASS_CHECK_OVERLAP
364 if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
365 /* fragment overlaps with previous or following, throw away */
368 #endif /* IP_REASS_CHECK_OVERLAP */
369 iprh_prev->next_pbuf = new_p;
371 /* fragment with the lowest offset */
375 } else if(iprh->start == iprh_tmp->start) {
376 /* received the same datagram twice: no need to keep the datagram */
378 #if IP_REASS_CHECK_OVERLAP
379 } else if(iprh->start < iprh_tmp->end) {
380 /* overlap: no need to keep the new datagram */
382 #endif /* IP_REASS_CHECK_OVERLAP */
384 /* Check if the fragments received so far have no wholes. */
385 if (iprh_prev != NULL) {
386 if (iprh_prev->end != iprh_tmp->start) {
387 /* There is a fragment missing between the current
388 * and the previous fragment */
393 q = iprh_tmp->next_pbuf;
394 iprh_prev = iprh_tmp;
397 /* If q is NULL, then we made it to the end of the list. Determine what to do now */
399 if (iprh_prev != NULL) {
400 /* this is (for now), the fragment with the highest offset:
401 * chain it to the last fragment */
402 #if IP_REASS_CHECK_OVERLAP
403 LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
404 #endif /* IP_REASS_CHECK_OVERLAP */
405 iprh_prev->next_pbuf = new_p;
406 if (iprh_prev->end != iprh->start) {
410 #if IP_REASS_CHECK_OVERLAP
411 LWIP_ASSERT("no previous fragment, this must be the first fragment!",
413 #endif /* IP_REASS_CHECK_OVERLAP */
414 /* this is the first fragment we ever received for this ip datagram */
419 /* At this point, the validation part begins: */
420 /* If we already received the last fragment */
421 if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {
422 /* and had no wholes so far */
424 /* then check if the rest of the fragments is here */
425 /* Check if the queue starts with the first datagram */
426 if (((struct ip_reass_helper*)ipr->p->payload)->start != 0) {
429 /* and check that there are no wholes after this datagram */
433 iprh = (struct ip_reass_helper*)q->payload;
434 if (iprh_prev->end != iprh->start) {
441 /* if still valid, all fragments are received
442 * (because to the MF==0 already arrived */
444 LWIP_ASSERT("sanity check", ipr->p != NULL);
445 LWIP_ASSERT("sanity check",
446 ((struct ip_reass_helper*)ipr->p->payload) != iprh);
447 LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
448 iprh->next_pbuf == NULL);
449 LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
450 iprh->end == ipr->datagram_len);
454 /* If valid is 0 here, there are some fragments missing in the middle
455 * (since MF == 0 has already arrived). Such datagrams simply time out if
456 * no more fragments are received... */
459 /* If we come here, not all fragments were received, yet! */
460 return 0; /* not yet valid! */
461 #if IP_REASS_CHECK_OVERLAP
463 ip_reass_pbufcount -= pbuf_clen(new_p);
466 #endif /* IP_REASS_CHECK_OVERLAP */
470 * Reassembles incoming IP fragments into an IP datagram.
472 * @param p points to a pbuf chain of the fragment
473 * @return NULL if reassembly is incomplete, ? otherwise
476 ip_reass(struct pbuf *p)
479 struct ip_hdr *fraghdr;
480 struct ip_reassdata *ipr;
481 struct ip_reass_helper *iprh;
484 struct ip_reassdata *ipr_prev = NULL;
486 IPFRAG_STATS_INC(ip_frag.recv);
487 snmp_inc_ipreasmreqds();
489 fraghdr = (struct ip_hdr*)p->payload;
491 if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {
492 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: IP options currently not supported!\n"));
493 IPFRAG_STATS_INC(ip_frag.err);
497 offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
498 len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
500 /* Check if we are allowed to enqueue more datagrams. */
502 if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
503 #if IP_REASS_FREE_OLDEST
504 if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
505 ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
506 #endif /* IP_REASS_FREE_OLDEST */
508 /* No datagram could be freed and still too many pbufs enqueued */
509 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
510 ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
511 IPFRAG_STATS_INC(ip_frag.memerr);
512 /* @todo: send ICMP time exceeded here? */
518 /* Look for the datagram the fragment belongs to in the current datagram queue,
519 * remembering the previous in the queue for later dequeueing. */
520 for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
521 /* Check if the incoming fragment matches the one currently present
522 in the reassembly buffer. If so, we proceed with copying the
523 fragment into the buffer. */
524 if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
525 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching previous fragment ID=%"X16_F"\n",
526 ntohs(IPH_ID(fraghdr))));
527 IPFRAG_STATS_INC(ip_frag.cachehit);
534 /* Enqueue a new datagram into the datagram queue */
535 ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
536 /* Bail if unable to enqueue */
541 if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
542 ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
543 /* ipr->iphdr is not the header from the first fragment, but fraghdr is
544 * -> copy fraghdr into ipr->iphdr since we want to have the header
545 * of the first fragment (for ICMP time exceeded and later, for copying
546 * all options, if supported)*/
547 SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
550 /* Track the current number of pbufs current 'in-flight', in order to limit
551 the number of fragments that may be enqueued at any one time */
552 ip_reass_pbufcount += clen;
554 /* At this point, we have either created a new entry or pointing
555 * to an existing one */
557 /* check for 'no more fragments', and update queue entry*/
558 if ((IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0) {
559 ipr->flags |= IP_REASS_FLAG_LASTFRAG;
560 ipr->datagram_len = offset + len;
561 LWIP_DEBUGF(IP_REASS_DEBUG,
562 ("ip_reass: last fragment seen, total len %"S16_F"\n",
565 /* find the right place to insert this pbuf */
566 /* @todo: trim pbufs if fragments are overlapping */
567 if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {
568 /* the totally last fragment (flag more fragments = 0) was received at least
569 * once AND all fragments are received */
570 ipr->datagram_len += IP_HLEN;
572 /* save the second pbuf before copying the header over the pointer */
573 r = ((struct ip_reass_helper*)ipr->p->payload)->next_pbuf;
575 /* copy the original ip header back to the first pbuf */
576 fraghdr = (struct ip_hdr*)(ipr->p->payload);
577 SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
578 IPH_LEN_SET(fraghdr, htons(ipr->datagram_len));
579 IPH_OFFSET_SET(fraghdr, 0);
580 IPH_CHKSUM_SET(fraghdr, 0);
581 /* @todo: do we need to set calculate the correct checksum? */
582 IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
586 /* chain together the pbufs contained within the reass_data list. */
588 iprh = (struct ip_reass_helper*)r->payload;
590 /* hide the ip header for every succeding fragment */
591 pbuf_header(r, -IP_HLEN);
595 /* release the sources allocate for the fragment queue entry */
596 ip_reass_dequeue_datagram(ipr, ipr_prev);
598 /* and adjust the number of pbufs currently queued for reassembly. */
599 ip_reass_pbufcount -= pbuf_clen(p);
601 /* Return the pbuf chain */
604 /* the datagram is not (yet?) reassembled completely */
605 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
609 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: nullreturn\n"));
610 IPFRAG_STATS_INC(ip_frag.drop);
614 #endif /* IP_REASSEMBLY */
617 #if IP_FRAG_USES_STATIC_BUF
618 static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)];
619 #endif /* IP_FRAG_USES_STATIC_BUF */
622 * Fragment an IP datagram if too large for the netif.
624 * Chop the datagram in MTU sized chunks and send them in order
625 * by using a fixed size static memory buffer (PBUF_REF) or
626 * point PBUF_REFs into p (depending on IP_FRAG_USES_STATIC_BUF).
628 * @param p ip packet to send
629 * @param netif the netif on which to send
630 * @param dest destination ip address to which to send
632 * @return ERR_OK if sent successfully, err_t otherwise
635 ip_frag(struct pbuf *p, struct netif *netif, ip_addr_t *dest)
638 #if IP_FRAG_USES_STATIC_BUF
641 #if !LWIP_NETIF_TX_SINGLE_PBUF
642 struct pbuf *newpbuf;
644 struct ip_hdr *original_iphdr;
646 struct ip_hdr *iphdr;
649 u16_t mtu = netif->mtu;
652 u16_t poff = IP_HLEN;
654 #if !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF
655 u16_t newpbuflen = 0;
659 /* Get a RAM based MTU sized pbuf */
660 #if IP_FRAG_USES_STATIC_BUF
661 /* When using a static buffer, we use a PBUF_REF, which we will
662 * use to reference the packet (without link header).
663 * Layer and length is irrelevant.
665 rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
666 if (rambuf == NULL) {
667 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
670 rambuf->tot_len = rambuf->len = mtu;
671 rambuf->payload = LWIP_MEM_ALIGN((void *)buf);
673 /* Copy the IP header in it */
674 iphdr = (struct ip_hdr *)rambuf->payload;
675 SMEMCPY(iphdr, p->payload, IP_HLEN);
676 #else /* IP_FRAG_USES_STATIC_BUF */
677 original_iphdr = p->payload;
678 iphdr = original_iphdr;
679 #endif /* IP_FRAG_USES_STATIC_BUF */
681 /* Save original offset */
682 tmp = ntohs(IPH_OFFSET(iphdr));
683 ofo = tmp & IP_OFFMASK;
686 left = p->tot_len - IP_HLEN;
688 nfb = (mtu - IP_HLEN) / 8;
691 last = (left <= mtu - IP_HLEN);
693 /* Set new offset and MF flag */
694 tmp = omf | (IP_OFFMASK & (ofo));
699 /* Fill this fragment */
700 cop = last ? left : nfb * 8;
702 #if IP_FRAG_USES_STATIC_BUF
703 poff += pbuf_copy_partial(p, (u8_t*)iphdr + IP_HLEN, cop, poff);
704 #else /* IP_FRAG_USES_STATIC_BUF */
705 #if LWIP_NETIF_TX_SINGLE_PBUF
706 rambuf = pbuf_alloc(PBUF_IP, cop, PBUF_RAM);
707 if (rambuf == NULL) {
710 LWIP_ASSERT("this needs a pbuf in one piece!",
711 (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
712 poff += pbuf_copy_partial(p, rambuf->payload, cop, poff);
713 /* make room for the IP header */
714 if(pbuf_header(rambuf, IP_HLEN)) {
718 /* fill in the IP header */
719 SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
720 iphdr = rambuf->payload;
721 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
722 /* When not using a static buffer, create a chain of pbufs.
723 * The first will be a PBUF_RAM holding the link and IP header.
724 * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
725 * but limited to the size of an mtu.
727 rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
728 if (rambuf == NULL) {
731 LWIP_ASSERT("this needs a pbuf in one piece!",
732 (p->len >= (IP_HLEN)));
733 SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
734 iphdr = rambuf->payload;
736 /* Can just adjust p directly for needed offset. */
737 p->payload = (u8_t *)p->payload + poff;
741 while (left_to_copy) {
742 newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
743 /* Is this pbuf already empty? */
748 newpbuf = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
749 if (newpbuf == NULL) {
753 /* Mirror this pbuf, although we might not need all of it. */
754 newpbuf->payload = p->payload;
755 newpbuf->len = newpbuf->tot_len = newpbuflen;
756 /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
757 * so that it is removed when pbuf_dechain is later called on rambuf.
759 pbuf_cat(rambuf, newpbuf);
760 left_to_copy -= newpbuflen;
766 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
767 #endif /* IP_FRAG_USES_STATIC_BUF */
770 IPH_OFFSET_SET(iphdr, htons(tmp));
771 IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
772 IPH_CHKSUM_SET(iphdr, 0);
773 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
775 #if IP_FRAG_USES_STATIC_BUF
777 pbuf_realloc(rambuf, left + IP_HLEN);
780 /* This part is ugly: we alloc a RAM based pbuf for
781 * the link level header for each chunk and then
782 * free it.A PBUF_ROM style pbuf for which pbuf_header
783 * worked would make things simpler.
785 header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
786 if (header != NULL) {
787 pbuf_chain(header, rambuf);
788 netif->output(netif, header, dest);
789 IPFRAG_STATS_INC(ip_frag.xmit);
790 snmp_inc_ipfragcreates();
793 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc() for header failed\n"));
797 #else /* IP_FRAG_USES_STATIC_BUF */
798 /* No need for separate header pbuf - we allowed room for it in rambuf
801 netif->output(netif, rambuf, dest);
802 IPFRAG_STATS_INC(ip_frag.xmit);
804 /* Unfortunately we can't reuse rambuf - the hardware may still be
805 * using the buffer. Instead we free it (and the ensuing chain) and
806 * recreate it next time round the loop. If we're lucky the hardware
807 * will have already sent the packet, the free will really free, and
808 * there will be zero memory penalty.
812 #endif /* IP_FRAG_USES_STATIC_BUF */
816 #if IP_FRAG_USES_STATIC_BUF
818 #endif /* IP_FRAG_USES_STATIC_BUF */
819 snmp_inc_ipfragoks();