• Uncategorized

About c : TUN-interface-created-from-C-not-showing-up-in-ifconfig-output

Question Detail

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?

Question Answer

I recommend you to check the source code of ifconfig tool. It should contain correct code to create tun devices. Use it as reference.

Most probably, the newest edition of “UNIX Network Programming” (Stevens et al.) should have a description somewher on how to create a tun device also.

Try “ip link” and you might see it there provided code above didn’t return -1. Also your interface is non-persistent therefore you need to keep the program running when u try ifconfig or IP link.
Try to add a sleep before “return fd” to buy time and see if that helps.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.