Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
GordonSin
Date:
Fri May 31 04:09:54 2013 +0000
Revision:
0:0ed2a7c7190c
31/5/2013;

Who changed what in which revision?

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