64 return "tap-windows6";
91 const static GUID
GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
92 const static GUID
GUID_DEVINTERFACE_NET = { 0xcac88484, 0x7515, 0x4c03, { 0x82, 0xe6, 0x71, 0xa8, 0x7a, 0xba, 0xc3, 0x61 } };
96 #define NI_TEST_FIRST (1<<0)
97 #define NI_IP_NETMASK (1<<1)
98 #define NI_OPTIONS (1<<2)
103 const in_addr_t netmask,
104 const unsigned int flags);
112 DWORD adapter_index);
116 static void exec_command(
const char *prefix,
const struct argv *a,
int n,
int msglevel);
144 if (addr.
family == AF_INET)
149 add ?
"add" :
"remove",
157 add ?
"add" :
"remove",
168 msg(
M_WARN,
"TUN: %s address failed using service: %s [status=%u if_index=%d]",
219 size_t dstlen = strlen(
dns.domains);
221 size_t extra = dstlen ? 2 : 1;
222 if (dstlen + srclen + extra >
sizeof(
dns.domains))
224 msg(
M_WARN,
"DNS search domains sent to service truncated to %d", i);
229 dns.domains[dstlen++] =
',';
234 msg(
D_LOW,
"%s DNS domains on '%s' (if_index = %d) using service",
235 (add ?
"Setting" :
"Deleting"),
dns.iface.name,
dns.iface.index);
243 msg(
M_WARN,
"TUN: %s DNS domains failed using service: %s [status=%u if_name=%s]",
249 msg(
M_INFO,
"DNS domains %s using service", (add ?
"set" :
"deleted"));
262 int addr_len = add ? len : 0;
263 const char *ip_proto_name = family == AF_INET6 ?
"IPv6" :
"IPv4";
288 if (addr_len > _countof(dns.
addr))
290 addr_len = _countof(dns.
addr);
292 msg(
M_WARN,
"Number of %s DNS addresses sent to service truncated to %d",
293 ip_proto_name, addr_len);
296 for (
int i = 0; i < addr_len; ++i)
298 if (family == AF_INET6)
308 msg(
D_LOW,
"%s %s dns servers on '%s' (if_index = %d) using service",
318 msg(
M_WARN,
"TUN: %s %s dns failed using service: %s [status=%u if_name=%s]",
324 msg(
M_INFO,
"%s dns servers %s using service", ip_proto_name, (add ?
"set" :
"deleted"));
358 if (addr_len > _countof(wins.
addr))
360 addr_len = _countof(wins.
addr);
362 msg(
M_WARN,
"Number of WINS addresses sent to service truncated to %d",
366 for (
int i = 0; i < addr_len; ++i)
371 msg(
D_LOW,
"%s WINS servers on '%s' (if_index = %d) using service",
381 msg(
M_WARN,
"TUN: %s WINS failed using service: %s [status=%u if_name=%s]",
387 msg(
M_INFO,
"WINS servers %s using service", (add ?
"set" :
"deleted"));
400 const char *family_name = (family == AF_INET6) ?
"IPv6" :
"IPv4";
412 if (family == AF_INET6 && mtu < 1280)
414 msg(
M_INFO,
"NOTE: IPv6 interface MTU < 1280 conflicts with IETF standards and might not work");
424 msg(
M_NONFATAL,
"TUN: setting %s mtu using service failed: %s [status=%u if_index=%d]",
429 msg(
M_INFO,
"%s MTU set to %d on interface %d using service", family_name, mtu, mtu_msg.
iface.
index);
447 argv_printf(&
argv,
"%s%s nicconfig where (InterfaceIndex=%ld) call SetDNSDomain '%s'",
456 #ifdef TARGET_SOLARIS
457 static void solaris_error_close(
struct tuntap *tt,
const struct env_set *
es,
const char *actual,
bool unplumb_inet6);
462 #if defined(TARGET_DARWIN)
463 #include <sys/kern_control.h>
464 #include <net/if_utun.h>
465 #include <sys/sys_domain.h>
471 is_dev_type(
const char *dev,
const char *dev_type,
const char *match_type)
480 return !strcmp(dev_type, match_type);
484 return !strncmp(dev, match_type, strlen(match_type));
519 return "[unknown-dev-type]";
529 const char *dev_type,
530 const char *dev_node,
560 const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
563 if (looks_like_netmask)
565 msg(
M_WARN,
"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",
572 if (!looks_like_netmask)
574 msg(
M_WARN,
"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",
590 in_addr_t remote_netmask)
594 msg(
M_INFO,
"CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
605 const in_addr_t test_netmask = 0xFFFFFF00;
606 const in_addr_t public_net =
public &test_netmask;
607 const in_addr_t local_net = local & test_netmask;
608 const in_addr_t remote_net = remote_netmask & test_netmask;
610 if (
public == local ||
public == remote_netmask)
613 "WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
621 if (public_net == local_net || public_net == remote_net)
624 "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",
634 const in_addr_t public_network =
public &remote_netmask;
635 const in_addr_t virtual_network = local & remote_netmask;
636 if (public_network == virtual_network)
639 "WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
659 if ((rgi.
flags & needed) == needed)
662 if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
664 msg(
M_WARN,
"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.");
750 bool tun_p2p =
false;
763 msg(
M_FATAL,
"Error: problem with tun vs. tap setting");
788 setenv_str(
es,
"ifconfig_remote", ifconfig_remote_netmask);
792 setenv_str(
es,
"ifconfig_netmask", ifconfig_remote_netmask);
801 setenv_str(
es,
"ifconfig_ipv6_local", ifconfig_ipv6_local);
803 setenv_str(
es,
"ifconfig_ipv6_remote", ifconfig_ipv6_remote);
817 const char *dev_type,
819 const char *ifconfig_local_parm,
820 const char *ifconfig_remote_netmask_parm,
821 const char *ifconfig_ipv6_local_parm,
822 int ifconfig_ipv6_netbits_parm,
823 const char *ifconfig_ipv6_remote_parm,
824 struct addrinfo *local_public,
825 struct addrinfo *remote_public,
826 const bool strict_warn,
840 if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
866 ifconfig_remote_netmask_parm,
876 struct addrinfo *curele;
884 for (curele = local_public; curele; curele = curele->ai_next)
886 if (curele->ai_family == AF_INET)
888 const in_addr_t local = ntohl(((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr);
897 for (curele = remote_public; curele; curele = curele->ai_next)
899 if (curele->ai_family == AF_INET)
901 const in_addr_t remote = ntohl(((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr);
930 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", ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
993 msg(
M_FATAL,
"Cannot allocate memory for ring buffer");
1001 msg(
M_FATAL,
"Cannot create events for ring buffer");
1050 #if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
1051 || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
1062 create_arbitrary_remote(
struct tuntap *tt )
1068 if (remote == tt->
local)
1090 #if !defined(TARGET_LINUX)
1096 #if defined(TARGET_LINUX)
1097 if (net_iface_mtu_set(ctx, ifname, tun_mtu) < 0)
1099 msg(
M_FATAL,
"Linux can't set mtu (%d) on %s", tun_mtu, ifname);
1102 if (net_iface_up(ctx, ifname,
true) < 0)
1104 msg(
M_FATAL,
"Linux can't bring %s up", ifname);
1107 if (net_addr_v6_add(ctx, ifname, &tt->
local_ipv6,
1110 msg(
M_FATAL,
"Linux can't add IPv6 to interface %s", ifname);
1112 #elif defined(TARGET_ANDROID)
1115 snprintf(out6,
sizeof(out6),
"%s/%d %d",
1117 management_android_control(
management,
"IFCONFIG6", out6);
1118 #elif defined(TARGET_SOLARIS)
1138 "Solaris ifconfig IPv6 (prepare) failed"))
1140 solaris_error_close(tt,
es, ifname,
true);
1159 solaris_error_close(tt,
es, ifname,
true);
1169 #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
1170 || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
1171 || defined(TARGET_DRAGONFLY)
1177 "generic BSD ifconfig inet6 failed");
1179 #if defined(TARGET_FREEBSD) && __FreeBSD_version >= 1200000 \
1180 && __FreeBSD_version < 1300000
1200 "FreeBSD BSD 'ifconfig inet6 -ifdisabled' failed");
1203 #elif defined(TARGET_AIX)
1213 "generic BSD ifconfig inet6 failed");
1216 #elif defined (_WIN32)
1219 msg(
M_INFO,
"******** NOTE: Please manually set the v6 IP of '%s' to %s (if it is not already set)",
1220 ifname, ifconfig_ipv6_local);
1249 argv_printf(&
argv,
"%s%s interface ipv6 set address %lu %s/%d store=active",
1251 ifconfig_ipv6_local, netbits);
1267 msg(
M_FATAL,
"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.");
1270 #if !defined(TARGET_LINUX)
1289 #if !defined(_WIN32) && !defined(TARGET_ANDROID)
1296 #if !defined(TARGET_LINUX)
1297 const char *ifconfig_local = NULL;
1298 const char *ifconfig_remote_netmask = NULL;
1309 #if defined(TARGET_LINUX)
1310 if (net_iface_mtu_set(ctx, ifname, tun_mtu) < 0)
1312 msg(
M_FATAL,
"Linux can't set mtu (%d) on %s", tun_mtu, ifname);
1315 if (net_iface_up(ctx, ifname,
true) < 0)
1317 msg(
M_FATAL,
"Linux can't bring %s up", ifname);
1322 if (net_addr_ptp_v4_add(ctx, ifname, &tt->
local,
1325 msg(
M_FATAL,
"Linux can't add IP to interface %s", ifname);
1330 if (net_addr_v4_add(ctx, ifname, &tt->
local,
1333 msg(
M_FATAL,
"Linux can't add IP to interface %s", ifname);
1336 #elif defined(TARGET_ANDROID)
1339 snprintf(out,
sizeof(out),
"%s %s %d %s", ifconfig_local,
1341 management_android_control(
management,
"IFCONFIG", out);
1343 #elif defined(TARGET_SOLARIS)
1352 ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1357 solaris_error_close(tt,
es, ifname,
false);
1366 ifname, ifconfig_local, ifconfig_local,
1367 ifconfig_remote_netmask, tun_mtu);
1373 ifconfig_remote_netmask);
1379 solaris_error_close(tt,
es, ifname,
false);
1395 #elif defined(TARGET_OPENBSD)
1397 in_addr_t remote_end;
1409 "%s %s %s %s mtu %d netmask 255.255.255.255 up -link0",
1411 ifconfig_remote_netmask, tun_mtu);
1415 remote_end = create_arbitrary_remote( tt );
1419 ifconfig_remote_netmask);
1425 ifconfig_remote_netmask, tun_mtu);
1442 #elif defined(TARGET_NETBSD)
1443 in_addr_t remote_end = INADDR_ANY;
1449 ifconfig_remote_netmask, tun_mtu);
1453 remote_end = create_arbitrary_remote(tt);
1456 tun_mtu, ifconfig_remote_netmask);
1467 ifconfig_remote_netmask, tun_mtu);
1484 #elif defined(TARGET_DARWIN)
1493 "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
1501 ifconfig_remote_netmask, tun_mtu);
1507 ifconfig_remote_netmask, tun_mtu);
1512 ifname, ifconfig_local, ifconfig_remote_netmask,
1531 #elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1538 ifconfig_remote_netmask, tun_mtu);
1544 ifname, ifconfig_local, netbits, tun_mtu );
1550 #elif defined(TARGET_AIX)
1558 msg(
M_FATAL,
"no tun support on AIX (canthappen)");
1563 ifname, ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1570 #elif defined (_WIN32)
1574 "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
1575 ifname, ifconfig_local,
1576 ifconfig_remote_netmask);
1609 #elif defined(TARGET_HAIKU)
1612 ifname, ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1617 msg(
M_FATAL,
"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.");
1620 #if !defined(TARGET_LINUX)
1634 #ifdef ENABLE_MANAGEMENT
1664 #if defined(TARGET_LINUX)
1672 msg(
M_WARN,
"Linux can't del IP from iface %s",
1680 msg(
M_WARN,
"Linux can't del IP from iface %s",
1684 #elif defined(TARGET_FREEBSD)
1703 #if defined(TARGET_LINUX)
1709 #elif defined(TARGET_FREEBSD)
1755 #ifdef TARGET_SOLARIS
1760 #if defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
1780 #include <netinet/ip.h>
1781 #include <sys/uio.h>
1784 header_modify_read_write_return(
int len)
1788 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
1797 write_tun_header(
struct tuntap *tt, uint8_t *buf,
int len)
1809 type = htonl(AF_INET6);
1813 type = htonl(AF_INET);
1816 iv[0].iov_base = &type;
1817 iv[0].iov_len =
sizeof(type);
1818 iv[1].iov_base = buf;
1819 iv[1].iov_len = len;
1821 return header_modify_read_write_return(writev(tt->fd, iv, 2));
1825 return write(tt->fd, buf, len);
1830 read_tun_header(
struct tuntap *tt, uint8_t *buf,
int len)
1837 iv[0].iov_base = &type;
1838 iv[0].iov_len =
sizeof(type);
1839 iv[1].iov_base = buf;
1840 iv[1].iov_len = len;
1842 return header_modify_read_write_return(readv(tt->fd, iv, 2));
1846 return read(tt->fd, buf, len);
1857 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
1859 tun_dco_enabled(
struct tuntap *tt)
1866 #if !(defined(_WIN32) || defined(TARGET_LINUX) || defined(TARGET_SOLARIS))
1868 open_tun_generic(
const char *dev,
const char *dev_type,
const char *dev_node,
1872 char dynamic_name[256];
1873 bool dynamic_opened =
false;
1880 snprintf(tunname,
sizeof(tunname),
"%s", dev_node);
1892 for (
int i = 0; i < 256; ++i)
1896 #if defined(TARGET_HAIKU)
1899 snprintf(tunname,
sizeof(tunname),
1900 "/dev/%s%s%d", dev, sep, i);
1901 snprintf(dynamic_name,
sizeof(dynamic_name),
1902 "%s%s%d", dev, sep, i);
1903 if ((tt->fd = open(tunname, O_RDWR)) > 0)
1905 dynamic_opened =
true;
1910 if (!dynamic_opened)
1912 msg(
M_FATAL,
"Cannot allocate TUN/TAP dev dynamically");
1920 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
1924 if (!dynamic_opened)
1927 if (if_nametoindex( dev ) > 0)
1929 msg(
M_INFO,
"TUN/TAP device %s exists previously, keep at program end", dev );
1933 if ((tt->fd = open(tunname, O_RDWR)) < 0)
1935 msg(
M_ERR,
"Cannot open TUN/TAP dev %s", tunname);
1941 msg(
M_INFO,
"TUN/TAP device %s opened", tunname);
1949 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
1951 open_tun_dco_generic(
const char *dev,
const char *dev_type,
1954 char dynamic_name[256];
1955 bool dynamic_opened =
false;
1965 if (strcmp(dev,
"tun") == 0)
1967 for (
int i = 0; i < 256; ++i)
1969 snprintf(dynamic_name,
sizeof(dynamic_name),
1974 dynamic_opened =
true;
1975 msg(
M_INFO,
"DCO device %s opened", dynamic_name);
1979 else if (ret == -EPERM)
1984 if (!dynamic_opened)
1986 msg(
M_FATAL,
"Cannot allocate DCO dev dynamically");
2000 msg(
M_INFO,
"DCO device %s already exists, won't be destroyed at shutdown",
2006 msg(
M_ERR,
"Cannot open DCO device %s: %s (%d)", dev,
2007 strerror(-ret), ret);
2011 msg(
M_INFO,
"DCO device %s opened", dev);
2020 #if !(defined(_WIN32) || defined(TARGET_SOLARIS))
2022 close_tun_generic(
struct tuntap *tt)
2034 #if defined (TARGET_ANDROID)
2036 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2039 #define ANDROID_TUNNAME "vpnservice-tun"
2044 int oldtunfd = tt->fd;
2050 management_android_control(
management,
"DNS6SERVER",
2056 management_android_control(
management,
"DNSSERVER",
2072 int android_method = managment_android_persisttun_action(
management);
2074 if (oldtunfd >=0 && android_method == ANDROID_KEEP_OLD_TUN)
2081 opentun = management_android_control(
management,
"OPENTUN", dev);
2088 if (oldtunfd >= 0 && android_method == ANDROID_OPEN_BEFORE_CLOSE)
2096 if ((tt->fd < 0) || !opentun)
2098 msg(
M_ERR,
"ERROR: Cannot open TUN");
2109 close_tun_generic(tt);
2122 return read(tt->fd, buf,
len);
2125 #elif defined(TARGET_LINUX)
2127 #ifndef HAVE_LINUX_SOCKIOS_H
2128 #error header file linux/sockios.h required
2134 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2139 if (tun_dco_enabled(tt))
2141 open_tun_dco_generic(dev, dev_type, tt, ctx);
2148 const char *node = dev_node;
2151 node =
"/dev/net/tun";
2157 if ((tt->fd = open(node, O_RDWR)) < 0)
2159 msg(
M_ERR,
"ERROR: Cannot open TUN/TAP dev %s", node);
2166 ifr.ifr_flags = IFF_NO_PI;
2168 #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
2169 ifr.ifr_flags |= IFF_ONE_QUEUE;
2177 ifr.ifr_flags |= IFF_TUN;
2181 ifr.ifr_flags |= IFF_TAP;
2185 msg(
M_FATAL,
"I don't recognize device %s as a tun or tap device",
2192 if (strcmp(dev,
"tun") && strcmp(dev,
"tap"))
2201 if (ioctl(tt->fd, TUNSETIFF, (
void *) &ifr) < 0)
2203 msg(
M_ERR,
"ERROR: Cannot ioctl TUNSETIFF %s", dev);
2206 msg(
M_INFO,
"TUN/TAP device %s opened", ifr.ifr_name);
2211 #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
2214 struct ifreq netifr;
2217 if ((ctl_fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
2220 strncpynt(netifr.ifr_name, ifr.ifr_name, IFNAMSIZ);
2221 netifr.ifr_qlen = tt->
options.txqueuelen;
2222 if (ioctl(ctl_fd, SIOCSIFTXQLEN, (
void *) &netifr) >= 0)
2228 msg(
M_WARN |
M_ERRNO,
"Note: Cannot set tx queue length on %s", ifr.ifr_name);
2234 msg(
M_WARN |
M_ERRNO,
"Note: Cannot open control socket on %s", ifr.ifr_name);
2249 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2257 #ifdef ENABLE_FEATURE_TUN_PERSIST
2261 #define TUNSETGROUP _IOW('T', 206, int)
2265 tuncfg(
const char *dev,
const char *dev_type,
const char *dev_node,
2266 int persist_mode,
const char *username,
const char *groupname,
2276 open_tun(dev, dev_type, dev_node, tt, ctx);
2277 if (ioctl(tt->fd, TUNSETPERSIST, persist_mode) < 0)
2279 msg(
M_ERR,
"Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode, dev);
2281 if (username != NULL)
2287 msg(
M_ERR,
"Cannot get user entry for %s", username);
2291 msg(
M_ERR,
"Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
2294 if (groupname != NULL)
2300 msg(
M_ERR,
"Cannot get group entry for %s", groupname);
2304 msg(
M_ERR,
"Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev);
2308 msg(
M_INFO,
"Persist state set to: %s", (persist_mode ?
"ON" :
"OFF"));
2318 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
2319 if (tun_dco_enabled(tt))
2324 close_tun_generic(tt);
2331 return write(tt->fd, buf, len);
2337 return read(tt->fd, buf, len);
2340 #elif defined(TARGET_SOLARIS)
2343 #error I need the symbol TUNNEWPPA from net/if_tun.h
2347 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2350 int if_fd = -1, ip_muxid = -1, arp_muxid = -1, arp_fd = -1, ppa = -1;
2353 const char *ip_node = NULL, *arp_node = NULL;
2354 const char *dev_tuntap_type;
2356 struct strioctl strioc_if, strioc_ppa;
2366 ip_node =
"/dev/udp";
2369 dev_node =
"/dev/tun";
2371 dev_tuntap_type =
"tun";
2372 link_type = I_PLINK;
2376 ip_node =
"/dev/udp";
2379 dev_node =
"/dev/tap";
2381 arp_node = dev_node;
2382 dev_tuntap_type =
"tap";
2383 link_type = I_PLINK;
2387 msg(
M_FATAL,
"I don't recognize device %s as a tun or tap device",
2391 if ((tt->ip_fd = open(ip_node, O_RDWR, 0)) < 0)
2393 msg(
M_ERR,
"Can't open %s", ip_node);
2396 if ((tt->fd = open(dev_node, O_RDWR, 0)) < 0)
2398 msg(
M_ERR,
"Can't open %s", dev_node);
2406 while (*ptr && !isdigit((
int) *ptr))
2414 strioc_ppa.ic_cmd = TUNNEWPPA;
2415 strioc_ppa.ic_timout = 0;
2416 strioc_ppa.ic_len =
sizeof(ppa);
2417 strioc_ppa.ic_dp = (
char *)&ppa;
2421 bool found_one =
false;
2422 while (!found_one && ppa < 64)
2424 int new_ppa = ioctl(tt->fd, I_STR, &strioc_ppa);
2427 msg(
M_INFO,
"open_tun: got dynamic interface '%s%d'", dev_tuntap_type, new_ppa );
2432 if (errno != EEXIST)
2434 msg(
M_ERR,
"open_tun: unexpected error trying to find free %s interface", dev_tuntap_type );
2440 msg(
M_ERR,
"open_tun: could not find free %s interface, give up.", dev_tuntap_type );
2445 if ((ppa = ioctl(tt->fd, I_STR, &strioc_ppa)) < 0)
2447 msg(
M_ERR,
"Can't assign PPA for new interface (%s%d)", dev_tuntap_type, ppa );
2451 if ((if_fd = open(dev_node, O_RDWR, 0)) < 0)
2453 msg(
M_ERR,
"Can't open %s (2)", dev_node);
2456 if (ioctl(if_fd, I_PUSH,
"ip") < 0)
2458 msg(
M_ERR,
"Can't push IP module");
2464 if (ioctl(if_fd, IF_UNITSEL, (
char *) &ppa) < 0)
2466 msg(
M_ERR,
"Can't set PPA %d", ppa);
2473 snprintf(tt->
actual_name, 32,
"%s%d", dev_tuntap_type, ppa);
2477 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
2484 if (ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0)
2486 msg(
M_ERR,
"Can't set PPA %d", ppa);
2488 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
2493 if (ioctl(if_fd, I_PUSH,
"arp") < 0)
2495 msg(
M_ERR,
"Can't push ARP module");
2501 if (ioctl(tt->ip_fd, I_POP, NULL) < 0)
2507 if (ioctl(tt->ip_fd, I_PUSH,
"arp") < 0)
2509 msg(
M_ERR,
"Can't push ARP module\n");
2513 if ((arp_fd = open(arp_node, O_RDWR, 0)) < 0)
2515 msg(
M_ERR,
"Can't open %s\n", arp_node);
2518 if (ioctl(arp_fd, I_PUSH,
"arp") < 0)
2520 msg(
M_ERR,
"Can't push ARP module\n");
2524 strioc_if.ic_cmd = SIOCSLIFNAME;
2525 strioc_if.ic_timout = 0;
2526 strioc_if.ic_len =
sizeof(ifr);
2527 strioc_if.ic_dp = (
char *)𝔦
2528 if (ioctl(arp_fd, I_STR, &strioc_if) < 0)
2530 msg(
M_ERR,
"Can't set ifname to arp\n");
2534 if ((ip_muxid = ioctl(tt->ip_fd, link_type, if_fd)) < 0)
2536 msg(
M_ERR,
"Can't link %s device to IP", dev_tuntap_type);
2541 if ((arp_muxid = ioctl(tt->ip_fd, link_type, arp_fd)) < 0)
2543 msg(
M_ERR,
"Can't link %s device to ARP", dev_tuntap_type);
2550 ifr.lifr_ip_muxid = ip_muxid;
2553 ifr.lifr_arp_muxid = arp_muxid;
2556 if (ioctl(tt->ip_fd, SIOCSLIFMUXID, &ifr) < 0)
2560 ioctl(tt->ip_fd, I_PUNLINK, arp_muxid);
2562 ioctl(tt->ip_fd, I_PUNLINK, ip_muxid);
2563 msg(
M_ERR,
"Can't set multiplexor id");
2574 solaris_close_tun(
struct tuntap *tt)
2593 if (ioctl(tt->ip_fd, SIOCGLIFFLAGS, &ifr) < 0)
2598 if (ioctl(tt->ip_fd, SIOCGLIFMUXID, &ifr) < 0)
2605 if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_arp_muxid) < 0)
2611 if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_ip_muxid) < 0)
2635 solaris_close_tun(tt);
2645 const char *actual,
bool unplumb_inet6 )
2674 sbuf.buf = (
char *)buf;
2675 return putmsg(tt->fd, NULL, &sbuf, 0) >= 0 ? sbuf.len : -1;
2685 sbuf.buf = (
char *)buf;
2686 return getmsg(tt->fd, NULL, &sbuf, &
f) >= 0 ? sbuf.len : -1;
2689 #elif defined(TARGET_OPENBSD)
2692 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2695 open_tun_generic(dev, dev_type, dev_node, tt);
2700 struct tuninfo info;
2702 if (ioctl(tt->fd, TUNGIFINFO, &info) < 0)
2707 #ifdef IFF_MULTICAST
2708 info.flags |= IFF_MULTICAST;
2711 if (ioctl(tt->fd, TUNSIFINFO, &info) < 0)
2736 close_tun_generic(tt);
2749 close_tun_generic(tt);
2761 return write_tun_header(tt, buf, len);
2767 return read_tun_header(tt, buf, len);
2770 #elif defined(TARGET_NETBSD)
2787 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2794 if (strcmp(dev,
"tap") == 0)
2797 if ((tt->fd = open(
"/dev/tap", O_RDWR)) < 0)
2799 msg(
M_FATAL,
"Cannot allocate NetBSD TAP dev dynamically");
2801 if (ioctl( tt->fd, TAPGIFNAME, (
void *)&ifr ) < 0)
2803 msg(
M_FATAL,
"Cannot query NetBSD TAP device name");
2807 msg(
M_INFO,
"TUN/TAP device %s opened", ifr.ifr_name);
2816 open_tun_generic(dev, dev_type, dev_node, tt);
2821 int i = IFF_POINTOPOINT|IFF_MULTICAST;
2822 ioctl(tt->fd, TUNSIFMODE, &i);
2824 ioctl(tt->fd, TUNSLMODE, &i);
2829 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
2850 close_tun_generic(tt);
2863 close_tun_generic(tt);
2873 netbsd_modify_read_write_return(
int len)
2877 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
2898 type = htonl(AF_INET6);
2902 type = htonl(AF_INET);
2905 iv[0].iov_base = (
char *)&type;
2906 iv[0].iov_len =
sizeof(type);
2907 iv[1].iov_base = buf;
2908 iv[1].iov_len = len;
2910 return netbsd_modify_read_write_return(writev(tt->fd, iv, 2));
2914 return write(tt->fd, buf, len);
2926 iv[0].iov_base = (
char *)&type;
2927 iv[0].iov_len =
sizeof(type);
2928 iv[1].iov_base = buf;
2929 iv[1].iov_len = len;
2931 return netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
2935 return read(tt->fd, buf, len);
2939 #elif defined(TARGET_FREEBSD)
2942 freebsd_modify_read_write_return(
int len)
2946 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
2955 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2958 if (tun_dco_enabled(tt))
2960 open_tun_dco_generic(dev, dev_type, tt, ctx);
2964 open_tun_generic(dev, dev_type, dev_node, tt);
2969 int i = IFF_POINTOPOINT | IFF_MULTICAST;
2972 i = IFF_BROADCAST | IFF_MULTICAST;
2975 if (ioctl(tt->fd, TUNSIFMODE, &i) < 0)
2982 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
3004 close_tun_generic(tt);
3018 close_tun_generic(tt);
3022 "FreeBSD 'destroy tun interface' failed (non-critical)");
3037 iph = (
struct ip *) buf;
3041 type = htonl(AF_INET6);
3045 type = htonl(AF_INET);
3048 iv[0].iov_base = (
char *)&type;
3049 iv[0].iov_len =
sizeof(type);
3050 iv[1].iov_base = buf;
3051 iv[1].iov_len = len;
3053 return freebsd_modify_read_write_return(writev(tt->fd, iv, 2));
3057 return write(tt->fd, buf, len);
3069 iv[0].iov_base = (
char *)&type;
3070 iv[0].iov_len =
sizeof(type);
3071 iv[1].iov_base = buf;
3072 iv[1].iov_len = len;
3074 return freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
3078 return read(tt->fd, buf, len);
3082 #elif defined(TARGET_DRAGONFLY)
3085 dragonfly_modify_read_write_return(
int len)
3089 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
3098 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3101 open_tun_generic(dev, dev_type, dev_node, tt);
3108 ioctl(tt->fd, TUNSLMODE, &i);
3110 ioctl(tt->fd, TUNSIFHEAD, &i);
3119 close_tun_generic(tt);
3132 iph = (
struct ip *) buf;
3136 type = htonl(AF_INET6);
3140 type = htonl(AF_INET);
3143 iv[0].iov_base = (
char *)&type;
3144 iv[0].iov_len =
sizeof(type);
3145 iv[1].iov_base = buf;
3146 iv[1].iov_len = len;
3148 return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2));
3152 return write(tt->fd, buf, len);
3164 iv[0].iov_base = (
char *)&type;
3165 iv[0].iov_len =
sizeof(type);
3166 iv[1].iov_base = buf;
3167 iv[1].iov_len = len;
3169 return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2));
3173 return read(tt->fd, buf, len);
3177 #elif defined(TARGET_DARWIN)
3199 utun_open_helper(
struct ctl_info ctlInfo,
int utunnum)
3201 struct sockaddr_ctl sc;
3204 fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
3213 if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
3222 sc.sc_id = ctlInfo.ctl_id;
3223 sc.sc_len =
sizeof(sc);
3224 sc.sc_family = AF_SYSTEM;
3225 sc.ss_sysaddr = AF_SYS_CONTROL;
3227 sc.sc_unit = utunnum+1;
3233 if (connect(fd, (
struct sockaddr *)&sc,
sizeof(sc)) < 0)
3248 open_darwin_utun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt)
3250 struct ctl_info ctlInfo;
3254 socklen_t utunname_len =
sizeof(utunname);
3258 if (dev_node && (strcmp(
"utun", dev_node) != 0 ))
3260 if (sscanf(dev_node,
"utun%d", &utunnum) != 1)
3262 msg(
M_FATAL,
"Cannot parse 'dev-node %s' please use 'dev-node utunX'"
3263 "to use a utun device number X", dev_node);
3270 if (strlcpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME,
sizeof(ctlInfo.ctl_name)) >=
3271 sizeof(ctlInfo.ctl_name))
3273 msg(
M_ERR,
"Opening utun: UTUN_CONTROL_NAME too long");
3279 for (utunnum = 0; utunnum < 255; utunnum++)
3283 ASSERT(snprintf(ifname,
sizeof(ifname),
"utun%d", utunnum) > 0);
3284 if (if_nametoindex(ifname))
3288 fd = utun_open_helper(ctlInfo, utunnum);
3299 fd = utun_open_helper(ctlInfo, utunnum);
3311 if (getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, utunname, &utunname_len))
3318 msg(
M_INFO,
"Opened utun device %s", utunname);
3323 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3328 || (dev_node && !strncmp(dev_node,
"utun", 4)))
3335 msg(
M_FATAL,
"Cannot use utun devices with --dev-type %s",
3341 open_darwin_utun(dev, dev_type, dev_node, tt);
3348 msg(
M_INFO,
"Failed to open utun device. Falling back to /dev/tun device");
3349 open_tun_generic(dev, dev_type, NULL, tt);
3366 if (dev_node && strcmp(dev_node,
"tun")==0)
3371 open_tun_generic(dev, dev_type, dev_node, tt);
3385 const char *ifconfig_ipv6_local =
3394 close_tun_generic(tt);
3405 return write_tun_header(tt, buf, len);
3409 return write(tt->fd, buf, len);
3418 return read_tun_header(tt, buf, len);
3422 return read(tt->fd, buf, len);
3426 #elif defined(TARGET_AIX)
3429 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3433 char dynamic_name[20];
3438 msg(
M_FATAL,
"no support for 'tun' devices on AIX" );
3441 if (strncmp( dev,
"tap", 3 ) != 0 || dev_node)
3443 msg(
M_FATAL,
"'--dev %s' and/or '--dev-node' not supported on AIX, use '--dev tap0', 'tap1', etc.", dev );
3446 if (strcmp( dev,
"tap" ) == 0)
3449 for (i = 0; i<99; i++)
3451 snprintf(tunname,
sizeof(tunname),
"/dev/tap%d", i);
3452 if (access( tunname, F_OK ) < 0 && errno == ENOENT)
3459 msg(
M_FATAL,
"cannot find unused tap device" );
3462 snprintf( dynamic_name,
sizeof(dynamic_name),
"tap%d", i );
3469 while (isdigit(*p) )
3475 msg(
M_FATAL,
"TAP device name must be '--dev tapNNNN'" );
3478 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
3483 if (access( tunname, F_OK ) < 0 && errno == ENOENT)
3503 if ((tt->fd = open(tunname, O_RDWR)) < 0)
3505 msg(
M_ERR,
"Cannot open TAP device '%s'", tunname);
3510 msg(
M_INFO,
"TUN/TAP device %s opened", tunname);
3539 close_tun_generic(tt);
3552 return write(tt->fd, buf, len);
3558 return read(tt->fd, buf, len);
3561 #elif defined(_WIN32)
3603 err = GetLastError();
3604 if (err == ERROR_IO_PENDING)
3660 dmsg(
D_WIN32_IO,
"WIN32 I/O: TAP Write immediate return [%d,%d]",
3666 err = GetLastError();
3667 if (err == ERROR_IO_PENDING)
3701 err = GetLastError();
3719 HDEVINFO dev_info_set;
3724 dev_info_set = SetupDiGetClassDevsEx(&
GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL);
3725 if (dev_info_set == INVALID_HANDLE_VALUE)
3727 err = GetLastError();
3732 for (DWORD i = 0;; ++i)
3734 SP_DEVINFO_DATA device_info_data;
3737 char net_cfg_instance_id_string[] =
"NetCfgInstanceId";
3739 char device_instance_id[256];
3743 ULONG dev_interface_list_size;
3746 ZeroMemory(&device_info_data,
sizeof(SP_DEVINFO_DATA));
3747 device_info_data.cbSize =
sizeof(SP_DEVINFO_DATA);
3748 res = SetupDiEnumDeviceInfo(dev_info_set, i, &device_info_data);
3751 if (GetLastError() == ERROR_NO_MORE_ITEMS)
3761 dev_key = SetupDiOpenDevRegKey(dev_info_set, &device_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
3762 if (dev_key == INVALID_HANDLE_VALUE)
3769 status = RegQueryValueEx(dev_key,
3770 net_cfg_instance_id_string,
3775 if (
status != ERROR_SUCCESS)
3780 len =
sizeof(device_instance_id);
3781 res = SetupDiGetDeviceInstanceId(dev_info_set, &device_info_data, device_instance_id, len, &len);
3787 cr = CM_Get_Device_Interface_List_Size(&dev_interface_list_size,
3790 CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
3792 if (cr != CR_SUCCESS)
3797 char *dev_interface_list =
gc_malloc(dev_interface_list_size,
false,
gc);
3800 dev_interface_list_size,
3801 CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
3802 if (cr != CR_SUCCESS)
3807 char *dev_if = dev_interface_list;
3810 while (strlen(dev_if) > 0)
3828 last->
next = dev_iif;
3832 dev_if += strlen(dev_if) + 1;
3836 RegCloseKey(dev_key);
3839 SetupDiDestroyDeviceInfoList(dev_info_set);
3861 if (
status != ERROR_SUCCESS)
3863 msg(
M_FATAL,
"Error opening registry key: %s", ADAPTER_KEY);
3869 char enum_name[256];
3870 char unit_string[256];
3872 char component_id_string[] =
"ComponentId";
3873 char component_id[256];
3874 char net_cfg_instance_id_string[] =
"NetCfgInstanceId";
3875 BYTE net_cfg_instance_id[256];
3878 len =
sizeof(enum_name);
3888 if (
status == ERROR_NO_MORE_ITEMS)
3892 else if (
status != ERROR_SUCCESS)
3894 msg(
M_FATAL,
"Error enumerating registry subkeys of key: %s",
3898 snprintf(unit_string,
sizeof(unit_string),
"%s\\%s",
3899 ADAPTER_KEY, enum_name);
3908 if (
status != ERROR_SUCCESS)
3914 len =
sizeof(component_id);
3915 status = RegQueryValueEx(
3917 component_id_string,
3920 (LPBYTE)component_id,
3923 if (
status != ERROR_SUCCESS || data_type != REG_SZ)
3926 unit_string, component_id_string);
3930 len =
sizeof(net_cfg_instance_id);
3931 status = RegQueryValueEx(
3933 net_cfg_instance_id_string,
3936 net_cfg_instance_id,
3939 if (
status == ERROR_SUCCESS && data_type == REG_SZ)
3952 else if (strcasecmp(component_id,
"ovpn-dco") == 0)
3980 RegCloseKey(unit_key);
3985 RegCloseKey(adapter_key);
3993 HKEY network_connections_key;
4001 NETWORK_CONNECTIONS_KEY,
4004 &network_connections_key);
4006 if (
status != ERROR_SUCCESS)
4008 msg(
M_FATAL,
"Error opening registry key: %s", NETWORK_CONNECTIONS_KEY);
4013 char enum_name[256];
4014 char connection_string[256];
4015 HKEY connection_key;
4016 WCHAR name_data[256];
4018 const WCHAR name_string[] = L
"Name";
4020 len =
sizeof(enum_name);
4022 network_connections_key,
4030 if (
status == ERROR_NO_MORE_ITEMS)
4034 else if (
status != ERROR_SUCCESS)
4036 msg(
M_FATAL,
"Error enumerating registry subkeys of key: %s",
4037 NETWORK_CONNECTIONS_KEY);
4040 snprintf(connection_string,
sizeof(connection_string),
4041 "%s\\%s\\Connection",
4042 NETWORK_CONNECTIONS_KEY, enum_name);
4051 if (
status != ERROR_SUCCESS)
4053 dmsg(
D_REGISTRY,
"Error opening registry key: %s", connection_string);
4057 len =
sizeof(name_data);
4058 status = RegQueryValueExW(
4066 if (
status != ERROR_SUCCESS || name_type != REG_SZ)
4069 NETWORK_CONNECTIONS_KEY, connection_string, name_string);
4078 n = WideCharToMultiByte(CP_UTF8, 0, name_data, -1, NULL, 0, NULL, NULL);
4080 WideCharToMultiByte(CP_UTF8, 0, name_data, -1,
name, n, NULL, NULL);
4095 RegCloseKey(connection_key);
4100 RegCloseKey(network_connections_key);
4112 const unsigned int mask = 3;
4113 const char *err = NULL;
4115 if (local == remote)
4117 err =
"must be different";
4120 if ((local & (~mask)) != (remote & (~mask)))
4122 err =
"must exist within the same 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
4125 if ((local & mask) == 0
4126 || (local & mask) == 3
4127 || (remote & mask) == 0
4128 || (remote & mask) == 3)
4130 err =
"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";
4138 msg(
M_FATAL,
"There is a problem in your selection of --ifconfig endpoints [local=%s, remote=%s]. The local and remote VPN endpoints %s. Try '" PACKAGE " --show-valid-subnets' option for more info.",
4151 printf(
"On Windows, point-to-point IP support (i.e. --dev tun)\n");
4152 printf(
"is emulated by the TAP-Windows driver. The major limitation\n");
4153 printf(
"imposed by this approach is that the --ifconfig local and\n");
4154 printf(
"remote endpoints must be part of the same 255.255.255.252\n");
4155 printf(
"subnet. The following list shows examples of endpoint\n");
4156 printf(
"pairs which satisfy this requirement. Only the final\n");
4157 printf(
"component of the IP address pairs is at issue.\n\n");
4158 printf(
"As an example, the following option would be correct:\n");
4159 printf(
" --ifconfig 10.7.0.5 10.7.0.6 (on host A)\n");
4160 printf(
" --ifconfig 10.7.0.6 10.7.0.5 (on host B)\n");
4161 printf(
"because [5,6] is part of the below list.\n\n");
4163 for (i = 0; i < 256; i += 4)
4165 printf(
"[%3d,%3d] ", i+1, i+2);
4183 bool warn_panel_null =
false;
4184 bool warn_panel_dup =
false;
4185 bool warn_tap_dup =
false;
4196 msg(msglev,
"Available adapters [name, GUID, driver]:");
4215 warn_panel_dup =
true;
4217 else if (links == 0)
4221 warn_panel_null =
true;
4222 msg(msglev,
"[NULL] %s", tr->
guid);
4231 if (tr != tr1 && !strcmp(tr->
guid, tr1->
guid))
4233 warn_tap_dup =
true;
4241 msg(warnlev,
"WARNING: Some TAP-Windows adapters have duplicate GUIDs");
4246 msg(warnlev,
"WARNING: Some TAP-Windows adapters have duplicate links from the Network Connections control panel");
4249 if (warn_panel_null)
4251 msg(warnlev,
"WARNING: Some TAP-Windows adapters have no link from the Network Connections control panel");
4313 msg(
M_FATAL,
"There are no TAP-Windows, Wintun or ovpn-dco adapters "
4314 "on this system. You should be able to create an adapter "
4315 "by using tapctl.exe utility.");
4325 uint8_t *actual_name,
4326 int actual_name_size,
4327 const struct tap_reg *tap_reg_src,
4336 ASSERT(device_number >= 0);
4347 ASSERT(actual_name_size > 0);
4352 for (i = 0; i < device_number; i++)
4378 if (windows_driver != NULL)
4391 uint8_t *actual_name,
4392 int actual_name_size,
4411 ASSERT(actual_name_size > 0);
4457 const IP_ADAPTER_INFO *
4461 IP_ADAPTER_INFO *pi = NULL;
4464 if ((
status = GetAdaptersInfo(NULL, &size)) != ERROR_BUFFER_OVERFLOW)
4466 msg(
M_INFO,
"GetAdaptersInfo #1 failed (status=%u) : %s",
4472 pi = (PIP_ADAPTER_INFO)
gc_malloc(size,
false,
gc);
4473 if ((
status = GetAdaptersInfo(pi, &size)) != NO_ERROR)
4475 msg(
M_INFO,
"GetAdaptersInfo #2 failed (status=%u) : %s",
4484 const IP_PER_ADAPTER_INFO *
4488 IP_PER_ADAPTER_INFO *pi = NULL;
4493 if ((
status = GetPerAdapterInfo(index, NULL, &size)) != ERROR_BUFFER_OVERFLOW)
4495 msg(
M_INFO,
"GetPerAdapterInfo #1 failed (status=%u) : %s",
4501 pi = (PIP_PER_ADAPTER_INFO)
gc_malloc(size,
false,
gc);
4502 if ((
status = GetPerAdapterInfo((ULONG)index, pi, &size)) == ERROR_SUCCESS)
4508 msg(
M_INFO,
"GetPerAdapterInfo #2 failed (status=%u) : %s",
4517 static const IP_INTERFACE_INFO *
4521 IP_INTERFACE_INFO *ii = NULL;
4524 if ((
status = GetInterfaceInfo(NULL, &size)) != ERROR_INSUFFICIENT_BUFFER)
4526 msg(
M_INFO,
"GetInterfaceInfo #1 failed (status=%u) : %s",
4532 ii = (PIP_INTERFACE_INFO)
gc_malloc(size,
false,
gc);
4533 if ((
status = GetInterfaceInfo(ii, &size)) == NO_ERROR)
4539 msg(
M_INFO,
"GetInterfaceInfo #2 failed (status=%u) : %s",
4547 static const IP_ADAPTER_INDEX_MAP *
4554 for (i = 0; i < list->NumAdapters; ++i)
4556 const IP_ADAPTER_INDEX_MAP *inter = &list->Adapter[i];
4557 if (index == inter->Index)
4571 const IP_ADAPTER_INFO *
4576 const IP_ADAPTER_INFO *a;
4579 for (a = ai; a != NULL; a = a->Next)
4581 if (a->Index == index)
4590 const IP_ADAPTER_INFO *
4602 const IP_ADDR_STRING *ip = &ai->IpAddressList;
4626 const IP_ADDR_STRING *iplist = &ai->IpAddressList;
4636 iplist = iplist->Next;
4642 const char *ip_str = iplist->IpAddress.String;
4643 const char *netmask_str = iplist->IpMask.String;
4644 bool succeed1 =
false;
4645 bool succeed2 =
false;
4647 if (ip_str && netmask_str && strlen(ip_str) && strlen(netmask_str))
4649 *ip =
getaddr(getaddr_flags, ip_str, 0, &succeed1, NULL);
4650 *netmask =
getaddr(getaddr_flags, netmask_str, 0, &succeed2, NULL);
4651 ret = (succeed1 ==
true && succeed2 ==
true);
4664 in_addr_t ip_adapter = 0;
4665 in_addr_t netmask_adapter = 0;
4667 return (
status && ip_adapter == ip && netmask_adapter == netmask);
4675 const IP_ADAPTER_INFO *
4701 for (i = 0; i < n; ++i)
4703 in_addr_t ip, netmask;
4739 if (highest_netmask)
4741 *highest_netmask = 0;
4747 for (i = 0; i < n; ++i)
4749 in_addr_t adapter_ip, adapter_netmask;
4752 if (adapter_ip && adapter_netmask && (ip & adapter_netmask) == (adapter_ip & adapter_netmask))
4754 if (highest_netmask && adapter_netmask > *highest_netmask)
4756 *highest_netmask = adapter_netmask;
4774 in_addr_t highest_netmask = 0;
4775 int lowest_metric = INT_MAX;
4790 if (first || hn > highest_netmask)
4792 highest_netmask = hn;
4795 lowest_metric = metric;
4804 else if (hn == highest_netmask)
4810 if (metric >= 0 && metric < lowest_metric)
4813 lowest_metric = metric;
4820 dmsg(
D_ROUTE_DEBUG,
"DEBUG: IP Locate: ip=%s nm=%s index=%d count=%d metric=%d",
4824 count ? *count : -1,
4834 *netmask = highest_netmask;
4846 #define DHCP_STATUS_UNDEF 0
4847 #define DHCP_STATUS_ENABLED 1
4848 #define DHCP_STATUS_DISABLED 2
4861 if (ai->DhcpEnabled)
4887 const IP_ADDR_STRING *ip = &a->IpAddressList;
4891 const DWORD
context = ip->Context;
4893 if ((
status = DeleteIPAddress((ULONG)
context)) == NO_ERROR)
4895 msg(
M_INFO,
"Successfully deleted previously set dynamic IP/netmask: %s/%s",
4896 ip->IpAddress.String,
4901 const char *empty =
"0.0.0.0";
4902 if (strcmp(ip->IpAddress.String, empty)
4903 || strcmp(ip->IpMask.String, empty))
4905 msg(
M_INFO,
"NOTE: could not delete previously set dynamic IP/netmask: %s/%s (status=%u)",
4906 ip->IpAddress.String,
4926 swprintf(wbuf,
SIZE(wbuf), L
"\\DEVICE\\TCPIP_%hs", guid);
4927 if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR)
4933 index = (DWORD)aindex;
4948 if (!strcmp(guid,
list->AdapterName))
4950 index =
list->Index;
4971 msg(
M_INFO,
"NOTE: could not get adapter index for %s", guid);
4985 buf_printf(&out,
"%s", ip->IpAddress.String);
4986 if (strlen(ip->IpMask.String))
5003 msg(msglev,
"%s", a->Description);
5004 msg(msglev,
" Index = %d", (
int)a->Index);
5005 msg(msglev,
" GUID = %s", a->AdapterName);
5007 msg(msglev,
" MAC = %s",
format_hex_ex(a->Address, a->AddressLength, 0, 1,
":",
gc));
5012 msg(msglev,
" DHCP LEASE OBTAINED = %s",
time_string(a->LeaseObtained, 0,
false,
gc));
5013 msg(msglev,
" DHCP LEASE EXPIRES = %s",
time_string(a->LeaseExpires, 0,
false,
gc));
5039 msg(msglev,
"SYSTEM ADAPTER LIST");
5042 const IP_ADAPTER_INFO *a;
5045 for (a = ai; a != NULL; a = a->Next)
5068 msg(
M_ERR,
"Error: init SA failed");
5071 status = SetKernelObjectSecurity(hand, DACL_SECURITY_INFORMATION, &
sa.sd);
5074 msg(
M_ERRNO,
"Error: SetKernelObjectSecurity failed on %s", device_path);
5088 const char *device_guid = NULL;
5090 uint8_t actual_buffer[256];
5091 char device_path[256];
5102 msg(
M_FATAL,
"TAP-Windows adapter '%s' not found", dev_node);
5106 snprintf(device_path,
sizeof(device_path),
"%s%s%s",
5117 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
5121 if (hand == INVALID_HANDLE_VALUE)
5123 msg(
M_ERR,
"CreateFile failed on TAP device: %s", device_path);
5131 int device_number = 0;
5138 sizeof(actual_buffer),
5150 snprintf(device_path,
sizeof(device_path),
"%s%s%s",
5161 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
5165 if (hand == INVALID_HANDLE_VALUE)
5167 msg(
M_WARN,
"CreateFile failed on TAP device: %s", device_path);
5193 DWORD
status = IpReleaseAddress((IP_ADAPTER_INDEX_MAP *)inter);
5201 msg(
M_WARN,
"NOTE: Release of DHCP-assigned IP address lease on TAP-Windows adapter failed: %s (code=%u)",
5233 DWORD
status = IpRenewAddress((IP_ADAPTER_INDEX_MAP *)inter);
5241 msg(
M_WARN,
"WARNING: Failed to renew DHCP IP address lease on TAP-Windows adapter: %s (code=%u)",
5267 for (i = 0; i < n; ++i)
5281 msg(msglevel,
"%s: command failed", prefix);
5294 const char err[] =
"ERROR: Windows ipconfig command failed";
5323 const char *ip_str = src->IpAddress.String;
5325 bool succeed =
false;
5331 if (!ip_str || !strlen(ip_str))
5336 ip =
getaddr(getaddr_flags, ip_str, 0, &succeed, NULL);
5350 msg(
M_INFO,
"ip_addr_string_to_array [%d]", *dest_len);
5351 for (i = 0; i < *dest_len; ++i)
5364 int a2len =
SIZE(a2);
5374 for (i = 0; i < a1len; ++i)
5392 for (i = 0; i < len; ++i)
5410 DWORD adapter_index)
5422 for (
int i = 0; i < addr_len; ++i)
5424 const char *fmt = (i == 0) ?
5425 "%s%s interface ipv6 set dns %lu static %s"
5426 :
"%s%s interface ipv6 add dns %lu %s";
5447 const in_addr_t *addr_list,
5449 const IP_ADDR_STRING *current,
5450 DWORD adapter_index,
5451 const bool test_first)
5455 bool delete_first =
false;
5456 bool is_dns = !strcmp(type,
"dns");
5463 delete_first =
true;
5468 delete_first =
true;
5486 for (i = 0; i < addr_len; ++i)
5490 const char *fmt = count ?
5491 "%s%s interface ip add %s %lu %s"
5492 :
"%s%s interface ip set %s %lu static %s";
5514 msg(
M_INFO,
"NETSH: %lu %s %s [already set]",
5534 dest[0].Next = NULL;
5539 dest[0].Next = &dest[1];
5540 dest[1].Next = NULL;
5546 DWORD adapter_index,
5548 const in_addr_t netmask,
5549 const unsigned int flags)
5553 const IP_ADAPTER_INFO *ai = NULL;
5554 const IP_PER_ADAPTER_INFO *pai = NULL;
5567 msg(
M_INFO,
"NETSH: %lu %s/%s [already set]",
5589 IP_ADDR_STRING wins[2];
5596 pai ? &pai->DnsServerList : NULL,
5599 if (ai && ai->HaveWins)
5623 "%s%s interface ip set address %lu dhcp",
5658 msg(
M_NONFATAL,
"TUN: enabling dhcp using service failed: %s [status=%u if_index=%d]",
5663 msg(
M_INFO,
"DHCP enabled on interface %d using service",
dhcp.iface.index);
5678 MIB_IPINTERFACE_ROW ipiface;
5679 InitializeIpInterfaceEntry(&ipiface);
5680 const char *family_name = (family == AF_INET6) ?
"IPv6" :
"IPv4";
5681 ipiface.Family = family;
5682 ipiface.InterfaceIndex = iface_index;
5683 if (family == AF_INET6 && mtu < 1280)
5685 msg(
M_INFO,
"NOTE: IPv6 interface MTU < 1280 conflicts with IETF standards and might not work");
5688 err = GetIpInterfaceEntry(&ipiface);
5689 if (err == NO_ERROR)
5691 if (family == AF_INET)
5693 ipiface.SitePrefixLength = 0;
5695 ipiface.NlMtu = mtu;
5696 err = SetIpInterfaceEntry(&ipiface);
5699 if (err != NO_ERROR)
5701 msg(
M_WARN,
"TUN: Setting %s mtu failed: %s [status=%lu if_index=%d]",
5706 msg(
M_INFO,
"%s MTU set to %d on interface %d using SetIpInterfaceEntry()", family_name, mtu, iface_index);
5742 else if (strcmp(
BSTR(&actual),
"NULL"))
5744 return BSTR(&actual);
5770 msg(
M_INFO,
"NOTE: now trying netsh (this may take some time)");
5796 msg(
M_WARN,
"write_dhcp_u8: buffer overflow building DHCP options");
5810 const int size =
len *
sizeof(uint32_t);
5815 msg(
M_WARN,
"write_dhcp_u32_array: buffer overflow building DHCP options");
5818 if (size < 1 || size > 255)
5821 msg(
M_WARN,
"write_dhcp_u32_array: size (%d) must be > 0 and <= 255", size);
5826 for (i = 0; i <
len; ++i)
5836 const int len = strlen(str);
5840 msg(
M_WARN,
"write_dhcp_str: buffer overflow building DHCP options");
5843 if (len < 1 || len > 255)
5846 msg(
M_WARN,
"write_dhcp_str: string '%s' must be > 0 bytes and <= 255 bytes", str);
5864 int array_len,
bool *error)
5869 int label_length_pos;
5871 for (i = 0; i < array_len; i++)
5873 const char *ptr = str_array[i];
5875 if (strlen(ptr) +
len + 1 >
sizeof(tmp_buf))
5878 msg(
M_WARN,
"write_dhcp_search_str: temp buffer overflow building DHCP options");
5886 label_length_pos =
len++;
5890 if (*ptr ==
'.' || *ptr ==
'\0')
5892 tmp_buf[label_length_pos] = (
len-label_length_pos)-1;
5893 label_length_pos =
len;
5899 tmp_buf[
len++] = *ptr++;
5908 msg(
M_WARN,
"write_search_dhcp_str: buffer overflow building DHCP options");
5914 msg(
M_WARN,
"write_dhcp_search_str: search domain string must be <= 255 bytes");
5961 msg(
M_WARN,
"build_dhcp_options_string: buffer overflow building DHCP options");
5981 const int pre_sleep = 1;
5983 buf_printf(&cmd,
"openvpn --verb %d --tap-sleep %d", verb, pre_sleep);
6016 msg(
M_WARN,
"Register_dns failed using service: %s [status=0x%x]",
6022 msg(
M_INFO,
"Register_dns request sent to the service");
6055 msg(
M_NONFATAL,
"Register ring buffers failed using service: %s [status=0x%x]",
6061 msg(
M_INFO,
"Ring buffers registered via service");
6081 buf_printf(&cmd,
"openvpn --verb %d --register-dns --rdns-internal", verb);
6095 dsa = (local | (~netmask)) + offset;
6099 dsa = (local & netmask) + offset;
6104 msg(
M_FATAL,
"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",
print_in_addr_t(dsa, 0, &
gc));
6107 if ((local & netmask) != (dsa & netmask))
6109 msg(
M_FATAL,
"ERROR: --ip-win32 dynamic [offset] : offset is outside of --ifconfig subnet");
6122 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_VERSION,
6123 &info,
sizeof(info),
6124 &info,
sizeof(info), &len, NULL))
6129 (info[2] ?
"(DEBUG)" :
""));
6134 msg(
M_FATAL,
"ERROR: This version of " PACKAGE_NAME " requires a TAP-Windows driver that is at least version %d.%d -- If you recently upgraded your " PACKAGE_NAME " distribution, a reboot is probably required at this point to get Windows to see the new driver.",
6143 && info[0] == 9 && info[1] < 8)
6145 msg(
M_INFO,
"WARNING: Tap-Win32 driver version %d.%d does not support IPv6 in TUN mode. IPv6 will not work. Upgrade your Tap-Win32 driver.", (
int)info[0], (
int)info[1]);
6151 && info[0] == 9 && info[1] == 8)
6153 msg(
M_FATAL,
"ERROR: Tap-Win32 driver version %d.%d is buggy regarding small IPv4 packets in TUN mode. Upgrade your Tap-Win32 driver.", (
int)info[0], (
int)info[1]);
6162 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_MTU,
6164 &mtu,
sizeof(mtu), &len, NULL))
6172 const char *device_guid,
6173 bool dhcp_masq_post)
6194 .iface = {.index = index, .name =
"" }
6205 status = FlushIpNetTable(index);
6210 msg(
M_INFO,
"Successful ARP Flush on interface [%lu] %s",
6216 msg(
D_TUNTAP_INFO,
"NOTE: FlushIpNetTable failed on interface [%lu] %s (status=%lu) : %s",
6233 msg(
M_WARN,
"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'");
6255 const char *error_suffix =
"I am having trouble using the Windows 'IP helper API' to automatically set the IP address -- consider using other --ip-win32 methods (not 'ipapi')";
6260 msg(
M_FATAL,
"ERROR: unable to get adapter index for interface %s -- %s",
6268 msg(
M_WARN,
"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'");
6282 msg(
M_INFO,
"Succeeded in adding a temporary IP/netmask of %s/%s to interface %s using the Win32 IP Helper API",
6290 msg(
M_FATAL,
"ERROR: AddIPAddress %s/%s failed on interface %s, index=%lu, status=%lu (windows error: '%s') -- %s",
6311 FILE_MAP_ALL_ACCESS,
6317 FILE_MAP_ALL_ACCESS,
6334 switch (GetLastError())
6336 case ERROR_ACCESS_DENIED:
6337 msg(
M_FATAL,
"ERROR: Wintun requires SYSTEM privileges and therefore "
6338 "should be used with interactive service. If you want to "
6339 "use openvpn from command line, you need to do SYSTEM "
6340 "elevation yourself (for example with psexec).");
6343 case ERROR_ALREADY_INITIALIZED:
6344 msg(
M_NONFATAL,
"Adapter %s is already in use", device_guid);
6362 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
6366 msg(
M_WARN,
"WARNING: The TAP-Windows driver rejected a TAP_WIN_IOCTL_SET_MEDIA_STATUS DeviceIoControl call.");
6372 msg(
M_INFO,
"Sleeping for %d seconds...", s);
6385 msg(
M_FATAL,
"ERROR: --dev tun also requires --ifconfig");
6396 ep[0] = htonl(tt->
local);
6400 status = DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_TUN,
6402 ep,
sizeof(ep), &len, NULL);
6410 status ?
"SUCCEEDED" :
"FAILED");
6415 status ?
"SUCCEEDED" :
"FAILED");
6421 ep[0] = htonl(tt->
local);
6424 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT,
6426 ep,
sizeof(ep), &len, NULL))
6428 msg(
M_FATAL,
"ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set Point-to-Point mode, which is required for --dev tun");
6443 ep[0] = htonl(tt->
local);
6469 #ifndef SIMULATE_DHCP_FAILED
6470 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_DHCP_MASQ,
6472 ep,
sizeof(ep), &len, NULL))
6474 msg(
M_FATAL,
"ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set TAP_WIN_IOCTL_CONFIG_DHCP_MASQ mode");
6477 msg(
M_INFO,
"Notified TAP-Windows driver to set a DHCP IP/netmask of %s/%s on interface %s [DHCP-serv: %s, lease-time: %d]",
6492 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT,
6496 msg(
M_FATAL,
"ERROR: The TAP-Windows driver rejected a TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT DeviceIoControl call");
6501 msg(
M_WARN,
"DHCP option string not set due to error");
6513 const char *path = NULL;
6514 char tuntap_device_path[256];
6549 snprintf(tuntap_device_path,
sizeof(tuntap_device_path),
"%s%s%s",
6553 path = tuntap_device_path;
6558 tt->
hand = CreateFile(path,
6559 GENERIC_READ | GENERIC_WRITE,
6563 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
6565 if (tt->
hand == INVALID_HANDLE_VALUE)
6576 msg(
D_TUNTAP_INFO,
"Failed to register %s adapter ring buffers", device_guid);
6577 CloseHandle(tt->
hand);
6592 uint8_t actual_buffer[256];
6608 msg(
M_FATAL,
"Adapter '%s' not found", dev_node);
6613 msg(
M_FATAL,
"Adapter '%s' is using %s driver, %s expected. If you want to use this device, adjust --windows-driver.",
6624 int device_number = 0;
6632 sizeof(actual_buffer),
6686 *dhcp_masq_post =
true;
6711 bool dhcp_masq =
false;
6712 bool dhcp_masq_post =
false;
6753 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
6759 msg(
M_WARN,
"Some --dhcp-option or --dns options require DHCP server,"
6760 " which is not supported by the selected %s driver. They will be"
6770 const char *device_guid = NULL;
6798 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_INFO,
6816 while (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_LOG_LINE,
6830 const char *ifconfig_ip_local;
6838 "%s%s interface %s delete dns %lu all",
6841 ipv6 ?
"ipv6" :
"ipv4",
6849 "%s%s interface ipv4 delete winsservers %lu all",
6875 "%s%s interface %s delete address %lu %s store=active",
6878 ipv6 ?
"ipv6" :
"ipv4",
6894 if (!CancelIo(tt->
hand))
6896 msg(
M_WARN |
M_ERRNO,
"Warning: CancelIO failed on %s adapter", adaptertype);
6900 dmsg(
D_WIN32_IO_LOW,
"Attempting close of overlapped read event on %s adapter", adaptertype);
6903 dmsg(
D_WIN32_IO_LOW,
"Attempting close of overlapped write event on %s adapter", adaptertype);
6909 if (!CloseHandle(tt->
hand))
6911 msg(
M_WARN |
M_ERRNO,
"Warning: CloseHandle failed on %s adapter", adaptertype);
6995 msg(
M_WARN,
"Warning: DeleteIPAddress[%u] failed on TAP-Windows adapter, status=%u : %s",
7051 return "[unknown --ip-win32 type]";
7080 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
7083 open_tun_generic(dev, dev_type, dev_node, tt);
7091 close_tun_generic(tt);
7104 return read(tt->fd, buf,
len);