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