[uClinux-dev] TUN/TAP in 2.6 kernel is hanging on read.. Please help.

Jordan Fuerst jfuerst at freewave.com
Mon Oct 24 19:47:23 EDT 2005


I am trying to utilize the TUN/TAP driver in kernel 2.6 in the latest
uClinux dist (2005).

I followed the /documentation/network/tuntap.txt document to a 'T' (I
think) and still no luck. I use the below code to open the
'/dev/net/tun' node and issue the ioctl. Everything goes along smoothly
until I try to read from tun/tap.

When I read from the driver the program just blocks indefinitely. It
never returns unless I ctrl+c the program away.

Any ideas on what I am doing wrong? I have beed screwing with this for a
couple of weeks now and have not gotten anywhere with this.

* I do indeed have a tun node in /dev/net
* The permissions are set to 777 (until I get this working)
* The node was added as tun,c,10,200 (as found in tuntap.txt)
* I am using kernel 2.6


All help is greatly appreciated.

-Jordan


#include <linux/if.h> 
#include <linux/if_tun.h> 
#include <linux/ioctl.h>    
#include <linux/fcntl.h> //for O_RDWR 
    
int tun_alloc(char *dev); //declare that there will be such a function 

int main(int argc, char **argv) 
{ 
   char *buf[2048], dev[10]="tun0\0"; 
    int fd, len; 
    
   fd = tun_alloc(dev); 
   printf("\r\n%s\r\n",dev); 

   while(1) 
   { 
      len = read(fd,buf,sizeof(buf)); //locks up here 
      if(len>=0) 
           printf("\r\nreceived %d Bytes",len); 
      else 
         printf("\rfailed"); 
   }       
   return 0; 
} 

int tun_alloc(char *dev) 
{ 
   struct ifreq ifr; 
    int fd, err ,r; 
   char buf[2048]; 

    if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) 
     { 
        printf("\r\n Error opening tun device\r\n"); 
        return -1; 
         //return tun_alloc_old(dev); 
     } 
     else 
        printf("\r\n Successfully opened TUN device.\r\n"); 

      memset(&ifr, 0, sizeof(ifr)); 

      /* Flags: IFF_TUN   - TUN device (no Ethernet headers) 
       *        IFF_TAP   - TAP device  
       * 
       *        IFF_NO_PI - Do not provide packet information  
       */ 
      ifr.ifr_flags = IFF_TAP; 
      if( *dev ) 
         strncpy(ifr.ifr_name, dev, IFNAMSIZ); 
     
      if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) 
     { 
         close(fd); 
        printf("\r\n Errot ioctl tun device\r\n"); 
         return err; 
      } 
     else 
        printf("\r\n ioctl successful\r\n"); 
      strcpy(dev, ifr.ifr_name); 

      return fd; 
}



More information about the uClinux-dev mailing list