2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2015-01-15 15:03:56 +01:00
|
|
|
* net/devif/ipv6_input.c
|
2024-09-11 14:39:39 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2021-09-18 00:12:48 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2015-01-15 15:03:56 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2018-11-02 19:06:57 +01:00
|
|
|
#include <stdbool.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
|
|
|
#include <string.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>
|
2018-11-02 19:06:57 +01:00
|
|
|
#include <nuttx/net/ipv6ext.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2015-01-17 16:27:05 +01:00
|
|
|
#include "neighbor/neighbor.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
|
|
|
#include "udp/udp.h"
|
2017-04-02 20:01:04 +02:00
|
|
|
#include "sixlowpan/sixlowpan.h"
|
2014-06-25 18:34:52 +02:00
|
|
|
#include "pkt/pkt.h"
|
2015-01-15 15:03:56 +01:00
|
|
|
#include "icmpv6/icmpv6.h"
|
|
|
|
|
2024-03-13 10:04:55 +01:00
|
|
|
#include "nat/nat.h"
|
2017-06-28 16:13:57 +02:00
|
|
|
#include "netdev/netdev.h"
|
2017-07-08 02:45:58 +02:00
|
|
|
#include "ipforward/ipforward.h"
|
2017-08-09 00:30:27 +02:00
|
|
|
#include "inet/inet.h"
|
2015-01-15 15:03:56 +01:00
|
|
|
#include "devif/devif.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"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2017-06-28 16:13:57 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: check_dev_destipaddr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the destination address in the IPv6 is destined for the
|
|
|
|
* provided network device.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 1 - This packet is destined for this network device
|
|
|
|
* 0 - This packet is NOT destined for this network device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-28 23:16:15 +02:00
|
|
|
static int check_dev_destipaddr(FAR struct net_driver_s *dev, FAR void *arg)
|
2017-06-28 16:13:57 +02:00
|
|
|
{
|
|
|
|
FAR struct ipv6_hdr_s *ipv6 = (FAR struct ipv6_hdr_s *)arg;
|
|
|
|
|
|
|
|
/* Check if the IPv6 destination address matches the IPv6 address assigned
|
|
|
|
* to this device.
|
|
|
|
*/
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if (NETDEV_IS_MY_V6ADDR(dev, ipv6->destipaddr))
|
2017-06-28 16:13:57 +02:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No match, return 0 to keep searching */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: check_destipaddr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the destination address in the IPv6 is destined for us. This
|
|
|
|
* is typically just a comparison the of the IPv6 destination address in
|
|
|
|
* the IPv6 packet with the IPv6 address assigned to the receiving device.
|
|
|
|
*
|
2017-06-28 23:39:21 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv6 packet.
|
|
|
|
* ipv6 - A convenience pointer to the IPv6 header in within the IPv6
|
|
|
|
* packet
|
|
|
|
*
|
2017-06-28 16:13:57 +02:00
|
|
|
* Returned Value:
|
|
|
|
* true - This packet is destined for us
|
|
|
|
* false - This packet is NOT destined for us and may need to be forwarded.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool check_destipaddr(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct ipv6_hdr_s *ipv6)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ipv6->destipaddr[0] == HTONS(0xff02))
|
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
|
|
|
/* Forward multicast packets */
|
|
|
|
|
|
|
|
ipv6_forward_broadcast(dev, ipv6);
|
|
|
|
#endif
|
2017-06-28 16:13:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We will also allow for a perverse case where we receive a packet
|
|
|
|
* addressed to us, but on a different device. Can that really happen?
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = netdev_foreach(check_dev_destipaddr, ipv6);
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
/* The traversal of the network devices will return 0 if there is
|
|
|
|
* no network device with that address or 1 if there is a network
|
|
|
|
* device with such an address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2022-11-23 12:39:40 +01:00
|
|
|
* Name: ipv6_in
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-06-28 16:13:57 +02:00
|
|
|
* Receive an IPv6 packet from the network device. Verify and forward to
|
|
|
|
* L3 packet handling logic if the packet is destined for us.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2022-11-23 12:39:40 +01:00
|
|
|
* This is the iob buffer version of ipv6_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.
|
|
|
|
*
|
2017-06-28 23:39:21 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet was received and which contains
|
|
|
|
* the IPv6 packet.
|
2013-09-06 17:18:07 +02:00
|
|
|
* Returned Value:
|
2017-06-28 23:39:21 +02:00
|
|
|
* OK - The packet was processed (or dropped) and can be discarded.
|
2017-07-17 15:14:12 +02:00
|
|
|
* 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
|
|
|
*
|
2017-06-28 16:13:57 +02:00
|
|
|
* If this function returns to the network driver with dev->d_len > 0,
|
|
|
|
* that is an indication to the driver that there is an outgoing response
|
|
|
|
* to this input.
|
|
|
|
*
|
2007-11-07 19:54:35 +01:00
|
|
|
* Assumptions:
|
2017-07-01 16:39:39 +02:00
|
|
|
* The network is locked.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
static int ipv6_in(FAR struct net_driver_s *dev)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2015-01-19 20:21:26 +01:00
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
2018-11-02 19:06:57 +01:00
|
|
|
FAR uint8_t *payload;
|
2018-11-03 00:50:01 +01:00
|
|
|
uint16_t iphdrlen;
|
2018-11-10 18:29:02 +01:00
|
|
|
uint16_t paylen;
|
2018-11-02 19:06:57 +01:00
|
|
|
uint8_t nxthdr;
|
2017-06-29 16:16:46 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
|
|
|
int ret;
|
|
|
|
#endif
|
2023-01-09 13:47:11 +01:00
|
|
|
#ifdef CONFIG_NET_IPFRAG
|
|
|
|
bool isfrag = false;
|
|
|
|
#endif
|
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.ipv6.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.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2015-01-19 20:21:26 +01:00
|
|
|
if ((ipv6->vtc & 0xf0) != 0x60)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* IP version and header length. */
|
|
|
|
|
2018-11-03 00:50:01 +01:00
|
|
|
nwarn("WARNING: Invalid IPv6 version: %d\n", ipv6->vtc >> 4);
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv6.vhlerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
goto drop;
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
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 (IPv6_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 IPv6 header\n");
|
2016-06-09 21:16:47 +02:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:50:01 +01:00
|
|
|
/* Make sure that all packet processing logic knows that there is an IPv6
|
|
|
|
* packet in the device buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
IFF_SET_IPv6(dev->d_flags);
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/* 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.
|
2015-01-15 15:03:56 +01:00
|
|
|
*
|
|
|
|
* The length reported in the IPv6 header is the length of the payload
|
2017-01-18 14:53:28 +01:00
|
|
|
* that follows the header. The device interface uses the d_len variable
|
|
|
|
* for holding the size of the entire packet, including the IP header but
|
2018-11-10 18:29:02 +01:00
|
|
|
* without the link layer header (subtracted out above).
|
|
|
|
*
|
|
|
|
* NOTE: The payload length in the includes the size of the Ipv6 extension
|
|
|
|
* options, but not the size of the IPv6 header.
|
2018-11-03 00:50:01 +01:00
|
|
|
*
|
|
|
|
* REVISIT: Length will be set to zero if the extension header carries
|
|
|
|
* a Jumbo payload option.
|
2012-02-18 23:09:09 +01:00
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2018-11-10 18:29:02 +01:00
|
|
|
paylen = ((uint16_t)ipv6->len[0] << 8) + (uint16_t)ipv6->len[1] +
|
|
|
|
IPv6_HDRLEN;
|
2015-01-23 23:40:18 +01:00
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
if (paylen < 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, paylen, false);
|
2018-11-10 18:29:02 +01:00
|
|
|
dev->d_len = paylen;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2022-11-23 12:39:40 +01:00
|
|
|
else if (paylen > 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;
|
|
|
|
}
|
|
|
|
|
2018-11-10 18:29:02 +01:00
|
|
|
/* Parse IPv6 extension headers (parsed but ignored) */
|
|
|
|
|
2023-02-07 09:15:57 +01:00
|
|
|
payload = IPBUF(IPv6_HDRLEN); /* Assume payload starts right after IPv6 header */
|
|
|
|
iphdrlen = IPv6_HDRLEN; /* Total length of the IPv6 header */
|
|
|
|
nxthdr = ipv6->proto; /* Next header determined by IPv6 header prototype */
|
2018-11-10 18:29:02 +01:00
|
|
|
|
|
|
|
while (ipv6_exthdr(nxthdr))
|
|
|
|
{
|
|
|
|
FAR struct ipv6_extension_s *exthdr;
|
|
|
|
uint16_t extlen;
|
|
|
|
|
|
|
|
/* Just skip over the extension header */
|
|
|
|
|
|
|
|
exthdr = (FAR struct ipv6_extension_s *)payload;
|
2023-01-09 13:47:11 +01:00
|
|
|
if (nxthdr == NEXT_FRAGMENT_EH)
|
|
|
|
{
|
|
|
|
extlen = EXTHDR_FRAG_LEN;
|
|
|
|
#ifdef CONFIG_NET_IPFRAG
|
|
|
|
isfrag = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extlen = EXTHDR_LEN((unsigned int)exthdr->len);
|
|
|
|
}
|
|
|
|
|
2018-11-10 18:29:02 +01:00
|
|
|
payload += extlen;
|
|
|
|
iphdrlen += extlen;
|
|
|
|
nxthdr = exthdr->nxthdr;
|
|
|
|
}
|
|
|
|
|
2024-03-13 10:04:55 +01:00
|
|
|
#ifdef CONFIG_NET_NAT66
|
|
|
|
/* Try NAT inbound, rule matching will be performed in NAT module. */
|
|
|
|
|
|
|
|
ipv6_nat_inbound(dev, ipv6);
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 23:26:49 +01:00
|
|
|
#ifdef CONFIG_NET_BROADCAST
|
|
|
|
/* Check for a multicast packet, which may be destined to us (even if
|
|
|
|
* there is no IP address yet assigned to the device). We only expect
|
|
|
|
* multicast packets destined for sockets that have joined a multicast
|
|
|
|
* group or for ICMPv6 Autoconfiguration and Neighbor discovery or ICMPv6
|
|
|
|
* MLD packets.
|
|
|
|
*
|
|
|
|
* We should actually pick off certain multicast address (all hosts
|
|
|
|
* multicast address, and the solicited-node multicast address). We
|
|
|
|
* will cheat here and accept all multicast packets that are sent to the
|
|
|
|
* ff00::/8 addresses (see net_is_addr_mcast).
|
2015-10-08 23:10:04 +02:00
|
|
|
*/
|
2007-12-01 00:12:11 +01:00
|
|
|
|
2018-11-09 23:26:49 +01:00
|
|
|
if (net_is_addr_mcast(ipv6->destipaddr))
|
2007-12-01 00:12:11 +01:00
|
|
|
{
|
2017-07-06 15:26:01 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD_BROADCAST
|
|
|
|
|
2021-01-30 01:58:47 +01:00
|
|
|
/* Packets sent to ffx0 are reserved, ffx1 are interface-local, and
|
|
|
|
* ffx2 are interface-local, and therefore, should not be forwarded
|
2018-10-27 20:12:37 +02:00
|
|
|
*/
|
|
|
|
|
2022-02-18 05:34:20 +01:00
|
|
|
if (((ipv6->destipaddr[0] & HTONS(0xff0f)) != HTONS(0xff00)) &&
|
|
|
|
((ipv6->destipaddr[0] & HTONS(0xff0f)) != HTONS(0xff01)) &&
|
|
|
|
((ipv6->destipaddr[0] & HTONS(0xff0f)) != HTONS(0xff02)))
|
2018-10-27 20:12:37 +02:00
|
|
|
{
|
|
|
|
/* Forward broadcast packets */
|
|
|
|
|
|
|
|
ipv6_forward_broadcast(dev, ipv6);
|
|
|
|
}
|
2017-07-06 15:26:01 +02:00
|
|
|
#endif
|
2018-11-09 23:26:49 +01:00
|
|
|
|
|
|
|
/* Fall through with no further address checks and handle the multicast
|
|
|
|
* address by its IPv6 nexthdr field.
|
|
|
|
*/
|
2007-12-01 00:12:11 +01:00
|
|
|
}
|
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
|
|
|
{
|
2017-06-28 16:13:57 +02:00
|
|
|
/* Check if the packet is destined for us. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2017-06-28 22:12:17 +02:00
|
|
|
if (!check_destipaddr(dev, ipv6))
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2017-06-28 23:39:21 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
|
|
|
/* Not destined for us, try to forward the packet */
|
|
|
|
|
2017-06-29 16:16:46 +02:00
|
|
|
ret = ipv6_forward(dev, ipv6);
|
2017-06-28 23:39:21 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* The packet was forwarded. Return success; d_len will
|
|
|
|
* be set appropriately by the forwarding logic: Cleared
|
2018-11-03 00:50:01 +01:00
|
|
|
* if the packet is forward via another device or non-
|
2017-06-28 23:39:21 +02:00
|
|
|
* 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-06-28 23:39:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#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 (nxthdr != IP_PROTO_UDP)
|
2021-04-28 22:18:16 +02:00
|
|
|
#endif
|
2017-06-28 23:39:21 +02:00
|
|
|
{
|
2021-01-30 01:58:47 +01:00
|
|
|
/* Not destined for us and not forwardable...
|
|
|
|
* drop the packet.
|
|
|
|
*/
|
2017-06-28 22:12:17 +02:00
|
|
|
|
2022-06-09 14:34:16 +02:00
|
|
|
ninfo("WARNING: Not destined for us... Dropping!\n");
|
2017-06-28 23:39:21 +02:00
|
|
|
goto drop;
|
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-04 17:33:41 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
|
|
|
/* Return success if the packet was forwarded. */
|
|
|
|
|
|
|
|
if (dev->d_len == 0)
|
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-18 09:43:52 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
|
|
|
|
/* In other cases, the device must be assigned a non-zero IP address
|
|
|
|
* (the all zero address is the "unspecified" address.
|
|
|
|
*/
|
|
|
|
|
2023-08-25 11:37:11 +02:00
|
|
|
if (!NETDEV_HAS_V6ADDR(dev))
|
2021-01-18 09:43:52 +01:00
|
|
|
{
|
|
|
|
nwarn("WARNING: No IP address assigned\n");
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2023-01-09 13:47:11 +01:00
|
|
|
#ifdef CONFIG_NET_IPFRAG
|
|
|
|
if (isfrag)
|
|
|
|
{
|
|
|
|
if (ipv6_fragin(dev) == OK)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.ipv6.fragerr++;
|
|
|
|
#endif
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-02-08 09:00:00 +01:00
|
|
|
#ifdef CONFIG_NET_IPFILTER
|
|
|
|
if (ipv6_filter_in(dev) != IPFILTER_TARGET_ACCEPT)
|
|
|
|
{
|
|
|
|
ninfo("Drop/Reject INPUT packet due to filter.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-02 19:06:57 +01:00
|
|
|
/* Now process the incoming packet according to the protocol specified in
|
|
|
|
* the next header IPv6 field.
|
|
|
|
*/
|
2015-01-24 14:29:43 +01:00
|
|
|
|
2018-11-02 19:06:57 +01:00
|
|
|
switch (nxthdr)
|
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 */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Forward the IPv6 TCP packet */
|
|
|
|
|
2018-11-03 00:50:01 +01:00
|
|
|
tcp_ipv6_input(dev, iphdrlen);
|
2017-04-02 19:15:46 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
2018-11-03 00:50:01 +01:00
|
|
|
/* TCP output comes through three different mechanisms. Either from:
|
2017-04-02 19:15:46 +02:00
|
|
|
*
|
|
|
|
* 1. TCP socket output. For the case of TCP output to an
|
|
|
|
* IEEE802.15.4, the TCP output is caught in the socket
|
2017-06-19 00:00:08 +02:00
|
|
|
* send()/sendto() logic and and redirected to 6LoWPAN logic.
|
2017-04-03 20:01:04 +02:00
|
|
|
* 2. TCP output from the TCP state machine. That will occur
|
2018-11-03 00:50:01 +01:00
|
|
|
* during TCP packet processing by the TCP state machine.
|
2017-04-03 20:01:04 +02:00
|
|
|
* 3. TCP output resulting from TX or timer polling
|
|
|
|
*
|
2017-07-09 19:35:26 +02:00
|
|
|
* Case 3 is handled here. Logic here detects if (1) an attempt
|
2017-04-03 20:01:04 +02:00
|
|
|
* to return with d_len > 0 and (2) that the device is an
|
2017-06-19 00:00:08 +02:00
|
|
|
* IEEE802.15.4 MAC network driver. Under those conditions, 6LoWPAN
|
2017-04-03 20:01:04 +02:00
|
|
|
* logic will be called to create the IEEE80215.4 frames.
|
2017-04-02 19:15:46 +02:00
|
|
|
*/
|
|
|
|
|
2017-04-03 20:01:04 +02:00
|
|
|
if (dev->d_len > 0 && dev->d_lltype == CONFIG_NET_6LOWPAN)
|
|
|
|
{
|
2017-06-19 00:00:08 +02:00
|
|
|
/* Let 6LoWPAN handle the TCP output */
|
2017-04-02 19:15:46 +02:00
|
|
|
|
2017-06-28 23:39:21 +02:00
|
|
|
sixlowpan_tcp_send(dev, dev, ipv6);
|
2017-04-02 19:15:46 +02:00
|
|
|
|
|
|
|
/* Drop the packet in the d_buf */
|
|
|
|
|
|
|
|
goto drop;
|
2017-04-03 20:01:04 +02:00
|
|
|
}
|
2017-04-02 19:15:46 +02:00
|
|
|
#endif /* CONFIG_NET_6LOWPAN */
|
|
|
|
break;
|
|
|
|
#endif /* NET_TCP_HAVE_STACK */
|
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 */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Forward the IPv6 UDP packet */
|
|
|
|
|
2018-11-03 00:50:01 +01:00
|
|
|
udp_ipv6_input(dev, iphdrlen);
|
2007-11-07 19:54:35 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2018-11-02 19:06:57 +01:00
|
|
|
/* Check for ICMP input */
|
|
|
|
|
2018-11-09 18:25:57 +01:00
|
|
|
#ifdef NET_ICMPv6_HAVE_STACK
|
2014-07-05 00:38:51 +02:00
|
|
|
case IP_PROTO_ICMP6: /* ICMP6 input */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Forward the ICMPv6 packet */
|
|
|
|
|
2018-11-05 15:56:07 +01:00
|
|
|
icmpv6_input(dev, iphdrlen);
|
2017-07-09 19:35:26 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
/* All outgoing ICMPv6 messages come through one of two mechanisms:
|
|
|
|
*
|
|
|
|
* 1. The output from internal ICMPv6 message passing. These
|
|
|
|
* outgoing messages will use device polling and will be
|
|
|
|
* handled elsewhere.
|
|
|
|
* 2. ICMPv6 output resulting from TX or timer polling.
|
|
|
|
*
|
|
|
|
* Case 2 is handled here. Logic here detects if (1) an attempt
|
|
|
|
* to return with d_len > 0 and (2) that the device is an
|
|
|
|
* IEEE802.15.4 MAC network driver. Under those conditions, 6LoWPAN
|
|
|
|
* logic will be called to create the IEEE80215.4 frames.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_len > 0 && dev->d_lltype == CONFIG_NET_6LOWPAN)
|
|
|
|
{
|
|
|
|
/* Let 6LoWPAN handle the ICMPv6 output */
|
|
|
|
|
|
|
|
sixlowpan_icmpv6_send(dev, dev, ipv6);
|
|
|
|
|
|
|
|
/* Drop the packet in the d_buf */
|
|
|
|
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_6LOWPAN */
|
|
|
|
break;
|
2018-11-09 18:25:57 +01:00
|
|
|
#endif /* NET_ICMPv6_HAVE_STACK */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
default: /* Unrecognized/unsupported protocol */
|
2018-11-03 00:50:01 +01:00
|
|
|
nwarn("WARNING: Unrecognized IP protocol: %04x\n", ipv6->proto);
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv6.protoerr++;
|
2007-11-07 19:54:35 +01:00
|
|
|
#endif
|
|
|
|
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-26 19:13:21 +01:00
|
|
|
done:
|
|
|
|
#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
|
|
|
|
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:
|
2018-11-03 00:50:01 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.ipv6.drop++;
|
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-09 13:47:11 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv6_exthdr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check whether it is an IPv6 extension header.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* The next header value extracted from an IPv6 frame.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Return true if the next header value is an IPv6 extension header.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool ipv6_exthdr(uint8_t nxthdr)
|
|
|
|
{
|
|
|
|
switch (nxthdr)
|
|
|
|
{
|
|
|
|
case NEXT_HOPBYBOT_EH: /* Hop-by-Hop Options Header */
|
|
|
|
case NEXT_ENCAP_EH: /* Encapsulated IPv6 Header */
|
|
|
|
case NEXT_ROUTING_EH: /* Routing Header */
|
|
|
|
case NEXT_FRAGMENT_EH: /* Fragment Header */
|
|
|
|
case NEXT_RRSVP_EH: /* Resource ReSerVation Protocol */
|
|
|
|
case NEXT_ENCAPSEC_EH: /* Encapsulating Security Payload */
|
|
|
|
case NEXT_AUTH_EH: /* Authentication Header */
|
|
|
|
case NEXT_DESTOPT_EH: /* Destination Options Header */
|
|
|
|
case NEXT_MOBILITY_EH: /* Mobility */
|
|
|
|
case NEXT_HOSTID_EH: /* Host Identity Protocol */
|
|
|
|
case NEXT_SHIM6_EH: /* Shim6 Protocol */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case NEXT_NOHEADER: /* No next header */
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv6_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive an IPv6 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 IPv6 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.
|
|
|
|
*
|
|
|
|
* If this function returns to the network driver with dev->d_len > 0,
|
|
|
|
* that is an indication to the driver that there is an outgoing response
|
|
|
|
* to this input.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv6_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 = ipv6_in(dev);
|
|
|
|
|
|
|
|
dev->d_buf = buf;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return netdev_input(dev, ipv6_in, true);
|
|
|
|
}
|
2015-01-15 15:03:56 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|