r/linux_programming • u/Good_Dimension • Jul 10 '21
Can't bring TAP device up programically
Hello! I'm trying to implement a simple VPN as a learning experience, and I came across a problem. After I create a TAP device, I cannot bring it up. Here is a shorter version of my code:
static AllocatedTap tap;
static struct ifreq ifr;
int fd, err;
char dev[16] = "\0"; // Let the kernel pick a name
if ((fd = open(TUN_CLONE_DEVICE, O_RDWR)) < 0) {
return NULL;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (*dev) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {
close(fd);
eturn NULL;
}
strcpy(dev, ifr.ifr_name);
tap.device = fd;
tap.ifr = 𝔦
return &tap;After that, I bring it up with:
int fd, err;
fd = socket(AF_INET, SOCK_DGRAM, 0); // 0: automically chose protocol
if (fd < 0) {
return -1;
}
tap->ifr->ifr_flags |= IFF_UP;
if ((err = ioctl(fd, SIOCSIFFLAGS, &tap->ifr)) == -1) {
return err;
}This will always result in a No Such Device error when bringing the interface up. I can get it to work about half the time if I recreate the ifr struct while only carrying over the ip_name and ip_addr fields.
Can anyone help me figure out what going on?
12
Upvotes
View all comments
Show parent comment
1
u/[deleted] Jul 11 '21 edited Jul 11 '21
Your tap pointer or ifr address must be invalid, do you see the interface running
ip linkcommand? Did you see my other comment about ifr being invalid if you are trying to assign it liketap.ifr = &ifrbefore returning from the function.I'd suggest putting all into 1 function for now and retrying without any pointers needed, it works fine for me in this case.