2002-11-22 15:46:50 +00:00
|
|
|
/*
|
2003-01-08 10:09:39 +00:00
|
|
|
* Copyright (c) 2001-2003 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>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* ip_frag.c
|
|
|
|
*
|
|
|
|
* This is the code for IP segmentation and reassembly
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-11-22 15:46:50 +00:00
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/ip.h"
|
|
|
|
#include "lwip/ip_frag.h"
|
|
|
|
#include "lwip/netif.h"
|
|
|
|
|
2003-02-06 22:18:56 +00:00
|
|
|
#include "lwip/stats.h"
|
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
|
|
|
|
*/
|
|
|
|
static struct pbuf *
|
|
|
|
copy_from_pbuf(struct pbuf *p, u16_t * offset,
|
2003-06-09 21:14:47 +00:00
|
|
|
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;
|
2002-12-18 10:40:01 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IP_REASS_BUFSIZE 5760
|
2003-02-06 22:18:56 +00:00
|
|
|
#define IP_REASS_MAXAGE 30
|
|
|
|
#define IP_REASS_TMO 1000
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
static u8_t ip_reassbuf[IP_HLEN + IP_REASS_BUFSIZE];
|
|
|
|
static u8_t ip_reassbitmap[IP_REASS_BUFSIZE / (8 * 8)];
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Reassembly timer */
|
|
|
|
static void
|
|
|
|
ip_reass_timer(void *arg)
|
|
|
|
{
|
2003-04-15 14:56:03 +00:00
|
|
|
(void)arg;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (ip_reasstmr > 1) {
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr--;
|
2003-02-18 19:27:48 +00:00
|
|
|
sys_timeout(IP_REASS_TMO, ip_reass_timer, NULL);
|
2003-05-01 13:24:01 +00:00
|
|
|
} else if (ip_reasstmr == 1)
|
2003-06-09 21:14:47 +00:00
|
|
|
ip_reasstmr = 0;
|
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-02-06 22:18:56 +00:00
|
|
|
#ifdef IP_STATS
|
|
|
|
++lwip_stats.ip_frag.recv;
|
|
|
|
#endif /* IP_STATS */
|
|
|
|
|
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) {
|
|
|
|
DEBUGF(IP_REASS_DEBUG, ("ip_reass: new packet\n"));
|
2002-12-18 10:40:01 +00:00
|
|
|
memcpy(iphdr, fraghdr, IP_HLEN);
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr = IP_REASS_MAXAGE;
|
2003-02-18 19:27:48 +00:00
|
|
|
sys_timeout(IP_REASS_TMO, ip_reass_timer, NULL);
|
2002-11-22 15:46:50 +00:00
|
|
|
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)) {
|
|
|
|
DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching old packet\n"));
|
2003-02-06 22:18:56 +00:00
|
|
|
#ifdef IP_STATS
|
|
|
|
++lwip_stats.ip_frag.cachehit;
|
|
|
|
#endif /* IP_STATS */
|
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. */
|
|
|
|
if (offset > IP_REASS_BUFSIZE || offset + len > IP_REASS_BUFSIZE) {
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
2003-06-09 21:14:47 +00:00
|
|
|
("ip_reass: fragment outside of buffer (%d:%d/%d).\n", offset,
|
|
|
|
offset + len, IP_REASS_BUFSIZE));
|
2003-02-18 19:27:48 +00:00
|
|
|
sys_untimeout(ip_reass_timer, NULL);
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr = 0;
|
|
|
|
goto nullreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the fragment into the reassembly buffer, at the right
|
|
|
|
offset. */
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
2003-06-09 21:14:47 +00:00
|
|
|
("ip_reass: copying with offset %d into %d:%d\n", offset,
|
|
|
|
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)) {
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
2003-06-09 21:14:47 +00:00
|
|
|
("ip_reass: updating single byte in bitmap.\n"));
|
2002-11-22 15:46:50 +00:00
|
|
|
/* If the two endpoints are in the same byte, we only update
|
|
|
|
that byte. */
|
|
|
|
ip_reassbitmap[offset / (8 * 8)] |=
|
2003-06-09 21:14:47 +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. */
|
|
|
|
ip_reassbitmap[offset / (8 * 8)] |= bitmap_bits[(offset / 8) & 7];
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
2003-06-09 21:14:47 +00:00
|
|
|
("ip_reass: updating many bytes in bitmap (%d:%d).\n",
|
|
|
|
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) {
|
2003-06-09 21:14:47 +00:00
|
|
|
ip_reassbitmap[i] = 0xff;
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
ip_reassbitmap[(offset + len) / (8 * 8)] |=
|
2003-06-09 21:14:47 +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;
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
2003-06-09 21:14:47 +00:00
|
|
|
("ip_reass: last fragment seen, total len %d\n",
|
|
|
|
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. */
|
|
|
|
for (i = 0; i < ip_reasslen / (8 * 8) - 1; ++i) {
|
2003-06-09 21:14:47 +00:00
|
|
|
if (ip_reassbitmap[i] != 0xff) {
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: last fragment seen, bitmap %d/%d failed (%x)\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. */
|
|
|
|
if (ip_reassbitmap[ip_reasslen / (8 * 8)] !=
|
2003-06-09 21:14:47 +00:00
|
|
|
(u8_t) ~ bitmap_bits[ip_reasslen / 8 & 7]) {
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: last fragment seen, bitmap %d didn't contain %x (%x)\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. */
|
2003-02-18 19:27:48 +00:00
|
|
|
sys_untimeout(ip_reass_timer, NULL);
|
2002-11-22 15:46:50 +00:00
|
|
|
ip_reasstmr = 0;
|
|
|
|
pbuf_free(p);
|
|
|
|
p = pbuf_alloc(PBUF_LINK, ip_reasslen, PBUF_POOL);
|
|
|
|
if (p != NULL) {
|
2003-06-09 21:14:47 +00:00
|
|
|
i = 0;
|
|
|
|
for (q = p; q != NULL; q = q->next) {
|
|
|
|
/* Copy enough bytes to fill this pbuf in the chain. The
|
|
|
|
available data in the pbuf is given by the q->len
|
|
|
|
variable. */
|
|
|
|
DEBUGF(IP_REASS_DEBUG,
|
|
|
|
("ip_reass: memcpy from %p (%d) to %p, %d bytes\n",
|
|
|
|
&ip_reassbuf[i], i, q->payload,
|
|
|
|
q->len > ip_reasslen - i ? ip_reasslen - i : q->len));
|
|
|
|
memcpy(q->payload, &ip_reassbuf[i],
|
|
|
|
q->len > ip_reasslen - i ? ip_reasslen - i : q->len);
|
|
|
|
i += q->len;
|
|
|
|
}
|
2003-02-06 22:18:56 +00:00
|
|
|
#ifdef IP_STATS
|
2003-06-09 21:14:47 +00:00
|
|
|
++lwip_stats.ip_frag.fw;
|
2003-02-06 22:18:56 +00:00
|
|
|
#endif /* IP_STATS */
|
|
|
|
} else {
|
|
|
|
#ifdef IP_STATS
|
2003-06-09 21:14:47 +00:00
|
|
|
++lwip_stats.ip_frag.memerr;
|
2003-02-06 22:18:56 +00:00
|
|
|
#endif /* IP_STATS */
|
2002-11-22 15:46:50 +00:00
|
|
|
}
|
|
|
|
DEBUGF(IP_REASS_DEBUG, ("ip_reass: p %p\n", (void*)p));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nullreturn:
|
2003-02-06 22:18:56 +00:00
|
|
|
#ifdef IP_STATS
|
|
|
|
++lwip_stats.ip_frag.drop;
|
|
|
|
#endif /* IP_STATS */
|
2002-11-22 15:46:50 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_MTU 1500
|
2003-04-15 14:56:03 +00:00
|
|
|
static u8_t buf[MEM_ALIGN_SIZE(MAX_MTU)];
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment an IP packet if too large
|
|
|
|
*
|
|
|
|
* Chop the packet in mtu sized chunks and send them in order
|
|
|
|
* by using a fixed size static memory buffer (PBUF_ROM)
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest)
|
|
|
|
{
|
|
|
|
struct pbuf *rambuf;
|
|
|
|
struct pbuf *header;
|
|
|
|
struct ip_hdr *iphdr;
|
|
|
|
u16_t nfb = 0;
|
|
|
|
u16_t left, cop;
|
|
|
|
u16_t mtu = netif->mtu;
|
|
|
|
u16_t ofo, omf;
|
|
|
|
u16_t last;
|
|
|
|
u16_t poff = IP_HLEN;
|
|
|
|
u16_t tmp;
|
|
|
|
|
|
|
|
/* Get a RAM based MTU sized pbuf */
|
2003-03-19 22:14:49 +00:00
|
|
|
rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
|
2002-11-22 15:46:50 +00:00
|
|
|
rambuf->tot_len = rambuf->len = mtu;
|
2003-04-15 14:56:03 +00:00
|
|
|
rambuf->payload = MEM_ALIGN((void *)buf);
|
2002-11-22 15:46:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Copy the IP header in it */
|
|
|
|
iphdr = rambuf->payload;
|
2002-12-18 10:40:01 +00:00
|
|
|
memcpy(iphdr, p->payload, IP_HLEN);
|
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;
|
|
|
|
|
|
|
|
while (left) {
|
|
|
|
last = (left <= mtu - IP_HLEN);
|
|
|
|
|
|
|
|
/* Set new offset and MF flag */
|
|
|
|
ofo += nfb;
|
|
|
|
tmp = omf | (IP_OFFMASK & (ofo));
|
|
|
|
if (!last)
|
|
|
|
tmp = tmp | IP_MF;
|
|
|
|
IPH_OFFSET_SET(iphdr, htons(tmp));
|
|
|
|
|
|
|
|
/* Fill this fragment */
|
|
|
|
nfb = (mtu - IP_HLEN) / 8;
|
|
|
|
cop = last ? left : nfb * 8;
|
|
|
|
|
|
|
|
p = copy_from_pbuf(p, &poff, (u8_t *) iphdr + IP_HLEN, cop);
|
|
|
|
|
|
|
|
/* Correct header */
|
|
|
|
IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
|
|
|
|
IPH_CHKSUM_SET(iphdr, 0);
|
|
|
|
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
pbuf_realloc(rambuf, left + IP_HLEN);
|
|
|
|
/* 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);
|
|
|
|
pbuf_chain(header, rambuf);
|
|
|
|
netif->output(netif, header, dest);
|
2003-02-06 22:18:56 +00:00
|
|
|
#ifdef IP_STATS
|
|
|
|
++lwip_stats.ip_frag.xmit;
|
|
|
|
#endif /* IP_STATS */
|
2002-11-22 15:46:50 +00:00
|
|
|
pbuf_free(header);
|
|
|
|
|
|
|
|
left -= cop;
|
|
|
|
}
|
|
|
|
pbuf_free(rambuf);
|
|
|
|
return ERR_OK;
|
|
|
|
}
|