From 6ec262c29b0cc249a44cf87e8833471f690a188b Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Wed, 7 Aug 2019 19:48:30 +0000 Subject: get current interface flags before we set IFF_UP|IFF_RUNNING --- src/main.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index dfe63d4..91dc9aa 100644 --- a/src/main.c +++ b/src/main.c @@ -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); -- cgit v1.2.3