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>
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
# include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <nuttx/wdog.h>
|
2016-12-03 23:28:19 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2015-03-11 13:52:56 +01:00
|
|
|
#include <nuttx/net/arp.h>
|
|
|
|
#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
|
|
|
|
****************************************************************************/
|
|
|
|
/* 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)
|
2016-12-03 23:28:19 +01:00
|
|
|
#else
|
2016-11-18 16:22:49 +01:00
|
|
|
|
|
|
|
# if defined(CONFIG_TUN_HPWORK)
|
|
|
|
# define TUNWORK HPWORK
|
|
|
|
# elif defined(CONFIG_TUN_LPWORK)
|
|
|
|
# define TUNWORK LPWORK
|
|
|
|
# else
|
|
|
|
# error "Neither CONFIG_TUN_HPWORK nor CONFIG_TUN_LPWORK defined"
|
|
|
|
# endif
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
/* TX poll delay = 1 seconds. CLK_TCK is the number of clock ticks per
|
|
|
|
* second
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TUN_WDDELAY (1*CLK_TCK)
|
|
|
|
|
2018-04-20 14:52:49 +02:00
|
|
|
/* This is a helper pointer for accessing the contents of the Ethernet header */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
# define BUF ((struct eth_hdr_s *)priv->dev.d_buf)
|
|
|
|
#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
|
|
|
|
{
|
|
|
|
bool bifup; /* true:ifup false:ifdown */
|
|
|
|
WDOG_ID txpoll; /* TX poll timer */
|
2017-03-03 21:45:09 +01:00
|
|
|
struct work_s work; /* For deferring poll work to the work queue */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
FAR struct file *filep;
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
FAR struct pollfd *poll_fds;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool read_wait;
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
uint8_t read_buf[CONFIG_NET_TUN_PKTSIZE];
|
2015-03-11 13:52:56 +01:00
|
|
|
size_t read_d_len;
|
2018-07-04 22:10:40 +02:00
|
|
|
uint8_t write_buf[CONFIG_NET_TUN_PKTSIZE];
|
2015-03-11 13:52:56 +01:00
|
|
|
size_t write_d_len;
|
|
|
|
|
|
|
|
sem_t waitsem;
|
|
|
|
sem_t read_wait_sem;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
uint8_t free_tuns;
|
|
|
|
sem_t waitsem;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tun_lock(FAR struct tun_device_s *priv);
|
|
|
|
static void tun_unlock(FAR struct tun_device_s *priv);
|
|
|
|
|
|
|
|
/* Common TX logic */
|
|
|
|
|
2016-10-24 23:17:01 +02:00
|
|
|
static int tun_fd_transmit(FAR struct tun_device_s *priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
static int tun_txpoll(struct net_driver_s *dev);
|
2018-04-20 14:52:49 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
|
|
|
static int tun_txpoll_tap(struct net_driver_s *dev);
|
|
|
|
#endif
|
|
|
|
static int tun_txpoll_tun(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);
|
|
|
|
|
|
|
|
/* Watchdog timer expirations */
|
|
|
|
|
|
|
|
static void tun_poll_work(FAR void *arg);
|
2015-06-04 14:54:55 +02:00
|
|
|
static void tun_poll_expiry(int argc, wdparm_t arg, ...);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
#ifdef CONFIG_NET_IGMP
|
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
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
static void tun_ipv6multicast(FAR struct tun_device_s *priv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int tun_dev_init(FAR struct tun_device_s *priv,
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct file *filep, FAR const char *devfmt);
|
2015-03-11 13:52:56 +01:00
|
|
|
static int tun_dev_uninit(FAR struct tun_device_s *priv);
|
|
|
|
|
|
|
|
/* File interface */
|
|
|
|
|
|
|
|
static int tun_open(FAR struct file *filep);
|
|
|
|
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);
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
static int tun_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct tun_driver_s g_tun;
|
|
|
|
static struct tun_device_s g_tun_devices[CONFIG_TUN_NINTERFACES];
|
|
|
|
|
|
|
|
static const struct file_operations g_tun_file_ops =
|
|
|
|
{
|
|
|
|
tun_open, /* open */
|
|
|
|
tun_close, /* close */
|
|
|
|
tun_read, /* read */
|
|
|
|
tun_write, /* write */
|
2016-10-24 23:17:01 +02:00
|
|
|
0, /* seek */
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_ioctl, /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
tun_poll, /* poll */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tundev_lock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tundev_lock(FAR struct tun_driver_s *tun)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&tun->waitsem);
|
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
2015-03-11 13:52:56 +01:00
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tundev_unlock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tundev_unlock(FAR struct tun_driver_s *tun)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&tun->waitsem);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_lock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tun_lock(FAR struct tun_device_s *priv)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&priv->waitsem);
|
|
|
|
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
2015-03-11 13:52:56 +01:00
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_unlock
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tun_unlock(FAR struct tun_device_s *priv)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->waitsem);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
static void tun_pollnotify(FAR struct tun_device_s *priv, pollevent_t eventset)
|
|
|
|
{
|
|
|
|
FAR struct pollfd *fds = priv->poll_fds;
|
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
if (fds == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
eventset &= fds->events;
|
|
|
|
|
|
|
|
if (eventset != 0)
|
|
|
|
{
|
|
|
|
fds->revents |= eventset;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2015-06-04 14:54:55 +02:00
|
|
|
# define tun_pollnotify(dev, event)
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_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:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-10-24 23:17:01 +02:00
|
|
|
static int tun_fd_transmit(FAR struct tun_device_s *priv)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXPACKETS(&priv->dev);
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Verify that the hardware is ready to send another packet. If we get
|
|
|
|
* here, then we are committed to sending a packet; Higher level logic
|
|
|
|
* must have assured that there is no transmission in progress.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->read_wait)
|
|
|
|
{
|
|
|
|
priv->read_wait = false;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->read_wait_sem);
|
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
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_txpoll
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-05-30 17:37:34 +02:00
|
|
|
* The transmitter is available, check if the network has any outgoing packets
|
2015-03-11 13:52:56 +01:00
|
|
|
* ready to send. This is a callback from devif_poll(). devif_poll() may
|
|
|
|
* be called:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_txpoll(struct net_driver_s *dev)
|
2018-04-20 14:52:49 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#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:
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
static int tun_txpoll_tap(struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
|
|
|
|
|
|
|
/* If the polling resulted in data that should be sent out on the network,
|
|
|
|
* the field d_len is set to a value > 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
/* Look up the destination MAC address and add it to the Ethernet
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (IFF_IS_IPv4(priv->dev.d_flags))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
arp_out(&priv->dev);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
neighbor_out(&priv->dev);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
/* Send the packet */
|
|
|
|
|
|
|
|
priv->read_d_len = priv->dev.d_len;
|
|
|
|
tun_fd_transmit(priv);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If zero is returned, the polling will continue until all connections have
|
|
|
|
* been examined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_txpoll_tun : for tun (IP tunneling) mode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_txpoll_tun(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;
|
|
|
|
|
|
|
|
/* If the polling resulted in data that should be sent out on the network,
|
|
|
|
* the field d_len is set to a value > 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
/* Send the packet */
|
|
|
|
|
2015-08-17 17:04:58 +02:00
|
|
|
priv->read_d_len = priv->dev.d_len;
|
2016-10-24 23:17:01 +02:00
|
|
|
tun_fd_transmit(priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If zero is returned, the polling will continue until all connections have
|
|
|
|
* been examined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-04-20 14:52:49 +02:00
|
|
|
* Name: tun_net_receive
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An interrupt was received indicating the availability of a new RX packet
|
|
|
|
*
|
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)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
/* When packet sockets are enabled, feed the frame into the packet tap */
|
|
|
|
|
|
|
|
pkt_input(&priv->dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We only accept IP packets of the configured type and ARP packets */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4)
|
|
|
|
if (BUF->type == HTONS(ETHTYPE_IP))
|
|
|
|
{
|
|
|
|
ninfo("IPv4 frame\n");
|
|
|
|
NETDEV_RXIPV4(&priv->dev);
|
|
|
|
|
|
|
|
/* Give the IPv4 packet to the network layer. ipv4_input will return
|
|
|
|
* an error if it is unable to dispatch the packet at this time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
arp_ipin(&priv->dev);
|
|
|
|
ret = ipv4_input(&priv->dev);
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Update the Ethernet header with the correct MAC address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (IFF_IS_IPv4(priv->dev.d_flags))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
arp_out(&priv->dev);
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
else
|
|
|
|
{
|
|
|
|
neighbor_out(&priv->dev);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* And send the packet */
|
|
|
|
|
|
|
|
priv->write_d_len = priv->dev.d_len;
|
|
|
|
tun_fd_transmit(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->dev.d_len = 0;
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (BUF->type == HTONS(ETHTYPE_IP6))
|
|
|
|
{
|
|
|
|
ninfo("Iv6 frame\n");
|
|
|
|
NETDEV_RXIPV6(&priv->dev);
|
|
|
|
|
|
|
|
/* Give the IPv6 packet to the network layer. ipv6_input will return
|
|
|
|
* an error if it is unable to dispatch the packet at this time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ipv6_input(&priv->dev);
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
/* Update the Ethernet header with the correct MAC address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (IFF_IS_IPv4(priv->dev.d_flags))
|
|
|
|
{
|
|
|
|
arp_out(&priv->dev);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
{
|
|
|
|
neighbor_out(&priv->dev);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
priv->write_d_len = priv->dev.d_len;
|
|
|
|
tun_fd_transmit(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->write_d_len = 0;
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ARP
|
|
|
|
if (BUF->type == htons(ETHTYPE_ARP))
|
|
|
|
{
|
|
|
|
arp_arpin(&priv->dev);
|
|
|
|
NETDEV_RXARP(&priv->dev);
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
priv->write_d_len = priv->dev.d_len;
|
|
|
|
tun_fd_transmit(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
NETDEV_RXDROPPED(&priv->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_net_receive_tun : for tun (IP tunneling) 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tun_net_receive_tun(FAR struct tun_device_s *priv)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2016-10-24 23:17:01 +02:00
|
|
|
int ret;
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Copy the data data from the hardware to priv->dev.d_buf. Set amount of
|
|
|
|
* data in priv->dev.d_len
|
|
|
|
*/
|
|
|
|
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXPACKETS(&priv->dev);
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
/* When packet sockets are enabled, feed the frame into the packet tap */
|
|
|
|
|
|
|
|
pkt_input(&priv->dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We only accept IP packets of the configured type and ARP packets */
|
|
|
|
|
2016-01-05 17:58:58 +01:00
|
|
|
#if defined(CONFIG_NET_IPv4)
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("IPv4 frame\n");
|
2016-01-05 17:58:58 +01:00
|
|
|
NETDEV_RXIPV4(&priv->dev);
|
2016-10-24 23:25:40 +02:00
|
|
|
|
2016-10-24 23:36:11 +02:00
|
|
|
/* Give the IPv4 packet to the network layer. ipv4_input will return
|
|
|
|
* an error if it is unable to dispatch the packet at this time.
|
|
|
|
*/
|
2016-10-24 23:25:40 +02:00
|
|
|
|
2016-10-24 23:36:11 +02:00
|
|
|
ret = ipv4_input(&priv->dev);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
2016-10-24 23:25:40 +02:00
|
|
|
|
2016-10-24 23:36:11 +02:00
|
|
|
if (priv->dev.d_len > 0)
|
2016-10-24 23:25:40 +02:00
|
|
|
{
|
2016-10-24 23:36:11 +02:00
|
|
|
priv->write_d_len = priv->dev.d_len;
|
|
|
|
tun_fd_transmit(priv);
|
2016-10-24 23:25:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
2016-01-05 17:58:58 +01:00
|
|
|
}
|
2016-10-24 23:36:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->dev.d_len = 0;
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-01-05 17:58:58 +01:00
|
|
|
#elif defined(CONFIG_NET_IPv6)
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Iv6 frame\n");
|
2016-01-05 17:58:58 +01:00
|
|
|
NETDEV_RXIPV6(&priv->dev);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-10-24 23:36:11 +02:00
|
|
|
/* Give the IPv6 packet to the network layer. ipv6_input will return
|
|
|
|
* an error if it is unable to dispatch the packet at this time.
|
2016-01-05 17:58:58 +01:00
|
|
|
*/
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-10-24 23:36:11 +02:00
|
|
|
ret = ipv6_input(&priv->dev);
|
|
|
|
if (ret == OK)
|
2016-01-05 17:58:58 +01:00
|
|
|
{
|
2016-10-24 23:36:11 +02:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
priv->write_d_len = priv->dev.d_len;
|
|
|
|
tun_fd_transmit(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
2015-11-26 19:08:09 +01:00
|
|
|
else
|
|
|
|
{
|
2016-01-05 17:58:58 +01:00
|
|
|
priv->write_d_len = 0;
|
|
|
|
tun_pollnotify(priv, POLLOUT);
|
2015-11-26 19:08:09 +01:00
|
|
|
}
|
2016-01-05 17:58:58 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
NETDEV_RXDROPPED(&priv->dev);
|
2015-03-11 13:52:56 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
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
|
|
|
|
|
|
|
priv->dev.d_buf = priv->read_buf;
|
|
|
|
(void)devif_poll(&priv->dev, tun_txpoll);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_poll_work
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-12-03 23:28:19 +01:00
|
|
|
* Perform periodic polling from the worker thread
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-12-03 23:28:19 +01:00
|
|
|
* arg - The argument passed when work_queue() as called.
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2016-12-03 23:28:19 +01:00
|
|
|
* OK on success
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2016-12-03 23:28:19 +01:00
|
|
|
* Ethernet interrupts are disabled
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
static void tun_poll_work(FAR void *arg)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
|
|
|
|
|
|
|
|
/* Perform the poll */
|
|
|
|
|
|
|
|
tun_lock(priv);
|
|
|
|
net_lock();
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Check if there is room in the send another TX packet. We cannot perform
|
|
|
|
* the TX poll if he are unable to accept another packet for transmission.
|
|
|
|
*/
|
|
|
|
|
2015-08-17 17:04:58 +02:00
|
|
|
if (priv->read_d_len == 0)
|
|
|
|
{
|
2016-05-30 17:37:34 +02:00
|
|
|
/* If so, poll the network for new XMIT data. */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2015-08-17 17:04:58 +02:00
|
|
|
priv->dev.d_buf = priv->read_buf;
|
2016-02-03 19:12:41 +01:00
|
|
|
(void)devif_timer(&priv->dev, tun_txpoll);
|
2015-08-17 17:04:58 +02:00
|
|
|
}
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/* Setup the watchdog poll timer again */
|
|
|
|
|
|
|
|
(void)wd_start(priv->txpoll, TUN_WDDELAY, tun_poll_expiry, 1, priv);
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-08-17 17:04:58 +02:00
|
|
|
tun_unlock(priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_poll_expiry
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Periodic timer handler. Called from the timer interrupt handler.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* argc - The number of available arguments
|
|
|
|
* arg - The first argument
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Global interrupts are disabled by the watchdog logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
static void tun_poll_expiry(int argc, wdparm_t arg, ...)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
|
|
|
|
|
2017-03-03 21:45:09 +01:00
|
|
|
/* Schedule to perform the timer expiration on the worker thread. */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2017-03-03 21:45:09 +01:00
|
|
|
work_queue(TUNWORK, &priv->work, tun_poll_work, priv, 0);
|
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:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_ifup(struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2016-06-12 16:31:22 +02:00
|
|
|
ninfo("Bringing up: %d.%d.%d.%d\n",
|
|
|
|
dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
|
|
|
|
(dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24);
|
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
|
|
|
|
|
|
|
|
/* Initialize PHYs, the Ethernet interface, and setup up Ethernet interrupts */
|
|
|
|
|
2017-04-22 19:10:30 +02:00
|
|
|
/* Instantiate the MAC address from priv->dev.d_mac.ether.ether_addr_octet */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
/* Set up IPv6 multicast address filtering */
|
|
|
|
|
|
|
|
tun_ipv6multicast(priv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set and activate a timer process */
|
|
|
|
|
2017-02-24 22:58:17 +01:00
|
|
|
(void)wd_start(priv->txpoll, TUN_WDDELAY, tun_poll_expiry,
|
|
|
|
1, (wdparm_t)priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
priv->bifup = true;
|
|
|
|
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:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_ifdown(struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-06-23 07:19:58 +02:00
|
|
|
flags = enter_critical_section();
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
/* Cancel the TX poll timer */
|
|
|
|
|
|
|
|
wd_cancel(priv->txpoll);
|
|
|
|
|
|
|
|
/* 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;
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
tun_lock(priv);
|
|
|
|
|
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
|
|
|
|
2016-10-24 23:17:01 +02:00
|
|
|
if (priv->read_d_len != 0 || priv->write_d_len != 0)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
tun_unlock(priv);
|
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
|
|
|
|
|
|
|
priv->dev.d_buf = priv->read_buf;
|
|
|
|
(void)devif_poll(&priv->dev, tun_txpoll);
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_unlock(priv);
|
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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_txavail(struct net_driver_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
|
|
|
|
|
|
|
|
/* Schedule to perform the TX poll on the worker thread. */
|
|
|
|
|
|
|
|
if (work_available(&priv->work))
|
|
|
|
{
|
|
|
|
work_queue(TUNWORK, &priv->work, tun_txavail_work, priv, 0);
|
|
|
|
}
|
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:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-09 19:35:26 +02:00
|
|
|
#ifdef CONFIG_NET_IGMP
|
2015-03-11 13:52:56 +01:00
|
|
|
static int tun_addmac(struct net_driver_s *dev, FAR const uint8_t *mac)
|
|
|
|
{
|
|
|
|
/* 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:
|
|
|
|
* NuttX Callback: Remove the specified MAC address from 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 removed
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IGMP
|
|
|
|
static int tun_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac)
|
|
|
|
{
|
|
|
|
/* Add the MAC address to the hardware multicast routing table */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_ipv6multicast
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the IPv6 multicast MAC address.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-03-11 13:52:56 +01:00
|
|
|
* priv - A reference to the private driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
static void tun_ipv6multicast(FAR struct tun_device_s *priv)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ICMPv6 */
|
|
|
|
|
|
|
|
/****************************************************************************
|
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:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_dev_init(FAR struct tun_device_s *priv, FAR struct file *filep,
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR const char *devfmt)
|
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 */
|
|
|
|
#ifdef CONFIG_NET_IGMP
|
|
|
|
priv->dev.d_addmac = tun_addmac; /* Add multicast MAC address */
|
|
|
|
priv->dev.d_rmmac = tun_rmmac; /* Remove multicast MAC address */
|
|
|
|
#endif
|
2015-10-10 18:41:00 +02:00
|
|
|
priv->dev.d_private = (FAR void *)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
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->waitsem, 0, 1);
|
|
|
|
nxsem_init(&priv->read_wait_sem, 0, 0);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
/* The wait semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_setprotocol(&priv->read_wait_sem, SEM_PRIO_NONE);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2018-01-22 15:17:31 +01:00
|
|
|
/* Create a watchdog for timing polling for and timing of transmissions */
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
priv->txpoll = wd_create(); /* Create periodic poll timer */
|
|
|
|
|
|
|
|
/* Initialize other variables */
|
|
|
|
|
|
|
|
priv->write_d_len = 0;
|
|
|
|
priv->read_wait = false;
|
|
|
|
|
|
|
|
/* Put the interface in the down state */
|
|
|
|
|
|
|
|
tun_ifdown(&priv->dev);
|
|
|
|
|
2018-08-14 15:13:41 +02:00
|
|
|
/* Assign d_ifname if specified. */
|
|
|
|
|
|
|
|
if (devfmt)
|
|
|
|
{
|
|
|
|
strncpy(priv->dev.d_ifname, devfmt, IFNAMSIZ);
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
/* Register the device with the OS so that socket IOCTLs can be performed */
|
|
|
|
|
|
|
|
ret = netdev_register(&priv->dev, NET_LL_TUN);
|
2018-04-20 14:52:49 +02:00
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->waitsem);
|
|
|
|
nxsem_destroy(&priv->read_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->filep = filep; /* Set link to file */
|
|
|
|
filep->f_priv = priv; /* Set link to TUN device */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_dev_uninit
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_dev_uninit(FAR struct tun_device_s *priv)
|
|
|
|
{
|
|
|
|
/* Put the interface in the down state */
|
|
|
|
|
|
|
|
tun_ifdown(&priv->dev);
|
|
|
|
|
|
|
|
/* Remove the device from the OS */
|
|
|
|
|
|
|
|
(void)netdev_unregister(&priv->dev);
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->waitsem);
|
|
|
|
nxsem_destroy(&priv->read_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tun_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
filep->f_priv = 0;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
intf = priv - g_tun_devices;
|
|
|
|
tundev_lock(tun);
|
|
|
|
|
|
|
|
tun->free_tuns |= (1 << intf);
|
|
|
|
(void)tun_dev_uninit(priv);
|
|
|
|
|
|
|
|
tundev_unlock(tun);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tun_lock(priv);
|
|
|
|
|
|
|
|
if (priv->write_d_len > 0)
|
|
|
|
{
|
|
|
|
tun_unlock(priv);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-03-11 13:52:56 +01:00
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
if (buflen > CONFIG_NET_TUN_PKTSIZE)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(priv->write_buf, buffer, buflen);
|
|
|
|
|
|
|
|
priv->dev.d_buf = priv->write_buf;
|
|
|
|
priv->dev.d_len = buflen;
|
|
|
|
|
2016-10-24 23:17:01 +02:00
|
|
|
tun_net_receive(priv);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
ret = (ssize_t)buflen;
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_unlock(priv);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
ssize_t ret;
|
|
|
|
size_t write_d_len;
|
|
|
|
size_t read_d_len;
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tun_lock(priv);
|
|
|
|
|
|
|
|
/* Check if there are data to read in write buffer */
|
|
|
|
|
|
|
|
write_d_len = priv->write_d_len;
|
|
|
|
if (write_d_len > 0)
|
|
|
|
{
|
|
|
|
if (buflen < write_d_len)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, priv->write_buf, write_d_len);
|
|
|
|
ret = (ssize_t)write_d_len;
|
|
|
|
|
|
|
|
priv->write_d_len = 0;
|
2015-06-04 14:54:55 +02:00
|
|
|
tun_pollnotify(priv, POLLOUT);
|
2016-10-24 23:17:01 +02:00
|
|
|
|
|
|
|
if (priv->read_d_len == 0)
|
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2016-10-24 23:17:01 +02:00
|
|
|
tun_txdone(priv);
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2016-10-24 23:17:01 +02:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
if (priv->read_d_len == 0)
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
if ((filep->f_oflags & O_NONBLOCK) != 0)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->read_wait = true;
|
|
|
|
tun_unlock(priv);
|
2017-10-04 23:22:27 +02:00
|
|
|
(void)nxsem_wait(&priv->read_wait_sem);
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_lock(priv);
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
read_d_len = priv->read_d_len;
|
|
|
|
if (buflen < read_d_len)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(buffer, priv->read_buf, read_d_len);
|
|
|
|
ret = (ssize_t)read_d_len;
|
|
|
|
}
|
|
|
|
|
2015-06-04 14:54:55 +02:00
|
|
|
priv->read_d_len = 0;
|
2015-03-11 13:52:56 +01:00
|
|
|
tun_txdone(priv);
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
out:
|
|
|
|
tun_unlock(priv);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tun_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
int tun_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
FAR struct tun_device_s *priv = filep->f_priv;
|
|
|
|
pollevent_t eventset;
|
|
|
|
int ret = OK;
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
|
|
|
if (!priv || !fds)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
tun_lock(priv);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
eventset |= (fds->events & POLLOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
eventset |= (fds->events & POLLIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
tun_pollnotify(priv, eventset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->poll_fds = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
tun_unlock(priv);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
2017-07-07 21:03:04 +02:00
|
|
|
if (cmd == TUNSETIFF && priv == NULL)
|
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
|
|
|
|
2017-04-13 14:16:03 +02:00
|
|
|
if (!ifr || ((ifr->ifr_flags & IFF_MASK) != IFF_TUN &&
|
|
|
|
(ifr->ifr_flags & IFF_MASK) != IFF_TAP))
|
2015-03-11 13:52:56 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tundev_lock(tun);
|
|
|
|
|
|
|
|
free_tuns = tun->free_tuns;
|
|
|
|
|
|
|
|
if (free_tuns == 0)
|
|
|
|
{
|
|
|
|
tundev_unlock(tun);
|
|
|
|
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,
|
|
|
|
*ifr->ifr_name ? ifr->ifr_name : 0);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
tundev_unlock(tun);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
tun->free_tuns &= ~(1 << intf);
|
|
|
|
|
|
|
|
priv = filep->f_priv;
|
|
|
|
strncpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ);
|
|
|
|
|
2017-08-20 20:00:12 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2017-04-13 13:07:03 +02:00
|
|
|
if ((ifr->ifr_flags & IFF_MASK) == IFF_TAP)
|
|
|
|
{
|
|
|
|
/* TAP device -> handling raw Ethernet packets
|
|
|
|
* -> set appropriate Ethernet header length
|
|
|
|
*/
|
2017-04-13 14:16:03 +02:00
|
|
|
|
2017-04-13 13:07:03 +02:00
|
|
|
priv->dev.d_llhdrlen = ETH_HDRLEN;
|
2018-04-20 14:52:49 +02:00
|
|
|
|
|
|
|
/* Also, set the link type to NET_LL_ETHERNET */
|
|
|
|
|
|
|
|
priv->dev.d_lltype = NET_LL_ETHERNET;
|
2017-04-13 13:07:03 +02:00
|
|
|
}
|
|
|
|
else if ((ifr->ifr_flags & IFF_MASK) == IFF_TUN)
|
2017-08-20 20:00:12 +02:00
|
|
|
#endif
|
2017-04-13 13:07:03 +02:00
|
|
|
{
|
|
|
|
/* TUN device -> handling an application data stream
|
|
|
|
* -> no header
|
|
|
|
*/
|
2017-04-13 14:16:03 +02:00
|
|
|
|
2017-04-13 13:07:03 +02:00
|
|
|
priv->dev.d_llhdrlen = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:52:56 +01:00
|
|
|
tundev_unlock(tun);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EBADFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tun_initialize
|
2015-03-11 13:52:56 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Instantiate a SLIP network interface.
|
|
|
|
*
|
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)
|
|
|
|
{
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_tun.waitsem, 0, 1);
|
2015-03-11 13:52:56 +01:00
|
|
|
|
|
|
|
g_tun.free_tuns = (1 << CONFIG_TUN_NINTERFACES) - 1;
|
|
|
|
|
|
|
|
(void)register_driver("/dev/tun", &g_tun_file_ops, 0644, &g_tun);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TUN */
|