version 1.97, 2015/04/24 01:36:00 |
version 1.98, 2015/10/07 00:54:06 |
|
|
} |
} |
} else { |
} else { |
debug("%s: invalid tunnel %u", __func__, tun); |
debug("%s: invalid tunnel %u", __func__, tun); |
return (-1); |
return -1; |
} |
} |
|
|
if (fd < 0) { |
if (fd < 0) { |
debug("%s: %s open failed: %s", __func__, name, strerror(errno)); |
debug("%s: %s open: %s", __func__, name, strerror(errno)); |
return (-1); |
return -1; |
} |
} |
|
|
debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
debug("%s: %s mode %d fd %d", __func__, name, mode, fd); |
|
|
/* Set the tunnel device operation mode */ |
/* Set the tunnel device operation mode */ |
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun); |
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun); |
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
|
error("%s: socket: %s", __func__, strerror(errno)); |
goto failed; |
goto failed; |
|
} |
|
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) { |
|
debug("%s: get interface %s flags: %s", __func__, |
|
ifr.ifr_name, strerror(errno)); |
goto failed; |
goto failed; |
|
} |
|
|
/* Set interface mode */ |
/* Set interface mode if not already in correct mode */ |
ifr.ifr_flags &= ~IFF_UP; |
if ((mode == SSH_TUNMODE_ETHERNET && !(ifr.ifr_flags & IFF_LINK0)) || |
if (mode == SSH_TUNMODE_ETHERNET) |
(mode != SSH_TUNMODE_ETHERNET && (ifr.ifr_flags & IFF_LINK0))) { |
ifr.ifr_flags |= IFF_LINK0; |
ifr.ifr_flags &= ~IFF_UP; |
else |
ifr.ifr_flags ^= IFF_LINK0; |
ifr.ifr_flags &= ~IFF_LINK0; |
if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) { |
if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
debug("%s: reset interface %s flags: %s", __func__, |
goto failed; |
ifr.ifr_name, strerror(errno)); |
|
goto failed; |
|
} |
|
} |
|
|
/* Bring interface up */ |
/* Bring interface up if it is not already */ |
ifr.ifr_flags |= IFF_UP; |
if (!(ifr.ifr_flags & IFF_UP)) { |
if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) |
ifr.ifr_flags |= IFF_UP; |
goto failed; |
if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) { |
|
debug("%s: activate interface %s: %s", __func__, |
|
ifr.ifr_name, strerror(errno)); |
|
goto failed; |
|
} |
|
} |
|
|
close(sock); |
close(sock); |
return (fd); |
return fd; |
|
|
failed: |
failed: |
if (fd >= 0) |
if (fd >= 0) |
close(fd); |
close(fd); |
if (sock >= 0) |
if (sock >= 0) |
close(sock); |
close(sock); |
debug("%s: failed to set %s mode %d: %s", __func__, name, |
return -1; |
mode, strerror(errno)); |
|
return (-1); |
|
} |
} |
|
|
void |
void |