2015-03-11 13:52:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/net/tun.c
|
|
|
|
*
|
2016-10-24 23:17:01 +02:00
|
|
|
* Copyright (C) 2015-2016 Max Nekludov. All rights reserved.
|
2015-03-11 13:52:56 +01:00
|
|
|
* Author: Max Nekludov <macscomp@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <debug.h>
|
2019-05-22 02:57:54 +02:00
|
|
|
#include <poll.h>
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2017-08-20 19:46:43 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
# include <nuttx/net/pkt.h>
|
|
|
|
#endif
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2016-12-03 23:28:19 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2023-01-10 13:03:02 +01:00
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
#include <nuttx/net/net.h>
|
2023-08-18 22:23:01 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2015-03-11 13:52:56 +01:00
|
|
|
#include <nuttx/net/netdev.h>
|
2017-08-20 19:46:43 +02:00
|
|
|
#include <nuttx/net/ethernet.h>
|
2015-03-11 13:52:56 +01:00
|
|
|
#include <nuttx/net/tun.h>
|
|
|
|
|
2017-08-20 19:46:43 +02:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TUN)
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2018-11-09 16:50:10 +01:00
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* If processing is not done at the interrupt level, then high priority
|
|
|
|
* work queue support is required.
|
|
|
|
*/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
#if !defined(CONFIG_SCHED_WORKQUEUE)
|
2016-11-18 16:22:49 +01:00
|
|
|
# error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE)
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
2018-11-21 14:57:26 +01:00
|
|
|
/* The low priority work queue is preferred. If it is not enabled, LPWORK
|
|
|
|
* will be the same as HPWORK.
|
|
|
|
*
|
|
|
|
* NOTE: However, the network should NEVER run on the high priority work
|
|
|
|
* queue! That queue is intended only to service short back end interrupt
|
|
|
|
* processing that never suspends. Suspending the high priority work queue
|
|
|
|
* may bring the system to its knees!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TUNWORK LPWORK
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* CONFIG_TUN_NINTERFACES determines the number of physical interfaces
|
|
|
|
* that will be supported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_TUN_NINTERFACES
|
2018-04-20 14:52:49 +02:00
|
|
|
# define CONFIG_TUN_NINTERFACES 1
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* This is a helper pointer for accessing the contents of the Ethernet
|
|
|
|
* header.
|
|
|
|
*/
|
2018-04-20 14:52:49 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2023-03-06 12:58:06 +01:00
|
|
|
# define ETHBUF ((FAR struct eth_hdr_s *)NETLLBUF)
|
2018-04-20 14:52:49 +02:00
|
|
|
#endif
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The tun_device_s encapsulates all state information for a single hardware
|
|
|
|
* interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct tun_device_s
|
|
|
|
{
|
2019-02-24 16:43:30 +01:00
|
|
|
bool bifup; /* true:ifup false:ifdown */
|
|
|
|
bool read_wait;
|
2020-02-02 15:02:17 +01:00
|
|
|
bool write_wait;
|
2019-02-24 16:43:30 +01:00
|
|
|
struct work_s work; /* For deferring poll work to the work queue */
|
2015-03-11 13:52:56 +01:00
|
|
|
FAR struct pollfd *poll_fds;
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock;
|
2019-02-24 16:43:30 +01:00
|
|
|
sem_t read_wait_sem;
|
2020-02-02 15:02:17 +01:00
|
|
|
sem_t write_wait_sem;
|
2015-03-11 13:52:56 +01:00
|
|
|
size_t read_d_len;
|
|
|
|
size_t write_d_len;
|
|
|
|
|
2019-02-24 16:43:30 +01:00
|
|
|
/* These packet buffer arrays required 16-bit alignment. That alignment
|
|
|
|
* is assured only by the preceding wide data types.
|
|
|
|
*/
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
FAR struct iob_s *read_buf;
|
|
|
|
FAR struct iob_s *write_buf;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* This holds the information visible to the NuttX network */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
struct net_driver_s dev; /* Interface understood by the network */
|
2015-03-11 13:52:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tun_driver_s
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock;
|
2022-09-06 08:18:45 +02:00
|
|
|
uint8_t free_tuns;
|
2015-03-11 13:52:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Common TX logic */
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
static void tun_fd_transmit(FAR struct tun_device_s *priv);
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txpoll(FAR struct net_driver_s *dev);
|
2018-04-20 14:52:49 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txpoll_tap(FAR struct net_driver_s *dev);
|
2018-04-20 14:52:49 +02:00
|
|
|
#endif
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txpoll_tun(FAR struct net_driver_s *dev);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/* Interrupt handling */
|
|
|
|
|
2016-10-24 23:17:01 +02:00
|
|
|
static void tun_net_receive(FAR struct tun_device_s *priv);
|
2018-04-20 14:52:49 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
static void tun_net_receive_tap(FAR struct tun_device_s *priv);
|
|
|
|
#endif
|
|
|
|
static void tun_net_receive_tun(FAR struct tun_device_s *priv);
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
static void tun_txdone(FAR struct tun_device_s *priv);
|
|
|
|
|
|
|
|
/* NuttX callback functions */
|
|
|
|
|
|
|
|
static int tun_ifup(FAR struct net_driver_s *dev);
|
|
|
|
static int tun_ifdown(FAR struct net_driver_s *dev);
|
|
|
|
static int tun_txavail(FAR struct net_driver_s *dev);
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2017-07-09 19:35:26 +02:00
|
|
|
static int tun_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
|
2015-03-11 13:52:56 +01:00
|
|
|
static int tun_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
static int tun_dev_init(FAR struct tun_device_s *priv,
|
|
|
|
FAR struct file *filep,
|
2018-11-09 16:50:10 +01:00
|
|
|
FAR const char *devfmt, bool tun);
|
2023-10-24 09:20:33 +02:00
|
|
|
static void tun_dev_uninit(FAR struct tun_device_s *priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/* File interface */
|
|
|
|
|
|
|
|
static int tun_close(FAR struct file *filep);
|
|
|
|
static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
|
|
|
static int tun_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static struct tun_driver_s g_tun =
|
|
|
|
{
|
|
|
|
NXMUTEX_INITIALIZER,
|
|
|
|
};
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
static struct tun_device_s g_tun_devices[CONFIG_TUN_NINTERFACES];
|
|
|
|
|
|
|
|
static const struct file_operations g_tun_file_ops =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_close, /* close */
|
|
|
|
tun_read, /* read */
|
|
|
|
tun_write, /* write */
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* seek */
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2022-01-10 15:51:17 +01:00
|
|
|
tun_poll /* poll */
|
2015-03-11 13:52:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 17:10:38 +01:00
|
|
|
static void tun_pollnotify(FAR struct tun_device_s *priv,
|
|
|
|
pollevent_t eventset)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
FAR struct pollfd *fds = priv->poll_fds;
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
if (priv->read_wait && (eventset & POLLIN))
|
|
|
|
{
|
|
|
|
priv->read_wait = false;
|
|
|
|
nxsem_post(&priv->read_wait_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->write_wait && (eventset & POLLOUT))
|
|
|
|
{
|
|
|
|
priv->write_wait = false;
|
|
|
|
nxsem_post(&priv->write_wait_sem);
|
|
|
|
}
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-11-09 16:50:10 +01:00
|
|
|
* Name: tun_fd_transmit
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start hardware transmission. Called either from the txdone interrupt
|
|
|
|
* handling or from watchdog based polling.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-02-02 15:02:17 +01:00
|
|
|
* None
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May or may not be called from an interrupt handler. In either case,
|
|
|
|
* global interrupts are disabled, either explicitly or indirectly through
|
|
|
|
* interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
static void tun_fd_transmit(FAR struct tun_device_s *priv)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2015-06-04 14:54:55 +02:00
|
|
|
tun_pollnotify(priv, POLLIN);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_txpoll
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 17:10:38 +01:00
|
|
|
* The transmitter is available, check if the network has any outgoing
|
|
|
|
* packets ready to send. This is a callback from devif_poll().
|
|
|
|
* devif_poll() may be called:
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* 1. When the preceding TX packet send is complete,
|
|
|
|
* 2. When the preceding TX packet send timesout and the interface is reset
|
|
|
|
* 3. During normal TX polling
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May or may not be called from an interrupt handler. In either case,
|
|
|
|
* global interrupts are disabled, either explicitly or indirectly through
|
|
|
|
* interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txpoll(FAR struct net_driver_s *dev)
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
2023-01-10 13:03:02 +01:00
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
2018-04-20 14:52:49 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
DEBUGASSERT(priv->read_buf == NULL);
|
2024-05-06 12:54:11 +02:00
|
|
|
|
2024-09-02 09:51:53 +02:00
|
|
|
NETDEV_TXPACKETS(dev);
|
2024-05-06 12:54:11 +02:00
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
/* When packet sockets are enabled, feed the frame into the tap */
|
|
|
|
|
|
|
|
pkt_input(dev);
|
|
|
|
#endif
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
priv->read_d_len = dev->d_len;
|
|
|
|
priv->read_buf = dev->d_iob;
|
|
|
|
netdev_iob_clear(dev);
|
|
|
|
|
2018-04-20 14:52:49 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
if (dev->d_lltype == NET_LL_ETHERNET)
|
|
|
|
{
|
|
|
|
ret = tun_txpoll_tap(dev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret = tun_txpoll_tun(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_txpoll_tap : for tap (ethernet bridge) mode
|
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 17:10:38 +01:00
|
|
|
* The transmitter is available, check if the network has any outgoing
|
|
|
|
* packets ready to send. This is a callback from devif_poll().
|
|
|
|
* devif_poll() may be called:
|
2018-04-20 14:52:49 +02:00
|
|
|
*
|
|
|
|
* 1. When the preceding TX packet send is complete,
|
|
|
|
* 2. When the preceding TX packet send timesout and the interface is reset
|
|
|
|
* 3. During normal TX polling
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May or may not be called from an interrupt handler. In either case,
|
|
|
|
* global interrupts are disabled, either explicitly or indirectly through
|
|
|
|
* interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txpoll_tap(FAR struct net_driver_s *dev)
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
/* Send the packet */
|
2018-04-20 14:52:49 +02:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
tun_fd_transmit(priv);
|
2018-04-20 14:52:49 +02:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
return 1;
|
2018-04-20 14:52:49 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_txpoll_tun : for tun (IP tunneling) mode
|
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 17:10:38 +01:00
|
|
|
* The transmitter is available, check if the network has any outgoing
|
|
|
|
* packets ready to send. This is a callback from devif_poll().
|
|
|
|
* devif_poll() may be called:
|
2018-04-20 14:52:49 +02:00
|
|
|
*
|
|
|
|
* 1. When the preceding TX packet send is complete,
|
|
|
|
* 2. When the preceding TX packet send timesout and the interface is reset
|
|
|
|
* 3. During normal TX polling
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May or may not be called from an interrupt handler. In either case,
|
|
|
|
* global interrupts are disabled, either explicitly or indirectly through
|
|
|
|
* interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txpoll_tun(FAR struct net_driver_s *dev)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
tun_fd_transmit(priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
return 1;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-04-20 14:52:49 +02:00
|
|
|
* Name: tun_net_receive
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 17:10:38 +01:00
|
|
|
* An interrupt was received indicating the availability of a new RX
|
|
|
|
* packet
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Global interrupts are disabled by interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-10-24 23:17:01 +02:00
|
|
|
static void tun_net_receive(FAR struct tun_device_s *priv)
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
if (priv->dev.d_lltype == NET_LL_ETHERNET)
|
|
|
|
{
|
|
|
|
tun_net_receive_tap(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
tun_net_receive_tun(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_net_receive_tap : for tap (ethernet bridge) mode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An interrupt was received indicating the availability of a new RX packet
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Global interrupts are disabled by interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
static void tun_net_receive_tap(FAR struct tun_device_s *priv)
|
|
|
|
{
|
2023-03-06 12:58:06 +01:00
|
|
|
FAR struct net_driver_s *dev = &priv->dev;
|
|
|
|
|
2018-04-20 14:52:49 +02:00
|
|
|
/* Copy the data data from the hardware to priv->dev.d_buf. Set amount of
|
|
|
|
* data in priv->dev.d_len
|
|
|
|
*/
|
|
|
|
|
|
|
|
NETDEV_RXPACKETS(&priv->dev);
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_PKT
|
2020-08-06 19:41:45 +02:00
|
|
|
/* When packet sockets are enabled, feed the frame into the tap */
|
2018-04-20 14:52:49 +02:00
|
|
|
|
|
|
|
pkt_input(&priv->dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We only accept IP packets of the configured type and ARP packets */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4)
|
2023-03-06 12:58:06 +01:00
|
|
|
if (ETHBUF->type == HTONS(ETHTYPE_IP))
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
|
|
|
ninfo("IPv4 frame\n");
|
|
|
|
NETDEV_RXIPV4(&priv->dev);
|
|
|
|
|
2022-11-26 19:13:21 +01:00
|
|
|
/* Receive an IPv4 packet from the network device */
|
2018-04-20 14:52:49 +02:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
ipv4_input(&priv->dev);
|
2018-04-20 14:52:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2023-03-06 12:58:06 +01:00
|
|
|
if (ETHBUF->type == HTONS(ETHTYPE_IP6))
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
2020-03-03 16:11:57 +01:00
|
|
|
ninfo("IPv6 frame\n");
|
2018-04-20 14:52:49 +02:00
|
|
|
NETDEV_RXIPV6(&priv->dev);
|
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
/* Give the IPv6 packet to the network layer. */
|
2018-04-20 14:52:49 +02:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
ipv6_input(&priv->dev);
|
2018-04-20 14:52:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ARP
|
2023-03-06 12:58:06 +01:00
|
|
|
if (ETHBUF->type == HTONS(ETHTYPE_ARP))
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
2022-12-04 08:01:53 +01:00
|
|
|
arp_input(&priv->dev);
|
2018-04-20 14:52:49 +02:00
|
|
|
NETDEV_RXARP(&priv->dev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
NETDEV_RXDROPPED(&priv->dev);
|
2020-02-02 15:02:17 +01:00
|
|
|
priv->dev.d_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the above function invocation resulted in data that should be
|
|
|
|
* sent out on the network, the field d_len will set to a value > 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
/* And send the packet */
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
DEBUGASSERT(priv->write_buf == NULL);
|
2020-02-02 15:02:17 +01:00
|
|
|
priv->write_d_len = priv->dev.d_len;
|
2023-01-10 13:03:02 +01:00
|
|
|
priv->write_buf = priv->dev.d_iob;
|
|
|
|
netdev_iob_clear(&priv->dev);
|
2020-02-02 15:02:17 +01:00
|
|
|
tun_fd_transmit(priv);
|
2018-04-20 14:52:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_net_receive_tun : for tun (IP tunneling) mode
|
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 17:10:38 +01:00
|
|
|
* An interrupt was received indicating the availability of a new RX
|
|
|
|
* packet
|
2018-04-20 14:52:49 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Global interrupts are disabled by interrupt handling logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tun_net_receive_tun(FAR struct tun_device_s *priv)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2022-10-26 06:35:08 +02:00
|
|
|
FAR struct net_driver_s *dev = &priv->dev;
|
|
|
|
|
|
|
|
/* Copy the data data from the hardware to dev->d_buf. Set amount of
|
|
|
|
* data in dev->d_len
|
2015-03-11 13:52:56 +01:00
|
|
|
*/
|
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
NETDEV_RXPACKETS(dev);
|
2015-11-26 19:08:09 +01:00
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
#ifdef CONFIG_NET_PKT
|
2020-08-06 19:41:45 +02:00
|
|
|
/* When packet sockets are enabled, feed the frame into the tap */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
pkt_input(dev);
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
/* We only accept IP packets of the configured type */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-01-05 17:58:58 +01:00
|
|
|
#if defined(CONFIG_NET_IPv4)
|
2018-11-09 16:50:10 +01:00
|
|
|
if ((IPv4BUF->vhl & IP_VERSION_MASK) == IPv4_VERSION)
|
|
|
|
{
|
|
|
|
ninfo("IPv4 frame\n");
|
2022-10-26 06:35:08 +02:00
|
|
|
NETDEV_RXIPV4(dev);
|
2016-10-24 23:25:40 +02:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
/* Give the IPv4 packet to the network layer. */
|
2016-10-24 23:25:40 +02:00
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
ipv4_input(dev);
|
2018-11-09 16:50:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_NET_IPv6)
|
|
|
|
if ((IPv6BUF->vtc & IP_VERSION_MASK) == IPv6_VERSION)
|
2016-10-24 23:36:11 +02:00
|
|
|
{
|
2020-03-03 16:11:57 +01:00
|
|
|
ninfo("IPv6 frame\n");
|
2022-10-26 06:35:08 +02:00
|
|
|
NETDEV_RXIPV6(dev);
|
2016-10-24 23:25:40 +02:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
/* Give the IPv6 packet to the network layer. */
|
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
ipv6_input(dev);
|
2016-01-05 17:58:58 +01:00
|
|
|
}
|
2016-10-24 23:36:11 +02:00
|
|
|
else
|
2018-11-09 16:50:10 +01:00
|
|
|
#endif
|
2016-10-24 23:36:11 +02:00
|
|
|
{
|
2022-10-26 06:35:08 +02:00
|
|
|
NETDEV_RXDROPPED(dev);
|
|
|
|
dev->d_len = 0;
|
2016-10-24 23:36:11 +02:00
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
/* If the above function invocation resulted in data that should be
|
2020-08-06 19:41:45 +02:00
|
|
|
* sent out on the network, d_len field will set to a value > 0.
|
2016-01-05 17:58:58 +01:00
|
|
|
*/
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-10-26 06:35:08 +02:00
|
|
|
if (dev->d_len > 0)
|
2015-11-26 19:08:09 +01:00
|
|
|
{
|
2023-01-10 13:03:02 +01:00
|
|
|
DEBUGASSERT(priv->write_buf == NULL);
|
2022-10-26 06:35:08 +02:00
|
|
|
priv->write_d_len = dev->d_len;
|
2023-01-10 13:03:02 +01:00
|
|
|
priv->write_buf = dev->d_iob;
|
|
|
|
netdev_iob_clear(dev);
|
2018-11-09 16:50:10 +01:00
|
|
|
tun_fd_transmit(priv);
|
2015-11-26 19:08:09 +01:00
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_txdone
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An interrupt was received indicating that the last TX packet(s) is done
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Global interrupts are disabled by the watchdog logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tun_txdone(FAR struct tun_device_s *priv)
|
|
|
|
{
|
|
|
|
/* Check for errors and update statistics */
|
|
|
|
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXDONE(&priv->dev);
|
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Then poll the network for new XMIT data */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
devif_poll(&priv->dev, tun_txpoll);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_ifup
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Bring up the Ethernet interface when an IP address is
|
|
|
|
* provided
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_ifup(FAR struct net_driver_s *dev)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2023-08-18 22:23:01 +02:00
|
|
|
ninfo("Bringing up: %u.%u.%u.%u\n",
|
|
|
|
ip4_addr1(dev->d_ipaddr), ip4_addr2(dev->d_ipaddr),
|
|
|
|
ip4_addr3(dev->d_ipaddr), ip4_addr4(dev->d_ipaddr));
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2016-06-12 16:31:22 +02:00
|
|
|
ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
|
|
|
|
dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2],
|
|
|
|
dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5],
|
|
|
|
dev->d_ipv6addr[6], dev->d_ipv6addr[7]);
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
priv->bifup = true;
|
2021-12-21 09:32:23 +01:00
|
|
|
netdev_carrier_on(dev);
|
2015-03-11 13:52:56 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_ifdown
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Stop the interface.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_ifdown(FAR struct net_driver_s *dev)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
2017-06-23 07:19:58 +02:00
|
|
|
irqstate_t flags;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2021-12-21 09:32:23 +01:00
|
|
|
netdev_carrier_off(dev);
|
|
|
|
|
2017-06-23 07:19:58 +02:00
|
|
|
flags = enter_critical_section();
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/* Mark the device "down" */
|
|
|
|
|
|
|
|
priv->bifup = false;
|
2017-06-23 07:19:58 +02:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2015-03-11 13:52:56 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-08 01:33:44 +02:00
|
|
|
* Name: tun_txavail_work
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Driver callback invoked when new TX data is available. This is a
|
|
|
|
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
|
|
|
|
* latency.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called in normal user mode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-08 01:33:44 +02:00
|
|
|
static void tun_txavail_work(FAR void *arg)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2017-07-08 01:33:44 +02:00
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Thread has been canceled, skip poll-related work */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
/* Check if there is room to hold another network packet. */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
if (priv->read_d_len != 0)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-07-08 01:33:44 +02:00
|
|
|
return;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-03-11 13:52:56 +01:00
|
|
|
if (priv->bifup)
|
|
|
|
{
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Poll the network for new XMIT data */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-05-29 15:47:28 +02:00
|
|
|
devif_poll(&priv->dev, tun_txpoll);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-07-08 01:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_txavail
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Driver callback invoked when new TX data is available. This is a
|
|
|
|
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
|
|
|
|
* latency.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-08 01:33:44 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from the network stack with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_txavail(FAR struct net_driver_s *dev)
|
2017-07-08 01:33:44 +02:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
2023-10-20 09:17:48 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
flags = enter_critical_section(); /* No interrupts */
|
2017-07-08 01:33:44 +02:00
|
|
|
|
2023-10-20 09:17:48 +02:00
|
|
|
/* Schedule to perform the TX poll on the worker thread when priv->bifup
|
|
|
|
* is true.
|
|
|
|
*/
|
2017-07-08 01:33:44 +02:00
|
|
|
|
2023-10-20 09:17:48 +02:00
|
|
|
if (priv->bifup && work_available(&priv->work))
|
2017-07-08 01:33:44 +02:00
|
|
|
{
|
|
|
|
work_queue(TUNWORK, &priv->work, tun_txavail_work, priv, 0);
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2023-10-20 09:17:48 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_addmac
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Add the specified MAC address to the hardware multicast
|
|
|
|
* address filtering
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
* mac - The MAC address to be added
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
/* Add the MAC address to the hardware multicast routing table */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_rmmac
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 17:10:38 +01:00
|
|
|
* NuttX Callback: Remove the specified MAC address from the hardware
|
|
|
|
* multicast address filtering
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
* mac - The MAC address to be removed
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2019-02-24 17:10:38 +01:00
|
|
|
static int tun_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
/* Add the MAC address to the hardware multicast routing table */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_dev_init
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the TUN device
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
static int tun_dev_init(FAR struct tun_device_s *priv,
|
|
|
|
FAR struct file *filep,
|
2018-11-09 16:50:10 +01:00
|
|
|
FAR const char *devfmt, bool tun)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the driver structure */
|
|
|
|
|
|
|
|
memset(priv, 0, sizeof(struct tun_device_s));
|
|
|
|
priv->dev.d_ifup = tun_ifup; /* I/F up (new IP address) callback */
|
|
|
|
priv->dev.d_ifdown = tun_ifdown; /* I/F down callback */
|
|
|
|
priv->dev.d_txavail = tun_txavail; /* New TX data callback */
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2015-03-11 13:52:56 +01:00
|
|
|
priv->dev.d_addmac = tun_addmac; /* Add multicast MAC address */
|
|
|
|
priv->dev.d_rmmac = tun_rmmac; /* Remove multicast MAC address */
|
|
|
|
#endif
|
2020-08-04 12:31:31 +02:00
|
|
|
priv->dev.d_private = priv; /* Used to recover private state from dev */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
/* Initialize the mutual exlcusion and wait semaphore */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lock);
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->read_wait_sem, 0, 0);
|
2020-02-02 15:02:17 +01:00
|
|
|
nxsem_init(&priv->write_wait_sem, 0, 0);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2018-08-14 15:13:41 +02:00
|
|
|
/* Assign d_ifname if specified. */
|
|
|
|
|
|
|
|
if (devfmt)
|
|
|
|
{
|
2022-08-24 04:23:20 +02:00
|
|
|
strlcpy(priv->dev.d_ifname, devfmt, IFNAMSIZ);
|
2018-08-14 15:13:41 +02:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Register the device with the OS so that socket IOCTLs can be performed */
|
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
ret = netdev_register(&priv->dev, tun ? NET_LL_TUN : NET_LL_ETHERNET);
|
2015-03-11 13:52:56 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->read_wait_sem);
|
2020-02-02 15:02:17 +01:00
|
|
|
nxsem_destroy(&priv->write_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
filep->f_priv = priv; /* Set link to TUN device */
|
2015-03-11 13:52:56 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_dev_uninit
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-10-24 09:20:33 +02:00
|
|
|
static void tun_dev_uninit(FAR struct tun_device_s *priv)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
/* Put the interface in the down state */
|
|
|
|
|
|
|
|
tun_ifdown(&priv->dev);
|
|
|
|
|
2023-10-20 09:17:48 +02:00
|
|
|
work_cancel_sync(TUNWORK, &priv->work);
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Remove the device from the OS */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
netdev_unregister(&priv->dev);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->read_wait_sem);
|
2020-02-02 15:02:17 +01:00
|
|
|
nxsem_destroy(&priv->write_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct tun_driver_s *tun = inode->i_private;
|
|
|
|
FAR struct tun_device_s *priv = filep->f_priv;
|
|
|
|
int intf;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
if (priv == NULL)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
intf = priv - g_tun_devices;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&tun->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2023-10-24 09:20:33 +02:00
|
|
|
tun->free_tuns |= (1 << intf);
|
|
|
|
tun_dev_uninit(priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&tun->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
return ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = filep->f_priv;
|
2023-01-10 13:03:02 +01:00
|
|
|
uint8_t llhdrlen;
|
2024-07-26 12:13:34 +02:00
|
|
|
ssize_t ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
if (priv == NULL || buflen > CONFIG_NET_TUN_PKTSIZE)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
llhdrlen = NET_LL_HDRLEN(&priv->dev);
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
for (; ; )
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2020-03-31 16:05:48 +02:00
|
|
|
/* Write must return immediately if interrupted by a signal (or if the
|
|
|
|
* thread is canceled) and no data has yet been written.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-07-26 12:13:34 +02:00
|
|
|
return ret;
|
2020-03-31 16:05:48 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
/* Check if there are free space to write */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
if (priv->write_d_len == 0)
|
|
|
|
{
|
|
|
|
net_lock();
|
2023-07-12 14:05:25 +02:00
|
|
|
netdev_iob_release(&priv->dev);
|
2023-01-10 13:03:02 +01:00
|
|
|
ret = netdev_iob_prepare(&priv->dev, false, 0);
|
2023-03-06 12:58:06 +01:00
|
|
|
priv->dev.d_buf = NULL;
|
2023-01-10 13:03:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
net_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iob_trycopyin(priv->dev.d_iob, (FAR const uint8_t *)buffer,
|
|
|
|
buflen, -llhdrlen, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
net_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
priv->dev.d_len = buflen;
|
|
|
|
|
|
|
|
tun_net_receive(priv);
|
|
|
|
net_unlock();
|
|
|
|
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = buflen;
|
2020-02-02 15:02:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
/* Wait if there are no free space to write */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
if ((filep->f_oflags & O_NONBLOCK) != 0)
|
|
|
|
{
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = -EAGAIN;
|
2020-02-02 15:02:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
priv->write_wait = true;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2020-02-02 15:02:17 +01:00
|
|
|
nxsem_wait(&priv->write_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2024-07-26 12:13:34 +02:00
|
|
|
return ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = filep->f_priv;
|
2023-01-10 13:03:02 +01:00
|
|
|
uint8_t llhdrlen;
|
2024-07-26 12:13:34 +02:00
|
|
|
ssize_t ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
if (priv == NULL)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
llhdrlen = NET_LL_HDRLEN(&priv->dev);
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
for (; ; )
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2020-03-31 16:05:48 +02:00
|
|
|
/* Read must return immediately if interrupted by a signal (or if the
|
|
|
|
* thread is canceled) and no data has yet been read.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-07-26 12:13:34 +02:00
|
|
|
return ret;
|
2020-03-31 16:05:48 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
/* Check if there are data to read in write buffer */
|
|
|
|
|
|
|
|
if (priv->write_d_len > 0)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2020-02-02 15:02:17 +01:00
|
|
|
if (buflen < priv->write_d_len)
|
|
|
|
{
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = -EINVAL;
|
2020-02-02 15:02:17 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
iob_copyout((FAR uint8_t *)buffer, priv->write_buf,
|
|
|
|
priv->write_d_len, -llhdrlen);
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = priv->write_d_len;
|
2023-01-10 13:03:02 +01:00
|
|
|
|
|
|
|
iob_free_chain(priv->write_buf);
|
|
|
|
priv->write_buf = NULL;
|
2020-02-02 15:02:17 +01:00
|
|
|
priv->write_d_len = 0;
|
|
|
|
|
|
|
|
NETDEV_TXDONE(&priv->dev);
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
break;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
/* Check if there are data to read in read buffer */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-02-02 15:02:17 +01:00
|
|
|
if (priv->read_d_len > 0)
|
|
|
|
{
|
|
|
|
if (buflen < priv->read_d_len)
|
|
|
|
{
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = -EINVAL;
|
2020-02-02 15:02:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-10-24 23:17:01 +02:00
|
|
|
|
2023-01-10 13:03:02 +01:00
|
|
|
iob_copyout((FAR uint8_t *)buffer, priv->read_buf,
|
|
|
|
priv->read_d_len, -llhdrlen);
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = priv->read_d_len;
|
2023-01-10 13:03:02 +01:00
|
|
|
|
|
|
|
iob_free_chain(priv->read_buf);
|
|
|
|
priv->read_buf = NULL;
|
2020-02-02 15:02:17 +01:00
|
|
|
priv->read_d_len = 0;
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
tun_txdone(priv);
|
|
|
|
net_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait if there are no data to read */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
if ((filep->f_oflags & O_NONBLOCK) != 0)
|
|
|
|
{
|
2024-07-26 12:13:34 +02:00
|
|
|
ret = -EAGAIN;
|
2020-02-02 15:02:17 +01:00
|
|
|
break;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->read_wait = true;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_wait(&priv->read_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2024-07-26 12:13:34 +02:00
|
|
|
return ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-30 07:28:05 +02:00
|
|
|
static int tun_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = filep->f_priv;
|
|
|
|
pollevent_t eventset;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Some sanity checking */
|
|
|
|
|
2018-11-09 16:50:10 +01:00
|
|
|
if (priv == NULL || fds == NULL)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2018-11-09 16:50:10 +01:00
|
|
|
return -EINVAL;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
if (priv->poll_fds)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->poll_fds = fds;
|
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
/* If write buffer is empty notify App. */
|
|
|
|
|
|
|
|
if (priv->write_d_len == 0)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLOUT;
|
2015-06-04 14:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The write buffer sometimes could be used for TX.
|
|
|
|
* So check it too.
|
|
|
|
*/
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
if (priv->read_d_len != 0 || priv->write_d_len != 0)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-30 07:28:05 +02:00
|
|
|
priv->poll_fds = NULL;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2015-03-11 13:52:56 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2015-08-21 15:39:57 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct tun_driver_s *tun = inode->i_private;
|
2015-03-11 13:52:56 +01:00
|
|
|
FAR struct tun_device_s *priv = filep->f_priv;
|
|
|
|
int ret = OK;
|
|
|
|
|
2022-12-20 09:32:20 +01:00
|
|
|
if (cmd == TUNSETIFF)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
uint8_t free_tuns;
|
|
|
|
int intf;
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct ifreq *ifr = (FAR struct ifreq *)arg;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-12-20 09:32:20 +01:00
|
|
|
if (priv != NULL || ifr == NULL ||
|
2018-11-09 16:50:10 +01:00
|
|
|
((ifr->ifr_flags & IFF_MASK) != IFF_TUN &&
|
|
|
|
(ifr->ifr_flags & IFF_MASK) != IFF_TAP))
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&tun->lock);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
free_tuns = tun->free_tuns;
|
|
|
|
|
|
|
|
if (free_tuns == 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&tun->lock);
|
2015-03-11 13:52:56 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (intf = 0;
|
|
|
|
intf < CONFIG_TUN_NINTERFACES && !(free_tuns & 1);
|
|
|
|
intf++, free_tuns >>= 1);
|
|
|
|
|
|
|
|
ret = tun_dev_init(&g_tun_devices[intf], filep,
|
2023-05-30 07:28:05 +02:00
|
|
|
*ifr->ifr_name ? ifr->ifr_name : NULL,
|
2018-11-09 16:50:10 +01:00
|
|
|
(ifr->ifr_flags & IFF_MASK) == IFF_TUN);
|
2015-03-11 13:52:56 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&tun->lock);
|
2015-03-11 13:52:56 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
tun->free_tuns &= ~(1 << intf);
|
|
|
|
|
|
|
|
priv = filep->f_priv;
|
2022-08-24 04:23:20 +02:00
|
|
|
strlcpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&tun->lock);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2022-12-20 09:32:20 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (cmd == TUNGETIFF)
|
|
|
|
{
|
|
|
|
FAR struct ifreq *ifr = (FAR struct ifreq *)arg;
|
|
|
|
if (priv == NULL || ifr == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ);
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2024-07-23 10:47:31 +02:00
|
|
|
else if (cmd == TUNSETCARRIER)
|
|
|
|
{
|
|
|
|
if (priv == NULL || arg == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*(FAR int *)((uintptr_t)arg))
|
|
|
|
{
|
|
|
|
netdev_carrier_on(&priv->dev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
netdev_carrier_off(&priv->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
return -ENOTTY;
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_initialize
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-11-09 16:50:10 +01:00
|
|
|
* Instantiate a TUN network interface.
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int tun_initialize(void)
|
|
|
|
{
|
|
|
|
g_tun.free_tuns = (1 << CONFIG_TUN_NINTERFACES) - 1;
|
2020-01-02 17:49:34 +01:00
|
|
|
register_driver("/dev/tun", &g_tun_file_ops, 0644, &g_tun);
|
2015-03-11 13:52:56 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TUN */
|