64 return "tap-windows6";
89 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 }
92 0xcac88484, 0x7515, 0x4c03, { 0x82, 0xe6, 0x71, 0xa8, 0x7a, 0xba, 0xc3, 0x61 }
97#define NI_TEST_FIRST (1 << 0)
98#define NI_IP_NETMASK (1 << 1)
99#define NI_OPTIONS (1 << 2)
102 const in_addr_t netmask,
const unsigned int flags);
104static void windows_set_mtu(
const int iface_index,
const short family,
const int mtu);
107 DWORD adapter_index);
115#if defined(__GNUC__) || defined(__clang__)
116#pragma GCC diagnostic push
117#pragma GCC diagnostic ignored "-Wsign-conversion"
138 if (addr.
family == AF_INET)
142 msg(
D_IFCONFIG,
"INET address service: %s %s/%d", add ?
"add" :
"remove",
149 msg(
D_IFCONFIG,
"INET6 address service: %s %s/%d", add ?
"add" :
"remove",
160 msg(
M_WARN,
"TUN: %s address failed using service: %s [status=%d if_index=%lu]",
207 size_t dstlen = strlen(
dns.domains);
209 size_t extra = dstlen ? 2 : 1;
210 if (dstlen + srclen + extra >
sizeof(
dns.domains))
212 msg(
M_WARN,
"DNS search domains sent to service truncated to %d", i);
217 dns.domains[dstlen++] =
',';
222 msg(
D_LOW,
"%s DNS domains on '%s' (if_index = %lu) using service",
223 (add ?
"Setting" :
"Deleting"),
dns.iface.name,
dns.iface.index);
231 msg(
M_WARN,
"TUN: %s DNS domains failed using service: %s [status=%d if_name=%s]",
237 msg(
M_INFO,
"DNS domains %s using service", (add ?
"set" :
"deleted"));
250 unsigned int addr_len = add ? len : 0;
251 const char *ip_proto_name = family == AF_INET6 ?
"IPv6" :
"IPv4";
265 .addr_len = addr_len };
270 if (addr_len > _countof(dns.
addr))
272 addr_len = _countof(dns.
addr);
274 msg(
M_WARN,
"Number of %s DNS addresses sent to service truncated to %u",
275 ip_proto_name, addr_len);
278 for (
unsigned int i = 0; i < addr_len; ++i)
280 if (family == AF_INET6)
290 msg(
D_LOW,
"%s %s dns servers on '%s' (if_index = %lu) using service",
300 msg(
M_WARN,
"TUN: %s %s dns failed using service: %s [status=%d if_name=%s]",
306 msg(
M_INFO,
"%s dns servers %s using service", ip_proto_name, (add ?
"set" :
"deleted"));
329 .addr_len = addr_len };
334 if (addr_len > _countof(wins.
addr))
336 addr_len = _countof(wins.
addr);
338 msg(
M_WARN,
"Number of WINS addresses sent to service truncated to %u", addr_len);
341 for (
unsigned int i = 0; i < addr_len; ++i)
346 msg(
D_LOW,
"%s WINS servers on '%s' (if_index = %lu) using service",
356 msg(
M_WARN,
"TUN: %s WINS failed using service: %s [status=%d if_name=%s]",
362 msg(
M_INFO,
"WINS servers %s using service", (add ?
"set" :
"deleted"));
375 const char *family_name = (family == AF_INET6) ?
"IPv6" :
"IPv4";
381 if (family == AF_INET6 && mtu < 1280)
384 "NOTE: IPv6 interface MTU < 1280 conflicts with IETF standards and might not work");
394 msg(
M_NONFATAL,
"TUN: setting %s mtu using service failed: %s [status=%d if_index=%lu]",
400 msg(
M_INFO,
"%s MTU set to %d on interface %lu using service", family_name, mtu,
420 "%s%s -NoProfile -NonInteractive -Command Set-DnsClient -InterfaceIndex %lu -ConnectionSpecificSuffix '%s'",
472 msg(
M_NONFATAL,
"TUN: creating %s adapter using service failed: %s [status=%d]",
487#if defined(__GNUC__) || defined(__clang__)
488#pragma GCC diagnostic pop
494static void solaris_error_close(
struct tuntap *tt,
const struct env_set *es,
const char *actual,
500#if defined(TARGET_DARWIN)
501#include <sys/kern_control.h>
502#include <net/if_utun.h>
503#include <sys/sys_domain.h>
509is_dev_type(
const char *dev,
const char *dev_type,
const char *match_type)
518 return !strcmp(dev_type, match_type);
522 return !strncmp(dev, match_type, strlen(match_type));
557 return "[unknown-dev-type]";
595 const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
598 if (looks_like_netmask)
601 "WARNING: Since you are using --dev tun with a point-to-point topology, the second argument to --ifconfig must be an IP address. You are using something (%s) that looks more like a netmask. %s",
607 if (!looks_like_netmask)
610 "WARNING: Since you are using subnet topology, the second argument to --ifconfig must be a netmask, for example something like 255.255.255.0. %s",
627 msg(
M_INFO,
"CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
638 const in_addr_t test_netmask = 0xFFFFFF00;
639 const in_addr_t public_net =
public & test_netmask;
640 const in_addr_t local_net = local & test_netmask;
641 const in_addr_t remote_net = remote_netmask & test_netmask;
643 if (
public == local ||
public == remote_netmask)
646 "WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
651 if (public_net == local_net || public_net == remote_net)
654 "WARNING: potential conflict between --%s address [%s] and --ifconfig address pair [%s, %s] -- this is a warning only that is triggered when local/remote addresses exist within the same /24 subnet as --ifconfig endpoints. %s",
661 const in_addr_t public_network =
public & remote_netmask;
662 const in_addr_t virtual_network = local & remote_netmask;
663 if (public_network == virtual_network)
666 "WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
682 if ((rgi.
flags & needed) == needed)
685 if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
688 "NOTE: your local LAN uses the extremely common subnet address 192.168.0.x or 192.168.1.x. Be aware that this might create routing conflicts if you connect to the VPN server from public locations such as internet cafes that use the same subnet.");
768 bool tun_p2p =
false;
780 msg(
M_FATAL,
"Error: problem with tun vs. tap setting");
803 setenv_str(es,
"ifconfig_local", ifconfig_local);
806 setenv_str(es,
"ifconfig_remote", ifconfig_remote_netmask);
810 setenv_str(es,
"ifconfig_netmask", ifconfig_remote_netmask);
819 setenv_str(es,
"ifconfig_ipv6_local", ifconfig_ipv6_local);
821 setenv_str(es,
"ifconfig_ipv6_remote", ifconfig_ipv6_remote);
827#if defined(__GNUC__) || defined(__clang__)
828#pragma GCC diagnostic push
829#pragma GCC diagnostic ignored "-Wsign-conversion"
840 const char *dev_type,
842 const char *ifconfig_local_parm,
843 const char *ifconfig_remote_netmask_parm,
844 const char *ifconfig_ipv6_local_parm,
845 int ifconfig_ipv6_netbits_parm,
846 const char *ifconfig_ipv6_remote_parm,
847 const struct addrinfo *local_public,
const struct addrinfo *remote_public,
const bool strict_warn,
859 if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
872 ifconfig_local_parm, 0, NULL, NULL);
876 ifconfig_remote_netmask_parm, 0, NULL, NULL);
883 const struct addrinfo *curele;
891 for (curele = local_public; curele; curele = curele->ai_next)
893 if (curele->ai_family == AF_INET)
896 ntohl(((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr);
901 for (curele = remote_public; curele; curele = curele->ai_next)
903 if (curele->ai_family == AF_INET)
906 ntohl(((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr);
931 if (ifconfig_ipv6_local_parm && ifconfig_ipv6_remote_parm)
937 if (inet_pton(AF_INET6, ifconfig_ipv6_local_parm, &tt->
local_ipv6) != 1
938 || inet_pton(AF_INET6, ifconfig_ipv6_remote_parm, &tt->
remote_ipv6) != 1)
940 msg(
M_FATAL,
"init_tun: problem converting IPv6 ifconfig addresses %s and %s to binary",
941 ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm);
982#if defined(_WIN32) || defined(TARGET_DRAGONFLY)
1019#if defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
1030create_arbitrary_remote(
const struct tuntap *tt)
1036 if (remote == tt->
local)
1058#if !defined(TARGET_LINUX)
1064#if defined(TARGET_LINUX)
1065 if (net_iface_mtu_set(ctx, ifname, tun_mtu) < 0)
1067 msg(
M_FATAL,
"Linux can't set mtu (%d) on %s", tun_mtu, ifname);
1070 if (net_iface_up(ctx, ifname,
true) < 0)
1072 msg(
M_FATAL,
"Linux can't bring %s up", ifname);
1077 msg(
M_FATAL,
"Linux can't add IPv6 to interface %s", ifname);
1079#elif defined(TARGET_ANDROID)
1082 snprintf(out6,
sizeof(out6),
"%s/%d %d", ifconfig_ipv6_local, tt->
netbits_ipv6, tun_mtu);
1083 management_android_control(
management,
"IFCONFIG6", out6);
1084#elif defined(TARGET_SOLARIS)
1093 argv_printf(&
argv,
"%s %s inet6 plumb %s/%d %s mtu %d up", IFCONFIG_PATH, ifname,
1094 ifconfig_ipv6_local, tt->
netbits_ipv6, ifconfig_ipv6_remote, tun_mtu);
1104 solaris_error_close(tt, es, ifname,
true);
1116 argv_printf(&
argv,
"%s %s inet6 addif %s/%d up", IFCONFIG_PATH, ifname, ifconfig_ipv6_local,
1123 solaris_error_close(tt, es, ifname,
true);
1128 argv_printf(&
argv,
"%s %s inet6 mtu %d", IFCONFIG_PATH, ifname, tun_mtu);
1132#elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) || defined(TARGET_DARWIN) \
1133 || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1134 argv_printf(&
argv,
"%s %s inet6 %s/%d mtu %d up", IFCONFIG_PATH, ifname, ifconfig_ipv6_local,
1140#if defined(TARGET_FREEBSD) && __FreeBSD_version >= 1200000 && __FreeBSD_version < 1300000
1162#ifdef TARGET_DRAGONFLY
1166#elif defined(TARGET_AIX)
1167 argv_printf(&
argv,
"%s %s inet6 %s/%d mtu %d up", IFCONFIG_PATH, ifname, ifconfig_ipv6_local,
1178#elif defined(_WIN32)
1182 "******** NOTE: Please manually set the v6 IP of '%s' to %s (if it is not already set)",
1183 ifname, ifconfig_ipv6_local);
1212 argv_printf(&
argv,
"%s%s interface ipv6 set address %lu %s/%d store=active",
1231 "Sorry, but I don't know how to do IPv6 'ifconfig' commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");
1234#if !defined(TARGET_LINUX)
1253#if !defined(_WIN32) && !defined(TARGET_ANDROID)
1260#if !defined(TARGET_LINUX)
1261 const char *ifconfig_local = NULL;
1262 const char *ifconfig_remote_netmask = NULL;
1273#if defined(TARGET_LINUX)
1274 if (net_iface_mtu_set(ctx, ifname, tun_mtu) < 0)
1276 msg(
M_FATAL,
"Linux can't set mtu (%d) on %s", tun_mtu, ifname);
1279 if (net_iface_up(ctx, ifname,
true) < 0)
1281 msg(
M_FATAL,
"Linux can't bring %s up", ifname);
1288 msg(
M_FATAL,
"Linux can't add IP to interface %s", ifname);
1295 msg(
M_FATAL,
"Linux can't add IP to interface %s", ifname);
1298#elif defined(TARGET_ANDROID)
1301 snprintf(out,
sizeof(out),
"%s %s %d %s", ifconfig_local, ifconfig_remote_netmask, tun_mtu,
1303 management_android_control(
management,
"IFCONFIG", out);
1305#elif defined(TARGET_SOLARIS)
1313 argv_printf(&
argv,
"%s %s %s %s mtu %d up", IFCONFIG_PATH, ifname, ifconfig_local,
1314 ifconfig_remote_netmask, tun_mtu);
1319 solaris_error_close(tt, es, ifname,
false);
1322 argv_printf(&
argv,
"%s %s netmask 255.255.255.255", IFCONFIG_PATH, ifname);
1326 argv_printf(&
argv,
"%s %s %s %s netmask %s mtu %d up", IFCONFIG_PATH, ifname,
1327 ifconfig_local, ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1331 argv_printf(&
argv,
"%s %s %s netmask %s up", IFCONFIG_PATH, ifname, ifconfig_local,
1332 ifconfig_remote_netmask);
1338 solaris_error_close(tt, es, ifname,
false);
1354#elif defined(TARGET_OPENBSD)
1367 argv_printf(&
argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up -link0", IFCONFIG_PATH,
1368 ifname, ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1372 remote_end = create_arbitrary_remote(tt);
1373 argv_printf(&
argv,
"%s %s %s %s mtu %d netmask %s up -link0", IFCONFIG_PATH, ifname,
1375 ifconfig_remote_netmask);
1379 argv_printf(&
argv,
"%s %s %s netmask %s mtu %d link0", IFCONFIG_PATH, ifname,
1380 ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1397#elif defined(TARGET_NETBSD)
1402 argv_printf(&
argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up", IFCONFIG_PATH, ifname,
1403 ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1407 remote_end = create_arbitrary_remote(tt);
1408 argv_printf(&
argv,
"%s %s %s %s mtu %d netmask %s up", IFCONFIG_PATH, ifname,
1410 ifconfig_remote_netmask);
1419 argv_printf(&
argv,
"%s %s %s netmask %s mtu %d", IFCONFIG_PATH, ifname, ifconfig_local,
1420 ifconfig_remote_netmask, tun_mtu);
1437#elif defined(TARGET_DARWIN)
1445 msg(
M_INFO,
"NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
1451 argv_printf(&
argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up", IFCONFIG_PATH, ifname,
1452 ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1456 argv_printf(&
argv,
"%s %s %s %s netmask %s mtu %d up", IFCONFIG_PATH, ifname,
1457 ifconfig_local, ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1461 argv_printf(&
argv,
"%s %s %s netmask %s mtu %d up", IFCONFIG_PATH, ifname, ifconfig_local,
1462 ifconfig_remote_netmask, tun_mtu);
1480#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1485 argv_printf(&
argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up", IFCONFIG_PATH, ifname,
1486 ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1491 argv_printf(&
argv,
"%s %s %s/%d mtu %d up", IFCONFIG_PATH, ifname, ifconfig_local, netbits,
1498#elif defined(TARGET_AIX)
1506 msg(
M_FATAL,
"no tun support on AIX (canthappen)");
1510 argv_printf(&
argv,
"%s %s %s netmask %s mtu %d up", IFCONFIG_PATH, ifname, ifconfig_local,
1511 ifconfig_remote_netmask, tun_mtu);
1518#elif defined(_WIN32)
1522 "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
1523 ifname, ifconfig_local, ifconfig_remote_netmask);
1557#elif defined(TARGET_HAIKU)
1559 argv_printf(&
argv,
"%s %s inet %s %s mtu %d up", IFCONFIG_PATH, ifname, ifconfig_local,
1560 ifconfig_remote_netmask, tun_mtu);
1566 "Sorry, but I don't know how to do 'ifconfig' commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");
1569#if !defined(TARGET_LINUX)
1575#if defined(__GNUC__) || defined(__clang__)
1576#pragma GCC diagnostic pop
1587#ifdef ENABLE_MANAGEMENT
1612#if defined(TARGET_LINUX)
1629#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1647#if defined(TARGET_LINUX)
1652#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1654#ifdef TARGET_DRAGONFLY
1703#ifdef TARGET_SOLARIS
1708#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
1726#include <netinet/ip.h>
1729#if defined(__GNUC__) || defined(__clang__)
1730#pragma GCC diagnostic push
1731#pragma GCC diagnostic ignored "-Wsign-conversion"
1734static inline ssize_t
1735header_modify_read_write_return(ssize_t len)
1739 return (
size_t)len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
1748write_tun_header(
const struct tuntap *tt,
const uint8_t *buf,
int len)
1754 const struct ip *iph = (
const struct ip *)buf;
1758 type = htonl(AF_INET6);
1762 type = htonl(AF_INET);
1767 iv[0].iov_base = &type;
1768 iv[0].iov_len =
sizeof(type);
1769 iv[1].iov_base = (
void *)buf;
1770 iv[1].iov_len = len;
1772 return header_modify_read_write_return(writev(tt->fd, iv, 2));
1776 return write(tt->fd, buf, len);
1781read_tun_header(
const struct tuntap *tt, uint8_t *buf,
int len)
1788 iv[0].iov_base = &type;
1789 iv[0].iov_len =
sizeof(type);
1790 iv[1].iov_base = buf;
1791 iv[1].iov_len = len;
1793 return header_modify_read_write_return(readv(tt->fd, iv, 2));
1797 return read(tt->fd, buf, len);
1805#if !defined(TARGET_DARWIN)
1809 return write_tun_header(tt, buf, len);
1815 return read_tun_header(tt, buf, len);
1819#if defined(__GNUC__) || defined(__clang__)
1820#pragma GCC diagnostic pop
1831#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
1833tun_dco_enabled(
const struct tuntap *tt)
1840#if !(defined(_WIN32) || defined(TARGET_LINUX) || defined(TARGET_SOLARIS) || defined(TARGET_ANDROID))
1842open_tun_generic(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt)
1845 char dynamic_name[256];
1846 bool dynamic_opened =
false;
1853 snprintf(tunname,
sizeof(tunname),
"%s", dev_node);
1865 for (
int i = 0; i < 256; ++i)
1868 const char *sep =
"";
1869#if defined(TARGET_HAIKU)
1872 snprintf(tunname,
sizeof(tunname),
"/dev/%s%s%d", dev, sep, i);
1873 snprintf(dynamic_name,
sizeof(dynamic_name),
"%s%s%d", dev, sep, i);
1874 if ((tt->fd = open(tunname, O_RDWR)) > 0)
1876 dynamic_opened =
true;
1881 if (!dynamic_opened)
1883 msg(
M_FATAL,
"Cannot allocate TUN/TAP dev dynamically");
1891 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
1895 if (!dynamic_opened)
1898 if (if_nametoindex(dev) > 0)
1900 msg(
M_INFO,
"TUN/TAP device %s exists previously, keep at program end", dev);
1904 if ((tt->fd = open(tunname, O_RDWR)) < 0)
1906 msg(
M_ERR,
"Cannot open TUN/TAP dev %s", tunname);
1912 msg(
M_INFO,
"TUN/TAP device %s opened", tunname);
1919#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
1921open_tun_dco_generic(
const char *dev,
const char *dev_type,
struct tuntap *tt,
1924 char dynamic_name[256];
1934 if (strcmp(dev,
"tun") == 0)
1936 bool dynamic_opened =
false;
1938 for (
int i = 0; i < 256; ++i)
1940 snprintf(dynamic_name,
sizeof(dynamic_name),
"%s%d", dev, i);
1944 dynamic_opened =
true;
1945 msg(
M_INFO,
"DCO device %s opened", dynamic_name);
1949 else if (ret == -EPERM)
1954 if (!dynamic_opened)
1956 msg(
M_FATAL,
"Cannot allocate DCO dev dynamically");
1970 msg(
M_INFO,
"DCO device %s already exists, won't be destroyed at shutdown", dev);
1975 msg(
M_ERR,
"Cannot open DCO device %s: %s (%d)", dev, strerror(-ret), ret);
1979 msg(
M_INFO,
"DCO device %s opened", dev);
1988#if !(defined(_WIN32) || defined(TARGET_SOLARIS))
1990close_tun_generic(
struct tuntap *tt)
2002#if defined(TARGET_ANDROID)
2004open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2007#define ANDROID_TUNNAME "vpnservice-tun"
2011 int oldtunfd = tt->fd;
2017 management_android_control(
management,
"DNS6SERVER",
2023 management_android_control(
management,
"DNSSERVER",
2039 int android_method = managment_android_persisttun_action(
management);
2041 if (oldtunfd >= 0 && android_method == ANDROID_KEEP_OLD_TUN)
2048 opentun = management_android_control(
management,
"OPENTUN", dev);
2055 if (oldtunfd >= 0 && android_method == ANDROID_OPEN_BEFORE_CLOSE)
2063 if ((tt->fd < 0) || !opentun)
2065 msg(
M_ERR,
"ERROR: Cannot open TUN");
2076 close_tun_generic(tt);
2080#if defined(__GNUC__) || defined(__clang__)
2081#pragma GCC diagnostic push
2082#pragma GCC diagnostic ignored "-Wsign-conversion"
2094 return read(tt->fd, buf,
len);
2097#if defined(__GNUC__) || defined(__clang__)
2098#pragma GCC diagnostic pop
2101#elif defined(TARGET_LINUX)
2106open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2111 if (tun_dco_enabled(tt))
2113 open_tun_dco_generic(dev, dev_type, tt, ctx);
2120 const char *node = dev_node;
2123 node =
"/dev/net/tun";
2129 if ((tt->fd = open(node, O_RDWR)) < 0)
2131 msg(
M_ERR,
"ERROR: Cannot open TUN/TAP dev %s", node);
2135 ifr.ifr_flags = IFF_NO_PI;
2137#ifdef IFF_BACKPRESSURE
2138 ifr.ifr_flags |= IFF_BACKPRESSURE;
2146 ifr.ifr_flags |= IFF_TUN;
2150 ifr.ifr_flags |= IFF_TAP;
2154 msg(
M_FATAL,
"I don't recognize device %s as a tun or tap device", dev);
2160 if (strcmp(dev,
"tun") && strcmp(dev,
"tap"))
2162 strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
2169 if (ioctl(tt->fd, TUNSETIFF, (
void *)&ifr) < 0)
2171 msg(
M_ERR,
"ERROR: Cannot ioctl TUNSETIFF %s", dev);
2174 msg(
M_INFO,
"TUN/TAP device %s opened", ifr.ifr_name);
2181 struct ifreq netifr;
2184 if ((ctl_fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
2187 strlcpy(netifr.ifr_name, ifr.ifr_name, IFNAMSIZ);
2188 netifr.ifr_qlen = tt->
options.txqueuelen;
2189 if (ioctl(ctl_fd, SIOCSIFTXQLEN, (
void *)&netifr) >= 0)
2195 msg(
M_WARN |
M_ERRNO,
"Note: Cannot set tx queue length on %s", ifr.ifr_name);
2201 msg(
M_WARN |
M_ERRNO,
"Note: Cannot open control socket on %s", ifr.ifr_name);
2215open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2223#ifdef ENABLE_FEATURE_TUN_PERSIST
2226tuncfg(
const char *dev,
const char *dev_type,
const char *dev_node,
int persist_mode,
2237 open_tun(dev, dev_type, dev_node, tt, ctx);
2238 if (ioctl(tt->fd, TUNSETPERSIST, persist_mode) < 0)
2240 msg(
M_ERR,
"Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode, dev);
2242 if (username != NULL)
2248 msg(
M_ERR,
"Cannot get user entry for %s", username);
2252 msg(
M_ERR,
"Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
2255 if (groupname != NULL)
2261 msg(
M_ERR,
"Cannot get group entry for %s", groupname);
2265 msg(
M_ERR,
"Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev);
2269 msg(
M_INFO,
"Persist state set to: %s", (persist_mode ?
"ON" :
"OFF"));
2279#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
2280 if (tun_dco_enabled(tt))
2285 close_tun_generic(tt);
2289#if defined(__GNUC__) || defined(__clang__)
2290#pragma GCC diagnostic push
2291#pragma GCC diagnostic ignored "-Wsign-conversion"
2297 return write(tt->fd, buf, len);
2303 return read(tt->fd, buf, len);
2306#if defined(__GNUC__) || defined(__clang__)
2307#pragma GCC diagnostic pop
2310#elif defined(TARGET_SOLARIS)
2313#error I need the symbol TUNNEWPPA from net/if_tun.h
2317open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2320 int if_fd = -1, ip_muxid = -1, arp_muxid = -1, arp_fd = -1, ppa = -1;
2323 const char *ip_node = NULL, *arp_node = NULL;
2324 const char *dev_tuntap_type;
2326 struct strioctl strioc_if, strioc_ppa;
2336 ip_node =
"/dev/udp";
2339 dev_node =
"/dev/tun";
2341 dev_tuntap_type =
"tun";
2342 link_type = I_PLINK;
2346 ip_node =
"/dev/udp";
2349 dev_node =
"/dev/tap";
2351 arp_node = dev_node;
2352 dev_tuntap_type =
"tap";
2353 link_type = I_PLINK;
2357 msg(
M_FATAL,
"I don't recognize device %s as a tun or tap device", dev);
2360 if ((tt->ip_fd = open(ip_node, O_RDWR, 0)) < 0)
2362 msg(
M_ERR,
"Can't open %s", ip_node);
2365 if ((tt->fd = open(dev_node, O_RDWR, 0)) < 0)
2367 msg(
M_ERR,
"Can't open %s", dev_node);
2375 while (*ptr && !isdigit((
int)*ptr))
2383 strioc_ppa.ic_cmd = TUNNEWPPA;
2384 strioc_ppa.ic_timout = 0;
2385 strioc_ppa.ic_len =
sizeof(ppa);
2386 strioc_ppa.ic_dp = (
char *)&ppa;
2390 bool found_one =
false;
2391 while (!found_one && ppa < 64)
2393 int new_ppa = ioctl(tt->fd, I_STR, &strioc_ppa);
2396 msg(
M_INFO,
"open_tun: got dynamic interface '%s%d'", dev_tuntap_type, new_ppa);
2401 if (errno != EEXIST)
2403 msg(
M_ERR,
"open_tun: unexpected error trying to find free %s interface",
2410 msg(
M_ERR,
"open_tun: could not find free %s interface, give up.", dev_tuntap_type);
2415 if ((ppa = ioctl(tt->fd, I_STR, &strioc_ppa)) < 0)
2417 msg(
M_ERR,
"Can't assign PPA for new interface (%s%d)", dev_tuntap_type, ppa);
2421 if ((if_fd = open(dev_node, O_RDWR, 0)) < 0)
2423 msg(
M_ERR,
"Can't open %s (2)", dev_node);
2426 if (ioctl(if_fd, I_PUSH,
"ip") < 0)
2428 msg(
M_ERR,
"Can't push IP module");
2434 if (ioctl(if_fd, IF_UNITSEL, (
char *)&ppa) < 0)
2436 msg(
M_ERR,
"Can't set PPA %d", ppa);
2443 snprintf(tt->
actual_name, 32,
"%s%d", dev_tuntap_type, ppa);
2447 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
2454 if (ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0)
2456 msg(
M_ERR,
"Can't set PPA %d", ppa);
2458 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
2463 if (ioctl(if_fd, I_PUSH,
"arp") < 0)
2465 msg(
M_ERR,
"Can't push ARP module");
2471 if (ioctl(tt->ip_fd, I_POP, NULL) < 0)
2477 if (ioctl(tt->ip_fd, I_PUSH,
"arp") < 0)
2479 msg(
M_ERR,
"Can't push ARP module");
2483 if ((arp_fd = open(arp_node, O_RDWR, 0)) < 0)
2485 msg(
M_ERR,
"Can't open %s", arp_node);
2488 if (ioctl(arp_fd, I_PUSH,
"arp") < 0)
2490 msg(
M_ERR,
"Can't push ARP module");
2494 strioc_if.ic_cmd = SIOCSLIFNAME;
2495 strioc_if.ic_timout = 0;
2496 strioc_if.ic_len =
sizeof(ifr);
2497 strioc_if.ic_dp = (
char *)𝔦
2498 if (ioctl(arp_fd, I_STR, &strioc_if) < 0)
2500 msg(
M_ERR,
"Can't set ifname to arp");
2504 if ((ip_muxid = ioctl(tt->ip_fd, link_type, if_fd)) < 0)
2506 msg(
M_ERR,
"Can't link %s device to IP", dev_tuntap_type);
2511 if ((arp_muxid = ioctl(tt->ip_fd, link_type, arp_fd)) < 0)
2513 msg(
M_ERR,
"Can't link %s device to ARP", dev_tuntap_type);
2520 ifr.lifr_ip_muxid = ip_muxid;
2523 ifr.lifr_arp_muxid = arp_muxid;
2526 if (ioctl(tt->ip_fd, SIOCSLIFMUXID, &ifr) < 0)
2530 ioctl(tt->ip_fd, I_PUNLINK, arp_muxid);
2532 ioctl(tt->ip_fd, I_PUNLINK, ip_muxid);
2533 msg(
M_ERR,
"Can't set multiplexor id");
2544solaris_close_tun(
struct tuntap *tt)
2562 if (ioctl(tt->ip_fd, SIOCGLIFFLAGS, &ifr) < 0)
2567 if (ioctl(tt->ip_fd, SIOCGLIFMUXID, &ifr) < 0)
2574 if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_arp_muxid) < 0)
2580 if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_ip_muxid) < 0)
2604 solaris_close_tun(tt);
2613solaris_error_close(
struct tuntap *tt,
const struct env_set *es,
const char *actual,
2639 sbuf.buf = (
char *)buf;
2640 return putmsg(tt->fd, NULL, &sbuf, 0) >= 0 ? sbuf.len : -1;
2650 sbuf.buf = (
char *)buf;
2651 return getmsg(tt->fd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
2654#elif defined(TARGET_OPENBSD)
2657open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2660 open_tun_generic(dev, dev_type, dev_node, tt);
2665 struct tuninfo info;
2667 if (ioctl(tt->fd, TUNGIFINFO, &info) < 0)
2673 info.flags |= IFF_MULTICAST;
2676 if (ioctl(tt->fd, TUNSIFINFO, &info) < 0)
2701 close_tun_generic(tt);
2713 close_tun_generic(tt);
2722#elif defined(TARGET_NETBSD)
2739open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2746 if (strcmp(dev,
"tap") == 0)
2749 if ((tt->fd = open(
"/dev/tap", O_RDWR)) < 0)
2751 msg(
M_FATAL,
"Cannot allocate NetBSD TAP dev dynamically");
2753 if (ioctl(tt->fd, TAPGIFNAME, (
void *)&ifr) < 0)
2755 msg(
M_FATAL,
"Cannot query NetBSD TAP device name");
2759 msg(
M_INFO,
"TUN/TAP device %s opened", ifr.ifr_name);
2768 open_tun_generic(dev, dev_type, dev_node, tt);
2773 int i = IFF_POINTOPOINT | IFF_MULTICAST;
2774 ioctl(tt->fd, TUNSIFMODE, &i);
2776 ioctl(tt->fd, TUNSLMODE, &i);
2781 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
2802 close_tun_generic(tt);
2814 close_tun_generic(tt);
2823#elif defined(TARGET_FREEBSD)
2826open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2829 if (tun_dco_enabled(tt))
2831 open_tun_dco_generic(dev, dev_type, tt, ctx);
2835 open_tun_generic(dev, dev_type, dev_node, tt);
2840 int i = IFF_POINTOPOINT | IFF_MULTICAST;
2843 i = IFF_BROADCAST | IFF_MULTICAST;
2846 if (ioctl(tt->fd, TUNSIFMODE, &i) < 0)
2853 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
2875 close_tun_generic(tt);
2888 close_tun_generic(tt);
2897#elif defined(TARGET_DRAGONFLY)
2909open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2912 if (!dev_node && (strcmp(dev,
"tap") == 0 || strcmp(dev,
"tun") == 0))
2915 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
2917 if ((tt->fd = open(tunname, O_RDWR)) < 0)
2919 msg(
M_ERR,
"Cannot allocate DragonFly TUN/TAP %s dynamically", tunname);
2922 char *devname = fdevname(tt->fd);
2923 if (devname == NULL)
2925 msg(
M_ERR,
"Cannot query DragonFly TUN/TAP device name");
2929 msg(
M_INFO,
"TUN/TAP device %s opened", devname);
2935 open_tun_generic(dev, dev_type, dev_node, tt);
2941 int i = IFF_POINTOPOINT | IFF_MULTICAST;
2944 i = IFF_BROADCAST | IFF_MULTICAST;
2947 if (ioctl(tt->fd, TUNSIFMODE, &i) < 0)
2955 ioctl(tt->fd, TUNSLMODE, &i);
2959 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
2971 close_tun_generic(tt);
2975#elif defined(TARGET_DARWIN)
2977#if defined(__GNUC__) || defined(__clang__)
2978#pragma GCC diagnostic push
2979#pragma GCC diagnostic ignored "-Wsign-conversion"
3002utun_open_helper(
struct ctl_info ctlInfo,
int utunnum)
3004 struct sockaddr_ctl sc;
3007 fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
3011 msg(
M_INFO |
M_ERRNO,
"Opening utun%d failed (socket(SYSPROTO_CONTROL))", utunnum);
3015 if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
3018 msg(
M_INFO |
M_ERRNO,
"Opening utun%d failed (ioctl(CTLIOCGINFO))", utunnum);
3023 sc.sc_id = ctlInfo.ctl_id;
3024 sc.sc_len =
sizeof(sc);
3025 sc.sc_family = AF_SYSTEM;
3026 sc.ss_sysaddr = AF_SYS_CONTROL;
3028 sc.sc_unit = utunnum + 1;
3034 if (connect(fd, (
struct sockaddr *)&sc,
sizeof(sc)) < 0)
3036 msg(
M_INFO |
M_ERRNO,
"Opening utun%d failed (connect(AF_SYS_CONTROL))", utunnum);
3047#if defined(__GNUC__) || defined(__clang__)
3048#pragma GCC diagnostic pop
3052open_darwin_utun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt)
3054 struct ctl_info ctlInfo;
3058 socklen_t utunname_len =
sizeof(utunname);
3062 if (dev_node && (strcmp(
"utun", dev_node) != 0))
3064 if (sscanf(dev_node,
"utun%d", &utunnum) != 1)
3067 "Cannot parse 'dev-node %s' please use 'dev-node utunX'"
3068 "to use a utun device number X",
3075 if (
strlcpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME,
sizeof(ctlInfo.ctl_name))
3076 >=
sizeof(ctlInfo.ctl_name))
3078 msg(
M_ERR,
"Opening utun: UTUN_CONTROL_NAME too long");
3084 for (utunnum = 0; utunnum < 255; utunnum++)
3088 ASSERT(snprintf(ifname,
sizeof(ifname),
"utun%d", utunnum) > 0);
3089 if (if_nametoindex(ifname))
3093 fd = utun_open_helper(ctlInfo, utunnum);
3104 fd = utun_open_helper(ctlInfo, utunnum);
3116 if (getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, utunname, &utunname_len))
3118 msg(
M_ERR,
"Error retrieving utun interface name");
3123 msg(
M_INFO,
"Opened utun device %s", utunname);
3128open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3132 if ((!dev_node && tt->
type ==
DEV_TYPE_TUN) || (dev_node && !strncmp(dev_node,
"utun", 4)))
3138 msg(
M_FATAL,
"Cannot use utun devices with --dev-type %s",
3144 open_darwin_utun(dev, dev_type, dev_node, tt);
3151 msg(
M_INFO,
"Failed to open utun device. Falling back to /dev/tun device");
3152 open_tun_generic(dev, dev_type, NULL, tt);
3168 if (dev_node && strcmp(dev_node,
"tun") == 0)
3173 open_tun_generic(dev, dev_type, dev_node, tt);
3189 argv_printf(&
argv,
"%s delete -inet6 %s", ROUTE_PATH, ifconfig_ipv6_local);
3194 close_tun_generic(tt);
3200#if defined(__GNUC__) || defined(__clang__)
3201#pragma GCC diagnostic push
3202#pragma GCC diagnostic ignored "-Wsign-conversion"
3210 return write_tun_header(tt, buf, len);
3214 return write(tt->fd, buf, len);
3223 return read_tun_header(tt, buf, len);
3227 return read(tt->fd, buf, len);
3231#if defined(__GNUC__) || defined(__clang__)
3232#pragma GCC diagnostic pop
3235#elif defined(TARGET_AIX)
3238open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3242 char dynamic_name[20];
3247 msg(
M_FATAL,
"no support for 'tun' devices on AIX");
3250 if (strncmp(dev,
"tap", 3) != 0 || dev_node)
3253 "'--dev %s' and/or '--dev-node' not supported on AIX, use '--dev tap0', 'tap1', etc.",
3257 if (strcmp(dev,
"tap") == 0)
3260 for (i = 0; i < 99; i++)
3262 snprintf(tunname,
sizeof(tunname),
"/dev/tap%d", i);
3263 if (access(tunname, F_OK) < 0 && errno == ENOENT)
3270 msg(
M_FATAL,
"cannot find unused tap device");
3273 snprintf(dynamic_name,
sizeof(dynamic_name),
"tap%d", i);
3286 msg(
M_FATAL,
"TAP device name must be '--dev tapNNNN'");
3289 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
3294 if (access(tunname, F_OK) < 0 && errno == ENOENT)
3313 if ((tt->fd = open(tunname, O_RDWR)) < 0)
3315 msg(
M_ERR,
"Cannot open TAP device '%s'", tunname);
3320 msg(
M_INFO,
"TUN/TAP device %s opened", tunname);
3347 close_tun_generic(tt);
3360 return write(tt->fd, buf, len);
3366 return read(tt->fd, buf, len);
3369#elif defined(_WIN32)
3371#if defined(__GNUC__) || defined(__clang__)
3372#pragma GCC diagnostic push
3373#pragma GCC diagnostic ignored "-Wsign-conversion"
3403 dmsg(
D_WIN32_IO,
"WIN32 I/O: TAP Read immediate return [%d,%lu]", len,
3408 const int err = GetLastError();
3409 if (err == ERROR_IO_PENDING)
3462 const int err = GetLastError();
3463 if (err == ERROR_IO_PENDING)
3495 err = GetLastError();
3513 HDEVINFO dev_info_set;
3519 SetupDiGetClassDevsEx(&
GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL);
3520 if (dev_info_set == INVALID_HANDLE_VALUE)
3522 err = GetLastError();
3523 msg(
M_FATAL,
"Error [%lu] opening device information set key: %s", err,
3528 for (DWORD i = 0;; ++i)
3530 SP_DEVINFO_DATA device_info_data;
3533 const char net_cfg_instance_id_string[] =
"NetCfgInstanceId";
3535 char device_instance_id[256];
3539 ULONG dev_interface_list_size;
3542 ZeroMemory(&device_info_data,
sizeof(SP_DEVINFO_DATA));
3543 device_info_data.cbSize =
sizeof(SP_DEVINFO_DATA);
3544 res = SetupDiEnumDeviceInfo(dev_info_set, i, &device_info_data);
3547 if (GetLastError() == ERROR_NO_MORE_ITEMS)
3557 dev_key = SetupDiOpenDevRegKey(dev_info_set, &device_info_data, DICS_FLAG_GLOBAL, 0,
3558 DIREG_DRV, KEY_QUERY_VALUE);
3559 if (dev_key == INVALID_HANDLE_VALUE)
3566 status = RegQueryValueEx(dev_key, net_cfg_instance_id_string, NULL, &data_type,
3568 if (
status != ERROR_SUCCESS)
3573 len =
sizeof(device_instance_id);
3574 res = SetupDiGetDeviceInstanceId(dev_info_set, &device_info_data, device_instance_id, len,
3581 cr = CM_Get_Device_Interface_List_Size(&dev_interface_list_size,
3583 CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
3585 if (cr != CR_SUCCESS)
3590 char *dev_interface_list =
gc_malloc(dev_interface_list_size,
false,
gc);
3592 dev_interface_list, dev_interface_list_size,
3593 CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
3594 if (cr != CR_SUCCESS)
3599 char *dev_if = dev_interface_list;
3602 while (strlen(dev_if) > 0)
3620 last->
next = dev_iif;
3624 dev_if += strlen(dev_if) + 1;
3628 RegCloseKey(dev_key);
3631 SetupDiDestroyDeviceInfoList(dev_info_set);
3646 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ADAPTER_KEY, 0, KEY_READ, &adapter_key);
3648 if (
status != ERROR_SUCCESS)
3650 msg(
M_FATAL,
"Error opening registry key: %s", ADAPTER_KEY);
3656 char enum_name[256];
3657 char unit_string[256];
3659 const char net_cfg_instance_id_string[] =
"NetCfgInstanceId";
3660 BYTE net_cfg_instance_id[256];
3663 len =
sizeof(enum_name);
3664 status = RegEnumKeyEx(adapter_key, i, enum_name, &len, NULL, NULL, NULL, NULL);
3665 if (
status == ERROR_NO_MORE_ITEMS)
3669 else if (
status != ERROR_SUCCESS)
3671 msg(
M_FATAL,
"Error enumerating registry subkeys of key: %s", ADAPTER_KEY);
3674 if (!
checked_snprintf(unit_string,
sizeof(unit_string),
"%s\\%s", ADAPTER_KEY, enum_name))
3676 msg(
M_WARN,
"Error constructing unit string for %s", enum_name);
3680 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, unit_string, 0, KEY_READ, &unit_key);
3682 if (
status != ERROR_SUCCESS)
3688 const char component_id_string[] =
"ComponentId";
3689 char component_id[256];
3690 len =
sizeof(component_id);
3691 status = RegQueryValueEx(unit_key, component_id_string, NULL, &data_type,
3692 (LPBYTE)component_id, &len);
3694 if (
status != ERROR_SUCCESS || data_type != REG_SZ)
3696 dmsg(
D_REGISTRY,
"Error opening registry key: %s\\%s", unit_string,
3697 component_id_string);
3701 len =
sizeof(net_cfg_instance_id);
3702 status = RegQueryValueEx(unit_key, net_cfg_instance_id_string, NULL, &data_type,
3703 net_cfg_instance_id, &len);
3705 if (
status == ERROR_SUCCESS && data_type == REG_SZ)
3709 if (strcasecmp(component_id, TAP_WIN_COMPONENT_ID) == 0
3710 || strcasecmp(component_id,
"root\\" TAP_WIN_COMPONENT_ID) == 0)
3714 else if (strcasecmp(component_id,
"ovpn-dco") == 0)
3742 RegCloseKey(unit_key);
3747 RegCloseKey(adapter_key);
3755 HKEY network_connections_key;
3761 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ,
3762 &network_connections_key);
3764 if (
status != ERROR_SUCCESS)
3766 msg(
M_FATAL,
"Error opening registry key: %s", NETWORK_CONNECTIONS_KEY);
3771 char enum_name[256];
3772 char connection_string[256];
3773 HKEY connection_key;
3775 const WCHAR name_string[] = L
"Name";
3777 len =
sizeof(enum_name);
3778 status = RegEnumKeyEx(network_connections_key, i, enum_name, &len, NULL, NULL, NULL, NULL);
3779 if (
status == ERROR_NO_MORE_ITEMS)
3783 else if (
status != ERROR_SUCCESS)
3785 msg(
M_FATAL,
"Error enumerating registry subkeys of key: %s", NETWORK_CONNECTIONS_KEY);
3789 "%s\\%s\\Connection",
3790 NETWORK_CONNECTIONS_KEY, enum_name))
3792 msg(
M_WARN,
"Error constructing connection string for %s", enum_name);
3797 status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, connection_string, 0, KEY_READ, &connection_key);
3799 if (
status != ERROR_SUCCESS)
3801 dmsg(
D_REGISTRY,
"Error opening registry key: %s", connection_string);
3805 WCHAR name_data[256];
3806 len =
sizeof(name_data);
3807 status = RegQueryValueExW(connection_key, name_string, NULL, &name_type,
3808 (LPBYTE)name_data, &len);
3810 if (
status != ERROR_SUCCESS || name_type != REG_SZ)
3812 dmsg(
D_REGISTRY,
"Error opening registry key: %s\\%s\\%ls", NETWORK_CONNECTIONS_KEY,
3813 connection_string, name_string);
3822 n = WideCharToMultiByte(CP_UTF8, 0, name_data, -1, NULL, 0, NULL, NULL);
3824 WideCharToMultiByte(CP_UTF8, 0, name_data, -1,
name, n, NULL, NULL);
3839 RegCloseKey(connection_key);
3844 RegCloseKey(network_connections_key);
3856 const unsigned int mask = 3;
3857 const char *err = NULL;
3859 if (local == remote)
3861 err =
"must be different";
3864 if ((local & (~mask)) != (remote & (~mask)))
3867 "must exist within the same 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
3870 if ((local & mask) == 0 || (local & mask) == 3 || (remote & mask) == 0 || (remote & mask) == 3)
3873 "cannot use the first or last address within a given 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
3882 "There is a problem in your selection of --ifconfig endpoints [local=%s, remote=%s]. The local and remote VPN endpoints %s. Try '" PACKAGE
3883 " --show-valid-subnets' option for more info.",
3894 printf(
"On Windows, point-to-point IP support (i.e. --dev tun)\n");
3895 printf(
"is emulated by the TAP-Windows driver. The major limitation\n");
3896 printf(
"imposed by this approach is that the --ifconfig local and\n");
3897 printf(
"remote endpoints must be part of the same 255.255.255.252\n");
3898 printf(
"subnet. The following list shows examples of endpoint\n");
3899 printf(
"pairs which satisfy this requirement. Only the final\n");
3900 printf(
"component of the IP address pairs is at issue.\n\n");
3901 printf(
"As an example, the following option would be correct:\n");
3902 printf(
" --ifconfig 10.7.0.5 10.7.0.6 (on host A)\n");
3903 printf(
" --ifconfig 10.7.0.6 10.7.0.5 (on host B)\n");
3904 printf(
"because [5,6] is part of the below list.\n\n");
3906 for (i = 0; i < 256; i += 4)
3908 printf(
"[%3d,%3d] ", i + 1, i + 2);
3926 bool warn_panel_null =
false;
3927 bool warn_panel_dup =
false;
3928 bool warn_tap_dup =
false;
3937 msg(msglevel,
"Available adapters [name, GUID, driver]:");
3957 warn_panel_dup =
true;
3959 else if (links == 0)
3963 warn_panel_null =
true;
3964 msg(msglevel,
"[NULL] %s", tr->
guid);
3973 if (tr != tr1 && !strcmp(tr->
guid, tr1->
guid))
3975 warn_tap_dup =
true;
3983 msg(warnlevel,
"WARNING: Some TAP-Windows adapters have duplicate GUIDs");
3989 "WARNING: Some TAP-Windows adapters have duplicate links from the Network Connections control panel");
3992 if (warn_panel_null)
3995 "WARNING: Some TAP-Windows adapters have no link from the Network Connections control panel");
4058 msg(
M_FATAL,
"There are no TAP-Windows or ovpn-dco adapters "
4059 "on this system. You should be able to create an adapter "
4060 "by using tapctl.exe utility.");
4070 const struct tap_reg *tap_reg_src,
4078 ASSERT(device_number >= 0);
4089 ASSERT(actual_name_size > 0);
4094 for (i = 0; i < device_number; i++)
4120 if (windows_driver != NULL)
4149 ASSERT(actual_name_size > 0);
4195const IP_ADAPTER_INFO *
4199 IP_ADAPTER_INFO *pi = NULL;
4202 if ((
status = GetAdaptersInfo(NULL, &size)) != ERROR_BUFFER_OVERFLOW)
4209 pi = (PIP_ADAPTER_INFO)
gc_malloc(size,
false,
gc);
4210 if ((
status = GetAdaptersInfo(pi, &size)) != NO_ERROR)
4220const IP_PER_ADAPTER_INFO *
4224 IP_PER_ADAPTER_INFO *pi = NULL;
4230 if ((
status = GetPerAdapterInfo(index, NULL, &size)) != ERROR_BUFFER_OVERFLOW)
4237 pi = (PIP_PER_ADAPTER_INFO)
gc_malloc(size,
false,
gc);
4238 if ((
status = GetPerAdapterInfo(index, pi, &size)) == ERROR_SUCCESS)
4253static const IP_INTERFACE_INFO *
4257 IP_INTERFACE_INFO *ii = NULL;
4260 if ((
status = GetInterfaceInfo(NULL, &size)) != ERROR_INSUFFICIENT_BUFFER)
4267 ii = (PIP_INTERFACE_INFO)
gc_malloc(size,
false,
gc);
4268 if ((
status = GetInterfaceInfo(ii, &size)) == NO_ERROR)
4282static const IP_ADAPTER_INDEX_MAP *
4289 for (i = 0; i < list->NumAdapters; ++i)
4291 const IP_ADAPTER_INDEX_MAP *inter = &list->Adapter[i];
4292 if (index == inter->Index)
4306const IP_ADAPTER_INFO *
4311 const IP_ADAPTER_INFO *a;
4314 for (a = ai; a != NULL; a = a->Next)
4316 if (a->Index == index)
4325const IP_ADAPTER_INFO *
4337 const IP_ADDR_STRING *ip = &ai->IpAddressList;
4361 const IP_ADDR_STRING *iplist = &ai->IpAddressList;
4371 iplist = iplist->Next;
4377 const char *ip_str = iplist->IpAddress.String;
4378 const char *netmask_str = iplist->IpMask.String;
4379 bool succeed1 =
false;
4380 bool succeed2 =
false;
4382 if (ip_str && netmask_str && strlen(ip_str) && strlen(netmask_str))
4384 *ip =
getaddr(getaddr_flags, ip_str, 0, &succeed1, NULL);
4385 *netmask =
getaddr(getaddr_flags, netmask_str, 0, &succeed2, NULL);
4386 ret = (succeed1 ==
true && succeed2 ==
true);
4402 return (
status && ip_adapter == ip && netmask_adapter == netmask);
4410const IP_ADAPTER_INFO *
4435 for (
int i = 0; i < n; ++i)
4471 if (highest_netmask)
4473 *highest_netmask = 0;
4479 for (
int i = 0; i < n; ++i)
4484 if (adapter_ip && adapter_netmask
4485 && (ip & adapter_netmask) == (adapter_ip & adapter_netmask))
4487 if (highest_netmask && adapter_netmask > *highest_netmask)
4489 *highest_netmask = adapter_netmask;
4505 int lowest_metric = INT_MAX;
4520 if (first || hn > highest_netmask)
4522 highest_netmask = hn;
4525 lowest_metric = metric;
4534 else if (hn == highest_netmask)
4540 if (metric >= 0 && metric < lowest_metric)
4543 lowest_metric = metric;
4550 dmsg(
D_ROUTE_DEBUG,
"DEBUG: IP Locate: ip=%s nm=%s index=%lu count=%d metric=%d",
4552 count ? *count : -1, lowest_metric);
4561 *netmask = highest_netmask;
4573#define DHCP_STATUS_UNDEF 0
4574#define DHCP_STATUS_ENABLED 1
4575#define DHCP_STATUS_DISABLED 2
4588 if (ai->DhcpEnabled)
4614 const IP_ADDR_STRING *ip = &a->IpAddressList;
4618 const DWORD
context = ip->Context;
4622 msg(
M_INFO,
"Successfully deleted previously set dynamic IP/netmask: %s/%s",
4623 ip->IpAddress.String, ip->IpMask.String);
4627 const char *empty =
"0.0.0.0";
4628 if (strcmp(ip->IpAddress.String, empty) || strcmp(ip->IpMask.String, empty))
4631 "NOTE: could not delete previously set dynamic IP/netmask: %s/%s (status=%lu)",
4632 ip->IpAddress.String, ip->IpMask.String,
status);
4650 swprintf(wbuf,
SIZE(wbuf), L
"\\DEVICE\\TCPIP_%hs", guid);
4651 if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR)
4657 index = (DWORD)aindex;
4672 if (!strcmp(guid,
list->AdapterName))
4674 index =
list->Index;
4695 msg(
M_INFO,
"NOTE: could not get adapter index for %s", guid);
4709 buf_printf(&out,
"%s", ip->IpAddress.String);
4710 if (strlen(ip->IpMask.String))
4727 msg(msglevel,
"%s", a->Description);
4728 msg(msglevel,
" Index = %lu", a->Index);
4729 msg(msglevel,
" GUID = %s", a->AdapterName);
4731 msg(msglevel,
" MAC = %s",
format_hex_ex(a->Address, a->AddressLength, 0, 1,
":",
gc));
4736 msg(msglevel,
" DHCP LEASE OBTAINED = %s",
time_string(a->LeaseObtained, 0,
false,
gc));
4737 msg(msglevel,
" DHCP LEASE EXPIRES = %s",
time_string(a->LeaseExpires, 0,
false,
gc));
4763 msg(msglevel,
"SYSTEM ADAPTER LIST");
4766 const IP_ADAPTER_INFO *a;
4769 for (a = ai; a != NULL; a = a->Next)
4792 msg(
M_ERR,
"Error: init SA failed");
4795 status = SetKernelObjectSecurity(hand, DACL_SECURITY_INFORMATION, &
sa.sd);
4798 msg(
M_ERRNO,
"Error: SetKernelObjectSecurity failed on %s", device_path);
4802 msg(
M_INFO |
M_NOPREFIX,
"TAP-Windows device: %s [Non-admin access allowed]", device_path);
4812 const char *device_guid = NULL;
4814 uint8_t actual_buffer[256];
4815 char device_path[256];
4827 msg(
M_FATAL,
"TAP-Windows adapter '%s' not found", dev_node);
4831 snprintf(device_path,
sizeof(device_path),
"%s%s%s", USERMODEDEVICEDIR, device_guid,
4834 hand = CreateFile(device_path, MAXIMUM_ALLOWED, 0,
4835 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
4837 if (hand == INVALID_HANDLE_VALUE)
4839 msg(
M_ERR,
"CreateFile failed on TAP device: %s", device_path);
4847 int device_number = 0;
4861 snprintf(device_path,
sizeof(device_path),
"%s%s%s", USERMODEDEVICEDIR, device_guid,
4864 hand = CreateFile(device_path, MAXIMUM_ALLOWED, 0,
4865 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
4867 if (hand == INVALID_HANDLE_VALUE)
4869 msg(
M_WARN,
"CreateFile failed on TAP device: %s", device_path);
4895 DWORD
status = IpReleaseAddress((IP_ADAPTER_INDEX_MAP *)inter);
4904 "NOTE: Release of DHCP-assigned IP address lease on TAP-Windows adapter failed: %s (code=%lu)",
4936 DWORD
status = IpRenewAddress((IP_ADAPTER_INDEX_MAP *)inter);
4945 "WARNING: Failed to renew DHCP IP address lease on TAP-Windows adapter: %s (code=%lu)",
4971 for (i = 0; i < n; ++i)
4985 msg(msglevel,
"%s: command failed", prefix);
4998 const char err[] =
"ERROR: Windows ipconfig command failed";
5023 const char *ip_str = src->IpAddress.String;
5025 bool succeed =
false;
5031 if (!ip_str || !strlen(ip_str))
5036 ip =
getaddr(getaddr_flags, ip_str, 0, &succeed, NULL);
5050 msg(
M_INFO,
"ip_addr_string_to_array [%d]", *dest_len);
5051 for (i = 0; i < *dest_len; ++i)
5074 for (
unsigned int i = 0; i < a1len; ++i)
5091 for (
unsigned int i = 0; i < len; ++i)
5118 for (
unsigned int i = 0; i < addr_len; ++i)
5120 const char *fmt = (i == 0) ?
"%s%s interface ipv6 set dns %lu static %s"
5121 :
"%s%s interface ipv6 add dns %lu %s";
5138 const IP_ADDR_STRING *current, DWORD adapter_index,
const bool test_first)
5142 bool delete_first =
false;
5143 bool is_dns = !strcmp(type,
"dns");
5150 delete_first =
true;
5155 delete_first =
true;
5169 for (
unsigned int i = 0; i < addr_len; ++i)
5173 const char *fmt = first ?
"%s%s interface ip set %s %lu static %s"
5174 :
"%s%s interface ip add %s %lu %s";
5191 msg(
M_INFO,
"NETSH: %lu %s %s [already set]", adapter_index, type,
5209 dest[0].Next = NULL;
5214 dest[0].Next = &dest[1];
5215 dest[1].Next = NULL;
5221 const in_addr_t netmask,
const unsigned int flags)
5225 const IP_ADAPTER_INFO *ai = NULL;
5226 const IP_PER_ADAPTER_INFO *pai = NULL;
5239 msg(
M_INFO,
"NETSH: %lu %s/%s [already set]", adapter_index,
5256 IP_ADDR_STRING wins[2];
5262 if (ai && ai->HaveWins)
5308 msg(
M_NONFATAL,
"TUN: enabling dhcp using service failed: %s [status=%d if_index=%lu]",
5313 msg(
M_INFO,
"DHCP enabled on interface %lu using service",
dhcp.iface.index);
5326 MIB_IPINTERFACE_ROW ipiface;
5327 InitializeIpInterfaceEntry(&ipiface);
5328 const char *family_name = (family == AF_INET6) ?
"IPv6" :
"IPv4";
5329 ipiface.Family = family;
5330 ipiface.InterfaceIndex = iface_index;
5331 if (family == AF_INET6 && mtu < 1280)
5334 "NOTE: IPv6 interface MTU < 1280 conflicts with IETF standards and might not work");
5337 err = GetIpInterfaceEntry(&ipiface);
5338 if (err == NO_ERROR)
5340 if (family == AF_INET)
5342 ipiface.SitePrefixLength = 0;
5344 ipiface.NlMtu = mtu;
5345 err = SetIpInterfaceEntry(&ipiface);
5348 if (err != NO_ERROR)
5351 msg(
M_WARN,
"TUN: Setting %s mtu failed: %s [status=%lu if_index=%d]", family_name,
5357 msg(
M_INFO,
"%s MTU set to %d on interface %d using SetIpInterfaceEntry()", family_name,
5397 else if (strcmp(
BSTR(&actual),
"NULL"))
5399 return BSTR(&actual);
5425 msg(
M_INFO,
"NOTE: now trying netsh (this may take some time)");
5445 const int pre_sleep = 1;
5447 buf_printf(&cmd,
"openvpn --verb %d --tap-sleep %d", verb, pre_sleep);
5480 msg(
M_WARN,
"Register_dns failed using service: %s [status=0x%x]",
5486 msg(
M_INFO,
"Register_dns request sent to the service");
5505 buf_printf(&cmd,
"openvpn --verb %d --register-dns --rdns-internal", verb);
5519 dsa = (local | (~netmask)) + offset;
5523 dsa = (local & netmask) + offset;
5529 "ERROR: There is a clash between the --ifconfig local address and the internal DHCP server address -- both are set to %s -- please use the --ip-win32 dynamic option to choose a different free address from the --ifconfig subnet for the internal DHCP server",
5533 if ((local & netmask) != (dsa & netmask))
5535 msg(
M_FATAL,
"ERROR: --ip-win32 dynamic [offset] : offset is outside of --ifconfig subnet");
5548 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_VERSION, &info,
sizeof(info), &info,
5549 sizeof(info), &len, NULL))
5551 msg(
D_TUNTAP_INFO,
"TAP-Windows Driver Version %lu.%lu %s", info[0], info[1],
5552 (info[2] ?
"(DEBUG)" :
""));
5554 if (!(info[0] == TAP_WIN_MIN_MAJOR && info[1] >= TAP_WIN_MIN_MINOR))
5557 "ERROR: This version of " PACKAGE_NAME
5558 " requires a TAP-Windows driver that is at least version %d.%d -- If you recently upgraded your " PACKAGE_NAME
5559 " distribution, a reboot is probably required at this point to get Windows to see the new driver.",
5560 TAP_WIN_MIN_MAJOR, TAP_WIN_MIN_MINOR);
5569 "WARNING: Tap-Win32 driver version %lu.%lu does not support IPv6 in TUN mode. IPv6 will not work. Upgrade your Tap-Win32 driver.",
5578 "ERROR: Tap-Win32 driver version 9.8 is buggy regarding small IPv4 packets in TUN mode. Upgrade your Tap-Win32 driver.");
5587 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_MTU, &mtu,
sizeof(mtu), &mtu,
sizeof(mtu), &len,
5603 DWORD
status = (DWORD)-1;
5611 .iface = { .index = index, .name =
"" } };
5620 status = FlushIpNetTable(index);
5625 msg(
M_INFO,
"Successful ARP Flush on interface [%lu] %s", index, device_guid);
5627 else if (
status != (DWORD)-1)
5630 "NOTE: FlushIpNetTable failed on interface [%lu] %s (status=%lu) : %s", index,
5645 "WARNING: You have selected '--ip-win32 dynamic', which will not work unless the TAP-Windows TCP/IP properties are set to 'Obtain an IP address automatically'");
5667 const char *error_suffix =
5668 "I am having trouble using the Windows 'IP helper API' to automatically set the IP address -- consider using other --ip-win32 methods (not 'ipapi')";
5673 msg(
M_FATAL,
"ERROR: unable to get adapter index for interface %s -- %s", device_guid,
5681 "NOTE: You have selected (explicitly or by default) '--ip-win32 ipapi', which has a better chance of working correctly if the TAP-Windows TCP/IP properties are set to 'Obtain an IP address automatically'");
5694 "Succeeded in adding a temporary IP/netmask of %s/%s to interface %s using the Win32 IP Helper API",
5701 "ERROR: AddIPAddress %s/%s failed on interface %s, index=%lu, status=%lu (windows error: '%s') -- %s",
5717 sizeof(
status), &len, NULL))
5720 "WARNING: The TAP-Windows driver rejected a TAP_WIN_IOCTL_SET_MEDIA_STATUS DeviceIoControl call.");
5726 msg(
M_INFO,
"Sleeping for %d seconds...", s);
5739 msg(
M_FATAL,
"ERROR: --dev tun also requires --ifconfig");
5750 ep[0] = htonl(tt->
local);
5754 status = DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_TUN, ep,
sizeof(ep), ep,
sizeof(ep),
5760 "Set TAP-Windows TUN subnet mode network/local/netmask = %s/%s/%s [%s]",
5768 status ?
"SUCCEEDED" :
"FAILED");
5774 ep[0] = htonl(tt->
local);
5777 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT, ep,
sizeof(ep), ep,
5778 sizeof(ep), &len, NULL))
5781 "ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set Point-to-Point mode, which is required for --dev tun");
5796 ep[0] = htonl(tt->
local);
5826#ifndef SIMULATE_DHCP_FAILED
5827 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_DHCP_MASQ, ep,
sizeof(ep), ep,
sizeof(ep),
5831 "ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set TAP_WIN_IOCTL_CONFIG_DHCP_MASQ mode");
5835 "Notified TAP-Windows driver to set a DHCP IP/netmask of %s/%s on interface %s [DHCP-serv: %s, lease-time: %d]",
5846 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT,
BPTR(&buf),
5850 "ERROR: The TAP-Windows driver rejected a TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT DeviceIoControl call");
5855 msg(
M_WARN,
"DHCP option string not set due to error");
5868 const char *path = NULL;
5869 char tuntap_device_path[256];
5899 snprintf(tuntap_device_path,
sizeof(tuntap_device_path),
"%s%s%s", USERMODEDEVICEDIR,
5900 device_guid, TAP_WIN_SUFFIX);
5901 path = tuntap_device_path;
5906 tt->
hand = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0,
5907 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
5908 if (tt->
hand == INVALID_HANDLE_VALUE)
5927 uint8_t actual_buffer[256];
5937 *device_guid =
get_device_guid(dev_node, actual_buffer,
sizeof(actual_buffer),
5942 msg(
M_FATAL,
"Adapter '%s' not found", dev_node);
5948 "Adapter '%s' is using %s driver, %s expected.",
5955 msg(
M_FATAL,
"Failed to open %s adapter: %s",
5961 int device_number = 0;
5962 int adapters_created = 0;
5975 if ((++adapters_created > 10)
5978 msg(
M_FATAL,
"All %s adapters on this system are currently in use or disabled.",
6037 *dhcp_masq_post =
true;
6059 bool dhcp_masq =
false;
6060 bool dhcp_masq_post =
false;
6101open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
6108 "Some --dhcp-option or --dns options require DHCP server,"
6109 " which is not supported by the selected %s driver. They will be"
6120 const char *device_guid = NULL;
6148 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_INFO,
BSTR(&out),
6150 (DWORD)
BCAP(&out), &
len, NULL))
6165 while (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_LOG_LINE,
BSTR(&out), (DWORD)
BCAP(&out),
6177 const char *ifconfig_ip_local;
6221#if defined(__GNUC__) || defined(__clang__)
6222#pragma GCC diagnostic pop
6233 if (!CancelIo(tt->
hand))
6235 msg(
M_WARN |
M_ERRNO,
"Warning: CancelIO failed on %s adapter", adaptertype);
6239 dmsg(
D_WIN32_IO_LOW,
"Attempting close of overlapped read event on %s adapter", adaptertype);
6242 dmsg(
D_WIN32_IO_LOW,
"Attempting close of overlapped write event on %s adapter", adaptertype);
6248 if (!CloseHandle(tt->
hand))
6250 msg(
M_WARN |
M_ERRNO,
"Warning: CloseHandle failed on %s adapter", adaptertype);
6327 "Warning: DeleteIPAddress[%lu] failed on TAP-Windows adapter, status=%lu : %s",
6354 {
"manual" }, {
"netsh" }, {
"ipapi" }, {
"dynamic" }, {
"adaptive" }
6378 return "[unknown --ip-win32 type]";
6407open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
6410 open_tun_generic(dev, dev_type, dev_node, tt);
6418 close_tun_generic(tt);
6431 return read(tt->fd, buf,
len);
void argv_msg(const msglvl_t msglevel, const struct argv *a)
Write the arguments stored in a struct argv via the msg() command.
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
void argv_msg_prefix(const msglvl_t msglevel, const struct argv *a, const char *prefix)
Similar to argv_msg() but prefixes the messages being written with a given string.
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
bool checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
static bool has_digit(const char *src)
Return true if the string contains at least one decimal digit.
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
static struct buffer clear_buf(void)
Return an empty, undefined struct buffer (all fields zero).
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
static void buf_set_write(struct buffer *buf, uint8_t *data, size_t size)
Initialise a buffer with an externally provided writable memory region.
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
#define BLEN(buf)
Return the length of the buffer content in bytes.
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Format a binary buffer as a hex string with spaces every 4 bytes.
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
size_t strlcpy(char *dest, const char *src, size_t maxlen)
static int open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev)
static void close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx)
bool build_dhcp_options_string(struct buffer *buf, const struct tuntap_options *o)
void env_set_destroy(struct env_set *es)
void setenv_int(struct env_set *es, const char *name, int value)
void setenv_str(struct env_set *es, const char *name, const char *value)
void env_set_add(struct env_set *es, const char *str)
struct env_set * env_set_create(struct gc_arena *gc)
void set_nonblock(socket_descriptor_t fd)
void set_cloexec(socket_descriptor_t fd)
static SERVICE_STATUS status
void management_sleep(const unsigned int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
#define OPENVPN_STATE_ASSIGN_IP
static void net_ctx_reset(openvpn_net_ctx_t *ctx)
static void net_ctx_free(openvpn_net_ctx_t *ctx)
#define TUN_ADAPTER_INDEX_INVALID
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
const char * print_topology(const int topology)
const char * time_string(time_t t, tv_usec_t usec, bool show_usec, struct gc_arena *gc)
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
#define GETADDR_HOST_ORDER
#define GETADDR_FATAL_ON_SIGNAL
int netmask_to_netbits2(in_addr_t netmask)
bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
bool add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openvpn_net_ctx_t *ctx)
Retrieves the best gateway for a given destination based on the routing table.
void delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt, const struct env_set *es, openvpn_net_ctx_t *ctx)
void route_ipv6_clear_host_bits(struct route_ipv6 *r6)
#define RT_METRIC_DEFINED
#define RGI_NETMASK_DEFINED
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
int offset
Offset in bytes of the actual content within the allocated memory.
Contains all state information for one tunnel.
const char * device_interface
struct device_instance_id_interface * next
LPBYTE net_cfg_instance_id
Packet geometry parameters.
Garbage collection arena used to keep track of dynamically allocated memory.
struct gc_entry * list
First element of the linked list of gc_entry structures.
struct man_connection connection
struct route_gateway_address gateway
enum tun_driver_type windows_driver
struct in6_addr dns6[N_DHCP_ADDR]
in_addr_t wins[N_DHCP_ADDR]
in_addr_t dns[N_DHCP_ADDR]
bool dhcp_masq_custom_offset
const char * domain_search_list[N_SEARCH_LIST_LEN]
unsigned int rwflags_debug
struct rw_handle rw_handle
enum tun_driver_type backend_driver
The backend driver that used for this tun/tap device.
bool did_ifconfig_ipv6_setup
if the internal variables related to ifconfig-ipv6 of this struct have been set up.
struct tuntap_options options
struct in6_addr remote_ipv6
bool did_ifconfig_setup
if the internal variables related to ifconfig of this struct have been set up.
struct overlapped_io writes
in_addr_t adapter_netmask
struct overlapped_io reads
struct in6_addr local_ipv6
bool ipapi_context_defined
void ipconfig_register_dns(const struct env_set *es)
static const struct tap_reg * get_tap_reg(struct gc_arena *gc)
static DWORD get_adapter_index_method_1(const char *guid)
static void tuntap_post_open(struct tuntap *tt, const char *device_guid)
static void netsh_set_dns6_servers(const struct in6_addr *addr_list, const unsigned int addr_len, DWORD adapter_index)
Set the ipv6 dns servers on the specified interface.
static bool do_address_service(const bool add, const short family, const struct tuntap *tt)
static void clear_tuntap(struct tuntap *tuntap)
struct tuntap * init_tun(const char *dev, const char *dev_type, int topology, const char *ifconfig_local_parm, const char *ifconfig_remote_netmask_parm, const char *ifconfig_ipv6_local_parm, int ifconfig_ipv6_netbits_parm, const char *ifconfig_ipv6_remote_parm, const struct addrinfo *local_public, const struct addrinfo *remote_public, const bool strict_warn, struct env_set *es, openvpn_net_ctx_t *ctx, struct tuntap *tt)
static const char * netsh_get_id(const char *dev_node, struct gc_arena *gc)
void open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt, openvpn_net_ctx_t *ctx)
int dev_type_enum(const char *dev, const char *dev_type)
static const struct panel_reg * get_panel_reg(struct gc_arena *gc)
void close_tun_handle(struct tuntap *tt)
static bool test_adapter_ip_netmask(const IP_ADAPTER_INFO *ai, const in_addr_t ip, const in_addr_t netmask)
static void netsh_enable_dhcp(DWORD adapter_index)
static const GUID GUID_DEVINTERFACE_NET
static bool do_create_adapter_service(HANDLE msg_channel, enum tun_driver_type driver_type)
Requests the interactive service to create a VPN adapter of the specified type.
void show_tap_win_adapters(msglvl_t msglevel, msglvl_t warnlevel)
bool dhcp_renew_by_adapter_index(const DWORD adapter_index)
static void tuntap_get_version_info(const struct tuntap *tt)
int tun_write_win32(struct tuntap *tt, const struct buffer *buf)
static bool service_enable_dhcp(const struct tuntap *tt)
static void netsh_delete_address_dns(const struct tuntap *tt, bool ipv6, struct gc_arena *gc)
static void exec_command(const char *prefix, const struct argv *a, int n, msglvl_t msglevel)
int ascii2ipset(const char *name)
const IP_ADAPTER_INFO * get_tun_adapter(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
static void check_addr_clash(const char *name, int type, in_addr_t public, in_addr_t local, in_addr_t remote_netmask)
bool is_adapter_up(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
static void tuntap_set_ptp(const struct tuntap *tt)
static bool tun_try_open_device(struct tuntap *tt, const char *device_guid, const struct device_instance_id_interface *device_instance_id_interface)
static DWORD get_adapter_index(const char *guid)
static void netsh_ifconfig(const struct tuntap_options *to, DWORD adapter_index, const in_addr_t ip, const in_addr_t netmask, const unsigned int flags)
const IP_ADAPTER_INFO * get_adapter_info(DWORD index, struct gc_arena *gc)
static bool dhcp_release(const struct tuntap *tt)
static void fork_dhcp_action(struct tuntap *tt)
static void show_adapter(msglvl_t msglevel, const IP_ADAPTER_INFO *a, struct gc_arena *gc)
static void tuntap_get_mtu(const struct tuntap *tt)
const char * tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
static bool get_adapter_ip_netmask(const IP_ADAPTER_INFO *ai, const int n, in_addr_t *ip, in_addr_t *netmask)
const IP_PER_ADAPTER_INFO * get_per_adapter_info(const DWORD index, struct gc_arena *gc)
static bool ip_addr_one_to_one(const in_addr_t *a1, const unsigned int a1len, const IP_ADDR_STRING *ias)
static void delete_route_connected_v6_net(const struct tuntap *tt)
static void ifconfig_sanity_check(bool tun_p2p, in_addr_t addr)
static void do_ifconfig_ipv4(const struct tuntap *tt, const char *ifname, int tun_mtu, const struct env_set *es, openvpn_net_ctx_t *ctx)
do_ifconfig_ipv4 - perform platform specific ifconfig commands
static void ip_addr_string_to_array(in_addr_t *dest, unsigned int *dest_len, const IP_ADDR_STRING *src)
#define DHCP_STATUS_DISABLED
bool is_ip_in_adapter_subnet(const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask)
void fork_register_dns_action(const struct tuntap *tt)
int tun_write_queue(struct tuntap *tt, const struct buffer *buf)
static void delete_temp_addresses(DWORD index)
static void tuntap_set_ip_addr(struct tuntap *tt, const char *device_guid, bool dhcp_masq_post)
const char * tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc)
static const struct tap_reg * get_adapter_by_name(const char *name, const struct tap_reg *tap_reg, const struct panel_reg *panel_reg)
static void do_ifconfig_ipv6(const struct tuntap *tt, const char *ifname, int tun_mtu, const struct env_set *es, openvpn_net_ctx_t *ctx)
do_ifconfig_ipv6 - perform platform specific ifconfig6 commands
static const IP_ADAPTER_INDEX_MAP * get_interface_info(DWORD index, struct gc_arena *gc)
static const IP_INTERFACE_INFO * get_interface_info_list(struct gc_arena *gc)
bool is_dev_type(const char *dev, const char *dev_type, const char *match_type)
static uint32_t dhcp_masq_addr(const in_addr_t local, const in_addr_t netmask, const int offset)
#define DHCP_STATUS_UNDEF
#define DHCP_STATUS_ENABLED
static void tuntap_set_ip_props(const struct tuntap *tt, bool *dhcp_masq, bool *dhcp_masq_post)
static void register_dns_service(const struct tuntap *tt)
static const char * get_unspecified_device_guid(const int device_number, uint8_t *actual_name, size_t actual_name_size, const struct tap_reg *tap_reg_src, const struct panel_reg *panel_reg_src, enum tun_driver_type *windows_driver, struct gc_arena *gc)
bool dhcp_release_by_adapter_index(const DWORD adapter_index)
static void add_route_connected_v6_net(const struct tuntap *tt, const struct env_set *es)
bool tun_standby(struct tuntap *tt)
static void tuntap_dhcp_mask(const struct tuntap *tt, const char *device_guid)
static void init_ip_addr_string2(IP_ADDR_STRING *dest, const IP_ADDR_STRING *src1, const IP_ADDR_STRING *src2)
void do_ifconfig(struct tuntap *tt, const char *ifname, int tun_mtu, const struct env_set *es, openvpn_net_ctx_t *ctx)
do_ifconfig - configure the tunnel interface
const char * dev_type_string(const char *dev, const char *dev_type)
static const char ifconfig_warn_how_to_silence[]
static const struct device_instance_id_interface * get_device_instance_id_interface(struct gc_arena *gc)
void close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
static const char * guid_to_name(const char *guid, const struct panel_reg *panel_reg)
static DWORD get_adapter_index_method_2(const char *guid)
static int dhcp_status(DWORD index)
const IP_ADAPTER_INFO * get_adapter_info_list(struct gc_arena *gc)
void tap_allow_nonadmin_access(const char *dev_node)
bool tun_name_is_fixed(const char *dev)
static bool ip_addr_member_of(const in_addr_t addr, const IP_ADDR_STRING *ias)
static const char * format_ip_addr_string(const IP_ADDR_STRING *ip, struct gc_arena *gc)
const IP_ADAPTER_INFO * get_adapter(const IP_ADAPTER_INFO *ai, DWORD index)
static void at_least_one_tap_win(const struct tap_reg *tap_reg)
void show_adapters(msglvl_t msglevel)
void warn_on_use_of_common_subnets(openvpn_net_ctx_t *ctx)
void undo_ifconfig(const struct tuntap *tt, openvpn_net_ctx_t *ctx)
undo_ifconfig - undo configuration of the tunnel interface
static bool dhcp_renew(const struct tuntap *tt)
static void undo_ifconfig_ipv6(const struct tuntap *tt, const openvpn_net_ctx_t *ctx)
int tun_read_queue(struct tuntap *tt, int maxsize)
static void do_dns_domain_pwsh(bool add, const struct tuntap *tt)
void init_tun_post(struct tuntap *tt, const struct frame *frame, const struct tuntap_options *options)
static void tuntap_set_connected(const struct tuntap *tt)
static const struct tap_reg * get_adapter_by_guid(const char *guid, const struct tap_reg *tap_reg)
static void do_dns_domain_service(bool add, const struct tuntap *tt)
const char * ifconfig_options_string(const struct tuntap *tt, bool remote, bool disable, struct gc_arena *gc)
static const char * get_device_guid(const char *name, uint8_t *actual_name, size_t actual_name_size, enum tun_driver_type *windows_driver, const struct tap_reg *tap_reg, const struct panel_reg *panel_reg, struct gc_arena *gc)
const char * guess_tuntap_dev(const char *dev, const char *dev_type, const char *dev_node, struct gc_arena *gc)
const char * ipset2ascii(int index)
void do_ifconfig_setenv(const struct tuntap *tt, struct env_set *es)
static bool do_set_mtu_service(const struct tuntap *tt, const short family, const int mtu)
bool is_tun_p2p(const struct tuntap *tt)
static void windows_set_mtu(const int iface_index, const short family, const int mtu)
const char * ipset2ascii_all(struct gc_arena *gc)
static int get_adapter_n_ip_netmask(const IP_ADAPTER_INFO *ai)
void tun_show_debug(const struct tuntap *tt)
void tun_standby_init(struct tuntap *tt)
void show_valid_win32_tun_subnets(void)
static void tap_allow_nonadmin_access_handle(const char *device_path, HANDLE hand)
static const GUID GUID_DEVCLASS_NET
static void undo_ifconfig_ipv4(const struct tuntap *tt, const openvpn_net_ctx_t *ctx)
const char * print_tun_backend_driver(enum tun_driver_type driver)
Return a string representation of the tun backed driver type.
static void do_wins_service(bool add, const struct tuntap *tt)
static void netsh_command(const struct argv *a, int n, msglvl_t msglevel)
DWORD adapter_index_of_ip(const IP_ADAPTER_INFO *list, const in_addr_t ip, int *count, in_addr_t *netmask)
static void do_dns_service(bool add, const short family, const struct tuntap *tt)
static void netsh_ifconfig_options(const char *type, const in_addr_t *addr_list, const unsigned int addr_len, const IP_ADDR_STRING *current, DWORD adapter_index, const bool test_first)
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
void verify_255_255_255_252(in_addr_t local, in_addr_t remote)
#define IPW32_SET_ADAPTIVE
#define DHCP_OPTIONS_DHCP_REQUIRED
ssize_t read_tun(const struct tuntap *tt, uint8_t *buf, int len)
#define N_SEARCH_LIST_LEN
#define IPW32_SET_DHCP_MASQ
#define DCO_WIN_REFERENCE_STRING
#define IPW32_SET_ADAPTIVE_TRY_NETSH
@ WINDOWS_DRIVER_UNSPECIFIED
@ DRIVER_UTUN
macOS internal tun driver
@ DRIVER_AFUNIX
using an AF_UNIX socket to pass packets from/to an external program.
@ WINDOWS_DRIVER_TAP_WINDOWS6
static bool tuntap_is_dco_win(const struct tuntap *tt)
ssize_t write_tun(const struct tuntap *tt, const uint8_t *buf, int len)
int get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, int *is_auto)
Return interface metric value for the specified interface index.
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
void fork_to_self(const char *cmdline)
char * overlapped_io_state_ascii(const struct overlapped_io *o)
void overlapped_io_close(struct overlapped_io *o)
void netcmd_semaphore_release(void)
char * get_win_sys_path(void)
void netcmd_semaphore_lock(void)
bool send_msg_iservice(HANDLE pipe, const void *data, DWORD size, ack_message_t *ack, const char *context)
Send the size bytes in buffer data to the interactive service pipe and read the result in ack.
bool init_security_attributes_allow_all(struct security_attributes *obj)
Initializes security attributes with a NULL DACL, allowing unrestricted access to the resulting objec...
#define IOSTATE_IMMEDIATE_RETURN
#define POWERSHELL_PATH_SUFFIX
#define WIN_IPCONFIG_PATH_SUFFIX
#define NETSH_PATH_SUFFIX
static bool overlapped_io_active(const struct overlapped_io *o)