LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Problems to bring up a TUN interface (https://www.linuxquestions.org/questions/linux-networking-3/problems-to-bring-up-a-tun-interface-518263/)

Peter John 01-11-2007 01:59 AM

Problems to bring up a TUN interface
 
ifconfig tun0 10.0.0.1 255.255.255.0 up
gives error saying

SIOIFADDR tun0 no such device
I have installed the latest RPM.

Peter

Peter John 01-11-2007 04:24 AM

I have installed an new RPM.. i still face the same problem

[root@localhost root]# modprobe tun
[root@localhost root]# lsmod | grep tun
tun 5696 0 (unused)
[root@localhost root]# ifconfig tun0 10.0.0.1 netmask 255.255.255.0 up
SIOCSIFADDR: No such device
tun0: unknown interface: No such device
SIOCSIFNETMASK: No such device
tun0: unknown interface: No such device
[root@localhost root]# uname -r
2.4.20-8
[root@localhost root]#

raskin 01-11-2007 04:51 AM

I checked documentation in kernel source, copied an example code and added includes to compile (frankly, I also changed error handling to compile, but it is not so interesting). While the program runs (after it has issued ioctl), tun0 exists. So if you want a program to use tun, this program should call tun_alloc before configuring tun0 interface. This way it will already have its pipe end to get data and tun0 created.

Code:

#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int tun_alloc(char *dev)
{
    struct ifreq ifr;
    int fd, err;

    if( (fd = open("/dev/tun", O_RDWR)) < 0 )
      return -1;

    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_TUN;
    if( *dev )
      strncpy(ifr.ifr_name, dev, IFNAMSIZ);

    if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
      close(fd);
      return err;
    }
    strcpy(dev, ifr.ifr_name);
    return fd;
}             

int main()
{
        char ifname[] = "tun0";
        char s[1024];

        tun_alloc(ifname);
        printf("%s\n",ifname);
        scanf("%s",s);
}


Peter John 01-11-2007 05:10 AM

Am getting compiling errors as
[root@localhost peter]# gcc mytun.c -o tun
In file included from mytun.c:3:
/usr/include/linux/if.h:86: field `ifru_addr' has incomplete type
/usr/include/linux/if.h:87: field `ifru_dstaddr' has incomplete type
/usr/include/linux/if.h:88: field `ifru_broadaddr' has incomplete type
/usr/include/linux/if.h:89: field `ifru_netmask' has incomplete type
/usr/include/linux/if.h:90: field `ifru_hwaddr' has incomplete type
[root@localhost peter]#

raskin 01-11-2007 05:21 AM

Am I right that just that:
Code:

#include <linux/if.h>
int main(){return 0;}

causes an error? And what does
Code:

rpm -qf /usr/include/linux/if.h
say?

Peter John 01-11-2007 05:41 AM

Got it fixed

shayamm.aley 03-29-2010 02:55 PM

Facing Same Problem
 
Hi Peter I am facing same problem how did you fix it?

/usr/include/linux/if.h:165: error: field 'ifru_addr' has incomplete type
/usr/include/linux/if.h:166: error: field 'ifru_dstaddr' has incomplete type
/usr/include/linux/if.h:167: error: field 'ifru_broadaddr' has incomplete type
/usr/include/linux/if.h:168: error: field 'ifru_netmask' has incomplete type
/usr/include/linux/if.h:169: error: field 'ifru_hwaddr' has incomplete type
Hello.c:48:2: warning: no newline at end of file


thanks in advance.

mlang 08-25-2010 11:30 AM

I fixed it by adding...
#include <sys/socket.h>
before
#include <linux/if_tun.h>
#include <linux/if.h>


All times are GMT -5. The time now is 10:19 AM.