2004-05-05 13:28:44 +00:00
|
|
|
/* @file
|
|
|
|
*
|
|
|
|
* This is the IP packet segmentation and reassembly implementation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-11-22 15:46:50 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2002-11-22 15:46:50 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Jani Monoses <jani@iv.ro>
|
|
|
|
* original reassembly code by Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-01-24 21:05:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-11-22 15:46:50 +00:00
|
|
|
#include "lwip/ip.h"
|
|
|
|
#include "lwip/ip_frag.h"
|
|
|
|
#include "lwip/netif.h"
|
2006-08-11 14:12:05 +00:00
|
|
|
#include "lwip/snmp.h"
|
2003-02-06 22:18:56 +00:00
|
|
|
#include "lwip/stats.h"
|
2002-11-22 15:46:50 +00:00
|
|
|
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_REASSEMBLY
|
2006-07-04 08:24:17 +00:00
|
|
|
static u8_t ip_reassbuf[IP_HLEN + IP_REASS_BUFSIZE];
|
|
|
|
static u8_t ip_reassbitmap[IP_REASS_BUFSIZE / (8 * 8) + 1];
|
|
|
|
static const u8_t bitmap_bits[8] = { 0xff, 0x7f, 0x3f, 0x1f,
|
|
|
|
0x0f, 0x07, 0x03, 0x01
|
|
|
|
};
|
|
|
|
static u16_t ip_reasslen;
|
|
|
|
static u8_t ip_reassflags;
|
|
|
|
#define IP_REASS_FLAG_LASTFRAG 0x01
|
|
|
|
|
|
|
|
static u8_t ip_reasstmr;
|
2007-06-03 18:36:42 +00:00
|
|
|
#endif /* IP_REASSEMBLY */
|
2002-11-22 15:46:50 +00:00
|
|
|
|
2007-06-08 22:20:59 +00:00
|
|
|
#if IP_REASSEMBLY || (IP_FRAG && IP_FRAG_USES_STATIC_BUF)
|
2002-11-22 15:46:50 +00:00
|
|
|
/*
|
|
|
|
* Copy len bytes from offset in pbuf to buffer
|
|
|
|
*
|
|
|
|
* helper used by both ip_reass and ip_frag
|
2007-06-08 12:08:44 +00:00
|
|
|
*
|
|
|
|
* @param p pbuf chain to copy from
|
|
|
|
* @param offset offset in bytes into the pbuf chain which should be skipped
|
|
|
|
* before starting to copy
|
|
|
|
* @param buffer destination to copy the data
|
|
|
|
* @param len number of bytes to copy (starting from offset)
|
|
|
|
* @return pointer to one pbuf in the chain p from which copied last
|
2002-11-22 15:46:50 +00:00
|
|
|
*/
|
|
|
|
static struct pbuf *
|
2007-06-08 12:08:44 +00:00
|
|
|
copy_from_pbuf(struct pbuf *p, u16_t *offset, u8_t *buffer, u16_t len)
|
2002-11-22 15:46:50 +00:00
|
|
|
{
|
|
|
|
u16_t l;
|
|
|
|
|
|
|
|
p->payload = (u8_t *)p->payload + *offset;
|
|
|
|
p->len -= *offset;
|
|
|
|
while (len) {
|
|
|
|
l = len < p->len ? len : p->len;
|
2007-05-10 05:20:05 +00:00
|
|
|
MEMCPY(buffer, p->payload, l);
|
2002-11-22 15:46:50 +00:00
|
|
|
buffer += l;
|
|
|
|
len -= l;
|
|
|
|
if (len)
|
|
|
|
p = p->next;
|
|
|
|
else
|
|
|
|
*offset = l;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2007-06-03 18:36:42 +00:00
|
|
|
#endif /* IP_REASSEMBLY || IP_FRAG_USES_STATIC_BUF */
|
2002-11-22 15:46:50 +00:00
|
|
|
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_REASSEMBLY
|
2006-07-04 08:24:17 +00:00
|
|
|
/**
|
2007-06-03 18:36:42 +00:00
|
|
|
* Initializes IP reassembly states.
|
2006-07-04 08:24:17 +00:00
|
|
|
*/
|
|
|
|
void
|
2007-06-03 18:36:42 +00:00
|
|
|
ip_reass_init(void)
|
2006-07-04 08:24:17 +00:00
|
|
|
{
|
|
|
|
ip_reasstmr = 0;
|
|
|
|
ip_reassflags = 0;
|
|
|
|
ip_reasslen = 0;
|
|
|
|
memset(ip_reassbitmap, 0, sizeof(ip_reassbitmap));
|
|
|
|
}
|
2002-11-22 15:46:50 +00:00
|
|
|
|
2006-02-13 08:12:07 +00:00
|
|
|
/**
|
|
|
|
* Reassembly timer base function
|
|
|
|
* for both NO_SYS == 0 and 1 (!).
|
|
|
|
*
|
|
|
|
* Should be called every 1000 msec.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ip_reass_tmr(void)
|
2002-11-22 15:46:50 +00:00
|
|
|
{
|
2006-02-13 08:12:07 +00:00
|
|
|
if (ip_reasstmr > 0) {
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr--;
|
2006-07-04 08:24:17 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n",(u16_t)ip_reasstmr));
|
2006-08-11 14:12:05 +00:00
|
|
|
if (ip_reasstmr == 0) {
|
|
|
|
/* reassembly timed out */
|
|
|
|
snmp_inc_ipreasmfails();
|
|
|
|
}
|
2006-02-13 08:12:07 +00:00
|
|
|
}
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
2006-02-13 08:12:07 +00:00
|
|
|
/**
|
|
|
|
* Reassembles incoming IP fragments into an IP datagram.
|
|
|
|
*
|
|
|
|
* @param p points to a pbuf chain of the fragment
|
|
|
|
* @return NULL if reassembly is incomplete, ? otherwise
|
|
|
|
*/
|
2002-11-22 15:46:50 +00:00
|
|
|
struct pbuf *
|
|
|
|
ip_reass(struct pbuf *p)
|
|
|
|
{
|
|
|
|
struct pbuf *q;
|
|
|
|
struct ip_hdr *fraghdr, *iphdr;
|
|
|
|
u16_t offset, len;
|
|
|
|
u16_t i;
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
IPFRAG_STATS_INC(ip_frag.recv);
|
2006-08-11 14:12:05 +00:00
|
|
|
snmp_inc_ipreasmreqds();
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2002-11-22 15:46:50 +00:00
|
|
|
iphdr = (struct ip_hdr *) ip_reassbuf;
|
|
|
|
fraghdr = (struct ip_hdr *) p->payload;
|
|
|
|
/* If ip_reasstmr is zero, no packet is present in the buffer, so we
|
|
|
|
write the IP header of the fragment into the reassembly
|
|
|
|
buffer. The timer is updated with the maximum age. */
|
|
|
|
if (ip_reasstmr == 0) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: new packet\n"));
|
2007-05-10 05:20:05 +00:00
|
|
|
SMEMCPY(iphdr, fraghdr, IP_HLEN);
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr = IP_REASS_MAXAGE;
|
|
|
|
ip_reassflags = 0;
|
|
|
|
/* Clear the bitmap. */
|
2002-12-18 10:40:01 +00:00
|
|
|
memset(ip_reassbitmap, 0, sizeof(ip_reassbitmap));
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the incoming fragment matches the one currently present
|
|
|
|
in the reasembly buffer. If so, we proceed with copying the
|
|
|
|
fragment into the buffer. */
|
|
|
|
if (ip_addr_cmp(&iphdr->src, &fraghdr->src) &&
|
|
|
|
ip_addr_cmp(&iphdr->dest, &fraghdr->dest) &&
|
|
|
|
IPH_ID(iphdr) == IPH_ID(fraghdr)) {
|
2006-02-13 08:12:07 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching previous fragment ID=%"X16_F"\n",
|
|
|
|
ntohs(IPH_ID(fraghdr))));
|
2003-11-14 13:17:23 +00:00
|
|
|
IPFRAG_STATS_INC(ip_frag.cachehit);
|
2002-11-22 15:46:50 +00:00
|
|
|
/* Find out the offset in the reassembly buffer where we should
|
|
|
|
copy the fragment. */
|
|
|
|
len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
|
|
|
|
offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
|
|
|
|
|
|
|
|
/* If the offset or the offset + fragment length overflows the
|
|
|
|
reassembly buffer, we discard the entire packet. */
|
2006-07-04 08:24:17 +00:00
|
|
|
if ((offset > IP_REASS_BUFSIZE) || ((offset + len) > IP_REASS_BUFSIZE)) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
2005-11-25 12:03:38 +00:00
|
|
|
("ip_reass: fragment outside of buffer (%"S16_F":%"S16_F"/%"S16_F").\n", offset,
|
2003-06-09 21:14:47 +00:00
|
|
|
offset + len, IP_REASS_BUFSIZE));
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr = 0;
|
2006-08-11 14:12:05 +00:00
|
|
|
snmp_inc_ipreasmfails();
|
2002-11-22 15:46:50 +00:00
|
|
|
goto nullreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the fragment into the reassembly buffer, at the right
|
|
|
|
offset. */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
2005-11-25 12:03:38 +00:00
|
|
|
("ip_reass: copying with offset %"S16_F" into %"S16_F":%"S16_F"\n", offset,
|
2003-06-09 21:14:47 +00:00
|
|
|
IP_HLEN + offset, IP_HLEN + offset + len));
|
2002-11-22 15:46:50 +00:00
|
|
|
i = IPH_HL(fraghdr) * 4;
|
|
|
|
copy_from_pbuf(p, &i, &ip_reassbuf[IP_HLEN + offset], len);
|
|
|
|
|
|
|
|
/* Update the bitmap. */
|
|
|
|
if (offset / (8 * 8) == (offset + len) / (8 * 8)) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
2003-06-09 21:14:47 +00:00
|
|
|
("ip_reass: updating single byte in bitmap.\n"));
|
2006-02-13 08:12:07 +00:00
|
|
|
/* If the two endpoints are in the same byte, we only update that byte. */
|
2006-03-02 15:45:19 +00:00
|
|
|
LWIP_ASSERT("offset / (8 * 8) < sizeof(ip_reassbitmap)",
|
|
|
|
offset / (8 * 8) < sizeof(ip_reassbitmap));
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reassbitmap[offset / (8 * 8)] |=
|
2006-03-02 15:45:19 +00:00
|
|
|
bitmap_bits[(offset / 8) & 7] &
|
|
|
|
~bitmap_bits[((offset + len) / 8) & 7];
|
2002-11-22 15:46:50 +00:00
|
|
|
} else {
|
|
|
|
/* If the two endpoints are in different bytes, we update the
|
|
|
|
bytes in the endpoints and fill the stuff inbetween with
|
|
|
|
0xff. */
|
2006-03-02 15:45:19 +00:00
|
|
|
LWIP_ASSERT("offset / (8 * 8) < sizeof(ip_reassbitmap)",
|
|
|
|
offset / (8 * 8) < sizeof(ip_reassbitmap));
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reassbitmap[offset / (8 * 8)] |= bitmap_bits[(offset / 8) & 7];
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
2005-11-25 12:03:38 +00:00
|
|
|
("ip_reass: updating many bytes in bitmap (%"S16_F":%"S16_F").\n",
|
2003-06-09 21:14:47 +00:00
|
|
|
1 + offset / (8 * 8), (offset + len) / (8 * 8)));
|
2002-11-22 15:46:50 +00:00
|
|
|
for (i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
|
2006-03-02 15:45:19 +00:00
|
|
|
ip_reassbitmap[i] = 0xff;
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
2006-03-02 15:45:19 +00:00
|
|
|
LWIP_ASSERT("(offset + len) / (8 * 8) < sizeof(ip_reassbitmap)",
|
|
|
|
(offset + len) / (8 * 8) < sizeof(ip_reassbitmap));
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reassbitmap[(offset + len) / (8 * 8)] |=
|
2006-03-02 15:45:19 +00:00
|
|
|
~bitmap_bits[((offset + len) / 8) & 7];
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this fragment has the More Fragments flag set to zero, we
|
|
|
|
know that this is the last fragment, so we can calculate the
|
|
|
|
size of the entire packet. We also set the
|
|
|
|
IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
|
|
|
|
the final fragment. */
|
|
|
|
|
|
|
|
if ((ntohs(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {
|
|
|
|
ip_reassflags |= IP_REASS_FLAG_LASTFRAG;
|
|
|
|
ip_reasslen = offset + len;
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
2005-11-25 12:03:38 +00:00
|
|
|
("ip_reass: last fragment seen, total len %"S16_F"\n",
|
2003-06-09 21:14:47 +00:00
|
|
|
ip_reasslen));
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, we check if we have a full packet in the buffer. We do
|
|
|
|
this by checking if we have the last fragment and if all bits
|
|
|
|
in the bitmap are set. */
|
|
|
|
if (ip_reassflags & IP_REASS_FLAG_LASTFRAG) {
|
|
|
|
/* Check all bytes up to and including all but the last byte in
|
|
|
|
the bitmap. */
|
2006-03-02 15:45:19 +00:00
|
|
|
LWIP_ASSERT("ip_reasslen / (8 * 8) - 1 < sizeof(ip_reassbitmap)",
|
|
|
|
ip_reasslen / (8 * 8) - 1 < sizeof(ip_reassbitmap));
|
2002-11-22 15:46:50 +00:00
|
|
|
for (i = 0; i < ip_reasslen / (8 * 8) - 1; ++i) {
|
2006-03-02 15:45:19 +00:00
|
|
|
if (ip_reassbitmap[i] != 0xff) {
|
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: last fragment seen, bitmap %"S16_F"/%"S16_F" failed (%"X16_F")\n",
|
|
|
|
i, ip_reasslen / (8 * 8) - 1, ip_reassbitmap[i]));
|
|
|
|
goto nullreturn;
|
|
|
|
}
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
/* Check the last byte in the bitmap. It should contain just the
|
|
|
|
right amount of bits. */
|
2006-03-02 15:45:19 +00:00
|
|
|
LWIP_ASSERT("ip_reasslen / (8 * 8) < sizeof(ip_reassbitmap)",
|
|
|
|
ip_reasslen / (8 * 8) < sizeof(ip_reassbitmap));
|
2002-11-22 15:46:50 +00:00
|
|
|
if (ip_reassbitmap[ip_reasslen / (8 * 8)] !=
|
2006-03-02 15:45:19 +00:00
|
|
|
(u8_t) ~ bitmap_bits[ip_reasslen / 8 & 7]) {
|
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: last fragment seen, bitmap %"S16_F" didn't contain %"X16_F" (%"X16_F")\n",
|
|
|
|
ip_reasslen / (8 * 8), ~bitmap_bits[ip_reasslen / 8 & 7],
|
|
|
|
ip_reassbitmap[ip_reasslen / (8 * 8)]));
|
|
|
|
goto nullreturn;
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pretend to be a "normal" (i.e., not fragmented) IP packet
|
|
|
|
from now on. */
|
|
|
|
ip_reasslen += IP_HLEN;
|
|
|
|
|
|
|
|
IPH_LEN_SET(iphdr, htons(ip_reasslen));
|
|
|
|
IPH_OFFSET_SET(iphdr, 0);
|
|
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
|
|
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
|
|
|
|
|
|
|
|
/* If we have come this far, we have a full packet in the
|
|
|
|
buffer, so we allocate a pbuf and copy the packet into it. We
|
|
|
|
also reset the timer. */
|
|
|
|
ip_reasstmr = 0;
|
|
|
|
pbuf_free(p);
|
|
|
|
p = pbuf_alloc(PBUF_LINK, ip_reasslen, PBUF_POOL);
|
|
|
|
if (p != NULL) {
|
2006-03-02 15:45:19 +00:00
|
|
|
i = 0;
|
|
|
|
for (q = p; q != NULL; q = q->next) {
|
|
|
|
/* Copy enough bytes to fill this pbuf in the chain. The
|
2006-02-13 08:12:07 +00:00
|
|
|
available data in the pbuf is given by the q->len variable. */
|
2006-03-02 15:45:19 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: memcpy from %p (%"S16_F") to %p, %"S16_F" bytes\n",
|
|
|
|
(void *)&ip_reassbuf[i], i, q->payload,
|
|
|
|
q->len > ip_reasslen - i ? ip_reasslen - i : q->len));
|
2007-05-10 05:20:05 +00:00
|
|
|
MEMCPY(q->payload, &ip_reassbuf[i],
|
2006-03-02 15:45:19 +00:00
|
|
|
q->len > ip_reasslen - i ? ip_reasslen - i : q->len);
|
|
|
|
i += q->len;
|
|
|
|
}
|
|
|
|
IPFRAG_STATS_INC(ip_frag.fw);
|
2006-08-11 14:12:05 +00:00
|
|
|
snmp_inc_ipreasmoks();
|
2003-02-06 22:18:56 +00:00
|
|
|
} else {
|
2006-07-04 08:24:17 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: pbuf_alloc(PBUF_LINK, ip_reasslen=%"U16_F", PBUF_POOL) failed\n", ip_reasslen));
|
2006-03-02 15:45:19 +00:00
|
|
|
IPFRAG_STATS_INC(ip_frag.memerr);
|
2006-08-11 14:12:05 +00:00
|
|
|
snmp_inc_ipreasmfails();
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: p %p\n", (void*)p));
|
2002-11-22 15:46:50 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nullreturn:
|
2003-11-14 13:17:23 +00:00
|
|
|
IPFRAG_STATS_INC(ip_frag.drop);
|
2002-11-22 15:46:50 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-03 18:36:42 +00:00
|
|
|
#endif /* IP_REASSEMBLY */
|
2002-11-22 15:46:50 +00:00
|
|
|
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_FRAG
|
|
|
|
#if IP_FRAG_USES_STATIC_BUF
|
2007-06-13 17:17:26 +00:00
|
|
|
static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU)];
|
2007-06-03 18:36:42 +00:00
|
|
|
#endif /* IP_FRAG_USES_STATIC_BUF */
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-13 08:12:07 +00:00
|
|
|
* Fragment an IP datagram if too large for the netif.
|
2002-11-22 15:46:50 +00:00
|
|
|
*
|
2006-02-13 08:12:07 +00:00
|
|
|
* Chop the datagram in MTU sized chunks and send them in order
|
2007-06-03 18:36:42 +00:00
|
|
|
* by using a fixed size static memory buffer (PBUF_REF) or
|
|
|
|
* point PBUF_REFs into p (depending on IP_FRAG_USES_STATIC_BUF).
|
|
|
|
*
|
|
|
|
* @param p ip packet to send
|
|
|
|
* @param netif the netif on which to send
|
|
|
|
* @param dest destination ip address to which to send
|
|
|
|
*
|
|
|
|
* @return ERR_OK if sent successfully, err_t otherwise
|
2002-11-22 15:46:50 +00:00
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest)
|
|
|
|
{
|
|
|
|
struct pbuf *rambuf;
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_FRAG_USES_STATIC_BUF
|
2002-11-22 15:46:50 +00:00
|
|
|
struct pbuf *header;
|
2007-06-03 18:36:42 +00:00
|
|
|
#else
|
|
|
|
struct pbuf *newpbuf;
|
|
|
|
struct ip_hdr *original_iphdr;
|
|
|
|
#endif
|
2002-11-22 15:46:50 +00:00
|
|
|
struct ip_hdr *iphdr;
|
2007-06-03 18:36:42 +00:00
|
|
|
u16_t nfb;
|
2002-11-22 15:46:50 +00:00
|
|
|
u16_t left, cop;
|
|
|
|
u16_t mtu = netif->mtu;
|
|
|
|
u16_t ofo, omf;
|
|
|
|
u16_t last;
|
|
|
|
u16_t poff = IP_HLEN;
|
|
|
|
u16_t tmp;
|
2007-06-03 18:36:42 +00:00
|
|
|
#if !IP_FRAG_USES_STATIC_BUF
|
|
|
|
u16_t newpbuflen, left_to_copy;
|
|
|
|
#endif
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
/* Get a RAM based MTU sized pbuf */
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_FRAG_USES_STATIC_BUF
|
|
|
|
/* When using a static buffer, we use a PBUF_REF, which we will
|
|
|
|
* use to reference the packet (without link header).
|
|
|
|
* Layer and length is irrelevant.
|
|
|
|
*/
|
2003-03-19 22:14:49 +00:00
|
|
|
rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
|
2006-03-03 11:25:36 +00:00
|
|
|
if (rambuf == NULL) {
|
2006-07-04 08:24:17 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc(PBUF_LINK, 0, PBUF_REF) failed\n"));
|
2006-03-03 11:25:36 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2002-11-22 15:46:50 +00:00
|
|
|
rambuf->tot_len = rambuf->len = mtu;
|
2007-06-13 17:17:26 +00:00
|
|
|
rambuf->payload = LWIP_MEM_ALIGN((void *)buf);
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
/* Copy the IP header in it */
|
|
|
|
iphdr = rambuf->payload;
|
2007-05-10 05:20:05 +00:00
|
|
|
SMEMCPY(iphdr, p->payload, IP_HLEN);
|
2007-06-03 18:36:42 +00:00
|
|
|
#else /* IP_FRAG_USES_STATIC_BUF */
|
|
|
|
original_iphdr = p->payload;
|
|
|
|
iphdr = original_iphdr;
|
|
|
|
#endif /* IP_FRAG_USES_STATIC_BUF */
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
/* Save original offset */
|
|
|
|
tmp = ntohs(IPH_OFFSET(iphdr));
|
|
|
|
ofo = tmp & IP_OFFMASK;
|
|
|
|
omf = tmp & IP_MF;
|
|
|
|
|
|
|
|
left = p->tot_len - IP_HLEN;
|
|
|
|
|
2007-06-03 18:36:42 +00:00
|
|
|
nfb = (mtu - IP_HLEN) / 8;
|
|
|
|
|
2002-11-22 15:46:50 +00:00
|
|
|
while (left) {
|
|
|
|
last = (left <= mtu - IP_HLEN);
|
|
|
|
|
|
|
|
/* Set new offset and MF flag */
|
|
|
|
tmp = omf | (IP_OFFMASK & (ofo));
|
|
|
|
if (!last)
|
|
|
|
tmp = tmp | IP_MF;
|
|
|
|
|
|
|
|
/* Fill this fragment */
|
|
|
|
cop = last ? left : nfb * 8;
|
|
|
|
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_FRAG_USES_STATIC_BUF
|
2002-11-22 15:46:50 +00:00
|
|
|
p = copy_from_pbuf(p, &poff, (u8_t *) iphdr + IP_HLEN, cop);
|
2007-06-03 18:36:42 +00:00
|
|
|
#else /* IP_FRAG_USES_STATIC_BUF */
|
|
|
|
/* When not using a static buffer, create a chain of pbufs.
|
|
|
|
* The first will be a PBUF_RAM holding the link and IP header.
|
|
|
|
* The rest will be PBUF_REFs mirroring the pbuf chain to be fragged,
|
|
|
|
* but limited to the size of an mtu.
|
|
|
|
*/
|
|
|
|
rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
|
|
|
|
if (rambuf == NULL) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
|
|
|
|
iphdr = rambuf->payload;
|
|
|
|
|
|
|
|
/* Can just adjust p directly for needed offset. */
|
|
|
|
p->payload = (u8_t *)p->payload + poff;
|
|
|
|
p->len -= poff;
|
|
|
|
|
|
|
|
left_to_copy = cop;
|
|
|
|
while (left_to_copy) {
|
|
|
|
newpbuflen = (left_to_copy < p->len) ? left_to_copy : p->len;
|
|
|
|
/* Is this pbuf already empty? */
|
|
|
|
if (!newpbuflen) {
|
|
|
|
p = p->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newpbuf = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
|
|
|
|
if (newpbuf == NULL) {
|
|
|
|
pbuf_free(rambuf);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
/* Mirror this pbuf, although we might not need all of it. */
|
|
|
|
newpbuf->payload = p->payload;
|
|
|
|
newpbuf->len = newpbuf->tot_len = newpbuflen;
|
|
|
|
/* Add it to end of rambuf's chain, but using pbuf_cat, not pbuf_chain
|
|
|
|
* so that it is removed when pbuf_dechain is later called on rambuf.
|
|
|
|
*/
|
|
|
|
pbuf_cat(rambuf, newpbuf);
|
|
|
|
left_to_copy -= newpbuflen;
|
|
|
|
if (left_to_copy)
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
poff = newpbuflen;
|
|
|
|
#endif /* IP_FRAG_USES_STATIC_BUF */
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
/* Correct header */
|
2007-06-03 18:36:42 +00:00
|
|
|
IPH_OFFSET_SET(iphdr, htons(tmp));
|
2002-11-22 15:46:50 +00:00
|
|
|
IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
|
|
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
|
|
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
|
|
|
|
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_FRAG_USES_STATIC_BUF
|
2002-11-22 15:46:50 +00:00
|
|
|
if (last)
|
|
|
|
pbuf_realloc(rambuf, left + IP_HLEN);
|
2007-06-03 18:36:42 +00:00
|
|
|
|
2002-11-22 15:46:50 +00:00
|
|
|
/* This part is ugly: we alloc a RAM based pbuf for
|
|
|
|
* the link level header for each chunk and then
|
|
|
|
* free it.A PBUF_ROM style pbuf for which pbuf_header
|
|
|
|
* worked would make things simpler.
|
|
|
|
*/
|
|
|
|
header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
|
2006-03-03 11:25:36 +00:00
|
|
|
if (header != NULL) {
|
|
|
|
pbuf_chain(header, rambuf);
|
|
|
|
netif->output(netif, header, dest);
|
|
|
|
IPFRAG_STATS_INC(ip_frag.xmit);
|
2006-08-11 14:12:05 +00:00
|
|
|
snmp_inc_ipfragcreates();
|
2006-03-03 11:25:36 +00:00
|
|
|
pbuf_free(header);
|
|
|
|
} else {
|
2006-07-04 08:24:17 +00:00
|
|
|
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_frag: pbuf_alloc() for header failed\n"));
|
2006-03-03 11:25:36 +00:00
|
|
|
pbuf_free(rambuf);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2007-06-03 18:36:42 +00:00
|
|
|
#else /* IP_FRAG_USES_STATIC_BUF */
|
|
|
|
/* No need for separate header pbuf - we allowed room for it in rambuf
|
|
|
|
* when allocated.
|
|
|
|
*/
|
|
|
|
netif->output(netif, rambuf, dest);
|
|
|
|
IPFRAG_STATS_INC(ip_frag.xmit);
|
|
|
|
|
|
|
|
/* Unfortunately we can't reuse rambuf - the hardware may still be
|
|
|
|
* using the buffer. Instead we free it (and the ensuing chain) and
|
|
|
|
* recreate it next time round the loop. If we're lucky the hardware
|
|
|
|
* will have already sent the packet, the free will really free, and
|
|
|
|
* there will be zero memory penalty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pbuf_free(rambuf);
|
|
|
|
#endif /* IP_FRAG_USES_STATIC_BUF */
|
2002-11-22 15:46:50 +00:00
|
|
|
left -= cop;
|
2007-06-03 18:36:42 +00:00
|
|
|
ofo += nfb;
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
2007-06-03 18:36:42 +00:00
|
|
|
#if IP_FRAG_USES_STATIC_BUF
|
2002-11-22 15:46:50 +00:00
|
|
|
pbuf_free(rambuf);
|
2007-06-03 18:36:42 +00:00
|
|
|
#endif /* IP_FRAG_USES_STATIC_BUF */
|
2006-08-11 14:12:05 +00:00
|
|
|
snmp_inc_ipfragoks();
|
2002-11-22 15:46:50 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2007-06-03 18:36:42 +00:00
|
|
|
#endif /* IP_FRAG */
|