2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2012-03-04 00:18:34 +01:00
|
|
|
* netuip/uip_input.c
|
2007-11-07 19:54:35 +01:00
|
|
|
* The uIP TCP/IP stack code.
|
|
|
|
*
|
2014-03-24 16:34:17 +01:00
|
|
|
* Copyright (C) 2007-2009, 2013-2014 Gregory Nutt. All rights reserved.
|
2012-02-18 23:09:09 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Adapted for NuttX from logic in uIP which also has a BSD-like license:
|
|
|
|
*
|
|
|
|
* uIP is an implementation of the TCP/IP protocol stack intended for
|
|
|
|
* small 8-bit and 16-bit microcontrollers.
|
|
|
|
*
|
|
|
|
* uIP provides the necessary protocols for Internet communication,
|
|
|
|
* with a very small code footprint and RAM requirements - the uIP
|
|
|
|
* code size is on the order of a few kilobytes and RAM usage is on
|
|
|
|
* the order of a few hundred bytes.
|
|
|
|
*
|
|
|
|
* Original author Adam Dunkels <adam@dunkels.com>
|
|
|
|
* Copyright () 2001-2003, Adam Dunkels.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* uIP is a small implementation of the IP, UDP and TCP protocols (as
|
|
|
|
* well as some basic ICMP stuff). The implementation couples the IP,
|
|
|
|
* UDP, TCP and the application layers very tightly. To keep the size
|
|
|
|
* of the compiled code down, this code frequently uses the goto
|
|
|
|
* statement. While it would be possible to break the uip_input()
|
|
|
|
* function into many smaller functions, this would increase the code
|
|
|
|
* size because of the overhead of parameter passing and the fact that
|
2014-06-25 02:12:49 +02:00
|
|
|
* the optimizer would not be as efficient.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* The principle is that we have a small buffer, called the d_buf,
|
|
|
|
* in which the device driver puts an incoming packet. The TCP/IP
|
|
|
|
* stack parses the headers in the packet, and calls the
|
|
|
|
* application. If the remote host has sent data to the application,
|
|
|
|
* this data is present in the d_buf and the application read the
|
|
|
|
* data from there. It is up to the application to put this data into
|
|
|
|
* a byte stream if needed. The application will not be fed with data
|
|
|
|
* that is out of sequence.
|
|
|
|
*
|
2014-04-12 20:13:01 +02:00
|
|
|
* If the application wishes to send data to the peer, it should put
|
2007-11-07 19:54:35 +01:00
|
|
|
* its data into the d_buf. The d_appdata pointer points to the
|
|
|
|
* first available byte. The TCP/IP stack will calculate the
|
|
|
|
* checksums, and fill in the necessary header fields and finally send
|
|
|
|
* the packet back to the peer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 18:14:15 +02:00
|
|
|
#include <nuttx/net/uip.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/netstats.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-29 01:42:34 +02:00
|
|
|
# include "net_neighbor.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
|
|
|
#include "udp/udp.h"
|
2014-06-25 18:34:52 +02:00
|
|
|
#include "pkt/pkt.h"
|
2014-06-25 17:12:47 +02:00
|
|
|
#include "icmp/icmp.h"
|
2014-06-25 17:52:36 +02:00
|
|
|
#include "igmp/igmp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 15:53:45 +01:00
|
|
|
* Pre-processor Definitions
|
2007-11-07 19:54:35 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Macros. */
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
#define BUF ((FAR struct net_iphdr_s *)&dev->d_buf[UIP_LLH_LEN])
|
2014-06-29 03:26:16 +02:00
|
|
|
#define FBUF ((FAR struct net_iphdr_s *)&g_reassembly_buffer[0])
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* IP fragment re-assembly */
|
|
|
|
|
|
|
|
#define IP_MF 0x20
|
2007-11-20 00:09:39 +01:00
|
|
|
#define UIP_REASS_BUFSIZE (CONFIG_NET_BUFSIZE - UIP_LLH_LEN)
|
2007-11-07 19:54:35 +01:00
|
|
|
#define UIP_REASS_FLAG_LASTFRAG 0x01
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if UIP_REASSEMBLY && !defined(CONFIG_NET_IPv6)
|
2014-06-29 03:26:16 +02:00
|
|
|
static uint8_t g_reassembly_buffer[UIP_REASS_BUFSIZE];
|
|
|
|
static uint8_t g_reassembly_bitmap[UIP_REASS_BUFSIZE / (8 * 8)];
|
|
|
|
static const uint8_t g_bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
|
|
|
|
static uint16_t g_reassembly_len;
|
|
|
|
static uint8_t g_reassembly_flags;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif /* UIP_REASSEMBLY */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-29 03:26:16 +02:00
|
|
|
* Function: devif_reassembly
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* IP fragment reassembly: not well-tested.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if UIP_REASSEMBLY && !defined(CONFIG_NET_IPv6)
|
2014-06-29 03:26:16 +02:00
|
|
|
static uint8_t devif_reassembly(void)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-06-28 01:51:32 +02:00
|
|
|
FAR struct net_iphdr_s *pbuf = BUF;
|
|
|
|
FAR struct net_iphdr_s *pfbuf = FBUF;
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t offset;
|
|
|
|
uint16_t len;
|
|
|
|
uint16_t i;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
/* If g_reassembly_timer 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.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
if (!g_reassembly_timer)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-06-29 03:26:16 +02:00
|
|
|
memcpy(g_reassembly_buffer, &pbuf->vhl, UIP_IPH_LEN);
|
2014-06-29 02:07:02 +02:00
|
|
|
g_reassembly_timer = UIP_REASS_MAXAGE;
|
2014-06-29 03:26:16 +02:00
|
|
|
g_reassembly_flags = 0;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Clear the bitmap. */
|
2012-03-09 23:05:14 +01:00
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
memset(g_reassembly_bitmap, 0, sizeof(g_reassembly_bitmap));
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the incoming fragment matches the one currently present
|
2014-04-12 20:13:01 +02:00
|
|
|
* in the reassembly buffer. If so, we proceed with copying the
|
2007-11-07 19:54:35 +01:00
|
|
|
* fragment into the buffer.
|
|
|
|
*/
|
|
|
|
|
2013-09-06 17:18:07 +02:00
|
|
|
if (uiphdr_addr_cmp(pbuf->srcipaddr, pfbuf->srcipaddr) &&
|
2009-03-08 15:42:36 +01:00
|
|
|
uiphdr_addr_cmp(pbuf->destipaddr == pfbuf->destipaddr) &&
|
|
|
|
pbuf->g_ipid[0] == pfbuf->g_ipid[0] && pbuf->g_ipid[1] == pfbuf->g_ipid[1])
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2009-03-08 15:42:36 +01:00
|
|
|
len = (pbuf->len[0] << 8) + pbuf->len[1] - (pbuf->vhl & 0x0f) * 4;
|
|
|
|
offset = (((pbuf->ipoffset[0] & 0x3f) << 8) + pbuf->ipoffset[1]) * 8;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* If the offset or the offset + fragment length overflows the
|
|
|
|
* reassembly buffer, we discard the entire packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (offset > UIP_REASS_BUFSIZE || offset + len > UIP_REASS_BUFSIZE)
|
|
|
|
{
|
2014-06-29 02:07:02 +02:00
|
|
|
g_reassembly_timer = 0;
|
2007-11-07 19:54:35 +01:00
|
|
|
goto nullreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the fragment into the reassembly buffer, at the right offset. */
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
memcpy(&g_reassembly_buffer[UIP_IPH_LEN + offset], (char *)pbuf + (int)((pbuf->vhl & 0x0f) * 4), len);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Update the bitmap. */
|
|
|
|
|
|
|
|
if (offset / (8 * 8) == (offset + len) / (8 * 8))
|
|
|
|
{
|
|
|
|
/* If the two endpoints are in the same byte, we only update that byte. */
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
g_reassembly_bitmap[offset / (8 * 8)] |=
|
|
|
|
g_bitmap_bits[(offset / 8 ) & 7] & ~g_bitmap_bits[((offset + len) / 8 ) & 7];
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the two endpoints are in different bytes, we update the bytes
|
|
|
|
* in the endpoints and fill the stuff inbetween with 0xff.
|
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
g_reassembly_bitmap[offset / (8 * 8)] |= g_bitmap_bits[(offset / 8 ) & 7];
|
2007-11-07 19:54:35 +01:00
|
|
|
for (i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i)
|
|
|
|
{
|
2014-06-29 03:26:16 +02:00
|
|
|
g_reassembly_bitmap[i] = 0xff;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2014-06-29 03:26:16 +02:00
|
|
|
g_reassembly_bitmap[(offset + len) / (8 * 8)] |= ~g_bitmap_bits[((offset + len) / 8 ) & 7];
|
2007-11-07 19:54:35 +01: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.
|
|
|
|
*/
|
|
|
|
|
2009-03-08 15:42:36 +01:00
|
|
|
if ((pbuf->ipoffset[0] & IP_MF) == 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-06-29 03:26:16 +02:00
|
|
|
g_reassembly_flags |= UIP_REASS_FLAG_LASTFRAG;
|
|
|
|
g_reassembly_len = offset + len;
|
2007-11-07 19:54:35 +01: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.
|
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
if (g_reassembly_flags & UIP_REASS_FLAG_LASTFRAG)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Check all bytes up to and including all but the last byte in
|
|
|
|
* the bitmap.
|
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
for (i = 0; i < g_reassembly_len / (8 * 8) - 1; ++i)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-06-29 03:26:16 +02:00
|
|
|
if (g_reassembly_bitmap[i] != 0xff)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
goto nullreturn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the last byte in the bitmap. It should contain just the
|
|
|
|
* right amount of bits.
|
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
if (g_reassembly_bitmap[g_reassembly_len / (8 * 8)] != (uint8_t)~g_bitmap_bits[g_reassembly_len / 8 & 7])
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
goto nullreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
g_reassembly_timer = 0;
|
2014-06-29 03:26:16 +02:00
|
|
|
memcpy(pbuf, pfbuf, g_reassembly_len);
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/* Pretend to be a "normal" (i.e., not fragmented) IP packet from
|
|
|
|
* now on.
|
|
|
|
*/
|
|
|
|
|
2009-03-08 15:42:36 +01:00
|
|
|
pbuf->ipoffset[0] = pbuf->ipoffset[1] = 0;
|
2014-06-29 03:26:16 +02:00
|
|
|
pbuf->len[0] = g_reassembly_len >> 8;
|
|
|
|
pbuf->len[1] = g_reassembly_len & 0xff;
|
2009-03-08 15:42:36 +01:00
|
|
|
pbuf->ipchksum = 0;
|
2014-06-28 01:51:32 +02:00
|
|
|
pbuf->ipchksum = ~(ip_chksum(dev));
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
return g_reassembly_len;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nullreturn:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* UIP_REASSEMBLY */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: uip_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
2013-09-06 17:18:07 +02:00
|
|
|
* Returned Value:
|
2013-10-11 18:48:00 +02:00
|
|
|
* OK The packet was processed (or dropped) and can be discarded.
|
|
|
|
* ERROR There is a matching connection, but could not dispatch the packet
|
|
|
|
* yet. Currently useful for UDP when a packet arrives before a recv
|
|
|
|
* call is in place.
|
|
|
|
*
|
2007-11-07 19:54:35 +01:00
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
int uip_input(FAR struct net_driver_s *dev)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-06-28 01:51:32 +02:00
|
|
|
FAR struct net_iphdr_s *pbuf = BUF;
|
2012-02-18 23:09:09 +01:00
|
|
|
uint16_t iplen;
|
2009-03-08 15:42:36 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* This is where the input processing starts. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.recv++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Start of IP input header processing code. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* Check validity of the IP header. */
|
|
|
|
|
2013-09-06 17:18:07 +02:00
|
|
|
if ((pbuf->vtc & 0xf0) != 0x60)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* IP version and header length. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
|
|
|
g_netstats.ip.vhlerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("Invalid IPv6 version: %d\n", pbuf->vtc >> 4);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
#else /* CONFIG_NET_IPv6 */
|
|
|
|
/* Check validity of the IP header. */
|
|
|
|
|
2009-03-08 15:42:36 +01:00
|
|
|
if (pbuf->vhl != 0x45)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* IP version and header length. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
|
|
|
g_netstats.ip.vhlerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("Invalid IP version or header length: %02x\n", pbuf->vhl);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
/* Check the size of the packet. If the size reported to us in d_len is
|
|
|
|
* smaller the size reported in the IP header, we assume that the packet
|
|
|
|
* has been corrupted in transit. If the size of d_len is larger than the
|
|
|
|
* size reported in the IP packet header, the packet has been padded and
|
|
|
|
* we set d_len to the correct value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2012-02-18 23:09:09 +01:00
|
|
|
/* The length reported in the IPv6 header is the length of the payload
|
|
|
|
* that follows the header. However, uIP uses the d_len variable for
|
|
|
|
* holding the size of the entire packet, including the IP header. For
|
|
|
|
* IPv4 this is not a problem as the length field in the IPv4 header
|
|
|
|
* contains the length of the entire packet. But for IPv6 we need to add
|
|
|
|
* the size of the IPv6 header (40 bytes).
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2012-02-18 23:09:09 +01:00
|
|
|
iplen = (pbuf->len[0] << 8) + pbuf->len[1] + UIP_IPH_LEN;
|
|
|
|
#else
|
|
|
|
iplen = (pbuf->len[0] << 8) + pbuf->len[1];
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2012-02-18 23:09:09 +01:00
|
|
|
|
|
|
|
if (iplen <= dev->d_len)
|
|
|
|
{
|
|
|
|
dev->d_len = iplen;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("IP packet shorter than length in IP header\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_IPv6
|
|
|
|
/* Check the fragment flag. */
|
|
|
|
|
2009-03-08 15:42:36 +01:00
|
|
|
if ((pbuf->ipoffset[0] & 0x3f) != 0 || pbuf->ipoffset[1] != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
#if UIP_REASSEMBLY
|
2014-06-29 03:26:16 +02:00
|
|
|
dev->d_len = devif_reassembly();
|
2007-11-07 19:54:35 +01:00
|
|
|
if (dev->d_len == 0)
|
|
|
|
{
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#else /* UIP_REASSEMBLY */
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
|
|
|
g_netstats.ip.fragerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("IP fragment dropped\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
#endif /* UIP_REASSEMBLY */
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2007-12-01 00:12:11 +01:00
|
|
|
/* If IP broadcast support is configured, we check for a broadcast
|
|
|
|
* UDP packet, which may be destined to us (even if there is no IP
|
|
|
|
* address yet assigned to the device as is the case when we are
|
|
|
|
* negotiating over DHCP for an address).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_BROADCAST) && defined(CONFIG_NET_UDP)
|
2009-03-08 15:42:36 +01:00
|
|
|
if (pbuf->proto == UIP_PROTO_UDP &&
|
2007-12-01 00:12:11 +01:00
|
|
|
#ifndef CONFIG_NET_IPv6
|
2009-03-08 15:42:36 +01:00
|
|
|
uip_ipaddr_cmp(uip_ip4addr_conv(pbuf->destipaddr), g_alloneaddr))
|
2007-12-01 00:12:11 +01:00
|
|
|
#else
|
2009-03-08 15:42:36 +01:00
|
|
|
uip_ipaddr_cmp(pbuf->destipaddr, g_alloneaddr))
|
2007-12-01 00:12:11 +01:00
|
|
|
#endif
|
|
|
|
{
|
2014-06-25 02:55:01 +02:00
|
|
|
return udp_input(dev);
|
2007-12-01 00:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In most other cases, the device must be assigned a non-zero IP
|
|
|
|
* address. Another exception is when CONFIG_NET_PINGADDRCONF is
|
|
|
|
* enabled...
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
#endif
|
2008-09-02 22:38:12 +02:00
|
|
|
#ifdef CONFIG_NET_ICMP
|
2007-11-22 15:42:52 +01:00
|
|
|
if (uip_ipaddr_cmp(dev->d_ipaddr, g_allzeroaddr))
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* If we are configured to use ping IP address configuration and
|
|
|
|
* hasn't been assigned an IP address yet, we accept all ICMP
|
|
|
|
* packets.
|
|
|
|
*/
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
#if defined(CONFIG_NET_PINGADDRCONF) && !defined(CONFIG_NET_IPv6)
|
2009-03-08 15:42:36 +01:00
|
|
|
if (pbuf->proto == UIP_PROTO_ICMP)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("Possible ping config packet received\n");
|
2014-06-25 17:12:47 +02:00
|
|
|
icmp_input(dev);
|
2013-10-11 18:48:00 +02:00
|
|
|
goto drop;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
else
|
2007-11-20 00:09:39 +01:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("No IP address assigned\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:12:11 +01:00
|
|
|
|
2010-07-11 17:17:11 +02:00
|
|
|
/* Check if the packet is destined for out IP address */
|
2007-11-07 19:54:35 +01:00
|
|
|
else
|
2008-09-02 22:38:12 +02:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Check if the packet is destined for our IP address. */
|
|
|
|
#ifndef CONFIG_NET_IPv6
|
2009-03-08 15:42:36 +01:00
|
|
|
if (!uip_ipaddr_cmp(uip_ip4addr_conv(pbuf->destipaddr), dev->d_ipaddr))
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-03-24 16:34:17 +01:00
|
|
|
#ifdef CONFIG_NET_IGMP
|
2014-06-29 20:22:50 +02:00
|
|
|
net_ipaddr_t destip = uip_ip4addr_conv(pbuf->destipaddr);
|
2014-06-25 17:52:36 +02:00
|
|
|
if (igmp_grpfind(dev, &destip) == NULL)
|
2014-03-24 16:34:17 +01:00
|
|
|
#endif
|
|
|
|
{
|
2007-11-07 19:54:35 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2014-03-24 16:34:17 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2014-03-24 16:34:17 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
#else /* CONFIG_NET_IPv6 */
|
|
|
|
/* For IPv6, packet reception is a little trickier as we need to
|
|
|
|
* make sure that we listen to certain multicast addresses (all
|
|
|
|
* hosts multicast address, and the solicited-node multicast
|
|
|
|
* address) as well. However, we will cheat here and accept all
|
|
|
|
* multicast packets that are sent to the ff02::/16 addresses.
|
|
|
|
*/
|
|
|
|
|
2009-03-08 15:42:36 +01:00
|
|
|
if (!uip_ipaddr_cmp(pbuf->destipaddr, dev->d_ipaddr) &&
|
2012-06-08 17:45:40 +02:00
|
|
|
pbuf->destipaddr[0] != 0xff02)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_IPv6
|
2014-06-28 01:51:32 +02:00
|
|
|
if (ip_chksum(dev) != 0xffff)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Compute and check the IP header checksum. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
|
|
|
g_netstats.ip.chkerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("Bad IP checksum\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
/* Everything looks good so far. Now process the incoming packet
|
|
|
|
* according to the protocol.
|
|
|
|
*/
|
|
|
|
|
2009-03-08 15:42:36 +01:00
|
|
|
switch (pbuf->proto)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2007-11-22 19:36:46 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2007-11-07 19:54:35 +01:00
|
|
|
case UIP_PROTO_TCP: /* TCP input */
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_input(dev);
|
2007-11-07 19:54:35 +01:00
|
|
|
break;
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP
|
|
|
|
case UIP_PROTO_UDP: /* UDP input */
|
2014-06-25 02:55:01 +02:00
|
|
|
udp_input(dev);
|
2007-11-07 19:54:35 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check for ICMP input */
|
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#ifdef CONFIG_NET_ICMP
|
2007-11-07 19:54:35 +01:00
|
|
|
#ifndef CONFIG_NET_IPv6
|
|
|
|
case UIP_PROTO_ICMP: /* ICMP input */
|
|
|
|
#else
|
|
|
|
case UIP_PROTO_ICMP6: /* ICMP6 input */
|
|
|
|
#endif
|
2014-06-25 17:12:47 +02:00
|
|
|
icmp_input(dev);
|
2007-11-07 19:54:35 +01:00
|
|
|
break;
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-07-08 02:59:51 +02:00
|
|
|
/* Check for ICMP input */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IGMP
|
|
|
|
#ifndef CONFIG_NET_IPv6
|
|
|
|
case UIP_PROTO_IGMP: /* IGMP input */
|
2014-06-25 17:52:36 +02:00
|
|
|
igmp_input(dev);
|
2010-07-08 02:59:51 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
default: /* Unrecognized/unsupported protocol */
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.ip.drop++;
|
|
|
|
g_netstats.ip.protoerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
|
2009-09-15 17:44:14 +02:00
|
|
|
nlldbg("Unrecognized IP protocol\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2013-10-11 18:48:00 +02:00
|
|
|
/* Return and let the caller do any pending transmission. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2013-09-06 17:18:07 +02:00
|
|
|
return OK;
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2013-10-11 18:48:00 +02:00
|
|
|
/* Drop the packet. NOTE that OK is returned meaning that the
|
|
|
|
* packet has been processed (although processed unsuccessfully).
|
|
|
|
*/
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
drop:
|
|
|
|
dev->d_len = 0;
|
2013-10-11 18:48:00 +02:00
|
|
|
return OK;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET */
|