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.
This commit is contained in:
Tobias Brunner
2017-05-19 15:28:46 +02:00
parent fa959c0732
commit 59e6e93323
+18 -3
View File
@@ -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