51#include <sys/socket.h>
56#define LOG_MODULE "Tun6"
57#define LOG_LEVEL LOG_LEVEL_WARN
61#include <linux/if_tun.h>
68static const char *config_ipaddr =
"fd00::1/64";
70static char config_tundev[IFNAMSIZ + 1] =
"tun0";
76static int set_fd(fd_set *rset, fd_set *wset);
77static void handle_fd(fd_set *rset, fd_set *wset);
78static const struct select_callback tun_select_callback = {
84static int ssystem(
const char *fmt, ...)
85 __attribute__((__format__ (__printf__, 1, 2)));
87ssystem(const
char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
90static ssystem(const
char *fmt, ...)
95 vsnprintf(cmd,
sizeof(cmd), fmt, ap);
97 LOG_INFO(
"%s\n", cmd);
106 ssystem(
"ifconfig %s down", config_tundev);
108 ssystem(
"sysctl -w net.ipv6.conf.all.forwarding=1");
110 ssystem(
"netstat -nr"
111 " | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
120 fprintf(stderr,
"signal %d\n", signo);
126ifconf(
const char *tundev,
const char *
ipaddr)
129 ssystem(
"ifconfig %s inet `hostname` up", tundev);
130 ssystem(
"ifconfig %s add %s", tundev,
ipaddr);
131#elif defined(__APPLE__)
132 ssystem(
"ifconfig %s inet6 %s up", tundev,
ipaddr);
133 ssystem(
"sysctl -w net.inet.ip.forwarding=1");
135 ssystem(
"ifconfig %s inet `hostname` %s up", tundev,
ipaddr);
136 ssystem(
"sysctl -w net.inet.ip.forwarding=1");
140 ssystem(
"ifconfig %s\n", tundev);
145tun_alloc(
char *dev, uint16_t devsize)
149 LOG_INFO(
"Opening: %s\n", dev);
150 if( (fd = open(
"/dev/net/tun", O_RDWR)) < 0 ) {
155 memset(&ifr, 0,
sizeof(ifr));
160 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
162 memcpy(ifr.ifr_name, dev, MIN(
sizeof(ifr.ifr_name), devsize));
164 if((err = ioctl(fd, TUNSETIFF, (
void *) &ifr)) < 0 ) {
170 LOG_INFO(
"Using '%s' vs '%s'\n", dev, ifr.ifr_name);
171 strncpy(dev, ifr.ifr_name, MIN(devsize - 1,
sizeof(ifr.ifr_name)));
172 dev[devsize - 1] =
'\0';
173 LOG_INFO(
"Using %s\n", dev);
178devopen(
const char *dev,
int flags)
182 strncat(t, dev,
sizeof(t) - 5);
183 return open(t, flags);
187tun_alloc(
char *dev, uint16_t devsize)
189 LOG_INFO(
"Opening: %s\n", dev);
190 return devopen(dev, O_RDWR);
199 setvbuf(stdout, NULL, _IOLBF, 0);
209 setvbuf(stdout, NULL, _IOLBF, 0);
211 LOG_INFO(
"Initializing tun interface\n");
213 tunfd = tun_alloc(config_tundev,
sizeof(config_tundev));
215 LOG_WARN(
"Failed to open tun device (you may be lacking permission). Running without network.\n");
220 LOG_INFO(
"Tun open:%d\n", tunfd);
222 select_set_callback(tunfd, &tun_select_callback);
224 fprintf(stderr,
"opened %s device ``/dev/%s''\n",
225 "tun", config_tundev);
228 signal(SIGHUP, sigcleanup);
229 signal(SIGTERM, sigcleanup);
230 signal(SIGINT, sigcleanup);
231 ifconf(config_tundev, config_ipaddr);
236tun_output(uint8_t *data,
int len)
239 if(tunfd != -1 && write(tunfd, data, len) != len) {
240 err(1,
"serial_to_tun: write");
246tun_input(
unsigned char *data,
int maxlen)
255 if((size = read(tunfd, data, maxlen)) == -1) {
256 err(1,
"tun_input: read");
263output(
const linkaddr_t *localdest)
276set_fd(fd_set *rset, fd_set *wset)
289handle_fd(fd_set *rset, fd_set *wset)
298 LOG_INFO(
"Tun6-handle FD\n");
300 if(FD_ISSET(tunfd, rset)) {
302 LOG_DBG(
"TUN data incoming read:%d\n", size);
309static void input(
void)
312 LOG_DBG(
"Tun6 - input\n");
#define CC_NORETURN
Configure if the C compiler supports functions that are not meant to return e.g.
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
static void input(void)
Process a received 6lowpan packet.
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
The structure of a network driver in Contiki.
Header file for IPv6-related data structures.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Header file for the uIP TCP/IP stack.