• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  *
4  * IPv6 fragmentation and reassembly.
5  */
6 
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 
58 /** Setting this to 0, you can turn off checking the fragments for overlapping
59  * regions. The code gets a little smaller. Only use this if you know that
60  * overlapping won't occur on your network! */
61 #ifndef IP_REASS_CHECK_OVERLAP
62 #define IP_REASS_CHECK_OVERLAP 1
63 #endif /* IP_REASS_CHECK_OVERLAP */
64 
65 /** Set to 0 to prevent freeing the oldest datagram when the reassembly buffer is
66  * full (IP_REASS_MAX_PBUFS pbufs are enqueued). The code gets a little smaller.
67  * Datagrams will be freed by timeout only. Especially useful when MEMP_NUM_REASSDATA
68  * is set to 1, so one datagram can be reassembled at a time, only. */
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 /* The number of bytes we need to "borrow" from (i.e., overwrite in) the header
75  * that precedes the fragment header for reassembly pruposes. */
76 #define IPV6_FRAG_REQROOM ((s16_t)(sizeof(struct ip6_reass_helper) - IP6_FRAG_HLEN))
77 #endif
78 
79 #define IP_REASS_FLAG_LASTFRAG 0x01
80 
81 /** This is a helper struct which holds the starting
82  * offset and the ending offset of this fragment to
83  * easily chain the fragments.
84  * It has the same packing requirements as the IPv6 header, since it replaces
85  * the Fragment Header in memory in incoming fragments to keep
86  * track of the various fragments.
87  */
88 #ifdef PACK_STRUCT_USE_INCLUDES
89 #  include "arch/bpstruct.h"
90 #endif
91 PACK_STRUCT_BEGIN
92 struct ip6_reass_helper {
93   PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
94   PACK_STRUCT_FIELD(u16_t start);
95   PACK_STRUCT_FIELD(u16_t end);
96 } PACK_STRUCT_STRUCT;
97 PACK_STRUCT_END
98 #ifdef PACK_STRUCT_USE_INCLUDES
99 #  include "arch/epstruct.h"
100 #endif
101 
102 /* static variables */
103 static struct ip6_reassdata *reassdatagrams;
104 static u32_t ip6_reass_pbufcount;
105 
106 /* Forward declarations. */
107 static void ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr);
108 #if IP_REASS_FREE_OLDEST
109 static void ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, u32_t pbufs_needed);
110 #endif /* IP_REASS_FREE_OLDEST */
111 
112 void
ip6_reass_tmr(void)113 ip6_reass_tmr(void)
114 {
115   struct ip6_reassdata *r, *tmp;
116 
117 #if !IPV6_FRAG_COPYHEADER
118   LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1",
119     sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN);
120 #endif /* !IPV6_FRAG_COPYHEADER */
121 
122   r = reassdatagrams;
123   while (r != NULL) {
124     /* Decrement the timer. Once it reaches 0,
125      * clean up the incomplete fragment assembly */
126     if (r->timer > 0) {
127       r->timer--;
128       r = r->next;
129     } else {
130       /* reassembly timed out */
131       tmp = r;
132       /* get the next pointer before freeing */
133       r = r->next;
134       /* free the helper struct and all enqueued pbufs */
135       ip6_reass_free_complete_datagram(tmp);
136      }
137    }
138 }
139 
140 #if LWIP_LOWPOWER
141 #include "lwip/lowpower.h"
142 u32_t
ip6_reass_tmr_tick(void)143 ip6_reass_tmr_tick(void)
144 {
145   u32_t tick = 0;
146   u32_t val = 0;
147   struct ip6_reassdata *r = NULL;
148 
149   r = reassdatagrams;
150   while (r != NULL) {
151     val = r->timer + 1;
152     SET_TMR_TICK(tick, val);
153     r = r->next;
154   }
155   LOWPOWER_DEBUG(("%s tmr tick: %u\n", __func__, tick));
156   return tick;
157 }
158 #endif /* LWIP_LOWPOWER */
159 
160 /**
161  * Free a datagram (struct ip6_reassdata) and all its pbufs.
162  * Updates the total count of enqueued pbufs (ip6_reass_pbufcount),
163  * sends an ICMP time exceeded packet.
164  *
165  * @param ipr datagram to free
166  */
167 static void
ip6_reass_free_complete_datagram(struct ip6_reassdata * ipr)168 ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr)
169 {
170   struct ip6_reassdata *prev;
171   u16_t pbufs_freed = 0;
172   u16_t clen;
173   struct pbuf *p;
174   struct ip6_reass_helper *iprh;
175 
176 #if LWIP_ICMP6
177   iprh = (struct ip6_reass_helper *)ipr->p->payload;
178   if (iprh->start == 0) {
179     /* The first fragment was received, send ICMP time exceeded. */
180     /* First, de-queue the first pbuf from r->p. */
181     p = ipr->p;
182     ipr->p = iprh->next_pbuf;
183     /* Restore the part that we've overwritten with our helper structure, or we
184      * might send garbage (and disclose a pointer) in the ICMPv6 reply. */
185     MEMCPY(p->payload, ipr->orig_hdr, sizeof(*iprh));
186     /* Then, move back to the original ipv6 header (we are now pointing to Fragment header).
187        This cannot fail since we already checked when receiving this fragment. */
188     if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)ipr->iphdr))) {
189       LWIP_ASSERT("ip6_reass_free: moving p->payload to ip6 header failed\n", 0);
190     }
191     else {
192       /* Reconstruct the zoned source and destination addresses, so that we do
193        * not end up sending the ICMP response over the wrong link. */
194       ip6_addr_t src_addr, dest_addr;
195       ip6_addr_copy_from_packed(src_addr, IPV6_FRAG_SRC(ipr));
196       ip6_addr_set_zone(&src_addr, ipr->src_zone);
197       ip6_addr_copy_from_packed(dest_addr, IPV6_FRAG_DEST(ipr));
198       ip6_addr_set_zone(&dest_addr, ipr->dest_zone);
199       /* Send the actual ICMP response. */
200       icmp6_time_exceeded_with_addrs(p, ICMP6_TE_FRAG, &src_addr, &dest_addr);
201     }
202     clen = pbuf_clen(p);
203     LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
204     pbufs_freed = (u16_t)(pbufs_freed + clen);
205     pbuf_free(p);
206   }
207 #endif /* LWIP_ICMP6 */
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 ip6_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 = (u16_t)(pbufs_freed + clen);
221     pbuf_free(pcur);
222   }
223 
224   /* Then, unchain the struct ip6_reassdata from the list and free it. */
225   if (ipr == reassdatagrams) {
226     reassdatagrams = ipr->next;
227   } else {
228     prev = reassdatagrams;
229     while (prev != NULL) {
230       if (prev->next == ipr) {
231         break;
232       }
233       prev = prev->next;
234     }
235     if (prev != NULL) {
236       prev->next = ipr->next;
237     }
238   }
239   memp_free(MEMP_IP6_REASSDATA, ipr);
240 
241   /* Finally, update number of pbufs in reassembly queue */
242   LWIP_ASSERT("ip_reass_pbufcount >= clen", ip6_reass_pbufcount >= pbufs_freed);
243   ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount - pbufs_freed);
244 }
245 
246 #if IP_REASS_FREE_OLDEST
247 /**
248  * Free the oldest datagram to make room for enqueueing new fragments.
249  * The datagram ipr is not freed!
250  *
251  * @param ipr ip6_reassdata for the current fragment
252  * @param pbufs_needed number of pbufs needed to enqueue
253  *        (used for freeing other datagrams if not enough space)
254  */
255 static void
ip6_reass_remove_oldest_datagram(struct ip6_reassdata * ipr,u32_t pbufs_needed)256 ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, u32_t pbufs_needed)
257 {
258   struct ip6_reassdata *r, *oldest;
259 
260   /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
261    * but don't free the current datagram! */
262   do {
263     r = oldest = reassdatagrams;
264     while (r != NULL) {
265       if (r != ipr) {
266         if (r->timer <= oldest->timer) {
267           /* older than the previous oldest */
268           oldest = r;
269         }
270       }
271       r = r->next;
272     }
273     if (oldest == ipr) {
274       /* nothing to free, ipr is the only element on the list */
275       return;
276     }
277     if (oldest != NULL) {
278       ip6_reass_free_complete_datagram(oldest);
279     }
280   } while (((ip6_reass_pbufcount + pbufs_needed) > IP_REASS_MAX_PBUFS) && (reassdatagrams != NULL));
281 }
282 #endif /* IP_REASS_FREE_OLDEST */
283 
284 /**
285  * Reassembles incoming IPv6 fragments into an IPv6 datagram.
286  *
287  * @param p points to the IPv6 Fragment Header
288  * @return NULL if reassembly is incomplete, pbuf pointing to
289  *         IPv6 Header if reassembly is complete
290  */
291 struct pbuf *
ip6_reass(struct pbuf * p)292 ip6_reass(struct pbuf *p)
293 {
294   struct ip6_reassdata *ipr, *ipr_prev;
295   struct ip6_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
296   struct ip6_frag_hdr *frag_hdr;
297   u16_t offset, len, start, end;
298   ptrdiff_t hdrdiff;
299   u16_t clen;
300   u8_t valid = 1;
301   struct pbuf *q, *next_pbuf;
302 
303   IP6_FRAG_STATS_INC(ip6_frag.recv);
304 
305   /* ip6_frag_hdr must be in the first pbuf, not chained. Checked by caller. */
306   LWIP_ASSERT("IPv6 fragment header does not fit in first pbuf",
307     p->len >= sizeof(struct ip6_frag_hdr));
308 
309   frag_hdr = (struct ip6_frag_hdr *) p->payload;
310 
311   clen = pbuf_clen(p);
312 
313   offset = lwip_ntohs(frag_hdr->_fragment_offset);
314 
315   /* Calculate fragment length from IPv6 payload length.
316    * Adjust for headers before Fragment Header.
317    * And finally adjust by Fragment Header length. */
318   len = lwip_ntohs(ip6_current_header()->_plen);
319   hdrdiff = (u8_t*)p->payload - (const u8_t*)ip6_current_header();
320   LWIP_ASSERT("not a valid pbuf (ip6_input check missing?)", hdrdiff <= 0xFFFF);
321   LWIP_ASSERT("not a valid pbuf (ip6_input check missing?)", hdrdiff >= IP6_HLEN);
322   hdrdiff -= IP6_HLEN;
323   hdrdiff += IP6_FRAG_HLEN;
324   if (hdrdiff > len) {
325     IP6_FRAG_STATS_INC(ip6_frag.proterr);
326     goto nullreturn;
327   }
328   len = (u16_t)(len - hdrdiff);
329   start = (offset & IP6_FRAG_OFFSET_MASK);
330   if (start > (0xFFFF - len)) {
331     /* u16_t overflow, cannot handle this */
332     IP6_FRAG_STATS_INC(ip6_frag.proterr);
333     goto nullreturn;
334   }
335 
336   /* Look for the datagram the fragment belongs to in the current datagram queue,
337    * remembering the previous in the queue for later dequeueing. */
338   for (ipr = reassdatagrams, ipr_prev = NULL; ipr != NULL; ipr = ipr->next) {
339     /* Check if the incoming fragment matches the one currently present
340        in the reassembly buffer. If so, we proceed with copying the
341        fragment into the buffer. */
342     if ((frag_hdr->_identification == ipr->identification) &&
343         ip6_addr_cmp_packed(ip6_current_src_addr(), &(IPV6_FRAG_SRC(ipr)), ipr->src_zone) &&
344         ip6_addr_cmp_packed(ip6_current_dest_addr(), &(IPV6_FRAG_DEST(ipr)), ipr->dest_zone)) {
345       IP6_FRAG_STATS_INC(ip6_frag.cachehit);
346       break;
347     }
348     ipr_prev = ipr;
349   }
350 
351   if (ipr == NULL) {
352   /* Enqueue a new datagram into the datagram queue */
353     ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
354     if (ipr == NULL) {
355 #if IP_REASS_FREE_OLDEST
356       /* Make room and try again. */
357       ip6_reass_remove_oldest_datagram(ipr, clen);
358       ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
359       if (ipr != NULL) {
360         /* re-search ipr_prev since it might have been removed */
361         for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
362           if (ipr_prev->next == ipr) {
363             break;
364           }
365         }
366       } else
367 #endif /* IP_REASS_FREE_OLDEST */
368       {
369         IP6_FRAG_STATS_INC(ip6_frag.memerr);
370         goto nullreturn;
371       }
372     }
373 
374     memset(ipr, 0, sizeof(struct ip6_reassdata));
375     ipr->timer = IPV6_REASS_MAXAGE;
376 
377     /* enqueue the new structure to the front of the list */
378     ipr->next = reassdatagrams;
379     reassdatagrams = ipr;
380 
381     /* Use the current IPv6 header for src/dest address reference.
382      * Eventually, we will replace it when we get the first fragment
383      * (it might be this one, in any case, it is done later). */
384     /* need to use the none-const pointer here: */
385     ipr->iphdr = ip_data.current_ip6_header;
386 #if IPV6_FRAG_COPYHEADER
387     MEMCPY(&ipr->src, &ip6_current_header()->src, sizeof(ipr->src));
388     MEMCPY(&ipr->dest, &ip6_current_header()->dest, sizeof(ipr->dest));
389 #endif /* IPV6_FRAG_COPYHEADER */
390 #if LWIP_IPV6_SCOPES
391     /* Also store the address zone information.
392      * @todo It is possible that due to netif destruction and recreation, the
393      * stored zones end up resolving to a different interface. In that case, we
394      * risk sending a "time exceeded" ICMP response over the wrong link.
395      * Ideally, netif destruction would clean up matching pending reassembly
396      * structures, but custom zone mappings would make that non-trivial. */
397     ipr->src_zone = ip6_addr_zone(ip6_current_src_addr());
398     ipr->dest_zone = ip6_addr_zone(ip6_current_dest_addr());
399 #endif /* LWIP_IPV6_SCOPES */
400     /* copy the fragmented packet id. */
401     ipr->identification = frag_hdr->_identification;
402 
403     /* copy the nexth field */
404     ipr->nexth = frag_hdr->_nexth;
405   } else {
406     if ((ipr->count + clen) > IP_REASS_MAX_PBUFS_PER_PKT) {
407         LWIP_DEBUGF(IP_REASS_DEBUG, ("ip6_reass: Overflow condition: Number of fragments exeeds per packet , "
408                                     "fragment count=%d, clen=%d, MAX per packet=%d\n",
409                                     ipr->count, clen, IP_REASS_MAX_PBUFS_PER_PKT));
410         /* free all fragments */
411       ip6_reass_free_complete_datagram(ipr);
412       IP6_FRAG_STATS_INC(ip6_frag.memerr);
413       IP6_FRAG_STATS_INC(ip6_frag.drop);
414       /* drop this pbuf */
415       goto nullreturn;
416     }
417   }
418 
419   /* Check if we are allowed to enqueue more datagrams. */
420   if ((ip6_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
421 #if IP_REASS_FREE_OLDEST
422     ip6_reass_remove_oldest_datagram(ipr, clen);
423     if ((ip6_reass_pbufcount + clen) <= IP_REASS_MAX_PBUFS) {
424       /* re-search ipr_prev since it might have been removed */
425       for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
426         if (ipr_prev->next == ipr) {
427           break;
428         }
429       }
430     } else
431 #endif /* IP_REASS_FREE_OLDEST */
432     {
433       /* @todo: send ICMPv6 time exceeded here? */
434       /* drop this pbuf */
435       IP6_FRAG_STATS_INC(ip6_frag.memerr);
436       goto nullreturn;
437     }
438   }
439 
440   /* Overwrite Fragment Header with our own helper struct. */
441 #if IPV6_FRAG_COPYHEADER
442   if (IPV6_FRAG_REQROOM > 0) {
443     /* Make room for struct ip6_reass_helper (only required if sizeof(void*) > 4).
444        This cannot fail since we already checked when receiving this fragment. */
445     u8_t hdrerr = pbuf_header_force(p, IPV6_FRAG_REQROOM);
446     LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
447     LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
448   }
449 #else /* IPV6_FRAG_COPYHEADER */
450   LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1",
451     sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN);
452 #endif /* IPV6_FRAG_COPYHEADER */
453 
454   /* Prepare the pointer to the helper structure, and its initial values.
455    * Do not yet write to the structure itself, as we still have to make a
456    * backup of the original data, and we should not do that until we know for
457    * sure that we are going to add this packet to the list. */
458   iprh = (struct ip6_reass_helper *)p->payload;
459   next_pbuf = NULL;
460   end = (u16_t)(start + len);
461 
462   /* find the right place to insert this pbuf */
463   /* Iterate through until we either get to the end of the list (append),
464    * or we find on with a larger offset (insert). */
465   for (q = ipr->p; q != NULL;) {
466     iprh_tmp = (struct ip6_reass_helper*)q->payload;
467     if (start < iprh_tmp->start) {
468 #if IP_REASS_CHECK_OVERLAP
469       if (end > iprh_tmp->start) {
470         /* fragment overlaps with following, throw away */
471         IP6_FRAG_STATS_INC(ip6_frag.proterr);
472         goto nullreturn;
473       }
474       if (iprh_prev != NULL) {
475         if (start < iprh_prev->end) {
476           /* fragment overlaps with previous, throw away */
477           IP6_FRAG_STATS_INC(ip6_frag.proterr);
478           goto nullreturn;
479         }
480       }
481 #endif /* IP_REASS_CHECK_OVERLAP */
482       /* the new pbuf should be inserted before this */
483       next_pbuf = q;
484       if (iprh_prev != NULL) {
485         /* not the fragment with the lowest offset */
486         iprh_prev->next_pbuf = p;
487       } else {
488         /* fragment with the lowest offset */
489         ipr->p = p;
490       }
491       break;
492     } else if (start == iprh_tmp->start) {
493       /* received the same datagram twice: no need to keep the datagram */
494       goto nullreturn;
495 #if IP_REASS_CHECK_OVERLAP
496     } else if (start < iprh_tmp->end) {
497       /* overlap: no need to keep the new datagram */
498       IP6_FRAG_STATS_INC(ip6_frag.proterr);
499       goto nullreturn;
500 #endif /* IP_REASS_CHECK_OVERLAP */
501     } else {
502       /* Check if the fragments received so far have no gaps. */
503       if (iprh_prev != NULL) {
504         if (iprh_prev->end != iprh_tmp->start) {
505           /* There is a fragment missing between the current
506            * and the previous fragment */
507           valid = 0;
508         }
509       }
510     }
511     q = iprh_tmp->next_pbuf;
512     iprh_prev = iprh_tmp;
513   }
514 
515   /* If q is NULL, then we made it to the end of the list. Determine what to do now */
516   if (q == NULL) {
517     if (iprh_prev != NULL) {
518       /* this is (for now), the fragment with the highest offset:
519        * chain it to the last fragment */
520 #if IP_REASS_CHECK_OVERLAP
521       LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= start);
522 #endif /* IP_REASS_CHECK_OVERLAP */
523       iprh_prev->next_pbuf = p;
524       if (iprh_prev->end != start) {
525         valid = 0;
526       }
527     } else {
528 #if IP_REASS_CHECK_OVERLAP
529       LWIP_ASSERT("no previous fragment, this must be the first fragment!",
530         ipr->p == NULL);
531 #endif /* IP_REASS_CHECK_OVERLAP */
532       /* this is the first fragment we ever received for this ip datagram */
533       ipr->p = p;
534     }
535   }
536 
537   /* Track the current number of pbufs current 'in-flight', in order to limit
538   the number of fragments that may be enqueued at any one time */
539   ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount + clen);
540   ipr->count         += clen;
541 
542   /* Remember IPv6 header if this is the first fragment. */
543   if (start == 0) {
544     /* need to use the none-const pointer here: */
545     ipr->iphdr = ip_data.current_ip6_header;
546     /* Make a backup of the part of the packet data that we are about to
547      * overwrite, so that we can restore the original later. */
548     MEMCPY(ipr->orig_hdr, p->payload, sizeof(*iprh));
549     /* For IPV6_FRAG_COPYHEADER there is no need to copy src/dst again, as they
550      * will be the same as they were. With LWIP_IPV6_SCOPES, the same applies
551      * to the source/destination zones. */
552   }
553   /* Only after the backup do we get to fill in the actual helper structure. */
554   iprh->next_pbuf = next_pbuf;
555   iprh->start = start;
556   iprh->end = end;
557 
558   /* If this is the last fragment, calculate total packet length. */
559   if ((offset & IP6_FRAG_MORE_FLAG) == 0) {
560     ipr->datagram_len = iprh->end;
561   }
562 
563   /* Additional validity tests: we have received first and last fragment. */
564   iprh_tmp = (struct ip6_reass_helper*)ipr->p->payload;
565   if (iprh_tmp->start != 0) {
566     valid = 0;
567   }
568 
569   /* Calculate recieved fragments length */
570   ipr->len += len;
571 
572   /* If last fragment is not received or the length of all fragments is less than the total length */
573   if ((ipr->datagram_len == 0) || (ipr->len < ipr->datagram_len)) {
574     valid = 0;
575   }
576 
577   /* Final validity test: no gaps between current and last fragment. */
578   iprh_prev = iprh;
579   q = iprh->next_pbuf;
580   while ((q != NULL) && valid) {
581     iprh = (struct ip6_reass_helper*)q->payload;
582     if (iprh_prev->end != iprh->start) {
583       valid = 0;
584       break;
585     }
586     iprh_prev = iprh;
587     q = iprh->next_pbuf;
588   }
589 
590   if (valid) {
591     /* All fragments have been received */
592     struct ip6_hdr* iphdr_ptr;
593 
594     /* chain together the pbufs contained within the ip6_reassdata list. */
595     iprh = (struct ip6_reass_helper*) ipr->p->payload;
596     while (iprh != NULL) {
597       next_pbuf = iprh->next_pbuf;
598       if (next_pbuf != NULL) {
599         /* Save next helper struct (will be hidden in next step). */
600         iprh_tmp = (struct ip6_reass_helper*)next_pbuf->payload;
601 
602         /* hide the fragment header for every succeeding fragment */
603         pbuf_remove_header(next_pbuf, IP6_FRAG_HLEN);
604 #if IPV6_FRAG_COPYHEADER
605         if (IPV6_FRAG_REQROOM > 0) {
606           /* hide the extra bytes borrowed from ip6_hdr for struct ip6_reass_helper */
607           u8_t hdrerr = pbuf_remove_header(next_pbuf, IPV6_FRAG_REQROOM);
608           LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
609           LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
610         }
611 #endif
612         pbuf_cat(ipr->p, next_pbuf);
613       }
614       else {
615         iprh_tmp = NULL;
616       }
617 
618       iprh = iprh_tmp;
619     }
620 
621     /* Get the first pbuf. */
622     p = ipr->p;
623 
624 #if IPV6_FRAG_COPYHEADER
625     if (IPV6_FRAG_REQROOM > 0) {
626       u8_t hdrerr;
627       /* Restore (only) the bytes that we overwrote beyond the fragment header.
628        * Those bytes may belong to either the IPv6 header or an extension
629        * header placed before the fragment header. */
630       MEMCPY(p->payload, ipr->orig_hdr, IPV6_FRAG_REQROOM);
631       /* get back room for struct ip6_reass_helper (only required if sizeof(void*) > 4) */
632       hdrerr = pbuf_remove_header(p, IPV6_FRAG_REQROOM);
633       LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
634       LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
635     }
636     iphdr_ptr = (struct ip6_hdr*)((u8_t*)ipr->p->payload - IP6_HLEN);
637     (void)memcpy_s(iphdr_ptr, IP6_HLEN, &ipr->iphdr, IP6_HLEN);
638 #else
639     iphdr_ptr = ipr->iphdr;
640 #endif
641 
642     /* Adjust datagram length by adding header lengths. */
643     ipr->datagram_len = (u16_t)(ipr->datagram_len + (((u8_t*)ipr->p->payload - (u8_t*)iphdr_ptr)
644                          + IP6_FRAG_HLEN
645                          - IP6_HLEN));
646 
647     /* Set payload length in ip header. */
648     iphdr_ptr->_plen = lwip_htons(ipr->datagram_len);
649 
650     /* Get the first pbuf. */
651     p = ipr->p;
652 
653     /* Restore Fragment Header in first pbuf. Mark as "single fragment"
654      * packet. Restore nexth. */
655     frag_hdr = (struct ip6_frag_hdr *) p->payload;
656     frag_hdr->_nexth = ipr->nexth;
657     frag_hdr->reserved = 0;
658     frag_hdr->_fragment_offset = 0;
659     frag_hdr->_identification = 0;
660 
661     /* release the resources allocated for the fragment queue entry */
662     if (reassdatagrams == ipr) {
663       /* it was the first in the list */
664       reassdatagrams = ipr->next;
665     } else {
666       /* it wasn't the first, so it must have a valid 'prev' */
667       LWIP_ASSERT("sanity check linked list", ipr_prev != NULL);
668       ipr_prev->next = ipr->next;
669     }
670     memp_free(MEMP_IP6_REASSDATA, ipr);
671 
672     /* adjust the number of pbufs currently queued for reassembly. */
673     clen = pbuf_clen(p);
674     LWIP_ASSERT("ip6_reass_pbufcount >= clen", ip6_reass_pbufcount >= clen);
675     ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount - clen);
676 
677     /* Move pbuf back to IPv6 header. This should never fail. */
678     if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)iphdr_ptr))) {
679       LWIP_ASSERT("ip6_reass: moving p->payload to ip6 header failed\n", 0);
680       pbuf_free(p);
681       return NULL;
682     }
683 
684     /* Return the pbuf chain */
685     return p;
686   }
687   /* the datagram is not (yet?) reassembled completely */
688   return NULL;
689 
690 nullreturn:
691   IP6_FRAG_STATS_INC(ip6_frag.drop);
692   pbuf_free(p);
693   return NULL;
694 }
695 
696 #endif /* LWIP_IPV6 && LWIP_IPV6_REASS */
697 
698 #if LWIP_IPV6 && LWIP_IPV6_FRAG
699 
700 #if !LWIP_NETIF_TX_SINGLE_PBUF
701 /** Allocate a new struct pbuf_custom_ref */
702 static struct pbuf_custom_ref*
ip6_frag_alloc_pbuf_custom_ref(void)703 ip6_frag_alloc_pbuf_custom_ref(void)
704 {
705   return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
706 }
707 
708 /** Free a struct pbuf_custom_ref */
709 static void
ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref * p)710 ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
711 {
712   LWIP_ASSERT("p != NULL", p != NULL);
713   memp_free(MEMP_FRAG_PBUF, p);
714 }
715 
716 /** Free-callback function to free a 'struct pbuf_custom_ref', called by
717  * pbuf_free. */
718 static void
ip6_frag_free_pbuf_custom(struct pbuf * p)719 ip6_frag_free_pbuf_custom(struct pbuf *p)
720 {
721   struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
722   LWIP_ASSERT("pcr != NULL", pcr != NULL);
723   LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
724   if (pcr->original != NULL) {
725     pbuf_free(pcr->original);
726   }
727   ip6_frag_free_pbuf_custom_ref(pcr);
728 }
729 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
730 
731 /**
732  * Fragment an IPv6 datagram if too large for the netif or path MTU.
733  *
734  * Chop the datagram in MTU sized chunks and send them in order
735  * by pointing PBUF_REFs into p
736  *
737  * @param p ipv6 packet to send
738  * @param netif the netif on which to send
739  * @param dest destination ipv6 address to which to send
740  *
741  * @return ERR_OK if sent successfully, err_t otherwise
742  */
743 err_t
ip6_frag(struct pbuf * p,struct netif * netif,const ip6_addr_t * dest)744 ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest)
745 {
746   struct ip6_hdr *original_ip6hdr;
747   struct ip6_hdr *ip6hdr;
748   struct ip6_frag_hdr *frag_hdr;
749   struct pbuf *rambuf;
750 #if LWIP_RIPPLE
751   struct ip6_hbh_hdr *hbh_hdr = NULL;
752 #endif
753 #if !LWIP_NETIF_TX_SINGLE_PBUF
754   struct pbuf *newpbuf;
755   u16_t newpbuflen = 0;
756   u16_t left_to_copy;
757 #endif
758   static u32_t identification;
759   u16_t left, cop;
760   u16_t fragment_offset = 0;
761   u16_t last;
762   u16_t hbh_len = 0;
763   u16_t poff = IP6_HLEN;
764 
765   identification++;
766 
767   original_ip6hdr = (struct ip6_hdr *)p->payload;
768 #if LWIP_RIPPLE
769   if ((!ip6_addr_ismulticast(dest)) &&
770       (IP6H_NEXTH(original_ip6hdr) == IP6_NEXTH_HOPBYHOP)) {
771     hbh_hdr = (struct ip6_hbh_hdr *)((char *)p->payload + IP6_HLEN);
772     /* 8 :offset for hbh in ipv6 header */
773     hbh_len = (hbh_hdr->_hlen << 3) + 8;
774   }
775 #endif
776   const u16_t mtu = nd6_get_destination_mtu(dest, netif);
777   const u16_t nfb = (u16_t)((mtu - (IP6_HLEN + hbh_len + IP6_FRAG_HLEN)) & IP6_FRAG_OFFSET_MASK);
778   poff += hbh_len;
779 
780   /* @todo we assume there are no options in the unfragmentable part (IPv6 header). */
781   LWIP_ASSERT("p->tot_len >= IP6_HLEN", p->tot_len >= IP6_HLEN);
782   left = (u16_t)(p->tot_len - IP6_HLEN - hbh_len);
783 
784   while (left) {
785     last = (left <= nfb);
786 
787     /* Fill this fragment */
788     cop = last ? left : nfb;
789 
790 #if LWIP_NETIF_TX_SINGLE_PBUF
791     rambuf = pbuf_alloc(PBUF_IP, cop + IP6_FRAG_HLEN, PBUF_RAM);
792     if (rambuf == NULL) {
793       IP6_FRAG_STATS_INC(ip6_frag.memerr);
794       return ERR_MEM;
795     }
796     LWIP_ASSERT("this needs a pbuf in one piece!",
797       (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
798 #if LWIP_RIPPLE
799     hbh_hdr = NULL;
800     if ((hbh_len != 0) && (rambuf->flags & PBUF_FLAG_HBH_SPACE)) {
801       /* fill in the HBH header */
802       if (memcpy_s(rambuf->payload, hbh_len, (u8_t *)original_ip6hdr + IP6_HLEN, hbh_len) != EOK) {
803         (void)pbuf_free(rambuf);
804         IP6_FRAG_STATS_INC(ip6_frag.memerr);
805         return ERR_MEM;
806       }
807       rambuf->tot_len += hbh_len;
808       rambuf->len += hbh_len;
809       hbh_hdr = (struct ip6_hbh_hdr *)rambuf->payload;
810       if (pbuf_header(rambuf, -hbh_len)) {
811         (void)pbuf_free(rambuf);
812         IP6_FRAG_STATS_INC(ip6_frag.memerr);
813         return ERR_MEM;
814       }
815     }
816 #endif
817     poff += pbuf_copy_partial(p, (u8_t*)rambuf->payload + IP6_FRAG_HLEN, cop, poff);
818     frag_hdr = (struct ip6_frag_hdr *)(rambuf->payload);
819 #if LWIP_RIPPLE
820     if ((hbh_len != 0) && (rambuf->flags & PBUF_FLAG_HBH_SPACE)) {
821       /* make room for the HBH header */
822       if (pbuf_header(rambuf, hbh_len)) {
823         (void)pbuf_free(rambuf);
824         IP6_FRAG_STATS_INC(ip6_frag.memerr);
825         return ERR_MEM;
826       }
827     }
828 #endif
829     /* make room for the IP header */
830     if (pbuf_add_header(rambuf, IP6_HLEN)) {
831       pbuf_free(rambuf);
832       IP6_FRAG_STATS_INC(ip6_frag.memerr);
833       return ERR_MEM;
834     }
835     /* fill in the IP header */
836     SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
837     ip6hdr = (struct ip6_hdr *)rambuf->payload;
838 #else
839     /* When not using a static buffer, create a chain of pbufs.
840      * The first will be a PBUF_RAM holding the link, IPv6, and Fragment header.
841      * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
842      * but limited to the size of an mtu.
843      */
844     rambuf = pbuf_alloc(PBUF_LINK, IP6_HLEN + IP6_FRAG_HLEN + hbh_len, PBUF_RAM);
845     if (rambuf == NULL) {
846       IP6_FRAG_STATS_INC(ip6_frag.memerr);
847       return ERR_MEM;
848     }
849     LWIP_ASSERT("this needs a pbuf in one piece!",
850                 (rambuf->len >= (IP6_HLEN)));
851     SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
852     ip6hdr = (struct ip6_hdr *)rambuf->payload;
853     frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN + hbh_len);
854 #if LWIP_RIPPLE
855     hbh_hdr = NULL;
856     if (hbh_len != 0) {
857       /* fill in the HBH header */
858       hbh_hdr = (struct ip6_hbh_hdr *)((u8_t *)rambuf->payload + IP6_HLEN);
859       (void)memcpy_s(hbh_hdr, hbh_len, (u8_t *)original_ip6hdr + IP6_HLEN, hbh_len);
860     }
861 #endif
862 
863     /* Can just adjust p directly for needed offset. */
864     p->payload = (u8_t *)p->payload + poff;
865     p->len = (u16_t)(p->len - poff);
866     p->tot_len = (u16_t)(p->tot_len - poff);
867 
868     left_to_copy = cop;
869     while (left_to_copy) {
870       struct pbuf_custom_ref *pcr;
871       newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
872       /* Is this pbuf already empty? */
873       if (!newpbuflen) {
874         p = p->next;
875         continue;
876       }
877       pcr = ip6_frag_alloc_pbuf_custom_ref();
878       if (pcr == NULL) {
879         pbuf_free(rambuf);
880         IP6_FRAG_STATS_INC(ip6_frag.memerr);
881         return ERR_MEM;
882       }
883       /* Mirror this pbuf, although we might not need all of it. */
884       newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
885       if (newpbuf == NULL) {
886         ip6_frag_free_pbuf_custom_ref(pcr);
887         pbuf_free(rambuf);
888         IP6_FRAG_STATS_INC(ip6_frag.memerr);
889         return ERR_MEM;
890       }
891       pbuf_ref(p);
892       pcr->original = p;
893       pcr->pc.custom_free_function = ip6_frag_free_pbuf_custom;
894 
895       /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
896        * so that it is removed when pbuf_dechain is later called on rambuf.
897        */
898       pbuf_cat(rambuf, newpbuf);
899       left_to_copy = (u16_t)(left_to_copy - newpbuflen);
900       if (left_to_copy) {
901         p = p->next;
902       }
903     }
904     poff = newpbuflen;
905 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
906 
907     /* Set headers */
908     frag_hdr->reserved = 0;
909     frag_hdr->_fragment_offset = lwip_htons((u16_t)((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG)));
910     frag_hdr->_identification = lwip_htonl(identification);
911 
912 #if LWIP_RIPPLE
913     if ((hbh_len != 0) && (hbh_hdr != NULL)) {
914       IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_HOPBYHOP);
915       frag_hdr->_nexth = hbh_hdr->_nexth;
916       IP6H_NEXTH_SET(hbh_hdr, IP6_NEXTH_FRAGMENT);
917       IP6H_PLEN_SET(ip6hdr, (u16_t)(cop + IP6_FRAG_HLEN + hbh_len));
918     } else {
919       IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
920       IP6H_PLEN_SET(ip6hdr, (u16_t)(cop + IP6_FRAG_HLEN));
921       frag_hdr->_nexth = original_ip6hdr->_nexth;
922     }
923 #else
924     frag_hdr->_nexth = original_ip6hdr->_nexth;
925     IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
926     IP6H_PLEN_SET(ip6hdr, (u16_t)(cop + IP6_FRAG_HLEN));
927 #endif
928 
929     /* No need for separate header pbuf - we allowed room for it in rambuf
930      * when allocated.
931      */
932     IP6_FRAG_STATS_INC(ip6_frag.xmit);
933 
934 #if LWIP_SO_PRIORITY
935     rambuf->priority = p->priority;
936 #endif /* LWIP_SO_PRIORITY */
937 
938     netif->output_ip6(netif, rambuf, dest);
939 
940     /* Unfortunately we can't reuse rambuf - the hardware may still be
941      * using the buffer. Instead we free it (and the ensuing chain) and
942      * recreate it next time round the loop. If we're lucky the hardware
943      * will have already sent the packet, the free will really free, and
944      * there will be zero memory penalty.
945      */
946 
947     pbuf_free(rambuf);
948     left = (u16_t)(left - cop);
949     fragment_offset = (u16_t)(fragment_offset + cop);
950   }
951   return ERR_OK;
952 }
953 
954 #endif /* LWIP_IPV6 && LWIP_IPV6_FRAG */
955