I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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