]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/lwip/lwip/core/pbuf.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / lwip / lwip / core / pbuf.c
1 /**
2  * @file
3  * Packet buffer management
4  *
5  * Packets are built from the pbuf data structure. It supports dynamic
6  * memory allocation for packet contents or can reference externally
7  * managed packet contents both in RAM and ROM. Quick allocation for
8  * incoming packets is provided through pools with fixed sized pbufs.
9  *
10  * A packet may span over multiple pbufs, chained as a singly linked
11  * list. This is called a "pbuf chain".
12  *
13  * Multiple packets may be queued, also using this singly linked list.
14  * This is called a "packet queue".
15  * 
16  * So, a packet queue consists of one or more pbuf chains, each of
17  * which consist of one or more pbufs. CURRENTLY, PACKET QUEUES ARE
18  * NOT SUPPORTED!!! Use helper structs to queue multiple packets.
19  * 
20  * The differences between a pbuf chain and a packet queue are very
21  * precise but subtle. 
22  *
23  * The last pbuf of a packet has a ->tot_len field that equals the
24  * ->len field. It can be found by traversing the list. If the last
25  * pbuf of a packet has a ->next field other than NULL, more packets
26  * are on the queue.
27  *
28  * Therefore, looping through a pbuf of a single packet, has an
29  * loop end condition (tot_len == p->len), NOT (next == NULL).
30  */
31
32 /*
33  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without modification,
37  * are permitted provided that the following conditions are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright notice,
40  *    this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright notice,
42  *    this list of conditions and the following disclaimer in the documentation
43  *    and/or other materials provided with the distribution.
44  * 3. The name of the author may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
48  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
49  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
50  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
52  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
55  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
56  * OF SUCH DAMAGE.
57  *
58  * This file is part of the lwIP TCP/IP stack.
59  *
60  * Author: Adam Dunkels <adam@sics.se>
61  *
62  */
63
64 #include "lwip/opt.h"
65
66 #include "lwip/stats.h"
67 #include "lwip/def.h"
68 #include "lwip/mem.h"
69 #include "lwip/memp.h"
70 #include "lwip/pbuf.h"
71 #include "lwip/sys.h"
72 #include "arch/perf.h"
73 #if TCP_QUEUE_OOSEQ
74 #include "lwip/tcp_impl.h"
75 #endif
76 #if LWIP_CHECKSUM_ON_COPY
77 #include "lwip/inet_chksum.h"
78 #endif
79
80 #include <string.h>
81
82 #define SIZEOF_STRUCT_PBUF        LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))
83 /* Since the pool is created in memp, PBUF_POOL_BUFSIZE will be automatically
84    aligned there. Therefore, PBUF_POOL_BUFSIZE_ALIGNED can be used here. */
85 #define PBUF_POOL_BUFSIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE)
86
87 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS
88 #define PBUF_POOL_IS_EMPTY()
89 #else /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
90 /** Define this to 0 to prevent freeing ooseq pbufs when the PBUF_POOL is empty */
91 #ifndef PBUF_POOL_FREE_OOSEQ
92 #define PBUF_POOL_FREE_OOSEQ 1
93 #endif /* PBUF_POOL_FREE_OOSEQ */
94
95 #if PBUF_POOL_FREE_OOSEQ
96 #include "lwip/tcpip.h"
97 #define PBUF_POOL_IS_EMPTY() pbuf_pool_is_empty()
98 static u8_t pbuf_free_ooseq_queued;
99 /**
100  * Attempt to reclaim some memory from queued out-of-sequence TCP segments
101  * if we run out of pool pbufs. It's better to give priority to new packets
102  * if we're running out.
103  *
104  * This must be done in the correct thread context therefore this function
105  * can only be used with NO_SYS=0 and through tcpip_callback.
106  */
107 static void
108 pbuf_free_ooseq(void* arg)
109 {
110   struct tcp_pcb* pcb;
111   SYS_ARCH_DECL_PROTECT(old_level);
112   LWIP_UNUSED_ARG(arg);
113
114   SYS_ARCH_PROTECT(old_level);
115   pbuf_free_ooseq_queued = 0;
116   SYS_ARCH_UNPROTECT(old_level);
117
118   for (pcb = tcp_active_pcbs; NULL != pcb; pcb = pcb->next) {
119     if (NULL != pcb->ooseq) {
120       /** Free the ooseq pbufs of one PCB only */
121       LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free_ooseq: freeing out-of-sequence pbufs\n"));
122       tcp_segs_free(pcb->ooseq);
123       pcb->ooseq = NULL;
124       return;
125     }
126   }
127 }
128
129 /** Queue a call to pbuf_free_ooseq if not already queued. */
130 static void
131 pbuf_pool_is_empty(void)
132 {
133   u8_t queued;
134   SYS_ARCH_DECL_PROTECT(old_level);
135
136   SYS_ARCH_PROTECT(old_level);
137   queued = pbuf_free_ooseq_queued;
138   pbuf_free_ooseq_queued = 1;
139   SYS_ARCH_UNPROTECT(old_level);
140
141   if(!queued) {
142     /* queue a call to pbuf_free_ooseq if not already queued */
143     if(tcpip_callback_with_block(pbuf_free_ooseq, NULL, 0) != ERR_OK) {
144       SYS_ARCH_PROTECT(old_level);
145       pbuf_free_ooseq_queued = 0;
146       SYS_ARCH_UNPROTECT(old_level);
147     }
148   }
149 }
150 #endif /* PBUF_POOL_FREE_OOSEQ */
151 #endif /* !LWIP_TCP || !TCP_QUEUE_OOSEQ || NO_SYS */
152
153 /**
154  * Allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
155  *
156  * The actual memory allocated for the pbuf is determined by the
157  * layer at which the pbuf is allocated and the requested size
158  * (from the size parameter).
159  *
160  * @param layer flag to define header size
161  * @param length size of the pbuf's payload
162  * @param type this parameter decides how and where the pbuf
163  * should be allocated as follows:
164  *
165  * - PBUF_RAM: buffer memory for pbuf is allocated as one large
166  *             chunk. This includes protocol headers as well.
167  * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
168  *             protocol headers. Additional headers must be prepended
169  *             by allocating another pbuf and chain in to the front of
170  *             the ROM pbuf. It is assumed that the memory used is really
171  *             similar to ROM in that it is immutable and will not be
172  *             changed. Memory which is dynamic should generally not
173  *             be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
174  * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
175  *             protocol headers. It is assumed that the pbuf is only
176  *             being used in a single thread. If the pbuf gets queued,
177  *             then pbuf_take should be called to copy the buffer.
178  * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
179  *              the pbuf pool that is allocated during pbuf_init().
180  *
181  * @return the allocated pbuf. If multiple pbufs where allocated, this
182  * is the first pbuf of a pbuf chain.
183  */
184 struct pbuf *
185 pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
186 {
187   struct pbuf *p, *q, *r;
188   u16_t offset;
189   s32_t rem_len; /* remaining length */
190   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F")\n", length));
191
192   /* determine header offset */
193   offset = 0;
194   switch (layer) {
195   case PBUF_TRANSPORT:
196     /* add room for transport (often TCP) layer header */
197     offset += PBUF_TRANSPORT_HLEN;
198     /* FALLTHROUGH */
199   case PBUF_IP:
200     /* add room for IP layer header */
201     offset += PBUF_IP_HLEN;
202     /* FALLTHROUGH */
203   case PBUF_LINK:
204     /* add room for link layer header */
205     offset += PBUF_LINK_HLEN;
206     break;
207   case PBUF_RAW:
208     break;
209   default:
210     LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
211     return NULL;
212   }
213
214   switch (type) {
215   case PBUF_POOL:
216     /* allocate head of pbuf chain into p */
217     p = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
218     LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
219     if (p == NULL) {
220       PBUF_POOL_IS_EMPTY();
221       return NULL;
222     }
223     p->type = type;
224     p->next = NULL;
225
226     /* make the payload pointer point 'offset' bytes into pbuf data memory */
227     p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + (SIZEOF_STRUCT_PBUF + offset)));
228     LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
229             ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
230     /* the total length of the pbuf chain is the requested size */
231     p->tot_len = length;
232     /* set the length of the first pbuf in the chain */
233     p->len = LWIP_MIN(length, PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset));
234     LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
235                 ((u8_t*)p->payload + p->len <=
236                  (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
237     LWIP_ASSERT("PBUF_POOL_BUFSIZE must be bigger than MEM_ALIGNMENT",
238       (PBUF_POOL_BUFSIZE_ALIGNED - LWIP_MEM_ALIGN_SIZE(offset)) > 0 );
239     /* set reference count (needed here in case we fail) */
240     p->ref = 1;
241
242     /* now allocate the tail of the pbuf chain */
243
244     /* remember first pbuf for linkage in next iteration */
245     r = p;
246     /* remaining length to be allocated */
247     rem_len = length - p->len;
248     /* any remaining pbufs to be allocated? */
249     while (rem_len > 0) {
250       q = (struct pbuf *)memp_malloc(MEMP_PBUF_POOL);
251       if (q == NULL) {
252         PBUF_POOL_IS_EMPTY();
253         /* free chain so far allocated */
254         pbuf_free(p);
255         /* bail out unsuccesfully */
256         return NULL;
257       }
258       q->type = type;
259       q->flags = 0;
260       q->next = NULL;
261       /* make previous pbuf point to this pbuf */
262       r->next = q;
263       /* set total length of this pbuf and next in chain */
264       LWIP_ASSERT("rem_len < max_u16_t", rem_len < 0xffff);
265       q->tot_len = (u16_t)rem_len;
266       /* this pbuf length is pool size, unless smaller sized tail */
267       q->len = LWIP_MIN((u16_t)rem_len, PBUF_POOL_BUFSIZE_ALIGNED);
268       q->payload = (void *)((u8_t *)q + SIZEOF_STRUCT_PBUF);
269       LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
270               ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
271       LWIP_ASSERT("check p->payload + p->len does not overflow pbuf",
272                   ((u8_t*)p->payload + p->len <=
273                    (u8_t*)p + SIZEOF_STRUCT_PBUF + PBUF_POOL_BUFSIZE_ALIGNED));
274       q->ref = 1;
275       /* calculate remaining length to be allocated */
276       rem_len -= q->len;
277       /* remember this pbuf for linkage in next iteration */
278       r = q;
279     }
280     /* end of chain */
281     /*r->next = NULL;*/
282
283     break;
284   case PBUF_RAM:
285     /* If pbuf is to be allocated in RAM, allocate memory for it. */
286     p = (struct pbuf*)mem_malloc(LWIP_MEM_ALIGN_SIZE(SIZEOF_STRUCT_PBUF + offset) + LWIP_MEM_ALIGN_SIZE(length));
287     if (p == NULL) {
288       return NULL;
289     }
290     /* Set up internal structure of the pbuf. */
291     p->payload = LWIP_MEM_ALIGN((void *)((u8_t *)p + SIZEOF_STRUCT_PBUF + offset));
292     p->len = p->tot_len = length;
293     p->next = NULL;
294     p->type = type;
295
296     LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
297            ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
298     break;
299   /* pbuf references existing (non-volatile static constant) ROM payload? */
300   case PBUF_ROM:
301   /* pbuf references existing (externally allocated) RAM payload? */
302   case PBUF_REF:
303     /* only allocate memory for the pbuf structure */
304     p = (struct pbuf *)memp_malloc(MEMP_PBUF);
305     if (p == NULL) {
306       LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
307                   ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n",
308                   (type == PBUF_ROM) ? "ROM" : "REF"));
309       return NULL;
310     }
311     /* caller must set this field properly, afterwards */
312     p->payload = NULL;
313     p->len = p->tot_len = length;
314     p->next = NULL;
315     p->type = type;
316     break;
317   default:
318     LWIP_ASSERT("pbuf_alloc: erroneous type", 0);
319     return NULL;
320   }
321   /* set reference count */
322   p->ref = 1;
323   /* set flags */
324   p->flags = 0;
325   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloc(length=%"U16_F") == %p\n", length, (void *)p));
326   return p;
327 }
328
329 #if LWIP_SUPPORT_CUSTOM_PBUF
330 /** Initialize a custom pbuf (already allocated).
331  *
332  * @param layer flag to define header size
333  * @param length size of the pbuf's payload
334  * @param type type of the pbuf (only used to treat the pbuf accordingly, as
335  *        this function allocates no memory)
336  * @param p pointer to the custom pbuf to initialize (already allocated)
337  * @param payload_mem pointer to the buffer that is used for payload and headers,
338  *        must be at least big enough to hold 'length' plus the header size,
339  *        may be NULL if set later
340  * @param payload_mem_len the size of the 'payload_mem' buffer, must be at least
341  *        big enough to hold 'length' plus the header size
342  */
343 struct pbuf*
344 pbuf_alloced_custom(pbuf_layer l, u16_t length, pbuf_type type, struct pbuf_custom *p,
345                     void *payload_mem, u16_t payload_mem_len)
346 {
347   u16_t offset;
348   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_alloced_custom(length=%"U16_F")\n", length));
349
350   /* determine header offset */
351   offset = 0;
352   switch (l) {
353   case PBUF_TRANSPORT:
354     /* add room for transport (often TCP) layer header */
355     offset += PBUF_TRANSPORT_HLEN;
356     /* FALLTHROUGH */
357   case PBUF_IP:
358     /* add room for IP layer header */
359     offset += PBUF_IP_HLEN;
360     /* FALLTHROUGH */
361   case PBUF_LINK:
362     /* add room for link layer header */
363     offset += PBUF_LINK_HLEN;
364     break;
365   case PBUF_RAW:
366     break;
367   default:
368     LWIP_ASSERT("pbuf_alloced_custom: bad pbuf layer", 0);
369     return NULL;
370   }
371
372   if (LWIP_MEM_ALIGN_SIZE(offset) + length < payload_mem_len) {
373     LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_WARNING, ("pbuf_alloced_custom(length=%"U16_F") buffer too short\n", length));
374     return NULL;
375   }
376
377   p->pbuf.next = NULL;
378   if (payload_mem != NULL) {
379     p->pbuf.payload = (void *)((u8_t *)payload_mem + offset);
380   } else {
381     p->pbuf.payload = NULL;
382   }
383   p->pbuf.flags = PBUF_FLAG_IS_CUSTOM;
384   p->pbuf.len = p->pbuf.tot_len = length;
385   p->pbuf.type = type;
386   p->pbuf.ref = 1;
387   return &p->pbuf;
388 }
389 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
390
391 /**
392  * Shrink a pbuf chain to a desired length.
393  *
394  * @param p pbuf to shrink.
395  * @param new_len desired new length of pbuf chain
396  *
397  * Depending on the desired length, the first few pbufs in a chain might
398  * be skipped and left unchanged. The new last pbuf in the chain will be
399  * resized, and any remaining pbufs will be freed.
400  *
401  * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted.
402  * @note May not be called on a packet queue.
403  *
404  * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain).
405  */
406 void
407 pbuf_realloc(struct pbuf *p, u16_t new_len)
408 {
409   struct pbuf *q;
410   u16_t rem_len; /* remaining length */
411   s32_t grow;
412
413   LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL);
414   LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL ||
415               p->type == PBUF_ROM ||
416               p->type == PBUF_RAM ||
417               p->type == PBUF_REF);
418
419   /* desired length larger than current length? */
420   if (new_len >= p->tot_len) {
421     /* enlarging not yet supported */
422     return;
423   }
424
425   /* the pbuf chain grows by (new_len - p->tot_len) bytes
426    * (which may be negative in case of shrinking) */
427   grow = new_len - p->tot_len;
428
429   /* first, step over any pbufs that should remain in the chain */
430   rem_len = new_len;
431   q = p;
432   /* should this pbuf be kept? */
433   while (rem_len > q->len) {
434     /* decrease remaining length by pbuf length */
435     rem_len -= q->len;
436     /* decrease total length indicator */
437     LWIP_ASSERT("grow < max_u16_t", grow < 0xffff);
438     q->tot_len += (u16_t)grow;
439     /* proceed to next pbuf in chain */
440     q = q->next;
441     LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL);
442   }
443   /* we have now reached the new last pbuf (in q) */
444   /* rem_len == desired length for pbuf q */
445
446   /* shrink allocated memory for PBUF_RAM */
447   /* (other types merely adjust their length fields */
448   if ((q->type == PBUF_RAM) && (rem_len != q->len)) {
449     /* reallocate and adjust the length of the pbuf that will be split */
450     q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
451     LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
452   }
453   /* adjust length fields for new last pbuf */
454   q->len = rem_len;
455   q->tot_len = q->len;
456
457   /* any remaining pbufs in chain? */
458   if (q->next != NULL) {
459     /* free remaining pbufs in chain */
460     pbuf_free(q->next);
461   }
462   /* q is last packet in chain */
463   q->next = NULL;
464
465 }
466
467 /**
468  * Adjusts the payload pointer to hide or reveal headers in the payload.
469  *
470  * Adjusts the ->payload pointer so that space for a header
471  * (dis)appears in the pbuf payload.
472  *
473  * The ->payload, ->tot_len and ->len fields are adjusted.
474  *
475  * @param p pbuf to change the header size.
476  * @param header_size_increment Number of bytes to increment header size which
477  * increases the size of the pbuf. New space is on the front.
478  * (Using a negative value decreases the header size.)
479  * If hdr_size_inc is 0, this function does nothing and returns succesful.
480  *
481  * PBUF_ROM and PBUF_REF type buffers cannot have their sizes increased, so
482  * the call will fail. A check is made that the increase in header size does
483  * not move the payload pointer in front of the start of the buffer.
484  * @return non-zero on failure, zero on success.
485  *
486  */
487 u8_t
488 pbuf_header(struct pbuf *p, s16_t header_size_increment)
489 {
490   u16_t type;
491   void *payload;
492   u16_t increment_magnitude;
493
494   LWIP_ASSERT("p != NULL", p != NULL);
495   if ((header_size_increment == 0) || (p == NULL)) {
496     return 0;
497   }
498  
499   if (header_size_increment < 0){
500     increment_magnitude = -header_size_increment;
501     /* Check that we aren't going to move off the end of the pbuf */
502     LWIP_ERROR("increment_magnitude <= p->len", (increment_magnitude <= p->len), return 1;);
503   } else {
504     increment_magnitude = header_size_increment;
505 #if 0
506     /* Can't assert these as some callers speculatively call
507          pbuf_header() to see if it's OK.  Will return 1 below instead. */
508     /* Check that we've got the correct type of pbuf to work with */
509     LWIP_ASSERT("p->type == PBUF_RAM || p->type == PBUF_POOL", 
510                 p->type == PBUF_RAM || p->type == PBUF_POOL);
511     /* Check that we aren't going to move off the beginning of the pbuf */
512     LWIP_ASSERT("p->payload - increment_magnitude >= p + SIZEOF_STRUCT_PBUF",
513                 (u8_t *)p->payload - increment_magnitude >= (u8_t *)p + SIZEOF_STRUCT_PBUF);
514 #endif
515   }
516
517   type = p->type;
518   /* remember current payload pointer */
519   payload = p->payload;
520
521   /* pbuf types containing payloads? */
522   if (type == PBUF_RAM || type == PBUF_POOL) {
523     /* set new payload pointer */
524     p->payload = (u8_t *)p->payload - header_size_increment;
525     /* boundary check fails? */
526     if ((u8_t *)p->payload < (u8_t *)p + SIZEOF_STRUCT_PBUF) {
527       LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
528         ("pbuf_header: failed as %p < %p (not enough space for new header size)\n",
529         (void *)p->payload, (void *)(p + 1)));
530       /* restore old payload pointer */
531       p->payload = payload;
532       /* bail out unsuccesfully */
533       return 1;
534     }
535   /* pbuf types refering to external payloads? */
536   } else if (type == PBUF_REF || type == PBUF_ROM) {
537     /* hide a header in the payload? */
538     if ((header_size_increment < 0) && (increment_magnitude <= p->len)) {
539       /* increase payload pointer */
540       p->payload = (u8_t *)p->payload - header_size_increment;
541     } else {
542       /* cannot expand payload to front (yet!)
543        * bail out unsuccesfully */
544       return 1;
545     }
546   } else {
547     /* Unknown type */
548     LWIP_ASSERT("bad pbuf type", 0);
549     return 1;
550   }
551   /* modify pbuf length fields */
552   p->len += header_size_increment;
553   p->tot_len += header_size_increment;
554
555   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_header: old %p new %p (%"S16_F")\n",
556     (void *)payload, (void *)p->payload, header_size_increment));
557
558   return 0;
559 }
560
561 /**
562  * Dereference a pbuf chain or queue and deallocate any no-longer-used
563  * pbufs at the head of this chain or queue.
564  *
565  * Decrements the pbuf reference count. If it reaches zero, the pbuf is
566  * deallocated.
567  *
568  * For a pbuf chain, this is repeated for each pbuf in the chain,
569  * up to the first pbuf which has a non-zero reference count after
570  * decrementing. So, when all reference counts are one, the whole
571  * chain is free'd.
572  *
573  * @param p The pbuf (chain) to be dereferenced.
574  *
575  * @return the number of pbufs that were de-allocated
576  * from the head of the chain.
577  *
578  * @note MUST NOT be called on a packet queue (Not verified to work yet).
579  * @note the reference counter of a pbuf equals the number of pointers
580  * that refer to the pbuf (or into the pbuf).
581  *
582  * @internal examples:
583  *
584  * Assuming existing chains a->b->c with the following reference
585  * counts, calling pbuf_free(a) results in:
586  * 
587  * 1->2->3 becomes ...1->3
588  * 3->3->3 becomes 2->3->3
589  * 1->1->2 becomes ......1
590  * 2->1->1 becomes 1->1->1
591  * 1->1->1 becomes .......
592  *
593  */
594 u8_t
595 pbuf_free(struct pbuf *p)
596 {
597   u16_t type;
598   struct pbuf *q;
599   u8_t count;
600
601   if (p == NULL) {
602     LWIP_ASSERT("p != NULL", p != NULL);
603     /* if assertions are disabled, proceed with debug output */
604     LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
605       ("pbuf_free(p == NULL) was called.\n"));
606     return 0;
607   }
608   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
609
610   PERF_START;
611
612   LWIP_ASSERT("pbuf_free: sane type",
613     p->type == PBUF_RAM || p->type == PBUF_ROM ||
614     p->type == PBUF_REF || p->type == PBUF_POOL);
615
616   count = 0;
617   /* de-allocate all consecutive pbufs from the head of the chain that
618    * obtain a zero reference count after decrementing*/
619   while (p != NULL) {
620     u16_t ref;
621     SYS_ARCH_DECL_PROTECT(old_level);
622     /* Since decrementing ref cannot be guaranteed to be a single machine operation
623      * we must protect it. We put the new ref into a local variable to prevent
624      * further protection. */
625     SYS_ARCH_PROTECT(old_level);
626     /* all pbufs in a chain are referenced at least once */
627     LWIP_ASSERT("pbuf_free: p->ref > 0", p->ref > 0);
628     /* decrease reference count (number of pointers to pbuf) */
629     ref = --(p->ref);
630     SYS_ARCH_UNPROTECT(old_level);
631     /* this pbuf is no longer referenced to? */
632     if (ref == 0) {
633       /* remember next pbuf in chain for next iteration */
634       q = p->next;
635       LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: deallocating %p\n", (void *)p));
636       type = p->type;
637 #if LWIP_SUPPORT_CUSTOM_PBUF
638       /* is this a custom pbuf? */
639       if ((p->flags & PBUF_FLAG_IS_CUSTOM) != 0) {
640         struct pbuf_custom *pc = (struct pbuf_custom*)p;
641         LWIP_ASSERT("pc->custom_free_function != NULL", pc->custom_free_function != NULL);
642         pc->custom_free_function(p);
643       } else
644 #endif /* LWIP_SUPPORT_CUSTOM_PBUF */
645       {
646         /* is this a pbuf from the pool? */
647         if (type == PBUF_POOL) {
648           memp_free(MEMP_PBUF_POOL, p);
649         /* is this a ROM or RAM referencing pbuf? */
650         } else if (type == PBUF_ROM || type == PBUF_REF) {
651           memp_free(MEMP_PBUF, p);
652         /* type == PBUF_RAM */
653         } else {
654           mem_free(p);
655         }
656       }
657       count++;
658       /* proceed to next pbuf */
659       p = q;
660     /* p->ref > 0, this pbuf is still referenced to */
661     /* (and so the remaining pbufs in chain as well) */
662     } else {
663       LWIP_DEBUGF( PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free: %p has ref %"U16_F", ending here.\n", (void *)p, ref));
664       /* stop walking through the chain */
665       p = NULL;
666     }
667   }
668   PERF_STOP("pbuf_free");
669   /* return number of de-allocated pbufs */
670   return count;
671 }
672
673 /**
674  * Count number of pbufs in a chain
675  *
676  * @param p first pbuf of chain
677  * @return the number of pbufs in a chain
678  */
679
680 u8_t
681 pbuf_clen(struct pbuf *p)
682 {
683   u8_t len;
684
685   len = 0;
686   while (p != NULL) {
687     ++len;
688     p = p->next;
689   }
690   return len;
691 }
692
693 /**
694  * Increment the reference count of the pbuf.
695  *
696  * @param p pbuf to increase reference counter of
697  *
698  */
699 void
700 pbuf_ref(struct pbuf *p)
701 {
702   SYS_ARCH_DECL_PROTECT(old_level);
703   /* pbuf given? */
704   if (p != NULL) {
705     SYS_ARCH_PROTECT(old_level);
706     ++(p->ref);
707     SYS_ARCH_UNPROTECT(old_level);
708   }
709 }
710
711 /**
712  * Concatenate two pbufs (each may be a pbuf chain) and take over
713  * the caller's reference of the tail pbuf.
714  * 
715  * @note The caller MAY NOT reference the tail pbuf afterwards.
716  * Use pbuf_chain() for that purpose.
717  * 
718  * @see pbuf_chain()
719  */
720
721 void
722 pbuf_cat(struct pbuf *h, struct pbuf *t)
723 {
724   struct pbuf *p;
725
726   LWIP_ERROR("(h != NULL) && (t != NULL) (programmer violates API)",
727              ((h != NULL) && (t != NULL)), return;);
728
729   /* proceed to last pbuf of chain */
730   for (p = h; p->next != NULL; p = p->next) {
731     /* add total length of second chain to all totals of first chain */
732     p->tot_len += t->tot_len;
733   }
734   /* { p is last pbuf of first h chain, p->next == NULL } */
735   LWIP_ASSERT("p->tot_len == p->len (of last pbuf in chain)", p->tot_len == p->len);
736   LWIP_ASSERT("p->next == NULL", p->next == NULL);
737   /* add total length of second chain to last pbuf total of first chain */
738   p->tot_len += t->tot_len;
739   /* chain last pbuf of head (p) with first of tail (t) */
740   p->next = t;
741   /* p->next now references t, but the caller will drop its reference to t,
742    * so netto there is no change to the reference count of t.
743    */
744 }
745
746 /**
747  * Chain two pbufs (or pbuf chains) together.
748  * 
749  * The caller MUST call pbuf_free(t) once it has stopped
750  * using it. Use pbuf_cat() instead if you no longer use t.
751  * 
752  * @param h head pbuf (chain)
753  * @param t tail pbuf (chain)
754  * @note The pbufs MUST belong to the same packet.
755  * @note MAY NOT be called on a packet queue.
756  *
757  * The ->tot_len fields of all pbufs of the head chain are adjusted.
758  * The ->next field of the last pbuf of the head chain is adjusted.
759  * The ->ref field of the first pbuf of the tail chain is adjusted.
760  *
761  */
762 void
763 pbuf_chain(struct pbuf *h, struct pbuf *t)
764 {
765   pbuf_cat(h, t);
766   /* t is now referenced by h */
767   pbuf_ref(t);
768   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_chain: %p references %p\n", (void *)h, (void *)t));
769 }
770
771 /**
772  * Dechains the first pbuf from its succeeding pbufs in the chain.
773  *
774  * Makes p->tot_len field equal to p->len.
775  * @param p pbuf to dechain
776  * @return remainder of the pbuf chain, or NULL if it was de-allocated.
777  * @note May not be called on a packet queue.
778  */
779 struct pbuf *
780 pbuf_dechain(struct pbuf *p)
781 {
782   struct pbuf *q;
783   u8_t tail_gone = 1;
784   /* tail */
785   q = p->next;
786   /* pbuf has successor in chain? */
787   if (q != NULL) {
788     /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
789     LWIP_ASSERT("p->tot_len == p->len + q->tot_len", q->tot_len == p->tot_len - p->len);
790     /* enforce invariant if assertion is disabled */
791     q->tot_len = p->tot_len - p->len;
792     /* decouple pbuf from remainder */
793     p->next = NULL;
794     /* total length of pbuf p is its own length only */
795     p->tot_len = p->len;
796     /* q is no longer referenced by p, free it */
797     LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_dechain: unreferencing %p\n", (void *)q));
798     tail_gone = pbuf_free(q);
799     if (tail_gone > 0) {
800       LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE,
801                   ("pbuf_dechain: deallocated %p (as it is no longer referenced)\n", (void *)q));
802     }
803     /* return remaining tail or NULL if deallocated */
804   }
805   /* assert tot_len invariant: (p->tot_len == p->len + (p->next? p->next->tot_len: 0) */
806   LWIP_ASSERT("p->tot_len == p->len", p->tot_len == p->len);
807   return ((tail_gone > 0) ? NULL : q);
808 }
809
810 /**
811  *
812  * Create PBUF_RAM copies of pbufs.
813  *
814  * Used to queue packets on behalf of the lwIP stack, such as
815  * ARP based queueing.
816  *
817  * @note You MUST explicitly use p = pbuf_take(p);
818  *
819  * @note Only one packet is copied, no packet queue!
820  *
821  * @param p_to pbuf destination of the copy
822  * @param p_from pbuf source of the copy
823  *
824  * @return ERR_OK if pbuf was copied
825  *         ERR_ARG if one of the pbufs is NULL or p_to is not big
826  *                 enough to hold p_from
827  */
828 err_t
829 pbuf_copy(struct pbuf *p_to, struct pbuf *p_from)
830 {
831   u16_t offset_to=0, offset_from=0, len;
832
833   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy(%p, %p)\n",
834     (void*)p_to, (void*)p_from));
835
836   /* is the target big enough to hold the source? */
837   LWIP_ERROR("pbuf_copy: target not big enough to hold source", ((p_to != NULL) &&
838              (p_from != NULL) && (p_to->tot_len >= p_from->tot_len)), return ERR_ARG;);
839
840   /* iterate through pbuf chain */
841   do
842   {
843     LWIP_ASSERT("p_to != NULL", p_to != NULL);
844     /* copy one part of the original chain */
845     if ((p_to->len - offset_to) >= (p_from->len - offset_from)) {
846       /* complete current p_from fits into current p_to */
847       len = p_from->len - offset_from;
848     } else {
849       /* current p_from does not fit into current p_to */
850       len = p_to->len - offset_to;
851     }
852     MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len);
853     offset_to += len;
854     offset_from += len;
855     LWIP_ASSERT("offset_to <= p_to->len", offset_to <= p_to->len);
856     if (offset_to == p_to->len) {
857       /* on to next p_to (if any) */
858       offset_to = 0;
859       p_to = p_to->next;
860     }
861     LWIP_ASSERT("offset_from <= p_from->len", offset_from <= p_from->len);
862     if (offset_from >= p_from->len) {
863       /* on to next p_from (if any) */
864       offset_from = 0;
865       p_from = p_from->next;
866     }
867
868     if((p_from != NULL) && (p_from->len == p_from->tot_len)) {
869       /* don't copy more than one packet! */
870       LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
871                  (p_from->next == NULL), return ERR_VAL;);
872     }
873     if((p_to != NULL) && (p_to->len == p_to->tot_len)) {
874       /* don't copy more than one packet! */
875       LWIP_ERROR("pbuf_copy() does not allow packet queues!\n",
876                   (p_to->next == NULL), return ERR_VAL;);
877     }
878   } while (p_from);
879   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_copy: end of chain reached.\n"));
880   return ERR_OK;
881 }
882
883 /**
884  * Copy (part of) the contents of a packet buffer
885  * to an application supplied buffer.
886  *
887  * @param buf the pbuf from which to copy data
888  * @param dataptr the application supplied buffer
889  * @param len length of data to copy (dataptr must be big enough). No more 
890  * than buf->tot_len will be copied, irrespective of len
891  * @param offset offset into the packet buffer from where to begin copying len bytes
892  * @return the number of bytes copied, or 0 on failure
893  */
894 u16_t
895 pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
896 {
897   struct pbuf *p;
898   u16_t left;
899   u16_t buf_copy_len;
900   u16_t copied_total = 0;
901
902   LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
903   LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
904
905   left = 0;
906
907   if((buf == NULL) || (dataptr == NULL)) {
908     return 0;
909   }
910
911   /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
912   for(p = buf; len != 0 && p != NULL; p = p->next) {
913     if ((offset != 0) && (offset >= p->len)) {
914       /* don't copy from this buffer -> on to the next */
915       offset -= p->len;
916     } else {
917       /* copy from this buffer. maybe only partially. */
918       buf_copy_len = p->len - offset;
919       if (buf_copy_len > len)
920           buf_copy_len = len;
921       /* copy the necessary parts of the buffer */
922       MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
923       copied_total += buf_copy_len;
924       left += buf_copy_len;
925       len -= buf_copy_len;
926       offset = 0;
927     }
928   }
929   return copied_total;
930 }
931
932 /**
933  * Copy application supplied data into a pbuf.
934  * This function can only be used to copy the equivalent of buf->tot_len data.
935  *
936  * @param buf pbuf to fill with data
937  * @param dataptr application supplied data buffer
938  * @param len length of the application supplied data buffer
939  *
940  * @return ERR_OK if successful, ERR_MEM if the pbuf is not big enough
941  */
942 err_t
943 pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
944 {
945   struct pbuf *p;
946   u16_t buf_copy_len;
947   u16_t total_copy_len = len;
948   u16_t copied_total = 0;
949
950   LWIP_ERROR("pbuf_take: invalid buf", (buf != NULL), return 0;);
951   LWIP_ERROR("pbuf_take: invalid dataptr", (dataptr != NULL), return 0;);
952
953   if ((buf == NULL) || (dataptr == NULL) || (buf->tot_len < len)) {
954     return ERR_ARG;
955   }
956
957   /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
958   for(p = buf; total_copy_len != 0; p = p->next) {
959     LWIP_ASSERT("pbuf_take: invalid pbuf", p != NULL);
960     buf_copy_len = total_copy_len;
961     if (buf_copy_len > p->len) {
962       /* this pbuf cannot hold all remaining data */
963       buf_copy_len = p->len;
964     }
965     /* copy the necessary parts of the buffer */
966     MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len);
967     total_copy_len -= buf_copy_len;
968     copied_total += buf_copy_len;
969   }
970   LWIP_ASSERT("did not copy all data", total_copy_len == 0 && copied_total == len);
971   return ERR_OK;
972 }
973
974 /**
975  * Creates a single pbuf out of a queue of pbufs.
976  *
977  * @remark: Either the source pbuf 'p' is freed by this function or the original
978  *          pbuf 'p' is returned, therefore the caller has to check the result!
979  *
980  * @param p the source pbuf
981  * @param layer pbuf_layer of the new pbuf
982  *
983  * @return a new, single pbuf (p->next is NULL)
984  *         or the old pbuf if allocation fails
985  */
986 struct pbuf*
987 pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
988 {
989   struct pbuf *q;
990   err_t err;
991   if (p->next == NULL) {
992     return p;
993   }
994   q = pbuf_alloc(layer, p->tot_len, PBUF_RAM);
995   if (q == NULL) {
996     /* @todo: what do we do now? */
997     return p;
998   }
999   err = pbuf_copy(q, p);
1000   LWIP_ASSERT("pbuf_copy failed", err == ERR_OK);
1001   /* next line references err variable even if LWIP_ASSERT is ignored. */
1002   (void)err;
1003   pbuf_free(p);
1004   return q;
1005 }
1006
1007 #if LWIP_CHECKSUM_ON_COPY
1008 /**
1009  * Copies data into a single pbuf (*not* into a pbuf queue!) and updates
1010  * the checksum while copying
1011  *
1012  * @param p the pbuf to copy data into
1013  * @param start_offset offset of p->payload where to copy the data to
1014  * @param dataptr data to copy into the pbuf
1015  * @param len length of data to copy into the pbuf
1016  * @param chksum pointer to the checksum which is updated
1017  * @return ERR_OK if successful, another error if the data does not fit
1018  *         within the (first) pbuf (no pbuf queues!)
1019  */
1020 err_t
1021 pbuf_fill_chksum(struct pbuf *p, u16_t start_offset, const void *dataptr,
1022                  u16_t len, u16_t *chksum)
1023 {
1024   u32_t acc;
1025   u16_t copy_chksum;
1026   char *dst_ptr;
1027   LWIP_ASSERT("p != NULL", p != NULL);
1028   LWIP_ASSERT("dataptr != NULL", dataptr != NULL);
1029   LWIP_ASSERT("chksum != NULL", chksum != NULL);
1030   LWIP_ASSERT("len != 0", len != 0);
1031
1032   if ((start_offset >= p->len) || (start_offset + len > p->len)) {
1033     return ERR_ARG;
1034   }
1035
1036   dst_ptr = ((char*)p->payload) + start_offset;
1037   copy_chksum = LWIP_CHKSUM_COPY(dst_ptr, dataptr, len);
1038   if ((start_offset & 1) != 0) {
1039     copy_chksum = SWAP_BYTES_IN_WORD(copy_chksum);
1040   }
1041   acc = *chksum;
1042   acc += copy_chksum;
1043   *chksum = FOLD_U32T(acc);
1044   return ERR_OK;
1045 }
1046 #endif /* LWIP_CHECKSUM_ON_COPY */
1047
1048  /** Get one byte from the specified position in a pbuf
1049  * WARNING: returns zero for offset >= p->tot_len
1050  *
1051  * @param p pbuf to parse
1052  * @param offset offset into p of the byte to return
1053  * @return byte at an offset into p OR ZERO IF 'offset' >= p->tot_len
1054  */
1055 u8_t
1056 pbuf_get_at(struct pbuf* p, u16_t offset)
1057 {
1058   u16_t copy_from = offset;
1059   struct pbuf* q = p;
1060
1061   /* get the correct pbuf */
1062   while ((q != NULL) && (q->len <= copy_from)) {
1063     copy_from -= q->len;
1064     q = q->next;
1065   }
1066   /* return requested data if pbuf is OK */
1067   if ((q != NULL) && (q->len > copy_from)) {
1068     return ((u8_t*)q->payload)[copy_from];
1069   }
1070   return 0;
1071 }
1072
1073 /** Compare pbuf contents at specified offset with memory s2, both of length n
1074  *
1075  * @param p pbuf to compare
1076  * @param offset offset into p at wich to start comparing
1077  * @param s2 buffer to compare
1078  * @param n length of buffer to compare
1079  * @return zero if equal, nonzero otherwise
1080  *         (0xffff if p is too short, diffoffset+1 otherwise)
1081  */
1082 u16_t
1083 pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n)
1084 {
1085   u16_t start = offset;
1086   struct pbuf* q = p;
1087
1088   /* get the correct pbuf */
1089   while ((q != NULL) && (q->len <= start)) {
1090     start -= q->len;
1091     q = q->next;
1092   }
1093   /* return requested data if pbuf is OK */
1094   if ((q != NULL) && (q->len > start)) {
1095     u16_t i;
1096     for(i = 0; i < n; i++) {
1097       u8_t a = pbuf_get_at(q, start + i);
1098       u8_t b = ((u8_t*)s2)[i];
1099       if (a != b) {
1100         return i+1;
1101       }
1102     }
1103     return 0;
1104   }
1105   return 0xffff;
1106 }
1107
1108 /** Find occurrence of mem (with length mem_len) in pbuf p, starting at offset
1109  * start_offset.
1110  *
1111  * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
1112  *        return value 'not found'
1113  * @param mem search for the contents of this buffer
1114  * @param mem_len length of 'mem'
1115  * @param start_offset offset into p at which to start searching
1116  * @return 0xFFFF if substr was not found in p or the index where it was found
1117  */
1118 u16_t
1119 pbuf_memfind(struct pbuf* p, const void* mem, u16_t mem_len, u16_t start_offset)
1120 {
1121   u16_t i;
1122   u16_t max = p->tot_len - mem_len;
1123   if (p->tot_len >= mem_len + start_offset) {
1124     for(i = start_offset; i <= max; ) {
1125       u16_t plus = pbuf_memcmp(p, i, mem, mem_len);
1126       if (plus == 0) {
1127         return i;
1128       } else {
1129         i += plus;
1130       }
1131     }
1132   }
1133   return 0xFFFF;
1134 }
1135
1136 /** Find occurrence of substr with length substr_len in pbuf p, start at offset
1137  * start_offset
1138  * WARNING: in contrast to strstr(), this one does not stop at the first \0 in
1139  * the pbuf/source string!
1140  *
1141  * @param p pbuf to search, maximum length is 0xFFFE since 0xFFFF is used as
1142  *        return value 'not found'
1143  * @param substr string to search for in p, maximum length is 0xFFFE
1144  * @return 0xFFFF if substr was not found in p or the index where it was found
1145  */
1146 u16_t
1147 pbuf_strstr(struct pbuf* p, const char* substr)
1148 {
1149   size_t substr_len;
1150   if ((substr == NULL) || (substr[0] == 0) || (p->tot_len == 0xFFFF)) {
1151     return 0xFFFF;
1152   }
1153   substr_len = strlen(substr);
1154   if (substr_len >= 0xFFFF) {
1155     return 0xFFFF;
1156   }
1157   return pbuf_memfind(p, substr, (u16_t)substr_len, 0);
1158 }