NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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