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