#if !defined(_WIN32) && !defined(_WIN64)
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include "lwip/opt.h"
#include "lwip/debug.h"
#include "lwip/def.h"
#include "lwip/ip.h"
#include "lwip/mem.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/pbuf.h"
#include "lwip/sys.h"
#include "lwip/timeouts.h"
#include "netif/etharp.h"
#include "lwip/ethip6.h"
#include "netif/tapif.h"
#define IFCONFIG_BIN "/sbin/ifconfig "
#if defined(LWIP_UNIX_LINUX)
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#ifndef DEVTAP_DEFAULT_IF
#define DEVTAP_DEFAULT_IF "tap0"
#endif
#ifndef DEVTAP
#define DEVTAP "/dev/net/tun"
#endif
#define NETMASK_ARGS "netmask %d.%d.%d.%d"
#define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
#elif defined(LWIP_UNIX_OPENBSD)
#define DEVTAP "/dev/tun0"
#define NETMASK_ARGS "netmask %d.%d.%d.%d"
#define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d " NETMASK_ARGS " link0"
#else
#define DEVTAP "/dev/tap0"
#define NETMASK_ARGS "netmask %d.%d.%d.%d"
#define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
#endif
#define IFNAME0 't'
#define IFNAME1 'p'
#ifndef TAPIF_DEBUG
#define TAPIF_DEBUG LWIP_DBG_OFF
#endif
struct tapif {
int fd;
};
static void tapif_input(struct netif *netif);
#if !NO_SYS
static void tapif_thread(void *arg);
#endif
static int run_command(const char *command) {
int res = -1;
#ifndef __APPLE__
res = system(command);
#endif
return res;
}
static void
low_level_init(struct netif *netif)
{
struct tapif *tapif;
#if LWIP_IPV4
int ret;
char buf[1024];
#endif
char *preconfigured_tapif = getenv("PRECONFIGURED_TAPIF");
tapif = (struct tapif *)netif->state;
netif->hwaddr[0] = 0x02;
netif->hwaddr[1] = 0x12;
netif->hwaddr[2] = 0x34;
netif->hwaddr[3] = 0x56;
netif->hwaddr[4] = 0x78;
netif->hwaddr[5] = 0xab;
netif->hwaddr_len = 6;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
tapif->fd = open(DEVTAP, O_RDWR);
LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd));
if (tapif->fd == -1) {
#ifdef LWIP_UNIX_LINUX
perror("tapif_init: try running \"modprobe tun\" or rebuilding your kernel with CONFIG_TUN; cannot open "DEVTAP);
#else
perror("tapif_init: cannot open "DEVTAP);
#endif
exit(1);
}
#ifdef LWIP_UNIX_LINUX
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
if (preconfigured_tapif) {
strncpy(ifr.ifr_name, preconfigured_tapif, sizeof(ifr.ifr_name));
} else {
strncpy(ifr.ifr_name, DEVTAP_DEFAULT_IF, sizeof(ifr.ifr_name));
}
ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0;
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
if (ioctl(tapif->fd, TUNSETIFF, (void *) &ifr) < 0) {
perror("tapif_init: "DEVTAP" ioctl TUNSETIFF");
exit(1);
}
}
#endif
netif_set_link_up(netif);
if (preconfigured_tapif == NULL) {
#if LWIP_IPV4
snprintf(buf, 1024, IFCONFIG_BIN IFCONFIG_ARGS,
ip4_addr1(netif_ip4_gw(netif)),
ip4_addr2(netif_ip4_gw(netif)),
ip4_addr3(netif_ip4_gw(netif)),
ip4_addr4(netif_ip4_gw(netif))
#ifdef NETMASK_ARGS
,
ip4_addr1(netif_ip4_netmask(netif)),
ip4_addr2(netif_ip4_netmask(netif)),
ip4_addr3(netif_ip4_netmask(netif)),
ip4_addr4(netif_ip4_netmask(netif))
#endif
);
LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: run_command(\"%s\");\n", buf));
ret = run_command(buf);
if (ret < 0) {
perror("ifconfig failed");
exit(1);
}
if (ret != 0) {
printf("ifconfig returned %d\n", ret);
}
#else
perror("todo: support IPv6 support for non-preconfigured tapif");
exit(1);
#endif
}
#if !NO_SYS
sys_thread_new("tapif_thread", tapif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
#endif
}
static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
struct tapif *tapif = (struct tapif *)netif->state;
char buf[1518];
ssize_t written;
#if 0#endif
if (p->tot_len > sizeof(buf)) {
MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
perror("tapif: packet too large");
return ERR_IF;
}
pbuf_copy_partial(p, buf, p->tot_len, 0);
written = write(tapif->fd, buf, p->tot_len);
if (written < p->tot_len) {
MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
perror("tapif: write");
return ERR_IF;
} else {
MIB2_STATS_NETIF_ADD(netif, ifoutoctets, (u32_t)written);
return ERR_OK;
}
}
static struct pbuf *
low_level_input(struct netif *netif)
{
struct pbuf *p;
u16_t len;
ssize_t readlen;
char buf[1518];
struct tapif *tapif = (struct tapif *)netif->state;
readlen = read(tapif->fd, buf, sizeof(buf));
if (readlen < 0) {
perror("read returned -1");
exit(1);
}
len = (u16_t)readlen;
MIB2_STATS_NETIF_ADD(netif, ifinoctets, len);
#if 0#endif
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
if (p != NULL) {
pbuf_take(p, buf, len);
} else {
MIB2_STATS_NETIF_INC(netif, ifindiscards);
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: could not allocate pbuf\n"));
}
return p;
}
static void
tapif_input(struct netif *netif)
{
struct pbuf *p = low_level_input(netif);
if (p == NULL) {
#if LINK_STATS
LINK_STATS_INC(link.recv);
#endif
LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: low_level_input returned NULL\n"));
return;
}
if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: netif input error\n"));
pbuf_free(p);
}
}
err_t
tapif_init(struct netif *netif)
{
struct tapif *tapif = (struct tapif *)mem_malloc(sizeof(struct tapif));
if (tapif == NULL) {
LWIP_DEBUGF(NETIF_DEBUG, ("tapif_init: out of memory for tapif\n"));
return ERR_MEM;
}
netif->state = tapif;
MIB2_INIT_NETIF(netif, snmp_ifType_other, 100000000);
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
#if LWIP_IPV4 && LWIP_ARP
netif->output = etharp_output;
#endif
#if LWIP_IPV6 && LWIP_ARP
netif->output_ip6 = ethip6_output;
#endif
netif->linkoutput = low_level_output;
netif->mtu = 1500;
low_level_init(netif);
return ERR_OK;
}
void
tapif_poll(struct netif *netif)
{
tapif_input(netif);
}
#if NO_SYS
int
tapif_select(struct netif *netif)
{
fd_set fdset;
int ret;
struct timeval tv;
struct tapif *tapif;
#if LWIP_TIMERS
u32_t msecs = sys_timeouts_sleeptime();
#else
u32_t msecs = -1;
#endif
tapif = (struct tapif *)netif->state;
tv.tv_sec = msecs / 1000;
tv.tv_usec = (msecs % 1000) * 1000;
FD_ZERO(&fdset);
FD_SET(tapif->fd, &fdset);
ret = select(tapif->fd + 1, &fdset, NULL, NULL, &tv);
if (ret > 0) {
tapif_input(netif);
}
return ret;
}
#else
static void
tapif_thread(void *arg)
{
struct netif *netif;
struct tapif *tapif;
fd_set fdset;
int ret;
netif = (struct netif *)arg;
tapif = (struct tapif *)netif->state;
while(1) {
FD_ZERO(&fdset);
FD_SET(tapif->fd, &fdset);
ret = select(tapif->fd + 1, &fdset, NULL, NULL, NULL);
if(ret == 1) {
tapif_input(netif);
} else if(ret == -1) {
perror("tapif_thread: select");
}
}
}
#endif
#endif