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
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* 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.
|
2017-07-04 18:19:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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-08-08 22:24:12 +02:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
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.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-05 19:01:16 +02:00
|
|
|
* 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 */
|
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
dev->d_appdata = IPBUF(IPv4UDP_HDRLEN);
|
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 */
|
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
dev->d_appdata = IPBUF(IPv6UDP_HDRLEN);
|
2017-07-05 19:01:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: ipfwd_eventhandler
|
2017-07-05 19:01:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-06-23 22:09:06 +02:00
|
|
|
* This function is called with the network locked to perform the actual
|
2017-07-05 19:01:16 +02:00
|
|
|
* send operation when polled by the lower, device interfacing layer.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-06-23 22:09:06 +02:00
|
|
|
* dev The structure of the network driver that generated the
|
|
|
|
* event
|
|
|
|
* pvpriv An instance of struct forward_s cast to (void *)
|
2017-07-05 19:01:16 +02:00
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Modified value of the input flags
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-19 15:01:46 +01:00
|
|
|
static uint16_t ipfwd_eventhandler(FAR struct net_driver_s *dev,
|
2017-08-29 22:08:04 +02:00
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2017-07-05 19:01:16 +02:00
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct forward_s *fwd = pvpriv;
|
2017-07-05 19:01:16 +02:00
|
|
|
|
|
|
|
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
|
2017-08-29 22:08:04 +02:00
|
|
|
* by a sendto event handler 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-05 19:01:16 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
{
|
2023-01-11 09:09:20 +01:00
|
|
|
/* Copy the user data into d_appdata and send it. */
|
|
|
|
|
|
|
|
devif_forward(fwd);
|
|
|
|
flags &= ~DEVPOLL_MASK;
|
|
|
|
|
2017-07-05 19:01:16 +02:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2022-08-08 04:21:03 +02:00
|
|
|
iob_free_chain(fwd->f_iob);
|
2017-07-05 19:01:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2017-08-29 22:08:04 +02:00
|
|
|
* will set up a send event handler that will perform the actual send
|
|
|
|
* asynchronously and must return without waiting for the send to
|
2017-07-08 04:19:26 +02:00
|
|
|
* 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-08-29 22:08:04 +02:00
|
|
|
fwd->f_cb->event = ipfwd_eventhandler;
|
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-08-08 22:24:12 +02:00
|
|
|
#endif /* CONFIG_NET_IPFORWARD */
|