Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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