tags:

views:

1305

answers:

2

Hello;

I'm having a hard time figuring this problem out - I am trying to write a program that will interact with the Linux tunnel driver. At a very basic level, I simply want to create an application that is able to transfer data over a network tunnel. However, I am completely at a loss as to how to properly set up the tunnel driver in order to accomplish this.

I am developing on Ubuntu 9.04, and I have the tunnel driver kernel module loaded.

There exists the device /dev/net/tun, however there are no /dev/tunX devices. I am unable to create these devices using ifconfig - whenever I run /sbin/ifconfig tun0 up, for example, I get the following error: tun0: ERROR while getting interface flags: No such device. If I attempt to look at the /dev/net/tun device, the following error is presented: cat: /dev/net/tun: File descriptor in bad state. Attempting to open /dev/tunX via a small program (basically, a simple tun_fd = open( "/dev/tun0", O_RDWR ) returns -1: the application is running as root and still cannot open this tunnel device. It is possible to open /dev/net/tun, however this does not appear to generate a new /dev/tunX device to use instead.

So, in summary - how does one go about writing an application that wishes to use the Linux tunnel driver? Any insights would be greatly appreciated.

Thanks; ~Robert

+2  A: 

Read /usr/src/linux/Documentation/networking/tuntap.txt.

You are supposed to open the /dev/net/tun device. A subsequent ioctl on the open fd will create the tun0 (or whatever you wish to name it) network interface. Linux's network interfaces do not correspond to any /dev/* device.

ephemient
@rmrobins; what did you do to actually get this working? I believe I have a very similar problem to your original question. I have the /dev/net/tun device visible, but opening this does not produce a network interface. I have been trying to use the br_select.c and br_sigio.c examples.
simon
As mentioned above, open /dev/net/tun. Then, an ioctl will be used to create the actual interface itself. The ioctl is called TUNSETIFF, and the argument is of type struct ifreq. The flags of the ifreq struct should be set to IFF_TUN. Once the ioctl has returned, the ifr_name field of the ifreq struct will be set with the name of the opened interface. Hope this helps!
rmrobins
A: 

See http://vtun.cvs.sourceforge.net/viewvc/vtun/tun/examples/ for working examples.

sharth
I have spent a lot of time looking at these examples, however they actually don't work! They attempt to directly open the /dev/tunX devices, which is incorrect (as shown by the link that ephemient provided). This actually caused much of my confusion.
rmrobins