diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2019-08-07 19:48:30 +0000 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2019-08-07 19:48:30 +0000 |
commit | 6ec262c29b0cc249a44cf87e8833471f690a188b (patch) | |
tree | 7a29a637d29a825346ae8a1a30c8cc950097f1b7 | |
parent | 49c9c9e64ea3a6adc42699c6a82a5ef3050f79f1 (diff) | |
download | ulfougretap-master.tar.gz |
-rw-r--r-- | src/main.c | 19 |
1 files changed, 14 insertions, 5 deletions
@@ -19,6 +19,7 @@ static void setup_tundev(const char *in_ifname) if (fd < 0) fatal("setup_tundev: open("DEVNETTUN"): %s", strerror(errno)); + // Now configure the interface. First set the interface name struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP|IFF_NO_PI; @@ -28,16 +29,24 @@ static void setup_tundev(const char *in_ifname) debug("setup_tundev: created tuntap interface: %s", in_ifname); - struct ifreq ifr2; - memset(&ifr2, 0, sizeof(ifr2)); - ifr2.ifr_flags = IFF_UP|IFF_RUNNING; - ifr2.ifr_mtu = 1438; - strncpy(ifr2.ifr_name, in_ifname, IFNAMSIZ - 1); + // A temporary fd is required int tmpfd = socket(AF_INET, SOCK_DGRAM, 0); if (tmpfd < 0) fatal("setup_tundev: socket(): %s", strerror(errno)); + + struct ifreq ifr2; + memset(&ifr2, 0, sizeof(ifr2)); + strncpy(ifr2.ifr_name, in_ifname, IFNAMSIZ - 1); + + // Bring the interface up. Get current flags first + if (ioctl(tmpfd, SIOCGIFFLAGS, &ifr2) < 0) + fatal("setup_tundev: ioctl(SIOCGIFFLAGS): %s", strerror(errno)); + ifr2.ifr_flags |= IFF_UP|IFF_RUNNING; if (ioctl(tmpfd, SIOCSIFFLAGS, &ifr2) < 0) fatal("setup_tundev: ioctl(SIOCSIFFLAGS): %s", strerror(errno)); + + // Set MTU to 1438 by default + ifr2.ifr_mtu = 1438; if (ioctl(tmpfd, SIOCSIFMTU, &ifr2) < 0) fatal("setup_tundev: ioctl(SIOCSIFMTU): %s", strerror(errno)); close(tmpfd); |