STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
ip_frag.c
Go to the documentation of this file.
1 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
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.
21  *
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
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Jani Monoses <jani@iv.ro>
36  * Simon Goldschmidt
37  * original reassembly code by Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 #include "lwip/opt.h"
42 
43 #if LWIP_IPV4
44 
45 #include "lwip/ip_frag.h"
46 #include "lwip/def.h"
47 #include "lwip/inet_chksum.h"
48 #include "lwip/netif.h"
49 #include "lwip/stats.h"
50 #include "lwip/icmp.h"
51 
52 #include <string.h>
53 
54 #if IP_REASSEMBLY
55 
68 #ifndef IP_REASS_CHECK_OVERLAP
69 #define IP_REASS_CHECK_OVERLAP 1
70 #endif /* IP_REASS_CHECK_OVERLAP */
71 
76 #ifndef IP_REASS_FREE_OLDEST
77 #define IP_REASS_FREE_OLDEST 1
78 #endif /* IP_REASS_FREE_OLDEST */
79 
80 #define IP_REASS_FLAG_LASTFRAG 0x01
81 
90 #ifdef PACK_STRUCT_USE_INCLUDES
91 # include "arch/bpstruct.h"
92 #endif
94 struct ip_reass_helper {
95  PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
96  PACK_STRUCT_FIELD(u16_t start);
100 #ifdef PACK_STRUCT_USE_INCLUDES
101 # include "arch/epstruct.h"
102 #endif
103 
104 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
105  (ip4_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
106  ip4_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
107  IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
108 
109 /* global variables */
110 static struct ip_reassdata *reassdatagrams;
111 static u16_t ip_reass_pbufcount;
112 
113 /* function prototypes */
114 static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
115 static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
116 
123 void
124 ip_reass_tmr(void)
125 {
126  struct ip_reassdata *r, *prev = NULL;
127 
128  r = reassdatagrams;
129  while (r != NULL) {
130  /* Decrement the timer. Once it reaches 0,
131  * clean up the incomplete fragment assembly */
132  if (r->timer > 0) {
133  r->timer--;
134  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)r->timer));
135  prev = r;
136  r = r->next;
137  } else {
138  /* reassembly timed out */
139  struct ip_reassdata *tmp;
140  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
141  tmp = r;
142  /* get the next pointer before freeing */
143  r = r->next;
144  /* free the helper struct and all enqueued pbufs */
145  ip_reass_free_complete_datagram(tmp, prev);
146  }
147  }
148 }
149 
159 static int
160 ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
161 {
162  u16_t pbufs_freed = 0;
163  u8_t clen;
164  struct pbuf *p;
165  struct ip_reass_helper *iprh;
166 
167  LWIP_ASSERT("prev != ipr", prev != ipr);
168  if (prev != NULL) {
169  LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
170  }
171 
172  MIB2_STATS_INC(mib2.ipreasmfails);
173 #if LWIP_ICMP
174  iprh = (struct ip_reass_helper *)ipr->p->payload;
175  if (iprh->start == 0) {
176  /* The first fragment was received, send ICMP time exceeded. */
177  /* First, de-queue the first pbuf from r->p. */
178  p = ipr->p;
179  ipr->p = iprh->next_pbuf;
180  /* Then, copy the original header into it. */
181  SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
182  icmp_time_exceeded(p, ICMP_TE_FRAG);
183  clen = pbuf_clen(p);
184  LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
185  pbufs_freed += clen;
186  pbuf_free(p);
187  }
188 #endif /* LWIP_ICMP */
189 
190  /* First, free all received pbufs. The individual pbufs need to be released
191  separately as they have not yet been chained */
192  p = ipr->p;
193  while (p != NULL) {
194  struct pbuf *pcur;
195  iprh = (struct ip_reass_helper *)p->payload;
196  pcur = p;
197  /* get the next pointer before freeing */
198  p = iprh->next_pbuf;
199  clen = pbuf_clen(pcur);
200  LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
201  pbufs_freed += clen;
202  pbuf_free(pcur);
203  }
204  /* Then, unchain the struct ip_reassdata from the list and free it. */
205  ip_reass_dequeue_datagram(ipr, prev);
206  LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= pbufs_freed);
207  ip_reass_pbufcount -= pbufs_freed;
208 
209  return pbufs_freed;
210 }
211 
212 #if IP_REASS_FREE_OLDEST
213 
222 static int
223 ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
224 {
225  /* @todo Can't we simply remove the last datagram in the
226  * linked list behind reassdatagrams?
227  */
228  struct ip_reassdata *r, *oldest, *prev, *oldest_prev;
229  int pbufs_freed = 0, pbufs_freed_current;
230  int other_datagrams;
231 
232  /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
233  * but don't free the datagram that 'fraghdr' belongs to! */
234  do {
235  oldest = NULL;
236  prev = NULL;
237  oldest_prev = NULL;
238  other_datagrams = 0;
239  r = reassdatagrams;
240  while (r != NULL) {
241  if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
242  /* Not the same datagram as fraghdr */
243  other_datagrams++;
244  if (oldest == NULL) {
245  oldest = r;
246  oldest_prev = prev;
247  } else if (r->timer <= oldest->timer) {
248  /* older than the previous oldest */
249  oldest = r;
250  oldest_prev = prev;
251  }
252  }
253  if (r->next != NULL) {
254  prev = r;
255  }
256  r = r->next;
257  }
258  if (oldest != NULL) {
259  pbufs_freed_current = ip_reass_free_complete_datagram(oldest, oldest_prev);
260  pbufs_freed += pbufs_freed_current;
261  }
262  } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
263  return pbufs_freed;
264 }
265 #endif /* IP_REASS_FREE_OLDEST */
266 
273 static struct ip_reassdata*
274 ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
275 {
276  struct ip_reassdata* ipr;
277 #if ! IP_REASS_FREE_OLDEST
278  LWIP_UNUSED_ARG(clen);
279 #endif
280 
281  /* No matching previous fragment found, allocate a new reassdata struct */
282  ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
283  if (ipr == NULL) {
284 #if IP_REASS_FREE_OLDEST
285  if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
286  ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
287  }
288  if (ipr == NULL)
289 #endif /* IP_REASS_FREE_OLDEST */
290  {
291  IPFRAG_STATS_INC(ip_frag.memerr);
292  LWIP_DEBUGF(IP_REASS_DEBUG,("Failed to alloc reassdata struct\n"));
293  return NULL;
294  }
295  }
296  memset(ipr, 0, sizeof(struct ip_reassdata));
297  ipr->timer = IP_REASS_MAXAGE;
298 
299  /* enqueue the new structure to the front of the list */
300  ipr->next = reassdatagrams;
301  reassdatagrams = ipr;
302  /* copy the ip header for later tests and input */
303  /* @todo: no ip options supported? */
304  SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
305  return ipr;
306 }
307 
312 static void
313 ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
314 {
315  /* dequeue the reass struct */
316  if (reassdatagrams == ipr) {
317  /* it was the first in the list */
318  reassdatagrams = ipr->next;
319  } else {
320  /* it wasn't the first, so it must have a valid 'prev' */
321  LWIP_ASSERT("sanity check linked list", prev != NULL);
322  prev->next = ipr->next;
323  }
324 
325  /* now we can free the ip_reassdata struct */
326  memp_free(MEMP_REASSDATA, ipr);
327 }
328 
338 static int
339 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p)
340 {
341  struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
342  struct pbuf *q;
343  u16_t offset,len;
344  struct ip_hdr *fraghdr;
345  int valid = 1;
346 
347  /* Extract length and fragment offset from current fragment */
348  fraghdr = (struct ip_hdr*)new_p->payload;
349  len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
350  offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
351 
352  /* overwrite the fragment's ip header from the pbuf with our helper struct,
353  * and setup the embedded helper structure. */
354  /* make sure the struct ip_reass_helper fits into the IP header */
355  LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
356  sizeof(struct ip_reass_helper) <= IP_HLEN);
357  iprh = (struct ip_reass_helper*)new_p->payload;
358  iprh->next_pbuf = NULL;
359  iprh->start = offset;
360  iprh->end = offset + len;
361 
362  /* Iterate through until we either get to the end of the list (append),
363  * or we find one with a larger offset (insert). */
364  for (q = ipr->p; q != NULL;) {
365  iprh_tmp = (struct ip_reass_helper*)q->payload;
366  if (iprh->start < iprh_tmp->start) {
367  /* the new pbuf should be inserted before this */
368  iprh->next_pbuf = q;
369  if (iprh_prev != NULL) {
370  /* not the fragment with the lowest offset */
371 #if IP_REASS_CHECK_OVERLAP
372  if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
373  /* fragment overlaps with previous or following, throw away */
374  goto freepbuf;
375  }
376 #endif /* IP_REASS_CHECK_OVERLAP */
377  iprh_prev->next_pbuf = new_p;
378  } else {
379  /* fragment with the lowest offset */
380  ipr->p = new_p;
381  }
382  break;
383  } else if (iprh->start == iprh_tmp->start) {
384  /* received the same datagram twice: no need to keep the datagram */
385  goto freepbuf;
386 #if IP_REASS_CHECK_OVERLAP
387  } else if (iprh->start < iprh_tmp->end) {
388  /* overlap: no need to keep the new datagram */
389  goto freepbuf;
390 #endif /* IP_REASS_CHECK_OVERLAP */
391  } else {
392  /* Check if the fragments received so far have no holes. */
393  if (iprh_prev != NULL) {
394  if (iprh_prev->end != iprh_tmp->start) {
395  /* There is a fragment missing between the current
396  * and the previous fragment */
397  valid = 0;
398  }
399  }
400  }
401  q = iprh_tmp->next_pbuf;
402  iprh_prev = iprh_tmp;
403  }
404 
405  /* If q is NULL, then we made it to the end of the list. Determine what to do now */
406  if (q == NULL) {
407  if (iprh_prev != NULL) {
408  /* this is (for now), the fragment with the highest offset:
409  * chain it to the last fragment */
410 #if IP_REASS_CHECK_OVERLAP
411  LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
412 #endif /* IP_REASS_CHECK_OVERLAP */
413  iprh_prev->next_pbuf = new_p;
414  if (iprh_prev->end != iprh->start) {
415  valid = 0;
416  }
417  } else {
418 #if IP_REASS_CHECK_OVERLAP
419  LWIP_ASSERT("no previous fragment, this must be the first fragment!",
420  ipr->p == NULL);
421 #endif /* IP_REASS_CHECK_OVERLAP */
422  /* this is the first fragment we ever received for this ip datagram */
423  ipr->p = new_p;
424  }
425  }
426 
427  /* At this point, the validation part begins: */
428  /* If we already received the last fragment */
429  if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {
430  /* and had no holes so far */
431  if (valid) {
432  /* then check if the rest of the fragments is here */
433  /* Check if the queue starts with the first datagram */
434  if ((ipr->p == NULL) || (((struct ip_reass_helper*)ipr->p->payload)->start != 0)) {
435  valid = 0;
436  } else {
437  /* and check that there are no holes after this datagram */
438  iprh_prev = iprh;
439  q = iprh->next_pbuf;
440  while (q != NULL) {
441  iprh = (struct ip_reass_helper*)q->payload;
442  if (iprh_prev->end != iprh->start) {
443  valid = 0;
444  break;
445  }
446  iprh_prev = iprh;
447  q = iprh->next_pbuf;
448  }
449  /* if still valid, all fragments are received
450  * (because to the MF==0 already arrived */
451  if (valid) {
452  LWIP_ASSERT("sanity check", ipr->p != NULL);
453  LWIP_ASSERT("sanity check",
454  ((struct ip_reass_helper*)ipr->p->payload) != iprh);
455  LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
456  iprh->next_pbuf == NULL);
457  LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
458  iprh->end == ipr->datagram_len);
459  }
460  }
461  }
462  /* If valid is 0 here, there are some fragments missing in the middle
463  * (since MF == 0 has already arrived). Such datagrams simply time out if
464  * no more fragments are received... */
465  return valid;
466  }
467  /* If we come here, not all fragments were received, yet! */
468  return 0; /* not yet valid! */
469 #if IP_REASS_CHECK_OVERLAP
470 freepbuf:
471  ip_reass_pbufcount -= pbuf_clen(new_p);
472  pbuf_free(new_p);
473  return 0;
474 #endif /* IP_REASS_CHECK_OVERLAP */
475 }
476 
483 struct pbuf *
484 ip4_reass(struct pbuf *p)
485 {
486  struct pbuf *r;
487  struct ip_hdr *fraghdr;
488  struct ip_reassdata *ipr;
489  struct ip_reass_helper *iprh;
490  u16_t offset, len;
491  u8_t clen;
492 
493  IPFRAG_STATS_INC(ip_frag.recv);
494  MIB2_STATS_INC(mib2.ipreasmreqds);
495 
496  fraghdr = (struct ip_hdr*)p->payload;
497 
498  if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {
499  LWIP_DEBUGF(IP_REASS_DEBUG,("ip4_reass: IP options currently not supported!\n"));
500  IPFRAG_STATS_INC(ip_frag.err);
501  goto nullreturn;
502  }
503 
504  offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
505  len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
506 
507  /* Check if we are allowed to enqueue more datagrams. */
508  clen = pbuf_clen(p);
509  if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
510 #if IP_REASS_FREE_OLDEST
511  if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
512  ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
513 #endif /* IP_REASS_FREE_OLDEST */
514  {
515  /* No datagram could be freed and still too many pbufs enqueued */
516  LWIP_DEBUGF(IP_REASS_DEBUG,("ip4_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
517  ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
518  IPFRAG_STATS_INC(ip_frag.memerr);
519  /* @todo: send ICMP time exceeded here? */
520  /* drop this pbuf */
521  goto nullreturn;
522  }
523  }
524 
525  /* Look for the datagram the fragment belongs to in the current datagram queue,
526  * remembering the previous in the queue for later dequeueing. */
527  for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
528  /* Check if the incoming fragment matches the one currently present
529  in the reassembly buffer. If so, we proceed with copying the
530  fragment into the buffer. */
531  if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
532  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: matching previous fragment ID=%"X16_F"\n",
533  ntohs(IPH_ID(fraghdr))));
534  IPFRAG_STATS_INC(ip_frag.cachehit);
535  break;
536  }
537  }
538 
539  if (ipr == NULL) {
540  /* Enqueue a new datagram into the datagram queue */
541  ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
542  /* Bail if unable to enqueue */
543  if (ipr == NULL) {
544  goto nullreturn;
545  }
546  } else {
547  if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
548  ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
549  /* ipr->iphdr is not the header from the first fragment, but fraghdr is
550  * -> copy fraghdr into ipr->iphdr since we want to have the header
551  * of the first fragment (for ICMP time exceeded and later, for copying
552  * all options, if supported)*/
553  SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
554  }
555  }
556  /* Track the current number of pbufs current 'in-flight', in order to limit
557  the number of fragments that may be enqueued at any one time */
558  ip_reass_pbufcount += clen;
559 
560  /* At this point, we have either created a new entry or pointing
561  * to an existing one */
562 
563  /* check for 'no more fragments', and update queue entry*/
564  if ((IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0) {
565  ipr->flags |= IP_REASS_FLAG_LASTFRAG;
566  ipr->datagram_len = offset + len;
568  ("ip4_reass: last fragment seen, total len %"S16_F"\n",
569  ipr->datagram_len));
570  }
571  /* find the right place to insert this pbuf */
572  /* @todo: trim pbufs if fragments are overlapping */
573  if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {
574  struct ip_reassdata *ipr_prev;
575  /* the totally last fragment (flag more fragments = 0) was received at least
576  * once AND all fragments are received */
577  ipr->datagram_len += IP_HLEN;
578 
579  /* save the second pbuf before copying the header over the pointer */
580  r = ((struct ip_reass_helper*)ipr->p->payload)->next_pbuf;
581 
582  /* copy the original ip header back to the first pbuf */
583  fraghdr = (struct ip_hdr*)(ipr->p->payload);
584  SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
585  IPH_LEN_SET(fraghdr, htons(ipr->datagram_len));
586  IPH_OFFSET_SET(fraghdr, 0);
587  IPH_CHKSUM_SET(fraghdr, 0);
588  /* @todo: do we need to set/calculate the correct checksum? */
589 #if CHECKSUM_GEN_IP
590  IF__NETIF_CHECKSUM_ENABLED(ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) {
591  IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
592  }
593 #endif /* CHECKSUM_GEN_IP */
594 
595  p = ipr->p;
596 
597  /* chain together the pbufs contained within the reass_data list. */
598  while (r != NULL) {
599  iprh = (struct ip_reass_helper*)r->payload;
600 
601  /* hide the ip header for every succeeding fragment */
602  pbuf_header(r, -IP_HLEN);
603  pbuf_cat(p, r);
604  r = iprh->next_pbuf;
605  }
606 
607  /* find the previous entry in the linked list */
608  if (ipr == reassdatagrams) {
609  ipr_prev = NULL;
610  } else {
611  for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
612  if (ipr_prev->next == ipr) {
613  break;
614  }
615  }
616  }
617 
618  /* release the sources allocate for the fragment queue entry */
619  ip_reass_dequeue_datagram(ipr, ipr_prev);
620 
621  /* and adjust the number of pbufs currently queued for reassembly. */
622  ip_reass_pbufcount -= pbuf_clen(p);
623 
624  MIB2_STATS_INC(mib2.ipreasmoks);
625 
626  /* Return the pbuf chain */
627  return p;
628  }
629  /* the datagram is not (yet?) reassembled completely */
630  LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
631  return NULL;
632 
633 nullreturn:
634  LWIP_DEBUGF(IP_REASS_DEBUG,("ip4_reass: nullreturn\n"));
635  IPFRAG_STATS_INC(ip_frag.drop);
636  pbuf_free(p);
637  return NULL;
638 }
639 #endif /* IP_REASSEMBLY */
640 
641 #if IP_FRAG
642 #if IP_FRAG_USES_STATIC_BUF
643 static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)];
644 #else /* IP_FRAG_USES_STATIC_BUF */
645 
646 #if !LWIP_NETIF_TX_SINGLE_PBUF
647 
648 static struct pbuf_custom_ref*
649 ip_frag_alloc_pbuf_custom_ref(void)
650 {
651  return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
652 }
653 
655 static void
656 ip_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
657 {
658  LWIP_ASSERT("p != NULL", p != NULL);
659  memp_free(MEMP_FRAG_PBUF, p);
660 }
661 
664 static void
665 ipfrag_free_pbuf_custom(struct pbuf *p)
666 {
667  struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
668  LWIP_ASSERT("pcr != NULL", pcr != NULL);
669  LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
670  if (pcr->original != NULL) {
671  pbuf_free(pcr->original);
672  }
673  ip_frag_free_pbuf_custom_ref(pcr);
674 }
675 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
676 #endif /* IP_FRAG_USES_STATIC_BUF */
677 
691 err_t
692 ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest)
693 {
694  struct pbuf *rambuf;
695 #if IP_FRAG_USES_STATIC_BUF
696  struct pbuf *header;
697 #else
698 #if !LWIP_NETIF_TX_SINGLE_PBUF
699  struct pbuf *newpbuf;
700 #endif
701  struct ip_hdr *original_iphdr;
702 #endif
703  struct ip_hdr *iphdr;
704  u16_t nfb;
705  u16_t left, cop;
706  u16_t mtu = netif->mtu;
707  u16_t ofo, omf;
708  u16_t last;
709  u16_t poff = IP_HLEN;
710  u16_t tmp;
711 #if !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF
712  u16_t newpbuflen = 0;
713  u16_t left_to_copy;
714 #endif
715 
716  /* Get a RAM based MTU sized pbuf */
717 #if IP_FRAG_USES_STATIC_BUF
718  /* When using a static buffer, we use a PBUF_REF, which we will
719  * use to reference the packet (without link header).
720  * Layer and length is irrelevant.
721  */
722  rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
723  if (rambuf == NULL) {
724  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
725  goto memerr;
726  }
727  rambuf->tot_len = rambuf->len = mtu;
728  rambuf->payload = LWIP_MEM_ALIGN((void *)buf);
729 
730  /* Copy the IP header in it */
731  iphdr = (struct ip_hdr *)rambuf->payload;
732  SMEMCPY(iphdr, p->payload, IP_HLEN);
733 #else /* IP_FRAG_USES_STATIC_BUF */
734  original_iphdr = (struct ip_hdr *)p->payload;
735  iphdr = original_iphdr;
736 #endif /* IP_FRAG_USES_STATIC_BUF */
737 
738  /* Save original offset */
739  tmp = ntohs(IPH_OFFSET(iphdr));
740  ofo = tmp & IP_OFFMASK;
741  omf = tmp & IP_MF;
742 
743  left = p->tot_len - IP_HLEN;
744 
745  nfb = (mtu - IP_HLEN) / 8;
746 
747  while (left) {
748  last = (left <= mtu - IP_HLEN);
749 
750  /* Set new offset and MF flag */
751  tmp = omf | (IP_OFFMASK & (ofo));
752  if (!last) {
753  tmp = tmp | IP_MF;
754  }
755 
756  /* Fill this fragment */
757  cop = last ? left : nfb * 8;
758 
759 #if IP_FRAG_USES_STATIC_BUF
760  poff += pbuf_copy_partial(p, (u8_t*)iphdr + IP_HLEN, cop, poff);
761 #else /* IP_FRAG_USES_STATIC_BUF */
762 #if LWIP_NETIF_TX_SINGLE_PBUF
763  rambuf = pbuf_alloc(PBUF_IP, cop, PBUF_RAM);
764  if (rambuf == NULL) {
765  goto memerr;
766  }
767  LWIP_ASSERT("this needs a pbuf in one piece!",
768  (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
769  poff += pbuf_copy_partial(p, rambuf->payload, cop, poff);
770  /* make room for the IP header */
771  if (pbuf_header(rambuf, IP_HLEN)) {
772  pbuf_free(rambuf);
773  goto memerr;
774  }
775  /* fill in the IP header */
776  SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
777  iphdr = (struct ip_hdr*)rambuf->payload;
778 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
779  /* When not using a static buffer, create a chain of pbufs.
780  * The first will be a PBUF_RAM holding the link and IP header.
781  * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
782  * but limited to the size of an mtu.
783  */
784  rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
785  if (rambuf == NULL) {
786  goto memerr;
787  }
788  LWIP_ASSERT("this needs a pbuf in one piece!",
789  (p->len >= (IP_HLEN)));
790  SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
791  iphdr = (struct ip_hdr *)rambuf->payload;
792 
793  /* Can just adjust p directly for needed offset. */
794  p->payload = (u8_t *)p->payload + poff;
795  p->len -= poff;
796 
797  left_to_copy = cop;
798  while (left_to_copy) {
799  struct pbuf_custom_ref *pcr;
800  newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
801  /* Is this pbuf already empty? */
802  if (!newpbuflen) {
803  p = p->next;
804  continue;
805  }
806  pcr = ip_frag_alloc_pbuf_custom_ref();
807  if (pcr == NULL) {
808  pbuf_free(rambuf);
809  goto memerr;
810  }
811  /* Mirror this pbuf, although we might not need all of it. */
812  newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
813  if (newpbuf == NULL) {
814  ip_frag_free_pbuf_custom_ref(pcr);
815  pbuf_free(rambuf);
816  goto memerr;
817  }
818  pbuf_ref(p);
819  pcr->original = p;
820  pcr->pc.custom_free_function = ipfrag_free_pbuf_custom;
821 
822  /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
823  * so that it is removed when pbuf_dechain is later called on rambuf.
824  */
825  pbuf_cat(rambuf, newpbuf);
826  left_to_copy -= newpbuflen;
827  if (left_to_copy) {
828  p = p->next;
829  }
830  }
831  poff = newpbuflen;
832 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
833 #endif /* IP_FRAG_USES_STATIC_BUF */
834 
835  /* Correct header */
836  IPH_OFFSET_SET(iphdr, htons(tmp));
837  IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
838  IPH_CHKSUM_SET(iphdr, 0);
839 #if CHECKSUM_GEN_IP
840  IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
841  IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
842  }
843 #endif /* CHECKSUM_GEN_IP */
844 
845 #if IP_FRAG_USES_STATIC_BUF
846  if (last) {
847  pbuf_realloc(rambuf, left + IP_HLEN);
848  }
849 
850  /* This part is ugly: we alloc a RAM based pbuf for
851  * the link level header for each chunk and then
852  * free it. A PBUF_ROM style pbuf for which pbuf_header
853  * worked would make things simpler.
854  */
855  header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
856  if (header != NULL) {
857  pbuf_chain(header, rambuf);
858  netif->output(netif, header, dest);
859  IPFRAG_STATS_INC(ip_frag.xmit);
860  MIB2_STATS_INC(mib2.ipfragcreates);
861  pbuf_free(header);
862  } else {
863  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc() for header failed\n"));
864  pbuf_free(rambuf);
865  goto memerr;
866  }
867 #else /* IP_FRAG_USES_STATIC_BUF */
868  /* No need for separate header pbuf - we allowed room for it in rambuf
869  * when allocated.
870  */
871  netif->output(netif, rambuf, dest);
872  IPFRAG_STATS_INC(ip_frag.xmit);
873 
874  /* Unfortunately we can't reuse rambuf - the hardware may still be
875  * using the buffer. Instead we free it (and the ensuing chain) and
876  * recreate it next time round the loop. If we're lucky the hardware
877  * will have already sent the packet, the free will really free, and
878  * there will be zero memory penalty.
879  */
880 
881  pbuf_free(rambuf);
882 #endif /* IP_FRAG_USES_STATIC_BUF */
883  left -= cop;
884  ofo += nfb;
885  }
886 #if IP_FRAG_USES_STATIC_BUF
887  pbuf_free(rambuf);
888 #endif /* IP_FRAG_USES_STATIC_BUF */
889  MIB2_STATS_INC(mib2.ipfragoks);
890  return ERR_OK;
891 memerr:
892  MIB2_STATS_INC(mib2.ipfragfails);
893  return ERR_MEM;
894 }
895 #endif /* IP_FRAG */
896 
897 #endif /* LWIP_IPV4 */
898 
#define MIB2_STATS_INC(x)
Definition: stats.h:407
#define PACK_STRUCT_STRUCT
Definition: arch.h:68
#define U16_F
Definition: cc.h:48
#define ip_current_input_netif()
Definition: ip.h:158
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition: pbuf.c:431
u16_t mtu
Definition: netif.h:263
#define htons(x)
Definition: def.h:86
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
Definition: netif.h:313
if(LCD_Lock==DISABLE)
Definition: lcd_log.c:249
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:72
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
Definition: pbuf.h:77
u16_t inet_chksum(const void *dataptr, u16_t len)
Definition: inet_chksum.c:558
#define MEM_ALIGNMENT
Definition: lwipopts.h:73
Definition: pbuf.h:66
#define IP_REASS_MAX_PBUFS
Definition: opt.h:614
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
Definition: pbuf.h:68
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:820
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
Definition: pbuf.h:108
#define IP_REASS_MAXAGE
Definition: opt.h:604
#define IP_REASS_DEBUG
Definition: opt.h:2844
s8_t err_t
Definition: err.h:47
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
struct pbuf * next
Definition: pbuf.h:110
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:757
#define PACK_STRUCT_BEGIN
Definition: arch.h:60
#define S16_F
Definition: cc.h:49
u16_t pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:952
u8_t pbuf_clen(struct pbuf *p)
Definition: pbuf.c:738
#define PP_NTOHS(x)
Definition: def.h:98
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
Definition: pbuf.h:85
unsigned char u8_t
Definition: cc.h:38
#define LWIP_MEM_ALIGN(addr)
Definition: mem.h:116
#define PACK_STRUCT_END
Definition: arch.h:64
#define IPFRAG_STATS_INC(x)
Definition: stats.h:302
Definition: pbuf.h:65
#define LWIP_MEM_ALIGN_SIZE(size)
Definition: mem.h:101
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
#define ntohs(x)
Definition: def.h:87
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define X16_F
Definition: cc.h:50