Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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