2

My app creates a tap interface, and everything works well. But on FreeBSD, when it exits, the tap interface remains. To delete it, I have to manually run this command:

sudo ifconfig tap0 destroy

But I'd like to do this programmatically within my application. Where can I find the docs for SIOCIFDESTROY? Here is what I've tried when my app exits:

struct ifreq ifr;
memset(&ifr, '\0', sizeof(ifr));
strcpy(ifr.ifr_name, "tap0");
int sock = socket(PF_INET, SOCK_STREAM, 0);
err = ioctl(sock, SIOCIFDESTROY, &ifr);

At this point, err is zero, but the tap interface still exists when the app ends. Anyone know what else I might be missing?

Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • Nevermind...! Of all the things I tried, this exact code I pasted in my question is what _did_ work. I just hadn't noticed I'd found the solution because of all the previously-created TAP interfaces. Not sure what to do with this question. Feel free to close, or leave open for any future developers looking to use SIOCIFDESTROY. – Stéphane Sep 01 '14 at 07:33
  • You might want to answer the question - and repeat the working code - and explain what was happening. This seems valuable and that way the question (probably) won't be deleted. – cnicutar Sep 01 '14 at 08:05

1 Answers1

2

The tricky part was trying to find documentation to describe is the parameter to pass to ioctl(). I never did find anything decent to read.

Turns out a completely blank ifreq with just the tap interface name set is all that is needed. In addition to the original code I included in the question, also note that I close the tap device file descriptor prior to deleting the actual tap interface. I can only imagine that might also be relevant:

    close(device_fd);
    struct ifreq ifr;
    memset(&ifr, '\0', sizeof(ifr));
    strcpy(ifr.ifr_name, "tap0");
    int sock = socket(PF_INET, SOCK_STREAM, 0);
    err = ioctl(sock, SIOCIFDESTROY, &ifr);
Stéphane
  • 19,459
  • 24
  • 95
  • 136