The Pedigree Project  0.1
pbuf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
99 /*
100  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
101  * All rights reserved.
102  *
103  * Redistribution and use in source and binary forms, with or without modification,
104  * are permitted provided that the following conditions are met:
105  *
106  * 1. Redistributions of source code must retain the above copyright notice,
107  * this list of conditions and the following disclaimer.
108  * 2. Redistributions in binary form must reproduce the above copyright notice,
109  * this list of conditions and the following disclaimer in the documentation
110  * and/or other materials provided with the distribution.
111  * 3. The name of the author may not be used to endorse or promote products
112  * derived from this software without specific prior written permission.
113  *
114  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
115  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
116  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
117  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
118  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
119  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
120  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
121  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
122  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
123  * OF SUCH DAMAGE.
124  *
125  * This file is part of the lwIP TCP/IP stack.
126  *
127  * Author: Adam Dunkels <adam@sics.se>
128  *
129  */
130 
131 #include "lwip/opt.h"
132 
133 #include "lwip/stats.h"
134 #include "lwip/def.h"
135 #include "lwip/mem.h"
136 #include "lwip/memp.h"
137 #include "lwip/pbuf.h"
138 #include "lwip/sys.h"
139 #if LWIP_TCP && TCP_QUEUE_OOSEQ
140 #include "lwip/priv/tcp_priv.h"
141 #endif
142 #if LWIP_CHECKSUM_ON_COPY
143 #include "lwip/inet_chksum.h"
144 #endif
145 
146 #include <string.h>
147 
148 #define SIZEOF_STRUCT_PBUF LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
149 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
150  aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
151 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
152 
153 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ
154 #define PBUF_POOL_IS_EMPTY()
155 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */
156 
157 #if !NO_SYS
158 #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
159 #include "lwip/tcpip.h"
160 #define PBUF_POOL_FREE_OOSEQ_QUEUE_CALL() do { \
161  if (tcpip_callback_with_block(pbuf_free_ooseq_callback, NULL, 0) != ERR_OK) { \
162  SYS_ARCH_PROTECT(old_level); \
163  pbuf_free_ooseq_pending = 0; \
164  SYS_ARCH_UNPROTECT(old_level); \
165  } } while(0)
166 #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
167 #endif /* !NO_SYS */
168 
169 volatile u8_t pbuf_free_ooseq_pending;
170 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
171 
180 #if !NO_SYS
181 static
182 #endif /* !NO_SYS */
183 void
184 pbuf_free_ooseq(void)
185 {
186  struct tcp_pcb* pcb;
187  SYS_ARCH_SET(pbuf_free_ooseq_pending, 0);
188 
189  for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
190  if (NULL != pcb->ooseq) {
192  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
193  tcp_segs_free(pcb->ooseq);
194  pcb->ooseq = NULL;
195  return;
196  }
197  }
198 }
199 
200 #if !NO_SYS
201 
204 static void
205 pbuf_free_ooseq_callback(void *arg)
206 {
207  LWIP_UNUSED_ARG(arg);
208  pbuf_free_ooseq();
209 }
210 #endif /* !NO_SYS */
211 
213 static void
214 pbuf_pool_is_empty(void)
215 {
216 #ifndef PBUF_POOL_FREE_OOSEQ_QUEUE_CALL
217  SYS_ARCH_SET(pbuf_free_ooseq_pending, 1);
218 #else /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
219  u8_t queued;
220  SYS_ARCH_DECL_PROTECT(old_level);
221  SYS_ARCH_PROTECT(old_level);
222  queued = pbuf_free_ooseq_pending;
223  pbuf_free_ooseq_pending = 1;
224  SYS_ARCH_UNPROTECT(old_level);
225 
226  if (!queued) {
227  /* queue a call to pbuf_free_ooseq if not already queued */
228  PBUF_POOL_FREE_OOSEQ_QUEUE_CALL();
229  }
230 #endif /* PBUF_POOL_FREE_OOSEQ_QUEUE_CALL */
231 }
232 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || !PBUF_POOL_FREE_OOSEQ */
233 
266 struct pbuf *
267 pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
268 {
269  struct pbuf *p, *q, *r;
270  u16_t offset;
271  s32_t rem_len; /* remaining length */
272  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
273 
274  /* determine header offset */
275  switch (layer) {
276  case PBUF_TRANSPORT:
277  /* add room for transport (often TCP) layer header */
278  offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN;
279  break;
280  case PBUF_IP:
281  /* add room for IP layer header */
282  offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN;
283  break;
284  case PBUF_LINK:
285  /* add room for link layer header */
287  break;
288  case PBUF_RAW_TX:
289  /* add room for encapsulating link layer headers (e.g. 802.11) */
291  break;
292  case PBUF_RAW:
293  /* no offset (e.g. RX buffers or chain successors) */
294  offset = 0;
295  break;
296  default:
297  LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
298  return NULL;
299  }
300 
301  switch (type) {
302  case PBUF_POOL:
303  /* allocate head of pbuf chain into p */
304  p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
305  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
306  if (p == NULL) {
307  PBUF_POOL_IS_EMPTY();
308  return NULL;
309  }
310  p->type = type;
311  p->next = NULL;
312 
313  /* make the payload pointer point 'offset' bytes into pbuf data memory */
314  p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
315  LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
316  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
317  /* the total length of the pbuf chain is the requested size */
318  p->tot_len = length;
319  /* set the length of the first pbuf in the chain */
320  p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset));
321  LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
322  ((u8_t*)p->payload + p->len <=
323  (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
324  LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
325  (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
326  /* set reference count (needed here in case we fail) */
327  p->ref = 1;
328 
329  /* now allocate the tail of the pbuf chain */
330 
331  /* remember first pbuf for linkage in next iteration */
332  r = p;
333  /* remaining length to be allocated */
334  rem_len = length - p->len;
335  /* any remaining pbufs to be allocated? */
336  while (rem_len > 0) {
337  q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
338  if (q == NULL) {
339  PBUF_POOL_IS_EMPTY();
340  /* free chain so far allocated */
341  pbuf_free(p);
342  /* bail out unsuccessfully */
343  return NULL;
344  }
345  q->type = type;
346  q->flags = 0;
347  q->next = NULL;
348  /* make previous pbuf point to this pbuf */
349  r->next = q;
350  /* set total length of this pbuf and next in chain */
351  LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
352  q->tot_len = (u16_t)rem_len;
353  /* this pbuf length is pool size, unless smaller sized tail */
354  q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);
355  q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
356  LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
357  ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
358  LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
359  ((u8_t*)p->payload + p->len <=
360  (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
361  q->ref = 1;
362  /* calculate remaining length to be allocated */
363  rem_len -= q->len;
364  /* remember this pbuf for linkage in next iteration */
365  r = q;
366  }
367  /* end of chain */
368  /*r->next = NULL;*/
369 
370  break;
371  case PBUF_RAM:
372  {
373  mem_size_t alloc_len = LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length);
374 
375  /* bug #50040: Check for integer overflow when calculating alloc_len */
376  if (alloc_len < LWIP_MEM_ALIGN_SIZE(length)) {
377  return NULL;
378  }
379 
380  /* If pbuf is to be allocated in RAM, allocate memory for it. */
381  p = (struct pbuf*)mem_malloc(alloc_len);
382  }
383 
384  if (p == NULL) {
385  return NULL;
386  }
387  /* Set up internal structure of the pbuf. */
388  p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
389  p->len = p->tot_len = length;
390  p->next = NULL;
391  p->type = type;
392 
393  LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
394  ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
395  break;
396  /* pbuf references existing (non-volatile static constant) ROM payload? */
397  case PBUF_ROM:
398  /* pbuf references existing (externally allocated) RAM payload? */
399  case PBUF_REF:
400  /* only allocate memory for the pbuf structure */
401  p = (struct pbuf *)memp_malloc(MEMP_PBUF);
402  if (p == NULL) {
404  ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
405  (type == PBUF_ROM) ? "ROM" : "REF"));
406  return NULL;
407  }
408  /* caller must set this field properly, afterwards */
409  p->payload = NULL;
410  p->len = p->tot_len = length;
411  p->next = NULL;
412  p->type = type;
413  break;
414  default:
415  LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
416  return NULL;
417  }
418  /* set reference count */
419  p->ref = 1;
420  /* set flags */
421  p->flags = 0;
422  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
423  return p;
424 }
425 
426 #if LWIP_SUPPORT_CUSTOM_PBUF
427 
443 struct pbuf*
444 pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
445  void *payload_mem, u16_t payload_mem_len)
446 {
447  u16_t offset;
448  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
449 
450  /* determine header offset */
451  switch (l) {
452  case PBUF_TRANSPORT:
453  /* add room for transport (often TCP) layer header */
454  offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN;
455  break;
456  case PBUF_IP:
457  /* add room for IP layer header */
458  offset = PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN;
459  break;
460  case PBUF_LINK:
461  /* add room for link layer header */
463  break;
464  case PBUF_RAW_TX:
465  /* add room for encapsulating link layer headers (e.g. 802.11) */
467  break;
468  case PBUF_RAW:
469  offset = 0;
470  break;
471  default:
472  LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
473  return NULL;
474  }
475 
476  if (LWIP_MEM_ALIGN_SIZE(offset) + length > payload_mem_len) {
477  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
478  return NULL;
479  }
480 
481  p->pbuf.next = NULL;
482  if (payload_mem != NULL) {
483  p->pbuf.payload = (u8_t *)payload_mem + LWIP_MEM_ALIGN_SIZE(offset);
484  } else {
485  p->pbuf.payload = NULL;
486  }
487  p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
488  p->pbuf.len = p->pbuf.tot_len = length;
489  p->pbuf.type = type;
490  p->pbuf.ref = 1;
491  return &p->pbuf;
492 }
493 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
494 
511 void
512 pbuf_realloc(struct pbuf *p, u16_t new_len)
513 {
514  struct pbuf *q;
515  u16_t rem_len; /* remaining length */
516  s32_t grow;
517 
518  LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
519  LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
520  p->type == PBUF_ROM ||
521  p->type == PBUF_RAM ||
522  p->type == PBUF_REF);
523 
524  /* desired length larger than current length? */
525  if (new_len >= p->tot_len) {
526  /* enlarging not yet supported */
527  return;
528  }
529 
530  /* the pbuf chain grows by (new_len - p->tot_len) bytes
531  * (which may be negative in case of shrinking) */
532  grow = new_len - p->tot_len;
533 
534  /* first, step over any pbufs that should remain in the chain */
535  rem_len = new_len;
536  q = p;
537  /* should this pbuf be kept? */
538  while (rem_len > q->len) {
539  /* decrease remaining length by pbuf length */
540  rem_len -= q->len;
541  /* decrease total length indicator */
542  LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
543  q->tot_len += (u16_t)grow;
544  /* proceed to next pbuf in chain */
545  q = q->next;
546  LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
547  }
548  /* we have now reached the new last pbuf (in q) */
549  /* rem_len == desired length for pbuf q */
550 
551  /* shrink allocated memory for PBUF_RAM */
552  /* (other types merely adjust their length fields */
553  if ((q->type == PBUF_RAM) && (rem_len != q->len)
555  && ((q->flags & PBUF_FLAG_IS_CUSTOM) == 0)
556 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
557  ) {
558  /* reallocate and adjust the length of the pbuf that will be split */
559  q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
560  LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
561  }
562  /* adjust length fields for new last pbuf */
563  q->len = rem_len;
564  q->tot_len = q->len;
565 
566  /* any remaining pbufs in chain? */
567  if (q->next != NULL) {
568  /* free remaining pbufs in chain */
569  pbuf_free(q->next);
570  }
571  /* q is last packet in chain */
572  q->next = NULL;
573 
574 }
575 
587 static u8_t
588 pbuf_header_impl(struct pbuf *p, s16_t header_size_increment, u8_t force)
589 {
590  u16_t type;
591  void *payload;
592  u16_t increment_magnitude;
593 
594  LWIP_ASSERT("p != NULL", p != NULL);
595  if ((header_size_increment == 0) || (p == NULL)) {
596  return 0;
597  }
598 
599  if (header_size_increment < 0) {
600  increment_magnitude = (u16_t)-header_size_increment;
601  /* Check that we aren't going to move off the end of the pbuf */
602  LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
603  } else {
604  increment_magnitude = (u16_t)header_size_increment;
605 #if 0
606  /* Can't assert these as some callers speculatively call
607  pbuf_header() to see if it's OK. Will return 1 below instead. */
608  /* Check that we've got the correct type of pbuf to work with */
609  LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL",
610  p->type == PBUF_RAM || p->type == PBUF_POOL);
611  /* Check that we aren't going to move off the beginning of the pbuf */
612  LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
613  (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
614 #endif
615  }
616 
617  type = p->type;
618  /* remember current payload pointer */
619  payload = p->payload;
620 
621  /* pbuf types containing payloads? */
622  if (type == PBUF_RAM || type == PBUF_POOL) {
623  /* set new payload pointer */
624  p->payload = (u8_t *)p->payload - header_size_increment;
625  /* boundary check fails? */
626  if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
628  ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
629  (void *)p->payload, (void *)((u8_t *)p + SIZEOF_STRUCT_PBUF)));
630  /* restore old payload pointer */
631  p->payload = payload;
632  /* bail out unsuccessfully */
633  return 1;
634  }
635  /* pbuf types referring to external payloads? */
636  } else if (type == PBUF_REF || type == PBUF_ROM) {
637  /* hide a header in the payload? */
638  if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
639  /* increase payload pointer */
640  p->payload = (u8_t *)p->payload - header_size_increment;
641  } else if ((header_size_increment > 0) && force) {
642  p->payload = (u8_t *)p->payload - header_size_increment;
643  } else {
644  /* cannot expand payload to front (yet!)
645  * bail out unsuccessfully */
646  return 1;
647  }
648  } else {
649  /* Unknown type */
650  LWIP_ASSERT("bad pbuf type", 0);
651  return 1;
652  }
653  /* modify pbuf length fields */
654  p->len += header_size_increment;
655  p->tot_len += header_size_increment;
656 
657  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
658  (void *)payload, (void *)p->payload, header_size_increment));
659 
660  return 0;
661 }
662 
683 u8_t
684 pbuf_header(struct pbuf *p, s16_t header_size_increment)
685 {
686  return pbuf_header_impl(p, header_size_increment, 0);
687 }
688 
693 u8_t
694 pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
695 {
696  return pbuf_header_impl(p, header_size_increment, 1);
697 }
698 
733 u8_t
734 pbuf_free(struct pbuf *p)
735 {
736  u16_t type;
737  struct pbuf *q;
738  u8_t count;
739 
740  if (p == NULL) {
741  LWIP_ASSERT("p != NULL", p != NULL);
742  /* if assertions are disabled, proceed with debug output */
744  ("pbuf_free(p == NULL) was called.\n"));
745  return 0;
746  }
747  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
748 
749  PERF_START;
750 
751  LWIP_ASSERT("pbuf_free: sane type",
752  p->type == PBUF_RAM || p->type == PBUF_ROM ||
753  p->type == PBUF_REF || p->type == PBUF_POOL);
754 
755  count = 0;
756  /* de-allocate all consecutive pbufs from the head of the chain that
757  * obtain a zero reference count after decrementing*/
758  while (p != NULL) {
759  u16_t ref;
760  SYS_ARCH_DECL_PROTECT(old_level);
761  /* Since decrementing ref cannot be guaranteed to be a single machine operation
762  * we must protect it. We put the new ref into a local variable to prevent
763  * further protection. */
764  SYS_ARCH_PROTECT(old_level);
765  /* all pbufs in a chain are referenced at least once */
766  LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
767  /* decrease reference count (number of pointers to pbuf) */
768  ref = --(p->ref);
769  SYS_ARCH_UNPROTECT(old_level);
770  /* this pbuf is no longer referenced to? */
771  if (ref == 0) {
772  /* remember next pbuf in chain for next iteration */
773  q = p->next;
774  LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
775  type = p->type;
776 #if LWIP_SUPPORT_CUSTOM_PBUF
777  /* is this a custom pbuf? */
778  if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
779  struct pbuf_custom *pc = (struct pbuf_custom*)p;
780  LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
781  pc->custom_free_function(p);
782  } else
783 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
784  {
785  /* is this a pbuf from the pool? */
786  if (type == PBUF_POOL) {
787  memp_free(MEMP_PBUF_POOL, p);
788  /* is this a ROM or RAM referencing pbuf? */
789  } else if (type == PBUF_ROM || type == PBUF_REF) {
790  memp_free(MEMP_PBUF, p);
791  /* type == PBUF_RAM */
792  } else {
793  mem_free(p);
794  }
795  }
796  count++;
797  /* proceed to next pbuf */
798  p = q;
799  /* p->ref > 0, this pbuf is still referenced to */
800  /* (and so the remaining pbufs in chain as well) */
801  } else {
802  LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
803  /* stop walking through the chain */
804  p = NULL;
805  }
806  }
807  PERF_STOP("pbuf_free");
808  /* return number of de-allocated pbufs */
809  return count;
810 }
811 
818 u16_t
819 pbuf_clen(const struct pbuf *p)
820 {
821  u16_t len;
822 
823  len = 0;
824  while (p != NULL) {
825  ++len;
826  p = p->next;
827  }
828  return len;
829 }
830 
838 void
839 pbuf_ref(struct pbuf *p)
840 {
841  /* pbuf given? */
842  if (p != NULL) {
843  SYS_ARCH_INC(p->ref, 1);
844  LWIP_ASSERT("pbuf ref overflow", p->ref > 0);
845  }
846 }
847 
858 void
859 pbuf_cat(struct pbuf *h, struct pbuf *t)
860 {
861  struct pbuf *p;
862 
863  LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
864  ((h != NULL) && (t != NULL)), return;);
865 
866  /* proceed to last pbuf of chain */
867  for (p = h; p->next != NULL; p = p->next) {
868  /* add total length of second chain to all totals of first chain */
869  p->tot_len += t->tot_len;
870  }
871  /* { p is last pbuf of first h chain, p->next == NULL } */
872  LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
873  LWIP_ASSERT("p->next == NULL", p->next == NULL);
874  /* add total length of second chain to last pbuf total of first chain */
875  p->tot_len += t->tot_len;
876  /* chain last pbuf of head (p) with first of tail (t) */
877  p->next = t;
878  /* p->next now references t, but the caller will drop its reference to t,
879  * so netto there is no change to the reference count of t.
880  */
881 }
882 
900 void
901 pbuf_chain(struct pbuf *h, struct pbuf *t)
902 {
903  pbuf_cat(h, t);
904  /* t is now referenced by h */
905  pbuf_ref(t);
906  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
907 }
908 
917 struct pbuf *
918 pbuf_dechain(struct pbuf *p)
919 {
920  struct pbuf *q;
921  u8_t tail_gone = 1;
922  /* tail */
923  q = p->next;
924  /* pbuf has successor in chain? */
925  if (q != NULL) {
926  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
927  LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
928  /* enforce invariant if assertion is disabled */
929  q->tot_len = p->tot_len - p->len;
930  /* decouple pbuf from remainder */
931  p->next = NULL;
932  /* total length of pbuf p is its own length only */
933  p->tot_len = p->len;
934  /* q is no longer referenced by p, free it */
935  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
936  tail_gone = pbuf_free(q);
937  if (tail_gone > 0) {
939  ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
940  }
941  /* return remaining tail or NULL if deallocated */
942  }
943  /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
944  LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
945  return ((tail_gone > 0) ? NULL : q);
946 }
947 
966 err_t
967 pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
968 {
969  u16_t offset_to=0, offset_from=0, len;
970 
971  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
972  (const void*)p_to, (const void*)p_from));
973 
974  /* is the target big enough to hold the source? */
975  LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
976  (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
977 
978  /* iterate through pbuf chain */
979  do
980  {
981  /* copy one part of the original chain */
982  if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
983  /* complete current p_from fits into current p_to */
984  len = p_from->len - offset_from;
985  } else {
986  /* current p_from does not fit into current p_to */
987  len = p_to->len - offset_to;
988  }
989  MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
990  offset_to += len;
991  offset_from += len;
992  LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
993  LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
994  if (offset_from >= p_from->len) {
995  /* on to next p_from (if any) */
996  offset_from = 0;
997  p_from = p_from->next;
998  }
999  if (offset_to == p_to->len) {
1000  /* on to next p_to (if any) */
1001  offset_to = 0;
1002  p_to = p_to->next;
1003  LWIP_ERROR("p_to != NULL", (p_to != NULL) || (p_from == NULL) , return ERR_ARG;);
1004  }
1005 
1006  if ((p_from != NULL) && (p_from->len == p_from->tot_len)) {
1007  /* don't copy more than one packet! */
1008  LWIP_ERROR("pbuf_copy() does not allow packet queues!",
1009  (p_from->next == NULL), return ERR_VAL;);
1010  }
1011  if ((p_to != NULL) && (p_to->len == p_to->tot_len)) {
1012  /* don't copy more than one packet! */
1013  LWIP_ERROR("pbuf_copy() does not allow packet queues!",
1014  (p_to->next == NULL), return ERR_VAL;);
1015  }
1016  } while (p_from);
1017  LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
1018  return ERR_OK;
1019 }
1020 
1033 u16_t
1034 pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
1035 {
1036  const struct pbuf *p;
1037  u16_t left;
1038  u16_t buf_copy_len;
1039  u16_t copied_total = 0;
1040 
1041  LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
1042  LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
1043 
1044  left = 0;
1045 
1046  if ((buf == NULL) || (dataptr == NULL)) {
1047  return 0;
1048  }
1049 
1050  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
1051  for (p = buf; len != 0 && p != NULL; p = p->next) {
1052  if ((offset != 0) && (offset >= p->len)) {
1053  /* don't copy from this buffer -> on to the next */
1054  offset -= p->len;
1055  } else {
1056  /* copy from this buffer. maybe only partially. */
1057  buf_copy_len = p->len - offset;
1058  if (buf_copy_len > len) {
1059  buf_copy_len = len;
1060  }
1061  /* copy the necessary parts of the buffer */
1062  MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
1063  copied_total += buf_copy_len;
1064  left += buf_copy_len;
1065  len -= buf_copy_len;
1066  offset = 0;
1067  }
1068  }
1069  return copied_total;
1070 }
1071 
1072 #if LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1073 
1085 void pbuf_split_64k(struct pbuf *p, struct pbuf **rest)
1086 {
1087  *rest = NULL;
1088  if ((p != NULL) && (p->next != NULL)) {
1089  u16_t tot_len_front = p->len;
1090  struct pbuf *i = p;
1091  struct pbuf *r = p->next;
1092 
1093  /* continue until the total length (summed up as u16_t) overflows */
1094  while ((r != NULL) && ((u16_t)(tot_len_front + r->len) > tot_len_front)) {
1095  tot_len_front += r->len;
1096  i = r;
1097  r = r->next;
1098  }
1099  /* i now points to last packet of the first segment. Set next
1100  pointer to NULL */
1101  i->next = NULL;
1102 
1103  if (r != NULL) {
1104  /* Update the tot_len field in the first part */
1105  for (i = p; i != NULL; i = i->next) {
1106  i->tot_len -= r->tot_len;
1107  LWIP_ASSERT("tot_len/len mismatch in last pbuf",
1108  (i->next != NULL) || (i->tot_len == i->len));
1109  }
1110  if (p->flags & PBUF_FLAG_TCP_FIN) {
1111  r->flags |= PBUF_FLAG_TCP_FIN;
1112  }
1113 
1114  /* tot_len field in rest does not need modifications */
1115  /* reference counters do not need modifications */
1116  *rest = r;
1117  }
1118  }
1119 }
1120 #endif /* LWIP_TCP && TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1121 
1122 /* Actual implementation of pbuf_skip() but returning const pointer... */
1123 static const struct pbuf*
1124 pbuf_skip_const(const struct pbuf* in, u16_t in_offset, u16_t* out_offset)
1125 {
1126  u16_t offset_left = in_offset;
1127  const struct pbuf* q = in;
1128 
1129  /* get the correct pbuf */
1130  while ((q != NULL) && (q->len <= offset_left)) {
1131  offset_left -= q->len;
1132  q = q->next;
1133  }
1134  if (out_offset != NULL) {
1135  *out_offset = offset_left;
1136  }
1137  return q;
1138 }
1139 
1149 struct pbuf*
1150 pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
1151 {
1152  const struct pbuf* out = pbuf_skip_const(in, in_offset, out_offset);
1153  return LWIP_CONST_CAST(struct pbuf*, out);
1154 }
1155 
1167 err_t
1168 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
1169 {
1170  struct pbuf *p;
1171  u16_t buf_copy_len;
1172  u16_t total_copy_len = len;
1173  u16_t copied_total = 0;
1174 
1175  LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return ERR_ARG;);
1176  LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return ERR_ARG;);
1177  LWIP_ERROR("pbuf_take: buf not large enough", (buf->tot_len >= len), return ERR_MEM;);
1178 
1179  if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
1180  return ERR_ARG;
1181  }
1182 
1183  /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
1184  for (p = buf; total_copy_len != 0; p = p->next) {
1185  LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
1186  buf_copy_len = total_copy_len;
1187  if (buf_copy_len > p->len) {
1188  /* this pbuf cannot hold all remaining data */
1189  buf_copy_len = p->len;
1190  }
1191  /* copy the necessary parts of the buffer */
1192  MEMCPY(p->payload, &((const char*)dataptr)[copied_total], buf_copy_len);
1193  total_copy_len -= buf_copy_len;
1194  copied_total += buf_copy_len;
1195  }
1196  LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
1197  return ERR_OK;
1198 }
1199 
1211 err_t
1212 pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
1213 {
1214  u16_t target_offset;
1215  struct pbuf* q = pbuf_skip(buf, offset, &target_offset);
1216 
1217  /* return requested data if pbuf is OK */
1218  if ((q != NULL) && (q->tot_len >= target_offset + len)) {
1219  u16_t remaining_len = len;
1220  const u8_t* src_ptr = (const u8_t*)dataptr;
1221  /* copy the part that goes into the first pbuf */
1222  u16_t first_copy_len = LWIP_MIN(q->len - target_offset, len);
1223  MEMCPY(((u8_t*)q->payload) + target_offset, dataptr, first_copy_len);
1224  remaining_len -= first_copy_len;
1225  src_ptr += first_copy_len;
1226  if (remaining_len > 0) {
1227  return pbuf_take(q->next, src_ptr, remaining_len);
1228  }
1229  return ERR_OK;
1230  }
1231  return ERR_MEM;
1232 }
1233 
1247 struct pbuf*
1248 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
1249 {
1250  struct pbuf *q;
1251  err_t err;
1252  if (p->next == NULL) {
1253  return p;
1254  }
1255  q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
1256  if (q == NULL) {
1257  /* @todo: what do we do now? */
1258  return p;
1259  }
1260  err = pbuf_copy(q, p);
1261  LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */
1262  LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
1263  pbuf_free(p);
1264  return q;
1265 }
1266 
1267 #if LWIP_CHECKSUM_ON_COPY
1268 
1280 err_t
1281 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
1282  u16_t len, u16_t *chksum)
1283 {
1284  u32_t acc;
1285  u16_t copy_chksum;
1286  char *dst_ptr;
1287  LWIP_ASSERT("p != NULL", p != NULL);
1288  LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
1289  LWIP_ASSERT("chksum != NULL", chksum != NULL);
1290  LWIP_ASSERT("len != 0", len != 0);
1291 
1292  if ((start_offset >= p->len) || (start_offset + len > p->len)) {
1293  return ERR_ARG;
1294  }
1295 
1296  dst_ptr = ((char*)p->payload) + start_offset;
1297  copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
1298  if ((start_offset & 1) != 0) {
1299  copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
1300  }
1301  acc = *chksum;
1302  acc += copy_chksum;
1303  *chksum = FOLD_U32T(acc);
1304  return ERR_OK;
1305 }
1306 #endif /* LWIP_CHECKSUM_ON_COPY */
1307 
1317 u8_t
1318 pbuf_get_at(const struct pbuf* p, u16_t offset)
1319 {
1320  int ret = pbuf_try_get_at(p, offset);
1321  if (ret >= 0) {
1322  return (u8_t)ret;
1323  }
1324  return 0;
1325 }
1326 
1335 int
1336 pbuf_try_get_at(const struct pbuf* p, u16_t offset)
1337 {
1338  u16_t q_idx;
1339  const struct pbuf* q = pbuf_skip_const(p, offset, &q_idx);
1340 
1341  /* return requested data if pbuf is OK */
1342  if ((q != NULL) && (q->len > q_idx)) {
1343  return ((u8_t*)q->payload)[q_idx];
1344  }
1345  return -1;
1346 }
1347 
1357 void
1358 pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
1359 {
1360  u16_t q_idx;
1361  struct pbuf* q = pbuf_skip(p, offset, &q_idx);
1362 
1363  /* write requested data if pbuf is OK */
1364  if ((q != NULL) && (q->len > q_idx)) {
1365  ((u8_t*)q->payload)[q_idx] = data;
1366  }
1367 }
1368 
1380 u16_t
1381 pbuf_memcmp(const struct pbuf* p, u16_t offset, const void* s2, u16_t n)
1382 {
1383  u16_t start = offset;
1384  const struct pbuf* q = p;
1385  u16_t i;
1386 
1387  /* pbuf long enough to perform check? */
1388  if(p->tot_len < (offset + n)) {
1389  return 0xffff;
1390  }
1391 
1392  /* get the correct pbuf from chain. We know it succeeds because of p->tot_len check above. */
1393  while ((q != NULL) && (q->len <= start)) {
1394  start -= q->len;
1395  q = q->next;
1396  }
1397 
1398  /* return requested data if pbuf is OK */
1399  for (i = 0; i < n; i++) {
1400  /* We know pbuf_get_at() succeeds because of p->tot_len check above. */
1401  u8_t a = pbuf_get_at(q, start + i);
1402  u8_t b = ((const u8_t*)s2)[i];
1403  if (a != b) {
1404  return i+1;
1405  }
1406  }
1407  return 0;
1408 }
1409 
1422 u16_t
1423 pbuf_memfind(const struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
1424 {
1425  u16_t i;
1426  u16_t max = p->tot_len - mem_len;
1427  if (p->tot_len >= mem_len + start_offset) {
1428  for (i = start_offset; i <= max; i++) {
1429  u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
1430  if (plus == 0) {
1431  return i;
1432  }
1433  }
1434  }
1435  return 0xFFFF;
1436 }
1437 
1449 u16_t
1450 pbuf_strstr(const struct pbuf* p, const char* substr)
1451 {
1452  size_t substr_len;
1453  if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1454  return 0xFFFF;
1455  }
1456  substr_len = strlen(substr);
1457  if (substr_len >= 0xFFFF) {
1458  return 0xFFFF;
1459  }
1460  return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1461 }
u16_t tot_len
Definition: pbuf.h:175
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition: pbuf.c:512
Definition: pbuf.h:113
struct pbuf * next
Definition: pbuf.h:163
u16_t pbuf_clen(const struct pbuf *p)
Definition: pbuf.c:819
u16_t len
Definition: pbuf.h:178
struct pbuf * pbuf_skip(struct pbuf *in, u16_t in_offset, u16_t *out_offset)
Definition: pbuf.c:1150
void mem_free(void *rmem)
Definition: mem.c:438
u16_t pbuf_memcmp(const struct pbuf *p, u16_t offset, const void *s2, u16_t n)
Definition: pbuf.c:1381
Definition: pbuf.h:131
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:684
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:839
#define LWIP_SUPPORT_CUSTOM_PBUF
Definition: pbuf.h:74
void memp_free(memp_t type, void *mem)
Definition: memp.c:488
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
Definition: pbuf.c:967
#define PBUF_LINK_HLEN
Definition: opt.h:1374
u16_t pbuf_memfind(const struct pbuf *p, const void *mem, u16_t mem_len, u16_t start_offset)
Definition: pbuf.c:1423
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:420
u8_t pbuf_get_at(const struct pbuf *p, u16_t offset)
Definition: pbuf.c:1318
Definition: err.h:84
err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1212
void * mem_trim(void *rmem, mem_size_t newsize)
Definition: mem.c:497
u16_t pbuf_strstr(const struct pbuf *p, const char *substr)
Definition: pbuf.c:1450
void pbuf_put_at(struct pbuf *p, u16_t offset, u8_t data)
Definition: pbuf.c:1358
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:901
u8_t flags
Definition: pbuf.h:184
Definition: pbuf.h:135
Definition: err.h:115
Definition: pbuf.h:161
#define SWAP_BYTES_IN_WORD(w)
Definition: inet_chksum.h:66
#define FOLD_U32T(u)
Definition: inet_chksum.h:71
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:859
pbuf_layer
Definition: pbuf.h:91
struct pbuf * pbuf_dechain(struct pbuf *p)
Definition: pbuf.c:918
s8_t err_t
Definition: err.h:76
#define PBUF_DEBUG
Definition: opt.h:2664
#define LWIP_DEBUGF(debug, message)
#define LWIP_MEM_ALIGN_SIZE(size)
Definition: arch.h:233
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:267
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1034
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
Definition: pbuf.c:1168
Definition: pbuf.h:127
#define PBUF_FLAG_IS_CUSTOM
Definition: pbuf.h:150
#define PBUF_FLAG_TCP_FIN
Definition: pbuf.h:158
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:694
Definition: err.h:94
#define LWIP_MEM_ALIGN(addr)
Definition: arch.h:248
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:327
#define LWIP_CONST_CAST(target_type, val)
Definition: arch.h:199
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:734
Definition: err.h:82
void * payload
Definition: pbuf.h:166
Definition: pbuf.h:99
Definition: mem.c:283
void * mem_malloc(mem_size_t size)
Definition: mem.c:622
#define PBUF_LINK_ENCAPSULATION_HLEN
Definition: opt.h:1383
u8_t type
Definition: pbuf.h:181
struct pbuf * pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
Definition: pbuf.c:1248
void * memp_malloc(memp_t type)
Definition: memp.c:404
u16_t ref
Definition: pbuf.h:191
int pbuf_try_get_at(const struct pbuf *p, u16_t offset)
Definition: pbuf.c:1336
pbuf_type
Definition: pbuf.h:120