UbixOS V2 2.0

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