SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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