Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-09-2010, 10:29 PM   #1
LQ Newbie
Registered: Sep 2010
Posts: 5

Rep: Reputation: 0
Problem with ioctl function when openning TUN/TAP device on Linux.

Hi all,

I have just study about using TUN/TAP. I tried to write a small program to open TUN (source code as the following), but the result is always : ioctl on TUNSETIFF failed. That means the function ioctl always return error. I don't know why.

Somebody can help me to solve this problem ? Thank you very much.

int tun_open (char *name)
struct ifreq ifr;
int err, fd;
// struct ip_tunnel_parm p;

if ((fd = open ("/dev/net/tun", O_RDWR)) < 0)
printf ("%s\n", name);
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 (*name)
strncpy (ifr.ifr_name, name, IFNAMSIZ);

if ((err = ioctl (fd, TUNSETIFF, (void *) &ifr)) < 0)
close (fd);
printf ("ioctl on TUNSETIFF failed %d\n", err);
return err;
strncpy (name, ifr.ifr_name, IFNAMSIZ); // the name actually allocated

printf ("Opened %s\n", name);
return fd;

int main ()
int tun_fd, nread, i;
char name[10] = "tun0";
tun_fd = tun_open (name); /* tun interface */
Old 09-09-2010, 11:32 PM   #2
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,125
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
What's the error it's returning? You can check errno or use something like perror
Old 09-10-2010, 03:14 AM   #3
Registered: Mar 2010
Distribution: Debian
Posts: 180

Rep: Reputation: 31
I'm not too clear on this code, but why are you typecasting &ifr with (void *). is ifr an enumerated type?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
static route for iptables NAT and tun/tap device j-osh Linux - Networking 2 10-05-2015 07:26 AM
create TUN/TAP device with non default name vofka Linux - Networking 2 04-29-2010 04:11 AM
how to allow tun/tap device in firewall? efm Linux - Networking 12 07-22-2005 05:47 AM
Universal tun/tap device driver support giddyupman Linux - Networking 4 05-14-2004 11:04 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration