Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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