43 #include <sys/types.h> 50 #include <sys/ioctl.h> 51 #include <sys/socket.h> 56 #define LOG_MODULE "Tun6" 57 #define LOG_LEVEL LOG_LEVEL_WARN 61 #include <linux/if_tun.h> 68 static const char *config_ipaddr =
"fd00::1/64";
70 static char config_tundev[IFNAMSIZ + 1] =
"tun0";
74 static int tunfd = -1;
76 static int set_fd(fd_set *rset, fd_set *wset);
77 static void handle_fd(fd_set *rset, fd_set *wset);
78 static const struct select_callback tun_select_callback = {
84 static int ssystem(
const char *fmt, ...)
85 __attribute__((__format__ (__printf__, 1, 2)));
87 ssystem(const
char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
90 static 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; }'" 118 sigcleanup(
int signo)
120 fprintf(stderr,
"signal %d\n", signo);
126 ifconf(
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);
145 tun_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);
178 devopen(
const char *dev,
int flags)
182 strncat(t, dev,
sizeof(t) - 5);
183 return open(t, flags);
187 tun_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);
236 tun_output(uint8_t *data,
int len)
239 if(tunfd != -1 && write(tunfd, data, len) != len) {
240 err(1,
"serial_to_tun: write");
247 tun_input(
unsigned char *data,
int maxlen)
256 if((size = read(tunfd, data, maxlen)) == -1) {
257 err(1,
"tun_input: read");
264 output(
const linkaddr_t *localdest)
277 set_fd(fd_set *rset, fd_set *wset)
290 handle_fd(fd_set *rset, fd_set *wset)
299 LOG_INFO(
"Tun6-handle FD\n");
301 if(FD_ISSET(tunfd, rset)) {
303 LOG_DBG(
"TUN data incoming read:%d\n", size);
310 static void input(
void)
313 LOG_DBG(
"Tun6 - input\n");
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
uint16_t uip_len
The length of the packet in the uip_buf buffer.
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.
Header file for IPv6-related data structures.
The structure of a network driver in Contiki.
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Header file for the uIP TCP/IP stack.
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the logging system
static void input(void)
Process a received 6lowpan packet.
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.