Rod Coleman / NetServSB2
Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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