60 const static GUID
GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
61 const static GUID
GUID_DEVINTERFACE_NET = { 0xcac88484, 0x7515, 0x4c03, { 0x82, 0xe6, 0x71, 0xa8, 0x7a, 0xba, 0xc3, 0x61 } };
65 #define NI_TEST_FIRST (1<<0)
66 #define NI_IP_NETMASK (1<<1)
67 #define NI_OPTIONS (1<<2)
72 const in_addr_t netmask,
73 const unsigned int flags);
85 static void exec_command(
const char *prefix,
const struct argv *a,
int n,
int msglevel);
113 if (addr.
family == AF_INET)
118 add ?
"add" :
"remove",
126 add ?
"add" :
"remove",
137 msg(
M_WARN,
"TUN: %s address failed using service: %s [status=%u if_index=%d]",
181 msg(
D_LOW,
"%s dns domain on '%s' (if_index = %d) using service",
190 msg(
M_WARN,
"TUN: %s dns domain failed using service: %s [status=%u if_name=%s]",
196 msg(
M_INFO,
"DNS domain %s using service", (add ?
"set" :
"deleted"));
209 int addr_len = add ? len : 0;
210 const char *ip_proto_name = family == AF_INET6 ?
"IPv6" :
"IPv4";
235 if (addr_len > _countof(dns.
addr))
237 addr_len = _countof(dns.
addr);
239 msg(
M_WARN,
"Number of %s DNS addresses sent to service truncated to %d",
240 ip_proto_name, addr_len);
243 for (
int i = 0; i < addr_len; ++i)
245 if (family == AF_INET6)
255 msg(
D_LOW,
"%s %s dns servers on '%s' (if_index = %d) using service",
265 msg(
M_WARN,
"TUN: %s %s dns failed using service: %s [status=%u if_name=%s]",
271 msg(
M_INFO,
"%s dns servers %s using service", ip_proto_name, (add ?
"set" :
"deleted"));
305 if (addr_len > _countof(wins.
addr))
307 addr_len = _countof(wins.
addr);
309 msg(
M_WARN,
"Number of WINS addresses sent to service truncated to %d",
313 for (
int i = 0; i < addr_len; ++i)
318 msg(
D_LOW,
"%s WINS servers on '%s' (if_index = %d) using service",
328 msg(
M_WARN,
"TUN: %s WINS failed using service: %s [status=%u if_name=%s]",
334 msg(
M_INFO,
"WINS servers %s using service", (add ?
"set" :
"deleted"));
347 const char *family_name = (family == AF_INET6) ?
"IPv6" :
"IPv4";
359 if (family == AF_INET6 && mtu < 1280)
361 msg(
M_INFO,
"NOTE: IPv6 interface MTU < 1280 conflicts with IETF standards and might not work");
371 msg(
M_NONFATAL,
"TUN: setting %s mtu using service failed: %s [status=%u if_index=%d]",
376 msg(
M_INFO,
"%s MTU set to %d on interface %d using service", family_name, mtu, mtu_msg.
iface.
index);
394 argv_printf(&
argv,
"%s%s nicconfig where (InterfaceIndex=%ld) call SetDNSDomain '%s'",
403 #ifdef TARGET_SOLARIS
404 static void solaris_error_close(
struct tuntap *tt,
const struct env_set *
es,
const char *actual,
bool unplumb_inet6);
409 #if defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H
410 #include <sys/kern_control.h>
411 #include <net/if_utun.h>
412 #include <sys/sys_domain.h>
418 is_dev_type(
const char *dev,
const char *dev_type,
const char *match_type)
427 return !strcmp(dev_type, match_type);
431 return !strncmp(dev, match_type, strlen(match_type));
471 return "[unknown-dev-type]";
481 const char *dev_type,
482 const char *dev_node,
512 const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
517 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",
524 if (!looks_like_netmask)
526 msg(
M_WARN,
"WARNING: Since you are using --dev tap, the second argument to --ifconfig must be a netmask, for example something like 255.255.255.0. %s",
542 in_addr_t remote_netmask)
546 msg(
M_INFO,
"CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
557 const in_addr_t test_netmask = 0xFFFFFF00;
558 const in_addr_t public_net =
public &test_netmask;
559 const in_addr_t local_net = local & test_netmask;
560 const in_addr_t remote_net = remote_netmask & test_netmask;
562 if (
public == local ||
public == remote_netmask)
565 "WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
573 if (public_net == local_net || public_net == remote_net)
576 "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",
586 const in_addr_t public_network =
public &remote_netmask;
587 const in_addr_t virtual_network = local & remote_netmask;
588 if (public_network == virtual_network)
591 "WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
612 const in_addr_t netmask,
617 in_addr_t lan_gw = 0;
618 in_addr_t lan_netmask = 0;
622 const in_addr_t lan_network = lan_gw & lan_netmask;
623 const in_addr_t network = ip & netmask;
626 if ((network & lan_netmask) == lan_network
627 || (lan_network & netmask) == network)
629 msg(
M_WARN,
"WARNING: potential %s subnet conflict between local LAN [%s/%s] and remote VPN [%s/%s]",
649 if ((rgi.
flags & needed) == needed)
652 if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
654 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.");
754 msg(
M_FATAL,
"Error: problem with tun vs. tap setting");
780 setenv_str(
es,
"ifconfig_remote", ifconfig_remote_netmask);
784 setenv_str(
es,
"ifconfig_netmask", ifconfig_remote_netmask);
793 setenv_str(
es,
"ifconfig_ipv6_local", ifconfig_ipv6_local);
795 setenv_str(
es,
"ifconfig_ipv6_remote", ifconfig_ipv6_remote);
809 const char *dev_type,
811 const char *ifconfig_local_parm,
812 const char *ifconfig_remote_netmask_parm,
813 const char *ifconfig_ipv6_local_parm,
814 int ifconfig_ipv6_netbits_parm,
815 const char *ifconfig_ipv6_remote_parm,
816 struct addrinfo *local_public,
817 struct addrinfo *remote_public,
818 const bool strict_warn,
832 if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
860 ifconfig_remote_netmask_parm,
870 struct addrinfo *curele;
878 for (curele = local_public; curele; curele = curele->ai_next)
880 if (curele->ai_family == AF_INET)
884 ((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
890 for (curele = remote_public; curele; curele = curele->ai_next)
892 if (curele->ai_family == AF_INET)
896 ((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
931 if (ifconfig_ipv6_local_parm && ifconfig_ipv6_remote_parm)
938 if (inet_pton( AF_INET6, ifconfig_ipv6_local_parm, &tt->
local_ipv6 ) != 1
939 || inet_pton( AF_INET6, ifconfig_ipv6_remote_parm, &tt->
remote_ipv6 ) != 1)
941 msg(
M_FATAL,
"init_tun: problem converting IPv6 ifconfig addresses %s and %s to binary", ifconfig_ipv6_local_parm, ifconfig_ipv6_remote_parm );
994 msg(
M_FATAL,
"Cannot allocate memory for ring buffer");
1002 msg(
M_FATAL,
"Cannot create events for ring buffer");
1013 #if defined(_WIN32) \
1014 || defined(TARGET_DARWIN) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
1052 #if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
1053 || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD)
1064 create_arbitrary_remote(
struct tuntap *tt )
1070 if (remote == tt->
local)
1092 #if !defined(TARGET_LINUX)
1098 #if defined(TARGET_LINUX)
1099 if (net_iface_mtu_set(ctx, ifname, tun_mtu) < 0)
1101 msg(
M_FATAL,
"Linux can't set mtu (%d) on %s", tun_mtu, ifname);
1104 if (net_iface_up(ctx, ifname,
true) < 0)
1106 msg(
M_FATAL,
"Linux can't bring %s up", ifname);
1109 if (net_addr_v6_add(ctx, ifname, &tt->
local_ipv6,
1112 msg(
M_FATAL,
"Linux can't add IPv6 to interface %s", ifname);
1114 #elif defined(TARGET_ANDROID)
1117 snprintf(out6,
sizeof(out6),
"%s/%d %d",
1119 management_android_control(
management,
"IFCONFIG6", out6);
1120 #elif defined(TARGET_SOLARIS)
1140 "Solaris ifconfig IPv6 (prepare) failed"))
1142 solaris_error_close(tt,
es, ifname,
true);
1161 solaris_error_close(tt,
es, ifname,
true);
1171 #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
1172 || defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) \
1173 || defined(TARGET_DRAGONFLY)
1179 "generic BSD ifconfig inet6 failed");
1181 #if defined(TARGET_FREEBSD) && __FreeBSD_version >= 1200000 \
1182 && __FreeBSD_version < 1300000
1202 "FreeBSD BSD 'ifconfig inet6 -ifdisabled' failed");
1205 #if defined(TARGET_OPENBSD) || defined(TARGET_NETBSD) \
1206 || defined(TARGET_DARWIN)
1210 #elif defined(TARGET_AIX)
1220 "generic BSD ifconfig inet6 failed");
1223 #elif defined (_WIN32)
1226 msg(
M_INFO,
"******** NOTE: Please manually set the v6 IP of '%s' to %s (if it is not already set)",
1227 ifname, ifconfig_ipv6_local);
1256 argv_printf(&
argv,
"%s%s interface ipv6 set address %lu %s/%d store=active",
1258 ifconfig_ipv6_local, netbits);
1274 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.");
1277 #if !defined(TARGET_LINUX)
1296 #if !defined(_WIN32) && !defined(TARGET_ANDROID)
1303 #if !defined(TARGET_LINUX)
1304 const char *ifconfig_local = NULL;
1305 const char *ifconfig_remote_netmask = NULL;
1316 #if defined(TARGET_LINUX)
1317 if (net_iface_mtu_set(ctx, ifname, tun_mtu) < 0)
1319 msg(
M_FATAL,
"Linux can't set mtu (%d) on %s", tun_mtu, ifname);
1322 if (net_iface_up(ctx, ifname,
true) < 0)
1324 msg(
M_FATAL,
"Linux can't bring %s up", ifname);
1329 if (net_addr_ptp_v4_add(ctx, ifname, &tt->
local,
1332 msg(
M_FATAL,
"Linux can't add IP to interface %s", ifname);
1337 if (net_addr_v4_add(ctx, ifname, &tt->
local,
1340 msg(
M_FATAL,
"Linux can't add IP to interface %s", ifname);
1343 #elif defined(TARGET_ANDROID)
1365 snprintf(out,
sizeof(out),
"%s %s %d %s", ifconfig_local,
1366 ifconfig_remote_netmask, tun_mtu, top);
1367 management_android_control(
management,
"IFCONFIG", out);
1369 #elif defined(TARGET_SOLARIS)
1378 ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1383 solaris_error_close(tt,
es, ifname,
false);
1392 ifname, ifconfig_local, ifconfig_local,
1393 ifconfig_remote_netmask, tun_mtu);
1399 ifconfig_remote_netmask);
1405 solaris_error_close(tt,
es, ifname,
false);
1421 #elif defined(TARGET_OPENBSD)
1423 in_addr_t remote_end;
1435 "%s %s %s %s mtu %d netmask 255.255.255.255 up -link0",
1437 ifconfig_remote_netmask, tun_mtu);
1441 remote_end = create_arbitrary_remote( tt );
1445 ifconfig_remote_netmask);
1451 ifconfig_remote_netmask, tun_mtu);
1468 #elif defined(TARGET_NETBSD)
1469 in_addr_t remote_end = INADDR_ANY;
1475 ifconfig_remote_netmask, tun_mtu);
1479 remote_end = create_arbitrary_remote(tt);
1482 tun_mtu, ifconfig_remote_netmask);
1493 ifconfig_remote_netmask, tun_mtu);
1510 #elif defined(TARGET_DARWIN)
1519 "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
1527 ifconfig_remote_netmask, tun_mtu);
1535 ifconfig_remote_netmask, tun_mtu);
1540 ifname, ifconfig_local, ifconfig_remote_netmask,
1560 #elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1567 ifconfig_remote_netmask, tun_mtu);
1573 ifname, ifconfig_local, netbits, tun_mtu );
1579 #elif defined(TARGET_AIX)
1587 msg(
M_FATAL,
"no tun support on AIX (canthappen)");
1592 ifname, ifconfig_local, ifconfig_remote_netmask, tun_mtu);
1599 #elif defined (_WIN32)
1603 "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
1604 ifname, ifconfig_local,
1605 ifconfig_remote_netmask);
1639 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.");
1642 #if !defined(TARGET_LINUX)
1656 #ifdef ENABLE_MANAGEMENT
1686 #if defined(TARGET_LINUX)
1694 msg(
M_WARN,
"Linux can't del IP from iface %s",
1702 msg(
M_WARN,
"Linux can't del IP from iface %s",
1706 #elif defined(TARGET_FREEBSD)
1725 #if defined(TARGET_LINUX)
1731 #elif defined(TARGET_FREEBSD)
1777 #ifdef TARGET_SOLARIS
1789 #if defined (TARGET_OPENBSD) || (defined(TARGET_DARWIN) && HAVE_NET_IF_UTUN_H)
1809 #include <netinet/ip.h>
1810 #include <sys/uio.h>
1813 header_modify_read_write_return(
int len)
1817 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
1826 write_tun_header(
struct tuntap *tt, uint8_t *buf,
int len)
1838 type = htonl(AF_INET6);
1842 type = htonl(AF_INET);
1845 iv[0].iov_base = &type;
1846 iv[0].iov_len =
sizeof(type);
1847 iv[1].iov_base = buf;
1848 iv[1].iov_len = len;
1850 return header_modify_read_write_return(writev(tt->fd, iv, 2));
1854 return write(tt->fd, buf, len);
1859 read_tun_header(
struct tuntap *tt, uint8_t *buf,
int len)
1866 iv[0].iov_base = &type;
1867 iv[0].iov_len =
sizeof(type);
1868 iv[1].iov_base = buf;
1869 iv[1].iov_len = len;
1871 return header_modify_read_write_return(readv(tt->fd, iv, 2));
1875 return read(tt->fd, buf, len);
1886 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
1888 tun_dco_enabled(
struct tuntap *tt)
1895 #if !(defined(_WIN32) || defined(TARGET_LINUX) || defined(TARGET_SOLARIS))
1897 open_tun_generic(
const char *dev,
const char *dev_type,
const char *dev_node,
1901 char dynamic_name[256];
1902 bool dynamic_opened =
false;
1915 snprintf(tunname,
sizeof(tunname),
"%s", dev_node);
1927 for (
int i = 0; i < 256; ++i)
1929 snprintf(tunname,
sizeof(tunname),
1930 "/dev/%s%d", dev, i);
1931 snprintf(dynamic_name,
sizeof(dynamic_name),
1933 if ((tt->fd = open(tunname, O_RDWR)) > 0)
1935 dynamic_opened =
true;
1940 if (!dynamic_opened)
1942 msg(
M_FATAL,
"Cannot allocate TUN/TAP dev dynamically");
1950 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
1954 if (!dynamic_opened)
1957 if (if_nametoindex( dev ) > 0)
1959 msg(
M_INFO,
"TUN/TAP device %s exists previously, keep at program end", dev );
1963 if ((tt->fd = open(tunname, O_RDWR)) < 0)
1965 msg(
M_ERR,
"Cannot open TUN/TAP dev %s", tunname);
1971 msg(
M_INFO,
"TUN/TAP device %s opened", tunname);
1979 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
1981 open_tun_dco_generic(
const char *dev,
const char *dev_type,
1984 char dynamic_name[256];
1985 bool dynamic_opened =
false;
2001 if (strcmp(dev,
"tun") == 0)
2003 for (
int i = 0; i < 256; ++i)
2005 snprintf(dynamic_name,
sizeof(dynamic_name),
2010 dynamic_opened =
true;
2011 msg(
M_INFO,
"DCO device %s opened", dynamic_name);
2015 else if (ret == -EPERM)
2020 if (!dynamic_opened)
2022 msg(
M_FATAL,
"Cannot allocate DCO dev dynamically");
2036 msg(
M_INFO,
"DCO device %s already exists, won't be destroyed at shutdown",
2042 msg(
M_ERR,
"Cannot open DCO device %s: %s (%d)", dev,
2043 strerror(-ret), ret);
2047 msg(
M_INFO,
"DCO device %s opened", dev);
2056 #if !(defined(_WIN32) || defined(TARGET_SOLARIS))
2058 close_tun_generic(
struct tuntap *tt)
2070 #if defined (TARGET_ANDROID)
2072 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2075 #define ANDROID_TUNNAME "vpnservice-tun"
2080 int oldtunfd = tt->fd;
2086 management_android_control(
management,
"DNS6SERVER",
2092 management_android_control(
management,
"DNSSERVER",
2108 int android_method = managment_android_persisttun_action(
management);
2110 if (oldtunfd >=0 && android_method == ANDROID_KEEP_OLD_TUN)
2117 opentun = management_android_control(
management,
"OPENTUN", dev);
2124 if (oldtunfd >= 0 && android_method == ANDROID_OPEN_BEFORE_CLOSE)
2132 if ((tt->fd < 0) || !opentun)
2134 msg(
M_ERR,
"ERROR: Cannot open TUN");
2145 close_tun_generic(tt);
2158 return read(tt->fd, buf,
len);
2161 #elif defined(TARGET_LINUX)
2163 #ifndef HAVE_LINUX_SOCKIOS_H
2164 #error header file linux/sockios.h required
2170 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2182 else if (tun_dco_enabled(tt))
2184 open_tun_dco_generic(dev, dev_type, tt, ctx);
2191 const char *node = dev_node;
2194 node =
"/dev/net/tun";
2200 if ((tt->fd = open(node, O_RDWR)) < 0)
2202 msg(
M_ERR,
"ERROR: Cannot open TUN/TAP dev %s", node);
2209 ifr.ifr_flags = IFF_NO_PI;
2211 #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
2212 ifr.ifr_flags |= IFF_ONE_QUEUE;
2220 ifr.ifr_flags |= IFF_TUN;
2224 ifr.ifr_flags |= IFF_TAP;
2228 msg(
M_FATAL,
"I don't recognize device %s as a tun or tap device",
2235 if (strcmp(dev,
"tun") && strcmp(dev,
"tap"))
2244 if (ioctl(tt->fd, TUNSETIFF, (
void *) &ifr) < 0)
2246 msg(
M_ERR,
"ERROR: Cannot ioctl TUNSETIFF %s", dev);
2249 msg(
M_INFO,
"TUN/TAP device %s opened", ifr.ifr_name);
2254 #if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
2257 struct ifreq netifr;
2260 if ((ctl_fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
2263 strncpynt(netifr.ifr_name, ifr.ifr_name, IFNAMSIZ);
2264 netifr.ifr_qlen = tt->
options.txqueuelen;
2265 if (ioctl(ctl_fd, SIOCSIFTXQLEN, (
void *) &netifr) >= 0)
2271 msg(
M_WARN |
M_ERRNO,
"Note: Cannot set tx queue length on %s", ifr.ifr_name);
2277 msg(
M_WARN |
M_ERRNO,
"Note: Cannot open control socket on %s", ifr.ifr_name);
2292 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2300 #ifdef ENABLE_FEATURE_TUN_PERSIST
2304 #define TUNSETGROUP _IOW('T', 206, int)
2308 tuncfg(
const char *dev,
const char *dev_type,
const char *dev_node,
2309 int persist_mode,
const char *username,
const char *groupname,
2319 open_tun(dev, dev_type, dev_node, tt, ctx);
2320 if (ioctl(tt->fd, TUNSETPERSIST, persist_mode) < 0)
2322 msg(
M_ERR,
"Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode, dev);
2324 if (username != NULL)
2330 msg(
M_ERR,
"Cannot get user entry for %s", username);
2334 msg(
M_ERR,
"Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
2337 if (groupname != NULL)
2343 msg(
M_ERR,
"Cannot get group entry for %s", groupname);
2347 msg(
M_ERR,
"Cannot ioctl TUNSETGROUP(%s) %s", groupname, dev);
2351 msg(
M_INFO,
"Persist state set to: %s", (persist_mode ?
"ON" :
"OFF"));
2361 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
2362 if (tun_dco_enabled(tt))
2367 close_tun_generic(tt);
2374 return write(tt->fd, buf, len);
2380 return read(tt->fd, buf, len);
2383 #elif defined(TARGET_SOLARIS)
2386 #error I need the symbol TUNNEWPPA from net/if_tun.h
2390 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2393 int if_fd = -1, ip_muxid = -1, arp_muxid = -1, arp_fd = -1, ppa = -1;
2396 const char *ip_node = NULL, *arp_node = NULL;
2397 const char *dev_tuntap_type;
2399 struct strioctl strioc_if, strioc_ppa;
2415 ip_node =
"/dev/udp";
2418 dev_node =
"/dev/tun";
2420 dev_tuntap_type =
"tun";
2421 link_type = I_PLINK;
2425 ip_node =
"/dev/udp";
2428 dev_node =
"/dev/tap";
2430 arp_node = dev_node;
2431 dev_tuntap_type =
"tap";
2432 link_type = I_PLINK;
2436 msg(
M_FATAL,
"I don't recognize device %s as a tun or tap device",
2440 if ((tt->ip_fd = open(ip_node, O_RDWR, 0)) < 0)
2442 msg(
M_ERR,
"Can't open %s", ip_node);
2445 if ((tt->fd = open(dev_node, O_RDWR, 0)) < 0)
2447 msg(
M_ERR,
"Can't open %s", dev_node);
2455 while (*ptr && !isdigit((
int) *ptr))
2463 strioc_ppa.ic_cmd = TUNNEWPPA;
2464 strioc_ppa.ic_timout = 0;
2465 strioc_ppa.ic_len =
sizeof(ppa);
2466 strioc_ppa.ic_dp = (
char *)&ppa;
2470 bool found_one =
false;
2471 while (!found_one && ppa < 64)
2473 int new_ppa = ioctl(tt->fd, I_STR, &strioc_ppa);
2476 msg(
M_INFO,
"open_tun: got dynamic interface '%s%d'", dev_tuntap_type, new_ppa );
2481 if (errno != EEXIST)
2483 msg(
M_ERR,
"open_tun: unexpected error trying to find free %s interface", dev_tuntap_type );
2489 msg(
M_ERR,
"open_tun: could not find free %s interface, give up.", dev_tuntap_type );
2494 if ((ppa = ioctl(tt->fd, I_STR, &strioc_ppa)) < 0)
2496 msg(
M_ERR,
"Can't assign PPA for new interface (%s%d)", dev_tuntap_type, ppa );
2500 if ((if_fd = open(dev_node, O_RDWR, 0)) < 0)
2502 msg(
M_ERR,
"Can't open %s (2)", dev_node);
2505 if (ioctl(if_fd, I_PUSH,
"ip") < 0)
2507 msg(
M_ERR,
"Can't push IP module");
2513 if (ioctl(if_fd, IF_UNITSEL, (
char *) &ppa) < 0)
2515 msg(
M_ERR,
"Can't set PPA %d", ppa);
2522 snprintf(tt->
actual_name, 32,
"%s%d", dev_tuntap_type, ppa);
2526 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
2533 if (ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0)
2535 msg(
M_ERR,
"Can't set PPA %d", ppa);
2537 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
2542 if (ioctl(if_fd, I_PUSH,
"arp") < 0)
2544 msg(
M_ERR,
"Can't push ARP module");
2550 if (ioctl(tt->ip_fd, I_POP, NULL) < 0)
2556 if (ioctl(tt->ip_fd, I_PUSH,
"arp") < 0)
2558 msg(
M_ERR,
"Can't push ARP module\n");
2562 if ((arp_fd = open(arp_node, O_RDWR, 0)) < 0)
2564 msg(
M_ERR,
"Can't open %s\n", arp_node);
2567 if (ioctl(arp_fd, I_PUSH,
"arp") < 0)
2569 msg(
M_ERR,
"Can't push ARP module\n");
2573 strioc_if.ic_cmd = SIOCSLIFNAME;
2574 strioc_if.ic_timout = 0;
2575 strioc_if.ic_len =
sizeof(ifr);
2576 strioc_if.ic_dp = (
char *)𝔦
2577 if (ioctl(arp_fd, I_STR, &strioc_if) < 0)
2579 msg(
M_ERR,
"Can't set ifname to arp\n");
2583 if ((ip_muxid = ioctl(tt->ip_fd, link_type, if_fd)) < 0)
2585 msg(
M_ERR,
"Can't link %s device to IP", dev_tuntap_type);
2590 if ((arp_muxid = ioctl(tt->ip_fd, link_type, arp_fd)) < 0)
2592 msg(
M_ERR,
"Can't link %s device to ARP", dev_tuntap_type);
2599 ifr.lifr_ip_muxid = ip_muxid;
2602 ifr.lifr_arp_muxid = arp_muxid;
2605 if (ioctl(tt->ip_fd, SIOCSLIFMUXID, &ifr) < 0)
2609 ioctl(tt->ip_fd, I_PUNLINK, arp_muxid);
2611 ioctl(tt->ip_fd, I_PUNLINK, ip_muxid);
2612 msg(
M_ERR,
"Can't set multiplexor id");
2623 solaris_close_tun(
struct tuntap *tt)
2642 if (ioctl(tt->ip_fd, SIOCGLIFFLAGS, &ifr) < 0)
2647 if (ioctl(tt->ip_fd, SIOCGLIFMUXID, &ifr) < 0)
2654 if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_arp_muxid) < 0)
2660 if (ioctl(tt->ip_fd, I_PUNLINK, ifr.lifr_ip_muxid) < 0)
2684 solaris_close_tun(tt);
2694 const char *actual,
bool unplumb_inet6 )
2723 sbuf.buf = (
char *)buf;
2724 return putmsg(tt->fd, NULL, &sbuf, 0) >= 0 ? sbuf.len : -1;
2734 sbuf.buf = (
char *)buf;
2735 return getmsg(tt->fd, NULL, &sbuf, &
f) >= 0 ? sbuf.len : -1;
2738 #elif defined(TARGET_OPENBSD)
2741 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2744 open_tun_generic(dev, dev_type, dev_node, tt);
2749 struct tuninfo info;
2751 if (ioctl(tt->fd, TUNGIFINFO, &info) < 0)
2756 #ifdef IFF_MULTICAST
2757 info.flags |= IFF_MULTICAST;
2760 if (ioctl(tt->fd, TUNSIFINFO, &info) < 0)
2785 close_tun_generic(tt);
2798 close_tun_generic(tt);
2810 return write_tun_header(tt, buf, len);
2816 return read_tun_header(tt, buf, len);
2819 #elif defined(TARGET_NETBSD)
2836 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
2843 if (strcmp(dev,
"tap") == 0)
2846 if ((tt->fd = open(
"/dev/tap", O_RDWR)) < 0)
2848 msg(
M_FATAL,
"Cannot allocate NetBSD TAP dev dynamically");
2850 if (ioctl( tt->fd, TAPGIFNAME, (
void *)&ifr ) < 0)
2852 msg(
M_FATAL,
"Cannot query NetBSD TAP device name");
2856 msg(
M_INFO,
"TUN/TAP device %s opened", ifr.ifr_name);
2865 open_tun_generic(dev, dev_type, dev_node, tt);
2870 int i = IFF_POINTOPOINT|IFF_MULTICAST;
2871 ioctl(tt->fd, TUNSIFMODE, &i);
2873 ioctl(tt->fd, TUNSLMODE, &i);
2878 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
2899 close_tun_generic(tt);
2912 close_tun_generic(tt);
2922 netbsd_modify_read_write_return(
int len)
2926 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
2947 type = htonl(AF_INET6);
2951 type = htonl(AF_INET);
2954 iv[0].iov_base = (
char *)&type;
2955 iv[0].iov_len =
sizeof(type);
2956 iv[1].iov_base = buf;
2957 iv[1].iov_len = len;
2959 return netbsd_modify_read_write_return(writev(tt->fd, iv, 2));
2963 return write(tt->fd, buf, len);
2975 iv[0].iov_base = (
char *)&type;
2976 iv[0].iov_len =
sizeof(type);
2977 iv[1].iov_base = buf;
2978 iv[1].iov_len = len;
2980 return netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
2984 return read(tt->fd, buf, len);
2988 #elif defined(TARGET_FREEBSD)
2991 freebsd_modify_read_write_return(
int len)
2995 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
3004 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3007 if (tun_dco_enabled(tt))
3009 open_tun_dco_generic(dev, dev_type, tt, ctx);
3013 open_tun_generic(dev, dev_type, dev_node, tt);
3018 int i = IFF_POINTOPOINT | IFF_MULTICAST;
3021 i = IFF_BROADCAST | IFF_MULTICAST;
3024 if (ioctl(tt->fd, TUNSIFMODE, &i) < 0)
3031 if (ioctl(tt->fd, TUNSIFHEAD, &i) < 0)
3053 close_tun_generic(tt);
3067 close_tun_generic(tt);
3071 "FreeBSD 'destroy tun interface' failed (non-critical)");
3086 iph = (
struct ip *) buf;
3090 type = htonl(AF_INET6);
3094 type = htonl(AF_INET);
3097 iv[0].iov_base = (
char *)&type;
3098 iv[0].iov_len =
sizeof(type);
3099 iv[1].iov_base = buf;
3100 iv[1].iov_len = len;
3102 return freebsd_modify_read_write_return(writev(tt->fd, iv, 2));
3106 return write(tt->fd, buf, len);
3118 iv[0].iov_base = (
char *)&type;
3119 iv[0].iov_len =
sizeof(type);
3120 iv[1].iov_base = buf;
3121 iv[1].iov_len = len;
3123 return freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
3127 return read(tt->fd, buf, len);
3131 #elif defined(TARGET_DRAGONFLY)
3134 dragonfly_modify_read_write_return(
int len)
3138 return len >
sizeof(u_int32_t) ? len -
sizeof(u_int32_t) : 0;
3147 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3150 open_tun_generic(dev, dev_type, dev_node, tt);
3157 ioctl(tt->fd, TUNSLMODE, &i);
3159 ioctl(tt->fd, TUNSIFHEAD, &i);
3168 close_tun_generic(tt);
3181 iph = (
struct ip *) buf;
3185 type = htonl(AF_INET6);
3189 type = htonl(AF_INET);
3192 iv[0].iov_base = (
char *)&type;
3193 iv[0].iov_len =
sizeof(type);
3194 iv[1].iov_base = buf;
3195 iv[1].iov_len = len;
3197 return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2));
3201 return write(tt->fd, buf, len);
3213 iv[0].iov_base = (
char *)&type;
3214 iv[0].iov_len =
sizeof(type);
3215 iv[1].iov_base = buf;
3216 iv[1].iov_len = len;
3218 return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2));
3222 return read(tt->fd, buf, len);
3226 #elif defined(TARGET_DARWIN)
3242 #ifdef HAVE_NET_IF_UTUN_H
3250 utun_open_helper(
struct ctl_info ctlInfo,
int utunnum)
3252 struct sockaddr_ctl sc;
3255 fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
3264 if (ioctl(fd, CTLIOCGINFO, &ctlInfo) == -1)
3273 sc.sc_id = ctlInfo.ctl_id;
3274 sc.sc_len =
sizeof(sc);
3275 sc.sc_family = AF_SYSTEM;
3276 sc.ss_sysaddr = AF_SYS_CONTROL;
3278 sc.sc_unit = utunnum+1;
3284 if (connect(fd, (
struct sockaddr *)&sc,
sizeof(sc)) < 0)
3299 open_darwin_utun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt)
3301 struct ctl_info ctlInfo;
3305 socklen_t utunname_len =
sizeof(utunname);
3309 if (dev_node && (strcmp(
"utun", dev_node) != 0 ))
3311 if (sscanf(dev_node,
"utun%d", &utunnum) != 1)
3313 msg(
M_FATAL,
"Cannot parse 'dev-node %s' please use 'dev-node utunX'"
3314 "to use a utun device number X", dev_node);
3321 if (strlcpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME,
sizeof(ctlInfo.ctl_name)) >=
3322 sizeof(ctlInfo.ctl_name))
3324 msg(
M_ERR,
"Opening utun: UTUN_CONTROL_NAME too long");
3330 for (utunnum = 0; utunnum < 255; utunnum++)
3334 ASSERT(snprintf(ifname,
sizeof(ifname),
"utun%d", utunnum) > 0);
3335 if (if_nametoindex(ifname))
3339 fd = utun_open_helper(ctlInfo, utunnum);
3350 fd = utun_open_helper(ctlInfo, utunnum);
3362 if (getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, utunname, &utunname_len))
3369 msg(
M_INFO,
"Opened utun device %s", utunname);
3376 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3379 #ifdef HAVE_NET_IF_UTUN_H
3382 || (dev_node && !strncmp(dev_node,
"utun", 4)))
3389 msg(
M_FATAL,
"Cannot use utun devices with --dev-type %s",
3395 open_darwin_utun(dev, dev_type, dev_node, tt);
3402 msg(
M_INFO,
"Failed to open utun device. Falling back to /dev/tun device");
3403 open_tun_generic(dev, dev_type, NULL, tt);
3421 if (dev_node && strcmp(dev_node,
"tun")==0)
3426 open_tun_generic(dev, dev_type, dev_node, tt);
3440 const char *ifconfig_ipv6_local =
3449 close_tun_generic(tt);
3458 #ifdef HAVE_NET_IF_UTUN_H
3461 return write_tun_header(tt, buf, len);
3465 return write(tt->fd, buf, len);
3471 #ifdef HAVE_NET_IF_UTUN_H
3474 return read_tun_header(tt, buf, len);
3478 return read(tt->fd, buf, len);
3481 #elif defined(TARGET_AIX)
3484 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
3488 char dynamic_name[20];
3499 msg(
M_FATAL,
"no support for 'tun' devices on AIX" );
3502 if (strncmp( dev,
"tap", 3 ) != 0 || dev_node)
3504 msg(
M_FATAL,
"'--dev %s' and/or '--dev-node' not supported on AIX, use '--dev tap0', 'tap1', etc.", dev );
3507 if (strcmp( dev,
"tap" ) == 0)
3510 for (i = 0; i<99; i++)
3512 snprintf(tunname,
sizeof(tunname),
"/dev/tap%d", i);
3513 if (access( tunname, F_OK ) < 0 && errno == ENOENT)
3520 msg(
M_FATAL,
"cannot find unused tap device" );
3523 snprintf( dynamic_name,
sizeof(dynamic_name),
"tap%d", i );
3530 while (isdigit(*p) )
3536 msg(
M_FATAL,
"TAP device name must be '--dev tapNNNN'" );
3539 snprintf(tunname,
sizeof(tunname),
"/dev/%s", dev);
3544 if (access( tunname, F_OK ) < 0 && errno == ENOENT)
3564 if ((tt->fd = open(tunname, O_RDWR)) < 0)
3566 msg(
M_ERR,
"Cannot open TAP device '%s'", tunname);
3571 msg(
M_INFO,
"TUN/TAP device %s opened", tunname);
3600 close_tun_generic(tt);
3613 return write(tt->fd, buf, len);
3619 return read(tt->fd, buf, len);
3622 #elif defined(_WIN32)
3664 err = GetLastError();
3665 if (err == ERROR_IO_PENDING)
3721 dmsg(
D_WIN32_IO,
"WIN32 I/O: TAP Write immediate return [%d,%d]",
3727 err = GetLastError();
3728 if (err == ERROR_IO_PENDING)
3762 err = GetLastError();
3780 HDEVINFO dev_info_set;
3785 dev_info_set = SetupDiGetClassDevsEx(&
GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT, NULL, NULL, NULL);
3786 if (dev_info_set == INVALID_HANDLE_VALUE)
3788 err = GetLastError();
3793 for (DWORD i = 0;; ++i)
3795 SP_DEVINFO_DATA device_info_data;
3798 char net_cfg_instance_id_string[] =
"NetCfgInstanceId";
3800 char device_instance_id[256];
3804 ULONG dev_interface_list_size;
3807 ZeroMemory(&device_info_data,
sizeof(SP_DEVINFO_DATA));
3808 device_info_data.cbSize =
sizeof(SP_DEVINFO_DATA);
3809 res = SetupDiEnumDeviceInfo(dev_info_set, i, &device_info_data);
3812 if (GetLastError() == ERROR_NO_MORE_ITEMS)
3822 dev_key = SetupDiOpenDevRegKey(dev_info_set, &device_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
3823 if (dev_key == INVALID_HANDLE_VALUE)
3830 status = RegQueryValueEx(dev_key,
3831 net_cfg_instance_id_string,
3836 if (
status != ERROR_SUCCESS)
3841 len =
sizeof(device_instance_id);
3842 res = SetupDiGetDeviceInstanceId(dev_info_set, &device_info_data, device_instance_id, len, &len);
3848 cr = CM_Get_Device_Interface_List_Size(&dev_interface_list_size,
3851 CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
3853 if (cr != CR_SUCCESS)
3858 char *dev_interface_list =
gc_malloc(dev_interface_list_size,
false, gc);
3861 dev_interface_list_size,
3862 CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
3863 if (cr != CR_SUCCESS)
3868 char *dev_if = dev_interface_list;
3871 while (strlen(dev_if) > 0)
3889 last->
next = dev_iif;
3893 dev_if += strlen(dev_if) + 1;
3897 RegCloseKey(dev_key);
3900 SetupDiDestroyDeviceInfoList(dev_info_set);
3922 if (
status != ERROR_SUCCESS)
3924 msg(
M_FATAL,
"Error opening registry key: %s", ADAPTER_KEY);
3930 char enum_name[256];
3931 char unit_string[256];
3933 char component_id_string[] =
"ComponentId";
3934 char component_id[256];
3935 char net_cfg_instance_id_string[] =
"NetCfgInstanceId";
3936 BYTE net_cfg_instance_id[256];
3939 len =
sizeof(enum_name);
3949 if (
status == ERROR_NO_MORE_ITEMS)
3953 else if (
status != ERROR_SUCCESS)
3955 msg(
M_FATAL,
"Error enumerating registry subkeys of key: %s",
3959 snprintf(unit_string,
sizeof(unit_string),
"%s\\%s",
3960 ADAPTER_KEY, enum_name);
3969 if (
status != ERROR_SUCCESS)
3975 len =
sizeof(component_id);
3976 status = RegQueryValueEx(
3978 component_id_string,
3981 (LPBYTE)component_id,
3984 if (
status != ERROR_SUCCESS || data_type != REG_SZ)
3987 unit_string, component_id_string);
3991 len =
sizeof(net_cfg_instance_id);
3992 status = RegQueryValueEx(
3994 net_cfg_instance_id_string,
3997 net_cfg_instance_id,
4000 if (
status == ERROR_SUCCESS && data_type == REG_SZ)
4013 else if (strcasecmp(component_id,
"ovpn-dco") == 0)
4041 RegCloseKey(unit_key);
4046 RegCloseKey(adapter_key);
4054 HKEY network_connections_key;
4062 NETWORK_CONNECTIONS_KEY,
4065 &network_connections_key);
4067 if (
status != ERROR_SUCCESS)
4069 msg(
M_FATAL,
"Error opening registry key: %s", NETWORK_CONNECTIONS_KEY);
4074 char enum_name[256];
4075 char connection_string[256];
4076 HKEY connection_key;
4077 WCHAR name_data[256];
4079 const WCHAR name_string[] = L
"Name";
4081 len =
sizeof(enum_name);
4083 network_connections_key,
4091 if (
status == ERROR_NO_MORE_ITEMS)
4095 else if (
status != ERROR_SUCCESS)
4097 msg(
M_FATAL,
"Error enumerating registry subkeys of key: %s",
4098 NETWORK_CONNECTIONS_KEY);
4101 snprintf(connection_string,
sizeof(connection_string),
4102 "%s\\%s\\Connection",
4103 NETWORK_CONNECTIONS_KEY, enum_name);
4112 if (
status != ERROR_SUCCESS)
4114 dmsg(
D_REGISTRY,
"Error opening registry key: %s", connection_string);
4118 len =
sizeof(name_data);
4119 status = RegQueryValueExW(
4127 if (
status != ERROR_SUCCESS || name_type != REG_SZ)
4130 NETWORK_CONNECTIONS_KEY, connection_string, name_string);
4139 n = WideCharToMultiByte(CP_UTF8, 0, name_data, -1, NULL, 0, NULL, NULL);
4141 WideCharToMultiByte(CP_UTF8, 0, name_data, -1,
name, n, NULL, NULL);
4156 RegCloseKey(connection_key);
4161 RegCloseKey(network_connections_key);
4173 const unsigned int mask = 3;
4174 const char *err = NULL;
4176 if (local == remote)
4178 err =
"must be different";
4181 if ((local & (~mask)) != (remote & (~mask)))
4183 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";
4186 if ((local & mask) == 0
4187 || (local & mask) == 3
4188 || (remote & mask) == 0
4189 || (remote & mask) == 3)
4191 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";
4199 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.",
4212 printf(
"On Windows, point-to-point IP support (i.e. --dev tun)\n");
4213 printf(
"is emulated by the TAP-Windows driver. The major limitation\n");
4214 printf(
"imposed by this approach is that the --ifconfig local and\n");
4215 printf(
"remote endpoints must be part of the same 255.255.255.252\n");
4216 printf(
"subnet. The following list shows examples of endpoint\n");
4217 printf(
"pairs which satisfy this requirement. Only the final\n");
4218 printf(
"component of the IP address pairs is at issue.\n\n");
4219 printf(
"As an example, the following option would be correct:\n");
4220 printf(
" --ifconfig 10.7.0.5 10.7.0.6 (on host A)\n");
4221 printf(
" --ifconfig 10.7.0.6 10.7.0.5 (on host B)\n");
4222 printf(
"because [5,6] is part of the below list.\n\n");
4224 for (i = 0; i < 256; i += 4)
4226 printf(
"[%3d,%3d] ", i+1, i+2);
4244 bool warn_panel_null =
false;
4245 bool warn_panel_dup =
false;
4246 bool warn_tap_dup =
false;
4257 msg(msglev,
"Available adapters [name, GUID, driver]:");
4276 warn_panel_dup =
true;
4278 else if (links == 0)
4282 warn_panel_null =
true;
4283 msg(msglev,
"[NULL] %s", tr->
guid);
4292 if (tr != tr1 && !strcmp(tr->
guid, tr1->
guid))
4294 warn_tap_dup =
true;
4302 msg(warnlev,
"WARNING: Some TAP-Windows adapters have duplicate GUIDs");
4307 msg(warnlev,
"WARNING: Some TAP-Windows adapters have duplicate links from the Network Connections control panel");
4310 if (warn_panel_null)
4312 msg(warnlev,
"WARNING: Some TAP-Windows adapters have no link from the Network Connections control panel");
4374 msg(
M_FATAL,
"There are no TAP-Windows, Wintun or ovpn-dco adapters "
4375 "on this system. You should be able to create an adapter "
4376 "by using tapctl.exe utility.");
4386 uint8_t *actual_name,
4387 int actual_name_size,
4388 const struct tap_reg *tap_reg_src,
4397 ASSERT(device_number >= 0);
4408 ASSERT(actual_name_size > 0);
4413 for (i = 0; i < device_number; i++)
4439 if (windows_driver != NULL)
4452 uint8_t *actual_name,
4453 int actual_name_size,
4472 ASSERT(actual_name_size > 0);
4518 const IP_ADAPTER_INFO *
4522 IP_ADAPTER_INFO *pi = NULL;
4525 if ((
status = GetAdaptersInfo(NULL, &size)) != ERROR_BUFFER_OVERFLOW)
4527 msg(
M_INFO,
"GetAdaptersInfo #1 failed (status=%u) : %s",
4533 pi = (PIP_ADAPTER_INFO)
gc_malloc(size,
false, gc);
4534 if ((
status = GetAdaptersInfo(pi, &size)) != NO_ERROR)
4536 msg(
M_INFO,
"GetAdaptersInfo #2 failed (status=%u) : %s",
4545 const IP_PER_ADAPTER_INFO *
4549 IP_PER_ADAPTER_INFO *pi = NULL;
4554 if ((
status = GetPerAdapterInfo(index, NULL, &size)) != ERROR_BUFFER_OVERFLOW)
4556 msg(
M_INFO,
"GetPerAdapterInfo #1 failed (status=%u) : %s",
4562 pi = (PIP_PER_ADAPTER_INFO)
gc_malloc(size,
false, gc);
4563 if ((
status = GetPerAdapterInfo((ULONG)index, pi, &size)) == ERROR_SUCCESS)
4569 msg(
M_INFO,
"GetPerAdapterInfo #2 failed (status=%u) : %s",
4578 static const IP_INTERFACE_INFO *
4582 IP_INTERFACE_INFO *ii = NULL;
4585 if ((
status = GetInterfaceInfo(NULL, &size)) != ERROR_INSUFFICIENT_BUFFER)
4587 msg(
M_INFO,
"GetInterfaceInfo #1 failed (status=%u) : %s",
4593 ii = (PIP_INTERFACE_INFO)
gc_malloc(size,
false, gc);
4594 if ((
status = GetInterfaceInfo(ii, &size)) == NO_ERROR)
4600 msg(
M_INFO,
"GetInterfaceInfo #2 failed (status=%u) : %s",
4608 static const IP_ADAPTER_INDEX_MAP *
4615 for (i = 0; i < list->NumAdapters; ++i)
4617 const IP_ADAPTER_INDEX_MAP *inter = &list->Adapter[i];
4618 if (index == inter->Index)
4632 const IP_ADAPTER_INFO *
4637 const IP_ADAPTER_INFO *a;
4640 for (a = ai; a != NULL; a = a->Next)
4642 if (a->Index == index)
4651 const IP_ADAPTER_INFO *
4663 const IP_ADDR_STRING *ip = &ai->IpAddressList;
4687 const IP_ADDR_STRING *iplist = &ai->IpAddressList;
4697 iplist = iplist->Next;
4703 const char *ip_str = iplist->IpAddress.String;
4704 const char *netmask_str = iplist->IpMask.String;
4705 bool succeed1 =
false;
4706 bool succeed2 =
false;
4708 if (ip_str && netmask_str && strlen(ip_str) && strlen(netmask_str))
4710 *ip =
getaddr(getaddr_flags, ip_str, 0, &succeed1, NULL);
4711 *netmask =
getaddr(getaddr_flags, netmask_str, 0, &succeed2, NULL);
4712 ret = (succeed1 ==
true && succeed2 ==
true);
4725 in_addr_t ip_adapter = 0;
4726 in_addr_t netmask_adapter = 0;
4728 return (
status && ip_adapter == ip && netmask_adapter == netmask);
4736 const IP_ADAPTER_INFO *
4762 for (i = 0; i < n; ++i)
4764 in_addr_t ip, netmask;
4800 if (highest_netmask)
4802 *highest_netmask = 0;
4808 for (i = 0; i < n; ++i)
4810 in_addr_t adapter_ip, adapter_netmask;
4813 if (adapter_ip && adapter_netmask && (ip & adapter_netmask) == (adapter_ip & adapter_netmask))
4815 if (highest_netmask && adapter_netmask > *highest_netmask)
4817 *highest_netmask = adapter_netmask;
4835 in_addr_t highest_netmask = 0;
4836 int lowest_metric = INT_MAX;
4851 if (first || hn > highest_netmask)
4853 highest_netmask = hn;
4856 lowest_metric = metric;
4865 else if (hn == highest_netmask)
4871 if (metric >= 0 && metric < lowest_metric)
4874 lowest_metric = metric;
4881 dmsg(
D_ROUTE_DEBUG,
"DEBUG: IP Locate: ip=%s nm=%s index=%d count=%d metric=%d",
4885 count ? *count : -1,
4895 *netmask = highest_netmask;
4907 #define DHCP_STATUS_UNDEF 0
4908 #define DHCP_STATUS_ENABLED 1
4909 #define DHCP_STATUS_DISABLED 2
4922 if (ai->DhcpEnabled)
4948 const IP_ADDR_STRING *ip = &a->IpAddressList;
4952 const DWORD
context = ip->Context;
4954 if ((
status = DeleteIPAddress((ULONG)
context)) == NO_ERROR)
4956 msg(
M_INFO,
"Successfully deleted previously set dynamic IP/netmask: %s/%s",
4957 ip->IpAddress.String,
4962 const char *empty =
"0.0.0.0";
4963 if (strcmp(ip->IpAddress.String, empty)
4964 || strcmp(ip->IpMask.String, empty))
4966 msg(
M_INFO,
"NOTE: could not delete previously set dynamic IP/netmask: %s/%s (status=%u)",
4967 ip->IpAddress.String,
4987 swprintf(wbuf,
SIZE(wbuf), L
"\\DEVICE\\TCPIP_%hs", guid);
4988 if (GetAdapterIndex(wbuf, &aindex) != NO_ERROR)
4994 index = (DWORD)aindex;
5009 if (!strcmp(guid,
list->AdapterName))
5011 index =
list->Index;
5032 msg(
M_INFO,
"NOTE: could not get adapter index for %s", guid);
5046 buf_printf(&out,
"%s", ip->IpAddress.String);
5047 if (strlen(ip->IpMask.String))
5064 msg(msglev,
"%s", a->Description);
5065 msg(msglev,
" Index = %d", (
int)a->Index);
5066 msg(msglev,
" GUID = %s", a->AdapterName);
5068 msg(msglev,
" MAC = %s",
format_hex_ex(a->Address, a->AddressLength, 0, 1,
":", gc));
5073 msg(msglev,
" DHCP LEASE OBTAINED = %s",
time_string(a->LeaseObtained, 0,
false, gc));
5074 msg(msglev,
" DHCP LEASE EXPIRES = %s",
time_string(a->LeaseExpires, 0,
false, gc));
5100 msg(msglev,
"SYSTEM ADAPTER LIST");
5103 const IP_ADAPTER_INFO *a;
5106 for (a = ai; a != NULL; a = a->Next)
5129 msg(
M_ERR,
"Error: init SA failed");
5132 status = SetKernelObjectSecurity(hand, DACL_SECURITY_INFORMATION, &
sa.sd);
5135 msg(
M_ERRNO,
"Error: SetKernelObjectSecurity failed on %s", device_path);
5149 const char *device_guid = NULL;
5151 uint8_t actual_buffer[256];
5152 char device_path[256];
5163 msg(
M_FATAL,
"TAP-Windows adapter '%s' not found", dev_node);
5167 snprintf(device_path,
sizeof(device_path),
"%s%s%s",
5178 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
5182 if (hand == INVALID_HANDLE_VALUE)
5184 msg(
M_ERR,
"CreateFile failed on TAP device: %s", device_path);
5192 int device_number = 0;
5199 sizeof(actual_buffer),
5211 snprintf(device_path,
sizeof(device_path),
"%s%s%s",
5222 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
5226 if (hand == INVALID_HANDLE_VALUE)
5228 msg(
M_WARN,
"CreateFile failed on TAP device: %s", device_path);
5254 DWORD
status = IpReleaseAddress((IP_ADAPTER_INDEX_MAP *)inter);
5262 msg(
M_WARN,
"NOTE: Release of DHCP-assigned IP address lease on TAP-Windows adapter failed: %s (code=%u)",
5294 DWORD
status = IpRenewAddress((IP_ADAPTER_INDEX_MAP *)inter);
5302 msg(
M_WARN,
"WARNING: Failed to renew DHCP IP address lease on TAP-Windows adapter: %s (code=%u)",
5328 for (i = 0; i < n; ++i)
5342 msg(msglevel,
"%s: command failed", prefix);
5355 const char err[] =
"ERROR: Windows ipconfig command failed";
5384 const char *ip_str = src->IpAddress.String;
5386 bool succeed =
false;
5392 if (!ip_str || !strlen(ip_str))
5397 ip =
getaddr(getaddr_flags, ip_str, 0, &succeed, NULL);
5411 msg(
M_INFO,
"ip_addr_string_to_array [%d]", *dest_len);
5412 for (i = 0; i < *dest_len; ++i)
5425 int a2len =
SIZE(a2);
5435 for (i = 0; i < a1len; ++i)
5453 for (i = 0; i < len; ++i)
5471 DWORD adapter_index)
5483 for (
int i = 0; i < addr_len; ++i)
5485 const char *fmt = (i == 0) ?
5486 "%s%s interface ipv6 set dns %lu static %s"
5487 :
"%s%s interface ipv6 add dns %lu %s";
5508 const in_addr_t *addr_list,
5510 const IP_ADDR_STRING *current,
5511 DWORD adapter_index,
5512 const bool test_first)
5516 bool delete_first =
false;
5517 bool is_dns = !strcmp(type,
"dns");
5524 delete_first =
true;
5529 delete_first =
true;
5547 for (i = 0; i < addr_len; ++i)
5551 const char *fmt = count ?
5552 "%s%s interface ip add %s %lu %s"
5553 :
"%s%s interface ip set %s %lu static %s";
5575 msg(
M_INFO,
"NETSH: %lu %s %s [already set]",
5595 dest[0].Next = NULL;
5600 dest[0].Next = &dest[1];
5601 dest[1].Next = NULL;
5607 DWORD adapter_index,
5609 const in_addr_t netmask,
5610 const unsigned int flags)
5614 const IP_ADAPTER_INFO *ai = NULL;
5615 const IP_PER_ADAPTER_INFO *pai = NULL;
5628 msg(
M_INFO,
"NETSH: %lu %s/%s [already set]",
5650 IP_ADDR_STRING wins[2];
5657 pai ? &pai->DnsServerList : NULL,
5660 if (ai && ai->HaveWins)
5684 "%s%s interface ip set address %lu dhcp",
5719 msg(
M_NONFATAL,
"TUN: enabling dhcp using service failed: %s [status=%u if_index=%d]",
5724 msg(
M_INFO,
"DHCP enabled on interface %d using service",
dhcp.iface.index);
5739 MIB_IPINTERFACE_ROW ipiface;
5740 InitializeIpInterfaceEntry(&ipiface);
5741 const char *family_name = (family == AF_INET6) ?
"IPv6" :
"IPv4";
5742 ipiface.Family = family;
5743 ipiface.InterfaceIndex = iface_index;
5744 if (family == AF_INET6 && mtu < 1280)
5746 msg(
M_INFO,
"NOTE: IPv6 interface MTU < 1280 conflicts with IETF standards and might not work");
5749 err = GetIpInterfaceEntry(&ipiface);
5750 if (err == NO_ERROR)
5752 if (family == AF_INET)
5754 ipiface.SitePrefixLength = 0;
5756 ipiface.NlMtu = mtu;
5757 err = SetIpInterfaceEntry(&ipiface);
5760 if (err != NO_ERROR)
5762 msg(
M_WARN,
"TUN: Setting %s mtu failed: %s [status=%lu if_index=%d]",
5767 msg(
M_INFO,
"%s MTU set to %d on interface %d using SetIpInterfaceEntry()", family_name, mtu, iface_index);
5803 else if (strcmp(
BSTR(&actual),
"NULL"))
5805 return BSTR(&actual);
5831 msg(
M_INFO,
"NOTE: now trying netsh (this may take some time)");
5857 msg(
M_WARN,
"write_dhcp_u8: buffer overflow building DHCP options");
5871 const int size =
len *
sizeof(uint32_t);
5876 msg(
M_WARN,
"write_dhcp_u32_array: buffer overflow building DHCP options");
5879 if (size < 1 || size > 255)
5882 msg(
M_WARN,
"write_dhcp_u32_array: size (%d) must be > 0 and <= 255", size);
5887 for (i = 0; i <
len; ++i)
5897 const int len = strlen(str);
5901 msg(
M_WARN,
"write_dhcp_str: buffer overflow building DHCP options");
5904 if (len < 1 || len > 255)
5907 msg(
M_WARN,
"write_dhcp_str: string '%s' must be > 0 bytes and <= 255 bytes", str);
5925 int array_len,
bool *error)
5930 int label_length_pos;
5932 for (i = 0; i < array_len; i++)
5934 const char *ptr = str_array[i];
5936 if (strlen(ptr) +
len + 1 >
sizeof(tmp_buf))
5939 msg(
M_WARN,
"write_dhcp_search_str: temp buffer overflow building DHCP options");
5947 label_length_pos =
len++;
5951 if (*ptr ==
'.' || *ptr ==
'\0')
5953 tmp_buf[label_length_pos] = (
len-label_length_pos)-1;
5954 label_length_pos =
len;
5960 tmp_buf[
len++] = *ptr++;
5969 msg(
M_WARN,
"write_search_dhcp_str: buffer overflow building DHCP options");
5975 msg(
M_WARN,
"write_dhcp_search_str: search domain string must be <= 255 bytes");
6022 msg(
M_WARN,
"build_dhcp_options_string: buffer overflow building DHCP options");
6042 const int pre_sleep = 1;
6044 buf_printf(&cmd,
"openvpn --verb %d --tap-sleep %d", verb, pre_sleep);
6077 msg(
M_WARN,
"Register_dns failed using service: %s [status=0x%x]",
6083 msg(
M_INFO,
"Register_dns request sent to the service");
6116 msg(
M_NONFATAL,
"Register ring buffers failed using service: %s [status=0x%x]",
6122 msg(
M_INFO,
"Ring buffers registered via service");
6142 buf_printf(&cmd,
"openvpn --verb %d --register-dns --rdns-internal", verb);
6156 dsa = (local | (~netmask)) + offset;
6160 dsa = (local & netmask) + offset;
6165 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));
6168 if ((local & netmask) != (dsa & netmask))
6170 msg(
M_FATAL,
"ERROR: --ip-win32 dynamic [offset] : offset is outside of --ifconfig subnet");
6183 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_VERSION,
6184 &info,
sizeof(info),
6185 &info,
sizeof(info), &len, NULL))
6190 (info[2] ?
"(DEBUG)" :
""));
6195 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.",
6204 && info[0] == 9 && info[1] < 8)
6206 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]);
6212 && info[0] == 9 && info[1] == 8)
6214 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]);
6223 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_MTU,
6225 &mtu,
sizeof(mtu), &len, NULL))
6233 const char *device_guid,
6234 bool dhcp_masq_post)
6255 .iface = {.index = index, .name =
"" }
6266 status = FlushIpNetTable(index);
6271 msg(
M_INFO,
"Successful ARP Flush on interface [%lu] %s",
6277 msg(
D_TUNTAP_INFO,
"NOTE: FlushIpNetTable failed on interface [%lu] %s (status=%lu) : %s",
6294 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'");
6316 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')";
6321 msg(
M_FATAL,
"ERROR: unable to get adapter index for interface %s -- %s",
6329 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'");
6343 msg(
M_INFO,
"Succeeded in adding a temporary IP/netmask of %s/%s to interface %s using the Win32 IP Helper API",
6351 msg(
M_FATAL,
"ERROR: AddIPAddress %s/%s failed on interface %s, index=%lu, status=%lu (windows error: '%s') -- %s",
6372 FILE_MAP_ALL_ACCESS,
6378 FILE_MAP_ALL_ACCESS,
6395 switch (GetLastError())
6397 case ERROR_ACCESS_DENIED:
6398 msg(
M_FATAL,
"ERROR: Wintun requires SYSTEM privileges and therefore "
6399 "should be used with interactive service. If you want to "
6400 "use openvpn from command line, you need to do SYSTEM "
6401 "elevation yourself (for example with psexec).");
6404 case ERROR_ALREADY_INITIALIZED:
6405 msg(
M_NONFATAL,
"Adapter %s is already in use", device_guid);
6423 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
6427 msg(
M_WARN,
"WARNING: The TAP-Windows driver rejected a TAP_WIN_IOCTL_SET_MEDIA_STATUS DeviceIoControl call.");
6433 msg(
M_INFO,
"Sleeping for %d seconds...", s);
6446 msg(
M_FATAL,
"ERROR: --dev tun also requires --ifconfig");
6457 ep[0] = htonl(tt->
local);
6461 status = DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_TUN,
6463 ep,
sizeof(ep), &len, NULL);
6471 status ?
"SUCCEEDED" :
"FAILED");
6476 status ?
"SUCCEEDED" :
"FAILED");
6482 ep[0] = htonl(tt->
local);
6485 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT,
6487 ep,
sizeof(ep), &len, NULL))
6489 msg(
M_FATAL,
"ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set Point-to-Point mode, which is required for --dev tun");
6504 ep[0] = htonl(tt->
local);
6530 #ifndef SIMULATE_DHCP_FAILED
6531 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_DHCP_MASQ,
6533 ep,
sizeof(ep), &len, NULL))
6535 msg(
M_FATAL,
"ERROR: The TAP-Windows driver rejected a DeviceIoControl call to set TAP_WIN_IOCTL_CONFIG_DHCP_MASQ mode");
6538 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]",
6553 if (!DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT,
6557 msg(
M_FATAL,
"ERROR: The TAP-Windows driver rejected a TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT DeviceIoControl call");
6562 msg(
M_WARN,
"DHCP option string not set due to error");
6574 const char *path = NULL;
6575 char tuntap_device_path[256];
6610 snprintf(tuntap_device_path,
sizeof(tuntap_device_path),
"%s%s%s",
6614 path = tuntap_device_path;
6619 tt->
hand = CreateFile(path,
6620 GENERIC_READ | GENERIC_WRITE,
6624 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
6626 if (tt->
hand == INVALID_HANDLE_VALUE)
6637 msg(
D_TUNTAP_INFO,
"Failed to register %s adapter ring buffers", device_guid);
6638 CloseHandle(tt->
hand);
6653 uint8_t actual_buffer[256];
6669 msg(
M_FATAL,
"Adapter '%s' not found", dev_node);
6674 msg(
M_FATAL,
"Adapter '%s' is using %s driver, %s expected. If you want to use this device, adjust --windows-driver.",
6685 int device_number = 0;
6693 sizeof(actual_buffer),
6748 *dhcp_masq_post =
true;
6773 bool dhcp_masq =
false;
6774 bool dhcp_masq_post =
false;
6815 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
6821 msg(
M_WARN,
"Some --dhcp-option or --dns options require DHCP server,"
6822 " which is not supported by the selected %s driver. They will be"
6832 const char *device_guid = NULL;
6865 if (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_INFO,
6883 while (DeviceIoControl(tt->
hand, TAP_WIN_IOCTL_GET_LOG_LINE,
6897 const char *ifconfig_ip_local;
6905 "%s%s interface %s delete dns %lu all",
6908 ipv6 ?
"ipv6" :
"ipv4",
6916 "%s%s interface ipv4 delete winsservers %lu all",
6942 "%s%s interface %s delete address %lu %s store=active",
6945 ipv6 ?
"ipv6" :
"ipv4",
6961 if (!CancelIo(tt->
hand))
6963 msg(
M_WARN |
M_ERRNO,
"Warning: CancelIO failed on %s adapter", adaptertype);
6967 dmsg(
D_WIN32_IO_LOW,
"Attempting close of overlapped read event on %s adapter", adaptertype);
6970 dmsg(
D_WIN32_IO_LOW,
"Attempting close of overlapped write event on %s adapter", adaptertype);
6976 if (!CloseHandle(tt->
hand))
6978 msg(
M_WARN |
M_ERRNO,
"Warning: CloseHandle failed on %s adapter", adaptertype);
7062 msg(
M_WARN,
"Warning: DeleteIPAddress[%u] failed on TAP-Windows adapter, status=%u : %s",
7118 return "[unknown --ip-win32 type]";
7147 switch (windows_driver)
7150 return "tap-windows6";
7159 return "unspecified";
7166 open_tun(
const char *dev,
const char *dev_type,
const char *dev_node,
struct tuntap *tt,
7169 open_tun_generic(dev, dev_type, dev_node, tt);
7177 close_tun_generic(tt);
7190 return read(tt->fd, buf,
len);