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 u16_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, int 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 LWIP_DEBUGF(LOWPOWER_DEBUG, ("%s tmr tick: %u\n", "ip6_reass_tmr_tick", 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 if (ipr->p != NULL) {
178 iprh = (struct ip6_reass_helper *)ipr->p->payload;
179 if (iprh->start == 0) {
180 /* The first fragment was received, send ICMP time exceeded. */
181 /* First, de-queue the first pbuf from r->p. */
182 p = ipr->p;
183 ipr->p = iprh->next_pbuf;
184 /* Restore the part that we've overwritten with our helper structure, or we
185 * might send garbage (and disclose a pointer) in the ICMPv6 reply. */
186 MEMCPY(p->payload, ipr->orig_hdr, sizeof(*iprh));
187 /* Then, move back to the original ipv6 header (we are now pointing to Fragment header).
188 This cannot fail since we already checked when receiving this fragment. */
189 if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)ipr->iphdr))) {
190 LWIP_ASSERT("ip6_reass_free: moving p->payload to ip6 header failed", 0);
191 }
192 else {
193 /* Reconstruct the zoned source and destination addresses, so that we do
194 * not end up sending the ICMP response over the wrong link. */
195 ip6_addr_t src_addr, dest_addr;
196 ip6_addr_copy_from_packed(src_addr, IPV6_FRAG_SRC(ipr));
197 ip6_addr_set_zone(&src_addr, ipr->src_zone);
198 ip6_addr_copy_from_packed(dest_addr, IPV6_FRAG_DEST(ipr));
199 ip6_addr_set_zone(&dest_addr, ipr->dest_zone);
200 /* Send the actual ICMP response. */
201 icmp6_time_exceeded_with_addrs(p, ICMP6_TE_FRAG, &src_addr, &dest_addr);
202 }
203 clen = pbuf_clen(p);
204 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
205 pbufs_freed = (u16_t)(pbufs_freed + clen);
206 pbuf_free(p);
207 }
208 }
209 #endif /* LWIP_ICMP6 */
210
211 /* First, free all received pbufs. The individual pbufs need to be released
212 separately as they have not yet been chained */
213 p = ipr->p;
214 while (p != NULL) {
215 struct pbuf *pcur;
216 iprh = (struct ip6_reass_helper *)p->payload;
217 pcur = p;
218 /* get the next pointer before freeing */
219 p = iprh->next_pbuf;
220 clen = pbuf_clen(pcur);
221 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
222 pbufs_freed = (u16_t)(pbufs_freed + clen);
223 pbuf_free(pcur);
224 }
225
226 /* Then, unchain the struct ip6_reassdata from the list and free it. */
227 if (ipr == reassdatagrams) {
228 reassdatagrams = ipr->next;
229 } else {
230 prev = reassdatagrams;
231 while (prev != NULL) {
232 if (prev->next == ipr) {
233 break;
234 }
235 prev = prev->next;
236 }
237 if (prev != NULL) {
238 prev->next = ipr->next;
239 }
240 }
241 memp_free(MEMP_IP6_REASSDATA, ipr);
242
243 /* Finally, update number of pbufs in reassembly queue */
244 LWIP_ASSERT("ip_reass_pbufcount >= clen", ip6_reass_pbufcount >= pbufs_freed);
245 ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount - pbufs_freed);
246 }
247
248 #if IP_REASS_FREE_OLDEST
249 /**
250 * Free the oldest datagram to make room for enqueueing new fragments.
251 * The datagram ipr is not freed!
252 *
253 * @param ipr ip6_reassdata for the current fragment
254 * @param pbufs_needed number of pbufs needed to enqueue
255 * (used for freeing other datagrams if not enough space)
256 */
257 static void
ip6_reass_remove_oldest_datagram(struct ip6_reassdata * ipr,int pbufs_needed)258 ip6_reass_remove_oldest_datagram(struct ip6_reassdata *ipr, int pbufs_needed)
259 {
260 struct ip6_reassdata *r, *oldest;
261
262 /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
263 * but don't free the current datagram! */
264 do {
265 r = oldest = reassdatagrams;
266 while (r != NULL) {
267 if (r != ipr) {
268 if (r->timer <= oldest->timer) {
269 /* older than the previous oldest */
270 oldest = r;
271 }
272 }
273 r = r->next;
274 }
275 if (oldest == ipr) {
276 /* nothing to free, ipr is the only element on the list */
277 return;
278 }
279 if (oldest != NULL) {
280 ip6_reass_free_complete_datagram(oldest);
281 }
282 } while (((ip6_reass_pbufcount + pbufs_needed) > IP_REASS_MAX_PBUFS) && (reassdatagrams != NULL));
283 }
284 #endif /* IP_REASS_FREE_OLDEST */
285
286 /**
287 * Reassembles incoming IPv6 fragments into an IPv6 datagram.
288 *
289 * @param p points to the IPv6 Fragment Header
290 * @return NULL if reassembly is incomplete, pbuf pointing to
291 * IPv6 Header if reassembly is complete
292 */
293 struct pbuf *
ip6_reass(struct pbuf * p)294 ip6_reass(struct pbuf *p)
295 {
296 struct ip6_reassdata *ipr, *ipr_prev;
297 struct ip6_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
298 struct ip6_frag_hdr *frag_hdr;
299 u16_t offset, len, start, end;
300 ptrdiff_t hdrdiff;
301 u16_t clen;
302 u8_t valid = 1;
303 struct pbuf *q, *next_pbuf;
304
305 IP6_FRAG_STATS_INC(ip6_frag.recv);
306
307 /* ip6_frag_hdr must be in the first pbuf, not chained. Checked by caller. */
308 LWIP_ASSERT("IPv6 fragment header does not fit in first pbuf",
309 p->len >= sizeof(struct ip6_frag_hdr));
310
311 frag_hdr = (struct ip6_frag_hdr *) p->payload;
312
313 clen = pbuf_clen(p);
314
315 offset = lwip_ntohs(frag_hdr->_fragment_offset);
316
317 /* Calculate fragment length from IPv6 payload length.
318 * Adjust for headers before Fragment Header.
319 * And finally adjust by Fragment Header length. */
320 len = lwip_ntohs(ip6_current_header()->_plen);
321 hdrdiff = (u8_t*)p->payload - (const u8_t*)ip6_current_header();
322 LWIP_ASSERT("not a valid pbuf (ip6_input check missing?)", hdrdiff <= 0xFFFF);
323 LWIP_ASSERT("not a valid pbuf (ip6_input check missing?)", hdrdiff >= IP6_HLEN);
324 hdrdiff -= IP6_HLEN;
325 hdrdiff += IP6_FRAG_HLEN;
326 if (hdrdiff > len) {
327 IP6_FRAG_STATS_INC(ip6_frag.proterr);
328 goto nullreturn;
329 }
330 len = (u16_t)(len - hdrdiff);
331 start = (offset & IP6_FRAG_OFFSET_MASK);
332 if (start > (0xFFFF - len)) {
333 /* u16_t overflow, cannot handle this */
334 IP6_FRAG_STATS_INC(ip6_frag.proterr);
335 goto nullreturn;
336 }
337
338 /* Look for the datagram the fragment belongs to in the current datagram queue,
339 * remembering the previous in the queue for later dequeueing. */
340 for (ipr = reassdatagrams, ipr_prev = NULL; ipr != NULL; ipr = ipr->next) {
341 /* Check if the incoming fragment matches the one currently present
342 in the reassembly buffer. If so, we proceed with copying the
343 fragment into the buffer. */
344 if ((frag_hdr->_identification == ipr->identification) &&
345 ip6_addr_packed_eq(ip6_current_src_addr(), &(IPV6_FRAG_SRC(ipr)), ipr->src_zone) &&
346 ip6_addr_packed_eq(ip6_current_dest_addr(), &(IPV6_FRAG_DEST(ipr)), ipr->dest_zone)) {
347 IP6_FRAG_STATS_INC(ip6_frag.cachehit);
348 break;
349 }
350 ipr_prev = ipr;
351 }
352
353 if (ipr == NULL) {
354 /* Enqueue a new datagram into the datagram queue */
355 ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
356 if (ipr == NULL) {
357 #if IP_REASS_FREE_OLDEST
358 /* Make room and try again. */
359 ip6_reass_remove_oldest_datagram(ipr, clen);
360 ipr = (struct ip6_reassdata *)memp_malloc(MEMP_IP6_REASSDATA);
361 if (ipr != NULL) {
362 /* re-search ipr_prev since it might have been removed */
363 for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
364 if (ipr_prev->next == ipr) {
365 break;
366 }
367 }
368 } else
369 #endif /* IP_REASS_FREE_OLDEST */
370 {
371 IP6_FRAG_STATS_INC(ip6_frag.memerr);
372 goto nullreturn;
373 }
374 }
375
376 memset(ipr, 0, sizeof(struct ip6_reassdata));
377 ipr->timer = IPV6_REASS_MAXAGE;
378
379 /* enqueue the new structure to the front of the list */
380 ipr->next = reassdatagrams;
381 reassdatagrams = ipr;
382
383 /* Use the current IPv6 header for src/dest address reference.
384 * Eventually, we will replace it when we get the first fragment
385 * (it might be this one, in any case, it is done later). */
386 /* need to use the none-const pointer here: */
387 ipr->iphdr = ip_data.current_ip6_header;
388 #if IPV6_FRAG_COPYHEADER
389 MEMCPY(&ipr->src, &ip6_current_header()->src, sizeof(ipr->src));
390 MEMCPY(&ipr->dest, &ip6_current_header()->dest, sizeof(ipr->dest));
391 #endif /* IPV6_FRAG_COPYHEADER */
392 #if LWIP_IPV6_SCOPES
393 /* Also store the address zone information.
394 * @todo It is possible that due to netif destruction and recreation, the
395 * stored zones end up resolving to a different interface. In that case, we
396 * risk sending a "time exceeded" ICMP response over the wrong link.
397 * Ideally, netif destruction would clean up matching pending reassembly
398 * structures, but custom zone mappings would make that non-trivial. */
399 ipr->src_zone = ip6_addr_zone(ip6_current_src_addr());
400 ipr->dest_zone = ip6_addr_zone(ip6_current_dest_addr());
401 #endif /* LWIP_IPV6_SCOPES */
402 /* copy the fragmented packet id. */
403 ipr->identification = frag_hdr->_identification;
404
405 /* copy the nexth field */
406 ipr->nexth = frag_hdr->_nexth;
407 }
408
409 /* Check if we are allowed to enqueue more datagrams. */
410 if ((ip6_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
411 #if IP_REASS_FREE_OLDEST
412 ip6_reass_remove_oldest_datagram(ipr, clen);
413 if ((ip6_reass_pbufcount + clen) <= IP_REASS_MAX_PBUFS) {
414 /* re-search ipr_prev since it might have been removed */
415 for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
416 if (ipr_prev->next == ipr) {
417 break;
418 }
419 }
420 } else
421 #endif /* IP_REASS_FREE_OLDEST */
422 {
423 /* @todo: send ICMPv6 time exceeded here? */
424 /* drop this pbuf */
425 IP6_FRAG_STATS_INC(ip6_frag.memerr);
426 goto nullreturn;
427 }
428 }
429
430 /* Overwrite Fragment Header with our own helper struct. */
431 #if IPV6_FRAG_COPYHEADER
432 if (IPV6_FRAG_REQROOM > 0) {
433 /* Make room for struct ip6_reass_helper (only required if sizeof(void*) > 4).
434 This cannot fail since we already checked when receiving this fragment. */
435 u8_t hdrerr = pbuf_header_force(p, IPV6_FRAG_REQROOM);
436 LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
437 LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
438 }
439 #else /* IPV6_FRAG_COPYHEADER */
440 LWIP_ASSERT("sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN, set IPV6_FRAG_COPYHEADER to 1",
441 sizeof(struct ip6_reass_helper) <= IP6_FRAG_HLEN);
442 #endif /* IPV6_FRAG_COPYHEADER */
443
444 /* Prepare the pointer to the helper structure, and its initial values.
445 * Do not yet write to the structure itself, as we still have to make a
446 * backup of the original data, and we should not do that until we know for
447 * sure that we are going to add this packet to the list. */
448 iprh = (struct ip6_reass_helper *)p->payload;
449 next_pbuf = NULL;
450 end = (u16_t)(start + len);
451
452 /* find the right place to insert this pbuf */
453 /* Iterate through until we either get to the end of the list (append),
454 * or we find on with a larger offset (insert). */
455 for (q = ipr->p; q != NULL;) {
456 iprh_tmp = (struct ip6_reass_helper*)q->payload;
457 if (start < iprh_tmp->start) {
458 #if IP_REASS_CHECK_OVERLAP
459 if (end > iprh_tmp->start) {
460 /* fragment overlaps with following, throw away */
461 IP6_FRAG_STATS_INC(ip6_frag.proterr);
462 goto nullreturn;
463 }
464 if (iprh_prev != NULL) {
465 if (start < iprh_prev->end) {
466 /* fragment overlaps with previous, throw away */
467 IP6_FRAG_STATS_INC(ip6_frag.proterr);
468 goto nullreturn;
469 }
470 }
471 #endif /* IP_REASS_CHECK_OVERLAP */
472 /* Check if the fragments received so far have no gaps. */
473 if (iprh_prev != NULL) {
474 if (iprh_prev->end != start) {
475 /* There is a fragment missing between the current
476 * and the previous fragment */
477 valid = 0;
478 }
479 }
480 if (end != iprh_tmp->start) {
481 /* There is a fragment missing between the current
482 * and the following fragment */
483 valid = 0;
484 }
485 /* the new pbuf should be inserted before this */
486 next_pbuf = q;
487 if (iprh_prev != NULL) {
488 /* not the fragment with the lowest offset */
489 iprh_prev->next_pbuf = p;
490 } else {
491 /* fragment with the lowest offset */
492 ipr->p = p;
493 }
494 break;
495 } else if (start == iprh_tmp->start) {
496 /* received the same datagram twice: no need to keep the datagram */
497 goto nullreturn;
498 #if IP_REASS_CHECK_OVERLAP
499 } else if (start < iprh_tmp->end) {
500 /* overlap: no need to keep the new datagram */
501 IP6_FRAG_STATS_INC(ip6_frag.proterr);
502 goto nullreturn;
503 #endif /* IP_REASS_CHECK_OVERLAP */
504 } else {
505 /* Check if the fragments received so far have no gaps. */
506 if (iprh_prev != NULL) {
507 if (iprh_prev->end != iprh_tmp->start) {
508 /* There is a fragment missing between the current
509 * and the previous fragment */
510 valid = 0;
511 }
512 }
513 }
514 q = iprh_tmp->next_pbuf;
515 iprh_prev = iprh_tmp;
516 }
517
518 /* If q is NULL, then we made it to the end of the list. Determine what to do now */
519 if (q == NULL) {
520 if (iprh_prev != NULL) {
521 /* this is (for now), the fragment with the highest offset:
522 * chain it to the last fragment */
523 #if IP_REASS_CHECK_OVERLAP
524 LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= start);
525 #endif /* IP_REASS_CHECK_OVERLAP */
526 iprh_prev->next_pbuf = p;
527 if (iprh_prev->end != start) {
528 valid = 0;
529 }
530 } else {
531 #if IP_REASS_CHECK_OVERLAP
532 LWIP_ASSERT("no previous fragment, this must be the first fragment!",
533 ipr->p == NULL);
534 #endif /* IP_REASS_CHECK_OVERLAP */
535 /* this is the first fragment we ever received for this ip datagram */
536 ipr->p = p;
537 }
538 }
539
540 /* Track the current number of pbufs current 'in-flight', in order to limit
541 the number of fragments that may be enqueued at any one time */
542 ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount + clen);
543
544 /* Remember IPv6 header if this is the first fragment. */
545 if (start == 0) {
546 /* need to use the none-const pointer here: */
547 ipr->iphdr = ip_data.current_ip6_header;
548 /* Make a backup of the part of the packet data that we are about to
549 * overwrite, so that we can restore the original later. */
550 MEMCPY(ipr->orig_hdr, p->payload, sizeof(*iprh));
551 /* For IPV6_FRAG_COPYHEADER there is no need to copy src/dst again, as they
552 * will be the same as they were. With LWIP_IPV6_SCOPES, the same applies
553 * to the source/destination zones. */
554 }
555 /* Only after the backup do we get to fill in the actual helper structure. */
556 iprh->next_pbuf = next_pbuf;
557 iprh->start = start;
558 iprh->end = end;
559
560 /* If this is the last fragment, calculate total packet length. */
561 if ((offset & IP6_FRAG_MORE_FLAG) == 0) {
562 ipr->datagram_len = iprh->end;
563 }
564
565 /* Additional validity tests: we have received first and last fragment. */
566 iprh_tmp = (struct ip6_reass_helper*)ipr->p->payload;
567 if (iprh_tmp->start != 0) {
568 valid = 0;
569 }
570 if (ipr->datagram_len == 0) {
571 valid = 0;
572 }
573
574 /* Final validity test: no gaps between current and last fragment. */
575 iprh_prev = iprh;
576 q = iprh->next_pbuf;
577 while ((q != NULL) && valid) {
578 iprh = (struct ip6_reass_helper*)q->payload;
579 if (iprh_prev->end != iprh->start) {
580 valid = 0;
581 break;
582 }
583 iprh_prev = iprh;
584 q = iprh->next_pbuf;
585 }
586
587 if (valid) {
588 /* All fragments have been received */
589 struct ip6_hdr* iphdr_ptr;
590
591 /* chain together the pbufs contained within the ip6_reassdata list. */
592 iprh = (struct ip6_reass_helper*) ipr->p->payload;
593 while (iprh != NULL) {
594 next_pbuf = iprh->next_pbuf;
595 if (next_pbuf != NULL) {
596 /* Save next helper struct (will be hidden in next step). */
597 iprh_tmp = (struct ip6_reass_helper*)next_pbuf->payload;
598
599 /* hide the fragment header for every succeeding fragment */
600 pbuf_remove_header(next_pbuf, IP6_FRAG_HLEN);
601 #if IPV6_FRAG_COPYHEADER
602 if (IPV6_FRAG_REQROOM > 0) {
603 /* hide the extra bytes borrowed from ip6_hdr for struct ip6_reass_helper */
604 u8_t hdrerr = pbuf_remove_header(next_pbuf, IPV6_FRAG_REQROOM);
605 LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
606 LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
607 }
608 #endif
609 pbuf_cat(ipr->p, next_pbuf);
610 }
611 else {
612 iprh_tmp = NULL;
613 }
614
615 iprh = iprh_tmp;
616 }
617
618 /* Get the first pbuf. */
619 p = ipr->p;
620
621 #if IPV6_FRAG_COPYHEADER
622 if (IPV6_FRAG_REQROOM > 0) {
623 u8_t hdrerr;
624 /* Restore (only) the bytes that we overwrote beyond the fragment header.
625 * Those bytes may belong to either the IPv6 header or an extension
626 * header placed before the fragment header. */
627 MEMCPY(p->payload, ipr->orig_hdr, IPV6_FRAG_REQROOM);
628 /* get back room for struct ip6_reass_helper (only required if sizeof(void*) > 4) */
629 hdrerr = pbuf_remove_header(p, IPV6_FRAG_REQROOM);
630 LWIP_UNUSED_ARG(hdrerr); /* in case of LWIP_NOASSERT */
631 LWIP_ASSERT("no room for struct ip6_reass_helper", hdrerr == 0);
632 }
633 #endif
634
635 /* We need to get rid of the fragment header itself, which is somewhere in
636 * the middle of the packet (but still in the first pbuf of the chain).
637 * Getting rid of the header is required by RFC 2460 Sec. 4.5 and necessary
638 * in order to be able to reassemble packets that are close to full size
639 * (i.e., around 65535 bytes). We simply move up all the headers before the
640 * fragment header, including the IPv6 header, and adjust the payload start
641 * accordingly. This works because all these headers are in the first pbuf
642 * of the chain, and because the caller adjusts all its pointers on
643 * successful reassembly. */
644 MEMMOVE((u8_t*)ipr->iphdr + sizeof(struct ip6_frag_hdr), ipr->iphdr,
645 (size_t)((u8_t*)p->payload - (u8_t*)ipr->iphdr));
646
647 /* This is where the IPv6 header is now. */
648 iphdr_ptr = (struct ip6_hdr*)((u8_t*)ipr->iphdr +
649 sizeof(struct ip6_frag_hdr));
650
651 /* Adjust datagram length by adding header lengths. */
652 ipr->datagram_len = (u16_t)(ipr->datagram_len + ((u8_t*)p->payload - (u8_t*)iphdr_ptr)
653 - IP6_HLEN);
654
655 /* Set payload length in ip header. */
656 iphdr_ptr->_plen = lwip_htons(ipr->datagram_len);
657
658 /* With the fragment header gone, we now need to adjust the next-header
659 * field of whatever header was originally before it. Since the packet made
660 * it through the original header processing routines at least up to the
661 * fragment header, we do not need any further sanity checks here. */
662 if (IP6H_NEXTH(iphdr_ptr) == IP6_NEXTH_FRAGMENT) {
663 iphdr_ptr->_nexth = ipr->nexth;
664 } else {
665 u8_t *ptr = (u8_t *)iphdr_ptr + IP6_HLEN;
666 while (*ptr != IP6_NEXTH_FRAGMENT) {
667 ptr += 8 * (1 + ptr[1]);
668 }
669 *ptr = ipr->nexth;
670 }
671
672 /* release the resources allocated for the fragment queue entry */
673 if (reassdatagrams == ipr) {
674 /* it was the first in the list */
675 reassdatagrams = ipr->next;
676 } else {
677 /* it wasn't the first, so it must have a valid 'prev' */
678 LWIP_ASSERT("sanity check linked list", ipr_prev != NULL);
679 ipr_prev->next = ipr->next;
680 }
681 memp_free(MEMP_IP6_REASSDATA, ipr);
682
683 /* adjust the number of pbufs currently queued for reassembly. */
684 clen = pbuf_clen(p);
685 LWIP_ASSERT("ip6_reass_pbufcount >= clen", ip6_reass_pbufcount >= clen);
686 ip6_reass_pbufcount = (u16_t)(ip6_reass_pbufcount - clen);
687
688 /* Move pbuf back to IPv6 header. This should never fail. */
689 if (pbuf_header_force(p, (s16_t)((u8_t*)p->payload - (u8_t*)iphdr_ptr))) {
690 LWIP_ASSERT("ip6_reass: moving p->payload to ip6 header failed", 0);
691 pbuf_free(p);
692 return NULL;
693 }
694
695 /* Return the pbuf chain */
696 MIB2_STATS_INC(mib2.ip6reasmoks);
697 return p;
698 }
699 /* the datagram is not (yet?) reassembled completely */
700 return NULL;
701
702 nullreturn:
703 IP6_FRAG_STATS_INC(ip6_frag.drop);
704 pbuf_free(p);
705 return NULL;
706 }
707
708 #endif /* LWIP_IPV6 && LWIP_IPV6_REASS */
709
710 #if LWIP_IPV6 && LWIP_IPV6_FRAG
711
712 #if !LWIP_NETIF_TX_SINGLE_PBUF
713 /** Allocate a new struct pbuf_custom_ref */
714 static struct pbuf_custom_ref*
ip6_frag_alloc_pbuf_custom_ref(void)715 ip6_frag_alloc_pbuf_custom_ref(void)
716 {
717 return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
718 }
719
720 /** Free a struct pbuf_custom_ref */
721 static void
ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref * p)722 ip6_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
723 {
724 LWIP_ASSERT("p != NULL", p != NULL);
725 memp_free(MEMP_FRAG_PBUF, p);
726 }
727
728 /** Free-callback function to free a 'struct pbuf_custom_ref', called by
729 * pbuf_free. */
730 static void
ip6_frag_free_pbuf_custom(struct pbuf * p)731 ip6_frag_free_pbuf_custom(struct pbuf *p)
732 {
733 struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
734 LWIP_ASSERT("pcr != NULL", pcr != NULL);
735 LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
736 if (pcr->original != NULL) {
737 pbuf_free(pcr->original);
738 }
739 ip6_frag_free_pbuf_custom_ref(pcr);
740 }
741 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
742
743 /**
744 * Fragment an IPv6 datagram if too large for the netif or path MTU.
745 *
746 * Chop the datagram in MTU sized chunks and send them in order
747 * by pointing PBUF_REFs into p
748 *
749 * @param p ipv6 packet to send
750 * @param netif the netif on which to send
751 * @param dest destination ipv6 address to which to send
752 *
753 * @return ERR_OK if sent successfully, err_t otherwise
754 */
755 err_t
ip6_frag(struct pbuf * p,struct netif * netif,const ip6_addr_t * dest)756 ip6_frag(struct pbuf *p, struct netif *netif, const ip6_addr_t *dest)
757 {
758 struct ip6_hdr *original_ip6hdr;
759 struct ip6_hdr *ip6hdr;
760 struct ip6_frag_hdr *frag_hdr;
761 struct pbuf *rambuf;
762 #if !LWIP_NETIF_TX_SINGLE_PBUF
763 struct pbuf *newpbuf;
764 u16_t newpbuflen = 0;
765 u16_t left_to_copy;
766 #endif
767 static u32_t identification;
768 u16_t left, cop;
769 const u16_t mtu = nd6_get_destination_mtu(dest, netif);
770 const u16_t nfb = (u16_t)((mtu - (IP6_HLEN + IP6_FRAG_HLEN)) & IP6_FRAG_OFFSET_MASK);
771 u16_t fragment_offset = 0;
772 u16_t last;
773 u16_t poff = IP6_HLEN;
774
775 identification++;
776
777 original_ip6hdr = (struct ip6_hdr *)p->payload;
778
779 /* @todo we assume there are no options in the unfragmentable part (IPv6 header). */
780 LWIP_ASSERT("p->tot_len >= IP6_HLEN", p->tot_len >= IP6_HLEN);
781 left = (u16_t)(p->tot_len - IP6_HLEN);
782
783 while (left) {
784 last = (left <= nfb);
785
786 /* Fill this fragment */
787 cop = last ? left : nfb;
788
789 #if LWIP_NETIF_TX_SINGLE_PBUF
790 rambuf = pbuf_alloc(PBUF_IP, cop + IP6_FRAG_HLEN, PBUF_RAM);
791 if (rambuf == NULL) {
792 IP6_FRAG_STATS_INC(ip6_frag.memerr);
793 return ERR_MEM;
794 }
795 LWIP_ASSERT("this needs a pbuf in one piece!",
796 (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
797 poff += pbuf_copy_partial(p, (u8_t*)rambuf->payload + IP6_FRAG_HLEN, cop, poff);
798 /* make room for the IP header */
799 if (pbuf_add_header(rambuf, IP6_HLEN)) {
800 pbuf_free(rambuf);
801 IP6_FRAG_STATS_INC(ip6_frag.memerr);
802 return ERR_MEM;
803 }
804 /* fill in the IP header */
805 SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
806 ip6hdr = (struct ip6_hdr *)rambuf->payload;
807 frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);
808 #else
809 /* When not using a static buffer, create a chain of pbufs.
810 * The first will be a PBUF_RAM holding the link, IPv6, and Fragment header.
811 * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
812 * but limited to the size of an mtu.
813 */
814 rambuf = pbuf_alloc(PBUF_LINK, IP6_HLEN + IP6_FRAG_HLEN, PBUF_RAM);
815 if (rambuf == NULL) {
816 IP6_FRAG_STATS_INC(ip6_frag.memerr);
817 return ERR_MEM;
818 }
819 LWIP_ASSERT("this needs a pbuf in one piece!",
820 (rambuf->len >= (IP6_HLEN)));
821 SMEMCPY(rambuf->payload, original_ip6hdr, IP6_HLEN);
822 ip6hdr = (struct ip6_hdr *)rambuf->payload;
823 frag_hdr = (struct ip6_frag_hdr *)((u8_t*)rambuf->payload + IP6_HLEN);
824
825 /* Can just adjust p directly for needed offset. */
826 p->payload = (u8_t *)p->payload + poff;
827 p->len = (u16_t)(p->len - poff);
828 p->tot_len = (u16_t)(p->tot_len - poff);
829
830 left_to_copy = cop;
831 while (left_to_copy) {
832 struct pbuf_custom_ref *pcr;
833 newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
834 /* Is this pbuf already empty? */
835 if (!newpbuflen) {
836 p = p->next;
837 continue;
838 }
839 pcr = ip6_frag_alloc_pbuf_custom_ref();
840 if (pcr == NULL) {
841 pbuf_free(rambuf);
842 IP6_FRAG_STATS_INC(ip6_frag.memerr);
843 return ERR_MEM;
844 }
845 /* Mirror this pbuf, although we might not need all of it. */
846 newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
847 if (newpbuf == NULL) {
848 ip6_frag_free_pbuf_custom_ref(pcr);
849 pbuf_free(rambuf);
850 IP6_FRAG_STATS_INC(ip6_frag.memerr);
851 return ERR_MEM;
852 }
853 pbuf_ref(p);
854 pcr->original = p;
855 pcr->pc.custom_free_function = ip6_frag_free_pbuf_custom;
856
857 /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
858 * so that it is removed when pbuf_dechain is later called on rambuf.
859 */
860 pbuf_cat(rambuf, newpbuf);
861 left_to_copy = (u16_t)(left_to_copy - newpbuflen);
862 if (left_to_copy) {
863 p = p->next;
864 }
865 }
866 poff = newpbuflen;
867 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
868
869 /* Set headers */
870 frag_hdr->_nexth = original_ip6hdr->_nexth;
871 frag_hdr->reserved = 0;
872 frag_hdr->_fragment_offset = lwip_htons((u16_t)((fragment_offset & IP6_FRAG_OFFSET_MASK) | (last ? 0 : IP6_FRAG_MORE_FLAG)));
873 frag_hdr->_identification = lwip_htonl(identification);
874
875 IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
876 IP6H_PLEN_SET(ip6hdr, (u16_t)(cop + IP6_FRAG_HLEN));
877
878 /* No need for separate header pbuf - we allowed room for it in rambuf
879 * when allocated.
880 */
881 IP6_FRAG_STATS_INC(ip6_frag.xmit);
882 netif->output_ip6(netif, rambuf, dest);
883
884 /* Unfortunately we can't reuse rambuf - the hardware may still be
885 * using the buffer. Instead we free it (and the ensuing chain) and
886 * recreate it next time round the loop. If we're lucky the hardware
887 * will have already sent the packet, the free will really free, and
888 * there will be zero memory penalty.
889 */
890
891 pbuf_free(rambuf);
892 left = (u16_t)(left - cop);
893 fragment_offset = (u16_t)(fragment_offset + cop);
894 }
895 return ERR_OK;
896 }
897
898 #endif /* LWIP_IPV6 && LWIP_IPV6_FRAG */
899