#include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { /* If IFF_NO_PI is specified, this still sort of works but it * bumps the device error counters, which we don't want, so * it's best not to use this trick with IFF_NO_PI. */ struct ifreq ifr = { .ifr_flags = IFF_TUN }; int tun, sock, ret; tun = open("/dev/net/tun", O_RDWR); if (tun < 0) { perror("[-] open(/dev/net/tun)"); return 1; } sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("[-] socket(AF_INET, SOCK_DGRAM)"); return 1; } ret = ioctl(tun, TUNSETIFF, &ifr); if (ret < 0) { perror("[-] ioctl(TUNSETIFF)"); return 1; } if (write(tun, NULL, 0) >= 0 || errno != EIO) perror("[-] write(if:down, NULL, 0) did not return -EIO"); else fprintf(stderr, "[+] write(if:down, NULL, 0) returned -EIO: test successful\n"); ifr.ifr_flags = IFF_UP; ret = ioctl(sock, SIOCSIFFLAGS, &ifr); if (ret < 0) { perror("[-] ioctl(SIOCSIFFLAGS)"); return 1; } if (write(tun, NULL, 0) >= 0 || errno != EINVAL) perror("[-] write(if:up, NULL, 0) did not return -EINVAL"); else fprintf(stderr, "[+] write(if:up, NULL, 0) returned -EINVAL: test successful\n"); return 0; }