2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2015-01-15 15:03:56 +01:00
|
|
|
* net/devif/ipv4_input.c
|
2024-09-11 14:39:39 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2019-08-31 20:25:30 +02:00
|
|
|
* Copyright (C) 2007-2009, 2013-2015, 2018-2019 Gregory Nutt. All rights
|
2018-06-24 20:06:12 +02:00
|
|
|
* 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
|
2015-01-15 15:03:56 +01:00
|
|
|
* statement. While it would be possible to break the ipv4_input()
|
2007-11-07 19:54:35 +01:00
|
|
|
* 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>
|
2015-01-15 15:03:56 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2015-05-29 23:16:11 +02:00
|
|
|
#include <netinet/in.h>
|
2015-01-24 14:29:43 +01:00
|
|
|
#include <net/if.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.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>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2022-12-04 08:26:59 +01:00
|
|
|
#include "arp/arp.h"
|
2017-08-07 21:03:23 +02:00
|
|
|
#include "inet/inet.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
|
|
|
|
2017-07-08 02:45:58 +02:00
|
|
|
#include "ipforward/ipforward.h"
|
2015-01-15 15:03:56 +01:00
|
|
|
#include "devif/devif.h"
|
2022-10-17 09:13:34 +02:00
|
|
|
#include "nat/nat.h"
|
2024-02-08 09:00:00 +01:00
|
|
|
#include "ipfilter/ipfilter.h"
|
2023-01-09 13:47:11 +01:00
|
|
|
#include "ipfrag/ipfrag.h"
|
2022-11-29 04:23:43 +01:00
|
|
|
#include "utils/utils.h"
|
2015-01-15 15:03:56 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2015-01-15 15:03:56 +01:00
|
|
|
* Private Data
|
2007-11-07 19:54:35 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-11-23 12:39:40 +01:00
|
|
|
* Name: ipv4_in
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2022-11-23 12:39:40 +01:00
|
|
|
* Receive an IPv4 packet from the network device. Verify and forward to
|
|
|
|
* L3 packet handling logic if the packet is destined for us.
|
|
|
|
*
|
|
|
|
* This is the iob buffer version of ipv4_input(),
|
|
|
|
* this function will support send/receive iob vectors directly between
|
|
|
|
* the driver and l3/l4 stack to avoid unnecessary memory copies,
|
|
|
|
* especially on hardware that supports Scatter/gather, which can
|
|
|
|
* greatly improve performance
|
|
|
|
* this function will uses d_iob as packets input which used by some
|
|
|
|
* NICs such as celluler net driver.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv4 packet.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2013-09-06 17:18:07 +02:00
|
|
|
* Returned Value:
|
2017-07-17 15:14:12 +02:00
|
|
|
* OK - The packet was processed (or dropped) and can be discarded.
|
|
|
|
* ERROR - Hold the packet and try again later. There is a listening
|
|
|
|
* socket but no receive in place to catch the packet yet. The
|
|
|
|
* device's d_len will be set to zero in this case as there is
|
|
|
|
* no outgoing data.
|
2013-10-11 18:48:00 +02:00
|
|
|
*
|
2007-11-07 19:54:35 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
static int ipv4_in(FAR struct net_driver_s *dev)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2022-10-26 06:35:08 +02:00
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
2017-11-22 19:06:36 +01:00
|
|
|
in_addr_t destipaddr;
|
2019-08-31 20:25:30 +02:00
|
|
|
uint16_t totlen;
|
2022-11-26 19:13:21 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Handle ARP on input then give the IPv4 packet to the network layer */
|
|
|
|
|
|
|
|
arp_ipin(dev);
|
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
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv4.recv++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
/* Start of IP input header processing code.
|
|
|
|
*
|
|
|
|
* Check validity of the IP header.
|
2019-08-31 17:08:32 +02:00
|
|
|
* REVISIT: Does not account for varying IP header length due to the
|
|
|
|
* presences of IPv4 options. The header length is encoded as a number
|
|
|
|
* 32-bit words in the HL nibble of the VHL.
|
2019-03-11 19:48:17 +01:00
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
if ((ipv4->vhl & IP_VERSION_MASK) != 0x40 ||
|
|
|
|
(ipv4->vhl & IPv4_HLMASK) < 5)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* IP version and header length. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv4.drop++;
|
|
|
|
g_netstats.ipv4.vhlerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Invalid IP version or header length: %02x\n",
|
2017-07-05 23:12:29 +02:00
|
|
|
ipv4->vhl);
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2016-06-09 21:16:47 +02:00
|
|
|
/* Get the size of the packet minus the size of link layer header */
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
if (IPv4_HDRLEN > dev->d_len)
|
2016-06-09 21:16:47 +02:00
|
|
|
{
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Packet shorter than IPv4 header\n");
|
2016-06-09 21:16:47 +02:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2022-11-22 05:02:55 +01:00
|
|
|
/* Make sure that all packet processing logic knows that there is an IPv4
|
|
|
|
* packet in the device buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
IFF_SET_IPv4(dev->d_flags);
|
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
/* Check the size of the packet. If the size reported to us in d_len is
|
2007-11-07 19:54:35 +01:00
|
|
|
* smaller the size reported in the IP header, we assume that the packet
|
2019-08-31 20:25:30 +02:00
|
|
|
* has been corrupted in transit. If the size of d_len is larger than the
|
2007-11-07 19:54:35 +01:00
|
|
|
* size reported in the IP packet header, the packet has been padded and
|
|
|
|
* we set d_len to the correct value.
|
|
|
|
*/
|
|
|
|
|
2019-08-31 20:25:30 +02:00
|
|
|
totlen = (ipv4->len[0] << 8) + ipv4->len[1];
|
2022-11-23 12:39:40 +01:00
|
|
|
if (totlen < dev->d_len)
|
2012-02-18 23:09:09 +01:00
|
|
|
{
|
2023-05-17 10:11:21 +02:00
|
|
|
iob_update_pktlen(dev->d_iob, totlen, false);
|
2019-08-31 20:25:30 +02:00
|
|
|
dev->d_len = totlen;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2022-11-23 12:39:40 +01:00
|
|
|
else if (totlen > dev->d_len)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: IP packet shorter than length in IP header\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the fragment flag. */
|
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
if ((ipv4->ipoffset[0] & 0x3f) != 0 || ipv4->ipoffset[1] != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2023-01-09 13:47:11 +01:00
|
|
|
#ifdef CONFIG_NET_IPFRAG
|
|
|
|
if (ipv4_fragin(dev) == OK)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2021-01-30 01:58:47 +01:00
|
|
|
g_netstats.ipv4.drop++;
|
|
|
|
g_netstats.ipv4.fragerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2021-01-30 01:58:47 +01:00
|
|
|
nwarn("WARNING: IP fragment dropped\n");
|
|
|
|
goto drop;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2024-03-13 10:04:55 +01:00
|
|
|
#ifdef CONFIG_NET_NAT44
|
2022-10-17 09:13:34 +02:00
|
|
|
/* Try NAT inbound, rule matching will be performed in NAT module. */
|
|
|
|
|
2024-03-13 10:04:55 +01:00
|
|
|
ipv4_nat_inbound(dev, ipv4);
|
2022-10-17 09:13:34 +02:00
|
|
|
#endif
|
|
|
|
|
2017-11-22 19:06:36 +01:00
|
|
|
/* Get the destination IP address in a friendlier form */
|
|
|
|
|
|
|
|
destipaddr = net_ip4addr_conv32(ipv4->destipaddr);
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(CONFIG_NET_BROADCAST) && defined(NET_UDP_HAVE_STACK)
|
2015-10-08 23:10:04 +02: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).
|
|
|
|
*/
|
2007-12-01 00:12:11 +01:00
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
if (ipv4->proto == IP_PROTO_UDP &&
|
2017-11-22 19:06:36 +01:00
|
|
|
net_ipv4addr_cmp(destipaddr, INADDR_BROADCAST))
|
2007-12-01 00:12:11 +01:00
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
|
|
|
/* Forward broadcast packets */
|
|
|
|
|
|
|
|
ipv4_forward_broadcast(dev, ipv4);
|
2022-12-04 17:33:41 +01:00
|
|
|
|
|
|
|
/* Process the incoming packet if not forwardable */
|
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
2017-07-06 15:26:01 +02:00
|
|
|
#endif
|
2022-12-04 17:33:41 +01:00
|
|
|
{
|
|
|
|
ret = udp_ipv4_input(dev);
|
|
|
|
}
|
|
|
|
|
2022-11-26 19:13:21 +01:00
|
|
|
goto done;
|
2007-12-01 00:12:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2017-11-22 19:06:36 +01:00
|
|
|
#if defined(CONFIG_NET_BROADCAST) && defined(NET_UDP_HAVE_STACK)
|
|
|
|
/* The address is not the broadcast address and we have been assigned a
|
|
|
|
* address. So there is also the possibility that the destination address
|
|
|
|
* is a sub-net broadcast address which we will need to handle just as for
|
|
|
|
* the broadcast address above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ipv4->proto == IP_PROTO_UDP &&
|
|
|
|
net_ipv4addr_maskcmp(destipaddr, dev->d_ipaddr, dev->d_netmask) &&
|
|
|
|
net_ipv4addr_broadcast(destipaddr, dev->d_netmask))
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-11-22 19:06:36 +01:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
|
|
|
/* Forward broadcast packets */
|
2015-01-15 15:03:56 +01:00
|
|
|
|
2017-11-22 19:06:36 +01:00
|
|
|
ipv4_forward_broadcast(dev, ipv4);
|
2022-12-04 17:33:41 +01:00
|
|
|
|
|
|
|
/* Process the incoming packet if not forwardable */
|
|
|
|
|
|
|
|
if (dev->d_len > 0)
|
2017-11-22 19:06:36 +01:00
|
|
|
#endif
|
2022-12-04 17:33:41 +01:00
|
|
|
{
|
|
|
|
ret = udp_ipv4_input(dev);
|
|
|
|
}
|
|
|
|
|
2022-11-26 19:13:21 +01:00
|
|
|
goto done;
|
2017-11-22 19:06:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
/* Check if the packet is destined for our IP address. */
|
|
|
|
|
|
|
|
if (!net_ipv4addr_cmp(destipaddr, dev->d_ipaddr))
|
|
|
|
{
|
|
|
|
/* No.. This is not our IP address. Check for an IPv4 IGMP group
|
|
|
|
* address
|
|
|
|
*/
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2014-03-24 16:34:17 +01:00
|
|
|
#ifdef CONFIG_NET_IGMP
|
2017-11-22 19:06:36 +01:00
|
|
|
in_addr_t destip = net_ip4addr_conv32(ipv4->destipaddr);
|
|
|
|
if (igmp_grpfind(dev, &destip) != NULL)
|
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
2017-11-22 19:06:36 +01:00
|
|
|
/* Forward multicast packets */
|
2017-07-06 15:26:01 +02:00
|
|
|
|
2017-11-22 19:06:36 +01:00
|
|
|
ipv4_forward_broadcast(dev, ipv4);
|
2022-12-04 17:33:41 +01:00
|
|
|
|
|
|
|
/* Return success if the packet was forwarded. */
|
|
|
|
|
|
|
|
if (dev->d_len == 0)
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
2017-07-06 15:26:01 +02:00
|
|
|
#endif
|
2017-11-22 19:06:36 +01:00
|
|
|
}
|
|
|
|
else
|
2014-03-24 16:34:17 +01:00
|
|
|
#endif
|
2017-11-22 19:06:36 +01:00
|
|
|
{
|
|
|
|
/* No.. The packet is not destined for us. */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
2017-11-22 19:06:36 +01:00
|
|
|
/* Try to forward the packet */
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2022-11-30 19:48:39 +01:00
|
|
|
if (ipv4_forward(dev, ipv4) >= 0)
|
2017-11-22 19:06:36 +01:00
|
|
|
{
|
|
|
|
/* The packet was forwarded. Return success; d_len will
|
|
|
|
* be set appropriately by the forwarding logic: Cleared
|
|
|
|
* if the packet is forward via anoother device or non-
|
|
|
|
* zero if it will be forwarded by the same device that
|
|
|
|
* it was received on.
|
|
|
|
*/
|
|
|
|
|
2022-11-26 19:13:21 +01:00
|
|
|
goto done;
|
2017-11-22 19:06:36 +01:00
|
|
|
}
|
|
|
|
else
|
2017-07-05 23:12:29 +02:00
|
|
|
#endif
|
2022-08-24 04:30:01 +02:00
|
|
|
#if defined(NET_UDP_HAVE_STACK) && defined(CONFIG_NET_BINDTODEVICE)
|
|
|
|
/* If the protocol specific socket option NET_BINDTODEVICE
|
2021-04-28 22:18:16 +02:00
|
|
|
* is selected, then we must forward all UDP packets to the bound
|
|
|
|
* socket.
|
|
|
|
*/
|
|
|
|
|
2022-08-24 04:30:01 +02:00
|
|
|
if (ipv4->proto != IP_PROTO_UDP)
|
2021-04-28 22:18:16 +02:00
|
|
|
#endif
|
2017-11-22 19:06:36 +01:00
|
|
|
{
|
|
|
|
/* Not destined for us and not forwardable... Drop the
|
|
|
|
* packet.
|
|
|
|
*/
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2022-06-09 14:34:16 +02:00
|
|
|
ninfo("WARNING: Not destined for us; not forwardable... "
|
2017-11-22 19:06:36 +01:00
|
|
|
"Dropping!\n");
|
2017-07-05 23:12:29 +02:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2017-11-22 19:06:36 +01:00
|
|
|
g_netstats.ipv4.drop++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2017-11-22 19:06:36 +01:00
|
|
|
goto drop;
|
2014-03-24 16:34:17 +01:00
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 09:43:52 +01:00
|
|
|
#ifdef CONFIG_NET_ICMP
|
2021-01-30 01:58:47 +01:00
|
|
|
|
2021-01-18 09:43:52 +01:00
|
|
|
/* In other cases, the device must be assigned a non-zero IP address. */
|
|
|
|
|
|
|
|
else if (net_ipv4addr_cmp(dev->d_ipaddr, INADDR_ANY))
|
|
|
|
{
|
|
|
|
nwarn("WARNING: No IP address assigned\n");
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2024-04-24 05:06:52 +02:00
|
|
|
#ifdef CONFIG_NET_IPV4_CHECKSUMS
|
2022-11-29 04:23:43 +01:00
|
|
|
if (ipv4_chksum(IPv4BUF) != 0xffff)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Compute and check the IP header checksum. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv4.drop++;
|
|
|
|
g_netstats.ipv4.chkerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Bad IP checksum\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
2024-04-24 05:06:52 +02:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2024-02-08 09:00:00 +01:00
|
|
|
#ifdef CONFIG_NET_IPFILTER
|
|
|
|
if (ipv4_filter_in(dev) != IPFILTER_TARGET_ACCEPT)
|
|
|
|
{
|
|
|
|
ninfo("Drop/Reject INPUT packet due to filter.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-24 14:29:43 +01:00
|
|
|
/* Now process the incoming packet according to the protocol. */
|
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
switch (ipv4->proto)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef NET_TCP_HAVE_STACK
|
2014-07-05 00:38:51 +02:00
|
|
|
case IP_PROTO_TCP: /* TCP input */
|
2015-01-15 22:06:46 +01:00
|
|
|
tcp_ipv4_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
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef NET_UDP_HAVE_STACK
|
2014-07-05 00:38:51 +02:00
|
|
|
case IP_PROTO_UDP: /* UDP input */
|
2015-01-15 22:06:46 +01:00
|
|
|
udp_ipv4_input(dev);
|
2007-11-07 19:54:35 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 18:25:57 +01:00
|
|
|
#ifdef NET_ICMP_HAVE_STACK
|
2007-11-07 19:54:35 +01:00
|
|
|
/* Check for ICMP input */
|
|
|
|
|
2014-07-05 00:38:51 +02:00
|
|
|
case IP_PROTO_ICMP: /* ICMP input */
|
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
|
|
|
|
2015-05-29 23:16:11 +02:00
|
|
|
#ifdef CONFIG_NET_IGMP
|
2015-01-15 15:03:56 +01:00
|
|
|
/* Check for IGMP input */
|
2010-07-08 02:59:51 +02:00
|
|
|
|
2014-07-05 00:38:51 +02:00
|
|
|
case IP_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
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
default: /* Unrecognized/unsupported protocol */
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv4.drop++;
|
|
|
|
g_netstats.ipv4.protoerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Unrecognized IP protocol\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:00:00 +01:00
|
|
|
#ifdef CONFIG_NET_IPFILTER
|
|
|
|
ipfilter_out(dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPFORWARD) || defined(CONFIG_NET_IPFILTER) || \
|
2022-11-27 07:49:47 +01:00
|
|
|
(defined(CONFIG_NET_BROADCAST) && defined(NET_UDP_HAVE_STACK))
|
2022-11-26 19:13:21 +01:00
|
|
|
done:
|
2022-11-27 07:49:47 +01:00
|
|
|
#endif
|
2023-01-09 13:47:11 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPFRAG
|
|
|
|
ip_fragout(dev);
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 10:47:44 +01:00
|
|
|
devif_out(dev);
|
2022-11-26 19:13:21 +01:00
|
|
|
|
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
|
|
|
|
2022-11-26 19:13:21 +01:00
|
|
|
return ret;
|
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
|
|
|
}
|
2022-11-23 12:39:40 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive an IPv4 packet from the network device. Verify and forward to
|
|
|
|
* L3 packet handling logic if the packet is destined for us.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv4 packet.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK - The packet was processed (or dropped) and can be discarded.
|
|
|
|
* ERROR - Hold the packet and try again later. There is a listening
|
|
|
|
* socket but no receive in place to catch the packet yet. The
|
|
|
|
* device's d_len will be set to zero in this case as there is
|
|
|
|
* no outgoing data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv4_input(FAR struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR uint8_t *buf;
|
|
|
|
int ret;
|
|
|
|
|
2023-11-17 08:44:49 +01:00
|
|
|
/* Store reception timestamp if enabled and not provided by hardware. */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_TIMESTAMP) && !defined(CONFIG_ARCH_HAVE_NETDEV_TIMESTAMP)
|
|
|
|
clock_gettime(CLOCK_REALTIME, &dev->d_rxtime);
|
|
|
|
#endif
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
if (dev->d_iob != NULL)
|
|
|
|
{
|
|
|
|
buf = dev->d_buf;
|
|
|
|
|
|
|
|
/* Set the device buffer to l2 */
|
|
|
|
|
2023-01-06 04:46:36 +01:00
|
|
|
dev->d_buf = NETLLBUF;
|
2022-11-23 12:39:40 +01:00
|
|
|
ret = ipv4_in(dev);
|
|
|
|
|
|
|
|
dev->d_buf = buf;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return netdev_input(dev, ipv4_in, true);
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:03:56 +01:00
|
|
|
#endif /* CONFIG_NET_IPv4 */
|