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