Tap interfaces and /dev/net/tun device, using ip tuntap command

C. Paul picture C. Paul · Mar 25, 2013 · Viewed 24.8k times · Source

I'm using ip tuntap to create a tap interface, like this:

$ sudo ip tuntap add mode tap tap0

Afterwards, I set the interface up and address it with the common IP commands. I can see then my interface up and addressed with a simple ifconfig.

Now, I was told by a teacher that by creating a tap interface (named tap0 in that case), I would find a /dev/net/tap0 node, and that I would be able to write in it or to read it. However, I can't find it. I "just" have a /dev/net/tun.

Do I have to deal with this tun node, or am I supposed to really have a tap0 node?

Answer

chrk picture chrk · Feb 6, 2017

It's been a long time since the question was asked, but I thought it would be a good idea to post an actual answer for future reference.

Tap interfaces, as well as tun interfaces, are virtual interfaces provided by the in-kernel TUN/TAP device driver. The only interface this driver provides initially is the character device /dev/net/tun mentioned in the question.

By issuing:

$ sudo ip tuntap add mode tap tap0

we instruct ip tuntap to create a network interface named tap0, which is accomplished using the proper ioctl(2) calls on the aforementioned device file /dev/net/tun to talk to the underlying TUN/TAP device driver, as we can observe in ip tuntap's source code.

One of these ioctl(2) calls (the one with TUNSETIFF option, which is called first) returns a file descriptor associated with the new virtual interface that was created and can be used by processes.


Summing it up:

Do I have to deal with this tun node, or am I supposed to really have a tap0 node?

The /dev/net/tun device file is only used as a starting point to create both tap and tun interfaces, by userspace utilities like iproute2. In the context of this question, there's no need to deal with it as ip tuntap does this job for us.

Any extra /dev/net/tap0 device files are not needed or expected to be created for the processes to use the tap interfaces.