Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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