]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/ipv6/ip6_frag.c
a43fd614195aaa411a2bb067fdaf2be12da041dd
[pes-rpp/rpp-lwip.git] / src / core / ipv6 / ip6_frag.c
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
48 #include "lwip/pbuf.h"
49 #include "lwip/memp.h"
50 #include "lwip/stats.h"
51
52 #include <string.h>
53
54 #if LWIP_IPV6 && LWIP_IPV6_REASS  /* don't build if not configured for use in lwipopts.h */
55
56
57 /** Setting this to 0, you can turn off checking the fragments for overlapping
58  * regions. The code gets a little smaller. Only use this if you know that
59  * overlapping won't occur on your network! */
60 #ifndef IP_REASS_CHECK_OVERLAP
61 #define IP_REASS_CHECK_OVERLAP 1
62 #endif /* IP_REASS_CHECK_OVERLAP */
63
64 /** Set to 0 to prevent freeing the oldest datagram when the reassembly buffer is
65  * full (IP_REASS_MAX_PBUFS pbufs are enqueued). The code gets a little smaller.
66  * Datagrams will be freed by timeout only. Especially useful when MEMP_NUM_REASSDATA
67  * is set to 1, so one datagram can be reassembled at a time, only. */
68 #ifndef IP_REASS_FREE_OLDEST
69 #define IP_REASS_FREE_OLDEST 1
70 #endif /* IP_REASS_FREE_OLDEST */
71
72 #define IP_REASS_FLAG_LASTFRAG 0x01
73
74 /** This is a helper struct which holds the starting
75  * offset and the ending offset of this fragment to
76  * easily chain the fragments.
77  * It has the same packing requirements as the IPv6 header, since it replaces
78  * the Fragment Header in memory in incoming fragments to keep
79  * track of the various fragments.
80  */
81 #ifdef PACK_STRUCT_USE_INCLUDES
82 #  include "arch/bpstruct.h"
83 #endif
84 PACK_STRUCT_BEGIN
85 struct ip6_reass_helper {
86   PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
87   PACK_STRUCT_FIELD(u16_t start);
88   PACK_STRUCT_FIELD(u16_t end);
89 } PACK_STRUCT_STRUCT;
90 PACK_STRUCT_END
91 #ifdef PACK_STRUCT_USE_INCLUDES
92 #  include "arch/epstruct.h"
93 #endif
94
95 /* static variables */
96 static struct ip6_reassdata *reassdatagrams;
97 static u16_t ip6_reass_pbufcount;
98
99 /* Forward declarations. */
100 static void ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr);
101 #if IP_REASS_FREE_OLDEST
102 static void ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed);
103 #endif /* IP_REASS_FREE_OLDEST */
104
105 void
106 ip6_reass_tmr(void)
107 {
108   struct ip6_reassdata *r, *tmp;
109
110   r = reassdatagrams;
111   while (r != NULL) {
112     /* Decrement the timer. Once it reaches 0,
113      * clean up the incomplete fragment assembly */
114     if (r->timer > 0) {
115       r->timer--;
116       r = r->next;
117     } else {
118       /* reassembly timed out */
119       tmp = r;
120       /* get the next pointer before freeing */
121       r = r->next;
122       /* free the helper struct and all enqueued pbufs */
123       ip6_reass_free_complete_datagram(tmp);
124      }
125    }
126 }
127
128 /**
129  * Free a datagram (struct ip6_reassdata) and all its pbufs.
130  * Updates the total count of enqueued pbufs (ip6_reass_pbufcount),
131  * sends an ICMP time exceeded packet.
132  *
133  * @param ipr datagram to free
134  */
135 static void
136 ip6_reass_free_complete_datagram(struct ip6_reassdata *ipr)
137 {
138   struct ip6_reassdata *prev;
139   u16_t pbufs_freed = 0;
140   u8_t clen;
141   struct pbuf *p;
142   struct ip6_reass_helper *iprh;
143
144 #if LWIP_ICMP6
145   iprh = (struct ip6_reass_helper *)ipr->p->payload;
146   if (iprh->start == 0) {
147     /* The first fragment was received, send ICMP time exceeded. */
148     /* First, de-queue the first pbuf from r->p. */
149     p = ipr->p;
150     ipr->p = iprh->next_pbuf;
151     /* Then, move back to the original header (we are now pointing to Fragment header). */
152     if (pbuf_header(p, (u8_t*)p->payload - (u8_t*)ipr->iphdr)) {
153       LWIP_ASSERT("ip6_reass_free: moving p->payload to ip6 header failed\n", 0);
154     }
155     else {
156       icmp6_time_exceeded(p, ICMP6_TE_FRAG);
157     }
158     clen = pbuf_clen(p);
159     LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
160     pbufs_freed += clen;
161     pbuf_free(p);
162   }
163 #endif /* LWIP_ICMP6 */
164
165   /* First, free all received pbufs.  The individual pbufs need to be released
166      separately as they have not yet been chained */
167   p = ipr->p;
168   while (p != NULL) {
169     struct pbuf *pcur;
170     iprh = (struct ip6_reass_helper *)p->payload;
171     pcur = p;
172     /* get the next pointer before freeing */
173     p = iprh->next_pbuf;
174     clen = pbuf_clen(pcur);
175     LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
176     pbufs_freed += clen;
177     pbuf_free(pcur);
178   }
179
180   /* Then, unchain the struct ip6_reassdata from the list and free it. */
181   if (ipr == reassdatagrams) {
182     reassdatagrams = ipr->next;
183   } else {
184     prev = reassdatagrams;
185     while (prev != NULL) {
186       if (prev->next == ipr) {
187         break;
188       }
189       prev = prev->next;
190     }
191     if (prev != NULL) {
192       prev->next = ipr->next;
193     }
194   }
195   memp_free(MEMP_IP6_REASSDATA, ipr);
196
197   /* Finally, update number of pbufs in reassembly queue */
198   LWIP_ASSERT("ip_reass_pbufcount >= clen", ip6_reass_pbufcount >= pbufs_freed);
199   ip6_reass_pbufcount -= pbufs_freed;
200 }
201
202 #if IP_REASS_FREE_OLDEST
203 /**
204  * Free the oldest datagram to make room for enqueueing new fragments.
205  * The datagram ipr is not freed!
206  *
207  * @param ipr ip6_reassdata for the current fragment
208  * @param pbufs_needed number of pbufs needed to enqueue
209  *        (used for freeing other datagrams if not enough space)
210  */
211 static void
212 ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed)
213 {
214   struct ip6_reassdata *r, *oldest;
215
216   /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
217    * but don't free the current datagram! */
218   do {
219     r = oldest = reassdatagrams;
220     while (r != NULL) {
221       if (r != ipr) {
222         if (r->timer <= oldest->timer) {
223           /* older than the previous oldest */
224           oldest = r;
225         }
226       }
227       r = r->next;
228     }
229     if (oldest != NULL) {
230       ip6_reass_free_complete_datagram(oldest);
231     }
232   } while (((ip6_reass_pbufcount + pbufs_needed) > IP_REASS_MAX_PBUFS) && (reassdatagrams != NULL));
233 }
234 #endif /* IP_REASS_FREE_OLDEST */
235
236 /**
237  * Reassembles incoming IPv6 fragments into an IPv6 datagram.
238  *
239  * @param p points to the IPv6 Fragment Header
240  * @param len the length of the payload (after Fragment Header)
241  * @return NULL if reassembly is incomplete, pbuf pointing to
242  *         IPv6 Header if reassembly is complete
243  */
244 struct pbuf *
245 ip6_reass(struct pbuf *p)
246 {
247   struct ip6_reassdata *ipr, *ipr_prev;
248   struct ip6_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
249   struct ip6_frag_hdr * frag_hdr;
250   u16_t offset, len;
251   u8_t clen, valid = 1;
252   struct pbuf *q;
253
254   IP6_FRAG_STATS_INC(ip6_frag.recv);
255
256   frag_hdr = (struct ip6_frag_hdr *) p->payload;
257
258   clen = pbuf_clen(p);
259
260   offset = ntohs(frag_hdr->_fragment_offset);
261
262   /* Calculate fragment length from IPv6 payload length.
263    * Adjust for headers before Fragment Header.
264    * And finally adjust by Fragment Header length. */
265   len = ntohs(ip6_current_header()->_plen);
266   len -= ((u8_t*)p->payload - (u8_t*)ip6_current_header()) - IP6_HLEN;
267   len -= IP6_FRAG_HLEN;
268
269   /* Look for the datagram the fragment belongs to in the current datagram queue,
270    * remembering the previous in the queue for later dequeueing. */
271   for (ipr = reassdatagrams, ipr_prev = NULL; ipr != NULL; ipr = ipr->next) {
272     /* Check if the incoming fragment matches the one currently present
273        in the reassembly buffer. If so, we proceed with copying the
274        fragment into the buffer. */
275     if ((frag_hdr->_identification == ipr->identification) &&
276         ip6_addr_cmp(ip6_current_src_addr(), &(ipr->iphdr->src)) &&
277         ip6_addr_cmp(ip6_current_dest_addr(), &(ipr->iphdr->dest))) {
278       IP6_FRAG_STATS_INC(ip6_frag.cachehit);
279       break;
280     }
281     ipr_prev = ipr;
282   }
283
284   if (ipr == NULL) {
285   /* Enqueue a new datagram into the datagram queue */
286     ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
287     if (ipr == NULL) {
288 #if IP_REASS_FREE_OLDEST
289       /* Make room and try again. */
290       ip6_reass_remove_oldest_datagram(ipr, clen);
291       ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
292       if (ipr == NULL)
293 #endif /* IP_REASS_FREE_OLDEST */
294       {
295         IP6_FRAG_STATS_INC(ip6_frag.memerr);
296         IP6_FRAG_STATS_INC(ip6_frag.drop);
297         goto nullreturn;
298       }
299     }
300
301     memset(ipr, 0, sizeof(struct ip6_reassdata));
302     ipr->timer = IP_REASS_MAXAGE;
303
304     /* enqueue the new structure to the front of the list */
305     ipr->next = reassdatagrams;
306     reassdatagrams = ipr;
307
308     /* Use the current IPv6 header for src/dest address reference.
309      * Eventually, we will replace it when we get the first fragment
310      * (it might be this one, in any case, it is done later). */
311     ipr->iphdr = (struct ip6_hdr *)ip6_current_header();
312
313     /* copy the fragmented packet id. */
314     ipr->identification = frag_hdr->_identification;
315
316     /* copy the nexth field */
317     ipr->nexth = frag_hdr->_nexth;
318   }
319
320   /* Check if we are allowed to enqueue more datagrams. */
321   if ((ip6_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
322 #if IP_REASS_FREE_OLDEST
323     ip6_reass_remove_oldest_datagram(ipr, clen);
324     if ((ip6_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS)
325 #endif /* IP_REASS_FREE_OLDEST */
326     {
327       /* @todo: send ICMPv6 time exceeded here? */
328       /* drop this pbuf */
329       IP6_FRAG_STATS_INC(ip6_frag.memerr);
330       IP6_FRAG_STATS_INC(ip6_frag.drop);
331       goto nullreturn;
332     }
333   }
334
335   /* Overwrite Fragment Header with our own helper struct. */
336   iprh = (struct ip6_reass_helper *)p->payload;
337   iprh->next_pbuf = NULL;
338   iprh->start = (offset & IP6_FRAG_OFFSET_MASK);
339   iprh->end = (offset & IP6_FRAG_OFFSET_MASK) + len;
340
341   /* find the right place to insert this pbuf */
342   /* Iterate through until we either get to the end of the list (append),
343    * or we find on with a larger offset (insert). */
344   for (q = ipr->p; q != NULL;) {
345     iprh_tmp = (struct ip6_reass_helper*)q->payload;
346     if (iprh->start < iprh_tmp->start) {
347 #if IP_REASS_CHECK_OVERLAP
348       if (iprh->end > iprh_tmp->start) {
349         /* fragment overlaps with following, throw away */
350         IP6_FRAG_STATS_INC(ip6_frag.proterr);
351         IP6_FRAG_STATS_INC(ip6_frag.drop);
352         goto nullreturn;
353       }
354       if (iprh_prev != NULL) {
355         if (iprh->start < iprh_prev->end) {
356           /* fragment overlaps with previous, throw away */
357           IP6_FRAG_STATS_INC(ip6_frag.proterr);
358           IP6_FRAG_STATS_INC(ip6_frag.drop);
359           goto nullreturn;
360         }
361       }
362 #endif /* IP_REASS_CHECK_OVERLAP */
363       /* the new pbuf should be inserted before this */
364       iprh->next_pbuf = q;
365       if (iprh_prev != NULL) {
366         /* not the fragment with the lowest offset */
367         iprh_prev->next_pbuf = p;
368       } else {
369         /* fragment with the lowest offset */
370         ipr->p = p;
371       }
372       break;
373     } else if(iprh->start == iprh_tmp->start) {
374       /* received the same datagram twice: no need to keep the datagram */
375       IP6_FRAG_STATS_INC(ip6_frag.drop);
376       goto nullreturn;
377 #if IP_REASS_CHECK_OVERLAP
378     } else if(iprh->start < iprh_tmp->end) {
379       /* overlap: no need to keep the new datagram */
380       IP6_FRAG_STATS_INC(ip6_frag.proterr);
381       IP6_FRAG_STATS_INC(ip6_frag.drop);
382       goto nullreturn;
383 #endif /* IP_REASS_CHECK_OVERLAP */
384     } else {
385       /* Check if the fragments received so far have no gaps. */
386       if (iprh_prev != NULL) {
387         if (iprh_prev->end != iprh_tmp->start) {
388           /* There is a fragment missing between the current
389            * and the previous fragment */
390           valid = 0;
391         }
392       }
393     }
394     q = iprh_tmp->next_pbuf;
395     iprh_prev = iprh_tmp;
396   }
397
398   /* If q is NULL, then we made it to the end of the list. Determine what to do now */
399   if (q == NULL) {
400     if (iprh_prev != NULL) {
401       /* this is (for now), the fragment with the highest offset:
402        * chain it to the last fragment */
403 #if IP_REASS_CHECK_OVERLAP
404       LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
405 #endif /* IP_REASS_CHECK_OVERLAP */
406       iprh_prev->next_pbuf = p;
407       if (iprh_prev->end != iprh->start) {
408         valid = 0;
409       }
410     } else {
411 #if IP_REASS_CHECK_OVERLAP
412       LWIP_ASSERT("no previous fragment, this must be the first fragment!",
413         ipr->p == NULL);
414 #endif /* IP_REASS_CHECK_OVERLAP */
415       /* this is the first fragment we ever received for this ip datagram */
416       ipr->p = p;
417     }
418   }
419
420   /* Track the current number of pbufs current 'in-flight', in order to limit
421   the number of fragments that may be enqueued at any one time */
422   ip6_reass_pbufcount += clen;
423
424   /* Remember IPv6 header if this is the first fragment. */
425   if (iprh->start == 0) {
426     ipr->iphdr = (struct ip6_hdr *)ip6_current_header();
427   }
428
429   /* If this is the last fragment, calculate total packet length. */
430   if ((offset & IP6_FRAG_MORE_FLAG) == 0) {
431     ipr->datagram_len = iprh->end;
432   }
433
434   /* Additional validity tests: we have received first and last fragment. */
435   iprh_tmp = (struct ip6_reass_helper*)ipr->p->payload;
436   if (iprh_tmp->start != 0) {
437     valid = 0;
438   }
439   if (ipr->datagram_len == 0) {
440     valid = 0;
441   }
442
443   /* Final validity test: no gaps between current and last fragment. */
444   iprh_prev = iprh;
445   q = iprh->next_pbuf;
446   while ((q != NULL) && valid) {
447     iprh = (struct ip6_reass_helper*)q->payload;
448     if (iprh_prev->end != iprh->start) {
449       valid = 0;
450       break;
451     }
452     iprh_prev = iprh;
453     q = iprh->next_pbuf;
454   }
455
456   if (valid) {
457     /* All fragments have been received */
458
459     /* chain together the pbufs contained within the ip6_reassdata list. */
460     iprh = (struct ip6_reass_helper*) ipr->p->payload;
461     while(iprh != NULL) {
462
463       if (iprh->next_pbuf != NULL) {
464         /* Save next helper struct (will be hidden in next step). */
465         iprh_tmp = (struct ip6_reass_helper*) iprh->next_pbuf->payload;
466
467         /* hide the fragment header for every succeding fragment */
468         pbuf_header(iprh->next_pbuf, -IP6_FRAG_HLEN);
469         pbuf_cat(ipr->p, iprh->next_pbuf);
470       }
471       else {
472         iprh_tmp = NULL;
473       }
474
475       iprh = iprh_tmp;
476     }
477
478     /* Adjust datagram length by adding header lengths. */
479     ipr->datagram_len += ((u8_t*)ipr->p->payload - (u8_t*)ipr->iphdr)
480                          + IP6_FRAG_HLEN
481                          - IP6_HLEN ;
482
483     /* Set payload length in ip header. */
484     ipr->iphdr->_plen = htons(ipr->datagram_len);
485
486     /* Get the furst pbuf. */
487     p = ipr->p;
488
489     /* Restore Fragment Header in first pbuf. Mark as "single fragment"
490      * packet. Restore nexth. */
491     frag_hdr = (struct ip6_frag_hdr *) p->payload;
492     frag_hdr->_nexth = ipr->nexth;
493     frag_hdr->reserved = 0;
494     frag_hdr->_fragment_offset = 0;
495     frag_hdr->_identification = 0;
496
497     /* release the sources allocate for the fragment queue entry */
498     if (reassdatagrams == ipr) {
499       /* it was the first in the list */
500       reassdatagrams = ipr->next;
501     } else {
502       /* it wasn't the first, so it must have a valid 'prev' */
503       LWIP_ASSERT("sanity check linked list", ipr_prev != NULL);
504       ipr_prev->next = ipr->next;
505     }
506     memp_free(MEMP_IP6_REASSDATA, ipr);
507
508     /* adjust the number of pbufs currently queued for reassembly. */
509     ip6_reass_pbufcount -= pbuf_clen(p);
510
511     /* Move pbuf back to IPv6 header. */
512     if (pbuf_header(p, (u8_t*)p->payload - (u8_t*)ipr->iphdr)) {
513       LWIP_ASSERT("ip6_reass: moving p->payload to ip6 header failed\n", 0);
514       pbuf_free(p);
515       return NULL;
516     }
517
518     /* Return the pbuf chain */
519     return p;
520   }
521   /* the datagram is not (yet?) reassembled completely */
522   return NULL;
523
524 nullreturn:
525   pbuf_free(p);
526   return NULL;
527 }
528
529 #endif /* LWIP_IPV6 ^^ LWIP_IPV6_REASS */
530
531 #if LWIP_IPV6 && LWIP_IPV6_FRAG
532
533 /** Allocate a new struct pbuf_custom_ref */
534 static struct pbuf_custom_ref*
535 ip6_frag_alloc_pbuf_custom_ref(void)
536 {
537   return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
538 }
539
540 /** Free a struct pbuf_custom_ref */
541 static void
542 ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
543 {
544   LWIP_ASSERT("p != NULL", p != NULL);
545   memp_free(MEMP_FRAG_PBUF, p);
546 }
547
548 /** Free-callback function to free a 'struct pbuf_custom_ref', called by
549  * pbuf_free. */
550 static void
551 ip6_frag_free_pbuf_custom(struct pbuf *p)
552 {
553   struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
554   LWIP_ASSERT("pcr != NULL", pcr != NULL);
555   LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
556   if (pcr->original != NULL) {
557     pbuf_free(pcr->original);
558   }
559   ip6_frag_free_pbuf_custom_ref(pcr);
560 }
561
562 /**
563  * Fragment an IPv6 datagram if too large for the netif or path MTU.
564  *
565  * Chop the datagram in MTU sized chunks and send them in order
566  * by pointing PBUF_REFs into p
567  *
568  * @param p ipv6 packet to send
569  * @param netif the netif on which to send
570  * @param dest destination ipv6 address to which to send
571  *
572  * @return ERR_OK if sent successfully, err_t otherwise
573  */
574 err_t
575 ip6_frag(struct pbuf *p, struct netif *netif, ip6_addr_t *dest)
576 {
577   struct ip6_hdr *original_ip6hdr;
578   struct ip6_hdr *ip6hdr;
579   struct ip6_frag_hdr * frag_hdr;
580   struct pbuf *rambuf;
581   struct pbuf *newpbuf;
582   static u32_t identification;
583   u16_t nfb;
584   u16_t left, cop;
585   u16_t mtu;
586   u16_t fragment_offset = 0;
587   u16_t last;
588   u16_t poff = IP6_HLEN;
589   u16_t newpbuflen = 0;
590   u16_t left_to_copy;
591
592   identification++;
593
594   original_ip6hdr = (struct ip6_hdr *)p->payload;
595
596   mtu = nd6_get_destination_mtu(dest, netif);
597
598   /* TODO we assume there are no options in the unfragmentable part (IPv6 header). */
599   left = p->tot_len - IP6_HLEN;
600
601   nfb = (mtu - (IP6_HLEN + IP6_FRAG_HLEN)) & IP6_FRAG_OFFSET_MASK;
602
603   while (left) {
604     last = (left <= nfb);
605
606     /* Fill this fragment */
607     cop = last ? left : nfb;
608
609     /* When not using a static buffer, create a chain of pbufs.
610      * The first will be a PBUF_RAM holding the link, IPv6, and Fragment header.
611      * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
612      * but limited to the size of an mtu.
613      */
614     rambuf = pbuf_alloc(PBUF_LINK, IP6_HLEN + IP6_FRAG_HLEN, PBUF_RAM);
615     if (rambuf == NULL) {
616       IP6_FRAG_STATS_INC(ip6_frag.memerr);
617       return ERR_MEM;
618     }
619     LWIP_ASSERT("this needs a pbuf in one piece!",
620                 (p->len >= (IP6_HLEN + IP6_FRAG_HLEN)));
621     SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
622     ip6hdr = (struct ip6_hdr *)rambuf->payload;
623     frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);
624
625     /* Can just adjust p directly for needed offset. */
626     p->payload = (u8_t *)p->payload + poff;
627     p->len -= poff;
628     p->tot_len -= poff;
629
630     left_to_copy = cop;
631     while (left_to_copy) {
632       struct pbuf_custom_ref *pcr;
633       newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
634       /* Is this pbuf already empty? */
635       if (!newpbuflen) {
636         p = p->next;
637         continue;
638       }
639       pcr = ip6_frag_alloc_pbuf_custom_ref();
640       if (pcr == NULL) {
641         pbuf_free(rambuf);
642         IP6_FRAG_STATS_INC(ip6_frag.memerr);
643         return ERR_MEM;
644       }
645       /* Mirror this pbuf, although we might not need all of it. */
646       newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
647       if (newpbuf == NULL) {
648         ip6_frag_free_pbuf_custom_ref(pcr);
649         pbuf_free(rambuf);
650         IP6_FRAG_STATS_INC(ip6_frag.memerr);
651         return ERR_MEM;
652       }
653       pbuf_ref(p);
654       pcr->original = p;
655       pcr->pc.custom_free_function = ip6_frag_free_pbuf_custom;
656
657       /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
658        * so that it is removed when pbuf_dechain is later called on rambuf.
659        */
660       pbuf_cat(rambuf, newpbuf);
661       left_to_copy -= newpbuflen;
662       if (left_to_copy) {
663         p = p->next;
664       }
665     }
666     poff = newpbuflen;
667
668     /* Set headers */
669     frag_hdr->_nexth = original_ip6hdr->_nexth;
670     frag_hdr->reserved = 0;
671     frag_hdr->_fragment_offset = htons((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG));
672     frag_hdr->_identification = htonl(identification);
673
674     IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
675     IP6H_PLEN_SET(ip6hdr, cop + IP6_FRAG_HLEN);
676
677     /* No need for separate header pbuf - we allowed room for it in rambuf
678      * when allocated.
679      */
680     IP6_FRAG_STATS_INC(ip6_frag.xmit);
681     netif->output_ip6(netif, rambuf, dest);
682
683     /* Unfortunately we can't reuse rambuf - the hardware may still be
684      * using the buffer. Instead we free it (and the ensuing chain) and
685      * recreate it next time round the loop. If we're lucky the hardware
686      * will have already sent the packet, the free will really free, and
687      * there will be zero memory penalty.
688      */
689
690     pbuf_free(rambuf);
691     left -= cop;
692     fragment_offset += cop;
693   }
694   return ERR_OK;
695 }
696
697 #endif /* LWIP_IPV6 && LWIP_IPV6_FRAG */