From: Tobias Brunner Date: Wed, 3 May 2017 08:01:12 +0000 (+0200) Subject: tun-device: Use next free TUN device on FreeBSD X-Git-Tag: 5.5.3~44 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=59e6e93323b2b19ee7fbfc111bafc47acf7e5122;p=thirdparty%2Fstrongswan.git tun-device: Use next free TUN device on FreeBSD While this API is documented as legacy (and there is a sysctl option to disable it) the documentation also mentions that it will probably stay enabled by default due to compatibility issues with existing applications. With the previous approach only 255 devices could be opened then the daemon had to be restarted. Fixes #2313. --- diff --git a/src/libstrongswan/networking/tun_device.c b/src/libstrongswan/networking/tun_device.c index ec6dac7ce7..86951f1e77 100644 --- a/src/libstrongswan/networking/tun_device.c +++ b/src/libstrongswan/networking/tun_device.c @@ -490,10 +490,25 @@ static bool init_tun(private_tun_device_t *this, const char *name_tmpl) strncpy(this->if_name, ifr.ifr_name, IFNAMSIZ); return TRUE; -#else /* !IFF_TUN */ +#elif defined(__FreeBSD__) - /* this works on FreeBSD and might also work on Linux with older TUN - * driver versions (no IFF_TUN) */ + if (name_tmpl) + { + DBG1(DBG_LIB, "arbitrary naming of TUN devices is not supported"); + } + + this->tunfd = open("/dev/tun", O_RDWR); + if (this->tunfd < 0) + { + DBG1(DBG_LIB, "failed to open /dev/tun: %s", strerror(errno)); + return FALSE; + } + fdevname_r(this->tunfd, this->if_name, IFNAMSIZ); + return TRUE; + +#else /* !__FreeBSD__ */ + + /* this might work on Linux with older TUN driver versions (no IFF_TUN) */ char devname[IFNAMSIZ]; /* the same process is allowed to open a device again, but that's not what * we want (unless we previously closed a device, which we don't know at