HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1 /**
mr_q 0:d8f2f7d5f31b 2 * @file
mr_q 0:d8f2f7d5f31b 3 * This is the IPv4 packet segmentation and reassembly implementation.
mr_q 0:d8f2f7d5f31b 4 *
mr_q 0:d8f2f7d5f31b 5 */
mr_q 0:d8f2f7d5f31b 6
mr_q 0:d8f2f7d5f31b 7 /*
mr_q 0:d8f2f7d5f31b 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
mr_q 0:d8f2f7d5f31b 9 * All rights reserved.
mr_q 0:d8f2f7d5f31b 10 *
mr_q 0:d8f2f7d5f31b 11 * Redistribution and use in source and binary forms, with or without modification,
mr_q 0:d8f2f7d5f31b 12 * are permitted provided that the following conditions are met:
mr_q 0:d8f2f7d5f31b 13 *
mr_q 0:d8f2f7d5f31b 14 * 1. Redistributions of source code must retain the above copyright notice,
mr_q 0:d8f2f7d5f31b 15 * this list of conditions and the following disclaimer.
mr_q 0:d8f2f7d5f31b 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
mr_q 0:d8f2f7d5f31b 17 * this list of conditions and the following disclaimer in the documentation
mr_q 0:d8f2f7d5f31b 18 * and/or other materials provided with the distribution.
mr_q 0:d8f2f7d5f31b 19 * 3. The name of the author may not be used to endorse or promote products
mr_q 0:d8f2f7d5f31b 20 * derived from this software without specific prior written permission.
mr_q 0:d8f2f7d5f31b 21 *
mr_q 0:d8f2f7d5f31b 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
mr_q 0:d8f2f7d5f31b 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mr_q 0:d8f2f7d5f31b 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
mr_q 0:d8f2f7d5f31b 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mr_q 0:d8f2f7d5f31b 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
mr_q 0:d8f2f7d5f31b 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mr_q 0:d8f2f7d5f31b 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mr_q 0:d8f2f7d5f31b 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
mr_q 0:d8f2f7d5f31b 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
mr_q 0:d8f2f7d5f31b 31 * OF SUCH DAMAGE.
mr_q 0:d8f2f7d5f31b 32 *
mr_q 0:d8f2f7d5f31b 33 * This file is part of the lwIP TCP/IP stack.
mr_q 0:d8f2f7d5f31b 34 *
mr_q 0:d8f2f7d5f31b 35 * Author: Jani Monoses <jani@iv.ro>
mr_q 0:d8f2f7d5f31b 36 * Simon Goldschmidt
mr_q 0:d8f2f7d5f31b 37 * original reassembly code by Adam Dunkels <adam@sics.se>
mr_q 0:d8f2f7d5f31b 38 *
mr_q 0:d8f2f7d5f31b 39 */
mr_q 0:d8f2f7d5f31b 40
mr_q 0:d8f2f7d5f31b 41 #include "lwip/opt.h"
mr_q 0:d8f2f7d5f31b 42 #include "lwip/ip_frag.h"
mr_q 0:d8f2f7d5f31b 43 #include "lwip/def.h"
mr_q 0:d8f2f7d5f31b 44 #include "lwip/inet_chksum.h"
mr_q 0:d8f2f7d5f31b 45 #include "lwip/netif.h"
mr_q 0:d8f2f7d5f31b 46 #include "lwip/snmp.h"
mr_q 0:d8f2f7d5f31b 47 #include "lwip/stats.h"
mr_q 0:d8f2f7d5f31b 48 #include "lwip/icmp.h"
mr_q 0:d8f2f7d5f31b 49
mr_q 0:d8f2f7d5f31b 50 #include <string.h>
mr_q 0:d8f2f7d5f31b 51
mr_q 0:d8f2f7d5f31b 52 #if IP_REASSEMBLY
mr_q 0:d8f2f7d5f31b 53 /**
mr_q 0:d8f2f7d5f31b 54 * The IP reassembly code currently has the following limitations:
mr_q 0:d8f2f7d5f31b 55 * - IP header options are not supported
mr_q 0:d8f2f7d5f31b 56 * - fragments must not overlap (e.g. due to different routes),
mr_q 0:d8f2f7d5f31b 57 * currently, overlapping or duplicate fragments are thrown away
mr_q 0:d8f2f7d5f31b 58 * if IP_REASS_CHECK_OVERLAP=1 (the default)!
mr_q 0:d8f2f7d5f31b 59 *
mr_q 0:d8f2f7d5f31b 60 * @todo: work with IP header options
mr_q 0:d8f2f7d5f31b 61 */
mr_q 0:d8f2f7d5f31b 62
mr_q 0:d8f2f7d5f31b 63 /** Setting this to 0, you can turn off checking the fragments for overlapping
mr_q 0:d8f2f7d5f31b 64 * regions. The code gets a little smaller. Only use this if you know that
mr_q 0:d8f2f7d5f31b 65 * overlapping won't occur on your network! */
mr_q 0:d8f2f7d5f31b 66 #ifndef IP_REASS_CHECK_OVERLAP
mr_q 0:d8f2f7d5f31b 67 #define IP_REASS_CHECK_OVERLAP 1
mr_q 0:d8f2f7d5f31b 68 #endif /* IP_REASS_CHECK_OVERLAP */
mr_q 0:d8f2f7d5f31b 69
mr_q 0:d8f2f7d5f31b 70 /** Set to 0 to prevent freeing the oldest datagram when the reassembly buffer is
mr_q 0:d8f2f7d5f31b 71 * full (IP_REASS_MAX_PBUFS pbufs are enqueued). The code gets a little smaller.
mr_q 0:d8f2f7d5f31b 72 * Datagrams will be freed by timeout only. Especially useful when MEMP_NUM_REASSDATA
mr_q 0:d8f2f7d5f31b 73 * is set to 1, so one datagram can be reassembled at a time, only. */
mr_q 0:d8f2f7d5f31b 74 #ifndef IP_REASS_FREE_OLDEST
mr_q 0:d8f2f7d5f31b 75 #define IP_REASS_FREE_OLDEST 1
mr_q 0:d8f2f7d5f31b 76 #endif /* IP_REASS_FREE_OLDEST */
mr_q 0:d8f2f7d5f31b 77
mr_q 0:d8f2f7d5f31b 78 #define IP_REASS_FLAG_LASTFRAG 0x01
mr_q 0:d8f2f7d5f31b 79
mr_q 0:d8f2f7d5f31b 80 /** This is a helper struct which holds the starting
mr_q 0:d8f2f7d5f31b 81 * offset and the ending offset of this fragment to
mr_q 0:d8f2f7d5f31b 82 * easily chain the fragments.
mr_q 0:d8f2f7d5f31b 83 * It has the same packing requirements as the IP header, since it replaces
mr_q 0:d8f2f7d5f31b 84 * the IP header in memory in incoming fragments (after copying it) to keep
mr_q 0:d8f2f7d5f31b 85 * track of the various fragments. (-> If the IP header doesn't need packing,
mr_q 0:d8f2f7d5f31b 86 * this struct doesn't need packing, too.)
mr_q 0:d8f2f7d5f31b 87 */
mr_q 0:d8f2f7d5f31b 88 #ifdef PACK_STRUCT_USE_INCLUDES
mr_q 0:d8f2f7d5f31b 89 # include "arch/bpstruct.h"
mr_q 0:d8f2f7d5f31b 90 #endif
mr_q 0:d8f2f7d5f31b 91 PACK_STRUCT_BEGIN
mr_q 0:d8f2f7d5f31b 92 struct ip_reass_helper {
mr_q 0:d8f2f7d5f31b 93 PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
mr_q 0:d8f2f7d5f31b 94 PACK_STRUCT_FIELD(u16_t start);
mr_q 0:d8f2f7d5f31b 95 PACK_STRUCT_FIELD(u16_t end);
mr_q 0:d8f2f7d5f31b 96 } PACK_STRUCT_STRUCT;
mr_q 0:d8f2f7d5f31b 97 PACK_STRUCT_END
mr_q 0:d8f2f7d5f31b 98 #ifdef PACK_STRUCT_USE_INCLUDES
mr_q 0:d8f2f7d5f31b 99 # include "arch/epstruct.h"
mr_q 0:d8f2f7d5f31b 100 #endif
mr_q 0:d8f2f7d5f31b 101
mr_q 0:d8f2f7d5f31b 102 #define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
mr_q 0:d8f2f7d5f31b 103 (ip_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
mr_q 0:d8f2f7d5f31b 104 ip_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
mr_q 0:d8f2f7d5f31b 105 IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0
mr_q 0:d8f2f7d5f31b 106
mr_q 0:d8f2f7d5f31b 107 /* global variables */
mr_q 0:d8f2f7d5f31b 108 static struct ip_reassdata *reassdatagrams;
mr_q 0:d8f2f7d5f31b 109 static u16_t ip_reass_pbufcount;
mr_q 0:d8f2f7d5f31b 110
mr_q 0:d8f2f7d5f31b 111 /* function prototypes */
mr_q 0:d8f2f7d5f31b 112 static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
mr_q 0:d8f2f7d5f31b 113 static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
mr_q 0:d8f2f7d5f31b 114
mr_q 0:d8f2f7d5f31b 115 /**
mr_q 0:d8f2f7d5f31b 116 * Reassembly timer base function
mr_q 0:d8f2f7d5f31b 117 * for both NO_SYS == 0 and 1 (!).
mr_q 0:d8f2f7d5f31b 118 *
mr_q 0:d8f2f7d5f31b 119 * Should be called every 1000 msec (defined by IP_TMR_INTERVAL).
mr_q 0:d8f2f7d5f31b 120 */
mr_q 0:d8f2f7d5f31b 121 void
mr_q 0:d8f2f7d5f31b 122 ip_reass_tmr(void)
mr_q 0:d8f2f7d5f31b 123 {
mr_q 0:d8f2f7d5f31b 124 struct ip_reassdata *r, *prev = NULL;
mr_q 0:d8f2f7d5f31b 125
mr_q 0:d8f2f7d5f31b 126 r = reassdatagrams;
mr_q 0:d8f2f7d5f31b 127 while (r != NULL) {
mr_q 0:d8f2f7d5f31b 128 /* Decrement the timer. Once it reaches 0,
mr_q 0:d8f2f7d5f31b 129 * clean up the incomplete fragment assembly */
mr_q 0:d8f2f7d5f31b 130 if (r->timer > 0) {
mr_q 0:d8f2f7d5f31b 131 r->timer--;
mr_q 0:d8f2f7d5f31b 132 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)r->timer));
mr_q 0:d8f2f7d5f31b 133 prev = r;
mr_q 0:d8f2f7d5f31b 134 r = r->next;
mr_q 0:d8f2f7d5f31b 135 } else {
mr_q 0:d8f2f7d5f31b 136 /* reassembly timed out */
mr_q 0:d8f2f7d5f31b 137 struct ip_reassdata *tmp;
mr_q 0:d8f2f7d5f31b 138 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
mr_q 0:d8f2f7d5f31b 139 tmp = r;
mr_q 0:d8f2f7d5f31b 140 /* get the next pointer before freeing */
mr_q 0:d8f2f7d5f31b 141 r = r->next;
mr_q 0:d8f2f7d5f31b 142 /* free the helper struct and all enqueued pbufs */
mr_q 0:d8f2f7d5f31b 143 ip_reass_free_complete_datagram(tmp, prev);
mr_q 0:d8f2f7d5f31b 144 }
mr_q 0:d8f2f7d5f31b 145 }
mr_q 0:d8f2f7d5f31b 146 }
mr_q 0:d8f2f7d5f31b 147
mr_q 0:d8f2f7d5f31b 148 /**
mr_q 0:d8f2f7d5f31b 149 * Free a datagram (struct ip_reassdata) and all its pbufs.
mr_q 0:d8f2f7d5f31b 150 * Updates the total count of enqueued pbufs (ip_reass_pbufcount),
mr_q 0:d8f2f7d5f31b 151 * SNMP counters and sends an ICMP time exceeded packet.
mr_q 0:d8f2f7d5f31b 152 *
mr_q 0:d8f2f7d5f31b 153 * @param ipr datagram to free
mr_q 0:d8f2f7d5f31b 154 * @param prev the previous datagram in the linked list
mr_q 0:d8f2f7d5f31b 155 * @return the number of pbufs freed
mr_q 0:d8f2f7d5f31b 156 */
mr_q 0:d8f2f7d5f31b 157 static int
mr_q 0:d8f2f7d5f31b 158 ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
mr_q 0:d8f2f7d5f31b 159 {
mr_q 0:d8f2f7d5f31b 160 u16_t pbufs_freed = 0;
mr_q 0:d8f2f7d5f31b 161 u8_t clen;
mr_q 0:d8f2f7d5f31b 162 struct pbuf *p;
mr_q 0:d8f2f7d5f31b 163 struct ip_reass_helper *iprh;
mr_q 0:d8f2f7d5f31b 164
mr_q 0:d8f2f7d5f31b 165 LWIP_ASSERT("prev != ipr", prev != ipr);
mr_q 0:d8f2f7d5f31b 166 if (prev != NULL) {
mr_q 0:d8f2f7d5f31b 167 LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
mr_q 0:d8f2f7d5f31b 168 }
mr_q 0:d8f2f7d5f31b 169
mr_q 0:d8f2f7d5f31b 170 snmp_inc_ipreasmfails();
mr_q 0:d8f2f7d5f31b 171 #if LWIP_ICMP
mr_q 0:d8f2f7d5f31b 172 iprh = (struct ip_reass_helper *)ipr->p->payload;
mr_q 0:d8f2f7d5f31b 173 if (iprh->start == 0) {
mr_q 0:d8f2f7d5f31b 174 /* The first fragment was received, send ICMP time exceeded. */
mr_q 0:d8f2f7d5f31b 175 /* First, de-queue the first pbuf from r->p. */
mr_q 0:d8f2f7d5f31b 176 p = ipr->p;
mr_q 0:d8f2f7d5f31b 177 ipr->p = iprh->next_pbuf;
mr_q 0:d8f2f7d5f31b 178 /* Then, copy the original header into it. */
mr_q 0:d8f2f7d5f31b 179 SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
mr_q 0:d8f2f7d5f31b 180 icmp_time_exceeded(p, ICMP_TE_FRAG);
mr_q 0:d8f2f7d5f31b 181 clen = pbuf_clen(p);
mr_q 0:d8f2f7d5f31b 182 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
mr_q 0:d8f2f7d5f31b 183 pbufs_freed += clen;
mr_q 0:d8f2f7d5f31b 184 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 185 }
mr_q 0:d8f2f7d5f31b 186 #endif /* LWIP_ICMP */
mr_q 0:d8f2f7d5f31b 187
mr_q 0:d8f2f7d5f31b 188 /* First, free all received pbufs. The individual pbufs need to be released
mr_q 0:d8f2f7d5f31b 189 separately as they have not yet been chained */
mr_q 0:d8f2f7d5f31b 190 p = ipr->p;
mr_q 0:d8f2f7d5f31b 191 while (p != NULL) {
mr_q 0:d8f2f7d5f31b 192 struct pbuf *pcur;
mr_q 0:d8f2f7d5f31b 193 iprh = (struct ip_reass_helper *)p->payload;
mr_q 0:d8f2f7d5f31b 194 pcur = p;
mr_q 0:d8f2f7d5f31b 195 /* get the next pointer before freeing */
mr_q 0:d8f2f7d5f31b 196 p = iprh->next_pbuf;
mr_q 0:d8f2f7d5f31b 197 clen = pbuf_clen(pcur);
mr_q 0:d8f2f7d5f31b 198 LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
mr_q 0:d8f2f7d5f31b 199 pbufs_freed += clen;
mr_q 0:d8f2f7d5f31b 200 pbuf_free(pcur);
mr_q 0:d8f2f7d5f31b 201 }
mr_q 0:d8f2f7d5f31b 202 /* Then, unchain the struct ip_reassdata from the list and free it. */
mr_q 0:d8f2f7d5f31b 203 ip_reass_dequeue_datagram(ipr, prev);
mr_q 0:d8f2f7d5f31b 204 LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= pbufs_freed);
mr_q 0:d8f2f7d5f31b 205 ip_reass_pbufcount -= pbufs_freed;
mr_q 0:d8f2f7d5f31b 206
mr_q 0:d8f2f7d5f31b 207 return pbufs_freed;
mr_q 0:d8f2f7d5f31b 208 }
mr_q 0:d8f2f7d5f31b 209
mr_q 0:d8f2f7d5f31b 210 #if IP_REASS_FREE_OLDEST
mr_q 0:d8f2f7d5f31b 211 /**
mr_q 0:d8f2f7d5f31b 212 * Free the oldest datagram to make room for enqueueing new fragments.
mr_q 0:d8f2f7d5f31b 213 * The datagram 'fraghdr' belongs to is not freed!
mr_q 0:d8f2f7d5f31b 214 *
mr_q 0:d8f2f7d5f31b 215 * @param fraghdr IP header of the current fragment
mr_q 0:d8f2f7d5f31b 216 * @param pbufs_needed number of pbufs needed to enqueue
mr_q 0:d8f2f7d5f31b 217 * (used for freeing other datagrams if not enough space)
mr_q 0:d8f2f7d5f31b 218 * @return the number of pbufs freed
mr_q 0:d8f2f7d5f31b 219 */
mr_q 0:d8f2f7d5f31b 220 static int
mr_q 0:d8f2f7d5f31b 221 ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
mr_q 0:d8f2f7d5f31b 222 {
mr_q 0:d8f2f7d5f31b 223 /* @todo Can't we simply remove the last datagram in the
mr_q 0:d8f2f7d5f31b 224 * linked list behind reassdatagrams?
mr_q 0:d8f2f7d5f31b 225 */
mr_q 0:d8f2f7d5f31b 226 struct ip_reassdata *r, *oldest, *prev;
mr_q 0:d8f2f7d5f31b 227 int pbufs_freed = 0, pbufs_freed_current;
mr_q 0:d8f2f7d5f31b 228 int other_datagrams;
mr_q 0:d8f2f7d5f31b 229
mr_q 0:d8f2f7d5f31b 230 /* Free datagrams until being allowed to enqueue 'pbufs_needed' pbufs,
mr_q 0:d8f2f7d5f31b 231 * but don't free the datagram that 'fraghdr' belongs to! */
mr_q 0:d8f2f7d5f31b 232 do {
mr_q 0:d8f2f7d5f31b 233 oldest = NULL;
mr_q 0:d8f2f7d5f31b 234 prev = NULL;
mr_q 0:d8f2f7d5f31b 235 other_datagrams = 0;
mr_q 0:d8f2f7d5f31b 236 r = reassdatagrams;
mr_q 0:d8f2f7d5f31b 237 while (r != NULL) {
mr_q 0:d8f2f7d5f31b 238 if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
mr_q 0:d8f2f7d5f31b 239 /* Not the same datagram as fraghdr */
mr_q 0:d8f2f7d5f31b 240 other_datagrams++;
mr_q 0:d8f2f7d5f31b 241 if (oldest == NULL) {
mr_q 0:d8f2f7d5f31b 242 oldest = r;
mr_q 0:d8f2f7d5f31b 243 } else if (r->timer <= oldest->timer) {
mr_q 0:d8f2f7d5f31b 244 /* older than the previous oldest */
mr_q 0:d8f2f7d5f31b 245 oldest = r;
mr_q 0:d8f2f7d5f31b 246 }
mr_q 0:d8f2f7d5f31b 247 }
mr_q 0:d8f2f7d5f31b 248 if (r->next != NULL) {
mr_q 0:d8f2f7d5f31b 249 prev = r;
mr_q 0:d8f2f7d5f31b 250 }
mr_q 0:d8f2f7d5f31b 251 r = r->next;
mr_q 0:d8f2f7d5f31b 252 }
mr_q 0:d8f2f7d5f31b 253 if (oldest != NULL) {
mr_q 0:d8f2f7d5f31b 254 pbufs_freed_current = ip_reass_free_complete_datagram(oldest, prev);
mr_q 0:d8f2f7d5f31b 255 pbufs_freed += pbufs_freed_current;
mr_q 0:d8f2f7d5f31b 256 }
mr_q 0:d8f2f7d5f31b 257 } while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
mr_q 0:d8f2f7d5f31b 258 return pbufs_freed;
mr_q 0:d8f2f7d5f31b 259 }
mr_q 0:d8f2f7d5f31b 260 #endif /* IP_REASS_FREE_OLDEST */
mr_q 0:d8f2f7d5f31b 261
mr_q 0:d8f2f7d5f31b 262 /**
mr_q 0:d8f2f7d5f31b 263 * Enqueues a new fragment into the fragment queue
mr_q 0:d8f2f7d5f31b 264 * @param fraghdr points to the new fragments IP hdr
mr_q 0:d8f2f7d5f31b 265 * @param clen number of pbufs needed to enqueue (used for freeing other datagrams if not enough space)
mr_q 0:d8f2f7d5f31b 266 * @return A pointer to the queue location into which the fragment was enqueued
mr_q 0:d8f2f7d5f31b 267 */
mr_q 0:d8f2f7d5f31b 268 static struct ip_reassdata*
mr_q 0:d8f2f7d5f31b 269 ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
mr_q 0:d8f2f7d5f31b 270 {
mr_q 0:d8f2f7d5f31b 271 struct ip_reassdata* ipr;
mr_q 0:d8f2f7d5f31b 272 /* No matching previous fragment found, allocate a new reassdata struct */
mr_q 0:d8f2f7d5f31b 273 ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
mr_q 0:d8f2f7d5f31b 274 if (ipr == NULL) {
mr_q 0:d8f2f7d5f31b 275 #if IP_REASS_FREE_OLDEST
mr_q 0:d8f2f7d5f31b 276 if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
mr_q 0:d8f2f7d5f31b 277 ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
mr_q 0:d8f2f7d5f31b 278 }
mr_q 0:d8f2f7d5f31b 279 if (ipr == NULL)
mr_q 0:d8f2f7d5f31b 280 #endif /* IP_REASS_FREE_OLDEST */
mr_q 0:d8f2f7d5f31b 281 {
mr_q 0:d8f2f7d5f31b 282 IPFRAG_STATS_INC(ip_frag.memerr);
mr_q 0:d8f2f7d5f31b 283 LWIP_DEBUGF(IP_REASS_DEBUG,("Failed to alloc reassdata struct\n"));
mr_q 0:d8f2f7d5f31b 284 return NULL;
mr_q 0:d8f2f7d5f31b 285 }
mr_q 0:d8f2f7d5f31b 286 }
mr_q 0:d8f2f7d5f31b 287 memset(ipr, 0, sizeof(struct ip_reassdata));
mr_q 0:d8f2f7d5f31b 288 ipr->timer = IP_REASS_MAXAGE;
mr_q 0:d8f2f7d5f31b 289
mr_q 0:d8f2f7d5f31b 290 /* enqueue the new structure to the front of the list */
mr_q 0:d8f2f7d5f31b 291 ipr->next = reassdatagrams;
mr_q 0:d8f2f7d5f31b 292 reassdatagrams = ipr;
mr_q 0:d8f2f7d5f31b 293 /* copy the ip header for later tests and input */
mr_q 0:d8f2f7d5f31b 294 /* @todo: no ip options supported? */
mr_q 0:d8f2f7d5f31b 295 SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
mr_q 0:d8f2f7d5f31b 296 return ipr;
mr_q 0:d8f2f7d5f31b 297 }
mr_q 0:d8f2f7d5f31b 298
mr_q 0:d8f2f7d5f31b 299 /**
mr_q 0:d8f2f7d5f31b 300 * Dequeues a datagram from the datagram queue. Doesn't deallocate the pbufs.
mr_q 0:d8f2f7d5f31b 301 * @param ipr points to the queue entry to dequeue
mr_q 0:d8f2f7d5f31b 302 */
mr_q 0:d8f2f7d5f31b 303 static void
mr_q 0:d8f2f7d5f31b 304 ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
mr_q 0:d8f2f7d5f31b 305 {
mr_q 0:d8f2f7d5f31b 306
mr_q 0:d8f2f7d5f31b 307 /* dequeue the reass struct */
mr_q 0:d8f2f7d5f31b 308 if (reassdatagrams == ipr) {
mr_q 0:d8f2f7d5f31b 309 /* it was the first in the list */
mr_q 0:d8f2f7d5f31b 310 reassdatagrams = ipr->next;
mr_q 0:d8f2f7d5f31b 311 } else {
mr_q 0:d8f2f7d5f31b 312 /* it wasn't the first, so it must have a valid 'prev' */
mr_q 0:d8f2f7d5f31b 313 LWIP_ASSERT("sanity check linked list", prev != NULL);
mr_q 0:d8f2f7d5f31b 314 prev->next = ipr->next;
mr_q 0:d8f2f7d5f31b 315 }
mr_q 0:d8f2f7d5f31b 316
mr_q 0:d8f2f7d5f31b 317 /* now we can free the ip_reass struct */
mr_q 0:d8f2f7d5f31b 318 memp_free(MEMP_REASSDATA, ipr);
mr_q 0:d8f2f7d5f31b 319 }
mr_q 0:d8f2f7d5f31b 320
mr_q 0:d8f2f7d5f31b 321 /**
mr_q 0:d8f2f7d5f31b 322 * Chain a new pbuf into the pbuf list that composes the datagram. The pbuf list
mr_q 0:d8f2f7d5f31b 323 * will grow over time as new pbufs are rx.
mr_q 0:d8f2f7d5f31b 324 * Also checks that the datagram passes basic continuity checks (if the last
mr_q 0:d8f2f7d5f31b 325 * fragment was received at least once).
mr_q 0:d8f2f7d5f31b 326 * @param root_p points to the 'root' pbuf for the current datagram being assembled.
mr_q 0:d8f2f7d5f31b 327 * @param new_p points to the pbuf for the current fragment
mr_q 0:d8f2f7d5f31b 328 * @return 0 if invalid, >0 otherwise
mr_q 0:d8f2f7d5f31b 329 */
mr_q 0:d8f2f7d5f31b 330 static int
mr_q 0:d8f2f7d5f31b 331 ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p)
mr_q 0:d8f2f7d5f31b 332 {
mr_q 0:d8f2f7d5f31b 333 struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev=NULL;
mr_q 0:d8f2f7d5f31b 334 struct pbuf *q;
mr_q 0:d8f2f7d5f31b 335 u16_t offset,len;
mr_q 0:d8f2f7d5f31b 336 struct ip_hdr *fraghdr;
mr_q 0:d8f2f7d5f31b 337 int valid = 1;
mr_q 0:d8f2f7d5f31b 338
mr_q 0:d8f2f7d5f31b 339 /* Extract length and fragment offset from current fragment */
mr_q 0:d8f2f7d5f31b 340 fraghdr = (struct ip_hdr*)new_p->payload;
mr_q 0:d8f2f7d5f31b 341 len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
mr_q 0:d8f2f7d5f31b 342 offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
mr_q 0:d8f2f7d5f31b 343
mr_q 0:d8f2f7d5f31b 344 /* overwrite the fragment's ip header from the pbuf with our helper struct,
mr_q 0:d8f2f7d5f31b 345 * and setup the embedded helper structure. */
mr_q 0:d8f2f7d5f31b 346 /* make sure the struct ip_reass_helper fits into the IP header */
mr_q 0:d8f2f7d5f31b 347 LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
mr_q 0:d8f2f7d5f31b 348 sizeof(struct ip_reass_helper) <= IP_HLEN);
mr_q 0:d8f2f7d5f31b 349 iprh = (struct ip_reass_helper*)new_p->payload;
mr_q 0:d8f2f7d5f31b 350 iprh->next_pbuf = NULL;
mr_q 0:d8f2f7d5f31b 351 iprh->start = offset;
mr_q 0:d8f2f7d5f31b 352 iprh->end = offset + len;
mr_q 0:d8f2f7d5f31b 353
mr_q 0:d8f2f7d5f31b 354 /* Iterate through until we either get to the end of the list (append),
mr_q 0:d8f2f7d5f31b 355 * or we find on with a larger offset (insert). */
mr_q 0:d8f2f7d5f31b 356 for (q = ipr->p; q != NULL;) {
mr_q 0:d8f2f7d5f31b 357 iprh_tmp = (struct ip_reass_helper*)q->payload;
mr_q 0:d8f2f7d5f31b 358 if (iprh->start < iprh_tmp->start) {
mr_q 0:d8f2f7d5f31b 359 /* the new pbuf should be inserted before this */
mr_q 0:d8f2f7d5f31b 360 iprh->next_pbuf = q;
mr_q 0:d8f2f7d5f31b 361 if (iprh_prev != NULL) {
mr_q 0:d8f2f7d5f31b 362 /* not the fragment with the lowest offset */
mr_q 0:d8f2f7d5f31b 363 #if IP_REASS_CHECK_OVERLAP
mr_q 0:d8f2f7d5f31b 364 if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
mr_q 0:d8f2f7d5f31b 365 /* fragment overlaps with previous or following, throw away */
mr_q 0:d8f2f7d5f31b 366 goto freepbuf;
mr_q 0:d8f2f7d5f31b 367 }
mr_q 0:d8f2f7d5f31b 368 #endif /* IP_REASS_CHECK_OVERLAP */
mr_q 0:d8f2f7d5f31b 369 iprh_prev->next_pbuf = new_p;
mr_q 0:d8f2f7d5f31b 370 } else {
mr_q 0:d8f2f7d5f31b 371 /* fragment with the lowest offset */
mr_q 0:d8f2f7d5f31b 372 ipr->p = new_p;
mr_q 0:d8f2f7d5f31b 373 }
mr_q 0:d8f2f7d5f31b 374 break;
mr_q 0:d8f2f7d5f31b 375 } else if(iprh->start == iprh_tmp->start) {
mr_q 0:d8f2f7d5f31b 376 /* received the same datagram twice: no need to keep the datagram */
mr_q 0:d8f2f7d5f31b 377 goto freepbuf;
mr_q 0:d8f2f7d5f31b 378 #if IP_REASS_CHECK_OVERLAP
mr_q 0:d8f2f7d5f31b 379 } else if(iprh->start < iprh_tmp->end) {
mr_q 0:d8f2f7d5f31b 380 /* overlap: no need to keep the new datagram */
mr_q 0:d8f2f7d5f31b 381 goto freepbuf;
mr_q 0:d8f2f7d5f31b 382 #endif /* IP_REASS_CHECK_OVERLAP */
mr_q 0:d8f2f7d5f31b 383 } else {
mr_q 0:d8f2f7d5f31b 384 /* Check if the fragments received so far have no wholes. */
mr_q 0:d8f2f7d5f31b 385 if (iprh_prev != NULL) {
mr_q 0:d8f2f7d5f31b 386 if (iprh_prev->end != iprh_tmp->start) {
mr_q 0:d8f2f7d5f31b 387 /* There is a fragment missing between the current
mr_q 0:d8f2f7d5f31b 388 * and the previous fragment */
mr_q 0:d8f2f7d5f31b 389 valid = 0;
mr_q 0:d8f2f7d5f31b 390 }
mr_q 0:d8f2f7d5f31b 391 }
mr_q 0:d8f2f7d5f31b 392 }
mr_q 0:d8f2f7d5f31b 393 q = iprh_tmp->next_pbuf;
mr_q 0:d8f2f7d5f31b 394 iprh_prev = iprh_tmp;
mr_q 0:d8f2f7d5f31b 395 }
mr_q 0:d8f2f7d5f31b 396
mr_q 0:d8f2f7d5f31b 397 /* If q is NULL, then we made it to the end of the list. Determine what to do now */
mr_q 0:d8f2f7d5f31b 398 if (q == NULL) {
mr_q 0:d8f2f7d5f31b 399 if (iprh_prev != NULL) {
mr_q 0:d8f2f7d5f31b 400 /* this is (for now), the fragment with the highest offset:
mr_q 0:d8f2f7d5f31b 401 * chain it to the last fragment */
mr_q 0:d8f2f7d5f31b 402 #if IP_REASS_CHECK_OVERLAP
mr_q 0:d8f2f7d5f31b 403 LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
mr_q 0:d8f2f7d5f31b 404 #endif /* IP_REASS_CHECK_OVERLAP */
mr_q 0:d8f2f7d5f31b 405 iprh_prev->next_pbuf = new_p;
mr_q 0:d8f2f7d5f31b 406 if (iprh_prev->end != iprh->start) {
mr_q 0:d8f2f7d5f31b 407 valid = 0;
mr_q 0:d8f2f7d5f31b 408 }
mr_q 0:d8f2f7d5f31b 409 } else {
mr_q 0:d8f2f7d5f31b 410 #if IP_REASS_CHECK_OVERLAP
mr_q 0:d8f2f7d5f31b 411 LWIP_ASSERT("no previous fragment, this must be the first fragment!",
mr_q 0:d8f2f7d5f31b 412 ipr->p == NULL);
mr_q 0:d8f2f7d5f31b 413 #endif /* IP_REASS_CHECK_OVERLAP */
mr_q 0:d8f2f7d5f31b 414 /* this is the first fragment we ever received for this ip datagram */
mr_q 0:d8f2f7d5f31b 415 ipr->p = new_p;
mr_q 0:d8f2f7d5f31b 416 }
mr_q 0:d8f2f7d5f31b 417 }
mr_q 0:d8f2f7d5f31b 418
mr_q 0:d8f2f7d5f31b 419 /* At this point, the validation part begins: */
mr_q 0:d8f2f7d5f31b 420 /* If we already received the last fragment */
mr_q 0:d8f2f7d5f31b 421 if ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0) {
mr_q 0:d8f2f7d5f31b 422 /* and had no wholes so far */
mr_q 0:d8f2f7d5f31b 423 if (valid) {
mr_q 0:d8f2f7d5f31b 424 /* then check if the rest of the fragments is here */
mr_q 0:d8f2f7d5f31b 425 /* Check if the queue starts with the first datagram */
mr_q 0:d8f2f7d5f31b 426 if (((struct ip_reass_helper*)ipr->p->payload)->start != 0) {
mr_q 0:d8f2f7d5f31b 427 valid = 0;
mr_q 0:d8f2f7d5f31b 428 } else {
mr_q 0:d8f2f7d5f31b 429 /* and check that there are no wholes after this datagram */
mr_q 0:d8f2f7d5f31b 430 iprh_prev = iprh;
mr_q 0:d8f2f7d5f31b 431 q = iprh->next_pbuf;
mr_q 0:d8f2f7d5f31b 432 while (q != NULL) {
mr_q 0:d8f2f7d5f31b 433 iprh = (struct ip_reass_helper*)q->payload;
mr_q 0:d8f2f7d5f31b 434 if (iprh_prev->end != iprh->start) {
mr_q 0:d8f2f7d5f31b 435 valid = 0;
mr_q 0:d8f2f7d5f31b 436 break;
mr_q 0:d8f2f7d5f31b 437 }
mr_q 0:d8f2f7d5f31b 438 iprh_prev = iprh;
mr_q 0:d8f2f7d5f31b 439 q = iprh->next_pbuf;
mr_q 0:d8f2f7d5f31b 440 }
mr_q 0:d8f2f7d5f31b 441 /* if still valid, all fragments are received
mr_q 0:d8f2f7d5f31b 442 * (because to the MF==0 already arrived */
mr_q 0:d8f2f7d5f31b 443 if (valid) {
mr_q 0:d8f2f7d5f31b 444 LWIP_ASSERT("sanity check", ipr->p != NULL);
mr_q 0:d8f2f7d5f31b 445 LWIP_ASSERT("sanity check",
mr_q 0:d8f2f7d5f31b 446 ((struct ip_reass_helper*)ipr->p->payload) != iprh);
mr_q 0:d8f2f7d5f31b 447 LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
mr_q 0:d8f2f7d5f31b 448 iprh->next_pbuf == NULL);
mr_q 0:d8f2f7d5f31b 449 LWIP_ASSERT("validate_datagram:datagram end!=datagram len",
mr_q 0:d8f2f7d5f31b 450 iprh->end == ipr->datagram_len);
mr_q 0:d8f2f7d5f31b 451 }
mr_q 0:d8f2f7d5f31b 452 }
mr_q 0:d8f2f7d5f31b 453 }
mr_q 0:d8f2f7d5f31b 454 /* If valid is 0 here, there are some fragments missing in the middle
mr_q 0:d8f2f7d5f31b 455 * (since MF == 0 has already arrived). Such datagrams simply time out if
mr_q 0:d8f2f7d5f31b 456 * no more fragments are received... */
mr_q 0:d8f2f7d5f31b 457 return valid;
mr_q 0:d8f2f7d5f31b 458 }
mr_q 0:d8f2f7d5f31b 459 /* If we come here, not all fragments were received, yet! */
mr_q 0:d8f2f7d5f31b 460 return 0; /* not yet valid! */
mr_q 0:d8f2f7d5f31b 461 #if IP_REASS_CHECK_OVERLAP
mr_q 0:d8f2f7d5f31b 462 freepbuf:
mr_q 0:d8f2f7d5f31b 463 ip_reass_pbufcount -= pbuf_clen(new_p);
mr_q 0:d8f2f7d5f31b 464 pbuf_free(new_p);
mr_q 0:d8f2f7d5f31b 465 return 0;
mr_q 0:d8f2f7d5f31b 466 #endif /* IP_REASS_CHECK_OVERLAP */
mr_q 0:d8f2f7d5f31b 467 }
mr_q 0:d8f2f7d5f31b 468
mr_q 0:d8f2f7d5f31b 469 /**
mr_q 0:d8f2f7d5f31b 470 * Reassembles incoming IP fragments into an IP datagram.
mr_q 0:d8f2f7d5f31b 471 *
mr_q 0:d8f2f7d5f31b 472 * @param p points to a pbuf chain of the fragment
mr_q 0:d8f2f7d5f31b 473 * @return NULL if reassembly is incomplete, ? otherwise
mr_q 0:d8f2f7d5f31b 474 */
mr_q 0:d8f2f7d5f31b 475 struct pbuf *
mr_q 0:d8f2f7d5f31b 476 ip_reass(struct pbuf *p)
mr_q 0:d8f2f7d5f31b 477 {
mr_q 0:d8f2f7d5f31b 478 struct pbuf *r;
mr_q 0:d8f2f7d5f31b 479 struct ip_hdr *fraghdr;
mr_q 0:d8f2f7d5f31b 480 struct ip_reassdata *ipr;
mr_q 0:d8f2f7d5f31b 481 struct ip_reass_helper *iprh;
mr_q 0:d8f2f7d5f31b 482 u16_t offset, len;
mr_q 0:d8f2f7d5f31b 483 u8_t clen;
mr_q 0:d8f2f7d5f31b 484 struct ip_reassdata *ipr_prev = NULL;
mr_q 0:d8f2f7d5f31b 485
mr_q 0:d8f2f7d5f31b 486 IPFRAG_STATS_INC(ip_frag.recv);
mr_q 0:d8f2f7d5f31b 487 snmp_inc_ipreasmreqds();
mr_q 0:d8f2f7d5f31b 488
mr_q 0:d8f2f7d5f31b 489 fraghdr = (struct ip_hdr*)p->payload;
mr_q 0:d8f2f7d5f31b 490
mr_q 0:d8f2f7d5f31b 491 if ((IPH_HL(fraghdr) * 4) != IP_HLEN) {
mr_q 0:d8f2f7d5f31b 492 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: IP options currently not supported!\n"));
mr_q 0:d8f2f7d5f31b 493 IPFRAG_STATS_INC(ip_frag.err);
mr_q 0:d8f2f7d5f31b 494 goto nullreturn;
mr_q 0:d8f2f7d5f31b 495 }
mr_q 0:d8f2f7d5f31b 496
mr_q 0:d8f2f7d5f31b 497 offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
mr_q 0:d8f2f7d5f31b 498 len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
mr_q 0:d8f2f7d5f31b 499
mr_q 0:d8f2f7d5f31b 500 /* Check if we are allowed to enqueue more datagrams. */
mr_q 0:d8f2f7d5f31b 501 clen = pbuf_clen(p);
mr_q 0:d8f2f7d5f31b 502 if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
mr_q 0:d8f2f7d5f31b 503 #if IP_REASS_FREE_OLDEST
mr_q 0:d8f2f7d5f31b 504 if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
mr_q 0:d8f2f7d5f31b 505 ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))
mr_q 0:d8f2f7d5f31b 506 #endif /* IP_REASS_FREE_OLDEST */
mr_q 0:d8f2f7d5f31b 507 {
mr_q 0:d8f2f7d5f31b 508 /* No datagram could be freed and still too many pbufs enqueued */
mr_q 0:d8f2f7d5f31b 509 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
mr_q 0:d8f2f7d5f31b 510 ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
mr_q 0:d8f2f7d5f31b 511 IPFRAG_STATS_INC(ip_frag.memerr);
mr_q 0:d8f2f7d5f31b 512 /* @todo: send ICMP time exceeded here? */
mr_q 0:d8f2f7d5f31b 513 /* drop this pbuf */
mr_q 0:d8f2f7d5f31b 514 goto nullreturn;
mr_q 0:d8f2f7d5f31b 515 }
mr_q 0:d8f2f7d5f31b 516 }
mr_q 0:d8f2f7d5f31b 517
mr_q 0:d8f2f7d5f31b 518 /* Look for the datagram the fragment belongs to in the current datagram queue,
mr_q 0:d8f2f7d5f31b 519 * remembering the previous in the queue for later dequeueing. */
mr_q 0:d8f2f7d5f31b 520 for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
mr_q 0:d8f2f7d5f31b 521 /* Check if the incoming fragment matches the one currently present
mr_q 0:d8f2f7d5f31b 522 in the reassembly buffer. If so, we proceed with copying the
mr_q 0:d8f2f7d5f31b 523 fragment into the buffer. */
mr_q 0:d8f2f7d5f31b 524 if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
mr_q 0:d8f2f7d5f31b 525 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching previous fragment ID=%"X16_F"\n",
mr_q 0:d8f2f7d5f31b 526 ntohs(IPH_ID(fraghdr))));
mr_q 0:d8f2f7d5f31b 527 IPFRAG_STATS_INC(ip_frag.cachehit);
mr_q 0:d8f2f7d5f31b 528 break;
mr_q 0:d8f2f7d5f31b 529 }
mr_q 0:d8f2f7d5f31b 530 ipr_prev = ipr;
mr_q 0:d8f2f7d5f31b 531 }
mr_q 0:d8f2f7d5f31b 532
mr_q 0:d8f2f7d5f31b 533 if (ipr == NULL) {
mr_q 0:d8f2f7d5f31b 534 /* Enqueue a new datagram into the datagram queue */
mr_q 0:d8f2f7d5f31b 535 ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
mr_q 0:d8f2f7d5f31b 536 /* Bail if unable to enqueue */
mr_q 0:d8f2f7d5f31b 537 if(ipr == NULL) {
mr_q 0:d8f2f7d5f31b 538 goto nullreturn;
mr_q 0:d8f2f7d5f31b 539 }
mr_q 0:d8f2f7d5f31b 540 } else {
mr_q 0:d8f2f7d5f31b 541 if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
mr_q 0:d8f2f7d5f31b 542 ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
mr_q 0:d8f2f7d5f31b 543 /* ipr->iphdr is not the header from the first fragment, but fraghdr is
mr_q 0:d8f2f7d5f31b 544 * -> copy fraghdr into ipr->iphdr since we want to have the header
mr_q 0:d8f2f7d5f31b 545 * of the first fragment (for ICMP time exceeded and later, for copying
mr_q 0:d8f2f7d5f31b 546 * all options, if supported)*/
mr_q 0:d8f2f7d5f31b 547 SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
mr_q 0:d8f2f7d5f31b 548 }
mr_q 0:d8f2f7d5f31b 549 }
mr_q 0:d8f2f7d5f31b 550 /* Track the current number of pbufs current 'in-flight', in order to limit
mr_q 0:d8f2f7d5f31b 551 the number of fragments that may be enqueued at any one time */
mr_q 0:d8f2f7d5f31b 552 ip_reass_pbufcount += clen;
mr_q 0:d8f2f7d5f31b 553
mr_q 0:d8f2f7d5f31b 554 /* At this point, we have either created a new entry or pointing
mr_q 0:d8f2f7d5f31b 555 * to an existing one */
mr_q 0:d8f2f7d5f31b 556
mr_q 0:d8f2f7d5f31b 557 /* check for 'no more fragments', and update queue entry*/
mr_q 0:d8f2f7d5f31b 558 if ((IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0) {
mr_q 0:d8f2f7d5f31b 559 ipr->flags |= IP_REASS_FLAG_LASTFRAG;
mr_q 0:d8f2f7d5f31b 560 ipr->datagram_len = offset + len;
mr_q 0:d8f2f7d5f31b 561 LWIP_DEBUGF(IP_REASS_DEBUG,
mr_q 0:d8f2f7d5f31b 562 ("ip_reass: last fragment seen, total len %"S16_F"\n",
mr_q 0:d8f2f7d5f31b 563 ipr->datagram_len));
mr_q 0:d8f2f7d5f31b 564 }
mr_q 0:d8f2f7d5f31b 565 /* find the right place to insert this pbuf */
mr_q 0:d8f2f7d5f31b 566 /* @todo: trim pbufs if fragments are overlapping */
mr_q 0:d8f2f7d5f31b 567 if (ip_reass_chain_frag_into_datagram_and_validate(ipr, p)) {
mr_q 0:d8f2f7d5f31b 568 /* the totally last fragment (flag more fragments = 0) was received at least
mr_q 0:d8f2f7d5f31b 569 * once AND all fragments are received */
mr_q 0:d8f2f7d5f31b 570 ipr->datagram_len += IP_HLEN;
mr_q 0:d8f2f7d5f31b 571
mr_q 0:d8f2f7d5f31b 572 /* save the second pbuf before copying the header over the pointer */
mr_q 0:d8f2f7d5f31b 573 r = ((struct ip_reass_helper*)ipr->p->payload)->next_pbuf;
mr_q 0:d8f2f7d5f31b 574
mr_q 0:d8f2f7d5f31b 575 /* copy the original ip header back to the first pbuf */
mr_q 0:d8f2f7d5f31b 576 fraghdr = (struct ip_hdr*)(ipr->p->payload);
mr_q 0:d8f2f7d5f31b 577 SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
mr_q 0:d8f2f7d5f31b 578 IPH_LEN_SET(fraghdr, htons(ipr->datagram_len));
mr_q 0:d8f2f7d5f31b 579 IPH_OFFSET_SET(fraghdr, 0);
mr_q 0:d8f2f7d5f31b 580 IPH_CHKSUM_SET(fraghdr, 0);
mr_q 0:d8f2f7d5f31b 581 /* @todo: do we need to set calculate the correct checksum? */
mr_q 0:d8f2f7d5f31b 582 IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
mr_q 0:d8f2f7d5f31b 583
mr_q 0:d8f2f7d5f31b 584 p = ipr->p;
mr_q 0:d8f2f7d5f31b 585
mr_q 0:d8f2f7d5f31b 586 /* chain together the pbufs contained within the reass_data list. */
mr_q 0:d8f2f7d5f31b 587 while(r != NULL) {
mr_q 0:d8f2f7d5f31b 588 iprh = (struct ip_reass_helper*)r->payload;
mr_q 0:d8f2f7d5f31b 589
mr_q 0:d8f2f7d5f31b 590 /* hide the ip header for every succeding fragment */
mr_q 0:d8f2f7d5f31b 591 pbuf_header(r, -IP_HLEN);
mr_q 0:d8f2f7d5f31b 592 pbuf_cat(p, r);
mr_q 0:d8f2f7d5f31b 593 r = iprh->next_pbuf;
mr_q 0:d8f2f7d5f31b 594 }
mr_q 0:d8f2f7d5f31b 595 /* release the sources allocate for the fragment queue entry */
mr_q 0:d8f2f7d5f31b 596 ip_reass_dequeue_datagram(ipr, ipr_prev);
mr_q 0:d8f2f7d5f31b 597
mr_q 0:d8f2f7d5f31b 598 /* and adjust the number of pbufs currently queued for reassembly. */
mr_q 0:d8f2f7d5f31b 599 ip_reass_pbufcount -= pbuf_clen(p);
mr_q 0:d8f2f7d5f31b 600
mr_q 0:d8f2f7d5f31b 601 /* Return the pbuf chain */
mr_q 0:d8f2f7d5f31b 602 return p;
mr_q 0:d8f2f7d5f31b 603 }
mr_q 0:d8f2f7d5f31b 604 /* the datagram is not (yet?) reassembled completely */
mr_q 0:d8f2f7d5f31b 605 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
mr_q 0:d8f2f7d5f31b 606 return NULL;
mr_q 0:d8f2f7d5f31b 607
mr_q 0:d8f2f7d5f31b 608 nullreturn:
mr_q 0:d8f2f7d5f31b 609 LWIP_DEBUGF(IP_REASS_DEBUG,("ip_reass: nullreturn\n"));
mr_q 0:d8f2f7d5f31b 610 IPFRAG_STATS_INC(ip_frag.drop);
mr_q 0:d8f2f7d5f31b 611 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 612 return NULL;
mr_q 0:d8f2f7d5f31b 613 }
mr_q 0:d8f2f7d5f31b 614 #endif /* IP_REASSEMBLY */
mr_q 0:d8f2f7d5f31b 615
mr_q 0:d8f2f7d5f31b 616 #if IP_FRAG
mr_q 0:d8f2f7d5f31b 617 #if IP_FRAG_USES_STATIC_BUF
mr_q 0:d8f2f7d5f31b 618 static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)];
mr_q 0:d8f2f7d5f31b 619 #else /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 620
mr_q 0:d8f2f7d5f31b 621 #if !LWIP_NETIF_TX_SINGLE_PBUF
mr_q 0:d8f2f7d5f31b 622 /** Allocate a new struct pbuf_custom_ref */
mr_q 0:d8f2f7d5f31b 623 static struct pbuf_custom_ref*
mr_q 0:d8f2f7d5f31b 624 ip_frag_alloc_pbuf_custom_ref(void)
mr_q 0:d8f2f7d5f31b 625 {
mr_q 0:d8f2f7d5f31b 626 return (struct pbuf_custom_ref*)memp_malloc(MEMP_FRAG_PBUF);
mr_q 0:d8f2f7d5f31b 627 }
mr_q 0:d8f2f7d5f31b 628
mr_q 0:d8f2f7d5f31b 629 /** Free a struct pbuf_custom_ref */
mr_q 0:d8f2f7d5f31b 630 static void
mr_q 0:d8f2f7d5f31b 631 ip_frag_free_pbuf_custom_ref(struct pbuf_custom_ref* p)
mr_q 0:d8f2f7d5f31b 632 {
mr_q 0:d8f2f7d5f31b 633 LWIP_ASSERT("p != NULL", p != NULL);
mr_q 0:d8f2f7d5f31b 634 memp_free(MEMP_FRAG_PBUF, p);
mr_q 0:d8f2f7d5f31b 635 }
mr_q 0:d8f2f7d5f31b 636
mr_q 0:d8f2f7d5f31b 637 /** Free-callback function to free a 'struct pbuf_custom_ref', called by
mr_q 0:d8f2f7d5f31b 638 * pbuf_free. */
mr_q 0:d8f2f7d5f31b 639 static void
mr_q 0:d8f2f7d5f31b 640 ipfrag_free_pbuf_custom(struct pbuf *p)
mr_q 0:d8f2f7d5f31b 641 {
mr_q 0:d8f2f7d5f31b 642 struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref*)p;
mr_q 0:d8f2f7d5f31b 643 LWIP_ASSERT("pcr != NULL", pcr != NULL);
mr_q 0:d8f2f7d5f31b 644 LWIP_ASSERT("pcr == p", (void*)pcr == (void*)p);
mr_q 0:d8f2f7d5f31b 645 if (pcr->original != NULL) {
mr_q 0:d8f2f7d5f31b 646 pbuf_free(pcr->original);
mr_q 0:d8f2f7d5f31b 647 }
mr_q 0:d8f2f7d5f31b 648 ip_frag_free_pbuf_custom_ref(pcr);
mr_q 0:d8f2f7d5f31b 649 }
mr_q 0:d8f2f7d5f31b 650 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
mr_q 0:d8f2f7d5f31b 651 #endif /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 652
mr_q 0:d8f2f7d5f31b 653 /**
mr_q 0:d8f2f7d5f31b 654 * Fragment an IP datagram if too large for the netif.
mr_q 0:d8f2f7d5f31b 655 *
mr_q 0:d8f2f7d5f31b 656 * Chop the datagram in MTU sized chunks and send them in order
mr_q 0:d8f2f7d5f31b 657 * by using a fixed size static memory buffer (PBUF_REF) or
mr_q 0:d8f2f7d5f31b 658 * point PBUF_REFs into p (depending on IP_FRAG_USES_STATIC_BUF).
mr_q 0:d8f2f7d5f31b 659 *
mr_q 0:d8f2f7d5f31b 660 * @param p ip packet to send
mr_q 0:d8f2f7d5f31b 661 * @param netif the netif on which to send
mr_q 0:d8f2f7d5f31b 662 * @param dest destination ip address to which to send
mr_q 0:d8f2f7d5f31b 663 *
mr_q 0:d8f2f7d5f31b 664 * @return ERR_OK if sent successfully, err_t otherwise
mr_q 0:d8f2f7d5f31b 665 */
mr_q 0:d8f2f7d5f31b 666 err_t
mr_q 0:d8f2f7d5f31b 667 ip_frag(struct pbuf *p, struct netif *netif, ip_addr_t *dest)
mr_q 0:d8f2f7d5f31b 668 {
mr_q 0:d8f2f7d5f31b 669 struct pbuf *rambuf;
mr_q 0:d8f2f7d5f31b 670 #if IP_FRAG_USES_STATIC_BUF
mr_q 0:d8f2f7d5f31b 671 struct pbuf *header;
mr_q 0:d8f2f7d5f31b 672 #else
mr_q 0:d8f2f7d5f31b 673 #if !LWIP_NETIF_TX_SINGLE_PBUF
mr_q 0:d8f2f7d5f31b 674 struct pbuf *newpbuf;
mr_q 0:d8f2f7d5f31b 675 #endif
mr_q 0:d8f2f7d5f31b 676 struct ip_hdr *original_iphdr;
mr_q 0:d8f2f7d5f31b 677 #endif
mr_q 0:d8f2f7d5f31b 678 struct ip_hdr *iphdr;
mr_q 0:d8f2f7d5f31b 679 u16_t nfb;
mr_q 0:d8f2f7d5f31b 680 u16_t left, cop;
mr_q 0:d8f2f7d5f31b 681 u16_t mtu = netif->mtu;
mr_q 0:d8f2f7d5f31b 682 u16_t ofo, omf;
mr_q 0:d8f2f7d5f31b 683 u16_t last;
mr_q 0:d8f2f7d5f31b 684 u16_t poff = IP_HLEN;
mr_q 0:d8f2f7d5f31b 685 u16_t tmp;
mr_q 0:d8f2f7d5f31b 686 #if !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF
mr_q 0:d8f2f7d5f31b 687 u16_t newpbuflen = 0;
mr_q 0:d8f2f7d5f31b 688 u16_t left_to_copy;
mr_q 0:d8f2f7d5f31b 689 #endif
mr_q 0:d8f2f7d5f31b 690
mr_q 0:d8f2f7d5f31b 691 /* Get a RAM based MTU sized pbuf */
mr_q 0:d8f2f7d5f31b 692 #if IP_FRAG_USES_STATIC_BUF
mr_q 0:d8f2f7d5f31b 693 /* When using a static buffer, we use a PBUF_REF, which we will
mr_q 0:d8f2f7d5f31b 694 * use to reference the packet (without link header).
mr_q 0:d8f2f7d5f31b 695 * Layer and length is irrelevant.
mr_q 0:d8f2f7d5f31b 696 */
mr_q 0:d8f2f7d5f31b 697 rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
mr_q 0:d8f2f7d5f31b 698 if (rambuf == NULL) {
mr_q 0:d8f2f7d5f31b 699 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
mr_q 0:d8f2f7d5f31b 700 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 701 }
mr_q 0:d8f2f7d5f31b 702 rambuf->tot_len = rambuf->len = mtu;
mr_q 0:d8f2f7d5f31b 703 rambuf->payload = LWIP_MEM_ALIGN((void *)buf);
mr_q 0:d8f2f7d5f31b 704
mr_q 0:d8f2f7d5f31b 705 /* Copy the IP header in it */
mr_q 0:d8f2f7d5f31b 706 iphdr = (struct ip_hdr *)rambuf->payload;
mr_q 0:d8f2f7d5f31b 707 SMEMCPY(iphdr, p->payload, IP_HLEN);
mr_q 0:d8f2f7d5f31b 708 #else /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 709 original_iphdr = (struct ip_hdr *)p->payload;
mr_q 0:d8f2f7d5f31b 710 iphdr = original_iphdr;
mr_q 0:d8f2f7d5f31b 711 #endif /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 712
mr_q 0:d8f2f7d5f31b 713 /* Save original offset */
mr_q 0:d8f2f7d5f31b 714 tmp = ntohs(IPH_OFFSET(iphdr));
mr_q 0:d8f2f7d5f31b 715 ofo = tmp & IP_OFFMASK;
mr_q 0:d8f2f7d5f31b 716 omf = tmp & IP_MF;
mr_q 0:d8f2f7d5f31b 717
mr_q 0:d8f2f7d5f31b 718 left = p->tot_len - IP_HLEN;
mr_q 0:d8f2f7d5f31b 719
mr_q 0:d8f2f7d5f31b 720 nfb = (mtu - IP_HLEN) / 8;
mr_q 0:d8f2f7d5f31b 721
mr_q 0:d8f2f7d5f31b 722 while (left) {
mr_q 0:d8f2f7d5f31b 723 last = (left <= mtu - IP_HLEN);
mr_q 0:d8f2f7d5f31b 724
mr_q 0:d8f2f7d5f31b 725 /* Set new offset and MF flag */
mr_q 0:d8f2f7d5f31b 726 tmp = omf | (IP_OFFMASK & (ofo));
mr_q 0:d8f2f7d5f31b 727 if (!last) {
mr_q 0:d8f2f7d5f31b 728 tmp = tmp | IP_MF;
mr_q 0:d8f2f7d5f31b 729 }
mr_q 0:d8f2f7d5f31b 730
mr_q 0:d8f2f7d5f31b 731 /* Fill this fragment */
mr_q 0:d8f2f7d5f31b 732 cop = last ? left : nfb * 8;
mr_q 0:d8f2f7d5f31b 733
mr_q 0:d8f2f7d5f31b 734 #if IP_FRAG_USES_STATIC_BUF
mr_q 0:d8f2f7d5f31b 735 poff += pbuf_copy_partial(p, (u8_t*)iphdr + IP_HLEN, cop, poff);
mr_q 0:d8f2f7d5f31b 736 #else /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 737 #if LWIP_NETIF_TX_SINGLE_PBUF
mr_q 0:d8f2f7d5f31b 738 rambuf = pbuf_alloc(PBUF_IP, cop, PBUF_RAM);
mr_q 0:d8f2f7d5f31b 739 if (rambuf == NULL) {
mr_q 0:d8f2f7d5f31b 740 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 741 }
mr_q 0:d8f2f7d5f31b 742 LWIP_ASSERT("this needs a pbuf in one piece!",
mr_q 0:d8f2f7d5f31b 743 (rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
mr_q 0:d8f2f7d5f31b 744 poff += pbuf_copy_partial(p, rambuf->payload, cop, poff);
mr_q 0:d8f2f7d5f31b 745 /* make room for the IP header */
mr_q 0:d8f2f7d5f31b 746 if(pbuf_header(rambuf, IP_HLEN)) {
mr_q 0:d8f2f7d5f31b 747 pbuf_free(rambuf);
mr_q 0:d8f2f7d5f31b 748 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 749 }
mr_q 0:d8f2f7d5f31b 750 /* fill in the IP header */
mr_q 0:d8f2f7d5f31b 751 SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
mr_q 0:d8f2f7d5f31b 752 iphdr = rambuf->payload;
mr_q 0:d8f2f7d5f31b 753 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
mr_q 0:d8f2f7d5f31b 754 /* When not using a static buffer, create a chain of pbufs.
mr_q 0:d8f2f7d5f31b 755 * The first will be a PBUF_RAM holding the link and IP header.
mr_q 0:d8f2f7d5f31b 756 * The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
mr_q 0:d8f2f7d5f31b 757 * but limited to the size of an mtu.
mr_q 0:d8f2f7d5f31b 758 */
mr_q 0:d8f2f7d5f31b 759 rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
mr_q 0:d8f2f7d5f31b 760 if (rambuf == NULL) {
mr_q 0:d8f2f7d5f31b 761 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 762 }
mr_q 0:d8f2f7d5f31b 763 LWIP_ASSERT("this needs a pbuf in one piece!",
mr_q 0:d8f2f7d5f31b 764 (p->len >= (IP_HLEN)));
mr_q 0:d8f2f7d5f31b 765 SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
mr_q 0:d8f2f7d5f31b 766 iphdr = (struct ip_hdr *)rambuf->payload;
mr_q 0:d8f2f7d5f31b 767
mr_q 0:d8f2f7d5f31b 768 /* Can just adjust p directly for needed offset. */
mr_q 0:d8f2f7d5f31b 769 p->payload = (u8_t *)p->payload + poff;
mr_q 0:d8f2f7d5f31b 770 p->len -= poff;
mr_q 0:d8f2f7d5f31b 771
mr_q 0:d8f2f7d5f31b 772 left_to_copy = cop;
mr_q 0:d8f2f7d5f31b 773 while (left_to_copy) {
mr_q 0:d8f2f7d5f31b 774 struct pbuf_custom_ref *pcr;
mr_q 0:d8f2f7d5f31b 775 newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
mr_q 0:d8f2f7d5f31b 776 /* Is this pbuf already empty? */
mr_q 0:d8f2f7d5f31b 777 if (!newpbuflen) {
mr_q 0:d8f2f7d5f31b 778 p = p->next;
mr_q 0:d8f2f7d5f31b 779 continue;
mr_q 0:d8f2f7d5f31b 780 }
mr_q 0:d8f2f7d5f31b 781 pcr = ip_frag_alloc_pbuf_custom_ref();
mr_q 0:d8f2f7d5f31b 782 if (pcr == NULL) {
mr_q 0:d8f2f7d5f31b 783 pbuf_free(rambuf);
mr_q 0:d8f2f7d5f31b 784 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 785 }
mr_q 0:d8f2f7d5f31b 786 /* Mirror this pbuf, although we might not need all of it. */
mr_q 0:d8f2f7d5f31b 787 newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc, p->payload, newpbuflen);
mr_q 0:d8f2f7d5f31b 788 if (newpbuf == NULL) {
mr_q 0:d8f2f7d5f31b 789 ip_frag_free_pbuf_custom_ref(pcr);
mr_q 0:d8f2f7d5f31b 790 pbuf_free(rambuf);
mr_q 0:d8f2f7d5f31b 791 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 792 }
mr_q 0:d8f2f7d5f31b 793 pbuf_ref(p);
mr_q 0:d8f2f7d5f31b 794 pcr->original = p;
mr_q 0:d8f2f7d5f31b 795 pcr->pc.custom_free_function = ipfrag_free_pbuf_custom;
mr_q 0:d8f2f7d5f31b 796
mr_q 0:d8f2f7d5f31b 797 /* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
mr_q 0:d8f2f7d5f31b 798 * so that it is removed when pbuf_dechain is later called on rambuf.
mr_q 0:d8f2f7d5f31b 799 */
mr_q 0:d8f2f7d5f31b 800 pbuf_cat(rambuf, newpbuf);
mr_q 0:d8f2f7d5f31b 801 left_to_copy -= newpbuflen;
mr_q 0:d8f2f7d5f31b 802 if (left_to_copy) {
mr_q 0:d8f2f7d5f31b 803 p = p->next;
mr_q 0:d8f2f7d5f31b 804 }
mr_q 0:d8f2f7d5f31b 805 }
mr_q 0:d8f2f7d5f31b 806 poff = newpbuflen;
mr_q 0:d8f2f7d5f31b 807 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
mr_q 0:d8f2f7d5f31b 808 #endif /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 809
mr_q 0:d8f2f7d5f31b 810 /* Correct header */
mr_q 0:d8f2f7d5f31b 811 IPH_OFFSET_SET(iphdr, htons(tmp));
mr_q 0:d8f2f7d5f31b 812 IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
mr_q 0:d8f2f7d5f31b 813 IPH_CHKSUM_SET(iphdr, 0);
mr_q 0:d8f2f7d5f31b 814 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
mr_q 0:d8f2f7d5f31b 815
mr_q 0:d8f2f7d5f31b 816 #if IP_FRAG_USES_STATIC_BUF
mr_q 0:d8f2f7d5f31b 817 if (last) {
mr_q 0:d8f2f7d5f31b 818 pbuf_realloc(rambuf, left + IP_HLEN);
mr_q 0:d8f2f7d5f31b 819 }
mr_q 0:d8f2f7d5f31b 820
mr_q 0:d8f2f7d5f31b 821 /* This part is ugly: we alloc a RAM based pbuf for
mr_q 0:d8f2f7d5f31b 822 * the link level header for each chunk and then
mr_q 0:d8f2f7d5f31b 823 * free it.A PBUF_ROM style pbuf for which pbuf_header
mr_q 0:d8f2f7d5f31b 824 * worked would make things simpler.
mr_q 0:d8f2f7d5f31b 825 */
mr_q 0:d8f2f7d5f31b 826 header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
mr_q 0:d8f2f7d5f31b 827 if (header != NULL) {
mr_q 0:d8f2f7d5f31b 828 pbuf_chain(header, rambuf);
mr_q 0:d8f2f7d5f31b 829 netif->output(netif, header, dest);
mr_q 0:d8f2f7d5f31b 830 IPFRAG_STATS_INC(ip_frag.xmit);
mr_q 0:d8f2f7d5f31b 831 snmp_inc_ipfragcreates();
mr_q 0:d8f2f7d5f31b 832 pbuf_free(header);
mr_q 0:d8f2f7d5f31b 833 } else {
mr_q 0:d8f2f7d5f31b 834 LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc() for header failed\n"));
mr_q 0:d8f2f7d5f31b 835 pbuf_free(rambuf);
mr_q 0:d8f2f7d5f31b 836 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 837 }
mr_q 0:d8f2f7d5f31b 838 #else /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 839 /* No need for separate header pbuf - we allowed room for it in rambuf
mr_q 0:d8f2f7d5f31b 840 * when allocated.
mr_q 0:d8f2f7d5f31b 841 */
mr_q 0:d8f2f7d5f31b 842 netif->output(netif, rambuf, dest);
mr_q 0:d8f2f7d5f31b 843 IPFRAG_STATS_INC(ip_frag.xmit);
mr_q 0:d8f2f7d5f31b 844
mr_q 0:d8f2f7d5f31b 845 /* Unfortunately we can't reuse rambuf - the hardware may still be
mr_q 0:d8f2f7d5f31b 846 * using the buffer. Instead we free it (and the ensuing chain) and
mr_q 0:d8f2f7d5f31b 847 * recreate it next time round the loop. If we're lucky the hardware
mr_q 0:d8f2f7d5f31b 848 * will have already sent the packet, the free will really free, and
mr_q 0:d8f2f7d5f31b 849 * there will be zero memory penalty.
mr_q 0:d8f2f7d5f31b 850 */
mr_q 0:d8f2f7d5f31b 851
mr_q 0:d8f2f7d5f31b 852 pbuf_free(rambuf);
mr_q 0:d8f2f7d5f31b 853 #endif /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 854 left -= cop;
mr_q 0:d8f2f7d5f31b 855 ofo += nfb;
mr_q 0:d8f2f7d5f31b 856 }
mr_q 0:d8f2f7d5f31b 857 #if IP_FRAG_USES_STATIC_BUF
mr_q 0:d8f2f7d5f31b 858 pbuf_free(rambuf);
mr_q 0:d8f2f7d5f31b 859 #endif /* IP_FRAG_USES_STATIC_BUF */
mr_q 0:d8f2f7d5f31b 860 snmp_inc_ipfragoks();
mr_q 0:d8f2f7d5f31b 861 return ERR_OK;
mr_q 0:d8f2f7d5f31b 862 }
mr_q 0:d8f2f7d5f31b 863 #endif /* IP_FRAG */