2017-07-04 18:19:52 +02:00
|
|
|
/****************************************************************************
|
2017-07-08 04:19:26 +02:00
|
|
|
* net/ipforward/ipfwd_forward.c
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
#include <assert.h>
|
2017-07-04 18:19:52 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
|
2017-07-04 18:19:52 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2017-07-05 20:40:26 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2017-07-04 18:19:52 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
2017-07-05 19:01:16 +02:00
|
|
|
#include <nuttx/net/netstats.h>
|
2017-07-04 18:19:52 +02:00
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "netdev/netdev.h"
|
2017-07-05 20:40:26 +02:00
|
|
|
#include "arp/arp.h"
|
|
|
|
#include "neighbor/neighbor.h"
|
2017-07-08 04:19:26 +02:00
|
|
|
#include "ipforward/ipforward.h"
|
2017-07-04 18:19:52 +02:00
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
#if defined(CONFIG_NET_IPFORWARD) && defined(CONFIG_NETDEV_MULTINIC)
|
2017-07-05 19:01:16 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: forward_ipselect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If both IPv4 and IPv6 support are enabled, then we will need to select
|
|
|
|
* which one to use when generating the outgoing packet. If only one
|
|
|
|
* domain is selected, then the setup is already in place and we need do
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fwd - The forwarding state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
static inline void forward_ipselect(FAR struct forward_s *fwd)
|
|
|
|
{
|
2017-07-08 04:19:26 +02:00
|
|
|
FAR struct net_driver_s *dev = fwd->f_dev;
|
2017-07-07 00:19:10 +02:00
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
/* Select IPv4 or IPv6 */
|
|
|
|
|
2017-07-08 02:33:06 +02:00
|
|
|
if (fwd->f_domain == PF_INET)
|
2017-07-05 19:01:16 +02:00
|
|
|
{
|
2017-07-08 04:19:26 +02:00
|
|
|
/* Clear a bit in the d_flags to distinguish this from an IPv6 packet */
|
|
|
|
|
|
|
|
IFF_SET_IPv4(dev->d_flags);
|
|
|
|
|
|
|
|
/* Set the offset to the beginning of the UDP data payload */
|
|
|
|
|
|
|
|
dev->d_appdata = &dev->d_buf[IPv4UDP_HDRLEN + NET_LL_HDRLEN(dev)];
|
2017-07-05 19:01:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-08 04:19:26 +02:00
|
|
|
/* Set a bit in the d_flags to distinguish this from an IPv6 packet */
|
|
|
|
|
|
|
|
IFF_SET_IPv6(dev->d_flags);
|
|
|
|
|
|
|
|
/* Set the offset to the beginning of the UDP data payload */
|
|
|
|
|
|
|
|
dev->d_appdata = &dev->d_buf[IPv6_HDRLEN + NET_LL_HDRLEN(dev)];
|
2017-07-05 19:01:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 04:19:26 +02:00
|
|
|
* Name: ipfwd_addrchk
|
2017-07-05 19:01:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the destination IP address is in the IPv4 ARP or IPv6 Neighbor
|
|
|
|
* tables. If not, then the send won't actually make it out... it will be
|
|
|
|
* replaced with an ARP request (IPv4) or a Neighbor Solicitation (IPv6).
|
|
|
|
*
|
|
|
|
* NOTE 1: This could be an expensive check if there are a lot of
|
|
|
|
* entries in the ARP or Neighbor tables.
|
|
|
|
*
|
|
|
|
* NOTE 2: If we are actually harvesting IP addresses on incoming IP
|
|
|
|
* packets, then this check should not be necessary; the MAC mapping
|
|
|
|
* should already be in the ARP table in many cases (IPv4 only).
|
|
|
|
*
|
|
|
|
* NOTE 3: If CONFIG_NET_ARP_SEND then we can be assured that the IP
|
|
|
|
* address mapping is already in the ARP table.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fwd - The forwarding state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-07-05 20:40:26 +02:00
|
|
|
* true - The Ethernet MAC address is in the ARP or Neighbor table (OR
|
|
|
|
* the network device is not Ethernet).
|
2017-07-05 19:01:16 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2017-07-08 04:19:26 +02:00
|
|
|
static inline bool ipfwd_addrchk(FAR struct forward_s *fwd)
|
2017-07-05 19:01:16 +02:00
|
|
|
{
|
2017-07-07 00:19:10 +02:00
|
|
|
DEBUGASSERT(fwd != NULL && fwd->f_iob != NULL && fwd->f_dev != NULL);
|
|
|
|
|
2017-07-05 20:40:26 +02:00
|
|
|
/* REVISIT: Could the MAC address not also be in a routing table? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_MULTILINK
|
|
|
|
if (fwd->f_dev->d_lltype != NET_LL_ETHERNET)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-07-08 02:33:06 +02:00
|
|
|
if (fwd->f_domain == PF_INET)
|
2017-07-05 19:01:16 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if !defined(CONFIG_NET_ARP_IPIN) && !defined(CONFIG_NET_ARP_SEND)
|
2017-07-08 05:32:10 +02:00
|
|
|
FAR stuct ipv4_hdr_s *ipv4 = (FAR stuct ipv4_hdr_s *)fwd->f_iob->io_data;
|
|
|
|
return (arp_find(ipv4->destipaddr) != NULL);
|
2017-07-05 19:01:16 +02:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if !defined(CONFIG_NET_ICMPv6_NEIGHBOR)
|
2017-07-08 05:32:10 +02:00
|
|
|
FAR stuct ipv6_hdr_s *ipv4 = (FAR stuct ipv6_hdr_s *)fwd->f_iob->io_data;
|
|
|
|
return (neighbor_findentry(ipv6->destipaddr) != NULL);
|
2017-07-05 19:01:16 +02:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_NET_ETHERNET */
|
2017-07-08 04:19:26 +02:00
|
|
|
# define ipfwd_addrchk(r) (true)
|
2017-07-05 19:01:16 +02:00
|
|
|
#endif /* CONFIG_NET_ETHERNET */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 04:19:26 +02:00
|
|
|
* Name: ipfwd_interrupt
|
2017-07-05 19:01:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called from the interrupt level to perform the actual
|
|
|
|
* send operation when polled by the lower, device interfacing layer.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* dev The structure of the network driver that caused the interrupt
|
2017-07-08 04:19:26 +02:00
|
|
|
* conn An instance of the forwarding structure cast to void *
|
2017-07-05 19:01:16 +02:00
|
|
|
* pvpriv An instance of struct forward_s cast to void*
|
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Modified value of the input flags
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
static uint16_t ipfwd_interrupt(FAR struct net_driver_s *dev, FAR void *conn,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2017-07-05 19:01:16 +02:00
|
|
|
{
|
|
|
|
FAR struct forward_s *fwd = (FAR struct forward_s *)pvpriv;
|
|
|
|
|
|
|
|
ninfo("flags: %04x\n", flags);
|
2017-07-07 00:19:10 +02:00
|
|
|
DEBUGASSERT(fwd != NULL && fwd->f_iob != NULL && fwd->f_dev != NULL);
|
2017-07-05 19:01:16 +02:00
|
|
|
|
|
|
|
/* Make sure that this is from the forwarding device */
|
|
|
|
|
|
|
|
if (dev == fwd->f_dev)
|
|
|
|
{
|
|
|
|
/* If the network device has gone down, then we will have terminate
|
|
|
|
* the wait now with an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
|
|
|
/* Terminate the transfer with an error. */
|
|
|
|
|
|
|
|
nwarn("WARNING: Network is down... Dropping\n");
|
2017-07-08 04:19:26 +02:00
|
|
|
ipfwd_dropstats(fwd);
|
2017-07-05 19:01:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the outgoing packet is available. It may have been claimed
|
|
|
|
* by a sendto interrupt serving a different thread -OR- if the output
|
|
|
|
* buffer currently contains unprocessed incoming data. In these cases
|
|
|
|
* we will just have to wait for the next polling cycle.
|
|
|
|
*/
|
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
else if (dev->d_sndlen > 0 || (flags & IPFWD_NEWDATA) != 0)
|
2017-07-05 19:01:16 +02:00
|
|
|
{
|
|
|
|
/* Another thread has beat us sending data or the buffer is busy,
|
|
|
|
* Wait for the next polling cycle and check again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It looks like we are good to forward the data */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
/* If both IPv4 and IPv6 support are enabled, then we will need to
|
|
|
|
* select which one to use when generating the outgoing packet.
|
|
|
|
* If only one domain is selected, then the setup is already in
|
|
|
|
* place and we need do nothing.
|
|
|
|
*/
|
|
|
|
|
2017-07-05 23:12:29 +02:00
|
|
|
forward_ipselect(fwd);
|
2017-07-05 19:01:16 +02:00
|
|
|
#endif
|
|
|
|
/* Copy the user data into d_appdata and send it. */
|
|
|
|
|
|
|
|
devif_forward(fwd);
|
2017-07-08 17:56:08 +02:00
|
|
|
flags &= ~DEVPOLL_MASK;
|
2017-07-05 19:01:16 +02:00
|
|
|
|
|
|
|
/* Check if the destination IP address is in the ARP or Neighbor
|
|
|
|
* table. If not, then the send won't actually make it out... it
|
|
|
|
* will be replaced with an ARP request or Neighbor Solicitation.
|
|
|
|
*/
|
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
if (!ipfwd_addrchk(fwd))
|
2017-07-05 19:01:16 +02:00
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the allocated callback structure */
|
|
|
|
|
|
|
|
fwd->f_cb->flags = 0;
|
|
|
|
fwd->f_cb->priv = NULL;
|
|
|
|
fwd->f_cb->event = NULL;
|
|
|
|
|
2017-07-08 17:56:08 +02:00
|
|
|
ipfwd_callback_free(dev, fwd->f_cb);
|
2017-07-05 19:01:16 +02:00
|
|
|
|
|
|
|
/* Free any IOBs */
|
|
|
|
|
|
|
|
if (fwd->f_iob != NULL)
|
|
|
|
{
|
|
|
|
iob_free_chain(fwd->f_iob);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And release the forwarding state structure */
|
|
|
|
|
2017-07-08 02:45:58 +02:00
|
|
|
ipfwd_free(fwd);
|
2017-07-05 19:01:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
2017-07-04 18:19:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 04:19:26 +02:00
|
|
|
* Name: ipfwd_forward
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-07-08 04:19:26 +02:00
|
|
|
* Called by the IP forwarding logic when a packet is received on one
|
|
|
|
* network device, but must be forwarded on another network device.
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
2017-07-08 04:19:26 +02:00
|
|
|
* Set up to forward the packet on the specified device. This function
|
|
|
|
* will set up a send "interrupt" handler that will perform the actual
|
|
|
|
* send asynchronously and must return without waiting for the send to
|
|
|
|
* complete.
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fwd - An initialized instance of the common forwarding structure that
|
|
|
|
* includes everything needed to perform the forwarding operation.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if the packet was successfully forwarded; A negated
|
|
|
|
* errno value is returned if the packet is not forwardable. In that
|
|
|
|
* latter case, the caller should free the IOB list and drop the packet.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
int ipfwd_forward(FAR struct forward_s *fwd)
|
2017-07-04 18:19:52 +02:00
|
|
|
{
|
2017-07-07 00:19:10 +02:00
|
|
|
DEBUGASSERT(fwd != NULL && fwd->f_iob != NULL && fwd->f_dev != NULL);
|
2017-07-04 18:19:52 +02:00
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
/* Set up the callback in the connection */
|
2017-07-04 18:19:52 +02:00
|
|
|
|
2017-07-08 17:56:08 +02:00
|
|
|
fwd->f_cb = ipfwd_callback_alloc(fwd->f_dev);
|
2017-07-05 19:01:16 +02:00
|
|
|
if (fwd->f_cb != NULL)
|
|
|
|
{
|
2017-07-08 04:19:26 +02:00
|
|
|
fwd->f_cb->flags = (IPFWD_POLL | NETDEV_DOWN);
|
2017-07-05 19:01:16 +02:00
|
|
|
fwd->f_cb->priv = (FAR void *)fwd;
|
2017-07-08 04:19:26 +02:00
|
|
|
fwd->f_cb->event = ipfwd_interrupt;
|
2017-07-04 18:19:52 +02:00
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
/* Notify the device driver of the availability of TX data */
|
2017-07-04 18:19:52 +02:00
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
netdev_txnotify_dev(fwd->f_dev);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EBUSY;
|
2017-07-04 18:19:52 +02:00
|
|
|
}
|
2017-07-05 19:01:16 +02:00
|
|
|
|
2017-07-08 04:19:26 +02:00
|
|
|
#endif /* CONFIG_NET_IPFORWARD && CONFIG_NETDEV_MULTINIC */
|