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 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 *************/

int tun_open()
{
struct ifreq ifr;
int istun = 1;
char tunname[14];
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);
}
else
{
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;
strcpy(ifr.ifr_name,buff);
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0)
{
if (errno == EBADFD)
{
/* Try old ioctl */
if (ioctl(fd, OTUNSETIFF, (void *) &ifr) < 0)
{
printf ("IOCONTROL Failed \n");
return -2;
}
}
printf ("IOCONTROL Succedeed with : %s \n",ifr.ifr_name);
return fd;
}
}

return fd;
}
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 )
continue;
return -1;
}
if( !w )
return 0;
len -= w; buf += w; t += w;
}

return t;
}
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 ?

Regards
Alan J