freebsdioctlifconfigtun-tap

how to use SIOCIFDESTROY in FreeBSD?


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?


Solution

  • 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);