The Pedigree Project  0.1
ip6_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) 2010 Inico Technologies Ltd.
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: Ivan Delamer <delamer@inicotech.com>
55  *
56  *
57  * Please coordinate changes and requests with Ivan Delamer
58  * <delamer@inicotech.com>
59  */
60 
61 #include "lwip/opt.h"
62 #include "lwip/ip6_frag.h"
63 #include "lwip/ip6.h"
64 #include "lwip/icmp6.h"
65 #include "lwip/nd6.h"
66 #include "lwip/ip.h"
67 
68 #include "lwip/pbuf.h"
69 #include "lwip/memp.h"
70 #include "lwip/stats.h"
71 
72 #include <string.h>
73 
74 #if LWIP_IPV6 && LWIP_IPV6_REASS /* don't build if not configured for use in lwipopts.h */
75 
76 
80 #ifndef IP_REASS_CHECK_OVERLAP
81 #define IP_REASS_CHECK_OVERLAP 1
82 #endif /* IP_REASS_CHECK_OVERLAP */
83 
88 #ifndef IP_REASS_FREE_OLDEST
89 #define IP_REASS_FREE_OLDEST 1
90 #endif /* IP_REASS_FREE_OLDEST */
91 
92 #if IPV6_FRAG_COPYHEADER
93 #define IPV6_FRAG_REQROOM ((s16_t)(sizeof(struct ip6_reass_helper) - IP6_FRAG_HLEN))
94 #endif
95 
96 #define IP_REASS_FLAG_LASTFRAG 0x01
97 
105 #ifdef PACK_STRUCT_USE_INCLUDES
106 # include "arch/bpstruct.h"
107 #endif
109 struct ip6_reass_helper {
110  PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
111  PACK_STRUCT_FIELD(u16_t start);
112  PACK_STRUCT_FIELD(u16_t end);
113 } PACK_STRUCT_STRUCT;
115 #ifdef PACK_STRUCT_USE_INCLUDES
116 # include "arch/epstruct.h"
117 #endif
118 
119 /* static variables */
120 static struct ip6_reassdata *reassdatagrams;
121 static u16_t ip6_reass_pbufcount;
122 
123 /* Forward declarations. */
124 static void ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr);
125 #if IP_REASS_FREE_OLDEST
126 static void ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed);
127 #endif /* IP_REASS_FREE_OLDEST */
128 
129 void
130 ip6_reass_tmr(void)
131 {
132  struct ip6_reassdata *r, *tmp;
133 
134 #if !IPV6_FRAG_COPYHEADER
135  LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1",
136  sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN);
137 #endif /* !IPV6_FRAG_COPYHEADER */
138 
139  r = reassdatagrams;
140  while (r != NULL) {
141  /* Decrement the timer. Once it reaches 0,
142  * clean up the incomplete fragment assembly */
143  if (r->timer > 0) {
144  r->timer--;
145  r = r->next;
146  } else {
147  /* reassembly timed out */
148  tmp = r;
149  /* get the next pointer before freeing */
150  r = r->next;
151  /* free the helper struct and all enqueued pbufs */
152  ip6_reass_free_complete_datagram(tmp);
153  }
154  }
155 }
156 
164 static void
165 ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr)
166 {
167  struct ip6_reassdata *prev;
168  u16_t pbufs_freed = 0;
169  u16_t clen;
170  struct pbuf *p;
171  struct ip6_reass_helper *iprh;
172 
173 #if LWIP_ICMP6
174  iprh = (struct ip6_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, move back to the original ipv6 header (we are now pointing to Fragment header).
181  This cannot fail since we already checked when receiving this fragment. */
182  if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)IPV6_FRAG_HDRREF(ipr->iphdr)))) {
183  LWIP_ASSERT("ip6_reass_free: moving p->payload to ip6 header failed\n", 0);
184  }
185  else {
186  icmp6_time_exceeded(p, ICMP6_TE_FRAG);
187  }
188  clen = pbuf_clen(p);
189  LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
190  pbufs_freed += clen;
191  pbuf_free(p);
192  }
193 #endif /* LWIP_ICMP6 */
194 
195  /* First, free all received pbufs. The individual pbufs need to be released
196  separately as they have not yet been chained */
197  p = ipr->p;
198  while (p != NULL) {
199  struct pbuf *pcur;
200  iprh = (struct ip6_reass_helper *)p->payload;
201  pcur = p;
202  /* get the next pointer before freeing */
203  p = iprh->next_pbuf;
204  clen = pbuf_clen(pcur);
205  LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
206  pbufs_freed += clen;
207  pbuf_free(pcur);
208  }
209 
210  /* Then, unchain the struct ip6_reassdata from the list and free it. */
211  if (ipr == reassdatagrams) {
212  reassdatagrams = ipr->next;
213  } else {
214  prev = reassdatagrams;
215  while (prev != NULL) {
216  if (prev->next == ipr) {
217  break;
218  }
219  prev = prev->next;
220  }
221  if (prev != NULL) {
222  prev->next = ipr->next;
223  }
224  }
225  memp_free(MEMP_IP6_REASSDATA, ipr);
226 
227  /* Finally, update number of pbufs in reassembly queue */
228  LWIP_ASSERT("ip_reass_pbufcount >= clen", ip6_reass_pbufcount >= pbufs_freed);
229  ip6_reass_pbufcount -= pbufs_freed;
230 }
231 
232 #if IP_REASS_FREE_OLDEST
233 
241 static void
242 ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed)
243 {
244  struct ip6_reassdata *r, *oldest;
245 
246  /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
247  * but don't free the current datagram! */
248  do {
249  r = oldest = reassdatagrams;
250  while (r != NULL) {
251  if (r != ipr) {
252  if (r->timer <= oldest->timer) {
253  /* older than the previous oldest */
254  oldest = r;
255  }
256  }
257  r = r->next;
258  }
259  if (oldest == ipr) {
260  /* nothing to free, ipr is the only element on the list */
261  return;
262  }
263  if (oldest != NULL) {
264  ip6_reass_free_complete_datagram(oldest);
265  }
266  } while (((ip6_reass_pbufcount + pbufs_needed) > IP_REASS_MAX_PBUFS) && (reassdatagrams != NULL));
267 }
268 #endif /* IP_REASS_FREE_OLDEST */
269 
277 struct pbuf *
278 ip6_reass(struct pbuf *p)
279 {
280  struct ip6_reassdata *ipr, *ipr_prev;
281  struct ip6_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
282  struct ip6_frag_hdr *frag_hdr;
283  u16_t offset, len;
284  u16_t clen;
285  u8_t valid = 1;
286  struct pbuf *q;
287 
288  IP6_FRAG_STATS_INC(ip6_frag.recv);
289 
290  if ((const void*)ip6_current_header() != ((u8_t*)p->payload) - IP6_HLEN) {
291  /* ip6_frag_hdr must be in the first pbuf, not chained */
292  IP6_FRAG_STATS_INC(ip6_frag.proterr);
293  IP6_FRAG_STATS_INC(ip6_frag.drop);
294  goto nullreturn;
295  }
296 
297  frag_hdr = (struct ip6_frag_hdr *) p->payload;
298 
299  clen = pbuf_clen(p);
300 
301  offset = lwip_ntohs(frag_hdr->_fragment_offset);
302 
303  /* Calculate fragment length from IPv6 payload length.
304  * Adjust for headers before Fragment Header.
305  * And finally adjust by Fragment Header length. */
306  len = lwip_ntohs(ip6_current_header()->_plen);
307  len -= (u16_t)(((u8_t*)p->payload - (const u8_t*)ip6_current_header()) - IP6_HLEN);
308  len -= IP6_FRAG_HLEN;
309 
310  /* Look for the datagram the fragment belongs to in the current datagram queue,
311  * remembering the previous in the queue for later dequeueing. */
312  for (ipr = reassdatagrams, ipr_prev = NULL; ipr != NULL; ipr = ipr->next) {
313  /* Check if the incoming fragment matches the one currently present
314  in the reassembly buffer. If so, we proceed with copying the
315  fragment into the buffer. */
316  if ((frag_hdr->_identification == ipr->identification) &&
317  ip6_addr_cmp(ip6_current_src_addr(), &(IPV6_FRAG_HDRREF(ipr->iphdr)->src)) &&
318  ip6_addr_cmp(ip6_current_dest_addr(), &(IPV6_FRAG_HDRREF(ipr->iphdr)->dest))) {
319  IP6_FRAG_STATS_INC(ip6_frag.cachehit);
320  break;
321  }
322  ipr_prev = ipr;
323  }
324 
325  if (ipr == NULL) {
326  /* Enqueue a new datagram into the datagram queue */
327  ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
328  if (ipr == NULL) {
329 #if IP_REASS_FREE_OLDEST
330  /* Make room and try again. */
331  ip6_reass_remove_oldest_datagram(ipr, clen);
332  ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
333  if (ipr != NULL) {
334  /* re-search ipr_prev since it might have been removed */
335  for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
336  if (ipr_prev->next == ipr) {
337  break;
338  }
339  }
340  } else
341 #endif /* IP_REASS_FREE_OLDEST */
342  {
343  IP6_FRAG_STATS_INC(ip6_frag.memerr);
344  IP6_FRAG_STATS_INC(ip6_frag.drop);
345  goto nullreturn;
346  }
347  }
348 
349  memset(ipr, 0, sizeof(struct ip6_reassdata));
350  ipr->timer = IP_REASS_MAXAGE;
351 
352  /* enqueue the new structure to the front of the list */
353  ipr->next = reassdatagrams;
354  reassdatagrams = ipr;
355 
356  /* Use the current IPv6 header for src/dest address reference.
357  * Eventually, we will replace it when we get the first fragment
358  * (it might be this one, in any case, it is done later). */
359 #if IPV6_FRAG_COPYHEADER
360  MEMCPY(&ipr->iphdr, ip6_current_header(), IP6_HLEN);
361 #else /* IPV6_FRAG_COPYHEADER */
362  /* need to use the none-const pointer here: */
363  ipr->iphdr = ip_data.current_ip6_header;
364 #endif /* IPV6_FRAG_COPYHEADER */
365 
366  /* copy the fragmented packet id. */
367  ipr->identification = frag_hdr->_identification;
368 
369  /* copy the nexth field */
370  ipr->nexth = frag_hdr->_nexth;
371  }
372 
373  /* Check if we are allowed to enqueue more datagrams. */
374  if ((ip6_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
375 #if IP_REASS_FREE_OLDEST
376  ip6_reass_remove_oldest_datagram(ipr, clen);
377  if ((ip6_reass_pbufcount + clen) <= IP_REASS_MAX_PBUFS) {
378  /* re-search ipr_prev since it might have been removed */
379  for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
380  if (ipr_prev->next == ipr) {
381  break;
382  }
383  }
384  } else
385 #endif /* IP_REASS_FREE_OLDEST */
386  {
387  /* @todo: send ICMPv6 time exceeded here? */
388  /* drop this pbuf */
389  IP6_FRAG_STATS_INC(ip6_frag.memerr);
390  IP6_FRAG_STATS_INC(ip6_frag.drop);
391  goto nullreturn;
392  }
393  }
394 
395  /* Overwrite Fragment Header with our own helper struct. */
396 #if IPV6_FRAG_COPYHEADER
397  if (IPV6_FRAG_REQROOM > 0) {
398  /* Make room for struct ip6_reass_helper (only required if sizeof(void*) > 4).
399  This cannot fail since we already checked when receiving this fragment. */
400  u8_t hdrerr = pbuf_header_force(p, IPV6_FRAG_REQROOM);
401  LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
402  LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
403  }
404 #else /* IPV6_FRAG_COPYHEADER */
405  LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1",
406  sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN);
407 #endif /* IPV6_FRAG_COPYHEADER */
408  iprh = (struct ip6_reass_helper *)p->payload;
409  iprh->next_pbuf = NULL;
410  iprh->start = (offset & IP6_FRAG_OFFSET_MASK);
411  iprh->end = (offset & IP6_FRAG_OFFSET_MASK) + len;
412 
413  /* find the right place to insert this pbuf */
414  /* Iterate through until we either get to the end of the list (append),
415  * or we find on with a larger offset (insert). */
416  for (q = ipr->p; q != NULL;) {
417  iprh_tmp = (struct ip6_reass_helper*)q->payload;
418  if (iprh->start < iprh_tmp->start) {
419 #if IP_REASS_CHECK_OVERLAP
420  if (iprh->end > iprh_tmp->start) {
421  /* fragment overlaps with following, throw away */
422  IP6_FRAG_STATS_INC(ip6_frag.proterr);
423  IP6_FRAG_STATS_INC(ip6_frag.drop);
424  goto nullreturn;
425  }
426  if (iprh_prev != NULL) {
427  if (iprh->start < iprh_prev->end) {
428  /* fragment overlaps with previous, throw away */
429  IP6_FRAG_STATS_INC(ip6_frag.proterr);
430  IP6_FRAG_STATS_INC(ip6_frag.drop);
431  goto nullreturn;
432  }
433  }
434 #endif /* IP_REASS_CHECK_OVERLAP */
435  /* the new pbuf should be inserted before this */
436  iprh->next_pbuf = q;
437  if (iprh_prev != NULL) {
438  /* not the fragment with the lowest offset */
439  iprh_prev->next_pbuf = p;
440  } else {
441  /* fragment with the lowest offset */
442  ipr->p = p;
443  }
444  break;
445  } else if (iprh->start == iprh_tmp->start) {
446  /* received the same datagram twice: no need to keep the datagram */
447  IP6_FRAG_STATS_INC(ip6_frag.drop);
448  goto nullreturn;
449 #if IP_REASS_CHECK_OVERLAP
450  } else if (iprh->start < iprh_tmp->end) {
451  /* overlap: no need to keep the new datagram */
452  IP6_FRAG_STATS_INC(ip6_frag.proterr);
453  IP6_FRAG_STATS_INC(ip6_frag.drop);
454  goto nullreturn;
455 #endif /* IP_REASS_CHECK_OVERLAP */
456  } else {
457  /* Check if the fragments received so far have no gaps. */
458  if (iprh_prev != NULL) {
459  if (iprh_prev->end != iprh_tmp->start) {
460  /* There is a fragment missing between the current
461  * and the previous fragment */
462  valid = 0;
463  }
464  }
465  }
466  q = iprh_tmp->next_pbuf;
467  iprh_prev = iprh_tmp;
468  }
469 
470  /* If q is NULL, then we made it to the end of the list. Determine what to do now */
471  if (q == NULL) {
472  if (iprh_prev != NULL) {
473  /* this is (for now), the fragment with the highest offset:
474  * chain it to the last fragment */
475 #if IP_REASS_CHECK_OVERLAP
476  LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
477 #endif /* IP_REASS_CHECK_OVERLAP */
478  iprh_prev->next_pbuf = p;
479  if (iprh_prev->end != iprh->start) {
480  valid = 0;
481  }
482  } else {
483 #if IP_REASS_CHECK_OVERLAP
484  LWIP_ASSERT("no previous fragment, this must be the first fragment!",
485  ipr->p == NULL);
486 #endif /* IP_REASS_CHECK_OVERLAP */
487  /* this is the first fragment we ever received for this ip datagram */
488  ipr->p = p;
489  }
490  }
491 
492  /* Track the current number of pbufs current 'in-flight', in order to limit
493  the number of fragments that may be enqueued at any one time */
494  ip6_reass_pbufcount += clen;
495 
496  /* Remember IPv6 header if this is the first fragment. */
497  if (iprh->start == 0) {
498 #if IPV6_FRAG_COPYHEADER
499  if (iprh->next_pbuf != NULL) {
500  MEMCPY(&ipr->iphdr, ip6_current_header(), IP6_HLEN);
501  }
502 #else /* IPV6_FRAG_COPYHEADER */
503  /* need to use the none-const pointer here: */
504  ipr->iphdr = ip_data.current_ip6_header;
505 #endif /* IPV6_FRAG_COPYHEADER */
506  }
507 
508  /* If this is the last fragment, calculate total packet length. */
509  if ((offset & IP6_FRAG_MORE_FLAG) == 0) {
510  ipr->datagram_len = iprh->end;
511  }
512 
513  /* Additional validity tests: we have received first and last fragment. */
514  iprh_tmp = (struct ip6_reass_helper*)ipr->p->payload;
515  if (iprh_tmp->start != 0) {
516  valid = 0;
517  }
518  if (ipr->datagram_len == 0) {
519  valid = 0;
520  }
521 
522  /* Final validity test: no gaps between current and last fragment. */
523  iprh_prev = iprh;
524  q = iprh->next_pbuf;
525  while ((q != NULL) && valid) {
526  iprh = (struct ip6_reass_helper*)q->payload;
527  if (iprh_prev->end != iprh->start) {
528  valid = 0;
529  break;
530  }
531  iprh_prev = iprh;
532  q = iprh->next_pbuf;
533  }
534 
535  if (valid) {
536  /* All fragments have been received */
537  struct ip6_hdr* iphdr_ptr;
538 
539  /* chain together the pbufs contained within the ip6_reassdata list. */
540  iprh = (struct ip6_reass_helper*) ipr->p->payload;
541  while (iprh != NULL) {
542  struct pbuf* next_pbuf = iprh->next_pbuf;
543  if (next_pbuf != NULL) {
544  /* Save next helper struct (will be hidden in next step). */
545  iprh_tmp = (struct ip6_reass_helper*)next_pbuf->payload;
546 
547  /* hide the fragment header for every succeeding fragment */
548  pbuf_header(next_pbuf, -IP6_FRAG_HLEN);
549 #if IPV6_FRAG_COPYHEADER
550  if (IPV6_FRAG_REQROOM > 0) {
551  /* hide the extra bytes borrowed from ip6_hdr for struct ip6_reass_helper */
552  u8_t hdrerr = pbuf_header(next_pbuf, -(s16_t)(IPV6_FRAG_REQROOM));
553  LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
554  LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
555  }
556 #endif
557  pbuf_cat(ipr->p, next_pbuf);
558  }
559  else {
560  iprh_tmp = NULL;
561  }
562 
563  iprh = iprh_tmp;
564  }
565 
566 #if IPV6_FRAG_COPYHEADER
567  if (IPV6_FRAG_REQROOM > 0) {
568  /* get back room for struct ip6_reass_helper (only required if sizeof(void*) > 4) */
569  u8_t hdrerr = pbuf_header(ipr->p, -(s16_t)(IPV6_FRAG_REQROOM));
570  LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
571  LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
572  }
573  iphdr_ptr = (struct ip6_hdr*)((u8_t*)ipr->p->payload - IP6_HLEN);
574  MEMCPY(iphdr_ptr, &ipr->iphdr, IP6_HLEN);
575 #else
576  iphdr_ptr = ipr->iphdr;
577 #endif
578 
579  /* Adjust datagram length by adding header lengths. */
580  ipr->datagram_len += (u16_t)(((u8_t*)ipr->p->payload - (u8_t*)iphdr_ptr)
581  + IP6_FRAG_HLEN
582  - IP6_HLEN);
583 
584  /* Set payload length in ip header. */
585  iphdr_ptr->_plen = lwip_htons(ipr->datagram_len);
586 
587  /* Get the first pbuf. */
588  p = ipr->p;
589 
590  /* Restore Fragment Header in first pbuf. Mark as "single fragment"
591  * packet. Restore nexth. */
592  frag_hdr = (struct ip6_frag_hdr *) p->payload;
593  frag_hdr->_nexth = ipr->nexth;
594  frag_hdr->reserved = 0;
595  frag_hdr->_fragment_offset = 0;
596  frag_hdr->_identification = 0;
597 
598  /* release the sources allocate for the fragment queue entry */
599  if (reassdatagrams == ipr) {
600  /* it was the first in the list */
601  reassdatagrams = ipr->next;
602  } else {
603  /* it wasn't the first, so it must have a valid 'prev' */
604  LWIP_ASSERT("sanity check linked list", ipr_prev != NULL);
605  ipr_prev->next = ipr->next;
606  }
607  memp_free(MEMP_IP6_REASSDATA, ipr);
608 
609  /* adjust the number of pbufs currently queued for reassembly. */
610  ip6_reass_pbufcount -= pbuf_clen(p);
611 
612  /* Move pbuf back to IPv6 header.
613  This cannot fail since we already checked when receiving this fragment. */
614  if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)iphdr_ptr))) {
615  LWIP_ASSERT("ip6_reass: moving p->payload to ip6 header failed\n", 0);
616  pbuf_free(p);
617  return NULL;
618  }
619 
620  /* Return the pbuf chain */
621  return p;
622  }
623  /* the datagram is not (yet?) reassembled completely */
624  return NULL;
625 
626 nullreturn:
627  pbuf_free(p);
628  return NULL;
629 }
630 
631 #endif /* LWIP_IPV6 && LWIP_IPV6_REASS */
632 
633 #if LWIP_IPV6 && LWIP_IPV6_FRAG
634 
635 #if !LWIP_NETIF_TX_SINGLE_PBUF
636 
637 static struct pbuf_custom_ref*
638 ip6_frag_alloc_pbuf_custom_ref(void)
639 {
640  return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
641 }
642 
644 static void
645 ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
646 {
647  LWIP_ASSERT("p != NULL", p != NULL);
648  memp_free(MEMP_FRAG_PBUF, p);
649 }
650 
653 static void
654 ip6_frag_free_pbuf_custom(struct pbuf *p)
655 {
656  struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
657  LWIP_ASSERT("pcr != NULL", pcr != NULL);
658  LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
659  if (pcr->original != NULL) {
660  pbuf_free(pcr->original);
661  }
662  ip6_frag_free_pbuf_custom_ref(pcr);
663 }
664 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
665 
678 err_t
679 ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest)
680 {
681  struct ip6_hdr *original_ip6hdr;
682  struct ip6_hdr *ip6hdr;
683  struct ip6_frag_hdr *frag_hdr;
684  struct pbuf *rambuf;
685 #if !LWIP_NETIF_TX_SINGLE_PBUF
686  struct pbuf *newpbuf;
687  u16_t newpbuflen = 0;
688  u16_t left_to_copy;
689 #endif
690  static u32_t identification;
691  u16_t nfb;
692  u16_t left, cop;
693  u16_t mtu;
694  u16_t fragment_offset = 0;
695  u16_t last;
696  u16_t poff = IP6_HLEN;
697 
698  identification++;
699 
700  original_ip6hdr = (struct ip6_hdr *)p->payload;
701 
702  mtu = nd6_get_destination_mtu(dest, netif);
703 
704  /* @todo we assume there are no options in the unfragmentable part (IPv6 header). */
705  left = p->tot_len - IP6_HLEN;
706 
707  nfb = (mtu - (IP6_HLEN + IP6_FRAG_HLEN)) & IP6_FRAG_OFFSET_MASK;
708 
709  while (left) {
710  last = (left <= nfb);
711 
712  /* Fill this fragment */
713  cop = last ? left : nfb;
714 
715 #if LWIP_NETIF_TX_SINGLE_PBUF
716  rambuf = pbuf_alloc(PBUF_IP, cop + IP6_FRAG_HLEN, PBUF_RAM);
717  if (rambuf == NULL) {
718  IP6_FRAG_STATS_INC(ip6_frag.memerr);
719  return ERR_MEM;
720  }
721  LWIP_ASSERT("this needs a pbuf in one piece!",
722  (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
723  poff += pbuf_copy_partial(p, (u8_t*)rambuf->payload + IP6_FRAG_HLEN, cop, poff);
724  /* make room for the IP header */
725  if (pbuf_header(rambuf, IP6_HLEN)) {
726  pbuf_free(rambuf);
727  IP6_FRAG_STATS_INC(ip6_frag.memerr);
728  return ERR_MEM;
729  }
730  /* fill in the IP header */
731  SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
732  ip6hdr = (struct ip6_hdr *)rambuf->payload;
733  frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);
734 #else
735  /* When not using a static buffer, create a chain of pbufs.
736  * The first will be a PBUF_RAM holding the link, IPv6, and Fragment header.
737  * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
738  * but limited to the size of an mtu.
739  */
740  rambuf = pbuf_alloc(PBUF_LINK, IP6_HLEN + IP6_FRAG_HLEN, PBUF_RAM);
741  if (rambuf == NULL) {
742  IP6_FRAG_STATS_INC(ip6_frag.memerr);
743  return ERR_MEM;
744  }
745  LWIP_ASSERT("this needs a pbuf in one piece!",
746  (p->len >= (IP6_HLEN)));
747  SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
748  ip6hdr = (struct ip6_hdr *)rambuf->payload;
749  frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);
750 
751  /* Can just adjust p directly for needed offset. */
752  p->payload = (u8_t *)p->payload + poff;
753  p->len -= poff;
754  p->tot_len -= poff;
755 
756  left_to_copy = cop;
757  while (left_to_copy) {
758  struct pbuf_custom_ref *pcr;
759  newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
760  /* Is this pbuf already empty? */
761  if (!newpbuflen) {
762  p = p->next;
763  continue;
764  }
765  pcr = ip6_frag_alloc_pbuf_custom_ref();
766  if (pcr == NULL) {
767  pbuf_free(rambuf);
768  IP6_FRAG_STATS_INC(ip6_frag.memerr);
769  return ERR_MEM;
770  }
771  /* Mirror this pbuf, although we might not need all of it. */
772  newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
773  if (newpbuf == NULL) {
774  ip6_frag_free_pbuf_custom_ref(pcr);
775  pbuf_free(rambuf);
776  IP6_FRAG_STATS_INC(ip6_frag.memerr);
777  return ERR_MEM;
778  }
779  pbuf_ref(p);
780  pcr->original = p;
781  pcr->pc.custom_free_function = ip6_frag_free_pbuf_custom;
782 
783  /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
784  * so that it is removed when pbuf_dechain is later called on rambuf.
785  */
786  pbuf_cat(rambuf, newpbuf);
787  left_to_copy -= newpbuflen;
788  if (left_to_copy) {
789  p = p->next;
790  }
791  }
792  poff = newpbuflen;
793 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
794 
795  /* Set headers */
796  frag_hdr->_nexth = original_ip6hdr->_nexth;
797  frag_hdr->reserved = 0;
798  frag_hdr->_fragment_offset = lwip_htons((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG));
799  frag_hdr->_identification = lwip_htonl(identification);
800 
801  IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
802  IP6H_PLEN_SET(ip6hdr, cop + IP6_FRAG_HLEN);
803 
804  /* No need for separate header pbuf - we allowed room for it in rambuf
805  * when allocated.
806  */
807  IP6_FRAG_STATS_INC(ip6_frag.xmit);
808  netif->output_ip6(netif, rambuf, dest);
809 
810  /* Unfortunately we can't reuse rambuf - the hardware may still be
811  * using the buffer. Instead we free it (and the ensuing chain) and
812  * recreate it next time round the loop. If we're lucky the hardware
813  * will have already sent the packet, the free will really free, and
814  * there will be zero memory penalty.
815  */
816 
817  pbuf_free(rambuf);
818  left -= cop;
819  fragment_offset += cop;
820  }
821  return ERR_OK;
822 }
823 
824 #endif /* LWIP_IPV6 && LWIP_IPV6_FRAG */
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
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_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
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:292
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
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:694
#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