I have written the following function that is supposed to create a TUN interface named “my_tun” on Linux:
int tun_open()
{
int fd;
if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
return -1;
}
struct ifreq ifr;
strncpy(ifr.ifr_name, "my_tun", IFNAMSIZ);
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
return -1;
}
return fd;
}
I’m testing it by running it inside a Ubuntu docker container started with --cap-add NET_ADMIN
. Inside this docker container I first execute:
mkdir /dev/net
mknod /dev/net/tun c 10 200
And the run my function. The latter does not fail yet afterwards ifconfig
does not show a TUN interface, only an ethernet and a loopback interface. What am I doing wrong?