Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

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