2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001, Swedish Institute of Computer Science.
|
2007-03-22 18:59:34 +00:00
|
|
|
* All rights reserved.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2007-03-22 18:59:34 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2007-03-22 18:59:34 +00:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2007-03-22 18:59:34 +00:00
|
|
|
* 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.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2007-03-22 18:59:34 +00:00
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2007-03-22 18:59:34 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ip/uip.h"
|
|
|
|
#include "net/ip/uipopt.h"
|
2013-05-20 00:27:30 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#if !NETSTACK_CONF_WITH_IPV6
|
2013-05-20 00:27:30 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#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>
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_tun.h>
|
|
|
|
#define DEVTAP "/dev/net/tun"
|
|
|
|
#else /* linux */
|
|
|
|
#define DEVTAP "/dev/tap0"
|
|
|
|
#endif /* linux */
|
|
|
|
|
2007-03-27 21:47:17 +00:00
|
|
|
#include "contiki-net.h"
|
|
|
|
#include "tapdev.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#define DROP 0
|
|
|
|
|
2007-03-27 21:47:17 +00:00
|
|
|
#if DROP
|
2006-06-17 22:41:10 +00:00
|
|
|
static int drop = 0;
|
2007-03-27 21:47:17 +00:00
|
|
|
#endif
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
static int fd;
|
|
|
|
|
|
|
|
static unsigned long lasttime;
|
|
|
|
|
|
|
|
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
|
|
|
|
|
2012-11-12 22:13:07 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2012-05-17 17:34:58 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
tapdev_fd(void)
|
|
|
|
{
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:26:17 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
remove_route(void)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
2012-05-17 17:34:58 +00:00
|
|
|
snprintf(buf, sizeof(buf), "route delete -net 172.18.0.0");
|
2007-03-29 22:26:17 +00:00
|
|
|
system(buf);
|
2012-11-12 22:13:07 +00:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
2007-03-29 22:26:17 +00:00
|
|
|
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
tapdev_init(void)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
fd = open(DEVTAP, O_RDWR);
|
|
|
|
if(fd == -1) {
|
|
|
|
perror("tapdev: tapdev_init: open");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
|
|
|
|
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
|
2013-04-16 18:06:01 +00:00
|
|
|
perror("ioctl(TUNSETIFF)");
|
2006-06-17 22:41:10 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* Linux */
|
|
|
|
|
2012-05-17 17:34:58 +00:00
|
|
|
snprintf(buf, sizeof(buf), "ifconfig tap0 inet 172.18.0.1/16");
|
2007-03-29 22:26:17 +00:00
|
|
|
system(buf);
|
2012-11-12 22:13:07 +00:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
2007-03-31 18:49:37 +00:00
|
|
|
#ifdef linux
|
|
|
|
/* route add for linux */
|
2012-05-17 17:34:58 +00:00
|
|
|
snprintf(buf, sizeof(buf), "route add -net 172.18.0.0/16 dev tap0");
|
2007-03-31 18:49:37 +00:00
|
|
|
#else /* linux */
|
|
|
|
/* route add for freebsd */
|
2012-05-17 17:34:58 +00:00
|
|
|
snprintf(buf, sizeof(buf), "route add -net 172.18.0.0/16 -iface tap0");
|
2007-03-31 18:49:37 +00:00
|
|
|
#endif /* linux */
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
system(buf);
|
2012-11-12 22:13:07 +00:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
2007-03-29 22:26:17 +00:00
|
|
|
atexit(remove_route);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
lasttime = 0;
|
2007-03-27 21:47:17 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-02-20 18:42:51 +00:00
|
|
|
uint16_t
|
2007-03-27 21:47:17 +00:00
|
|
|
tapdev_poll(void)
|
|
|
|
{
|
|
|
|
fd_set fdset;
|
|
|
|
struct timeval tv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
if(fd > 0) {
|
|
|
|
FD_SET(fd, &fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = select(fd + 1, &fdset, NULL, NULL, &tv);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-03-27 21:47:17 +00:00
|
|
|
if(ret == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = read(fd, uip_buf, UIP_BUFSIZE);
|
2012-11-12 22:13:07 +00:00
|
|
|
PRINTF("tapdev_poll: read %d bytes\n", ret);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-03-27 21:47:17 +00:00
|
|
|
if(ret == -1) {
|
|
|
|
perror("tapdev_poll: read");
|
|
|
|
}
|
|
|
|
return ret;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-27 21:47:17 +00:00
|
|
|
void
|
|
|
|
tapdev_send(void)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if(fd <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2007-03-27 21:47:17 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/* printf("tapdev_send: sending %d bytes\n", size);*/
|
|
|
|
/* check_checksum(uip_buf, size);*/
|
2007-03-27 21:47:17 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#if DROP
|
|
|
|
drop++;
|
|
|
|
if(drop % 8 == 7) {
|
2012-11-12 22:13:07 +00:00
|
|
|
fprintf(stderr, "Dropped an output packet!\n");
|
2006-06-17 22:41:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* DROP */
|
|
|
|
|
2012-11-12 22:13:07 +00:00
|
|
|
PRINTF("tapdev_send: sending %d bytes\n", uip_len);
|
2007-03-27 21:47:17 +00:00
|
|
|
ret = write(fd, uip_buf, uip_len);
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
if(ret == -1) {
|
|
|
|
perror("tap_dev: tapdev_send: writev");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-22 18:59:34 +00:00
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-20 21:29:39 +00:00
|
|
|
void
|
|
|
|
tapdev_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-05-20 00:27:30 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* !NETSTACK_CONF_WITH_IPV6 */
|