2017-07-05 00:19:52 +08:00
|
|
|
/****************************************************************************
|
2017-07-08 08:45:58 +08:00
|
|
|
* net/ipforward/ipfwd_alloc.c
|
2017-07-05 00:19:52 +08: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. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "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
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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>
|
|
|
|
|
|
|
|
#include <string.h>
|
2017-07-07 09:37:01 +08:00
|
|
|
#include <assert.h>
|
2017-07-05 00:19:52 +08:00
|
|
|
#include <errno.h>
|
2017-07-07 09:37:01 +08:00
|
|
|
#include <debug.h>
|
2017-07-05 00:19:52 +08:00
|
|
|
|
2017-07-08 11:32:10 +08:00
|
|
|
#include <nuttx/net/tcp.h>
|
|
|
|
#include <nuttx/net/udp.h>
|
|
|
|
#include <nuttx/net/icmp.h>
|
|
|
|
#include <nuttx/net/icmpv6.h>
|
|
|
|
|
2017-07-08 08:45:58 +08:00
|
|
|
#include "ipforward/ipforward.h"
|
2017-07-05 00:19:52 +08:00
|
|
|
|
2017-08-09 04:24:12 +08:00
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
2017-07-05 00:19:52 +08:00
|
|
|
|
2017-07-08 11:32:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
# define L2_MAXHDRLEN IPv6_HDRLEN
|
|
|
|
#else
|
|
|
|
# define L2_MAXHDRLEN IPv4_HDRLEN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_TCP)
|
2017-08-27 00:00:47 +08:00
|
|
|
# define L3_MAXHDRLEN TCP_HDRLEN /* Could be up to TCP_MAX_HDRLEN */
|
2017-07-08 11:32:10 +08:00
|
|
|
#elif defined(CONFIG_NET_UDP)
|
|
|
|
# define L3_MAXHDRLEN UDP_HDRLEN
|
|
|
|
#elif defined(CONFIG_NET_ICMPv6)
|
|
|
|
# define L3_MAXHDRLEN ICMPv6_HDRLEN
|
|
|
|
#elif defined(CONFIG_NET_ICMP)
|
|
|
|
# define L3_MAXHDRLEN ICMP_HDRLEN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_HDRLEN (L2_MAXHDRLEN + L3_MAXHDRLEN)
|
|
|
|
|
2017-07-05 00:19:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is an array of pre-allocating forwarding structures */
|
|
|
|
|
|
|
|
static struct forward_s g_fwdpool[CONFIG_NET_IPFORWARD_NSTRUCT];
|
|
|
|
|
|
|
|
/* This is a list of free forwarding structures */
|
|
|
|
|
|
|
|
static FAR struct forward_s *g_fwdfree;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 08:45:58 +08:00
|
|
|
* Name: ipfwd_initialize
|
2017-07-05 00:19:52 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the struct forward_s allocator.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called early in system initialization.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-08 08:45:58 +08:00
|
|
|
void ipfwd_initialize(void)
|
2017-07-05 00:19:52 +08:00
|
|
|
{
|
|
|
|
FAR struct forward_s *fwd;
|
|
|
|
int i;
|
|
|
|
|
2017-07-07 09:37:01 +08:00
|
|
|
/* The IOB size must be such that the maximum L2 and L3 headers fit into
|
|
|
|
* the contiguous memory of the first IOB in the IOB chain.
|
|
|
|
*/
|
|
|
|
|
2017-07-08 11:32:10 +08:00
|
|
|
DEBUGASSERT(MAX_HDRLEN <= CONFIG_IOB_BUFSIZE);
|
2017-07-07 09:37:01 +08:00
|
|
|
|
2017-07-05 00:19:52 +08:00
|
|
|
/* Add all pre-allocated forwarding structures to the free list */
|
|
|
|
|
|
|
|
g_fwdfree = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_NET_IPFORWARD_NSTRUCT; i++)
|
|
|
|
{
|
|
|
|
fwd = &g_fwdpool[i];
|
|
|
|
fwd->f_flink = g_fwdfree;
|
|
|
|
g_fwdfree = fwd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 08:45:58 +08:00
|
|
|
* Name: ipfwd_alloc
|
2017-07-05 00:19:52 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a forwarding structure by removing a pre-allocated entry from
|
|
|
|
* a free list.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Caller holds the network lock. Mutually excluvive access to the free
|
|
|
|
* list is assured by this lock.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-08 08:45:58 +08:00
|
|
|
FAR struct forward_s *ipfwd_alloc(void)
|
2017-07-05 00:19:52 +08:00
|
|
|
{
|
|
|
|
FAR struct forward_s *fwd;
|
|
|
|
|
|
|
|
fwd = g_fwdfree;
|
|
|
|
if (fwd != NULL)
|
|
|
|
{
|
|
|
|
g_fwdfree = fwd->f_flink;
|
|
|
|
memset (fwd, 0, sizeof(struct forward_s));
|
|
|
|
}
|
|
|
|
|
|
|
|
return fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 08:45:58 +08:00
|
|
|
* Name: ipfwd_free
|
2017-07-05 00:19:52 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a forwarding structure by adding it to a free list.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Caller holds the network lock. Mutually excluvive access to the free
|
|
|
|
* list is assured by this lock.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-08 08:45:58 +08:00
|
|
|
void ipfwd_free(FAR struct forward_s *fwd)
|
2017-07-05 00:19:52 +08:00
|
|
|
{
|
|
|
|
fwd->f_flink = g_fwdfree;
|
|
|
|
g_fwdfree = fwd;
|
|
|
|
}
|
|
|
|
|
2017-08-09 04:24:12 +08:00
|
|
|
#endif /* CONFIG_NET_IPFORWARD */
|