Results 1 to 1 of 1
Hi all, I am new to Linux networking. I want to use IP and ethernet tunnelling using TUN/TAP. I was able to open tun device, but unable to write any ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 05-14-2009 #1
- Join Date
- May 2009
IP and ethernet tunnelling using TUN/TAP
I am new to Linux networking. I want to use IP and ethernet tunnelling using TUN/TAP.
I was able to open tun device, but unable to write any data to it. Can any one guide me of how to configure the TUN/TAP device and write IP /ethernet packet to the opened file system.
I have written some small piece of code to open the TUN device and write to it using putmsg()
/******** Code snippet *************/
struct ifreq ifr;
int istun = 1;
int i,fd, err,k;
char *buff = "tun0";
if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
printf ("The file descriptor for Error :%d\n",fd);
memset(&ifr, 0, sizeof(ifr));
printf ("The file descriptor for /dev/net/tun is %d\n",fd);
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0)
if (errno == EBADFD)
/* Try old ioctl */
if (ioctl(fd, OTUNSETIFF, (void *) &ifr) < 0)
printf ("IOCONTROL Failed \n");
printf ("IOCONTROL Succedeed with : %s \n",ifr.ifr_name);
int tun_write(int fd. char *buf )
int t=0, w;
int len =strlen (buf);
while (len > 0)
if( (w = write(fd, buf, len)) < 0 )
if( errno == EINTR || errno == EAGAIN )
if( !w )
len -= w; buf += w; t += w;
int main ()
int fd, iretunr;
fd = tun_open();
ireturn = tun_write(fd);
I was able to get proper file descriptor for /dev/net/tun. Using this file descriptor I was not able to write to it .The Write API returns -1with the Error Number as Zero. I have tun module compiled with the kernel (2.6.XXX).
do I need to install any TUN/TAP driver for Debian ?