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