|
|
|
@ -52,7 +52,7 @@
|
|
|
|
|
static int
|
|
|
|
|
tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
{
|
|
|
|
|
int if_fd, ip_muxid, ppa = -1;
|
|
|
|
|
int if_fd, fd, ip_muxid, ppa = -1;
|
|
|
|
|
struct lifreq lifr;
|
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
const char *ptr = NULL;
|
|
|
|
@ -74,7 +74,7 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((dev->tun_fd = open("/dev/tun", O_RDWR, 0)) < 0)
|
|
|
|
|
if ((if_fd = open("/dev/tun", O_RDWR, 0)) < 0)
|
|
|
|
|
{
|
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "Can't open /dev/tun");
|
|
|
|
|
return -1;
|
|
|
|
@ -102,7 +102,7 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
bool found_one = false;
|
|
|
|
|
while (!found_one && ppa < 64)
|
|
|
|
|
{
|
|
|
|
|
int new_ppa = ioctl(dev->tun_fd, I_STR, &strioc_ppa);
|
|
|
|
|
int new_ppa = ioctl(if_fd, I_STR, &strioc_ppa);
|
|
|
|
|
if (new_ppa >= 0)
|
|
|
|
|
{
|
|
|
|
|
char* msg = alloca(512);
|
|
|
|
@ -127,7 +127,7 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
}
|
|
|
|
|
else /* try this particular one */
|
|
|
|
|
{
|
|
|
|
|
if ((ppa = ioctl(dev->tun_fd, I_STR, &strioc_ppa)) < 0)
|
|
|
|
|
if ((ppa = ioctl(if_fd, I_STR, &strioc_ppa)) < 0)
|
|
|
|
|
{
|
|
|
|
|
char *msg = alloca(512);
|
|
|
|
|
sprintf(msg, "Can't assign PPA for new interface (tun%i)", ppa);
|
|
|
|
@ -136,20 +136,20 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((if_fd = open("/dev/tun", O_RDWR, 0)) < 0)
|
|
|
|
|
if ((fd = open("/dev/tun", O_RDWR, 0)) < 0)
|
|
|
|
|
{
|
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "Can't open /dev/tun (2)");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ioctl(if_fd, I_PUSH, "ip") < 0)
|
|
|
|
|
if (ioctl(fd, I_PUSH, "ip") < 0)
|
|
|
|
|
{
|
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "Can't push IP module");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Assign ppa according to the unit number returned by tun device */
|
|
|
|
|
if (ioctl(if_fd, IF_UNITSEL, (char *) &ppa) < 0)
|
|
|
|
|
if (ioctl(fd, IF_UNITSEL, (char *) &ppa) < 0)
|
|
|
|
|
{
|
|
|
|
|
char *msg = alloca(512);
|
|
|
|
|
sprintf(msg, "Can't set PPA %i", ppa);
|
|
|
|
@ -159,7 +159,7 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
|
|
|
|
|
snprintf(dev->internal_name, IF_NAMESIZE, "%s%d", "tun", ppa);
|
|
|
|
|
|
|
|
|
|
if ((ip_muxid = ioctl(dev->ip_fd, I_PLINK, if_fd)) < 0)
|
|
|
|
|
if ((ip_muxid = ioctl(dev->ip_fd, I_PLINK, fd)) < 0)
|
|
|
|
|
{
|
|
|
|
|
tuntap_log(TUNTAP_LOG_ERR, "Can't link tun device to IP");
|
|
|
|
|
return -1;
|
|
|
|
@ -177,8 +177,10 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fcntl(dev->tun_fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
fcntl(dev->tun_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
fcntl(if_fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
fcntl(if_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
fcntl(dev->ip_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
char *msg = alloca(512);
|
|
|
|
|
sprintf(msg, "TUN device %s opened as %s", dev->if_name, dev->internal_name);
|
|
|
|
@ -194,7 +196,8 @@ tuntap_sys_create_dev(struct device *dev, int tun)
|
|
|
|
|
}
|
|
|
|
|
/* Save flags for tuntap_{up, down} */
|
|
|
|
|
dev->flags = ifr.ifr_flags;
|
|
|
|
|
return 0;
|
|
|
|
|
dev->reserved = fd;
|
|
|
|
|
return if_fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
@ -245,6 +248,8 @@ tuntap_sys_destroy(struct device *dev)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(dev->ip_fd);
|
|
|
|
|
close(dev->reserved);
|
|
|
|
|
dev->reserved = -1;
|
|
|
|
|
dev->ip_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|