* net/netforward/netfwd_forward.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <net/if.h>
#include <nuttx/mutex.h>
#include <nuttx/mm/iob.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/netstats.h>
#include "devif/devif.h"
#include "netdev/netdev.h"
#include "arp/arp.h"
#include "neighbor/neighbor.h"
#include "netforward/netforward.h"
#ifdef CONFIG_NET_IPFORWARD
* 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.
*
* Input 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)
{
FAR struct net_driver_s *dev = fwd->f_dev;
if (fwd->f_domain == PF_INET)
{
IFF_SET_IPv4(dev->d_flags);
dev->d_appdata = IPBUF(IPv4UDP_HDRLEN);
}
else
{
IFF_SET_IPv6(dev->d_flags);
dev->d_appdata = IPBUF(IPv6UDP_HDRLEN);
}
}
#endif
* Name: netfwd_eventhandler
*
* Description:
* This function is called with the network locked to perform the actual
* send operation when polled by the lower, device interfacing layer.
*
* Input Parameters:
* dev The structure of the network driver that generated the
* event
* 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
*
****************************************************************************/
static uint32_t netfwd_eventhandler(FAR struct net_driver_s *dev,
FAR void *pvpriv, uint32_t flags)
{
FAR struct forward_s *fwd = pvpriv;
ninfo("flags: %" PRIx32 "\n", flags);
DEBUGASSERT(fwd != NULL && fwd->f_iob != NULL && fwd->f_dev != NULL);
if (dev == fwd->f_dev)
{
* the wait now with an error.
*/
if ((flags & NETDEV_DOWN) != 0)
{
nwarn("WARNING: Network is down... Dropping\n");
netfwd_dropstats(fwd);
}
* 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.
*/
else if (dev->d_sndlen > 0 || (flags & IPFWD_NEWDATA) != 0)
{
* Wait for the next polling cycle and check again.
*/
return flags;
}
else
{
devif_forward(fwd);
flags &= ~IPFWD_POLL;
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
* 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.
*/
forward_ipselect(fwd);
#endif
}
fwd->f_cb->flags = 0;
fwd->f_cb->priv = NULL;
fwd->f_cb->event = NULL;
netfwd_callback_free(dev, fwd->f_cb);
if (fwd->f_iob != NULL)
{
iob_free_chain(fwd->f_iob);
}
netfwd_free(fwd);
}
return flags;
}
* Name: netfwd_forward_work
*
* Description:
* Perform the forwarding operation on the worker thread.
*
* Input Parameters:
* arg - An initialized instance of the common forwarding structure that
* includes everything needed to perform the forwarding operation.
*
****************************************************************************/
static void netfwd_forward_work(void *arg)
{
FAR struct forward_s *fwd = (FAR struct forward_s *)arg;
netdev_txnotify_dev(fwd->f_dev, IPFWD_POLL);
}
* Public Functions
****************************************************************************/
* Name: netfwd_forward
*
* Description:
* Called by the IP forwarding logic when a packet is received on one
* network device, but must be forwarded on another network device.
*
* Set up to forward the packet on the specified device. This function
* will set up a send event handler that will perform the actual send
* asynchronously and must return without waiting for the send to
* complete.
*
* 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.
*
****************************************************************************/
int netfwd_forward(FAR struct net_driver_s *dev, FAR struct forward_s *fwd)
{
FAR struct net_driver_s *fwddev;
DEBUGASSERT(fwd != NULL && fwd->f_iob != NULL && fwd->f_dev != NULL);
fwddev = fwd->f_dev;
fwd->f_cb = netfwd_callback_alloc(fwddev);
if (fwd->f_cb != NULL)
{
fwd->f_cb->flags = (IPFWD_POLL | NETDEV_DOWN);
fwd->f_cb->priv = (FAR void *)fwd;
fwd->f_cb->event = netfwd_eventhandler;
if (nxrmutex_trylock(&fwddev->d_lock) != OK)
{
if (work_available(&fwddev->d_fwdwork))
{
work_queue(LPWORK, &fwddev->d_fwdwork, netfwd_forward_work,
fwd, 0);
}
}
else
{
FAR struct iob_s *iob = dev->d_iob;
FAR uint8_t *buf = dev->d_buf;
int len = dev->d_len;
* current d_iob and other resources when forwarding recursion
*/
dev->d_iob = NULL;
dev->d_buf = NULL;
dev->d_len = 0;
netdev_txnotify_dev(fwddev, IPFWD_POLL);
dev->d_iob = iob;
dev->d_buf = buf;
dev->d_len = len;
nxrmutex_unlock(&fwddev->d_lock);
}
return OK;
}
return -EBUSY;
}
#endif