Tap interfaces and /dev/net/tun device, using ip tuntap command
Solution 1
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.
Solution 2
you need to activate that link with command
ip link set dev tap0 up
after that you can use it.
C. Paul
Updated on February 07, 2020Comments
-
C. Paul over 4 years
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?
-
Void Star over 5 yearsthis did not work for me, and I think more explanation of why this is necessary is needed
-
Void Star over 5 years"Any extra /dev/net/tap0 device files are not needed or expected to be created for the processes to use the tap interfaces." Why? how would I use the device without a device file or a file descriptor?
-
chrk over 5 years@VoidStar It depends. As you would have noticed if you had actually followed the link to
ip tuntap
's source code in my answer, you actually do have a file descriptor if you create the tap interface programatically--it's the one you get byopen(2)
ing the/dev/net/tun
device and use to do the appropriateioctl(2)
calls. If you don't create it programmatically, then you can think of the tap interface as a network interface, much like e.g.eth0
. Do you need a file descriptor to "use"eth0
? How do you "use" a network interface? You may attach it to a bridge, set routing rules, etc, etc. -
Void Star over 5 yearsIf I use
ip tuntap add
I am not directly manipulating/dev/net/tun
norioctl(2)
so I do not really understand how that is relevant. The kernel appears to be aware of the device, but again, how does the user space program access it? -
chrk over 5 years@VoidStar Any other process can use it in exactly the same way as it uses the rest of the network interfaces on the host. Assign an address to the tap interface, configure it properly, e.g. set routing rules, iptables rules, etc, and use it as always, e.g. using sockets. There's not much special in using the tap interface in a non-programmatic manner in comparison to the other network interfaces on the host.
-
chrk over 5 years@VoidStar Configuring network interfaces is obviously way out of the scope of the original question here, but there are plenty of resources you can find about it, on this site, on other blogs, articles and forums, and of course the man pages. Consider posting a new question if you actually have a specific question, as the rules suggest.
-
Aleksei Kurepin over 5 yearsHi. it would be nice, when you describe your case, kernel, modules etc. Since year 2013 many things have changed. Maybe you try to implement this on docker?
-
rakslice almost 3 years@voidstar a 'persistent' tap interface you create with
ip tuntap add
is just sort of preset so you can configure the user and group ownership and set up the host system side of the network interface configuration before the user space program has even opened the tap interface. How the user space program accesses it is that it opens/dev/net/tun
, and gives its fd a configuration using theioctl
TUNSETIFF
and as part of that it gives it a name that matches the existing 'persistent' interface kernel.org/doc/html/latest/networking/…