The Pedigree Project  0.1
ip4_frag.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
26 /*
27  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without modification,
31  * are permitted provided that the following conditions are met:
32  *
33  * 1. Redistributions of source code must retain the above copyright notice,
34  * this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright notice,
36  * this list of conditions and the following disclaimer in the documentation
37  * and/or other materials provided with the distribution.
38  * 3. The name of the author may not be used to endorse or promote products
39  * derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
44  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
46  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
49  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
50  * OF SUCH DAMAGE.
51  *
52  * This file is part of the lwIP TCP/IP stack.
53  *
54  * Author: Jani Monoses <jani@iv.ro>
55  * Simon Goldschmidt
56  * original reassembly code by Adam Dunkels <adam@sics.se>
57  *
58  */
59 
60 #include "lwip/opt.h"
61 
62 #if LWIP_IPV4
63 
64 #include "lwip/ip4_frag.h"
65 #include "lwip/def.h"
66 #include "lwip/inet_chksum.h"
67 #include "lwip/netif.h"
68 #include "lwip/stats.h"
69 #include "lwip/icmp.h"
70 
71 #include <string.h>
72 
73 #if IP_REASSEMBLY
74 
87 #ifndef IP_REASS_CHECK_OVERLAP
88 #define IP_REASS_CHECK_OVERLAP 1
89 #endif /* IP_REASS_CHECK_OVERLAP */
90 
95 #ifndef IP_REASS_FREE_OLDEST
96 #define IP_REASS_FREE_OLDEST 1
97 #endif /* IP_REASS_FREE_OLDEST */
98 
99 #define IP_REASS_FLAG_LASTFRAG 0x01
100 
109 #ifdef PACK_STRUCT_USE_INCLUDES
110 # include "arch/bpstruct.h"
111 #endif
113 struct ip_reass_helper {
114  PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
115  PACK_STRUCT_FIELD(u16_t start);
116  PACK_STRUCT_FIELD(u16_t end);
117 } PACK_STRUCT_STRUCT;
119 #ifdef PACK_STRUCT_USE_INCLUDES
120 # include "arch/epstruct.h"
121 #endif
122 
123 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
124  (ip4_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
125  ip4_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
126  IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
127 
128 /* global variables */
129 static struct ip_reassdata *reassdatagrams;
130 static u16_t ip_reass_pbufcount;
131 
132 /* function prototypes */
133 static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
134 static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
135 
142 void
143 ip_reass_tmr(void)
144 {
145  struct ip_reassdata *r, *prev = NULL;
146 
147  r = reassdatagrams;
148  while (r != NULL) {
149  /* Decrement the timer. Once it reaches 0,
150  * clean up the incomplete fragment assembly */
151  if (r->timer > 0) {
152  r->timer--;
153  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)r->timer));
154  prev = r;
155  r = r->next;
156  } else {
157  /* reassembly timed out */
158  struct ip_reassdata *tmp;
159  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
160  tmp = r;
161  /* get the next pointer before freeing */
162  r = r->next;
163  /* free the helper struct and all enqueued pbufs */
164  ip_reass_free_complete_datagram(tmp, prev);
165  }
166  }
167 }
168 
178 static int
179 ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
180 {
181  u16_t pbufs_freed = 0;
182  u16_t clen;
183  struct pbuf *p;
184  struct ip_reass_helper *iprh;
185 
186  LWIP_ASSERT("prev != ipr", prev != ipr);
187  if (prev != NULL) {
188  LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
189  }
190 
191  MIB2_STATS_INC(mib2.ipreasmfails);
192 #if LWIP_ICMP
193  iprh = (struct ip_reass_helper *)ipr->p->payload;
194  if (iprh->start == 0) {
195  /* The first fragment was received, send ICMP time exceeded. */
196  /* First, de-queue the first pbuf from r->p. */
197  p = ipr->p;
198  ipr->p = iprh->next_pbuf;
199  /* Then, copy the original header into it. */
200  SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
201  icmp_time_exceeded(p, ICMP_TE_FRAG);
202  clen = pbuf_clen(p);
203  LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
204  pbufs_freed += clen;
205  pbuf_free(p);
206  }
207 #endif /* LWIP_ICMP */
208 
209  /* First, free all received pbufs. The individual pbufs need to be released
210  separately as they have not yet been chained */
211  p = ipr->p;
212  while (p != NULL) {
213  struct pbuf *pcur;
214  iprh = (struct ip_reass_helper *)p->payload;
215  pcur = p;
216  /* get the next pointer before freeing */
217  p = iprh->next_pbuf;
218  clen = pbuf_clen(pcur);
219  LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
220  pbufs_freed += clen;
221  pbuf_free(pcur);
222  }
223  /* Then, unchain the struct ip_reassdata from the list and free it. */
224  ip_reass_dequeue_datagram(ipr, prev);
225  LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= pbufs_freed);
226  ip_reass_pbufcount -= pbufs_freed;
227 
228  return pbufs_freed;
229 }
230 
231 #if IP_REASS_FREE_OLDEST
232 
241 static int
242 ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
243 {
244  /* @todo Can't we simply remove the last datagram in the
245  * linked list behind reassdatagrams?
246  */
247  struct ip_reassdata *r, *oldest, *prev, *oldest_prev;
248  int pbufs_freed = 0, pbufs_freed_current;
249  int other_datagrams;
250 
251  /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
252  * but don't free the datagram that 'fraghdr' belongs to! */
253  do {
254  oldest = NULL;
255  prev = NULL;
256  oldest_prev = NULL;
257  other_datagrams = 0;
258  r = reassdatagrams;
259  while (r != NULL) {
260  if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
261  /* Not the same datagram as fraghdr */
262  other_datagrams++;
263  if (oldest == NULL) {
264  oldest = r;
265  oldest_prev = prev;
266  } else if (r->timer <= oldest->timer) {
267  /* older than the previous oldest */
268  oldest = r;
269  oldest_prev = prev;
270  }
271  }
272  if (r->next != NULL) {
273  prev = r;
274  }
275  r = r->next;
276  }
277  if (oldest != NULL) {
278  pbufs_freed_current = ip_reass_free_complete_datagram(oldest, oldest_prev);
279  pbufs_freed += pbufs_freed_current;
280  }
281  } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
282  return pbufs_freed;
283 }
284 #endif /* IP_REASS_FREE_OLDEST */
285 
292 static struct ip_reassdata*
293 ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
294 {
295  struct ip_reassdata* ipr;
296 #if ! IP_REASS_FREE_OLDEST
297  LWIP_UNUSED_ARG(clen);
298 #endif
299 
300  /* No matching previous fragment found, allocate a new reassdata struct */
301  ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
302  if (ipr == NULL) {
303 #if IP_REASS_FREE_OLDEST
304  if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
305  ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
306  }
307  if (ipr == NULL)
308 #endif /* IP_REASS_FREE_OLDEST */
309  {
310  IPFRAG_STATS_INC(ip_frag.memerr);
311  LWIP_DEBUGF(IP_REASS_DEBUG,("Failed to alloc reassdata struct\n"));
312  return NULL;
313  }
314  }
315  memset(ipr, 0, sizeof(struct ip_reassdata));
316  ipr->timer = IP_REASS_MAXAGE;
317 
318  /* enqueue the new structure to the front of the list */
319  ipr->next = reassdatagrams;
320  reassdatagrams = ipr;
321  /* copy the ip header for later tests and input */
322  /* @todo: no ip options supported? */
323  SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
324  return ipr;
325 }
326 
331 static void
332 ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
333 {
334  /* dequeue the reass struct */
335  if (reassdatagrams == ipr) {
336  /* it was the first in the list */
337  reassdatagrams = ipr->next;
338  } else {
339  /* it wasn't the first, so it must have a valid 'prev' */
340  LWIP_ASSERT("sanity check linked list", prev != NULL);
341  prev->next = ipr->next;
342  }
343 
344  /* now we can free the ip_reassdata struct */
345  memp_free(MEMP_REASSDATA, ipr);
346 }
347 
357 static int
358 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p)
359 {
360  struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
361  struct pbuf *q;
362  u16_t offset, len;
363  struct ip_hdr *fraghdr;
364  int valid = 1;
365 
366  /* Extract length and fragment offset from current fragment */
367  fraghdr = (struct ip_hdr*)new_p->payload;
368  len = lwip_ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
369  offset = (lwip_ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
370 
371  /* overwrite the fragment's ip header from the pbuf with our helper struct,
372  * and setup the embedded helper structure. */
373  /* make sure the struct ip_reass_helper fits into the IP header */
374  LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
375  sizeof(struct ip_reass_helper) <= IP_HLEN);
376  iprh = (struct ip_reass_helper*)new_p->payload;
377  iprh->next_pbuf = NULL;
378  iprh->start = offset;
379  iprh->end = offset + len;
380 
381  /* Iterate through until we either get to the end of the list (append),
382  * or we find one with a larger offset (insert). */
383  for (q = ipr->p; q != NULL;) {
384  iprh_tmp = (struct ip_reass_helper*)q->payload;
385  if (iprh->start < iprh_tmp->start) {
386  /* the new pbuf should be inserted before this */
387  iprh->next_pbuf = q;
388  if (iprh_prev != NULL) {
389  /* not the fragment with the lowest offset */
390 #if IP_REASS_CHECK_OVERLAP
391  if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
392  /* fragment overlaps with previous or following, throw away */
393  goto freepbuf;
394  }
395 #endif /* IP_REASS_CHECK_OVERLAP */
396  iprh_prev->next_pbuf = new_p;
397  } else {
398  /* fragment with the lowest offset */
399  ipr->p = new_p;
400  }
401  break;
402  } else if (iprh->start == iprh_tmp->start) {
403  /* received the same datagram twice: no need to keep the datagram */
404  goto freepbuf;
405 #if IP_REASS_CHECK_OVERLAP
406  } else if (iprh->start < iprh_tmp->end) {
407  /* overlap: no need to keep the new datagram */
408  goto freepbuf;
409 #endif /* IP_REASS_CHECK_OVERLAP */
410  } else {
411  /* Check if the fragments received so far have no holes. */
412  if (iprh_prev != NULL) {
413  if (iprh_prev->end != iprh_tmp->start) {
414  /* There is a fragment missing between the current
415  * and the previous fragment */
416  valid = 0;
417  }
418  }
419  }
420  q = iprh_tmp->next_pbuf;
421  iprh_prev = iprh_tmp;
422  }
423 
424  /* If q is NULL, then we made it to the end of the list. Determine what to do now */
425  if (q == NULL) {
426  if (iprh_prev != NULL) {
427  /* this is (for now), the fragment with the highest offset:
428  * chain it to the last fragment */
429 #if IP_REASS_CHECK_OVERLAP
430  LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
431 #endif /* IP_REASS_CHECK_OVERLAP */
432  iprh_prev->next_pbuf = new_p;
433  if (iprh_prev->end != iprh->start) {
434  valid = 0;
435  }
436  } else {
437 #if IP_REASS_CHECK_OVERLAP
438  LWIP_ASSERT("no previous fragment, this must be the first fragment!",
439  ipr->p == NULL);
440 #endif /* IP_REASS_CHECK_OVERLAP */
441  /* this is the first fragment we ever received for this ip datagram */
442  ipr->p = new_p;
443  }
444  }
445 
446  /* At this point, the validation part begins: */
447  /* If we already received the last fragment */
448  if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {
449  /* and had no holes so far */
450  if (valid) {
451  /* then check if the rest of the fragments is here */
452  /* Check if the queue starts with the first datagram */
453  if ((ipr->p == NULL) || (((struct ip_reass_helper*)ipr->p->payload)->start != 0)) {
454  valid = 0;
455  } else {
456  /* and check that there are no holes after this datagram */
457  iprh_prev = iprh;
458  q = iprh->next_pbuf;
459  while (q != NULL) {
460  iprh = (struct ip_reass_helper*)q->payload;
461  if (iprh_prev->end != iprh->start) {
462  valid = 0;
463  break;
464  }
465  iprh_prev = iprh;
466  q = iprh->next_pbuf;
467  }
468  /* if still valid, all fragments are received
469  * (because to the MF==0 already arrived */
470  if (valid) {
471  LWIP_ASSERT("sanity check", ipr->p != NULL);
472  LWIP_ASSERT("sanity check",
473  ((struct ip_reass_helper*)ipr->p->payload) != iprh);
474  LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
475  iprh->next_pbuf == NULL);
476  LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
477  iprh->end == ipr->datagram_len);
478  }
479  }
480  }
481  /* If valid is 0 here, there are some fragments missing in the middle
482  * (since MF == 0 has already arrived). Such datagrams simply time out if
483  * no more fragments are received... */
484  return valid;
485  }
486  /* If we come here, not all fragments were received, yet! */
487  return 0; /* not yet valid! */
488 #if IP_REASS_CHECK_OVERLAP
489 freepbuf:
490  ip_reass_pbufcount -= pbuf_clen(new_p);
491  pbuf_free(new_p);
492  return 0;
493 #endif /* IP_REASS_CHECK_OVERLAP */
494 }
495 
502 struct pbuf *
503 ip4_reass(struct pbuf *p)
504 {
505  struct pbuf *r;
506  struct ip_hdr *fraghdr;
507  struct ip_reassdata *ipr;
508  struct ip_reass_helper *iprh;
509  u16_t offset, len, clen;
510 
511  IPFRAG_STATS_INC(ip_frag.recv);
512  MIB2_STATS_INC(mib2.ipreasmreqds);
513 
514  fraghdr = (struct ip_hdr*)p->payload;
515 
516  if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {
517  LWIP_DEBUGF(IP_REASS_DEBUG,("ip4_reass: IP options currently not supported!\n"));
518  IPFRAG_STATS_INC(ip_frag.err);
519  goto nullreturn;
520  }
521 
522  offset = (lwip_ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
523  len = lwip_ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
524 
525  /* Check if we are allowed to enqueue more datagrams. */
526  clen = pbuf_clen(p);
527  if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
528 #if IP_REASS_FREE_OLDEST
529  if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
530  ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
531 #endif /* IP_REASS_FREE_OLDEST */
532  {
533  /* No datagram could be freed and still too many pbufs enqueued */
534  LWIP_DEBUGF(IP_REASS_DEBUG,("ip4_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
535  ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
536  IPFRAG_STATS_INC(ip_frag.memerr);
537  /* @todo: send ICMP time exceeded here? */
538  /* drop this pbuf */
539  goto nullreturn;
540  }
541  }
542 
543  /* Look for the datagram the fragment belongs to in the current datagram queue,
544  * remembering the previous in the queue for later dequeueing. */
545  for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
546  /* Check if the incoming fragment matches the one currently present
547  in the reassembly buffer. If so, we proceed with copying the
548  fragment into the buffer. */
549  if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
550  LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: matching previous fragment ID=%"X16_F"\n",
551  lwip_ntohs(IPH_ID(fraghdr))));
552  IPFRAG_STATS_INC(ip_frag.cachehit);
553  break;
554  }
555  }
556 
557  if (ipr == NULL) {
558  /* Enqueue a new datagram into the datagram queue */
559  ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
560  /* Bail if unable to enqueue */
561  if (ipr == NULL) {
562  goto nullreturn;
563  }
564  } else {
565  if (((lwip_ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
566  ((lwip_ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
567  /* ipr->iphdr is not the header from the first fragment, but fraghdr is
568  * -> copy fraghdr into ipr->iphdr since we want to have the header
569  * of the first fragment (for ICMP time exceeded and later, for copying
570  * all options, if supported)*/
571  SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
572  }
573  }
574  /* Track the current number of pbufs current 'in-flight', in order to limit
575  the number of fragments that may be enqueued at any one time */
576  ip_reass_pbufcount += clen;
577 
578  /* At this point, we have either created a new entry or pointing
579  * to an existing one */
580 
581  /* check for 'no more fragments', and update queue entry*/
582  if ((IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0) {
583  ipr->flags |= IP_REASS_FLAG_LASTFRAG;
584  ipr->datagram_len = offset + len;
586  ("ip4_reass: last fragment seen, total len %"S16_F"\n",
587  ipr->datagram_len));
588  }
589  /* find the right place to insert this pbuf */
590  /* @todo: trim pbufs if fragments are overlapping */
591  if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {
592  struct ip_reassdata *ipr_prev;
593  /* the totally last fragment (flag more fragments = 0) was received at least
594  * once AND all fragments are received */
595  ipr->datagram_len += IP_HLEN;
596 
597  /* save the second pbuf before copying the header over the pointer */
598  r = ((struct ip_reass_helper*)ipr->p->payload)->next_pbuf;
599 
600  /* copy the original ip header back to the first pbuf */
601  fraghdr = (struct ip_hdr*)(ipr->p->payload);
602  SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
603  IPH_LEN_SET(fraghdr, lwip_htons(ipr->datagram_len));
604  IPH_OFFSET_SET(fraghdr, 0);
605  IPH_CHKSUM_SET(fraghdr, 0);
606  /* @todo: do we need to set/calculate the correct checksum? */
607 #if CHECKSUM_GEN_IP
608  IF__NETIF_CHECKSUM_ENABLED(ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) {
609  IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
610  }
611 #endif /* CHECKSUM_GEN_IP */
612 
613  p = ipr->p;
614 
615  /* chain together the pbufs contained within the reass_data list. */
616  while (r != NULL) {
617  iprh = (struct ip_reass_helper*)r->payload;
618 
619  /* hide the ip header for every succeeding fragment */
620  pbuf_header(r, -IP_HLEN);
621  pbuf_cat(p, r);
622  r = iprh->next_pbuf;
623  }
624 
625  /* find the previous entry in the linked list */
626  if (ipr == reassdatagrams) {
627  ipr_prev = NULL;
628  } else {
629  for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
630  if (ipr_prev->next == ipr) {
631  break;
632  }
633  }
634  }
635 
636  /* release the sources allocate for the fragment queue entry */
637  ip_reass_dequeue_datagram(ipr, ipr_prev);
638 
639  /* and adjust the number of pbufs currently queued for reassembly. */
640  ip_reass_pbufcount -= pbuf_clen(p);
641 
642  MIB2_STATS_INC(mib2.ipreasmoks);
643 
644  /* Return the pbuf chain */
645  return p;
646  }
647  /* the datagram is not (yet?) reassembled completely */
648  LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
649  return NULL;
650 
651 nullreturn:
652  LWIP_DEBUGF(IP_REASS_DEBUG,("ip4_reass: nullreturn\n"));
653  IPFRAG_STATS_INC(ip_frag.drop);
654  pbuf_free(p);
655  return NULL;
656 }
657 #endif /* IP_REASSEMBLY */
658 
659 #if IP_FRAG
660 #if !LWIP_NETIF_TX_SINGLE_PBUF
661 
662 static struct pbuf_custom_ref*
663 ip_frag_alloc_pbuf_custom_ref(void)
664 {
665  return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
666 }
667 
669 static void
670 ip_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
671 {
672  LWIP_ASSERT("p != NULL", p != NULL);
673  memp_free(MEMP_FRAG_PBUF, p);
674 }
675 
678 static void
679 ipfrag_free_pbuf_custom(struct pbuf *p)
680 {
681  struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
682  LWIP_ASSERT("pcr != NULL", pcr != NULL);
683  LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
684  if (pcr->original != NULL) {
685  pbuf_free(pcr->original);
686  }
687  ip_frag_free_pbuf_custom_ref(pcr);
688 }
689 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
690 
703 err_t
704 ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest)
705 {
706  struct pbuf *rambuf;
707 #if !LWIP_NETIF_TX_SINGLE_PBUF
708  struct pbuf *newpbuf;
709  u16_t newpbuflen = 0;
710  u16_t left_to_copy;
711 #endif
712  struct ip_hdr *original_iphdr;
713  struct ip_hdr *iphdr;
714  const u16_t nfb = (netif->mtu - IP_HLEN) / 8;
715  u16_t left, fragsize;
716  u16_t ofo;
717  int last;
718  u16_t poff = IP_HLEN;
719  u16_t tmp;
720 
721  original_iphdr = (struct ip_hdr *)p->payload;
722  iphdr = original_iphdr;
723  LWIP_ERROR("ip4_frag() does not support IP options", IPH_HL(iphdr) * 4 == IP_HLEN, return ERR_VAL);
724 
725  /* Save original offset */
726  tmp = lwip_ntohs(IPH_OFFSET(iphdr));
727  ofo = tmp & IP_OFFMASK;
728  LWIP_ERROR("ip_frag(): MF already set", (tmp & IP_MF) == 0, return ERR_VAL);
729 
730  left = p->tot_len - IP_HLEN;
731 
732  while (left) {
733  /* Fill this fragment */
734  fragsize = LWIP_MIN(left, nfb * 8);
735 
736 #if LWIP_NETIF_TX_SINGLE_PBUF
737  rambuf = pbuf_alloc(PBUF_IP, fragsize, PBUF_RAM);
738  if (rambuf == NULL) {
739  goto memerr;
740  }
741  LWIP_ASSERT("this needs a pbuf in one piece!",
742  (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
743  poff += pbuf_copy_partial(p, rambuf->payload, fragsize, poff);
744  /* make room for the IP header */
745  if (pbuf_header(rambuf, IP_HLEN)) {
746  pbuf_free(rambuf);
747  goto memerr;
748  }
749  /* fill in the IP header */
750  SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
751  iphdr = (struct ip_hdr*)rambuf->payload;
752 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
753  /* When not using a static buffer, create a chain of pbufs.
754  * The first will be a PBUF_RAM holding the link and IP header.
755  * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
756  * but limited to the size of an mtu.
757  */
758  rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
759  if (rambuf == NULL) {
760  goto memerr;
761  }
762  LWIP_ASSERT("this needs a pbuf in one piece!",
763  (p->len >= (IP_HLEN)));
764  SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
765  iphdr = (struct ip_hdr *)rambuf->payload;
766 
767  left_to_copy = fragsize;
768  while (left_to_copy) {
769  struct pbuf_custom_ref *pcr;
770  u16_t plen = p->len - poff;
771  newpbuflen = LWIP_MIN(left_to_copy, plen);
772  /* Is this pbuf already empty? */
773  if (!newpbuflen) {
774  poff = 0;
775  p = p->next;
776  continue;
777  }
778  pcr = ip_frag_alloc_pbuf_custom_ref();
779  if (pcr == NULL) {
780  pbuf_free(rambuf);
781  goto memerr;
782  }
783  /* Mirror this pbuf, although we might not need all of it. */
784  newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc,
785  (u8_t*)p->payload + poff, newpbuflen);
786  if (newpbuf == NULL) {
787  ip_frag_free_pbuf_custom_ref(pcr);
788  pbuf_free(rambuf);
789  goto memerr;
790  }
791  pbuf_ref(p);
792  pcr->original = p;
793  pcr->pc.custom_free_function = ipfrag_free_pbuf_custom;
794 
795  /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
796  * so that it is removed when pbuf_dechain is later called on rambuf.
797  */
798  pbuf_cat(rambuf, newpbuf);
799  left_to_copy -= newpbuflen;
800  if (left_to_copy) {
801  poff = 0;
802  p = p->next;
803  }
804  }
805  poff += newpbuflen;
806 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
807 
808  /* Correct header */
809  last = (left <= netif->mtu - IP_HLEN);
810 
811  /* Set new offset and MF flag */
812  tmp = (IP_OFFMASK & (ofo));
813  if (!last) {
814  tmp = tmp | IP_MF;
815  }
816  IPH_OFFSET_SET(iphdr, lwip_htons(tmp));
817  IPH_LEN_SET(iphdr, lwip_htons(fragsize + IP_HLEN));
818  IPH_CHKSUM_SET(iphdr, 0);
819 #if CHECKSUM_GEN_IP
820  IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
821  IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
822  }
823 #endif /* CHECKSUM_GEN_IP */
824 
825  /* No need for separate header pbuf - we allowed room for it in rambuf
826  * when allocated.
827  */
828  netif->output(netif, rambuf, dest);
829  IPFRAG_STATS_INC(ip_frag.xmit);
830 
831  /* Unfortunately we can't reuse rambuf - the hardware may still be
832  * using the buffer. Instead we free it (and the ensuing chain) and
833  * recreate it next time round the loop. If we're lucky the hardware
834  * will have already sent the packet, the free will really free, and
835  * there will be zero memory penalty.
836  */
837 
838  pbuf_free(rambuf);
839  left -= fragsize;
840  ofo += nfb;
841  }
842  MIB2_STATS_INC(mib2.ipfragoks);
843  return ERR_OK;
844 memerr:
845  MIB2_STATS_INC(mib2.ipfragfails);
846  return ERR_MEM;
847 }
848 #endif /* IP_FRAG */
849 
850 #endif /* LWIP_IPV4 */
u16_t tot_len
Definition: pbuf.h:175
Definition: pbuf.h:113
struct pbuf * next
Definition: pbuf.h:163
u16_t pbuf_clen(const struct pbuf *p)
Definition: pbuf.c:819
u16_t len
Definition: pbuf.h:178
#define ip_current_input_netif()
Definition: ip.h:156
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:684
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:839
void memp_free(memp_t type, void *mem)
Definition: memp.c:488
#define PACK_STRUCT_BEGIN
Definition: arch.h:261
Definition: err.h:84
#define IP_REASS_DEBUG
Definition: opt.h:2720
#define IP_REASS_MAX_PBUFS
Definition: opt.h:724
Definition: pbuf.h:135
Definition: pbuf.h:161
Definition: netif.h:244
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:859
#define PACK_STRUCT_END
Definition: arch.h:270
s8_t err_t
Definition: err.h:76
u16_t mtu
Definition: netif.h:318
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:292
#define LWIP_DEBUGF(debug, message)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:267
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1034
#define IP_REASS_MAXAGE
Definition: opt.h:714
Definition: pbuf.h:127
Definition: err.h:94
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:327
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:734
Definition: err.h:82
void * payload
Definition: pbuf.h:166
Definition: pbuf.h:99
void * memp_malloc(memp_t type)
Definition: memp.c:404