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
I came across a nice intro tutorial about this
http://backreference.org/2010/03/26/tuntap-interface-tutorial/
It comes with a source tarball.
It was in the same set of Google results as this question. :-)
Read
/usr/src/linux/Documentation/networking/tuntap.txt
.You are supposed to
open
the/dev/net/tun
device. A subsequentioctl
on the open fd will create thetun0
(or whatever you wish to name it) network interface. Linux's network interfaces do not correspond to any/dev/*
device.There are no
/dev/tunX
device files. Instead, you open the/dev/net/tun
and configure it viaioctl()
to "point" totun0
. To show the basic procedure, I will create the TUN interface using the command line toolip tun tap
and then show the C code to read from that TUN device. So to create the tun interface via commands line:Now we have
tun0
created. To read / write packets to this interface from an user space program you need to interact with the/dev/net/tun
device file usingioctl()
. Here is an example that will read the packets arriving at thetun0
interface and print the size: