title: How to interface with the Linux tun driver
body: <p>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.</p>
  
  <p>I am developing on Ubuntu 9.04, and I have the tunnel driver kernel module loaded.</p>
  
  <p>There exists the device <code>/dev/net/tun</code>, however there are no <code>/dev/tunX</code> devices. I am unable to create these devices using <code>ifconfig</code> - whenever I run <code>/sbin/ifconfig tun0 up</code>, for example, I get the following error: </p>
  
  <blockquote>
    <p>tun0: ERROR while getting interface flags: No such device.</p>
  </blockquote>
  
  <p>If I attempt to look at the <code>/dev/net/tun</code> device, the following error is presented:</p>
  
  <blockquote>
    <p>cat: /dev/net/tun: File descriptor in bad state.</p>
  </blockquote>
  
  <p>Attempting to open <code>/dev/tunX</code> via a small program, basically, a simple </p>
  
  <pre><code>tun_fd = open( "/dev/tun0", O_RDWR )
  </code></pre>
  
  <p>returns -1: the application is running as root and still cannot open this tunnel device. It is possible to open <code>/dev/net/tun</code>, however this does not appear to generate a new <code>/dev/tunX</code> device to use instead.</p>
  
  <p>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.</p>
  
  <p>Thanks;
  ~Robert</p>